7253a06ccd2e4d9d695332d81d2c2d5bc8ec8dd0
[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, null, LIBXML_NOERROR | LIBXML_NOWARNING );
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 $keepReading = $this->reader->read();
71
72 /* Skip until first element */
73 while( $keepReading && $this->reader->nodeType != XmlReader::ELEMENT ) {
74 $keepReading = $this->reader->read();
75 }
76
77 if ( !$this->qualifiedNameEquals( $this->reader->name, 'svg', 'svg' ) ) {
78 throw new MWException( "Expected <svg> tag, got ".
79 $this->reader->name );
80 }
81 $this->debug( "<svg> tag is correct." );
82 $this->handleSVGAttribs();
83
84 $exitDepth = $this->reader->depth;
85 $keepReading = $this->reader->read();
86 $skip = false;
87 while ( $keepReading ) {
88 $tag = $this->reader->name;
89 $type = $this->reader->nodeType;
90
91 $this->debug( "$tag" );
92
93 if ( $this->qualifiedNameEquals( $tag, 'svg', 'svg' ) && $type == XmlReader::END_ELEMENT && $this->reader->depth <= $exitDepth ) {
94 break;
95 } elseif ( $this->qualifiedNameEquals( $tag, 'svg', 'title' ) ) {
96 $this->readField( $tag, 'title' );
97 } elseif ( $this->qualifiedNameEquals( $tag, 'svg', 'desc' ) ) {
98 $this->readField( $tag, 'description' );
99 } elseif ( $this->qualifiedNameEquals( $tag, 'svg', '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 );
104 //$skip = true;
105 }
106
107 if ($skip) {
108 $keepReading = $this->reader->next();
109 $skip = false;
110 $this->debug( "Skip" );
111 } else {
112 $keepReading = $this->reader->read();
113 }
114 }
115
116 $this->reader->close();
117
118 return true;
119 }
120
121 /*
122 * Read a textelement from an element
123 *
124 * @param String $name of the element that we are reading from
125 * @param String $metafield that we will fill with the result
126 */
127 private function readField( $name, $metafield=null ) {
128 $this->debug ( "Read field $metafield" );
129 if( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) {
130 return;
131 }
132 $keepReading = $this->reader->read();
133 while( $keepReading ) {
134 if( $this->reader->name == $name && $this->reader->nodeType == XmlReader::END_ELEMENT ) {
135 break;
136 } elseif( $this->reader->nodeType == XmlReader::TEXT ){
137 $this->metadata[$metafield] = $this->reader->value;
138 }
139 $keepReading = $this->reader->read();
140 }
141 }
142
143 /*
144 * Read an XML snippet from an element
145 *
146 * @param String $metafield that we will fill with the result
147 */
148 private function readXml( $metafield=null ) {
149 $this->debug ( "Read top level metadata" );
150 if( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) {
151 return;
152 }
153 // TODO: find and store type of xml snippet. metadata['metadataType'] = "rdf"
154 $this->metadata[$metafield] = $this->reader->readInnerXML();
155 $this->reader->next();
156 }
157
158 /*
159 * Filter all children, looking for animate elements
160 *
161 * @param String $name of the element that we are reading from
162 */
163 private function animateFilter( $name ) {
164 $this->debug ( "animate filter" );
165 if( $this->reader->nodeType != XmlReader::ELEMENT ) {
166 return;
167 }
168 $exitDepth = $this->reader->depth;
169 $keepReading = $this->reader->read();
170 while( $keepReading ) {
171 if( $this->reader->name == $name && $this->reader->depth <= $exitDepth
172 && $this->reader->nodeType == XmlReader::END_ELEMENT ) {
173 break;
174 } elseif ( $this->reader->nodeType == XmlReader::ELEMENT ) {
175 switch( $this->reader->name ) {
176 case 'animate':
177 case 'svg:animate':
178 case 'set':
179 case 'svg:set':
180 case 'animateMotion':
181 case 'svg:animateMotion':
182 case 'animateColor':
183 case 'svg:animateColor':
184 case 'animateTransform':
185 case 'svg:animateTransform':
186 $this->debug( "HOUSTON WE HAVE ANIMATION" );
187 $this->metadata['animated'] = true;
188 break;
189 }
190 }
191 $keepReading = $this->reader->read();
192 }
193 }
194
195 private function throwXmlError( $err ) {
196 $this->debug( "FAILURE: $err" );
197 wfDebug( "SVGReader XML error: $err\n" );
198 }
199
200 private function debug( $data ) {
201 if( $this->mDebug ) {
202 wfDebug( "SVGReader: $data\n" );
203 }
204 }
205
206 private function warn( $data ) {
207 wfDebug( "SVGReader: $data\n" );
208 }
209
210 private function notice( $data ) {
211 wfDebug( "SVGReader WARN: $data\n" );
212 }
213
214 /*
215 * Parse the attributes of an SVG element
216 *
217 * The parser has to be in the start element of <svg>
218 */
219 private function handleSVGAttribs( ) {
220 $defaultWidth = self::DEFAULT_WIDTH;
221 $defaultHeight = self::DEFAULT_HEIGHT;
222 $aspect = 1.0;
223 $width = null;
224 $height = null;
225
226 if( $this->reader->getAttribute('viewBox') ) {
227 // min-x min-y width height
228 $viewBox = preg_split( '/\s+/', trim( $this->reader->getAttribute('viewBox') ) );
229 if( count( $viewBox ) == 4 ) {
230 $viewWidth = $this->scaleSVGUnit( $viewBox[2] );
231 $viewHeight = $this->scaleSVGUnit( $viewBox[3] );
232 if( $viewWidth > 0 && $viewHeight > 0 ) {
233 $aspect = $viewWidth / $viewHeight;
234 $defaultHeight = $defaultWidth / $aspect;
235 }
236 }
237 }
238 if( $this->reader->getAttribute('width') ) {
239 $width = $this->scaleSVGUnit( $this->reader->getAttribute('width'), $defaultWidth );
240 }
241 if( $this->reader->getAttribute('height') ) {
242 $height = $this->scaleSVGUnit( $this->reader->getAttribute('height'), $defaultHeight );
243 }
244
245 if( !isset( $width ) && !isset( $height ) ) {
246 $width = $defaultWidth;
247 $height = $width / $aspect;
248 } elseif( isset( $width ) && !isset( $height ) ) {
249 $height = $width / $aspect;
250 } elseif( isset( $height ) && !isset( $width ) ) {
251 $width = $height * $aspect;
252 }
253
254 if( $width > 0 && $height > 0 ) {
255 $this->metadata['width'] = intval( round( $width ) );
256 $this->metadata['height'] = intval( round( $height ) );
257 }
258 }
259
260 /**
261 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
262 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
263 *
264 * @param $length String: CSS/SVG length.
265 * @param $viewportSize: Float optional scale for percentage units...
266 * @return float: length in pixels
267 */
268 static function scaleSVGUnit( $length, $viewportSize=512 ) {
269 static $unitLength = array(
270 'px' => 1.0,
271 'pt' => 1.25,
272 'pc' => 15.0,
273 'mm' => 3.543307,
274 'cm' => 35.43307,
275 'in' => 90.0,
276 'em' => 16.0, // fake it?
277 'ex' => 12.0, // fake it?
278 '' => 1.0, // "User units" pixels by default
279 );
280 $matches = array();
281 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
282 $length = floatval( $matches[1] );
283 $unit = $matches[2];
284 if( $unit == '%' ) {
285 return $length * 0.01 * $viewportSize;
286 } else {
287 return $length * $unitLength[$unit];
288 }
289 } else {
290 // Assume pixels
291 return floatval( $length );
292 }
293 }
294
295 /**
296 * XML namespace check
297 *
298 * Check if a read node name matches the expected nodeName
299 * @param $qualifiedName as read by XMLReader
300 * @param $prefix the namespace prefix that you expect for this element, defaults to svg namespace
301 * @param $localName the localName part of the element that you want to match
302 *
303 * @return boolean
304 */
305 private function qualifiedNameEquals( $qualifiedName, $prefix="svg", $localName ) {
306 if( ($qualifiedName == $localName && $prefix == "svg" ) ||
307 $qualifiedName == ($prefix . ":" . $localName) ) {
308 return true;
309 }
310 return false;
311 }
312 }