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 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;
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
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) ||
00251 (platformId == 3 && languageId == 0x0409)) {
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
00278 if (!(type & TRUETYPE_FONTTYPE)) return 1;
00279
00280 if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
00281
00282
00283 if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
00284
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;
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
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
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
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
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
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
00376 if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
00377
00378
00379
00380
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
00404
00405
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
00421 pat = FcPatternCreate();
00422
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
00432 match = FcFontMatch(0, pat, &result);
00433
00434 if (match == NULL) {
00435 goto error_pattern;
00436 }
00437
00438
00439 fs = FcFontSetCreate();
00440 FcFontSetAdd(fs, match);
00441
00442
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
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
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
00486 error = FT_Select_Charmap(*face, ft_encoding_unicode);
00487 if (error == FT_Err_Ok) return;
00488
00489 if (error == FT_Err_Invalid_CharMap_Handle) {
00490
00491
00492
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
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
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
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
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
00657 static bool GetFontAAState(FontSize size)
00658 {
00659
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
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
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
00705 aa = (slot->bitmap.palette_mode == FT_PIXEL_MODE_GRAY);
00706
00707
00708 width = max(1, slot->bitmap.width + (size == FS_NORMAL));
00709 height = max(1, slot->bitmap.rows + (size == FS_NORMAL));
00710
00711
00712 sprite.AllocateData(width * height);
00713 sprite.width = width;
00714 sprite.height = height;
00715 sprite.x_offs = slot->bitmap_left;
00716
00717 y_adj = (size == FS_NORMAL) ? 2 : 0;
00718 sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
00719
00720
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
00772
00773
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
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
00832
00833
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 }