r44651 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44650‎ | r44651 | r44652 >
Date:05:39, 16 December 2008
Author:brion
Status:ok
Tags:
Comment:
* (bug 3691) Aspect ratio from viewBox attribute is now preserved for SVG
images which do not specify width and height attributes.

Examples from bugs:
http://commons.wikimedia.org/wiki/Image:Gcj.svg
http://commons.wikimedia.org/wiki/File:Providence_Metro_Area.svg
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/ImageFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ImageFunctions.php
@@ -4,9 +4,10 @@
55 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
66 *
77 * @param $length String: CSS/SVG length.
 8+ * @param $viewpoerSize: Float optional scale for percentage units...
89 * @return Integer: length in pixels
910 */
10 -function wfScaleSVGUnit( $length ) {
 11+function wfScaleSVGUnit( $length, $viewportSize=512 ) {
1112 static $unitLength = array(
1213 'px' => 1.0,
1314 'pt' => 1.25,
@@ -14,14 +15,19 @@
1516 'mm' => 3.543307,
1617 'cm' => 35.43307,
1718 'in' => 90.0,
 19+ 'em' => 16.0, // fake it?
 20+ 'ex' => 12.0, // fake it?
1821 '' => 1.0, // "User units" pixels by default
19 - '%' => 2.0, // Fake it!
2022 );
2123 $matches = array();
2224 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
2325 $length = floatval( $matches[1] );
2426 $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+ }
2632 } else {
2733 // Assume pixels
2834 return round( floatval( $length ) );
@@ -29,17 +35,49 @@
3036 }
3137
3238 class XmlSizeFilter {
 39+ const DEFAULT_WIDTH = 512;
 40+ const DEFAULT_HEIGHT = 512;
3341 var $first = true;
34 - var $width = 256;
35 - var $height = 256;
 42+ var $width = self::DEFAULT_WIDTH;
 43+ var $height = self::DEFAULT_HEIGHT;
3644 function filter( $name, $attribs ) {
3745 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+ }
3864 if( isset( $attribs['width'] ) ) {
39 - $this->width = wfScaleSVGUnit( $attribs['width'] );
 65+ $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
4066 }
4167 if( isset( $attribs['height'] ) ) {
42 - $this->height = wfScaleSVGUnit( $attribs['height'] );
 68+ $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
4369 }
 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;
4482 $this->first = false;
4583 }
4684 }
Index: trunk/phase3/RELEASE-NOTES
@@ -422,6 +422,8 @@
423423 Arabic script (Persian, Urdu, Sindhi, Punjabi)
424424 * (bug 16656) cleanupTitles and friends should now work in load-balanced
425425 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.
426428
427429 === API changes in 1.14 ===
428430

Follow-up revisions

RevisionCommit summaryAuthorDate
r44652Follow-up to r44651 -- stay in floats until we finish the SVG size, so aspect...brion05:45, 16 December 2008

Status & tagging log