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

Remove glyph id's outside the range of valid glyphs. #6715

Merged
merged 1 commit into from
Dec 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2798,11 +2798,15 @@ var Font = (function FontClosure() {
};
}

function getRanges(glyphs) {
function getRanges(glyphs, numGlyphs) {
// Array.sort() sorts by characters, not numerically, so convert to an
// array of characters.
var codes = [];
for (var charCode in glyphs) {
// Remove an invalid glyph ID mappings to make OTS happy.
if (glyphs[charCode] >= numGlyphs) {
continue;
}
codes.push({ fontCharCode: charCode | 0, glyphId: glyphs[charCode] });
}
codes.sort(function fontGetRangesSort(a, b) {
Expand Down Expand Up @@ -2831,8 +2835,8 @@ var Font = (function FontClosure() {
return ranges;
}

function createCmapTable(glyphs) {
var ranges = getRanges(glyphs);
function createCmapTable(glyphs, numGlyphs) {
var ranges = getRanges(glyphs, numGlyphs);
var numTables = ranges[ranges.length - 1][1] > 0xFFFF ? 2 : 1;
var cmap = '\x00\x00' + // version
string16(numTables) + // numTables
Expand Down Expand Up @@ -4335,7 +4339,7 @@ var Font = (function FontClosure() {
this.toFontChar = newMapping.toFontChar;
tables.cmap = {
tag: 'cmap',
data: createCmapTable(newMapping.charCodeToGlyphId)
data: createCmapTable(newMapping.charCodeToGlyphId, numGlyphs)
};

if (!tables['OS/2'] || !validateOS2Table(tables['OS/2'])) {
Expand Down Expand Up @@ -4473,7 +4477,8 @@ var Font = (function FontClosure() {
builder.addTable('OS/2', createOS2Table(properties,
newMapping.charCodeToGlyphId));
// Character to glyphs mapping
builder.addTable('cmap', createCmapTable(newMapping.charCodeToGlyphId));
builder.addTable('cmap', createCmapTable(newMapping.charCodeToGlyphId,
numGlyphs));
// Font header
builder.addTable('head',
'\x00\x01\x00\x00' + // Version number
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
!bug1020858.pdf
!bug1050040.pdf
!bug1200096.pdf
!issue5564_reduced.pdf
!canvas.pdf
!complex_ttf_font.pdf
!extgstate.pdf
Expand Down
Binary file added test/pdfs/issue5564_reduced.pdf
Binary file not shown.
10 changes: 10 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"type": "load",
"about": "PDF with undefined stream length."
},
{ "id": "issue5564_reduced",
"file": "pdfs/issue5564_reduced.pdf",
"md5": "097853614b56fc10bfbf7e56daa0c66b",
"rounds": 1,
"link": false,
"firstPage": 1,
"lastPage": 1,
"type": "eq",
"about": "Causes cmap to be created with invalid glyph ids."
},
{ "id": "bug946506",
"file": "pdfs/bug946506.pdf",
"md5": "c28911b5c31bdc337c2ce404c5971cfc",
Expand Down