版博士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.
 
 
 
 

116 rivejä
3.6 KiB

  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 isUnsetKeyword = (value) => value === "unset" || value === "undefined" || value === "none";
  6. function iconToSVG(icon, customisations) {
  7. const fullIcon = {
  8. ...icon_defaults.defaultIconProps,
  9. ...icon
  10. };
  11. const fullCustomisations = {
  12. ...customisations_defaults.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 = '<g transform="' + transformations.join(" ") + '">' + body + "</g>";
  82. }
  83. });
  84. const customisationsWidth = fullCustomisations.width;
  85. const customisationsHeight = fullCustomisations.height;
  86. const boxWidth = box.width;
  87. const boxHeight = box.height;
  88. let width;
  89. let height;
  90. if (customisationsWidth === null) {
  91. height = customisationsHeight === null ? "1em" : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
  92. width = svg_size.calculateSize(height, boxWidth / boxHeight);
  93. } else {
  94. width = customisationsWidth === "auto" ? boxWidth : customisationsWidth;
  95. height = customisationsHeight === null ? svg_size.calculateSize(width, boxHeight / boxWidth) : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
  96. }
  97. const attributes = {};
  98. const setAttr = (prop, value) => {
  99. if (!isUnsetKeyword(value)) {
  100. attributes[prop] = value.toString();
  101. }
  102. };
  103. setAttr("width", width);
  104. setAttr("height", height);
  105. attributes.viewBox = box.left.toString() + " " + box.top.toString() + " " + boxWidth.toString() + " " + boxHeight.toString();
  106. return {
  107. attributes,
  108. body
  109. };
  110. }
  111. exports.iconToSVG = iconToSVG;
  112. exports.isUnsetKeyword = isUnsetKeyword;