Skip to content

Commit 1dd485a

Browse files
committed
feat: add enum with enum-with-union-type pattern
1 parent ad743a1 commit 1dd485a

12 files changed

Lines changed: 468 additions & 86 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.14.4
1+
2.15.0

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"require": {
2828
"php": ">=8.2",
2929
"ext-zlib": "*",
30-
"tecnickcom/tc-lib-color": "^2.12",
31-
"tecnickcom/tc-lib-pdf-encrypt": "^2.8"
30+
"tecnickcom/tc-lib-color": "^2.13",
31+
"tecnickcom/tc-lib-pdf-encrypt": "^2.9"
3232
},
3333
"minimum-stability": "stable",
3434
"prefer-stable": true,

resources/debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ Vcs-Browser: https://github.com/~#VENDOR#~/~#PROJECT#~
1212
Package: ~#PKGNAME#~
1313
Provides: php-~#PROJECT#~
1414
Architecture: all
15-
Depends: php (>= 8.2.0), php-zip, php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.12.5), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.8.4), ${misc:Depends}
15+
Depends: php (>= 8.2.0), php-zip, php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.13.0), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.9.0), ${misc:Depends}
1616
Description: PHP PDF Graph Library
1717
PHP library containing PDF graphic and geometric methods.

resources/rpm/rpm.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ BuildArch: noarch
1717
Requires: php(language) >= 8.2.0
1818
Requires: php-zlib
1919
Requires: php-composer(%{c_vendor}/tc-lib-color) < 3.0.0
20-
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.12.5
20+
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.13.0
2121
Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) < 3.0.0
22-
Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 2.8.4
22+
Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 2.9.0
2323

2424
Provides: php-composer(%{c_vendor}/%{gh_project}) = %{version}
2525
Provides: php-%{gh_project} = %{version}

src/BlendMode.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* BlendMode.php
7+
*
8+
* @since 2026-07-17
9+
* @category Library
10+
* @package PdfGraph
11+
* @author Nicola Asuni <info@tecnick.com>
12+
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
13+
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
14+
* @link https://github.com/tecnickcom/tc-lib-pdf-graph
15+
*
16+
* This file is part of tc-lib-pdf-graph software library.
17+
*/
18+
19+
namespace Com\Tecnick\Pdf\Graph;
20+
21+
/**
22+
* Com\Tecnick\Pdf\Graph\BlendMode
23+
*
24+
* Backed enum for the PDF blend modes (PDF 32000-1:2008 - 11.3.5). The backing
25+
* value of each case is the canonical /BM name validated by Gradient::getAlpha().
26+
*
27+
* @since 2026-07-17
28+
* @category Library
29+
* @package PdfGraph
30+
* @author Nicola Asuni <info@tecnick.com>
31+
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
32+
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
33+
* @link https://github.com/tecnickcom/tc-lib-pdf-graph
34+
*/
35+
enum BlendMode: string
36+
{
37+
case Normal = 'Normal';
38+
39+
case Multiply = 'Multiply';
40+
41+
case Screen = 'Screen';
42+
43+
case Overlay = 'Overlay';
44+
45+
case Darken = 'Darken';
46+
47+
case Lighten = 'Lighten';
48+
49+
case ColorDodge = 'ColorDodge';
50+
51+
case ColorBurn = 'ColorBurn';
52+
53+
case HardLight = 'HardLight';
54+
55+
case SoftLight = 'SoftLight';
56+
57+
case Difference = 'Difference';
58+
59+
case Exclusion = 'Exclusion';
60+
61+
case Hue = 'Hue';
62+
63+
case Saturation = 'Saturation';
64+
65+
case Color = 'Color';
66+
67+
case Luminosity = 'Luminosity';
68+
69+
/**
70+
* Resolve a loose blend mode value to the matching enum case.
71+
*
72+
* Accepts an enum instance (returned unchanged) or a string (with an
73+
* optional leading '/' stripped, as PDF /BM names carry). Unknown values
74+
* fall back to Normal, matching Gradient::getAlpha().
75+
*
76+
* @param string|self $value Blend mode name or enum case.
77+
*/
78+
public static function fromLoose(string|self $value): self
79+
{
80+
if ($value instanceof self) {
81+
return $value;
82+
}
83+
84+
if ($value !== '' && $value[0] === '/') {
85+
$value = \substr($value, 1);
86+
}
87+
88+
return self::tryFrom($value) ?? self::Normal;
89+
}
90+
}

src/Draw.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getLine(float $posx1, float $posy1, float $posx2, float $posy2,
7070
* @param float $posy2 Ordinate of control point 2.
7171
* @param float $posx3 Abscissa of end point.
7272
* @param float $posy3 Ordinate of end point.
73-
* @param string $mode Mode of rendering. @see getPathPaintOp()
73+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
7474
* @param StyleDataOpt $style Style.
7575
*
7676
* @return string PDF command
@@ -86,7 +86,7 @@ public function getCurve(
8686
float $posy2,
8787
float $posx3,
8888
float $posy3,
89-
string $mode = 'S',
89+
string|PathPaintOp $mode = 'S',
9090
array $style = [],
9191
): string {
9292
return (
@@ -104,7 +104,7 @@ public function getCurve(
104104
* @param float $posx0 Abscissa of start point.
105105
* @param float $posy0 Ordinate of start point.
106106
* @param array<array<float>> $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
107-
* @param string $mode Mode of rendering. @see getPathPaintOp()
107+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
108108
* @param StyleDataOpt $style Style.
109109
*
110110
* @return string PDF command
@@ -113,7 +113,7 @@ public function getPolycurve(
113113
float $posx0,
114114
float $posy0,
115115
array $segments,
116-
string $mode = 'S',
116+
string|PathPaintOp $mode = 'S',
117117
array $style = [],
118118
): string {
119119
$out = $this->getStyleCmd($style) . $this->getRawPoint($posx0, $posy0);
@@ -145,7 +145,7 @@ public function getPolycurve(
145145
* @param float $angle Angle oriented (anti-clockwise). Default value: 0.
146146
* @param float $angs Angle in degrees at which starting drawing.
147147
* @param float $angf Angle in degrees at which stop drawing.
148-
* @param string $mode Mode of rendering. @see getPathPaintOp()
148+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
149149
* @param StyleDataOpt $style Style.
150150
* @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse.
151151
*
@@ -161,7 +161,7 @@ public function getEllipse(
161161
float $angle = 0,
162162
float $angs = 0,
163163
float $angf = 360,
164-
string $mode = 'S',
164+
string|PathPaintOp $mode = 'S',
165165
array $style = [],
166166
int $ncv = 2,
167167
): string {
@@ -198,7 +198,7 @@ public function getEllipse(
198198
* @param float $rad Radius.
199199
* @param float $angs Angle in degrees at which starting drawing.
200200
* @param float $angf Angle in degrees at which stop drawing.
201-
* @param string $mode Mode of rendering. @see getPathPaintOp()
201+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
202202
* @param StyleDataOpt $style Style.
203203
* @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse.
204204
*
@@ -210,7 +210,7 @@ public function getCircle(
210210
float $rad,
211211
float $angs = 0,
212212
float $angf = 360,
213-
string $mode = 'S',
213+
string|PathPaintOp $mode = 'S',
214214
array $style = [],
215215
int $ncv = 2,
216216
): string {
@@ -225,7 +225,7 @@ public function getCircle(
225225
* @param float $rad Radius.
226226
* @param float $angs Angle in degrees at which starting drawing.
227227
* @param float $angf Angle in degrees at which stop drawing.
228-
* @param string $mode Mode of rendering. @see getPathPaintOp()
228+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
229229
* @param StyleDataOpt $style Style.
230230
* @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse.
231231
*
@@ -237,7 +237,7 @@ public function getPieSector(
237237
float $rad,
238238
float $angs = 0,
239239
float $angf = 360,
240-
string $mode = 'FD',
240+
string|PathPaintOp $mode = 'FD',
241241
array $style = [],
242242
int $ncv = 2,
243243
): string {
@@ -252,12 +252,12 @@ public function getPieSector(
252252
* Draws a basic polygon.
253253
*
254254
* @param array<float> $points Points - array containing 4 points for each segment: (x0, y0, x1, y1, x2, y2, ...)
255-
* @param string $mode Mode of rendering. @see getPathPaintOp()
255+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
256256
* @param StyleDataOpt $style Style.
257257
*
258258
* @return string PDF command
259259
*/
260-
public function getBasicPolygon(array $points, string $mode = 'S', array $style = []): string
260+
public function getBasicPolygon(array $points, string|PathPaintOp $mode = 'S', array $style = []): string
261261
{
262262
$nco = \count($points); // number of coordinates
263263
if ($nco < 4 || ($points[0] ?? null) === null || ($points[1] ?? null) === null) {
@@ -301,7 +301,7 @@ protected function getDefaultSegStyle(array $styles = []): string
301301
* Draws a polygon with a different style for each segment.
302302
*
303303
* @param array<float> $points Points - array with values (x0, y0, x1, y1,..., x(n-1), y(n-1))
304-
* @param string $mode Mode of rendering. @see getPathPaintOp()
304+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
305305
* @param array<StyleDataOpt> $styles Array of styles -
306306
* one style entry for each polygon segment and/or one global "all" entry.
307307
*
@@ -310,8 +310,12 @@ protected function getDefaultSegStyle(array $styles = []): string
310310
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
311311
* @SuppressWarnings("PHPMD.NPathComplexity")
312312
*/
313-
public function getPolygon(array $points, string $mode = 'S', array $styles = []): string
313+
public function getPolygon(array $points, string|PathPaintOp $mode = 'S', array $styles = []): string
314314
{
315+
if ($mode instanceof PathPaintOp) {
316+
$mode = $mode->value;
317+
}
318+
315319
$nco = \count($points); // number of points
316320
if ($nco < 6 || ($points[0] ?? null) === null || ($points[1] ?? null) === null) {
317321
return ''; // we need at least 3 points
@@ -406,10 +410,10 @@ public function getPolygon(array $points, string $mode = 'S', array $styles = []
406410
* @param float $radius Radius of inscribed circle.
407411
* @param int $sides Number of sides.
408412
* @param float $angle Angle of the orientation (anti-clockwise).
409-
* @param string $mode Mode of rendering. @see getPathPaintOp()
413+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
410414
* @param array<StyleDataOpt> $styles Array of styles -
411415
* one style entry for each polygon segment and/or one global "all" entry.
412-
* @param string $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp()
416+
* @param string|PathPaintOp $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp()
413417
* @param StyleDataOpt $cirstyle Style of inscribed circle.
414418
*
415419
* @return string PDF command
@@ -420,9 +424,9 @@ public function getRegularPolygon(
420424
float $radius,
421425
int $sides,
422426
float $angle = 0,
423-
string $mode = 'S',
427+
string|PathPaintOp $mode = 'S',
424428
array $styles = [],
425-
string $cirmode = '',
429+
string|PathPaintOp $cirmode = '',
426430
array $cirstyle = [],
427431
): string {
428432
if ($sides < 3) { // triangle is the minimum polygon
@@ -453,10 +457,10 @@ public function getRegularPolygon(
453457
* @param int $nvert Number of vertices.
454458
* @param int $ngaps Number of gaps (if ($ngaps % $nvert = 1) then is a regular polygon).
455459
* @param float $angle Angle oriented (anti-clockwise).
456-
* @param string $mode Mode of rendering. @see getPathPaintOp()
460+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
457461
* @param array<StyleDataOpt> $styles Array of styles -
458462
* one style entry for each polygon segment and/or one global "all" entry.
459-
* @param string $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp()
463+
* @param string|PathPaintOp $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp()
460464
* @param StyleDataOpt $cirstyle Style of inscribed circle.
461465
*
462466
* @return string PDF command
@@ -470,9 +474,9 @@ public function getStarPolygon(
470474
int $nvert,
471475
int $ngaps,
472476
float $angle = 0,
473-
string $mode = 'S',
477+
string|PathPaintOp $mode = 'S',
474478
array $styles = [],
475-
string $cirmode = '',
479+
string|PathPaintOp $cirmode = '',
476480
array $cirstyle = [],
477481
): string {
478482
if ($nvert < 2) {
@@ -509,7 +513,7 @@ public function getStarPolygon(
509513
* @param float $posy Ordinate of upper-left corner.
510514
* @param float $width Width.
511515
* @param float $height Height.
512-
* @param string $mode Mode of rendering. @see getPathPaintOp()
516+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
513517
* @param array<StyleDataOpt> $styles Array of styles -
514518
* one style entry for each side (T,R,B,L) and/or one global "all" entry.
515519
*
@@ -520,7 +524,7 @@ public function getRect(
520524
float $posy,
521525
float $width,
522526
float $height,
523-
string $mode = 'S',
527+
string|PathPaintOp $mode = 'S',
524528
array $styles = [],
525529
): string {
526530
$points = [
@@ -550,7 +554,7 @@ public function getRect(
550554
* @param string $corner Round corners to draw: 0 (square i-corner) or 1 (rounded i-corner) in i-position.
551555
* Positions are int the following order: top right, bottom right, bottom left and
552556
* top left.
553-
* @param string $mode Mode of rendering. @see getPathPaintOp()
557+
* @param string|PathPaintOp $mode Mode of rendering. @see getPathPaintOp()
554558
* @param StyleDataOpt $style Style.
555559
*
556560
* @return string PDF command
@@ -566,7 +570,7 @@ public function getRoundedRect(
566570
float $hrad,
567571
float $vrad,
568572
string $corner = '1111',
569-
string $mode = 'S',
573+
string|PathPaintOp $mode = 'S',
570574
array $style = [],
571575
): string {
572576
if ($corner === '0000' || $hrad === 0.0 && $vrad === 0.0) {

0 commit comments

Comments
 (0)