Fix gradients for fill and stroke.

This commit is contained in:
Brendan Dahl 2012-03-29 08:53:51 -07:00
parent e641dabd2c
commit 7008d07fde
6 changed files with 42 additions and 31 deletions

View File

@ -924,7 +924,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
var pattern = new TilingPattern(IR, color, this.ctx, this.objs); var pattern = new TilingPattern(IR, color, this.ctx, this.objs);
} else if (IR[0] == 'RadialAxial' || IR[0] == 'Dummy') { } else if (IR[0] == 'RadialAxial' || IR[0] == 'Dummy') {
var pattern = Pattern.shadingFromIR(this.ctx, IR); var pattern = Pattern.shadingFromIR(IR);
} else { } else {
error('Unkown IR type ' + IR[0]); error('Unkown IR type ' + IR[0]);
} }
@ -1008,7 +1008,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var ctx = this.ctx; var ctx = this.ctx;
this.save(); this.save();
ctx.fillStyle = Pattern.shadingFromIR(ctx, patternIR); var pattern = Pattern.shadingFromIR(patternIR);
ctx.fillStyle = pattern.getPattern(ctx);
var inv = ctx.mozCurrentTransformInverse; var inv = ctx.mozCurrentTransformInverse;
if (inv) { if (inv) {

View File

@ -305,8 +305,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
else if (typeNum == SHADING_PATTERN) { else if (typeNum == SHADING_PATTERN) {
var shading = xref.fetchIfRef(dict.get('Shading')); var shading = xref.fetchIfRef(dict.get('Shading'));
var matrix = dict.get('Matrix'); var matrix = dict.get('Matrix');
var pattern = Pattern.parseShading(shading, matrix, xref, res, var pattern = Pattern.parseShading(shading, matrix, xref,
null /*ctx*/); res);
args = pattern.getIR(); args = pattern.getIR();
} else { } else {
error('Unkown PatternType ' + typeNum); error('Unkown PatternType ' + typeNum);
@ -377,8 +377,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
if (!shading) if (!shading)
error('No shading object found'); error('No shading object found');
var shadingFill = Pattern.parseShading(shading, null, xref, res, var shadingFill = Pattern.parseShading(shading, null, xref, res);
null);
var patternIR = shadingFill.getIR(); var patternIR = shadingFill.getIR();
args = [patternIR]; args = [patternIR];
fn = 'shadingFill'; fn = 'shadingFill';

View File

@ -22,12 +22,11 @@ var Pattern = (function PatternClosure() {
} }
}; };
Pattern.shadingFromIR = function pattern_shadingFromIR(ctx, raw) { Pattern.shadingFromIR = function pattern_shadingFromIR(raw) {
return Shadings[raw[0]].fromIR(ctx, raw); return Shadings[raw[0]].fromIR(raw);
}; };
Pattern.parseShading = function pattern_shading(shading, matrix, xref, Pattern.parseShading = function pattern_shading(shading, matrix, xref, res) {
res, ctx) {
var dict = isStream(shading) ? shading.dict : shading; var dict = isStream(shading) ? shading.dict : shading;
var type = dict.get('ShadingType'); var type = dict.get('ShadingType');
@ -36,7 +35,7 @@ var Pattern = (function PatternClosure() {
case PatternType.AXIAL: case PatternType.AXIAL:
case PatternType.RADIAL: case PatternType.RADIAL:
// Both radial and axial shadings are handled by RadialAxial shading. // Both radial and axial shadings are handled by RadialAxial shading.
return new Shadings.RadialAxial(dict, matrix, xref, res, ctx); return new Shadings.RadialAxial(dict, matrix, xref, res);
default: default:
return new Shadings.Dummy(); return new Shadings.Dummy();
} }
@ -102,36 +101,40 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
this.colorStops = colorStops; this.colorStops = colorStops;
} }
RadialAxial.fromIR = function radialAxialShadingGetIR(ctx, raw) { RadialAxial.fromIR = function radialAxialShadingGetIR(raw) {
var type = raw[1]; var type = raw[1];
var colorStops = raw[2]; var colorStops = raw[2];
var p0 = raw[3]; var p0 = raw[3];
var p1 = raw[4]; var p1 = raw[4];
var r0 = raw[5]; var r0 = raw[5];
var r1 = raw[6]; var r1 = raw[6];
return {
type: 'Pattern',
getPattern: function(ctx) {
var curMatrix = ctx.mozCurrentTransform;
if (curMatrix) {
var userMatrix = ctx.mozCurrentTransformInverse;
var curMatrix = ctx.mozCurrentTransform; p0 = Util.applyTransform(p0, curMatrix);
if (curMatrix) { p0 = Util.applyTransform(p0, userMatrix);
var userMatrix = ctx.mozCurrentTransformInverse;
p0 = Util.applyTransform(p0, curMatrix); p1 = Util.applyTransform(p1, curMatrix);
p0 = Util.applyTransform(p0, userMatrix); p1 = Util.applyTransform(p1, userMatrix);
}
p1 = Util.applyTransform(p1, curMatrix); var grad;
p1 = Util.applyTransform(p1, userMatrix); if (type == PatternType.AXIAL)
} grad = ctx.createLinearGradient(p0[0], p0[1], p1[0], p1[1]);
else if (type == PatternType.RADIAL)
grad = ctx.createRadialGradient(p0[0], p0[1], r0, p1[0], p1[1], r1);
var grad; for (var i = 0, ii = colorStops.length; i < ii; ++i) {
if (type == PatternType.AXIAL) var c = colorStops[i];
grad = ctx.createLinearGradient(p0[0], p0[1], p1[0], p1[1]); grad.addColorStop(c[0], c[1]);
else if (type == PatternType.RADIAL) }
grad = ctx.createRadialGradient(p0[0], p0[1], r0, p1[0], p1[1], r1); return grad;
}
for (var i = 0, ii = colorStops.length; i < ii; ++i) { };
var c = colorStops[i];
grad.addColorStop(c[0], c[1]);
}
return grad;
}; };
RadialAxial.prototype = { RadialAxial.prototype = {

View File

@ -30,3 +30,5 @@
!zerowidthline.pdf !zerowidthline.pdf
!issue1002.pdf !issue1002.pdf
!issue925.pdf !issue925.pdf
!gradientfill.pdf

BIN
test/pdfs/gradientfill.pdf Normal file

Binary file not shown.

View File

@ -558,5 +558,11 @@
"rounds": 1, "rounds": 1,
"link": true, "link": true,
"type": "eq" "type": "eq"
},
{ "id": "gradientfill",
"file": "pdfs/gradientfill.pdf",
"md5": "cbc1988e4803f647fa83467a85f0e231",
"rounds": 1,
"type": "eq"
} }
] ]