Skip to content

Commit

Permalink
New methods for XOBjects support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Aug 17, 2024
1 parent dcd4f57 commit c24766f
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.13
2.1.0
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": ">=8.0",
"ext-zlib": "*",
"tecnickcom/tc-lib-color": "^2.0",
"tecnickcom/tc-lib-color": "^2.2",
"tecnickcom/tc-lib-pdf-encrypt": "^2.1"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion resources/debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git
Package: ~#PKGNAME#~
Provides: php-~#PROJECT#~
Architecture: all
Depends: php (>= 8.0.0), php-zip, php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.0.9), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.1.3), ${misc:Depends}
Depends: php (>= 8.0.0), php-zip, php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.2.1), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.1.3), ${misc:Depends}
Description: PHP PDF Graph Library
PHP library containing PDF graphic and geometric methods.
2 changes: 1 addition & 1 deletion resources/rpm/rpm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BuildArch: noarch
Requires: php(language) >= 8.0.0
Requires: php-zlib
Requires: php-composer(%{c_vendor}/tc-lib-color) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.0.9
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.2.1
Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) >= 2.1.3

Expand Down
99 changes: 92 additions & 7 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,31 @@ public function getOutExtGState(int $pon): string
}

/**
* Get the PDF output string for ExtGState Resource Dictionary
* Returns the last extgstate ID to be used with XOBjects.
*
* @return ?int
*/
public function getLastExtGStateID(): ?int
{
return array_key_last($this->extgstates);
}

/**
* Get the PDF output string for ExtGState Resource Dictionary.
*
* @param array<int, array{'name': string, 'n': int}> $data extgstates data.
*
* @return string PDF command
*/
public function getOutExtGStateResources(): string
private function getOutExtGStateResDict(array $data): string
{
if ($this->pdfa || $this->extgstates === []) {
return '';
}

$out = ' /ExtGState <<';
foreach ($this->extgstates as $key => $ext) {

foreach ($data as $key => $ext) {
if (! empty($ext['name'])) {
$out .= ' /' . $ext['name'];
} else {
Expand All @@ -303,19 +316,56 @@ public function getOutExtGStateResources(): string
}

/**
* Get the PDF output string for Gradients Resource Dictionary
* Get the PDF output string for ExtGState Resource Dictionary
*
* @return string PDF command
*/
public function getOutGradientResources(): string
public function getOutExtGStateResources(): string
{
if ($this->pdfa || $this->gradients === []) {
return $this->getOutExtGStateResDict($this->extgstates);
}

/**
* Get the PDF output string for ExtGState Resource Dictionary for XOBjects.
*
* @param array<int> $keys Array of extgstates keys.
*
* @return string PDF command
*/
public function getOutExtGStateResourcesByKeys(array $keys): string
{
if (empty($keys)) {
return '';
}

$data = [];
foreach ($keys as $key) {
$data[$key] = [
'name' => $this->extgstates[$key]['name'],
'n' => $this->extgstates[$key]['n'],
];
}

return $this->getOutExtGStateResDict($data);
}

/**
* Get the PDF output string for Gradients Resource Dictionary.
*
* @param array<int, array{'id': int, 'pattern': int}> $data gradients data.
*
* @return string PDF command
*/
private function getOutGradientResDict(array $data): string
{
if ($this->pdfa || empty($data)) {
return '';
}

$grp = '';
$grs = '';
foreach ($this->gradients as $idx => $grad) {

foreach ($data as $idx => $grad) {
// gradient patterns
$grp .= ' /p' . $idx . ' ' . $grad['pattern'] . ' 0 R';
// gradient shadings
Expand All @@ -326,6 +376,41 @@ public function getOutGradientResources(): string
. ' /Shading <<' . $grs . ' >>' . "\n";
}

/**
* Get the PDF output string for Gradients Resource Dictionary
*
* @return string PDF command
*/
public function getOutGradientResources(): string
{
return $this->getOutGradientResDict($this->gradients);
}


/**
* Returns the PDF command to output gradient resources.
*
* @param array<int> $keys Array of gradient keys.
*
* @return string PDF command
*/
public function getOutGradientResourcesByKeys(array $keys): string
{
if (empty($keys)) {
return '';
}

$data = [];
foreach ($keys as $key) {
$data[$key] = [
'id' => $this->gradients[$key]['id'],
'pattern' => $this->gradients[$key]['pattern'],
];
}

return $this->getOutGradientResDict($data);
}

/**
* Get the PDF output string for gradient colors and transparency
*
Expand Down
12 changes: 11 additions & 1 deletion src/Gradient.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function getGradientTransform(float $posx, float $posy, float $width, flo
}

/**
* Get a color gradient command.
* Get a color gradient PDF command.
*
* @param int $type Type of gradient (Not all types are currently supported):
* 1 = Function-based shading; 2 = Axial shading; 3 = Radial
Expand Down Expand Up @@ -319,6 +319,16 @@ public function getGradient(
return $out;
}

/**
* Returns the last gradient ID to be used with XOBjects.
*
* @return ?int
*/
public function getLastGradientID(): ?int
{
return array_key_last($this->gradients);
}

/**
* Process the gradient stops.
*
Expand Down
15 changes: 15 additions & 0 deletions test/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function testGetOutExtGState(): void
);

$this->assertEquals(12, $draw->getObjectNumber());

$this->assertEquals(2, $draw->getLastExtGStateID());
}

/**
Expand Down Expand Up @@ -106,6 +108,13 @@ public function testGetOutGradientShaders(): void
. ' /Shading << /Sh1 12 0 R >>' . "\n",
$res
);

$resx = $draw->getOutGradientResourcesByKeys([1]);
$this->assertEquals(
' /Pattern << /p1 13 0 R >>' . "\n"
. ' /Shading << /Sh1 12 0 R >>' . "\n",
$resx
);
}

public function testGetOutShaders(): void
Expand Down Expand Up @@ -158,6 +167,12 @@ public function testGetOutShaders(): void
' /ExtGState << /GS1 1 0 R /GS2 2 0 R /TGS1 20 0 R >>' . "\n",
$res
);

$resx = $draw->getOutExtGStateResourcesByKeys([1,2]);
$this->assertEquals(
' /ExtGState << /GS1 1 0 R /GS2 2 0 R >>' . "\n",
$resx
);
}

public function testGetOutShadersRadial(): void
Expand Down
4 changes: 4 additions & 0 deletions test/GradientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function testGetGradientPDFA(): void
'',
$draw->getGradient(2, [], [], '', false)
);

$this->assertNull($draw->getLastGradientID());
}

public function testGetGradient(): void
Expand Down Expand Up @@ -193,6 +195,8 @@ public function testGetGradient(): void
],
];
$this->bcAssertEqualsWithDelta($exp, $draw->getGradientsArray());

$this->assertEquals(1, $draw->getLastGradientID());
}

public function testGetCoonsPatchMeshPDFA(): void
Expand Down

0 comments on commit c24766f

Please sign in to comment.