17#include <Availability.h>
30#ifdef MAC_OS_X_VERSION_10_11
50 color->GetRGB(
r,
g,
b);
51 a = color->GetAlpha();
63 CFStringRef keys[] = {kCTFontAttributeName};
64 CFTypeRef values[] = {font};
66 Init(textLine, 1, keys, values);
76 CFStringRef keys[] = {kCTFontAttributeName};
77 CFTypeRef values[] = {font};
79 Init(unichars, 1, keys, values);
90 const CFScopeGuard<CGColorSpaceRef> rgbColorSpace(CGColorSpaceCreateDeviceRGB());
91 if (!rgbColorSpace.Get())
92 throw std::runtime_error(
"TextLine: color space");
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]};
98 const CFScopeGuard<CGColorRef> textColor(CGColorCreate(rgbColorSpace.Get(), cgRgba));
101 CFStringRef keys[] = {kCTFontAttributeName, kCTForegroundColorAttributeName};
102 CFTypeRef values[] = {font, textColor.Get()};
104 Init(textLine, 2, keys, values);
114 CFScopeGuard<CGColorSpaceRef> rgbColorSpace(CGColorSpaceCreateDeviceRGB());
116 if (!rgbColorSpace.Get())
117 throw std::runtime_error(
"TexLine: color space is null");
119 CFScopeGuard<CGColorRef> textColor(CGColorCreate(rgbColorSpace.Get(), rgb));
122 CFStringRef keys[] = {kCTFontAttributeName, kCTForegroundColorAttributeName};
123 CFTypeRef values[] = {font, textColor.Get()};
125 Init(textLine, 2, keys, values);
136 const CFScopeGuard<CGColorSpaceRef> rgbColorSpace(CGColorSpaceCreateDeviceRGB());
137 if (!rgbColorSpace.Get())
138 throw std::runtime_error(
"TextLine: color space");
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]};
144 const CFScopeGuard<CGColorRef> textColor(CGColorCreate(rgbColorSpace.Get(), cgRgba));
147 CFStringRef keys[] = {kCTFontAttributeName, kCTForegroundColorAttributeName};
148 CFTypeRef values[] = {font, textColor.Get()};
150 Init(unichars, 2, keys, values);
165 CGFloat ascent = 0., descent = 0., leading = 0.;
166 w =
UInt_t(CTLineGetTypographicBounds(
fCTLine, &ascent, &descent, &leading));
177 const CFScopeGuard<CFDictionaryRef> stringAttribs(CFDictionaryCreate(kCFAllocatorDefault, (
const void **)keys, (
const void **)values,
178 nAttribs, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
179 if (!stringAttribs.Get())
180 throw std::runtime_error(
"TextLine: null attribs");
182 const CFScopeGuard<CFStringRef> wrappedCString(CFStringCreateWithCString(kCFAllocatorDefault, textLine, kCFStringEncodingMacRoman));
183 if (!wrappedCString.Get())
184 throw std::runtime_error(
"TextLine: cstr wrapper");
186 CFScopeGuard<CFAttributedStringRef> attributedString(CFAttributedStringCreate(kCFAllocatorDefault,
187 wrappedCString.Get(), stringAttribs.Get()));
188 fCTLine = CTLineCreateWithAttributedString(attributedString.Get());
191 throw std::runtime_error(
"TextLine: attrib string");
195void TextLine::Init(
const std::vector<UniChar> &unichars,
UInt_t nAttribs, CFStringRef *keys, CFTypeRef *values)
199 const CFScopeGuard<CFStringRef> wrappedUniString(CFStringCreateWithCharacters(kCFAllocatorDefault, &unichars[0], unichars.size()));
200 const CFScopeGuard<CFDictionaryRef> stringAttribs(CFDictionaryCreate(kCFAllocatorDefault, (
const void **)keys, (
const void **)values,
201 nAttribs, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
203 if (!stringAttribs.Get())
204 throw std::runtime_error(
"TextLine: null attribs");
206 if (!wrappedUniString.Get())
207 throw std::runtime_error(
"TextLine: cstr wrapper");
209 const CFScopeGuard<CFAttributedStringRef> attributedString(CFAttributedStringCreate(kCFAllocatorDefault,
210 wrappedUniString.Get(), stringAttribs.Get()));
211 fCTLine = CTLineCreateWithAttributedString(attributedString.Get());
214 throw std::runtime_error(
"TextLine: attrib string");
220 assert(ctx != 0 &&
"DrawLine, ctx parameter is null");
228 assert(ctx != 0 &&
"DrawLine, ctx parameter is null");
230 CGContextSetAllowsAntialiasing(ctx, 1);
260 CGContextSetTextPosition(ctx, 0., 0.);
261 CGContextTranslateCTM(ctx,
x,
y);
263 CGContextTranslateCTM(ctx, xc, yc);
264 CGContextTranslateCTM(ctx, -0.5 *
w, -0.5 *
h);
272 typedef std::vector<CGSize>::size_type size_type;
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");
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");
287 std::vector<CGSize> glyphAdvances(glyphs.size());
290 CGFloat currentX =
x;
291 std::vector<CGPoint> glyphPositions(glyphs.size());
292 glyphPositions[0].x = currentX;
293 glyphPositions[0].y =
y;
295 for (size_type i = 1; i < glyphs.size(); ++i) {
296 currentX += std::ceil(glyphAdvances[i - 1].
width);
297 glyphPositions[i].x = currentX;
298 glyphPositions[i].y =
y;
301 CTFontDrawGlyphs(font, &glyphs[0], &glyphPositions[0], glyphs.size(), ctx);
int Int_t
Signed integer 4 bytes (int)
short Color_t
Color number (short)
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
float Float_t
Float 4 bytes (float)
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
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 TPoint TPoint const char text
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)
void DrawLine(CGContextRef ctx) const
virtual Short_t GetTextAlign() const
Return the text alignment.
virtual Float_t GetTextAngle() const
Return the text angle.
The color creation and management class.
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
const CTFontOrientation defaultFontOrientation
void DrawTextLineNoKerning(CGContextRef ctx, CTFontRef font, const std::vector< UniChar > &text, Int_t x, Int_t y)
const CTFontOrientation horizontalFontOrientation
const CTFontOrientation verticalFontOrientation
constexpr Double_t DegToRad()
Conversion from degree to radian: .