Skip to content

Commit 58c982b

Browse files
committed
improve ergonomics of loading AFM fonts #93
Instead of creating an instance of `Font` and providing the JSON data describing an AFM font, AFM fonts are required directly as `AFMFont` instances.
1 parent 68ea555 commit 58c982b

20 files changed

+81
-58
lines changed

docs/README.md

+28-23
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Creates a new [PDF document](document.md).
3838

3939
```js
4040
const doc = new pdf.Document({
41-
font: new pdf.Font(require('pdfjs/font/helvetica.json')),
41+
font: require('pdfjs/font/Helvetica'),
4242
padding: 10
4343
})
4444
doc.pipe(fs.createWriteStream('output.pdf'))
@@ -52,38 +52,43 @@ For an explanation of the units and different paper sizes have a look at the [un
5252

5353
### new pdf.Font(arg)
5454

55-
Creates a new AFM font pr OTF font object that can be used with PDF documents. TFont objects can be used multiple times.
55+
Creates a new OTF font object that can be used with PDF documents. Font objects can be used multiple times.
5656

5757
**Arguments:**
5858

59-
- **arg** - the font data, as either Buffer or ArrayBuffer of an OTF font or a JSON object descriping an AFM font
59+
- **arg** - the font data, as either Buffer or ArrayBuffer of an OTF font
60+
61+
```js
62+
new pdf.Font(fs.readFileSync('./opensans/regular.ttf'))
63+
```
64+
65+
### AFM fonts
66+
67+
AFM fonts are default fonts understood by every PDF reader. It is therefore not necessary to embed
68+
additional font data.
6069

6170
**Available AFM fonts:**
6271

63-
- `pdfjs/font/Courier.json`
64-
- `pdfjs/font/Courier-Bold.json`
65-
- `pdfjs/font/Courier-BoldOblique.json`
66-
- `pdfjs/font/Courier-Oblique.json`
67-
- `pdfjs/font/Helvetica.json`
68-
- `pdfjs/font/Helvetica-Bold.json`
69-
- `pdfjs/font/Helvetica-BoldOblique.json`
70-
- `pdfjs/font/Helvetica-Oblique.json`
71-
- `pdfjs/font/Symbol.json`
72-
- `pdfjs/font/Times.json`
73-
- `pdfjs/font/Times-Bold.json`
74-
- `pdfjs/font/Times-BoldItalic.json`
75-
- `pdfjs/font/Times-Italic.json`
76-
- `pdfjs/font/Times-Roman.json`
77-
- `pdfjs/font/ZapfDingbats.json`
72+
- `pdfjs/font/Courier`
73+
- `pdfjs/font/Courier-Bold`
74+
- `pdfjs/font/Courier-BoldOblique`
75+
- `pdfjs/font/Courier-Oblique`
76+
- `pdfjs/font/Helvetica`
77+
- `pdfjs/font/Helvetica-Bold`
78+
- `pdfjs/font/Helvetica-BoldOblique`
79+
- `pdfjs/font/Helvetica-Oblique`
80+
- `pdfjs/font/Symbol`
81+
- `pdfjs/font/Times`
82+
- `pdfjs/font/Times-Bold`
83+
- `pdfjs/font/Times-BoldItalic`
84+
- `pdfjs/font/Times-Italic`
85+
- `pdfjs/font/Times-Roman`
86+
- `pdfjs/font/ZapfDingbats`
7887

7988
**Examples:**
8089

8190
```js
82-
new pdf.Font(require('pdfjs/font/Helvetica.json'))
83-
```
84-
85-
```js
86-
new pdf.Font(fs.readFileSync('./opensans/regular.ttf'))
91+
require('pdfjs/font/Helvetica')
8792
```
8893

8994
### new pdf.Image(src)

font/Courier-Bold.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Courier-Bold.json'))

font/Courier-BoldOblique.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Courier-BoldOblique.json'))

font/Courier-Oblique.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Courier-Oblique.json'))

font/Courier.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Courier.json'))

font/Helvetica-Bold.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Helvetica-Bold.json'))

font/Helvetica-BoldOblique.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Helvetica-BoldOblique.json'))

font/Helvetica-Oblique.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Helvetica-Oblique.json'))

font/Helvetica.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Helvetica.json'))

font/Symbol.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Symbol.json'))

font/Times-Bold.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Times-Bold.json'))

font/Times-BoldItalic.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Times-BoldItalic.json'))

font/Times-Italic.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Times-Italic.json'))

font/Times-Roman.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./Times-Roman.json'))

font/ZapfDingbats.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const AFMFont = require('../lib/font/afm')
2+
module.exports = new AFMFont(require('./ZapfDingbats.json'))

font/afm/convert.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,21 @@ for (const filename of files) {
109109

110110
properties.widths = widths
111111

112+
const basename = path.basename(filename, '.afm')
113+
112114
fs.writeFileSync(
113-
path.join(__dirname, '../', path.basename(filename, '.afm') + '.json'),
115+
path.join(__dirname, '../', basename + '.json'),
114116
JSON.stringify(properties),
115117
{ encoding: 'utf8' }
116118
)
119+
120+
fs.writeFileSync(
121+
path.join(__dirname, '../', basename + '.js'),
122+
`const AFMFont = require('../lib/font/afm')\n` +
123+
`module.exports = new AFMFont(require('./${basename}.json'))`,
124+
{ encoding: 'utf8' }
125+
)
126+
117127
// console.log(widths)
118128
// console.log(properties)
119129
// console.log(kerning)

lib/font.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = class Font {
88
if (arg instanceof Buffer) {
99
return new OTFFont(arg)
1010
} else {
11+
console.warn("Manual construction of AFM fonts will be removed in the next version (use `require('pdfjs/font/Helvetica')` instead of `new Font(require('pdfjs/font/Helvetica.json'))`")
1112
return new AFMFont(arg)
1213
}
1314
}

test/fixtures/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ exports.create = function() {
1616
bold: new pdf.Font(openSansBold)
1717
},
1818
afm: {
19-
regular: new pdf.Font(require('../../font/Helvetica.json')),
20-
bold: new pdf.Font(require('../../font/Helvetica-Bold.json')),
21-
monoRegular: new pdf.Font(require('../../font/Courier.json')),
22-
monoBold: new pdf.Font(require('../../font/Courier-Bold.json')),
19+
regular: require('../../font/Helvetica'),
20+
bold: require('../../font/Helvetica-Bold'),
21+
monoRegular: require('../../font/Courier'),
22+
monoBold: require('../../font/Courier-Bold'),
2323
}
2424
},
2525
image: {

types/main.d.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,9 @@ declare module "pdfjs" {
1313
* TFont objects can be used multiple times.
1414
* @param arg the font data, as either Buffer or ArrayBuffer of an OTF font or a JSON object descriping an AFM font
1515
*/
16-
constructor(arg: Buffer | ArrayBuffer | FontDescription);
16+
constructor(arg: Buffer | ArrayBuffer);
1717
}
1818

19-
export type FontDescription = {
20-
fontName: string;
21-
fullName: string;
22-
familyName: string;
23-
italicAngle: number;
24-
characterSet: string;
25-
fontBBox: number[];
26-
underlinePosition: number;
27-
underlineThickness: number;
28-
kerning: any;
29-
widths: (number | null)[];
30-
};
31-
3219
export const mm: number;
3320
export const cm: number;
3421

types/pdfjs-tests.ts

+8-16
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ import * as fs from "fs"
33
import * as path from "path"
44

55

6-
const helveticaFont: pdf.Font = new pdf.Font(
7-
require("pdfjs/font/Helvetica.json")
8-
);
9-
const helveticaBoldFont: pdf.Font = new pdf.Font(
10-
require("pdfjs/font/Helvetica-Bold.json")
11-
);
12-
const timesRomanFont: pdf.Font = new pdf.Font(
13-
require("pdfjs/font/TimesRoman.json")
14-
);
6+
const helveticaFont: pdf.Font = require("pdfjs/font/Helvetica");
7+
const helveticaBoldFont: pdf.Font = require("pdfjs/font/Helvetica-Bold");
8+
const timesRomanFont: pdf.Font = require("pdfjs/font/TimesRoman");
159

1610
const src: Buffer = readFileSync("tor-logo.jpg");
1711
const logo: pdf.Image = new pdf.Image(src);
@@ -21,7 +15,7 @@ let doc: pdf.Document;
2115
// from docs
2216
// ReadMe
2317
doc = new pdf.Document({
24-
font: new pdf.Font(require("pdfjs/font/helvetica.json")),
18+
font: require("pdfjs/font/helvetica"),
2519
padding: 10
2620
});
2721
doc.pipe(createWriteStream("output.pdf"));
@@ -32,8 +26,6 @@ async () => {
3226
await doc.end();
3327
};
3428

35-
new pdf.Font(require("pdfjs/font/Helvetica.json"));
36-
3729
new pdf.Font(readFileSync("./opensans/regular.ttf"));
3830

3931
const image1 = readFileSync("image.jpg");
@@ -242,10 +234,10 @@ const f = {
242234
bold: new pdf.Font(openSansBold)
243235
},
244236
afm: {
245-
regular: new pdf.Font(require('../../font/Helvetica.json')),
246-
bold: new pdf.Font(require('../../font/Helvetica-Bold.json')),
247-
monoRegular: new pdf.Font(require('../../font/Courier.json')),
248-
monoBold: new pdf.Font(require('../../font/Courier-Bold.json')),
237+
regular: require('../../font/Helvetica'),
238+
bold: require('../../font/Helvetica-Bold'),
239+
monoRegular: require('../../font/Courier'),
240+
monoBold: require('../../font/Courier-Bold'),
249241
}
250242
},
251243
image: {

0 commit comments

Comments
 (0)