Index: trunk/phase3/includes/ImageFunctions.php |
— | — | @@ -4,9 +4,10 @@ |
5 | 5 | * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers |
6 | 6 | * |
7 | 7 | * @param $length String: CSS/SVG length. |
| 8 | + * @param $viewpoerSize: Float optional scale for percentage units... |
8 | 9 | * @return Integer: length in pixels |
9 | 10 | */ |
10 | | -function wfScaleSVGUnit( $length ) { |
| 11 | +function wfScaleSVGUnit( $length, $viewportSize=512 ) { |
11 | 12 | static $unitLength = array( |
12 | 13 | 'px' => 1.0, |
13 | 14 | 'pt' => 1.25, |
— | — | @@ -14,14 +15,19 @@ |
15 | 16 | 'mm' => 3.543307, |
16 | 17 | 'cm' => 35.43307, |
17 | 18 | 'in' => 90.0, |
| 19 | + 'em' => 16.0, // fake it? |
| 20 | + 'ex' => 12.0, // fake it? |
18 | 21 | '' => 1.0, // "User units" pixels by default |
19 | | - '%' => 2.0, // Fake it! |
20 | 22 | ); |
21 | 23 | $matches = array(); |
22 | 24 | if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) { |
23 | 25 | $length = floatval( $matches[1] ); |
24 | 26 | $unit = $matches[2]; |
25 | | - return round( $length * $unitLength[$unit] ); |
| 27 | + if( $unit == '%' ) { |
| 28 | + return round( $length * 0.01 * $viewportSize ); |
| 29 | + } else { |
| 30 | + return round( $length * $unitLength[$unit] ); |
| 31 | + } |
26 | 32 | } else { |
27 | 33 | // Assume pixels |
28 | 34 | return round( floatval( $length ) ); |
— | — | @@ -29,17 +35,49 @@ |
30 | 36 | } |
31 | 37 | |
32 | 38 | class XmlSizeFilter { |
| 39 | + const DEFAULT_WIDTH = 512; |
| 40 | + const DEFAULT_HEIGHT = 512; |
33 | 41 | var $first = true; |
34 | | - var $width = 256; |
35 | | - var $height = 256; |
| 42 | + var $width = self::DEFAULT_WIDTH; |
| 43 | + var $height = self::DEFAULT_HEIGHT; |
36 | 44 | function filter( $name, $attribs ) { |
37 | 45 | if( $this->first ) { |
| 46 | + $defaultWidth = self::DEFAULT_WIDTH; |
| 47 | + $defaultHeight = self::DEFAULT_HEIGHT; |
| 48 | + $aspect = 1.0; |
| 49 | + $width = null; |
| 50 | + $height = null; |
| 51 | + |
| 52 | + if( isset( $attribs['viewBox'] ) ) { |
| 53 | + // min-x min-y width height |
| 54 | + $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) ); |
| 55 | + if( count( $viewBox ) == 4 ) { |
| 56 | + $viewWidth = wfScaleSVGUnit( $viewBox[2] ); |
| 57 | + $viewHeight = wfScaleSVGUnit( $viewBox[3] ); |
| 58 | + if( $viewWidth > 0 && $viewHeight > 0 ) { |
| 59 | + $aspect = $viewWidth / $viewHeight; |
| 60 | + $defaultHeight = $defaultWidth / $aspect; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
38 | 64 | if( isset( $attribs['width'] ) ) { |
39 | | - $this->width = wfScaleSVGUnit( $attribs['width'] ); |
| 65 | + $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth ); |
40 | 66 | } |
41 | 67 | if( isset( $attribs['height'] ) ) { |
42 | | - $this->height = wfScaleSVGUnit( $attribs['height'] ); |
| 68 | + $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight ); |
43 | 69 | } |
| 70 | + |
| 71 | + if( !isset( $width ) && !isset( $height ) ) { |
| 72 | + $width = $defaultWidth; |
| 73 | + $height = $width / $aspect; |
| 74 | + } elseif( isset( $width ) && !isset( $height ) ) { |
| 75 | + $height = $width / $aspect; |
| 76 | + } elseif( isset( $height ) && !isset( $width ) ) { |
| 77 | + $width = $height * $aspect; |
| 78 | + } |
| 79 | + |
| 80 | + $this->width = $width; |
| 81 | + $this->height = $height; |
44 | 82 | $this->first = false; |
45 | 83 | } |
46 | 84 | } |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -422,6 +422,8 @@ |
423 | 423 | Arabic script (Persian, Urdu, Sindhi, Punjabi) |
424 | 424 | * (bug 16656) cleanupTitles and friends should now work in load-balanced |
425 | 425 | DB environments when $wgDBserver isn't set. |
| 426 | +* (bug 3691) Aspect ratio from viewBox attribute is now preserved for SVG |
| 427 | + images which do not specify width and height attributes. |
426 | 428 | |
427 | 429 | === API changes in 1.14 === |
428 | 430 | |