dompdf / php-svg-lib

SVG file parsing / rendering library
GNU Lesser General Public License v3.0
1.4k stars 77 forks source link

Missing "none" handler for PHP 7.1 #13

Closed UweKeim closed 7 years ago

UweKeim commented 7 years ago

SurfaceCpdf.php, line 379 reads:

if ($stroke = $style->stroke) {

Unfortunately this breaks when $stroke is "none" which was the case for an image of mine.

In my opinion, it should read like in SurfacePDFLib.php, line 318:

if (($stroke = $style->stroke) && $stroke !== "none") {

I discovered that this breaks for PHP 7.1 (not 7.0) with the error

A non-numeric value encountered

because the next line after the if condition fails:

$stroke[0]/255

I'm not sure what the exact difference between these two files is.

My suggestion:

Please add the check condition for "none".

UweKeim commented 7 years ago

Same is true for the next if condition just below the previous one:

Instead of

if ($fill = $style->fill) {

You should write

if (($fill = $style->fill) && $fill !== "none") {

Even afer adding this check, an SVG image of mine caused an error in the very next line:

$canvas->setColor(array($fill[0]/255, $fill[1]/255, $fill[2]/255), true);

Because $fill was set to the value "black".

It seems that there is a function Style::parseColor() which translates values like "black" to their hex values.

I'm not sure, why SurfaceCpdf.php does not use this parse function.

My suggestion:

Please make the erroneous line work for PHP 7.1, e.g. by using the parseColor function.

UweKeim commented 7 years ago

I also get errors in the parseColor function in Styles.php:

if ($color[0] === "#") {

This failed for $color being an empty string with an error

Uninitialized string offset: 0

I'm not sure whether the SVG stuff was ever tested with PHP 7.1, or whether I'm simply completely on the wrong track.

sgc-fireball commented 7 years ago

Same errors here: vendor/phenx/php-svg-lib/src/Svg/Surface/SurfaceCpdf.php. \Svg\Surface\SurfaceCpdf::setStyle

    if ($stroke = $style->stroke && is_array($style->stroke)) {
        $canvas->setStrokeColor(array($stroke[0]/255, $stroke[1]/255, $stroke[2]/255), true);
    }

    if ($fill = $style->fill && is_array($style->fill)) {
        $canvas->setColor(array($fill[0]/255, $fill[1]/255, $fill[2]/255), true);
    }
PhenX commented 7 years ago

I think this is solved by 34b53c9a149cb7fd978d3d9be9879ee6607fa198