From 9157a51774c11bed8a7f851717ccec5cda0e6c64 Mon Sep 17 00:00:00 2001 From: Ralf Sternberg Date: Sat, 12 Oct 2024 14:08:13 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Merge=20FontLoader=20w?= =?UTF-8?q?ith=20FontStore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fonts aren't actually _loaded_, as the data is already part of the font definition. The class `FontLoader` actually _selected_ the font from the set of registered fonts. The separation between `FontLoader` and `FontStore` is not necessary. This commit merges the two classes into `FontStore`. The interface is kept async in preparation for loading fonts on demand. --- src/api/make-pdf.ts | 5 +- src/font-loader.test.ts | 234 +++++++++++++----------------- src/font-loader.ts | 136 ++++++++--------- src/image-loader.test.ts | 6 +- src/layout/layout-columns.test.ts | 4 +- src/layout/layout-rows.test.ts | 4 +- src/layout/layout-text.test.ts | 4 +- src/layout/layout.test.ts | 4 +- src/text.test.ts | 4 +- 9 files changed, 177 insertions(+), 224 deletions(-) diff --git a/src/api/make-pdf.ts b/src/api/make-pdf.ts index 9043cf0..9ccd729 100644 --- a/src/api/make-pdf.ts +++ b/src/api/make-pdf.ts @@ -1,4 +1,4 @@ -import { FontLoader, FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-loader.ts'; import { ImageLoader, ImageStore } from '../image-loader.ts'; import { layoutPages } from '../layout/layout.ts'; import { readDocumentDefinition } from '../read-document.ts'; @@ -14,9 +14,8 @@ import type { DocumentDefinition } from './document.ts'; */ export async function makePdf(definition: DocumentDefinition): Promise { const def = readAs(definition, 'definition', readDocumentDefinition); - const fontLoader = new FontLoader(def.fonts ?? []); + const fontStore = new FontStore(def.fonts ?? []); const imageLoader = new ImageLoader(def.images ?? []); - const fontStore = new FontStore(fontLoader); const imageStore = new ImageStore(imageLoader); const guides = !!def.dev?.guides; const ctx = { fontStore, imageStore, guides }; diff --git a/src/font-loader.test.ts b/src/font-loader.test.ts index b9a44fc..bfcd57e 100644 --- a/src/font-loader.test.ts +++ b/src/font-loader.test.ts @@ -1,22 +1,23 @@ import fontkit from '@pdf-lib/fontkit'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { FontLoader, FontStore } from './font-loader.ts'; -import type { Font, FontDef, FontSelector } from './fonts.ts'; -import { fakeFont, mkData } from './test/test-utils.ts'; - -describe('font-loader', () => { - let normalFont: FontDef; - let italicFont: FontDef; - let obliqueFont: FontDef; - let boldFont: FontDef; - let italicBoldFont: FontDef; - let obliqueBoldFont: FontDef; - let otherFont: FontDef; - let fontLoader: FontLoader; - - describe('new FontLoader', () => { +import { FontStore } from './font-loader.ts'; +import type { FontDef } from './fonts.ts'; +import { mkData } from './test/test-utils.ts'; + +describe('font-store', () => { + describe('FontStore', () => { + let normalFont: FontDef; + let italicFont: FontDef; + let obliqueFont: FontDef; + let boldFont: FontDef; + let italicBoldFont: FontDef; + let obliqueBoldFont: FontDef; + let otherFont: FontDef; + let store: FontStore; + beforeEach(() => { + vi.spyOn(fontkit, 'create').mockReturnValue({ fake: true } as any); normalFont = fakeFontDef('Test'); italicFont = fakeFontDef('Test', { style: 'italic' }); obliqueFont = fakeFontDef('Test', { style: 'oblique' }); @@ -24,160 +25,138 @@ describe('font-loader', () => { italicBoldFont = fakeFontDef('Test', { style: 'italic', weight: 700 }); obliqueBoldFont = fakeFontDef('Test', { style: 'oblique', weight: 700 }); otherFont = fakeFontDef('Other'); - fontLoader = new FontLoader([normalFont, italicFont, boldFont, italicBoldFont, otherFont]); + + store = new FontStore([ + normalFont, + italicFont, + obliqueFont, + boldFont, + italicBoldFont, + obliqueBoldFont, + otherFont, + ]); + }); + + afterEach(() => { + vi.restoreAllMocks(); }); - it('rejects when no fonts defined', () => { - const loader = new FontLoader([]); + it('rejects when no fonts defined', async () => { + const store = new FontStore([]); - expect(() => loader.loadFont({})).toThrowError('No fonts defined'); + await expect(store.selectFont({ fontFamily: 'Foo' })).rejects.toThrow( + expect.objectContaining({ + message: "Could not load font for 'Foo', style=normal, weight=normal", + cause: new Error('No fonts defined'), + }), + ); }); - it('rejects for unknown font name', () => { - expect(() => fontLoader.loadFont({ fontFamily: 'Unknown' })).toThrowError( - "No font defined for 'Unknown'", + it('rejects for unknown font name', async () => { + await expect(store.selectFont({ fontFamily: 'Unknown' })).rejects.toThrow( + expect.objectContaining({ + message: "Could not load font for 'Unknown', style=normal, weight=normal", + cause: new Error("No font defined for 'Unknown'"), + }), ); }); - it('rejects when no matching font style can be found', () => { - expect(() => fontLoader.loadFont({ fontFamily: 'Other', fontStyle: 'italic' })).toThrowError( - "No font defined for 'Other', style=italic", + it('rejects when no matching font style can be found', async () => { + store = new FontStore([normalFont, boldFont]); + + await expect(store.selectFont({ fontFamily: 'Test', fontStyle: 'italic' })).rejects.toThrow( + expect.objectContaining({ + message: "Could not load font for 'Test', style=italic, weight=normal", + cause: new Error("No font defined for 'Test', style=italic"), + }), ); }); it('selects different font variants', async () => { const fontFamily = 'Test'; - expect(await fontLoader.loadFont({ fontFamily })).toEqual({ - name: 'Test', - data: normalFont.data, - }); - expect(await fontLoader.loadFont({ fontFamily, fontWeight: 'bold' })).toEqual({ - name: 'Test', - data: boldFont.data, - }); - expect(await fontLoader.loadFont({ fontFamily, fontStyle: 'italic' })).toEqual({ - name: 'Test', - data: italicFont.data, - }); - expect( - await fontLoader.loadFont({ fontFamily, fontStyle: 'italic', fontWeight: 'bold' }), - ).toEqual({ - name: 'Test', - data: italicBoldFont.data, - }); - }); - - it('selects first matching font if no family specified', () => { - expect(fontLoader.loadFont({})).toEqual({ - name: 'Test', - data: normalFont.data, - }); - expect(fontLoader.loadFont({ fontWeight: 'bold' })).toEqual({ - name: 'Test', - data: boldFont.data, - }); - expect(fontLoader.loadFont({ fontStyle: 'italic' })).toEqual({ - name: 'Test', - data: italicFont.data, - }); - expect(fontLoader.loadFont({ fontStyle: 'italic', fontWeight: 'bold' })).toEqual({ - name: 'Test', - data: italicBoldFont.data, - }); + await expect(store.selectFont({ fontFamily })).resolves.toEqual( + expect.objectContaining({ data: normalFont.data }), + ); + await expect(store.selectFont({ fontFamily, fontWeight: 'bold' })).resolves.toEqual( + expect.objectContaining({ data: boldFont.data }), + ); + await expect(store.selectFont({ fontFamily, fontStyle: 'italic' })).resolves.toEqual( + expect.objectContaining({ data: italicFont.data }), + ); + await expect( + store.selectFont({ fontFamily, fontStyle: 'italic', fontWeight: 'bold' }), + ).resolves.toEqual(expect.objectContaining({ data: italicBoldFont.data })); }); - it('selects font with matching font family', () => { - expect(fontLoader.loadFont({ fontFamily: 'Other' })).toEqual({ - name: 'Other', - data: otherFont.data, - }); + it('selects first matching font if no family specified', async () => { + await expect(store.selectFont({})).resolves.toEqual( + expect.objectContaining({ data: normalFont.data }), + ); + await expect(store.selectFont({ fontWeight: 'bold' })).resolves.toEqual( + expect.objectContaining({ data: boldFont.data }), + ); + await expect(store.selectFont({ fontStyle: 'italic' })).resolves.toEqual( + expect.objectContaining({ data: italicFont.data }), + ); + await expect(store.selectFont({ fontStyle: 'italic', fontWeight: 'bold' })).resolves.toEqual( + expect.objectContaining({ data: italicBoldFont.data }), + ); }); - it('falls back to oblique when no italic font can be found', () => { - fontLoader = new FontLoader([normalFont, obliqueFont, boldFont, obliqueBoldFont]); - expect(fontLoader.loadFont({ fontFamily: 'Test', fontStyle: 'italic' })).toEqual({ - name: 'Test', - data: obliqueFont.data, - }); + it('selects font with matching font family', async () => { + await expect(store.selectFont({ fontFamily: 'Other' })).resolves.toEqual( + expect.objectContaining({ data: otherFont.data }), + ); }); - it('falls back to italic when no oblique font can be found', () => { - expect(fontLoader.loadFont({ fontFamily: 'Test', fontStyle: 'oblique' })).toEqual({ - name: 'Test', - data: italicFont.data, - }); - }); + it('falls back to oblique when no italic font can be found', async () => { + store = new FontStore([normalFont, obliqueFont, boldFont, obliqueBoldFont]); - it('falls back when no matching font weight can be found', () => { - expect(fontLoader.loadFont({ fontFamily: 'Other', fontWeight: 'bold' })).toEqual({ - name: 'Other', - data: otherFont.data, - }); - expect(fontLoader.loadFont({ fontFamily: 'Other', fontWeight: 200 })).toEqual({ - name: 'Other', - data: otherFont.data, - }); + await expect(store.selectFont({ fontFamily: 'Test', fontStyle: 'italic' })).resolves.toEqual( + expect.objectContaining({ data: obliqueFont.data }), + ); }); - }); - describe('FontStore', () => { - let testFont: Font; - let fontLoader: FontLoader; + it('falls back to italic when no oblique font can be found', async () => { + store = new FontStore([normalFont, italicFont, boldFont, italicBoldFont]); - beforeEach(() => { - testFont = fakeFont('Test'); - fontLoader = new FontLoader([]); - fontLoader.loadFont = vi.fn((selector: FontSelector) => { - if (selector.fontFamily === 'Test') return testFont; - throw new Error('No such font defined'); - }); - vi.spyOn(fontkit, 'create').mockReturnValue({ fake: true } as any); + await expect(store.selectFont({ fontFamily: 'Test', fontStyle: 'italic' })).resolves.toEqual( + expect.objectContaining({ data: italicFont.data }), + ); }); - afterEach(() => { - vi.restoreAllMocks(); + it('falls back when no matching font weight can be found', async () => { + await expect(store.selectFont({ fontFamily: 'Other', fontWeight: 'bold' })).resolves.toEqual( + expect.objectContaining({ data: otherFont.data }), + ); + await expect(store.selectFont({ fontFamily: 'Other', fontWeight: 200 })).resolves.toEqual( + expect.objectContaining({ data: otherFont.data }), + ); }); it('rejects if font could not be loaded', async () => { - const store = new FontStore(fontLoader); - - await expect(() => store.selectFont({ fontFamily: 'foo' })).rejects.toThrow( + await expect(store.selectFont({ fontFamily: 'foo' })).rejects.toThrow( expect.objectContaining({ message: "Could not load font for 'foo', style=normal, weight=normal", - cause: new Error('No such font defined'), + cause: new Error("No font defined for 'foo'"), }), ); }); it('creates fontkit font object', async () => { - const store = new FontStore(fontLoader); - const font = await store.selectFont({ fontFamily: 'Test' }); expect(font).toEqual({ name: 'Test', style: 'normal', weight: 400, - data: testFont.data, + data: normalFont.data, fkFont: { fake: true }, }); }); - it('calls font loader only once per selector', async () => { - const store = new FontStore(fontLoader); - - await store.selectFont({ fontFamily: 'Test' }); - await store.selectFont({ fontFamily: 'Test', fontStyle: 'italic' }); - await store.selectFont({ fontFamily: 'Test' }); - await store.selectFont({ fontFamily: 'Test', fontStyle: 'italic' }); - - expect(fontLoader.loadFont).toHaveBeenCalledTimes(2); - }); - it('returns same font object for concurrent calls', async () => { - const store = new FontStore(fontLoader); - const [font1, font2] = await Promise.all([ store.selectFont({ fontFamily: 'Test' }), store.selectFont({ fontFamily: 'Test' }), @@ -185,17 +164,6 @@ describe('font-loader', () => { expect(font1).toBe(font2); }); - - it('caches errors from font loader', async () => { - const store = new FontStore(fontLoader); - - await expect(() => store.selectFont({ fontFamily: 'foo' })).rejects.toThrow( - expect.objectContaining({ - message: "Could not load font for 'foo', style=normal, weight=normal", - cause: new Error('No such font defined'), - }), - ); - }); }); }); diff --git a/src/font-loader.ts b/src/font-loader.ts index ca8cd6c..36d095c 100644 --- a/src/font-loader.ts +++ b/src/font-loader.ts @@ -6,54 +6,77 @@ import type { Font, FontDef, FontSelector } from './fonts.ts'; import { weightToNumber } from './fonts.ts'; import { pickDefined } from './types.ts'; -export type LoadedFont = { - name: string; - data: Uint8Array; -}; - -export class FontLoader { +export class FontStore { readonly #fontDefs: FontDef[]; + readonly #fontCache: Record> = {}; constructor(fontDefs: FontDef[]) { this.#fontDefs = fontDefs; } - loadFont(selector: FontSelector): LoadedFont { - if (!this.#fontDefs.length) { - throw new Error('No fonts defined'); - } - const fontsWithMatchingFamily = selector.fontFamily - ? this.#fontDefs.filter((def) => def.family === selector.fontFamily) - : this.#fontDefs; - if (!fontsWithMatchingFamily.length) { - throw new Error(`No font defined for '${selector.fontFamily}'`); - } - let fontsWithMatchingStyle = fontsWithMatchingFamily.filter( - (def) => def.style === (selector.fontStyle ?? 'normal'), - ); - if (!fontsWithMatchingStyle.length) { - fontsWithMatchingStyle = fontsWithMatchingFamily.filter( - (def) => - (def.style === 'italic' && selector.fontStyle === 'oblique') || - (def.style === 'oblique' && selector.fontStyle === 'italic'), - ); - } - if (!fontsWithMatchingStyle.length) { - const { fontFamily: family, fontStyle: style } = selector; - const selectorStr = `'${family}', style=${style ?? 'normal'}`; - throw new Error(`No font defined for ${selectorStr}`); - } - const selected = selectFontForWeight(fontsWithMatchingStyle, selector.fontWeight ?? 'normal'); - if (!selected) { + async selectFont(selector: FontSelector): Promise { + const cacheKey = [ + selector.fontFamily ?? 'any', + selector.fontStyle ?? 'normal', + selector.fontWeight ?? 'normal', + ].join(':'); + try { + return await (this.#fontCache[cacheKey] ??= this.loadFont(selector)); + } catch (error) { const { fontFamily: family, fontStyle: style, fontWeight: weight } = selector; const selectorStr = `'${family}', style=${style ?? 'normal'}, weight=${weight ?? 'normal'}`; - throw new Error(`No font defined for ${selectorStr}`); + throw new Error(`Could not load font for ${selectorStr}`, { cause: error }); } - return pickDefined({ - name: selected.family, - data: toUint8Array(selected.data), - }); } + + loadFont(selector: FontSelector): Promise { + const selectedFont = selectFont(this.#fontDefs, selector); + const data = toUint8Array(selectedFont.data); + const fkFont = fontkit.create(data); + return Promise.resolve( + pickDefined({ + name: fkFont.fullName ?? fkFont.postscriptName ?? selectedFont.family, + data, + style: selector.fontStyle ?? 'normal', + weight: weightToNumber(selector.fontWeight ?? 400), + fkFont, + }), + ); + } +} + +function selectFont(fontDefs: FontDef[], selector: FontSelector): FontDef { + if (!fontDefs.length) { + throw new Error('No fonts defined'); + } + const fontsWithMatchingFamily = selector.fontFamily + ? fontDefs.filter((def) => def.family === selector.fontFamily) + : fontDefs; + if (!fontsWithMatchingFamily.length) { + throw new Error(`No font defined for '${selector.fontFamily}'`); + } + let fontsWithMatchingStyle = fontsWithMatchingFamily.filter( + (def) => def.style === (selector.fontStyle ?? 'normal'), + ); + if (!fontsWithMatchingStyle.length) { + fontsWithMatchingStyle = fontsWithMatchingFamily.filter( + (def) => + (def.style === 'italic' && selector.fontStyle === 'oblique') || + (def.style === 'oblique' && selector.fontStyle === 'italic'), + ); + } + if (!fontsWithMatchingStyle.length) { + const { fontFamily: family, fontStyle: style } = selector; + const selectorStr = `'${family}', style=${style ?? 'normal'}`; + throw new Error(`No font defined for ${selectorStr}`); + } + const selected = selectFontForWeight(fontsWithMatchingStyle, selector.fontWeight ?? 'normal'); + if (!selected) { + const { fontFamily: family, fontStyle: style, fontWeight: weight } = selector; + const selectorStr = `'${family}', style=${style ?? 'normal'}, weight=${weight ?? 'normal'}`; + throw new Error(`No font defined for ${selectorStr}`); + } + return selected; } function selectFontForWeight(fonts: FontDef[], weight: FontWeight): FontDef | undefined { @@ -86,40 +109,3 @@ function selectFontForWeight(fonts: FontDef[], weight: FontWeight): FontDef | un } throw new Error(`Could not find font for weight ${weight}`); } - -export class FontStore { - readonly #fontLoader: FontLoader; - readonly #fontCache: Record> = {}; - - constructor(fontLoader: FontLoader) { - this.#fontLoader = fontLoader; - } - - selectFont(selector: FontSelector): Promise { - const cacheKey = [ - selector.fontFamily ?? 'any', - selector.fontStyle ?? 'normal', - selector.fontWeight ?? 'normal', - ].join(':'); - return (this.#fontCache[cacheKey] ??= this.loadFont(selector)); - } - - async loadFont(selector: FontSelector): Promise { - let loadedFont: LoadedFont; - try { - loadedFont = await this.#fontLoader.loadFont(selector); - } catch (error) { - const { fontFamily: family, fontStyle: style, fontWeight: weight } = selector; - const selectorStr = `'${family}', style=${style ?? 'normal'}, weight=${weight ?? 'normal'}`; - throw new Error(`Could not load font for ${selectorStr}`, { cause: error }); - } - const fkFont = fontkit.create(loadedFont.data); - return pickDefined({ - name: loadedFont.name, - data: loadedFont.data, - style: selector.fontStyle ?? 'normal', - weight: weightToNumber(selector.fontWeight ?? 400), - fkFont, - }); - } -} diff --git a/src/image-loader.test.ts b/src/image-loader.test.ts index 447eb1a..21acc69 100644 --- a/src/image-loader.test.ts +++ b/src/image-loader.test.ts @@ -21,7 +21,7 @@ describe('image-loader', () => { it('rejects if image cannot be loaded', async () => { const loader = new ImageLoader([]); - await expect(() => loader.loadImage({ name: 'foo' })).rejects.toThrow( + await expect(loader.loadImage({ name: 'foo' })).rejects.toThrow( expect.objectContaining({ message: "Could not load image 'foo'", cause: new Error("ENOENT: no such file or directory, open 'foo'"), @@ -67,7 +67,7 @@ describe('image-loader', () => { it('rejects if image could not be loaded', async () => { const store = new ImageStore(imageLoader); - await expect(() => store.selectImage({ name: 'foo' })).rejects.toThrow( + await expect(store.selectImage({ name: 'foo' })).rejects.toThrow( expect.objectContaining({ message: "Could not load image 'foo'", cause: new Error('No such image'), @@ -126,7 +126,7 @@ describe('image-loader', () => { it('caches errors from image loader', async () => { const store = new ImageStore(imageLoader); - await expect(() => store.selectImage({ name: 'foo' })).rejects.toThrow( + await expect(store.selectImage({ name: 'foo' })).rejects.toThrow( expect.objectContaining({ message: "Could not load image 'foo'", cause: new Error('No such image'), diff --git a/src/layout/layout-columns.test.ts b/src/layout/layout-columns.test.ts index ba1509d..d016fab 100644 --- a/src/layout/layout-columns.test.ts +++ b/src/layout/layout-columns.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import type { Box } from '../box.ts'; -import { FontLoader, FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-loader.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import type { Block } from '../read-block.ts'; import { fakeFont, span } from '../test/test-utils.ts'; @@ -12,7 +12,7 @@ describe('layout-columns', () => { let box: Box; beforeEach(() => { - const fontStore = new FontStore(new FontLoader([])); + const fontStore = new FontStore([]); fontStore.selectFont = () => { return Promise.resolve(fakeFont('Test')); }; diff --git a/src/layout/layout-rows.test.ts b/src/layout/layout-rows.test.ts index 40951ce..dbde535 100644 --- a/src/layout/layout-rows.test.ts +++ b/src/layout/layout-rows.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import type { Box } from '../box.ts'; -import { FontLoader, FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-loader.ts'; import type { Frame } from '../frame.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import type { Block } from '../read-block.ts'; @@ -13,7 +13,7 @@ describe('layout-rows', () => { let box: Box; beforeEach(() => { - const fontStore = new FontStore(new FontLoader([])); + const fontStore = new FontStore([]); fontStore.selectFont = () => Promise.resolve(fakeFont('Test')); ctx = { fontStore } as MakerCtx; box = { x: 20, y: 30, width: 400, height: 700 }; diff --git a/src/layout/layout-text.test.ts b/src/layout/layout-text.test.ts index 84d5443..f9e315f 100644 --- a/src/layout/layout-text.test.ts +++ b/src/layout/layout-text.test.ts @@ -2,7 +2,7 @@ import { rgb } from 'pdf-lib'; import { beforeEach, describe, expect, it } from 'vitest'; import type { Box } from '../box.ts'; -import { FontLoader, FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-loader.ts'; import type { Font, FontSelector } from '../fonts.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import { extractTextRows, fakeFont, range, span } from '../test/test-utils.ts'; @@ -16,7 +16,7 @@ describe('layout-text', () => { beforeEach(() => { defaultFont = fakeFont('Test'); const italicFont = fakeFont('Test', { style: 'italic' }); - const fontStore = new FontStore(new FontLoader([])); + const fontStore = new FontStore([]); fontStore.selectFont = (selector: FontSelector) => { return Promise.resolve(selector.fontStyle === 'italic' ? italicFont : defaultFont); }; diff --git a/src/layout/layout.test.ts b/src/layout/layout.test.ts index c747da5..3d0b826 100644 --- a/src/layout/layout.test.ts +++ b/src/layout/layout.test.ts @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import { paperSizes } from '../api/sizes.ts'; import type { Box } from '../box.ts'; -import { FontLoader, FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-loader.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import type { Block, TextAttrs, TextSpan } from '../read-block.ts'; import type { PageInfo } from '../read-document.ts'; @@ -17,7 +17,7 @@ describe('layout', () => { let box: Box; beforeEach(() => { - const fontStore = new FontStore(new FontLoader([])); + const fontStore = new FontStore([]); fontStore.loadFont = () => Promise.resolve(fakeFont('Test')); ctx = { fontStore } as MakerCtx; box = { x: 20, y: 30, width: 400, height: 700 }; diff --git a/src/text.test.ts b/src/text.test.ts index 514aecf..fad7816 100644 --- a/src/text.test.ts +++ b/src/text.test.ts @@ -1,7 +1,7 @@ import { rgb } from 'pdf-lib'; import { beforeEach, describe, expect, it } from 'vitest'; -import { FontLoader, FontStore } from './font-loader.ts'; +import { FontStore } from './font-loader.ts'; import type { Font } from './fonts.ts'; import { fakeFont } from './test/test-utils.ts'; import type { TextSegment } from './text.ts'; @@ -20,7 +20,7 @@ describe('text', () => { beforeEach(() => { normalFont = fakeFont('Test'); const italicFont = fakeFont('Test', { style: 'italic' }); - fontStore = new FontStore(new FontLoader([])); + fontStore = new FontStore([]); fontStore.selectFont = (selector) => { return Promise.resolve(selector.fontStyle === 'italic' ? italicFont : normalFont); }; From 09da34f7a315bc2d47662f8d2bc2612de55de46b Mon Sep 17 00:00:00 2001 From: Ralf Sternberg Date: Sat, 12 Oct 2024 14:20:41 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20module=20font?= =?UTF-8?q?-loader=20to=20font-store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to merging `FontLoader` into `FontStore` classes. --- src/api/make-pdf.ts | 2 +- src/{font-loader.test.ts => font-store.test.ts} | 2 +- src/{font-loader.ts => font-store.ts} | 0 src/layout/layout-columns.test.ts | 2 +- src/layout/layout-rows.test.ts | 2 +- src/layout/layout-text.test.ts | 2 +- src/layout/layout.test.ts | 2 +- src/maker-ctx.ts | 2 +- src/text.test.ts | 2 +- src/text.ts | 2 +- 10 files changed, 9 insertions(+), 9 deletions(-) rename src/{font-loader.test.ts => font-store.test.ts} (99%) rename src/{font-loader.ts => font-store.ts} (100%) diff --git a/src/api/make-pdf.ts b/src/api/make-pdf.ts index 9ccd729..994b98a 100644 --- a/src/api/make-pdf.ts +++ b/src/api/make-pdf.ts @@ -1,4 +1,4 @@ -import { FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-store.ts'; import { ImageLoader, ImageStore } from '../image-loader.ts'; import { layoutPages } from '../layout/layout.ts'; import { readDocumentDefinition } from '../read-document.ts'; diff --git a/src/font-loader.test.ts b/src/font-store.test.ts similarity index 99% rename from src/font-loader.test.ts rename to src/font-store.test.ts index bfcd57e..67e7176 100644 --- a/src/font-loader.test.ts +++ b/src/font-store.test.ts @@ -1,7 +1,7 @@ import fontkit from '@pdf-lib/fontkit'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { FontStore } from './font-loader.ts'; +import { FontStore } from './font-store.ts'; import type { FontDef } from './fonts.ts'; import { mkData } from './test/test-utils.ts'; diff --git a/src/font-loader.ts b/src/font-store.ts similarity index 100% rename from src/font-loader.ts rename to src/font-store.ts diff --git a/src/layout/layout-columns.test.ts b/src/layout/layout-columns.test.ts index d016fab..b2bf47b 100644 --- a/src/layout/layout-columns.test.ts +++ b/src/layout/layout-columns.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import type { Box } from '../box.ts'; -import { FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-store.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import type { Block } from '../read-block.ts'; import { fakeFont, span } from '../test/test-utils.ts'; diff --git a/src/layout/layout-rows.test.ts b/src/layout/layout-rows.test.ts index dbde535..b5b5467 100644 --- a/src/layout/layout-rows.test.ts +++ b/src/layout/layout-rows.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import type { Box } from '../box.ts'; -import { FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-store.ts'; import type { Frame } from '../frame.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import type { Block } from '../read-block.ts'; diff --git a/src/layout/layout-text.test.ts b/src/layout/layout-text.test.ts index f9e315f..177db04 100644 --- a/src/layout/layout-text.test.ts +++ b/src/layout/layout-text.test.ts @@ -2,7 +2,7 @@ import { rgb } from 'pdf-lib'; import { beforeEach, describe, expect, it } from 'vitest'; import type { Box } from '../box.ts'; -import { FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-store.ts'; import type { Font, FontSelector } from '../fonts.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import { extractTextRows, fakeFont, range, span } from '../test/test-utils.ts'; diff --git a/src/layout/layout.test.ts b/src/layout/layout.test.ts index 3d0b826..157f9fe 100644 --- a/src/layout/layout.test.ts +++ b/src/layout/layout.test.ts @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import { paperSizes } from '../api/sizes.ts'; import type { Box } from '../box.ts'; -import { FontStore } from '../font-loader.ts'; +import { FontStore } from '../font-store.ts'; import type { MakerCtx } from '../maker-ctx.ts'; import type { Block, TextAttrs, TextSpan } from '../read-block.ts'; import type { PageInfo } from '../read-document.ts'; diff --git a/src/maker-ctx.ts b/src/maker-ctx.ts index c16d0d3..e62c7f4 100644 --- a/src/maker-ctx.ts +++ b/src/maker-ctx.ts @@ -1,4 +1,4 @@ -import type { FontStore } from './font-loader.ts'; +import type { FontStore } from './font-store.ts'; import type { ImageStore } from './image-loader.ts'; export type MakerCtx = { diff --git a/src/text.test.ts b/src/text.test.ts index fad7816..b754e81 100644 --- a/src/text.test.ts +++ b/src/text.test.ts @@ -1,7 +1,7 @@ import { rgb } from 'pdf-lib'; import { beforeEach, describe, expect, it } from 'vitest'; -import { FontStore } from './font-loader.ts'; +import { FontStore } from './font-store.ts'; import type { Font } from './fonts.ts'; import { fakeFont } from './test/test-utils.ts'; import type { TextSegment } from './text.ts'; diff --git a/src/text.ts b/src/text.ts index 219e551..307df81 100644 --- a/src/text.ts +++ b/src/text.ts @@ -1,6 +1,6 @@ import type { FontStyle, FontWeight } from './api/text.ts'; -import type { FontStore } from './font-loader.ts'; import { getTextHeight, getTextWidth } from './font-metrics.ts'; +import type { FontStore } from './font-store.ts'; import type { Font } from './fonts.ts'; import type { TextSpan } from './read-block.ts'; import type { Color } from './read-color.ts';