# fontcache.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "fontcache.h"
    00014 #include "blitter/factory.hpp"
    00015 #include "core/math_func.hpp"
    00016 #include "strings_func.h"
    00017 #include "zoom_type.h"
    00018
    00019 #include "table/sprites.h"
    00020 #include "table/control_codes.h"
    00021
    00022 static const int ASCII_LETTERSTART = 32;
    00023 static const int MAX_FONT_SIZE     = 72;
    00024
    00026 int _font_height[FS_END];
    00028 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
    00029
    00034 void ResetFontSizes(bool monospace)
    00035 {
    00036   if (monospace) {
    00037     _font_height[FS_MONO]   = _default_font_height[FS_MONO];
    00038   } else {
    00039     _font_height[FS_SMALL]  = _default_font_height[FS_SMALL];
    00040     _font_height[FS_NORMAL] = _default_font_height[FS_NORMAL];
    00041     _font_height[FS_LARGE]  = _default_font_height[FS_LARGE];
    00042   }
    00043 }
    00044
    00045 #ifdef WITH_FREETYPE
    00046 #include <ft2build.h>
    00047 #include FT_FREETYPE_H
    00048 #include FT_GLYPH_H
    00049 #include FT_TRUETYPE_TABLES_H
    00050
    00051 #ifdef WITH_FONTCONFIG
    00052 #include <fontconfig/fontconfig.h>
    00053 #endif
    00054
    00055 static FT_Library _library = NULL;
    00056 static FT_Face _face_small = NULL;
    00057 static FT_Face _face_medium = NULL;
    00058 static FT_Face _face_large = NULL;
    00059 static FT_Face _face_mono = NULL;
    00060 static int _ascender[FS_END];
    00061
    00062 FreeTypeSettings _freetype;
    00063
    00064 static const byte FACE_COLOUR   = 1;
    00065 static const byte SHADOW_COLOUR = 2;
    00066
    00072 /* ========================================================================================
    00073  * Windows support
    00074  * ======================================================================================== */
    00075
    00076 #ifdef WIN32
    00077 #include <windows.h>
    00078 #include <shlobj.h> /* SHGetFolderPath */
    00079 #include "os/windows/win32.h"
    00080
    00091 char *GetShortPath(const char *long_path)
    00092 {
    00093   static char short_path[MAX_PATH];
    00094 #ifdef UNICODE
    00095   /* The non-unicode GetShortPath doesn't support UTF-8...,
    00096    * so convert the path to wide chars, then get the short
    00097    * path and convert it back again. */
    00098   wchar_t long_path_w[MAX_PATH];
    00099   MultiByteToWideChar(CP_UTF8, 0, long_path, -1, long_path_w, MAX_PATH);
    00100
    00101   wchar_t short_path_w[MAX_PATH];
    00102   GetShortPathNameW(long_path_w, short_path_w, MAX_PATH);
    00103
    00104   WideCharToMultiByte(CP_ACP, 0, short_path_w, -1, short_path, MAX_PATH, NULL, NULL);
    00105 #else
    00106   /* Technically not needed, but do it for consistency. */
    00107   GetShortPathNameA(long_path, short_path, MAX_PATH);
    00108 #endif
    00109   return short_path;
    00110 }
    00111
    00112 /* Get the font file to be loaded into Freetype by looping the registry
    00113  * location where windows lists all installed fonts. Not very nice, will
    00114  * surely break if the registry path changes, but it works. Much better
    00115  * solution would be to use CreateFont, and extract the font data from it
    00116  * by GetFontData. The problem with this is that the font file needs to be
    00117  * kept in memory then until the font is no longer needed. This could mean
    00118  * an additional memory usage of 30MB (just for fonts!) when using an eastern
    00119  * font for all font sizes */
    00120 #define FONT_DIR_NT "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
    00121 #define FONT_DIR_9X "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Fonts"
    00122 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
    00123 {
    00124   FT_Error err = FT_Err_Cannot_Open_Resource;
    00125   HKEY hKey;
    00126   LONG ret;
    00127   TCHAR vbuffer[MAX_PATH], dbuffer[256];
    00128   TCHAR *font_namep;
    00129   char *font_path;
    00130   uint index;
    00131
    00132   /* On windows NT (2000, NT3.5, XP, etc.) the fonts are stored in the
    00133    * "Windows NT" key, on Windows 9x in the Windows key. To save us having
    00134    * to retrieve the windows version, we'll just query both */
    00135   ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_NT), 0, KEY_READ, &hKey);
    00136   if (ret != ERROR_SUCCESS) ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_9X), 0, KEY_READ, &hKey);
    00137
    00138   if (ret != ERROR_SUCCESS) {
    00139     DEBUG(freetype, 0, "Cannot open registry key HKLM\\SOFTWARE\\Microsoft\\Windows (NT)\\CurrentVersion\\Fonts");
    00140     return err;
    00141   }
    00142
    00143   /* For Unicode we need some conversion between widechar and
    00144    * normal char to match the data returned by RegEnumValue,
    00145    * otherwise just use parameter */
    00146 #if defined(UNICODE)
    00147   font_namep = MallocT<TCHAR>(MAX_PATH);
    00148   MB_TO_WIDE_BUFFER(font_name, font_namep, MAX_PATH * sizeof(TCHAR));
    00149 #else
    00150   font_namep = const_cast<char *>(font_name); // only cast because in unicode pointer is not const
    00151 #endif
    00152
    00153   for (index = 0;; index++) {
    00154     TCHAR *s;
    00155     DWORD vbuflen = lengthof(vbuffer);
    00156     DWORD dbuflen = lengthof(dbuffer);
    00157
    00158     ret = RegEnumValue(hKey, index, vbuffer, &vbuflen, NULL, NULL, (byte*)dbuffer, &dbuflen);
    00159     if (ret != ERROR_SUCCESS) goto registry_no_font_found;
    00160
    00161     /* The font names in the registry are of the following 3 forms:
    00162      * - ADMUI3.fon
    00163      * - Book Antiqua Bold (TrueType)
    00164      * - Batang & BatangChe & Gungsuh & GungsuhChe (TrueType)
    00165      * We will strip the font-type '()' if any and work with the font name
    00166      * itself, which must match exactly; if...
    00167      * TTC files, font files which contain more than one font are separated
    00168      * by '&'. Our best bet will be to do substr match for the fontname
    00169      * and then let FreeType figure out which index to load */
    00170     s = _tcschr(vbuffer, _T('('));
    00171     if (s != NULL) s[-1] = '\0';
    00172
    00173     if (_tcschr(vbuffer, _T('&')) == NULL) {
    00174       if (_tcsicmp(vbuffer, font_namep) == 0) break;
    00175     } else {
    00176       if (_tcsstr(vbuffer, font_namep) != NULL) break;
    00177     }
    00178   }
    00179
    00180   if (!SUCCEEDED(OTTDSHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, vbuffer))) {
    00181     DEBUG(freetype, 0, "SHGetFolderPath cannot return fonts directory");
    00182     goto folder_error;
    00183   }
    00184
    00185   /* Some fonts are contained in .ttc files, TrueType Collection fonts. These
    00186    * contain multiple fonts inside this single file. GetFontData however
    00187    * returns the whole file, so we need to check each font inside to get the
    00188    * proper font.
    00189    * Also note that FreeType does not support UNICODE filenames! */
    00190 #if defined(UNICODE)
    00191   /* We need a cast here back from wide because FreeType doesn't support
    00192    * widechar filenames. Just use the buffer we allocated before for the
    00193    * font_name search */
    00194   font_path = (char*)font_namep;
    00195   WIDE_TO_MB_BUFFER(vbuffer, font_path, MAX_PATH * sizeof(TCHAR));
    00196 #else
    00197   font_path = vbuffer;
    00198 #endif
    00199
    00200   ttd_strlcat(font_path, "\\", MAX_PATH * sizeof(TCHAR));
    00201   ttd_strlcat(font_path, WIDE_TO_MB(dbuffer), MAX_PATH * sizeof(TCHAR));
    00202
    00203   /* Convert the path into something that FreeType understands */
    00204   font_path = GetShortPath(font_path);
    00205
    00206   index = 0;
    00207   do {
    00208     err = FT_New_Face(_library, font_path, index, face);
    00209     if (err != FT_Err_Ok) break;
    00210
    00211     if (strncasecmp(font_name, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
    00212     /* Try english name if font name failed */
    00213     if (strncasecmp(font_name + strlen(font_name) + 1, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
    00214     err = FT_Err_Cannot_Open_Resource;
    00215
    00216   } while ((FT_Long)++index != (*face)->num_faces);
    00217
    00218
    00219 folder_error:
    00220 registry_no_font_found:
    00221 #if defined(UNICODE)
    00222   free(font_namep);
    00223 #endif
    00224   RegCloseKey(hKey);
    00225   return err;
    00226 }
    00227
    00241 static const char *GetEnglishFontName(const ENUMLOGFONTEX *logfont)
    00242 {
    00243   static char font_name[MAX_PATH];
    00244   const char *ret_font_name = NULL;
    00245   uint pos = 0;
    00246   HDC dc;
    00247   HGDIOBJ oldfont;
    00248   byte *buf;
    00249   DWORD dw;
    00250   uint16 format, count, stringOffset, platformId, encodingId, languageId, nameId, length, offset;
    00251
    00252   HFONT font = CreateFontIndirect(&logfont->elfLogFont);
    00253   if (font == NULL) goto err1;
    00254
    00255   dc = GetDC(NULL);
    00256   oldfont = SelectObject(dc, font);
    00257   dw = GetFontData(dc, 'eman', 0, NULL, 0);
    00258   if (dw == GDI_ERROR) goto err2;
    00259
    00260   buf = MallocT<byte>(dw);
    00261   dw = GetFontData(dc, 'eman', 0, buf, dw);
    00262   if (dw == GDI_ERROR) goto err3;
    00263
    00264   format = buf[pos++] << 8;
    00265   format += buf[pos++];
    00266   assert(format == 0);
    00267   count = buf[pos++] << 8;
    00268   count += buf[pos++];
    00269   stringOffset = buf[pos++] << 8;
    00270   stringOffset += buf[pos++];
    00271   for (uint i = 0; i < count; i++) {
    00272     platformId = buf[pos++] << 8;
    00273     platformId += buf[pos++];
    00274     encodingId = buf[pos++] << 8;
    00275     encodingId += buf[pos++];
    00276     languageId = buf[pos++] << 8;
    00277     languageId += buf[pos++];
    00278     nameId = buf[pos++] << 8;
    00279     nameId += buf[pos++];
    00280     if (nameId != 1) {
    00281       pos += 4; // skip length and offset
    00282       continue;
    00283     }
    00284     length = buf[pos++] << 8;
    00285     length += buf[pos++];
    00286     offset = buf[pos++] << 8;
    00287     offset += buf[pos++];
    00288
    00289     /* Don't buffer overflow */
    00290     length = min(length, MAX_PATH - 1);
    00291     for (uint j = 0; j < length; j++) font_name[j] = buf[stringOffset + offset + j];
    00292     font_name[length] = '\0';
    00293
    00294     if ((platformId == 1 && languageId == 0) ||      // Macintosh English
    00295         (platformId == 3 && languageId == 0x0409)) { // Microsoft English (US)
    00296       ret_font_name = font_name;
    00297       break;
    00298     }
    00299   }
    00300
    00301 err3:
    00302   free(buf);
    00303 err2:
    00304   SelectObject(dc, oldfont);
    00305   ReleaseDC(NULL, dc);
    00306   DeleteObject(font);
    00307 err1:
    00308   return ret_font_name == NULL ? WIDE_TO_MB((const TCHAR*)logfont->elfFullName) : ret_font_name;
    00309 }
    00310
    00311 class FontList {
    00312 protected:
    00313   TCHAR **fonts;
    00314   uint items;
    00315   uint capacity;
    00316
    00317 public:
    00318   FontList() : fonts(NULL), items(0), capacity(0) { };
    00319
    00320   ~FontList() {
    00321     if (this->fonts == NULL) return;
    00322
    00323     for (uint i = 0; i < this->items; i++) {
    00324       free(this->fonts[i]);
    00325     }
    00326
    00327     free(this->fonts);
    00328   }
    00329
    00330   bool Add(const TCHAR *font) {
    00331     for (uint i = 0; i < this->items; i++) {
    00332       if (_tcscmp(this->fonts[i], font) == 0) return false;
    00333     }
    00334
    00335     if (this->items == this->capacity) {
    00336       this->capacity += 10;
    00337       this->fonts = ReallocT(this->fonts, this->capacity);
    00338     }
    00339
    00340     this->fonts[this->items++] = _tcsdup(font);
    00341
    00342     return true;
    00343   }
    00344 };
    00345
    00346 struct EFCParam {
    00347   FreeTypeSettings *settings;
    00348   LOCALESIGNATURE  locale;
    00349   MissingGlyphSearcher *callback;
    00350   FontList fonts;
    00351 };
    00352
    00353 static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
    00354 {
    00355   EFCParam *info = (EFCParam *)lParam;
    00356
    00357   /* Skip duplicates */
    00358   if (!info->fonts.Add((const TCHAR*)logfont->elfFullName)) return 1;
    00359   /* Only use TrueType fonts */
    00360   if (!(type & TRUETYPE_FONTTYPE)) return 1;
    00361   /* Don't use SYMBOL fonts */
    00362   if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
    00363   /* Use monospaced fonts when asked for it. */
    00364   if (info->callback->Monospace() && (logfont->elfLogFont.lfPitchAndFamily & (FF_MODERN | FIXED_PITCH)) != (FF_MODERN | FIXED_PITCH)) return 1;
    00365
    00366   /* The font has to have at least one of the supported locales to be usable. */
    00367   if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
    00368     /* On win9x metric->ntmFontSig seems to contain garbage. */
    00369     FONTSIGNATURE fs;
    00370     memset(&fs, 0, sizeof(fs));
    00371     HFONT font = CreateFontIndirect(&logfont->elfLogFont);
    00372     if (font != NULL) {
    00373       HDC dc = GetDC(NULL);
    00374       HGDIOBJ oldfont = SelectObject(dc, font);
    00375       GetTextCharsetInfo(dc, &fs, 0);
    00376       SelectObject(dc, oldfont);
    00377       ReleaseDC(NULL, dc);
    00378       DeleteObject(font);
    00379     }
    00380     if ((fs.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (fs.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) return 1;
    00381   }
    00382
    00383   char font_name[MAX_PATH];
    00384 #if defined(UNICODE)
    00385   WIDE_TO_MB_BUFFER((const TCHAR*)logfont->elfFullName, font_name, lengthof(font_name));
    00386 #else
    00387   strecpy(font_name, (const TCHAR*)logfont->elfFullName, lastof(font_name));
    00388 #endif
    00389
    00390   /* Add english name after font name */
    00391   const char *english_name = GetEnglishFontName(logfont);
    00392   strecpy(font_name + strlen(font_name) + 1, english_name, lastof(font_name));
    00393
    00394   /* Check whether we can actually load the font. */
    00395   bool ft_init = _library != NULL;
    00396   bool found = false;
    00397   FT_Face face;
    00398   /* Init FreeType if needed. */
    00399   if ((ft_init || FT_Init_FreeType(&_library) == FT_Err_Ok) && GetFontByFaceName(font_name, &face) == FT_Err_Ok) {
    00400     FT_Done_Face(face);
    00401     found = true;
    00402   }
    00403   if (!ft_init) {
    00404     /* Uninit FreeType if we did the init. */
    00405     FT_Done_FreeType(_library);
    00406     _library = NULL;
    00407   }
    00408
    00409   if (!found) return 1;
    00410
    00411   info->callback->SetFontNames(info->settings, font_name);
    00412   if (info->callback->FindMissingGlyphs(NULL)) return 1;
    00413   DEBUG(freetype, 1, "Fallback font: %s (%s)", font_name, english_name);
    00414   return 0; // stop enumerating
    00415 }
    00416
    00417 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
    00418 {
    00419   DEBUG(freetype, 1, "Trying fallback fonts");
    00420   EFCParam langInfo;
    00421   if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale, sizeof(langInfo.locale) / sizeof(TCHAR)) == 0) {
    00422     /* Invalid langid or some other mysterious error, can't determine fallback font. */
    00423     DEBUG(freetype, 1, "Can't get locale info for fallback font (langid=0x%x)", winlangid);
    00424     return false;
    00425   }
    00426   langInfo.settings = settings;
    00427   langInfo.callback = callback;
    00428
    00429   LOGFONT font;
    00430   /* Enumerate all fonts. */
    00431   font.lfCharSet = DEFAULT_CHARSET;
    00432   font.lfFaceName[0] = '\0';
    00433   font.lfPitchAndFamily = 0;
    00434
    00435   HDC dc = GetDC(NULL);
    00436   int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
    00437   ReleaseDC(NULL, dc);
    00438   return ret == 0;
    00439 }
    00440
    00441 #elif defined(__APPLE__) /* end ifdef Win32 */
    00442 /* ========================================================================================
    00443  * OSX support
    00444  * ======================================================================================== */
    00445
    00446 #include "os/macosx/macos.h"
    00447
    00448 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
    00449 {
    00450   FT_Error err = FT_Err_Cannot_Open_Resource;
    00451
    00452   /* Get font reference from name. */
    00453   CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name, kCFStringEncodingUTF8);
    00454   ATSFontRef font = ATSFontFindFromName(name, kATSOptionFlagsDefault);
    00455   CFRelease(name);
    00456   if (font == kInvalidFont) return err;
    00457
    00458   /* Get a file system reference for the font. */
    00459   FSRef ref;
    00460   OSStatus os_err = -1;
    00461 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
    00462   if (MacOSVersionIsAtLeast(10, 5, 0)) {
    00463     os_err = ATSFontGetFileReference(font, &ref);
    00464   } else
    00465 #endif
    00466   {
    00467 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) && !__LP64__
    00468     /* This type was introduced with the 10.5 SDK. */
    00469 #if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
    00470   #define ATSFSSpec FSSpec
    00471 #endif
    00472     FSSpec spec;
    00473     os_err = ATSFontGetFileSpecification(font, (ATSFSSpec *)&spec);
    00474     if (os_err == noErr) os_err = FSpMakeFSRef(&spec, &ref);
    00475 #endif
    00476   }
    00477
    00478   if (os_err == noErr) {
    00479     /* Get unix path for file. */
    00480     UInt8 file_path[PATH_MAX];
    00481     if (FSRefMakePath(&ref, file_path, sizeof(file_path)) == noErr) {
    00482       DEBUG(freetype, 3, "Font path for %s: %s", font_name, file_path);
    00483       err = FT_New_Face(_library, (const char *)file_path, 0, face);
    00484     }
    00485   }
    00486
    00487   return err;
    00488 }
    00489
    00490 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
    00491 {
    00492   const char *str;
    00493   bool result = false;
    00494
    00495   callback->FindMissingGlyphs(&str);
    00496
    00497 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
    00498   if (MacOSVersionIsAtLeast(10, 5, 0)) {
    00499     /* Determine fallback font using CoreText. This uses the language isocode
    00500      * to find a suitable font. CoreText is available from 10.5 onwards. */
    00501     char lang[16];
    00502     if (strcmp(language_isocode, "zh_TW") == 0) {
    00503       /* Traditional Chinese */
    00504       strecpy(lang, "zh-Hant", lastof(lang));
    00505     } else if (strcmp(language_isocode, "zh_CN") == 0) {
    00506       /* Simplified Chinese */
    00507       strecpy(lang, "zh-Hans", lastof(lang));
    00508     } else if (strncmp(language_isocode, "ur", 2) == 0) {
    00509       /* The urdu alphabet is variant of persian. As OS X has no default
    00510        * font that advertises an urdu language code, search for persian
    00511        * support instead. */
    00512       strecpy(lang, "fa", lastof(lang));
    00513     } else {
    00514       /* Just copy the first part of the isocode. */
    00515       strecpy(lang, language_isocode, lastof(lang));
    00516       char *sep = strchr(lang, '_');
    00517       if (sep != NULL) *sep = '\0';
    00518     }
    00519
    00520     CFStringRef lang_code;
    00521     lang_code = CFStringCreateWithCString(kCFAllocatorDefault, lang, kCFStringEncodingUTF8);
    00522
    00523     /* Create a font iterator and iterate over all fonts that
    00524      * are available to the application. */
    00525     ATSFontIterator itr;
    00526     ATSFontRef font;
    00527     ATSFontIteratorCreate(kATSFontContextLocal, NULL, NULL, kATSOptionFlagsUnRestrictedScope, &itr);
    00528     while (!result && ATSFontIteratorNext(itr, &font) == noErr) {
    00529       /* Get CoreText font handle. */
    00530       CTFontRef font_ref = CTFontCreateWithPlatformFont(font, 0.0, NULL, NULL);
    00531       CFArrayRef langs = CTFontCopySupportedLanguages(font_ref);
    00532       if (langs != NULL) {
    00533         /* Font has a list of supported languages. */
    00534         for (CFIndex i = 0; i < CFArrayGetCount(langs); i++) {
    00535           CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(langs, i);
    00536           if (CFStringCompare(lang, lang_code, kCFCompareAnchored) == kCFCompareEqualTo) {
    00537             /* Lang code is supported by font, get full font name. */
    00538             CFStringRef font_name = CTFontCopyFullName(font_ref);
    00539             char name[128];
    00540             CFStringGetCString(font_name, name, lengthof(name), kCFStringEncodingUTF8);
    00541             CFRelease(font_name);
    00542             /* Skip some inappropriate or ugly looking fonts that have better alternatives. */
    00543             if (strncmp(name, "Courier", 7) == 0 || strncmp(name, "Apple Symbols", 13) == 0 ||
    00544                 strncmp(name, ".Aqua", 5) == 0 || strncmp(name, "LastResort", 10) == 0 ||
    00545                 strncmp(name, "GB18030 Bitmap", 14) == 0) continue;
    00546
    00547             /* Save result. */
    00548             callback->SetFontNames(settings, name);
    00549             DEBUG(freetype, 2, "CT-Font for %s: %s", language_isocode, name);
    00550             result = true;
    00551             break;
    00552           }
    00553         }
    00554         CFRelease(langs);
    00555       }
    00556       CFRelease(font_ref);
    00557     }
    00558     ATSFontIteratorRelease(&itr);
    00559     CFRelease(lang_code);
    00560   } else
    00561 #endif
    00562   {
    00563 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) && !__LP64__
    00564     /* Determine fallback font using ATSUI. This uses a string sample with
    00565      * missing characters. This is not failure-proof, but a better way like
    00566      * using the isocode as in the CoreText code path is not available.
    00567      * ATSUI was deprecated with 10.6 and is only partially available in
    00568      * 64-bit mode. */
    00569
    00570     /* Remove all control characters in the range from SCC_CONTROL_START to
    00571      * SCC_CONTROL_END as well as all ASCII < 0x20 from the string as it will
    00572      * mess with the automatic font detection */
    00573     char buff[256]; // This length is enough to find a suitable replacement font
    00574     strecpy(buff, str, lastof(buff));
    00575     str_validate(buff, lastof(buff), SVS_ALLOW_NEWLINE);
    00576
    00577     /* Extract a UniChar representation of the sample string. */
    00578     CFStringRef cf_str = CFStringCreateWithCString(kCFAllocatorDefault, buff, kCFStringEncodingUTF8);
    00579     if (cf_str == NULL) {
    00580       /* Something went wrong. Corrupt/invalid sample string? */
    00581       return false;
    00582     }
    00583     CFIndex str_len = CFStringGetLength(cf_str);
    00584     UniChar string[str_len];
    00585     CFStringGetCharacters(cf_str, CFRangeMake(0, str_len), string);
    00586
    00587     /* Create a default text style with the default font. */
    00588     ATSUStyle style;
    00589     ATSUCreateStyle(&style);
    00590
    00591     /* Create a text layout object from the sample string using the text style. */
    00592     UniCharCount run_len = kATSUToTextEnd;
    00593     ATSUTextLayout text_layout;
    00594     ATSUCreateTextLayoutWithTextPtr(string, kATSUFromTextBeginning, kATSUToTextEnd, str_len, 1, &run_len, &style, &text_layout);
    00595
    00596     /* Try to match a font for the sample text. ATSUMatchFontsToText stops after
    00597      * it finds the first continuous character run not renderable with the currently
    00598      * selected font starting at offset. The matching needs to be repeated until
    00599      * the end of the string is reached to make sure the fallback font matches for
    00600      * all characters in the string and not only the first run. */
    00601     UniCharArrayOffset offset = kATSUFromTextBeginning;
    00602     OSStatus os_err;
    00603     do {
    00604       ATSUFontID font;
    00605       UniCharCount run_len;
    00606       os_err = ATSUMatchFontsToText(text_layout, offset, kATSUToTextEnd, &font, &offset, &run_len);
    00607       if (os_err == kATSUFontsMatched) {
    00608         /* Found a better fallback font. Update the text layout
    00609          * object with the new font. */
    00610         ATSUAttributeTag tag = kATSUFontTag;
    00611         ByteCount size = sizeof(font);
    00612         ATSUAttributeValuePtr val = &font;
    00613         ATSUSetAttributes(style, 1, &tag, &size, &val);
    00614         offset += run_len;
    00615       }
    00616       /* Exit if the end of the string is reached or some other error occurred. */
    00617     } while (os_err == kATSUFontsMatched && offset < (UniCharArrayOffset)str_len);
    00618
    00619     if (os_err == noErr || os_err == kATSUFontsMatched) {
    00620       /* ATSUMatchFontsToText exited normally. Extract font
    00621        * out of the text layout object. */
    00622       ATSUFontID font;
    00623       ByteCount act_len;
    00624       ATSUGetAttribute(style, kATSUFontTag, sizeof(font), &font, &act_len);
    00625
    00626       /* Get unique font name. The result is not a c-string, we have
    00627        * to leave space for a \0 and terminate it ourselves. */
    00628       char name[128];
    00629       ATSUFindFontName(font, kFontUniqueName, kFontNoPlatformCode, kFontNoScriptCode, kFontNoLanguageCode, 127, name, &act_len, NULL);
    00630       name[act_len > 127 ? 127 : act_len] = '\0';
    00631
    00632       /* Save Result. */
    00633       callback->SetFontNames(settings, name);
    00634       DEBUG(freetype, 2, "ATSUI-Font for %s: %s", language_isocode, name);
    00635       result = true;
    00636     }
    00637
    00638     ATSUDisposeTextLayout(text_layout);
    00639     ATSUDisposeStyle(style);
    00640     CFRelease(cf_str);
    00641 #endif
    00642   }
    00643
    00644   if (result && strncmp(settings->medium_font, "Geeza Pro", 9) == 0) {
    00645     /* The font 'Geeza Pro' is often found for arabic characters, but
    00646      * it has the 'tiny' problem of not having any latin characters.
    00647      * 'Arial Unicode MS' on the other hand has arabic and latin glyphs,
    00648      * but seems to 'forget' to inform the OS about this fact. Manually
    00649      * substitute the latter for the former if it is loadable. */
    00650     bool ft_init = _library != NULL;
    00651     FT_Face face;
    00652     /* Init FreeType if needed. */
    00653     if ((ft_init || FT_Init_FreeType(&_library) == FT_Err_Ok) && GetFontByFaceName("Arial Unicode MS", &face) == FT_Err_Ok) {
    00654       FT_Done_Face(face);
    00655       callback->SetFontNames(settings, "Arial Unicode MS");
    00656       DEBUG(freetype, 1, "Replacing font 'Geeza Pro' with 'Arial Unicode MS'");
    00657     }
    00658     if (!ft_init) {
    00659       /* Uninit FreeType if we did the init. */
    00660       FT_Done_FreeType(_library);
    00661       _library = NULL;
    00662     }
    00663    }
    00664
    00665   callback->FindMissingGlyphs(NULL);
    00666   return result;
    00667 }
    00668
    00669 #elif defined(WITH_FONTCONFIG) /* end ifdef __APPLE__ */
    00670 /* ========================================================================================
    00671  * FontConfig (unix) support
    00672  * ======================================================================================== */
    00673 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
    00674 {
    00675   FT_Error err = FT_Err_Cannot_Open_Resource;
    00676
    00677   if (!FcInit()) {
    00678     ShowInfoF("Unable to load font configuration");
    00679   } else {
    00680     FcPattern *match;
    00681     FcPattern *pat;
    00682     FcFontSet *fs;
    00683     FcResult  result;
    00684     char *font_style;
    00685     char *font_family;
    00686
    00687     /* Split & strip the font's style */
    00688     font_family = strdup(font_name);
    00689     font_style = strchr(font_family, ',');
    00690     if (font_style != NULL) {
    00691       font_style[0] = '\0';
    00692       font_style++;
    00693       while (*font_style == ' ' || *font_style == '\t') font_style++;
    00694     }
    00695
    00696     /* Resolve the name and populate the information structure */
    00697     pat = FcNameParse((FcChar8*)font_family);
    00698     if (font_style != NULL) FcPatternAddString(pat, FC_STYLE, (FcChar8*)font_style);
    00699     FcConfigSubstitute(0, pat, FcMatchPattern);
    00700     FcDefaultSubstitute(pat);
    00701     fs = FcFontSetCreate();
    00702     match = FcFontMatch(0, pat, &result);
    00703
    00704     if (fs != NULL && match != NULL) {
    00705       int i;
    00706       FcChar8 *family;
    00707       FcChar8 *style;
    00708       FcChar8 *file;
    00709       FcFontSetAdd(fs, match);
    00710
    00711       for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
    00712         /* Try the new filename */
    00713         if (FcPatternGetString(fs->fonts[i], FC_FILE,   0, &file)   == FcResultMatch &&
    00714             FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
    00715             FcPatternGetString(fs->fonts[i], FC_STYLE,  0, &style)  == FcResultMatch) {
    00716
    00717           /* The correct style? */
    00718           if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
    00719
    00720           /* Font config takes the best shot, which, if the family name is spelled
    00721            * wrongly a 'random' font, so check whether the family name is the
    00722            * same as the supplied name */
    00723           if (strcasecmp(font_family, (char*)family) == 0) {
    00724             err = FT_New_Face(_library, (char *)file, 0, face);
    00725           }
    00726         }
    00727       }
    00728     }
    00729
    00730     free(font_family);
    00731     FcPatternDestroy(pat);
    00732     FcFontSetDestroy(fs);
    00733     FcFini();
    00734   }
    00735
    00736   return err;
    00737 }
    00738
    00739 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
    00740 {
    00741   if (!FcInit()) return false;
    00742
    00743   bool ret = false;
    00744
    00745   /* Fontconfig doesn't handle full language isocodes, only the part
    00746    * before the _ of e.g. en_GB is used, so "remove" everything after
    00747    * the _. */
    00748   char lang[16];
    00749   seprintf(lang, lastof(lang), ":lang=%s", language_isocode);
    00750   char *split = strchr(lang, '_');
    00751   if (split != NULL) *split = '\0';
    00752
    00753   /* First create a pattern to match the wanted language. */
    00754   FcPattern *pat = FcNameParse((FcChar8*)lang);
    00755   /* We only want to know the filename. */
    00756   FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_SPACING, FC_SLANT, FC_WEIGHT, NULL);
    00757   /* Get the list of filenames matching the wanted language. */
    00758   FcFontSet *fs = FcFontList(NULL, pat, os);
    00759
    00760   /* We don't need these anymore. */
    00761   FcObjectSetDestroy(os);
    00762   FcPatternDestroy(pat);
    00763
    00764   if (fs != NULL) {
    00765     int best_weight = -1;
    00766     const char *best_font = NULL;
    00767
    00768     for (int i = 0; i < fs->nfont; i++) {
    00769       FcPattern *font = fs->fonts[i];
    00770
    00771       FcChar8 *file = NULL;
    00772       FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
    00773       if (res != FcResultMatch || file == NULL) {
    00774         continue;
    00775       }
    00776
    00777       /* Get a font with the right spacing .*/
    00778       int value = 0;
    00779       FcPatternGetInteger(font, FC_SPACING, 0, &value);
    00780       if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
    00781
    00782       /* Do not use those that explicitly say they're slanted. */
    00783       FcPatternGetInteger(font, FC_SLANT, 0, &value);
    00784       if (value != 0) continue;
    00785
    00786       /* We want the fatter font as they look better at small sizes. */
    00787       FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
    00788       if (value <= best_weight) continue;
    00789
    00790       callback->SetFontNames(settings, (const char*)file);
    00791
    00792       bool missing = callback->FindMissingGlyphs(NULL);
    00793       DEBUG(freetype, 1, "Font \"%s\" misses%s glyphs", file, missing ? "" : " no");
    00794
    00795       if (!missing) {
    00796         best_weight = value;
    00797         best_font   = (const char *)file;
    00798       }
    00799     }
    00800
    00801     if (best_font != NULL) {
    00802       ret = true;
    00803       callback->SetFontNames(settings, best_font);
    00804       InitFreeType(callback->Monospace());
    00805     }
    00806
    00807     /* Clean up the list of filenames. */
    00808     FcFontSetDestroy(fs);
    00809   }
    00810
    00811   FcFini();
    00812   return ret;
    00813 }
    00814
    00815 #else /* without WITH_FONTCONFIG */
    00816 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_Cannot_Open_Resource;}
    00817 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
    00818 #endif /* WITH_FONTCONFIG */
    00819
    00820 static void SetFontGeometry(FT_Face face, FontSize size, int pixels)
    00821 {
    00822   if (pixels == 0) {
    00823     /* Try to determine a good height based on the minimal height recommended by the font. */
    00824     pixels = _default_font_height[size];
    00825
    00826     TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(face, ft_sfnt_head);
    00827     if (head != NULL) {
    00828       /* Font height is minimum height plus the difference between the default
    00829        * height for this font size and the small size. */
    00830       int diff = _default_font_height[size] - _default_font_height[FS_SMALL];
    00831       pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[size], MAX_FONT_SIZE);
    00832     }
    00833   }
    00834
    00835   FT_Error err = FT_Set_Pixel_Sizes(face, 0, pixels);
    00836   if (err == FT_Err_Invalid_Pixel_Size) {
    00837
    00838     /* Find nearest size to that requested */
    00839     FT_Bitmap_Size *bs = face->available_sizes;
    00840     int i = face->num_fixed_sizes;
    00841     int n = bs->height;
    00842     for (; --i; bs++) {
    00843       if (abs(pixels - bs->height) < abs(pixels - n)) n = bs->height;
    00844     }
    00845
    00846     FT_Set_Pixel_Sizes(face, 0, n);
    00847   }
    00848
    00849   int asc = face->size->metrics.ascender >> 6;
    00850   int dec = face->size->metrics.descender >> 6;
    00851
    00852   _ascender[size] = asc;
    00853   _font_height[size] = asc - dec;
    00854 }
    00855
    00862 static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
    00863 {
    00864   FT_Error error;
    00865
    00866   if (StrEmpty(font_name)) return;
    00867
    00868   error = FT_New_Face(_library, font_name, 0, face);
    00869
    00870   if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
    00871
    00872   if (error == FT_Err_Ok) {
    00873     DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, (*face)->family_name, (*face)->style_name);
    00874
    00875     /* Attempt to select the unicode character map */
    00876     error = FT_Select_Charmap(*face, ft_encoding_unicode);
    00877     if (error == FT_Err_Ok) return; // Success
    00878
    00879     if (error == FT_Err_Invalid_CharMap_Handle) {
    00880       /* Try to pick a different character map instead. We default to
    00881        * the first map, but platform_id 0 encoding_id 0 should also
    00882        * be unicode (strange system...) */
    00883       FT_CharMap found = (*face)->charmaps[0];
    00884       int i;
    00885
    00886       for (i = 0; i < (*face)->num_charmaps; i++) {
    00887         FT_CharMap charmap = (*face)->charmaps[i];
    00888         if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
    00889           found = charmap;
    00890         }
    00891       }
    00892
    00893       if (found != NULL) {
    00894         error = FT_Set_Charmap(*face, found);
    00895         if (error == FT_Err_Ok) return;
    00896       }
    00897     }
    00898   }
    00899
    00900   FT_Done_Face(*face);
    00901   *face = NULL;
    00902
    00903   ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, type, error);
    00904 }
    00905
    00906
    00907 static void ResetGlyphCache(bool monospace);
    00908
    00913 static void UnloadFace(FT_Face *face)
    00914 {
    00915   if (*face == NULL) return;
    00916
    00917   FT_Done_Face(*face);
    00918   *face = NULL;
    00919 }
    00920
    00925 void InitFreeType(bool monospace)
    00926 {
    00927   ResetFontSizes(monospace);
    00928   ResetGlyphCache(monospace);
    00929
    00930   if (monospace) {
    00931     UnloadFace(&_face_mono);
    00932   } else {
    00933     UnloadFace(&_face_small);
    00934     UnloadFace(&_face_medium);
    00935     UnloadFace(&_face_large);
    00936   }
    00937
    00938   if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font) && StrEmpty(_freetype.mono_font)) {
    00939     DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
    00940     return;
    00941   }
    00942
    00943   if (_library == NULL) {
    00944     if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
    00945       ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
    00946       return;
    00947     }
    00948
    00949     DEBUG(freetype, 2, "Initialized");
    00950   }
    00951
    00952   /* Load each font */
    00953   if (monospace) {
    00954     LoadFreeTypeFont(_freetype.mono_font ,  &_face_mono,   "mono");
    00955
    00956     if (_face_mono != NULL) {
    00957       SetFontGeometry(_face_mono, FS_MONO, _freetype.mono_size);
    00958     }
    00959   } else {
    00960     LoadFreeTypeFont(_freetype.small_font,  &_face_small,  "small");
    00961     LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium");
    00962     LoadFreeTypeFont(_freetype.large_font,  &_face_large,  "large");
    00963
    00964     /* Set each font size */
    00965     if (_face_small != NULL) {
    00966       SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size);
    00967     }
    00968     if (_face_medium != NULL) {
    00969       SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size);
    00970     }
    00971     if (_face_large != NULL) {
    00972       SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size);
    00973     }
    00974   }
    00975 }
    00976
    00980 void UninitFreeType()
    00981 {
    00982   ResetGlyphCache(true);
    00983   ResetGlyphCache(false);
    00984
    00985   UnloadFace(&_face_small);
    00986   UnloadFace(&_face_medium);
    00987   UnloadFace(&_face_large);
    00988   UnloadFace(&_face_mono);
    00989
    00990   FT_Done_FreeType(_library);
    00991   _library = NULL;
    00992 }
    00993
    00997 void ClearFontCache()
    00998 {
    00999   ResetGlyphCache(true);
    01000   ResetGlyphCache(false);
    01001 }
    01002
    01003 static FT_Face GetFontFace(FontSize size)
    01004 {
    01005   switch (size) {
    01006     default: NOT_REACHED();
    01007     case FS_NORMAL: return _face_medium;
    01008     case FS_SMALL:  return _face_small;
    01009     case FS_LARGE:  return _face_large;
    01010     case FS_MONO:   return _face_mono;
    01011   }
    01012 }
    01013
    01014
    01015 struct GlyphEntry {
    01016   Sprite *sprite;
    01017   byte width;
    01018   bool duplicate;
    01019 };
    01020
    01021
    01022 /* The glyph cache. This is structured to reduce memory consumption.
    01023  * 1) There is a 'segment' table for each font size.
    01024  * 2) Each segment table is a discrete block of characters.
    01025  * 3) Each block contains 256 (aligned) characters sequential characters.
    01026  *
    01027  * The cache is accessed in the following way:
    01028  * For character 0x0041  ('A'): _glyph_ptr[FS_NORMAL][0x00][0x41]
    01029  * For character 0x20AC (Euro): _glyph_ptr[FS_NORMAL][0x20][0xAC]
    01030  *
    01031  * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
    01032  * This can be simply changed in the two functions Get & SetGlyphPtr.
    01033  */
    01034 static GlyphEntry **_glyph_ptr[FS_END];
    01035
    01040 static void ResetGlyphCache(bool monospace)
    01041 {
    01042   for (FontSize i = FS_BEGIN; i < FS_END; i++) {
    01043     if (monospace != (i == FS_MONO)) continue;
    01044     if (_glyph_ptr[i] == NULL) continue;
    01045
    01046     for (int j = 0; j < 256; j++) {
    01047       if (_glyph_ptr[i][j] == NULL) continue;
    01048
    01049       for (int k = 0; k < 256; k++) {
    01050         if (_glyph_ptr[i][j][k].duplicate) continue;
    01051         free(_glyph_ptr[i][j][k].sprite);
    01052       }
    01053
    01054       free(_glyph_ptr[i][j]);
    01055     }
    01056
    01057     free(_glyph_ptr[i]);
    01058     _glyph_ptr[i] = NULL;
    01059   }
    01060 }
    01061
    01062 static GlyphEntry *GetGlyphPtr(FontSize size, WChar key)
    01063 {
    01064   if (_glyph_ptr[size] == NULL) return NULL;
    01065   if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) return NULL;
    01066   return &_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)];
    01067 }
    01068
    01069
    01070 static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph, bool duplicate = false)
    01071 {
    01072   if (_glyph_ptr[size] == NULL) {
    01073     DEBUG(freetype, 3, "Allocating root glyph cache for size %u", size);
    01074     _glyph_ptr[size] = CallocT<GlyphEntry*>(256);
    01075   }
    01076
    01077   if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) {
    01078     DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), size);
    01079     _glyph_ptr[size][GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
    01080   }
    01081
    01082   DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, size);
    01083   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite    = glyph->sprite;
    01084   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width     = glyph->width;
    01085   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
    01086 }
    01087
    01088 static void *AllocateFont(size_t size)
    01089 {
    01090   return MallocT<byte>(size);
    01091 }
    01092
    01093
    01094 /* Check if a glyph should be rendered with antialiasing */
    01095 static bool GetFontAAState(FontSize size)
    01096 {
    01097   /* AA is only supported for 32 bpp */
    01098   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
    01099
    01100   switch (size) {
    01101     default: NOT_REACHED();
    01102     case FS_NORMAL: return _freetype.medium_aa;
    01103     case FS_SMALL:  return _freetype.small_aa;
    01104     case FS_LARGE:  return _freetype.large_aa;
    01105     case FS_MONO:   return _freetype.mono_aa;
    01106   }
    01107 }
    01108
    01109
    01110 const Sprite *GetGlyph(FontSize size, WChar key)
    01111 {
    01112   FT_Face face = GetFontFace(size);
    01113   FT_GlyphSlot slot;
    01114   GlyphEntry new_glyph;
    01115   GlyphEntry *glyph;
    01116   SpriteLoader::Sprite sprite;
    01117   int width;
    01118   int height;
    01119   int x;
    01120   int y;
    01121
    01122   assert(IsPrintable(key));
    01123
    01124   /* Bail out if no face loaded, or for our special characters */
    01125   if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
    01126     SpriteID sprite = GetUnicodeGlyph(size, key);
    01127     if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
    01128
    01129     /* Load the sprite if it's known. */
    01130     if (sprite != 0) return GetSprite(sprite, ST_FONT);
    01131
    01132     /* For the 'rare' case there is no font available at all. */
    01133     if (face == NULL) error("No sprite font and no real font either... bailing!");
    01134
    01135     /* Use the '?' from the freetype font. */
    01136     key = '?';
    01137   }
    01138
    01139   /* Check for the glyph in our cache */
    01140   glyph = GetGlyphPtr(size, key);
    01141   if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
    01142
    01143   slot = face->glyph;
    01144
    01145   bool aa = GetFontAAState(size);
    01146
    01147   FT_UInt glyph_index = FT_Get_Char_Index(face, key);
    01148   if (glyph_index == 0) {
    01149     if (key == '?') {
    01150       /* The font misses the '?' character. Use sprite font. */
    01151       SpriteID sprite = GetUnicodeGlyph(size, key);
    01152       Sprite *spr = (Sprite*)GetRawSprite(sprite, ST_FONT, AllocateFont);
    01153       assert(spr != NULL);
    01154       new_glyph.sprite = spr;
    01155       new_glyph.width  = spr->width + (size != FS_NORMAL);
    01156       SetGlyphPtr(size, key, &new_glyph, false);
    01157       return new_glyph.sprite;
    01158     } else {
    01159       /* Use '?' for missing characters. */
    01160       GetGlyph(size, '?');
    01161       glyph = GetGlyphPtr(size, '?');
    01162       SetGlyphPtr(size, key, glyph, true);
    01163       return glyph->sprite;
    01164     }
    01165   }
    01166   FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
    01167   FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
    01168
    01169   /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
    01170   aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
    01171
    01172   /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
    01173   width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
    01174   height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
    01175
    01176   /* Limit glyph size to prevent overflows later on. */
    01177   if (width > 256 || height > 256) usererror("Font glyph is too large");
    01178
    01179   /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
    01180   sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
    01181   sprite.type = ST_FONT;
    01182   sprite.width = width;
    01183   sprite.height = height;
    01184   sprite.x_offs = slot->bitmap_left;
    01185   sprite.y_offs = _ascender[size] - slot->bitmap_top;
    01186
    01187   /* Draw shadow for medium size */
    01188   if (size == FS_NORMAL && !aa) {
    01189     for (y = 0; y < slot->bitmap.rows; y++) {
    01190       for (x = 0; x < slot->bitmap.width; x++) {
    01191         if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
    01192           sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
    01193           sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
    01194         }
    01195       }
    01196     }
    01197   }
    01198
    01199   for (y = 0; y < slot->bitmap.rows; y++) {
    01200     for (x = 0; x < slot->bitmap.width; x++) {
    01201       if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
    01202         sprite.data[x + y * sprite.width].m = FACE_COLOUR;
    01203         sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
    01204       }
    01205     }
    01206   }
    01207
    01208   new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
    01209   new_glyph.width  = slot->advance.x >> 6;
    01210
    01211   SetGlyphPtr(size, key, &new_glyph);
    01212
    01213   return new_glyph.sprite;
    01214 }
    01215
    01216
    01217 bool GetDrawGlyphShadow()
    01218 {
    01219   return GetFontFace(FS_NORMAL) != NULL && GetFontAAState(FS_NORMAL);
    01220 }
    01221
    01222
    01223 uint GetGlyphWidth(FontSize size, WChar key)
    01224 {
    01225   FT_Face face = GetFontFace(size);
    01226   GlyphEntry *glyph;
    01227
    01228   if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
    01229     SpriteID sprite = GetUnicodeGlyph(size, key);
    01230     if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
    01231     return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL && size != FS_MONO) : 0;
    01232   }
    01233
    01234   glyph = GetGlyphPtr(size, key);
    01235   if (glyph == NULL || glyph->sprite == NULL) {
    01236     GetGlyph(size, key);
    01237     glyph = GetGlyphPtr(size, key);
    01238   }
    01239
    01240   return glyph->width;
    01241 }
    01242
    01243
    01244 #endif /* WITH_FREETYPE */
    01245
    01246 /* Sprite based glyph mapping */
    01247
    01248 #include "table/unicode.h"
    01249
    01250 static SpriteID **_unicode_glyph_map[FS_END];
    01251
    01252
    01254 static SpriteID GetFontBase(FontSize size)
    01255 {
    01256   switch (size) {
    01257     default: NOT_REACHED();
    01258     case FS_NORMAL: return SPR_ASCII_SPACE;
    01259     case FS_SMALL:  return SPR_ASCII_SPACE_SMALL;
    01260     case FS_LARGE:  return SPR_ASCII_SPACE_BIG;
    01261     case FS_MONO:   return SPR_ASCII_SPACE;
    01262   }
    01263 }
    01264
    01265
    01266 SpriteID GetUnicodeGlyph(FontSize size, uint32 key)
    01267 {
    01268   if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) return 0;
    01269   return _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)];
    01270 }
    01271
    01272
    01273 void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite)
    01274 {
    01275   if (_unicode_glyph_map[size] == NULL) _unicode_glyph_map[size] = CallocT<SpriteID*>(256);
    01276   if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) _unicode_glyph_map[size][GB(key, 8, 8)] = CallocT<SpriteID>(256);
    01277   _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
    01278 }
    01279
    01280
    01281 void InitializeUnicodeGlyphMap()
    01282 {
    01283   for (FontSize size = FS_BEGIN; size != FS_END; size++) {
    01284     /* Clear out existing glyph map if it exists */
    01285     if (_unicode_glyph_map[size] != NULL) {
    01286       for (uint i = 0; i < 256; i++) {
    01287         free(_unicode_glyph_map[size][i]);
    01288       }
    01289       free(_unicode_glyph_map[size]);
    01290       _unicode_glyph_map[size] = NULL;
    01291     }
    01292
    01293     SpriteID base = GetFontBase(size);
    01294
    01295     for (uint i = ASCII_LETTERSTART; i < 256; i++) {
    01296       SpriteID sprite = base + i - ASCII_LETTERSTART;
    01297       if (!SpriteExists(sprite)) continue;
    01298       SetUnicodeGlyph(size, i, sprite);
    01299       SetUnicodeGlyph(size, i + SCC_SPRITE_START, sprite);
    01300     }
    01301
    01302     for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
    01303       byte key = _default_unicode_map[i].key;
    01304       if (key == CLRA) {
    01305         /* Clear the glyph. This happens if the glyph at this code point
    01306          * is non-standard and should be accessed by an SCC_xxx enum
    01307          * entry only. */
    01308         SetUnicodeGlyph(size, _default_unicode_map[i].code, 0);
    01309       } else {
    01310         SpriteID sprite = base + key - ASCII_LETTERSTART;
    01311         SetUnicodeGlyph(size, _default_unicode_map[i].code, sprite);
    01312       }
    01313     }
    01314   }
    01315 }