2 // "$Id: fl_set_fonts_mac.cxx 8504 2011-03-04 16:48:10Z manolo $"
4 // MacOS font utilities for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2011 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
30 // #inclde <SFNTTypes.h>
32 // This function fills in the fltk font table with all the fonts that
33 // are found on the X server. It tries to place the fonts into families
34 // and to sort them so the first 4 in a family are normal, bold, italic,
37 // Bug: older versions calculated the value for *ap as a side effect of
38 // making the name, and then forgot about it. To avoid having to change
39 // the header files I decided to store this value in the last character
40 // of the font name array.
41 #define ENDOFBUFFER 127 // sizeof(Fl_Font.fontname)-1
43 // turn a stored font name into a pretty name:
44 const char* Fl::get_font_name(Fl_Font fnum
, int* ap
) {
45 Fl_Fontdesc
*f
= fl_fonts
+ fnum
;
46 if (!f
->fontname
[0]) {
47 const char* p
= f
->name
;
48 if (!p
|| !*p
) {if (ap
) *ap
= 0; return "";}
49 strlcpy(f
->fontname
, p
, ENDOFBUFFER
);
51 if (strstr(f
->name
, "Bold")) type
|= FL_BOLD
;
52 if (strstr(f
->name
, "Italic")) type
|= FL_ITALIC
;
53 f
->fontname
[ENDOFBUFFER
] = (char)type
;
55 if (ap
) *ap
= f
->fontname
[ENDOFBUFFER
];
59 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
60 static int name_compare(const void *a
, const void *b
)
62 return strcmp(*(char**)a
, *(char**)b
);
66 static int fl_free_font
= FL_FREE_FONT
;
68 Fl_Font
Fl::set_fonts(const char* xstarname
) {
69 #pragma unused ( xstarname )
70 if (fl_free_font
> FL_FREE_FONT
) return (Fl_Font
)fl_free_font
; // if already called
72 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
73 if(fl_mac_os_version
>= 100500) {
74 //if(CTFontCreateWithFontDescriptor != NULL) {// CTFontCreateWithFontDescriptor != NULL on 10.4 also!
76 CFDictionaryRef dict
= CFDictionaryCreate(NULL
,
77 (const void **)kCTFontCollectionRemoveDuplicatesOption
,
78 (const void **)&value
, 1, NULL
, NULL
);
79 CTFontCollectionRef fcref
= CTFontCollectionCreateFromAvailableFonts(dict
);
81 CFArrayRef arrayref
= CTFontCollectionCreateMatchingFontDescriptors(fcref
);
83 CFIndex count
= CFArrayGetCount(arrayref
);
85 char **tabfontnames
= new char*[count
];
86 for (i
= 0; i
< count
; i
++) {
87 CTFontDescriptorRef fdesc
= (CTFontDescriptorRef
)CFArrayGetValueAtIndex(arrayref
, i
);
88 CTFontRef font
= CTFontCreateWithFontDescriptor(fdesc
, 0., NULL
);
89 CFStringRef cfname
= CTFontCopyFullName(font
);
91 static char fname
[100];
92 CFStringGetCString(cfname
, fname
, sizeof(fname
), kCFStringEncodingUTF8
);
93 tabfontnames
[i
] = strdup(fname
); // never free'ed
97 qsort(tabfontnames
, count
, sizeof(char*), name_compare
);
98 for (i
= 0; i
< count
; i
++) {
99 Fl::set_font((Fl_Font
)(fl_free_font
++), tabfontnames
[i
]);
101 delete[] tabfontnames
;
102 return (Fl_Font
)fl_free_font
;
107 ItemCount oFontCount
, oCountAgain
;
108 ATSUFontID
*oFontIDs
;
110 ATSUFontCount (&oFontCount
);
111 // now allocate space for them...
112 oFontIDs
= (ATSUFontID
*)malloc((oFontCount
+1) * sizeof(ATSUFontID
));
113 ATSUGetFontIDs (oFontIDs
, oFontCount
, &oCountAgain
);
114 // Now oFontIDs should contain a list of all the available Unicode fonts
115 // Iterate through the list to get each font name
116 for (ItemCount idx
= 0; idx
< oFontCount
; idx
++)
118 // ByteCount actualLength = 0;
120 // How to get the name - Apples docs say call this twice, once to get the length, then again
121 // to get the actual name...
122 // ATSUFindFontName (oFontIDs[idx], kFontFullName, kFontMacintoshPlatform, kFontRomanScript, kFontEnglishLanguage,
123 // 0, NULL, &actualLength, NULL);
124 // Now actualLength tells us the length of buffer we need
125 // oName = (Ptr)malloc(actualLength + 8);
126 // But who's got time for that nonsense? Let's just hard code a fixed buffer (urgh!)
127 ByteCount actualLength
= 511;
129 ATSUFindFontName (oFontIDs
[idx
], kFontFullName
, kFontMacintoshPlatform
, kFontRomanScript
, kFontEnglishLanguage
,
130 actualLength
, oName
, &actualLength
, &oCountAgain
);
131 // bounds check and terminate the returned name
132 if(actualLength
> 511)
135 oName
[actualLength
] = 0;
136 Fl::set_font((Fl_Font
)(fl_free_font
++), strdup(oName
));
140 return (Fl_Font
)fl_free_font
;
142 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
148 static int array
[128];
149 int Fl::get_font_sizes(Fl_Font fnum
, int*& sizep
) {
150 Fl_Fontdesc
*s
= fl_fonts
+fnum
;
151 if (!s
->name
) s
= fl_fonts
; // empty slot in table, use entry 0
154 // ATS supports all font size
163 // End of "$Id: fl_set_fonts_mac.cxx 8504 2011-03-04 16:48:10Z manolo $".