00001
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>
00042 #include "win32.h"
00043
00054 char *GetShortPath(const char *long_path)
00055 {
00056 static char short_path[MAX_PATH];
00057 #ifdef UNICODE
00058
00059
00060
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
00070 GetShortPathNameA(long_path, short_path, MAX_PATH);
00071 #endif
00072 return short_path;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
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
00096
00097
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
00107
00108
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;
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
00125
00126
00127
00128
00129
00130
00131
00132
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
00149
00150
00151
00152
00153 #if defined(UNICODE)
00154
00155
00156
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
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
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;
00245 continue;
00246 }
00247 length = buf[pos++] << 8;
00248 length += buf[pos++];
00249 offset = buf[pos++] << 8;
00250 offset += buf[pos++];
00251
00252
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) ||
00258 (platformId == 3 && languageId == 0x0409)) {
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
00285 if (!(type & TRUETYPE_FONTTYPE)) return 1;
00286
00287 if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
00288
00289
00290 if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
00291
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
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;
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
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
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
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
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
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
00389 if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
00390
00391
00392
00393
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
00417
00418
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
00434 pat = FcPatternCreate();
00435
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
00445 match = FcFontMatch(0, pat, &result);
00446
00447 if (match == NULL) {
00448 goto error_pattern;
00449 }
00450
00451
00452 fs = FcFontSetCreate();
00453 FcFontSetAdd(fs, match);
00454
00455
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
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
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
00499 error = FT_Select_Charmap(*face, ft_encoding_unicode);
00500 if (error == FT_Err_Ok) return;
00501
00502 if (error == FT_Err_Invalid_CharMap_Handle) {
00503
00504
00505
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
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
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
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
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
00670 static bool GetFontAAState(FontSize size)
00671 {
00672
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
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
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
00718 aa = (slot->bitmap.palette_mode == FT_PIXEL_MODE_GRAY);
00719
00720
00721 width = max(1, slot->bitmap.width + (size == FS_NORMAL));
00722 height = max(1, slot->bitmap.rows + (size == FS_NORMAL));
00723
00724
00725 sprite.AllocateData(width * height);
00726 sprite.width = width;
00727 sprite.height = height;
00728 sprite.x_offs = slot->bitmap_left;
00729
00730 y_adj = (size == FS_NORMAL) ? 2 : 0;
00731 sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
00732
00733
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
00785
00786
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
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
00845
00846
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 }