Skip to content

Commit 8094918

Browse files
authored
Fix closePath by using PDF command (#3304)
1 parent 99927b0 commit 8094918

18 files changed

+60
-28
lines changed

src/modules/context2d.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -823,21 +823,10 @@ import {
823823
typeof this.path[i + 1].x === "number"
824824
) {
825825
pathBegin = new Point(this.path[i + 1].x, this.path[i + 1].y);
826-
this.path.push({
827-
type: "lt",
828-
x: pathBegin.x,
829-
y: pathBegin.y
830-
});
831826
break;
832827
}
833828
}
834829
}
835-
if (
836-
typeof this.path[i + 2] === "object" &&
837-
typeof this.path[i + 2].x === "number"
838-
) {
839-
this.path.push(JSON.parse(JSON.stringify(this.path[i + 2])));
840-
}
841830
this.path.push({
842831
type: "close"
843832
});
@@ -995,9 +984,6 @@ import {
995984
}
996985
counterclockwise = Boolean(counterclockwise);
997986

998-
var x_start = x + radius * Math.cos(startAngle);
999-
var y_start = y + radius * Math.sin(startAngle);
1000-
1001987
if (!this.ctx.transform.isIdentity) {
1002988
var xpt = this.ctx.transform.applyToPoint(new Point(x, y));
1003989
x = xpt.x;
@@ -1014,7 +1000,6 @@ import {
10141000
startAngle = 0;
10151001
endAngle = 2 * Math.PI;
10161002
}
1017-
this.lineTo(x_start, y_start);
10181003

10191004
this.path.push({
10201005
type: "arc",
@@ -2096,6 +2081,7 @@ import {
20962081
style = null;
20972082
}
20982083

2084+
var began = false;
20992085
for (var k = 0; k < moves.length; k++) {
21002086
if (moves[k].arc) {
21012087
var arcs = moves[k].abs;
@@ -2113,21 +2099,22 @@ import {
21132099
arc.endAngle,
21142100
arc.counterclockwise,
21152101
undefined,
2116-
isClip
2102+
isClip,
2103+
!began
21172104
);
21182105
} else {
21192106
drawLine.call(this, arc.x, arc.y);
21202107
}
2108+
began = true;
21212109
}
2122-
putStyle.call(this, style);
2110+
} else if (moves[k].close === true) {
21232111
this.pdf.internal.out("h");
2124-
}
2125-
if (!moves[k].arc) {
2126-
if (moves[k].close !== true && moves[k].begin !== true) {
2127-
var x = moves[k].start.x;
2128-
var y = moves[k].start.y;
2129-
drawLines.call(this, moves[k].deltas, x, y);
2130-
}
2112+
began = false;
2113+
} else if (moves[k].begin !== true) {
2114+
var x = moves[k].start.x;
2115+
var y = moves[k].start.y;
2116+
drawLines.call(this, moves[k].deltas, x, y);
2117+
began = true;
21312118
}
21322119
}
21332120

@@ -2205,15 +2192,28 @@ import {
22052192
* @param style
22062193
* @param isClip
22072194
*/
2208-
var drawArc = function(x, y, r, a1, a2, counterclockwise, style, isClip) {
2195+
var drawArc = function(
2196+
x,
2197+
y,
2198+
r,
2199+
a1,
2200+
a2,
2201+
counterclockwise,
2202+
style,
2203+
isClip,
2204+
includeMove
2205+
) {
22092206
// http://hansmuller-flex.blogspot.com/2011/10/more-about-approximating-circular-arcs.html
2210-
var includeMove = true;
22112207
var curves = createArc.call(this, r, a1, a2, counterclockwise);
22122208

22132209
for (var i = 0; i < curves.length; i++) {
22142210
var curve = curves[i];
2215-
if (includeMove && i === 0) {
2216-
doMove.call(this, curve.x1 + x, curve.y1 + y);
2211+
if (i === 0) {
2212+
if (includeMove) {
2213+
doMove.call(this, curve.x1 + x, curve.y1 + y);
2214+
} else {
2215+
drawLine.call(this, curve.x1 + x, curve.y1 + y);
2216+
}
22172217
}
22182218
drawCurve.call(
22192219
this,

test/reference/arc.pdf

906 Bytes
Binary file not shown.
-1.58 KB
Binary file not shown.
-116 Bytes
Binary file not shown.
-174 Bytes
Binary file not shown.

test/reference/html-margin-x-y.pdf

-72 Bytes
Binary file not shown.

test/reference/html-margin.pdf

-72 Bytes
Binary file not shown.

test/reference/html-multiple.pdf

-216 Bytes
Binary file not shown.

test/reference/html-x-y.pdf

-72 Bytes
Binary file not shown.

test/reference/paths.pdf

-4 Bytes
Binary file not shown.

test/reference/piechart.pdf

-109 Bytes
Binary file not shown.

test/reference/sierpinski.pdf

-22.4 KB
Binary file not shown.

test/reference/smiley.pdf

-56 Bytes
Binary file not shown.

test/reference/w3s_arc.pdf

-4 Bytes
Binary file not shown.

test/reference/w3s_closePath_v1.pdf

-52 Bytes
Binary file not shown.

test/reference/w3s_closePath_v2.pdf

-104 Bytes
Binary file not shown.

test/reference/warnsign.pdf

-78 Bytes
Binary file not shown.

test/specs/context2d.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,38 @@ describe("Context2D: standard tests", () => {
375375
ctx.arc(50, y, 20, 0, Math.PI);
376376
ctx.closePath();
377377
ctx.stroke();
378+
y += pad + 40;
379+
380+
ctx.beginPath();
381+
ctx.arc(50, y, 20, -Math.PI / 3, Math.PI, true);
382+
ctx.closePath();
383+
ctx.stroke();
384+
385+
ctx.lineWidth = 4;
386+
ctx.strokeStyle = "red";
387+
y = 80;
388+
ctx.beginPath();
389+
ctx.moveTo(150, y);
390+
ctx.lineTo(150, y + 35);
391+
ctx.arc(150, y, 65, 0, Math.PI * 0.8);
392+
ctx.closePath();
393+
ctx.fill();
394+
ctx.stroke();
395+
396+
y = 160;
397+
ctx.beginPath();
398+
ctx.moveTo(150, y);
399+
ctx.arc(150, y, 65, 0, Math.PI * 0.8);
400+
ctx.fill();
401+
ctx.stroke();
402+
403+
y = 280;
404+
ctx.beginPath();
405+
ctx.moveTo(150, y);
406+
ctx.arc(150, y, 30, 0, 2 * Math.PI);
407+
ctx.fill();
408+
ctx.stroke();
409+
378410
comparePdf(doc.output(), "arc.pdf", "context2d");
379411
});
380412

0 commit comments

Comments
 (0)