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_set_fonts_mac.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // MacOS font utilities 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 
21 // #inclde <SFNTTypes.h>
22 
23 
24 // Bug: older versions calculated the value for *ap as a side effect of
25 // making the name, and then forgot about it. To avoid having to change
26 // the header files I decided to store this value in the last character
27 // of the font name array.
28 #define ENDOFBUFFER sizeof(fl_fonts->fontname)-1
29 
30 // turn a stored font name into a pretty name:
31 const char* Fl::get_font_name(Fl_Font fnum, int* ap) {
32  if (!fl_fonts) fl_fonts = Fl_X::calc_fl_fonts();
33  Fl_Fontdesc *f = fl_fonts + fnum;
34  if (!f->fontname[0]) {
35 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
36  if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
37  CFStringRef cfname = CFStringCreateWithCString(NULL, f->name, kCFStringEncodingUTF8);
38  CTFontRef ctfont = CTFontCreateWithName(cfname, 0, NULL);
39  CFRelease(cfname);
40  cfname = CTFontCopyFullName(ctfont);
41  CFRelease(ctfont);
42  CFStringGetCString(cfname, f->fontname, ENDOFBUFFER, kCFStringEncodingUTF8);
43  CFRelease(cfname);
44  }
45  else
46 #endif
47  strlcpy(f->fontname, f->name, ENDOFBUFFER);
48  const char* p = f->name;
49  if (!p || !*p) {if (ap) *ap = 0; return "";}
50  int type = 0;
51  if (strstr(f->name, "Bold")) type |= FL_BOLD;
52  if (strstr(f->name, "Italic") || strstr(f->name, "Oblique")) type |= FL_ITALIC;
53  f->fontname[ENDOFBUFFER] = (char)type;
54  }
55  if (ap) *ap = f->fontname[ENDOFBUFFER];
56  return f->fontname;
57 }
58 
59 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
60 
61 static char *skip(char *p, int& derived)
62 {
63  if (memcmp(p, "-BoldItalic", 11) == 0) { p += 11; derived = 3; }
64  else if (memcmp(p, "-BoldOblique", 12) == 0) { p += 12; derived = 3; }
65  else if (memcmp(p, "-Bold", 5) == 0) {p += 5; derived = 1; }
66  else if (memcmp(p, "-Italic", 7) == 0) {p += 7; derived = 2; }
67  else if (memcmp(p, "-Oblique", 8) == 0) {p += 8; derived = 2; }
68  else if (memcmp(p, "-Regular", 8) == 0) {p += 8; }
69  else if (memcmp(p, "-Roman", 6) == 0) {p += 6; }
70  return p;
71 }
72 
73 static int name_compare(const void *a, const void *b)
74 {
75  /* Compare PostScript font names.
76  First compare font family names ignoring bold, italic and oblique qualifiers.
77  When families are identical, order them according to regular, bold, italic, bolditalic.
78  */
79  char *n1 = *(char**)a;
80  char *n2 = *(char**)b;
81  int derived1 = 0;
82  int derived2 = 0;
83  while (true) {
84  if (*n1 == '-') n1 = skip(n1, derived1);
85  if (*n2 == '-') n2 = skip(n2, derived2);
86  if (*n1 < *n2) return -1;
87  if (*n1 > *n2) return +1;
88  if (*n1 == 0) {
89  return derived1 - derived2;
90  }
91  n1++; n2++;
92  }
93 }
94 #endif
95 
97 
98 // This function fills in the fltk font table with all the fonts that
99 // are found on the X server. It tries to place the fonts into families
100 // and to sort them so the first 4 in a family are normal, bold, italic,
101 // and bold italic.
102 
103 Fl_Font Fl::set_fonts(const char* xstarname) {
104 #pragma unused ( xstarname )
105 if (fl_free_font > FL_FREE_FONT) return (Fl_Font)fl_free_font; // if already called
106 
107 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
108 if(fl_mac_os_version >= Fl_X::CoreText_threshold) {
109  int value[1] = {1};
110  CFDictionaryRef dict = CFDictionaryCreate(NULL,
111  (const void **)kCTFontCollectionRemoveDuplicatesOption,
112  (const void **)&value, 1, NULL, NULL);
113  CTFontCollectionRef fcref = CTFontCollectionCreateFromAvailableFonts(dict);
114  CFRelease(dict);
115  CFArrayRef arrayref = CTFontCollectionCreateMatchingFontDescriptors(fcref);
116  CFRelease(fcref);
117  CFIndex count = CFArrayGetCount(arrayref);
118  CFIndex i;
119  char **tabfontnames = new char*[count];
120  for (i = 0; i < count; i++) {
121  CTFontDescriptorRef fdesc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(arrayref, i);
122  CTFontRef font = CTFontCreateWithFontDescriptor(fdesc, 0., NULL);
123  CFStringRef cfname = CTFontCopyPostScriptName(font);
124  CFRelease(font);
125  static char fname[200];
126  CFStringGetCString(cfname, fname, sizeof(fname), kCFStringEncodingUTF8);
127  tabfontnames[i] = strdup(fname); // never free'ed
128  CFRelease(cfname);
129  }
130  CFRelease(arrayref);
131  qsort(tabfontnames, count, sizeof(char*), name_compare);
132  for (i = 0; i < count; i++) {
133  Fl::set_font((Fl_Font)(fl_free_font++), tabfontnames[i]);
134  }
135  delete[] tabfontnames;
136  return (Fl_Font)fl_free_font;
137 }
138 else {
139 #endif
140 #if (!defined(__LP64__) || !__LP64__) && MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11
141  ItemCount oFontCount, oCountAgain;
142  ATSUFontID *oFontIDs;
143  // How many fonts?
144  ATSUFontCount (&oFontCount);
145  // now allocate space for them...
146  oFontIDs = (ATSUFontID *)malloc((oFontCount+1) * sizeof(ATSUFontID));
147  ATSUGetFontIDs (oFontIDs, oFontCount, &oCountAgain);
148  // Now oFontIDs should contain a list of all the available Unicode fonts
149  // Iterate through the list to get each font name
150  for (ItemCount idx = 0; idx < oFontCount; idx++)
151  {
152 // ByteCount actualLength = 0;
153 // Ptr oName;
154  // How to get the name - Apples docs say call this twice, once to get the length, then again
155  // to get the actual name...
156 // ATSUFindFontName (oFontIDs[idx], kFontFullName, kFontMacintoshPlatform, kFontRomanScript, kFontEnglishLanguage,
157 // 0, NULL, &actualLength, NULL);
158  // Now actualLength tells us the length of buffer we need
159 // oName = (Ptr)malloc(actualLength + 8);
160 // But who's got time for that nonsense? Let's just hard code a fixed buffer (urgh!)
161  ByteCount actualLength = 511;
162  char oName[512];
163  ATSUFindFontName (oFontIDs[idx], kFontFullName, kFontMacintoshPlatform, kFontRomanScript, kFontEnglishLanguage,
164  actualLength, oName, &actualLength, &oCountAgain);
165  // bounds check and terminate the returned name
166  if(actualLength > 511)
167  oName[511] = 0;
168  else
169  oName[actualLength] = 0;
170  Fl::set_font((Fl_Font)(fl_free_font++), strdup(oName));
171 // free(oName);
172  }
173  free(oFontIDs);
174  return (Fl_Font)fl_free_font;
175 #endif //__LP64__
176 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
177  }
178 #endif
179  return 0;
180 }
181 
182 static int array[128];
183 int Fl::get_font_sizes(Fl_Font fnum, int*& sizep) {
184  if (!fl_fonts) fl_fonts = Fl_X::calc_fl_fonts();
185  Fl_Fontdesc *s = fl_fonts+fnum;
186  if (!s->name) s = fl_fonts; // empty slot in table, use entry 0
187  int cnt = 0;
188 
189  // ATS supports all font size
190  array[0] = 0;
191  sizep = array;
192  cnt = 1;
193 
194  return cnt;
195 }
196 
197 //
198 // End of "$Id$".
199 //
array
static int array[128]
Definition: fl_set_fonts_mac.cxx:182
Fl::get_font_sizes
static int get_font_sizes(Fl_Font, int *&sizep)
Definition: fl_set_fonts_mac.cxx:183
FL_ITALIC
const Fl_Font FL_ITALIC
add this to helvetica, courier, or times
Definition: Enumerations.H:898
Fl_Fontdesc
Definition: Fl_Font.H:86
Fl::set_fonts
static Fl_Font set_fonts(const char *=0)
Definition: fl_set_fonts_mac.cxx:103
free
void free()
NULL
#define NULL
Definition: forms.H:34
name_compare
static int name_compare(const void *a, const void *b)
Definition: fl_set_fonts_mac.cxx:73
b
long b
Definition: jpegint.h:397
Fl_Fontdesc::name
const char * name
Definition: Fl_Font.H:87
p
static menustate * p
Definition: Fl_Menu.cxx:606
FL_FREE_FONT
const Fl_Font FL_FREE_FONT
first one to allocate
Definition: Enumerations.H:896
fname
static const char * fname
Definition: file.cxx:128
Fl::get_font_name
static const char * get_font_name(Fl_Font, int *attributes=0)
Definition: fl_set_fonts_mac.cxx:31
strlcpy
#define strlcpy
Definition: flstring.h:84
ENDOFBUFFER
#define ENDOFBUFFER
Definition: fl_set_fonts_mac.cxx:28
FL_BOLD
const Fl_Font FL_BOLD
add this to helvetica, courier, or times
Definition: Enumerations.H:897
Fl::set_font
static void set_font(Fl_Font, const char *)
Definition: fl_set_font.cxx:34
Fl_Font
int Fl_Font
Definition: Enumerations.H:877
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
skip
static char * skip(char *p, int &derived)
Definition: fl_set_fonts_mac.cxx:61
malloc
voidp malloc()
fl_free_font
static int fl_free_font
Definition: fl_set_fonts_mac.cxx:96
fl_fonts
FL_EXPORT Fl_Fontdesc * fl_fonts
Definition: fl_font_mac.cxx:22
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)