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

Try to skip mapping of missing TrueType and CIDFontType2 glyphs #5651

Merged
merged 3 commits into from
Feb 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 45 additions & 21 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,10 @@ var ToUnicodeMap = (function ToUnicodeMapClosure() {
}
},

has: function(i) {
return this._map[i] !== undefined;
},

get: function(i) {
return this._map[i];
},
Expand Down Expand Up @@ -2232,6 +2236,10 @@ var IdentityToUnicodeMap = (function IdentityToUnicodeMapClosure() {
}
},

has: function (i) {
return this.firstChar <= i && i <= this.lastChar;
},

get: function (i) {
if (this.firstChar <= i && i <= this.lastChar) {
return String.fromCharCode(i);
Expand Down Expand Up @@ -2664,7 +2672,6 @@ var Font = (function FontClosure() {
var isSymbolic = !!(properties.flags & FontFlags.Symbolic);
var isIdentityUnicode =
properties.toUnicode instanceof IdentityToUnicodeMap;
var isCidFontType2 = (properties.type === 'CIDFontType2');
var newMap = Object.create(null);
var toFontChar = [];
var usedFontCharCodes = [];
Expand All @@ -2675,17 +2682,11 @@ var Font = (function FontClosure() {
var fontCharCode = originalCharCode;
// First try to map the value to a unicode position if a non identity map
// was created.
if (!isIdentityUnicode) {
if (toUnicode.get(originalCharCode) !== undefined) {
var unicode = toUnicode.get(fontCharCode);
// TODO: Try to map ligatures to the correct spot.
if (unicode.length === 1) {
fontCharCode = unicode.charCodeAt(0);
}
} else if (isCidFontType2) {
// For CIDFontType2, move characters not present in toUnicode
// to the private use area (fixes bug 1028735 and issue 4881).
fontCharCode = nextAvailableFontCharCode;
if (!isIdentityUnicode && toUnicode.has(originalCharCode)) {
var unicode = toUnicode.get(fontCharCode);
// TODO: Try to map ligatures to the correct spot.
if (unicode.length === 1) {
fontCharCode = unicode.charCodeAt(0);
}
}
// Try to move control characters, special characters and already mapped
Expand Down Expand Up @@ -3526,6 +3527,7 @@ var Font = (function FontClosure() {
var newGlyfData = new Uint8Array(oldGlyfDataLength);
var startOffset = itemDecode(locaData, 0);
var writeOffset = 0;
var missingGlyphData = {};
itemEncode(locaData, 0, writeOffset);
var i, j;
for (i = 0, j = itemSize; i < numGlyphs; i++, j += itemSize) {
Expand All @@ -3543,6 +3545,10 @@ var Font = (function FontClosure() {
continue;
}

if (startOffset === endOffset) {
missingGlyphData[i] = true;
}

var newLength = sanitizeGlyph(oldGlyfData, startOffset, endOffset,
newGlyfData, writeOffset, hintsValid);
writeOffset += newLength;
Expand All @@ -3559,7 +3565,7 @@ var Font = (function FontClosure() {
itemEncode(locaData, j, simpleGlyph.length);
}
glyf.data = simpleGlyph;
return;
return missingGlyphData;
}

if (dupFirstEntry) {
Expand All @@ -3576,6 +3582,7 @@ var Font = (function FontClosure() {
} else {
glyf.data = newGlyfData.subarray(0, writeOffset);
}
return missingGlyphData;
}

function readPostScriptTable(post, properties, maxpNumGlyphs) {
Expand Down Expand Up @@ -4035,11 +4042,13 @@ var Font = (function FontClosure() {

sanitizeHead(tables.head, numGlyphs, isTrueType ? tables.loca.length : 0);

var missingGlyphs = {};
if (isTrueType) {
var isGlyphLocationsLong = int16(tables.head.data[50],
tables.head.data[51]);
sanitizeGlyphLocations(tables.loca, tables.glyf, numGlyphs,
isGlyphLocationsLong, hintsValid, dupFirstEntry);
missingGlyphs = sanitizeGlyphLocations(tables.loca, tables.glyf,
numGlyphs, isGlyphLocationsLong,
hintsValid, dupFirstEntry);
}

if (!tables.hhea) {
Expand All @@ -4061,19 +4070,33 @@ var Font = (function FontClosure() {
}
}

var charCodeToGlyphId = [], charCode;
var charCodeToGlyphId = [], charCode, toUnicode = properties.toUnicode;

function hasGlyph(glyphId, charCode) {
if (!missingGlyphs[glyphId]) {
return true;
}
if (charCode >= 0 && toUnicode.has(charCode)) {
return true;
}
return false;
}

if (properties.type === 'CIDFontType2') {
var cidToGidMap = properties.cidToGidMap || [];
var cidToGidMapLength = cidToGidMap.length;
var isCidToGidMapEmpty = cidToGidMap.length === 0;

properties.cMap.forEach(function(charCode, cid) {
assert(cid <= 0xffff, 'Max size of CID is 65,535');
var glyphId = -1;
if (cidToGidMapLength === 0) {
if (isCidToGidMapEmpty) {
glyphId = charCode;
} else if (cidToGidMap[cid] !== undefined) {
glyphId = cidToGidMap[cid];
}
if (glyphId >= 0 && glyphId < numGlyphs) {

if (glyphId >= 0 && glyphId < numGlyphs &&
hasGlyph(glyphId, charCode)) {
charCodeToGlyphId[charCode] = glyphId;
}
});
Expand Down Expand Up @@ -4133,7 +4156,8 @@ var Font = (function FontClosure() {

var found = false;
for (i = 0; i < cmapMappingsLength; ++i) {
if (cmapMappings[i].charCode === unicodeOrCharCode) {
if (cmapMappings[i].charCode === unicodeOrCharCode &&
hasGlyph(cmapMappings[i].glyphId, unicodeOrCharCode)) {
charCodeToGlyphId[charCode] = cmapMappings[i].glyphId;
found = true;
break;
Expand All @@ -4143,7 +4167,7 @@ var Font = (function FontClosure() {
// Try to map using the post table. There are currently no known
// pdfs that this fixes.
var glyphId = properties.glyphNames.indexOf(glyphName);
if (glyphId > 0) {
if (glyphId > 0 && hasGlyph(glyphId, -1)) {
charCodeToGlyphId[charCode] = glyphId;
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
!sizes.pdf
!close-path-bug.pdf
!issue4630.pdf
!issue5202.pdf
!issue5280.pdf
!alphatrans.pdf
!devicen.pdf
Expand Down Expand Up @@ -58,10 +59,13 @@
!zerowidthline.pdf
!bug868745.pdf
!mmtype1.pdf
!issue5704.pdf
!bug893730.pdf
!bug864847.pdf
!issue1002.pdf
!issue925.pdf
!issue2840.pdf
!issue4061.pdf
!issue4668.pdf
!issue5039.pdf
!issue5070.pdf
Expand Down
Binary file added test/pdfs/bug893730.pdf
Binary file not shown.
Binary file added test/pdfs/issue4061.pdf
Binary file not shown.
Binary file added test/pdfs/issue5202.pdf
Binary file not shown.
Binary file added test/pdfs/issue5704.pdf
Binary file not shown.
28 changes: 28 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,20 @@
"lastPage": 1,
"type": "eq"
},
{ "id": "issue4061",
"file": "pdfs/issue4061.pdf",
"md5": "236aaa8840a47c3c061f8e3034549764",
"rounds": 1,
"link": false,
"type": "eq"
},
{ "id": "issue5202",
"file": "pdfs/issue5202.pdf",
"md5": "bb9cc69211112e66aab40828086a4e5a",
"rounds": 1,
"link": false,
"type": "eq"
},
{ "id": "issue5238",
"file": "pdfs/issue5238.pdf",
"md5": "6ddecda00893be1793de20a70c83a3c2",
Expand Down Expand Up @@ -1008,6 +1022,20 @@
"link": true,
"type": "eq"
},
{ "id": "issue5704",
"file": "pdfs/issue5704.pdf",
"md5": "6e0b62585feef24dff2d7e7687cd8128",
"rounds": 1,
"link": false,
"type": "eq"
},
{ "id": "bug893730",
"file": "pdfs/bug893730.pdf",
"md5": "2587379fb1b3bbff89c14f0863e78383",
"rounds": 1,
"link": false,
"type": "eq"
},
{ "id": "pdfkit_compressed",
"file": "pdfs/pdfkit_compressed.pdf",
"md5": "ffe9c571d0a1572e234253e6c7cdee6c",
Expand Down