From 0c8f0cd6c2563b91d060d9312f2d1496c02a1b4c Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 7 Oct 2011 22:24:26 -0500 Subject: [PATCH] Fixing PDF bad structure and 'fi' ligature --- fonts.js | 5 +++-- pdf.js | 11 +++++++---- test/pdfs/usmanm-bad.pdf.link | 1 + test/test_manifest.json | 6 ++++++ 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 test/pdfs/usmanm-bad.pdf.link diff --git a/fonts.js b/fonts.js index 7d9b48465..2ab3a90b7 100644 --- a/fonts.js +++ b/fonts.js @@ -2550,7 +2550,7 @@ var Type2CFF = (function type2CFF() { var defaultWidth = privateDict['defaultWidthX']; var charstrings = []; var differences = properties.differences; - var index = 0; + var index = properties.firstChar || 0; for (var i = 1; i < charsets.length; i++) { var code = -1; var glyph = charsets[i]; @@ -2562,7 +2562,8 @@ var Type2CFF = (function type2CFF() { } } - var mapping = properties.glyphs[glyph] || {}; + var mapping = + properties.glyphs[glyph] || properties.glyphs[index] || {}; if (code == -1) index = code = mapping.unicode || index; diff --git a/pdf.js b/pdf.js index 943986e0d..e804126fe 100644 --- a/pdf.js +++ b/pdf.js @@ -3260,7 +3260,7 @@ var XRef = (function xRefXRef() { var stream = this.stream; stream.pos = 0; var buffer = stream.getBytes(); - var position = 0, length = buffer.length; + var position = stream.start, length = buffer.length; var trailers = [], xrefStms = []; var state = 0; var currentToken; @@ -3311,6 +3311,7 @@ var XRef = (function xRefXRef() { this.readXRef(xrefStms[i]); } // finding main trailer + var dict; for (var i = 0; i < trailers.length; ++i) { stream.pos = trailers[i]; var parser = new Parser(new Lexer(stream), true); @@ -3318,13 +3319,15 @@ var XRef = (function xRefXRef() { if (!isCmd(obj, 'trailer')) continue; // read the trailer dictionary - var dict; if (!isDict(dict = parser.getObj())) continue; // taking the first one with 'ID' if (dict.has('ID')) return dict; } + // no tailer with 'ID', taking last one (if exists) + if (dict) + return dict; // nothing helps error('Invalid PDF structure'); return null; @@ -4595,7 +4598,7 @@ var PartialEvaluator = (function partialEvaluator() { var glyph = differences[i]; var replaceGlyph = true; if (!glyph) { - glyph = baseEncoding[i]; + glyph = baseEncoding[i] || i; replaceGlyph = false; } var index = GlyphsUnicode[glyph] || i; @@ -4605,7 +4608,7 @@ var PartialEvaluator = (function partialEvaluator() { width: isNum(width) ? width : properties.defaultWidth }; - if (glyph && (replaceGlyph || !glyphs[glyph])) + if (replaceGlyph || !glyphs[glyph]) glyphs[glyph] = map[i]; // If there is no file, the character mapping can't be modified diff --git a/test/pdfs/usmanm-bad.pdf.link b/test/pdfs/usmanm-bad.pdf.link new file mode 100644 index 000000000..f7ebc3610 --- /dev/null +++ b/test/pdfs/usmanm-bad.pdf.link @@ -0,0 +1 @@ +http://www.mit.edu/~6.033/writing-samples/usmanm_dp1.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index f074c06c3..15771438c 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -169,6 +169,12 @@ "rounds": 1, "type": "load" }, + { "id": "usmanm-bad", + "file": "pdfs/usmanm-bad.pdf", + "link": true, + "rounds": 1, + "type": "eq" + }, { "id": "vesta-bad", "file": "pdfs/vesta.pdf", "link": true,