-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Decouple layout from PDF lib types
The layout phase relied on the `pdf-lib` types `PDFFont` and `PDFImage`. These types can only be created by embedding a font or image into the PDF. In preparation of loading font and image resources on demand, this commit decouples the layout process from these PDF lib types and uses only `Font` and `Image` during layout. Fonts are now only *loaded*, i.e. the fontkit font is being created before layout. The layout relies on fontkit to calculate sizes instead of the `PdfFont` instance. In the render phase, the fonts are embedded into the PDF. Likewise, images are loaded before layout to get their size and embedded into the PDF in the render phase. During render, the PDF ref is attached to the `Font` and `Image` instances.
- Loading branch information
Showing
22 changed files
with
264 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This code was borrowed from `pdf-lib` | ||
// https://github.com/Hopding/pdf-lib/blob/v1.17.1/src/core/embedders/CustomFontEmbedder.ts | ||
|
||
import { Font, TypeFeatures } from '@pdf-lib/fontkit'; | ||
|
||
export function FontMetrics(font: Font, fontFeatures?: TypeFeatures) { | ||
const scale = 1000 / font.unitsPerEm; | ||
|
||
return { font, widthOfTextAtSize, heightAtSize }; | ||
|
||
function widthOfTextAtSize(text: string, size: number): number { | ||
const { glyphs } = font.layout(text, fontFeatures); | ||
let totalWidth = 0; | ||
for (let idx = 0, len = glyphs.length; idx < len; idx++) { | ||
totalWidth += glyphs[idx].advanceWidth * scale; | ||
} | ||
return (totalWidth * size) / 1000; | ||
} | ||
|
||
function heightAtSize(size: number, options: { descender?: boolean } = {}): number { | ||
const { descender = true } = options; | ||
const { ascent, descent, bbox } = font; | ||
const yTop = (ascent || bbox.maxY) * scale; | ||
const yBottom = (descent || bbox.minY) * scale; | ||
let height = yTop - yBottom; | ||
if (!descender) height -= Math.abs(descent) || 0; | ||
return (height / 1000) * size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.