From 00df9b82eed490d5807fbd57cf0f36004f5d6da1 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Wed, 22 Jun 2011 07:46:41 +0200 Subject: [PATCH] Start of a TTF Format6 to Format4 converter (sigh) --- fonts.js | 30 ++++++++++++++++++++---------- pdf.js | 7 +++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/fonts.js b/fonts.js index f5a531785..c7230a55a 100644 --- a/fonts.js +++ b/fonts.js @@ -284,8 +284,8 @@ var Font = (function () { idDeltas += string16(delta); idRangeOffsets += string16(0); - for (var j = start; j <= end; j++) - glyphsIds += String.fromCharCode(j); + for (var j = 0; j < range.length; j++) + glyphsIds += String.fromCharCode(range[j]); } startCount += "\xFF\xFF"; @@ -393,11 +393,10 @@ var Font = (function () { } }; - function replaceCMapTable(font, properties) { + function replaceCMapTable(cmap, font, properties) { var version = FontsUtils.bytesToInteger(font.getBytes(2)); var numTables = FontsUtils.bytesToInteger(font.getBytes(2)); - var tables = []; for (var i = 0; i < numTables; i++) { var platformID = FontsUtils.bytesToInteger(font.getBytes(2)); var encodingID = FontsUtils.bytesToInteger(font.getBytes(2)); @@ -406,14 +405,15 @@ var Font = (function () { var length = FontsUtils.bytesToInteger(font.getBytes(2)); var language = FontsUtils.bytesToInteger(font.getBytes(2)); - if (format == 0 && numTables == 1) { + if ((format == 0 && numTables == 1) || + (format == 6 && numTables == 1 && !properties.encoding.empty)) { // Format 0 alone is not allowed by the sanitizer so let's rewrite // that to a 3-1-4 Unicode BMP table var charset = properties.charset; var glyphs = []; - for (var i = 0; i < charset.length; i++) { + for (var j = 0; j < charset.length; j++) { glyphs.push({ - unicode: GlyphsUnicode[charset[i]] || 0 + unicode: GlyphsUnicode[charset[j]] || 0 }); } @@ -421,15 +421,25 @@ var Font = (function () { } else if (format == 6 && numTables == 1) { // Format 6 is a 2-bytes dense mapping, which means the font data // lives glue together even if they are pretty far in the unicode - // table. (This looks weird, so I can have missed something) + // table. (This looks weird, so I can have missed something), this + // works on Linux but seems to fails on Mac so let's rewrite the + // cmap table to a 3-1-4 style var firstCode = FontsUtils.bytesToInteger(font.getBytes(2)); var entryCount = FontsUtils.bytesToInteger(font.getBytes(2)); var encoding = properties.encoding; + var glyphs = []; for (var j = 0; j < entryCount; j++) { var charcode = FontsUtils.bytesToInteger(font.getBytes(2)); - encoding[charcode + firstCode] = charcode + firstCode; + glyphs.push({unicode: charcode + firstCode }); } + + var ranges = getRanges(glyphs); + var denseRange = ranges[0]; + var pos = 0; + for (var j = denseRange[0]; j <= denseRange[1]; j++) + encoding[j - 1] = glyphs[pos++].unicode; + cmap.data = createCMapTable(glyphs); } } }; @@ -490,7 +500,7 @@ var Font = (function () { }); // Replace the old CMAP table with a shiny new one - replaceCMapTable(font, properties); + replaceCMapTable(cmap, font, properties); // Rewrite the 'post' table if needed if (!post) { diff --git a/pdf.js b/pdf.js index 72a7b7970..9d06241f7 100644 --- a/pdf.js +++ b/pdf.js @@ -2143,7 +2143,7 @@ var CanvasGraphics = (function() { // Fonts with an embedded cmap but without any assignment in // it are not yet supported, so ask the fonts loader to ignore // them to not pay a stupid one sec latence. - var ignoreFont = true; + var ignoreFont = false; var encodingMap = {}; var charset = []; @@ -2187,6 +2187,7 @@ var CanvasGraphics = (function() { } } } else if (fontDict.has("ToUnicode")) { + encodingMap = {empty: true}; var cmapObj = xref.fetchIfRef(fontDict.get("ToUnicode")); if (IsName(cmapObj)) { error("ToUnicode file cmap translation not implemented"); @@ -2230,7 +2231,9 @@ var CanvasGraphics = (function() { var code = parseInt("0x" + tokens[j+2]); for (var k = startRange; k <= endRange; k++) { - encodingMap[k] = GlyphsUnicode[encoding[code]]; + // The encoding mapping table will be filled + // later during the building phase + //encodingMap[k] = GlyphsUnicode[encoding[code]]; charset.push(encoding[code++]); } }