cleanup DCTStream rendering code a tad

This commit is contained in:
Andreas Gal 2011-06-20 19:36:40 -04:00
parent 442afb102e
commit a6c48e3bc2

31
pdf.js
View File

@ -48,6 +48,14 @@ function shadow(obj, prop, value) {
return value; return value;
} }
function bytesToString(bytes) {
var str = "";
var length = bytes.length;
for (var n = 0; n < length; ++n)
str += String.fromCharCode(bytes[n]);
return str;
}
var Stream = (function() { var Stream = (function() {
function constructor(arrayBuffer, start, length, dict) { function constructor(arrayBuffer, start, length, dict) {
this.bytes = Uint8Array(arrayBuffer); this.bytes = Uint8Array(arrayBuffer);
@ -518,26 +526,20 @@ var FlateStream = (function() {
return constructor; return constructor;
})(); })();
// A JpegStream can't be read directly. We use the platform to render the underlying
// JPEG data for us.
var JpegStream = (function() { var JpegStream = (function() {
function constructor(bytes, dict) { function constructor(bytes, dict) {
// TODO per poppler, some images may have "junk" before that need to be removed // TODO: per poppler, some images may have "junk" before that need to be removed
this.bytes = bytes;
this.dict = dict; this.dict = dict;
// create DOM image // create DOM image
var buffer = "", i, n = bytes.length;
for (i = 0; i < n; ++i) {
buffer += String.fromCharCode(bytes[i]);
}
var img = new Image(); var img = new Image();
img.src = "data:image/jpeg;base64," + window.btoa(buffer); img.src = "data:image/jpeg;base64," + window.btoa(bytesToString(bytes));
this.domImage = img; this.domImage = img;
} }
constructor.prototype = { constructor.prototype = {
getChar: function() {
TODO("read direct pixels data");
},
getImage: function() { getImage: function() {
return this.domImage; return this.domImage;
} }
@ -2441,7 +2443,7 @@ var CanvasGraphics = (function() {
Fonts.active = fontName; Fonts.active = fontName;
} }
if (!fontName) { if (!fontName) {
// fontDescriptor is not available, fallback to default font (TODO ?) // 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';
return; return;
@ -2854,10 +2856,13 @@ var CanvasGraphics = (function() {
// scale the image to the unit square // scale the image to the unit square
ctx.scale(1/w, -1/h); ctx.scale(1/w, -1/h);
if (image instanceof JpegStream) { // If the platform can render the image format directly, the
// stream has a getImage property which directly returns a
// suitable DOM Image object.
if (image.getImage) {
var domImage = image.getImage(); var domImage = image.getImage();
ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height, ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height,
0, -h, w, h); 0, -h, w, h);
this.restore(); this.restore();
return; return;
} }