Skip to content

Commit 7926073

Browse files
committed
Conditionally close a path when adding stroke
Fill/stroke operations are a "finalizing" instruction for paths in the PDF spec. As part of the stroke operation the path can be left open or closed. This was not accounted for in the Surface interface. Additionally, CPDF was also always closing the path on a fillStroke operation. PDFLib does support both options, but only the open-path method was used. This change updates the Surface interface with an optional argument for the stroke and fillStroke methods that indicates whether or not to close the path. The default is to leave the path open.
1 parent e02e6f7 commit 7926073

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

src/Svg/Surface/CPdf.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,19 +4174,19 @@ function rect($x1, $y1, $width, $height)
41744174
$this->addContent(sprintf("\n%.3F %.3F %.3F %.3F re", $x1, $y1, $width, $height));
41754175
}
41764176

4177-
function stroke()
4177+
function stroke(bool $close = false)
41784178
{
4179-
$this->addContent("\nS");
4179+
$this->addContent("\n" . ($close ? "s" : "S"));
41804180
}
41814181

41824182
function fill()
41834183
{
41844184
$this->addContent("\nf" . ($this->fillRule === "evenodd" ? "*" : ""));
41854185
}
41864186

4187-
function fillStroke()
4187+
function fillStroke(bool $close = false)
41884188
{
4189-
$this->addContent("\nb" . ($this->fillRule === "evenodd" ? "*" : ""));
4189+
$this->addContent("\n" . ($close ? "b" : "B") . ($this->fillRule === "evenodd" ? "*" : ""));
41904190
}
41914191

41924192
/**

src/Svg/Surface/SurfaceCpdf.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ public function closePath()
122122
$this->canvas->closePath();
123123
}
124124

125-
public function fillStroke()
125+
public function fillStroke(bool $close = false)
126126
{
127127
if (self::DEBUG) echo __FUNCTION__ . "\n";
128-
$this->canvas->fillStroke();
128+
$this->canvas->fillStroke($close);
129129
}
130130

131131
public function clip()
@@ -342,10 +342,10 @@ public function strokeRect($x, $y, $w, $h)
342342
$this->stroke();
343343
}
344344

345-
public function stroke()
345+
public function stroke(bool $close = false)
346346
{
347347
if (self::DEBUG) echo __FUNCTION__ . "\n";
348-
$this->canvas->stroke();
348+
$this->canvas->stroke($close);
349349
}
350350

351351
public function endPath()

src/Svg/Surface/SurfaceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public function closePath();
3737

3838
public function fill();
3939

40-
public function stroke();
40+
public function stroke(bool $close = false);
4141

4242
public function endPath();
4343

44-
public function fillStroke();
44+
public function fillStroke(bool $close = false);
4545

4646
public function clip();
4747

src/Svg/Surface/SurfacePDFLib.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ public function closePath()
120120
$this->canvas->closepath();
121121
}
122122

123-
public function fillStroke()
123+
public function fillStroke(bool $close = false)
124124
{
125125
if (self::DEBUG) echo __FUNCTION__ . "\n";
126-
$this->canvas->fill_stroke();
126+
if ($close) {
127+
$this->canvas->closepath_fill_stroke();
128+
} else {
129+
$this->canvas->fill_stroke();
130+
}
127131
}
128132

129133
public function clip()
@@ -281,10 +285,14 @@ public function strokeRect($x, $y, $w, $h)
281285
$this->stroke();
282286
}
283287

284-
public function stroke()
288+
public function stroke(bool $close = false)
285289
{
286290
if (self::DEBUG) echo __FUNCTION__ . "\n";
287-
$this->canvas->stroke();
291+
if ($close) {
292+
$this->canvas->closepath_stroke();
293+
} else {
294+
$this->canvas->stroke();
295+
}
288296
}
289297

290298
public function endPath()

src/Svg/Tag/Shape.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function after()
3838

3939
if ($fill) {
4040
if ($stroke) {
41-
$surface->fillStroke();
41+
$surface->fillStroke(false);
4242
} else {
4343
// if (is_string($style->fill)) {
4444
// /** @var LinearGradient|RadialGradient $gradient */
@@ -51,7 +51,7 @@ protected function after()
5151
}
5252
}
5353
elseif ($stroke) {
54-
$surface->stroke();
54+
$surface->stroke(false);
5555
}
5656
else {
5757
$surface->endPath();

0 commit comments

Comments
 (0)