Refactoring charsToUnicode into charsToGlyphs

This commit is contained in:
notmasteryet 2011-09-10 20:21:20 -05:00
parent beea86db31
commit faf8b8ac0b
2 changed files with 23 additions and 30 deletions

View File

@ -1296,7 +1296,7 @@ var Font = (function Font() {
return rule; return rule;
}, },
charsToUnicode: function fonts_chars2Unicode(chars) { charsToGlyphs: function fonts_chars2Glyphs(chars) {
var charsCache = this.charsCache; var charsCache = this.charsCache;
var str; var str;
@ -1315,7 +1315,7 @@ var Font = (function Font() {
var encoding = this.encoding; var encoding = this.encoding;
if (!encoding) if (!encoding)
return chars; return chars;
str = ''; var glyphs = [];
if (this.composite) { if (this.composite) {
// composite fonts have multi-byte strings convert the string from // composite fonts have multi-byte strings convert the string from
@ -1329,11 +1329,9 @@ var Font = (function Font() {
var unicode = encoding[charcode]; var unicode = encoding[charcode];
if ('undefined' == typeof(unicode)) { if ('undefined' == typeof(unicode)) {
warn('Unencoded charcode ' + charcode); warn('Unencoded charcode ' + charcode);
unicode = charcode; unicode = { unicode: charcode };
} else {
unicode = unicode.unicode;
} }
str += String.fromCharCode(unicode); glyphs.push(unicode);
} }
} }
else { else {
@ -1342,22 +1340,14 @@ var Font = (function Font() {
var unicode = encoding[charcode]; var unicode = encoding[charcode];
if ('undefined' == typeof(unicode)) { if ('undefined' == typeof(unicode)) {
warn('Unencoded charcode ' + charcode); warn('Unencoded charcode ' + charcode);
unicode = charcode; unicode = { unicode: charcode };
} else {
unicode = unicode.unicode;
} }
glyphs.push(unicode);
// Handle surrogate pairs
if (unicode > 0xFFFF) {
str += String.fromCharCode(unicode & 0xFFFF);
unicode >>= 16;
}
str += String.fromCharCode(unicode);
} }
} }
// Enter the translated string into the cache // Enter the translated string into the cache
return charsCache[chars] = str; return charsCache[chars] = glyphs;
} }
}; };

29
pdf.js
View File

@ -4954,7 +4954,6 @@ var CanvasGraphics = (function() {
showText: function(text) { showText: function(text) {
var ctx = this.ctx; var ctx = this.ctx;
var current = this.current; var current = this.current;
var originalText = text;
ctx.save(); ctx.save();
ctx.transform.apply(ctx, current.textMatrix); ctx.transform.apply(ctx, current.textMatrix);
@ -4963,9 +4962,15 @@ var CanvasGraphics = (function() {
ctx.translate(current.x, -1 * current.y); ctx.translate(current.x, -1 * current.y);
var font = current.font; var font = current.font;
var glyphs = [];
if (font) { if (font) {
ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX); 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; var composite = font.composite;
@ -4977,21 +4982,19 @@ var CanvasGraphics = (function() {
ctx.scale(1 / textHScale, 1); ctx.scale(1 / textHScale, 1);
var width = 0; var width = 0;
for (var i = 0; i < text.length; i++) { for (var i = 0; i < glyphs.length; i++) {
if (composite) { var glyph = glyphs[i];
var position = i * 2 + 1; var unicode = glyph.unicode;
var charcode = (originalText.charCodeAt(position - 1) << 8) + var char = unicode >= 0x10000 ?
originalText.charCodeAt(position); String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
} else { 0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode);
var charcode = originalText.charCodeAt(i);
}
var charWidth = font.encoding[charcode].width * fontSize * 0.001; var charWidth = glyph.width * fontSize * 0.001;
charWidth += charSpacing; charWidth += charSpacing;
if (charcode == 32) if (unicode == 32)
charWidth += wordSpacing; charWidth += wordSpacing;
ctx.fillText(text.charAt(i), 0, 0); ctx.fillText(char, 0, 0);
ctx.translate(charWidth, 0); ctx.translate(charWidth, 0);
width += charWidth; width += charWidth;
} }