From 55fedd7b72364fdb4841e32fc0063453e8b57d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20H=C3=A4ber?= Date: Thu, 20 Jan 2022 10:11:16 +0100 Subject: [PATCH] fix(BE-2482): replace curly braces for compatibility with newer PHP versions; code style --- lib/PHPPdf/Core/Engine/ZF/Font.php | 87 ++++++++++++------------------ 1 file changed, 33 insertions(+), 54 deletions(-) diff --git a/lib/PHPPdf/Core/Engine/ZF/Font.php b/lib/PHPPdf/Core/Engine/ZF/Font.php index 81dd2430..080c078f 100644 --- a/lib/PHPPdf/Core/Engine/ZF/Font.php +++ b/lib/PHPPdf/Core/Engine/ZF/Font.php @@ -9,10 +9,10 @@ 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 @@ -20,11 +20,11 @@ class Font extends AbstractFont { private $fonts = array(); - + /** * @internal Public method within PHPPdf\Core\Engine\ZF namespace - * - * @return ZendPdf\Resource\Font + * + * @return ZendFont */ public function getCurrentWrappedFont() { @@ -33,40 +33,33 @@ 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(\ZendPdf\Exception\ExceptionInterface $e) - { + } catch (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)); } @@ -77,31 +70,29 @@ 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 */ @@ -112,43 +103,31 @@ private function ordUtf8($text, $index = 0, $bytes = null) $char = false; - if ($index < $len) - { - $h = ord($text{$index}); + if ($index < $len) { + $h = ord($text[$index]); - if($h <= 0x7F) - { + // if ($h < 0xC2) > keep $char = false; + if ($h <= 0x7F) { $bytes = 1; $char = $h; - } - elseif ($h < 0xC2) - { - $char = false; - } - elseif ($h <= 0xDF && $index < $len - 1) - { + } 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) - { + $char = ($h & 0x0F) << 12 | (ord($text[$index + 1]) & 0x3F) << 6 + | (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); + $char = ($h & 0x0F) << 18 | (ord($text[$index + 1]) & 0x3F) << 12 + | (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];