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 /src/driver/svg.php | |
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
Diffstat (limited to 'src/driver/svg.php')
-rw-r--r-- | src/driver/svg.php | 50 |
1 files changed, 46 insertions, 4 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' ); |