UNPKG

2.09 kBJavaScriptView Raw
1(function() {
2 var EmbeddedFont, PDFFont, StandardFont, fontkit;
3
4 fontkit = require('fontkit');
5
6 PDFFont = (function() {
7 PDFFont.open = function(document, src, family, id) {
8 var font;
9 if (typeof src === 'string') {
10 if (StandardFont.isStandardFont(src)) {
11 return new StandardFont(document, src, id);
12 }
13 font = fontkit.openSync(src, family);
14 } else if (Buffer.isBuffer(src)) {
15 font = fontkit.create(src, family);
16 } else if (src instanceof Uint8Array) {
17 font = fontkit.create(new Buffer(src), family);
18 } else if (src instanceof ArrayBuffer) {
19 font = fontkit.create(new Buffer(new Uint8Array(src)), family);
20 }
21 if (font == null) {
22 throw new Error('Not a supported font format or standard PDF font.');
23 }
24 return new EmbeddedFont(document, font, id);
25 };
26
27 function PDFFont() {
28 throw new Error('Cannot construct a PDFFont directly.');
29 }
30
31 PDFFont.prototype.encode = function(text) {
32 throw new Error('Must be implemented by subclasses');
33 };
34
35 PDFFont.prototype.widthOfString = function(text) {
36 throw new Error('Must be implemented by subclasses');
37 };
38
39 PDFFont.prototype.ref = function() {
40 return this.dictionary != null ? this.dictionary : this.dictionary = this.document.ref();
41 };
42
43 PDFFont.prototype.finalize = function() {
44 if (this.embedded || (this.dictionary == null)) {
45 return;
46 }
47 this.embed();
48 return this.embedded = true;
49 };
50
51 PDFFont.prototype.embed = function() {
52 throw new Error('Must be implemented by subclasses');
53 };
54
55 PDFFont.prototype.lineHeight = function(size, includeGap) {
56 var gap;
57 if (includeGap == null) {
58 includeGap = false;
59 }
60 gap = includeGap ? this.lineGap : 0;
61 return (this.ascender + gap - this.descender) / 1000 * size;
62 };
63
64 return PDFFont;
65
66 })();
67
68 module.exports = PDFFont;
69
70 StandardFont = require('./font/standard');
71
72 EmbeddedFont = require('./font/embedded');
73
74}).call(this);