-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So far, font data had to be embedded directly within the document definition, despite being more logically associated with the renderer than the document itself. This also made it hard to inspect or debug document definitions due to the inclusion of large binary font data. This commit introduces a new `PdfMaker` class that replaces the `makePdf` function. This class allows font data to be registered separately and reused across multiple documents. With this approach, font data is no longer part of the document definition. Example: ```ts const pdfMaker = new PdfMaker(); pdfMaker.registerFont(await readFile('path/to/MyFont.ttf')); pdfMaker.registerFont(await readFile('path/to/MyFont-Bold.ttf')); const pdf1 = await pdfMaker.makePdf(doc1); const pdf2 = await pdfMaker.makePdf(doc2); ```
- Loading branch information
Showing
7 changed files
with
106 additions
and
3 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,56 @@ | ||
import { FontStore } from '../font-store.ts'; | ||
import { ImageStore } from '../image-store.ts'; | ||
import { layoutPages } from '../layout/layout.ts'; | ||
import type { MakerCtx } from '../maker-ctx.ts'; | ||
import { readDocumentDefinition } from '../read-document.ts'; | ||
import { renderDocument } from '../render/render-document.ts'; | ||
import { readAs } from '../types.ts'; | ||
import type { DocumentDefinition } from './document.ts'; | ||
import type { FontStyle, FontWeight } from './text.ts'; | ||
|
||
export type FontConfig = { | ||
name?: string; | ||
style?: FontStyle; | ||
weight?: FontWeight; | ||
}; | ||
|
||
/** | ||
* Generates PDF documents. | ||
*/ | ||
export class PdfMaker { | ||
#ctx: MakerCtx; | ||
|
||
constructor() { | ||
const fontStore = new FontStore([]); | ||
const imageStore = new ImageStore([]); | ||
this.#ctx = { fontStore, imageStore }; | ||
} | ||
|
||
/** | ||
* Registers a font to be used in generated PDFs. | ||
* | ||
* @param data The font data. Must be in OpenType (OTF) or TrueType | ||
* (TTF) format. | ||
* @param config Additional configuration of the font, only needed if | ||
* the meta data cannot be extracted from the font. | ||
*/ | ||
registerFont(data: Uint8Array, config?: FontConfig): void { | ||
this.#ctx.fontStore.registerFont(data, config); | ||
} | ||
|
||
/** | ||
* Generates a PDF from the given document definition. | ||
* | ||
* @param definition The definition of the document to generate. | ||
* @returns The generated PDF document. | ||
*/ | ||
async makePdf(definition: DocumentDefinition): Promise<Uint8Array> { | ||
const def = readAs(definition, 'definition', readDocumentDefinition); | ||
const ctx = { ...this.#ctx }; | ||
if (def.fonts) ctx.fontStore = new FontStore(def.fonts); | ||
if (def.images) ctx.imageStore = new ImageStore(def.images); | ||
if (def.dev?.guides != null) ctx.guides = def.dev.guides; | ||
const pages = await layoutPages(def, ctx); | ||
return await renderDocument(def, pages); | ||
} | ||
} |
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