| 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 = { // relative to px
|
|---|
| 20 | cm: 96/2.54,
|
|---|
| 21 | mm: 96/25.4,
|
|---|
| 22 | in: 96,
|
|---|
| 23 | pt: 4/3,
|
|---|
| 24 | pc: 16
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Round list of values to the fixed precision.
|
|---|
| 29 | *
|
|---|
| 30 | * @example
|
|---|
| 31 | * <svg viewBox="0 0 200.28423 200.28423" enable-background="new 0 0 200.28423 200.28423">
|
|---|
| 32 | * ⬇
|
|---|
| 33 | * <svg viewBox="0 0 200.284 200.284" enable-background="new 0 0 200.284 200.284">
|
|---|
| 34 | *
|
|---|
| 35 | *
|
|---|
| 36 | * <polygon points="208.250977 77.1308594 223.069336 ... "/>
|
|---|
| 37 | * ⬇
|
|---|
| 38 | * <polygon points="208.251 77.131 223.069 ... "/>
|
|---|
| 39 | *
|
|---|
| 40 | *
|
|---|
| 41 | * @param {Object} item current iteration item
|
|---|
| 42 | * @param {Object} params plugin params
|
|---|
| 43 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 44 | *
|
|---|
| 45 | * @author kiyopikko
|
|---|
| 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 | // if attribute value matches regNumericValues
|
|---|
| 99 | if (match) {
|
|---|
| 100 | // round it to the fixed precision
|
|---|
| 101 | num = +(+match[1]).toFixed(params.floatPrecision),
|
|---|
| 102 | units = match[3] || '';
|
|---|
| 103 |
|
|---|
| 104 | // convert absolute values to pixels
|
|---|
| 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 | // and remove leading zero
|
|---|
| 114 | if (params.leadingZero) {
|
|---|
| 115 | num = removeLeadingZero(num);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // remove default 'px' units
|
|---|
| 119 | if (params.defaultPx && units === 'px') {
|
|---|
| 120 | units = '';
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | roundedListArr.push(num+units);
|
|---|
| 124 | }
|
|---|
| 125 | // if attribute value is "new"(only enable-background).
|
|---|
| 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 | };
|
|---|