00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "fontcache.h"
00014 #include "blitter/factory.hpp"
00015 #include "core/math_func.hpp"
00016 #include "strings_func.h"
00017 #include "zoom_type.h"
00018
00019 #include "table/sprites.h"
00020 #include "table/control_codes.h"
00021
00022 static const int ASCII_LETTERSTART = 32;
00023 static const int MAX_FONT_SIZE = 72;
00024
00026 int _font_height[FS_END];
00028 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
00029
00034 void ResetFontSizes(bool monospace)
00035 {
00036 if (monospace) {
00037 _font_height[FS_MONO] = _default_font_height[FS_MONO];
00038 } else {
00039 _font_height[FS_SMALL] = _default_font_height[FS_SMALL];
00040 _font_height[FS_NORMAL] = _default_font_height[FS_NORMAL];
00041 _font_height[FS_LARGE] = _default_font_height[FS_LARGE];
00042 }
00043 }
00044
00045 #ifdef WITH_FREETYPE
00046 #include <ft2build.h>
00047 #include FT_FREETYPE_H
00048 #include FT_GLYPH_H
00049 #include FT_TRUETYPE_TABLES_H
00050
00051 #ifdef WITH_FONTCONFIG
00052 #include <fontconfig/fontconfig.h>
00053 #endif
00054
00055 static FT_Library _library = NULL;
00056 static FT_Face _face_small = NULL;
00057 static FT_Face _face_medium = NULL;
00058 static FT_Face _face_large = NULL;
00059 static FT_Face _face_mono = NULL;
00060 static int _ascender[FS_END];
00061
00062 FreeTypeSettings _freetype;
00063
00064 static const byte FACE_COLOUR = 1;
00065 static const byte SHADOW_COLOUR = 2;
00066
00072
00073
00074
00075
00076 #ifdef WIN32
00077 #include <windows.h>
00078 #include <shlobj.h>
00079 #include "os/windows/win32.h"
00080
00091 char *GetShortPath(const char *long_path)
00092 {
00093 static char short_path[MAX_PATH];
00094 #ifdef UNICODE
00095
00096
00097
00098 wchar_t long_path_w[MAX_PATH];
00099 MultiByteToWideChar(CP_UTF8, 0, long_path, -1, long_path_w, MAX_PATH);
00100
00101 wchar_t short_path_w[MAX_PATH];
00102 GetShortPathNameW(long_path_w, short_path_w, MAX_PATH);
00103
00104 WideCharToMultiByte(CP_ACP, 0, short_path_w, -1, short_path, MAX_PATH, NULL, NULL);
00105 #else
00106
00107 GetShortPathNameA(long_path, short_path, MAX_PATH);
00108 #endif
00109 return short_path;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 #define FONT_DIR_NT "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
00121 #define FONT_DIR_9X "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Fonts"
00122 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
00123 {
00124 FT_Error err = FT_Err_Cannot_Open_Resource;
00125 HKEY hKey;
00126 LONG ret;
00127 TCHAR vbuffer[MAX_PATH], dbuffer[256];
00128 TCHAR *font_namep;
00129 char *font_path;
00130 uint index;
00131
00132
00133
00134
00135 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_NT), 0, KEY_READ, &hKey);
00136 if (ret != ERROR_SUCCESS) ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T(FONT_DIR_9X), 0, KEY_READ, &hKey);
00137
00138 if (ret != ERROR_SUCCESS) {
00139 DEBUG(freetype, 0, "Cannot open registry key HKLM\\SOFTWARE\\Microsoft\\Windows (NT)\\CurrentVersion\\Fonts");
00140 return err;
00141 }
00142
00143
00144
00145
00146 #if defined(UNICODE)
00147 font_namep = MallocT<TCHAR>(MAX_PATH);
00148 MB_TO_WIDE_BUFFER(font_name, font_namep, MAX_PATH * sizeof(TCHAR));
00149 #else
00150 font_namep = const_cast<char *>(font_name);
00151 #endif
00152
00153 for (index = 0;; index++) {
00154 TCHAR *s;
00155 DWORD vbuflen = lengthof(vbuffer);
00156 DWORD dbuflen = lengthof(dbuffer);
00157
00158 ret = RegEnumValue(hKey, index, vbuffer, &vbuflen, NULL, NULL, (byte*)dbuffer, &dbuflen);
00159 if (ret != ERROR_SUCCESS) goto registry_no_font_found;
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 s = _tcschr(vbuffer, _T('('));
00171 if (s != NULL) s[-1] = '\0';
00172
00173 if (_tcschr(vbuffer, _T('&')) == NULL) {
00174 if (_tcsicmp(vbuffer, font_namep) == 0) break;
00175 } else {
00176 if (_tcsstr(vbuffer, font_namep) != NULL) break;
00177 }
00178 }
00179
00180 if (!SUCCEEDED(OTTDSHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, vbuffer))) {
00181 DEBUG(freetype, 0, "SHGetFolderPath cannot return fonts directory");
00182 goto folder_error;
00183 }
00184
00185
00186
00187
00188
00189
00190 #if defined(UNICODE)
00191
00192
00193
00194 font_path = (char*)font_namep;
00195 WIDE_TO_MB_BUFFER(vbuffer, font_path, MAX_PATH * sizeof(TCHAR));
00196 #else
00197 font_path = vbuffer;
00198 #endif
00199
00200 ttd_strlcat(font_path, "\\", MAX_PATH * sizeof(TCHAR));
00201 ttd_strlcat(font_path, WIDE_TO_MB(dbuffer), MAX_PATH * sizeof(TCHAR));
00202
00203
00204 font_path = GetShortPath(font_path);
00205
00206 index = 0;
00207 do {
00208 err = FT_New_Face(_library, font_path, index, face);
00209 if (err != FT_Err_Ok) break;
00210
00211 if (strncasecmp(font_name, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
00212
00213 if (strncasecmp(font_name + strlen(font_name) + 1, (*face)->family_name, strlen((*face)->family_name)) == 0) break;
00214 err = FT_Err_Cannot_Open_Resource;
00215
00216 } while ((FT_Long)++index != (*face)->num_faces);
00217
00218
00219 folder_error:
00220 registry_no_font_found:
00221 #if defined(UNICODE)
00222 free(font_namep);
00223 #endif
00224 RegCloseKey(hKey);
00225 return err;
00226 }
00227
00241 static const char *GetEnglishFontName(const ENUMLOGFONTEX *logfont)
00242 {
00243 static char font_name[MAX_PATH];
00244 const char *ret_font_name = NULL;
00245 uint pos = 0;
00246 HDC dc;
00247 HGDIOBJ oldfont;
00248 byte *buf;
00249 DWORD dw;
00250 uint16 format, count, stringOffset, platformId, encodingId, languageId, nameId, length, offset;
00251
00252 HFONT font = CreateFontIndirect(&logfont->elfLogFont);
00253 if (font == NULL) goto err1;
00254
00255 dc = GetDC(NULL);
00256 oldfont = SelectObject(dc, font);
00257 dw = GetFontData(dc, 'eman', 0, NULL, 0);
00258 if (dw == GDI_ERROR) goto err2;
00259
00260 buf = MallocT<byte>(dw);
00261 dw = GetFontData(dc, 'eman', 0, buf, dw);
00262 if (dw == GDI_ERROR) goto err3;
00263
00264 format = buf[pos++] << 8;
00265 format += buf[pos++];
00266 assert(format == 0);
00267 count = buf[pos++] << 8;
00268 count += buf[pos++];
00269 stringOffset = buf[pos++] << 8;
00270 stringOffset += buf[pos++];
00271 for (uint i = 0; i < count; i++) {
00272 platformId = buf[pos++] << 8;
00273 platformId += buf[pos++];
00274 encodingId = buf[pos++] << 8;
00275 encodingId += buf[pos++];
00276 languageId = buf[pos++] << 8;
00277 languageId += buf[pos++];
00278 nameId = buf[pos++] << 8;
00279 nameId += buf[pos++];
00280 if (nameId != 1) {
00281 pos += 4;
00282 continue;
00283 }
00284 length = buf[pos++] << 8;
00285 length += buf[pos++];
00286 offset = buf[pos++] << 8;
00287 offset += buf[pos++];
00288
00289
00290 length = min(length, MAX_PATH - 1);
00291 for (uint j = 0; j < length; j++) font_name[j] = buf[stringOffset + offset + j];
00292 font_name[length] = '\0';
00293
00294 if ((platformId == 1 && languageId == 0) ||
00295 (platformId == 3 && languageId == 0x0409)) {
00296 ret_font_name = font_name;
00297 break;
00298 }
00299 }
00300
00301 err3:
00302 free(buf);
00303 err2:
00304 SelectObject(dc, oldfont);
00305 ReleaseDC(NULL, dc);
00306 DeleteObject(font);
00307 err1:
00308 return ret_font_name == NULL ? WIDE_TO_MB((const TCHAR*)logfont->elfFullName) : ret_font_name;
00309 }
00310
00311 class FontList {
00312 protected:
00313 TCHAR **fonts;
00314 uint items;
00315 uint capacity;
00316
00317 public:
00318 FontList() : fonts(NULL), items(0), capacity(0) { };
00319
00320 ~FontList() {
00321 if (this->fonts == NULL) return;
00322
00323 for (uint i = 0; i < this->items; i++) {
00324 free(this->fonts[i]);
00325 }
00326
00327 free(this->fonts);
00328 }
00329
00330 bool Add(const TCHAR *font) {
00331 for (uint i = 0; i < this->items; i++) {
00332 if (_tcscmp(this->fonts[i], font) == 0) return false;
00333 }
00334
00335 if (this->items == this->capacity) {
00336 this->capacity += 10;
00337 this->fonts = ReallocT(this->fonts, this->capacity);
00338 }
00339
00340 this->fonts[this->items++] = _tcsdup(font);
00341
00342 return true;
00343 }
00344 };
00345
00346 struct EFCParam {
00347 FreeTypeSettings *settings;
00348 LOCALESIGNATURE locale;
00349 MissingGlyphSearcher *callback;
00350 FontList fonts;
00351 };
00352
00353 static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
00354 {
00355 EFCParam *info = (EFCParam *)lParam;
00356
00357
00358 if (!info->fonts.Add((const TCHAR*)logfont->elfFullName)) return 1;
00359
00360 if (!(type & TRUETYPE_FONTTYPE)) return 1;
00361
00362 if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
00363
00364 if (info->callback->Monospace() && (logfont->elfLogFont.lfPitchAndFamily & (FF_MODERN | FIXED_PITCH)) != (FF_MODERN | FIXED_PITCH)) return 1;
00365
00366
00367 if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
00368
00369 FONTSIGNATURE fs;
00370 memset(&fs, 0, sizeof(fs));
00371 HFONT font = CreateFontIndirect(&logfont->elfLogFont);
00372 if (font != NULL) {
00373 HDC dc = GetDC(NULL);
00374 HGDIOBJ oldfont = SelectObject(dc, font);
00375 GetTextCharsetInfo(dc, &fs, 0);
00376 SelectObject(dc, oldfont);
00377 ReleaseDC(NULL, dc);
00378 DeleteObject(font);
00379 }
00380 if ((fs.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (fs.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) return 1;
00381 }
00382
00383 char font_name[MAX_PATH];
00384 #if defined(UNICODE)
00385 WIDE_TO_MB_BUFFER((const TCHAR*)logfont->elfFullName, font_name, lengthof(font_name));
00386 #else
00387 strecpy(font_name, (const TCHAR*)logfont->elfFullName, lastof(font_name));
00388 #endif
00389
00390
00391 const char *english_name = GetEnglishFontName(logfont);
00392 strecpy(font_name + strlen(font_name) + 1, english_name, lastof(font_name));
00393
00394
00395 bool ft_init = _library != NULL;
00396 bool found = false;
00397 FT_Face face;
00398
00399 if ((ft_init || FT_Init_FreeType(&_library) == FT_Err_Ok) && GetFontByFaceName(font_name, &face) == FT_Err_Ok) {
00400 FT_Done_Face(face);
00401 found = true;
00402 }
00403 if (!ft_init) {
00404
00405 FT_Done_FreeType(_library);
00406 _library = NULL;
00407 }
00408
00409 if (!found) return 1;
00410
00411 info->callback->SetFontNames(info->settings, font_name);
00412 if (info->callback->FindMissingGlyphs(NULL)) return 1;
00413 DEBUG(freetype, 1, "Fallback font: %s (%s)", font_name, english_name);
00414 return 0;
00415 }
00416
00417 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
00418 {
00419 DEBUG(freetype, 1, "Trying fallback fonts");
00420 EFCParam langInfo;
00421 if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale, sizeof(langInfo.locale) / sizeof(TCHAR)) == 0) {
00422
00423 DEBUG(freetype, 1, "Can't get locale info for fallback font (langid=0x%x)", winlangid);
00424 return false;
00425 }
00426 langInfo.settings = settings;
00427 langInfo.callback = callback;
00428
00429 LOGFONT font;
00430
00431 font.lfCharSet = DEFAULT_CHARSET;
00432 font.lfFaceName[0] = '\0';
00433 font.lfPitchAndFamily = 0;
00434
00435 HDC dc = GetDC(NULL);
00436 int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
00437 ReleaseDC(NULL, dc);
00438 return ret == 0;
00439 }
00440
00441 #elif defined(__APPLE__)
00442
00443
00444
00445
00446 #include "os/macosx/macos.h"
00447
00448 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
00449 {
00450 FT_Error err = FT_Err_Cannot_Open_Resource;
00451
00452
00453 CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name, kCFStringEncodingUTF8);
00454 ATSFontRef font = ATSFontFindFromName(name, kATSOptionFlagsDefault);
00455 CFRelease(name);
00456 if (font == kInvalidFont) return err;
00457
00458
00459 FSRef ref;
00460 OSStatus os_err = -1;
00461 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
00462 if (MacOSVersionIsAtLeast(10, 5, 0)) {
00463 os_err = ATSFontGetFileReference(font, &ref);
00464 } else
00465 #endif
00466 {
00467 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) && !__LP64__
00468
00469 #if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
00470 #define ATSFSSpec FSSpec
00471 #endif
00472 FSSpec spec;
00473 os_err = ATSFontGetFileSpecification(font, (ATSFSSpec *)&spec);
00474 if (os_err == noErr) os_err = FSpMakeFSRef(&spec, &ref);
00475 #endif
00476 }
00477
00478 if (os_err == noErr) {
00479
00480 UInt8 file_path[PATH_MAX];
00481 if (FSRefMakePath(&ref, file_path, sizeof(file_path)) == noErr) {
00482 DEBUG(freetype, 3, "Font path for %s: %s", font_name, file_path);
00483 err = FT_New_Face(_library, (const char *)file_path, 0, face);
00484 }
00485 }
00486
00487 return err;
00488 }
00489
00490 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
00491 {
00492 const char *str;
00493 bool result = false;
00494
00495 callback->FindMissingGlyphs(&str);
00496
00497 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
00498 if (MacOSVersionIsAtLeast(10, 5, 0)) {
00499
00500
00501 char lang[16];
00502 if (strcmp(language_isocode, "zh_TW") == 0) {
00503
00504 strecpy(lang, "zh-Hant", lastof(lang));
00505 } else if (strcmp(language_isocode, "zh_CN") == 0) {
00506
00507 strecpy(lang, "zh-Hans", lastof(lang));
00508 } else if (strncmp(language_isocode, "ur", 2) == 0) {
00509
00510
00511
00512 strecpy(lang, "fa", lastof(lang));
00513 } else {
00514
00515 strecpy(lang, language_isocode, lastof(lang));
00516 char *sep = strchr(lang, '_');
00517 if (sep != NULL) *sep = '\0';
00518 }
00519
00520 CFStringRef lang_code;
00521 lang_code = CFStringCreateWithCString(kCFAllocatorDefault, lang, kCFStringEncodingUTF8);
00522
00523
00524
00525 ATSFontIterator itr;
00526 ATSFontRef font;
00527 ATSFontIteratorCreate(kATSFontContextLocal, NULL, NULL, kATSOptionFlagsUnRestrictedScope, &itr);
00528 while (!result && ATSFontIteratorNext(itr, &font) == noErr) {
00529
00530 CTFontRef font_ref = CTFontCreateWithPlatformFont(font, 0.0, NULL, NULL);
00531 CFArrayRef langs = CTFontCopySupportedLanguages(font_ref);
00532 if (langs != NULL) {
00533
00534 for (CFIndex i = 0; i < CFArrayGetCount(langs); i++) {
00535 CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(langs, i);
00536 if (CFStringCompare(lang, lang_code, kCFCompareAnchored) == kCFCompareEqualTo) {
00537
00538 CFStringRef font_name = CTFontCopyFullName(font_ref);
00539 char name[128];
00540 CFStringGetCString(font_name, name, lengthof(name), kCFStringEncodingUTF8);
00541 CFRelease(font_name);
00542
00543 if (strncmp(name, "Courier", 7) == 0 || strncmp(name, "Apple Symbols", 13) == 0 ||
00544 strncmp(name, ".Aqua", 5) == 0 || strncmp(name, "LastResort", 10) == 0 ||
00545 strncmp(name, "GB18030 Bitmap", 14) == 0) continue;
00546
00547
00548 callback->SetFontNames(settings, name);
00549 DEBUG(freetype, 2, "CT-Font for %s: %s", language_isocode, name);
00550 result = true;
00551 break;
00552 }
00553 }
00554 CFRelease(langs);
00555 }
00556 CFRelease(font_ref);
00557 }
00558 ATSFontIteratorRelease(&itr);
00559 CFRelease(lang_code);
00560 } else
00561 #endif
00562 {
00563 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) && !__LP64__
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 char buff[256];
00574 strecpy(buff, str, lastof(buff));
00575 str_validate(buff, lastof(buff), SVS_ALLOW_NEWLINE);
00576
00577
00578 CFStringRef cf_str = CFStringCreateWithCString(kCFAllocatorDefault, buff, kCFStringEncodingUTF8);
00579 if (cf_str == NULL) {
00580
00581 return false;
00582 }
00583 CFIndex str_len = CFStringGetLength(cf_str);
00584 UniChar string[str_len];
00585 CFStringGetCharacters(cf_str, CFRangeMake(0, str_len), string);
00586
00587
00588 ATSUStyle style;
00589 ATSUCreateStyle(&style);
00590
00591
00592 UniCharCount run_len = kATSUToTextEnd;
00593 ATSUTextLayout text_layout;
00594 ATSUCreateTextLayoutWithTextPtr(string, kATSUFromTextBeginning, kATSUToTextEnd, str_len, 1, &run_len, &style, &text_layout);
00595
00596
00597
00598
00599
00600
00601 UniCharArrayOffset offset = kATSUFromTextBeginning;
00602 OSStatus os_err;
00603 do {
00604 ATSUFontID font;
00605 UniCharCount run_len;
00606 os_err = ATSUMatchFontsToText(text_layout, offset, kATSUToTextEnd, &font, &offset, &run_len);
00607 if (os_err == kATSUFontsMatched) {
00608
00609
00610 ATSUAttributeTag tag = kATSUFontTag;
00611 ByteCount size = sizeof(font);
00612 ATSUAttributeValuePtr val = &font;
00613 ATSUSetAttributes(style, 1, &tag, &size, &val);
00614 offset += run_len;
00615 }
00616
00617 } while (os_err == kATSUFontsMatched && offset < (UniCharArrayOffset)str_len);
00618
00619 if (os_err == noErr || os_err == kATSUFontsMatched) {
00620
00621
00622 ATSUFontID font;
00623 ByteCount act_len;
00624 ATSUGetAttribute(style, kATSUFontTag, sizeof(font), &font, &act_len);
00625
00626
00627
00628 char name[128];
00629 ATSUFindFontName(font, kFontUniqueName, kFontNoPlatformCode, kFontNoScriptCode, kFontNoLanguageCode, 127, name, &act_len, NULL);
00630 name[act_len > 127 ? 127 : act_len] = '\0';
00631
00632
00633 callback->SetFontNames(settings, name);
00634 DEBUG(freetype, 2, "ATSUI-Font for %s: %s", language_isocode, name);
00635 result = true;
00636 }
00637
00638 ATSUDisposeTextLayout(text_layout);
00639 ATSUDisposeStyle(style);
00640 CFRelease(cf_str);
00641 #endif
00642 }
00643
00644 if (result && strncmp(settings->medium_font, "Geeza Pro", 9) == 0) {
00645
00646
00647
00648
00649
00650 bool ft_init = _library != NULL;
00651 FT_Face face;
00652
00653 if ((ft_init || FT_Init_FreeType(&_library) == FT_Err_Ok) && GetFontByFaceName("Arial Unicode MS", &face) == FT_Err_Ok) {
00654 FT_Done_Face(face);
00655 callback->SetFontNames(settings, "Arial Unicode MS");
00656 DEBUG(freetype, 1, "Replacing font 'Geeza Pro' with 'Arial Unicode MS'");
00657 }
00658 if (!ft_init) {
00659
00660 FT_Done_FreeType(_library);
00661 _library = NULL;
00662 }
00663 }
00664
00665 callback->FindMissingGlyphs(NULL);
00666 return result;
00667 }
00668
00669 #elif defined(WITH_FONTCONFIG)
00670
00671
00672
00673 static FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
00674 {
00675 FT_Error err = FT_Err_Cannot_Open_Resource;
00676
00677 if (!FcInit()) {
00678 ShowInfoF("Unable to load font configuration");
00679 } else {
00680 FcPattern *match;
00681 FcPattern *pat;
00682 FcFontSet *fs;
00683 FcResult result;
00684 char *font_style;
00685 char *font_family;
00686
00687
00688 font_family = strdup(font_name);
00689 font_style = strchr(font_family, ',');
00690 if (font_style != NULL) {
00691 font_style[0] = '\0';
00692 font_style++;
00693 while (*font_style == ' ' || *font_style == '\t') font_style++;
00694 }
00695
00696
00697 pat = FcNameParse((FcChar8*)font_family);
00698 if (font_style != NULL) FcPatternAddString(pat, FC_STYLE, (FcChar8*)font_style);
00699 FcConfigSubstitute(0, pat, FcMatchPattern);
00700 FcDefaultSubstitute(pat);
00701 fs = FcFontSetCreate();
00702 match = FcFontMatch(0, pat, &result);
00703
00704 if (fs != NULL && match != NULL) {
00705 int i;
00706 FcChar8 *family;
00707 FcChar8 *style;
00708 FcChar8 *file;
00709 FcFontSetAdd(fs, match);
00710
00711 for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
00712
00713 if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
00714 FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
00715 FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch) {
00716
00717
00718 if (font_style != NULL && strcasecmp(font_style, (char*)style) != 0) continue;
00719
00720
00721
00722
00723 if (strcasecmp(font_family, (char*)family) == 0) {
00724 err = FT_New_Face(_library, (char *)file, 0, face);
00725 }
00726 }
00727 }
00728 }
00729
00730 free(font_family);
00731 FcPatternDestroy(pat);
00732 FcFontSetDestroy(fs);
00733 FcFini();
00734 }
00735
00736 return err;
00737 }
00738
00739 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
00740 {
00741 if (!FcInit()) return false;
00742
00743 bool ret = false;
00744
00745
00746
00747
00748 char lang[16];
00749 seprintf(lang, lastof(lang), ":lang=%s", language_isocode);
00750 char *split = strchr(lang, '_');
00751 if (split != NULL) *split = '\0';
00752
00753
00754 FcPattern *pat = FcNameParse((FcChar8*)lang);
00755
00756 FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_SPACING, FC_SLANT, FC_WEIGHT, NULL);
00757
00758 FcFontSet *fs = FcFontList(NULL, pat, os);
00759
00760
00761 FcObjectSetDestroy(os);
00762 FcPatternDestroy(pat);
00763
00764 if (fs != NULL) {
00765 int best_weight = -1;
00766 const char *best_font = NULL;
00767
00768 for (int i = 0; i < fs->nfont; i++) {
00769 FcPattern *font = fs->fonts[i];
00770
00771 FcChar8 *file = NULL;
00772 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
00773 if (res != FcResultMatch || file == NULL) {
00774 continue;
00775 }
00776
00777
00778 int value = 0;
00779 FcPatternGetInteger(font, FC_SPACING, 0, &value);
00780 if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
00781
00782
00783 FcPatternGetInteger(font, FC_SLANT, 0, &value);
00784 if (value != 0) continue;
00785
00786
00787 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
00788 if (value <= best_weight) continue;
00789
00790 callback->SetFontNames(settings, (const char*)file);
00791
00792 bool missing = callback->FindMissingGlyphs(NULL);
00793 DEBUG(freetype, 1, "Font \"%s\" misses%s glyphs", file, missing ? "" : " no");
00794
00795 if (!missing) {
00796 best_weight = value;
00797 best_font = (const char *)file;
00798 }
00799 }
00800
00801 if (best_font != NULL) {
00802 ret = true;
00803 callback->SetFontNames(settings, best_font);
00804 InitFreeType(callback->Monospace());
00805 }
00806
00807
00808 FcFontSetDestroy(fs);
00809 }
00810
00811 FcFini();
00812 return ret;
00813 }
00814
00815 #else
00816 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_Cannot_Open_Resource;}
00817 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
00818 #endif
00819
00820 static void SetFontGeometry(FT_Face face, FontSize size, int pixels)
00821 {
00822 if (pixels == 0) {
00823
00824 pixels = _default_font_height[size];
00825
00826 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(face, ft_sfnt_head);
00827 if (head != NULL) {
00828
00829
00830 int diff = _default_font_height[size] - _default_font_height[FS_SMALL];
00831 pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[size], MAX_FONT_SIZE);
00832 }
00833 }
00834
00835 FT_Error err = FT_Set_Pixel_Sizes(face, 0, pixels);
00836 if (err == FT_Err_Invalid_Pixel_Size) {
00837
00838
00839 FT_Bitmap_Size *bs = face->available_sizes;
00840 int i = face->num_fixed_sizes;
00841 int n = bs->height;
00842 for (; --i; bs++) {
00843 if (abs(pixels - bs->height) < abs(pixels - n)) n = bs->height;
00844 }
00845
00846 FT_Set_Pixel_Sizes(face, 0, n);
00847 }
00848
00849 int asc = face->size->metrics.ascender >> 6;
00850 int dec = face->size->metrics.descender >> 6;
00851
00852 _ascender[size] = asc;
00853 _font_height[size] = asc - dec;
00854 }
00855
00862 static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
00863 {
00864 FT_Error error;
00865
00866 if (StrEmpty(font_name)) return;
00867
00868 error = FT_New_Face(_library, font_name, 0, face);
00869
00870 if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
00871
00872 if (error == FT_Err_Ok) {
00873 DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, (*face)->family_name, (*face)->style_name);
00874
00875
00876 error = FT_Select_Charmap(*face, ft_encoding_unicode);
00877 if (error == FT_Err_Ok) return;
00878
00879 if (error == FT_Err_Invalid_CharMap_Handle) {
00880
00881
00882
00883 FT_CharMap found = (*face)->charmaps[0];
00884 int i;
00885
00886 for (i = 0; i < (*face)->num_charmaps; i++) {
00887 FT_CharMap charmap = (*face)->charmaps[i];
00888 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
00889 found = charmap;
00890 }
00891 }
00892
00893 if (found != NULL) {
00894 error = FT_Set_Charmap(*face, found);
00895 if (error == FT_Err_Ok) return;
00896 }
00897 }
00898 }
00899
00900 FT_Done_Face(*face);
00901 *face = NULL;
00902
00903 ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, type, error);
00904 }
00905
00906
00907 static void ResetGlyphCache(bool monospace);
00908
00913 static void UnloadFace(FT_Face *face)
00914 {
00915 if (*face == NULL) return;
00916
00917 FT_Done_Face(*face);
00918 *face = NULL;
00919 }
00920
00925 void InitFreeType(bool monospace)
00926 {
00927 ResetFontSizes(monospace);
00928 ResetGlyphCache(monospace);
00929
00930 if (monospace) {
00931 UnloadFace(&_face_mono);
00932 } else {
00933 UnloadFace(&_face_small);
00934 UnloadFace(&_face_medium);
00935 UnloadFace(&_face_large);
00936 }
00937
00938 if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font) && StrEmpty(_freetype.mono_font)) {
00939 DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
00940 return;
00941 }
00942
00943 if (_library == NULL) {
00944 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
00945 ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
00946 return;
00947 }
00948
00949 DEBUG(freetype, 2, "Initialized");
00950 }
00951
00952
00953 if (monospace) {
00954 LoadFreeTypeFont(_freetype.mono_font , &_face_mono, "mono");
00955
00956 if (_face_mono != NULL) {
00957 SetFontGeometry(_face_mono, FS_MONO, _freetype.mono_size);
00958 }
00959 } else {
00960 LoadFreeTypeFont(_freetype.small_font, &_face_small, "small");
00961 LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium");
00962 LoadFreeTypeFont(_freetype.large_font, &_face_large, "large");
00963
00964
00965 if (_face_small != NULL) {
00966 SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size);
00967 }
00968 if (_face_medium != NULL) {
00969 SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size);
00970 }
00971 if (_face_large != NULL) {
00972 SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size);
00973 }
00974 }
00975 }
00976
00980 void UninitFreeType()
00981 {
00982 ResetGlyphCache(true);
00983 ResetGlyphCache(false);
00984
00985 UnloadFace(&_face_small);
00986 UnloadFace(&_face_medium);
00987 UnloadFace(&_face_large);
00988 UnloadFace(&_face_mono);
00989
00990 FT_Done_FreeType(_library);
00991 _library = NULL;
00992 }
00993
00997 void ClearFontCache()
00998 {
00999 ResetGlyphCache(true);
01000 ResetGlyphCache(false);
01001 }
01002
01003 static FT_Face GetFontFace(FontSize size)
01004 {
01005 switch (size) {
01006 default: NOT_REACHED();
01007 case FS_NORMAL: return _face_medium;
01008 case FS_SMALL: return _face_small;
01009 case FS_LARGE: return _face_large;
01010 case FS_MONO: return _face_mono;
01011 }
01012 }
01013
01014
01015 struct GlyphEntry {
01016 Sprite *sprite;
01017 byte width;
01018 bool duplicate;
01019 };
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034 static GlyphEntry **_glyph_ptr[FS_END];
01035
01040 static void ResetGlyphCache(bool monospace)
01041 {
01042 for (FontSize i = FS_BEGIN; i < FS_END; i++) {
01043 if (monospace != (i == FS_MONO)) continue;
01044 if (_glyph_ptr[i] == NULL) continue;
01045
01046 for (int j = 0; j < 256; j++) {
01047 if (_glyph_ptr[i][j] == NULL) continue;
01048
01049 for (int k = 0; k < 256; k++) {
01050 if (_glyph_ptr[i][j][k].duplicate) continue;
01051 free(_glyph_ptr[i][j][k].sprite);
01052 }
01053
01054 free(_glyph_ptr[i][j]);
01055 }
01056
01057 free(_glyph_ptr[i]);
01058 _glyph_ptr[i] = NULL;
01059 }
01060 }
01061
01062 static GlyphEntry *GetGlyphPtr(FontSize size, WChar key)
01063 {
01064 if (_glyph_ptr[size] == NULL) return NULL;
01065 if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) return NULL;
01066 return &_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)];
01067 }
01068
01069
01070 static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph, bool duplicate = false)
01071 {
01072 if (_glyph_ptr[size] == NULL) {
01073 DEBUG(freetype, 3, "Allocating root glyph cache for size %u", size);
01074 _glyph_ptr[size] = CallocT<GlyphEntry*>(256);
01075 }
01076
01077 if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) {
01078 DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), size);
01079 _glyph_ptr[size][GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
01080 }
01081
01082 DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, size);
01083 _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
01084 _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
01085 _glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
01086 }
01087
01088 static void *AllocateFont(size_t size)
01089 {
01090 return MallocT<byte>(size);
01091 }
01092
01093
01094
01095 static bool GetFontAAState(FontSize size)
01096 {
01097
01098 if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
01099
01100 switch (size) {
01101 default: NOT_REACHED();
01102 case FS_NORMAL: return _freetype.medium_aa;
01103 case FS_SMALL: return _freetype.small_aa;
01104 case FS_LARGE: return _freetype.large_aa;
01105 case FS_MONO: return _freetype.mono_aa;
01106 }
01107 }
01108
01109
01110 const Sprite *GetGlyph(FontSize size, WChar key)
01111 {
01112 FT_Face face = GetFontFace(size);
01113 FT_GlyphSlot slot;
01114 GlyphEntry new_glyph;
01115 GlyphEntry *glyph;
01116 SpriteLoader::Sprite sprite;
01117 int width;
01118 int height;
01119 int x;
01120 int y;
01121
01122 assert(IsPrintable(key));
01123
01124
01125 if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
01126 SpriteID sprite = GetUnicodeGlyph(size, key);
01127 if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
01128
01129
01130 if (sprite != 0) return GetSprite(sprite, ST_FONT);
01131
01132
01133 if (face == NULL) error("No sprite font and no real font either... bailing!");
01134
01135
01136 key = '?';
01137 }
01138
01139
01140 glyph = GetGlyphPtr(size, key);
01141 if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
01142
01143 slot = face->glyph;
01144
01145 bool aa = GetFontAAState(size);
01146
01147 FT_UInt glyph_index = FT_Get_Char_Index(face, key);
01148 if (glyph_index == 0) {
01149 if (key == '?') {
01150
01151 SpriteID sprite = GetUnicodeGlyph(size, key);
01152 Sprite *spr = (Sprite*)GetRawSprite(sprite, ST_FONT, AllocateFont);
01153 assert(spr != NULL);
01154 new_glyph.sprite = spr;
01155 new_glyph.width = spr->width + (size != FS_NORMAL);
01156 SetGlyphPtr(size, key, &new_glyph, false);
01157 return new_glyph.sprite;
01158 } else {
01159
01160 GetGlyph(size, '?');
01161 glyph = GetGlyphPtr(size, '?');
01162 SetGlyphPtr(size, key, glyph, true);
01163 return glyph->sprite;
01164 }
01165 }
01166 FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
01167 FT_Render_Glyph(face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
01168
01169
01170 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
01171
01172
01173 width = max(1, slot->bitmap.width + (size == FS_NORMAL));
01174 height = max(1, slot->bitmap.rows + (size == FS_NORMAL));
01175
01176
01177 if (width > 256 || height > 256) usererror("Font glyph is too large");
01178
01179
01180 sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
01181 sprite.type = ST_FONT;
01182 sprite.width = width;
01183 sprite.height = height;
01184 sprite.x_offs = slot->bitmap_left;
01185 sprite.y_offs = _ascender[size] - slot->bitmap_top;
01186
01187
01188 if (size == FS_NORMAL && !aa) {
01189 for (y = 0; y < slot->bitmap.rows; y++) {
01190 for (x = 0; x < slot->bitmap.width; x++) {
01191 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
01192 sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
01193 sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
01194 }
01195 }
01196 }
01197 }
01198
01199 for (y = 0; y < slot->bitmap.rows; y++) {
01200 for (x = 0; x < slot->bitmap.width; x++) {
01201 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
01202 sprite.data[x + y * sprite.width].m = FACE_COLOUR;
01203 sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
01204 }
01205 }
01206 }
01207
01208 new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
01209 new_glyph.width = slot->advance.x >> 6;
01210
01211 SetGlyphPtr(size, key, &new_glyph);
01212
01213 return new_glyph.sprite;
01214 }
01215
01216
01217 bool GetDrawGlyphShadow()
01218 {
01219 return GetFontFace(FS_NORMAL) != NULL && GetFontAAState(FS_NORMAL);
01220 }
01221
01222
01223 uint GetGlyphWidth(FontSize size, WChar key)
01224 {
01225 FT_Face face = GetFontFace(size);
01226 GlyphEntry *glyph;
01227
01228 if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
01229 SpriteID sprite = GetUnicodeGlyph(size, key);
01230 if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
01231 return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL && size != FS_MONO) : 0;
01232 }
01233
01234 glyph = GetGlyphPtr(size, key);
01235 if (glyph == NULL || glyph->sprite == NULL) {
01236 GetGlyph(size, key);
01237 glyph = GetGlyphPtr(size, key);
01238 }
01239
01240 return glyph->width;
01241 }
01242
01243
01244 #endif
01245
01246
01247
01248 #include "table/unicode.h"
01249
01250 static SpriteID **_unicode_glyph_map[FS_END];
01251
01252
01254 static SpriteID GetFontBase(FontSize size)
01255 {
01256 switch (size) {
01257 default: NOT_REACHED();
01258 case FS_NORMAL: return SPR_ASCII_SPACE;
01259 case FS_SMALL: return SPR_ASCII_SPACE_SMALL;
01260 case FS_LARGE: return SPR_ASCII_SPACE_BIG;
01261 case FS_MONO: return SPR_ASCII_SPACE;
01262 }
01263 }
01264
01265
01266 SpriteID GetUnicodeGlyph(FontSize size, uint32 key)
01267 {
01268 if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) return 0;
01269 return _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)];
01270 }
01271
01272
01273 void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite)
01274 {
01275 if (_unicode_glyph_map[size] == NULL) _unicode_glyph_map[size] = CallocT<SpriteID*>(256);
01276 if (_unicode_glyph_map[size][GB(key, 8, 8)] == NULL) _unicode_glyph_map[size][GB(key, 8, 8)] = CallocT<SpriteID>(256);
01277 _unicode_glyph_map[size][GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
01278 }
01279
01280
01281 void InitializeUnicodeGlyphMap()
01282 {
01283 for (FontSize size = FS_BEGIN; size != FS_END; size++) {
01284
01285 if (_unicode_glyph_map[size] != NULL) {
01286 for (uint i = 0; i < 256; i++) {
01287 free(_unicode_glyph_map[size][i]);
01288 }
01289 free(_unicode_glyph_map[size]);
01290 _unicode_glyph_map[size] = NULL;
01291 }
01292
01293 SpriteID base = GetFontBase(size);
01294
01295 for (uint i = ASCII_LETTERSTART; i < 256; i++) {
01296 SpriteID sprite = base + i - ASCII_LETTERSTART;
01297 if (!SpriteExists(sprite)) continue;
01298 SetUnicodeGlyph(size, i, sprite);
01299 SetUnicodeGlyph(size, i + SCC_SPRITE_START, sprite);
01300 }
01301
01302 for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
01303 byte key = _default_unicode_map[i].key;
01304 if (key == CLRA) {
01305
01306
01307
01308 SetUnicodeGlyph(size, _default_unicode_map[i].code, 0);
01309 } else {
01310 SpriteID sprite = base + key - ASCII_LETTERSTART;
01311 SetUnicodeGlyph(size, _default_unicode_map[i].code, sprite);
01312 }
01313 }
01314 }
01315 }