fix to colorspaces

This commit is contained in:
sbarman 2011-07-09 16:58:25 -07:00
parent f2a4756c77
commit d38a5488d4

93
pdf.js
View File

@ -4,7 +4,7 @@
'use strict'; 'use strict';
var ERRORS = 0, WARNINGS = 1, TODOS = 5; var ERRORS = 0, WARNINGS = 1, TODOS = 5;
var verbosity = WARNINGS; var verbosity = TODOS;
function log(msg) { function log(msg) {
if (console && console.log) if (console && console.log)
@ -4078,9 +4078,13 @@ var CanvasGraphics = (function() {
this.setStrokeRGBColor.apply(this, color); this.setStrokeRGBColor.apply(this, color);
}, },
setStrokeColorN: function(/*...*/) { setStrokeColorN: function(/*...*/) {
// TODO real impl var cs = this.getStrokeColorSpace();
TODO('check for special color spaces');
this.setStrokeColor.apply(this, arguments); if (cs.name == 'Pattern') {
this.ctx.strokeStyle = this.getPattern(cs, arguments);
} else {
this.setStrokeColor.apply(this, arguments);
}
}, },
setFillColor: function(/*...*/) { setFillColor: function(/*...*/) {
var cs = this.getFillColorSpace(); var cs = this.getFillColorSpace();
@ -4091,28 +4095,28 @@ var CanvasGraphics = (function() {
var cs = this.getFillColorSpace(); var cs = this.getFillColorSpace();
if (cs.name == 'Pattern') { if (cs.name == 'Pattern') {
var length = arguments.length; this.ctx.fillStyle = this.getPattern(cs, arguments);
var base = cs.base;
if (base) {
var baseComps = base.numComps;
if (baseComps != length - 1)
error("invalid base color for pattern colorspace");
var color = [];
for (var i = 0; i < baseComps; ++i)
color.push(arguments[i]);
color = base.getRgb(color);
}
var patternName = arguments[length - 1];
this.setFillPattern(patternName, base, color);
} else { } else {
// TODO real impl
this.setFillColor.apply(this, arguments); this.setFillColor.apply(this, arguments);
} }
}, },
setFillPattern: function(patternName, baseCS, color) { getPattern: function(cs, args) {
var length = args.length;
var base = cs.base;
if (base) {
var baseComps = base.numComps;
if (baseComps != length - 1)
error("invalid base color for pattern colorspace");
var color = [];
for (var i = 0; i < baseComps; ++i)
color.push(args[i]);
color = base.getRgb(color);
}
var patternName = args[length - 1];
if (!IsName(patternName)) if (!IsName(patternName))
error("Bad args to getPattern"); error("Bad args to getPattern");
@ -4124,17 +4128,17 @@ var CanvasGraphics = (function() {
var pattern = xref.fetchIfRef(patternRes.get(patternName.name)); var pattern = xref.fetchIfRef(patternRes.get(patternName.name));
var dict = IsStream(pattern) ? pattern.dict : pattern; var dict = IsStream(pattern) ? pattern.dict : pattern;
var types = [null, this.setTilingPattern, this.setShadingPattern]; var types = [null, this.getTilingPattern, this.getShadingPattern];
var typeNum = dict.get("PatternType"); var typeNum = dict.get("PatternType");
var patternFn = types[typeNum]; var patternFn = types[typeNum];
if (!patternFn) if (!patternFn)
error("Unhandled pattern type"); error("Unhandled pattern type");
patternFn.call(this, pattern, dict, baseCS, color); return patternFn.call(this, pattern, dict, color);
}, },
setShadingPattern: function(pattern, dict) { getShadingPattern: function(pattern, dict) {
var matrix = dict.get("Matrix"); var matrix = dict.get("Matrix");
/*
var inv = [0,0,0,0,0,0]; var inv = [0,0,0,0,0,0];
var det = 1 / (matrix[0] * matrix[3] - matrix[1] * matrix[2]); var det = 1 / (matrix[0] * matrix[3] - matrix[1] * matrix[2]);
inv[0] = matrix[3] * det; inv[0] = matrix[3] * det;
@ -4145,15 +4149,17 @@ var CanvasGraphics = (function() {
inv[5] = det * (matrix[1] * matrix[4] - matrix[0] * matrix[5]); inv[5] = det * (matrix[1] * matrix[4] - matrix[0] * matrix[5]);
this.transform.apply(this, matrix); this.transform.apply(this, matrix);
var shading = this.getShading(pattern.get("Shading")); //var shading = this.getShading(pattern.get("Shading"));
this.ctx.fillStyle = shading; */ return this.getShading(pattern.get("Shading"));
/* this.ctx.fillStyle = shading;
// HACK to get the gradient to show at the right location. If // HACK to get the gradient to show at the right location. If
// removed, the gradient will show at the pre-transform coordinates. // removed, the gradient will show at the pre-transform coordinates.
this.ctx.fillRect(0,0,0,0); this.ctx.fillRect(0,0,0,0);
this.transform.apply(this, inv); this.transform.apply(this, inv);
}, */ },
setTilingPattern: function(pattern, dict, baseCS, color) { getTilingPattern: function(pattern, dict, color) {
function multiply(m, tm) { function multiply(m, tm) {
var a = m[0] * tm[0] + m[1] * tm[2]; var a = m[0] * tm[0] + m[1] * tm[2];
var b = m[0] * tm[1] + m[1] * tm[3]; var b = m[0] * tm[1] + m[1] * tm[3];
@ -4205,8 +4211,8 @@ var CanvasGraphics = (function() {
tmpCtx.strokeStyle = this.makeCssRgb(0, 0, 0); tmpCtx.strokeStyle = this.makeCssRgb(0, 0, 0);
break; break;
case PAINT_TYPE_UNCOLORED: case PAINT_TYPE_UNCOLORED:
tmpCtx.fillStyle = this.makeCssRgb.apply(this, baseCS.getRgb(color)); tmpCtx.fillStyle = this.makeCssRgb.apply(this, color);
tmpCtx.strokeStyle = this.makeCssRgb.apply(this, baseCS.getRgb(color)); tmpCtx.strokeStyle = this.makeCssRgb.apply(this, color);
break; break;
default: default:
error('Unsupported paint type'); error('Unsupported paint type');
@ -4241,8 +4247,7 @@ var CanvasGraphics = (function() {
this.restore(); this.restore();
TODO('Inverse pattern is painted'); TODO('Inverse pattern is painted');
pattern = this.ctx.createPattern(tmpCanvas, 'repeat'); return this.ctx.createPattern(tmpCanvas, 'repeat');
this.ctx.fillStyle = pattern;
}, },
setStrokeGray: function(gray) { setStrokeGray: function(gray) {
this.setStrokeRGBColor(gray, gray, gray); this.setStrokeRGBColor(gray, gray, gray);
@ -4312,20 +4317,23 @@ var CanvasGraphics = (function() {
this.restore(); this.restore();
}, },
getShading: function(shading) { getShading: function(shading) {
this.save();
shading = this.xref.fetchIfRef(shading); shading = this.xref.fetchIfRef(shading);
var dict = IsStream(shading) ? shading.dict : shading;
var bbox = shading.get('BBox'); var bbox = dict.get('BBox');
if (bbox && IsArray(bbox) && 4 == bbox.length) { if (bbox && IsArray(bbox) && 4 == bbox.length) {
this.rectangle.apply(this, bbox); this.rectangle.apply(this, bbox);
this.clip(); this.clip();
this.endPath(); this.endPath();
} }
var background = shading.get('Background'); var background = dict.get('Background');
if (background) if (background)
TODO('handle background colors'); TODO('handle background colors');
var cs = shading.get('ColorSpace', 'CS'); var cs = dict.get('ColorSpace', 'CS');
cs = ColorSpace.parse(cs, this.xref, this.res); cs = ColorSpace.parse(cs, this.xref, this.res);
var types = [null, var types = [null,
@ -4333,16 +4341,20 @@ var CanvasGraphics = (function() {
this.getAxialShading, this.getAxialShading,
this.getRadialShading]; this.getRadialShading];
var typeNum = shading.get('ShadingType'); var typeNum = dict.get('ShadingType');
var shadingFn = types[typeNum]; var shadingFn = types[typeNum];
this.restore();
// Most likely we will not implement other types of shading // Most likely we will not implement other types of shading
// unless the browser supports them // unless the browser supports them
if (!shadingFn) if (!shadingFn) {
TODO("Unknown or NYI type of shading '"+ typeNum +"'"); TODO("Unknown or NYI type of shading '"+ typeNum +"'");
return this.makeCssRgb(0, 0, 0);
}
return shadingFn.call(this, shading, cs); return shadingFn.call(this, shading, cs);
}, },
getAxialShading: function(sh, cs) { getAxialShading: function(sh, cs) {
var coordsArr = sh.get('Coords'); var coordsArr = sh.get('Coords');
var x0 = coordsArr[0], y0 = coordsArr[1], var x0 = coordsArr[0], y0 = coordsArr[1],
@ -4425,7 +4437,6 @@ var CanvasGraphics = (function() {
gradient.addColorStop((i - t0) / diff, gradient.addColorStop((i - t0) / diff,
this.makeCssRgb.apply(this, rgbColor)); this.makeCssRgb.apply(this, rgbColor));
} }
return gradient; return gradient;
}, },