From d340e043d6484d09af13be001102242b594ca5c9 Mon Sep 17 00:00:00 2001 From: Mikhail Genin Date: Mon, 8 Oct 2018 16:04:28 +0300 Subject: [PATCH 1/2] Update src/EasySVG.php new line symbol - bug fix --- src/EasySVG.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/EasySVG.php b/src/EasySVG.php index fad618a..ed916f4 100644 --- a/src/EasySVG.php +++ b/src/EasySVG.php @@ -244,11 +244,6 @@ public function textDef($text) { $letter = $text[$i]; - //ignore this glyph instead of throwing an error if the font does not define it - if(!array_key_exists($letter, $this->font->glyphs)){ - continue; - } - // line break support (10 is unicode for linebreak) if($letter==10){ $horizAdvX = 0; @@ -256,6 +251,12 @@ public function textDef($text) { continue; } + //ignore this glyph instead of throwing an error if the font does not define it + if(!array_key_exists($letter, $this->font->glyphs)){ + continue; + } + + // extract character definition $d = $this->font->glyphs[$letter]->d; @@ -293,11 +294,6 @@ public function textDimensions($text) { $letter = $text[$i]; - //ignore this glyph instead of throwing an error if the font does not define it - if(!array_key_exists($letter, $this->font->glyphs)){ - continue; - } - // line break support (10 is unicode for linebreak) if($letter==10){ $width = $lineWidth>$width ? $lineWidth : $width; @@ -306,6 +302,13 @@ public function textDimensions($text) { continue; } + //ignore this glyph instead of throwing an error if the font does not define it + if(!array_key_exists($letter, $this->font->glyphs)){ + continue; + } + + + $lineWidth += $this->font->glyphs[$letter]->horizAdvX * $fontSize + $this->font->em * $this->font->letterSpacing * $fontSize; } From e99d4e7d9ae111f4011de919ab65b46e27f5be38 Mon Sep 17 00:00:00 2001 From: Mikhail Genin Date: Mon, 8 Oct 2018 16:24:56 +0300 Subject: [PATCH 2/2] Correct work with multibyte text This library did not work correctly with multibyte characters. I made the appropriate correct operation with multibyte text. Before PHP 7.2.0, you need to use the utf8_ord () alternative function. If you have PHP version 7.2.0+, you can limit the work to the mb_ord () function. I also attach a font to check the work. --- demo/Lobster-Regular-Cyrillic.svg | 4664 +++++++++++++++++++++++++++++ demo/index.php | 82 +- src/EasySVG.php | 81 +- 3 files changed, 4764 insertions(+), 63 deletions(-) create mode 100644 demo/Lobster-Regular-Cyrillic.svg diff --git a/demo/Lobster-Regular-Cyrillic.svg b/demo/Lobster-Regular-Cyrillic.svg new file mode 100644 index 0000000..ea721ad --- /dev/null +++ b/demo/Lobster-Regular-Cyrillic.svg @@ -0,0 +1,4664 @@ + + + + +Created by FontForge 20181001 at Wed Nov 1 12:37:02 2017 + By convertio +Copyright 2010 The Lobster Project Authors (https://github.com/impallari/The-Lobster-Font), with Reserved Font Name "Lobster". + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/demo/index.php b/demo/index.php index b90831f..156d94f 100644 --- a/demo/index.php +++ b/demo/index.php @@ -9,30 +9,30 @@ -
-
+
+
-

EasySVG demo

+

EasySVG demo

- setFontSVG("om_telolet_om-webfont.svg"); - $svg->setFontSize(80); - $svg->setFontColor('#000000'); - $svg->setLineHeight(1.2); - $svg->setLetterSpacing(.1); - $svg->addText($text); - // set width/height according to text - list($textWidth, $textHeight) = $svg->textDimensions($text); - $svg->addAttribute("width", $textWidth."px"); - $svg->addAttribute("height", $textHeight."px"); - echo $svg->asXML(); - ?> -


-

PHP code

-
+        $svg = new EasySVG();
+        $svg->setFontSVG("Lobster-Regular-Cyrillic.svg");
+        $svg->setFontSize(80);
+        $svg->setFontColor('#000000');
+        $svg->setLineHeight(1.2);
+        $svg->setLetterSpacing(.1);
+        $svg->addText($text);
+        // set width/height according to text
+        list($textWidth, $textHeight) = $svg->textDimensions($text);
+        $svg->addAttribute("width", $textWidth."px");
+        $svg->addAttribute("height", $textHeight."px");
+        echo $svg->asXML();
+        ?>
+        


+

PHP code

+
                 $text = "Simple text display\netc.";
 
                 $svg = new EasySVG();
@@ -48,26 +48,26 @@
                 $svg->addAttribute("height", $textHeight."px");
                 echo $svg->asXML();
             
-


+


-

Centered text

- Centered text + addAttribute("width", "600px"); - $svg->addAttribute("height", "200px"); - $svg->addAttribute("style", "border: dashed 1px #aaa"); - $svg->setFontSVG("om_telolet_om-webfont.svg"); - $svg->setFontSize(80); - $svg->setFontColor('#000000'); - $svg->setLetterSpacing(.1); - $svg->addText($text, "center", "center"); - echo $svg->asXML(); - ?> -


-

PHP code

-
+        $svg = new EasySVG();
+        $svg->addAttribute("width", "600px");
+        $svg->addAttribute("height", "200px");
+        $svg->addAttribute("style", "border: dashed 1px #aaa");
+        $svg->setFontSVG("om_telolet_om-webfont.svg");
+        $svg->setFontSize(80);
+        $svg->setFontColor('#000000');
+        $svg->setLetterSpacing(.1);
+        $svg->addText($text, "center", "center");
+        echo $svg->asXML();
+        ?>
+        


+

PHP code

+
                 $text = "Simple text display\netc.";
 
                 $svg = new EasySVG();
@@ -81,9 +81,9 @@
                 $svg->addText($text, "center", "center");
                 echo $svg->asXML();
             
-


-
+


+
diff --git a/src/EasySVG.php b/src/EasySVG.php index ed916f4..7759bdd 100644 --- a/src/EasySVG.php +++ b/src/EasySVG.php @@ -2,7 +2,8 @@ /** * EasySVG - Generate SVG from PHP * @author Simon Tarchichi - * @version 0.1b + * @author Mikhail Genin + * @version 0.2b * * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform * @see http://stackoverflow.com/questions/14684846/flattening-svg-matrix-transforms-in-inkscape @@ -36,6 +37,57 @@ public function clearSVG() { $this->svg->addAttribute('xmlns', 'http://www.w3.org/2000/svg'); } + private function utf8_ord($chr) + { + $ord0 = ord($chr); + if ($ord0 >= 0 && $ord0 <= 127) + return $ord0; + if (!isset($chr[1])) + { + trigger_error('Short sequence - at least 2 bytes expected, only 1 seen'); + return false; + } + $ord1 = ord($chr[1]); + if ($ord0 >= 192 && $ord0 <= 223) + return ($ord0 - 192) * 64 + ($ord1 - 128); + if (!isset($chr[2])) + { + trigger_error('Short sequence - at least 3 bytes expected, only 2 seen'); + return false; + } + $ord2 = ord($chr[2]); + if ($ord0 >= 224 && $ord0 <= 239) + return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128); + if (!isset($chr[3])) + { + trigger_error('Short sequence - at least 4 bytes expected, only 3 seen'); + return false; + } + $ord3 = ord($chr[3]); + if ($ord0 >= 240 && $ord0 <= 247) + return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2 - 128) * 64 + ($ord3 - 128); + if (!isset($chr[4])) + { + trigger_error('Short sequence - at least 5 bytes expected, only 4 seen'); + return false; + } + $ord4 = ord($chr[4]); + if ($ord0 >= 248 && $ord0 <= 251) + return ($ord0 - 248) * 16777216 + ($ord1 - 128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128); + if (!isset($chr[5])) + { + trigger_error('Short sequence - at least 6 bytes expected, only 5 seen'); + return false; + } + if ($ord0 >= 252 && $ord0 <= 253) + return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128); + if ($ord0 >= 254 && $ord0 <= 255) + { + trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0); + return false; + } + } + /** * Function takes UTF-8 encoded string and returns unicode number for every character. * @param string $str @@ -43,25 +95,10 @@ public function clearSVG() { */ private function _utf8ToUnicode( $str ) { $unicode = array(); - $values = array(); - $lookingFor = 1; - - for ($i = 0; $i < strlen( $str ); $i++ ) { - $thisValue = ord( $str[ $i ] ); - if ( $thisValue < 128 ) $unicode[] = $thisValue; - else { - if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3; - $values[] = $thisValue; - if ( count( $values ) == $lookingFor ) { - $number = ( $lookingFor == 3 ) ? - ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ): - ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 ); - - $unicode[] = $number; - $values = array(); - $lookingFor = 1; - } - } + + for ($i = 0; $i < mb_strlen( $str ); $i++ ) { + $thisValue = $this->utf8_ord(mb_substr($str,$i,1) ); //in PHP 7.2.0+ pls use mb_ord ! + $unicode[] = $thisValue; } return $unicode; @@ -196,7 +233,7 @@ public function addText($text, $x=0, $y=0, $attributes=array()) { $def = $this->textDef($text); if ($x === 'center' || $y === 'center') { - list($textWidth, $textHeight) = $this->textDimensions($text); + list($textWidth, $textHeight) = $this->textDimensions($text); } // center horizontally @@ -550,4 +587,4 @@ public function asXML(){ public function addAttribute($key, $value){ return $this->svg->addAttribute($key, $value); } -} +} \ No newline at end of file