Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
QuartzText.mm
Go to the documentation of this file.
1// @(#)root/graf2d:$Id$
2// Author: Timur Pocheptsov 26/01/2012
3
4/*************************************************************************
5 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <stdexcept>
13#include <cassert>
14#include <vector>
15#include <cmath>
16
17#include <Availability.h>
18
19#include "QuartzText.h"
20#include "CocoaUtils.h"
21#include "TAttText.h"
22#include "TColor.h"
23#include "TError.h"
24#include "TROOT.h"
25#include "TMath.h"
26
27namespace ROOT {
28namespace Quartz {
29
30#ifdef MAC_OS_X_VERSION_10_11
31
35
36#else
37// Constants deprecated starting from 10.11
41
42#endif
43
44namespace {
45
46//______________________________________________________________________________
48{
49 if (const TColor * const color = gROOT->GetColor(colorIndex)) {
50 color->GetRGB(r, g, b);
51 a = color->GetAlpha();
52 }
53}
54
55}
56
57//_________________________________________________________________
58TextLine::TextLine(const char *textLine, CTFontRef font)
59 : fCTLine(0),
60 fCTFont(font)
61{
62 //Create attributed string with one attribue: the font.
64 CFTypeRef values[] = {font};
65
66 Init(textLine, 1, keys, values);
67}
68
69//_________________________________________________________________
70TextLine::TextLine(const std::vector<UniChar> &unichars, CTFontRef font)
71 : fCTLine(0),
72 fCTFont(font)
73
74{
75 //Create attributed string with one attribue: the font.
77 CFTypeRef values[] = {font};
78
79 Init(unichars, 1, keys, values);
80}
81
82//_________________________________________________________________
83TextLine::TextLine(const char *textLine, CTFontRef font, Color_t color)
84 : fCTLine(0),
85 fCTFont(font)
86{
87 //Create attributed string with font and color.
89
91 if (!rgbColorSpace.Get())
92 throw std::runtime_error("TextLine: color space");
93
94 Float_t rgba[] = {0.f, 0.f, 0.f, 1.f};
95 GetTextColorForIndex(color, rgba[0], rgba[1], rgba[2], rgba[3]);
96 const CGFloat cgRgba[] = {rgba[0], rgba[1], rgba[2], rgba[3]};
97
99 //Not clear from docs, if textColor.Get() can be 0.
100
102 CFTypeRef values[] = {font, textColor.Get()};
103
104 Init(textLine, 2, keys, values);
105}
106
107//_________________________________________________________________
108TextLine::TextLine(const char *textLine, CTFontRef font, const CGFloat *rgb)
109 : fCTLine(0),
110 fCTFont(font)
111{
112 //Create attributed string with font and color.
115
116 if (!rgbColorSpace.Get())
117 throw std::runtime_error("TexLine: color space is null");
118
120 //Not clear from docs, if textColor can be 0.
121
123 CFTypeRef values[] = {font, textColor.Get()};
124
125 Init(textLine, 2, keys, values);
126}
127
128//_________________________________________________________________
129TextLine::TextLine(const std::vector<UniChar> &unichars, CTFontRef font, Color_t color)
130 : fCTLine(0),
131 fCTFont(font)
132{
133 //Create attributed string with font and color.
135
137 if (!rgbColorSpace.Get())
138 throw std::runtime_error("TextLine: color space");
139
140 Float_t rgba[] = {0.f, 0.f, 0.f, 1.f};
141 GetTextColorForIndex(color, rgba[0], rgba[1], rgba[2], rgba[3]);
142 const CGFloat cgRgba[] = {rgba[0], rgba[1], rgba[2], rgba[3]};
143
145 //Not clear from docs, if textColor.Get() can be 0.
146
148 CFTypeRef values[] = {font, textColor.Get()};
149
150 Init(unichars, 2, keys, values);
151}
152
153
154//_________________________________________________________________
159
160
161//_________________________________________________________________
163{
164 //The old 'fallback' version:
165 CGFloat ascent = 0., descent = 0., leading = 0.;
167 h = UInt_t(ascent);// + descent + leading);
168}
169
170
171//_________________________________________________________________
172void TextLine::Init(const char *textLine, UInt_t nAttribs, CFStringRef *keys, CFTypeRef *values)
173{
175
176 //Strong reference must be replaced with scope guards.
177 const CFScopeGuard<CFDictionaryRef> stringAttribs(CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values,
179 if (!stringAttribs.Get())
180 throw std::runtime_error("TextLine: null attribs");
181
183 if (!wrappedCString.Get())
184 throw std::runtime_error("TextLine: cstr wrapper");
185
187 wrappedCString.Get(), stringAttribs.Get()));
189
190 if (!fCTLine)
191 throw std::runtime_error("TextLine: attrib string");
192}
193
194//_________________________________________________________________
195void TextLine::Init(const std::vector<UniChar> &unichars, UInt_t nAttribs, CFStringRef *keys, CFTypeRef *values)
196{
198
200 const CFScopeGuard<CFDictionaryRef> stringAttribs(CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values,
202
203 if (!stringAttribs.Get())
204 throw std::runtime_error("TextLine: null attribs");
205
206 if (!wrappedUniString.Get())
207 throw std::runtime_error("TextLine: cstr wrapper");
208
210 wrappedUniString.Get(), stringAttribs.Get()));
212
213 if (!fCTLine)
214 throw std::runtime_error("TextLine: attrib string");
215}
216
217//_________________________________________________________________
219{
220 assert(ctx != 0 && "DrawLine, ctx parameter is null");
222}
223
224
225//______________________________________________________________________________
227{
228 assert(ctx != 0 && "DrawLine, ctx parameter is null");
229
231 UInt_t w = 0, h = 0;
232
233 GetBounds(w, h);
234
235 Double_t xc = 0., yc = 0.;
236 Int_t hAlign = att.GetTextAlign() / 10;
237 switch (hAlign) {
238 case 1:
239 xc = 0.5 * w;
240 break;
241 case 2:
242 break;
243 case 3:
244 xc = -0.5 * w;
245 break;
246 }
247
248 Int_t vAlign = att.GetTextAlign() % 10;
249 switch (vAlign) {
250 case 1:
251 yc = 0.5 * h;
252 break;
253 case 2:
254 break;
255 case 3:
256 yc = -0.5 * h;
257 break;
258 }
259
262 CGContextRotateCTM(ctx, att.GetTextAngle() * TMath::DegToRad());
264 CGContextTranslateCTM(ctx, -0.5 * w, -0.5 * h);
265
266 DrawLine(ctx);
267}
268
269//______________________________________________________________________________
270void DrawTextLineNoKerning(CGContextRef ctx, CTFontRef font, const std::vector<UniChar> &text, Int_t x, Int_t y)
271{
272 typedef std::vector<CGSize>::size_type size_type;
273
274 if (!text.size())//This can happen with ROOT's GUI.
275 return;
276
277 assert(ctx != 0 && "DrawTextLineNoKerning, ctx parameter is null");
278 assert(font != 0 && "DrawTextLineNoKerning, font parameter is null");
279 assert(text.size() && "DrawTextLineNoKerning, text parameter is an empty vector");
280
281 std::vector<CGGlyph> glyphs(text.size());
282 if (!CTFontGetGlyphsForCharacters(font, &text[0], &glyphs[0], text.size())) {
283 ::Error("DrawTextLineNoKerning", "Font could not encode all Unicode characters in a text");
284 return;
285 }
286
287 std::vector<CGSize> glyphAdvances(glyphs.size());
289
290 CGFloat currentX = x;
291 std::vector<CGPoint> glyphPositions(glyphs.size());
293 glyphPositions[0].y = y;
294
295 for (size_type i = 1; i < glyphs.size(); ++i) {
296 currentX += std::ceil(glyphAdvances[i - 1].width);
298 glyphPositions[i].y = y;
299 }
300
301 CTFontDrawGlyphs(font, &glyphs[0], &glyphPositions[0], glyphs.size(), ctx);
302}
303
304}//Quartz
305}//ROOT
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
short Color_t
Color number (short)
Definition RtypesCore.h:100
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char DrawLine
Option_t Option_t width
Option_t Option_t TPoint TPoint const char text
#define gROOT
Definition TROOT.h:417
void GetBounds(UInt_t &w, UInt_t &h) const
void Init(const char *textLine, UInt_t nAttribs, CFStringRef *keys, CFTypeRef *values)
TextLine(const char *textLine, CTFontRef font)
Definition QuartzText.mm:58
void DrawLine(CGContextRef ctx) const
Text Attributes class.
Definition TAttText.h:21
The color creation and management class.
Definition TColor.h:22
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1939
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const CTFontOrientation defaultFontOrientation
Definition QuartzText.mm:38
void DrawTextLineNoKerning(CGContextRef ctx, CTFontRef font, const std::vector< UniChar > &text, Int_t x, Int_t y)
const CTFontOrientation horizontalFontOrientation
Definition QuartzText.mm:39
const CTFontOrientation verticalFontOrientation
Definition QuartzText.mm:40
constexpr Double_t DegToRad()
Conversion from degree to radian: .
Definition TMath.h:82