Resolve the char->glyphs mapping issue

This commit is contained in:
Vivien Nicolas 2011-06-16 03:55:45 +02:00
parent 2e87e8ca83
commit 3dbfde89a3
3 changed files with 21 additions and 15 deletions

View File

@ -31,7 +31,7 @@ var fontCount = 0;
var Fonts = { var Fonts = {
_active: null, _active: null,
get active() { get active() {
return this._active || { encoding: {} }; return this._active || { encoding: [] };
}, },
set active(aName) { set active(aName) {

11
pdf.js
View File

@ -795,7 +795,6 @@ var Lexer = (function() {
} }
} }
x = Fonts.unicodeFromCode(x);
str += String.fromCharCode(x); str += String.fromCharCode(x);
break; break;
case '\r': case '\r':
@ -811,8 +810,7 @@ var Lexer = (function() {
} }
break; break;
default: default:
var unicode = Fonts.unicodeFromCode(ch.charCodeAt(0)); str += ch;
str += String.fromCharCode(unicode);
break; break;
} }
} while (!done); } while (!done);
@ -2054,7 +2052,12 @@ var CanvasGraphics = (function() {
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(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();

19
test.js
View File

@ -1,7 +1,7 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- / /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- /
/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */ /* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */
var pdfDocument, canvas, pageDisplay, pageNum, pageTimeout; var pdfDocument, canvas, pageDisplay, pageNum, pageInterval;
function load() { function load() {
canvas = document.getElementById("canvas"); canvas = document.getElementById("canvas");
canvas.mozOpaque = true; canvas.mozOpaque = true;
@ -48,7 +48,7 @@ function gotoPage(num) {
function displayPage(num) { function displayPage(num) {
if (pageNum != num) if (pageNum != num)
window.clearTimeout(pageTimeout); window.clearTimeout(pageInterval);
document.getElementById("pageNumber").value = num; document.getElementById("pageNumber").value = num;
@ -57,7 +57,6 @@ function displayPage(num) {
var page = pdfDocument.getPage(pageNum = num); var page = pdfDocument.getPage(pageNum = num);
var t1 = Date.now(); var t1 = Date.now();
var ctx = canvas.getContext("2d"); var ctx = canvas.getContext("2d");
ctx.save(); ctx.save();
ctx.fillStyle = "rgb(255, 255, 255)"; ctx.fillStyle = "rgb(255, 255, 255)";
@ -73,17 +72,21 @@ function displayPage(num) {
page.compile(gfx, fonts); page.compile(gfx, fonts);
var t2 = Date.now(); 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++) { for (var i = 0; i < fonts.length; i++) {
if (fonts[i].loading) if (fonts[i].loading)
return; return;
} }
page.display(gfx);
var t3 = Date.now(); var t3 = Date.now();
clearInterval(pageInterval);
page.display(gfx);
var t4 = Date.now();
var infoDisplay = document.getElementById("info"); var infoDisplay = document.getElementById("info");
infoDisplay.innerHTML = "Time to load/compile/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + " ms"; infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms";
clearInterval(interval);
}, 10); }, 10);
} }