Skip to content

Commit e1f3085

Browse files
committed
refactor(vfs): external fonts
1 parent 6016d60 commit e1f3085

3 files changed

Lines changed: 16 additions & 38 deletions

File tree

packages/pdfeasy/src/resolvers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
RunOptionsBase,
1010
Color,
1111
Fonts,
12+
FontKey,
1213
} from './types'
1314
import type PDFDocumentWithTables from 'pdfkit-table'
1415
import { HEXToCMYK } from './schemas'
@@ -345,10 +346,7 @@ export const resolveFontFamily = (
345346
return font
346347
}
347348

348-
export const resolveFontName = (
349-
name: string,
350-
type: 'normal' | 'italic' | 'bold' | 'bolditalic'
351-
) => {
349+
export const resolveFontName = (name: string, type: FontKey) => {
352350
switch (type) {
353351
case 'normal':
354352
return name

packages/pdfeasy/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export type LocalFonts =
145145

146146
export type Fonts<T extends string = string> = LocalFonts | T
147147

148+
export type FontKey = 'normal' | 'italic' | 'bold' | 'bolditalic'
149+
148150
export type ItemType =
149151
| 'paragraph'
150152
| 'image'

packages/pdfeasy/src/vfs.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PDFEasy } from './runner'
33
import path from 'path'
44
import { regex } from './utils'
55
import { resolveFontName } from './resolvers'
6+
import { FontKey } from './types'
67

78
export const setExternalFonts = async (instance: PDFEasy) => {
89
const fontTarget = (str: string) =>
@@ -30,40 +31,17 @@ export const setExternalFonts = async (instance: PDFEasy) => {
3031
instance.runOptions?.type === 'server' && !regex().http(font)
3132

3233
for (const font of instance.fonts) {
33-
const normal = await getBase64ByURL(
34-
isLocalServer(font.normal) ? fontTarget(font.normal) : font.normal,
35-
'arraybuffer'
36-
)
37-
const italic = await getBase64ByURL(
38-
isLocalServer(font.italic) ? fontTarget(font.italic) : font.italic,
39-
'arraybuffer'
40-
)
41-
const bold = await getBase64ByURL(
42-
isLocalServer(font.bold) ? fontTarget(font.bold) : font.bold,
43-
'arraybuffer'
44-
)
45-
const bolditalic = await getBase64ByURL(
46-
isLocalServer(font.bolditalic)
47-
? fontTarget(font.bolditalic)
48-
: font.bolditalic,
49-
'arraybuffer'
50-
)
34+
const keys = ['normal', 'italic', 'bold', 'bolditalic'] as FontKey[]
5135

52-
await instance.pdfkit?.registerFont(
53-
resolveFontName(font.name, 'normal'),
54-
normal
55-
)
56-
await instance.pdfkit?.registerFont(
57-
resolveFontName(font.name, 'italic'),
58-
italic
59-
)
60-
await instance.pdfkit?.registerFont(
61-
resolveFontName(font.name, 'bold'),
62-
bold
63-
)
64-
await instance.pdfkit?.registerFont(
65-
resolveFontName(font.name, 'bolditalic'),
66-
bolditalic
67-
)
36+
for (const key of keys) {
37+
const item = font[key]
38+
39+
const url = await getBase64ByURL(
40+
isLocalServer(item) ? fontTarget(item) : item,
41+
'arraybuffer'
42+
)
43+
44+
await instance.pdfkit?.registerFont(resolveFontName(font.name, key), url)
45+
}
6846
}
6947
}

0 commit comments

Comments
 (0)