diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-12-07 14:48:11 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-12-07 14:48:11 +0000 |
commit | 10d2b024859787aad075161d62f60d8a02d054a2 (patch) | |
tree | 5cc15653435725f9620c92cebda6fcecfcace5d4 | |
parent | b18f980f00c730e679ab47a1d58afdd3b080f0c9 (diff) | |
download | zetacomponents-graph-10d2b024859787aad075161d62f60d8a02d054a2.zip zetacomponents-graph-10d2b024859787aad075161d62f60d8a02d054a2.tar.gz |
- Tested and improved handling of texts with different charsets in all drivers
-rw-r--r-- | src/driver/svg.php | 50 | ||||
-rw-r--r-- | src/options/svg_driver.php | 6 | ||||
-rw-r--r-- | tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsFlash.swf | bin | 0 -> 1039 bytes | |||
-rw-r--r-- | tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsGD.png | bin | 0 -> 12468 bytes | |||
-rw-r--r-- | tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsSVG.svg | 2 | ||||
-rw-r--r-- | tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsGD.png | bin | 0 -> 12468 bytes | |||
-rw-r--r-- | tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsSVG.svg | 2 | ||||
-rw-r--r-- | tests/driver_svg_test.php | 18 | ||||
-rw-r--r-- | tests/font_test.php | 256 |
9 files changed, 329 insertions, 5 deletions
diff --git a/src/driver/svg.php b/src/driver/svg.php index dd14912..4ce289d 100644 --- a/src/driver/svg.php +++ b/src/driver/svg.php @@ -86,9 +86,18 @@ class ezcGraphSvgDriver extends ezcGraphDriver { if ( $this->dom === null ) { + // Create encoding based dom document + if ( $this->options->encoding !== null ) + { + $this->dom = new DOMDocument( '1.0', $this->options->encoding ); + } + else + { + $this->dom = new DOMDocument( '1.0' ); + } + if ( $this->options->templateDocument !== false ) { - $this->dom = new DOMDocument(); // @TODO: Add $this->dom->format $this->dom->load( $this->options->templateDocument ); @@ -97,7 +106,6 @@ class ezcGraphSvgDriver extends ezcGraphDriver } else { - $this->dom = new DOMDocument(); $svg = $this->dom->createElementNS( 'http://www.w3.org/2000/svg', 'svg' ); $this->dom->appendChild( $svg ); @@ -468,6 +476,15 @@ class ezcGraphSvgDriver extends ezcGraphDriver */ protected function getTextWidth( $string, $size ) { + switch ( strtolower( $this->options->encoding ) ) + { + case '': + case 'utf-8': + case 'utf-16': + $string = utf8_decode( $string ); + break; + } + if ( is_numeric( $string ) ) { return $size * strlen( $string ) * $this->options->assumedNumericCharacterWidth; @@ -479,6 +496,31 @@ class ezcGraphSvgDriver extends ezcGraphDriver } /** + * Encodes non-utf-8 strings + * + * Transforms non-utf-8 strings to their hex entities, because ext/DOM + * fails here with conversion errors. + * + * @param string $string + * @return string + */ + protected function encode( $string ) + { + $string = htmlspecialchars( $string ); + + switch ( strtolower( $this->options->encoding ) ) + { + case '': + case 'utf-8': + case 'utf-16': + return $string; + default: + // Manual escaping of ANSII characters, because ext/DOM fails here + return preg_replace( '/[\\x80-\\xFF]/e', 'sprintf( \'&#x%02x;\', ord( \'\\0\') )', $string ); + } + } + + /** * Draw all collected texts * * The texts are collected and their maximum possible font size is @@ -657,7 +699,7 @@ class ezcGraphSvgDriver extends ezcGraphDriver // Optionally draw text shadow if ( $text['font']->textShadow === true ) { - $textNode = $this->dom->createElement( 'text', htmlspecialchars( $string ) ); + $textNode = $this->dom->createElement( 'text', $this->encode( $string ) ); $textNode->setAttribute( 'id', $text['id'] . '_shadow' ); $textNode->setAttribute( 'x', $position->x + $this->options->graphOffset->x + $text['font']->textShadowOffset ); $textNode->setAttribute( 'text-length', $this->getTextWidth( $string, $size ) . 'px' ); @@ -678,7 +720,7 @@ class ezcGraphSvgDriver extends ezcGraphDriver } // Finally draw text - $textNode = $this->dom->createElement( 'text', htmlspecialchars( $string ) ); + $textNode = $this->dom->createElement( 'text', $this->encode( $string ) ); $textNode->setAttribute( 'id', $text['id'] . '_text' ); $textNode->setAttribute( 'x', $position->x + $this->options->graphOffset->x ); $textNode->setAttribute( 'text-length', $this->getTextWidth( $string, $size ) . 'px' ); diff --git a/src/options/svg_driver.php b/src/options/svg_driver.php index 27bc597..ebd8d1e 100644 --- a/src/options/svg_driver.php +++ b/src/options/svg_driver.php @@ -10,6 +10,8 @@ /** * Class containing the basic options for charts * + * @property string $encoding + * Encoding of the SVG XML document * @property float $assumedNumericCharacterWidth * Assumed percentual average width of chars in numeric strings with * the used font. @@ -61,6 +63,7 @@ class ezcGraphSvgDriverOptions extends ezcGraphDriverOptions */ public function __construct( array $options = array() ) { + $this->properties['encoding'] = null; $this->properties['assumedNumericCharacterWidth'] = .62; $this->properties['assumedTextCharacterWidth'] = .53; $this->properties['strokeLineJoin'] = 'round'; @@ -228,6 +231,9 @@ class ezcGraphSvgDriverOptions extends ezcGraphDriverOptions case 'idPrefix': $this->properties['idPrefix'] = (string) $propertyValue; break; + case 'encoding': + $this->properties['encoding'] = (string) $propertyValue; + break; default: parent::__set( $propertyName, $propertyValue ); break; diff --git a/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsFlash.swf b/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsFlash.swf Binary files differnew file mode 100644 index 0000000..357b86b --- /dev/null +++ b/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsFlash.swf diff --git a/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsGD.png b/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsGD.png Binary files differnew file mode 100644 index 0000000..70084b1 --- /dev/null +++ b/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsGD.png diff --git a/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsSVG.svg b/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsSVG.svg new file mode 100644 index 0000000..012e3fb --- /dev/null +++ b/tests/data/compare/ezcGraphFontTest_testISO_8859_15SpecialCharsSVG.svg @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="ISO-8859-15"?> +<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.0" id="ezcGraph"><defs/><g id="ezcGraphChart" color-rendering="optimizeQuality" shape-rendering="geometricPrecision" text-rendering="optimizeLegibility"><path d=" M 10.0000,80.0000 L 10.0000,10.0000 L 160.0000,10.0000 L 160.0000,80.0000 L 10.0000,80.0000 z " style="fill: #eeeeec; fill-opacity: 1.00; stroke: none;" id="ezcGraphPolygon_1"/><g id="ezcGraphTextBox_2"><path d=" M 9.5000,51.0000 L 9.5000,9.5000 L 159.4000,9.5000 L 159.4000,51.0000 L 9.5000,51.0000 z " style="fill: #ffffff; fill-opacity: 0.00; stroke: none;" id="ezcGraphPolygon_3"/><text id="ezcGraphTextBox_2_text" x="10" text-length="148.4px" y="44" style="font-size: 40px; font-family: sans-serif; fill: #000000; fill-opacity: 1.00; stroke: none;">öäüÖÄÜß</text></g></g></svg> diff --git a/tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsGD.png b/tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsGD.png Binary files differnew file mode 100644 index 0000000..70084b1 --- /dev/null +++ b/tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsGD.png diff --git a/tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsSVG.svg b/tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsSVG.svg new file mode 100644 index 0000000..9dba499 --- /dev/null +++ b/tests/data/compare/ezcGraphFontTest_testUTF8SpecialCharsSVG.svg @@ -0,0 +1,2 @@ +<?xml version="1.0"?> +<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.0" id="ezcGraph"><defs/><g id="ezcGraphChart" color-rendering="optimizeQuality" shape-rendering="geometricPrecision" text-rendering="optimizeLegibility"><path d=" M 10.0000,80.0000 L 10.0000,10.0000 L 160.0000,10.0000 L 160.0000,80.0000 L 10.0000,80.0000 z " style="fill: #eeeeec; fill-opacity: 1.00; stroke: none;" id="ezcGraphPolygon_1"/><g id="ezcGraphTextBox_2"><path d=" M 9.5000,51.0000 L 9.5000,9.5000 L 159.4000,9.5000 L 159.4000,51.0000 L 9.5000,51.0000 z " style="fill: #ffffff; fill-opacity: 0.00; stroke: none;" id="ezcGraphPolygon_3"/><text id="ezcGraphTextBox_2_text" x="10" text-length="148.4px" y="44" style="font-size: 40px; font-family: sans-serif; fill: #000000; fill-opacity: 1.00; stroke: none;">öäüÖÄÜß</text></g></g></svg> diff --git a/tests/driver_svg_test.php b/tests/driver_svg_test.php index 88da61d..2db6ded 100644 --- a/tests/driver_svg_test.php +++ b/tests/driver_svg_test.php @@ -1530,5 +1530,23 @@ class ezcGraphSvgDriverTest extends ezcTestCase 'Setting property value did not work for property idPrefix in class ezcGraphSvgDriverOptions' ); } + + public function testSvgDriverOptionsPropertyEncoding() + { + $options = new ezcGraphSvgDriverOptions(); + + $this->assertSame( + null, + $options->encoding, + 'Wrong default value for property idPrefix in class ezcGraphSvgDriverOptions' + ); + + $options->encoding = 'ISO-8859-15'; + $this->assertSame( + 'ISO-8859-15', + $options->encoding, + 'Setting property value did not work for property idPrefix in class ezcGraphSvgDriverOptions' + ); + } } ?> diff --git a/tests/font_test.php b/tests/font_test.php index 460c64d..4b5208b 100644 --- a/tests/font_test.php +++ b/tests/font_test.php @@ -15,8 +15,10 @@ * @package ImageAnalysis * @subpackage Tests */ -class ezcGraphFontTest extends ezcTestCase +class ezcGraphFontTest extends ezcImageTestCase { + protected $tempDir; + protected $basePath; public static function suite() @@ -26,9 +28,47 @@ class ezcGraphFontTest extends ezcTestCase protected function setUp() { + static $i = 0; + $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/'; $this->basePath = dirname( __FILE__ ) . '/data/'; } + protected function tearDown() + { + unset( $this->driver ); + if ( !$this->hasFailed() ) + { + $this->removeTempDir(); + } + } + + /** + * Compares a generated image with a stored file + * + * @param string $generated Filename of generated image + * @param string $compare Filename of stored image + * @return void + */ + protected function compare( $generated, $compare ) + { + $this->assertTrue( + file_exists( $generated ), + 'No image file has been created.' + ); + + $this->assertTrue( + file_exists( $compare ), + 'Comparision image does not exist.' + ); + + if ( md5_file( $generated ) !== md5_file( $compare ) ) + { + // Adding a diff makes no sense here, because created XML uses + // only two lines + $this->fail( 'Rendered image is not correct.'); + } + } + public function testSetGeneralFont() { $chart = new ezcGraphPieChart(); @@ -632,5 +672,219 @@ class ezcGraphFontTest extends ezcTestCase $this->fail( 'Expected ezcBasePropertyNotFoundException.' ); } + + public function testUTF8SpecialCharsSVG() + { + $filename = $this->tempDir . __FUNCTION__ . '.svg'; + + $driver = new ezcGraphSvgDriver(); + $driver->options->width = 200; + $driver->options->height = 100; + + $driver->drawPolygon( + array( + new ezcGraphCoordinate( 10, 10 ), + new ezcGraphCoordinate( 160, 10 ), + new ezcGraphCoordinate( 160, 80 ), + new ezcGraphCoordinate( 10, 80 ), + ), + ezcGraphColor::fromHex( '#eeeeec' ), + true + ); + $driver->drawTextBox( + 'öäüÖÄÜß', + new ezcGraphCoordinate( 10, 10 ), + 150, + 70, + ezcGraph::LEFT + ); + + $driver->render( $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' + ); + } + + public function testISO_8859_15SpecialCharsSVG() + { + $filename = $this->tempDir . __FUNCTION__ . '.svg'; + + $driver = new ezcGraphSvgDriver(); + $driver->options->width = 200; + $driver->options->height = 100; + $driver->options->encoding = 'ISO-8859-15'; + + $driver->drawPolygon( + array( + new ezcGraphCoordinate( 10, 10 ), + new ezcGraphCoordinate( 160, 10 ), + new ezcGraphCoordinate( 160, 80 ), + new ezcGraphCoordinate( 10, 80 ), + ), + ezcGraphColor::fromHex( '#eeeeec' ), + true + ); + $driver->drawTextBox( + mb_convert_encoding( 'öäüÖÄÜß', 'ISO-8859-15', 'UTF-8' ), + new ezcGraphCoordinate( 10, 10 ), + 150, + 70, + ezcGraph::LEFT + ); + + $driver->render( $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' + ); + } + + public function testUTF8SpecialCharsGD() + { + $filename = $this->tempDir . __FUNCTION__ . '.png'; + + $driver = new ezcGraphGdDriver(); + $driver->options->font->path = $this->basePath . 'font.ttf'; + $driver->options->width = 200; + $driver->options->height = 100; + + $driver->drawPolygon( + array( + new ezcGraphCoordinate( 10, 10 ), + new ezcGraphCoordinate( 160, 10 ), + new ezcGraphCoordinate( 160, 80 ), + new ezcGraphCoordinate( 10, 80 ), + ), + ezcGraphColor::fromHex( '#eeeeec' ), + true + ); + $return = $driver->drawTextBox( + 'öäüÖÄÜß', + new ezcGraphCoordinate( 10, 10 ), + 150, + 70, + ezcGraph::LEFT + ); + + $driver->render( $filename ); + + $this->assertImageSimilar( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.png', + 'Image does not look as expected.', + 2000 + ); + } + + public function testISO_8859_15SpecialCharsGD() + { + $filename = $this->tempDir . __FUNCTION__ . '.png'; + + $driver = new ezcGraphGdDriver(); + $driver->options->font->path = $this->basePath . 'font.ttf'; + $driver->options->width = 200; + $driver->options->height = 100; + + $driver->drawPolygon( + array( + new ezcGraphCoordinate( 10, 10 ), + new ezcGraphCoordinate( 160, 10 ), + new ezcGraphCoordinate( 160, 80 ), + new ezcGraphCoordinate( 10, 80 ), + ), + ezcGraphColor::fromHex( '#eeeeec' ), + true + ); + $return = $driver->drawTextBox( + mb_convert_encoding( 'öäüÖÄÜß', 'ISO-8859-15', 'UTF-8' ), + new ezcGraphCoordinate( 10, 10 ), + 150, + 70, + ezcGraph::LEFT + ); + + $driver->render( $filename ); + + $this->assertImageSimilar( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.png', + 'Image does not look as expected.', + 2000 + ); + } + + public function testUTF8SpecialCharsFlash() + { + $this->fail( 'No support for UTF-8 chars in SWFTextField. SWFText is not usable by driver for other reasons.' ); + $filename = $this->tempDir . __FUNCTION__ . '.swf'; + + $driver = new ezcGraphFlashDriver(); + $driver->options->font->path = $this->basePath . 'fdb_font.fdb'; + $driver->options->width = 200; + $driver->options->height = 100; + + $driver->drawPolygon( + array( + new ezcGraphCoordinate( 10, 10 ), + new ezcGraphCoordinate( 160, 10 ), + new ezcGraphCoordinate( 160, 80 ), + new ezcGraphCoordinate( 10, 80 ), + ), + ezcGraphColor::fromHex( '#eeeeec' ), + true + ); + $driver->drawTextBox( + 'öäüÖÄÜß', + new ezcGraphCoordinate( 10, 10 ), + 150, + 70, + ezcGraph::LEFT + ); + + $driver->render( $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.swf' + ); + } + + public function testISO_8859_15SpecialCharsFlash() + { + $filename = $this->tempDir . __FUNCTION__ . '.swf'; + + $driver = new ezcGraphFlashDriver(); + $driver->options->font->path = $this->basePath . 'fdb_font.fdb'; + $driver->options->width = 200; + $driver->options->height = 100; + + $driver->drawPolygon( + array( + new ezcGraphCoordinate( 10, 10 ), + new ezcGraphCoordinate( 160, 10 ), + new ezcGraphCoordinate( 160, 80 ), + new ezcGraphCoordinate( 10, 80 ), + ), + ezcGraphColor::fromHex( '#eeeeec' ), + true + ); + $driver->drawTextBox( + mb_convert_encoding( 'öäüÖÄÜß', 'ISO-8859-15', 'UTF-8' ), + new ezcGraphCoordinate( 10, 10 ), + 150, + 70, + ezcGraph::LEFT + ); + + $driver->render( $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.swf' + ); + } } ?> |