1 | 'use strict';
|
2 |
|
3 | exports.type = 'perItem';
|
4 |
|
5 | exports.active = false;
|
6 |
|
7 | exports.description = 'rounds list of values to the fixed precision';
|
8 |
|
9 | exports.params = {
|
10 | floatPrecision: 3,
|
11 | leadingZero: true,
|
12 | defaultPx: true,
|
13 | convertToPx: true
|
14 | };
|
15 |
|
16 | var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
|
17 | regSeparator = /\s+,?\s*|,\s*/,
|
18 | removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero,
|
19 | absoluteLengths = {
|
20 | cm: 96/2.54,
|
21 | mm: 96/25.4,
|
22 | in: 96,
|
23 | pt: 4/3,
|
24 | pc: 16
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | exports.fn = function(item, params) {
|
48 |
|
49 |
|
50 | if ( item.hasAttr('points') ) {
|
51 | roundValues(item.attrs.points);
|
52 | }
|
53 |
|
54 | if ( item.hasAttr('enable-background') ) {
|
55 | roundValues(item.attrs['enable-background']);
|
56 | }
|
57 |
|
58 | if ( item.hasAttr('viewBox') ) {
|
59 | roundValues(item.attrs.viewBox);
|
60 | }
|
61 |
|
62 | if ( item.hasAttr('stroke-dasharray') ) {
|
63 | roundValues(item.attrs['stroke-dasharray']);
|
64 | }
|
65 |
|
66 | if ( item.hasAttr('dx') ) {
|
67 | roundValues(item.attrs.dx);
|
68 | }
|
69 |
|
70 | if ( item.hasAttr('dy') ) {
|
71 | roundValues(item.attrs.dy);
|
72 | }
|
73 |
|
74 | if ( item.hasAttr('x') ) {
|
75 | roundValues(item.attrs.x);
|
76 | }
|
77 |
|
78 | if ( item.hasAttr('y') ) {
|
79 | roundValues(item.attrs.y);
|
80 | }
|
81 |
|
82 |
|
83 | function roundValues($prop){
|
84 |
|
85 | var num, units,
|
86 | match,
|
87 | matchNew,
|
88 | lists = $prop.value,
|
89 | listsArr = lists.split(regSeparator),
|
90 | roundedListArr = [],
|
91 | roundedList;
|
92 |
|
93 | listsArr.forEach(function(elem){
|
94 |
|
95 | match = elem.match(regNumericValues);
|
96 | matchNew = elem.match(/new/);
|
97 |
|
98 |
|
99 | if (match) {
|
100 |
|
101 | num = +(+match[1]).toFixed(params.floatPrecision),
|
102 | units = match[3] || '';
|
103 |
|
104 |
|
105 | if (params.convertToPx && units && (units in absoluteLengths)) {
|
106 | var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);
|
107 |
|
108 | if (String(pxNum).length < match[0].length)
|
109 | num = pxNum,
|
110 | units = 'px';
|
111 | }
|
112 |
|
113 |
|
114 | if (params.leadingZero) {
|
115 | num = removeLeadingZero(num);
|
116 | }
|
117 |
|
118 |
|
119 | if (params.defaultPx && units === 'px') {
|
120 | units = '';
|
121 | }
|
122 |
|
123 | roundedListArr.push(num+units);
|
124 | }
|
125 |
|
126 | else if (matchNew) {
|
127 | roundedListArr.push('new');
|
128 | } else if (elem) {
|
129 | roundedListArr.push(elem);
|
130 | }
|
131 |
|
132 | });
|
133 |
|
134 | roundedList = roundedListArr.join(' ');
|
135 | $prop.value = roundedList;
|
136 |
|
137 | }
|
138 |
|
139 | };
|