speedup unicode/glyph conversion and cache translated strings in the font
This commit is contained in:
parent
dea351911a
commit
d2e18d35b5
37
fonts.js
37
fonts.js
@ -30,6 +30,7 @@ var fontCount = 0;
|
|||||||
*/
|
*/
|
||||||
var Fonts = {
|
var Fonts = {
|
||||||
_active: null,
|
_active: null,
|
||||||
|
|
||||||
get active() {
|
get active() {
|
||||||
return this._active;
|
return this._active;
|
||||||
},
|
},
|
||||||
@ -38,12 +39,34 @@ var Fonts = {
|
|||||||
this._active = this[aName];
|
this._active = this[aName];
|
||||||
},
|
},
|
||||||
|
|
||||||
unicodeFromCode: function fonts_unicodeFromCode(aCode) {
|
chars2Unicode: function(chars) {
|
||||||
var active = this._active;
|
var active = this._active;
|
||||||
if (!active || !active.properties.encoding)
|
if (!active)
|
||||||
return aCode;
|
return chars;
|
||||||
|
|
||||||
return GlyphsUnicode[active.properties.encoding[aCode]];
|
// if we translated this string before, just grab it from the cache
|
||||||
|
var ret = active.cache[chars];
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
// translate the string using the font's encoding
|
||||||
|
var encoding = active.properties.encoding;
|
||||||
|
if (!encoding)
|
||||||
|
return chars;
|
||||||
|
|
||||||
|
var ret = "";
|
||||||
|
for (var i = 0; i < chars.length; ++i) {
|
||||||
|
var ch = chars.charCodeAt(i);
|
||||||
|
var uc = encoding[ch];
|
||||||
|
if (typeof uc != "number") // we didn't convert the glyph yet
|
||||||
|
uc = encoding[ch] = GlyphsUnicode[uc];
|
||||||
|
ret += String.fromCharCode(uc);
|
||||||
|
}
|
||||||
|
|
||||||
|
// enter the translated string into the cache
|
||||||
|
active.cache[chars] = ret;
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -83,7 +106,8 @@ var Font = function(aName, aFile, aProperties) {
|
|||||||
encoding: {},
|
encoding: {},
|
||||||
charset: null
|
charset: null
|
||||||
},
|
},
|
||||||
loading: false
|
loading: false,
|
||||||
|
cache: Object.create(null)
|
||||||
};
|
};
|
||||||
|
|
||||||
this.mimetype = "font/ttf";
|
this.mimetype = "font/ttf";
|
||||||
@ -99,7 +123,8 @@ var Font = function(aName, aFile, aProperties) {
|
|||||||
Fonts[aName] = {
|
Fonts[aName] = {
|
||||||
data: this.font,
|
data: this.font,
|
||||||
properties: aProperties,
|
properties: aProperties,
|
||||||
loading: true
|
loading: true,
|
||||||
|
cache: Object.create(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach the font to the document
|
// Attach the font to the document
|
||||||
|
8
pdf.js
8
pdf.js
@ -2245,13 +2245,7 @@ var CanvasGraphics = (function() {
|
|||||||
this.ctx.translate(0, 2 * this.current.y);
|
this.ctx.translate(0, 2 * this.current.y);
|
||||||
this.ctx.scale(1, -1);
|
this.ctx.scale(1, -1);
|
||||||
this.ctx.transform.apply(this.ctx, this.current.textMatrix);
|
this.ctx.transform.apply(this.ctx, this.current.textMatrix);
|
||||||
|
this.ctx.fillText(Fonts.chars2Unicode(text), this.current.x, this.current.y);
|
||||||
// Replace characters code by glyphs code
|
|
||||||
var glyphs = [];
|
|
||||||
for (var i = 0; i < text.length; i++)
|
|
||||||
glyphs[i] = String.fromCharCode(Fonts.unicodeFromCode(text[i].charCodeAt(0)));
|
|
||||||
|
|
||||||
this.ctx.fillText(glyphs.join(""), this.current.x, this.current.y);
|
|
||||||
this.current.x += this.ctx.measureText(text).width;
|
this.current.x += this.ctx.measureText(text).width;
|
||||||
|
|
||||||
this.ctx.restore();
|
this.ctx.restore();
|
||||||
|
Loading…
Reference in New Issue
Block a user