* The SVGMetadataExtractor now based on XmlReader
[lhc/web/wiklou.git] / includes / media / SVGMetadataExtractor.php
1 <?php
2 /**
3 * SVGMetadataExtractor.php
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
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
26 */
27
28 class SVGMetadataExtractor {
29 static function getMetadata( $filename ) {
30 $svg = new SVGReader( $filename );
31 return $svg->getMetadata();
32 }
33 }
34
35 class SVGReader {
36 const DEFAULT_WIDTH = 512;
37 const DEFAULT_HEIGHT = 512;
38
39 private $reader = null;
40 private $mDebug = false;
41 private $metadata = Array();
42
43 /**
44 * Constructor
45 *
46 * Creates an SVGReader drawing from the source provided
47 * @param $source String: URI from which to read
48 */
49 function __construct( $source ) {
50 $this->reader = new XMLReader();
51 $this->reader->open( $source );
52
53 $this->metadata['width'] = self::DEFAULT_WIDTH;
54 $this->metadata['height'] = self::DEFAULT_HEIGHT;
55
56 $this->read();
57 }
58
59 /*
60 * @return Array with the known metadata
61 */
62 public function getMetadata() {
63 return $this->metadata;
64 }
65
66 /*
67 * Read the SVG
68 */
69 public function read() {
70 $this->reader->read();
71
72 if ( $this->reader->name != 'svg' ) {
73 throw new MWException( "Expected <svg> tag, got ".
74 $this->reader->name );
75 }
76 $this->debug( "<svg> tag is correct." );
77
78 $this->debug( "Starting primary dump processing loop." );
79 $this->handleSVGAttribs();
80 $exitDepth = $this->reader->depth;
81
82 $keepReading = $this->reader->read();
83 $skip = false;
84 while ( $keepReading ) {
85 $tag = $this->reader->name;
86 $type = $this->reader->nodeType;
87
88 $this->debug( "$tag" );
89
90 if ( $tag == 'svg' && $type == XmlReader::END_ELEMENT && $this->reader->depth <= $exitDepth ) {
91 break;
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 );
101 //$skip = true;
102 }
103
104 if ($skip) {
105 $keepReading = $this->reader->next();
106 $skip = false;
107 $this->debug( "Skip" );
108 } else {
109 $keepReading = $this->reader->read();
110 }
111 }
112
113 return true;
114 }
115
116 /*
117 * Read a textelement from an element
118 *
119 * @param String $name of the element that we are reading from
120 * @param String $metafield that we will fill with the result
121 */
122 private function readField( $name, $metafield=null ) {
123 $this->debug ( "Read field $metafield" );
124 if( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) {
125 return;
126 }
127 $keepReading = $this->reader->read();
128 while( $keepReading ) {
129 if( $this->reader->name == $name && $this->reader->nodeType == XmlReader::END_ELEMENT ) {
130 $keepReading = false;
131 break;
132 } elseif( $this->reader->nodeType == XmlReader::TEXT ){
133 $this->metadata[$metafield] = $this->reader->value;
134 }
135 $keepReading = $this->reader->read();
136 }
137 }
138
139 /*
140 * Read an XML snippet from an element
141 *
142 * @param String $metafield that we will fill with the result
143 */
144 private function readXml( $metafield=null ) {
145 $this->debug ( "Read top level metadata" );
146 if( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) {
147 return;
148 }
149 // TODO: find and store type of xml snippet. metadata['metadataType'] = "rdf"
150 $this->metadata[$metafield] = $this->reader->readInnerXML();
151 $this->reader->next();
152 }
153
154 /*
155 * Filter all children, looking for animate elements
156 *
157 * @param String $name of the element that we are reading from
158 */
159 private function animateFilter( $name ) {
160 $this->debug ( "animate filter" );
161 if( $this->reader->nodeType != XmlReader::ELEMENT ) {
162 return;
163 }
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;
170 break;
171 } elseif( $this->reader->nodeType == XmlReader::ELEMENT ){
172 switch( $this->reader->name ) {
173 case 'animate':
174 case 'set':
175 case 'animateMotion':
176 case 'animateColor':
177 case 'animateTransform':
178 $this->debug( "HOUSTON WE HAVE ANIMATION" );
179 $this->metadata['animated'] = true;
180 break;
181 }
182 }
183 $keepReading = $this->reader->read();
184 }
185 }
186
187 private function throwXmlError( $err ) {
188 $this->debug( "FAILURE: $err" );
189 wfDebug( "SVGReader XML error: $err\n" );
190 }
191
192 private function debug( $data ) {
193 if( $this->mDebug ) {
194 wfDebug( "SVGReader: $data\n" );
195 }
196 }
197
198 private function warn( $data ) {
199 wfDebug( "SVGReader: $data\n" );
200 }
201
202 private function notice( $data ) {
203 wfDebug( "SVGReader WARN: $data\n" );
204 }
205
206 /*
207 * Parse the attributes of an SVG element
208 *
209 * The parser has to be in the start element of <svg>
210 */
211 private function handleSVGAttribs( ) {
212 $defaultWidth = self::DEFAULT_WIDTH;
213 $defaultHeight = self::DEFAULT_HEIGHT;
214 $aspect = 1.0;
215 $width = null;
216 $height = null;
217
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;
227 }
228 }
229 }
230 if( $this->reader->getAttribute('width') ) {
231 $width = $this->scaleSVGUnit( $this->reader->getAttribute('width'), $defaultWidth );
232 }
233 if( $this->reader->getAttribute('height') ) {
234 $height = $this->scaleSVGUnit( $this->reader->getAttribute('height'), $defaultHeight );
235 }
236
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;
244 }
245
246 if( $width > 0 && $height > 0 ) {
247 $this->metadata['width'] = intval( round( $width ) );
248 $this->metadata['height'] = intval( round( $height ) );
249 }
250 }
251
252 /**
253 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
254 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
255 *
256 * @param $length String: CSS/SVG length.
257 * @param $viewportSize: Float optional scale for percentage units...
258 * @return float: length in pixels
259 */
260 static function scaleSVGUnit( $length, $viewportSize=512 ) {
261 static $unitLength = array(
262 'px' => 1.0,
263 'pt' => 1.25,
264 'pc' => 15.0,
265 'mm' => 3.543307,
266 'cm' => 35.43307,
267 'in' => 90.0,
268 'em' => 16.0, // fake it?
269 'ex' => 12.0, // fake it?
270 '' => 1.0, // "User units" pixels by default
271 );
272 $matches = array();
273 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
274 $length = floatval( $matches[1] );
275 $unit = $matches[2];
276 if( $unit == '%' ) {
277 return $length * 0.01 * $viewportSize;
278 } else {
279 return $length * $unitLength[$unit];
280 }
281 } else {
282 // Assume pixels
283 return floatval( $length );
284 }
285 }
286 }