Skip to content

Commit 79fd1bc

Browse files
authored
refactor(pdfkit): align font.js with upstream pdfkit (#3388)
Move factory logic (PDFFont.open) from font.js into font_factory.js, matching the upstream pdfkit structure where font.js is only the base class.
1 parent f069bca commit 79fd1bc

4 files changed

Lines changed: 23 additions & 50 deletions

File tree

packages/pdfkit/src/font.js

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
1-
import * as fontkit from 'fontkit';
2-
import createStandardFont from './font/standard';
3-
import createEmbeddedFont from './font/embedded';
4-
5-
export class PDFFont {
6-
static open(document, src, family, id) {
7-
let font;
8-
9-
if (typeof src === 'string') {
10-
if (StandardFont.isStandardFont(src)) {
11-
return new StandardFont(document, src, id);
12-
}
13-
14-
if (!BROWSER) {
15-
font = fontkit.openSync(src, family);
16-
} else {
17-
throw new Error(`Can't open ${src} in browser build`);
18-
}
19-
} else if (src instanceof Uint8Array) {
20-
font = fontkit.create(src, family);
21-
} else if (src instanceof ArrayBuffer) {
22-
font = fontkit.create(new Uint8Array(src), family);
23-
} else if (typeof src === 'object') {
24-
font = src;
25-
}
26-
27-
if (font == null) {
28-
throw new Error('Not a supported font format or standard PDF font.');
29-
}
30-
31-
return new EmbeddedFont(document, font, id);
32-
}
1+
class PDFFont {
2+
constructor() {}
333

344
encode() {
355
throw new Error('Must be implemented by subclasses');
@@ -51,23 +21,17 @@ export class PDFFont {
5121
}
5222

5323
this.embed();
54-
return (this.embedded = true);
24+
this.embedded = true;
5525
}
5626

5727
embed() {
5828
throw new Error('Must be implemented by subclasses');
5929
}
6030

61-
lineHeight(size, includeGap) {
62-
if (includeGap == null) {
63-
includeGap = false;
64-
}
31+
lineHeight(size, includeGap = false) {
6532
const gap = includeGap ? this.lineGap : 0;
6633
return ((this.ascender + gap - this.descender) / 1000) * size;
6734
}
6835
}
6936

70-
export const StandardFont = createStandardFont(PDFFont);
71-
export const EmbeddedFont = createEmbeddedFont(PDFFont);
72-
7337
export default PDFFont;

packages/pdfkit/src/font_factory.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
import fs from 'fs';
21
import * as fontkit from 'fontkit';
3-
import StandardFont from './font/standard';
4-
import EmbeddedFont from './font/embedded';
2+
import PDFFont from './font';
3+
import createStandardFont from './font/standard';
4+
import createEmbeddedFont from './font/embedded';
5+
6+
const StandardFont = createStandardFont(PDFFont);
7+
const EmbeddedFont = createEmbeddedFont(PDFFont);
58

69
class PDFFontFactory {
710
static open(document, src, family, id) {
811
let font;
12+
913
if (typeof src === 'string') {
1014
if (StandardFont.isStandardFont(src)) {
1115
return new StandardFont(document, src, id);
1216
}
1317

14-
src = fs.readFileSync(src);
15-
}
16-
if (src instanceof Uint8Array) {
18+
if (!BROWSER) {
19+
font = fontkit.openSync(src, family);
20+
} else {
21+
throw new Error(`Can't open ${src} in browser build`);
22+
}
23+
} else if (src instanceof Uint8Array) {
1724
font = fontkit.create(src, family);
1825
} else if (src instanceof ArrayBuffer) {
1926
font = fontkit.create(new Uint8Array(src), family);
27+
} else if (typeof src === 'object') {
28+
font = src;
2029
}
2130

2231
if (font == null) {

packages/pdfkit/src/mixins/fonts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import PDFFont from '../font';
1+
import PDFFontFactory from '../font_factory';
22

33
export default {
44
initFonts() {
@@ -47,7 +47,7 @@ export default {
4747

4848
// load the font
4949
const id = `F${++this._fontCount}`;
50-
this._font = PDFFont.open(this, src, family, id);
50+
this._font = PDFFontFactory.open(this, src, family, id);
5151

5252
// check for existing font familes with the same name already in the PDF
5353
// useful if the font was passed as a buffer

packages/pdfkit/tests/font/standard.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import StandardFont from '../../src/font.js';
3+
import PDFFontFactory from '../../src/font_factory.js';
44

55
describe('standard fonts', () => {
66
it('should resolve advanceWidth of soft hyphen to be zero', () => {
77
const SOFT_HYPHEN = '\u00AD';
8-
const font = StandardFont.open({}, 'Helvetica', 'Helvetica', 'foobar');
8+
const font = PDFFontFactory.open({}, 'Helvetica', 'Helvetica', 'foobar');
99

1010
expect(font.encode(SOFT_HYPHEN)[1][0].advanceWidth).toBe(0);
1111
});

0 commit comments

Comments
 (0)