From 9512838a5e9a4ad6f008bd0423e1056eb15cb242 Mon Sep 17 00:00:00 2001 From: rubidium Date: Sun, 20 Nov 2011 11:59:36 +0000 Subject: (svn r23275) -Codechange: allow loading of the monospace (freetype) font at another moment than the other fonts --- src/fontcache.cpp | 83 +++++++++++++++++++++++++++++++++---------------------- src/fontcache.h | 4 +-- src/openttd.cpp | 4 +-- src/strings.cpp | 4 +-- 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 1f4fa33b1..7c020eeb2 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -25,13 +25,17 @@ int _font_height[FS_END]; /** * Reset the font sizes to the defaults of the sprite based fonts. + * @param monospace Whether to reset the monospace or regular fonts. */ -void ResetFontSizes() +void ResetFontSizes(bool monospace) { - _font_height[FS_MONO] = 10; - _font_height[FS_SMALL] = 6; - _font_height[FS_NORMAL] = 10; - _font_height[FS_LARGE] = 18; + if (monospace) { + _font_height[FS_MONO] = 10; + } else { + _font_height[FS_SMALL] = 6; + _font_height[FS_NORMAL] = 10; + _font_height[FS_LARGE] = 18; + } } #ifdef WITH_FREETYPE @@ -821,7 +825,7 @@ static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *t } -static void ResetGlyphCache(); +static void ResetGlyphCache(bool monospace); /** * Unload a face and set it to NULL. @@ -837,16 +841,20 @@ static void UnloadFace(FT_Face *face) /** * (Re)initialize the freetype related things, i.e. load the non-sprite fonts. + * @param monospace Whether to initialise the monospace or regular fonts. */ -void InitFreeType() +void InitFreeType(bool monospace) { - ResetFontSizes(); - ResetGlyphCache(); + ResetFontSizes(monospace); + ResetGlyphCache(monospace); - UnloadFace(&_face_mono); - UnloadFace(&_face_small); - UnloadFace(&_face_medium); - UnloadFace(&_face_large); + if (monospace) { + UnloadFace(&_face_mono); + } else { + UnloadFace(&_face_small); + UnloadFace(&_face_medium); + UnloadFace(&_face_large); + } if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font) && StrEmpty(_freetype.mono_font)) { DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead"); @@ -863,23 +871,27 @@ void InitFreeType() } /* Load each font */ - LoadFreeTypeFont(_freetype.mono_font , &_face_mono, "mono"); - LoadFreeTypeFont(_freetype.small_font, &_face_small, "small"); - LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium"); - LoadFreeTypeFont(_freetype.large_font, &_face_large, "large"); - - /* Set each font size */ - if (_face_mono != NULL) { - SetFontGeometry(_face_mono, FS_MONO, _freetype.mono_size); - } - if (_face_small != NULL) { - SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size); - } - if (_face_medium != NULL) { - SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size); - } - if (_face_large != NULL) { - SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size); + if (monospace) { + LoadFreeTypeFont(_freetype.mono_font , &_face_mono, "mono"); + + if (_face_mono != NULL) { + SetFontGeometry(_face_mono, FS_MONO, _freetype.mono_size); + } + } else { + LoadFreeTypeFont(_freetype.small_font, &_face_small, "small"); + LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium"); + LoadFreeTypeFont(_freetype.large_font, &_face_large, "large"); + + /* Set each font size */ + if (_face_small != NULL) { + SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size); + } + if (_face_medium != NULL) { + SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size); + } + if (_face_large != NULL) { + SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size); + } } } @@ -888,7 +900,8 @@ void InitFreeType() */ void UninitFreeType() { - ResetGlyphCache(); + ResetGlyphCache(true); + ResetGlyphCache(false); UnloadFace(&_face_small); UnloadFace(&_face_medium); @@ -933,10 +946,14 @@ struct GlyphEntry { */ static GlyphEntry **_glyph_ptr[FS_END]; -/** Clear the complete cache */ -static void ResetGlyphCache() +/** + * Clear the complete cache + * @param monospace Whether to reset the monospace or regular font. + */ +static void ResetGlyphCache(bool monospace) { for (FontSize i = FS_BEGIN; i < FS_END; i++) { + if (monospace != (i == FS_MONO)) continue; if (_glyph_ptr[i] == NULL) continue; for (int j = 0; j < 256; j++) { diff --git a/src/fontcache.h b/src/fontcache.h index 708070a7e..bfb547332 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -42,7 +42,7 @@ struct FreeTypeSettings { extern FreeTypeSettings _freetype; -void InitFreeType(); +void InitFreeType(bool monospace); void UninitFreeType(); const Sprite *GetGlyph(FontSize size, uint32 key); uint GetGlyphWidth(FontSize size, uint32 key); @@ -63,7 +63,7 @@ bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, i #else /* Stub for initializiation */ -static inline void InitFreeType() { extern void ResetFontSizes(); ResetFontSizes(); } +static inline void InitFreeType(bool monospace) { extern void ResetFontSizes(bool monospace); ResetFontSizes(monospace); } static inline void UninitFreeType() {} /** Get the Sprite for a glyph */ diff --git a/src/openttd.cpp b/src/openttd.cpp index 98025a4ab..a0c16f910 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -697,8 +697,8 @@ int ttd_main(int argc, char *argv[]) /* enumerate language files */ InitializeLanguagePacks(); - /* Initialize FreeType */ - InitFreeType(); + /* Initialize the regular font for FreeType */ + InitFreeType(false); /* This must be done early, since functions use the SetWindowDirty* calls */ InitWindowSystem(); diff --git a/src/strings.cpp b/src/strings.cpp index dd549bdfc..eadde4edc 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1750,7 +1750,7 @@ const char *GetCurrentLanguageIsoCode() */ bool MissingGlyphSearcher::FindMissingGlyphs(const char **str) { - InitFreeType(); + InitFreeType(false); const Sprite *question_mark[FS_END]; for (FontSize size = FS_BEGIN; size < FS_END; size++) { @@ -1856,7 +1856,7 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher) /* Our fallback font does miss characters too, so keep the * user chosen font as that is more likely to be any good than * the wild guess we made */ - InitFreeType(); + InitFreeType(false); } } #endif -- cgit v1.2.3-54-g00ecf