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;
},
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;
}
};

29
pdf.js
View File

@ -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;
}