Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02dcf78

Browse files
committed
Convert abolute lengths to pixel values
1 parent 1e81329 commit 02dcf78

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

plugins/cleanupListOfValues.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ exports.active = false;
77
exports.params = {
88
floatPrecision: 3,
99
leadingZero: true,
10-
defaultPx: true
10+
defaultPx: true,
11+
convertToPx: true
1112
};
1213

1314
var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
1415
regSeparator = /\s+,?\s*|,\s*/,
15-
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;
16+
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero,
17+
absoluteLengths = { // relative to px
18+
cm: 96/2.54,
19+
mm: 9600/2.54,
20+
in: 96,
21+
pt: 4/3,
22+
pc: 16
23+
};
1624

1725
/**
1826
* Round list of values to the fixed precision.
@@ -92,6 +100,15 @@ exports.fn = function(item, params) {
92100
num = +(+match[1]).toFixed(params.floatPrecision),
93101
units = match[3] || '';
94102

103+
// convert absolute values to pixels
104+
if (params.convertToPx && units && (units in absoluteLengths)) {
105+
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);
106+
107+
if (String(pxNum).length < match[0].length)
108+
num = pxNum,
109+
units = 'px';
110+
}
111+
95112
// and remove leading zero
96113
if (params.leadingZero) {
97114
num = removeLeadingZero(num);

plugins/cleanupNumericValues.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ exports.active = true;
77
exports.params = {
88
floatPrecision: 3,
99
leadingZero: true,
10-
defaultPx: true
10+
defaultPx: true,
11+
convertToPx: true
1112
};
1213

1314
var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
14-
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;
15+
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero,
16+
absoluteLengths = { // relative to px
17+
cm: 96/2.54,
18+
mm: 9600/2.54,
19+
in: 96,
20+
pt: 4/3,
21+
pc: 16
22+
};
1523

1624
/**
1725
* Round numeric values to the fixed precision,
@@ -34,10 +42,19 @@ exports.fn = function(item, params) {
3442

3543
// if attribute value matches regNumericValues
3644
if (match) {
37-
// round it to the fixed precision
45+
// round it to the fixed precision
3846
var num = +(+match[1]).toFixed(params.floatPrecision),
3947
units = match[3] || '';
4048

49+
// convert absolute values to pixels
50+
if (params.convertToPx && units && (units in absoluteLengths)) {
51+
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);
52+
53+
if (String(pxNum).length < match[0].length)
54+
num = pxNum,
55+
units = 'px';
56+
}
57+
4158
// and remove leading zero
4259
if (params.leadingZero) {
4360
num = removeLeadingZero(num);
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)