build.cjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const icon_defaults = require('../icon/defaults.cjs');
  3. const customisations_defaults = require('../customisations/defaults.cjs');
  4. const svg_size = require('./size.cjs');
  5. const svg_defs = require('./defs.cjs');
  6. const isUnsetKeyword = (value) => value === "unset" || value === "undefined" || value === "none";
  7. function iconToSVG(icon, customisations) {
  8. const fullIcon = {
  9. ...icon_defaults.defaultIconProps,
  10. ...icon
  11. };
  12. const fullCustomisations = {
  13. ...customisations_defaults.defaultIconCustomisations,
  14. ...customisations
  15. };
  16. const box = {
  17. left: fullIcon.left,
  18. top: fullIcon.top,
  19. width: fullIcon.width,
  20. height: fullIcon.height
  21. };
  22. let body = fullIcon.body;
  23. [fullIcon, fullCustomisations].forEach((props) => {
  24. const transformations = [];
  25. const hFlip = props.hFlip;
  26. const vFlip = props.vFlip;
  27. let rotation = props.rotate;
  28. if (hFlip) {
  29. if (vFlip) {
  30. rotation += 2;
  31. } else {
  32. transformations.push(
  33. "translate(" + (box.width + box.left).toString() + " " + (0 - box.top).toString() + ")"
  34. );
  35. transformations.push("scale(-1 1)");
  36. box.top = box.left = 0;
  37. }
  38. } else if (vFlip) {
  39. transformations.push(
  40. "translate(" + (0 - box.left).toString() + " " + (box.height + box.top).toString() + ")"
  41. );
  42. transformations.push("scale(1 -1)");
  43. box.top = box.left = 0;
  44. }
  45. let tempValue;
  46. if (rotation < 0) {
  47. rotation -= Math.floor(rotation / 4) * 4;
  48. }
  49. rotation = rotation % 4;
  50. switch (rotation) {
  51. case 1:
  52. tempValue = box.height / 2 + box.top;
  53. transformations.unshift(
  54. "rotate(90 " + tempValue.toString() + " " + tempValue.toString() + ")"
  55. );
  56. break;
  57. case 2:
  58. transformations.unshift(
  59. "rotate(180 " + (box.width / 2 + box.left).toString() + " " + (box.height / 2 + box.top).toString() + ")"
  60. );
  61. break;
  62. case 3:
  63. tempValue = box.width / 2 + box.left;
  64. transformations.unshift(
  65. "rotate(-90 " + tempValue.toString() + " " + tempValue.toString() + ")"
  66. );
  67. break;
  68. }
  69. if (rotation % 2 === 1) {
  70. if (box.left !== box.top) {
  71. tempValue = box.left;
  72. box.left = box.top;
  73. box.top = tempValue;
  74. }
  75. if (box.width !== box.height) {
  76. tempValue = box.width;
  77. box.width = box.height;
  78. box.height = tempValue;
  79. }
  80. }
  81. if (transformations.length) {
  82. body = svg_defs.wrapSVGContent(
  83. body,
  84. '<g transform="' + transformations.join(" ") + '">',
  85. "</g>"
  86. );
  87. }
  88. });
  89. const customisationsWidth = fullCustomisations.width;
  90. const customisationsHeight = fullCustomisations.height;
  91. const boxWidth = box.width;
  92. const boxHeight = box.height;
  93. let width;
  94. let height;
  95. if (customisationsWidth === null) {
  96. height = customisationsHeight === null ? "1em" : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
  97. width = svg_size.calculateSize(height, boxWidth / boxHeight);
  98. } else {
  99. width = customisationsWidth === "auto" ? boxWidth : customisationsWidth;
  100. height = customisationsHeight === null ? svg_size.calculateSize(width, boxHeight / boxWidth) : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
  101. }
  102. const attributes = {};
  103. const setAttr = (prop, value) => {
  104. if (!isUnsetKeyword(value)) {
  105. attributes[prop] = value.toString();
  106. }
  107. };
  108. setAttr("width", width);
  109. setAttr("height", height);
  110. const viewBox = [box.left, box.top, boxWidth, boxHeight];
  111. attributes.viewBox = viewBox.join(" ");
  112. return {
  113. attributes,
  114. viewBox,
  115. body
  116. };
  117. }
  118. exports.iconToSVG = iconToSVG;
  119. exports.isUnsetKeyword = isUnsetKeyword;