whitespace fixes and move inheriting of dict props into the page since it only happens there

This commit is contained in:
Andreas Gal 2011-06-21 15:05:33 -04:00
parent bad69c984c
commit 5a96404705

128
pdf.js
View File

@ -554,12 +554,12 @@ var JpegStream = (function() {
var PredictorStream = (function() { var PredictorStream = (function() {
function constructor(stream, params) { function constructor(stream, params) {
var predictor = this.predictor = params.get("Predictor") || 1; var predictor = this.predictor = params.get("Predictor") || 1;
if (predictor <= 1) if (predictor <= 1)
return stream; // no prediction return stream; // no prediction
if (predictor !== 2 && (predictor < 10 || predictor > 15)) if (predictor !== 2 && (predictor < 10 || predictor > 15))
error("Unsupported predictor"); error("Unsupported predictor");
if (predictor === 2) if (predictor === 2)
this.readRow = this.readRowTiff; this.readRow = this.readRowTiff;
else else
@ -573,7 +573,7 @@ var PredictorStream = (function() {
var colors = this.colors = params.get("Colors") || 1; var colors = this.colors = params.get("Colors") || 1;
var bits = this.bits = params.get("BitsPerComponent") || 8; var bits = this.bits = params.get("BitsPerComponent") || 8;
var columns = this.columns = params.get("Columns") || 1; var columns = this.columns = params.get("Columns") || 1;
var pixBytes = this.pixBytes = (colors * bits + 7) >> 3; var pixBytes = this.pixBytes = (colors * bits + 7) >> 3;
// add an extra pixByte to represent the pixel left of column 0 // add an extra pixByte to represent the pixel left of column 0
var rowBytes = this.rowBytes = ((columns * colors * bits + 7) >> 3) + pixBytes; var rowBytes = this.rowBytes = ((columns * colors * bits + 7) >> 3) + pixBytes;
@ -784,16 +784,6 @@ var Dict = (function() {
get2: function(key1, key2) { get2: function(key1, key2) {
return this.get(key1) || this.get(key2); return this.get(key1) || this.get(key2);
}, },
getOrInherit: function(key, xref) {
var obj = this.map[key];
var dict = this;
while (!obj && dict) {
dict = xref.fetchIfRef(dict.get("Parent"));
if (dict)
obj = dict.get(key);
}
return obj;
},
has: function(key) { has: function(key) {
return key in this.map; return key in this.map;
}, },
@ -1116,7 +1106,7 @@ var Lexer = (function() {
break; break;
} }
} }
// start reading token // start reading token
switch (ch) { switch (ch) {
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
@ -1290,7 +1280,7 @@ var Parser = (function() {
} }
return str; return str;
} }
// simple object // simple object
var obj = this.buf1; var obj = this.buf1;
this.shift(); this.shift();
@ -1303,7 +1293,7 @@ var Parser = (function() {
// get stream start position // get stream start position
lexer.skipToNextLine(); lexer.skipToNextLine();
var pos = stream.pos; var pos = stream.pos;
// get length // get length
var length = dict.get("Length"); var length = dict.get("Length");
var xref = this.xref; var xref = this.xref;
@ -1373,7 +1363,7 @@ var Parser = (function() {
return constructor; return constructor;
})(); })();
var Linearization = (function() { var Linearization = (function() {
function constructor(stream) { function constructor(stream) {
this.parser = new Parser(new Lexer(stream), false); this.parser = new Parser(new Lexer(stream), false);
@ -1707,15 +1697,28 @@ var Page = (function() {
} }
constructor.prototype = { constructor.prototype = {
getPageProp: function(key) {
return this.pageDict.get(key);
},
inheritPageProp: function(key) {
var dict = this.pageDict;
var obj = dict.get(key);
while (!obj) {
dict = this.xref.fetchIfRef(dict.get("Parent"));
if (!dict)
break;
obj = dict.get(key);
}
return obj;
},
get content() { get content() {
return shadow(this, "content", this.pageDict.get("Contents")); return shadow(this, "content", this.getPageProp("Contents"));
}, },
get resources() { get resources() {
return shadow(this, "resources", return shadow(this, "resources", this.inheritPageProp("Resources"));
this.pageDict.getOrInherit("Resources", this.xref));
}, },
get mediaBox() { get mediaBox() {
var obj = this.pageDict.getOrInherit("MediaBox", this.xref); var obj = this.inheritPageProp("MediaBox");
return shadow(this, "mediaBox", ((IsArray(obj) && obj.length == 4) return shadow(this, "mediaBox", ((IsArray(obj) && obj.length == 4)
? obj ? obj
: null)); : null));
@ -1804,7 +1807,7 @@ var Catalog = (function() {
pageCache.push(new Page(this.xref, pageCache.length, obj)); pageCache.push(new Page(this.xref, pageCache.length, obj));
} else { // must be a child page dictionary } else { // must be a child page dictionary
assertWellFormed(IsDict(obj), assertWellFormed(IsDict(obj),
"page dictionary kid reference points to wrong type of object"); "page dictionary kid reference points to wrong type of object");
this.traverseKids(obj); this.traverseKids(obj);
} }
} }
@ -2890,7 +2893,6 @@ var CanvasGraphics = (function() {
shadingFill: function(entryRef) { shadingFill: function(entryRef) {
var xref = this.xref; var xref = this.xref;
var res = this.res; var res = this.res;
var shadingRes = xref.fetchIfRef(res.get("Shading")); var shadingRes = xref.fetchIfRef(res.get("Shading"));
if (!shadingRes) if (!shadingRes)
error("No shading resource found"); error("No shading resource found");
@ -2915,12 +2917,14 @@ var CanvasGraphics = (function() {
if (background) if (background)
TODO("handle background colors"); TODO("handle background colors");
const types = [null, this.fillFunctionShading, const types = [null,
this.fillAxialShading, this.fillRadialShading]; this.fillFunctionShading,
this.fillAxialShading,
this.fillRadialShading];
var typeNum = shading.get("ShadingType"); var typeNum = shading.get("ShadingType");
var fillFn = types[typeNum]; var fillFn = types[typeNum];
if (!fillFn) if (!fillFn)
error("Unknown type of shading"); error("Unknown type of shading");
fillFn.apply(this, [shading]); fillFn.apply(this, [shading]);
@ -2931,7 +2935,7 @@ var CanvasGraphics = (function() {
var coordsArr = sh.get("Coords"); var coordsArr = sh.get("Coords");
var x0 = coordsArr[0], y0 = coordsArr[1], var x0 = coordsArr[0], y0 = coordsArr[1],
x1 = coordsArr[2], y1 = coordsArr[3]; x1 = coordsArr[2], y1 = coordsArr[3];
var t0 = 0.0, t1 = 1.0; var t0 = 0.0, t1 = 1.0;
if (sh.has("Domain")) { if (sh.has("Domain")) {
var domainArr = sh.get("Domain"); var domainArr = sh.get("Domain");
@ -2990,12 +2994,12 @@ var CanvasGraphics = (function() {
return; return;
xobj = this.xref.fetchIfRef(xobj); xobj = this.xref.fetchIfRef(xobj);
assertWellFormed(IsStream(xobj), "XObject should be a stream"); assertWellFormed(IsStream(xobj), "XObject should be a stream");
var oc = xobj.dict.get("OC"); var oc = xobj.dict.get("OC");
if (oc) { if (oc) {
TODO("oc for xobject"); TODO("oc for xobject");
} }
var opi = xobj.dict.get("OPI"); var opi = xobj.dict.get("OPI");
if (opi) { if (opi) {
TODO("opi for xobject"); TODO("opi for xobject");
@ -3097,15 +3101,14 @@ var CanvasGraphics = (function() {
// actual image // actual image
var csStream = dict.get2("ColorSpace", "CS"); var csStream = dict.get2("ColorSpace", "CS");
csStream = xref.fetchIfRef(csStream); csStream = xref.fetchIfRef(csStream);
if (IsName(csStream) && inline) if (IsName(csStream) && inline)
csStream = colorSpaces.get(csStream); csStream = colorSpaces.get(csStream);
var colorSpace = new ColorSpace(xref, csStream);
var colorSpace = new ColorSpace(xref, csStream);
var decode = dict.get2("Decode", "D"); var decode = dict.get2("Decode", "D");
TODO("create color map"); TODO("create color map");
var mask = image.dict.get("Mask"); var mask = image.dict.get("Mask");
mask = xref.fetchIfRef(mask); mask = xref.fetchIfRef(mask);
var smask = image.dict.get("SMask"); var smask = image.dict.get("SMask");
@ -3130,7 +3133,7 @@ var CanvasGraphics = (function() {
var maskBPC = maskDict.get2("BitsPerComponent", "BPC"); var maskBPC = maskDict.get2("BitsPerComponent", "BPC");
if (!maskBPC) if (!maskBPC)
error("Invalid image mask bpc"); error("Invalid image mask bpc");
var maskCsStream = maskDict.get2("ColorSpace", "CS"); var maskCsStream = maskDict.get2("ColorSpace", "CS");
maskCsStream = xref.fetchIfRef(maskCsStream); maskCsStream = xref.fetchIfRef(maskCsStream);
var maskColorSpace = new ColorSpace(xref, maskCsStream); var maskColorSpace = new ColorSpace(xref, maskCsStream);
@ -3140,7 +3143,7 @@ var CanvasGraphics = (function() {
var maskDecode = maskDict.get2("Decode", "D"); var maskDecode = maskDict.get2("Decode", "D");
if (maskDecode) if (maskDecode)
TODO("Handle mask decode"); TODO("Handle mask decode");
// handle matte object // handle matte object
} }
var tmpCanvas = document.createElement("canvas"); var tmpCanvas = document.createElement("canvas");
@ -3149,21 +3152,21 @@ var CanvasGraphics = (function() {
var tmpCtx = tmpCanvas.getContext("2d"); var tmpCtx = tmpCanvas.getContext("2d");
var imgData = tmpCtx.getImageData(0, 0, w, h); var imgData = tmpCtx.getImageData(0, 0, w, h);
var pixels = imgData.data; var pixels = imgData.data;
if (bitsPerComponent != 8) if (bitsPerComponent != 8)
error("unhandled number of bits per component"); error("unhandled number of bits per component");
if (smask) { if (smask) {
if (maskColorSpace.numComps != 1) if (maskColorSpace.numComps != 1)
error("Incorrect number of components in smask"); error("Incorrect number of components in smask");
var numComps = colorSpace.numComps; var numComps = colorSpace.numComps;
var imgArray = image.getBytes(numComps * w * h); var imgArray = image.getBytes(numComps * w * h);
var imgIdx = 0; var imgIdx = 0;
var smArray = smask.getBytes(w * h); var smArray = smask.getBytes(w * h);
var smIdx = 0; var smIdx = 0;
var length = 4 * w * h; var length = 4 * w * h;
switch (numComps) { switch (numComps) {
case 1: case 1:
@ -3190,7 +3193,7 @@ var CanvasGraphics = (function() {
var numComps = colorSpace.numComps; var numComps = colorSpace.numComps;
var imgArray = image.getBytes(numComps * w * h); var imgArray = image.getBytes(numComps * w * h);
var imgIdx = 0; var imgIdx = 0;
var length = 4 * w * h; var length = 4 * w * h;
switch (numComps) { switch (numComps) {
case 1: case 1:
@ -3300,7 +3303,7 @@ var ColorSpace = (function() {
} else if (IsArray(cs)) { } else if (IsArray(cs)) {
var mode = cs[0].name; var mode = cs[0].name;
this.mode = mode; this.mode = mode;
var stream = cs[1]; var stream = cs[1];
stream = xref.fetchIfRef(stream); stream = xref.fetchIfRef(stream);
@ -3313,7 +3316,6 @@ var ColorSpace = (function() {
break; break;
case "ICCBased": case "ICCBased":
var dict = stream.dict; var dict = stream.dict;
this.stream = stream; this.stream = stream;
this.dict = dict; this.dict = dict;
this.numComps = dict.get("N"); this.numComps = dict.get("N");
@ -3325,7 +3327,7 @@ var ColorSpace = (function() {
error("unrecognized color space object"); error("unrecognized color space object");
} }
}; };
constructor.prototype = { constructor.prototype = {
}; };
@ -3338,13 +3340,15 @@ var PDFFunction = (function() {
if (!dict) if (!dict)
dict = fn; dict = fn;
const types = [this.constructSampled, null, const types = [this.constructSampled,
this.constructInterpolated, this.constructStiched, null,
this.constructPostScript]; this.constructInterpolated,
this.constructStiched,
this.constructPostScript];
var typeNum = dict.get("FunctionType"); var typeNum = dict.get("FunctionType");
var typeFn = types[typeNum]; var typeFn = types[typeNum];
if (!typeFn) if (!typeFn)
error("Unknown type of function"); error("Unknown type of function");
typeFn.apply(this, [fn, dict]); typeFn.apply(this, [fn, dict]);
@ -3357,7 +3361,7 @@ var PDFFunction = (function() {
if (!domain || !range) if (!domain || !range)
error("No domain or range"); error("No domain or range");
var inputSize = domain.length / 2; var inputSize = domain.length / 2;
var outputSize = range.length / 2; var outputSize = range.length / 2;
@ -3371,7 +3375,7 @@ var PDFFunction = (function() {
order = 1; order = 1;
if (order !== 1) if (order !== 1)
error ("No support for cubic spline interpolation"); error ("No support for cubic spline interpolation");
var encode = dict.get("Encode"); var encode = dict.get("Encode");
if (!encode) { if (!encode) {
encode = []; encode = [];
@ -3400,15 +3404,14 @@ var PDFFunction = (function() {
for (var i = 0; i < inputSize; i++) { for (var i = 0; i < inputSize; i++) {
var i2 = i * 2; var i2 = i * 2;
// clip to the domain // clip to the domain
var v = clip(args[i], domain[i2], domain[i2 + 1]); var v = clip(args[i], domain[i2], domain[i2 + 1]);
// encode // encode
v = encode[i2] + ((v - domain[i2]) * v = encode[i2] + ((v - domain[i2]) *
(encode[i2 + 1] - encode[i2]) / (encode[i2 + 1] - encode[i2]) /
(domain[i2 + 1] - domain[i2])); (domain[i2 + 1] - domain[i2]));
// clip to the size // clip to the size
args[i] = clip(v, 0, size[i] - 1); args[i] = clip(v, 0, size[i] - 1);
} }
@ -3431,12 +3434,11 @@ var PDFFunction = (function() {
var high = samples[ceil + i]; var high = samples[ceil + i];
var v = low * scale + high * (1 - scale); var v = low * scale + high * (1 - scale);
} }
var i2 = i * 2; var i2 = i * 2;
// decode // decode
v = decode[i2] + (v * (decode[i2 + 1] - decode[i2]) / v = decode[i2] + (v * (decode[i2 + 1] - decode[i2]) /
((1 << bps) - 1)); ((1 << bps) - 1));
// clip to the domain // clip to the domain
output.push(clip(v, range[i2], range[i2 + 1])); output.push(clip(v, range[i2], range[i2 + 1]));
} }
@ -3471,10 +3473,10 @@ var PDFFunction = (function() {
}, },
constructInterpolated: function() { constructInterpolated: function() {
error("unhandled type of function"); error("unhandled type of function");
}, },
constructStiched: function() { constructStiched: function() {
error("unhandled type of function"); error("unhandled type of function");
}, },
constructPostScript: function() { constructPostScript: function() {
error("unhandled type of function"); error("unhandled type of function");
} }