Add a fake canvas for scaling fonts to improve perfs

This commit is contained in:
Vivien Nicolas 2011-06-28 18:24:16 +02:00
parent 63e1601464
commit 80f650b6bb
2 changed files with 21 additions and 9 deletions

View File

@ -26,12 +26,15 @@ var fontName = "";
*/ */
var kDisableFonts = false; var kDisableFonts = false;
/** /**
* Hold a map of decoded fonts and of the standard fourteen Type1 fonts and * Hold a map of decoded fonts and of the standard fourteen Type1 fonts and
* their acronyms. * their acronyms.
* TODO Add the standard fourteen Type1 fonts list by default * TODO Add the standard fourteen Type1 fonts list by default
* http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65 * http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65
*/ */
var kScalePrecision = 100;
var Fonts = { var Fonts = {
_active: null, _active: null,
@ -39,8 +42,9 @@ var Fonts = {
return this._active; return this._active;
}, },
set active(name) { setActive: function fonts_setActive(name, size) {
this._active = this[name]; this._active = this[name];
this.ctx.font = (size * kScalePrecision) + 'px "' + name + '"';
}, },
charsToUnicode: function fonts_chars2Unicode(chars) { charsToUnicode: function fonts_chars2Unicode(chars) {
@ -77,6 +81,17 @@ var Fonts = {
// Enter the translated string into the cache // Enter the translated string into the cache
return active.cache[chars] = str; return active.cache[chars] = str;
},
get ctx() {
delete this.ctx;
var ctx = document.createElement("canvas").getContext("2d");
ctx.scale(1 / kScalePrecision, 1);
return this.ctx = ctx;
},
measureText: function fonts_measureText(text) {
return this.ctx.measureText(text).width / kScalePrecision;
} }
}; };
@ -1292,7 +1307,7 @@ CFF.prototype = {
var glyph = glyphs[i]; var glyph = glyphs[i];
var unicode = GlyphsUnicode[glyph.glyph]; var unicode = GlyphsUnicode[glyph.glyph];
if (!unicode) { if (!unicode) {
if (glyph != ".notdef") if (glyph.glyph != ".notdef")
warn(glyph + " does not have an entry in the glyphs unicode dictionary"); warn(glyph + " does not have an entry in the glyphs unicode dictionary");
} else { } else {
charstrings.push({ charstrings.push({

11
pdf.js
View File

@ -2797,19 +2797,20 @@ var CanvasGraphics = (function() {
if (fontDescriptor && fontDescriptor.num) { if (fontDescriptor && fontDescriptor.num) {
var fontDescriptor = this.xref.fetchIfRef(fontDescriptor); var fontDescriptor = this.xref.fetchIfRef(fontDescriptor);
fontName = fontDescriptor.get("FontName").name.replace("+", "_"); fontName = fontDescriptor.get("FontName").name.replace("+", "_");
Fonts.active = fontName; Fonts.setActive(fontName, size);
} }
if (!fontName) { if (!fontName) {
// TODO: fontDescriptor is not available, fallback to default font // TODO: fontDescriptor is not available, fallback to default font
this.current.fontSize = size; this.current.fontSize = size;
this.ctx.font = this.current.fontSize + 'px sans-serif'; this.ctx.font = this.current.fontSize + 'px sans-serif';
Fonts.setActive("sans-serif", this.current.fontSize);
return; return;
} }
this.current.fontName = fontName; this.current.fontName = fontName;
this.current.fontSize = size; this.current.fontSize = size;
this.ctx.font = this.current.fontSize +'px "' + fontName + '", Symbol'; this.ctx.font = this.current.fontSize + 'px "' + this.current.fontName + '"';
}, },
setTextRenderingMode: function(mode) { setTextRenderingMode: function(mode) {
TODO("text rendering mode"); TODO("text rendering mode");
@ -2851,11 +2852,7 @@ var CanvasGraphics = (function() {
text = Fonts.charsToUnicode(text); text = Fonts.charsToUnicode(text);
this.ctx.translate(this.current.x, -1 * this.current.y); this.ctx.translate(this.current.x, -1 * this.current.y);
this.ctx.fillText(text, 0, 0); this.ctx.fillText(text, 0, 0);
this.current.x += Fonts.measureText(text);
this.ctx.scale(0.05, 1);
this.ctx.font = (this.current.fontSize * 20) + 'px "' + this.current.fontName + '", Symbol';
this.current.x += this.ctx.measureText(text).width / 20;
this.ctx.scale(1, 1);
} }
this.ctx.restore(); this.ctx.restore();