diff --git a/ink2canvas_lib/canvas.py b/ink2canvas_lib/canvas.py index 091bde38a201a0d19f25df7e96d4be0b13ed0145..ef293e376fad7243e95bbdc43ae3bfa8b3c9e97b 100644 --- a/ink2canvas_lib/canvas.py +++ b/ink2canvas_lib/canvas.py @@ -129,6 +129,9 @@ class Canvas(object): def lineTo(self, x, y): self.write("ctx.lineTo(%f, %f);" % (x, y)) + def closePath(self): + self.write("ctx.closePath();") + def quadraticCurveTo(self, cpx, cpy, x, y): data = (cpx, cpy, x, y) self.write("ctx.quadraticCurveTo(%f, %f, %f, %f);" % data) @@ -178,7 +181,7 @@ class Canvas(object): def restore(self): self.write("ctx.restore();") - def closePath(self): + def finishPath(self): if self.style("fill") is not None: self.write("ctx.fill();") if self.style("stroke") is not None: diff --git a/ink2canvas_lib/svg.py b/ink2canvas_lib/svg.py index 4789eb9cf9910ab2e84ce78ede1259d3006048ec..1aab455a3ad79a7acf0b4009d73eb8617d321508 100644 --- a/ink2canvas_lib/svg.py +++ b/ink2canvas_lib/svg.py @@ -88,6 +88,8 @@ class AbstractShape(Element): if hasattr(self.ctx, method) and style[key] != "none": getattr(self.ctx, method)(style[key]) # saves style to compare in next iteration + if hasattr(self.ctx, "style_cache") and self.ctx.style_cache("opacity") != style("opacity"): + self.ctx.setOpacity(style("opacity")) # opacity is kept in memory, need to reset self.ctx.style_cache = style def has_transform(self): @@ -126,7 +128,7 @@ class AbstractShape(Element): self.set_style(style) # unpacks "data" in parameters to given method getattr(self.ctx, self.command)(*data) - self.ctx.closePath() + self.ctx.finishPath() def end(self): if self.has_transform() or self.has_clip(): @@ -189,7 +191,7 @@ class Ellipse(AbstractShape): self.ctx.bezierCurveTo(cx + rx, cy + (KAPPA * ry), cx + (KAPPA * rx), cy + ry, cx, cy + ry) self.ctx.bezierCurveTo(cx - (KAPPA * rx), cy + ry, cx - rx, cy + (KAPPA * ry), cx - rx, cy) self.ctx.bezierCurveTo(cx - rx, cy - (KAPPA * ry), cx - (KAPPA * rx), cy - ry, cx, cy - ry) - self.ctx.closePath() + self.ctx.finishPath() class Path(AbstractShape): @@ -207,6 +209,9 @@ class Path(AbstractShape): self.ctx.bezierCurveTo(x1, y1, x2, y2, x, y) self.currentPosition = x, y + def pathClose(self, data): + self.ctx.closePath() + def draw(self): """Gets the node type and calls the given method""" style = self.get_style() @@ -219,13 +224,14 @@ class Path(AbstractShape): # Draws path commands path_command = {"M": self.pathMoveTo, "L": self.pathLineTo, - "C": self.pathCurveTo} + "C": self.pathCurveTo, + "Z": self.pathClose} # Make sure we only have Lines and curves (no arcs etc) for comm, data in self.node.path.to_superpath().to_path().to_arrays(): if comm in path_command: path_command[comm](data) - self.ctx.closePath() + self.ctx.finishPath() class Line(Path): diff --git a/tests/data/refs/ink2canvas.out b/tests/data/refs/ink2canvas.out index 9080d0f0ff973b3a5f31a9e111ca2dd2cf86f257..c6bc3c09a2f34bef19bdbdffaadbf17b9e829be1 100644 --- a/tests/data/refs/ink2canvas.out +++ b/tests/data/refs/ink2canvas.out @@ -24,6 +24,7 @@ // #c1 ctx.beginPath(); ctx.fillStyle = 'rgb(0, 0, 128)'; + ctx.globalAlpha = 1.0; ctx.arc(150.000000, 450.000000, 50.000000, 0.000000, 6.28318531, 1); ctx.fill(); @@ -49,6 +50,7 @@ ctx.bezierCurveTo(599.052409, 429.312488, 626.453123, 409.709909, 667.736130, 402.673890); ctx.bezierCurveTo(709.019129, 395.637872, 754.660551, 402.791598, 780.522930, 420.351820); ctx.lineTo(700.000000, 450.000000); + ctx.closePath(); ctx.fill(); ctx.stroke(); @@ -87,6 +89,7 @@ ctx.lineTo(200.000000, 815.037620); ctx.lineTo(245.458240, 807.599150); ctx.lineTo(223.711430, 848.205300); + ctx.closePath(); ctx.fill(); ctx.stroke(); diff --git a/tests/data/refs/ink2canvas__--id__path31.out b/tests/data/refs/ink2canvas__--id__path31.out new file mode 100644 index 0000000000000000000000000000000000000000..fc3062627c2405b92a1fc40e943b2a2012bdb568 --- /dev/null +++ b/tests/data/refs/ink2canvas__--id__path31.out @@ -0,0 +1,49 @@ + + + +
+