Skip to content

Commit

Permalink
Add sizeOfFontAtHeight() methods (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding authored May 4, 2019
1 parent a4235ee commit 987706f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/core/pdf-structures/factories/PDFEmbeddedFontFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class PDFEmbeddedFontFactory {
* Measures the height of this font at a particular size. Note that the height
* of the font is independent of the particular glyphs being displayed, so
* this method does not accept a `text` param, like
* [[PDFStandardFontFactory.widthOfTextAtSize]] does.
* [[PDFEmbeddedFontFactory.widthOfTextAtSize]] does.
*/
heightOfFontAtSize = (size: number) => {
const { ascent, descent, bbox } = this.font;
Expand All @@ -162,6 +162,19 @@ class PDFEmbeddedFontFactory {
return ((yTop - yBottom) / 1000) * size;
};

/**
* Measures the size of this font at a particular height. Note that the size
* of the font is independent of the particular glyphs being displayed, so
* this method does not accept a `text` param, like
* [[PDFEmbeddedFontFactory.widthOfTextAtSize]] does.
*/
sizeOfFontAtHeight = (height: number): number => {
const { ascent, descent, bbox } = this.font;
const yTop = (ascent || bbox.maxY) * this.scale;
const yBottom = (descent || bbox.minY) * this.scale;
return (1000 * height) / (yTop - yBottom);
};

private embedFontDictionaryIn = (pdfDoc: PDFDocument, fontName: string) => {
const cidFontDictRef = this.embedCIDFontDictionaryIn(pdfDoc, fontName);
const unicodeCMap = this.embedUnicodeCmapIn(pdfDoc);
Expand Down
13 changes: 13 additions & 0 deletions src/core/pdf-structures/factories/PDFStandardFontFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ class PDFStandardFontFactory {
return ((yTop - yBottom) / 1000) * size;
};

/**
* Measures the size of this font at a particular height. Note that the size
* of the font is independent of the particular glyphs being displayed, so
* this method does not accept a `text` param, like
* [[PDFStandardFontFactory.widthOfTextAtSize]] does.
*/
sizeOfFontAtHeight = (height: number): number => {
const { Ascender, Descender, FontBBox } = this.font;
const yTop = Ascender || FontBBox[3];
const yBottom = Descender || FontBBox[1];
return (1000 * height) / (yTop - yBottom);
};

// We'll default to 250 if our font metrics don't specify a width
private widthOfGlyph = (glyphName: string) =>
this.font.getWidthOfGlyph(glyphName) || 250;
Expand Down

0 comments on commit 987706f

Please sign in to comment.