@defra/interactive-map 0.0.52-alpha → 0.0.53-alpha

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 (147) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/umd/im-core.js +1 -1
  3. package/dist/umd/index.js +1 -1
  4. package/docs/api/context.md +18 -12
  5. package/docs/api/marker-config.md +16 -8
  6. package/docs/api/symbol-config.md +24 -9
  7. package/docs/api/symbol-registry.md +49 -14
  8. package/docs/plugins/datasets.md +4 -10
  9. package/docs/plugins/draw.md +2 -1
  10. package/docs/plugins/map-key.md +6 -4
  11. package/package.json +1 -1
  12. package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  13. package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  14. package/plugins/beta/map-styles/dist/umd/index.js +1 -1
  15. package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
  16. package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
  17. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  18. package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
  19. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  20. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  21. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  22. package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +9 -9
  23. package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +19 -3
  24. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.js +9 -3
  25. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.test.js +18 -3
  26. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.js +3 -3
  27. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +19 -26
  28. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +15 -67
  29. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +32 -105
  30. package/plugins/datasets/src/adapters/openlayers/layerBuilders.js +11 -13
  31. package/plugins/datasets/src/adapters/openlayers/layerBuilders.test.js +11 -3
  32. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.js +48 -19
  33. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +99 -11
  34. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +26 -26
  35. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +48 -29
  36. package/plugins/datasets/src/initialise/defaults.js +4 -3
  37. package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
  38. package/plugins/datasets/src/registry/dataset.js +2 -1
  39. package/plugins/datasets/src/registry/dataset.test.js +13 -0
  40. package/plugins/datasets/src/registry/mapboxStyleDataset.js +1 -1
  41. package/plugins/datasets/src/registry/mapboxStyleDataset.test.js +1 -1
  42. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  43. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  44. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  45. package/plugins/draw/dist/esm/index.js +1 -1
  46. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  47. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  48. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  49. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  50. package/plugins/draw/dist/umd/index.js +1 -1
  51. package/plugins/draw/src/DrawInit.jsx +7 -5
  52. package/plugins/draw/src/DrawInit.test.jsx +18 -2
  53. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +11 -5
  54. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -11
  55. package/plugins/draw/src/adapters/maplibre/mapboxDraw.js +12 -9
  56. package/plugins/draw/src/adapters/maplibre/mapboxDraw.test.js +18 -0
  57. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.js +81 -56
  58. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.test.js +149 -80
  59. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +7 -6
  60. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +22 -25
  61. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +26 -8
  62. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +58 -16
  63. package/plugins/draw/src/adapters/openlayers/core/styles.js +33 -20
  64. package/plugins/draw/src/adapters/openlayers/core/styles.test.js +47 -21
  65. package/plugins/draw/src/adapters/openlayers/olDraw.js +16 -12
  66. package/plugins/draw/src/adapters/openlayers/olDraw.test.js +27 -32
  67. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.js +44 -49
  68. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.test.js +100 -119
  69. package/plugins/draw/src/defaults.js +2 -6
  70. package/plugins/draw/src/utils/stripInternalProperties.test.js +2 -1
  71. package/plugins/draw/src/utils/symbolKeys.js +3 -4
  72. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  73. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  74. package/plugins/interact/dist/umd/index.js +1 -1
  75. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  76. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -2
  77. package/plugins/map-key/dist/umd/index.js +1 -1
  78. package/plugins/map-key/src/components/Key/Key.jsx +8 -4
  79. package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +3 -1
  80. package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
  81. package/plugins/map-key/src/components/Key/KeySvg.jsx +15 -14
  82. package/plugins/map-key/src/components/Key/KeySvg.test.jsx +21 -14
  83. package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +1 -2
  84. package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +3 -1
  85. package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +26 -12
  86. package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +24 -74
  87. package/plugins/map-key/src/components/Key/MapKey.jsx +3 -1
  88. package/plugins/map-key/src/components/Key/svgProperties.js +8 -2
  89. package/plugins/map-key/src/reducers/mergeKeyGroupItems.test.js +15 -0
  90. package/plugins/map-key/src/registry/index.js +0 -2
  91. package/plugins/map-key/src/registry/index.test.js +1 -9
  92. package/plugins/menu/src/registry/index.js +0 -2
  93. package/plugins/menu/src/registry/index.test.js +1 -9
  94. package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
  95. package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  96. package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  97. package/providers/beta/openlayers/dist/umd/index.js +1 -1
  98. package/providers/beta/openlayers/src/openlayersProvider.js +102 -2
  99. package/providers/beta/openlayers/src/openlayersProvider.test.js +95 -0
  100. package/providers/beta/openlayers/src/utils/highlightFeatures.js +20 -25
  101. package/providers/beta/openlayers/src/utils/highlightFeatures.test.js +73 -38
  102. package/providers/beta/openlayers/src/utils/patternImages.js +61 -0
  103. package/providers/beta/openlayers/src/utils/patternImages.test.js +74 -0
  104. package/providers/beta/openlayers/src/utils/symbolImages.js +103 -95
  105. package/providers/beta/openlayers/src/utils/symbolImages.test.js +136 -85
  106. package/providers/mapProvider.js +53 -1
  107. package/providers/mapProvider.test.js +6 -0
  108. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  109. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  110. package/providers/maplibre/dist/umd/index.js +1 -1
  111. package/providers/maplibre/src/maplibreProvider.js +50 -17
  112. package/providers/maplibre/src/maplibreProvider.test.js +74 -20
  113. package/providers/maplibre/src/utils/highlightFeatures.js +19 -14
  114. package/providers/maplibre/src/utils/highlightFeatures.test.js +20 -14
  115. package/providers/maplibre/src/utils/patternImages.js +46 -29
  116. package/providers/maplibre/src/utils/patternImages.test.js +52 -4
  117. package/providers/maplibre/src/utils/symbolImages.js +80 -39
  118. package/providers/maplibre/src/utils/symbolImages.test.js +157 -54
  119. package/src/App/components/Markers/Markers.jsx +16 -25
  120. package/src/App/components/Markers/Markers.test.jsx +34 -48
  121. package/src/App/components/Markers/SymbolLabelMarker.jsx +1 -1
  122. package/src/App/components/Markers/SymbolMarker.jsx +1 -1
  123. package/src/App/store/ServiceProvider.jsx +6 -3
  124. package/src/App/store/ServiceProvider.test.jsx +24 -0
  125. package/src/config/appConfig.js +1 -5
  126. package/src/config/mapSizeScales.js +8 -0
  127. package/src/config/symbolConfig.js +101 -25
  128. package/src/services/patternRegistry.js +97 -76
  129. package/src/services/patternRegistry.test.js +19 -1
  130. package/src/services/symbolComposer.js +184 -0
  131. package/src/services/symbolComposer.test.js +132 -0
  132. package/src/services/symbolRegistry.js +326 -155
  133. package/src/services/symbolRegistry.test.js +334 -32
  134. package/src/types.js +9 -3
  135. package/src/utils/hashString.js +23 -0
  136. package/src/utils/hashString.test.js +29 -0
  137. package/src/utils/imageDataCache.js +43 -0
  138. package/src/utils/imageDataCache.test.js +38 -0
  139. package/src/utils/pathBounds.js +330 -0
  140. package/src/utils/pathBounds.test.js +145 -0
  141. package/src/utils/patternUtils.js +0 -8
  142. package/src/utils/patternUtils.test.js +0 -25
  143. package/src/utils/recentImageSets.js +35 -0
  144. package/src/utils/recentImageSets.test.js +46 -0
  145. package/src/utils/symbolUtils.js +26 -29
  146. package/src/utils/symbolUtils.test.js +27 -41
  147. package/plugins/map-key/dist/umd/im-map-key-plugin.js.LICENSE.txt +0 -1
@@ -0,0 +1,184 @@
1
+ import {
2
+ HALO_STROKE_WIDTH, SELECTED_RING_STROKE_WIDTH, ACTIVE_RING_STROKE_WIDTH, SYMBOL_PADDING, DEFAULT_SYMBOL_ANCHOR
3
+ } from '../config/symbolConfig.js'
4
+ import { getPathBounds } from '../utils/pathBounds.js'
5
+ import { logger } from './logger.js'
6
+
7
+ // Pure geometry and SVG building for symbols — no registry state. symbolRegistry resolves which
8
+ // definition and token values apply; this turns them into a sized definition and SVG markup.
9
+
10
+ const GRAPHIC_SIZE = 16 // graphics are authored in a 16×16 space...
11
+ const GRAPHIC_SCALE = 0.8 // ...and drawn at 0.8 of that, centred on the shape's graphicCentre
12
+ const PRECISION = 1000 // 3dp: sub-pixel precision for SVG attributes
13
+ const HALF = 0.5
14
+ const BOTH_SIDES = 2
15
+ // Built-in viewBoxes are rounded up to a multiple of this, so viewBox × pixel ratio is a whole
16
+ // number of pixels at every common ratio (device 1, 1.25, 1.5, 2 × map size 1, 1.5, 2 — all but
17
+ // 1.875). A fractional size would be truncated when rasterised, squashing the image slightly
18
+ // and shifting its anchor.
19
+ const SIZE_STEP = 4
20
+ const roundUpToStep = (value) => Math.ceil(value / SIZE_STEP) * SIZE_STEP
21
+ const round3 = (value) => Math.round(value * PRECISION) / PRECISION
22
+
23
+ // Each invalid symbolAnchor is reported once, rather than on every render
24
+ const reportedAnchors = new Set()
25
+ const ANCHOR_LENGTH = 2
26
+ const isValidAnchor = (anchor) => Array.isArray(anchor) && anchor.length === ANCHOR_LENGTH && anchor.every(Number.isFinite)
27
+
28
+ // Built-in (path-based) shapes, composed per scale: symbolDef → Map(scale → sized def)
29
+ const composedCache = new WeakMap()
30
+
31
+ /**
32
+ * Sizes a path-based built-in shape. The body and graphic scale; the halo and rings are drawn
33
+ * as strokes at a fixed width (see renderComposed), so the viewBox is the scaled body plus a
34
+ * fixed SYMBOL_PADDING, rounded up to a SIZE_STEP so rasterised images have exact dimensions.
35
+ *
36
+ * @param {Object} symbolDef - { path, bounds, anchorPoint, graphicCentre, ... }
37
+ * @param {number} scale
38
+ * @returns {Object} the def plus scale, viewBox, anchor (fractional), bodyBox and transform
39
+ */
40
+ export const composeSymbolDef = (symbolDef, scale) => {
41
+ let byScale = composedCache.get(symbolDef)
42
+ if (!byScale) {
43
+ byScale = new Map()
44
+ composedCache.set(symbolDef, byScale)
45
+ }
46
+ if (byScale.has(scale)) {
47
+ return byScale.get(scale)
48
+ }
49
+ const [boundsX, boundsY, boundsWidth, boundsHeight] = symbolDef.bounds
50
+ const width = roundUpToStep(boundsWidth * scale + SYMBOL_PADDING * BOTH_SIDES)
51
+ const height = roundUpToStep(boundsHeight * scale + SYMBOL_PADDING * BOTH_SIDES)
52
+ const offsetX = (width - boundsWidth * scale) * HALF
53
+ const offsetY = (height - boundsHeight * scale) * HALF
54
+ const [anchorX, anchorY] = symbolDef.anchorPoint
55
+ const sized = {
56
+ ...symbolDef,
57
+ scale,
58
+ viewBox: `0 0 ${width} ${height}`,
59
+ anchor: [((anchorX - boundsX) * scale + offsetX) / width, ((anchorY - boundsY) * scale + offsetY) / height],
60
+ // The body's own box within the viewBox — what a symbolAnchor override is a fraction of
61
+ bodyBox: [offsetX, offsetY, boundsWidth * scale, boundsHeight * scale],
62
+ transform: `translate(${round3(offsetX)}, ${round3(offsetY)}) scale(${scale}) translate(${-boundsX}, ${-boundsY})`
63
+ }
64
+ byScale.set(scale, sized)
65
+ return sized
66
+ }
67
+
68
+ /**
69
+ * Applies a symbolAnchor override to a sized def. For a built-in shape the override is a
70
+ * fraction of the shape itself ([0.5, 1] = its bottom edge), so it stays on the same point of the
71
+ * shape at every size; for an SVG-template symbol it's a fraction of its own viewBox. An invalid
72
+ * override is warned about (once) and ignored, keeping the symbol's own anchor.
73
+ *
74
+ * @param {Object} sizedDef
75
+ * @param {number[]} [anchor] - [x, y] in 0–1
76
+ * @returns {Object}
77
+ */
78
+ export const applyAnchorOverride = (sizedDef, anchor) => {
79
+ if (!anchor) {
80
+ return sizedDef
81
+ }
82
+ if (!isValidAnchor(anchor)) {
83
+ const reportKey = JSON.stringify(anchor)
84
+ if (!reportedAnchors.has(reportKey)) {
85
+ reportedAnchors.add(reportKey)
86
+ logger.warn(`Invalid symbolAnchor ${reportKey} — expected [x, y] numbers. Using the symbol's own anchor.`)
87
+ }
88
+ return sizedDef
89
+ }
90
+ if (!sizedDef.bodyBox) {
91
+ return { ...sizedDef, anchor }
92
+ }
93
+ const [bodyX, bodyY, bodyWidth, bodyHeight] = sizedDef.bodyBox
94
+ const [,, width, height] = sizedDef.viewBox.split(' ').map(Number)
95
+ return { ...sizedDef, anchor: [(bodyX + anchor[0] * bodyWidth) / width, (bodyY + anchor[1] * bodyHeight) / height] }
96
+ }
97
+
98
+ /**
99
+ * Sizes an SVG-template symbol (a custom symbol, or symbolSvgContent) by scaling it as a whole —
100
+ * its own rings, if it draws any, scale with it. A symbol with no anchor of its own is centred.
101
+ *
102
+ * @param {Object} symbolDef - { svg, anchor?, ... }
103
+ * @param {string} viewBox
104
+ * @param {number} scale
105
+ * @returns {Object}
106
+ */
107
+ export const scaleSvgSymbolDef = (symbolDef, viewBox, scale) => {
108
+ const anchored = { anchor: DEFAULT_SYMBOL_ANCHOR, ...symbolDef }
109
+ if (scale === 1) {
110
+ return { ...anchored, viewBox }
111
+ }
112
+ const scaledViewBox = viewBox.split(' ').map((value) => round3(Number(value) * scale)).join(' ')
113
+ return { ...anchored, viewBox: scaledViewBox, svg: `<g transform="scale(${scale})">${symbolDef.svg}</g>` }
114
+ }
115
+
116
+ // graphic path → transform that centres it and, if bigger than 16×16, shrinks it to fit
117
+ const graphicFitCache = new Map()
118
+
119
+ /**
120
+ * Transform for a graphic, placing it on the shape's graphic centre. The graphic's own bounds are centred there
121
+ * and it's shrunk (never enlarged) to fit the 16×16 graphic area — so an icon drawn in any
122
+ * coordinate space (e.g. a 24×24 Material icon) still sits centred inside the shape. A graphic
123
+ * that can't be measured falls back to the 16×16 space's own centre.
124
+ *
125
+ * @param {string} graphic - SVG path data
126
+ * @param {number} centreX - the shape's graphic centre
127
+ * @param {number} centreY
128
+ * @returns {string}
129
+ */
130
+ export const getGraphicTransform = (graphic, centreX, centreY) => {
131
+ if (!graphicFitCache.has(graphic)) {
132
+ let fit = { scale: 1, graphicCentreX: GRAPHIC_SIZE * HALF, graphicCentreY: GRAPHIC_SIZE * HALF }
133
+ if (graphic) {
134
+ try {
135
+ const [graphicX, graphicY, graphicWidth, graphicHeight] = getPathBounds(graphic)
136
+ fit = {
137
+ scale: Math.min(1, GRAPHIC_SIZE / Math.max(graphicWidth, graphicHeight, Number.EPSILON)),
138
+ graphicCentreX: graphicX + graphicWidth * HALF,
139
+ graphicCentreY: graphicY + graphicHeight * HALF
140
+ }
141
+ } catch (error) {
142
+ logger.warn(`Symbol graphic could not be measured, so it isn't centred — ${error.message}`)
143
+ }
144
+ }
145
+ graphicFitCache.set(graphic, fit)
146
+ }
147
+ const { scale, graphicCentreX, graphicCentreY } = graphicFitCache.get(graphic)
148
+ return `translate(${centreX}, ${centreY}) scale(${round3(GRAPHIC_SCALE * scale)}) translate(${round3(-graphicCentreX)}, ${round3(-graphicCentreY)})`
149
+ }
150
+
151
+ const hasColor = (color) => !!color && color !== 'none'
152
+
153
+ /**
154
+ * SVG markup for a sized built-in shape. Rings are only emitted when shown, so an unselected
155
+ * symbol is a single path plus its graphic.
156
+ *
157
+ * @param {Object} sizedDef - from composeSymbolDef
158
+ * @param {Object} values - resolved tokens (backgroundColor, foregroundColor, haloColor, selectedColor, activeColor, graphic)
159
+ * @returns {string}
160
+ */
161
+ export const renderComposed = (sizedDef, values) => {
162
+ const { path, scale, transform, graphicCentre: [centreX, centreY] } = sizedDef
163
+ const ring = (color, strokeWidth) =>
164
+ `<path d="${path}" fill="none" stroke="${color}" stroke-width="${round3(strokeWidth / scale)}" stroke-linejoin="round"/>`
165
+ const layers = [
166
+ hasColor(values.activeColor) ? ring(values.activeColor, ACTIVE_RING_STROKE_WIDTH) : '',
167
+ hasColor(values.selectedColor) ? ring(values.selectedColor, SELECTED_RING_STROKE_WIDTH) : '',
168
+ `<path d="${path}" fill="${values.backgroundColor}" stroke="${values.haloColor}" stroke-width="${round3(HALO_STROKE_WIDTH / scale)}" stroke-linejoin="round" paint-order="stroke fill"/>`,
169
+ `<g transform="${getGraphicTransform(values.graphic, centreX, centreY)}"><path d="${values.graphic}" fill="${values.foregroundColor}"/></g>`
170
+ ]
171
+ return `<g transform="${transform}">${layers.join('')}</g>`
172
+ }
173
+
174
+ /**
175
+ * Substitutes {{token}} placeholders in an SVG template.
176
+ *
177
+ * @param {string} svgString
178
+ * @param {Object} values
179
+ * @returns {string}
180
+ */
181
+ export const renderTemplate = (svgString, values) => Object.entries(values).reduce(
182
+ (svg, [token, value]) => svg.replaceAll(`{{${token}}}`, value),
183
+ svgString
184
+ )
@@ -0,0 +1,132 @@
1
+ import {
2
+ composeSymbolDef, applyAnchorOverride, scaleSvgSymbolDef, getGraphicTransform, renderComposed, renderTemplate
3
+ } from './symbolComposer.js'
4
+ import { circle, pin, graphics, SYMBOL_PADDING } from '../config/symbolConfig.js'
5
+ import { logger } from './logger.js'
6
+
7
+ jest.mock('./logger.js', () => ({ logger: { warn: jest.fn() } }))
8
+
9
+ const dims = (viewBox) => viewBox.split(' ').map(Number).slice(2)
10
+ const values = {
11
+ backgroundColor: '#ca3535',
12
+ foregroundColor: '#ffffff',
13
+ haloColor: '#ffffff',
14
+ selectedColor: 'none',
15
+ activeColor: 'none',
16
+ graphic: graphics.dot
17
+ }
18
+
19
+ describe('composeSymbolDef', () => {
20
+ it('pads the scaled body by SYMBOL_PADDING on every side, rounded up to a multiple of 4', () => {
21
+ const [width, height] = dims(composeSymbolDef(circle, 1.25).viewBox)
22
+ // 26 × 1.25 + 16 = 48.5, rounded up to 52
23
+ expect(26 * 1.25 + SYMBOL_PADDING * 2).toBe(48.5)
24
+ expect(width).toBe(52)
25
+ expect(height).toBe(width)
26
+ })
27
+
28
+ it('returns the same object for the same def and scale', () => {
29
+ expect(composeSymbolDef(pin, 0.75)).toBe(composeSymbolDef(pin, 0.75))
30
+ })
31
+
32
+ it('records the body\'s box within the viewBox', () => {
33
+ const { bodyBox, viewBox } = composeSymbolDef(circle, 1)
34
+ expect(bodyBox).toEqual([9, 9, 26, 26])
35
+ expect(dims(viewBox)).toEqual([44, 44])
36
+ })
37
+ })
38
+
39
+ describe('applyAnchorOverride', () => {
40
+ it('returns the def unchanged when there\'s no override', () => {
41
+ const sized = composeSymbolDef(circle, 1)
42
+ expect(applyAnchorOverride(sized, undefined)).toBe(sized)
43
+ })
44
+
45
+ it('treats a built-in shape\'s override as a fraction of the shape, at every size', () => {
46
+ [0.75, 1, 1.25].forEach((scale) => {
47
+ const sized = composeSymbolDef(circle, scale)
48
+ const { anchor, viewBox } = applyAnchorOverride(sized, [0.5, 1])
49
+ const [width, height] = dims(viewBox)
50
+ const [x, y, w, h] = sized.bodyBox
51
+ // lands on the bottom-centre of the circle itself, not of its padded viewBox
52
+ expect(anchor[0] * width).toBeCloseTo(x + w / 2)
53
+ expect(anchor[1] * height).toBeCloseTo(y + h)
54
+ })
55
+ })
56
+
57
+ it.each([[[0.5]], [['0.5', '1']], [[0.5, Number.NaN]], ['bottom']])('ignores an invalid override %j, warning once', (anchor) => {
58
+ const sized = composeSymbolDef(circle, 1)
59
+ logger.warn.mockClear()
60
+ expect(applyAnchorOverride(sized, anchor)).toBe(sized)
61
+ expect(applyAnchorOverride(sized, anchor)).toBe(sized)
62
+ expect(logger.warn).toHaveBeenCalledTimes(1)
63
+ expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Invalid symbolAnchor'))
64
+ })
65
+
66
+ it('uses an SVG-template symbol\'s override as given — a fraction of its own viewBox', () => {
67
+ const sized = scaleSvgSymbolDef({ svg: '<rect/>' }, '0 0 20 20', 1)
68
+ expect(applyAnchorOverride(sized, [0.1, 0.9]).anchor).toEqual([0.1, 0.9])
69
+ })
70
+ })
71
+
72
+ describe('scaleSvgSymbolDef', () => {
73
+ it('leaves the svg as it is at scale 1', () => {
74
+ expect(scaleSvgSymbolDef({ svg: '<rect/>' }, '0 0 20 10', 1)).toEqual({ svg: '<rect/>', viewBox: '0 0 20 10', anchor: [0.5, 0.5] })
75
+ })
76
+
77
+ it('keeps the symbol\'s own anchor', () => {
78
+ expect(scaleSvgSymbolDef({ svg: '<rect/>', anchor: [0.5, 1] }, '0 0 20 10', 2).anchor).toEqual([0.5, 1])
79
+ })
80
+
81
+ it('scales the viewBox, origin included, and wraps the svg in a scale transform', () => {
82
+ expect(scaleSvgSymbolDef({ svg: '<rect/>' }, '2 4 20 10', 1.5)).toEqual({ svg: '<g transform="scale(1.5)"><rect/></g>', viewBox: '3 6 30 15', anchor: [0.5, 0.5] })
83
+ })
84
+ })
85
+
86
+ describe('getGraphicTransform', () => {
87
+ it('leaves a graphic already centred in the 16×16 space where it is, at 0.8', () => {
88
+ Object.values(graphics).forEach((graphic) => {
89
+ expect(getGraphicTransform(graphic, 22, 20)).toBe('translate(22, 20) scale(0.8) translate(-8, -8)')
90
+ })
91
+ })
92
+
93
+ it('centres and shrinks a graphic drawn in a bigger space (e.g. a 24×24 icon)', () => {
94
+ // a square filling 0–24: centred on its middle (12, 12) and shrunk by 16/24
95
+ expect(getGraphicTransform('M0 0H24V24H0Z', 22, 22)).toBe('translate(22, 22) scale(0.533) translate(-12, -12)')
96
+ })
97
+
98
+ it('centres a small off-centre graphic without enlarging it', () => {
99
+ expect(getGraphicTransform('M0 0H4V4H0Z', 22, 22)).toBe('translate(22, 22) scale(0.8) translate(-2, -2)')
100
+ })
101
+
102
+ it('falls back to the 16×16 space\'s centre, with a warning, for a graphic it can\'t measure', () => {
103
+ expect(getGraphicTransform('not a path', 22, 22)).toBe('translate(22, 22) scale(0.8) translate(-8, -8)')
104
+ expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('could not be measured'))
105
+ })
106
+
107
+ it('falls back to the 16×16 space\'s centre for no graphic', () => {
108
+ expect(getGraphicTransform('', 22, 22)).toBe('translate(22, 22) scale(0.8) translate(-8, -8)')
109
+ })
110
+ })
111
+
112
+ describe('renderComposed', () => {
113
+ const countPaths = (svg) => svg.match(/<path /g).length
114
+
115
+ it('emits only the body and graphic when no ring is showing', () => {
116
+ expect(countPaths(renderComposed(composeSymbolDef(circle, 1), values))).toBe(2)
117
+ })
118
+
119
+ it('emits the selected ring, then the active ring, when their colours are set', () => {
120
+ const selected = renderComposed(composeSymbolDef(circle, 1), { ...values, selectedColor: '#0b0c0c' })
121
+ expect(countPaths(selected)).toBe(3)
122
+ const active = renderComposed(composeSymbolDef(circle, 1), { ...values, selectedColor: '#0b0c0c', activeColor: '#ffdd00' })
123
+ expect(countPaths(active)).toBe(4)
124
+ expect(active.indexOf('#ffdd00')).toBeLessThan(active.indexOf('#0b0c0c')) // active ring is drawn underneath
125
+ })
126
+ })
127
+
128
+ describe('renderTemplate', () => {
129
+ it('substitutes every occurrence of each token', () => {
130
+ expect(renderTemplate('<a fill="{{c}}"/><b fill="{{c}}"/>', { c: 'red' })).toBe('<a fill="red"/><b fill="red"/>')
131
+ })
132
+ })