build.mjs 3.6 KB

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