From 118668cb534214fd3897e350369aa19e48255a8d Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Mon, 3 Oct 2011 10:16:18 +0100 Subject: [PATCH] Fix regression caused by pull request #491. Glyphs in positions that match complex scripts or special characters can be messed up by the browser when drawn to the canvas. Limit to fonts with a small to medium sized glyph set. --- fonts.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/fonts.js b/fonts.js index 50dfa2337..a4af5fb48 100644 --- a/fonts.js +++ b/fonts.js @@ -11,6 +11,10 @@ var kMaxWaitForFontFace = 1000; // Unicode Private Use Area var kCmapGlyphOffset = 0xE000; +var kSizeOfGlyphArea = 0x1900; + +//start of CJK block +var kUnicodeCJKStart = 0x2E80; // PDF Glyph Space Units are one Thousandth of a TextSpace Unit // except for Type 3 fonts @@ -1212,19 +1216,25 @@ var Font = (function Font() { } var encoding = properties.encoding, i; + + // offsetting glyphs to avoid problematic unicode ranges should only be + // done for fonts with a medium-sized glyph count otherwise we could + // overflow the glyph range or overwrite existing glyph positions + var cmapGlyphOffset = numGlyphs < kSizeOfGlyphArea ? kCmapGlyphOffset : 0; + for (i in encoding) { if (encoding.hasOwnProperty(i)) { var unicode = encoding[i].unicode; - if (unicode <= 0x1f || (unicode >= 127 && unicode <= 255)) - encoding[i].unicode = unicode += kCmapGlyphOffset; + if (unicode <= 0x1f || (unicode >= 127 && unicode <= kUnicodeCJKStart)) + encoding[i].unicode = unicode += cmapGlyphOffset; } } var glyphs = []; for (i = 1; i < numGlyphs; i++) { glyphs.push({ - unicode: i <= 0x1f || (i >= 127 && i <= 255) ? - i + kCmapGlyphOffset : i + unicode: i <= 0x1f || (i >= 127 && i < kUnicodeCJKStart) ? + i + cmapGlyphOffset : i }); } cmap.data = createCMapTable(glyphs);