diff --git a/fonts.js b/fonts.js index cc353c03b..ac070ed18 100755 --- a/fonts.js +++ b/fonts.js @@ -1296,7 +1296,7 @@ var Font = (function Font() { return rule; }, - charsToUnicode: function fonts_chars2Unicode(chars) { + charsToGlyphs: function fonts_chars2Glyphs(chars) { var charsCache = this.charsCache; var str; @@ -1315,7 +1315,7 @@ var Font = (function Font() { var encoding = this.encoding; if (!encoding) return chars; - str = ''; + var glyphs = []; if (this.composite) { // composite fonts have multi-byte strings convert the string from @@ -1329,11 +1329,9 @@ var Font = (function Font() { var unicode = encoding[charcode]; if ('undefined' == typeof(unicode)) { warn('Unencoded charcode ' + charcode); - unicode = charcode; - } else { - unicode = unicode.unicode; + unicode = { unicode: charcode }; } - str += String.fromCharCode(unicode); + glyphs.push(unicode); } } else { @@ -1342,22 +1340,14 @@ var Font = (function Font() { var unicode = encoding[charcode]; if ('undefined' == typeof(unicode)) { warn('Unencoded charcode ' + charcode); - unicode = charcode; - } else { - unicode = unicode.unicode; + unicode = { unicode: charcode }; } - - // Handle surrogate pairs - if (unicode > 0xFFFF) { - str += String.fromCharCode(unicode & 0xFFFF); - unicode >>= 16; - } - str += String.fromCharCode(unicode); + glyphs.push(unicode); } } // Enter the translated string into the cache - return charsCache[chars] = str; + return charsCache[chars] = glyphs; } }; diff --git a/pdf.js b/pdf.js index 3e8249524..17ef1f3ca 100644 --- a/pdf.js +++ b/pdf.js @@ -4954,7 +4954,6 @@ var CanvasGraphics = (function() { showText: function(text) { var ctx = this.ctx; var current = this.current; - var originalText = text; ctx.save(); ctx.transform.apply(ctx, current.textMatrix); @@ -4963,9 +4962,15 @@ var CanvasGraphics = (function() { ctx.translate(current.x, -1 * current.y); var font = current.font; + var glyphs = []; if (font) { ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX); - text = font.charsToUnicode(text); + glyphs = font.charsToGlyphs(text); + } else { + // fallback to simple glyphs + glyphs = []; + for (var i = 0; i < text.length; ++i) + glyphs.push({unicode: text.charCodeAt(i)}); } var composite = font.composite; @@ -4977,21 +4982,19 @@ var CanvasGraphics = (function() { ctx.scale(1 / textHScale, 1); var width = 0; - for (var i = 0; i < text.length; i++) { - if (composite) { - var position = i * 2 + 1; - var charcode = (originalText.charCodeAt(position - 1) << 8) + - originalText.charCodeAt(position); - } else { - var charcode = originalText.charCodeAt(i); - } + for (var i = 0; i < glyphs.length; i++) { + var glyph = glyphs[i]; + var unicode = glyph.unicode; + var char = unicode >= 0x10000 ? + String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10), + 0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode); - var charWidth = font.encoding[charcode].width * fontSize * 0.001; + var charWidth = glyph.width * fontSize * 0.001; charWidth += charSpacing; - if (charcode == 32) + if (unicode == 32) charWidth += wordSpacing; - ctx.fillText(text.charAt(i), 0, 0); + ctx.fillText(char, 0, 0); ctx.translate(charWidth, 0); width += charWidth; }