Skip to content

Commit c8ba19c

Browse files
author
Vsevolod Volkov
committed
Added respect for the "useTypoMetrics" flag
Signed-off-by: Vsevolod Volkov <st.lyn4@gmail.com>
1 parent 6b9538a commit c8ba19c

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The following properties describe the general metrics of the font. See [here](ht
8383
* `underlinePosition` - the offset from the normal underline position that should be used
8484
* `underlineThickness` - the weight of the underline that should be used
8585
* `italicAngle` - if this is an italic font, the angle the cursor should be drawn at to match the font design
86+
* `lineHeight` - is the vertical space between adjacent lines (their baselines) of text, also known as leading. See [here](https://en.wikipedia.org/wiki/Leading) for more details.
8687
* `capHeight` - the height of capital letters above the baseline. See [here](http://en.wikipedia.org/wiki/Cap_height) for more details.
8788
* `xHeight`- the height of lower case letters. See [here](http://en.wikipedia.org/wiki/X-height) for more details.
8889
* `bbox` - the font’s bounding box, i.e. the box that encloses all glyphs in the font

src/TTFFont.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,44 @@ export default class TTFFont {
9191
return result;
9292
}
9393

94+
_getMetrics() {
95+
if (this._metrics) { return this._metrics; }
96+
97+
let { 'OS/2': os2, hhea } = this;
98+
let useTypoMetrics = os2.fsSelection.useTypoMetrics;
99+
let ascent, descent, lineGap, lineHeight;
100+
101+
// Use the same approach as FreeType
102+
// https://gitlab.freedesktop.org/freetype/freetype/-/blob/4d8db130ea4342317581bab65fc96365ce806b77/src/sfnt/sfobjs.c#L1310
103+
104+
if (useTypoMetrics) {
105+
ascent = os2.typoAscender;
106+
descent = os2.typoDescender;
107+
lineGap = os2.typoLineGap;
108+
lineHeight = ascent - descent + lineGap;
109+
} else {
110+
ascent = hhea.ascent;
111+
descent = hhea.descent;
112+
lineGap = hhea.lineGap;
113+
lineHeight = ascent - descent + lineGap;
114+
}
115+
116+
if (!ascent || !descent) {
117+
if (os2.typoAscender || os2.typoDescender) {
118+
ascent = os2.typoAscender;
119+
descent = os2.typoDescender;
120+
lineGap = os2.typoLineGap;
121+
lineHeight = ascent - descent + lineGap;
122+
} else {
123+
ascent = os2.winAscent;
124+
descent = -os2.winDescent;
125+
lineHeight = ascent - descent;
126+
}
127+
}
128+
129+
return this._metrics = {ascent, descent, lineGap, lineHeight};
130+
}
131+
94132
/**
95133
* Gets a string from the font's `name` table
96134
* `lang` is a BCP-47 language code.
@@ -166,23 +204,23 @@ export default class TTFFont {
166204
* @type {number}
167205
*/
168206
get ascent() {
169-
return this.hhea.ascent;
207+
return this._getMetrics().ascent;
170208
}
171209

172210
/**
173211
* The font’s [descender](https://en.wikipedia.org/wiki/Descender)
174212
* @type {number}
175213
*/
176214
get descent() {
177-
return this.hhea.descent;
215+
return this._getMetrics().descent;
178216
}
179217

180218
/**
181219
* The amount of space that should be included between lines
182220
* @type {number}
183221
*/
184222
get lineGap() {
185-
return this.hhea.lineGap;
223+
return this._getMetrics().lineGap;
186224
}
187225

188226
/**
@@ -209,6 +247,15 @@ export default class TTFFont {
209247
return this.post.italicAngle;
210248
}
211249

250+
/**
251+
* The vertical space between adjacent lines (their baselines) of text.
252+
* See [here](https://en.wikipedia.org/wiki/Leading) for more details.
253+
* @type {number}
254+
*/
255+
get lineHeight() {
256+
return this._getMetrics().lineHeight;
257+
}
258+
212259
/**
213260
* The height of capital letters above the baseline.
214261
* See [here](https://en.wikipedia.org/wiki/Cap_height) for more details.

src/glyph/Glyph.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,12 @@ export default class Glyph {
6464

6565
let {advance:advanceWidth, bearing:leftBearing} = this._getTableMetrics(this._font.hmtx);
6666

67-
// For vertical metrics, use vmtx if available, or fall back to global data from OS/2 or hhea
67+
// For vertical metrics, use vmtx if available, or fall back to global data
6868
if (this._font.vmtx) {
6969
var {advance:advanceHeight, bearing:topBearing} = this._getTableMetrics(this._font.vmtx);
70-
7170
} else {
72-
let os2;
73-
if (typeof cbox === 'undefined' || cbox === null) { ({ cbox } = this); }
74-
75-
if ((os2 = this._font['OS/2']) && os2.version > 0) {
76-
var advanceHeight = Math.abs(os2.typoAscender - os2.typoDescender);
77-
var topBearing = os2.typoAscender - cbox.maxY;
78-
79-
} else {
80-
let { hhea } = this._font;
81-
var advanceHeight = Math.abs(hhea.ascent - hhea.descent);
82-
var topBearing = hhea.ascent - cbox.maxY;
83-
}
71+
var advanceHeight = Math.abs(this._font.ascent - this._font.descent);
72+
var topBearing = this._font.ascent - cbox.maxY;
8473
}
8574

8675
if (this._font._variationProcessor && this._font.HVAR) {

0 commit comments

Comments
 (0)