Added hack to display gradients

This commit is contained in:
sbarman 2011-07-05 22:51:58 -07:00
parent 8621e1c358
commit 493fb45efd

41
pdf.js
View File

@ -3948,13 +3948,13 @@ var CanvasGraphics = (function() {
if (cs.name == "Pattern") { if (cs.name == "Pattern") {
var patternName = arguments[0]; var patternName = arguments[0];
this.ctx.fillStyle = this.getPattern(patternName); this.setFillPattern(patternName);
} else { } else {
// TODO real impl // TODO real impl
this.setFillColor.apply(this, arguments); this.setFillColor.apply(this, arguments);
} }
}, },
getPattern: function(patternName) { setFillPattern: function(patternName) {
if (!IsName(patternName)) if (!IsName(patternName))
error("Bad args to getPattern"); error("Bad args to getPattern");
@ -3964,27 +3964,40 @@ var CanvasGraphics = (function() {
error("Unable to find pattern resource"); error("Unable to find pattern resource");
var pattern = xref.fetchIfRef(patternRes.get(patternName.name)); var pattern = xref.fetchIfRef(patternRes.get(patternName.name));
var patternDict = IsStream(pattern) ? pattern.dict : pattern; var dict = IsStream(pattern) ? pattern.dict : pattern;
var types = [null, this.getTilingPattern, var types = [null, this.setTilingPattern,
this.getShadingPattern]; this.setShadingPattern];
var typeNum = patternDict.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");
return patternFn.call(this, pattern, patternDict); patternFn.call(this, pattern, dict);
}, },
getShadingPattern: function(pattern) { setShadingPattern: function(pattern, dict) {
var matrix = pattern.get("Matrix"); var matrix = dict.get("Matrix");
var inv = [0,0,0,0,0,0];
var det = 1 / (matrix[0] * matrix[3] - matrix[1] * matrix[2]);
inv[0] = matrix[3] * det;
inv[1] = -matrix[1] * det;
inv[2] = -matrix[2] * det;
inv[3] = matrix[0] * det;
inv[4] = det * (matrix[2] * matrix[5] - matrix[3] * matrix[4]);
inv[5] = det * (matrix[1] * matrix[4] - matrix[0] * matrix[5]);
this.save();
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.restore(); this.restore();
this.ctx.fillStyle = shading;
return shading; // HACK to get the gradient to show at the right location. If
// removed, the gradient will show at the pre-transform coordinates.
this.ctx.fillRect(0,0,0,0);
this.transform.apply(this, inv);
}, },
getTilingPattern: function(pattern) { setTilingPattern: function(pattern, dict) {
function applyMatrix(point, m) { function applyMatrix(point, m) {
var x = point[0] * m[0] + point[1] * m[2] + m[4]; var x = point[0] * m[0] + point[1] * m[2] + m[4];
var y = point[0] * m[1] + point[1] * m[3] + m[5]; var y = point[0] * m[1] + point[1] * m[3] + m[5];
@ -4002,7 +4015,6 @@ var CanvasGraphics = (function() {
}; };
this.save(); this.save();
var dict = pattern.dict;
var ctx = this.ctx; var ctx = this.ctx;
var paintType = dict.get("PaintType"); var paintType = dict.get("PaintType");
@ -4075,8 +4087,7 @@ var CanvasGraphics = (function() {
this.restore(); this.restore();
TODO("Inverse pattern is painted"); TODO("Inverse pattern is painted");
pattern = this.ctx.createPattern(tmpCanvas, "repeat"); this.ctx.fillStyle = this.ctx.createPattern(tmpCanvas, "repeat");
return pattern;
}, },
setStrokeGray: function(gray) { setStrokeGray: function(gray) {
this.setStrokeRGBColor(gray, gray, gray); this.setStrokeRGBColor(gray, gray, gray);