forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bpampuch#221 from TW-QEN/qen-fixToManyCharacterIssue
Embed fonts multiple times to allow more than 92 characters.
- Loading branch information
Showing
6 changed files
with
345 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* jslint node: true */ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var FontWrapper = require('./fontWrapper'); | ||
|
||
function typeName(bold, italics){ | ||
var type = 'normal'; | ||
if (bold && italics) type = 'bolditalics'; | ||
else if (bold) type = 'bold'; | ||
else if (italics) type = 'italics'; | ||
return type; | ||
} | ||
|
||
function FontProvider(fontDescriptors, pdfDoc) { | ||
this.fonts = {}; | ||
this.pdfDoc = pdfDoc; | ||
this.fontWrappers = {}; | ||
|
||
for(var font in fontDescriptors) { | ||
if (fontDescriptors.hasOwnProperty(font)) { | ||
var fontDef = fontDescriptors[font]; | ||
|
||
this.fonts[font] = { | ||
normal: fontDef.normal, | ||
bold: fontDef.bold, | ||
italics: fontDef.italics, | ||
bolditalics: fontDef.bolditalics | ||
}; | ||
} | ||
} | ||
} | ||
|
||
FontProvider.prototype.provideFont = function(familyName, bold, italics) { | ||
if (!this.fonts[familyName]) return this.pdfDoc._font; | ||
var type = typeName(bold, italics); | ||
|
||
this.fontWrappers[familyName] = this.fontWrappers[familyName] || {}; | ||
|
||
if (!this.fontWrappers[familyName][type]) { | ||
this.fontWrappers[familyName][type] = new FontWrapper(this.pdfDoc, this.fonts[familyName][type], familyName + '(' + type + ')'); | ||
} | ||
|
||
return this.fontWrappers[familyName][type]; | ||
}; | ||
|
||
FontProvider.prototype.setFontRefsToPdfDoc = function(){ | ||
var self = this; | ||
|
||
_.each(self.fontWrappers, function(fontFamily) { | ||
_.each(fontFamily, function(fontWrapper){ | ||
_.each(fontWrapper.pdfFonts, function(font){ | ||
if (!self.pdfDoc.page.fonts[font.id]) { | ||
self.pdfDoc.page.fonts[font.id] = font.ref(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = FontProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* jslint node: true */ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
|
||
function FontWrapper(pdfkitDoc, path, fontName){ | ||
this.MAX_CHAR_TYPES = 92; | ||
|
||
this.pdfkitDoc = pdfkitDoc; | ||
this.path = path; | ||
this.pdfFonts = []; | ||
this.charCatalogue = []; | ||
this.name = fontName; | ||
|
||
this.__defineGetter__('ascender', function(){ | ||
var font = this.getFont(0); | ||
return font.ascender; | ||
}); | ||
this.__defineGetter__('decender', function(){ | ||
var font = this.getFont(0); | ||
return font.decender; | ||
}); | ||
|
||
} | ||
// private | ||
|
||
FontWrapper.prototype.getFont = function(index){ | ||
if(!this.pdfFonts[index]){ | ||
|
||
var pseudoName = this.name + index; | ||
|
||
if(this.postscriptName){ | ||
delete this.pdfkitDoc._fontFamilies[this.postscriptName]; | ||
} | ||
|
||
this.pdfFonts[index] = this.pdfkitDoc.font(this.path, pseudoName)._font; | ||
if(!this.postscriptName){ | ||
this.postscriptName = this.pdfFonts[index].name; | ||
} | ||
} | ||
|
||
return this.pdfFonts[index]; | ||
}; | ||
|
||
// public | ||
FontWrapper.prototype.widthOfString = function(){ | ||
var font = this.getFont(0); | ||
return font.widthOfString.apply(font, arguments); | ||
}; | ||
|
||
FontWrapper.prototype.lineHeight = function(){ | ||
var font = this.getFont(0); | ||
return font.lineHeight.apply(font, arguments); | ||
}; | ||
|
||
FontWrapper.prototype.ref = function(){ | ||
var font = this.getFont(0); | ||
return font.ref.apply(font, arguments); | ||
}; | ||
|
||
var toCharCode = function(char){ | ||
return char.charCodeAt(0); | ||
}; | ||
|
||
FontWrapper.prototype.encode = function(text){ | ||
var self = this; | ||
|
||
var charTypesInInline = _.chain(text.split('')).map(toCharCode).uniq().value(); | ||
if (charTypesInInline.length > self.MAX_CHAR_TYPES) { | ||
throw new Error('Inline has more than '+ self.MAX_CHAR_TYPES + ': ' + text + ' different character types and therefore cannot be properly embedded into pdf.'); | ||
} | ||
|
||
|
||
var characterFitInFontWithIndex = function (charCatalogue) { | ||
|
||
return _.uniq(charCatalogue.concat(charTypesInInline)).length <= self.MAX_CHAR_TYPES; | ||
}; | ||
|
||
var index = _.findIndex(self.charCatalogue, characterFitInFontWithIndex); | ||
|
||
if(index < 0){ | ||
index = self.charCatalogue.length; | ||
self.charCatalogue[index] = []; | ||
} | ||
|
||
var font = this.getFont(index); | ||
font.use(text); | ||
|
||
_.each(charTypesInInline, function(charCode){ | ||
if(!_.includes(self.charCatalogue[index], charCode)){ | ||
self.charCatalogue[index].push(charCode); | ||
} | ||
}); | ||
|
||
var encodedText = _.map(font.encode(text), function (char) { | ||
return char.charCodeAt(0).toString(16); | ||
}).join(''); | ||
|
||
return { | ||
encodedText: encodedText, | ||
fontId: font.id | ||
}; | ||
}; | ||
|
||
|
||
module.exports = FontWrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.