From 1f047495b1650f1317c54d54e05ae88a0a13630d Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Sat, 17 Sep 2011 17:13:22 -0500 Subject: [PATCH] Fixing large cmap-s; reduce changes in the indent encoding --- fonts.js | 77 +++++++++++++++++++++++----------- test/pdfs/wnv_chinese.pdf.link | 1 + test/test_manifest.json | 6 +++ 3 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 test/pdfs/wnv_chinese.pdf.link diff --git a/fonts.js b/fonts.js index 4f95d16f0..f9462ccbb 100644 --- a/fonts.js +++ b/fonts.js @@ -594,19 +594,24 @@ var Font = (function Font() { var codes = []; var length = glyphs.length; for (var n = 0; n < length; ++n) - codes.push(String.fromCharCode(glyphs[n].unicode)); - codes.sort(); + codes.push({ unicode: glyphs[n].unicode, code: n }); + codes.sort(function(a, b) { + return a.unicode - b.unicode; + }); // Split the sorted codes into ranges. var ranges = []; for (var n = 0; n < length; ) { - var start = codes[n++].charCodeAt(0); + var start = codes[n].unicode; + var startCode = codes[n].code; + ++n; var end = start; - while (n < length && end + 1 == codes[n].charCodeAt(0)) { + while (n < length && end + 1 == codes[n].unicode) { ++end; ++n; } - ranges.push([start, end]); + var endCode = codes[n - 1].code; + ranges.push([start, end, startCode, endCode]); } return ranges; @@ -635,22 +640,39 @@ var Font = (function Font() { var idRangeOffsets = ''; var glyphsIds = ''; var bias = 0; - for (var i = 0; i < segCount - 1; i++) { - var range = ranges[i]; - var start = range[0]; - var end = range[1]; - var offset = (segCount - i) * 2 + bias * 2; - bias += (end - start + 1); - startCount += string16(start); - endCount += string16(end); - idDeltas += string16(0); - idRangeOffsets += string16(offset); + if (deltas) { + for (var i = 0; i < segCount - 1; i++) { + var range = ranges[i]; + var start = range[0]; + var end = range[1]; + var offset = (segCount - i) * 2 + bias * 2; + bias += (end - start + 1); + + startCount += string16(start); + endCount += string16(end); + idDeltas += string16(0); + idRangeOffsets += string16(offset); + + var startCode = range[2]; + var endCode = range[3]; + for (var j = startCode; j <= endCode; ++j) + glyphsIds += string16(deltas[j]); + } + } else { + for (var i = 0; i < segCount - 1; i++) { + var range = ranges[i]; + var start = range[0]; + var end = range[1]; + var startCode = range[2]; + + startCount += string16(start); + endCount += string16(end); + idDeltas += string16((startCode - start + 1) & 0xFFFF); + idRangeOffsets += string16(0); + } } - for (var i = 0; i < glyphs.length; i++) - glyphsIds += string16(deltas ? deltas[i] : i + 1); - endCount += '\xFF\xFF'; startCount += '\xFF\xFF'; idDeltas += '\x00\x01'; @@ -1124,7 +1146,7 @@ var Font = (function Font() { tables.push(cmap); } - var encoding = properties.encoding; + var encoding = properties.encoding, i; if (!encoding[0]) { // the font is directly characters to glyphs with no encoding // so create an identity encoding @@ -1132,18 +1154,25 @@ var Font = (function Font() { for (i = 0; i < numGlyphs; i++) { var width = widths[i]; encoding[i] = { - unicode: i + kCmapGlyphOffset, + unicode: i <= 0x1f || (i >= 127 && i <= 255) ? + i + kCmapGlyphOffset : i, width: IsNum(width) ? width : properties.defaultWidth }; } } else { - for (var code in encoding) - encoding[code].unicode += kCmapGlyphOffset; + for (i = 0; i <= 0x1f; i++) + encoding[i].unicode += kCmapGlyphOffset; + for (i = 127; i <= 255; i++) + encoding[i].unicode += kCmapGlyphOffset; } var glyphs = []; - for (var i = 1; i < numGlyphs; i++) - glyphs.push({ unicode: i + kCmapGlyphOffset }); + for (i = 1; i < numGlyphs; i++) { + glyphs.push({ + unicode: i <= 0x1f || (i >= 127 && i <= 255) ? + i + kCmapGlyphOffset : i + }); + } cmap.data = createCMapTable(glyphs); } else { replaceCMapTable(cmap, font, properties); diff --git a/test/pdfs/wnv_chinese.pdf.link b/test/pdfs/wnv_chinese.pdf.link new file mode 100644 index 000000000..fbbc81760 --- /dev/null +++ b/test/pdfs/wnv_chinese.pdf.link @@ -0,0 +1 @@ +http://www.cdc.gov/ncidod/dvbid/westnile/languages/chinese.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index 926e37c18..66d26c4e9 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -69,6 +69,12 @@ "rounds": 1, "type": "load" }, + { "id": "wnv_chinese-pdf", + "file": "pdfs/wnv_chinese.pdf", + "link": true, + "rounds": 1, + "type": "eq" + }, { "id": "i9-pdf", "file": "pdfs/i9.pdf", "link": true,