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 $this->reader
->read();
72 if ( $this->reader
->name
!= 'svg' ) {
73 throw new MWException( "Expected <svg> tag, got ".
74 $this->reader
->name
);
76 $this->debug( "<svg> tag is correct." );
78 $this->debug( "Starting primary dump processing loop." );
79 $this->handleSVGAttribs();
80 $exitDepth = $this->reader
->depth
;
82 $keepReading = $this->reader
->read();
84 while ( $keepReading ) {
85 $tag = $this->reader
->name
;
86 $type = $this->reader
->nodeType
;
88 $this->debug( "$tag" );
90 if ( $tag == 'svg' && $type == XmlReader
::END_ELEMENT
&& $this->reader
->depth
<= $exitDepth ) {
92 } elseif ( $tag == 'title' ) {
93 $this->readField( $tag, 'title' );
94 } elseif ( $tag == 'desc' ) {
95 $this->readField( $tag, 'description' );
96 } elseif ( $tag == 'metadata' && $type == XmlReader
::ELEMENT
) {
97 $this->readXml( $tag, 'metadata' );
98 } elseif ( $tag !== '#text' ) {
99 $this->debug( "Unhandled top-level XML tag $tag" );
100 $this->animateFilter( $tag );
105 $keepReading = $this->reader
->next();
107 $this->debug( "Skip" );
109 $keepReading = $this->reader
->read();
117 * Read a textelement from an element
119 * @param String $name of the element that we are reading from
120 * @param String $metafield that we will fill with the result
122 private function readField( $name, $metafield=null ) {
123 $this->debug ( "Read field $metafield" );
124 if( !$metafield ||
$this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
127 $keepReading = $this->reader
->read();
128 while( $keepReading ) {
129 if( $this->reader
->name
== $name && $this->reader
->nodeType
== XmlReader
::END_ELEMENT
) {
130 $keepReading = false;
132 } elseif( $this->reader
->nodeType
== XmlReader
::TEXT
){
133 $this->metadata
[$metafield] = $this->reader
->value
;
135 $keepReading = $this->reader
->read();
140 * Read an XML snippet from an element
142 * @param String $metafield that we will fill with the result
144 private function readXml( $metafield=null ) {
145 $this->debug ( "Read top level metadata" );
146 if( !$metafield ||
$this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
149 // TODO: find and store type of xml snippet. metadata['metadataType'] = "rdf"
150 $this->metadata
[$metafield] = $this->reader
->readInnerXML();
151 $this->reader
->next();
155 * Filter all children, looking for animate elements
157 * @param String $name of the element that we are reading from
159 private function animateFilter( $name ) {
160 $this->debug ( "animate filter" );
161 if( $this->reader
->nodeType
!= XmlReader
::ELEMENT
) {
164 $exitDepth = $this->reader
->depth
;
165 $keepReading = $this->reader
->read();
166 while( $keepReading ) {
167 if( $this->reader
->name
== $name && $this->reader
->depth
<= $exitDepth
168 && $this->reader
->nodeType
== XmlReader
::END_ELEMENT
) {
169 $keepReading = false;
171 } elseif( $this->reader
->nodeType
== XmlReader
::ELEMENT
){
172 switch( $this->reader
->name
) {
175 case 'animateMotion':
177 case 'animateTransform':
178 $this->debug( "HOUSTON WE HAVE ANIMATION" );
179 $this->metadata
['animated'] = true;
183 $keepReading = $this->reader
->read();
187 private function throwXmlError( $err ) {
188 $this->debug( "FAILURE: $err" );
189 wfDebug( "SVGReader XML error: $err\n" );
192 private function debug( $data ) {
193 if( $this->mDebug
) {
194 wfDebug( "SVGReader: $data\n" );
198 private function warn( $data ) {
199 wfDebug( "SVGReader: $data\n" );
202 private function notice( $data ) {
203 wfDebug( "SVGReader WARN: $data\n" );
207 * Parse the attributes of an SVG element
209 * The parser has to be in the start element of <svg>
211 private function handleSVGAttribs( ) {
212 $defaultWidth = self
::DEFAULT_WIDTH
;
213 $defaultHeight = self
::DEFAULT_HEIGHT
;
218 if( $this->reader
->getAttribute('viewBox') ) {
219 // min-x min-y width height
220 $viewBox = preg_split( '/\s+/', trim( $this->reader
->getAttribute('viewBox') ) );
221 if( count( $viewBox ) == 4 ) {
222 $viewWidth = $this->scaleSVGUnit( $viewBox[2] );
223 $viewHeight = $this->scaleSVGUnit( $viewBox[3] );
224 if( $viewWidth > 0 && $viewHeight > 0 ) {
225 $aspect = $viewWidth / $viewHeight;
226 $defaultHeight = $defaultWidth / $aspect;
230 if( $this->reader
->getAttribute('width') ) {
231 $width = $this->scaleSVGUnit( $this->reader
->getAttribute('width'), $defaultWidth );
233 if( $this->reader
->getAttribute('height') ) {
234 $height = $this->scaleSVGUnit( $this->reader
->getAttribute('height'), $defaultHeight );
237 if( !isset( $width ) && !isset( $height ) ) {
238 $width = $defaultWidth;
239 $height = $width / $aspect;
240 } elseif( isset( $width ) && !isset( $height ) ) {
241 $height = $width / $aspect;
242 } elseif( isset( $height ) && !isset( $width ) ) {
243 $width = $height * $aspect;
246 if( $width > 0 && $height > 0 ) {
247 $this->metadata
['width'] = intval( round( $width ) );
248 $this->metadata
['height'] = intval( round( $height ) );
253 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
254 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
256 * @param $length String: CSS/SVG length.
257 * @param $viewportSize: Float optional scale for percentage units...
258 * @return float: length in pixels
260 static function scaleSVGUnit( $length, $viewportSize=512 ) {
261 static $unitLength = array(
268 'em' => 16.0, // fake it?
269 'ex' => 12.0, // fake it?
270 '' => 1.0, // "User units" pixels by default
273 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
274 $length = floatval( $matches[1] );
277 return $length * 0.01 * $viewportSize;
279 return $length * $unitLength[$unit];
283 return floatval( $length );