Skip to content

Commit

Permalink
Merge pull request dompdf#14 from sgc-fireball/master
Browse files Browse the repository at this point in the history
Fixes SVG color handling
  • Loading branch information
PhenX authored Mar 19, 2017
2 parents 06e2d40 + 31f5440 commit 34b53c9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
},
"require": {
"sabberworm/php-css-parser": "8.1.*"
},
"require-dev": {
"phpunit/phpunit": "~5.0"
}
}
4 changes: 2 additions & 2 deletions src/Svg/Surface/SurfaceCpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ public function setStyle(Style $style)
$this->style = $style;
$canvas = $this->canvas;

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Svg/Surface/SurfaceGmagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ public function setStyle(Style $style)
$this->style = $style;
$canvas = $this->canvas;

if ($stroke = $style->stroke) {
if (is_array($style->stroke) && $stroke = $style->stroke) {
$canvas->setcolor("stroke", "rgb", $stroke[0] / 255, $stroke[1] / 255, $stroke[2] / 255, null);
}

if ($fill = $style->fill) {
if (is_array($style->fill) && $fill = $style->fill) {
// $canvas->setcolor("fill", "rgb", $fill[0] / 255, $fill[1] / 255, $fill[2] / 255, null);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Svg/Surface/SurfacePDFLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function setStyle(Style $style)
$this->style = $style;
$canvas = $this->canvas;

if (($stroke = $style->stroke) && $stroke !== "none") {
if ($stroke = $style->stroke && is_array($style->stroke)) {
$canvas->setcolor(
"stroke",
"rgb",
Expand All @@ -326,7 +326,7 @@ public function setStyle(Style $style)
);
}

if (($fill = $style->fill) && $fill !== "none") {
if ($fill = $style->fill && is_array($style->fill)) {
$canvas->setcolor(
"fill",
"rgb",
Expand Down
6 changes: 3 additions & 3 deletions src/Svg/Tag/Shape.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @package php-svg-lib
* @link http://github.com/PhenX/php-svg-lib
* @author Fabien Ménager <[email protected]>
* @author Fabien Ménager <[email protected]>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/

Expand Down Expand Up @@ -33,8 +33,8 @@ protected function after()
if ($this->hasShape) {
$style = $surface->getStyle();

$fill = $style->fill && $style->fill !== "none";
$stroke = $style->stroke && $style->stroke !== "none";
$fill = $style->fill && is_array($style->fill);
$stroke = $style->stroke && is_array($style->stroke);

if ($fill) {
if ($stroke) {
Expand Down
60 changes: 37 additions & 23 deletions tests/Svg/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,53 @@

namespace Svg\Tests;

include_once __DIR__."/../../src/autoload.php";
include_once __DIR__ . "/../../src/autoload.php";

use Svg\Style;

class StyleTest extends \PHPUnit_Framework_TestCase {
public function test_parseColor() {
$this->assertEquals("none", Style::parseColor("none"));
$this->assertEquals(array(255, 0, 0), Style::parseColor("RED"));
$this->assertEquals(array(0, 0, 255), Style::parseColor("blue"));
$this->assertEquals(null, Style::parseColor("foo"));
}
class StyleTest extends \PHPUnit_Framework_TestCase
{

public function test_parseColor()
{
$this->assertEquals("none", Style::parseColor("none"));
$this->assertEquals(array(255, 0, 0), Style::parseColor("RED"));
$this->assertEquals(array(0, 0, 255), Style::parseColor("blue"));
$this->assertEquals(null, Style::parseColor("foo"));
$this->assertEquals(array(0, 0, 0), Style::parseColor("black"));
$this->assertEquals(array(255, 255, 255), Style::parseColor("white"));
$this->assertEquals(array(0, 0, 0), Style::parseColor("#000000"));
$this->assertEquals(array(255, 255, 255), Style::parseColor("#ffffff"));
$this->assertEquals(array(0, 0, 0), Style::parseColor("rgb(0,0,0)"));
$this->assertEquals(array(255, 255, 255), Style::parseColor("rgb(255,255,255)"));
$this->assertEquals(array(0, 0, 0), Style::parseColor("rgb(0, 0, 0)"));
$this->assertEquals(array(255, 255, 255), Style::parseColor("rgb(255, 255, 255)"));
}

public function test_fromAttributes() {
$style = new Style();
public function test_fromAttributes()
{
$style = new Style();

$attributes = array(
"color" => "blue",
"fill" => "#fff",
"stroke" => "none",
);
$attributes = array(
"color" => "blue",
"fill" => "#fff",
"stroke" => "none",
);

$style->fromAttributes($attributes);
$style->fromAttributes($attributes);

$this->assertEquals(array(0, 0, 255), $style->color);
$this->assertEquals(array(255, 255, 255), $style->fill);
$this->assertEquals("none", $style->stroke);
}
$this->assertEquals(array(0, 0, 255), $style->color);
$this->assertEquals(array(255, 255, 255), $style->fill);
$this->assertEquals("none", $style->stroke);
}

public function test_convertSize(){
$this->assertEquals(1, Style::convertSize(1));
public function test_convertSize()
{
$this->assertEquals(1, Style::convertSize(1));
$this->assertEquals(10, Style::convertSize("10px")); // FIXME
$this->assertEquals(10, Style::convertSize("10pt"));
$this->assertEquals(8, Style::convertSize("80%", 10, 72));
$this->assertEquals(8, Style::convertSize("80%", 10, 72));
}

}

0 comments on commit 34b53c9

Please sign in to comment.