From 6c2145785bdef07082fc15fef81df53223db9b38 Mon Sep 17 00:00:00 2001 From: Nicola Asuni Date: Sun, 12 Nov 2023 20:47:08 +0000 Subject: [PATCH] Php8 (#3) * Require PHP 8.0+ * Set max phpstan level and fix errors --- .github/workflows/check.yml | 8 +- .gitignore | 4 + README.md | 6 +- VERSION | 2 +- composer.json | 10 +- phpstan.neon | 2 +- resources/debian/control | 2 +- resources/rpm/rpm.spec | 6 +- src/Base.php | 362 +++++++++----- src/Draw.php | 750 +++++++++++++++++----------- src/Exception.php | 28 +- src/Gradient.php | 942 ++++++++++++++++++++++++------------ src/Raw.php | 155 +++--- src/Style.php | 500 ++++++++++++------- src/Transform.php | 145 +++--- test/BaseTest.php | 171 ++++--- test/DrawTest.php | 247 ++++++---- test/GradientTest.php | 339 +++++++------ test/RawTest.php | 104 ++-- test/StyleTest.php | 433 ++++++++++------- test/TestUtil.php | 55 +-- test/TransformTest.php | 219 ++++----- 22 files changed, 2725 insertions(+), 1765 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index aa68ef4..defda12 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -23,16 +23,10 @@ jobs: strategy: fail-fast: false matrix: - php-version: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2"] + php-version: ["8.0", "8.1", "8.2"] experimental: [false] os: [ubuntu-latest] coverage-extension: [pcov] - include: - #- { php-version: '5.3', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - #- { php-version: '5.4', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - - { php-version: '5.5', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - - { php-version: '5.6', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } - - { php-version: '7.1', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' } steps: - uses: actions/checkout@v4 - name: Use php ${{ matrix.php-version }} diff --git a/.gitignore b/.gitignore index 291bb86..4dd7d5e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,14 @@ **/.vagrant **/auth.json **/nbproject +**/temp.php +**/test.php .phpdoc .phpunit.cache .phpunit.result.cache composer.lock +ecs.php phpunit.xml +rector.php target vendor diff --git a/README.md b/README.md index 117204b..0b6c64d 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ All artifacts are generated in the target directory. Examples are located in the `example` directory. -Start a development server (requires PHP 5.4) using the command: +Start a development server (requires PHP 8.0+) using the command: ``` make server @@ -78,7 +78,7 @@ Create a composer.json in your projects root-directory: ```json { "require": { - "tecnickcom/tc-lib-pdf-graph": "^1.5" + "tecnickcom/tc-lib-pdf-graph": "^2.0" } } ``` @@ -86,7 +86,7 @@ Create a composer.json in your projects root-directory: Or add to an existing project with: ```bash -composer require tecnickcom/tc-lib-pdf-graph ^1.5 +composer require tecnickcom/tc-lib-pdf-graph ^2.0 ``` diff --git a/VERSION b/VERSION index 52a89d4..157e54f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.7.23 +2.0.6 diff --git a/composer.json b/composer.json index 9b93e92..3d042fd 100644 --- a/composer.json +++ b/composer.json @@ -19,16 +19,16 @@ } ], "require": { - "php": ">=5.4", + "php": ">=8.0", "ext-zlib": "*", - "tecnickcom/tc-lib-color": "^1.14", - "tecnickcom/tc-lib-pdf-encrypt": "^1.6" + "tecnickcom/tc-lib-color": "^2.0", + "tecnickcom/tc-lib-pdf-encrypt": "^2.0" }, "require-dev": { "pdepend/pdepend": "2.13.0", "phpmd/phpmd": "2.13.0", - "phpunit/phpunit": "10.1.2 || 9.6.7 || 8.5.31 || 7.5.20 || 6.5.14 || 5.7.27 || 4.8.36", - "squizlabs/php_codesniffer": "3.7.2 || 2.9.2" + "phpunit/phpunit": "10.1.2 || 9.6.13", + "squizlabs/php_codesniffer": "3.7.2" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index c42b364..bf592ce 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 5 + level: max paths: - src - test diff --git a/resources/debian/control b/resources/debian/control index 8069075..ec247e4 100644 --- a/resources/debian/control +++ b/resources/debian/control @@ -10,6 +10,6 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git Package: ~#PKGNAME#~ Provides: php-~#PROJECT#~ Architecture: all -Depends: php (>= 5.4.0), php-zip, php-tecnickcom-tc-lib-color (<< 2.0.0), php-tecnickcom-tc-lib-color (>= 1.14.39), php-tecnickcom-tc-lib-pdf-encrypt (<< 2.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 1.6.35), ${misc:Depends} +Depends: php (>= 8.0.0), php-zip, php-tecnickcom-tc-lib-color (<< 2.0.0), php-tecnickcom-tc-lib-color (>= 2.0.3), php-tecnickcom-tc-lib-pdf-encrypt (<< 2.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.0.6), ${misc:Depends} Description: PHP PDF Graph Library PHP library containing PDF graphic and geometric methods. diff --git a/resources/rpm/rpm.spec b/resources/rpm/rpm.spec index 12360b7..b054688 100644 --- a/resources/rpm/rpm.spec +++ b/resources/rpm/rpm.spec @@ -16,12 +16,12 @@ URL: https://github.com/%{gh_owner}/%{gh_project} BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n) BuildArch: noarch -Requires: php(language) >= 5.4.0 +Requires: php(language) >= 8.0.0 Requires: php-zlib Requires: php-composer(%{c_vendor}/tc-lib-color) < 2.0.0 -Requires: php-composer(%{c_vendor}/tc-lib-color) >= 1.14.39 +Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.0.3 Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) < 2.0.0 -Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 1.6.35 +Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 2.0.6 Provides: php-composer(%{c_vendor}/%{gh_project}) = %{version} Provides: php-%{gh_project} = %{version} diff --git a/src/Base.php b/src/Base.php index e7a7077..6dac97f 100644 --- a/src/Base.php +++ b/src/Base.php @@ -3,13 +3,13 @@ /** * Base.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ @@ -18,18 +18,19 @@ use Com\Tecnick\Color\Pdf as PdfColor; use Com\Tecnick\Pdf\Encrypt\Encrypt; -use Com\Tecnick\Pdf\Graph\Exception as GraphException; /** * Com\Tecnick\Pdf\Graph\Base * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ abstract class Base { @@ -39,70 +40,82 @@ abstract class Base * * @var float */ - const MPI = 3.14159265358979323846264338327950288419716939937510; + public const MPI = 3.14159265358979323846264338327950288419716939937510; /** * Current PDF object number - * - * @var int */ - protected $pon; + protected int $pon = 0; /** * Current page height - * - * @var float */ - protected $pageh = 0; + protected float $pageh = 0; /** * Current page width - * - * @var float */ - protected $pagew = 0; + protected float $pagew = 0; /** * Unit of measure conversion ratio - * - * @var float - */ - protected $kunit = 1.0; - - /** - * Color object - * - * @var PdfColor */ - protected $col; + protected float $kunit = 1.0; /** - * Encrypt object - * - * @var Encrypt + * Stack index. */ - protected $enc; + protected int $styleid = -1; /** - * True if we are in PDF/A mode. + * Stack containing style data. * - * @var bool + * @var array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * }> */ - protected $pdfa = false; + protected array $style = []; /** - * Enable stream compression. + * Array of transparency objects and parameters. * - * @var bool + * @var array, + * }> */ - protected $compress = true; + protected array $extgstates = []; /** - * Array of transparency objects and parameters. + * Array of gradients * - * @var array + * @var array, + * 'colspace': string, + * 'coords': array, + * 'id': int, + * 'pattern': int, + * 'stream': string, + * 'transparency': bool, + * 'type': int, + * }> */ - protected $extgstates = array(); + protected array $gradients = []; /** * Initialize @@ -110,74 +123,126 @@ abstract class Base * @param float $kunit Unit of measure conversion ratio. * @param float $pagew Page width. * @param float $pageh Page height. - * @param PdfColor $color Color object. + * @param PdfColor $pdfColor Color object. * @param bool $pdfa True if we are in PDF/A mode. * @param bool $compress Set to false to disable stream compression. */ public function __construct( - $kunit, - $pagew, - $pageh, - PdfColor $color, - Encrypt $enc, - $pdfa = false, - $compress = true + float $kunit, + float $pagew, + float $pageh, + /** + * Color object + */ + protected PdfColor $pdfColor, + /** + * Encrypt object + */ + protected Encrypt $encrypt, + protected bool $pdfa = false, + protected bool $compress = true ) { $this->setKUnit($kunit); $this->setPageWidth($pagew); $this->setPageHeight($pageh); - $this->col = $color; - $this->enc = $enc; - $this->pdfa = (bool) $pdfa; - $this->compress = (bool) $compress; - $this->init(); + $this->initStyle(); } /** - * Returns current PDF object number + * Initialize default style + */ + public function initStyle(): void + { + $this->style[++$this->styleid] = $this->getDefaultStyle(); + } + + /** + * Returns the default style. + * + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style parameters to merge to the default ones. * - * @return int + * @return array{ + * 'lineWidth': float, + * 'lineCap': string, + * 'lineJoin': string, + * 'miterLimit': float, + * 'dashArray': array, + * 'dashPhase': float, + * 'lineColor': string, + * 'fillColor': string, + * } */ - public function getObjectNumber() + public function getDefaultStyle(array $style = []): array { - return $this->pon; + $def = [ + // line thickness in user units + 'lineWidth' => (1.0 / $this->kunit), + // shape of the endpoints for any open path that is stroked + 'lineCap' => 'butt', + // shape of joints between connected segments of a stroked path + 'lineJoin' => 'miter', + // maximum length of mitered line joins for stroked paths + 'miterLimit' => (10.0 / $this->kunit), + // lengths of alternating dashes and gaps + 'dashArray' => [], + // distance at which to start the dash + 'dashPhase' => 0, + // line (drawing) color + 'lineColor' => 'black', + // background (filling) color + 'fillColor' => 'black', + ]; + + return array_merge($def, $style); } /** - * Initialize objects + * Returns current PDF object number */ - abstract public function init(); + public function getObjectNumber(): int + { + return $this->pon; + } /** * Set page height * - * @param float $pageh Page height + * @param float $pageh Page height */ - public function setPageHeight($pageh) + public function setPageHeight(float $pageh): static { - $this->pageh = (float) $pageh; + $this->pageh = $pageh; return $this; } /** * Set page width * - * @param float $pagew Page width + * @param float $pagew Page width */ - public function setPageWidth($pagew) + public function setPageWidth(float $pagew): static { - $this->pagew = (float) $pagew; + $this->pagew = $pagew; return $this; } /** * Set unit of measure conversion ratio. * - * @param float $kunit Unit of measure conversion ratio. + * @param float $kunit Unit of measure conversion ratio. */ - public function setKUnit($kunit) + public function setKUnit(float $kunit): static { - $this->kunit = (float) $kunit; + $this->kunit = $kunit; return $this; } @@ -188,9 +253,9 @@ public function setKUnit($kunit) * * @return string PDF command */ - public function getOutExtGState($pon) + public function getOutExtGState(int $pon): string { - $this->pon = (int) $pon; + $this->pon = $pon; $out = ''; foreach ($this->extgstates as $idx => $ext) { $this->extgstates[$idx]['n'] = ++$this->pon; @@ -204,11 +269,14 @@ public function getOutExtGState($pon) } elseif ($val === false) { $val = 'false'; } + $out .= ' /' . $key . ' ' . $val; } + $out .= ' >>' . "\n" . 'endobj' . "\n"; } + return $out; } @@ -217,22 +285,24 @@ public function getOutExtGState($pon) * * @return string PDF command */ - public function getOutExtGStateResources() + public function getOutExtGStateResources(): string { - if ($this->pdfa || empty($this->extgstates)) { + if ($this->pdfa || $this->extgstates === []) { return ''; } + $out = ' /ExtGState <<'; foreach ($this->extgstates as $key => $ext) { - if (isset($ext['name'])) { + if (! empty($ext['name'])) { $out .= ' /' . $ext['name']; } else { $out .= ' /GS' . $key; } + $out .= ' ' . $ext['n'] . ' 0 R'; } - $out .= ' >>' . "\n"; - return $out; + + return $out . (' >>' . "\n"); } /** @@ -240,11 +310,12 @@ public function getOutExtGStateResources() * * @return string PDF command */ - public function getOutGradientResources() + public function getOutGradientResources(): string { - if ($this->pdfa || empty($this->gradients)) { + if ($this->pdfa || $this->gradients === []) { return ''; } + $grp = ''; $grs = ''; foreach ($this->gradients as $idx => $grad) { @@ -253,6 +324,7 @@ public function getOutGradientResources() // gradient shadings $grs .= ' /Sh' . $idx . ' ' . $grad['id'] . ' 0 R'; } + return ' /Pattern <<' . $grp . ' >>' . "\n" . ' /Shading <<' . $grs . ' >>' . "\n"; } @@ -260,16 +332,32 @@ public function getOutGradientResources() /** * Get the PDF output string for gradient colors and transparency * - * @param array $grad Array of gradient colors + * @param array{ + * 'antialias': bool, + * 'background': ?\Com\Tecnick\Color\Model, + * 'colors': array, + * 'colspace': string, + * 'coords': array, + * 'id': int, + * 'pattern': int, + * 'stream': string, + * 'transparency': bool, + * 'type': int, + * } $grad Array of gradient colors * @param string $type Type of output: 'color' or 'opacity' * * @return string PDF command * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - protected function getOutGradientCols($grad, $type) + protected function getOutGradientCols(array $grad, string $type): string { - if (($type == 'opacity') && !$grad['transparency']) { + if (($type == 'opacity') && ! $grad['transparency']) { return ''; } @@ -277,34 +365,44 @@ protected function getOutGradientCols($grad, $type) if (($grad['type'] == 2) || ($grad['type'] == 3)) { $num_cols = count($grad['colors']); $lastcols = ($num_cols - 1); - $funct = array(); // color and transparency objects - $bounds = array(); - $encode = array(); + $funct = []; // color and transparency objects + $bounds = []; + $encode = []; for ($idx = 1; $idx < $num_cols; ++$idx) { $col0 = $grad['colors'][($idx - 1)][$type]; $col1 = $grad['colors'][$idx][$type]; if ($type == 'color') { - $col0 = $this->col->getColorObject($grad['colors'][($idx - 1)][$type]); - $col1 = $this->col->getColorObject($grad['colors'][$idx][$type]); - if (($col0 === null) || ($col1 === null)) { + $col0 = $this->pdfColor->getColorObject($grad['colors'][($idx - 1)][$type]); + $col1 = $this->pdfColor->getColorObject($grad['colors'][$idx][$type]); + if (! $col0 instanceof \Com\Tecnick\Color\Model) { + continue; + } + + if (! $col1 instanceof \Com\Tecnick\Color\Model) { continue; } + $col0 = $col0->getComponentsString(); $col1 = $col1->getComponentsString(); } + $encode[] = '0 1'; - if ($idx < $lastcols) { + if ($idx < $lastcols && isset($grad['colors'][$idx]['offset'])) { $bounds[] = sprintf('%F ', $grad['colors'][$idx]['offset']); } + $out .= ++$this->pon . ' 0 obj' . "\n" . '<<' . ' /FunctionType 2' . ' /Domain [0 1]' . ' /C0 [' . $col0 . ']' - . ' /C1 [' . $col1 . ']' - . ' /N ' . $grad['colors'][$idx]['exponent'] - . ' >>' . "\n" + . ' /C1 [' . $col1 . ']'; + if (isset($grad['colors'][$idx]['exponent'])) { + $out .= ' /N ' . $grad['colors'][$idx]['exponent']; + } + + $out .= ' >>' . "\n" . 'endobj' . "\n"; $funct[] = $this->pon . ' 0 R'; } @@ -320,19 +418,34 @@ protected function getOutGradientCols($grad, $type) . 'endobj' . "\n"; } - $out .= $this->getOutPatternObj($grad, $this->pon); - return $out; + return $out . $this->getOutPatternObj($grad, $this->pon); } /** * Get the PDF output string for the pattern and shading object * - * @param array $grad Array of gradient colors - * @param int $objref Refrence object number + * @param array{ + * 'antialias': bool, + * 'background': ?\Com\Tecnick\Color\Model, + * 'colors': array, + * 'colspace': string, + * 'coords': array, + * 'id': int, + * 'pattern': int, + * 'stream': string, + * 'transparency': bool, + * 'type': int, + * } $grad Array of gradient colors + * @param int $objref Refrence object number * * @return string PDF command */ - protected function getOutPatternObj($grad, $objref) + protected function getOutPatternObj(array $grad, int $objref): string { // set shading object if ($grad['transparency']) { @@ -344,12 +457,14 @@ protected function getOutPatternObj($grad, $objref) . '<<' . ' /ShadingType ' . $grad['type'] . ' /ColorSpace /' . $grad['colspace']; - if (!empty($grad['background'])) { + if (! empty($grad['background'])) { $out .= ' /Background [' . $grad['background']->getComponentsString() . ']'; } - if (!empty($grad['antialias'])) { + + if ($grad['antialias']) { $out .= ' /AntiAlias true'; } + if ($grad['type'] == 2) { $out .= ' ' . sprintf( '/Coords [%F %F %F %F]', @@ -378,17 +493,15 @@ protected function getOutPatternObj($grad, $objref) . ' /Extend [true true]' . ' >>' . "\n"; } elseif ($grad['type'] == 6) { - $stream = $this->enc->encryptString($grad['stream'], $this->pon); - $out .= ' /BitsPerCoordinate 16' - . ' /BitsPerComponent 8' - . ' /Decode[0 1 0 1 0 1 0 1 0 1]' - . ' /BitsPerFlag 8' - . ' /Length ' . strlen($stream) + $stream = $this->encrypt->encryptString($grad['stream'], $this->pon); + $out .= ' /BitsPerCoordinate 16 /BitsPerComponent 8/Decode[0 1 0 1 0 1 0 1 0 1] /BitsPerFlag 8 /Length ' + . strlen($stream) . ' >>' . "\n" . ' stream' . "\n" . $stream . "\n" . 'endstream' . "\n"; } + $out .= 'endobj' . "\n"; // pattern object @@ -411,20 +524,19 @@ protected function getOutPatternObj($grad, $objref) * * @return string PDF command */ - public function getOutGradientShaders($pon) + public function getOutGradientShaders(int $pon): string { - $this->pon = (int) $pon; + $this->pon = $pon; - if ($this->pdfa || empty($this->gradients)) { + if ($this->pdfa || $this->gradients === []) { return ''; } $idt = count($this->gradients); // index for transparency gradients - $out = ''; foreach ($this->gradients as $idx => $grad) { $gcol = $this->getOutGradientCols($grad, 'color'); - if (!empty($gcol)) { + if ($gcol !== '') { $out .= $gcol; $this->gradients[$idx]['id'] = ($this->pon - 1); $this->gradients[$idx]['pattern'] = $this->pon; @@ -433,7 +545,7 @@ public function getOutGradientShaders($pon) $gopa = $this->getOutGradientCols($grad, 'opacity'); $idgs = ($idx + $idt); - if (!empty($gopa)) { + if ($gopa !== '') { $out .= $gopa; $this->gradients[$idgs]['id'] = ($this->pon - 1); $this->gradients[$idgs]['pattern'] = $this->pon; @@ -452,10 +564,14 @@ public function getOutGradientShaders($pon) . ' /FormType 1'; $stream = 'q /a0 gs /Pattern cs /p' . $idgs . ' scn 0 0 ' . $pwidth . ' ' . $pheight . ' re f Q'; if ($this->compress) { - $stream = gzcompress($stream); - $out .= ' /Filter /FlateDecode'; + $cmpstream = gzcompress($stream); + if ($cmpstream !== false) { + $stream = $cmpstream; + $out .= ' /Filter /FlateDecode'; + } } - $stream = $this->enc->encryptString($stream, $oid); + + $stream = $this->encrypt->encryptString($stream, $oid); $out .= ' /Length ' . strlen($stream) . ' /BBox [0 0 ' . $rect . ']' . ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>' @@ -488,7 +604,11 @@ public function getOutGradientShaders($pon) . ' /AIS false' . ' >>' . "\n" . 'endobj' . "\n"; - $this->extgstates[] = array('n' => $objext, 'name' => 'TGS' . $idx); + $this->extgstates[] = [ + 'n' => $objext, + 'name' => 'TGS' . $idx, + 'parms' => [], + ]; } } diff --git a/src/Draw.php b/src/Draw.php index 09e1351..2819a92 100644 --- a/src/Draw.php +++ b/src/Draw.php @@ -3,13 +3,13 @@ /** * Draw.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ @@ -21,13 +21,13 @@ /** * Com\Tecnick\Pdf\Graph\Draw * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ @@ -36,16 +36,30 @@ class Draw extends \Com\Tecnick\Pdf\Graph\Gradient /** * Draws a line between two points. * - * @param float $posx1 Abscissa of first point. - * @param float $posy1 Ordinate of first point. - * @param float $posx2 Abscissa of second point. - * @param float $posy2 Ordinate of second point. - * @param array $style Line style to apply. + * @param float $posx1 Abscissa of first point. + * @param float $posy1 Ordinate of first point. + * @param float $posx2 Abscissa of second point. + * @param float $posy2 Ordinate of second point. + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Line style to apply. * * @return string PDF command */ - public function getLine($posx1, $posy1, $posx2, $posy2, array $style = array()) - { + public function getLine( + float $posx1, + float $posy1, + float $posx2, + float $posy2, + array $style = [], + ): string { return $this->getStyleCmd($style) . $this->getRawPoint($posx1, $posy1) . $this->getRawLine($posx2, $posy2) @@ -65,24 +79,33 @@ public function getLine($posx1, $posy1, $posx2, $posy2, array $style = array()) * @param float $posx3 Abscissa of end point. * @param float $posy3 Ordinate of end point. * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. * * @return string PDF command * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function getCurve( - $posx0, - $posy0, - $posx1, - $posy1, - $posx2, - $posy2, - $posx3, - $posy3, - $mode = 'S', - array $style = array() - ) { + float $posx0, + float $posy0, + float $posx1, + float $posy1, + float $posx2, + float $posy2, + float $posx3, + float $posy3, + string $mode = 'S', + array $style = [], + ): string { return $this->getStyleCmd($style) . $this->getRawPoint($posx0, $posy0) . $this->getRawCurve($posx1, $posy1, $posx2, $posy2, $posx3, $posy3) @@ -93,60 +116,84 @@ public function getCurve( * Draws a poly-Bezier curve. * Each Bezier curve segment is a tangent to the line between the control points at either end of the curve. * - * @param float $posx0 Abscissa of start point. - * @param float $posy0 Ordinate of start point. - * @param array $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3). - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. + * @param float $posx0 Abscissa of start point. + * @param float $posy0 Ordinate of start point. + * @param array> $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3). + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. * * @return string PDF command */ - public function getPolycurve($posx0, $posy0, $segments, $mode = 'S', array $style = array()) - { + public function getPolycurve( + float $posx0, + float $posy0, + array $segments, + string $mode = 'S', + array $style = [], + ): string { $out = $this->getStyleCmd($style) . $this->getRawPoint($posx0, $posy0); - foreach ($segments as $seg) { - list($posx1, $posy1, $posx2, $posy2, $posx3, $posy3) = $seg; + foreach ($segments as $segment) { + [$posx1, $posy1, $posx2, $posy2, $posx3, $posy3] = $segment; $out .= $this->getRawCurve($posx1, $posy1, $posx2, $posy2, $posx3, $posy3); } - $out .= $this->getPathPaintOp($mode); - return $out; + + return $out . $this->getPathPaintOp($mode); } /** * Draws an ellipse. * An ellipse is formed from n Bezier curves. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $hrad Horizontal radius. - * @param float $vrad Vertical radius. - * @param float $angle Angle oriented (anti-clockwise). Default value: 0. - * @param float $angs Angle in degrees at which starting drawing. - * @param float $angf Angle in degrees at which stop drawing. - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. - * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $hrad Horizontal radius. + * @param float $vrad Vertical radius. + * @param float $angle Angle oriented (anti-clockwise). Default value: 0. + * @param float $angs Angle in degrees at which starting drawing. + * @param float $angf Angle in degrees at which stop drawing. + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. + * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. * * @return string PDF command * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function getEllipse( - $posx, - $posy, - $hrad, - $vrad = 0, - $angle = 0, - $angs = 0, - $angf = 360, - $mode = 'S', - array $style = array(), - $ncv = 2 - ) { + float $posx, + float $posy, + float $hrad, + float $vrad = 0, + float $angle = 0, + float $angs = 0, + float $angf = 360, + string $mode = 'S', + array $style = [], + int $ncv = 2 + ): string { if (empty($vrad)) { $vrad = $hrad; } + return $this->getStyleCmd($style) . $this->getRawEllipticalArc( $posx, @@ -169,54 +216,72 @@ public function getEllipse( * Draws a circle. * A circle is formed from n Bezier curves. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $rad Radius. - * @param float $angs Angle in degrees at which starting drawing. - * @param float $angf Angle in degrees at which stop drawing. - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. - * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $rad Radius. + * @param float $angs Angle in degrees at which starting drawing. + * @param float $angf Angle in degrees at which stop drawing. + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. + * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. * * @return string PDF command */ public function getCircle( - $posx, - $posy, - $rad, - $angs = 0, - $angf = 360, - $mode = 'S', - array $style = array(), - $ncv = 2 - ) { + float $posx, + float $posy, + float $rad, + float $angs = 0, + float $angf = 360, + string $mode = 'S', + array $style = [], + int $ncv = 2 + ): string { return $this->getEllipse($posx, $posy, $rad, $rad, 0, $angs, $angf, $mode, $style, $ncv); } /** * Draws a circle pie sector. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $rad Radius. - * @param float $angs Angle in degrees at which starting drawing. - * @param float $angf Angle in degrees at which stop drawing. - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. - * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $rad Radius. + * @param float $angs Angle in degrees at which starting drawing. + * @param float $angf Angle in degrees at which stop drawing. + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. + * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. * * @return string PDF command */ public function getPieSector( - $posx, - $posy, - $rad, - $angs = 0, - $angf = 360, - $mode = 'FD', - array $style = array(), - $ncv = 2 - ) { + float $posx, + float $posy, + float $rad, + float $angs = 0, + float $angf = 360, + string $mode = 'FD', + array $style = [], + int $ncv = 2 + ): string { return $this->getStyleCmd($style) . $this->getRawEllipticalArc( $posx, @@ -238,61 +303,94 @@ public function getPieSector( /** * Draws a basic polygon. * - * @param array $points Points - array containing 4 points for each segment: (x0, y0, x1, y1, x2, y2, ...) - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. + * @param array $points Points - array containing 4 points for each segment: (x0, y0, x1, y1, x2, y2, ...) + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. * * @return string PDF command */ - public function getBasicPolygon($points, $mode = 'S', array $style = array()) - { + public function getBasicPolygon( + array $points, + string $mode = 'S', + array $style = [], + ): string { $nco = count($points); // number of coordinates $out = $this->getStyleCmd($style) . $this->getRawPoint($points[0], $points[1]); for ($idx = 2; $idx < $nco; $idx += 2) { $out .= $this->getRawLine($points[$idx], $points[($idx + 1)]); } - $out .= $this->getPathPaintOp($mode); - return $out; + + return $out . $this->getPathPaintOp($mode); } /** * Returns the polygon default style command and initialize the first segment style if missing. * - * @param array $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. + * @param array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * }> $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. * * @return string PDF command */ - protected function getDefaultSegStyle(array &$styles = array()) + protected function getDefaultSegStyle(array $styles = []): string { $out = ''; - if (!empty($styles['all'])) { + if (! empty($styles['all'])) { $out .= $this->getStyleCmd($styles['all']); } + if (empty($styles[0])) { - $styles[0] = array(); + $styles[0] = []; } + return $out; } /** * Draws a polygon with a different style for each segment. * - * @param array $points Points - array with values (x0, y0, x1, y1,..., x(n-1), y(n-1)) - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. + * @param array $points Points - array with values (x0, y0, x1, y1,..., x(n-1), y(n-1)) + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * }> $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. * * @return string PDF command * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function getPolygon($points, $mode = 'S', array $styles = array()) + public function getPolygon(array $points, string $mode = 'S', array $styles = []): string { $nco = count($points); // number of points if ($nco < 6) { return ''; // we need at least 3 points } + $nseg = (int) ($nco / 2); // number of segments (including the closing one) $out = $this->getDefaultSegStyle($styles); @@ -321,9 +419,10 @@ public function getPolygon($points, $mode = 'S', array $styles = array()) // paint the outline for ($idx = 0; $idx < $nco; $idx += 2) { $segid = (int) ($idx / 2); - if (!isset($styles[$segid])) { - $styles[$segid] = array(); + if (! isset($styles[$segid])) { + $styles[$segid] = []; } + $out .= $this->getLine( $points[$idx], $points[($idx + 1)], @@ -339,95 +438,133 @@ public function getPolygon($points, $mode = 'S', array $styles = array()) /** * Draws a regular polygon. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $radius Radius of inscribed circle. - * @param int $sides Number of sides. - * @param float $angle Angle of the orientation (anti-clockwise). - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. - * @param string $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp() - * @param array $cirstyle Style of inscribed circle. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $radius Radius of inscribed circle. + * @param int $sides Number of sides. + * @param float $angle Angle of the orientation (anti-clockwise). + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * }> $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. + * @param string $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $cirstyle Style of inscribed circle. * * @return string PDF command */ public function getRegularPolygon( - $posx, - $posy, - $radius, - $sides, - $angle = 0, - $mode = 'S', - array $styles = array(), - $cirmode = '', - $cirstyle = array() - ) { + float $posx, + float $posy, + float $radius, + int $sides, + float $angle = 0, + string $mode = 'S', + array $styles = [], + string $cirmode = '', + array $cirstyle = [] + ): string { if ($sides < 3) { // triangle is the minimum polygon return ''; } + $out = ''; - if (!empty($cirmode)) { + if ($cirmode !== '') { $out .= $this->getCircle($posx, $posy, $radius, 0, 360, $cirmode, $cirstyle); } - $points = array(); + + $points = []; for ($idx = 0; $idx < $sides; ++$idx) { - $angrad = $this->degToRad((float) ($angle + ($idx * 360 / $sides))); + $angrad = $this->degToRad($angle + ($idx * 360 / $sides)); $points[] = ($posx + ($radius * sin($angrad))); $points[] = ($posy + ($radius * cos($angrad))); } - $out .= $this->getPolygon($points, $mode, $styles); - return $out; - } + return $out . $this->getPolygon($points, $mode, $styles); + } /** * Draws a star polygon. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $radius Radius of inscribed circle. - * @param int $nvert Number of vertices. - * @param int $ngaps Number of gaps (if ($ngaps % $nvert = 1) then is a regular polygon). - * @param float $angle Angle oriented (anti-clockwise). - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. - * @param string $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp() - * @param array $cirstyle Style of inscribed circle. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $radius Radius of inscribed circle. + * @param int $nvert Number of vertices. + * @param int $ngaps Number of gaps (if ($ngaps % $nvert = 1) then is a regular polygon). + * @param float $angle Angle oriented (anti-clockwise). + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * }> $styles Array of styles - one style entry for each polygon segment and/or one global "all" entry. + * @param string $cirmode Mode of rendering of the inscribed circle (if any). @see getPathPaintOp() + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $cirstyle Style of inscribed circle. * * @return string PDF command * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function getStarPolygon( - $posx, - $posy, - $radius, - $nvert, - $ngaps, - $angle = 0, - $mode = 'S', - array $styles = array(), - $cirmode = '', - $cirstyle = array() - ) { + float $posx, + float $posy, + float $radius, + int $nvert, + int $ngaps, + float $angle = 0, + string $mode = 'S', + array $styles = [], + string $cirmode = '', + array $cirstyle = [] + ): string { if ($nvert < 2) { return ''; } + $out = ''; - if (!empty($cirmode)) { + if ($cirmode !== '') { $out .= $this->getCircle($posx, $posy, $radius, 0, 360, $cirmode, $cirstyle); } - $points2 = array(); - $visited = array(); + $points2 = []; + $visited = []; for ($idx = 0; $idx < $nvert; ++$idx) { - $angrad = $this->degToRad((float) ($angle + ($idx * 360 / $nvert))); + $angrad = $this->degToRad($angle + ($idx * 360 / $nvert)); $points2[] = $posx + ($radius * sin($angrad)); $points2[] = $posy + ($radius * cos($angrad)); $visited[] = false; } - $points = array(); + $points = []; $idx = 0; do { $points[] = $points2[($idx * 2)]; @@ -435,38 +572,47 @@ public function getStarPolygon( $visited[$idx] = true; $idx += $ngaps; $idx %= $nvert; - } while (!$visited[$idx]); + } while (! $visited[$idx]); - $out .= $this->getPolygon($points, $mode, $styles); - return $out; + return $out . $this->getPolygon($points, $mode, $styles); } /** * Draws a rectangle with a different style for each segment. * - * @param float $posx Abscissa of upper-left corner. - * @param float $posy Ordinate of upper-left corner. - * @param float $width Width. - * @param float $height Height. - * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $styles Array of styles - one style entry for each side (T,R,B,L) and/or one global "all" entry. + * @param float $posx Abscissa of upper-left corner. + * @param float $posy Ordinate of upper-left corner. + * @param float $width Width. + * @param float $height Height. + * @param string $mode Mode of rendering. @see getPathPaintOp() + * @param array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * }> $styles Array of styles - one style entry for each side (T,R,B,L) and/or one global "all" entry. * @return string PDF command */ - public function getRect($posx, $posy, $width, $height, $mode = 'S', array $styles = array()) - { - $points = array( - $posx, - $posy, - $posx + $width, - $posy, - $posx + $width, - $posy + $height, - $posx, - $posy + $height, - $posx, - $posy - ); + public function getRect( + float $posx, + float $posy, + float $width, + float $height, + string $mode = 'S', + array $styles = [] + ): string { + $points = [ + $posx, $posy, + $posx + $width, $posy, + $posx + $width, $posy + $height, + $posx, $posy + $height, + $posx, $posy, + ]; return $this->getPolygon($points, $mode, $styles); } @@ -482,32 +628,45 @@ public function getRect($posx, $posy, $width, $height, $mode = 'S', array $style * @param string $corner Round corners to draw: 0 (square i-corner) or 1 (rounded i-corner) in i-position. * Positions are int the following order: top right, bottom right, bottom left and top left. * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. * * @return string PDF command + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getRoundedRect( - $posx, - $posy, - $width, - $height, - $hrad, - $vrad, - $corner = '1111', - $mode = 'S', - array $style = array() - ) { + float $posx, + float $posy, + float $width, + float $height, + float $hrad, + float $vrad, + string $corner = '1111', + string $mode = 'S', + array $style = [], + ): string { if (($corner === '0000') || (empty($hrad) && empty($vrad))) { // basic rectangle with straight corners return $this->getBasicRect($posx, $posy, $width, $height, $mode, $style); } $out = $this->getStyleCmd($style); - if ($corner[3]) { + if ($corner[3] !== '' && $corner[3] !== '0') { $out .= $this->getRawPoint(($posx + $hrad), $posy); } else { $out .= $this->getRawPoint($posx, $posy); } + $posxc = ($posx + $width - $hrad); $posyc = ($posy + $vrad); $out .= $this->getRawLine($posxc, $posy); @@ -515,7 +674,7 @@ public function getRoundedRect( $harc = ($hrad * $arc); $varc = ($vrad * $arc); - if ($corner[0]) { + if ($corner[0] !== '' && $corner[0] !== '0') { $out .= $this->getRawCurve( ($posxc + $harc), ($posyc - $vrad), @@ -527,11 +686,12 @@ public function getRoundedRect( } else { $out .= $this->getRawLine(($posx + $width), $posy); } + $posxc = ($posx + $width - $hrad); $posyc = ($posy + $height - $vrad); $out .= $this->getRawLine(($posx + $width), $posyc); - if ($corner[1]) { + if ($corner[1] !== '' && $corner[1] !== '0') { $out .= $this->getRawCurve( ($posxc + $hrad), ($posyc + $varc), @@ -543,11 +703,12 @@ public function getRoundedRect( } else { $out .= $this->getRawLine(($posx + $width), ($posy + $height)); } + $posxc = ($posx + $hrad); $posyc = ($posy + $height - $vrad); $out .= $this->getRawLine($posxc, ($posy + $height)); - if ($corner[2]) { + if ($corner[2] !== '' && $corner[2] !== '0') { $out .= $this->getRawCurve( ($posxc - $harc), ($posyc + $vrad), @@ -559,11 +720,12 @@ public function getRoundedRect( } else { $out .= $this->getRawLine($posx, ($posy + $height)); } + $posxc = ($posx + $hrad); $posyc = ($posy + $vrad); $out .= $this->getRawLine($posx, $posyc); - if ($corner[3]) { + if ($corner[3] !== '' && $corner[3] !== '0') { $out .= $this->getRawCurve( ($posxc - $hrad), ($posyc - $varc), @@ -576,58 +738,67 @@ public function getRoundedRect( $out .= $this->getRawLine($posx, $posy); } - $out .= $this->getPathPaintOp($mode); - - return $out; + return $out . $this->getPathPaintOp($mode); } /** * Draws an arrow. * - * @param float $posx0 Abscissa of first point. - * @param float $posy0 Ordinate of first point. - * @param float $posx1 Abscissa of second point (head side). - * @param float $posy1 Ordinate of second point (head side) - * @param int $headmode Arrow head mode: - * 0 = draw only head arms; - * 1 = draw closed head without filling; - * 2 = closed and filled head; - * 3 = filled head. - * @param float $armsize Length of head arms. - * @param int $armangle Angle between an head arm and the arrow shaft. - * @param array $style Line style to apply. + * @param float $posx0 Abscissa of first point. + * @param float $posy0 Ordinate of first point. + * @param float $posx1 Abscissa of second point (head side). + * @param float $posy1 Ordinate of second point (head side) + * @param int $headmode Arrow head mode: + * 0 = draw only + * head arms; 1 = + * draw closed head + * without filling; + * 2 = closed and + * filled head; 3 = + * filled head. + * @param float $armsize Length of head arms. + * @param int $armangle Angle between an head arm and the arrow shaft. + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Line style to apply. * * @return string PDF command */ public function getArrow( - $posx0, - $posy0, - $posx1, - $posy1, - $headmode = 0, - $armsize = 5, - $armangle = 15, - array $style = array() - ) { + float $posx0, + float $posy0, + float $posx1, + float $posy1, + int $headmode = 0, + float $armsize = 5, + int $armangle = 15, + array $style = [], + ): string { // getting arrow direction angle; 0 deg angle is when both arms go along X axis; angle grows clockwise. $dir_angle = atan2(($posy0 - $posy1), ($posx0 - $posx1)); if ($dir_angle < 0) { $dir_angle += (2 * self::MPI); } + $armangle = $this->degToRad($armangle); $sx1 = $posx1; $sy1 = $posy1; if ($headmode > 0) { // calculate the stopping point for the arrow shaft $linewidth = 0; - if (isset($style['lineWidth'])) { - $linewidth = $style['lineWidth']; - } else { - $linewidth = $this->getLastStyleProperty('lineWidth', $linewidth); - } + $linewidth = $style['lineWidth'] ?? $this->getLastStyleProperty('lineWidth', $linewidth); + $sx1 = ($posx1 + (($armsize - $linewidth) * cos($dir_angle))); $sy1 = ($posy1 + (($armsize - $linewidth) * sin($dir_angle))); } + $out = $this->getStyleCmd($style); // main arrow line / shaft $out .= $this->getLine($posx0, $posy0, $sx1, $sy1); @@ -637,50 +808,66 @@ public function getArrow( // right arrowhead arm tip $hxr = ($posx1 + ($armsize * cos($dir_angle - $armangle))); $hyr = ($posy1 + ($armsize * sin($dir_angle - $armangle))); - $modemap = array(0 => 'S', 1 => 's', 2 => 'b', 3 => 'f', ); - $points = array($hxl, $hyl, $posx1, $posy1, $hxr, $hyr); - $out .= $this->getBasicPolygon($points, $modemap[$headmode], $style); - return $out; + $modemap = [ + 0 => 'S', + 1 => 's', + 2 => 'b', + 3 => 'f', + ]; + $points = [$hxl, $hyl, $posx1, $posy1, $hxr, $hyr]; + return $out . $this->getBasicPolygon($points, $modemap[$headmode], $style); } /** * Get a registration mark. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $rad Radius. - * @param boolean $double If true prints two concentric crop marks. - * @param string $color Color. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $rad Radius. + * @param bool $double If true prints two concentric crop marks. + * @param string $color Color. * * @return string PDF command */ - public function getRegistrationMark($posx, $posy, $rad, $double = false, $color = 'all') - { - $style = array( - 'lineWidth' => max((0.5 / $this->kunit), ($rad / 30)), - 'lineCap' => 'butt', - 'lineJoin' => 'miter', + public function getRegistrationMark( + float $posx, + float $posy, + float $rad, + bool $double = false, + string $color = 'all' + ): string { + $style = [ + 'lineWidth' => max((0.5 / $this->kunit), ($rad / 30)), + 'lineCap' => 'butt', + 'lineJoin' => 'miter', 'miterLimit' => (10.0 / $this->kunit), - 'dashArray' => array(), - 'dashPhase' => 0, - 'lineColor' => $color, - 'fillColor' => $color, - ); - $out = $this->col->getColorObject($color)->getPdfColor() + 'dashArray' => [], + 'dashPhase' => 0, + 'lineColor' => $color, + 'fillColor' => $color, + ]; + + $colobj = $this->pdfColor->getColorObject($color); + if (! $colobj instanceof \Com\Tecnick\Color\Model) { + throw new GraphException('Unknow color: ' . $color); + } + + $out = $colobj->getPdfColor() . $this->getPieSector($posx, $posy, $rad, 90, 180, 'F') . $this->getPieSector($posx, $posy, $rad, 270, 360, 'F') - . $this->getCircle($posx, $posy, $rad, 0, 360, 'S', array(), 8); + . $this->getCircle($posx, $posy, $rad, 0, 360, 'S', [], 8); if ($double) { $radi = ($rad * 0.5); - $out .= $this->col->getColorObject($color)->invertColor()->getPdfColor() + $out .= $colobj->invertColor()->getPdfColor() . $this->getPieSector($posx, $posy, $radi, 90, 180, 'F') . $this->getPieSector($posx, $posy, $radi, 270, 360, 'F') - . $this->getCircle($posx, $posy, $radi, 0, 360, 'S', array(), 8) - . $this->col->getColorObject($color)->getPdfColor() + . $this->getCircle($posx, $posy, $radi, 0, 360, 'S', [], 8) + . $colobj->getPdfColor() . $this->getPieSector($posx, $posy, $radi, 0, 90, 'F') . $this->getPieSector($posx, $posy, $radi, 180, 270, 'F') - . $this->getCircle($posx, $posy, $radi, 0, 360, 'S', array(), 8); + . $this->getCircle($posx, $posy, $radi, 0, 360, 'S', [], 8); } + return $this->getStartTransform() . $this->getStyleCmd($style) . $out @@ -690,40 +877,45 @@ public function getRegistrationMark($posx, $posy, $rad, $double = false, $color /** * Get a CMYK registration mark. * - * @param float $posx Abscissa of center point. - * @param float $posy Ordinate of center point. - * @param float $rad Radius. + * @param float $posx Abscissa of center point. + * @param float $posy Ordinate of center point. + * @param float $rad Radius. * * @return string PDF command */ - public function getCmykRegistrationMark($posx, $posy, $rad) + public function getCmykRegistrationMark(float $posx, float $posy, float $rad): string { // internal radius $radi = ($rad * 0.6); // external radius $rade = ($rad * 1.3); // line style for external circle - $style = array( - 'lineWidth' => max((0.5 / $this->kunit), ($rad / 30)), - 'lineCap' => 'butt', - 'lineJoin' => 'miter', + $style = [ + 'lineWidth' => max((0.5 / $this->kunit), ($rad / 30)), + 'lineCap' => 'butt', + 'lineJoin' => 'miter', 'miterLimit' => (10.0 / $this->kunit), - 'dashArray' => array(), - 'dashPhase' => 0, - 'lineColor' => 'All', - 'fillColor' => '', - ); + 'dashArray' => [], + 'dashPhase' => 0, + 'lineColor' => 'All', + 'fillColor' => '', + ]; + return $this->getStartTransform() - . $this->col->getColorObject('Cyan')->getPdfColor() + . (($this->pdfColor->getColorObject('Cyan') instanceof \Com\Tecnick\Color\Model) + ? $this->pdfColor->getColorObject('Cyan')->getPdfColor() : '') . $this->getPieSector($posx, $posy, $radi, 270, 360, 'F') - . $this->col->getColorObject('Magenta')->getPdfColor() + . (($this->pdfColor->getColorObject('Magenta') instanceof \Com\Tecnick\Color\Model) + ? $this->pdfColor->getColorObject('Magenta')->getPdfColor() : '') . $this->getPieSector($posx, $posy, $radi, 0, 90, 'F') - . $this->col->getColorObject('Yellow')->getPdfColor() + . (($this->pdfColor->getColorObject('Yellow') instanceof \Com\Tecnick\Color\Model) + ? $this->pdfColor->getColorObject('Yellow')->getPdfColor() : '') . $this->getPieSector($posx, $posy, $radi, 90, 180, 'F') - . $this->col->getColorObject('Key')->getPdfColor() + . (($this->pdfColor->getColorObject('Key') instanceof \Com\Tecnick\Color\Model) + ? $this->pdfColor->getColorObject('Key')->getPdfColor() : '') . $this->getPieSector($posx, $posy, $radi, 180, 270, 'F') . $this->getStyleCmd($style) - . $this->getCircle($posx, $posy, $rad, 0, 360, 'S', array(), 8) + . $this->getCircle($posx, $posy, $rad, 0, 360, 'S', [], 8) . $this->getLine($posx, ($posy - $rade), $posx, ($posy - $radi)) . $this->getLine($posx, ($posy + $radi), $posx, ($posy + $rade)) . $this->getLine(($posx - $rade), $posy, ($posx - $radi), $posy) diff --git a/src/Exception.php b/src/Exception.php index 448b334..ace6484 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -3,13 +3,13 @@ /** * Exception.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ @@ -21,13 +21,13 @@ * * Custom Exception class * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class Exception extends \Exception { diff --git a/src/Gradient.php b/src/Gradient.php index c659e1a..86f12ac 100644 --- a/src/Gradient.php +++ b/src/Gradient.php @@ -3,13 +3,13 @@ /** * Gradient.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ @@ -21,29 +21,75 @@ /** * Com\Tecnick\Pdf\Graph\Gradient * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ abstract class Gradient extends \Com\Tecnick\Pdf\Graph\Raw { /** - * Array of gradients + * Blend mode. + * + * @var array + */ + protected const BLENDMODE = [ + 'Color' => true, + 'ColorBurn' => true, + 'ColorDodge' => true, + 'Darken' => true, + 'Difference' => true, + 'Exclusion' => true, + 'HardLight' => true, + 'Hue' => true, + 'Lighten' => true, + 'Luminosity' => true, + 'Multiply' => true, + 'Normal' => true, + 'Overlay' => true, + 'Saturation' => true, + 'Screen' => true, + 'SoftLight' => true, + ]; + + /** + * Blend mode. * - * @var array + * @var array */ - protected $gradients = array(); + protected const COLSPACE = [ + 'CMYK' => 'DeviceCMYK', + 'RGB' => 'DeviceRGB', + 'GRAY' => 'DeviceGray', + ]; /** * Returns the gradients array * - * @return array + * @return array, + * 'colspace': string, + * 'coords': array, + * 'id': int, + * 'pattern': int, + * 'stream': string, + * 'transparency': bool, + * 'type': int, + * }> */ - public function getGradientsArray() + public function getGradientsArray(): array { return $this->gradients; } @@ -56,12 +102,27 @@ public function getGradientsArray() * @param float $width Width. * @param float $height Height. * @param string $mode Mode of rendering. @see getPathPaintOp() - * @param array $style Style. + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style. * * @return string PDF command */ - public function getBasicRect($posx, $posy, $width, $height, $mode = 'S', array $style = array()) - { + public function getBasicRect( + float $posx, + float $posy, + float $width, + float $height, + string $mode = 'S', + array $style = [] + ): string { return $this->getStyleCmd($style) . $this->getRawRect($posx, $posy, $width, $height) . $this->getPathPaintOp($mode); @@ -70,43 +131,45 @@ public function getBasicRect($posx, $posy, $width, $height, $mode = 'S', array $ /** * Get a linear colour gradient command. * - * @param float $posx Abscissa of the top left corner of the rectangle. - * @param float $posy Ordinate of the top left corner of the rectangle. - * @param float $width Width of the rectangle. - * @param float $height Height of the rectangle. - * @param string $colorstart Starting color. - * @param string $colorend Ending color. - * @param array $coords Gradient vector (x1, y1, x2, y2). + * @param float $posx Abscissa of the top left corner of the rectangle. + * @param float $posy Ordinate of the top left corner of the rectangle. + * @param float $width Width of the rectangle. + * @param float $height Height of the rectangle. + * @param string $colorstart Starting color. + * @param string $colorend Ending color. + * @param array $coords Gradient vector (x1, y1, x2, y2). * * @return string PDF command */ public function getLinearGradient( - $posx, - $posy, - $width, - $height, - $colorstart, - $colorend, - $coords = array(0,0,1,0) - ) { + float $posx, + float $posy, + float $width, + float $height, + string $colorstart, + string $colorend, + array $coords = [0, 0, 1, 0] + ): string { return $this->getStartTransform() . $this->getClippingRect($posx, $posy, $width, $height) . $this->getGradientTransform($posx, $posy, $width, $height) . $this->getGradient( 2, $coords, - array( - array( + [ + [ 'color' => $colorstart, - 'offset' => 0, - 'exponent' => 1 - ), - array( + 'exponent' => 1.0, + 'offset' => 0.0, + 'opacity' => 1.0, + ], + [ 'color' => $colorend, - 'offset' => 1, - 'exponent' => 1 - ) - ), + 'exponent' => 1.0, + 'offset' => 1.0, + 'opacity' => 1.0, + ], + ], '', false ) @@ -116,46 +179,50 @@ public function getLinearGradient( /** * Get a radial colour gradient command. * - * @param float $posx Abscissa of the top left corner of the rectangle. - * @param float $posy Ordinate of the top left corner of the rectangle. - * @param float $width Width of the rectangle. - * @param float $height Height of the rectangle. - * @param string $colorstart Starting color. - * @param string $colorend Ending color. - * @param array $coords Array of the form (fx, fy, cx, cy, r) where - * (fx, fy) is the starting point of the gradient with $colorstart (be inside the circle), - * (cx, cy) is the center of the circle with $colorend, - * and r is the radius of the circle. + * @param float $posx Abscissa of the top left corner of the rectangle. + * @param float $posy Ordinate of the top left corner of the rectangle. + * @param float $width Width of the rectangle. + * @param float $height Height of the rectangle. + * @param string $colorstart Starting color. + * @param string $colorend Ending color. + * @param array $coords Array of the form (fx, fy, cx, cy, r) where + * (fx, fy) is the starting point of the + * gradient with $colorstart (be inside the + * circle), (cx, cy) is the center of the + * circle with $colorend, and r is the radius + * of the circle. * * @return string PDF command */ public function getRadialGradient( - $posx, - $posy, - $width, - $height, - $colorstart, - $colorend, - $coords = array(0.5,0.5,0.5,0.5,1) - ) { + float $posx, + float $posy, + float $width, + float $height, + string $colorstart, + string $colorend, + array $coords = [0.5, 0.5, 0.5, 0.5, 1] + ): string { return $this->getStartTransform() . $this->getClippingRect($posx, $posy, $width, $height) . $this->getGradientTransform($posx, $posy, $width, $height) . $this->getGradient( 3, $coords, - array( - array( + [ + [ 'color' => $colorstart, - 'offset' => 0, - 'exponent' => 1 - ), - array( + 'exponent' => 1.0, + 'offset' => 0.0, + 'opacity' => 1.0, + ], + [ 'color' => $colorend, - 'offset' => 1, - 'exponent' => 1 - ) - ), + 'exponent' => 1.0, + 'offset' => 1.0, + 'opacity' => 1.0, + ], + ], '', false ) @@ -169,10 +236,8 @@ public function getRadialGradient( * @param float $posy Ordinate of the top left corner of the rectangle. * @param float $width Width of the rectangle. * @param float $height Height of the rectangle. - * - * @return string */ - public function getClippingRect($posx, $posy, $width, $height) + public function getClippingRect(float $posx, float $posy, float $width, float $height): string { return sprintf( '%F %F %F %F re W n' . "\n", @@ -190,60 +255,77 @@ public function getClippingRect($posx, $posy, $width, $height) * @param float $posy Ordinate of the top left corner of the rectangle. * @param float $width Width of the rectangle. * @param float $height Height of the rectangle. - * - * @return string */ - public function getGradientTransform($posx, $posy, $width, $height) + public function getGradientTransform(float $posx, float $posy, float $width, float $height): string { - $ctm = array( + $ctm = [ ($width * $this->kunit), 0, 0, ($height * $this->kunit), ($posx * $this->kunit), - (($this->pageh - ($posy + $height)) * $this->kunit) - ); + (($this->pageh - ($posy + $height)) * $this->kunit), + ]; return $this->getTransformation($ctm); } /** * Get a color gradient command. * - * @param int $type Type of gradient (Not all types are currently supported): - * 1 = Function-based shading; - * 2 = Axial shading; - * 3 = Radial shading; - * 4 = Free-form Gouraud-shaded triangle mesh; - * 5 = Lattice-form Gouraud-shaded triangle mesh; - * 6 = Coons patch mesh; 7 Tensor-product patch mesh - * @param array $coords Array of coordinates. - * @param array $stops Array gradient color components: - * color = color; - * offset = (0 to 1) represents a location along the gradient vector; - * exponent = exponent of the exponential interpolation function (default = 1). - * @param string $bgcolor Background color - * @param bool $antialias Flag indicating whether to filter the shading function to prevent aliasing artifacts. + * @param int $type Type of gradient (Not all types are currently supported): + * 1 = Function-based shading; 2 = Axial shading; 3 = Radial + * shading; 4 = Free-form Gouraud-shaded triangle mesh; 5 = + * Lattice-form Gouraud-shaded triangle mesh; 6 = Coons + * patch mesh; 7 Tensor-product patch mesh + * @param array $coords Array of coordinates. + * @param array $stops Array gradient color components: + * color = color; offset = (0 to 1) + * represents a location along the + * gradient vector; exponent = + * exponent of the exponential + * interpolation function (default + * = 1). + * @param string $bgcolor Background color + * @param bool $antialias Flag indicating whether to filter the + * shading function to prevent aliasing artifacts. * * @return string PDF command */ - public function getGradient($type, $coords, $stops, $bgcolor, $antialias = false) - { + public function getGradient( + int $type, + array $coords, + array $stops, + string $bgcolor, + bool $antialias = false + ): string { if ($this->pdfa) { return ''; } - $map_colspace = array('CMYK' => 'DeviceCMYK', 'RGB' => 'DeviceRGB', 'GRAY' => 'DeviceGray'); + $model = $this->pdfColor->getColorObject($stops[0]['color']); + if (! $model instanceof \Com\Tecnick\Color\Model) { + throw new GraphException('Invalid color'); + } + $ngr = (1 + count($this->gradients)); $this->gradients[$ngr] = $this->getGradientStops( - array( - 'type' => $type, - 'coords' => $coords, + [ 'antialias' => $antialias, - 'colors' => array(), + 'background' => $this->pdfColor->getColorObject($bgcolor), + 'colors' => [], + 'colspace' => self::COLSPACE[$model->getType()], + 'coords' => $coords, + 'id' => 0, + 'pattern' => 0, + 'stream' => '', 'transparency' => false, - 'background' => $this->col->getColorObject($bgcolor), - 'colspace' => $map_colspace[$this->col->getColorObject($stops[0]['color'])->getType()], - ), + 'type' => $type, + ], $stops ); @@ -252,6 +334,7 @@ public function getGradient($type, $coords, $stops, $bgcolor, $antialias = false // paint luminosity gradient $out .= '/TGS' . $ngr . ' gs' . "\n"; } + // paint the gradient $out .= '/Sh' . $ngr . ' sh' . "\n"; @@ -261,44 +344,81 @@ public function getGradient($type, $coords, $stops, $bgcolor, $antialias = false /** * Process the gradient stops. * - * @param array $grad Array containing gradient info - * @param array $stops Array gradient color components: - * color = color; - * offset = (0 to 1) represents a location along the gradient vector; - * exponent = exponent of the exponential interpolation function (default = 1). + * @param array{ + * 'antialias': bool, + * 'background': ?\Com\Tecnick\Color\Model, + * 'colors': array, + * 'colspace': string, + * 'coords': array, + * 'id': int, + * 'pattern': int, + * 'stream': string, + * 'transparency': bool, + * 'type': int, + * } $grad Array containing gradient info + * @param array $stops Array gradient color components: + * color = color; + * offset = (0 to 1) represents a location along the gradient vector; + * exponent = exponent of the exponential interpolation function (default = 1). * - * @return array Gradient array. + * @return array{ + * 'antialias': bool, + * 'background': ?\Com\Tecnick\Color\Model, + * 'colors': array, + * 'colspace': string, + * 'coords': array, + * 'id': int, + * 'pattern': int, + * 'stream': string, + * 'transparency': bool, + * 'type': int, + * } Gradient array. */ - protected function getGradientStops($grad, $stops) + protected function getGradientStops(array $grad, array $stops): array { $num_stops = count($stops); $last_stop_id = ($num_stops - 1); foreach ($stops as $key => $stop) { - $grad['colors'][$key] = array(); + $grad['colors'][$key] = []; $grad['colors'][$key]['color'] = $stop['color']; $grad['colors'][$key]['exponent'] = 1; if (isset($stop['exponent'])) { // exponent for the interpolation function $grad['colors'][$key]['exponent'] = $stop['exponent']; } + $grad['colors'][$key]['opacity'] = 1; if (isset($stop['opacity'])) { $grad['colors'][$key]['opacity'] = $stop['opacity']; $grad['transparency'] = ($grad['transparency'] || ($stop['opacity'] < 1)); } + // offset represents a location along the gradient vector if (isset($stop['offset'])) { $grad['colors'][$key]['offset'] = $stop['offset']; - } else { - if ($key == 0) { - $grad['colors'][$key]['offset'] = 0; - } elseif ($key == $last_stop_id) { - $grad['colors'][$key]['offset'] = 1; - } else { - $offsetstep = ((1 - $grad['colors'][($key - 1)]['offset']) / ($num_stops - $key)); - $grad['colors'][$key]['offset'] = ($grad['colors'][($key - 1)]['offset'] + $offsetstep); - } + } elseif ($key == 0) { + $grad['colors'][$key]['offset'] = 0; + } elseif ($key == $last_stop_id) { + $grad['colors'][$key]['offset'] = 1; + } elseif (isset($grad['colors'][($key - 1)]['offset'])) { + $offsetstep = ((1.0 - $grad['colors'][($key - 1)]['offset']) / ($num_stops - $key)); + $grad['colors'][$key]['offset'] = ($grad['colors'][($key - 1)]['offset'] + $offsetstep); } } @@ -308,105 +428,285 @@ protected function getGradientStops($grad, $stops) /** * Paints a coons patch mesh. * - * @param float $posx Abscissa of the top left corner of the rectangle. - * @param float $posy Ordinate of the top left corner of the rectangle. - * @param float $width Width of the rectangle. - * @param float $height Height of the rectangle. - * @param string $colll Lower-Left corner color. - * @param string $collr Lower-Right corner color. - * @param string $colur Upper-Right corner color. - * @param string $colul Upper-Left corner color. - * @param array $coords For one patch mesh: - * array(float x1, float y1, .... float x12, float y12): - * 12 pairs of coordinates (normally from 0 to 1) - * which specify the Bezier control points that define the patch. - * First pair is the lower left edge point, - * next is its right control point (control point 2). - * Then the other points are defined in the order: - * control point 1, edge point, control point 2 going counter-clockwise around the patch. - * Last (x12, y12) is the first edge point's left control point (control point 1). - * For two or more patch meshes: - * array[number of patches] - arrays with the following keys for each patch: - * f: where to put that patch (0 = first patch, 1, 2, 3 = right, top and left) - * points: 12 pairs of coordinates of the Bezier control points as above for the first patch, - * 8 pairs of coordinates for the following patches, - * ignoring the coordinates already defined by the precedent patch - * colors: must be 4 colors for the first patch, 2 colors for the following patches - * @param float $coords_min Minimum value used by the coordinates. - * If a coordinate's value is smaller than this it will be cut to coords_min. - * @param float $coords_max Maximum value used by the coordinates. - * If a coordinate's value is greater than this it will be cut to coords_max. - * @param boolean $antialias Flag indicating whether to filter the shading function to prevent aliasing artifacts. + * @param float $posx Abscissa of the top left corner of the rectangle. + * @param float $posy Ordinate of the top left corner of the rectangle. + * @param float $width Width of the rectangle. + * @param float $height Height of the rectangle. + * @param string $colll Lower-Left corner color. + * @param string $collr Lower-Right corner color. + * @param string $colur Upper-Right corner color. + * @param string $colul Upper-Left corner color. + * @param array $coords Coordinates + * @param float $coords_min Minimum value used by the coordinates. + * If a coordinate's value is smaller + * than this it will be cut to + * coords_min. + * @param float $coords_max Maximum value used by the coordinates. + * If a coordinate's value is greater + * than this it will be cut to + * coords_max. + * @param bool $antialias Flag indicating whether to filter the + * shading function to prevent aliasing artifacts. * * @return string PDF command * * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function getCoonsPatchMeshWithCoords( + float $posx, + float $posy, + float $width, + float $height, + string $colll = 'yellow', + string $collr = 'blue', + string $colur = 'green', + string $colul = 'red', + array $coords = [ + 0.00, + 0.00, + 0.33, + 0.00, + 0.67, + 0.00, + 1.00, + 0.00, + 1.00, + 0.33, + 1.00, + 0.67, + 1.00, + 1.00, + 0.67, + 1.00, + 0.33, + 1.00, + 0.00, + 1.00, + 0.00, + 0.67, + 0.00, + 0.33, + ], + float $coords_min = 0.0, + float $coords_max = 1.0, + bool $antialias = false + ): string { + if ($this->pdfa) { + return ''; + } + + // simple array -> convert to multi patch array + + $patch_array = [ + 0 => [ + 'f' => 0, + 'points' => $coords, + 'colors' => [ + 0 => [ + 'red' => 1, + 'green' => 1, + 'blue' => 0, + 'alpha' => 1, + ], + 1 => [ + 'red' => 0, + 'green' => 0, + 'blue' => 1, + 'alpha' => 1, + ], + 2 => [ + 'red' => 0, + 'green' => 1, + 'blue' => 0, + 'alpha' => 1, + ], + 3 => [ + 'red' => 1, + 'green' => 0, + 'blue' => 0, + 'alpha' => 1, + ], + ], + ], + ]; + + $colllobj = $this->pdfColor->getColorObject($colll); + if (! $colllobj instanceof \Com\Tecnick\Color\Model) { + throw new GraphException('Invalid Lower-Left corner color'); + } + + $patch_array[0]['colors'][0] = $colllobj->toRgbArray(); + + $collrobj = $this->pdfColor->getColorObject($collr); + if (! $collrobj instanceof \Com\Tecnick\Color\Model) { + throw new GraphException('Invalid Lower-Right corner color'); + } + + $patch_array[0]['colors'][1] = $collrobj->toRgbArray(); + + $colurobj = $this->pdfColor->getColorObject($colur); + if (! $colurobj instanceof \Com\Tecnick\Color\Model) { + throw new GraphException('Invalid Upper-Right corner color'); + } + + $patch_array[0]['colors'][2] = $colurobj->toRgbArray(); + + $colulobj = $this->pdfColor->getColorObject($colul); + if (! $colulobj instanceof \Com\Tecnick\Color\Model) { + throw new GraphException('Invalid Upper-Left corner color'); + } + + $patch_array[0]['colors'][3] = $colulobj->toRgbArray(); + + return $this->getCoonsPatchMesh( + $posx, + $posy, + $width, + $height, + $patch_array, + $coords_min, + $coords_max, + $antialias, + ); + } + + /** + * Paints a coons patch mesh. + * + * @param float $posx Abscissa of the top left corner of the rectangle. + * @param float $posy Ordinate of the top left corner of the rectangle. + * @param float $width Width of the rectangle. + * @param float $height Height of the rectangle. + * @param array, + * 'colors': array>, + * }> $patch_array For one patch mesh: + * array(float x1, + * float y1, .... + * float x12, float + * y12): 12 pairs of + * coordinates + * (normally from 0 to + * 1) which specify + * the Bezier control + * points that define + * the patch. First + * pair is the lower + * left edge point, + * next is its right + * control point + * (control point 2). + * Then the other + * points are defined + * in the order: + * control point 1, + * edge point, control + * point 2 going + * counter-clockwise + * around the patch. + * Last (x12, y12) is + * the first edge + * point's left + * control point + * (control point 1). + * For two or more + * patch meshes: + * array[number of + * patches] - arrays + * with the following + * keys for each + * patch: f: where to + * put that patch (0 = + * first patch, 1, 2, + * 3 = right, top and + * left) points: 12 + * pairs of + * coordinates of the + * Bezier control + * points as above for + * the first patch, 8 + * pairs of + * coordinates for the + * following patches, + * ignoring the + * coordinates already + * defined by the + * precedent patch + * colors: must be 4 + * colors for the + * first patch, 2 + * colors for the + * following patches + * @param float $coords_min Minimum value used by the coordinates. + * If a coordinate's value is smaller + * than this it will be cut to + * coords_min. + * @param float $coords_max Maximum value used by the coordinates. + * If a coordinate's value is greater + * than this it will be cut to + * coords_max. + * @param bool $antialias Flag indicating whether to filter the + * shading function to prevent aliasing artifacts. + * + * @return string PDF command + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getCoonsPatchMesh( - $posx, - $posy, - $width, - $height, - $colll = 'yellow', - $collr = 'blue', - $colur = 'green', - $colul = 'red', - $coords = array( - 0.00,0.00, 0.33,0.00, 0.67,0.00, 1.00,0.00, 1.00,0.33, 1.00,0.67, - 1.00,1.00, 0.67,1.00, 0.33,1.00, 0.00,1.00, 0.00,0.67, 0.00,0.33 - ), - $coords_min = 0, - $coords_max = 1, - $antialias = false - ) { + float $posx, + float $posy, + float $width, + float $height, + array $patch_array = [], + float $coords_min = 0.0, + float $coords_max = 1.0, + bool $antialias = false + ): string { if ($this->pdfa) { return ''; } $ngr = (1 + count($this->gradients)); - $this->gradients[$ngr] = array( - 'type' => 6, //coons patch mesh - 'coords' => array(), + $this->gradients[$ngr] = [ 'antialias' => $antialias, - 'colors' => array(), - 'transparency' => false, - 'background' => '', + 'colors' => [], + 'background' => null, 'colspace' => 'DeviceRGB', - ); - - // check the coords array if it is the simple array or the multi patch array - if (!isset($coords[0]['f'])) { - // simple array -> convert to multi patch array - $patch_array[0]['f'] = 0; - $patch_array[0]['points'] = $coords; - $patch_array[0]['colors'][0] = $this->col->getColorObject($colll)->toRgbArray(); - $patch_array[0]['colors'][1] = $this->col->getColorObject($collr)->toRgbArray(); - $patch_array[0]['colors'][2] = $this->col->getColorObject($colur)->toRgbArray(); - $patch_array[0]['colors'][3] = $this->col->getColorObject($colul)->toRgbArray(); - } else { - // multi patch array - $patch_array = $coords; - } + 'coords' => [], + 'id' => 0, + 'pattern' => 0, + 'stream' => '', + 'transparency' => false, + 'type' => 6, //coons patch mesh + ]; $bpcd = 65535; // 16 bits per coordinate - // build the data stream - $this->gradients[$ngr]['stream'] = ''; foreach ($patch_array as $par) { $this->gradients[$ngr]['stream'] .= chr($par['f']); // start with the edge flag as 8 bit foreach ($par['points'] as $point) { // each point as 16 bit - $point = floor(max(0, min( - $bpcd, - ((($point - $coords_min) / ($coords_max - $coords_min)) * $bpcd) - ))); - $this->gradients[$ngr]['stream'] .= chr((int)floor($point / 256)) . chr((int)floor($point % 256)); + $point = floor( + max( + 0, + min( + $bpcd, + ((($point - $coords_min) / ($coords_max - $coords_min)) * $bpcd) + ) + ) + ); + $this->gradients[$ngr]['stream'] .= chr((int) floor($point / 256)) . chr((int) floor($point % 256)); } + foreach ($par['colors'] as $color) { // each color component as 8 bit - $this->gradients[$ngr]['stream'] .= chr((int)floor($color['red'] * 255)) - . chr((int)floor($color['green'] * 255)) - . chr((int)floor($color['blue'] * 255)); + $this->gradients[$ngr]['stream'] .= chr((int) floor($color['red'] * 255)) + . chr((int) floor($color['green'] * 255)) + . chr((int) floor($color['blue'] * 255)); } } @@ -420,33 +720,45 @@ public function getCoonsPatchMesh( /** * Paints registration bars with color transtions * - * @param float $posx Abscissa of the top left corner of the rectangle. - * @param float $posy Ordinate of the top left corner of the rectangle. - * @param float $width Width of the rectangle. - * @param float $height Height of the rectangle. - * @param boolean $vertical If true prints bar vertically, otherwise horizontally. - * @param array $colors Array of colors to print, - * each entry is a color string or an array of two transition colors; + * @param float $posx Abscissa of the top left corner of the rectangle. + * @param float $posy Ordinate of the top left corner of the rectangle. + * @param float $width Width of the rectangle. + * @param float $height Height of the rectangle. + * @param bool $vertical If true prints bar vertically, otherwise horizontally. + * @param array> $colors Array of colors to print, + * each entry is a color + * string or an array of two + * transition colors; * * @return string PDF command + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getColorRegistrationBar( - $posx, - $posy, - $width, - $height, - $vertical = false, - $colors = array( - array('g(0%)', 'g(100%)'), // GRAY : black to white - array('rgb(100%,0%,0%)', 'rgb(100%,100%,100%)'), // RGB : red to white - array('rgb(0%,100%,0%)', 'rgb(100%,100%,100%)'), // RGB : green to white - array('rgb(0%,0%,100%)', 'rgb(100%,100%,100%)'), // RGB : blue to white - array('cmyk(100%,0%,0,0%)', 'cmyk(0%,0%,0,0%)'), // CMYK : cyan to white - array('cmyk(0%,100%,0,0%)', 'cmyk(0%,0%,0,0%)'), // CMYK : magenta to white - array('cmyk(0%,0%,100,0%)', 'cmyk(0%,0%,0,0%)'), // CMYK : yellow to white - array('cmyk(0%,0%,0,100%)', 'cmyk(0%,0%,0,0%)'), // CMYK : black to white - ) - ) { + float $posx, + float $posy, + float $width, + float $height, + bool $vertical = false, + array $colors = [ + // GRAY : black to white + ['g(0%)', 'g(100%)'], + // RGB : red to white + ['rgb(100%,0%,0%)', 'rgb(100%,100%,100%)'], + // RGB : green to white + ['rgb(0%,100%,0%)', 'rgb(100%,100%,100%)'], + // RGB : blue to white + ['rgb(0%,0%,100%)', 'rgb(100%,100%,100%)'], + // CMYK : cyan to white + ['cmyk(100%,0%,0,0%)', 'cmyk(0%,0%,0,0%)'], + // CMYK : magenta to white + ['cmyk(0%,100%,0,0%)', 'cmyk(0%,0%,0,0%)'], + // CMYK : yellow to white + ['cmyk(0%,0%,100,0%)', 'cmyk(0%,0%,0,0%)'], + // CMYK : black to white + ['cmyk(0%,0%,0,100%)', 'cmyk(0%,0%,0,0%)'], + ] + ): string { $numbars = count($colors); if ($numbars <= 0) { return ''; @@ -454,41 +766,46 @@ public function getColorRegistrationBar( // set bar measures if ($vertical) { - $coords = array(0, 1, 0, 0); // coordinates for gradient transition + $coords = [0, 1, 0, 0]; // coordinates for gradient transition $wbr = ($width / $numbars); // bar width $hbr = $height; // bar height $xdt = $wbr; // delta x $ydt = 0; // delta y } else { - $coords = array(0, 0, 1, 0); + $coords = [0, 0, 1, 0]; $wbr = $width; $hbr = ($height / $numbars); $xdt = 0; $ydt = $hbr; } + $xbr = $posx; $ybr = $posy; $out = ''; - foreach ($colors as $col) { - if (!empty($col)) { - if (!is_array($col)) { - $col = array($col, $col); - } - if (!isset($col[1])) { - $col[1] = $col[0]; + foreach ($colors as $color) { + if (! empty($color) && ! empty($color[0])) { + if (! isset($color[1])) { + $color[1] = $color[0]; } - if (($col[0] != $col[1]) && (!$this->pdfa)) { + + if (($color[0] !== $color[1]) && (! $this->pdfa)) { // color gradient - $out .= $this->getLinearGradient($xbr, $ybr, $wbr, $hbr, $col[0], $col[1], $coords); + $out .= $this->getLinearGradient($xbr, $ybr, $wbr, $hbr, $color[0], $color[1], $coords); } else { // colored rectangle - $out .= $this->getStartTransform() - . $this->col->getColorObject($col[0])->getPdfColor() - . $this->getBasicRect($xbr, $ybr, $wbr, $hbr, 'F') + $out .= $this->getStartTransform(); + + $colobj = $this->pdfColor->getColorObject($color[0]); + if ($colobj instanceof \Com\Tecnick\Color\Model) { + $out .= $colobj->getPdfColor(); + } + + $out .= $this->getBasicRect($xbr, $ybr, $wbr, $hbr, 'F') . $this->getStopTransform(); } } + $xbr += $xdt; $ybr += $ydt; } @@ -499,24 +816,33 @@ public function getColorRegistrationBar( /** * Get a crop-mark. * - * @param float $posx Abscissa of the crop-mark center. - * @param float $posy Ordinate of the crop-mark center. - * @param float $width Width of the crop-mark. - * @param float $height Height of the crop-mark. - * @param string $type Type of crop mark - one symbol per type: - * T = TOP, B = BOTTOM, L = LEFT, R = RIGHT - * @param array $style Line style to apply. + * @param float $posx Abscissa of the crop-mark center. + * @param float $posy Ordinate of the crop-mark center. + * @param float $width Width of the crop-mark. + * @param float $height Height of the crop-mark. + * @param string $type Type of crop mark - one symbol per type: + * T = TOP, B = BOTTOM, L = LEFT, R = RIGHT + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Line style to apply. * * @return string PDF command */ public function getCropMark( - $posx, - $posy, - $width, - $height, - $type = 'TBLR', - array $style = array() - ) { + float $posx, + float $posy, + float $width, + float $height, + string $type = 'TBLR', + array $style = [] + ): string { $crops = array_unique(str_split(strtoupper($type), 1)); $space_ratio = 4; $dhw = ($width / $space_ratio); // horizontal space to leave before the intersection point @@ -552,12 +878,13 @@ public function getCropMark( default: continue 2; } + $out .= $this->getRawPoint($posx1, $posy1) . $this->getRawLine($posx2, $posy2) . $this->getPathPaintOp('S'); } - if (empty($out)) { + if ($out === '') { return ''; } @@ -571,45 +898,74 @@ public function getCropMark( * Get overprint mode for stroking (OP) and non-stroking (op) painting operations. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008). * - * @param boolean $stroking If true apply overprint for stroking operations. - * @param boolean $nonstroking If true apply overprint for painting operations other than stroking. - * @param integer $mode Overprint mode: - * 0 = each source colour component value replaces the value previously - * painted for the corresponding device colorant; - * 1 = a tint value of 0.0 for a source colour component shall leave the - * corresponding component of the previously painted colour unchanged. + * @param bool $stroking If true apply overprint for stroking operations. + * @param bool $nonstroking If true apply overprint for painting operations other than stroking. + * @param int $mode Overprint mode: + * 0 = each source + * colour + * component value + * replaces the + * value + * previously + * painted for the + * corresponding + * device + * colorant; 1 = a + * tint value of + * 0.0 for a + * source colour + * component shall + * leave the + * corresponding + * component of + * the previously + * painted colour + * unchanged. * * @return string PDF command */ - public function getOverprint($stroking = true, $nonstroking = null, $mode = 0) - { + public function getOverprint( + bool $stroking = true, + bool $nonstroking = null, + int $mode = 0 + ): string { if ($nonstroking === null) { $nonstroking = $stroking; } + return $this->getExtGState( - array( - 'OP' => (bool)$stroking, - 'op' => (bool)$nonstroking, - 'OPM' => max(0, min(1, (int) $mode)), - ) + [ + 'OP' => $stroking, + 'op' => $nonstroking, + 'OPM' => max(0, min(1, $mode)), + ] ); } /** * Set alpha for stroking (CA) and non-stroking (ca) operations. * - * @param float $stroking Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque). - * @param string $bmv Blend mode, one of the following: - * Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, - * HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity. - * @param float|string $nonstroking Alpha value for non-stroking operations: - * real value from 0 (transparent) to 1 (opaque). - * @param boolean $ais + * @param float $stroking Alpha value for stroking operations: + * real value from 0 (transparent) to 1 (opaque). + * @param string $bmv Blend mode, one of the following: + * Normal, Multiply, Screen, + * Overlay, Darken, Lighten, + * ColorDodge, ColorBurn, HardLight, + * SoftLight, Difference, Exclusion, + * Hue, Saturation, Color, + * Luminosity. + * @param float|string $nonstroking Alpha value for non-stroking operations: + * real value from 0 (transparent) to 1 + * (opaque). * * @return string PDF command */ - public function getAlpha($stroking = 1, $bmv = 'Normal', $nonstroking = '', $ais = false) - { + public function getAlpha( + float $stroking = 1, + string $bmv = 'Normal', + float|string $nonstroking = '', + bool $ais = false + ): string { if ($nonstroking == '') { $nonstroking = $stroking; } @@ -618,34 +974,18 @@ public function getAlpha($stroking = 1, $bmv = 'Normal', $nonstroking = '', $ais // remove trailing slash $bmv = substr($bmv, 1); } - $map = array( - 'Normal', - 'Multiply', - 'Screen', - 'Overlay', - 'Darken', - 'Lighten', - 'ColorDodge', - 'ColorBurn', - 'HardLight', - 'SoftLight', - 'Difference', - 'Exclusion', - 'Hue', - 'Saturation', - 'Color', - 'Luminosity', - ); - if (!in_array($bmv, $map)) { - $bmv = $map[0]; + + if (! isset(self::BLENDMODE[$bmv])) { + $bmv = 'Normal'; } + return $this->getExtGState( - array( - 'CA' => floatval($stroking), - 'ca' => floatval($nonstroking), + [ + 'CA' => $stroking, + 'ca' => (float) $nonstroking, 'BM' => '/' . $bmv, - 'AIS' => (bool)$ais, - ) + 'AIS' => $ais, + ] ); } } diff --git a/src/Raw.php b/src/Raw.php index 8286d7d..32698b9 100644 --- a/src/Raw.php +++ b/src/Raw.php @@ -3,31 +3,29 @@ /** * Raw.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Com\Tecnick\Pdf\Graph; -use Com\Tecnick\Pdf\Graph\Exception as GraphException; - /** * Com\Tecnick\Pdf\Graph\Raw * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ abstract class Raw extends \Com\Tecnick\Pdf\Graph\Transform { @@ -40,7 +38,7 @@ abstract class Raw extends \Com\Tecnick\Pdf\Graph\Transform * * @return string PDF command */ - public function getRawPoint($posx, $posy) + public function getRawPoint(float $posx, float $posy): string { return sprintf( '%F %F m' . "\n", @@ -58,7 +56,7 @@ public function getRawPoint($posx, $posy) * * @return string PDF command */ - public function getRawLine($posx, $posy) + public function getRawLine(float $posx, float $posy): string { return sprintf( '%F %F l' . "\n", @@ -78,7 +76,7 @@ public function getRawLine($posx, $posy) * * @return string PDF command */ - public function getRawRect($posx, $posy, $width, $height) + public function getRawRect(float $posx, float $posy, float $width, float $height): string { return sprintf( '%F %F %F %F re' . "\n", @@ -104,8 +102,14 @@ public function getRawRect($posx, $posy, $width, $height) * * @return string PDF command */ - public function getRawCurve($posx1, $posy1, $posx2, $posy2, $posx3, $posy3) - { + public function getRawCurve( + float $posx1, + float $posy1, + float $posx2, + float $posy2, + float $posx3, + float $posy3 + ): string { return sprintf( '%F %F %F %F %F %F c' . "\n", ($posx1 * $this->kunit), @@ -130,7 +134,7 @@ public function getRawCurve($posx1, $posy1, $posx2, $posy2, $posx3, $posy3) * * @return string PDF command */ - public function getRawCurveV($posx2, $posy2, $posx3, $posy3) + public function getRawCurveV(float $posx2, float $posy2, float $posx3, float $posy3): string { return sprintf( '%F %F %F %F v' . "\n", @@ -154,7 +158,7 @@ public function getRawCurveV($posx2, $posy2, $posx3, $posy3) * * @return string PDF command */ - public function getRawCurveY($posx1, $posy1, $posx3, $posy3) + public function getRawCurveY(float $posx1, float $posy1, float $posx3, float $posy3): string { return sprintf( '%F %F %F %F y' . "\n", @@ -175,24 +179,33 @@ public function getRawCurveY($posx1, $posy1, $posx3, $posy3) * @param bool $ccw If true draws in counter-clockwise direction. * @param bool $svg If true the angles are in svg mode (already calculated). */ - protected function setRawEllipticalArcAngles(&$ags, &$agf, $rdv, $rdh, $ccw, $svg) - { - $ags = $this->degToRad((float) $ags); - $agf = $this->degToRad((float) $agf); - if (!$svg) { + protected function setRawEllipticalArcAngles( + float &$ags, + float &$agf, + float $rdv, + float $rdh, + bool $ccw, + bool $svg + ): void { + $ags = $this->degToRad($ags); + $agf = $this->degToRad($agf); + if (! $svg) { $ags = atan2((sin($ags) / $rdv), (cos($ags) / $rdh)); $agf = atan2((sin($agf) / $rdv), (cos($agf) / $rdh)); } + if ($ags < 0) { $ags += (2 * self::MPI); } + if ($agf < 0) { $agf += (2 * self::MPI); } + if ($ccw && ($ags > $agf)) { // reverse rotation $ags -= (2 * self::MPI); - } elseif (!$ccw && ($ags < $agf)) { + } elseif (! $ccw && ($ags < $agf)) { // reverse rotation $agf -= (2 * self::MPI); } @@ -202,49 +215,52 @@ protected function setRawEllipticalArcAngles(&$ags, &$agf, $rdv, $rdh, $ccw, $sv * Append an elliptical arc to the current path. * An ellipse is formed from n Bezier curves. * - * @param float $posxc Abscissa of center point. - * @param float $posyc Ordinate of center point. - * @param float $rdh Horizontal radius. - * @param float $rdv Vertical radius (if = 0 then it is a circle). - * @param float $posxang Angle between the X-axis and the major axis of the ellipse. - * @param float $angs Angle in degrees at which starting drawing. - * @param float $angf Angle in degrees at which stop drawing. - * @param bool $pie If true do not mark the border point (used to draw pie sectors). - * @param int $ncv Number of curves used to draw a 90 degrees portion of ellipse. - * @param bool $startpoint If true output a starting point. - * @param bool $ccw If true draws in counter-clockwise direction. - * @param bool $svg If true the angles are in svg mode (already calculated). - * @param array $bbox If provided, it will be filled with the bounding box coordinates - * (x min, y min, x max, y max). + * @param float $posxc Abscissa of center point. + * @param float $posyc Ordinate of center point. + * @param float $rdh Horizontal radius. + * @param float $rdv Vertical radius (if = 0 then it is a circle). + * @param float $posxang Angle between the X-axis and the major axis of the ellipse. + * @param float $angs Angle in degrees at which starting drawing. + * @param float $angf Angle in degrees at which stop drawing. + * @param bool $pie If true do not mark the border point (used to draw pie sectors). + * @param float $ncv Number of curves used to draw a 90 degrees portion of ellipse. + * @param bool $startpoint If true output a starting point. + * @param bool $ccw If true draws in counter-clockwise direction. + * @param bool $svg If true the angles are in svg mode (already calculated). + * @param array $bbox If provided, it will be filled with the bounding box coordinates + * (x min, y min, x max, y max). * * @return string PDF command * * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function getRawEllipticalArc( - $posxc, - $posyc, - $rdh, - $rdv, - $posxang = 0, - $angs = 0, - $angf = 360, - $pie = false, - $ncv = 2, - $startpoint = true, - $ccw = true, - $svg = false, - &$bbox = array() - ) { + float $posxc, + float $posyc, + float $rdh, + float $rdv, + float $posxang = 0.0, + float $angs = 0.0, + float $angf = 360.0, + bool $pie = false, + float $ncv = 2, + bool $startpoint = true, + bool $ccw = true, + bool $svg = false, + array &$bbox = [] + ): string { $out = ''; if (($rdh <= 0) || ($rdv < 0)) { return ''; } - $bbox = array(PHP_INT_MAX, PHP_INT_MAX, 0, 0); + + $bbox = [PHP_INT_MAX, PHP_INT_MAX, 0, 0]; if ($pie) { $out .= $this->getRawPoint($posxc, $posyc); // center of the arc } - $posxang = $this->degToRad((float) $posxang); + + $posxang = $this->degToRad($posxang); $ags = $angs; $agf = $angf; $this->setRawEllipticalArcAngles($ags, $agf, $rdv, $rdh, $ccw, $svg); @@ -256,7 +272,7 @@ public function getRawEllipticalArc( $posx0 = $posxc; // X center point in PDF coordinates $posy0 = ($this->pageh - $posyc); // Y center point in PDF coordinates $ang = $ags; // starting angle - $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3); + $alpha = sin($arcang) * ((sqrt(4 + (3 * tan(($arcang) / 2) ** 2)) - 1) / 3); $cos_xang = cos($posxang); $sin_xang = sin($posxang); $cos_ang = cos($ang); @@ -272,12 +288,14 @@ public function getRawEllipticalArc( } elseif ($startpoint) { $out .= $this->getRawPoint($px1, ($this->pageh - $py1)); // arc starting point } + // draw arcs for ($idx = 1; $idx <= $ncv; ++$idx) { $ang = $ags + ($idx * $arcang); // starting angle if ($idx == $ncv) { $ang = $agf; } + $cos_ang = cos($ang); $sin_ang = sin($ang); // second arc point @@ -295,23 +313,30 @@ public function getRawEllipticalArc( $cy3 = ($this->pageh - $py2); $out .= $this->getRawCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3); // get bounding box coordinates - $bbox = array( + $bbox = [ min($bbox[0], $cx1, $cx2, $cx3), min($bbox[1], $cy1, $cy2, $cy3), max($bbox[2], $cx1, $cx2, $cx3), max($bbox[3], $cy1, $cy2, $cy3), - ); + ]; // move to next point $px1 = $px2; $py1 = $py2; $qx1 = $qx2; $qy1 = $qy2; } + if ($pie) { $out .= $this->getRawLine($posxc, $posyc); // get bounding box coordinates - $bbox = array(min($bbox[0], $posxc), min($bbox[1], $posyc), max($bbox[2], $posxc), max($bbox[3], $posyc)); + $bbox = [ + min($bbox[0], $posxc), + min($bbox[1], $posyc), + max($bbox[2], $posxc), + max($bbox[3], $posyc), + ]; } + return $out; } @@ -326,7 +351,7 @@ public function getRawEllipticalArc( * * @return float Angle in radiants */ - public function getVectorsAngle($posx1, $posy1, $posx2, $posy2) + public function getVectorsAngle(int $posx1, int $posy1, int $posx2, int $posy2): float { $dprod = (($posx1 * $posx2) + ($posy1 * $posy2)); $dist1 = sqrt(($posx1 * $posx1) + ($posy1 * $posy1)); @@ -335,10 +360,12 @@ public function getVectorsAngle($posx1, $posy1, $posx2, $posy2) if ($distprod == 0) { return 0; } + $angle = acos(min(1, max(-1, ($dprod / $distprod)))); if ((($posx1 * $posy2) - ($posx2 * $posy1)) < 0) { $angle *= -1; } + return $angle; } } diff --git a/src/Style.php b/src/Style.php index 4409fde..96c72d3 100644 --- a/src/Style.php +++ b/src/Style.php @@ -3,13 +3,13 @@ /** * Style.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ @@ -21,130 +21,205 @@ /** * Com\Tecnick\Pdf\Graph\Style * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ abstract class Style extends \Com\Tecnick\Pdf\Graph\Base { /** - * Stack containing style data. + * Array of restore points (style ID). * - * @var array + * @var array */ - protected $style = array(); + protected array $stylemark = [0]; /** - * Stack index. + * Map values for lineCap. * - * @var int + * @var array */ - protected $styleid = -1; + protected const LINECAPMAP = [ + 0 => 0, + 1 => 1, + 2 => 2, + 'butt' => 0, + 'round' => 1, + 'square' => 2, + ]; /** - * Array of restore points (style ID). + * Map values for lineJoin. * - * @var array + * @var array */ - protected $stylemark = array(0); + protected const LINEJOINMAP = [ + 0 => 0, + 1 => 1, + 2 => 2, + 'miter' => 0, + 'round' => 1, + 'bevel' => 2, + ]; /** - * Unit of measure conversion ratio. + * Map path paint operators. * - * @var float + * @var array */ - protected $kunit = 1.0; + protected const PPOPMAP = [ + 'S' => 'S', + 'D' => 'S', + 's' => 's', + 'h S' => 's', + 'd' => 's', + 'f' => 'f', + 'F' => 'f', + 'h f' => 'h f', + 'f*' => 'f*', + 'F*' => 'f*', + 'h f*' => 'h f*', + 'B' => 'B', + 'FD' => 'B', + 'DF' => 'B', + 'B*' => 'B*', + 'F*D' => 'B*', + 'DF*' => 'B*', + 'b' => 'b', + 'h B' => 'b', + 'fd' => 'b', + 'df' => 'b', + 'b*' => 'b*', + 'h B*' => 'b*', + 'f*d' => 'b*', + 'df*' => 'b*', + 'W n' => 'W n', + 'CNZ' => 'W n', + 'W* n' => 'W* n', + 'CEO' => 'W* n', + 'h' => 'h', + 'n' => 'n', + ]; /** - * Map values for lineCap. + * Filling modes. * - * @var array + * @var array */ - protected static $linecapmap = array(0 => 0, 1 => 1, 2 => 2, 'butt' => 0, 'round' => 1, 'square' => 2); + protected const MODEFILLING = [ + 'f' => true, + 'f*' => true, + 'B' => true, + 'B*' => true, + 'b' => true, + 'b*' => true, + ]; /** - * Map values for lineJoin. + * Stroking Modes. * - * @var array + * @var array */ - protected static $linejoinmap = array(0 => 0, 1 => 1, 2 => 2, 'miter' => 0, 'round' => 1, 'bevel' => 2); + protected const MODESTROKING = [ + 'S' => true, + 's' => true, + 'B' => true, + 'B*' => true, + 'b' => true, + 'b*' => true, + ]; /** - * Map path paint operators. + * Closing Modes. * - * @var array + * @var array */ - protected static $ppopmap = array( - 'S' => 'S', - 'D' => 'S', - 's' => 's', - 'h S' => 's', - 'd' => 's', - 'f' => 'f', - 'F' => 'f', - 'h f' => 'h f', - 'f*' => 'f*', - 'F*' => 'f*', - 'h f*' => 'h f*', - 'B' => 'B', - 'FD' => 'B', - 'DF' => 'B', - 'B*' => 'B*', - 'F*D' => 'B*', - 'DF*' => 'B*', - 'b' => 'b', - 'h B' => 'b', - 'fd' => 'b', - 'df' => 'b', - 'b*' => 'b*', - 'h B*' => 'b*', - 'f*d' => 'b*', - 'df*' => 'b*', - 'W n' => 'W n', - 'CNZ' => 'W n', - 'W* n' => 'W* n', - 'CEO' => 'W* n', - 'h' => 'h', - 'n' => 'n' - ); + protected const MODECLOSING = [ + 'b' => true, + 'b*' => true, + 's' => true, + ]; /** - * Initialize default style + * Clipping Modes. + * + * @var array */ - public function init() - { - $this->style[++$this->styleid] = array( - 'lineWidth' => (1.0 / $this->kunit), // line thickness in user units - 'lineCap' => 'butt', // shape of the endpoints for any open path that is stroked - 'lineJoin' => 'miter', // shape of joints between connected segments of a stroked path - 'miterLimit' => (10.0 / $this->kunit), // maximum length of mitered line joins for stroked paths - 'dashArray' => array(), // lengths of alternating dashes and gaps - 'dashPhase' => 0, // distance at which to start the dash - 'lineColor' => 'black', // line (drawing) color - 'fillColor' => 'black', // background (filling) color - ); - return $this; - } + protected const MODECLIPPING = [ + 'CEO' => true, + 'CNZ' => true, + 'W n' => true, + 'W* n' => true, + ]; + + /** + * Map of equivalent modes without close. + * + * @var array + */ + protected const MODETONOCLOSE = [ + 's' => 'S', + 'b' => 'B', + 'b*' => 'B*', + ]; + + /** + * Map of equivalent modes without fill. + * + * @var array + */ + protected const MODETONOFILL = [ + 'f' => '', + 'f*' => '', + 'B' => 'S', + 'B*' => 'S', + 'b' => 's', + 'b*' => 's', + ]; + + /** + * Map of equivalent modes without STROKE. + * + * @var array + */ + protected const MODETONOSTROKE = [ + 'S' => '', + 's' => 'h', + 'B' => 'f', + 'B*' => 'f*', + 'b' => 'h f', + 'b*' => 'h f*', + ]; /** * Add a new style * - * @param array $style Style to add. - * @param bool $inheritlast If true inherit missing values from the last style. + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style to add. + * @param bool $inheritlast If true inherit missing values from the last style. * * @return string PDF style string */ - public function add(array $style = array(), $inheritlast = false) + public function add(array $style = [], bool $inheritlast = false): string { if ($inheritlast) { $style = array_merge($this->style[$this->styleid], $style); } + $this->style[++$this->styleid] = $style; return $this->getStyle(); } @@ -154,11 +229,12 @@ public function add(array $style = array(), $inheritlast = false) * * @return string PDF style string. */ - public function pop() + public function pop(): string { if ($this->styleid <= 0) { throw new GraphException('The style stack is empty'); } + $style = $this->getStyle(); unset($this->style[$this->styleid]); --$this->styleid; @@ -168,7 +244,7 @@ public function pop() /** * Save the current style ID to be restored later. */ - public function saveStyleStatus() + public function saveStyleStatus(): void { $this->stylemark[] = $this->styleid; } @@ -176,18 +252,33 @@ public function saveStyleStatus() /** * Restore the saved style status. */ - public function restoreStyleStatus() + public function restoreStyleStatus(): void { - $this->styleid = array_pop($this->stylemark); + $styleid = array_pop($this->stylemark); + if ($styleid === null) { + $styleid = 0; + } + + $this->styleid = $styleid; + $this->style = array_slice($this->style, 0, ($this->styleid + 1), true); } /** * Returns the last style array. * - * @return array + * @return array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } */ - public function getCurrentStyleArray() + public function getCurrentStyleArray(): array { return $this->style[$this->styleid]; } @@ -200,34 +291,35 @@ public function getCurrentStyleArray() * * @return mixed Property value or $default in case the property is not found. */ - public function getLastStyleProperty($property, $default = null) + public function getLastStyleProperty(string $property, mixed $default = null): mixed { for ($idx = $this->styleid; $idx >= 0; --$idx) { if (isset($this->style[$idx][$property])) { return $this->style[$idx][$property]; } } + return $default; } /** * Returns the value of th especified item from the last inserted style. * - * @return mixed + * @param string $item Item to search. */ - public function getCurrentStyleItem($item) + public function getCurrentStyleItem(string $item): mixed { - if (!isset($this->style[$this->styleid][$item])) { + if (! isset($this->style[$this->styleid][$item])) { throw new GraphException('The ' . $item . ' value is not set in the current style'); } + return $this->style[$this->styleid][$item]; } + /** * Returns the PDF string of the last style added. - * - * @return string */ - public function getStyle() + public function getStyle(): string { return $this->getStyleCmd($this->style[$this->styleid]); } @@ -235,106 +327,140 @@ public function getStyle() /** * Returns the PDF string of the specified style. * - * @param array $style Style to represent. - * - * @return string + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style to represent. */ - public function getStyleCmd(array $style) + public function getStyleCmd(array $style = []): string { $out = ''; if (isset($style['lineWidth'])) { - $out .= sprintf('%F w' . "\n", ((float) $style['lineWidth'] * $this->kunit)); + $out .= sprintf('%F w' . "\n", ($style['lineWidth'] * $this->kunit)); } + $out .= $this->getLineModeCmd($style); + if (isset($style['lineColor'])) { - $out .= $this->col->getPdfColor($style['lineColor'], true); + $out .= $this->pdfColor->getPdfColor($style['lineColor'], true); } + if (isset($style['fillColor'])) { - $out .= $this->col->getPdfColor($style['fillColor'], false); + $out .= $this->pdfColor->getPdfColor($style['fillColor'], false); } + return $out; } /** * Returns the PDF string of the specified line style. * - * @param array $style Style to represent. - * - * @return string + * @param array{ + * 'lineWidth'?: float, + * 'lineCap'?: string, + * 'lineJoin'?: string, + * 'miterLimit'?: float, + * 'dashArray'?: array, + * 'dashPhase'?: float, + * 'lineColor'?: string, + * 'fillColor'?: string, + * } $style Style to represent. * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - protected function getLineModeCmd(array $style) + protected function getLineModeCmd(array $style = []): string { $out = ''; - if (isset($style['lineCap']) && isset(self::$linecapmap[$style['lineCap']])) { - $out .= self::$linecapmap[$style['lineCap']] . ' J' . "\n"; + + if (isset($style['lineCap']) && isset(self::LINECAPMAP[$style['lineCap']])) { + $out .= self::LINECAPMAP[$style['lineCap']] . ' J' . "\n"; } - if (isset($style['lineJoin']) && isset(self::$linejoinmap[$style['lineJoin']])) { - $out .= self::$linejoinmap[$style['lineJoin']] . ' j' . "\n"; + + if (isset($style['lineJoin']) && isset(self::LINEJOINMAP[$style['lineJoin']])) { + $out .= self::LINEJOINMAP[$style['lineJoin']] . ' j' . "\n"; } + if (isset($style['miterLimit'])) { - $out .= sprintf('%F M' . "\n", ((float) $style['miterLimit'] * $this->kunit)); + $out .= sprintf('%F M' . "\n", ($style['miterLimit'] * $this->kunit)); } - if (isset($style['dashArray']) && is_array($style['dashArray'])) { - $dash = array(); + + if (isset($style['dashArray'])) { + $dash = []; foreach ($style['dashArray'] as $val) { $dash[] = sprintf('%F', ((float) $val * $this->kunit)); } - if (!isset($style['dashPhase'])) { + + if (! isset($style['dashPhase'])) { $style['dashPhase'] = 0; } - $out .= sprintf('[%s] %F d' . "\n", implode(' ', $dash), $style['dashPhase']); + + return $out .= sprintf('[%s] %F d' . "\n", implode(' ', $dash), $style['dashPhase']); } + return $out; } /** * Get the Path-Painting Operators. * - * @param string $mode Mode of rendering. Possible values are: - * - S or D: Stroke the path. - * - s or d: Close and stroke the path. - * - f or F: Fill the path, using the nonzero winding number rule to determine the region to fill. - * - f* or F*: Fill the path, using the even-odd rule to determine the region to fill. - * - B or FD or DF: Fill and then stroke the path, - * using the nonzero winding number rule to determine the region to fill. - * - B* or F*D or DF*: Fill and then stroke the path, - * using the even-odd rule to determine the region to fill. - * - b or fd or df: Close, fill, and then stroke the path, - * using the nonzero winding number rule to determine the region to fill. - * - b or f*d or df*: Close, fill, and then stroke the path, - * using the even-odd rule to determine the region to fill. - * - CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path. - * - CEO: Clipping mode using the nonzero winding number rule to determine - * which regions lie inside the clipping path - * - n: End the path object without filling or stroking it. + * @param string $mode Mode of rendering. Possible values are: + * - S or D: Stroke the path. - s or d: + * Close and stroke the path. - f or F: + * Fill the path, using the nonzero + * winding number rule to determine the + * region to fill. - f* or F*: Fill the + * path, using the even-odd rule to + * determine the region to fill. - B or FD + * or DF: Fill and then stroke the path, + * using the nonzero winding number rule + * to determine the region to fill. - B* + * or F*D or DF*: Fill and then stroke the + * path, using the even-odd rule to + * determine the region to fill. - b or fd + * or df: Close, fill, and then stroke the + * path, using the nonzero winding number + * rule to determine the region to fill. - + * b or f*d or df*: Close, fill, and then + * stroke the path, using the even-odd + * rule to determine the region to fill. - + * CNZ: Clipping mode using the even-odd + * rule to determine which regions lie + * inside the clipping path. - CEO: + * Clipping mode using the nonzero winding + * number rule to determine which regions + * lie inside the clipping path - n: End + * the path object without filling or + * stroking it. * @param string $default Default style - * - * @return string */ - public function getPathPaintOp($mode, $default = 'S') + public function getPathPaintOp(string $mode, string $default = 'S'): string { - if (!empty(self::$ppopmap[$mode])) { - return self::$ppopmap[$mode] . "\n"; + if (! isset(self::PPOPMAP[$mode])) { + $mode = $default; } - if (!empty(self::$ppopmap[$default])) { - return self::$ppopmap[$default] . "\n"; + + if (! isset(self::PPOPMAP[$mode])) { + return ''; } - return ''; + + return self::PPOPMAP[$mode] . "\n"; } /** * Returns true if the specified path paint operator includes the filling option. * * @param string $mode Path paint operator (mode of rendering). - * - * @return bool */ - public function isFillingMode($mode) + public function isFillingMode(string $mode): bool { - return (!empty(self::$ppopmap[$mode]) - && (in_array(self::$ppopmap[$mode], array('f', 'f*', 'B', 'B*', 'b', 'b*')) + return (isset(self::PPOPMAP[$mode]) && self::PPOPMAP[$mode] !== '' + && (isset(self::MODEFILLING[self::PPOPMAP[$mode]]) || $this->isClippingMode($mode)) ); } @@ -343,13 +469,11 @@ public function isFillingMode($mode) * Returns true if the specified mode includes the stroking option. * * @param string $mode Path paint operator (mode of rendering). - * - * @return bool */ - public function isStrokingMode($mode) + public function isStrokingMode(string $mode): bool { - return (!empty(self::$ppopmap[$mode]) - && in_array(self::$ppopmap[$mode], array('S', 's', 'B', 'B*', 'b', 'b*')) + return (isset(self::PPOPMAP[$mode]) && self::PPOPMAP[$mode] !== '' + && isset(self::MODESTROKING[self::PPOPMAP[$mode]]) ); } @@ -357,13 +481,11 @@ public function isStrokingMode($mode) * Returns true if the specified mode includes "closing the path" option. * * @param string $mode Path paint operator (mode of rendering). - * - * @return bool */ - public function isClosingMode($mode) + public function isClosingMode(string $mode): bool { - return (!empty(self::$ppopmap[$mode]) - && (in_array(self::$ppopmap[$mode], array('b','b*','s')) + return (isset(self::PPOPMAP[$mode]) && self::PPOPMAP[$mode] !== '' + && (isset(self::MODECLOSING[self::PPOPMAP[$mode]]) || $this->isClippingMode($mode)) ); } @@ -372,13 +494,11 @@ public function isClosingMode($mode) * Returns true if the specified mode is of clippping type. * * @param string $mode Path paint operator (mode of rendering). - * - * @return bool */ - public function isClippingMode($mode) + public function isClippingMode(string $mode): bool { - return (!empty(self::$ppopmap[$mode]) - && in_array(self::$ppopmap[$mode], array('CEO','CNZ','W n','W* n')) + return (isset(self::PPOPMAP[$mode]) && self::PPOPMAP[$mode] !== '' + && isset(self::MODECLIPPING[self::PPOPMAP[$mode]]) ); } @@ -386,15 +506,17 @@ public function isClippingMode($mode) * Remove the Close option from the specified Path paint operator. * * @param string $mode Path paint operator (mode of rendering). - * - * @return string */ - public function getModeWithoutClose($mode) + public function getModeWithoutClose(string $mode): string { - $map = array('s' => 'S', 'b' => 'B', 'b*' => 'B*'); - if (!empty(self::$ppopmap[$mode]) && isset($map[self::$ppopmap[$mode]])) { - return $map[self::$ppopmap[$mode]]; + if ( + isset(self::PPOPMAP[$mode]) + && (self::PPOPMAP[$mode] !== '') + && isset(self::MODETONOCLOSE[self::PPOPMAP[$mode]]) + ) { + return self::MODETONOCLOSE[self::PPOPMAP[$mode]]; } + return $mode; } @@ -402,15 +524,17 @@ public function getModeWithoutClose($mode) * Remove the Fill option from the specified Path paint operator. * * @param string $mode Path paint operator (mode of rendering). - * - * @return string */ - public function getModeWithoutFill($mode) + public function getModeWithoutFill(string $mode): string { - $map = array('f' => '', 'f*' => '', 'B' => 'S', 'B*' => 'S', 'b' => 's', 'b*' => 's'); - if (!empty(self::$ppopmap[$mode]) && isset($map[self::$ppopmap[$mode]])) { - return $map[self::$ppopmap[$mode]]; + if ( + isset(self::PPOPMAP[$mode]) + && (self::PPOPMAP[$mode] !== '') + && isset(self::MODETONOFILL[self::PPOPMAP[$mode]]) + ) { + return self::MODETONOFILL[self::PPOPMAP[$mode]]; } + return $mode; } @@ -418,26 +542,28 @@ public function getModeWithoutFill($mode) * Remove the Stroke option from the specified Path paint operator. * * @param string $mode Path paint operator (mode of rendering). - * - * @return string */ - public function getModeWithoutStroke($mode) + public function getModeWithoutStroke(string $mode): string { - $map = array('S' => '', 's' => 'h', 'B' => 'f', 'B*' => 'f*', 'b' => 'h f', 'b*' => 'h f*'); - if (!empty(self::$ppopmap[$mode]) && isset($map[self::$ppopmap[$mode]])) { - return $map[self::$ppopmap[$mode]]; + if ( + isset(self::PPOPMAP[$mode]) + && (self::PPOPMAP[$mode] !== '') + && isset(self::MODETONOSTROKE[self::PPOPMAP[$mode]]) + ) { + return self::MODETONOSTROKE[self::PPOPMAP[$mode]]; } + return $mode; } /** * Add transparency parameters to the current extgstate. * - * @param array $parms parameters. + * @param array $parms parameters. * * @return string PDF command. */ - public function getExtGState($parms) + public function getExtGState(array $parms): string { if ($this->pdfa) { return ''; @@ -451,9 +577,15 @@ public function getExtGState($parms) break; } } + if (empty($this->extgstates[$gsx])) { - $this->extgstates[$gsx] = array('parms' => $parms); + $this->extgstates[$gsx] = [ + 'n' => 0, + 'name' => '', + 'parms' => $parms, + ]; } + return '/GS' . $gsx . ' gs' . "\n"; } } diff --git a/src/Transform.php b/src/Transform.php index ad9f7c5..2844415 100644 --- a/src/Transform.php +++ b/src/Transform.php @@ -3,13 +3,13 @@ /** * Transform.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ @@ -21,47 +21,43 @@ /** * Com\Tecnick\Pdf\Graph\Transform * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ abstract class Transform extends \Com\Tecnick\Pdf\Graph\Style { /** - * Array (stack) of Current Transformation Matrix (CTM), - * which maps user space coordinates used within a PDF content stream into output device coordinates. - * - * @var array + * Current ID for transformation matrix. */ - protected $ctm = array(); + protected int $ctmid = -1; /** - * Current ID for transformation matrix. + * Array (stack) of Current Transformation Matrix (CTM), + * which maps user space coordinates used within a PDF content stream into output device coordinates. * - * @var int + * @var array>> */ - protected $ctmid = -1; + protected array $ctm = []; /** * Returns the transformation stack. * - * @return array + * @return array>> */ - public function getTransformStack() + public function getTransformStack(): array { return $this->ctm; } /** * Returns the transformation stack index. - * - * @return int */ - public function getTransformIndex() + public function getTransformIndex(): int { return $this->ctmid; } @@ -69,27 +65,24 @@ public function getTransformIndex() /** * Starts a 2D transformation saving current graphic state. * This function must be called before calling transformation methods - * - * @return string */ - public function getStartTransform() + public function getStartTransform(): string { $this->saveStyleStatus(); - $this->ctm[++$this->ctmid] = array(); + $this->ctm[++$this->ctmid] = []; return 'q' . "\n"; } /** * Stops a 2D tranformation restoring previous graphic state. * This function must be called after calling transformation methods. - * - * @return string */ - public function getStopTransform() + public function getStopTransform(): string { - if (!isset($this->ctm[$this->ctmid])) { + if (! isset($this->ctm[$this->ctmid])) { return ''; } + unset($this->ctm[$this->ctmid]); --$this->ctmid; $this->restoreStyleStatus(); @@ -99,11 +92,9 @@ public function getStopTransform() /** * Get the tranformation matrix (CTM) PDF string * - * @param array $ctm Transformation matrix array. - * - * @return string + * @param array $ctm Transformation matrix array. */ - public function getTransformation($ctm) + public function getTransformation(array $ctm): string { $this->ctm[$this->ctmid][] = $ctm; return sprintf('%F %F %F %F %F %F cm' . "\n", $ctm[0], $ctm[1], $ctm[2], $ctm[3], $ctm[4], $ctm[5]); @@ -119,14 +110,15 @@ public function getTransformation($ctm) * * @return string Transformation string */ - public function getScaling($skx, $sky, $posx, $posy) + public function getScaling(float $skx, float $sky, float $posx, float $posy): string { if (($skx == 0) || ($sky == 0)) { throw new GraphException('Scaling factors must be different than zero'); } + $posy = (($this->pageh - $posy) * $this->kunit); - $posx = ($posx * $this->kunit); - $ctm = array($skx, 0, 0, $sky, ($posx * (1 - $skx)), ($posy * (1 - $sky))); + $posx *= $this->kunit; + $ctm = [$skx, 0, 0, $sky, ($posx * (1 - $skx)), ($posy * (1 - $sky))]; return $this->getTransformation($ctm); } @@ -139,7 +131,7 @@ public function getScaling($skx, $sky, $posx, $posy) * * @return string Transformation string */ - public function getHorizScaling($skx, $posx, $posy) + public function getHorizScaling(float $skx, float $posx, float $posy): string { return $this->getScaling($skx, 1, $posx, $posy); } @@ -153,7 +145,7 @@ public function getHorizScaling($skx, $posx, $posy) * * @return string Transformation string */ - public function getVertScaling($sky, $posx, $posy) + public function getVertScaling(float $sky, float $posx, float $posy): string { return $this->getScaling(1, $sky, $posx, $posy); } @@ -167,7 +159,7 @@ public function getVertScaling($sky, $posx, $posy) * * @return string Transformation string */ - public function getPropScaling($skf, $posx, $posy) + public function getPropScaling(float $skf, float $posx, float $posy): string { return $this->getScaling($skf, $skf, $posx, $posy); } @@ -176,16 +168,16 @@ public function getPropScaling($skf, $posx, $posy) * Rotation. * * @param float $angle Angle in degrees for counter-clockwise rotation. - * @param float $posx Abscissa of the rotation center. - * @param float $posy Ordinate of the rotation center. + * @param float $posx Abscissa of the rotation center. + * @param float $posy Ordinate of the rotation center. * * @return string Transformation string */ - public function getRotation($angle, $posx, $posy) + public function getRotation(float $angle, float $posx, float $posy): string { $posy = (($this->pageh - $posy) * $this->kunit); - $posx = ($posx * $this->kunit); - $ctm = array(); + $posx *= $this->kunit; + $ctm = []; $ctm[0] = cos($this->degToRad($angle)); $ctm[1] = sin($this->degToRad($angle)); $ctm[2] = -$ctm[1]; @@ -202,7 +194,7 @@ public function getRotation($angle, $posx, $posy) * * @return string Transformation string */ - public function getHorizMirroring($posx) + public function getHorizMirroring(float $posx): string { return $this->getScaling(-1, 1, $posx, 0); } @@ -214,7 +206,7 @@ public function getHorizMirroring($posx) * * @return string Transformation string */ - public function getVertMirroring($posy) + public function getVertMirroring(float $posy): string { return $this->getScaling(1, -1, 0, $posy); } @@ -227,7 +219,7 @@ public function getVertMirroring($posy) * * @return string Transformation string */ - public function getPointMirroring($posx, $posy) + public function getPointMirroring(float $posx, float $posy): string { return $this->getScaling(-1, -1, $posx, $posy); } @@ -241,7 +233,7 @@ public function getPointMirroring($posx, $posy) * * @return string Transformation string */ - public function getReflection($ang, $posx, $posy) + public function getReflection(float $ang, float $posx, float $posy): string { return $this->getScaling(-1, 1, $posx, $posy) . $this->getRotation((-2 * ($ang - 90)), $posx, $posy); } @@ -254,10 +246,10 @@ public function getReflection($ang, $posx, $posy) * * @return string Transformation string */ - public function getTranslation($trx, $try) + public function getTranslation(float $trx, float $try): string { //calculate elements of transformation matrix - $ctm = array(1, 0, 0, 1, ($trx * $this->kunit), (-$try * $this->kunit)); + $ctm = [1, 0, 0, 1, ($trx * $this->kunit), (-$try * $this->kunit)]; return $this->getTransformation($ctm); } @@ -268,7 +260,7 @@ public function getTranslation($trx, $try) * * @return string Transformation string */ - public function getHorizTranslation($trx) + public function getHorizTranslation(float $trx): string { return $this->getTranslation($trx, 0); } @@ -280,7 +272,7 @@ public function getHorizTranslation($trx) * * @return string Transformation string */ - public function getVertTranslation($try) + public function getVertTranslation(float $try): string { return $this->getTranslation(0, $try); } @@ -295,14 +287,15 @@ public function getVertTranslation($try) * * @return string Transformation string */ - public function getSkewing($angx, $angy, $posx, $posy) + public function getSkewing(float $angx, float $angy, float $posx, float $posy): string { if (($angx <= -90) || ($angx >= 90) || ($angy <= -90) || ($angy >= 90)) { throw new GraphException('Angle values must be beweeen -90 and +90 degrees.'); } + $posy = (($this->pageh - $posy) * $this->kunit); - $posx = ($posx * $this->kunit); - $ctm = array(); + $posx *= $this->kunit; + $ctm = []; $ctm[0] = 1; $ctm[1] = tan($this->degToRad($angy)); $ctm[2] = tan($this->degToRad($angx)); @@ -321,7 +314,7 @@ public function getSkewing($angx, $angy, $posx, $posy) * * @return string Transformation string */ - public function getHorizSkewing($angx, $posx, $posy) + public function getHorizSkewing(float $angx, float $posx, float $posy): string { return $this->getSkewing($angx, 0, $posx, $posy); } @@ -335,7 +328,7 @@ public function getHorizSkewing($angx, $posx, $posy) * * @return string Transformation string */ - public function getVertSkewing($angy, $posx, $posy) + public function getVertSkewing(float $angy, float $posx, float $posy): string { return $this->getSkewing(0, $angy, $posx, $posy); } @@ -343,21 +336,21 @@ public function getVertSkewing($angy, $posx, $posy) /** * Get the product of two Tranformation Matrix. * - * @param array $tma First Tranformation Matrix. - * @param array $tmb Second Tranformation Matrix. + * @param array $tma First Tranformation Matrix. + * @param array $tmb Second Tranformation Matrix. * - * @return array CTM Transformation Matrix. + * @return array CTM Transformation Matrix. */ - public function getCtmProduct($tma, $tmb) + public function getCtmProduct(array $tma, array $tmb): array { - return array( - (((float) $tma[0] * (float) $tmb[0]) + ((float) $tma[2] * (float) $tmb[1])), - (((float) $tma[1] * (float) $tmb[0]) + ((float) $tma[3] * (float) $tmb[1])), - (((float) $tma[0] * (float) $tmb[2]) + ((float) $tma[2] * (float) $tmb[3])), - (((float) $tma[1] * (float) $tmb[2]) + ((float) $tma[3] * (float) $tmb[3])), - (((float) $tma[0] * (float) $tmb[4]) + ((float) $tma[2] * (float) $tmb[5]) + (float) $tma[4]), - (((float) $tma[1] * (float) $tmb[4]) + ((float) $tma[3] * (float) $tmb[5]) + (float) $tma[5]) - ); + return [ + (($tma[0] * $tmb[0]) + ($tma[2] * $tmb[1])), + (($tma[1] * $tmb[0]) + ($tma[3] * $tmb[1])), + (($tma[0] * $tmb[2]) + ($tma[2] * $tmb[3])), + (($tma[1] * $tmb[2]) + ($tma[3] * $tmb[3])), + (($tma[0] * $tmb[4]) + ($tma[2] * $tmb[5]) + $tma[4]), + (($tma[1] * $tmb[4]) + ($tma[3] * $tmb[5]) + $tma[5]), + ]; } /** @@ -368,7 +361,7 @@ public function getCtmProduct($tma, $tmb) * * @return float Angle in radiants */ - public function degToRad($deg) + public function degToRad(float $deg): float { return ($deg * self::MPI / 180); } diff --git a/test/BaseTest.php b/test/BaseTest.php index 1574871..ee4683d 100644 --- a/test/BaseTest.php +++ b/test/BaseTest.php @@ -3,35 +3,33 @@ /** * BaseTest.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Base Test * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class BaseTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\Pdf\Graph\Draw { return new \Com\Tecnick\Pdf\Graph\Draw( 0.75, @@ -43,18 +41,19 @@ protected function getTestObject() ); } - public function testGetOutExtGState() + public function testGetOutExtGState(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getOutExtGState(10); + $draw = $this->getTestObject(); + $res = $draw->getOutExtGState(10); $this->assertEquals( '', $res ); - $testObj->getOverprint(); - $testObj->getAlpha(); - $res = $testObj->getOutExtGState(10); + $draw->getOverprint(); + $draw->getAlpha(); + + $res = $draw->getOutExtGState(10); $this->assertEquals( '11 0 obj' . "\n" . '<< /Type /ExtGState /OP true /op true /OPM 0.000000 >>' . "\n" @@ -65,43 +64,43 @@ public function testGetOutExtGState() $res ); - $this->assertEquals(12, $testObj->getObjectNumber()); + $this->assertEquals(12, $draw->getObjectNumber()); } - public function testGetOutExtGStateResourcesEmpty() + /** + * @SuppressWarnings(PHPMD.LongVariable) + */ + public function testGetOutExtGStateResourcesEmpty(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getOutExtGStateResources(); - $this->assertEquals( - '', - $res - ); + $draw = $this->getTestObject(); + $outExtGStateResources = $draw->getOutExtGStateResources(); + $this->assertEquals('', $outExtGStateResources); } - public function testGetOutGradientResourcesEmpty() + public function testGetOutGradientResourcesEmpty(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getOutGradientResources(); + $draw = $this->getTestObject(); + $outGradientResources = $draw->getOutGradientResources(); $this->assertEquals( '', - $res + $outGradientResources ); } - public function testGetOutGradientShaders() + public function testGetOutGradientShaders(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getOutGradientShaders(10); + $draw = $this->getTestObject(); + $res = $draw->getOutGradientShaders(10); $this->assertEquals( '', $res ); - $testObj->getCoonsPatchMesh(3, 5, 7, 11); - $testObj->getOutGradientShaders(11); - $this->assertEquals(13, $testObj->getObjectNumber()); + $draw->getCoonsPatchMeshWithCoords(3, 5, 7, 11); + $draw->getOutGradientShaders(11); + $this->assertEquals(13, $draw->getObjectNumber()); - $res = $testObj->getOutGradientResources(); + $res = $draw->getOutGradientResources(); $this->assertEquals( ' /Pattern << /p1 13 0 R >>' . "\n" . ' /Shading << /Sh1 12 0 R >>' . "\n", @@ -109,59 +108,87 @@ public function testGetOutGradientShaders() ); } - public function testGetOutShaders() + public function testGetOutShaders(): void { - $testObj = $this->getTestObject(); - $stops = array( - array('color' => 'red', 'exponent' => 1, 'opacity' => 0.5), - array('color' => 'blue', 'offset' => 0.2, 'exponent' => 1, 'opacity' => 0.6), - array('color' => '#98fb98', 'exponent' => 1, 'opacity' => 0.7), - array('color' => 'rgb(64,128,191)', 'offset' => 0.8, 'exponent' => 1, 'opacity' => 0.8), - array('color' => 'skyblue', 'exponent' => 1, 'opacity' => 0.9), - ); + $draw = $this->getTestObject(); + $stops = [ + [ + 'color' => 'red', + 'exponent' => 1, + 'opacity' => 0.5, + ], + [ + 'color' => 'blue', + 'exponent' => 1, + 'offset' => 0.2, + 'opacity' => 0.6, + ], + [ + 'color' => '#98fb98', + 'exponent' => 1, + 'opacity' => 0.7, + ], + [ + 'color' => 'rgb(64,128,191)', + 'exponent' => 1, + 'offset' => 0.8, + 'opacity' => 0.8, + ], + [ + 'color' => 'skyblue', + 'exponent' => 1, + 'opacity' => 0.9, + ], + ]; $this->assertEquals( '/TGS1 gs' . "\n" . '/Sh1 sh' . "\n", - $testObj->getGradient(2, array(0,0,1,0), $stops, '', false) + $draw->getGradient(2, [0, 0, 1, 0], $stops, '', false) ); - $testObj->getOverprint(); - $testObj->getAlpha(); - $testObj->getOutExtGState($testObj->getObjectNumber()); + $draw->getOverprint(); + $draw->getAlpha(); + $draw->getOutExtGState($draw->getObjectNumber()); - $testObj->getOutGradientShaders($testObj->getObjectNumber()); - $this->assertEquals(19, $testObj->getObjectNumber()); + $draw->getOutGradientShaders($draw->getObjectNumber()); + $this->assertEquals(19, $draw->getObjectNumber()); - $res = $testObj->getOutExtGStateResources(); + $res = $draw->getOutExtGStateResources(); $this->assertEquals( ' /ExtGState << /GS1 1 0 R /GS2 2 0 R /TGS1 20 0 R >>' . "\n", $res ); } - public function testGetOutShadersRadial() + public function testGetOutShadersRadial(): void { - $testObj = $this->getTestObject(); - $testObj->getGradient( + $draw = $this->getTestObject(); + $out = $draw->getGradient( 3, - array(0.6,0.5,0.4,0.3,1), - array( - array( + [0.6, 0.5, 0.4, 0.3, 1], + [ + [ 'color' => 'red', - 'offset' => 0, 'exponent' => 1, - ), - array( + 'offset' => 0, + ], + [ 'color' => 'green', - 'offset' => 1, 'exponent' => 1, - ), - ), + 'offset' => 1, + ], + ], 'white', true ); - $testObj->getOutGradientShaders($testObj->getObjectNumber()); - $this->assertEquals(4, $testObj->getObjectNumber()); + $this->assertEquals( + '/Sh1 sh' . "\n", + $out + ); + + $this->assertEquals(0, $draw->getObjectNumber()); + $draw->getOutGradientShaders($draw->getObjectNumber()); + $this->assertEquals(4, $draw->getObjectNumber()); } } diff --git a/test/DrawTest.php b/test/DrawTest.php index 769c2f5..2706b76 100644 --- a/test/DrawTest.php +++ b/test/DrawTest.php @@ -3,35 +3,33 @@ /** * DrawTest.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Draw Test * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class DrawTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\Pdf\Graph\Draw { return new \Com\Tecnick\Pdf\Graph\Draw( 0.75, @@ -43,24 +41,38 @@ protected function getTestObject() ); } - protected function getTestStyle() + /** + * Get test style + * + * @return array{ + * 'lineWidth': float, + * 'lineCap': string, + * 'lineJoin': string, + * 'miterLimit': float, + * 'dashArray': array, + * 'dashPhase': float, + * 'lineColor': string, + * 'fillColor': string, + * } + */ + protected function getTestStyle(): array { - return array( - 'lineWidth' => 3, - 'lineCap' => 'round', - 'lineJoin' => 'bevel', + return [ + 'lineWidth' => 3, + 'lineCap' => 'round', + 'lineJoin' => 'bevel', 'miterLimit' => 11, - 'dashArray' => array(5, 7), - 'dashPhase' => 0.75, - 'lineColor' => 'greenyellow', - 'fillColor' => '["RGB",0.250000,0.500000,0.750000]', - ); + 'dashArray' => [5, 7], + 'dashPhase' => 0.75, + 'lineColor' => 'greenyellow', + 'fillColor' => '["RGB",0.250000,0.500000,0.750000]', + ]; } - public function testGetLine() + public function testGetLine(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getLine(3, 5, 7, 11); + $draw = $this->getTestObject(); + $res = $draw->getLine(3, 5, 7, 11); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '5.250000 66.750000 l' . "\n" @@ -69,7 +81,7 @@ public function testGetLine() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getLine(3, 5, 7, 11, $testStyle); + $res = $draw->getLine(3, 5, 7, 11, $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -85,10 +97,10 @@ public function testGetLine() ); } - public function testGetCurve() + public function testGetCurve(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getCurve(3, 5, 7, 11, 13, 17, 19, 23); + $draw = $this->getTestObject(); + $res = $draw->getCurve(3, 5, 7, 11, 13, 17, 19, 23); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '5.250000 66.750000 9.750000 62.250000 14.250000 57.750000 c' . "\n" @@ -97,7 +109,7 @@ public function testGetCurve() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getCurve(3, 5, 7, 11, 13, 17, 19, 23, 'B*', $testStyle); + $res = $draw->getCurve(3, 5, 7, 11, 13, 17, 19, 23, 'B*', $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -113,15 +125,12 @@ public function testGetCurve() ); } - public function testGetPolycurve() + public function testGetPolycurve(): void { - $testObj = $this->getTestObject(); - $segments = array( - array(7, 11, 13, 17, 19, 23), - array(29, 31, 37, 41, 43, 47), - ); + $draw = $this->getTestObject(); + $segments = [[7, 11, 13, 17, 19, 23], [29, 31, 37, 41, 43, 47]]; - $res = $testObj->getPolycurve(3, 5, $segments); + $res = $draw->getPolycurve(3, 5, $segments); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '5.250000 66.750000 9.750000 62.250000 14.250000 57.750000 c' . "\n" @@ -131,7 +140,7 @@ public function testGetPolycurve() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getPolycurve(3, 5, $segments, 'B*', $testStyle); + $res = $draw->getPolycurve(3, 5, $segments, 'B*', $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -148,10 +157,10 @@ public function testGetPolycurve() ); } - public function testGetEllipse() + public function testGetEllipse(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getEllipse(3, 5, 7, 0, 13, 61, 349); + $draw = $this->getTestObject(); + $res = $draw->getEllipse(3, 5, 7, 0, 13, 61, 349); $this->assertEquals( '3.697096 76.296624 m' . "\n" . '2.476319 76.646676 1.169043 76.542151 0.019397 76.002569 c' . "\n" @@ -166,7 +175,7 @@ public function testGetEllipse() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getEllipse(3, 5, 7, 11, 13, 61, 349, 'B*', $testStyle, 4); + $res = $draw->getEllipse(3, 5, 7, 11, 13, 61, 349, 'B*', $testStyle, 4); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -196,10 +205,10 @@ public function testGetEllipse() ); } - public function testGetCircle() + public function testGetCircle(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getCircle(3, 5, 7, 61, 349); + $draw = $this->getTestObject(); + $res = $draw->getCircle(3, 5, 7, 61, 349); $this->assertEquals( '4.795251 75.841753 m' . "\n" . '3.684506 76.457449 2.387223 76.649676 1.145663 76.382537 c' . "\n" @@ -214,7 +223,7 @@ public function testGetCircle() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getCircle(3, 5, 7, 61, 349, 'B*', $testStyle, 4); + $res = $draw->getCircle(3, 5, 7, 61, 349, 'B*', $testStyle, 4); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -243,10 +252,10 @@ public function testGetCircle() ); } - public function testGetPieSector() + public function testGetPieSector(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getPieSector(3, 5, 7, 90, 120); + $draw = $this->getTestObject(); + $res = $draw->getPieSector(3, 5, 7, 90, 120); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '2.250000 76.500000 l' . "\n" @@ -258,12 +267,12 @@ public function testGetPieSector() ); } - public function testGetBasicPolygon() + public function testGetBasicPolygon(): void { - $testObj = $this->getTestObject(); - $points = array(3, 5, 7, 11, 13, 17, 19, 23); + $draw = $this->getTestObject(); + $points = [3, 5, 7, 11, 13, 17, 19, 23]; - $res = $testObj->getBasicPolygon($points); + $res = $draw->getBasicPolygon($points); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '5.250000 66.750000 l' . "\n" @@ -274,7 +283,7 @@ public function testGetBasicPolygon() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getBasicPolygon($points, 'B*', $testStyle); + $res = $draw->getBasicPolygon($points, 'B*', $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -292,15 +301,15 @@ public function testGetBasicPolygon() ); } - public function testGetPolygon() + public function testGetPolygon(): void { - $testObj = $this->getTestObject(); - $points = array(3, 5, 7, 11, 13, 17, 19, 23); + $draw = $this->getTestObject(); + $points = [3, 5, 7, 11, 13, 17, 19, 23]; - $res = $testObj->getPolygon(array(3, 5, 7, 11)); + $res = $draw->getPolygon([3, 5, 7, 11]); $this->assertEquals('', $res); - $res = $testObj->getPolygon($points); + $res = $draw->getPolygon($points); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '5.250000 66.750000 l' . "\n" @@ -315,10 +324,15 @@ public function testGetPolygon() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getPolygon( + $res = $draw->getPolygon( $points, 'b*', - array('all' => $testStyle, 0 => array('lineColor' => '#ff0000')) + [ + 'all' => $testStyle, + 0 => [ + 'lineColor' => '#ff0000', + ], + ] ); $this->assertEquals( '2.250000 w' . "\n" @@ -352,13 +366,13 @@ public function testGetPolygon() ); } - public function testGetRegularPolygon() + public function testGetRegularPolygon(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getRegularPolygon(3, 5, 7, 2); + $draw = $this->getTestObject(); + $res = $draw->getRegularPolygon(3, 5, 7, 2); $this->assertEquals('', $res); - $res = $testObj->getRegularPolygon(3, 5, 7, 5, 11); + $res = $draw->getRegularPolygon(3, 5, 7, 5, 11); $this->assertEquals( '3.251747 66.096457 m' . "\n" . '7.460867 70.610186 l' . "\n" @@ -376,16 +390,23 @@ public function testGetRegularPolygon() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getRegularPolygon( + $res = $draw->getRegularPolygon( 3, 5, 7, 5, 11, 'b*', - array('all' => $testStyle, 0 => array('lineColor' => '#ff0000')), + [ + 'all' => $testStyle, + 0 => [ + 'lineColor' => '#ff0000', + ], + ], 'f', - array('fillColor' => '#cccccc') + [ + 'fillColor' => '#cccccc', + ] ); $this->assertEquals( '0.800000 0.800000 0.800000 rg' . "\n" @@ -435,13 +456,13 @@ public function testGetRegularPolygon() ); } - public function testGetStarPolygon() + public function testGetStarPolygon(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getStarPolygon(3, 5, 7, 1, 0); + $draw = $this->getTestObject(); + $res = $draw->getStarPolygon(3, 5, 7, 1, 0); $this->assertEquals('', $res); - $res = $testObj->getStarPolygon(3, 5, 7, 11, 13); + $res = $draw->getStarPolygon(3, 5, 7, 11, 13); $this->assertEquals( '2.250000 66.000000 m' . "\n" . '7.025568 69.069071 l' . "\n" @@ -477,7 +498,7 @@ public function testGetStarPolygon() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getStarPolygon( + $res = $draw->getStarPolygon( 3, 5, 7, @@ -485,9 +506,16 @@ public function testGetStarPolygon() 13, 61, 'b*', - array('all' => $testStyle, 0 => array('lineColor' => '#ff0000')), + [ + 'all' => $testStyle, + 0 => [ + 'lineColor' => '#ff0000', + ], + ], 'f', - array('fillColor' => '#cccccc') + [ + 'fillColor' => '#cccccc', + ] ); $this->assertEquals( '0.800000 0.800000 0.800000 rg' . "\n" @@ -561,10 +589,10 @@ public function testGetStarPolygon() ); } - public function testGetBasicRect() + public function testGetBasicRect(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getBasicRect(3, 5, 7, 11); + $draw = $this->getTestObject(); + $res = $draw->getBasicRect(3, 5, 7, 11); $this->assertEquals( '2.250000 71.250000 5.250000 -8.250000 re' . "\n" . 'S' . "\n", @@ -572,7 +600,7 @@ public function testGetBasicRect() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getBasicRect(3, 5, 7, 11, 'b*', $testStyle); + $res = $draw->getBasicRect(3, 5, 7, 11, 'b*', $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -587,10 +615,10 @@ public function testGetBasicRect() ); } - public function testGetRect() + public function testGetRect(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getRect(3, 5, 7, 11); + $draw = $this->getTestObject(); + $res = $draw->getRect(3, 5, 7, 11); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '7.500000 71.250000 l' . "\n" @@ -608,13 +636,18 @@ public function testGetRect() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getRect( + $res = $draw->getRect( 3, 5, 7, 11, 'b*', - array('all' => $testStyle, 1 => array('lineColor' => '#ff0000')) + [ + 'all' => $testStyle, + 1 => [ + 'lineColor' => '#ff0000', + ], + ] ); $this->assertEquals( '2.250000 w' . "\n" @@ -647,10 +680,10 @@ public function testGetRect() ); } - public function testGetRoundedRect() + public function testGetRoundedRect(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getRoundedRect(3, 5, 137, 241, 13, 17); + $draw = $this->getTestObject(); + $res = $draw->getRoundedRect(3, 5, 137, 241, 13, 17); $this->assertEquals( '12.000000 71.250000 m' . "\n" . '95.250000 71.250000 l' . "\n" @@ -665,7 +698,7 @@ public function testGetRoundedRect() $res ); - $res = $testObj->getRoundedRect(3, 5, 137, 241, 13, 17, '0000'); + $res = $draw->getRoundedRect(3, 5, 137, 241, 13, 17, '0000'); $this->assertEquals( '2.250000 71.250000 102.750000 -180.750000 re' . "\n" . 'S' . "\n", @@ -673,7 +706,7 @@ public function testGetRoundedRect() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getRoundedRect(3, 5, 137, 241, 13, 17, '1010', 'b*', $testStyle); + $res = $draw->getRoundedRect(3, 5, 137, 241, 13, 17, '1010', 'b*', $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -695,7 +728,7 @@ public function testGetRoundedRect() $res ); - $res = $testObj->getRoundedRect(3, 5, 137, 241, 13, 17, '0101'); + $res = $draw->getRoundedRect(3, 5, 137, 241, 13, 17, '0101'); $this->assertEquals( '12.000000 71.250000 m' . "\n" . '95.250000 71.250000 l' . "\n" @@ -711,10 +744,10 @@ public function testGetRoundedRect() ); } - public function testGetArrow() + public function testGetArrow(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getArrow(3, 5, 7, 11); + $draw = $this->getTestObject(); + $res = $draw->getArrow(3, 5, 7, 11); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '5.250000 66.750000 l' . "\n" @@ -727,7 +760,7 @@ public function testGetArrow() ); $testStyle = $this->getTestStyle(); - $res = $testObj->getArrow(3, 5, 7, 11, 1, 6, 17, $testStyle); + $res = $draw->getArrow(3, 5, 7, 11, 1, 6, 17, $testStyle); $this->assertEquals( '2.250000 w' . "\n" . '1 J' . "\n" @@ -753,7 +786,7 @@ public function testGetArrow() $res ); - $res = $testObj->getArrow(3, 5, 7, 11, 2, 6, 17, array()); + $res = $draw->getArrow(3, 5, 7, 11, 2, 6, 17); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '3.308549 69.662176 l' . "\n" @@ -765,7 +798,7 @@ public function testGetArrow() $res ); - $res = $testObj->getArrow(3, 5, 7, 11, 3, 6, 17, array()); + $res = $draw->getArrow(3, 5, 7, 11, 3, 6, 17); $this->assertEquals( '2.250000 71.250000 m' . "\n" . '3.308549 69.662176 l' . "\n" @@ -778,10 +811,10 @@ public function testGetArrow() ); } - public function testGetRegistrationMark() + public function testGetRegistrationMark(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getRegistrationMark(3, 5, 7, true, 'All'); + $draw = $this->getTestObject(); + $res = $draw->getRegistrationMark(3, 5, 7, true, 'All'); $this->assertEquals( 'q' . "\n" . '0.500000 w' . "\n" @@ -946,10 +979,10 @@ public function testGetRegistrationMark() ); } - public function testGetCmykRegistrationMark() + public function testGetCmykRegistrationMark(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getCmykRegistrationMark(3, 5, 11); + $draw = $this->getTestObject(); + $res = $draw->getCmykRegistrationMark(3, 5, 11); $this->assertEquals( 'q' . "\n" . '1.000000 0.000000 0.000000 0.000000 k' . "\n" diff --git a/test/GradientTest.php b/test/GradientTest.php index c735ecf..65cdcb0 100644 --- a/test/GradientTest.php +++ b/test/GradientTest.php @@ -3,35 +3,33 @@ /** * GradientTest.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Gradient Test * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class GradientTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\Pdf\Graph\Draw { return new \Com\Tecnick\Pdf\Graph\Draw( 0.75, @@ -43,53 +41,53 @@ protected function getTestObject() ); } - public function testGetClippingRect() + public function testGetClippingRect(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '2.250000 71.250000 5.250000 -8.250000 re W n' . "\n", - $testObj->getClippingRect(3, 5, 7, 11) + $draw->getClippingRect(3, 5, 7, 11) ); } - public function testGetGradientTransform() + public function testGetGradientTransform(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '5.250000 0.000000 0.000000 8.250000 2.250000 63.000000 cm' . "\n", - $testObj->getGradientTransform(3, 5, 7, 11) + $draw->getGradientTransform(3, 5, 7, 11) ); } - public function testGetLinearGradient() + public function testGetLinearGradient(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( 'q' . "\n" . '2.250000 71.250000 5.250000 -8.250000 re W n' . "\n" . '5.250000 0.000000 0.000000 8.250000 2.250000 63.000000 cm' . "\n" . '/Sh1 sh' . "\n" . 'Q' . "\n", - $testObj->getLinearGradient(3, 5, 7, 11, 'red', 'green', array(1,2,3,4)) + $draw->getLinearGradient(3, 5, 7, 11, 'red', 'green', [1, 2, 3, 4]) ); } - public function testGetRadialGradient() + public function testGetRadialGradient(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( 'q' . "\n" . '2.250000 71.250000 5.250000 -8.250000 re W n' . "\n" . '5.250000 0.000000 0.000000 8.250000 2.250000 63.000000 cm' . "\n" . '/Sh1 sh' . "\n" . 'Q' . "\n", - $testObj->getRadialGradient(3, 5, 7, 11, 'red', 'green', array(0.6,0.5,0.4,0.3,1)) + $draw->getRadialGradient(3, 5, 7, 11, 'red', 'green', [0.6, 0.5, 0.4, 0.3, 1]) ); } - public function testGetGradientPDFA() + public function testGetGradientPDFA(): void { - $obj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 0.75, 80, 100, @@ -99,79 +97,107 @@ public function testGetGradientPDFA() ); $this->assertEquals( '', - $obj->getGradient(2, array(), array(), '', false) + $draw->getGradient(2, [], [], '', false) ); } - public function testGetGradient() + public function testGetGradient(): void { - $testObj = $this->getTestObject(); - $stops = array( - array('color' => 'red', 'exponent' => 1, 'opacity' => 0.5), - array('color' => 'blue', 'offset' => 0.2, 'exponent' => 1, 'opacity' => 0.6), - array('color' => '#98fb98', 'exponent' => 1, 'opacity' => 0.7), - array('color' => 'rgb(64,128,191)', 'offset' => 0.8, 'exponent' => 1, 'opacity' => 0.8), - array('color' => 'skyblue', 'exponent' => 1, 'opacity' => 0.9), - ); + $draw = $this->getTestObject(); + $stops = [ + [ + 'color' => 'red', + 'exponent' => 1.0, + 'offset' => 0.0, + 'opacity' => 0.5, + ], + [ + 'color' => 'blue', + 'exponent' => 1.0, + 'offset' => 0.2, + 'opacity' => 0.6, + ], + [ + 'color' => '#98fb98', + 'exponent' => 1.0, + 'offset' => 0.47, + 'opacity' => 0.7, + ], + [ + 'color' => 'rgb(64,128,191)', + 'exponent' => 1.0, + 'offset' => 0.8, + 'opacity' => 0.8, + ], + [ + 'color' => 'skyblue', + 'exponent' => 1.0, + 'offset' => 1.0, + 'opacity' => 0.9, + ], + ]; $this->assertEquals( '/TGS1 gs' . "\n" . '/Sh1 sh' . "\n", - $testObj->getGradient(2, array(0,0,1,0), $stops, '', false) + $draw->getGradient(2, [0, 0, 1, 0], $stops, '', false) ); - $exp = array ( - 1 => array ( + $exp = [ + 1 => [ 'type' => 2, - 'coords' => array ( + 'coords' => [ 0 => 0, 1 => 0, 2 => 1, 3 => 0, - ), + ], 'antialias' => false, - 'colors' => array ( - 0 => array ( + 'colors' => [ + 0 => [ 'color' => 'red', 'exponent' => 1, - 'opacity' => 0.5, 'offset' => 0, - ), - 1 => array ( + 'opacity' => 0.5, + ], + 1 => [ 'color' => 'blue', 'exponent' => 1, - 'opacity' => 0.60, 'offset' => 0.20, - ), - 2 => array ( + 'opacity' => 0.60, + ], + 2 => [ 'color' => '#98fb98', 'exponent' => 1, - 'opacity' => 0.70, 'offset' => 0.47, - ), - 3 => array ( + 'opacity' => 0.70, + ], + 3 => [ 'color' => 'rgb(64,128,191)', 'exponent' => 1, - 'opacity' => 0.80, 'offset' => 0.80, - ), - 4 => array ( + 'opacity' => 0.80, + ], + 4 => [ 'color' => 'skyblue', 'exponent' => 1, - 'opacity' => 0.90, 'offset' => 1, - ), - ), + 'opacity' => 0.90, + ], + ], 'transparency' => true, 'background' => null, 'colspace' => 'DeviceCMYK', - ), - ); - $this->bcAssertEqualsWithDelta($exp, $testObj->getGradientsArray()); + 'id' => 0, + 'pattern' => 0, + 'stream' => '', + ], + ]; + $this->bcAssertEqualsWithDelta($exp, $draw->getGradientsArray()); } - public function testGetCoonsPatchMeshPDFA() + public function testGetCoonsPatchMeshPDFA(): void { - $obj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 0.75, 80, 100, @@ -181,26 +207,26 @@ public function testGetCoonsPatchMeshPDFA() ); $this->assertEquals( '', - $obj->getCoonsPatchMesh(3, 5, 7, 11) + $draw->getCoonsPatchMesh(3, 5, 7, 11) ); } - public function testGetCoonsPatchMesh() + public function testGetCoonsPatchMesh(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( 'q' . "\n" . '2.250000 71.250000 5.250000 -8.250000 re W n' . "\n" . '5.250000 0.000000 0.000000 8.250000 2.250000 63.000000 cm' . "\n" . '/Sh1 sh' . "\n" . 'Q' . "\n", - $testObj->getCoonsPatchMesh(3, 5, 7, 11) + $draw->getCoonsPatchMesh(3, 5, 7, 11) ); - $patch_array = array ( - 0 => array ( + $patch_array = [ + 0 => [ 'f' => 0, - 'points' => array ( + 'points' => [ 0 => 0.0, 1 => 0.0, 2 => 0.33, @@ -225,33 +251,33 @@ public function testGetCoonsPatchMesh() 21 => 0.67, 22 => 0.0, 23 => 0.33, - ), - 'colors' => array ( - 0 => array ( + ], + 'colors' => [ + 0 => [ 'red' => 255, 'green' => 255, 'blue' => 0, - ), - 1 => array ( + ], + 1 => [ 'red' => 0, 'green' => 0, 'blue' => 255, - ), - 2 => array ( + ], + 2 => [ 'red' => 0, 'green' => 255, 'blue' => 0, - ), - 3 => array ( + ], + 3 => [ 'red' => 255, 'green' => 0, 'blue' => 0, - ), - ), - ), - 1 => array ( + ], + ], + ], + 1 => [ 'f' => 2, - 'points' => array ( + 'points' => [ 0 => 0.0, 1 => 1.33, 2 => 0.0, @@ -268,23 +294,23 @@ public function testGetCoonsPatchMesh() 13 => 1.67, 14 => 1.5, 15 => 1.33, - ), - 'colors' => array ( - 0 => array ( + ], + 'colors' => [ + 0 => [ 'red' => 0, 'green' => 0, 'blue' => 0, - ), - 1 => array ( + ], + 1 => [ 'red' => 255, 'green' => 0, 'blue' => 255, - ), - ), - ), - 2 => array ( + ], + ], + ], + 2 => [ 'f' => 3, - 'points' => array ( + 'points' => [ 0 => 1.33, 1 => 0.80, 2 => 1.67, @@ -301,23 +327,23 @@ public function testGetCoonsPatchMesh() 13 => 2.0, 14 => 1.33, 15 => 2.0, - ), - 'colors' => array ( - 0 => array ( + ], + 'colors' => [ + 0 => [ 'red' => 0, 'green' => 255, 'blue' => 255, - ), - 1 => array ( + ], + 1 => [ 'red' => 0, 'green' => 0, 'blue' => 0, - ), - ), - ), - 3 => array ( + ], + ], + ], + 3 => [ 'f' => 1, - 'points' => array ( + 'points' => [ 0 => 2.0, 1 => 0.67, 2 => 2.0, @@ -334,21 +360,21 @@ public function testGetCoonsPatchMesh() 13 => 0.33, 14 => 0.80, 15 => 0.67, - ), - 'colors' => array ( - 0 => array ( + ], + 'colors' => [ + 0 => [ 'red' => 0, 'green' => 0, 'blue' => 0, - ), - 1 => array ( + ], + 1 => [ 'red' => 0, 'green' => 0, 'blue' => 255, - ), - ), - ), - ); + ], + ], + ], + ]; $this->assertEquals( 'q' . "\n" @@ -356,14 +382,14 @@ public function testGetCoonsPatchMesh() . '142.500000 0.000000 0.000000 150.000000 7.500000 -108.750000 cm' . "\n" . '/Sh2 sh' . "\n" . 'Q' . "\n", - $testObj->getCoonsPatchMesh(10, 45, 190, 200, '', '', '', '', $patch_array, 0, 2) + $draw->getCoonsPatchMesh(10, 45, 190, 200, $patch_array, 0, 2) ); } - public function testGetColorRegistrationBar() + public function testGetColorRegistrationBar(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getColorRegistrationBar(50, 70, 40, 40); + $draw = $this->getTestObject(); + $res = $draw->getColorRegistrationBar(50, 70, 40, 40); $this->assertEquals( 'q' . "\n" . '37.500000 22.500000 30.000000 -3.750000 re W n' . "\n" @@ -408,7 +434,7 @@ public function testGetColorRegistrationBar() $res ); - $res = $testObj->getColorRegistrationBar(50, 70, 40, 40, true); + $res = $draw->getColorRegistrationBar(50, 70, 40, 40, true); $this->assertEquals( 'q' . "\n" . '37.500000 22.500000 3.750000 -30.000000 re W n' . "\n" @@ -453,25 +479,25 @@ public function testGetColorRegistrationBar() $res ); - $res = $testObj->getColorRegistrationBar( + $res = $draw->getColorRegistrationBar( 50, 70, 40, 40, true, - array( - '', - 'g(50%)', - 'rgb(50%,50%,50%)', - 'cmyk(50%,50%,50,50%)', - array('rgb(100%,0%,0%)'), - array('red', 'white'), - array('black', 'black'), - array('g(11%)', 'g(11%)'), - array('rgb(30%,50%,70%)', 'rgb(170%,150%,130%)'), - array('cmyk(10%,20%,30,40%)', 'cmyk(100%,90%,80,70%)'), - array(), - ) + [ + [''], + ['g(50%)'], + ['rgb(50%,50%,50%)'], + ['cmyk(50%,50%,50,50%)'], + ['rgb(100%,0%,0%)'], + ['red', 'white'], + ['black', 'black'], + ['g(11%)', 'g(11%)'], + ['rgb(30%,50%,70%)', 'rgb(170%,150%,130%)'], + ['cmyk(10%,20%,30,40%)', 'cmyk(100%,90%,80,70%)'], + [], + ] ); $this->assertEquals( @@ -523,24 +549,24 @@ public function testGetColorRegistrationBar() $res ); - $res = $testObj->getColorRegistrationBar( + $res = $draw->getColorRegistrationBar( 50, 70, 40, 40, false, - array() + [] ); $this->assertEquals('', $res); } - public function testGetCropMark() + public function testGetCropMark(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getCropMark(3, 5, 7, 11, ''); + $draw = $this->getTestObject(); + $res = $draw->getCropMark(3, 5, 7, 11, ''); $this->assertEquals('', $res); - $res = $testObj->getCropMark(3, 5, 7, 11, 'TBLR'); + $res = $draw->getCropMark(3, 5, 7, 11, 'TBLR'); $this->assertEquals( 'q' . "\n" . '2.250000 79.500000 m' . "\n" @@ -559,13 +585,14 @@ public function testGetCropMark() $res ); - $style = array( + $style = [ 'lineWidth' => 0.3, 'lineColor' => 'black', - 'lineCap' => 'butt', - 'lineJoin' => 'miter', - ); - $res = $testObj->getCropMark(3, 5, 7, 11, 'TBLR', $style); + 'lineCap' => 'butt', + 'lineJoin' => 'miter', + ]; + + $res = $draw->getCropMark(3, 5, 7, 11, 'TBLR', $style); $this->assertEquals( 'q' . "\n" . '0.225000 w' . "\n" @@ -589,32 +616,32 @@ public function testGetCropMark() ); } - public function testGetOverprint() + public function testGetOverprint(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getOverprint(); + $draw = $this->getTestObject(); + $res = $draw->getOverprint(); $this->assertEquals( '/GS1 gs' . "\n", $res ); - $res = $testObj->getOverprint(false, true, 1); + $res = $draw->getOverprint(false, true, 1); $this->assertEquals( '/GS2 gs' . "\n", $res ); } - public function testGetAlpha() + public function testGetAlpha(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getAlpha(); + $draw = $this->getTestObject(); + $res = $draw->getAlpha(); $this->assertEquals( '/GS1 gs' . "\n", $res ); - $res = $testObj->getAlpha(0.5, '/Missing', 0.4, true); + $res = $draw->getAlpha(0.5, '/Missing', 0.4, true); $this->assertEquals( '/GS2 gs' . "\n", $res diff --git a/test/RawTest.php b/test/RawTest.php index 68dd342..8883ef5 100644 --- a/test/RawTest.php +++ b/test/RawTest.php @@ -3,35 +3,33 @@ /** * RawTest.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Raw Test * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class RawTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\Pdf\Graph\Draw { return new \Com\Tecnick\Pdf\Graph\Draw( 0.75, @@ -43,52 +41,52 @@ protected function getTestObject() ); } - public function testGetRawPoint() + public function testGetRawPoint(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('2.250000 71.250000 m' . "\n", $testObj->getRawPoint(3, 5)); + $draw = $this->getTestObject(); + $this->assertEquals('2.250000 71.250000 m' . "\n", $draw->getRawPoint(3, 5)); } - public function testGetRawLine() + public function testGetRawLine(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('2.250000 71.250000 l' . "\n", $testObj->getRawLine(3, 5)); + $draw = $this->getTestObject(); + $this->assertEquals('2.250000 71.250000 l' . "\n", $draw->getRawLine(3, 5)); } - public function testGetRawRect() + public function testGetRawRect(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('2.250000 71.250000 5.250000 -8.250000 re' . "\n", $testObj->getRawRect(3, 5, 7, 11)); + $draw = $this->getTestObject(); + $this->assertEquals('2.250000 71.250000 5.250000 -8.250000 re' . "\n", $draw->getRawRect(3, 5, 7, 11)); } - public function testGetRawCurve() + public function testGetRawCurve(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '2.250000 71.250000 5.250000 66.750000 9.750000 62.250000 c' . "\n", - $testObj->getRawCurve(3, 5, 7, 11, 13, 17) + $draw->getRawCurve(3, 5, 7, 11, 13, 17) ); } - public function testGetRawCurveV() + public function testGetRawCurveV(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('2.250000 71.250000 5.250000 66.750000 v' . "\n", $testObj->getRawCurveV(3, 5, 7, 11)); + $draw = $this->getTestObject(); + $this->assertEquals('2.250000 71.250000 5.250000 66.750000 v' . "\n", $draw->getRawCurveV(3, 5, 7, 11)); } - public function testGetRawCurveY() + public function testGetRawCurveY(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('2.250000 71.250000 5.250000 66.750000 y' . "\n", $testObj->getRawCurveY(3, 5, 7, 11)); + $draw = $this->getTestObject(); + $this->assertEquals('2.250000 71.250000 5.250000 66.750000 y' . "\n", $draw->getRawCurveY(3, 5, 7, 11)); } - public function testGetRawEllipticalArc() + public function testGetRawEllipticalArc(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getRawEllipticalArc(0, 0, 0, 0); + $draw = $this->getTestObject(); + $res = $draw->getRawEllipticalArc(0, 0, 0, 0); $this->assertEquals('', $res); - $res = $testObj->getRawEllipticalArc(3, 5, 7, 11); + $res = $draw->getRawEllipticalArc(3, 5, 7, 11); $this->assertEquals( '7.500000 71.250000 m' . "\n" . '7.500000 73.189135 7.064930 75.067534 6.271733 76.552998 c' . "\n" @@ -103,8 +101,8 @@ public function testGetRawEllipticalArc() $res ); - $bbox = array(); - $res = $testObj->getRawEllipticalArc( + $bbox = []; + $res = $draw->getRawEllipticalArc( 3, 5, 7, @@ -132,9 +130,7 @@ public function testGetRawEllipticalArc() . '2.250000 71.250000 l' . "\n", $res ); - - $bbox = array(); - $res = $testObj->getRawEllipticalArc(3, 5, 7, 11, 0, 90, 45); + $res = $draw->getRawEllipticalArc(3, 5, 7, 11, 0, 90, 45); $this->assertEquals( '2.250000 79.500000 m' . "\n" . '1.084822 79.500000 -0.047846 78.890447 -0.968406 77.767993 c' . "\n" @@ -149,31 +145,31 @@ public function testGetRawEllipticalArc() ); } - public function testGetVectorsAngle() + public function testGetVectorsAngle(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getVectorsAngle(0, 0, 0, 0); + $draw = $this->getTestObject(); + $res = $draw->getVectorsAngle(0, 0, 0, 0); $this->bcAssertEqualsWithDelta(0, $res); - $res = $testObj->getVectorsAngle(0, 1, 0, 1); + $res = $draw->getVectorsAngle(0, 1, 0, 1); $this->bcAssertEqualsWithDelta(0, $res); - $res = $testObj->getVectorsAngle(1, 1, 2, 2); + $res = $draw->getVectorsAngle(1, 1, 2, 2); $this->bcAssertEqualsWithDelta(0, $res); - $res = $testObj->getVectorsAngle(1, 0, 0, 1); + $res = $draw->getVectorsAngle(1, 0, 0, 1); $this->bcAssertEqualsWithDelta(1.57, $res); - $res = $testObj->getVectorsAngle(0, 1, 1, 0); + $res = $draw->getVectorsAngle(0, 1, 1, 0); $this->bcAssertEqualsWithDelta(-1.57, $res); - $res = $testObj->getVectorsAngle(1, 0, 1, 1); + $res = $draw->getVectorsAngle(1, 0, 1, 1); $this->bcAssertEqualsWithDelta(0.79, $res); - $res = $testObj->getVectorsAngle(-1, -1, 1, 1); + $res = $draw->getVectorsAngle(-1, -1, 1, 1); $this->bcAssertEqualsWithDelta(M_PI, $res); - $res = $testObj->getVectorsAngle(1, 0, -1, 0); + $res = $draw->getVectorsAngle(1, 0, -1, 0); $this->bcAssertEqualsWithDelta(M_PI, $res); } } diff --git a/test/StyleTest.php b/test/StyleTest.php index 26a55a2..f8d56aa 100644 --- a/test/StyleTest.php +++ b/test/StyleTest.php @@ -3,35 +3,33 @@ /** * StyleTest.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Style Test * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class StyleTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\Pdf\Graph\Draw { return new \Com\Tecnick\Pdf\Graph\Draw( 1, @@ -43,24 +41,25 @@ protected function getTestObject() ); } - public function testGetStyleCmd() + public function testGetStyleCmd(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); - $res1 = $testObj->getStyleCmd(array()); + $styleCmd = $draw->getStyleCmd(); $exp1 = ''; - $this->assertEquals($exp1, $res1); + $this->assertEquals($exp1, $styleCmd); - $style2 = array( - 'lineWidth' => 3, - 'lineCap' => 'round', - 'lineJoin' => 'bevel', + $style2 = [ + 'lineWidth' => 3, + 'lineCap' => 'round', + 'lineJoin' => 'bevel', 'miterLimit' => 11, - 'dashArray' => array(5, 7), - 'lineColor' => 'greenyellow', - 'fillColor' => '["RGB",0.250000,0.500000,0.750000]', - ); - $res2 = $testObj->getStyleCmd($style2); + 'dashArray' => [5, 7], + 'dashPhase' => 0, + 'lineColor' => 'greenyellow', + 'fillColor' => '["RGB",0.250000,0.500000,0.750000]', + ]; + $res2 = $draw->getStyleCmd($style2); $exp2 = '3.000000 w' . "\n" . '1 J' . "\n" . '2 j' . "\n" @@ -71,11 +70,11 @@ public function testGetStyleCmd() $this->assertEquals($exp2, $res2); } - public function testStyle() + public function testStyle(): void { - $testObj = $this->getTestObject(); - $style = array(); - $res1 = $testObj->add($style, true); + $draw = $this->getTestObject(); + $style = []; + $res1 = $draw->add($style, true); $exp1 = '1.000000 w' . "\n" . '0 J' . "\n" . '0 j' . "\n" @@ -85,17 +84,17 @@ public function testStyle() . '/CS1 cs 1.000000 scn' . "\n"; $this->assertEquals($exp1, $res1); - $style = array( - 'lineWidth' => 3, - 'lineCap' => 'round', - 'lineJoin' => 'bevel', + $style = [ + 'lineWidth' => 3, + 'lineCap' => 'round', + 'lineJoin' => 'bevel', 'miterLimit' => 11, - 'dashArray' => array(5, 7), - 'dashPhase' => 1, - 'lineColor' => 'greenyellow', - 'fillColor' => '["RGB",0.250000,0.500000,0.750000]', - ); - $res2 = $testObj->add($style, false); + 'dashArray' => [5, 7], + 'dashPhase' => 1, + 'lineColor' => 'greenyellow', + 'fillColor' => '["RGB",0.250000,0.500000,0.750000]', + ]; + $res2 = $draw->add($style, false); $exp2 = '3.000000 w' . "\n" . '1 J' . "\n" . '2 j' . "\n" @@ -104,15 +103,15 @@ public function testStyle() . '0.678431 1.000000 0.184314 RG' . "\n" . '0.250000 0.500000 0.750000 rg' . "\n"; $this->assertEquals($exp2, $res2); - $this->assertEquals($style, $testObj->getCurrentStyleArray()); - - $style = array( - 'lineCap' => 'round', - 'lineJoin' => 'bevel', - 'lineColor' => 'transparent', - 'fillColor' => 'cmyk(67,33,0,25)', - ); - $res3 = $testObj->add($style, true); + $this->assertEquals($style, $draw->getCurrentStyleArray()); + + $style = [ + 'lineCap' => 'round', + 'lineJoin' => 'bevel', + 'lineColor' => 'transparent', + 'fillColor' => 'cmyk(67,33,0,25)', + ]; + $res3 = $draw->add($style, true); $exp3 = '3.000000 w' . "\n" . '1 J' . "\n" . '2 j' . "\n" @@ -121,14 +120,14 @@ public function testStyle() . '0.670000 0.330000 0.000000 0.250000 k' . "\n"; $this->assertEquals($exp3, $res3); - $style = array( - 'lineCap' => 'round', - 'lineJoin' => 'bevel', - 'lineColor' => 'transparent', - 'fillColor' => 'cmyk(67,33,0,25)', - 'dashArray' => array(), - ); - $res4 = $testObj->add($style, true); + $style = [ + 'lineCap' => 'round', + 'lineJoin' => 'bevel', + 'lineColor' => 'transparent', + 'fillColor' => 'cmyk(67,33,0,25)', + 'dashArray' => [], + ]; + $res4 = $draw->add($style, true); $exp4 = '3.000000 w' . "\n" . '1 J' . "\n" . '2 j' . "\n" @@ -137,195 +136,269 @@ public function testStyle() . '0.670000 0.330000 0.000000 0.250000 k' . "\n"; $this->assertEquals($exp4, $res4); - $style = array('lineWidth' => 7.123); - $res5 = $testObj->add($style, false); + $style = [ + 'lineWidth' => 7.123, + ]; + $res5 = $draw->add($style, false); $exp5 = '7.123000 w' . "\n"; $this->assertEquals($exp5, $res5); - $res = $testObj->pop(); + $res = $draw->pop(); $this->assertEquals($exp5, $res); - $res = $testObj->pop(); + $res = $draw->pop(); $this->assertEquals($exp4, $res); - $res = $testObj->pop(); + $res = $draw->pop(); $this->assertEquals($exp3, $res); - $res = $testObj->pop(); + $res = $draw->pop(); $this->assertEquals($exp2, $res); - $res = $testObj->pop(); + $res = $draw->pop(); $this->assertEquals($exp1, $res); } - public function testStyleEx() + public function testStyleEx(): void { - $this->bcExpectException('\Com\Tecnick\Pdf\Graph\Exception'); - $testObj = $this->getTestObject(); - $testObj->pop(); + $this->bcExpectException('\\' . \Com\Tecnick\Pdf\Graph\Exception::class); + $draw = $this->getTestObject(); + $draw->pop(); } - public function testSaveRestoreStyle() + public function testSaveRestoreStyle(): void { - $testObj = $this->getTestObject(); - $testObj->add(array('lineWidth' => 1), false); - $testObj->add(array('lineWidth' => 2), false); - $testObj->add(array('lineWidth' => 3), false); - $testObj->saveStyleStatus(); - $testObj->add(array('lineWidth' => 4), false); - $testObj->add(array('lineWidth' => 5), false); - $testObj->add(array('lineWidth' => 6), false); - $this->assertEquals(array('lineWidth' => 6), $testObj->getCurrentStyleArray()); - $testObj->restoreStyleStatus(); - $this->assertEquals(array('lineWidth' => 3), $testObj->getCurrentStyleArray()); + $draw = $this->getTestObject(); + $draw->add( + [ + 'lineWidth' => 1, + ], + false + ); + $draw->add( + [ + 'lineWidth' => 2, + ], + false + ); + $draw->add( + [ + 'lineWidth' => 3, + ], + false + ); + $draw->saveStyleStatus(); + $draw->add( + [ + 'lineWidth' => 4, + ], + false + ); + $draw->add( + [ + 'lineWidth' => 5, + ], + false + ); + $draw->add( + [ + 'lineWidth' => 6, + ], + false + ); + $this->assertEquals( + [ + 'lineWidth' => 6, + ], + $draw->getCurrentStyleArray() + ); + $draw->restoreStyleStatus(); + $this->assertEquals( + [ + 'lineWidth' => 3, + ], + $draw->getCurrentStyleArray() + ); } - public function testStyleItem() + public function testStyleItem(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getCurrentStyleItem('lineCap'); + $draw = $this->getTestObject(); + $res = $draw->getCurrentStyleItem('lineCap'); $this->assertEquals('butt', $res); } - public function testStyleItemEx() + public function testStyleItemEx(): void { - $this->bcExpectException('\Com\Tecnick\Pdf\Graph\Exception'); - $testObj = $this->getTestObject(); - $testObj->getCurrentStyleItem('wrongField'); + $this->bcExpectException('\\' . \Com\Tecnick\Pdf\Graph\Exception::class); + $draw = $this->getTestObject(); + $draw->getCurrentStyleItem('wrongField'); } - public function testGetLastStyleProperty() + public function testGetLastStyleProperty(): void { - $testObj = $this->getTestObject(); - $testObj->add(array('lineWidth' => 1), false); - $testObj->add(array('lineWidth' => 2), false); - $testObj->add(array('lineWidth' => 3), false); - $this->assertEquals(3, $testObj->getLastStyleProperty('lineWidth', 0)); - $testObj->add(array('lineWidth' => 4), false); - $this->assertEquals(4, $testObj->getLastStyleProperty('lineWidth', 0)); - $this->assertEquals(7, $testObj->getLastStyleProperty('unknown', 7)); + $draw = $this->getTestObject(); + $draw->add( + [ + 'lineWidth' => 1, + ], + false + ); + $draw->add( + [ + 'lineWidth' => 2, + ], + false + ); + $draw->add( + [ + 'lineWidth' => 3, + ], + false + ); + $this->assertEquals(3, $draw->getLastStyleProperty('lineWidth', 0)); + $draw->add( + [ + 'lineWidth' => 4, + ], + false + ); + $this->assertEquals(4, $draw->getLastStyleProperty('lineWidth', 0)); + $this->assertEquals(7, $draw->getLastStyleProperty('unknown', 7)); } - public function testGetPathPaintOp() + public function testGetPathPaintOp(): void { - $testObj = $this->getTestObject(); - $res = $testObj->getPathPaintOp('', ''); + $draw = $this->getTestObject(); + $res = $draw->getPathPaintOp('', ''); $this->assertEquals('', $res); - $res = $testObj->getPathPaintOp(''); + $res = $draw->getPathPaintOp(''); $this->assertEquals('S' . "\n", $res); - $res = $testObj->getPathPaintOp('', 'df'); + $res = $draw->getPathPaintOp('', 'df'); $this->assertEquals('b' . "\n", $res); - $res = $testObj->getPathPaintOp('CEO'); + $res = $draw->getPathPaintOp('CEO'); $this->assertEquals('W* n' . "\n", $res); - $res = $testObj->getPathPaintOp('F*D'); + $res = $draw->getPathPaintOp('F*D'); $this->assertEquals('B*' . "\n", $res); } - public function testIsFillingMode() + public function testIsFillingMode(): void { - $testObj = $this->getTestObject(); - $this->assertTrue($testObj->isFillingMode('f')); - $this->assertTrue($testObj->isFillingMode('f*')); - $this->assertTrue($testObj->isFillingMode('B')); - $this->assertTrue($testObj->isFillingMode('B*')); - $this->assertTrue($testObj->isFillingMode('b')); - $this->assertTrue($testObj->isFillingMode('b*')); - $this->assertFalse($testObj->isFillingMode('S')); - $this->assertFalse($testObj->isFillingMode('s')); - $this->assertFalse($testObj->isFillingMode('n')); - $this->assertFalse($testObj->isFillingMode('')); + $draw = $this->getTestObject(); + $this->assertTrue($draw->isFillingMode('f')); + $this->assertTrue($draw->isFillingMode('f*')); + $this->assertTrue($draw->isFillingMode('B')); + $this->assertTrue($draw->isFillingMode('B*')); + $this->assertTrue($draw->isFillingMode('b')); + $this->assertTrue($draw->isFillingMode('b*')); + $this->assertFalse($draw->isFillingMode('S')); + $this->assertFalse($draw->isFillingMode('s')); + $this->assertFalse($draw->isFillingMode('n')); + $this->assertFalse($draw->isFillingMode('')); } - public function testIsStrokingMode() + public function testIsStrokingMode(): void { - $testObj = $this->getTestObject(); - $this->assertTrue($testObj->isStrokingMode('S')); - $this->assertTrue($testObj->isStrokingMode('s')); - $this->assertTrue($testObj->isStrokingMode('B')); - $this->assertTrue($testObj->isStrokingMode('B*')); - $this->assertTrue($testObj->isStrokingMode('b')); - $this->assertTrue($testObj->isStrokingMode('b*')); - $this->assertFalse($testObj->isStrokingMode('f')); - $this->assertFalse($testObj->isStrokingMode('f*')); - $this->assertFalse($testObj->isStrokingMode('n')); - $this->assertFalse($testObj->isStrokingMode('')); + $draw = $this->getTestObject(); + $this->assertTrue($draw->isStrokingMode('S')); + $this->assertTrue($draw->isStrokingMode('s')); + $this->assertTrue($draw->isStrokingMode('B')); + $this->assertTrue($draw->isStrokingMode('B*')); + $this->assertTrue($draw->isStrokingMode('b')); + $this->assertTrue($draw->isStrokingMode('b*')); + $this->assertFalse($draw->isStrokingMode('f')); + $this->assertFalse($draw->isStrokingMode('f*')); + $this->assertFalse($draw->isStrokingMode('n')); + $this->assertFalse($draw->isStrokingMode('')); } - public function testIsClosingMode() + public function testIsClosingMode(): void { - $testObj = $this->getTestObject(); - $this->assertTrue($testObj->isClosingMode('s')); - $this->assertTrue($testObj->isClosingMode('b')); - $this->assertTrue($testObj->isClosingMode('b*')); - $this->assertFalse($testObj->isClosingMode('f')); - $this->assertFalse($testObj->isClosingMode('f*')); - $this->assertFalse($testObj->isClosingMode('S')); - $this->assertFalse($testObj->isClosingMode('B')); - $this->assertFalse($testObj->isClosingMode('B*')); - $this->assertFalse($testObj->isClosingMode('n')); - $this->assertFalse($testObj->isClosingMode('')); + $draw = $this->getTestObject(); + $this->assertTrue($draw->isClosingMode('s')); + $this->assertTrue($draw->isClosingMode('b')); + $this->assertTrue($draw->isClosingMode('b*')); + $this->assertFalse($draw->isClosingMode('f')); + $this->assertFalse($draw->isClosingMode('f*')); + $this->assertFalse($draw->isClosingMode('S')); + $this->assertFalse($draw->isClosingMode('B')); + $this->assertFalse($draw->isClosingMode('B*')); + $this->assertFalse($draw->isClosingMode('n')); + $this->assertFalse($draw->isClosingMode('')); } - public function testGetModeWithoutClose() + public function testGetModeWithoutClose(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('', $testObj->getModeWithoutClose('')); - $this->assertEquals('S', $testObj->getModeWithoutClose('s')); - $this->assertEquals('B', $testObj->getModeWithoutClose('b')); - $this->assertEquals('B*', $testObj->getModeWithoutClose('b*')); - $this->assertEquals('n', $testObj->getModeWithoutClose('n')); + $draw = $this->getTestObject(); + $this->assertEquals('', $draw->getModeWithoutClose('')); + $this->assertEquals('S', $draw->getModeWithoutClose('s')); + $this->assertEquals('B', $draw->getModeWithoutClose('b')); + $this->assertEquals('B*', $draw->getModeWithoutClose('b*')); + $this->assertEquals('n', $draw->getModeWithoutClose('n')); } - public function testGetModeWithoutFill() + public function testGetModeWithoutFill(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('', $testObj->getModeWithoutFill('')); - $this->assertEquals('', $testObj->getModeWithoutFill('f')); - $this->assertEquals('', $testObj->getModeWithoutFill('f*')); - $this->assertEquals('S', $testObj->getModeWithoutFill('B')); - $this->assertEquals('S', $testObj->getModeWithoutFill('B*')); - $this->assertEquals('s', $testObj->getModeWithoutFill('b')); - $this->assertEquals('s', $testObj->getModeWithoutFill('b*')); - $this->assertEquals('n', $testObj->getModeWithoutFill('n')); + $draw = $this->getTestObject(); + $this->assertEquals('', $draw->getModeWithoutFill('')); + $this->assertEquals('', $draw->getModeWithoutFill('f')); + $this->assertEquals('', $draw->getModeWithoutFill('f*')); + $this->assertEquals('S', $draw->getModeWithoutFill('B')); + $this->assertEquals('S', $draw->getModeWithoutFill('B*')); + $this->assertEquals('s', $draw->getModeWithoutFill('b')); + $this->assertEquals('s', $draw->getModeWithoutFill('b*')); + $this->assertEquals('n', $draw->getModeWithoutFill('n')); } - public function testGetModeWithoutStroke() + public function testGetModeWithoutStroke(): void { - $testObj = $this->getTestObject(); - $this->assertEquals('', $testObj->getModeWithoutStroke('')); - $this->assertEquals('', $testObj->getModeWithoutStroke('S')); - $this->assertEquals('h', $testObj->getModeWithoutStroke('s')); - $this->assertEquals('f', $testObj->getModeWithoutStroke('B')); - $this->assertEquals('f*', $testObj->getModeWithoutStroke('B*')); - $this->assertEquals('h f', $testObj->getModeWithoutStroke('b')); - $this->assertEquals('h f*', $testObj->getModeWithoutStroke('b*')); - $this->assertEquals('n', $testObj->getModeWithoutStroke('n')); + $draw = $this->getTestObject(); + $this->assertEquals('', $draw->getModeWithoutStroke('')); + $this->assertEquals('', $draw->getModeWithoutStroke('S')); + $this->assertEquals('h', $draw->getModeWithoutStroke('s')); + $this->assertEquals('f', $draw->getModeWithoutStroke('B')); + $this->assertEquals('f*', $draw->getModeWithoutStroke('B*')); + $this->assertEquals('h f', $draw->getModeWithoutStroke('b')); + $this->assertEquals('h f*', $draw->getModeWithoutStroke('b*')); + $this->assertEquals('n', $draw->getModeWithoutStroke('n')); } - public function testGetExtGState() + public function testGetExtGState(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '/GS1 gs' . "\n", - $testObj->getExtGState(array('A' => 'B')) + $draw->getExtGState( + [ + 'A' => 'B', + ] + ) ); $this->assertEquals( '/GS1 gs' . "\n", - $testObj->getExtGState(array('A' => 'B')) + $draw->getExtGState( + [ + 'A' => 'B', + ] + ) ); $this->assertEquals( '/GS2 gs' . "\n", - $testObj->getExtGState(array('C' => 'D')) + $draw->getExtGState( + [ + 'C' => 'D', + ] + ) ); } - public function testGetExtGStatePdfa() + public function testGetExtGStatePdfa(): void { - $obj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 1, 0, 0, @@ -335,7 +408,11 @@ public function testGetExtGStatePdfa() ); $this->assertEquals( '', - $obj->getExtGState(array('A' => 'B')) + $draw->getExtGState( + [ + 'A' => 'B', + ] + ) ); } } diff --git a/test/TestUtil.php b/test/TestUtil.php index 45618a8..e542eae 100644 --- a/test/TestUtil.php +++ b/test/TestUtil.php @@ -3,13 +3,13 @@ /** * TestUtil.php * - * @since 2020-12-19 - * @category Library - * @package file - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2020-12-19 + * @category Library + * @package file + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file * * This file is part of tc-lib-file software library. */ @@ -21,33 +21,30 @@ /** * Test Util * - * @since 2020-12-19 - * @category Library - * @package file - * @author Nicola Asuni - * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-file + * @since 2020-12-19 + * @category Library + * @package file + * @author Nicola Asuni + * @copyright 2015-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-file */ class TestUtil extends TestCase { - public function bcAssertEqualsWithDelta($expected, $actual, $delta = 0.01, $message = '') - { - if (\is_callable([self::class, 'assertEqualsWithDelta'])) { - parent::assertEqualsWithDelta($expected, $actual, $delta, $message); - return; - } - /* @phpstan-ignore-next-line */ - $this->assertEquals($expected, $actual, $message, $delta); + public function bcAssertEqualsWithDelta( + mixed $expected, + mixed $actual, + float $delta = 0.01, + string $message = '' + ): void { + parent::assertEqualsWithDelta($expected, $actual, $delta, $message); } - public function bcExpectException($exception) + /** + * @param class-string<\Throwable> $exception + */ + public function bcExpectException($exception): void { - if (\is_callable([self::class, 'expectException'])) { - parent::expectException($exception); - return; - } - /* @phpstan-ignore-next-line */ - parent::setExpectedException($exception); + parent::expectException($exception); } } diff --git a/test/TransformTest.php b/test/TransformTest.php index 9421174..6bd0531 100644 --- a/test/TransformTest.php +++ b/test/TransformTest.php @@ -3,37 +3,35 @@ /** * TransformTest.php * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph * * This file is part of tc-lib-pdf-graph software library. */ namespace Test; -use PHPUnit\Framework\TestCase; - /** * Transform Test * - * @since 2011-05-23 - * @category Library - * @package PdfGraph - * @author Nicola Asuni - * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD - * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) - * @link https://github.com/tecnickcom/tc-lib-pdf-graph + * @since 2011-05-23 + * @category Library + * @package PdfGraph + * @author Nicola Asuni + * @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD + * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) + * @link https://github.com/tecnickcom/tc-lib-pdf-graph */ class TransformTest extends TestUtil { - protected function getTestObject() + protected function getTestObject(): \Com\Tecnick\Pdf\Graph\Draw { - $testObj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 1, 0, 0, @@ -41,14 +39,14 @@ protected function getTestObject() new \Com\Tecnick\Pdf\Encrypt\Encrypt(), false ); - $this->assertEquals(-1, $testObj->getTransformIndex()); - $this->assertEquals('q' . "\n", $testObj->getStartTransform()); - return $testObj; + $this->assertEquals(-1, $draw->getTransformIndex()); + $this->assertEquals('q' . "\n", $draw->getStartTransform()); + return $draw; } - public function testGetStartStopTransform() + public function testGetStartStopTransform(): void { - $obj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 1, 0, 0, @@ -56,43 +54,46 @@ public function testGetStartStopTransform() new \Com\Tecnick\Pdf\Encrypt\Encrypt(), false ); - $this->assertEquals(-1, $obj->getTransformIndex()); - $this->assertEquals('q' . "\n", $obj->getStartTransform()); - $this->assertEquals(0, $obj->getTransformIndex()); + $this->assertEquals(-1, $draw->getTransformIndex()); + $this->assertEquals('q' . "\n", $draw->getStartTransform()); + $this->assertEquals(0, $draw->getTransformIndex()); - $tmx = array(0.1, 1.2, 2.3, 3.4, 4.5, 5.6); + $tmx = [0.1, 1.2, 2.3, 3.4, 4.5, 5.6]; $this->assertEquals( '0.100000 1.200000 2.300000 3.400000 4.500000 5.600000 cm' . "\n", - $obj->getTransformation($tmx) + $draw->getTransformation($tmx) ); $this->bcAssertEqualsWithDelta( - array(0 => array(0 => array(0.1, 1.2, 2.3, 3.4, 4.5, 5.6))), - $obj->getTransformStack(), + [ + 0 => [ + 0 => [0.1, 1.2, 2.3, 3.4, 4.5, 5.6], + ], + ], + $draw->getTransformStack(), 0.0001, '' ); - $this->assertEquals('Q' . "\n", $obj->getStopTransform()); - $this->assertEquals(-1, $obj->getTransformIndex()); - $this->assertEquals('', $obj->getStopTransform()); - $this->assertEquals(-1, $obj->getTransformIndex()); + $this->assertEquals('Q' . "\n", $draw->getStopTransform()); + $this->assertEquals(-1, $draw->getTransformIndex()); + $this->assertEquals('', $draw->getStopTransform()); + $this->assertEquals(-1, $draw->getTransformIndex()); } - - public function testGetTransform() + public function testGetTransform(): void { - $testObj = $this->getTestObject(); - $tmx = array(0.1, 1.2, 2.3, 3.4, 4.5, 5.6); + $draw = $this->getTestObject(); + $tmx = [0.1, 1.2, 2.3, 3.4, 4.5, 5.6]; $this->assertEquals( '0.100000 1.200000 2.300000 3.400000 4.500000 5.600000 cm' . "\n", - $testObj->getTransformation($tmx) + $draw->getTransformation($tmx) ); } - public function testSetPageHeight() + public function testSetPageHeight(): void { - $obj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 1, 0, 0, @@ -100,17 +101,17 @@ public function testSetPageHeight() new \Com\Tecnick\Pdf\Encrypt\Encrypt(), false ); - $obj->setPageHeight(100); - $this->assertEquals('q' . "\n", $obj->getStartTransform()); + $draw->setPageHeight(100); + $this->assertEquals('q' . "\n", $draw->getStartTransform()); $this->assertEquals( '3.000000 0.000000 0.000000 5.000000 -14.000000 -356.000000 cm' . "\n", - $obj->getScaling(3, 5, 7, 11) + $draw->getScaling(3, 5, 7, 11) ); } - public function testSetKUnit() + public function testSetKUnit(): void { - $obj = new \Com\Tecnick\Pdf\Graph\Draw( + $draw = new \Com\Tecnick\Pdf\Graph\Draw( 1, 0, 0, @@ -118,174 +119,174 @@ public function testSetKUnit() new \Com\Tecnick\Pdf\Encrypt\Encrypt(), false ); - $obj->setKUnit(0.75); - $this->assertEquals('q' . "\n", $obj->getStartTransform()); + $draw->setKUnit(0.75); + $this->assertEquals('q' . "\n", $draw->getStartTransform()); $this->assertEquals( '3.000000 0.000000 0.000000 5.000000 -10.500000 33.000000 cm' . "\n", - $obj->getScaling(3, 5, 7, 11) + $draw->getScaling(3, 5, 7, 11) ); } - public function testGetScaling() + public function testGetScaling(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '3.000000 0.000000 0.000000 5.000000 -14.000000 44.000000 cm' . "\n", - $testObj->getScaling(3, 5, 7, 11) + $draw->getScaling(3, 5, 7, 11) ); $this->assertEquals( '3.000000 0.000000 0.000000 3.000000 -14.000000 22.000000 cm' . "\n", - $testObj->getScaling(3, 3, 7, 11) + $draw->getScaling(3, 3, 7, 11) ); } - public function testGetScalingEx() + public function testGetScalingEx(): void { - $this->bcExpectException('\Com\Tecnick\Pdf\Graph\Exception'); - $testObj = $this->getTestObject(); - $testObj->getScaling(0, 0, 7, 11); + $this->bcExpectException('\\' . \Com\Tecnick\Pdf\Graph\Exception::class); + $draw = $this->getTestObject(); + $draw->getScaling(0, 0, 7, 11); } - public function testGetHorizScaling() + public function testGetHorizScaling(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '3.000000 0.000000 0.000000 1.000000 -14.000000 0.000000 cm' . "\n", - $testObj->getHorizScaling(3, 7, 11) + $draw->getHorizScaling(3, 7, 11) ); } - public function testGetVertScaling() + public function testGetVertScaling(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.000000 0.000000 5.000000 0.000000 44.000000 cm' . "\n", - $testObj->getVertScaling(5, 7, 11) + $draw->getVertScaling(5, 7, 11) ); } - public function testGetPropScaling() + public function testGetPropScaling(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '3.000000 0.000000 0.000000 3.000000 -14.000000 22.000000 cm' . "\n", - $testObj->getPropScaling(3, 7, 11) + $draw->getPropScaling(3, 7, 11) ); } - public function testGetRotation() + public function testGetRotation(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '0.707107 0.707107 -0.707107 0.707107 -5.727922 -8.171573 cm' . "\n", - $testObj->getRotation(45, 7, 11) + $draw->getRotation(45, 7, 11) ); } - public function testGetHorizMirroring() + public function testGetHorizMirroring(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '-1.000000 0.000000 0.000000 1.000000 14.000000 0.000000 cm' . "\n", - $testObj->getHorizMirroring(7) + $draw->getHorizMirroring(7) ); } - public function testGetVertMirroring() + public function testGetVertMirroring(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.000000 0.000000 -1.000000 0.000000 -22.000000 cm' . "\n", - $testObj->getVertMirroring(11) + $draw->getVertMirroring(11) ); } - public function testGetPointMirroring() + public function testGetPointMirroring(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '-1.000000 0.000000 0.000000 -1.000000 14.000000 -22.000000 cm' . "\n", - $testObj->getPointMirroring(7, 11) + $draw->getPointMirroring(7, 11) ); } - public function testGetReflection() + public function testGetReflection(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '-1.000000 0.000000 0.000000 1.000000 14.000000 0.000000 cm' . "\n" . '0.000000 1.000000 -1.000000 0.000000 -4.000000 -18.000000 cm' . "\n", - $testObj->getReflection(45, 7, 11) + $draw->getReflection(45, 7, 11) ); } - public function testGetTranslation() + public function testGetTranslation(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.000000 0.000000 1.000000 3.000000 -5.000000 cm' . "\n", - $testObj->getTranslation(3, 5) + $draw->getTranslation(3, 5) ); } - public function testGetHorizTranslation() + public function testGetHorizTranslation(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.000000 0.000000 1.000000 3.000000 0.000000 cm' . "\n", - $testObj->getHorizTranslation(3) + $draw->getHorizTranslation(3) ); } - public function testGetVertTranslation() + public function testGetVertTranslation(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.000000 0.000000 1.000000 0.000000 -5.000000 cm' . "\n", - $testObj->getVertTranslation(5) + $draw->getVertTranslation(5) ); } - public function testGetSkewing() + public function testGetSkewing(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.087489 0.052408 1.000000 0.576486 -0.612421 cm' . "\n", - $testObj->getSkewing(3, 5, 7, 11) + $draw->getSkewing(3, 5, 7, 11) ); } - public function testGetSkewingEx() + public function testGetSkewingEx(): void { - $testObj = $this->getTestObject(); - $this->bcExpectException('\Com\Tecnick\Pdf\Graph\Exception'); - $testObj->getSkewing(90, -90, 7, 11); + $draw = $this->getTestObject(); + $this->bcExpectException('\\' . \Com\Tecnick\Pdf\Graph\Exception::class); + $draw->getSkewing(90, -90, 7, 11); } - public function testGetHorizSkewing() + public function testGetHorizSkewing(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.000000 0.052408 1.000000 0.576486 0.000000 cm' . "\n", - $testObj->getHorizSkewing(3, 7, 11) + $draw->getHorizSkewing(3, 7, 11) ); } - public function testGetVertSkewing() + public function testGetVertSkewing(): void { - $testObj = $this->getTestObject(); + $draw = $this->getTestObject(); $this->assertEquals( '1.000000 0.087489 0.000000 1.000000 0.000000 -0.612421 cm' . "\n", - $testObj->getVertSkewing(5, 7, 11) + $draw->getVertSkewing(5, 7, 11) ); } - public function testGetCtmProduct() + public function testGetCtmProduct(): void { - $testObj = $this->getTestObject(); - $tma = array(3.1, 5.2, 7.3, 11.4, 13.5, 17.6); - $tmb = array(19.1, 23.2, 29.3, 31.4, 37.5, 41.6); - $ctm = $testObj->getCtmProduct($tma, $tmb); - $this->bcAssertEqualsWithDelta(array(228.570, 363.800, 320.050, 510.320, 433.430, 686.840), $ctm, 0.001); + $draw = $this->getTestObject(); + $tma = [3.1, 5.2, 7.3, 11.4, 13.5, 17.6]; + $tmb = [19.1, 23.2, 29.3, 31.4, 37.5, 41.6]; + $ctm = $draw->getCtmProduct($tma, $tmb); + $this->bcAssertEqualsWithDelta([228.570, 363.800, 320.050, 510.320, 433.430, 686.840], $ctm, 0.001); } }