From 3dbfde89a34796ff8060738f38a227d70be27580 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Thu, 16 Jun 2011 03:55:45 +0200 Subject: [PATCH] Resolve the char->glyphs mapping issue --- PDFFont.js | 2 +- pdf.js | 15 +++++++++------ test.js | 19 +++++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/PDFFont.js b/PDFFont.js index d106e0b23..48554d9d1 100644 --- a/PDFFont.js +++ b/PDFFont.js @@ -31,7 +31,7 @@ var fontCount = 0; var Fonts = { _active: null, get active() { - return this._active || { encoding: {} }; + return this._active || { encoding: [] }; }, set active(aName) { diff --git a/pdf.js b/pdf.js index 2b7eb1e1b..fe636bcf9 100644 --- a/pdf.js +++ b/pdf.js @@ -795,7 +795,6 @@ var Lexer = (function() { } } - x = Fonts.unicodeFromCode(x); str += String.fromCharCode(x); break; case '\r': @@ -811,8 +810,7 @@ var Lexer = (function() { } break; default: - var unicode = Fonts.unicodeFromCode(ch.charCodeAt(0)); - str += String.fromCharCode(unicode); + str += ch; break; } } while (!done); @@ -1730,7 +1728,7 @@ var CanvasGraphics = (function() { var descriptor = xref.fetch(fontDict.get("FontDescriptor")); var fontName = descriptor.get("FontName").name; fontName = fontName.replace("+", "_"); - + var font = Fonts[fontName]; if (!font) { var fontFile = descriptor.get2("FontFile", "FontFile2"); @@ -1760,7 +1758,7 @@ var CanvasGraphics = (function() { for (var j = 0; j < widths.length; j++) { var index = widths[j]; if (index) - charset.push(encoding[j + firstchar]); + charset.push(encoding[j + firstchar]); } } } @@ -2054,7 +2052,12 @@ var CanvasGraphics = (function() { this.ctx.scale(1, -1); this.ctx.transform.apply(this.ctx, this.current.textMatrix); - this.ctx.fillText(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.ctx.restore(); diff --git a/test.js b/test.js index 75b720002..d0d386872 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- / /* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */ -var pdfDocument, canvas, pageDisplay, pageNum, pageTimeout; +var pdfDocument, canvas, pageDisplay, pageNum, pageInterval; function load() { canvas = document.getElementById("canvas"); canvas.mozOpaque = true; @@ -48,7 +48,7 @@ function gotoPage(num) { function displayPage(num) { if (pageNum != num) - window.clearTimeout(pageTimeout); + window.clearTimeout(pageInterval); document.getElementById("pageNumber").value = num; @@ -57,7 +57,6 @@ function displayPage(num) { var page = pdfDocument.getPage(pageNum = num); var t1 = Date.now(); - var ctx = canvas.getContext("2d"); ctx.save(); ctx.fillStyle = "rgb(255, 255, 255)"; @@ -73,17 +72,21 @@ function displayPage(num) { page.compile(gfx, fonts); var t2 = Date.now(); - var interval = setInterval(function() { + // FIXME This need to be replaced by an event + pageInterval = setInterval(function() { for (var i = 0; i < fonts.length; i++) { if (fonts[i].loading) return; } - - page.display(gfx); var t3 = Date.now(); + + clearInterval(pageInterval); + page.display(gfx); + + var t4 = Date.now(); + var infoDisplay = document.getElementById("info"); - infoDisplay.innerHTML = "Time to load/compile/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + " ms"; - clearInterval(interval); + infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms"; }, 10); }