+ return floatval( $length );
+ }
+}
+
+class XmlSizeFilter {
+ const DEFAULT_WIDTH = 512;
+ const DEFAULT_HEIGHT = 512;
+ var $first = true;
+ var $width = self::DEFAULT_WIDTH;
+ var $height = self::DEFAULT_HEIGHT;
+ function filter( $name, $attribs ) {
+ if( $this->first ) {
+ $defaultWidth = self::DEFAULT_WIDTH;
+ $defaultHeight = self::DEFAULT_HEIGHT;
+ $aspect = 1.0;
+ $width = null;
+ $height = null;
+
+ if( isset( $attribs['viewBox'] ) ) {
+ // min-x min-y width height
+ $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) );
+ if( count( $viewBox ) == 4 ) {
+ $viewWidth = wfScaleSVGUnit( $viewBox[2] );
+ $viewHeight = wfScaleSVGUnit( $viewBox[3] );
+ if( $viewWidth > 0 && $viewHeight > 0 ) {
+ $aspect = $viewWidth / $viewHeight;
+ $defaultHeight = $defaultWidth / $aspect;
+ }
+ }
+ }
+ if( isset( $attribs['width'] ) ) {
+ $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
+ }
+ if( isset( $attribs['height'] ) ) {
+ $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
+ }
+
+ if( !isset( $width ) && !isset( $height ) ) {
+ $width = $defaultWidth;
+ $height = $width / $aspect;
+ } elseif( isset( $width ) && !isset( $height ) ) {
+ $height = $width / $aspect;
+ } elseif( isset( $height ) && !isset( $width ) ) {
+ $width = $height * $aspect;
+ }
+
+ if( $width > 0 && $height > 0 ) {
+ $this->width = intval( round( $width ) );
+ $this->height = intval( round( $height ) );
+ }
+
+ $this->first = false;
+ }