-
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 an 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. Before layout, fonts are now only *loaded*, i.e. the `fontkit` font is being created. To calculate text sizes, the layout now relies only on `fontkit` instead of a `PDFFont` instance. The fonts are embedded into the PDF only in the render phase. Likewise, images are loaded before layout and embedded into the PDF in the render phase. They need to be loaded in advance to get their size.
- Loading branch information
Showing
23 changed files
with
269 additions
and
191 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,20 @@ | ||
import { Font } from '@pdf-lib/fontkit'; | ||
|
||
export function getTextWidth(text: string, font: Font, fontSize: number): number { | ||
const { glyphs } = font.layout(text); | ||
const scale = 1000 / font.unitsPerEm; | ||
let totalWidth = 0; | ||
for (let idx = 0, len = glyphs.length; idx < len; idx++) { | ||
totalWidth += glyphs[idx].advanceWidth * scale; | ||
} | ||
return (totalWidth * fontSize) / 1000; | ||
} | ||
|
||
export function getTextHeight(font: Font, fontSize: number): number { | ||
const { ascent, descent, bbox } = font; | ||
const scale = 1000 / font.unitsPerEm; | ||
const yTop = (ascent || bbox.maxY) * scale; | ||
const yBottom = (descent || bbox.minY) * scale; | ||
const height = yTop - yBottom; | ||
return (height / 1000) * fontSize; | ||
} |
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.