summaryrefslogtreecommitdiff
path: root/src/fontcache.cpp
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2009-11-15 14:39:37 +0000
committerpeter1138 <peter1138@openttd.org>2009-11-15 14:39:37 +0000
commitf28d32943df0029ae37f818ac0209ab3679000bc (patch)
tree510305f8ec259afa29cef4144b1bc5516d498a9d /src/fontcache.cpp
parentf6ec2fa5b9f7ba9376eac96dfdcfa9e921d94363 (diff)
downloadopenttd-f28d32943df0029ae37f818ac0209ab3679000bc.tar.xz
(svn r18096) -Fix (r5079/r7158??): Use free type ascender/descender metrics to position font offset correctly.
Diffstat (limited to 'src/fontcache.cpp')
-rw-r--r--src/fontcache.cpp31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/fontcache.cpp b/src/fontcache.cpp
index 43852fa29..4966670d9 100644
--- a/src/fontcache.cpp
+++ b/src/fontcache.cpp
@@ -36,6 +36,7 @@ static FT_Library _library = NULL;
static FT_Face _face_small = NULL;
static FT_Face _face_medium = NULL;
static FT_Face _face_large = NULL;
+static int _ascender[FS_END];
FreeTypeSettings _freetype;
@@ -707,6 +708,22 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) {return FT_Err_
bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, const char *str) { return false; }
#endif /* WITH_FONTCONFIG */
+static void SetFontGeometry(FT_Face face, FontSize size, int pixels)
+{
+ FT_Set_Pixel_Sizes(face, 0, pixels);
+
+ if (FT_IS_SCALABLE(face)) {
+ int asc = face->ascender * pixels / face->units_per_EM;
+ int dec = face->descender * pixels / face->units_per_EM;
+
+ _ascender[size] = asc;
+ _font_height[size] = asc - dec;
+ } else {
+ _ascender[size] = pixels;
+ _font_height[size] = pixels;
+ }
+}
+
/**
* Loads the freetype font.
* First type to load the fontname as if it were a path. If that fails,
@@ -781,16 +798,13 @@ void InitFreeType()
/* Set each font size */
if (_face_small != NULL) {
- FT_Set_Pixel_Sizes(_face_small, 0, _freetype.small_size);
- _font_height[FS_SMALL] = _freetype.small_size;
+ SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size);
}
if (_face_medium != NULL) {
- FT_Set_Pixel_Sizes(_face_medium, 0, _freetype.medium_size);
- _font_height[FS_NORMAL] = _freetype.medium_size;
+ SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size);
}
if (_face_large != NULL) {
- FT_Set_Pixel_Sizes(_face_large, 0, _freetype.large_size);
- _font_height[FS_LARGE] = _freetype.large_size;
+ SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size);
}
}
@@ -934,7 +948,6 @@ const Sprite *GetGlyph(FontSize size, WChar key)
int height;
int x;
int y;
- int y_adj;
assert(IsPrintable(key));
@@ -968,9 +981,7 @@ const Sprite *GetGlyph(FontSize size, WChar key)
sprite.width = width;
sprite.height = height;
sprite.x_offs = slot->bitmap_left;
- /* XXX 2 should be determined somehow... it's right for the normal face */
- y_adj = (size == FS_NORMAL) ? 2 : 0;
- sprite.y_offs = GetCharacterHeight(size) - slot->bitmap_top - y_adj;
+ sprite.y_offs = _ascender[size] - slot->bitmap_top;
/* Draw shadow for medium size */
if (size == FS_NORMAL) {