From 02dcf78d74303d802176ee65b7965fec3adaf34a Mon Sep 17 00:00:00 2001 From: GreLI Date: Mon, 23 Feb 2015 18:19:51 +0300 Subject: [PATCH] Convert abolute lengths to pixel values --- plugins/cleanupListOfValues.js | 21 +++++++++++++++++++-- plugins/cleanupNumericValues.js | 23 ++++++++++++++++++++--- test/plugins/cleanupNumericValues.01.svg | 4 ++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/plugins/cleanupListOfValues.js b/plugins/cleanupListOfValues.js index 2bd92cb1..5f617f0e 100644 --- a/plugins/cleanupListOfValues.js +++ b/plugins/cleanupListOfValues.js @@ -7,12 +7,20 @@ exports.active = false; exports.params = { floatPrecision: 3, leadingZero: true, - defaultPx: true + defaultPx: true, + convertToPx: true }; var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/, regSeparator = /\s+,?\s*|,\s*/, - removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero; + removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero, + absoluteLengths = { // relative to px + cm: 96/2.54, + mm: 9600/2.54, + in: 96, + pt: 4/3, + pc: 16 + }; /** * Round list of values to the fixed precision. @@ -92,6 +100,15 @@ exports.fn = function(item, params) { num = +(+match[1]).toFixed(params.floatPrecision), units = match[3] || ''; + // convert absolute values to pixels + if (params.convertToPx && units && (units in absoluteLengths)) { + var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision); + + if (String(pxNum).length < match[0].length) + num = pxNum, + units = 'px'; + } + // and remove leading zero if (params.leadingZero) { num = removeLeadingZero(num); diff --git a/plugins/cleanupNumericValues.js b/plugins/cleanupNumericValues.js index e23d4e26..a2865aa4 100644 --- a/plugins/cleanupNumericValues.js +++ b/plugins/cleanupNumericValues.js @@ -7,11 +7,19 @@ exports.active = true; exports.params = { floatPrecision: 3, leadingZero: true, - defaultPx: true + defaultPx: true, + convertToPx: true }; var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/, - removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero; + removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero, + absoluteLengths = { // relative to px + cm: 96/2.54, + mm: 9600/2.54, + in: 96, + pt: 4/3, + pc: 16 + }; /** * Round numeric values to the fixed precision, @@ -34,10 +42,19 @@ exports.fn = function(item, params) { // if attribute value matches regNumericValues if (match) { - // round it to the fixed precision + // round it to the fixed precision var num = +(+match[1]).toFixed(params.floatPrecision), units = match[3] || ''; + // convert absolute values to pixels + if (params.convertToPx && units && (units in absoluteLengths)) { + var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision); + + if (String(pxNum).length < match[0].length) + num = pxNum, + units = 'px'; + } + // and remove leading zero if (params.leadingZero) { num = removeLeadingZero(num); diff --git a/test/plugins/cleanupNumericValues.01.svg b/test/plugins/cleanupNumericValues.01.svg index b71e4b2d..62bf6586 100644 --- a/test/plugins/cleanupNumericValues.01.svg +++ b/test/plugins/cleanupNumericValues.01.svg @@ -1,9 +1,9 @@ - test + @@@ - test +