Skip to content
forked from psliwa/PHPPdf

Commit

Permalink
Merge pull request #4 from jobcloud/feat/BE-2692/adjustments
Browse files Browse the repository at this point in the history
feat(BE-2692): adjustments
  • Loading branch information
calinblaga authored Jul 14, 2022
2 parents 1229b46 + 080ad84 commit 693c589
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 103 deletions.
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
],
"require": {
"zendframework/zendpdf": "~2.0.0",
"laminas/laminas-cache": "^2.0",
"laminas/laminas-zendframework-bridge": "^1.1"
"laminas/laminas-cache": "^2.11.3"
},
"require-dev": {
"laminas/laminas-barcode": "^2.0",
"laminas/laminas-validator": "^2.0",
"imagine/imagine": ">=0.2.0,<0.6.0",
"laminas/laminas-barcode": "~2.8.0",
"laminas/laminas-validator": "~2.10.0",
"imagine/Imagine": ">=0.2.0,<0.6.0",
"phpunit/phpunit": ">=4,<5.4.0"
},
"provide": {
"laminas/laminas-cache-storage-implementation": "1.0"
},
"suggest": {
"laminas/laminas-barcode": "If you want to use barcodes",
"laminas/laminas-validator": "If you want to use barcodes (required by zend-barcode)",
Expand Down
16 changes: 8 additions & 8 deletions lib/PHPPdf/Cache/CacheImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace PHPPdf\Cache;

use Zend\Cache\StorageFactory;
use Zend\Cache\Storage\StorageInterface;
use Laminas\Cache\StorageFactory;
use Laminas\Cache\Storage\StorageInterface;
use PHPPdf\Exception\RuntimeException;

/**
Expand All @@ -25,7 +25,7 @@ class CacheImpl implements Cache
const ENGINE_FILESYSTEM = 'Filesystem';

/**
* @var Zend\Cache\Storage\Adapter
* @var Laminas\Cache\Storage\Adapter
*/
private $adapter;

Expand Down Expand Up @@ -93,7 +93,7 @@ public function load($id)

return $data;
}
catch(\Zend\Cache\Exception\ExceptionInterface $e)
catch(\Laminas\Cache\Exception\ExceptionInterface $e)
{
$this->wrapLowLevelException($e, __METHOD__);
}
Expand All @@ -110,7 +110,7 @@ public function test($id)
{
return $this->adapter->hasItem($id);
}
catch(\Zend\Cache\Exception\ExceptionInterface $e)
catch(\Laminas\Cache\Exception\ExceptionInterface $e)
{
$this->wrapLowLevelException($e, __METHOD__);
}
Expand All @@ -133,7 +133,7 @@ public function save($data, $id)

return $this->adapter->setItem($id, $data);
}
catch(\Zend\Cache\Exception\ExceptionInterface $e)
catch(\Laminas\Cache\Exception\ExceptionInterface $e)
{
$this->wrapLowLevelException($e, __METHOD__);
}
Expand All @@ -145,9 +145,9 @@ public function remove($id)
{
return $this->adapter->removeItem($id);
}
catch(\Zend\Cache\Exception\ExceptionInterface $e)
catch(\Laminas\Cache\Exception\ExceptionInterface $e)
{
$this->wrapLowLevelException($e, __METHOD__);
}
}
}
}
12 changes: 8 additions & 4 deletions lib/PHPPdf/Core/ComplexAttribute/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace PHPPdf\Core\ComplexAttribute;

use PHPPdf\Exception\InvalidArgumentException;
use PHPPdf\Exception\InvalidArgumentException;

use PHPPdf\Core\Node\Page;
use PHPPdf\Core\UnitConverter;
Expand Down Expand Up @@ -196,7 +196,9 @@ private function getXCoord(Node $node, $width, $x, UnitConverter $converter)
case self::POSITION_LEFT:
return $x;
default:
return $x + $converter->convertUnit($converter->convertPercentageValue($this->positionX, $node->getWidth()));
$value = $converter->convertUnit($converter->convertPercentageValue($this->positionX, $node->getWidth()));
$value = str_replace(['px', 'pt', '%'], '', $value);
return $x + $value;
}
}

Expand All @@ -213,7 +215,9 @@ private function getYCoord(Node $node, $height, $y, UnitConverter $converter)
case self::POSITION_TOP:
return $y;
default:
return $y - $converter->convertUnit($converter->convertPercentageValue($this->positionY, $node->getHeight()));
$value = $converter->convertUnit($converter->convertPercentageValue($this->positionY, $node->getHeight()));
$value = str_replace(['px', 'pt', '%'], '', $value);
return $y - $value;
}
}

Expand Down Expand Up @@ -304,4 +308,4 @@ private function drawCircleBackground(GraphicsContext $gc, Node $node, Document

$this->drawCircle($gc, $node->getAttribute('radius'), $point->getX(), $point->getY(), GraphicsContext::SHAPE_DRAW_FILL);
}
}
}
3 changes: 1 addition & 2 deletions lib/PHPPdf/Core/ComplexAttribute/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace PHPPdf\Core\ComplexAttribute;

use Zend\Code\Generator\DocBlockGenerator;

use PHPPdf\Core\Node\Page,
PHPPdf\Core\Node\Node,
Expand Down Expand Up @@ -261,4 +260,4 @@ public function isEmpty()
{
return $this->getType() === self::TYPE_NONE;
}
}
}
4 changes: 2 additions & 2 deletions lib/PHPPdf/Core/Engine/AbstractGraphicsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace PHPPdf\Core\Engine;

use Zend\Barcode\Object\ObjectInterface as Barcode;
use Laminas\Barcode\Object\ObjectInterface as Barcode;

/**
* Base class for GraphicsContext classes.
Expand Down Expand Up @@ -177,4 +177,4 @@ public function drawArc($x, $y, $width, $height, $start, $end, $fillType = self:
}

abstract protected function doDrawArc($x, $y, $width, $height, $start, $end, $fillType);
}
}
4 changes: 2 additions & 2 deletions lib/PHPPdf/Core/Engine/GraphicsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace PHPPdf\Core\Engine;

use Zend\Barcode\Object\ObjectInterface;
use Laminas\Barcode\Object\ObjectInterface;

/**
* Interface of graphics context.
Expand Down Expand Up @@ -88,4 +88,4 @@ public function rotate($x, $y, $angle);
public function drawBarcode($x, $y, ObjectInterface $barcode);

public function copy();
}
}
6 changes: 3 additions & 3 deletions lib/PHPPdf/Core/Engine/Imagine/GraphicsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use PHPPdf\Core\Engine\Font as BaseFont;
use Imagine\Image\Color as ImagineColor;
use Imagine\Image\FontInterface as ImagineFont;
use Zend\Barcode\Object\ObjectInterface as Barcode;
use Laminas\Barcode\Object\ObjectInterface as Barcode;

/**
* Graphics context for Imagine
Expand Down Expand Up @@ -588,7 +588,7 @@ public function addBookmark($identifier, $name, $top, $ancestorsIdentifier = nul

protected function doDrawBarcode($x, $y, Barcode $barcode)
{
$renderer = new \Zend\Barcode\Renderer\Image(array());
$renderer = new \Laminas\Barcode\Renderer\Image(array());
$imageResource = imagecreate($barcode->getWidth(true) + 1, $barcode->getHeight(true) + 1);

$renderer->setResource($imageResource);
Expand Down Expand Up @@ -659,4 +659,4 @@ protected function doDrawArc($x, $y, $width, $height, $start, $end, $fillType)
$image->draw()->pieSlice($point, $box, $start, $end, $color, false);
}
}
}
}
83 changes: 52 additions & 31 deletions lib/PHPPdf/Core/Engine/ZF/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
namespace PHPPdf\Core\Engine\ZF;

use PHPPdf\Exception\InvalidArgumentException;

use PHPPdf\Exception\InvalidResourceException;
use PHPPdf\Core\Engine\AbstractFont;
use ZendPdf\Font as ZendFont;
use ZendPdf\Exception\ExceptionInterface;

/**
* @author Piotr Śliwa <peter.pl7@gmail.com>
*/
class Font extends AbstractFont
{
private $fonts = array();

/**
* @internal Public method within PHPPdf\Core\Engine\ZF namespace
*
* @return ZendFont
*
* @return ZendPdf\Resource\Font
*/
public function getCurrentWrappedFont()
{
Expand All @@ -33,33 +33,40 @@ public function getCurrentWrappedFont()

private function getResourceByStyle($style)
{
try {
if (!isset($this->fonts[$style])) {
try
{
if(!isset($this->fonts[$style]))
{
$data = $this->fontResources[$style];
if ($this->isNamedFont($data)) {
if($this->isNamedFont($data))
{
$name = $this->retrieveFontName($data);
$this->fonts[$style] = ZendFont::fontWithName($name);
} else {
}
else
{
$this->fonts[$style] = ZendFont::fontWithPath($data);
}
}

return $this->fonts[$style];
} catch (ExceptionInterface $e) {
}
catch(\ZendPdf\Exception\ExceptionInterface $e)
{
throw InvalidResourceException::invalidFontException($this->fontResources[$style], $e);
}
}

private function isNamedFont($fontData)
{
return strpos($fontData, '/') === false;
}

private static function retrieveFontName($name)
{
$const = sprintf('ZendPdf\Font::FONT_%s', str_replace('-', '_', strtoupper($name)));

if (!defined($const))
if(!defined($const))
{
throw new InvalidArgumentException(sprintf('Unrecognized font name: "%s".".', $name));
}
Expand All @@ -70,29 +77,31 @@ private static function retrieveFontName($name)
public function getWidthOfText($text, $fontSize)
{
$chars = $this->convertTextToChars($text);

$glyphs = $this->getCurrentWrappedFont()->glyphNumbersForCharacters($chars);
$widths = $this->getCurrentWrappedFont()->widthsForGlyphs($glyphs);
$textWidth = (array_sum($widths) / $this->getCurrentWrappedFont()->getUnitsPerEm()) * $fontSize;

return $textWidth;
}

private function convertTextToChars($text)
{
$length = strlen($text);
$chars = array();
$bytes = 1;
for ($i = 0; $i < $length; $i += $bytes) {
for($i=0; $i<$length; $i+=$bytes)
{
list($char, $bytes) = $this->ordUtf8($text, $i, $bytes);
if ($char !== false) {
if($char !== false)
{
$chars[] = $char;
}
}

return $chars;
}

/**
* code from http://php.net/manual/en/function.ord.php#78032
*/
Expand All @@ -103,33 +112,45 @@ private function ordUtf8($text, $index = 0, $bytes = null)

$char = false;

if ($index < $len) {
if ($index < $len)
{
$h = ord($text[$index]);

// if ($h < 0xC2) > keep $char = false;
if ($h <= 0x7F) {
if($h <= 0x7F)
{
$bytes = 1;
$char = $h;
} elseif ($h <= 0xDF && $index < $len - 1) {
}
elseif ($h < 0xC2)
{
$char = false;
}
elseif ($h <= 0xDF && $index < $len - 1)
{
$bytes = 2;
$char = ($h & 0x1F) << 6 | (ord($text[$index + 1]) & 0x3F);
} elseif ($h <= 0xEF && $index < $len - 2) {
$char = ($h & 0x1F) << 6 | (ord($text[$index + 1]) & 0x3F);
}
elseif($h <= 0xEF && $index < $len - 2)
{
$bytes = 3;
$char = ($h & 0x0F) << 12 | (ord($text[$index + 1]) & 0x3F) << 6
| (ord($text[$index + 2]) & 0x3F);
} elseif ($h <= 0xF4 && $index < $len - 3) {
| (ord($text[$index + 2]) & 0x3F);
}
elseif($h <= 0xF4 && $index < $len - 3)
{
$bytes = 4;
$char = ($h & 0x0F) << 18 | (ord($text[$index + 1]) & 0x3F) << 12
| (ord($text[$index + 2]) & 0x3F) << 6
| (ord($text[$index + 3]) & 0x3F);
| (ord($text[$index + 2]) & 0x3F) << 6
| (ord($text[$index + 3]) & 0x3F);
}
}


return array($char, $bytes);
}

public function getCurrentResourceIdentifier()
{
return isset($this->fontResources[$this->currentStyle]) ? $this->fontResources[$this->currentStyle] : $this->fontResources[self::STYLE_NORMAL];
}
}
}
6 changes: 3 additions & 3 deletions lib/PHPPdf/Core/Engine/ZF/GraphicsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use ZendPdf\Font as ZendFont;
use ZendPdf\Resource\Font\AbstractFont as ZendResourceFont;
use ZendPdf\Color\Html as ZendColor;
use Zend\Barcode\Object\ObjectInterface as Barcode;
use Laminas\Barcode\Object\ObjectInterface as Barcode;

/**
* @author Piotr Śliwa <peter.pl7@gmail.com>
Expand Down Expand Up @@ -427,7 +427,7 @@ protected function doRotate($x, $y, $angle)

protected function doDrawBarcode($x, $y, Barcode $barcode)
{
$renderer = new \Zend\Barcode\Renderer\Pdf();
$renderer = new \Laminas\Barcode\Renderer\Pdf();

$page = $this->getIndexOfPage();

Expand Down Expand Up @@ -476,4 +476,4 @@ protected function doDrawArc($x, $y, $width, $height, $start, $end, $fillType)
$end = deg2rad($end + 180);
$this->page->drawCircle($x, $y, $width/2, $start, $end, $this->translateFillType($fillType));
}
}
}
Loading

0 comments on commit 693c589

Please sign in to comment.