@@ -5,7 +5,6 @@ import type { PDFEmbeddedFont } from '@ralfstx/pdf-core';
55import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
66
77import { FontStore } from './font-store.ts' ;
8- import type { FontDef } from './fonts.ts' ;
98import { mkData } from './test/test-utils.ts' ;
109
1110vi . mock ( '@ralfstx/pdf-core' , async ( importOriginal ) => {
@@ -39,14 +38,6 @@ vi.mock('@ralfstx/pdf-core', async (importOriginal) => {
3938} ) ;
4039
4140describe ( 'FontStore' , ( ) => {
42- let normalFont : FontDef ;
43- let italicFont : FontDef ;
44- let obliqueFont : FontDef ;
45- let boldFont : FontDef ;
46- let italicBoldFont : FontDef ;
47- let obliqueBoldFont : FontDef ;
48- let otherFont : FontDef ;
49-
5041 describe ( 'registerFont' , ( ) => {
5142 let robotoRegular : Uint8Array ;
5243 let robotoLightItalic : Uint8Array ;
@@ -93,23 +84,7 @@ describe('FontStore', () => {
9384 let store : FontStore ;
9485
9586 beforeEach ( ( ) => {
96- normalFont = fakeFontDef ( 'Test' ) ;
97- italicFont = fakeFontDef ( 'Test' , { style : 'italic' } ) ;
98- obliqueFont = fakeFontDef ( 'Test' , { style : 'oblique' } ) ;
99- boldFont = fakeFontDef ( 'Test' , { weight : 700 } ) ;
100- italicBoldFont = fakeFontDef ( 'Test' , { style : 'italic' , weight : 700 } ) ;
101- obliqueBoldFont = fakeFontDef ( 'Test' , { style : 'oblique' , weight : 700 } ) ;
102- otherFont = fakeFontDef ( 'Other' ) ;
103-
104- store = new FontStore ( [
105- normalFont ,
106- italicFont ,
107- obliqueFont ,
108- boldFont ,
109- italicBoldFont ,
110- obliqueBoldFont ,
111- otherFont ,
112- ] ) ;
87+ store = createTestStore ( ) ;
11388 } ) ;
11489
11590 afterEach ( ( ) => {
@@ -137,7 +112,9 @@ describe('FontStore', () => {
137112 } ) ;
138113
139114 it ( 'rejects when no matching font style can be found' , async ( ) => {
140- store = new FontStore ( [ normalFont , boldFont ] ) ;
115+ const store = new FontStore ( ) ;
116+ registerFakeFont ( store , 'Test' ) ;
117+ registerFakeFont ( store , 'Test' , { weight : 700 } ) ;
141118
142119 await expect ( store . selectFont ( { fontFamily : 'Test' , fontStyle : 'italic' } ) ) . rejects . toThrow (
143120 new Error ( "Could not load font for 'Test', style=italic, weight=normal" , {
@@ -181,15 +158,23 @@ describe('FontStore', () => {
181158 } ) ;
182159
183160 it ( 'falls back to oblique when no italic font can be found' , async ( ) => {
184- store = new FontStore ( [ normalFont , obliqueFont , boldFont , obliqueBoldFont ] ) ;
161+ const store = new FontStore ( ) ;
162+ registerFakeFont ( store , 'Test' ) ;
163+ registerFakeFont ( store , 'Test' , { style : 'oblique' } ) ;
164+ registerFakeFont ( store , 'Test' , { weight : 700 } ) ;
165+ registerFakeFont ( store , 'Test' , { style : 'oblique' , weight : 700 } ) ;
185166
186167 await expect ( store . selectFont ( { fontFamily : 'Test' , fontStyle : 'italic' } ) ) . resolves . toEqual (
187168 expect . objectContaining ( { name : 'MockFont:Test:oblique:400' } ) ,
188169 ) ;
189170 } ) ;
190171
191172 it ( 'falls back to italic when no oblique font can be found' , async ( ) => {
192- store = new FontStore ( [ normalFont , italicFont , boldFont , italicBoldFont ] ) ;
173+ const store = new FontStore ( ) ;
174+ registerFakeFont ( store , 'Test' ) ;
175+ registerFakeFont ( store , 'Test' , { style : 'italic' } ) ;
176+ registerFakeFont ( store , 'Test' , { weight : 700 } ) ;
177+ registerFakeFont ( store , 'Test' , { style : 'italic' , weight : 700 } ) ;
193178
194179 const font = await store . selectFont ( { fontFamily : 'Test' , fontStyle : 'italic' } ) ;
195180
@@ -228,9 +213,25 @@ describe('FontStore', () => {
228213 } ) ;
229214} ) ;
230215
231- function fakeFontDef ( family : string , options ?: Partial < FontDef > ) : FontDef {
216+ function registerFakeFont (
217+ store : FontStore ,
218+ family : string ,
219+ options ?: { style ?: string ; weight ?: number } ,
220+ ) {
232221 const style = options ?. style ?? 'normal' ;
233222 const weight = options ?. weight ?? 400 ;
234- const data = options ?. data ?? mkData ( [ family , style , weight ] . join ( ':' ) ) ;
235- return { family, style, weight, data } ;
223+ const data = mkData ( [ family , style , weight ] . join ( ':' ) ) ;
224+ store . registerFont ( data , { family, style : style as 'normal' , weight } ) ;
225+ }
226+
227+ function createTestStore ( ) : FontStore {
228+ const store = new FontStore ( ) ;
229+ registerFakeFont ( store , 'Test' ) ;
230+ registerFakeFont ( store , 'Test' , { style : 'italic' } ) ;
231+ registerFakeFont ( store , 'Test' , { style : 'oblique' } ) ;
232+ registerFakeFont ( store , 'Test' , { weight : 700 } ) ;
233+ registerFakeFont ( store , 'Test' , { style : 'italic' , weight : 700 } ) ;
234+ registerFakeFont ( store , 'Test' , { style : 'oblique' , weight : 700 } ) ;
235+ registerFakeFont ( store , 'Other' ) ;
236+ return store ;
236237}
0 commit comments