fontcache.cpp

Go to the documentation of this file.
00001 /* $Id: fontcache.cpp 16638 2009-06-23 20:48:48Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "spritecache.h"
00007 #include "fontcache.h"
00008 #include "blitter/factory.hpp"
00009 #include "gfx_func.h"
00010 #include "core/alloc_func.hpp"
00011 #include "core/math_func.hpp"
00012 
00013 #include "table/sprites.h"
00014 #include "table/control_codes.h"
00015 
00016 #ifdef WITH_FREETYPE
00017 #include <ft2build.h>
00018 #include FT_FREETYPE_H
00019 #include FT_GLYPH_H
00020 
00021 #ifdef WITH_FONTCONFIG
00022 #include <fontconfig/fontconfig.h>
00023 #endif
00024 
00025 static FT_Library _library = NULL;
00026 static FT_Face _face_small = NULL;
00027 static FT_Face _face_medium = NULL;
00028 static FT_Face _face_large = NULL;
00029 
00030 FreeTypeSettings _freetype;
00031 
00032 enum {
00033   FACE_COLOUR = 1,
00034   SHADOW_COLOUR = 2,
00035 };
00036 
00039 #ifdef WIN32
00040 #include <windows.h>
00041 #include <shlobj.h> /* SHGetFolderPath */
00042 #include "win32.h"
00043 
00054 char *GetShortPath(const char *long_path)
00055 {
00056   static char short_path[MAX_PATH];
00057 #ifdef UNICODE
00058   /* The non-unicode GetShortPath doesn't support UTF-8...,
00059    * so convert the path to wide chars, then get the short
00060    * path and convert it back again. */
00061   wchar_t long_path_w[MAX_PATH];
00062   MultiByteToWideChar(CP_UTF8, 0, long_path, -1, long_path_w, MAX_PATH);
00063 
00064   wchar_t short_path_w[MAX_PATH];
00065   GetShortPathNameW(long_path_w, short_path_w, MAX_PATH);
00066 
00067   WideCharToMultiByte(CP_ACP, 0, short_path_w, -1, short_path, MAX_PATH, NULL, NULL);
00068 #else
00069   /* Technically not needed, but do it for consistency. */
00070   GetShortPathNameA(long_path, short_path, MAX_PATH);
00071 #endif
00072   return short_path;
00073 }
00074 
00075 /* Get the font file to be loaded into Freetype by looping the registry
00076  * location where windows lists all installed fonts. Not very nice, will
00077  * surely break if the registry path changes, but it works. Much better
00078  * solution would be to use CreateFont, and extract the font data from it
00079  * by GetFontData. The problem with this is that the font file needs to be
00080  * kept in memory then until the font is no longer needed. This could mean
00081  * an additional memory usage of 30MB (just for fonts!) when using an eastern
00082  * font for all font sizes */
00083 #define FONT_DIR_NT "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
00084 #define FONT_DIR_9X "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Fonts"
00085 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
00086 {
00087   FT_Error err = FT_Err_Cannot_Open_Resource;
00088   HKEY hKey;
00089   LONG ret;
00090   TCHAR vbuffer[MAX_PATH], dbuffer[256];
00091   TCHAR *font_namep;
00092   char *font_path;
00093   uint index;
00094 
00095   /* On windows NT (2000, NT3.5, XP, etc.) the fonts are stored in the
00096    * "Windows NT" key, on Windows 9x in the Windows key. To save us having
00097    * to retrieve the windows version, we'll just query both */
00098   ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_NT), 0, KEY_READ, &hKey);
00099   if (ret != ERROR_SUCCESS) ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_9X), 0, KEY_READ, &hKey);
00100 
00101   if (ret != ERROR_SUCCESS) {
00102     DEBUG(freetype, 0, "Cannot open registry key HKLM\\SOFTWARE\\Microsoft\\Windows (NT)\\CurrentVersion\\Fonts");
00103     return err;
00104   }
00105 
00106   /* For Unicode we need some conversion between widechar and
00107    * normal char to match the data returned by RegEnumValue,
00108    * otherwise just use parameter */
00109 #if defined(UNICODE)
00110   font_namep = MallocT<TCHAR>(MAX_PATH);
00111   MB_TO_WIDE_BUFFER(font_name, font_namep, MAX_PATH * sizeof(TCHAR));
00112 #else
00113   font_namep = (char*)font_name; // only cast because in unicode pointer is not const
00114 #endif
00115 
00116   for (index = 0;; index++) {
00117     TCHAR *s;
00118     DWORD vbuflen = lengthof(vbuffer);
00119     DWORD dbuflen = lengthof(dbuffer);
00120 
00121     ret = RegEnumValue(hKey, index, vbuffer, &vbuflen, NULL, NULL, (byte*)dbuffer, &dbuflen);
00122     if (ret != ERROR_SUCCESS) goto registry_no_font_found;
00123 
00124     /* The font names in the registry are of the following 3 forms:
00125      * - ADMUI3.fon
00126      * - Book Antiqua Bold (TrueType)
00127      * - Batang & BatangChe & Gungsuh & GungsuhChe (TrueType)
00128      * We will strip the font-type '()' if any and work with the font name
00129      * itself, which must match exactly; if...
00130      * TTC files, font files which contain more than one font are seperated
00131      * byt '&'. Our best bet will be to do substr match for the fontname
00132      * and then let FreeType figure out which index to load */
00133     s = _tcschr(vbuffer, _T('('));
00134     if (s != NULL) s[-1] = '\0';
00135 
00136     if (_tcschr(vbuffer, _T('&')) == NULL) {
00137       if (_tcsicmp(vbuffer, font_namep) == 0) break;
00138     } else {
00139       if (_tcsstr(vbuffer, font_namep) != NULL) break;
00140     }
00141   }
00142 
00143   if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, vbuffer))) {
00144     DEBUG(freetype, 0, "SHGetFolderPath cannot return fonts directory");
00145     goto folder_error;
00146   }
00147 
00148   /* Some fonts are contained in .ttc files, TrueType Collection fonts. These
00149    * contain multiple fonts inside this single file. GetFontData however
00150    * returns the whole file, so we need to check each font inside to get the
00151    * proper font.
00152    * Also note that FreeType does not support UNICODE filesnames! */
00153 #if defined(UNICODE)
00154   /* We need a cast here back from wide because FreeType doesn't support
00155    * widechar filenames. Just use the buffer we allocated before for the
00156    * font_name search */
00157   font_path = (char*)font_namep;
00158   WIDE_TO_MB_BUFFER(vbuffer, font_path, MAX_PATH * sizeof(TCHAR));
00159 #else
00160   font_path = vbuffer;
00161 #endif
00162 
00163   ttd_strlcat(font_path, "\\", MAX_PATH * sizeof(TCHAR));
00164   ttd_strlcat(font_path, WIDE_TO_MB(dbuffer), MAX_PATH * sizeof(TCHAR));
00165 
00166   /* Convert the path into something that FreeType understands */
00167   font_path = GetShortPath(font_path);
00168 
00169   index = 0;
00170   do {
00171     err = FT_New_Face(_library, font_path, index, face);
00172     if (err != FT_Err_Ok) break;
00173 
00174     if (strncasecmp(font_name, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
00175     /* Try english name if font name failed */
00176     if (strncasecmp(font_name + strlen(font_name) + 1, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
00177     err = FT_Err_Cannot_Open_Resource;
00178 
00179   } while ((FT_Long)++index != (*face)->num_faces);
00180 
00181 
00182 folder_error:
00183 registry_no_font_found:
00184 #if defined(UNICODE)
00185   free(font_namep);
00186 #endif
00187   RegCloseKey(hKey);
00188   return err;
00189 }
00190 
00204 static const char *GetEnglishFontName(const ENUMLOGFONTEX *logfont)
00205 {
00206   static char font_name[MAX_PATH];
00207   const char *ret_font_name = NULL;
00208   uint pos = 0;
00209   HDC dc;
00210   HGDIOBJ oldfont;
00211   byte *buf;
00212   DWORD dw;
00213   uint16 format, count, stringOffset, platformId, encodingId, languageId, nameId, length, offset;
00214 
00215   HFONT font = CreateFontIndirect(&logfont->elfLogFont);
00216   if (font == NULL) goto err1;
00217 
00218   dc = GetDC(NULL);
00219   oldfont = SelectObject(dc, font);
00220   dw = GetFontData(dc, 'eman', 0, NULL, 0);
00221   if (dw == GDI_ERROR) goto err2;
00222 
00223   buf = MallocT<byte>(dw);
00224   dw = GetFontData(dc, 'eman', 0, buf, dw);
00225   if (dw == GDI_ERROR) goto err3;
00226 
00227   format = buf[pos++] << 8;
00228   format += buf[pos++];
00229   assert(format == 0);
00230   count = buf[pos++] << 8;
00231   count += buf[pos++];
00232   stringOffset = buf[pos++] << 8;
00233   stringOffset += buf[pos++];
00234   for (uint i = 0; i < count; i++) {
00235     platformId = buf[pos++] << 8;
00236     platformId += buf[pos++];
00237     encodingId = buf[pos++] << 8;
00238     encodingId += buf[pos++];
00239     languageId = buf[pos++] << 8;
00240     languageId += buf[pos++];
00241     nameId = buf[pos++] << 8;
00242     nameId += buf[pos++];
00243     if (nameId != 1) {
00244       pos += 4; // skip length and offset
00245       continue;
00246     }
00247     length = buf[pos++] << 8;
00248     length += buf[pos++];
00249     offset = buf[pos++] << 8;
00250     offset += buf[pos++];
00251 
00252     /* Don't buffer overflow */
00253     length = min(length, MAX_PATH - 1);
00254     for (uint j = 0; j < length; j++) font_name[j] = buf[stringOffset + offset + j];
00255     font_name[length] = '\0';
00256 
00257     if ((platformId == 1 && languageId == 0) ||      // Macintosh English
00258       (platformId == 3 && languageId == 0x0409)) { // Microsoft English (US)
00259       ret_font_name = font_name;
00260       break;
00261     }
00262   }
00263 
00264 err3:
00265   free(buf);
00266 err2:
00267   SelectObject(dc, oldfont);
00268   ReleaseDC(NULL, dc);
00269 err1:
00270   DeleteObject(font);
00271 
00272   return ret_font_name == NULL ? WIDE_TO_MB((const TCHAR*)logfont->elfFullName) : ret_font_name;
00273 }
00274 
00275 struct EFCParam {
00276   FreeTypeSettings *settings;
00277   LOCALESIGNATURE  locale;
00278 };
00279 
00280 static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
00281 {
00282   EFCParam *info = (EFCParam *)lParam;
00283 
00284   /* Only use TrueType fonts */
00285   if (!(type & TRUETYPE_FONTTYPE)) return 1;
00286   /* Don't use SYMBOL fonts */
00287   if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
00288 
00289   /* The font has to have at least one of the supported locales to be usable. */
00290   if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
00291     /* On win9x metric->ntmFontSig seems to contain garbage. */
00292     FONTSIGNATURE fs;
00293     memset(&fs, 0, sizeof(fs));
00294     HFONT font = CreateFontIndirect(&logfont->elfLogFont);
00295     if (font != NULL) {
00296       HDC dc = GetDC(NULL);
00297       HGDIOBJ oldfont = SelectObject(dc, font);
00298       GetTextCharsetInfo(dc, &fs, 0);
00299       SelectObject(dc, oldfont);
00300       ReleaseDC(NULL, dc);
00301       DeleteObject(font);
00302     }
00303     if ((fs.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (fs.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) return 1;
00304   }
00305 
00306   const char *english_name = GetEnglishFontName(logfont);
00307   const char *font_name = WIDE_TO_MB((const TCHAR*)logfont->elfFullName);
00308   DEBUG(freetype, 1, "Fallback font: %s (%s)", font_name, english_name);
00309 
00310   strecpy(info->settings->small_font,  font_name, lastof(info->settings->small_font));
00311   strecpy(info->settings->medium_font, font_name, lastof(info->settings->medium_font));
00312   strecpy(info->settings->large_font,  font_name, lastof(info->settings->large_font));
00313 
00314   /* Add english name after font name */
00315   strecpy(info->settings->small_font + strlen(info->settings->small_font) + 1, english_name, lastof(info->settings->small_font));
00316   strecpy(info->settings->medium_font + strlen(info->settings->medium_font) + 1, english_name, lastof(info->settings->medium_font));
00317   strecpy(info->settings->large_font + strlen(info->settings->large_font) + 1, english_name, lastof(info->settings->large_font));
00318   return 0; // stop enumerating
00319 }
00320 
00321 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid)
00322 {
00323   EFCParam langInfo;
00324   if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale, sizeof(langInfo.locale) / sizeof(TCHAR)) == 0) {
00325     /* Invalid langid or some other mysterious error, can't determine fallback font. */
00326     DEBUG(freetype, 1, "Can't get locale info for fallback font (langid=0x%x)", winlangid);
00327     return false;
00328   }
00329   langInfo.settings = settings;
00330 
00331   LOGFONT font;
00332   /* Enumerate all fonts. */
00333   font.lfCharSet = DEFAULT_CHARSET;
00334   font.lfFaceName[0] = '\0';
00335   font.lfPitchAndFamily = 0;
00336 
00337   HDC dc = GetDC(NULL);
00338   int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
00339   ReleaseDC(NULL, dc);
00340   return ret == 0;
00341 }
00342 
00343 #elif defined(WITH_FONTCONFIG)
00344 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
00345 {
00346   FT_Error err = FT_Err_Cannot_Open_Resource;
00347 
00348   if (!FcInit()) {
00349     ShowInfoF("Unable to load font configuration");
00350   } else {
00351     FcPattern *match;
00352     FcPattern *pat;
00353     FcFontSet *fs;
00354     FcResult  result;
00355     char *font_style;
00356     char *font_family;
00357 
00358     /* Split & strip the font's style */
00359     font_family = strdup(font_name);
00360     font_style = strchr(font_family, ',');
00361     if (font_style != NULL) {
00362       font_style[0] = '\0';
00363       font_style++;
00364       while (*font_style == ' ' || *font_style == '\t') font_style++;
00365     }
00366 
00367     /* Resolve the name and populate the information structure */
00368     pat = FcNameParse((FcChar8*)font_family);
00369     if (font_style != NULL) FcPatternAddString(pat, FC_STYLE, (FcChar8*)font_style);
00370     FcConfigSubstitute(0, pat, FcMatchPattern);
00371     FcDefaultSubstitute(pat);
00372     fs = FcFontSetCreate();
00373     match = FcFontMatch(0, pat, &result);
00374 
00375     if (fs != NULL && match != NULL) {
00376       int i;
00377       FcChar8 *family;
00378       FcChar8 *style;
00379       FcChar8 *file;
00380       FcFontSetAdd(fs, match);
00381 
00382       for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
00383         /* Try the new filename */
00384         if (FcPatternGetString(fs->fonts[i], FC_FILE,   0, &file)   == FcResultMatch &&
00385             FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
00386             FcPatternGetString(fs->fonts[i], FC_STYLE,  0, &style)  == FcResultMatch) {
00387 
00388           /* The correct style? */
00389           if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
00390 
00391           /* Font config takes the best shot, which, if the family name is spelled
00392            * wrongly a 'random' font, so check whether the family name is the
00393            * same as the supplied name */
00394           if (strcasecmp(font_family, (char*)family) == 0) {
00395             err = FT_New_Face(_library, (char *)file, 0, face);
00396           }
00397         }
00398       }
00399     }
00400 
00401     free(font_family);
00402     FcPatternDestroy(pat);
00403     FcFontSetDestroy(fs);
00404     FcFini();
00405   }
00406 
00407   return err;
00408 }
00409 
00410 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid)
00411 {
00412   if (!FcInit()) return false;
00413 
00414   bool ret = false;
00415 
00416   /* Fontconfig doesn't handle full language isocodes, only the part
00417    * before the _ of e.g. en_GB is used, so "remove" everything after
00418    * the _. */
00419   char lang[16];
00420   strecpy(lang, language_isocode, lastof(lang));
00421   char *split = strchr(lang, '_');
00422   if (split != NULL) *split = '\0';
00423 
00424   FcPattern *pat;
00425   FcPattern *match;
00426   FcResult result;
00427   FcChar8 *file;
00428   FcFontSet *fs;
00429   FcValue val;
00430   val.type = FcTypeString;
00431   val.u.s = (FcChar8*)lang;
00432 
00433   /* First create a pattern to match the wanted language */
00434   pat = FcPatternCreate();
00435   /* And fill it with the language and other defaults */
00436   if (pat == NULL ||
00437       !FcPatternAdd(pat, "lang", val, false) ||
00438       !FcConfigSubstitute(0, pat, FcMatchPattern)) {
00439     goto error_pattern;
00440   }
00441 
00442   FcDefaultSubstitute(pat);
00443 
00444   /* Then create a font set and match that */
00445   match = FcFontMatch(0, pat, &result);
00446 
00447   if (match == NULL) {
00448     goto error_pattern;
00449   }
00450 
00451   /* Find all fonts that do match */
00452   fs = FcFontSetCreate();
00453   FcFontSetAdd(fs, match);
00454 
00455   /* And take the first, if it exists */
00456   if (fs->nfont <= 0 || FcPatternGetString(fs->fonts[0], FC_FILE, 0, &file)) {
00457     goto error_fontset;
00458   }
00459 
00460   strecpy(settings->small_font,  (const char*)file, lastof(settings->small_font));
00461   strecpy(settings->medium_font, (const char*)file, lastof(settings->medium_font));
00462   strecpy(settings->large_font,  (const char*)file, lastof(settings->large_font));
00463 
00464   ret = true;
00465 
00466 error_fontset:
00467   FcFontSetDestroy(fs);
00468 error_pattern:
00469   if (pat != NULL) FcPatternDestroy(pat);
00470   FcFini();
00471   return ret;
00472 }
00473 
00474 #else /* without WITH_FONTCONFIG */
00475 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_Cannot_Open_Resource;}
00476 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid) { return false; }
00477 #endif /* WITH_FONTCONFIG */
00478 
00485 static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
00486 {
00487   FT_Error error;
00488 
00489   if (StrEmpty(font_name)) return;
00490 
00491   error = FT_New_Face(_library, font_name, 0, face);
00492 
00493   if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
00494 
00495   if (error == FT_Err_Ok) {
00496     DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, (*face)->family_name, (*face)->style_name);
00497 
00498     /* Attempt to select the unicode character map */
00499     error = FT_Select_Charmap(*face, ft_encoding_unicode);
00500     if (error == FT_Err_Ok) return; // Success
00501 
00502     if (error == FT_Err_Invalid_CharMap_Handle) {
00503       /* Try to pick a different character map instead. We default to
00504        * the first map, but platform_id 0 encoding_id 0 should also
00505        * be unicode (strange system...) */
00506       FT_CharMap found = (*face)->charmaps[0];
00507       int i;
00508 
00509       for (i = 0; i < (*face)->num_charmaps; i++) {
00510         FT_CharMap charmap = (*face)->charmaps[i];
00511         if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
00512           found = charmap;
00513         }
00514       }
00515 
00516       if (found != NULL) {
00517         error = FT_Set_Charmap(*face, found);
00518         if (error == FT_Err_Ok) return;
00519       }
00520     }
00521   }
00522 
00523   FT_Done_Face(*face);
00524   *face = NULL;
00525 
00526   ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, type, error);
00527 }
00528 
00529 
00530 void InitFreeType()
00531 {
00532   if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font)) {
00533     DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
00534     return;
00535   }
00536 
00537   if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
00538     ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
00539     return;
00540   }
00541 
00542   DEBUG(freetype, 2, "Initialized");
00543 
00544   /* Load each font */
00545   LoadFreeTypeFont(_freetype.small_font,  &_face_small,  "small");
00546   LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium");
00547   LoadFreeTypeFont(_freetype.large_font,  &_face_large,  "large");
00548 
00549   /* Set each font size */
00550   if (_face_small  != NULL) FT_Set_Pixel_Sizes(_face_small,  0, _freetype.small_size);
00551   if (_face_medium != NULL) FT_Set_Pixel_Sizes(_face_medium, 0, _freetype.medium_size);
00552   if (_face_large  != NULL) FT_Set_Pixel_Sizes(_face_large,  0, _freetype.large_size);
00553 }
00554 
00555 static void ResetGlyphCache();
00556 
00561 static void UnloadFace(FT_Face *face)
00562 {
00563   if (*face == NULL) return;
00564 
00565   FT_Done_Face(*face);
00566   *face = NULL;
00567 }
00568 
00572 void UninitFreeType()
00573 {
00574   ResetGlyphCache();
00575 
00576   UnloadFace(&_face_small);
00577   UnloadFace(&_face_medium);
00578   UnloadFace(&_face_large);
00579 
00580   FT_Done_FreeType(_library);
00581   _library = NULL;
00582 }
00583 
00584 
00585 static FT_Face GetFontFace(FontSize size)
00586 {
00587   switch (size) {
00588     default: NOT_REACHED();
00589     case FS_NORMAL: return _face_medium;
00590     case FS_SMALL:  return _face_small;
00591     case FS_LARGE:  return _face_large;
00592   }
00593 }
00594 
00595 
00596 struct GlyphEntry {
00597   Sprite *sprite;
00598   byte width;
00599 };
00600 
00601 
00602 /* The glyph cache. This is structured to reduce memory consumption.
00603  * 1) There is a 'segment' table for each font size.
00604  * 2) Each segment table is a discrete block of characters.
00605  * 3) Each block contains 256 (aligned) characters sequential characters.
00606  *
00607  * The cache is accessed in the following way:
00608  * For character 0x0041  ('A'): _glyph_ptr[FS_NORMAL][0x00][0x41]
00609  * For character 0x20AC (Euro): _glyph_ptr[FS_NORMAL][0x20][0xAC]
00610  *
00611  * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
00612  * This can be simply changed in the two functions Get & SetGlyphPtr.
00613  */
00614 static GlyphEntry **_glyph_ptr[FS_END];
00615 
00617 static void ResetGlyphCache()
00618 {
00619   for (int i = 0; i < FS_END; i++) {
00620     if (_glyph_ptr[i] == NULL) continue;
00621 
00622     for (int j = 0; j < 256; j++) {
00623       if (_glyph_ptr[i][j] == NULL) continue;
00624 
00625       for (int k = 0; k < 256; k++) {
00626         if (_glyph_ptr[i][j][k].sprite == NULL) continue;
00627         free(_glyph_ptr[i][j][k].sprite);
00628       }
00629 
00630       free(_glyph_ptr[i][j]);
00631     }
00632 
00633     free(_glyph_ptr[i]);
00634     _glyph_ptr[i] = NULL;
00635   }
00636 }
00637 
00638 static GlyphEntry *GetGlyphPtr(FontSize size, WChar key)
00639 {
00640   if (_glyph_ptr[size] == NULL) return NULL;
00641   if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) return NULL;
00642   return &_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)];
00643 }
00644 
00645 
00646 static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph)
00647 {
00648   if (_glyph_ptr[size] == NULL) {
00649     DEBUG(freetype, 3, "Allocating root glyph cache for size %u", size);
00650     _glyph_ptr[size] = CallocT<GlyphEntry*>(256);
00651   }
00652 
00653   if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) {
00654     DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), size);
00655     _glyph_ptr[size][GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
00656   }
00657 
00658   DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, size);
00659   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
00660   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width  = glyph->width;
00661 }
00662 
00663 void *AllocateFont(size_t size)
00664 {
00665   return MallocT<byte>(size);
00666 }
00667 
00668 
00669 /* Check if a glyph should be rendered with antialiasing */
00670 static bool GetFontAAState(FontSize size)
00671 {
00672   /* AA is only supported for 32 bpp */
00673   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
00674 
00675   switch (size) {
00676     default: NOT_REACHED();
00677     case FS_NORMAL: return _freetype.medium_aa;
00678     case FS_SMALL:  return _freetype.small_aa;
00679     case FS_LARGE:  return _freetype.large_aa;
00680   }
00681 }
00682 
00683 
00684 const Sprite *GetGlyph(FontSize size, WChar key)
00685 {
00686   FT_Face face = GetFontFace(size);
00687   FT_GlyphSlot slot;
00688   GlyphEntry new_glyph;
00689   GlyphEntry *glyph;
00690   SpriteLoader::Sprite sprite;
00691   int width;
00692   int height;
00693   int x;
00694   int y;
00695   int y_adj;
00696 
00697   assert(IsPrintable(key));
00698 
00699   /* Bail out if no face loaded, or for our special characters */
00700   if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
00701     SpriteID sprite = GetUnicodeGlyph(size, key);
00702     if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
00703     return GetSprite(sprite, ST_FONT);
00704   }
00705 
00706   /* Check for the glyph in our cache */
00707   glyph = GetGlyphPtr(size, key);
00708   if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
00709 
00710   slot = face->glyph;
00711 
00712   bool aa = GetFontAAState(size);
00713 
00714   FT_Load_Char(face, key, FT_LOAD_DEFAULT);
00715   FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
00716 
00717   /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
00718   aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
00719 
00720   /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
00721   width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
00722   height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
00723 
00724   /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
00725   sprite.AllocateData(width * height);
00726   sprite.width = width;
00727   sprite.height = height;
00728   sprite.x_offs = slot->bitmap_left;
00729   /* XXX 2 should be determined somehow... it's right for the normal face */
00730   y_adj = (size == FS_NORMAL) ? 2 : 0;
00731   sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
00732 
00733   /* Draw shadow for medium size */
00734   if (size == FS_NORMAL) {
00735     for (y = 0; y < slot->bitmap.rows; y++) {
00736       for (x = 0; x < slot->bitmap.width; x++) {
00737         if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
00738           sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
00739           sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
00740         }
00741       }
00742     }
00743   }
00744 
00745   for (y = 0; y < slot->bitmap.rows; y++) {
00746     for (x = 0; x < slot->bitmap.width; x++) {
00747       if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
00748         sprite.data[x + y * sprite.width].m = FACE_COLOUR;
00749         sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
00750       }
00751     }
00752   }
00753 
00754   new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
00755   new_glyph.width  = (slot->advance.x >> 6) + (size != FS_NORMAL);
00756 
00757   SetGlyphPtr(size, key, &new_glyph);
00758 
00759   return new_glyph.sprite;
00760 }
00761 
00762 
00763 uint GetGlyphWidth(FontSize size, WChar key)
00764 {
00765   FT_Face face = GetFontFace(size);
00766   GlyphEntry *glyph;
00767 
00768   if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
00769     SpriteID sprite = GetUnicodeGlyph(size, key);
00770     if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
00771     return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL) : 0;
00772   }
00773 
00774   glyph = GetGlyphPtr(size, key);
00775   if (glyph == NULL || glyph->sprite == NULL) {
00776     GetGlyph(size, key);
00777     glyph = GetGlyphPtr(size, key);
00778   }
00779 
00780   return glyph->width;
00781 }
00782 
00783 
00784 #endif /* WITH_FREETYPE */
00785 
00786 /* Sprite based glyph mapping */
00787 
00788 #include "table/unicode.h"
00789 
00790 static SpriteID **_unicode_glyph_map[FS_END];
00791 
00792 
00794 static SpriteID GetFontBase(FontSize size)
00795 {
00796   switch (size) {
00797     default: NOT_REACHED();
00798     case FS_NORMAL: return SPR_ASCII_SPACE;
00799     case FS_SMALL:  return SPR_ASCII_SPACE_SMALL;
00800     case FS_LARGE:  return SPR_ASCII_SPACE_BIG;
00801   }
00802 }
00803 
00804 
00805 SpriteID GetUnicodeGlyph(FontSize size, uint32 key)
00806 {
00807   if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) return 0;
00808   return _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)];
00809 }
00810 
00811 
00812 void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite)
00813 {
00814   if (_unicode_glyph_map[size] == NULL) _unicode_glyph_map[size] = CallocT<SpriteID*>(256);
00815   if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) _unicode_glyph_map[size][GB(key, 8, 8)] = CallocT<SpriteID>(256);
00816   _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
00817 }
00818 
00819 
00820 void InitializeUnicodeGlyphMap()
00821 {
00822   for (FontSize size = FS_NORMAL; size != FS_END; size++) {
00823     /* Clear out existing glyph map if it exists */
00824     if (_unicode_glyph_map[size] != NULL) {
00825       for (uint i = 0; i < 256; i++) {
00826         if (_unicode_glyph_map[size][i] != NULL) free(_unicode_glyph_map[size][i]);
00827       }
00828       free(_unicode_glyph_map[size]);
00829       _unicode_glyph_map[size] = NULL;
00830     }
00831 
00832     SpriteID base = GetFontBase(size);
00833 
00834     for (uint i = ASCII_LETTERSTART; i < 256; i++) {
00835       SpriteID sprite = base + i - ASCII_LETTERSTART;
00836       if (!SpriteExists(sprite)) continue;
00837       SetUnicodeGlyph(size, i, sprite);
00838       SetUnicodeGlyph(size, i + SCC_SPRITE_START, sprite);
00839     }
00840 
00841     for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
00842       byte key = _default_unicode_map[i].key;
00843       if (key == CLRA || key == CLRL) {
00844         /* Clear the glyph. This happens if the glyph at this code point
00845          * is non-standard and should be accessed by an SCC_xxx enum
00846          * entry only. */
00847         if (key == CLRA || size == FS_LARGE) {
00848           SetUnicodeGlyph(size, _default_unicode_map[i].code, 0);
00849         }
00850       } else {
00851         SpriteID sprite = base + key - ASCII_LETTERSTART;
00852         SetUnicodeGlyph(size, _default_unicode_map[i].code, sprite);
00853       }
00854     }
00855   }
00856 }

Generated on Tue Dec 1 00:06:15 2009 for OpenTTD by  doxygen 1.5.6