From ab391c318d4481abb6c091d9248fb25515bbf7d7 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Wed, 28 Sep 2011 21:19:49 -0500 Subject: [PATCH] Moving CID-0 encoding hack to fonts.js --- fonts.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pdf.js | 59 +++++++++++-------------------------------------------- 2 files changed, 71 insertions(+), 48 deletions(-) diff --git a/fonts.js b/fonts.js index b565c67e7..22b6a6c99 100644 --- a/fonts.js +++ b/fonts.js @@ -429,6 +429,9 @@ var Font = (function Font() { return; } + // Trying to fix encoding using glyph widths and CIDSystemInfo + this.fixWidths(properties); + if (!file) { // The file data is not specified. Trying to fix the font name // to be used with the canvas.font. @@ -1403,6 +1406,63 @@ var Font = (function Font() { return stringToArray(otf.file); }, + fixWidths: function font_fixWidths(properties) { + var encoding = properties.encoding; + if (encoding[0]) + return; + var glyphsWidths = properties.widths; + if (!glyphsWidths) + return; + + var cidSystemInfo = properties.cidSystemInfo; + var cidToUnicode; + if (cidSystemInfo) { + cidToUnicode = CIDToUnicodeMaps[cidSystemInfo.registry + + '-' + cidSystemInfo.ordering]; + } + if (!cidToUnicode) + return; + + encoding[0] = { unicode: 0, width: 0 }; + var glyph = 1, i, j; + for (i = 0; i < cidToUnicode.length; ++i) { + var unicode = cidToUnicode[i]; + if (isArray(unicode)) { + var length = unicode.length; + if (glyph in glyphsWidths) { + for (j = 0; j < length; j++) { + encoding[unicode[j]] = { + unicode: unicode[j], + width: glyphsWidths[glyph] + }; + } + } + glyph++; + } else if (typeof unicode === 'object') { + var fillLength = unicode.f; + if (fillLength) { + unicode = unicode.c; + for (j = 0; j < fillLength; ++j) { + if (!(glyph in glyphsWidths)) + continue; + encoding[unicode] = { + unicode: unicode, + width: glyphsWidths[glyph] + }; + unicode++; + glyph++; + } + } else + glyph += unicode.s; + } else if (unicode) { + encoding[unicode] = { + unicode: unicode, + width: glyphsWidths[glyph++] + }; + } + } + }, + bindWorker: function font_bindWorker(data) { postMessage({ action: 'font', diff --git a/pdf.js b/pdf.js index 66e575abf..08c962406 100644 --- a/pdf.js +++ b/pdf.js @@ -4472,56 +4472,19 @@ var PartialEvaluator = (function partialEvaluator() { // Glyph ids are big-endian 2-byte values encoding = properties.encoding; + // CIDSystemInfo might help to match width and glyphs + var cidSystemInfo = dict.get('CIDSystemInfo'); + if (isDict(cidSystemInfo)) { + properties.cidSystemInfo = { + registry: cidSystemInfo.get('Registry'), + ordering: cidSystemInfo.get('Ordering'), + supplement: cidSystemInfo.get('Supplement') + }; + } + var cidToGidMap = dict.get('CIDToGIDMap'); if (!cidToGidMap || !isRef(cidToGidMap)) { - // trying to guess encoding from CIDSystemInfo - var cidSystemInfo = dict.get('CIDSystemInfo'); - var cidToUnicode; - if (isDict(cidSystemInfo)) { - cidToUnicode = CIDToUnicodeMaps[ - cidSystemInfo.get('Registry') + '-' + - cidSystemInfo.get('Ordering')]; - } - if (cidToUnicode) { - encoding[0] = { unicode: 0, width: 0 }; - var glyph = 1, i, j; - for (i = 0; i < cidToUnicode.length; ++i) { - var unicode = cidToUnicode[i]; - if (isArray(unicode)) { - var length = unicode.length; - if (glyph in glyphsWidths) { - for (j = 0; j < length; j++) { - encoding[unicode[j]] = { - unicode: unicode[j], - width: glyphsWidths[glyph] - }; - } - } - glyph++; - } else if (typeof unicode === 'object') { - var fillLength = unicode.f; - if (fillLength) { - unicode = unicode.c; - for (j = 0; j < fillLength; ++j) { - if (!(glyph in glyphsWidths)) - continue; - encoding[unicode] = { - unicode: unicode, - width: glyphsWidths[glyph] - }; - unicode++; - glyph++; - } - } else - glyph += unicode.s; - } else if (unicode) { - encoding[unicode] = { - unicode: unicode, - width: glyphsWidths[glyph++] - }; - } - } - } + return Object.create(GlyphsUnicode); }