From 31db7e468e628aba0f15956630a0ab1f019e9ea9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 22 Jul 2016 21:59:57 +0300 Subject: [PATCH] Add cap height and x height metrics to Font Since FreeType (and consequently ttf2png) doesn't provide these metrics, they are estimated by the makefont script. --- scripts/makefont.py | 25 +++++++++++++++++++++---- source/font.cpp | 6 +++++- source/font.h | 4 ++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/scripts/makefont.py b/scripts/makefont.py index 64c8e684..1d7a4332 100755 --- a/scripts/makefont.py +++ b/scripts/makefont.py @@ -1,8 +1,15 @@ #!/usr/bin/python +caps = list(range(ord("A"), ord("Z")+1)) +xchars = [ord(c) for c in "acemnorsuvwxz"] + def convert_def(fn): src = file(fn) + result = "" + header = "" + cap_height = [] + x_height = [] for line in src.readlines(): line = line.strip() if not line or line[0]=='#': @@ -12,9 +19,9 @@ def convert_def(fn): if parts[0]=="font": tw, th, fh, fa, fd = map(int, parts[1:]) - result = "native_size %d;\n"%fh - result += "ascent %.3f;\n"%(float(fa)/fh) - result += "descent %.3f;\n"%(float(fd)/fh) + header += "native_size %d;\n"%fh + header += "ascent %.3f;\n"%(float(fa)/fh) + header += "descent %.3f;\n"%(float(fd)/fh) elif parts[0]=="glyph": g, x, y, w, h, ox, oy, a = map(int, parts[1:]) result += "glyph %d\n{\n"%g @@ -23,11 +30,21 @@ def convert_def(fn): result += "\toffset %.3f %.3f;\n"%(float(ox)/fh, float(oy)/fh) result += "\tadvance %.3f;\n"%(float(a)/fh) result += "};\n" + + if g in caps: + cap_height.append(h) + elif g in xchars: + x_height.append(h) elif parts[0]=="kern": l, r, d = map(int, parts[1:]) result += "kerning %d %d %.3f;\n"%(l, r, float(d)/fh) - return result + if cap_height: + header += "cap_height %.3f;\n"%(float(cap_height[len(cap_height)*2/3])/fh) + if x_height: + header += "x_height %.3f;\n"%(float(x_height[len(x_height)*2/3])/fh) + + return header+result def make_font(fn, size, ch_range, autohinter, margin, padding): import maketex diff --git a/source/font.cpp b/source/font.cpp index 6780f5fa..54631926 100644 --- a/source/font.cpp +++ b/source/font.cpp @@ -15,7 +15,9 @@ namespace GL { Font::Font(): native_size(1), ascent(1), - descent(0) + descent(0), + cap_height(1), + x_height(0.5) { } // Avoid synthesizing ~RefPtr in files including font.h @@ -139,11 +141,13 @@ void Font::Loader::init() { add("native_size", &Font::native_size); add("ascent", &Font::ascent); + add("cap_height", &Font::cap_height); add("descent", &Font::descent); add("texture", &Loader::texture); add("texture", &Loader::texture_ref); add("glyph", &Loader::glyph); add("kerning", &Loader::kerning); + add("x_height", &Font::x_height); } void Font::Loader::glyph(unsigned c) diff --git a/source/font.h b/source/font.h index efef8351..4eaed963 100644 --- a/source/font.h +++ b/source/font.h @@ -60,6 +60,8 @@ private: float native_size; float ascent; float descent; + float cap_height; + float x_height; GlyphMap glyphs; KerningMap kerning; @@ -81,6 +83,8 @@ public: float get_ascent() const { return ascent; } float get_descent() const { return descent; } + float get_cap_height() const { return cap_height; } + float get_x_height() const { return x_height; } /** Returns the width of a string, in multiples of the font size. Scale the result according to the size used in rendering. */ -- 2.45.2