1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-28 09:22:00 +03:00

Convert abolute lengths to pixel values

This commit is contained in:
GreLI
2015-02-23 18:19:51 +03:00
parent 1e81329982
commit 02dcf78d74
3 changed files with 41 additions and 7 deletions

View File

@ -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);