@trismegisto/charts 3.3.14 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/LICENSE +56 -56
  2. package/MIGRATION.md +831 -0
  3. package/NOTICE +8 -13
  4. package/README.md +53 -32
  5. package/dist/core/bar-chart.d.ts +32 -0
  6. package/dist/core/bar-chart.js +108 -0
  7. package/dist/core/bar-chart.js.map +1 -0
  8. package/dist/core/boxplot-chart.d.ts +15 -0
  9. package/dist/core/boxplot-chart.js +44 -0
  10. package/dist/core/boxplot-chart.js.map +1 -0
  11. package/dist/core/bullet-chart.d.ts +29 -0
  12. package/dist/core/bullet-chart.js +93 -0
  13. package/dist/core/bullet-chart.js.map +1 -0
  14. package/dist/core/calendar-heatmap.d.ts +20 -0
  15. package/dist/core/calendar-heatmap.js +78 -0
  16. package/dist/core/calendar-heatmap.js.map +1 -0
  17. package/dist/core/category-range.d.ts +15 -0
  18. package/dist/core/category-range.js +35 -0
  19. package/dist/core/category-range.js.map +1 -0
  20. package/dist/core/chart-host.d.ts +36 -0
  21. package/dist/core/chart-host.js +209 -0
  22. package/dist/core/chart-host.js.map +1 -0
  23. package/dist/core/charts-i18n.d.ts +25 -0
  24. package/dist/core/charts-i18n.js +33 -0
  25. package/dist/core/charts-i18n.js.map +1 -0
  26. package/dist/core/charts-messages.d.ts +143 -0
  27. package/dist/core/charts-messages.en.d.ts +2 -0
  28. package/dist/core/charts-messages.en.js +143 -0
  29. package/dist/core/charts-messages.en.js.map +1 -0
  30. package/dist/core/charts-messages.es.d.ts +2 -0
  31. package/dist/core/charts-messages.es.js +143 -0
  32. package/dist/core/charts-messages.es.js.map +1 -0
  33. package/dist/core/charts-messages.js +143 -0
  34. package/dist/core/charts-messages.js.map +1 -0
  35. package/dist/core/colors.d.ts +27 -0
  36. package/dist/core/colors.js +76 -0
  37. package/dist/core/colors.js.map +1 -0
  38. package/dist/core/context.d.ts +42 -0
  39. package/dist/core/context.js +36 -0
  40. package/dist/core/context.js.map +1 -0
  41. package/dist/core/engine-locale.d.ts +4 -0
  42. package/dist/core/engine-locale.js +38 -0
  43. package/dist/core/engine-locale.js.map +1 -0
  44. package/dist/core/format.d.ts +13 -0
  45. package/dist/core/format.js +64 -0
  46. package/dist/core/format.js.map +1 -0
  47. package/dist/core/frame.d.ts +31 -0
  48. package/dist/core/frame.js +64 -0
  49. package/dist/core/frame.js.map +1 -0
  50. package/dist/core/funnel-chart.d.ts +8 -0
  51. package/dist/core/funnel-chart.js +30 -0
  52. package/dist/core/funnel-chart.js.map +1 -0
  53. package/dist/core/gauge-chart.d.ts +44 -0
  54. package/dist/core/gauge-chart.js +186 -0
  55. package/dist/core/gauge-chart.js.map +1 -0
  56. package/dist/core/gauge-layout.d.ts +67 -0
  57. package/dist/core/gauge-layout.js +140 -0
  58. package/dist/core/gauge-layout.js.map +1 -0
  59. package/dist/core/heatmap-chart.d.ts +22 -0
  60. package/dist/core/heatmap-chart.js +62 -0
  61. package/dist/core/heatmap-chart.js.map +1 -0
  62. package/dist/core/index.d.ts +46 -0
  63. package/dist/core/index.js +52 -0
  64. package/dist/core/index.js.map +1 -0
  65. package/dist/core/line-chart.d.ts +43 -0
  66. package/dist/core/line-chart.js +99 -0
  67. package/dist/core/line-chart.js.map +1 -0
  68. package/dist/core/line-extras.d.ts +20 -0
  69. package/dist/core/line-extras.js +49 -0
  70. package/dist/core/line-extras.js.map +1 -0
  71. package/dist/core/linear-gauge.d.ts +64 -0
  72. package/dist/core/linear-gauge.js +78 -0
  73. package/dist/core/linear-gauge.js.map +1 -0
  74. package/dist/core/live-session.d.ts +58 -0
  75. package/dist/core/live-session.js +200 -0
  76. package/dist/core/live-session.js.map +1 -0
  77. package/dist/core/live.d.ts +78 -0
  78. package/dist/core/live.js +245 -0
  79. package/dist/core/live.js.map +1 -0
  80. package/dist/core/navigation.d.ts +20 -0
  81. package/dist/core/navigation.js +62 -0
  82. package/dist/core/navigation.js.map +1 -0
  83. package/dist/core/normalize.d.ts +19 -0
  84. package/dist/core/normalize.js +51 -0
  85. package/dist/core/normalize.js.map +1 -0
  86. package/dist/core/observe.d.ts +28 -0
  87. package/dist/core/observe.js +151 -0
  88. package/dist/core/observe.js.map +1 -0
  89. package/dist/core/options.d.ts +29 -0
  90. package/dist/core/options.js +69 -0
  91. package/dist/core/options.js.map +1 -0
  92. package/dist/core/pie-chart.d.ts +25 -0
  93. package/dist/core/pie-chart.js +99 -0
  94. package/dist/core/pie-chart.js.map +1 -0
  95. package/dist/core/radar-chart.d.ts +18 -0
  96. package/dist/core/radar-chart.js +34 -0
  97. package/dist/core/radar-chart.js.map +1 -0
  98. package/dist/core/range-interaction.d.ts +62 -0
  99. package/dist/core/range-interaction.js +196 -0
  100. package/dist/core/range-interaction.js.map +1 -0
  101. package/dist/core/range.d.ts +83 -0
  102. package/dist/core/range.js +200 -0
  103. package/dist/core/range.js.map +1 -0
  104. package/dist/core/references.d.ts +25 -0
  105. package/dist/core/references.js +71 -0
  106. package/dist/core/references.js.map +1 -0
  107. package/dist/core/sankey-chart.d.ts +15 -0
  108. package/dist/core/sankey-chart.js +28 -0
  109. package/dist/core/sankey-chart.js.map +1 -0
  110. package/dist/core/scale-legend.d.ts +10 -0
  111. package/dist/core/scale-legend.js +16 -0
  112. package/dist/core/scale-legend.js.map +1 -0
  113. package/dist/core/scatter-chart.d.ts +38 -0
  114. package/dist/core/scatter-chart.js +74 -0
  115. package/dist/core/scatter-chart.js.map +1 -0
  116. package/dist/core/scope-cache.d.ts +6 -0
  117. package/dist/core/scope-cache.js +49 -0
  118. package/dist/core/scope-cache.js.map +1 -0
  119. package/dist/core/series-extras.d.ts +46 -0
  120. package/dist/core/series-extras.js +125 -0
  121. package/dist/core/series-extras.js.map +1 -0
  122. package/dist/core/sparkline-chart.d.ts +31 -0
  123. package/dist/core/sparkline-chart.js +69 -0
  124. package/dist/core/sparkline-chart.js.map +1 -0
  125. package/dist/core/stats.d.ts +12 -0
  126. package/dist/core/stats.js +23 -0
  127. package/dist/core/stats.js.map +1 -0
  128. package/dist/core/theme.d.ts +9 -0
  129. package/dist/core/theme.js +138 -0
  130. package/dist/core/theme.js.map +1 -0
  131. package/dist/core/time-series-chart.d.ts +91 -0
  132. package/dist/core/time-series-chart.js +390 -0
  133. package/dist/core/time-series-chart.js.map +1 -0
  134. package/dist/core/treemap-chart.d.ts +21 -0
  135. package/dist/core/treemap-chart.js +41 -0
  136. package/dist/core/treemap-chart.js.map +1 -0
  137. package/dist/core/types.d.ts +67 -0
  138. package/dist/core/types.js +2 -0
  139. package/dist/core/types.js.map +1 -0
  140. package/dist/core/view.d.ts +95 -0
  141. package/dist/core/view.js +2 -0
  142. package/dist/core/view.js.map +1 -0
  143. package/dist/core/warn.d.ts +5 -0
  144. package/dist/core/warn.js +22 -0
  145. package/dist/core/warn.js.map +1 -0
  146. package/dist/core/waterfall-chart.d.ts +32 -0
  147. package/dist/core/waterfall-chart.js +114 -0
  148. package/dist/core/waterfall-chart.js.map +1 -0
  149. package/dist/engine/aria.d.ts +3 -0
  150. package/dist/engine/aria.js +18 -0
  151. package/dist/engine/aria.js.map +1 -0
  152. package/dist/engine/component/legend.d.ts +11 -0
  153. package/dist/engine/component/legend.js +348 -0
  154. package/dist/engine/component/legend.js.map +1 -0
  155. package/dist/engine/component/reference-marks.d.ts +46 -0
  156. package/dist/engine/component/reference-marks.js +197 -0
  157. package/dist/engine/component/reference-marks.js.map +1 -0
  158. package/dist/engine/component/text-style.d.ts +6 -0
  159. package/dist/engine/component/text-style.js +26 -0
  160. package/dist/engine/component/text-style.js.map +1 -0
  161. package/dist/engine/component/title.d.ts +2 -0
  162. package/dist/engine/component/title.js +90 -0
  163. package/dist/engine/component/title.js.map +1 -0
  164. package/dist/engine/component/tooltip.d.ts +40 -0
  165. package/dist/engine/component/tooltip.js +394 -0
  166. package/dist/engine/component/tooltip.js.map +1 -0
  167. package/dist/engine/component/view-window.d.ts +29 -0
  168. package/dist/engine/component/view-window.js +39 -0
  169. package/dist/engine/component/view-window.js.map +1 -0
  170. package/dist/engine/component/visual-map.d.ts +9 -0
  171. package/dist/engine/component/visual-map.js +381 -0
  172. package/dist/engine/component/visual-map.js.map +1 -0
  173. package/dist/engine/contract.d.ts +469 -0
  174. package/dist/engine/contract.js +9 -0
  175. package/dist/engine/contract.js.map +1 -0
  176. package/dist/engine/coord/axis.d.ts +55 -0
  177. package/dist/engine/coord/axis.js +358 -0
  178. package/dist/engine/coord/axis.js.map +1 -0
  179. package/dist/engine/coord/calendar.d.ts +44 -0
  180. package/dist/engine/coord/calendar.js +282 -0
  181. package/dist/engine/coord/calendar.js.map +1 -0
  182. package/dist/engine/coord/cartesian.d.ts +22 -0
  183. package/dist/engine/coord/cartesian.js +481 -0
  184. package/dist/engine/coord/cartesian.js.map +1 -0
  185. package/dist/engine/coord/polar.d.ts +24 -0
  186. package/dist/engine/coord/polar.js +232 -0
  187. package/dist/engine/coord/polar.js.map +1 -0
  188. package/dist/engine/coord/radar.d.ts +90 -0
  189. package/dist/engine/coord/radar.js +288 -0
  190. package/dist/engine/coord/radar.js.map +1 -0
  191. package/dist/engine/index.d.ts +3 -0
  192. package/dist/engine/index.js +5 -0
  193. package/dist/engine/index.js.map +1 -0
  194. package/dist/engine/instance.d.ts +101 -0
  195. package/dist/engine/instance.js +993 -0
  196. package/dist/engine/instance.js.map +1 -0
  197. package/dist/engine/label-layout.d.ts +3 -0
  198. package/dist/engine/label-layout.js +35 -0
  199. package/dist/engine/label-layout.js.map +1 -0
  200. package/dist/engine/locale.d.ts +6 -0
  201. package/dist/engine/locale.js +100 -0
  202. package/dist/engine/locale.js.map +1 -0
  203. package/dist/engine/model.d.ts +21 -0
  204. package/dist/engine/model.js +160 -0
  205. package/dist/engine/model.js.map +1 -0
  206. package/dist/engine/option/axis.d.ts +167 -0
  207. package/dist/engine/option/axis.js +2 -0
  208. package/dist/engine/option/axis.js.map +1 -0
  209. package/dist/engine/option/components.d.ts +434 -0
  210. package/dist/engine/option/components.js +2 -0
  211. package/dist/engine/option/components.js.map +1 -0
  212. package/dist/engine/registry.d.ts +7 -0
  213. package/dist/engine/registry.js +40 -0
  214. package/dist/engine/registry.js.map +1 -0
  215. package/dist/engine/render/path.d.ts +25 -0
  216. package/dist/engine/render/path.js +378 -0
  217. package/dist/engine/render/path.js.map +1 -0
  218. package/dist/engine/render/svg.d.ts +60 -0
  219. package/dist/engine/render/svg.js +651 -0
  220. package/dist/engine/render/svg.js.map +1 -0
  221. package/dist/engine/render/text.d.ts +13 -0
  222. package/dist/engine/render/text.js +144 -0
  223. package/dist/engine/render/text.js.map +1 -0
  224. package/dist/engine/scale.d.ts +48 -0
  225. package/dist/engine/scale.js +238 -0
  226. package/dist/engine/scale.js.map +1 -0
  227. package/dist/engine/series/bar.d.ts +67 -0
  228. package/dist/engine/series/bar.js +428 -0
  229. package/dist/engine/series/bar.js.map +1 -0
  230. package/dist/engine/series/boxplot.d.ts +31 -0
  231. package/dist/engine/series/boxplot.js +125 -0
  232. package/dist/engine/series/boxplot.js.map +1 -0
  233. package/dist/engine/series/cartesian-data.d.ts +38 -0
  234. package/dist/engine/series/cartesian-data.js +159 -0
  235. package/dist/engine/series/cartesian-data.js.map +1 -0
  236. package/dist/engine/series/circular-common.d.ts +50 -0
  237. package/dist/engine/series/circular-common.js +178 -0
  238. package/dist/engine/series/circular-common.js.map +1 -0
  239. package/dist/engine/series/circular-options.d.ts +151 -0
  240. package/dist/engine/series/circular-options.js +2 -0
  241. package/dist/engine/series/circular-options.js.map +1 -0
  242. package/dist/engine/series/common.d.ts +48 -0
  243. package/dist/engine/series/common.js +198 -0
  244. package/dist/engine/series/common.js.map +1 -0
  245. package/dist/engine/series/funnel.d.ts +37 -0
  246. package/dist/engine/series/funnel.js +204 -0
  247. package/dist/engine/series/funnel.js.map +1 -0
  248. package/dist/engine/series/gauge.d.ts +106 -0
  249. package/dist/engine/series/gauge.js +349 -0
  250. package/dist/engine/series/gauge.js.map +1 -0
  251. package/dist/engine/series/heatmap.d.ts +31 -0
  252. package/dist/engine/series/heatmap.js +222 -0
  253. package/dist/engine/series/heatmap.js.map +1 -0
  254. package/dist/engine/series/label-fit.d.ts +7 -0
  255. package/dist/engine/series/label-fit.js +48 -0
  256. package/dist/engine/series/label-fit.js.map +1 -0
  257. package/dist/engine/series/line.d.ts +65 -0
  258. package/dist/engine/series/line.js +354 -0
  259. package/dist/engine/series/line.js.map +1 -0
  260. package/dist/engine/series/mark-point.d.ts +4 -0
  261. package/dist/engine/series/mark-point.js +196 -0
  262. package/dist/engine/series/mark-point.js.map +1 -0
  263. package/dist/engine/series/pie.d.ts +64 -0
  264. package/dist/engine/series/pie.js +402 -0
  265. package/dist/engine/series/pie.js.map +1 -0
  266. package/dist/engine/series/radar.d.ts +34 -0
  267. package/dist/engine/series/radar.js +168 -0
  268. package/dist/engine/series/radar.js.map +1 -0
  269. package/dist/engine/series/sankey.d.ts +64 -0
  270. package/dist/engine/series/sankey.js +486 -0
  271. package/dist/engine/series/sankey.js.map +1 -0
  272. package/dist/engine/series/scatter.d.ts +40 -0
  273. package/dist/engine/series/scatter.js +167 -0
  274. package/dist/engine/series/scatter.js.map +1 -0
  275. package/dist/engine/series/treemap.d.ts +77 -0
  276. package/dist/engine/series/treemap.js +493 -0
  277. package/dist/engine/series/treemap.js.map +1 -0
  278. package/dist/engine/spec/compile.d.ts +9 -0
  279. package/dist/engine/spec/compile.js +593 -0
  280. package/dist/engine/spec/compile.js.map +1 -0
  281. package/dist/engine/spec/theme.d.ts +5 -0
  282. package/dist/engine/spec/theme.js +83 -0
  283. package/dist/engine/spec/theme.js.map +1 -0
  284. package/dist/engine/spec/types.d.ts +564 -0
  285. package/dist/engine/spec/types.js +3 -0
  286. package/dist/engine/spec/types.js.map +1 -0
  287. package/dist/engine/theme.d.ts +8 -0
  288. package/dist/engine/theme.js +85 -0
  289. package/dist/engine/theme.js.map +1 -0
  290. package/dist/engine/util.d.ts +73 -0
  291. package/dist/engine/util.js +285 -0
  292. package/dist/engine/util.js.map +1 -0
  293. package/package.json +43 -31
  294. package/fesm2022/trismegisto-charts.mjs +0 -12428
  295. package/fesm2022/trismegisto-charts.mjs.map +0 -1
  296. package/types/trismegisto-charts.d.ts +0 -1970
  297. package/types/trismegisto-charts.d.ts.map +0 -1
@@ -0,0 +1,651 @@
1
+ import { interpolateShape, shapeToPath } from './path';
2
+ import { DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, lineHeightOf, measureText } from './text';
3
+ const SVG_NS = 'http://www.w3.org/2000/svg';
4
+ let instanceCounter = 0;
5
+ const EASINGS = {
6
+ linear: (t) => t,
7
+ cubicOut: (t) => 1 - (1 - t) ** 3,
8
+ cubicInOut: (t) => (t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2),
9
+ quadraticOut: (t) => 1 - (1 - t) ** 2,
10
+ exponentialOut: (t) => (t >= 1 ? 1 : 1 - 2 ** (-10 * t)),
11
+ backOut: (t) => 1 + 2.70158 * (t - 1) ** 3 + 1.70158 * (t - 1) ** 2,
12
+ elasticOut: (t) => (t === 0 || t === 1 ? t : 2 ** (-10 * t) * Math.sin(((t * 10 - 0.75) * 2 * Math.PI) / 3) + 1),
13
+ };
14
+ export class SvgRenderer {
15
+ host;
16
+ svg;
17
+ defs;
18
+ root;
19
+ doc;
20
+ entries = new Map();
21
+ nodeOf = new WeakMap();
22
+ defsUsed = new Set();
23
+ defsMade = new Map();
24
+ prefix = `hmc${++instanceCounter}-`;
25
+ frame = 0;
26
+ settings = { animate: false, duration: 0, durationUpdate: 0, easing: 'cubicOut', easingUpdate: 'cubicInOut' };
27
+ keyCounts = new Map();
28
+ listeners = [];
29
+ animating = new Set();
30
+ /** Chamado quando as animações terminam (evento `finished`). */
31
+ onFinished = () => { };
32
+ constructor(host, callbacks) {
33
+ this.host = host;
34
+ this.doc = host.ownerDocument;
35
+ this.svg = this.doc.createElementNS(SVG_NS, 'svg');
36
+ this.svg.setAttribute('xmlns', SVG_NS);
37
+ this.svg.setAttribute('version', '1.1');
38
+ this.svg.style.display = 'block';
39
+ this.svg.style.userSelect = 'none';
40
+ this.svg.style.overflow = 'hidden';
41
+ this.defs = this.doc.createElementNS(SVG_NS, 'defs');
42
+ this.root = this.doc.createElementNS(SVG_NS, 'g');
43
+ this.svg.append(this.defs, this.root);
44
+ host.appendChild(this.svg);
45
+ const point = (e) => {
46
+ const r = this.svg.getBoundingClientRect();
47
+ return [e.clientX - r.left, e.clientY - r.top];
48
+ };
49
+ const on = (type, fn) => {
50
+ const listener = fn;
51
+ this.svg.addEventListener(type, listener);
52
+ this.listeners.push([type, listener]);
53
+ };
54
+ on('mousemove', (e) => callbacks.onPointer(this.itemAt(e.target), ...point(e), e));
55
+ on('click', (e) => callbacks.onClick(this.itemAt(e.target), ...point(e), e));
56
+ on('mouseleave', (e) => callbacks.onLeave(e));
57
+ }
58
+ setSize(width, height) {
59
+ this.svg.setAttribute('width', String(width));
60
+ this.svg.setAttribute('height', String(height));
61
+ this.svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
62
+ }
63
+ /** Rótulo acessível do SVG (descrição do gráfico) ou `null` para tirá-lo da árvore de acessibilidade. */
64
+ setLabel(label) {
65
+ if (label) {
66
+ this.svg.setAttribute('role', 'img');
67
+ this.svg.setAttribute('aria-label', label);
68
+ this.svg.removeAttribute('aria-hidden');
69
+ }
70
+ else {
71
+ this.svg.removeAttribute('role');
72
+ this.svg.removeAttribute('aria-label');
73
+ this.svg.setAttribute('aria-hidden', 'true');
74
+ }
75
+ }
76
+ setBackground(color) {
77
+ this.svg.style.backgroundColor = color && color !== 'transparent' ? color : '';
78
+ }
79
+ /** Item de série (ou componente) sob um elemento: sobe até achar um nó com `item` que não seja `silent`. */
80
+ itemAt(target) {
81
+ let el = target;
82
+ while (el && el !== this.svg) {
83
+ const entry = this.nodeOf.get(el);
84
+ if (entry && entry.node.item && !entry.node.silent)
85
+ return entry.node.item;
86
+ el = el.parentElement;
87
+ }
88
+ return null;
89
+ }
90
+ render(nodes, settings) {
91
+ this.settings = settings;
92
+ for (const entry of this.entries.values())
93
+ entry.seen = false;
94
+ this.defsUsed.clear();
95
+ this.keyCounts.clear();
96
+ this.reconcile(this.root, nodes);
97
+ for (const [key, entry] of this.entries) {
98
+ if (entry.seen)
99
+ continue;
100
+ entry.el.remove();
101
+ entry.decalEl?.remove();
102
+ this.animating.delete(entry);
103
+ this.entries.delete(key);
104
+ }
105
+ for (const [id, el] of this.defsMade) {
106
+ if (!this.defsUsed.has(id)) {
107
+ el.remove();
108
+ this.defsMade.delete(id);
109
+ }
110
+ }
111
+ if (this.animating.size)
112
+ this.schedule();
113
+ else
114
+ this.onFinished();
115
+ }
116
+ toString() {
117
+ return new XMLSerializer().serializeToString(this.svg);
118
+ }
119
+ dispose() {
120
+ if (this.frame)
121
+ cancelAnimationFrame(this.frame);
122
+ for (const [type, l] of this.listeners)
123
+ this.svg.removeEventListener(type, l);
124
+ this.svg.remove();
125
+ this.entries.clear();
126
+ }
127
+ // ---------------------------------------------------------------- árvore
128
+ uniqueKey(key) {
129
+ const count = this.keyCounts.get(key) ?? 0;
130
+ this.keyCounts.set(key, count + 1);
131
+ return count ? `${key}#${count}` : key;
132
+ }
133
+ reconcile(parent, nodes) {
134
+ // Ordem por z (estável): o que tem z maior fica por cima.
135
+ const ordered = nodes.map((node, i) => ({ node, i })).sort((a, b) => (a.node.z ?? 0) - (b.node.z ?? 0) || a.i - b.i);
136
+ const keys = ordered.map(({ node }) => this.uniqueKey(node.key));
137
+ // Elementos que continuam neste pai: os outros (que saem no fim) não servem de cursor. Sem isto, um nó que sai do
138
+ // começo (o ponto mais antigo de uma janela deslizante) fazia todos os seguintes serem movidos de novo.
139
+ const staying = new Set();
140
+ for (const key of keys) {
141
+ const e = this.entries.get(key);
142
+ if (e?.el.parentNode === parent)
143
+ staying.add(e.el);
144
+ if (e?.decalEl?.parentNode === parent)
145
+ staying.add(e.decalEl);
146
+ }
147
+ const skipStale = (c) => {
148
+ while (c && !staying.has(c))
149
+ c = c.nextSibling;
150
+ return c;
151
+ };
152
+ let cursor = skipStale(parent.firstChild);
153
+ for (let k = 0; k < ordered.length; k++) {
154
+ const { node } = ordered[k];
155
+ const key = keys[k];
156
+ let entry = this.entries.get(key);
157
+ const boxed = node.kind === 'text' && hasTextBox(node.style);
158
+ if (entry && (entry.kind !== node.kind || (node.kind === 'text' && entry.boxed !== boxed))) {
159
+ // O nó trocado pode ser justamente o cursor (o próximo do lugar certo): o cursor avança antes de ele sair.
160
+ while (cursor && (cursor === entry.el || cursor === entry.decalEl))
161
+ cursor = cursor.nextSibling;
162
+ cursor = skipStale(cursor);
163
+ entry.el.remove();
164
+ entry.decalEl?.remove();
165
+ this.entries.delete(key);
166
+ entry = undefined;
167
+ }
168
+ const isNew = !entry;
169
+ if (!entry) {
170
+ const tag = node.kind === 'group' || boxed ? 'g' : node.kind === 'path' ? 'path' : 'text';
171
+ const el = this.doc.createElementNS(SVG_NS, tag);
172
+ entry = { key, kind: node.kind, el, node, opacity: 1, seen: true, boxed };
173
+ this.entries.set(key, entry);
174
+ this.nodeOf.set(el, entry);
175
+ }
176
+ entry.seen = true;
177
+ const previous = entry.node;
178
+ entry.node = node;
179
+ // Posição na ordem certa (sem mexer em quem já está no lugar).
180
+ if (cursor !== entry.el)
181
+ parent.insertBefore(entry.el, cursor);
182
+ cursor = skipStale(entry.el.nextSibling);
183
+ if (node.kind === 'path') {
184
+ this.updatePath(entry, node, isNew, previous);
185
+ if (entry.decalEl) {
186
+ if (cursor !== entry.decalEl)
187
+ parent.insertBefore(entry.decalEl, cursor);
188
+ cursor = skipStale(entry.decalEl.nextSibling);
189
+ }
190
+ }
191
+ else if (node.kind === 'text')
192
+ this.updateText(entry, node, isNew);
193
+ else
194
+ this.updateGroup(entry, node);
195
+ this.applyCommon(entry.el, node);
196
+ }
197
+ // O que sobrou depois do cursor não está mais na cena deste pai: sai no fim (entradas não vistas).
198
+ }
199
+ applyCommon(el, node) {
200
+ const t = node.transform;
201
+ // Símbolo com `rotate` (graus, sentido anti-horário, como `symbolRotate`) gira em torno do próprio centro.
202
+ const symbolRotate = node.kind === 'path' && node.shape.type === 'symbol' && node.shape.rotate ? node.shape : null;
203
+ if ((t || symbolRotate) && node.kind !== 'text') {
204
+ const parts = [];
205
+ if (symbolRotate && symbolRotate.type === 'symbol')
206
+ parts.push(`rotate(${num(-(symbolRotate.rotate ?? 0))} ${num(symbolRotate.x)} ${num(symbolRotate.y)})`);
207
+ if (!t) {
208
+ setAttr(el, 'transform', parts.join(' '));
209
+ }
210
+ }
211
+ if (t && node.kind !== 'text') {
212
+ const parts = symbolRotate && symbolRotate.type === 'symbol' ? [`rotate(${num(-(symbolRotate.rotate ?? 0))} ${num(symbolRotate.x)} ${num(symbolRotate.y)})`] : [];
213
+ if (t.x || t.y)
214
+ parts.push(`translate(${num(t.x ?? 0)} ${num(t.y ?? 0)})`);
215
+ if (t.rotation || (t.scaleX ?? 1) !== 1 || (t.scaleY ?? 1) !== 1) {
216
+ const ox = t.originX ?? 0;
217
+ const oy = t.originY ?? 0;
218
+ if (ox || oy)
219
+ parts.push(`translate(${num(ox)} ${num(oy)})`);
220
+ if (t.rotation)
221
+ parts.push(`rotate(${num((t.rotation * 180) / Math.PI)})`);
222
+ if ((t.scaleX ?? 1) !== 1 || (t.scaleY ?? 1) !== 1)
223
+ parts.push(`scale(${num(t.scaleX ?? 1)} ${num(t.scaleY ?? 1)})`);
224
+ if (ox || oy)
225
+ parts.push(`translate(${num(-ox)} ${num(-oy)})`);
226
+ }
227
+ // A rotação do símbolo vem por último (aplicada primeiro, no espaço do próprio nó). Só ela muda de lugar:
228
+ // sem rotação de símbolo, a ordem translate(origem) → rotate → translate(-origem) fica como está.
229
+ if (symbolRotate) {
230
+ const own = parts.shift();
231
+ if (own)
232
+ parts.push(own);
233
+ }
234
+ setAttr(el, 'transform', parts.join(' ') || null);
235
+ }
236
+ else if (node.kind !== 'text' && !symbolRotate)
237
+ setAttr(el, 'transform', null);
238
+ setAttr(el, 'aria-hidden', node.ariaHidden ? 'true' : null);
239
+ setAttr(el, 'class', node.className ?? null);
240
+ if (node.silent)
241
+ el.style.pointerEvents = 'none';
242
+ else {
243
+ el.style.pointerEvents = '';
244
+ el.style.cursor = node.cursor ?? (node.item && node.item.componentType !== 'title' ? 'pointer' : '');
245
+ }
246
+ }
247
+ updateGroup(entry, node) {
248
+ if (node.clip) {
249
+ const id = this.clipPath(node.key, node.clip);
250
+ setAttr(entry.el, 'clip-path', `url(#${id})`);
251
+ }
252
+ else
253
+ setAttr(entry.el, 'clip-path', null);
254
+ this.reconcile(entry.el, node.children);
255
+ // Translação de entrada pela API de animações do navegador (sem ela, o grupo só aparece no lugar).
256
+ const slide = node.slide;
257
+ const el = entry.el;
258
+ if (slide && slide.dx && slide.duration > 0 && typeof el.animate === 'function') {
259
+ el.animate([{ transform: `translate(${slide.dx}px, 0px)` }, { transform: 'translate(0px, 0px)' }], { duration: slide.duration, easing: 'linear' });
260
+ }
261
+ }
262
+ // ---------------------------------------------------------------- caminhos
263
+ updatePath(entry, node, isNew, previous) {
264
+ const el = entry.el;
265
+ const style = node.style;
266
+ this.applyFillStyle(el, style, node.key);
267
+ const targetOpacity = style.opacity ?? 1;
268
+ const anim = node.animation;
269
+ const s = this.settings;
270
+ const lineWidth = style.stroke ? (style.lineWidth ?? 1) : 0;
271
+ let tween;
272
+ if (s.animate && anim !== false) {
273
+ if (isNew && anim?.from) {
274
+ const fromShape = anim.from.shape ? { ...node.shape, ...anim.from.shape } : undefined;
275
+ tween = {
276
+ start: now(),
277
+ delay: anim.delay ?? 0,
278
+ duration: anim.duration ?? s.duration,
279
+ easing: anim.easing ?? s.easing,
280
+ fromShape,
281
+ toShape: fromShape ? node.shape : undefined,
282
+ fromOpacity: anim.from.style?.opacity,
283
+ toOpacity: anim.from.style?.opacity !== undefined ? targetOpacity : undefined,
284
+ };
285
+ }
286
+ else if (!isNew && entry.shape && !sameShape(entry.shape, node.shape) && previous.kind === 'path') {
287
+ tween = { start: now(), delay: 0, duration: anim?.duration ?? s.durationUpdate, easing: s.easingUpdate, fromShape: entry.shape, toShape: node.shape };
288
+ }
289
+ if (!isNew && entry.opacity !== targetOpacity) {
290
+ tween = tween ?? { start: now(), delay: 0, duration: s.durationUpdate, easing: s.easingUpdate };
291
+ tween.fromOpacity = entry.opacity;
292
+ tween.toOpacity = targetOpacity;
293
+ }
294
+ }
295
+ if (tween && tween.duration > 0) {
296
+ entry.tween = tween;
297
+ this.animating.add(entry);
298
+ this.applyFrame(entry, 0);
299
+ }
300
+ else {
301
+ entry.tween = undefined;
302
+ this.animating.delete(entry);
303
+ entry.shape = node.shape;
304
+ entry.opacity = targetOpacity;
305
+ setAttr(el, 'd', shapeToPath(node.shape, lineWidth) || 'M0 0');
306
+ setAttr(el, 'opacity', targetOpacity === 1 ? null : num(targetOpacity));
307
+ }
308
+ // Padrão de acessibilidade (decal): um segundo caminho por cima, com o padrão.
309
+ if (style.decal) {
310
+ if (!entry.decalEl) {
311
+ entry.decalEl = this.doc.createElementNS(SVG_NS, 'path');
312
+ entry.decalEl.setAttribute('aria-hidden', 'true');
313
+ entry.decalEl.style.pointerEvents = 'none';
314
+ this.nodeOf.set(entry.decalEl, entry);
315
+ }
316
+ setAttr(entry.decalEl, 'fill', `url(#${this.pattern(style.decal)})`);
317
+ setAttr(entry.decalEl, 'd', el.getAttribute('d'));
318
+ }
319
+ else if (entry.decalEl) {
320
+ entry.decalEl.remove();
321
+ entry.decalEl = undefined;
322
+ }
323
+ }
324
+ applyFillStyle(el, style, key) {
325
+ setAttr(el, 'fill', style.fill === undefined || style.fill === 'none' ? 'none' : this.paint(style.fill, key, 'fill'));
326
+ setAttr(el, 'stroke', style.stroke === undefined || style.stroke === 'none' ? null : this.paint(style.stroke, key, 'stroke'));
327
+ const hasStroke = style.stroke !== undefined && style.stroke !== 'none';
328
+ setAttr(el, 'stroke-width', hasStroke ? num(style.lineWidth ?? 1) : null);
329
+ setAttr(el, 'fill-opacity', style.fillOpacity === undefined || style.fillOpacity === 1 ? null : num(style.fillOpacity));
330
+ setAttr(el, 'stroke-opacity', style.strokeOpacity === undefined || style.strokeOpacity === 1 ? null : num(style.strokeOpacity));
331
+ setAttr(el, 'stroke-dasharray', hasStroke && style.lineDash?.length ? style.lineDash.map(num).join(' ') : null);
332
+ setAttr(el, 'stroke-dashoffset', hasStroke && style.lineDashOffset ? num(style.lineDashOffset) : null);
333
+ setAttr(el, 'stroke-linecap', hasStroke && style.lineCap && style.lineCap !== 'butt' ? style.lineCap : null);
334
+ setAttr(el, 'stroke-linejoin', hasStroke && style.lineJoin && style.lineJoin !== 'miter' ? style.lineJoin : null);
335
+ setAttr(el, 'filter', style.shadowBlur || style.shadowOffsetX || style.shadowOffsetY ? `url(#${this.shadow(style)})` : null);
336
+ }
337
+ // ---------------------------------------------------------------- texto
338
+ updateText(entry, node, isNew) {
339
+ const s = this.settings;
340
+ const target = [node.x, node.y];
341
+ const anim = node.animation;
342
+ if (s.animate && anim !== false && !isNew && entry.xy && (entry.xy[0] !== target[0] || entry.xy[1] !== target[1])) {
343
+ entry.tween = { start: now(), delay: 0, duration: s.durationUpdate, easing: s.easingUpdate, fromXY: entry.xy, toXY: target };
344
+ this.animating.add(entry);
345
+ }
346
+ else if (s.animate && anim && isNew && anim.from?.style?.opacity !== undefined) {
347
+ entry.tween = { start: now(), delay: anim.delay ?? 0, duration: anim.duration ?? s.duration, easing: anim.easing ?? s.easing, fromOpacity: anim.from.style.opacity, toOpacity: node.style.opacity ?? 1 };
348
+ this.animating.add(entry);
349
+ }
350
+ else {
351
+ entry.tween = undefined;
352
+ this.animating.delete(entry);
353
+ }
354
+ this.drawText(entry, node, entry.tween?.fromXY ?? target);
355
+ entry.xy = entry.tween?.fromXY ?? target;
356
+ if (!entry.tween)
357
+ entry.xy = target;
358
+ entry.opacity = node.style.opacity ?? 1;
359
+ if (entry.tween?.fromOpacity !== undefined)
360
+ setAttr(entry.el, 'opacity', num(entry.tween.fromOpacity));
361
+ }
362
+ drawText(entry, node, [x, y]) {
363
+ const style = node.style;
364
+ const fontSize = style.fontSize ?? DEFAULT_FONT_SIZE;
365
+ const measured = measureText(node.text, style);
366
+ const lh = lineHeightOf(style);
367
+ const [pt, pr, pb, pl] = padding(style.padding);
368
+ const align = style.align ?? 'left';
369
+ const valign = style.verticalAlign ?? 'top';
370
+ const blockH = measured.height;
371
+ const top = (valign === 'middle' ? y - blockH / 2 : valign === 'bottom' ? y - blockH : y) + (valign === 'top' ? pt : valign === 'bottom' ? -pb : (pt - pb) / 2);
372
+ const anchorX = x + (align === 'left' ? pl : align === 'right' ? -pr : (pl - pr) / 2);
373
+ const text = (entry.boxed ? entry.el.lastChild : entry.el);
374
+ let textEl = text;
375
+ if (entry.boxed) {
376
+ let rect = entry.el.firstChild;
377
+ if (!rect || rect.tagName !== 'rect') {
378
+ entry.el.textContent = '';
379
+ rect = this.doc.createElementNS(SVG_NS, 'rect');
380
+ textEl = this.doc.createElementNS(SVG_NS, 'text');
381
+ entry.el.append(rect, textEl);
382
+ }
383
+ const boxW = measured.width + pl + pr;
384
+ const boxH = blockH + pt + pb;
385
+ const left = (align === 'left' ? x : align === 'right' ? x - boxW : x - boxW / 2);
386
+ const boxTop = top - pt;
387
+ setAttr(rect, 'x', num(left));
388
+ setAttr(rect, 'y', num(boxTop));
389
+ setAttr(rect, 'width', num(boxW));
390
+ setAttr(rect, 'height', num(boxH));
391
+ setAttr(rect, 'rx', style.borderRadius ? num(style.borderRadius) : null);
392
+ setAttr(rect, 'fill', style.backgroundColor ?? 'none');
393
+ setAttr(rect, 'stroke', style.borderColor ?? null);
394
+ setAttr(rect, 'stroke-width', style.borderColor ? num(style.borderWidth ?? 1) : null);
395
+ }
396
+ const el = textEl;
397
+ setAttr(el, 'text-anchor', align === 'center' ? 'middle' : align === 'right' ? 'end' : null);
398
+ setAttr(el, 'font-size', num(fontSize));
399
+ setAttr(el, 'font-family', style.fontFamily ?? DEFAULT_FONT_FAMILY);
400
+ setAttr(el, 'font-weight', style.fontWeight !== undefined && style.fontWeight !== 'normal' ? String(style.fontWeight) : null);
401
+ setAttr(el, 'font-style', style.fontStyle && style.fontStyle !== 'normal' ? style.fontStyle : null);
402
+ setAttr(el, 'fill', style.color ?? '#333');
403
+ setAttr(el, 'opacity', style.opacity !== undefined && style.opacity !== 1 ? num(style.opacity) : null);
404
+ if (style.textBorderColor && (style.textBorderWidth ?? 0) > 0) {
405
+ setAttr(el, 'stroke', style.textBorderColor);
406
+ setAttr(el, 'stroke-width', num((style.textBorderWidth ?? 0) * 2));
407
+ setAttr(el, 'paint-order', 'stroke');
408
+ setAttr(el, 'stroke-linejoin', 'round');
409
+ }
410
+ else {
411
+ setAttr(el, 'stroke', null);
412
+ setAttr(el, 'stroke-width', null);
413
+ setAttr(el, 'paint-order', null);
414
+ }
415
+ const holder = entry.boxed ? entry.el : el;
416
+ setAttr(holder, 'transform', node.rotate ? `rotate(${num(-node.rotate)} ${num(x)} ${num(y)})` : null);
417
+ // Linha de base calculada (sem `dominant-baseline`, que varia entre navegadores).
418
+ const baseline = (i) => top + i * lh + (lh - fontSize) / 2 + fontSize * 0.82;
419
+ const lines = measured.lines;
420
+ if (lines.length === 1) {
421
+ if (el.childNodes.length !== 1 || el.firstChild?.nodeType !== 3)
422
+ el.textContent = lines[0];
423
+ else if (el.textContent !== lines[0])
424
+ el.textContent = lines[0];
425
+ setAttr(el, 'x', num(anchorX));
426
+ setAttr(el, 'y', num(baseline(0)));
427
+ }
428
+ else {
429
+ el.textContent = '';
430
+ el.removeAttribute('x');
431
+ el.removeAttribute('y');
432
+ lines.forEach((line, i) => {
433
+ const tspan = this.doc.createElementNS(SVG_NS, 'tspan');
434
+ tspan.setAttribute('x', num(anchorX));
435
+ tspan.setAttribute('y', num(baseline(i)));
436
+ tspan.textContent = line;
437
+ el.appendChild(tspan);
438
+ });
439
+ }
440
+ }
441
+ // ---------------------------------------------------------------- animação
442
+ schedule() {
443
+ if (this.frame)
444
+ return;
445
+ const raf = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : null;
446
+ if (!raf) {
447
+ for (const entry of this.animating)
448
+ this.applyFrame(entry, Number.POSITIVE_INFINITY);
449
+ this.animating.clear();
450
+ this.onFinished();
451
+ return;
452
+ }
453
+ this.frame = raf(() => {
454
+ this.frame = 0;
455
+ const t = now();
456
+ for (const entry of [...this.animating]) {
457
+ if (this.applyFrame(entry, t))
458
+ this.animating.delete(entry);
459
+ }
460
+ if (this.animating.size)
461
+ this.schedule();
462
+ else
463
+ this.onFinished();
464
+ });
465
+ }
466
+ /** Desenha o quadro `time` da animação do nó; `true` quando acabou. */
467
+ applyFrame(entry, time) {
468
+ const tw = entry.tween;
469
+ if (!tw)
470
+ return true;
471
+ const elapsed = time - tw.start - tw.delay;
472
+ const raw = tw.duration > 0 ? Math.max(0, Math.min(1, elapsed / tw.duration)) : 1;
473
+ const k = EASINGS[tw.easing]?.(raw) ?? raw;
474
+ const node = entry.node;
475
+ if (node.kind === 'path') {
476
+ const lineWidth = node.style.stroke ? (node.style.lineWidth ?? 1) : 0;
477
+ if (tw.fromShape && tw.toShape) {
478
+ const shape = interpolateShape(tw.fromShape, tw.toShape, k);
479
+ entry.shape = shape;
480
+ setAttr(entry.el, 'd', shapeToPath(shape, lineWidth) || 'M0 0');
481
+ if (entry.decalEl)
482
+ setAttr(entry.decalEl, 'd', entry.el.getAttribute('d'));
483
+ }
484
+ else {
485
+ entry.shape = node.shape;
486
+ setAttr(entry.el, 'd', shapeToPath(node.shape, lineWidth) || 'M0 0');
487
+ }
488
+ }
489
+ else if (node.kind === 'text' && tw.fromXY && tw.toXY) {
490
+ const xy = [tw.fromXY[0] + (tw.toXY[0] - tw.fromXY[0]) * k, tw.fromXY[1] + (tw.toXY[1] - tw.fromXY[1]) * k];
491
+ entry.xy = xy;
492
+ this.drawText(entry, node, xy);
493
+ }
494
+ if (tw.fromOpacity !== undefined && tw.toOpacity !== undefined) {
495
+ const o = tw.fromOpacity + (tw.toOpacity - tw.fromOpacity) * k;
496
+ entry.opacity = o;
497
+ setAttr(entry.el, 'opacity', o >= 1 ? null : num(o));
498
+ }
499
+ if (raw >= 1) {
500
+ entry.tween = undefined;
501
+ return true;
502
+ }
503
+ return false;
504
+ }
505
+ // ---------------------------------------------------------------- defs
506
+ def(id, make) {
507
+ const full = this.prefix + id;
508
+ this.defsUsed.add(full);
509
+ if (!this.defsMade.has(full)) {
510
+ const el = make();
511
+ el.setAttribute('id', full);
512
+ this.defs.appendChild(el);
513
+ this.defsMade.set(full, el);
514
+ }
515
+ return full;
516
+ }
517
+ paint(paint, key, role) {
518
+ if (typeof paint === 'string')
519
+ return paint;
520
+ const id = `g${hash(JSON.stringify(paint))}`;
521
+ void key;
522
+ void role;
523
+ return `url(#${this.def(id, () => this.gradient(paint))})`;
524
+ }
525
+ gradient(g) {
526
+ const el = this.doc.createElementNS(SVG_NS, g.type === 'radial' ? 'radialGradient' : 'linearGradient');
527
+ if (g.global)
528
+ el.setAttribute('gradientUnits', 'userSpaceOnUse');
529
+ if (g.type === 'radial') {
530
+ el.setAttribute('cx', num(g.x ?? 0.5));
531
+ el.setAttribute('cy', num(g.y ?? 0.5));
532
+ el.setAttribute('r', num(g.r ?? 0.5));
533
+ }
534
+ else {
535
+ el.setAttribute('x1', num(g.x ?? 0));
536
+ el.setAttribute('y1', num(g.y ?? 0));
537
+ el.setAttribute('x2', num(g.x2 ?? 1));
538
+ el.setAttribute('y2', num(g.y2 ?? 0));
539
+ }
540
+ for (const stop of g.colorStops) {
541
+ const s = this.doc.createElementNS(SVG_NS, 'stop');
542
+ s.setAttribute('offset', num(stop.offset));
543
+ s.setAttribute('stop-color', stop.color);
544
+ el.appendChild(s);
545
+ }
546
+ return el;
547
+ }
548
+ pattern(decal) {
549
+ const id = `p${hash(JSON.stringify(decal))}`;
550
+ return this.def(id, () => {
551
+ const size = Math.max(4, decal.size);
552
+ const p = this.doc.createElementNS(SVG_NS, 'pattern');
553
+ p.setAttribute('patternUnits', 'userSpaceOnUse');
554
+ p.setAttribute('width', num(size));
555
+ p.setAttribute('height', num(size));
556
+ if (decal.rotation)
557
+ p.setAttribute('patternTransform', `rotate(${num((decal.rotation * 180) / Math.PI)})`);
558
+ const shape = this.doc.createElementNS(SVG_NS, 'path');
559
+ const c = size / 2;
560
+ const r = size / 4;
561
+ const d = {
562
+ rect: `M${c - r} ${c - r}h${2 * r}v${2 * r}h${-2 * r}Z`,
563
+ circle: `M${c - r} ${c}a${r} ${r} 0 1 0 ${2 * r} 0a${r} ${r} 0 1 0 ${-2 * r} 0Z`,
564
+ triangle: `M${c} ${c - r}L${c + r} ${c + r}L${c - r} ${c + r}Z`,
565
+ diamond: `M${c} ${c - r}L${c + r} ${c}L${c} ${c + r}L${c - r} ${c}Z`,
566
+ line: `M0 ${c - 1}h${size}v2H0Z`,
567
+ cross: `M${c - 1} 0h2v${size}h-2ZM0 ${c - 1}h${size}v2H0Z`,
568
+ };
569
+ shape.setAttribute('d', d[decal.symbol]);
570
+ shape.setAttribute('fill', decal.color);
571
+ p.appendChild(shape);
572
+ return p;
573
+ });
574
+ }
575
+ shadow(style) {
576
+ const key = `${style.shadowBlur ?? 0}|${style.shadowColor ?? ''}|${style.shadowOffsetX ?? 0}|${style.shadowOffsetY ?? 0}`;
577
+ return this.def(`s${hash(key)}`, () => {
578
+ const f = this.doc.createElementNS(SVG_NS, 'filter');
579
+ f.setAttribute('x', '-50%');
580
+ f.setAttribute('y', '-50%');
581
+ f.setAttribute('width', '200%');
582
+ f.setAttribute('height', '200%');
583
+ const drop = this.doc.createElementNS(SVG_NS, 'feDropShadow');
584
+ drop.setAttribute('dx', num(style.shadowOffsetX ?? 0));
585
+ drop.setAttribute('dy', num(style.shadowOffsetY ?? 0));
586
+ drop.setAttribute('stdDeviation', num((style.shadowBlur ?? 0) / 2));
587
+ drop.setAttribute('flood-color', style.shadowColor ?? 'rgba(0,0,0,0.3)');
588
+ f.appendChild(drop);
589
+ return f;
590
+ });
591
+ }
592
+ clipPath(key, rect) {
593
+ const id = `c${hash(`${key}|${rect.x}|${rect.y}|${rect.width}|${rect.height}`)}`;
594
+ return this.def(id, () => {
595
+ const c = this.doc.createElementNS(SVG_NS, 'clipPath');
596
+ const r = this.doc.createElementNS(SVG_NS, 'rect');
597
+ r.setAttribute('x', num(rect.x));
598
+ r.setAttribute('y', num(rect.y));
599
+ r.setAttribute('width', num(Math.max(0, rect.width)));
600
+ r.setAttribute('height', num(Math.max(0, rect.height)));
601
+ c.appendChild(r);
602
+ return c;
603
+ });
604
+ }
605
+ }
606
+ // ---------------------------------------------------------------- auxiliares
607
+ function now() {
608
+ return typeof performance !== 'undefined' ? performance.now() : Date.now();
609
+ }
610
+ function num(v) {
611
+ if (!Number.isFinite(v))
612
+ return '0';
613
+ const r = Math.round(v * 1000) / 1000;
614
+ return Object.is(r, -0) ? '0' : String(r);
615
+ }
616
+ function setAttr(el, name, value) {
617
+ if (value === null || value === undefined) {
618
+ if (el.hasAttribute(name))
619
+ el.removeAttribute(name);
620
+ }
621
+ else if (el.getAttribute(name) !== value)
622
+ el.setAttribute(name, value);
623
+ }
624
+ function hasTextBox(style) {
625
+ return !!(style.backgroundColor || style.borderColor);
626
+ }
627
+ function padding(p) {
628
+ if (Array.isArray(p)) {
629
+ if (p.length === 2)
630
+ return [p[0], p[1], p[0], p[1]];
631
+ if (p.length === 4)
632
+ return [p[0], p[1], p[2], p[3]];
633
+ const v = p[0] ?? 0;
634
+ return [v, v, v, v];
635
+ }
636
+ const v = p ?? 0;
637
+ return [v, v, v, v];
638
+ }
639
+ function sameShape(a, b) {
640
+ return JSON.stringify(a) === JSON.stringify(b);
641
+ }
642
+ /** Hash curto e estável (FNV-1a) para ids de defs. */
643
+ function hash(text) {
644
+ let h = 0x811c9dc5;
645
+ for (let i = 0; i < text.length; i++) {
646
+ h ^= text.charCodeAt(i);
647
+ h = Math.imul(h, 0x01000193);
648
+ }
649
+ return (h >>> 0).toString(36);
650
+ }
651
+ //# sourceMappingURL=svg.js.map