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

fl_set_fonts_mac.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: fl_set_fonts_mac.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
00003 //
00004 // MacOS font utilities for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 #include <config.h>
00029 
00030 // #inclde <SFNTTypes.h>
00031 
00032 // This function fills in the fltk font table with all the fonts that
00033 // are found on the X server.  It tries to place the fonts into families
00034 // and to sort them so the first 4 in a family are normal, bold, italic,
00035 // and bold italic.
00036 
00037 // Bug: older versions calculated the value for *ap as a side effect of
00038 // making the name, and then forgot about it. To avoid having to change
00039 // the header files I decided to store this value in the last character
00040 // of the font name array.
00041 #define ENDOFBUFFER 127 // sizeof(Fl_Font.fontname)-1
00042 
00043 // turn a stored font name into a pretty name:
00044 const char* Fl::get_font_name(Fl_Font fnum, int* ap) {
00045   Fl_Fontdesc *f = fl_fonts + fnum;
00046   if (!f->fontname[0]) {
00047     const char* p = f->name;
00048     if (!p || !*p) {if (ap) *ap = 0; return "";}
00049     strlcpy(f->fontname, p, ENDOFBUFFER);
00050     int type = 0;
00051     if (strstr(f->name, "Bold")) type |= FL_BOLD;
00052     if (strstr(f->name, "Italic")) type |= FL_ITALIC;
00053     f->fontname[ENDOFBUFFER] = (char)type;
00054   }
00055   if (ap) *ap = f->fontname[ENDOFBUFFER];
00056   return f->fontname;
00057 }
00058 
00059 static int fl_free_font = FL_FREE_FONT;
00060 
00061 Fl_Font Fl::set_fonts(const char* xstarname) {
00062 #pragma unused ( xstarname )
00063 if (fl_free_font > FL_FREE_FONT) return (Fl_Font)fl_free_font; // if already called
00064 
00065 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
00066   if(fl_mac_os_version == 0) fl_open_display();
00067 if(fl_mac_os_version >= 0x1050) {
00068 //if(CTFontCreateWithFontDescriptor != NULL) {// CTFontCreateWithFontDescriptor != NULL on 10.4 also!
00069   int value[1] = {1};
00070   CFDictionaryRef dict = CFDictionaryCreate(NULL, 
00071                                             (const void **)kCTFontCollectionRemoveDuplicatesOption, 
00072                                             (const void **)&value, 1, NULL, NULL);
00073   CTFontCollectionRef fcref = CTFontCollectionCreateFromAvailableFonts(dict);
00074   CFRelease(dict);
00075   CFArrayRef arrayref = CTFontCollectionCreateMatchingFontDescriptors(fcref);
00076   CFRelease(fcref);
00077   CFIndex count = CFArrayGetCount(arrayref);
00078   CFIndex i;
00079   for (i = 0; i < count; i++) {
00080         CTFontDescriptorRef fdesc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(arrayref, i);
00081         CTFontRef font = CTFontCreateWithFontDescriptor(fdesc, 0., NULL);
00082         CFStringRef cfname = CTFontCopyPostScriptName(font);
00083         CFRelease(font);
00084         static char fname[100];
00085         CFStringGetCString(cfname, fname, sizeof(fname), kCFStringEncodingUTF8);
00086         CFRelease(cfname);
00087         Fl::set_font((Fl_Font)(fl_free_font++), strdup(fname));
00088         }
00089   CFRelease(arrayref);
00090   return (Fl_Font)fl_free_font;
00091 }
00092 else {
00093 #endif
00094 #if ! __LP64__
00095   ItemCount oFontCount, oCountAgain;
00096   ATSUFontID *oFontIDs;
00097   // How many fonts?
00098   ATSUFontCount (&oFontCount);
00099   // now allocate space for them...
00100   oFontIDs = (ATSUFontID *)malloc((oFontCount+1) * sizeof(ATSUFontID));
00101   ATSUGetFontIDs (oFontIDs, oFontCount, &oCountAgain);
00102   // Now oFontIDs should contain a list of all the available Unicode fonts
00103   // Iterate through the list to get each font name
00104   for (ItemCount idx = 0; idx < oFontCount; idx++)
00105   {
00106 //  ByteCount actualLength = 0;
00107 //      Ptr oName;
00108     // How to get the name - Apples docs say call this twice, once to get the length, then again 
00109         // to get the actual name...
00110 //    ATSUFindFontName (oFontIDs[idx], kFontFullName, kFontMacintoshPlatform, kFontRomanScript, kFontEnglishLanguage,
00111 //                      0, NULL, &actualLength, NULL);
00112     // Now actualLength tells us the length of buffer we need
00113 //      oName = (Ptr)malloc(actualLength + 8);
00114 // But who's got time for that nonsense? Let's just hard code a fixed buffer (urgh!)
00115     ByteCount actualLength = 511;
00116         char oName[512];
00117     ATSUFindFontName (oFontIDs[idx], kFontFullName, kFontMacintoshPlatform, kFontRomanScript, kFontEnglishLanguage,
00118                       actualLength, oName, &actualLength, &oCountAgain);
00119     // bounds check and terminate the returned name
00120     if(actualLength > 511)
00121       oName[511] = 0;
00122     else
00123       oName[actualLength] = 0;
00124         Fl::set_font((Fl_Font)(fl_free_font++), strdup(oName));
00125 //      free(oName);
00126   }
00127   free(oFontIDs);
00128   return (Fl_Font)fl_free_font;
00129 #endif //__LP64__
00130 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
00131   }
00132 #endif
00133   return 0;
00134 }
00135 
00136 static int array[128];
00137 int Fl::get_font_sizes(Fl_Font fnum, int*& sizep) {
00138   Fl_Fontdesc *s = fl_fonts+fnum;
00139   if (!s->name) s = fl_fonts; // empty slot in table, use entry 0
00140   int cnt = 0;
00141 
00142   // ATS supports all font size 
00143   array[0] = 0;
00144   sizep = array;
00145   cnt = 1;
00146 
00147   return cnt;
00148 }
00149 
00150 //
00151 // End of "$Id: fl_set_fonts_mac.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".
00152 //