Skip to content

Commit

Permalink
Merge pull request #27 from clue-labs/utf8
Browse files Browse the repository at this point in the history
Use UTF-8 encoding (Unicode) by default and respect charset attribute
  • Loading branch information
clue authored Oct 9, 2018
2 parents 81fe782 + 115e65e commit 1a9b92a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ matrix:

sudo: false

addons:
apt:
packages:
- graphviz

install:
- composer install --no-interaction

Expand Down
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ GraPHP attributes as documented below.
For the full list of all GraphViz attributes, please refer to the
[GraphViz documentation](https://graphviz.gitlab.io/_pages/doc/info/attrs.html).

Note that all [attributes](#attributes) will be quoted and escaped by default,
so a `>` will appear as-is and will not be interpreted as HTML. See also
[HTML-like labels](#html-like-labels) below for more details.
Note that all attributes use UTF-8 encoding (Unicode) and will be quoted and
escaped by default, so a `ö` and `>` will appear as-is and will not be
interpreted as HTML. See also [HTML-like labels](#html-like-labels) below for
more details.

### Graph attributes

Expand All @@ -82,6 +83,22 @@ $graph->setAttribute('graphviz.graph.bgcolor', 'transparent');
is done for consistency reasons with respect to default vertex and edge
attributes as documented below.

For example, the `rankdir` attribute can be used to change the orientation to
horizontal mode (left to right) like this:

```php
$graph = new Fhaculty\Graph\Graph();
$graph->setAttribute('graphviz.graph.rankdir', 'LR');

$hello = $graph->createVertex('hello');
$world = $graph->createVertex('wörld');
$hello->createEdgeTo($world);
```

![html graph example](examples/02-html.png)

See also the [examples](examples/).

### Vertex attributes

GraphViz supports a number of attributes on each vertex instance (GraphViz calls
Expand Down
25 changes: 25 additions & 0 deletions examples/02-html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// $ php -S localhost:8080 examples/02-html.php

require __DIR__ . '/../vendor/autoload.php';

$graph = new Fhaculty\Graph\Graph();
$graph->setAttribute('graphviz.graph.rankdir', 'LR');

$hello = $graph->createVertex('hello');
$world = $graph->createVertex('wörld');
$hello->createEdgeTo($world);

$graphviz = new Graphp\GraphViz\GraphViz();
$graphviz->setFormat('svg');

echo '<!DOCTYPE html>
<html>
<head>
<title>hello wörld</title>
<body>
' . $graphviz->createImageHtml($graph) . '
</body>
</html>
';
Binary file added examples/02-html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions src/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Graphp\Algorithms\Groups;
use Graphp\Algorithms\Degree;
use Fhaculty\Graph\Exception\UnexpectedValueException;
use Fhaculty\Graph\Exception\InvalidArgumentException;
use Fhaculty\Graph\Edge\Base as Edge;
use \stdClass;
use Fhaculty\Graph\Attribute\AttributeBagNamespaced;
Expand Down Expand Up @@ -155,7 +154,10 @@ public function createImageData(Graph $graph)
*/
public function createImageSrc(Graph $graph)
{
$format = ($this->format === 'svg' || $this->format === 'svgz') ? 'svg+xml' : $this->format;
$format = $this->format;
if ($this->format === 'svg' || $this->format === 'svgz') {
$format = 'svg+xml;charset=' . $graph->getAttribute('graphviz.graph.charset', 'UTF-8');
}

return 'data:image/' . $format . ';base64,' . base64_encode($this->createImageData($graph));
}
Expand Down
30 changes: 30 additions & 0 deletions tests/GraphVizTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,34 @@ public function testEdgeLabels()

$this->assertEquals($expected, $this->graphViz->createScript($graph));
}

public function testCreateImageSrcWillExportPngDefaultFormat()
{
$graph = new Graph();

$src = $this->graphViz->createImageSrc($graph);

$this->assertStringStartsWith('data:image/png;base64,', $src);
}

public function testCreateImageSrcAsSvgWithUtf8DefaultCharset()
{
$graph = new Graph();

$this->graphViz->setFormat('svg');
$src = $this->graphViz->createImageSrc($graph);

$this->assertStringStartsWith('data:image/svg+xml;charset=UTF-8;base64,', $src);
}

public function testCreateImageSrcAsSvgzWithExplicitIsoCharsetLatin1()
{
$graph = new Graph();
$graph->setAttribute('graphviz.graph.charset', 'iso-8859-1');

$this->graphViz->setFormat('svgz');
$src = $this->graphViz->createImageSrc($graph);

$this->assertStringStartsWith('data:image/svg+xml;charset=iso-8859-1;base64,', $src);
}
}

0 comments on commit 1a9b92a

Please sign in to comment.