Skip to content

Commit 34b53c9

Browse files
authored
Merge pull request #14 from sgc-fireball/master
Fixes SVG color handling
2 parents 06e2d40 + 31f5440 commit 34b53c9

File tree

6 files changed

+49
-32
lines changed

6 files changed

+49
-32
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
},
1818
"require": {
1919
"sabberworm/php-css-parser": "8.1.*"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "~5.0"
2023
}
2124
}

src/Svg/Surface/SurfaceCpdf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,11 @@ public function setStyle(Style $style)
376376
$this->style = $style;
377377
$canvas = $this->canvas;
378378

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

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

src/Svg/Surface/SurfaceGmagick.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ public function setStyle(Style $style)
256256
$this->style = $style;
257257
$canvas = $this->canvas;
258258

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

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

src/Svg/Surface/SurfacePDFLib.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public function setStyle(Style $style)
315315
$this->style = $style;
316316
$canvas = $this->canvas;
317317

318-
if (($stroke = $style->stroke) && $stroke !== "none") {
318+
if ($stroke = $style->stroke && is_array($style->stroke)) {
319319
$canvas->setcolor(
320320
"stroke",
321321
"rgb",
@@ -326,7 +326,7 @@ public function setStyle(Style $style)
326326
);
327327
}
328328

329-
if (($fill = $style->fill) && $fill !== "none") {
329+
if ($fill = $style->fill && is_array($style->fill)) {
330330
$canvas->setcolor(
331331
"fill",
332332
"rgb",

src/Svg/Tag/Shape.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @package php-svg-lib
44
* @link http://github.com/PhenX/php-svg-lib
5-
* @author Fabien Ménager <[email protected]>
5+
* @author Fabien Ménager <[email protected]>
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
77
*/
88

@@ -33,8 +33,8 @@ protected function after()
3333
if ($this->hasShape) {
3434
$style = $surface->getStyle();
3535

36-
$fill = $style->fill && $style->fill !== "none";
37-
$stroke = $style->stroke && $style->stroke !== "none";
36+
$fill = $style->fill && is_array($style->fill);
37+
$stroke = $style->stroke && is_array($style->stroke);
3838

3939
if ($fill) {
4040
if ($stroke) {

tests/Svg/StyleTest.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,53 @@
88

99
namespace Svg\Tests;
1010

11-
include_once __DIR__."/../../src/autoload.php";
11+
include_once __DIR__ . "/../../src/autoload.php";
1212

1313
use Svg\Style;
1414

15-
class StyleTest extends \PHPUnit_Framework_TestCase {
16-
public function test_parseColor() {
17-
$this->assertEquals("none", Style::parseColor("none"));
18-
$this->assertEquals(array(255, 0, 0), Style::parseColor("RED"));
19-
$this->assertEquals(array(0, 0, 255), Style::parseColor("blue"));
20-
$this->assertEquals(null, Style::parseColor("foo"));
21-
}
15+
class StyleTest extends \PHPUnit_Framework_TestCase
16+
{
17+
18+
public function test_parseColor()
19+
{
20+
$this->assertEquals("none", Style::parseColor("none"));
21+
$this->assertEquals(array(255, 0, 0), Style::parseColor("RED"));
22+
$this->assertEquals(array(0, 0, 255), Style::parseColor("blue"));
23+
$this->assertEquals(null, Style::parseColor("foo"));
24+
$this->assertEquals(array(0, 0, 0), Style::parseColor("black"));
25+
$this->assertEquals(array(255, 255, 255), Style::parseColor("white"));
26+
$this->assertEquals(array(0, 0, 0), Style::parseColor("#000000"));
27+
$this->assertEquals(array(255, 255, 255), Style::parseColor("#ffffff"));
28+
$this->assertEquals(array(0, 0, 0), Style::parseColor("rgb(0,0,0)"));
29+
$this->assertEquals(array(255, 255, 255), Style::parseColor("rgb(255,255,255)"));
30+
$this->assertEquals(array(0, 0, 0), Style::parseColor("rgb(0, 0, 0)"));
31+
$this->assertEquals(array(255, 255, 255), Style::parseColor("rgb(255, 255, 255)"));
32+
}
2233

23-
public function test_fromAttributes() {
24-
$style = new Style();
34+
public function test_fromAttributes()
35+
{
36+
$style = new Style();
2537

26-
$attributes = array(
27-
"color" => "blue",
28-
"fill" => "#fff",
29-
"stroke" => "none",
30-
);
38+
$attributes = array(
39+
"color" => "blue",
40+
"fill" => "#fff",
41+
"stroke" => "none",
42+
);
3143

32-
$style->fromAttributes($attributes);
44+
$style->fromAttributes($attributes);
3345

34-
$this->assertEquals(array(0, 0, 255), $style->color);
35-
$this->assertEquals(array(255, 255, 255), $style->fill);
36-
$this->assertEquals("none", $style->stroke);
37-
}
46+
$this->assertEquals(array(0, 0, 255), $style->color);
47+
$this->assertEquals(array(255, 255, 255), $style->fill);
48+
$this->assertEquals("none", $style->stroke);
49+
}
3850

39-
public function test_convertSize(){
40-
$this->assertEquals(1, Style::convertSize(1));
51+
public function test_convertSize()
52+
{
53+
$this->assertEquals(1, Style::convertSize(1));
4154
$this->assertEquals(10, Style::convertSize("10px")); // FIXME
4255
$this->assertEquals(10, Style::convertSize("10pt"));
43-
$this->assertEquals(8, Style::convertSize("80%", 10, 72));
56+
$this->assertEquals(8, Style::convertSize("80%", 10, 72));
4457
}
58+
4559
}
4660

0 commit comments

Comments
 (0)