fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_font_mac.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // MacOS font selection routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2015 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 #include <config.h>
20 #include <math.h>
21 
23 
24 /* from fl_utf.c */
25 extern unsigned fl_utf8toUtf16(const char* src, unsigned srclen, unsigned short* dst, unsigned dstlen);
26 
27 static CGAffineTransform font_mx = { 1, 0, 0, -1, 0, 0 };
28 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
29 static CFMutableDictionaryRef attributes = NULL;
30 #endif
31 
32 const int Fl_X::CoreText_threshold = 100500; // this represents Mac OS 10.5
33 // condition when the ATSU API is available at compile time
34 #define HAS_ATSU (!__LP64__) && MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11
35 
37  next = 0;
38 # if HAVE_GL
39  listbase = 0;
40 # endif
41 
42 // knowWidths = 0;
43  // OpenGL needs those for its font handling
44  size = Size;
45 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
46 if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
47  CFStringRef str = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
48  fontref = CTFontCreateWithName(str, size, NULL);
49  CGGlyph glyph[2];
50  const UniChar A[2]={'W','.'};
51  CTFontGetGlyphsForCharacters(fontref, A, glyph, 2);
52  CGSize advances[2];
53  double w;
54  CTFontGetAdvancesForGlyphs(fontref, kCTFontHorizontalOrientation, glyph, advances, 2);
55  w = advances[0].width;
56  if ( fabs(advances[0].width - advances[1].width) < 1E-2 ) {//this is a fixed-width font
57  // slightly rescale fixed-width fonts so the character width has an integral value
58  CFRelease(fontref);
59  CGFloat fsize = size / ( w/floor(w + 0.5) );
60  fontref = CTFontCreateWithName(str, fsize, NULL);
61  w = CTFontGetAdvancesForGlyphs(fontref, kCTFontHorizontalOrientation, glyph, NULL, 1);
62  }
63  CFRelease(str);
64  ascent = (short)(CTFontGetAscent(fontref) + 0.5);
65  descent = (short)(CTFontGetDescent(fontref) + 0.5);
66  q_width = w + 0.5;
67  for (unsigned i = 0; i < sizeof(width)/sizeof(float*); i++) width[i] = NULL;
68  if (!attributes) {
69  static CFNumberRef zero_ref;
70  float zero = 0.;
71  zero_ref = CFNumberCreate(NULL, kCFNumberFloat32Type, &zero);
72  // deactivate kerning for all fonts, so that string width = sum of character widths
73  // which allows fast fl_width() implementation.
74  attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
75  3,
76  &kCFTypeDictionaryKeyCallBacks,
77  &kCFTypeDictionaryValueCallBacks);
78  CFDictionarySetValue (attributes, kCTKernAttributeName, zero_ref);
79  }
80  if (ascent == 0) { // this may happen with some third party fonts
81  CFDictionarySetValue (attributes, kCTFontAttributeName, fontref);
82  CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, CFSTR("Wj"), attributes);
83  CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
84  CFRelease(mastr);
85  CGFloat fascent, fdescent;
86  CTLineGetTypographicBounds(ctline, &fascent, &fdescent, NULL);
87  CFRelease(ctline);
88  ascent = (short)(fascent + 0.5);
89  descent = (short)(fdescent + 0.5);
90  }
91 }
92 else {
93 #endif
94 #if HAS_ATSU
95  OSStatus err;
96  // fill our structure with a few default values
97  ascent = Size*3/4.;
98  descent = Size-ascent;
99  q_width = Size*2/3.;
100  // now we allocate everything needed to render text in this font later
101  // get us the default layout and style
102  err = ATSUCreateTextLayout(&layout);
103  UniChar mTxt[2] = { 65, 0 };
104  err = ATSUSetTextPointerLocation(layout, mTxt, kATSUFromTextBeginning, 1, 1);
105  err = ATSUCreateStyle(&style);
106  err = ATSUSetRunStyle(layout, style, kATSUFromTextBeginning, kATSUToTextEnd);
107  // now set the actual font, size and attributes. We also set the font matrix to
108  // render our font up-side-down, so when rendered through our inverted CGContext,
109  // text will appear normal again.
110  Fixed fsize = IntToFixed(Size);
111  ATSUFontID fontID;
112  ATSUFindFontFromName(name, strlen(name), kFontFullName, kFontMacintoshPlatform, kFontNoScriptCode, kFontEnglishLanguage, &fontID);
113 
114  // draw the font upside-down... Compensate for fltk/OSX origin differences
115  ATSUAttributeTag sTag[] = { kATSUFontTag, kATSUSizeTag, kATSUFontMatrixTag };
116  ByteCount sBytes[] = { sizeof(ATSUFontID), sizeof(Fixed), sizeof(CGAffineTransform) };
117  ATSUAttributeValuePtr sAttr[] = { &fontID, &fsize, &font_mx };
118  if (fontID != kATSUInvalidFontID) err = ATSUSetAttributes(style, 1, sTag, sBytes, sAttr); // set the font attribute
119  err = ATSUSetAttributes(style, 2, sTag + 1, sBytes + 1, sAttr + 1); // then the size and matrix attributes
120  // next, make sure that Quartz will only render at integer coordinates
121  ATSLineLayoutOptions llo = kATSLineUseDeviceMetrics | kATSLineDisableAllLayoutOperations;
122  ATSUAttributeTag aTag[] = { kATSULineLayoutOptionsTag };
123  ByteCount aBytes[] = { sizeof(ATSLineLayoutOptions) };
124  ATSUAttributeValuePtr aAttr[] = { &llo };
125  err = ATSUSetLineControls (layout, kATSUFromTextBeginning, 1, aTag, aBytes, aAttr);
126  // now we are finally ready to measure some letter to get the bounding box
127  Fixed bBefore, bAfter, bAscent, bDescent;
128  err = ATSUGetUnjustifiedBounds(layout, kATSUFromTextBeginning, 1, &bBefore, &bAfter, &bAscent, &bDescent);
129  // Requesting a certain height font on Mac does not guarantee that ascent+descent
130  // equal the requested height. fl_height will reflect the actual height that we got.
131  // The font "Apple Chancery" is a pretty extreme example of overlapping letters.
132  float fa = -FixedToFloat(bAscent), fd = -FixedToFloat(bDescent);
133  if (fa>0.0f && fd>0.0f) {
134  //float f = Size/(fa+fd);
135  ascent = int(fa); //int(fa*f+0.5f);
136  descent = int(fd); //Size - ascent;
137  }
138  int w = FixedToInt(bAfter);
139  if (w)
140  q_width = FixedToInt(bAfter);
141 
142 # define ENABLE_TRANSIENT_FONTS 1
143 
144 # ifdef ENABLE_TRANSIENT_FONTS
145  // Now, by way of experiment, try enabling Transient Font Matching, this will
146  // cause ATSU to find a suitable font to render any chars the current font can't do...
147  ATSUSetTransientFontMatching (layout, true);
148 # endif
149 #endif//HAS_ATSU
150 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
151  }
152 #endif
153 }
154 
156 /*
157 #if HAVE_GL
158  // ++ todo: remove OpenGL font alocations
159 // Delete list created by gl_draw(). This is not done by this code
160 // as it will link in GL unnecessarily. There should be some kind
161 // of "free" routine pointer, or a subclass?
162 // if (listbase) {
163 // int base = font->min_char_or_byte2;
164 // int size = font->max_char_or_byte2-base+1;
165 // int base = 0; int size = 256;
166 // glDeleteLists(listbase+base,size);
167 // }
168 #endif
169  */
171 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
172  if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
173  CFRelease(fontref);
174  for (unsigned i = 0; i < sizeof(width)/sizeof(float*); i++) {
175  if (width[i]) free(width[i]);
176  }
177  }
178 #endif
179 }
180 
182 
183 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
184 static Fl_Fontdesc built_in_table_PS[] = { // PostScript font names preferred when Mac OS ≥ 10.5
185 {"ArialMT"},
186 {"Arial-BoldMT"},
187 {"Arial-ItalicMT"},
188 {"Arial-BoldItalicMT"},
189 {"Courier"},
190 {"Courier-Bold"},
191 {"Courier-Oblique"},
192 {"Courier-BoldOblique"},
193 {"TimesNewRomanPSMT"},
194 {"TimesNewRomanPS-BoldMT"},
195 {"TimesNewRomanPS-ItalicMT"},
196 {"TimesNewRomanPS-BoldItalicMT"},
197 {"Symbol"},
198 {"Monaco"},
199 {"AndaleMono"}, // there is no bold Monaco font on standard Mac
200 {"ZapfDingbatsITC"}
201 };
202 #endif
203 
204 static Fl_Fontdesc built_in_table_full[] = { // full font names used before 10.5
205  {"Arial"},
206  {"Arial Bold"},
207  {"Arial Italic"},
208  {"Arial Bold Italic"},
209  {"Courier"},
210  {"Courier Bold"},
211  {"Courier New Italic"},
212  {"Courier New Bold Italic"},
213  {"Times New Roman"},
214  {"Times New Roman Bold"},
215  {"Times New Roman Italic"},
216  {"Times New Roman Bold Italic"},
217  {"Symbol"},
218  {"Monaco"},
219  {"Andale Mono"}, // there is no bold Monaco font on standard Mac
220  {"Webdings"}
221 };
222 
223 static UniChar *utfWbuf = 0;
224 static unsigned utfWlen = 0;
225 
226 static UniChar *mac_Utf8_to_Utf16(const char *txt, int len, int *new_len)
227 {
228  unsigned wlen = fl_utf8toUtf16(txt, len, (unsigned short*)utfWbuf, utfWlen);
229  if (wlen >= utfWlen)
230  {
231  utfWlen = wlen + 100;
232  if (utfWbuf) free(utfWbuf);
233  utfWbuf = (UniChar*)malloc((utfWlen)*sizeof(UniChar));
234  wlen = fl_utf8toUtf16(txt, len, (unsigned short*)utfWbuf, utfWlen);
235  }
236  *new_len = wlen;
237  return utfWbuf;
238 } // mac_Utf8_to_Utf16
239 
240 Fl_Fontdesc* Fl_X::calc_fl_fonts(void)
241 {
242  if (!fl_mac_os_version) fl_mac_os_version = calc_mac_os_version();
243 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
244  return (fl_mac_os_version >= Fl_X::CoreText_threshold ? built_in_table_PS : built_in_table_full);
245 #else
246  return built_in_table_full;
247 #endif
248 }
249 
251  if (!fl_fonts) fl_fonts = Fl_X::calc_fl_fonts();
252  Fl_Fontdesc* s = fl_fonts+fnum;
253  if (!s->name) s = fl_fonts; // use 0 if fnum undefined
255  for (f = s->first; f; f = f->next)
256  if (f->size == size) return f;
257  f = new Fl_Font_Descriptor(s->name, size);
258  f->next = s->first;
259  s->first = f;
260  return f;
261 }
262 
264 // Public interface:
265 
266 void Fl_Quartz_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize size) {
267  if (fnum==-1) {
269  return;
270  }
271  Fl_Graphics_Driver::font(fnum, size);
272  this->font_descriptor( find(fnum, size) );
273 }
274 
275 int Fl_Quartz_Graphics_Driver::height() {
276  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
277  Fl_Font_Descriptor *fl_fontsize = font_descriptor();
278  return fl_fontsize->ascent + fl_fontsize->descent;
279 }
280 
281 int Fl_Quartz_Graphics_Driver::descent() {
282  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
283  Fl_Font_Descriptor *fl_fontsize = font_descriptor();
284  return fl_fontsize->descent+1;
285 }
286 
287 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
288 // returns width of a pair of UniChar's in the surrogate range
289 static CGFloat surrogate_width(const UniChar *txt, Fl_Font_Descriptor *fl_fontsize)
290 {
291  CTFontRef font2 = fl_fontsize->fontref;
292  bool must_release = false;
293  CGGlyph glyphs[2];
294  bool b = CTFontGetGlyphsForCharacters(font2, txt, glyphs, 2);
295  CGSize a;
296  if(!b) { // the current font doesn't contain this char
297  CFStringRef str = CFStringCreateWithCharactersNoCopy(NULL, txt, 2, kCFAllocatorNull);
298  // find a font that contains it
299  font2 = CTFontCreateForString(font2, str, CFRangeMake(0,2));
300  must_release = true;
301  CFRelease(str);
302  b = CTFontGetGlyphsForCharacters(font2, txt, glyphs, 2);
303  }
304  if (b) CTFontGetAdvancesForGlyphs(font2, kCTFontHorizontalOrientation, glyphs, &a, 1);
305  else a.width = fl_fontsize->q_width;
306  if(must_release) CFRelease(font2);
307  return a.width;
308 }
309 
310 static CGFloat variation_selector_width(CFStringRef str16, Fl_Font_Descriptor *fl_fontsize)
311 {
312  CGFloat retval;
313  CFDictionarySetValue(attributes, kCTFontAttributeName, fl_fontsize->fontref);
314  CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, str16, attributes);
315  CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
316  CFRelease(mastr);
317  retval = CTLineGetOffsetForStringIndex(ctline, 2, NULL);
318  CFRelease(ctline);
319  return retval;
320 }
321 #endif
322 
323 static double fl_mac_width(const UniChar* txt, int n, Fl_Font_Descriptor *fl_fontsize) {
324 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
325 if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
326  double retval = 0;
327  UniChar uni;
328  int i;
329  for (i = 0; i < n; i++) { // loop over txt
330  uni = txt[i];
331  if (uni >= 0xD800 && uni <= 0xDBFF) { // handles the surrogate range
332  retval += surrogate_width(&txt[i], fl_fontsize);
333  i++; // because a pair of UniChar's represent a single character
334  continue;
335  }
336  if (i+1 < n && txt[i+1] >= 0xFE00 && txt[i+1] <= 0xFE0F) { // handles variation selectors
337  CFStringRef substr = CFStringCreateWithCharacters(NULL, txt + i, 2);
338  retval += variation_selector_width(substr, fl_fontsize);
339  CFRelease(substr);
340  i++;
341  continue;
342  }
343  const int block = 0x10000 / (sizeof(fl_fontsize->width)/sizeof(float*)); // block size
344  // r: index of the character block containing uni
345  unsigned int r = uni >> 7; // change 7 if sizeof(width) is changed
346  if (!fl_fontsize->width[r]) { // this character block has not been hit yet
347  //fprintf(stderr,"r=%d size=%d name=%s\n",r,fl_fontsize->size,fl_fonts[fl_font()].name);
348  // allocate memory to hold width of each character in the block
349  fl_fontsize->width[r] = (float*) malloc(sizeof(float) * block);
350  UniChar ii = r * block;
351  CGSize advance_size;
352  CGGlyph glyph;
353  for (int j = 0; j < block; j++) { // loop over the block
354  // ii spans all characters of this block
355  bool b = CTFontGetGlyphsForCharacters(fl_fontsize->fontref, &ii, &glyph, 1);
356  if (b)
357  CTFontGetAdvancesForGlyphs(fl_fontsize->fontref, kCTFontHorizontalOrientation, &glyph, &advance_size, 1);
358  else
359  advance_size.width = -1e9; // calculate this later
360  // the width of one character of this block of characters
361  fl_fontsize->width[r][j] = advance_size.width;
362  ii++;
363  }
364  }
365  // sum the widths of all characters of txt
366  double wdt = fl_fontsize->width[r][uni & (block-1)];
367  if (wdt == -1e9) {
368  CGSize advance_size;
369  CGGlyph glyph;
370  CTFontRef font2 = fl_fontsize->fontref;
371  bool must_release = false;
372  bool b = CTFontGetGlyphsForCharacters(font2, &uni, &glyph, 1);
373  if (!b) { // the current font doesn't contain this char
374  CFStringRef str = CFStringCreateWithCharactersNoCopy(NULL, &uni, 1, kCFAllocatorNull);
375  // find a font that contains it
376  font2 = CTFontCreateForString(font2, str, CFRangeMake(0,1));
377  must_release = true;
378  CFRelease(str);
379  b = CTFontGetGlyphsForCharacters(font2, &uni, &glyph, 1);
380  }
381  if (b) CTFontGetAdvancesForGlyphs(font2, kCTFontHorizontalOrientation, &glyph, &advance_size, 1);
382  else advance_size.width = 0.;
383  // the width of the 'uni' character
384  wdt = fl_fontsize->width[r][uni & (block-1)] = advance_size.width;
385  if (must_release) CFRelease(font2);
386  }
387  retval += wdt;
388  }
389  return retval;
390 } else {
391 #endif
392 #if HAS_ATSU
393  OSStatus err;
394  Fixed bBefore, bAfter, bAscent, bDescent;
395  ATSUTextLayout layout;
396  ByteCount iSize;
397  ATSUAttributeTag iTag;
398  ATSUAttributeValuePtr iValuePtr;
399 
400  // Here's my ATSU text measuring attempt... This seems to do the Right Thing
401  // now collect our ATSU resources and measure our text string
402  layout = fl_fontsize->layout;
403  // activate the current GC
404  iSize = sizeof(CGContextRef);
405  iTag = kATSUCGContextTag;
406  iValuePtr = &fl_gc;
407  ATSUSetLayoutControls(layout, 1, &iTag, &iSize, &iValuePtr);
408  // now measure the bounding box
409  err = ATSUSetTextPointerLocation(layout, txt, kATSUFromTextBeginning, n, n);
410  err = ATSUGetUnjustifiedBounds(layout, kATSUFromTextBeginning, n, &bBefore, &bAfter, &bAscent, &bDescent);
411  // If err is OK then return length, else return 0. Or something...
412  int len = FixedToInt(bAfter);
413  return len;
414 #endif
415 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
416  }
417 #endif
418  return 0;
419 }
420 
421 double Fl_Quartz_Graphics_Driver::width(const char* txt, int n) {
422  int wc_len = n;
423  UniChar *uniStr = mac_Utf8_to_Utf16(txt, n, &wc_len);
424  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
425  return fl_mac_width(uniStr, wc_len, font_descriptor());
426 }
427 
428 double Fl_Quartz_Graphics_Driver::width(unsigned int wc) {
429  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
430 
431  UniChar utf16[3];
432  int l = 1;
433  if (wc <= 0xFFFF) {
434  *utf16 = wc;
435  }
436  else {
437 // char buf[4];
438 // l = fl_utf8encode(wc, buf);
439 // l = (int)fl_utf8toUtf16(buf, l, utf16, 3);
440  l = (int)fl_ucs_to_Utf16(wc, utf16, 3);
441  }
442  return fl_mac_width(utf16, l, font_descriptor());
443 }
444 
445 // text extent calculation
446 void Fl_Quartz_Graphics_Driver::text_extents(const char *str8, int n, int &dx, int &dy, int &w, int &h) {
447  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
448  Fl_Font_Descriptor *fl_fontsize = font_descriptor();
449  UniChar *txt = mac_Utf8_to_Utf16(str8, n, &n);
450 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
451 if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
452  CFStringRef str16 = CFStringCreateWithCharactersNoCopy(NULL, txt, n, kCFAllocatorNull);
453  CFDictionarySetValue (attributes, kCTFontAttributeName, fl_fontsize->fontref);
454  CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, str16, attributes);
455  CFRelease(str16);
456  CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
457  CFRelease(mastr);
458  CGContextSetTextPosition(fl_gc, 0, 0);
459  CGContextSetShouldAntialias(fl_gc, true);
460  CGRect rect = CTLineGetImageBounds(ctline, fl_gc);
461  CGContextSetShouldAntialias(fl_gc, false);
462  CFRelease(ctline);
463  dx = floor(rect.origin.x + 0.5);
464  dy = floor(- rect.origin.y - rect.size.height + 0.5);
465  w = rect.size.width + 0.5;
466  h = rect.size.height + 0.5;
467  }
468 else {
469 #endif
470 #if HAS_ATSU
471  OSStatus err;
472  ATSUTextLayout layout;
473  ByteCount iSize;
474  ATSUAttributeTag iTag;
475  ATSUAttributeValuePtr iValuePtr;
476 
477 // Here's my ATSU text measuring attempt... This seems to do the Right Thing
478  // now collect our ATSU resources and measure our text string
479  layout = fl_fontsize->layout;
480  // activate the current GC
481  iSize = sizeof(CGContextRef);
482  iTag = kATSUCGContextTag;
483  iValuePtr = &fl_gc;
484  ATSUSetLayoutControls(layout, 1, &iTag, &iSize, &iValuePtr);
485  // now measure the bounding box
486  err = ATSUSetTextPointerLocation(layout, txt, kATSUFromTextBeginning, n, n);
487  Rect bbox;
488  err = ATSUMeasureTextImage(layout, kATSUFromTextBeginning, n, 0, 0, &bbox);
489  w = bbox.right - bbox.left;
490  h = bbox.bottom - bbox.top;
491  dx = bbox.left;
492  dy = -bbox.bottom;
493 //printf("r: %d l: %d t: %d b: %d w: %d h: %d\n", bbox.right, bbox.left, bbox.top, bbox.bottom, w, h);
494 #endif
495 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
496  }
497 #endif
498  return;
499 } // fl_text_extents
500 
501 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
502 static CGColorRef flcolortocgcolor(Fl_Color i)
503 {
504  uchar r, g, b;
505  Fl::get_color(i, r, g, b);
506  CGFloat components[4] = {r/255.0f, g/255.0f, b/255.0f, 1.};
507  static CGColorSpaceRef cspace = NULL;
508  if (cspace == NULL) {
509  cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
510  }
511  return CGColorCreate(cspace, components);
512 }
513 #endif
514 
515 static void fl_mac_draw(const char *str, int n, float x, float y, Fl_Graphics_Driver *driver) {
516  // convert to UTF-16 first
517  UniChar *uniStr = mac_Utf8_to_Utf16(str, n, &n);
518 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
519  if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
520  CFMutableStringRef str16 = CFStringCreateMutableWithExternalCharactersNoCopy(NULL, uniStr, n, n, kCFAllocatorNull);
521  if (str16 == NULL) return; // shd not happen
522  CGColorRef color = flcolortocgcolor(driver->color());
523  CFDictionarySetValue (attributes, kCTFontAttributeName, driver->font_descriptor()->fontref);
524  CFDictionarySetValue (attributes, kCTForegroundColorAttributeName, color);
525  CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, str16, attributes);
526  CFRelease(str16);
527  CFRelease(color);
528  CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
529  CFRelease(mastr);
530  CGContextSetTextMatrix(fl_gc, font_mx);
531  CGContextSetTextPosition(fl_gc, x, y);
532  CGContextSetShouldAntialias(fl_gc, true);
533  CTLineDraw(ctline, fl_gc);
534  CGContextSetShouldAntialias(fl_gc, false);
535  CFRelease(ctline);
536  } else {
537 #endif
538 #if HAS_ATSU
539  OSStatus err;
540  // now collect our ATSU resources
541  ATSUTextLayout layout = driver->font_descriptor()->layout;
542 
543  ByteCount iSize = sizeof(CGContextRef);
544  ATSUAttributeTag iTag = kATSUCGContextTag;
545  ATSUAttributeValuePtr iValuePtr=&fl_gc;
546  ATSUSetLayoutControls(layout, 1, &iTag, &iSize, &iValuePtr);
547 
548  err = ATSUSetTextPointerLocation(layout, uniStr, kATSUFromTextBeginning, n, n);
549  CGContextSetShouldAntialias(fl_gc, true);
550  err = ATSUDrawText(layout, kATSUFromTextBeginning, n, FloatToFixed(x), FloatToFixed(y));
551  CGContextSetShouldAntialias(fl_gc, false);
552 #endif
553 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
554  }
555 #endif
556 }
557 
558 void Fl_Quartz_Graphics_Driver::draw(const char *str, int n, float x, float y) {
559  // avoid a crash if no font has been selected by user yet !
560  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
561  fl_mac_draw(str, n, x, y, this);
562 }
563 
564 void Fl_Quartz_Graphics_Driver::draw(const char* str, int n, int x, int y) {
565  // avoid a crash if no font has been selected by user yet !
566  if (!font_descriptor()) font(FL_HELVETICA, FL_NORMAL_SIZE);
567  fl_mac_draw(str, n, (float)x-0.0f, (float)y+0.5f, this);
568 }
569 
570 void Fl_Quartz_Graphics_Driver::draw(int angle, const char *str, int n, int x, int y) {
572  CGContextTranslateCTM(fl_gc, x, y);
573  CGContextRotateCTM(fl_gc, - angle*(M_PI/180) );
574  draw(str, n, 0, 0);
576 }
577 
578 void Fl_Quartz_Graphics_Driver::rtl_draw(const char* c, int n, int x, int y) {
579  int dx, dy, w, h;
580  text_extents(c, n, dx, dy, w, h);
581  draw(c, n, x - w - dx, y);
582 }
583 
584 //
585 // End of "$Id$".
586 //
fl_fonts
Fl_Fontdesc * fl_fonts
Definition: fl_font_mac.cxx:22
Fl_Color
unsigned int Fl_Color
Definition: Enumerations.H:934
fl_utf8toUtf16
unsigned fl_utf8toUtf16(const char *src, unsigned srclen, unsigned short *dst, unsigned dstlen)
Definition: fl_utf.c:432
M_PI
#define M_PI
Definition: math.h:38
uni
char uni[0x10000]
Definition: euc_tw.c:23
Fl_Fontdesc
Definition: Fl_Font.H:86
find
static Fl_Font_Descriptor * find(Fl_Font fnum, Fl_Fontsize size)
Definition: fl_font_mac.cxx:250
utfWlen
static unsigned utfWlen
Definition: fl_font_mac.cxx:224
free
void free()
flcolortocgcolor
static CGColorRef flcolortocgcolor(Fl_Color i)
Definition: fl_font_mac.cxx:502
Fl_Graphics_Driver::font
Fl_Font font()
see fl_font(void).
Definition: Fl_Device.H:388
built_in_table_PS
static Fl_Fontdesc built_in_table_PS[]
Definition: fl_font_mac.cxx:184
NULL
#define NULL
Definition: forms.H:34
CGContextRef
struct CGContext * CGContextRef
Definition: mac.H:39
b
long b
Definition: jpegint.h:397
CGContextRestoreGState
#define CGContextRestoreGState(a)
Definition: cgdebug.h:195
fl_mac_draw
static void fl_mac_draw(const char *str, int n, float x, float y, Fl_Graphics_Driver *driver)
Definition: fl_font_mac.cxx:515
Fl_Fontdesc::name
const char * name
Definition: Fl_Font.H:87
surrogate_width
static CGFloat surrogate_width(const UniChar *txt, Fl_Font_Descriptor *fl_fontsize)
Definition: fl_font_mac.cxx:289
utfWbuf
static UniChar * utfWbuf
Definition: fl_font_mac.cxx:223
math.h
Fl::get_color
static unsigned get_color(Fl_Color i)
Definition: fl_color.cxx:359
Fl_Font_Descriptor::next
Fl_Font_Descriptor * next
Definition: Fl_Font.H:44
draw
static void draw(int which, int x, int y, int w, int h, int inset)
Definition: fl_gtk.cxx:166
FL_HELVETICA
const Fl_Font FL_HELVETICA
Helvetica (or Arial) normal (0)
Definition: Enumerations.H:879
Fl_Graphics_Driver::color
virtual void color(Fl_Color c)
see fl_color(Fl_Color c).
Definition: Fl_Device.H:260
CGContextSaveGState
#define CGContextSaveGState(a)
Definition: cgdebug.h:185
Fl_Fontsize
int Fl_Fontsize
Definition: Enumerations.H:906
fl_mac_width
static double fl_mac_width(const UniChar *txt, int n, Fl_Font_Descriptor *fl_fontsize)
Definition: fl_font_mac.cxx:323
FL_NORMAL_SIZE
Fl_Fontsize FL_NORMAL_SIZE
normal font size
Definition: Fl_Widget.cxx:117
fl_graphics_driver
FL_EXPORT Fl_Graphics_Driver * fl_graphics_driver
Points to the driver that currently receives all graphics requests.
Definition: Fl_Device.cxx:50
fd
static struct FD * fd
Fl_Fontdesc::first
Fl_Font_Descriptor * first
Definition: Fl_Font.H:89
color
void color(int n)
Definition: gl2opengl.h:29
x
int x
Definition: test.c:73
Fl_Graphics_Driver
A virtual class subclassed for each graphics driver FLTK uses. Typically, FLTK applications do not us...
Definition: Fl_Device.H:110
Fl_Font
int Fl_Font
Definition: Enumerations.H:877
Fl_Font_Descriptor::Fl_Font_Descriptor
FL_EXPORT Fl_Font_Descriptor(const char *xfontname)
Definition: fl_font_mac.cxx:36
font_mx
static CGAffineTransform font_mx
Definition: fl_font_mac.cxx:27
dy
uchar dy
Definition: fl_boxtype.cxx:286
fl_gc
CGContextRef fl_gc
Definition: Fl_win32.cxx:2568
mac_Utf8_to_Utf16
static UniChar * mac_Utf8_to_Utf16(const char *txt, int len, int *new_len)
Definition: fl_font_mac.cxx:226
y
int y
Definition: test.c:74
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
malloc
voidp malloc()
Fl_Font_Descriptor::size
Fl_Fontsize size
Definition: Fl_Font.H:45
variation_selector_width
static CGFloat variation_selector_width(CFStringRef str16, Fl_Font_Descriptor *fl_fontsize)
Definition: fl_font_mac.cxx:310
rect
void rect(int x, int y, int r, int t)
Definition: gl2opengl.h:30
fl_mac_os_version
int fl_mac_os_version
The version number of the running Mac OS X (e.g., 100604 for 10.6.4)
attributes
static CFMutableDictionaryRef attributes
Definition: fl_font_mac.cxx:29
uchar
unsigned char uchar
Definition: fl_types.h:30
name
static const char * name
Definition: Fl_arg.cxx:53
fl_ucs_to_Utf16
unsigned fl_ucs_to_Utf16(const unsigned ucs, unsigned short *dst, const unsigned dstlen)
Definition: fl_utf.c:363
built_in_table_full
static Fl_Fontdesc built_in_table_full[]
Definition: fl_font_mac.cxx:204
dx
uchar dx
Definition: fl_boxtype.cxx:286
Fl_Font_Descriptor::~Fl_Font_Descriptor
FL_EXPORT ~Fl_Font_Descriptor()
Definition: fl_font_mac.cxx:155
Fl_Graphics_Driver::font_descriptor
Fl_Font_Descriptor * font_descriptor()
Definition: Fl_Device.H:404
Fl_Font_Descriptor
Definition: Fl_Font.H:41