Skip to content

Commit

Permalink
Added respect for the "useTypoMetrics" flag
Browse files Browse the repository at this point in the history
Signed-off-by: Vsevolod Volkov <[email protected]>
  • Loading branch information
Vsevolod Volkov committed Apr 7, 2023
1 parent 6b9538a commit c8ba19c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ The following properties describe the general metrics of the font. See [here](ht
* `underlinePosition` - the offset from the normal underline position that should be used
* `underlineThickness` - the weight of the underline that should be used
* `italicAngle` - if this is an italic font, the angle the cursor should be drawn at to match the font design
* `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.
* `capHeight` - the height of capital letters above the baseline. See [here](http://en.wikipedia.org/wiki/Cap_height) for more details.
* `xHeight`- the height of lower case letters. See [here](http://en.wikipedia.org/wiki/X-height) for more details.
* `bbox` - the font’s bounding box, i.e. the box that encloses all glyphs in the font
Expand Down
53 changes: 50 additions & 3 deletions src/TTFFont.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,44 @@ export default class TTFFont {
return result;
}

_getMetrics() {
if (this._metrics) { return this._metrics; }

let { 'OS/2': os2, hhea } = this;
let useTypoMetrics = os2.fsSelection.useTypoMetrics;
let ascent, descent, lineGap, lineHeight;

// Use the same approach as FreeType
// https://gitlab.freedesktop.org/freetype/freetype/-/blob/4d8db130ea4342317581bab65fc96365ce806b77/src/sfnt/sfobjs.c#L1310

if (useTypoMetrics) {
ascent = os2.typoAscender;
descent = os2.typoDescender;
lineGap = os2.typoLineGap;
lineHeight = ascent - descent + lineGap;
} else {
ascent = hhea.ascent;
descent = hhea.descent;
lineGap = hhea.lineGap;
lineHeight = ascent - descent + lineGap;
}

if (!ascent || !descent) {
if (os2.typoAscender || os2.typoDescender) {
ascent = os2.typoAscender;
descent = os2.typoDescender;
lineGap = os2.typoLineGap;
lineHeight = ascent - descent + lineGap;
} else {
ascent = os2.winAscent;
descent = -os2.winDescent;
lineHeight = ascent - descent;
}
}

return this._metrics = {ascent, descent, lineGap, lineHeight};
}

/**
* Gets a string from the font's `name` table
* `lang` is a BCP-47 language code.
Expand Down Expand Up @@ -166,23 +204,23 @@ export default class TTFFont {
* @type {number}
*/
get ascent() {
return this.hhea.ascent;
return this._getMetrics().ascent;
}

/**
* The font’s [descender](https://en.wikipedia.org/wiki/Descender)
* @type {number}
*/
get descent() {
return this.hhea.descent;
return this._getMetrics().descent;
}

/**
* The amount of space that should be included between lines
* @type {number}
*/
get lineGap() {
return this.hhea.lineGap;
return this._getMetrics().lineGap;
}

/**
Expand All @@ -209,6 +247,15 @@ export default class TTFFont {
return this.post.italicAngle;
}

/**
* The vertical space between adjacent lines (their baselines) of text.
* See [here](https://en.wikipedia.org/wiki/Leading) for more details.
* @type {number}
*/
get lineHeight() {
return this._getMetrics().lineHeight;
}

/**
* The height of capital letters above the baseline.
* See [here](https://en.wikipedia.org/wiki/Cap_height) for more details.
Expand Down
17 changes: 3 additions & 14 deletions src/glyph/Glyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,12 @@ export default class Glyph {

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

// For vertical metrics, use vmtx if available, or fall back to global data from OS/2 or hhea
// For vertical metrics, use vmtx if available, or fall back to global data
if (this._font.vmtx) {
var {advance:advanceHeight, bearing:topBearing} = this._getTableMetrics(this._font.vmtx);

} else {
let os2;
if (typeof cbox === 'undefined' || cbox === null) { ({ cbox } = this); }

if ((os2 = this._font['OS/2']) && os2.version > 0) {
var advanceHeight = Math.abs(os2.typoAscender - os2.typoDescender);
var topBearing = os2.typoAscender - cbox.maxY;

} else {
let { hhea } = this._font;
var advanceHeight = Math.abs(hhea.ascent - hhea.descent);
var topBearing = hhea.ascent - cbox.maxY;
}
var advanceHeight = Math.abs(this._font.ascent - this._font.descent);
var topBearing = this._font.ascent - cbox.maxY;
}

if (this._font._variationProcessor && this._font.HVAR) {
Expand Down

0 comments on commit c8ba19c

Please sign in to comment.