Initial ToUnicode modifications

This commit is contained in:
notmasteryet 2011-11-24 09:38:09 -06:00
parent 59d9dfc014
commit 709dc1a0c9
3 changed files with 55 additions and 10 deletions

View File

@ -474,13 +474,11 @@ var CanvasGraphics = (function canvasGraphics() {
continue; continue;
} }
var unicode = glyph.unicode; var char = glyph.fontChar;
var char = (unicode >= 0x10000) ?
String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode);
ctx.fillText(char, width, 0); ctx.fillText(char, width, 0);
width += glyph.width * fontSize * 0.001 + charSpacing; width += glyph.width * fontSize * 0.001 + charSpacing;
// TODO actual characters can be extracted from the glyph.unicode
} }
current.x += width; current.x += width;

View File

@ -512,6 +512,7 @@ var PartialEvaluator = (function partialEvaluator() {
error('Encoding is not a Name nor a Dict'); error('Encoding is not a Name nor a Dict');
} }
} }
properties.differences = differences; properties.differences = differences;
properties.baseEncoding = baseEncoding; properties.baseEncoding = baseEncoding;
properties.hasEncoding = hasEncoding; properties.hasEncoding = hasEncoding;
@ -595,9 +596,18 @@ var PartialEvaluator = (function partialEvaluator() {
} }
} else if (byte == 0x3E) { } else if (byte == 0x3E) {
if (token.length) { if (token.length) {
if (token.length <= 4) {
// parsing hex number // parsing hex number
tokens.push(parseInt(token, 16)); tokens.push(parseInt(token, 16));
token = ''; 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 { } else {
token += String.fromCharCode(byte); token += String.fromCharCode(byte);

View File

@ -771,7 +771,6 @@ var Font = (function Font() {
this.widths = properties.widths; this.widths = properties.widths;
this.defaultWidth = properties.defaultWidth; this.defaultWidth = properties.defaultWidth;
this.composite = properties.composite; this.composite = properties.composite;
this.toUnicode = properties.toUnicode;
this.hasEncoding = properties.hasEncoding; this.hasEncoding = properties.hasEncoding;
this.fontMatrix = properties.fontMatrix; this.fontMatrix = properties.fontMatrix;
@ -781,6 +780,11 @@ var Font = (function Font() {
// Trying to fix encoding using glyph CIDSystemInfo. // Trying to fix encoding using glyph CIDSystemInfo.
this.loadCidToUnicode(properties); this.loadCidToUnicode(properties);
if (properties.toUnicode)
this.toUnicode = properties.toUnicode;
else
this.rebuildToUnicode(properties);
if (!file) { if (!file) {
// The file data is not specified. Trying to fix the font name // The file data is not specified. Trying to fix the font name
// to be used with the canvas.font. // to be used with the canvas.font.
@ -1898,6 +1902,29 @@ var Font = (function Font() {
return stringToArray(otf.file); 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) { loadCidToUnicode: function font_loadCidToUnicode(properties) {
if (properties.cidToGidMap) { if (properties.cidToGidMap) {
this.cidToUnicode = properties.cidToGidMap; this.cidToUnicode = properties.cidToGidMap;
@ -2039,8 +2066,18 @@ var Font = (function Font() {
warn('Unsupported font type: ' + this.type); warn('Unsupported font type: ' + this.type);
break; 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 { return {
unicode: unicode, fontChar: String.fromCharCode(unicode),
unicode: unicodeChars,
width: isNum(width) ? width : this.defaultWidth, width: isNum(width) ? width : this.defaultWidth,
codeIRQueue: codeIRQueue codeIRQueue: codeIRQueue
}; };