fontcache.cpp

Go to the documentation of this file.
00001 /* $Id: fontcache.cpp 15718 2009-03-15 00:32:18Z 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     err = FT_Err_Cannot_Open_Resource;
00176 
00177   } while ((FT_Long)++index != (*face)->num_faces);
00178 
00179 
00180 folder_error:
00181 registry_no_font_found:
00182 #if defined(UNICODE)
00183   free(font_namep);
00184 #endif
00185   RegCloseKey(hKey);
00186   return err;
00187 }
00188 
00202 static const char *GetEnglishFontName(const ENUMLOGFONTEX *logfont)
00203 {
00204   static char font_name[MAX_PATH];
00205   const char *ret_font_name = NULL;
00206   uint pos = 0;
00207 
00208   HFONT font = CreateFontIndirect(&logfont->elfLogFont);
00209   if (font == NULL) goto err1;
00210 
00211   HDC dc = GetDC(NULL);
00212   HGDIOBJ oldfont = SelectObject(dc, font);
00213   DWORD dw = GetFontData(dc, 'eman', 0, NULL, 0);
00214   if (dw == GDI_ERROR) goto err2;
00215 
00216   byte *buf = MallocT<byte>(dw);
00217   dw = GetFontData(dc, 'eman', 0, buf, dw);
00218   if (dw == GDI_ERROR) goto err3;
00219 
00220   uint16 format = buf[pos++] << 8;
00221   format += buf[pos++];
00222   assert(format == 0);
00223   uint16 count = buf[pos++] << 8;
00224   count += buf[pos++];
00225   uint16 stringOffset = buf[pos++] << 8;
00226   stringOffset += buf[pos++];
00227   for (uint i = 0; i < count; i++) {
00228     uint16 platformId = buf[pos++] << 8;
00229     platformId += buf[pos++];
00230     uint16 encodingId = buf[pos++] << 8;
00231     encodingId += buf[pos++];
00232     uint16 languageId = buf[pos++] << 8;
00233     languageId += buf[pos++];
00234     uint16 nameId = buf[pos++] << 8;
00235     nameId += buf[pos++];
00236     if (nameId != 1) {
00237       pos += 4; // skip length and offset
00238       continue;
00239     }
00240     uint16 length = buf[pos++] << 8;
00241     length += buf[pos++];
00242     uint16 offset = buf[pos++] << 8;
00243     offset += buf[pos++];
00244 
00245     /* Don't buffer overflow */
00246     length = min(length, MAX_PATH - 1);
00247     for (uint j = 0; j < length; j++) font_name[j] = buf[stringOffset + offset + j];
00248     font_name[length] = '\0';
00249 
00250     if ((platformId == 1 && languageId == 0) ||      // Macintosh English
00251       (platformId == 3 && languageId == 0x0409)) { // Microsoft English (US)
00252       ret_font_name = font_name;
00253       break;
00254     }
00255   }
00256 
00257 err3:
00258   free(buf);
00259 err2:
00260   SelectObject(dc, oldfont);
00261   ReleaseDC(NULL, dc);
00262 err1:
00263   DeleteObject(font);
00264 
00265   return ret_font_name == NULL ? WIDE_TO_MB((const TCHAR*)logfont->elfFullName) : ret_font_name;
00266 }
00267 
00268 struct EFCParam {
00269   FreeTypeSettings *settings;
00270   LOCALESIGNATURE  locale;
00271 };
00272 
00273 static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
00274 {
00275   EFCParam *info = (EFCParam *)lParam;
00276 
00277   /* Only use TrueType fonts */
00278   if (!(type & TRUETYPE_FONTTYPE)) return 1;
00279   /* Don't use SYMBOL fonts */
00280   if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
00281 
00282   /* The font has to have at least one of the supported locales to be usable. */
00283   if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
00284     /* On win9x metric->ntmFontSig seems to contain garbage. */
00285     FONTSIGNATURE fs;
00286     memset(&fs, 0, sizeof(fs));
00287     HFONT font = CreateFontIndirect(&logfont->elfLogFont);
00288     if (font != NULL) {
00289       HDC dc = GetDC(NULL);
00290       HGDIOBJ oldfont = SelectObject(dc, font);
00291       GetTextCharsetInfo(dc, &fs, 0);
00292       SelectObject(dc, oldfont);
00293       ReleaseDC(NULL, dc);
00294       DeleteObject(font);
00295     }
00296     if ((fs.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (fs.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) return 1;
00297   }
00298 
00299   const char *font_name = GetEnglishFontName(logfont);
00300   DEBUG(freetype, 1, "Fallback font: %s", font_name);
00301 
00302   strecpy(info->settings->small_font,  font_name, lastof(info->settings->small_font));
00303   strecpy(info->settings->medium_font, font_name, lastof(info->settings->medium_font));
00304   strecpy(info->settings->large_font,  font_name, lastof(info->settings->large_font));
00305   return 0; // stop enumerating
00306 }
00307 
00308 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid)
00309 {
00310   EFCParam langInfo;
00311   if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale, sizeof(langInfo.locale) / sizeof(TCHAR)) == 0) {
00312     /* Invalid langid or some other mysterious error, can't determine fallback font. */
00313     DEBUG(freetype, 1, "Can't get locale info for fallback font (langid=0x%x)", winlangid);
00314     return false;
00315   }
00316   langInfo.settings = settings;
00317 
00318   LOGFONT font;
00319   /* Enumerate all fonts. */
00320   font.lfCharSet = DEFAULT_CHARSET;
00321   font.lfFaceName[0] = '\0';
00322   font.lfPitchAndFamily = 0;
00323 
00324   HDC dc = GetDC(NULL);
00325   int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
00326   ReleaseDC(NULL, dc);
00327   return ret == 0;
00328 }
00329 
00330 #elif defined(WITH_FONTCONFIG)
00331 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
00332 {
00333   FT_Error err = FT_Err_Cannot_Open_Resource;
00334 
00335   if (!FcInit()) {
00336     ShowInfoF("Unable to load font configuration");
00337   } else {
00338     FcPattern *match;
00339     FcPattern *pat;
00340     FcFontSet *fs;
00341     FcResult  result;
00342     char *font_style;
00343     char *font_family;
00344 
00345     /* Split & strip the font's style */
00346     font_family = strdup(font_name);
00347     font_style = strchr(font_family, ',');
00348     if (font_style != NULL) {
00349       font_style[0] = '\0';
00350       font_style++;
00351       while (*font_style == ' ' || *font_style == '\t') font_style++;
00352     }
00353 
00354     /* Resolve the name and populate the information structure */
00355     pat = FcNameParse((FcChar8*)font_family);
00356     if (font_style != NULL) FcPatternAddString(pat, FC_STYLE, (FcChar8*)font_style);
00357     FcConfigSubstitute(0, pat, FcMatchPattern);
00358     FcDefaultSubstitute(pat);
00359     fs = FcFontSetCreate();
00360     match = FcFontMatch(0, pat, &result);
00361 
00362     if (fs != NULL && match != NULL) {
00363       int i;
00364       FcChar8 *family;
00365       FcChar8 *style;
00366       FcChar8 *file;
00367       FcFontSetAdd(fs, match);
00368 
00369       for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
00370         /* Try the new filename */
00371         if (FcPatternGetString(fs->fonts[i], FC_FILE,   0, &file)   == FcResultMatch &&
00372             FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
00373             FcPatternGetString(fs->fonts[i], FC_STYLE,  0, &style)  == FcResultMatch) {
00374 
00375           /* The correct style? */
00376           if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
00377 
00378           /* Font config takes the best shot, which, if the family name is spelled
00379            * wrongly a 'random' font, so check whether the family name is the
00380            * same as the supplied name */
00381           if (strcasecmp(font_family, (char*)family) == 0) {
00382             err = FT_New_Face(_library, (char *)file, 0, face);
00383           }
00384         }
00385       }
00386     }
00387 
00388     free(font_family);
00389     FcPatternDestroy(pat);
00390     FcFontSetDestroy(fs);
00391     FcFini();
00392   }
00393 
00394   return err;
00395 }
00396 
00397 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid)
00398 {
00399   if (!FcInit()) return false;
00400 
00401   bool ret = false;
00402 
00403   /* Fontconfig doesn't handle full language isocodes, only the part
00404    * before the _ of e.g. en_GB is used, so "remove" everything after
00405    * the _. */
00406   char lang[16];
00407   strecpy(lang, language_isocode, lastof(lang));
00408   char *split = strchr(lang, '_');
00409   if (split != NULL) *split = '\0';
00410 
00411   FcPattern *pat;
00412   FcPattern *match;
00413   FcResult result;
00414   FcChar8 *file;
00415   FcFontSet *fs;
00416   FcValue val;
00417   val.type = FcTypeString;
00418   val.u.s = (FcChar8*)lang;
00419 
00420   /* First create a pattern to match the wanted language */
00421   pat = FcPatternCreate();
00422   /* And fill it with the language and other defaults */
00423   if (pat == NULL ||
00424       !FcPatternAdd(pat, "lang", val, false) ||
00425       !FcConfigSubstitute(0, pat, FcMatchPattern)) {
00426     goto error_pattern;
00427   }
00428 
00429   FcDefaultSubstitute(pat);
00430 
00431   /* Then create a font set and match that */
00432   match = FcFontMatch(0, pat, &result);
00433 
00434   if (match == NULL) {
00435     goto error_pattern;
00436   }
00437 
00438   /* Find all fonts that do match */
00439   fs = FcFontSetCreate();
00440   FcFontSetAdd(fs, match);
00441 
00442   /* And take the first, if it exists */
00443   if (fs->nfont <= 0 || FcPatternGetString(fs->fonts[0], FC_FILE, 0, &file)) {
00444     goto error_fontset;
00445   }
00446 
00447   strecpy(settings->small_font,  (const char*)file, lastof(settings->small_font));
00448   strecpy(settings->medium_font, (const char*)file, lastof(settings->medium_font));
00449   strecpy(settings->large_font,  (const char*)file, lastof(settings->large_font));
00450 
00451   ret = true;
00452 
00453 error_fontset:
00454   FcFontSetDestroy(fs);
00455 error_pattern:
00456   if (pat != NULL) FcPatternDestroy(pat);
00457   FcFini();
00458   return ret;
00459 }
00460 
00461 #else /* without WITH_FONTCONFIG */
00462 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_Cannot_Open_Resource;}
00463 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid) { return false; }
00464 #endif /* WITH_FONTCONFIG */
00465 
00472 static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
00473 {
00474   FT_Error error;
00475 
00476   if (StrEmpty(font_name)) return;
00477 
00478   error = FT_New_Face(_library, font_name, 0, face);
00479 
00480   if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
00481 
00482   if (error == FT_Err_Ok) {
00483     DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, (*face)->family_name, (*face)->style_name);
00484 
00485     /* Attempt to select the unicode character map */
00486     error = FT_Select_Charmap(*face, ft_encoding_unicode);
00487     if (error == FT_Err_Ok) return; // Success
00488 
00489     if (error == FT_Err_Invalid_CharMap_Handle) {
00490       /* Try to pick a different character map instead. We default to
00491        * the first map, but platform_id 0 encoding_id 0 should also
00492        * be unicode (strange system...) */
00493       FT_CharMap found = (*face)->charmaps[0];
00494       int i;
00495 
00496       for (i = 0; i < (*face)->num_charmaps; i++) {
00497         FT_CharMap charmap = (*face)->charmaps[i];
00498         if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
00499           found = charmap;
00500         }
00501       }
00502 
00503       if (found != NULL) {
00504         error = FT_Set_Charmap(*face, found);
00505         if (error == FT_Err_Ok) return;
00506       }
00507     }
00508   }
00509 
00510   FT_Done_Face(*face);
00511   *face = NULL;
00512 
00513   ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, type, error);
00514 }
00515 
00516 
00517 void InitFreeType()
00518 {
00519   if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font)) {
00520     DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
00521     return;
00522   }
00523 
00524   if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
00525     ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
00526     return;
00527   }
00528 
00529   DEBUG(freetype, 2, "Initialized");
00530 
00531   /* Load each font */
00532   LoadFreeTypeFont(_freetype.small_font,  &_face_small,  "small");
00533   LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium");
00534   LoadFreeTypeFont(_freetype.large_font,  &_face_large,  "large");
00535 
00536   /* Set each font size */
00537   if (_face_small  != NULL) FT_Set_Pixel_Sizes(_face_small,  0, _freetype.small_size);
00538   if (_face_medium != NULL) FT_Set_Pixel_Sizes(_face_medium, 0, _freetype.medium_size);
00539   if (_face_large  != NULL) FT_Set_Pixel_Sizes(_face_large,  0, _freetype.large_size);
00540 }
00541 
00542 static void ResetGlyphCache();
00543 
00548 static void UnloadFace(FT_Face *face)
00549 {
00550   if (*face == NULL) return;
00551 
00552   FT_Done_Face(*face);
00553   *face = NULL;
00554 }
00555 
00559 void UninitFreeType()
00560 {
00561   ResetGlyphCache();
00562 
00563   UnloadFace(&_face_small);
00564   UnloadFace(&_face_medium);
00565   UnloadFace(&_face_large);
00566 
00567   FT_Done_FreeType(_library);
00568   _library = NULL;
00569 }
00570 
00571 
00572 static FT_Face GetFontFace(FontSize size)
00573 {
00574   switch (size) {
00575     default: NOT_REACHED();
00576     case FS_NORMAL: return _face_medium;
00577     case FS_SMALL:  return _face_small;
00578     case FS_LARGE:  return _face_large;
00579   }
00580 }
00581 
00582 
00583 struct GlyphEntry {
00584   Sprite *sprite;
00585   byte width;
00586 };
00587 
00588 
00589 /* The glyph cache. This is structured to reduce memory consumption.
00590  * 1) There is a 'segment' table for each font size.
00591  * 2) Each segment table is a discrete block of characters.
00592  * 3) Each block contains 256 (aligned) characters sequential characters.
00593  *
00594  * The cache is accessed in the following way:
00595  * For character 0x0041  ('A'): _glyph_ptr[FS_NORMAL][0x00][0x41]
00596  * For character 0x20AC (Euro): _glyph_ptr[FS_NORMAL][0x20][0xAC]
00597  *
00598  * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
00599  * This can be simply changed in the two functions Get & SetGlyphPtr.
00600  */
00601 static GlyphEntry **_glyph_ptr[FS_END];
00602 
00604 static void ResetGlyphCache()
00605 {
00606   for (int i = 0; i < FS_END; i++) {
00607     if (_glyph_ptr[i] == NULL) continue;
00608 
00609     for (int j = 0; j < 256; j++) {
00610       if (_glyph_ptr[i][j] == NULL) continue;
00611 
00612       for (int k = 0; k < 256; k++) {
00613         if (_glyph_ptr[i][j][k].sprite == NULL) continue;
00614         free(_glyph_ptr[i][j][k].sprite);
00615       }
00616 
00617       free(_glyph_ptr[i][j]);
00618     }
00619 
00620     free(_glyph_ptr[i]);
00621     _glyph_ptr[i] = NULL;
00622   }
00623 }
00624 
00625 static GlyphEntry *GetGlyphPtr(FontSize size, WChar key)
00626 {
00627   if (_glyph_ptr[size] == NULL) return NULL;
00628   if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) return NULL;
00629   return &_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)];
00630 }
00631 
00632 
00633 static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph)
00634 {
00635   if (_glyph_ptr[size] == NULL) {
00636     DEBUG(freetype, 3, "Allocating root glyph cache for size %u", size);
00637     _glyph_ptr[size] = CallocT<GlyphEntry*>(256);
00638   }
00639 
00640   if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) {
00641     DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), size);
00642     _glyph_ptr[size][GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
00643   }
00644 
00645   DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, size);
00646   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
00647   _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width  = glyph->width;
00648 }
00649 
00650 void *AllocateFont(size_t size)
00651 {
00652   return MallocT<byte>(size);
00653 }
00654 
00655 
00656 /* Check if a glyph should be rendered with antialiasing */
00657 static bool GetFontAAState(FontSize size)
00658 {
00659   /* AA is only supported for 32 bpp */
00660   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
00661 
00662   switch (size) {
00663     default: NOT_REACHED();
00664     case FS_NORMAL: return _freetype.medium_aa;
00665     case FS_SMALL:  return _freetype.small_aa;
00666     case FS_LARGE:  return _freetype.large_aa;
00667   }
00668 }
00669 
00670 
00671 const Sprite *GetGlyph(FontSize size, WChar key)
00672 {
00673   FT_Face face = GetFontFace(size);
00674   FT_GlyphSlot slot;
00675   GlyphEntry new_glyph;
00676   GlyphEntry *glyph;
00677   SpriteLoader::Sprite sprite;
00678   int width;
00679   int height;
00680   int x;
00681   int y;
00682   int y_adj;
00683 
00684   assert(IsPrintable(key));
00685 
00686   /* Bail out if no face loaded, or for our special characters */
00687   if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
00688     SpriteID sprite = GetUnicodeGlyph(size, key);
00689     if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
00690     return GetSprite(sprite, ST_FONT);
00691   }
00692 
00693   /* Check for the glyph in our cache */
00694   glyph = GetGlyphPtr(size, key);
00695   if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
00696 
00697   slot = face->glyph;
00698 
00699   bool aa = GetFontAAState(size);
00700 
00701   FT_Load_Char(face, key, FT_LOAD_DEFAULT);
00702   FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
00703 
00704   /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
00705   aa = (slot->bitmap.palette_mode == FT_PIXEL_MODE_GRAY);
00706 
00707   /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
00708   width  = max(1, slot->bitmap.width + (size == FS_NORMAL));
00709   height = max(1, slot->bitmap.rows  + (size == FS_NORMAL));
00710 
00711   /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
00712   sprite.AllocateData(width * height);
00713   sprite.width = width;
00714   sprite.height = height;
00715   sprite.x_offs = slot->bitmap_left;
00716   /* XXX 2 should be determined somehow... it's right for the normal face */
00717   y_adj = (size == FS_NORMAL) ? 2 : 0;
00718   sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
00719 
00720   /* Draw shadow for medium size */
00721   if (size == FS_NORMAL) {
00722     for (y = 0; y < slot->bitmap.rows; y++) {
00723       for (x = 0; x < slot->bitmap.width; x++) {
00724         if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
00725           sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
00726           sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
00727         }
00728       }
00729     }
00730   }
00731 
00732   for (y = 0; y < slot->bitmap.rows; y++) {
00733     for (x = 0; x < slot->bitmap.width; x++) {
00734       if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
00735         sprite.data[x + y * sprite.width].m = FACE_COLOUR;
00736         sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
00737       }
00738     }
00739   }
00740 
00741   new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
00742   new_glyph.width  = (slot->advance.x >> 6) + (size != FS_NORMAL);
00743 
00744   SetGlyphPtr(size, key, &new_glyph);
00745 
00746   return new_glyph.sprite;
00747 }
00748 
00749 
00750 uint GetGlyphWidth(FontSize size, WChar key)
00751 {
00752   FT_Face face = GetFontFace(size);
00753   GlyphEntry *glyph;
00754 
00755   if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
00756     SpriteID sprite = GetUnicodeGlyph(size, key);
00757     if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
00758     return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL) : 0;
00759   }
00760 
00761   glyph = GetGlyphPtr(size, key);
00762   if (glyph == NULL || glyph->sprite == NULL) {
00763     GetGlyph(size, key);
00764     glyph = GetGlyphPtr(size, key);
00765   }
00766 
00767   return glyph->width;
00768 }
00769 
00770 
00771 #endif /* WITH_FREETYPE */
00772 
00773 /* Sprite based glyph mapping */
00774 
00775 #include "table/unicode.h"
00776 
00777 static SpriteID **_unicode_glyph_map[FS_END];
00778 
00779 
00781 static SpriteID GetFontBase(FontSize size)
00782 {
00783   switch (size) {
00784     default: NOT_REACHED();
00785     case FS_NORMAL: return SPR_ASCII_SPACE;
00786     case FS_SMALL:  return SPR_ASCII_SPACE_SMALL;
00787     case FS_LARGE:  return SPR_ASCII_SPACE_BIG;
00788   }
00789 }
00790 
00791 
00792 SpriteID GetUnicodeGlyph(FontSize size, uint32 key)
00793 {
00794   if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) return 0;
00795   return _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)];
00796 }
00797 
00798 
00799 void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite)
00800 {
00801   if (_unicode_glyph_map[size] == NULL) _unicode_glyph_map[size] = CallocT<SpriteID*>(256);
00802   if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) _unicode_glyph_map[size][GB(key, 8, 8)] = CallocT<SpriteID>(256);
00803   _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
00804 }
00805 
00806 
00807 void InitializeUnicodeGlyphMap()
00808 {
00809   for (FontSize size = FS_NORMAL; size != FS_END; size++) {
00810     /* Clear out existing glyph map if it exists */
00811     if (_unicode_glyph_map[size] != NULL) {
00812       for (uint i = 0; i < 256; i++) {
00813         if (_unicode_glyph_map[size][i] != NULL) free(_unicode_glyph_map[size][i]);
00814       }
00815       free(_unicode_glyph_map[size]);
00816       _unicode_glyph_map[size] = NULL;
00817     }
00818 
00819     SpriteID base = GetFontBase(size);
00820 
00821     for (uint i = ASCII_LETTERSTART; i < 256; i++) {
00822       SpriteID sprite = base + i - ASCII_LETTERSTART;
00823       if (!SpriteExists(sprite)) continue;
00824       SetUnicodeGlyph(size, i, sprite);
00825       SetUnicodeGlyph(size, i + SCC_SPRITE_START, sprite);
00826     }
00827 
00828     for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
00829       byte key = _default_unicode_map[i].key;
00830       if (key == CLRA || key == CLRL) {
00831         /* Clear the glyph. This happens if the glyph at this code point
00832          * is non-standard and should be accessed by an SCC_xxx enum
00833          * entry only. */
00834         if (key == CLRA || size == FS_LARGE) {
00835           SetUnicodeGlyph(size, _default_unicode_map[i].code, 0);
00836         }
00837       } else {
00838         SpriteID sprite = base + key - ASCII_LETTERSTART;
00839         SetUnicodeGlyph(size, _default_unicode_map[i].code, sprite);
00840       }
00841     }
00842   }
00843 }

Generated on Sun Mar 15 22:49:46 2009 for openttd by  doxygen 1.5.6