3 * SVGMetadataExtractor.php
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * @author Derk-Jan Hartman <hartman _at_ videolan d0t org>
23 * @author Brion Vibber
24 * @copyright Copyright © 2010-2010 Brion Vibber, Derk-Jan Hartman
25 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
28 class SVGMetadataExtractor
{
29 static function getMetadata( $filename ) {
30 $svg = new SVGReader( $filename );
31 return $svg->getMetadata();
36 const DEFAULT_WIDTH
= 512;
37 const DEFAULT_HEIGHT
= 512;
39 private $reader = null;
40 private $mDebug = false;
41 private $metadata = Array();
46 * Creates an SVGReader drawing from the source provided
47 * @param $source String: URI from which to read
49 function __construct( $source ) {
50 $this->reader
= new XMLReader();
51 $this->reader
->open( $source );
53 $this->metadata
['width'] = self
::DEFAULT_WIDTH
;
54 $this->metadata
['height'] = self
::DEFAULT_HEIGHT
;
60 * @return Array with the known metadata
62 public function getMetadata() {
63 return $this->metadata
;
69 public function read() {
70 $keepReading = $this->reader
->read();
72 /* Skip until first element */
73 while( $keepReading && $this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
74 $keepReading = $this->reader
->read();
77 if ( $this->reader
->name
!= 'svg' ) {
78 throw new MWException( "Expected <svg> tag, got ".
79 $this->reader
->name
);
81 $this->debug( "<svg> tag is correct." );
82 $this->handleSVGAttribs();
84 $exitDepth = $this->reader
->depth
;
85 $keepReading = $this->reader
->read();
87 while ( $keepReading ) {
88 $tag = $this->reader
->name
;
89 $type = $this->reader
->nodeType
;
91 $this->debug( "$tag" );
93 if ( $tag == 'svg' && $type == XmlReader
::END_ELEMENT
&& $this->reader
->depth
<= $exitDepth ) {
95 } elseif ( $tag == 'title' ) {
96 $this->readField( $tag, 'title' );
97 } elseif ( $tag == 'desc' ) {
98 $this->readField( $tag, 'description' );
99 } elseif ( $tag == 'metadata' && $type == XmlReader
::ELEMENT
) {
100 $this->readXml( $tag, 'metadata' );
101 } elseif ( $tag !== '#text' ) {
102 $this->debug( "Unhandled top-level XML tag $tag" );
103 $this->animateFilter( $tag );
108 $keepReading = $this->reader
->next();
110 $this->debug( "Skip" );
112 $keepReading = $this->reader
->read();
120 * Read a textelement from an element
122 * @param String $name of the element that we are reading from
123 * @param String $metafield that we will fill with the result
125 private function readField( $name, $metafield=null ) {
126 $this->debug ( "Read field $metafield" );
127 if( !$metafield ||
$this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
130 $keepReading = $this->reader
->read();
131 while( $keepReading ) {
132 if( $this->reader
->name
== $name && $this->reader
->nodeType
== XmlReader
::END_ELEMENT
) {
133 $keepReading = false;
135 } elseif( $this->reader
->nodeType
== XmlReader
::TEXT
){
136 $this->metadata
[$metafield] = $this->reader
->value
;
138 $keepReading = $this->reader
->read();
143 * Read an XML snippet from an element
145 * @param String $metafield that we will fill with the result
147 private function readXml( $metafield=null ) {
148 $this->debug ( "Read top level metadata" );
149 if( !$metafield ||
$this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
152 // TODO: find and store type of xml snippet. metadata['metadataType'] = "rdf"
153 $this->metadata
[$metafield] = $this->reader
->readInnerXML();
154 $this->reader
->next();
158 * Filter all children, looking for animate elements
160 * @param String $name of the element that we are reading from
162 private function animateFilter( $name ) {
163 $this->debug ( "animate filter" );
164 if( $this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
167 $exitDepth = $this->reader
->depth
;
168 $keepReading = $this->reader
->read();
169 while( $keepReading ) {
170 if( $this->reader
->name
== $name && $this->reader
->depth
<= $exitDepth
171 && $this->reader
->nodeType
== XmlReader
::END_ELEMENT
) {
172 $keepReading = false;
174 } elseif( $this->reader
->nodeType
== XmlReader
::ELEMENT
){
175 switch( $this->reader
->name
) {
178 case 'animateMotion':
180 case 'animateTransform':
181 $this->debug( "HOUSTON WE HAVE ANIMATION" );
182 $this->metadata
['animated'] = true;
186 $keepReading = $this->reader
->read();
190 private function throwXmlError( $err ) {
191 $this->debug( "FAILURE: $err" );
192 wfDebug( "SVGReader XML error: $err\n" );
195 private function debug( $data ) {
196 if( $this->mDebug
) {
197 wfDebug( "SVGReader: $data\n" );
201 private function warn( $data ) {
202 wfDebug( "SVGReader: $data\n" );
205 private function notice( $data ) {
206 wfDebug( "SVGReader WARN: $data\n" );
210 * Parse the attributes of an SVG element
212 * The parser has to be in the start element of <svg>
214 private function handleSVGAttribs( ) {
215 $defaultWidth = self
::DEFAULT_WIDTH
;
216 $defaultHeight = self
::DEFAULT_HEIGHT
;
221 if( $this->reader
->getAttribute('viewBox') ) {
222 // min-x min-y width height
223 $viewBox = preg_split( '/\s+/', trim( $this->reader
->getAttribute('viewBox') ) );
224 if( count( $viewBox ) == 4 ) {
225 $viewWidth = $this->scaleSVGUnit( $viewBox[2] );
226 $viewHeight = $this->scaleSVGUnit( $viewBox[3] );
227 if( $viewWidth > 0 && $viewHeight > 0 ) {
228 $aspect = $viewWidth / $viewHeight;
229 $defaultHeight = $defaultWidth / $aspect;
233 if( $this->reader
->getAttribute('width') ) {
234 $width = $this->scaleSVGUnit( $this->reader
->getAttribute('width'), $defaultWidth );
236 if( $this->reader
->getAttribute('height') ) {
237 $height = $this->scaleSVGUnit( $this->reader
->getAttribute('height'), $defaultHeight );
240 if( !isset( $width ) && !isset( $height ) ) {
241 $width = $defaultWidth;
242 $height = $width / $aspect;
243 } elseif( isset( $width ) && !isset( $height ) ) {
244 $height = $width / $aspect;
245 } elseif( isset( $height ) && !isset( $width ) ) {
246 $width = $height * $aspect;
249 if( $width > 0 && $height > 0 ) {
250 $this->metadata
['width'] = intval( round( $width ) );
251 $this->metadata
['height'] = intval( round( $height ) );
256 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
257 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
259 * @param $length String: CSS/SVG length.
260 * @param $viewportSize: Float optional scale for percentage units...
261 * @return float: length in pixels
263 static function scaleSVGUnit( $length, $viewportSize=512 ) {
264 static $unitLength = array(
271 'em' => 16.0, // fake it?
272 'ex' => 12.0, // fake it?
273 '' => 1.0, // "User units" pixels by default
276 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
277 $length = floatval( $matches[1] );
280 return $length * 0.01 * $viewportSize;
282 return $length * $unitLength[$unit];
286 return floatval( $length );