版博士V2.0程序
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.
 
 
 
 

113 rivejä
3.5 KiB

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