From 709dc1a0c9f334d5079c03feb69323bec1c21372 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Thu, 24 Nov 2011 09:38:09 -0600 Subject: [PATCH] Initial ToUnicode modifications --- src/canvas.js | 8 +++----- src/evaluator.js | 16 +++++++++++++--- src/fonts.js | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 9b3ed0ba9..8d6fb046d 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -474,13 +474,11 @@ var CanvasGraphics = (function canvasGraphics() { continue; } - var unicode = glyph.unicode; - var char = (unicode >= 0x10000) ? - String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10), - 0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode); - + var char = glyph.fontChar; ctx.fillText(char, width, 0); width += glyph.width * fontSize * 0.001 + charSpacing; + + // TODO actual characters can be extracted from the glyph.unicode } current.x += width; diff --git a/src/evaluator.js b/src/evaluator.js index a863a531e..03fce2d9a 100644 --- a/src/evaluator.js +++ b/src/evaluator.js @@ -512,6 +512,7 @@ var PartialEvaluator = (function partialEvaluator() { error('Encoding is not a Name nor a Dict'); } } + properties.differences = differences; properties.baseEncoding = baseEncoding; properties.hasEncoding = hasEncoding; @@ -595,9 +596,18 @@ var PartialEvaluator = (function partialEvaluator() { } } else if (byte == 0x3E) { if (token.length) { - // parsing hex number - tokens.push(parseInt(token, 16)); - token = ''; + if (token.length <= 4) { + // parsing hex number + tokens.push(parseInt(token, 16)); + token = ''; + } else { + // parsing hex UTF-16BE numbers + var str = []; + for (var i = 0, ii = token.length; i < ii; i += 4) + str.push(parseInt(token.substr(i, 4), 16)); + tokens.push(String.fromCharCode.apply(String, str)); + token = ''; + } } } else { token += String.fromCharCode(byte); diff --git a/src/fonts.js b/src/fonts.js index 116bb4dfc..d028a9786 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -771,7 +771,6 @@ var Font = (function Font() { this.widths = properties.widths; this.defaultWidth = properties.defaultWidth; this.composite = properties.composite; - this.toUnicode = properties.toUnicode; this.hasEncoding = properties.hasEncoding; this.fontMatrix = properties.fontMatrix; @@ -781,6 +780,11 @@ var Font = (function Font() { // Trying to fix encoding using glyph CIDSystemInfo. this.loadCidToUnicode(properties); + if (properties.toUnicode) + this.toUnicode = properties.toUnicode; + else + this.rebuildToUnicode(properties); + if (!file) { // The file data is not specified. Trying to fix the font name // to be used with the canvas.font. @@ -1898,6 +1902,29 @@ var Font = (function Font() { return stringToArray(otf.file); }, + rebuildToUnicode: function font_rebuildToUnicode(properties) { + var map = []; + if (properties.composite) { + for (var i = properties.firstChar, ii = properties.lastChar; i <= ii; i++) { + // TODO missing map the character according font's CMap + var cid = i; + map[i] = this.cidToUnicode[cid]; + } + } else { + for (var i = properties.firstChar, ii = properties.lastChar; i <= ii; i++) { + var glyph = properties.differences[i]; + if (!glyph) + glyph = properties.baseEncoding[i]; + if (!!glyph && (glyph in GlyphsUnicode)) + map[i] = GlyphsUnicode[glyph] + } + } + this.toUnicode = map; + this.refreshToUnicode = function refreshToUnicode() { + this.font_rebuildToUnicode(properties); + }; + }, + loadCidToUnicode: function font_loadCidToUnicode(properties) { if (properties.cidToGidMap) { this.cidToUnicode = properties.cidToGidMap; @@ -2039,8 +2066,18 @@ var Font = (function Font() { warn('Unsupported font type: ' + this.type); break; } + + var unicodeChars = this.toUnicode ? this.toUnicode[charcode] : charcode; + if (typeof unicodeChars === 'number') { + unicodeChars = (unicodeChars >= 0x10000) ? + String.fromCharCode(0xD800 | ((unicodeChars - 0x10000) >> 10), + 0xDC00 | (unicodeChars & 0x3FF)) : String.fromCharCode(unicodeChars); + // TODO we probably don't need convert high/low surrogate... keeping for now + } + return { - unicode: unicode, + fontChar: String.fromCharCode(unicode), + unicode: unicodeChars, width: isNum(width) ? width : this.defaultWidth, codeIRQueue: codeIRQueue };