Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to embed entire font #1335

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions lib/font/embedded.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
var fs = require('fs');
import PDFFont from '../font';

const toHex = function(num) {
return `0000${num.toString(16)}`.slice(-4);
};

class EmbeddedFont extends PDFFont {
constructor(document, font, id) {
constructor(document, font, id, subset = true) {
super();
this.document = document;
this.font = font;
this.id = id;
this.subset = this.font.createSubset();

this.subset = subset
this.subsetFont = this.subset && this.font.createSubset();
this.unicode = [[0]];
this.widths = [this.font.getGlyph(0).advanceWidth];

if (this.subset == false) {
this.font.characterSet.forEach(codePoint => {
if (this.font.hasGlyphForCodePoint(codePoint)) {
const glyph = this.font.glyphForCodePoint(codePoint);
this.unicode[glyph.id] = [codePoint];
this.widths[glyph.id] = (glyph.advanceWidth * this.scale);
}
});
}

this.name = this.font.postscriptName;
this.scale = 1000 / this.font.unitsPerEm;
this.ascender = this.font.ascent * this.scale;
Expand Down Expand Up @@ -100,14 +113,17 @@ class EmbeddedFont extends PDFFont {
const res = [];
for (let i = 0; i < glyphs.length; i++) {
const glyph = glyphs[i];
const gid = this.subset.includeGlyph(glyph.id);
const gid = this.subset ? this.subsetFont.includeGlyph(glyph.id) : glyph.id;

res.push(`0000${gid.toString(16)}`.slice(-4));

if (this.widths[gid] == null) {
this.widths[gid] = glyph.advanceWidth * this.scale;
}
if (this.unicode[gid] == null) {
this.unicode[gid] = glyph.codePoints;
if (this.subset) {
if (this.widths[gid] == null) {
this.widths[gid] = glyph.advanceWidth * this.scale;
}
if (this.unicode[gid] == null) {
this.unicode[gid] = glyph.codePoints;
}
}
}

Expand All @@ -121,15 +137,14 @@ class EmbeddedFont extends PDFFont {
}

embed() {
const isCFF = this.subset.cff != null;
const isCFF = (this.subsetFont || this.font).cff != null;
const fontFile = this.document.ref();

if (isCFF) {
fontFile.data.Subtype = 'CIDFontType0C';
}

this.subset
.encodeStream()
(this.subset ? this.font.encodeStream() : fs.createReadStream(this.font.filename))
.on('data', data => fontFile.write(data))
.on('end', () => fontFile.end());

Expand Down
4 changes: 2 additions & 2 deletions lib/font_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import StandardFont from './font/standard';
import EmbeddedFont from './font/embedded';

class PDFFontFactory {
static open(document, src, family, id) {
static open(document, src, family, id, subset = true) {
let font;
if (typeof src === 'string') {
if (StandardFont.isStandardFont(src)) {
Expand All @@ -25,7 +25,7 @@ class PDFFontFactory {
throw new Error('Not a supported font format or standard PDF font.');
}

return new EmbeddedFont(document, font, id);
return new EmbeddedFont(document, font, id, subset);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/mixins/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
}
},

font(src, family, size) {
font(src, family, size, subset) {
let cacheKey, font;
if (typeof family === 'number') {
size = family;
Expand Down Expand Up @@ -48,7 +48,7 @@ export default {

// load the font
const id = `F${++this._fontCount}`;
this._font = PDFFontFactory.open(this, src, family, id);
this._font = PDFFontFactory.open(this, src, family, id, subset);

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