From 80f650b6bb95f3c2d10057b4d268f66af608eb97 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Tue, 28 Jun 2011 18:24:16 +0200 Subject: [PATCH] Add a fake canvas for scaling fonts to improve perfs --- fonts.js | 19 +++++++++++++++++-- pdf.js | 11 ++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/fonts.js b/fonts.js index d69746993..4ac5935b5 100644 --- a/fonts.js +++ b/fonts.js @@ -26,12 +26,15 @@ var fontName = ""; */ var kDisableFonts = false; + /** * Hold a map of decoded fonts and of the standard fourteen Type1 fonts and * their acronyms. * TODO Add the standard fourteen Type1 fonts list by default * http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65 */ + +var kScalePrecision = 100; var Fonts = { _active: null, @@ -39,8 +42,9 @@ var Fonts = { return this._active; }, - set active(name) { + setActive: function fonts_setActive(name, size) { this._active = this[name]; + this.ctx.font = (size * kScalePrecision) + 'px "' + name + '"'; }, charsToUnicode: function fonts_chars2Unicode(chars) { @@ -77,6 +81,17 @@ var Fonts = { // Enter the translated string into the cache 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 unicode = GlyphsUnicode[glyph.glyph]; if (!unicode) { - if (glyph != ".notdef") + if (glyph.glyph != ".notdef") warn(glyph + " does not have an entry in the glyphs unicode dictionary"); } else { charstrings.push({ diff --git a/pdf.js b/pdf.js index 2d25cf707..f09ccd866 100644 --- a/pdf.js +++ b/pdf.js @@ -2797,19 +2797,20 @@ var CanvasGraphics = (function() { if (fontDescriptor && fontDescriptor.num) { var fontDescriptor = this.xref.fetchIfRef(fontDescriptor); fontName = fontDescriptor.get("FontName").name.replace("+", "_"); - Fonts.active = fontName; + Fonts.setActive(fontName, size); } if (!fontName) { // TODO: fontDescriptor is not available, fallback to default font this.current.fontSize = size; this.ctx.font = this.current.fontSize + 'px sans-serif'; + Fonts.setActive("sans-serif", this.current.fontSize); return; } this.current.fontName = fontName; 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) { TODO("text rendering mode"); @@ -2851,11 +2852,7 @@ var CanvasGraphics = (function() { text = Fonts.charsToUnicode(text); this.ctx.translate(this.current.x, -1 * this.current.y); this.ctx.fillText(text, 0, 0); - - 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.current.x += Fonts.measureText(text); } this.ctx.restore();