From 77776c943fc919f084b498986733645bf0112a2b Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Tue, 7 Aug 2018 21:47:24 +0200 Subject: [PATCH] SVG: SVG unit parser support for wider range of number - Add support for radix leading numbers like 0.41 - Add support for scientific number notation - Add support for plus and minus signs - Add testcases for SVGUnit parsing and scaling Bug: T201274 Change-Id: I0446b19b0f22851d968c991007e73255f309f4bc --- includes/media/SVGMetadataExtractor.php | 6 ++- .../media/SVGMetadataExtractorTest.php | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/includes/media/SVGMetadataExtractor.php b/includes/media/SVGMetadataExtractor.php index e00a5b380c..ee467b084f 100644 --- a/includes/media/SVGMetadataExtractor.php +++ b/includes/media/SVGMetadataExtractor.php @@ -380,7 +380,11 @@ class SVGReader { '' => 1.0, // "User units" pixels by default ]; $matches = []; - if ( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) { + if ( preg_match( + '/^\s*([-+]?\d*(?:\.\d+|\d+)(?:[Ee][-+]?\d+)?)\s*(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', + $length, + $matches + ) ) { $length = floatval( $matches[1] ); $unit = $matches[2]; if ( $unit == '%' ) { diff --git a/tests/phpunit/includes/media/SVGMetadataExtractorTest.php b/tests/phpunit/includes/media/SVGMetadataExtractorTest.php index 6fbb47401b..c258b6a745 100644 --- a/tests/phpunit/includes/media/SVGMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/SVGMetadataExtractorTest.php @@ -26,6 +26,17 @@ class SVGMetadataExtractorTest extends MediaWikiTestCase { $this->assertMetadata( $infile, $expected ); } + /** + * @dataProvider provideSvgUnits + */ + public function testScaleSVGUnit( $inUnit, $expected ) { + $this->assertEquals( + $expected, + SVGReader::scaleSVGUnit( $inUnit ), + 'SVG unit conversion and scaling failure' + ); + } + function assertMetadata( $infile, $expected ) { try { $data = SVGMetadataExtractor::getMetadata( $infile ); @@ -152,4 +163,34 @@ class SVGMetadataExtractorTest extends MediaWikiTestCase { ], ]; } + + public static function provideSvgUnits() { + return [ + [ '1' , 1 ], + [ '1.1' , 1.1 ], + [ '0.1' , 0.1 ], + [ '.1' , 0.1 ], + [ '1e2' , 100 ], + [ '1E2' , 100 ], + [ '+1' , 1 ], + [ '-1' , -1 ], + [ '-1.1' , -1.1 ], + [ '1e+2' , 100 ], + [ '1e-2' , 0.01 ], + [ '10px' , 10 ], + [ '10pt' , 10 * 1.25 ], + [ '10pc' , 10 * 15 ], + [ '10mm' , 10 * 3.543307 ], + [ '10cm' , 10 * 35.43307 ], + [ '10in' , 10 * 90 ], + [ '10em' , 10 * 16 ], + [ '10ex' , 10 * 12 ], + [ '10%' , 51.2 ], + [ '10 px' , 10 ], + // Invalid values + [ '1e1.1', 10 ], + [ '10bp', 10 ], + [ 'p10', null ], + ]; + } } -- 2.20.1