Skip to content

Commit

Permalink
Fix closePath by using PDF command (#3304)
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtGokhan authored Nov 3, 2021
1 parent 99927b0 commit 8094918
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/modules/context2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,21 +823,10 @@ import {
typeof this.path[i + 1].x === "number"
) {
pathBegin = new Point(this.path[i + 1].x, this.path[i + 1].y);
this.path.push({
type: "lt",
x: pathBegin.x,
y: pathBegin.y
});
break;
}
}
}
if (
typeof this.path[i + 2] === "object" &&
typeof this.path[i + 2].x === "number"
) {
this.path.push(JSON.parse(JSON.stringify(this.path[i + 2])));
}
this.path.push({
type: "close"
});
Expand Down Expand Up @@ -995,9 +984,6 @@ import {
}
counterclockwise = Boolean(counterclockwise);

var x_start = x + radius * Math.cos(startAngle);
var y_start = y + radius * Math.sin(startAngle);

if (!this.ctx.transform.isIdentity) {
var xpt = this.ctx.transform.applyToPoint(new Point(x, y));
x = xpt.x;
Expand All @@ -1014,7 +1000,6 @@ import {
startAngle = 0;
endAngle = 2 * Math.PI;
}
this.lineTo(x_start, y_start);

this.path.push({
type: "arc",
Expand Down Expand Up @@ -2096,6 +2081,7 @@ import {
style = null;
}

var began = false;
for (var k = 0; k < moves.length; k++) {
if (moves[k].arc) {
var arcs = moves[k].abs;
Expand All @@ -2113,21 +2099,22 @@ import {
arc.endAngle,
arc.counterclockwise,
undefined,
isClip
isClip,
!began
);
} else {
drawLine.call(this, arc.x, arc.y);
}
began = true;
}
putStyle.call(this, style);
} else if (moves[k].close === true) {
this.pdf.internal.out("h");
}
if (!moves[k].arc) {
if (moves[k].close !== true && moves[k].begin !== true) {
var x = moves[k].start.x;
var y = moves[k].start.y;
drawLines.call(this, moves[k].deltas, x, y);
}
began = false;
} else if (moves[k].begin !== true) {
var x = moves[k].start.x;
var y = moves[k].start.y;
drawLines.call(this, moves[k].deltas, x, y);
began = true;
}
}

Expand Down Expand Up @@ -2205,15 +2192,28 @@ import {
* @param style
* @param isClip
*/
var drawArc = function(x, y, r, a1, a2, counterclockwise, style, isClip) {
var drawArc = function(
x,
y,
r,
a1,
a2,
counterclockwise,
style,
isClip,
includeMove
) {
// http://hansmuller-flex.blogspot.com/2011/10/more-about-approximating-circular-arcs.html
var includeMove = true;
var curves = createArc.call(this, r, a1, a2, counterclockwise);

for (var i = 0; i < curves.length; i++) {
var curve = curves[i];
if (includeMove && i === 0) {
doMove.call(this, curve.x1 + x, curve.y1 + y);
if (i === 0) {
if (includeMove) {
doMove.call(this, curve.x1 + x, curve.y1 + y);
} else {
drawLine.call(this, curve.x1 + x, curve.y1 + y);
}
}
drawCurve.call(
this,
Expand Down
Binary file modified test/reference/arc.pdf
Binary file not shown.
Binary file modified test/reference/bar_graph_with_text_and_lines.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-page-break-image.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-page-break.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-x-y.pdf
Binary file not shown.
Binary file modified test/reference/html-margin.pdf
Binary file not shown.
Binary file modified test/reference/html-multiple.pdf
Binary file not shown.
Binary file modified test/reference/html-x-y.pdf
Binary file not shown.
Binary file modified test/reference/paths.pdf
Binary file not shown.
Binary file modified test/reference/piechart.pdf
Binary file not shown.
Binary file modified test/reference/sierpinski.pdf
Binary file not shown.
Binary file modified test/reference/smiley.pdf
Binary file not shown.
Binary file modified test/reference/w3s_arc.pdf
Binary file not shown.
Binary file modified test/reference/w3s_closePath_v1.pdf
Binary file not shown.
Binary file modified test/reference/w3s_closePath_v2.pdf
Binary file not shown.
Binary file modified test/reference/warnsign.pdf
Binary file not shown.
32 changes: 32 additions & 0 deletions test/specs/context2d.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,38 @@ describe("Context2D: standard tests", () => {
ctx.arc(50, y, 20, 0, Math.PI);
ctx.closePath();
ctx.stroke();
y += pad + 40;

ctx.beginPath();
ctx.arc(50, y, 20, -Math.PI / 3, Math.PI, true);
ctx.closePath();
ctx.stroke();

ctx.lineWidth = 4;
ctx.strokeStyle = "red";
y = 80;
ctx.beginPath();
ctx.moveTo(150, y);
ctx.lineTo(150, y + 35);
ctx.arc(150, y, 65, 0, Math.PI * 0.8);
ctx.closePath();
ctx.fill();
ctx.stroke();

y = 160;
ctx.beginPath();
ctx.moveTo(150, y);
ctx.arc(150, y, 65, 0, Math.PI * 0.8);
ctx.fill();
ctx.stroke();

y = 280;
ctx.beginPath();
ctx.moveTo(150, y);
ctx.arc(150, y, 30, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();

comparePdf(doc.output(), "arc.pdf", "context2d");
});

Expand Down

0 comments on commit 8094918

Please sign in to comment.