summaryrefslogtreecommitdiffstats
path: root/src/driver/svg.php
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2008-05-03 16:36:57 +0000
committerKore Nordmann <github@kore-nordmann.de>2008-05-03 16:36:57 +0000
commit5d4947a0cb1ba3bd7e4097b3da9b772dd16d4094 (patch)
tree088ede950f85e6af9aca1902bcc34a1a463e7d37 /src/driver/svg.php
parent92c7fc36f830d168e02cd26729d7f099bddce707 (diff)
downloadzetacomponents-graph-5d4947a0cb1ba3bd7e4097b3da9b772dd16d4094.zip
zetacomponents-graph-5d4947a0cb1ba3bd7e4097b3da9b772dd16d4094.tar.gz
- Implemented feature #10957: Embed glyphs for exact SVG font width estimation
# Lots of binary diffs, because font-family names are now enclosed in ' in SVG # files.
Diffstat (limited to 'src/driver/svg.php')
-rw-r--r--src/driver/svg.php51
1 files changed, 37 insertions, 14 deletions
diff --git a/src/driver/svg.php b/src/driver/svg.php
index d49ab45..9a814f1 100644
--- a/src/driver/svg.php
+++ b/src/driver/svg.php
@@ -100,6 +100,13 @@ class ezcGraphSvgDriver extends ezcGraphDriver
protected $elementID = 0;
/**
+ * Font storage for SVG font glyphs and kernings.
+ *
+ * @var ezcGraphSvgFont
+ */
+ protected $font = null;
+
+ /**
* Constructor
*
* @param array $options Default option array
@@ -110,6 +117,7 @@ class ezcGraphSvgDriver extends ezcGraphDriver
{
ezcBase::checkDependency( 'Graph', ezcBase::DEP_PHP_EXTENSION, 'dom' );
$this->options = new ezcGraphSvgDriverOptions( $options );
+ $this->font = new ezcGraphSvgFont();
}
/**
@@ -136,7 +144,6 @@ class ezcGraphSvgDriver extends ezcGraphDriver
if ( $this->options->templateDocument !== false )
{
-// @TODO: Add $this->dom->format
$this->dom->load( $this->options->templateDocument );
$this->defs = $this->dom->getElementsByTagName( 'defs' )->item( 0 );
@@ -450,12 +457,25 @@ class ezcGraphSvgDriver extends ezcGraphDriver
*/
protected function getTextBoundings( $size, ezcGraphFontOptions $font, $text )
{
- return new ezcGraphBoundings(
- 0,
- 0,
- $this->getTextWidth( $text, $size ),
- $size
- );
+ if ( $font->type === ezcGraph::SVG_FONT )
+ {
+ return new ezcGraphBoundings(
+ 0,
+ 0,
+ $this->font->calculateStringWidth( $font->path, $text ) * $size,
+ $size
+ );
+ }
+ else
+ {
+ // If we didn't get a SVG font, continue guessing the font width.
+ return new ezcGraphBoundings(
+ 0,
+ 0,
+ $this->getTextWidth( $text, $size ),
+ $size
+ );
+ }
}
/**
@@ -655,7 +675,7 @@ class ezcGraphSvgDriver extends ezcGraphDriver
foreach ( $text['text'] as $line )
{
$string = implode( ' ', $line );
- if ( ( $strWidth = $this->getTextWidth( $string, $size ) ) > $width )
+ if ( ( $strWidth = $this->getTextBoundings( $size, $text['font'], $string )->width ) > $width )
{
$width = $strWidth;
}
@@ -767,13 +787,13 @@ class ezcGraphSvgDriver extends ezcGraphDriver
break;
case ( $text['align'] & ezcGraph::RIGHT ):
$position = new ezcGraphCoordinate(
- $text['position']->x + ( $text['width'] - $this->getTextWidth( $string, $size ) ),
+ $text['position']->x + ( $text['width'] - $this->getTextBoundings( $size, $text['font'], $string )->width ),
$text['position']->y + $yOffset
);
break;
case ( $text['align'] & ezcGraph::CENTER ):
$position = new ezcGraphCoordinate(
- $text['position']->x + ( ( $text['width'] - $this->getTextWidth( $string, $size ) ) / 2 ),
+ $text['position']->x + ( ( $text['width'] - $this->getTextBoundings( $size, $text['font'], $string )->width ) / 2 ),
$text['position']->y + $yOffset
);
break;
@@ -785,12 +805,12 @@ class ezcGraphSvgDriver extends ezcGraphDriver
$textNode = $this->dom->createElement( 'text', $this->encode( $string ) );
$textNode->setAttribute( 'id', $text['id'] . '_shadow' );
$textNode->setAttribute( 'x', sprintf( '%.4F', $position->x + $this->options->graphOffset->x + $text['font']->textShadowOffset ) );
- $textNode->setAttribute( 'text-length', sprintf( '%.4Fpx', $this->getTextWidth( $string, $size ) ) );
+ $textNode->setAttribute( 'text-length', sprintf( '%.4Fpx', $this->getTextBoundings( $size, $text['font'], $string )->width ) );
$textNode->setAttribute( 'y', sprintf( '%.4F', $position->y + $this->options->graphOffset->y + $text['font']->textShadowOffset ) );
$textNode->setAttribute(
'style',
sprintf(
- 'font-size: %dpx; font-family: %s; fill: #%02x%02x%02x; fill-opacity: %.2F; stroke: none;',
+ 'font-size: %dpx; font-family: \'%s\'; fill: #%02x%02x%02x; fill-opacity: %.2F; stroke: none;',
$size,
$text['font']->name,
$text['font']->textShadowColor->red,
@@ -806,12 +826,12 @@ class ezcGraphSvgDriver extends ezcGraphDriver
$textNode = $this->dom->createElement( 'text', $this->encode( $string ) );
$textNode->setAttribute( 'id', $text['id'] . '_text' );
$textNode->setAttribute( 'x', sprintf( '%.4F', $position->x + $this->options->graphOffset->x ) );
- $textNode->setAttribute( 'text-length', sprintf( '%.4Fpx', $this->getTextWidth( $string, $size ) ) );
+ $textNode->setAttribute( 'text-length', sprintf( '%.4Fpx', $this->getTextBoundings( $size, $text['font'], $string )->width ) );
$textNode->setAttribute( 'y', sprintf( '%.4F', $position->y + $this->options->graphOffset->y ) );
$textNode->setAttribute(
'style',
sprintf(
- 'font-size: %dpx; font-family: %s; fill: #%02x%02x%02x; fill-opacity: %.2F; stroke: none;',
+ 'font-size: %dpx; font-family: \'%s\'; fill: #%02x%02x%02x; fill-opacity: %.2F; stroke: none;',
$size,
$text['font']->name,
$text['font']->color->red,
@@ -1186,6 +1206,9 @@ class ezcGraphSvgDriver extends ezcGraphDriver
{
$this->createDocument();
$this->drawAllTexts();
+
+ // Embed used glyphs
+ $this->font->addFontToDocument( $this->dom );
$this->dom->save( $file );
}
}
OpenPOWER on IntegriCloud