Merge pull request #223 from andreasgal/staging

move unicode translation cache into Font
This commit is contained in:
sbarman 2011-07-08 16:58:11 -07:00
commit 7e42cf781b
2 changed files with 44 additions and 42 deletions

View File

@ -38,12 +38,10 @@ var Fonts = (function Fonts() {
this.properties = properties; this.properties = properties;
this.id = fontCount++; this.id = fontCount++;
this.loading = true; this.loading = true;
this.charsCache = Object.create(null);
this.sizes = []; this.sizes = [];
} }
var current; var current;
var charsCache;
var measureCache; var measureCache;
return { return {
@ -64,51 +62,15 @@ var Fonts = (function Fonts() {
// |current| can be null is fontName is a built-in font // |current| can be null is fontName is a built-in font
// (e.g. "sans-serif") // (e.g. "sans-serif")
if (fontObj && (current = fonts[fontObj.id])) { if (fontObj && (current = fonts[fontObj.id])) {
charsCache = current.charsCache;
var sizes = current.sizes; var sizes = current.sizes;
if (!(measureCache = sizes[size])) if (!(measureCache = sizes[size]))
measureCache = sizes[size] = Object.create(null); measureCache = sizes[size] = Object.create(null);
} else { } else {
charsCache = null;
measureCache = null measureCache = null
} }
ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"'; ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"';
}, },
charsToUnicode: function fonts_chars2Unicode(chars) {
if (!charsCache)
return chars;
// if we translated this string before, just grab it from the cache
var str = charsCache[chars];
if (str)
return str;
// translate the string using the font's encoding
var encoding = current ? current.properties.encoding : null;
if (!encoding)
return chars;
str = '';
for (var i = 0; i < chars.length; ++i) {
var charcode = chars.charCodeAt(i);
var unicode = encoding[charcode];
// Check if the glyph has already been converted
if (!IsNum(unicode))
unicode = encoding[unicode] = GlyphsUnicode[unicode.name];
// Handle surrogate pairs
if (unicode > 0xFFFF) {
str += String.fromCharCode(unicode & 0xFFFF);
unicode >>= 16;
}
str += String.fromCharCode(unicode);
}
// Enter the translated string into the cache
return charsCache[chars] = str;
},
measureText: function fonts_measureText(text) { measureText: function fonts_measureText(text) {
var width; var width;
if (measureCache && (width = measureCache[text])) if (measureCache && (width = measureCache[text]))
@ -1146,6 +1108,46 @@ var Font = (function() {
styleSheet.insertRule(rule, styleSheet.cssRules.length); styleSheet.insertRule(rule, styleSheet.cssRules.length);
return rule; return rule;
},
charsToUnicode: function fonts_chars2Unicode(chars) {
var charsCache = this.charsCache;
// if we translated this string before, just grab it from the cache
if (charsCache) {
var str = charsCache[chars];
if (str)
return str;
}
// translate the string using the font's encoding
var encoding = this.encoding;
if (!encoding)
return chars;
// lazily create the translation cache
if (!charsCache)
charsCache = this.charsCache = Object.create(null);
str = '';
for (var i = 0; i < chars.length; ++i) {
var charcode = chars.charCodeAt(i);
var unicode = encoding[charcode];
// Check if the glyph has already been converted
if (!IsNum(unicode))
unicode = encoding[unicode] = GlyphsUnicode[unicode.name];
// Handle surrogate pairs
if (unicode > 0xFFFF) {
str += String.fromCharCode(unicode & 0xFFFF);
unicode >>= 16;
}
str += String.fromCharCode(unicode);
}
// Enter the translated string into the cache
return charsCache[chars] = str;
} }
}; };

8
pdf.js
View File

@ -4015,14 +4015,14 @@ var CanvasGraphics = (function() {
ctx.scale(1, -1); ctx.scale(1, -1);
if (this.ctx.$showText) { if (this.ctx.$showText) {
ctx.$showText(current.y, Fonts.charsToUnicode(text)); ctx.$showText(current.y, text);
} else { } else {
text = Fonts.charsToUnicode(text);
ctx.translate(current.x, -1 * current.y); ctx.translate(current.x, -1 * current.y);
var font = this.current.font; var font = this.current.font;
if (font) if (font) {
ctx.transform.apply(ctx, font.textMatrix); ctx.transform.apply(ctx, font.textMatrix);
text = font.charsToUnicode(text);
}
ctx.fillText(text, 0, 0); ctx.fillText(text, 0, 0);
current.x += Fonts.measureText(text); current.x += Fonts.measureText(text);
} }