You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cleanupListOfValues.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = false;
  4. exports.description = 'rounds list of values to the fixed precision';
  5. exports.params = {
  6. floatPrecision: 3,
  7. leadingZero: true,
  8. defaultPx: true,
  9. convertToPx: true
  10. };
  11. var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
  12. regSeparator = /\s+,?\s*|,\s*/,
  13. removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero,
  14. absoluteLengths = { // relative to px
  15. cm: 96/2.54,
  16. mm: 96/25.4,
  17. in: 96,
  18. pt: 4/3,
  19. pc: 16
  20. };
  21. /**
  22. * Round list of values to the fixed precision.
  23. *
  24. * @example
  25. * <svg viewBox="0 0 200.28423 200.28423" enable-background="new 0 0 200.28423 200.28423">
  26. * ⬇
  27. * <svg viewBox="0 0 200.284 200.284" enable-background="new 0 0 200.284 200.284">
  28. *
  29. *
  30. * <polygon points="208.250977 77.1308594 223.069336 ... "/>
  31. * ⬇
  32. * <polygon points="208.251 77.131 223.069 ... "/>
  33. *
  34. *
  35. * @param {Object} item current iteration item
  36. * @param {Object} params plugin params
  37. * @return {Boolean} if false, item will be filtered out
  38. *
  39. * @author kiyopikko
  40. */
  41. exports.fn = function(item, params) {
  42. if ( item.hasAttr('points') ) {
  43. roundValues(item.attrs.points);
  44. }
  45. if ( item.hasAttr('enable-background') ) {
  46. roundValues(item.attrs['enable-background']);
  47. }
  48. if ( item.hasAttr('viewBox') ) {
  49. roundValues(item.attrs.viewBox);
  50. }
  51. if ( item.hasAttr('stroke-dasharray') ) {
  52. roundValues(item.attrs['stroke-dasharray']);
  53. }
  54. if ( item.hasAttr('dx') ) {
  55. roundValues(item.attrs.dx);
  56. }
  57. if ( item.hasAttr('dy') ) {
  58. roundValues(item.attrs.dy);
  59. }
  60. if ( item.hasAttr('x') ) {
  61. roundValues(item.attrs.x);
  62. }
  63. if ( item.hasAttr('y') ) {
  64. roundValues(item.attrs.y);
  65. }
  66. function roundValues($prop){
  67. var num, units,
  68. match,
  69. matchNew,
  70. lists = $prop.value,
  71. listsArr = lists.split(regSeparator),
  72. roundedListArr = [],
  73. roundedList;
  74. listsArr.forEach(function(elem){
  75. match = elem.match(regNumericValues);
  76. matchNew = elem.match(/new/);
  77. // if attribute value matches regNumericValues
  78. if (match) {
  79. // round it to the fixed precision
  80. num = +(+match[1]).toFixed(params.floatPrecision),
  81. units = match[3] || '';
  82. // convert absolute values to pixels
  83. if (params.convertToPx && units && (units in absoluteLengths)) {
  84. var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);
  85. if (String(pxNum).length < match[0].length)
  86. num = pxNum,
  87. units = 'px';
  88. }
  89. // and remove leading zero
  90. if (params.leadingZero) {
  91. num = removeLeadingZero(num);
  92. }
  93. // remove default 'px' units
  94. if (params.defaultPx && units === 'px') {
  95. units = '';
  96. }
  97. roundedListArr.push(num+units);
  98. }
  99. // if attribute value is "new"(only enable-background).
  100. else if (matchNew) {
  101. roundedListArr.push('new');
  102. } else if (elem) {
  103. roundedListArr.push(elem);
  104. }
  105. });
  106. roundedList = roundedListArr.join(' ');
  107. $prop.value = roundedList;
  108. }
  109. };