themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / fl_set_fonts_mac.cxx
blob7c3eb6f50557ebc02c5b5ecc76abd23af71f3616
1 //
2 // "$Id: fl_set_fonts_mac.cxx 8504 2011-03-04 16:48:10Z manolo $"
3 //
4 // MacOS font utilities for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2011 by Bill Spitzak and others.
7 //
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
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include <config.h>
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,
35 // and 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);
50 int type = 0;
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];
56 return f->fontname;
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);
64 #endif
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!
75 int value[1] = {1};
76 CFDictionaryRef dict = CFDictionaryCreate(NULL,
77 (const void **)kCTFontCollectionRemoveDuplicatesOption,
78 (const void **)&value, 1, NULL, NULL);
79 CTFontCollectionRef fcref = CTFontCollectionCreateFromAvailableFonts(dict);
80 CFRelease(dict);
81 CFArrayRef arrayref = CTFontCollectionCreateMatchingFontDescriptors(fcref);
82 CFRelease(fcref);
83 CFIndex count = CFArrayGetCount(arrayref);
84 CFIndex i;
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);
90 CFRelease(font);
91 static char fname[100];
92 CFStringGetCString(cfname, fname, sizeof(fname), kCFStringEncodingUTF8);
93 tabfontnames[i] = strdup(fname); // never free'ed
94 CFRelease(cfname);
96 CFRelease(arrayref);
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;
104 else {
105 #endif
106 #if ! __LP64__
107 ItemCount oFontCount, oCountAgain;
108 ATSUFontID *oFontIDs;
109 // How many fonts?
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;
119 // Ptr oName;
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;
128 char oName[512];
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)
133 oName[511] = 0;
134 else
135 oName[actualLength] = 0;
136 Fl::set_font((Fl_Font)(fl_free_font++), strdup(oName));
137 // free(oName);
139 free(oFontIDs);
140 return (Fl_Font)fl_free_font;
141 #endif //__LP64__
142 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
144 #endif
145 return 0;
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
152 int cnt = 0;
154 // ATS supports all font size
155 array[0] = 0;
156 sizep = array;
157 cnt = 1;
159 return cnt;
163 // End of "$Id: fl_set_fonts_mac.cxx 8504 2011-03-04 16:48:10Z manolo $".