@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.
- package/dist/esm/im-core.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/context.md +18 -12
- package/docs/api/marker-config.md +16 -8
- package/docs/api/symbol-config.md +24 -9
- package/docs/api/symbol-registry.md +49 -14
- package/docs/plugins/datasets.md +4 -10
- package/docs/plugins/draw.md +2 -1
- package/docs/plugins/map-key.md +6 -4
- package/package.json +1 -1
- package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
- package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
- package/plugins/beta/map-styles/dist/umd/index.js +1 -1
- package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
- package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
- package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
- package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +9 -9
- package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +19 -3
- package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.js +9 -3
- package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.test.js +18 -3
- package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.js +3 -3
- package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +19 -26
- package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +15 -67
- package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +32 -105
- package/plugins/datasets/src/adapters/openlayers/layerBuilders.js +11 -13
- package/plugins/datasets/src/adapters/openlayers/layerBuilders.test.js +11 -3
- package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.js +48 -19
- package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +99 -11
- package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +26 -26
- package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +48 -29
- package/plugins/datasets/src/initialise/defaults.js +4 -3
- package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
- package/plugins/datasets/src/registry/dataset.js +2 -1
- package/plugins/datasets/src/registry/dataset.test.js +13 -0
- package/plugins/datasets/src/registry/mapboxStyleDataset.js +1 -1
- package/plugins/datasets/src/registry/mapboxStyleDataset.test.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/esm/index.js +1 -1
- package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/DrawInit.jsx +7 -5
- package/plugins/draw/src/DrawInit.test.jsx +18 -2
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +11 -5
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -11
- package/plugins/draw/src/adapters/maplibre/mapboxDraw.js +12 -9
- package/plugins/draw/src/adapters/maplibre/mapboxDraw.test.js +18 -0
- package/plugins/draw/src/adapters/maplibre/pointSymbolImages.js +81 -56
- package/plugins/draw/src/adapters/maplibre/pointSymbolImages.test.js +149 -80
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +7 -6
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +22 -25
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +26 -8
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +58 -16
- package/plugins/draw/src/adapters/openlayers/core/styles.js +33 -20
- package/plugins/draw/src/adapters/openlayers/core/styles.test.js +47 -21
- package/plugins/draw/src/adapters/openlayers/olDraw.js +16 -12
- package/plugins/draw/src/adapters/openlayers/olDraw.test.js +27 -32
- package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.js +44 -49
- package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.test.js +100 -119
- package/plugins/draw/src/defaults.js +2 -6
- package/plugins/draw/src/utils/stripInternalProperties.test.js +2 -1
- package/plugins/draw/src/utils/symbolKeys.js +3 -4
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/index.js +1 -1
- package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
- package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -2
- package/plugins/map-key/dist/umd/index.js +1 -1
- package/plugins/map-key/src/components/Key/Key.jsx +8 -4
- package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +3 -1
- package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
- package/plugins/map-key/src/components/Key/KeySvg.jsx +15 -14
- package/plugins/map-key/src/components/Key/KeySvg.test.jsx +21 -14
- package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +1 -2
- package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +3 -1
- package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +26 -12
- package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +24 -74
- package/plugins/map-key/src/components/Key/MapKey.jsx +3 -1
- package/plugins/map-key/src/components/Key/svgProperties.js +8 -2
- package/plugins/map-key/src/reducers/mergeKeyGroupItems.test.js +15 -0
- package/plugins/map-key/src/registry/index.js +0 -2
- package/plugins/map-key/src/registry/index.test.js +1 -9
- package/plugins/menu/src/registry/index.js +0 -2
- package/plugins/menu/src/registry/index.test.js +1 -9
- package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
- package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
- package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
- package/providers/beta/openlayers/dist/umd/index.js +1 -1
- package/providers/beta/openlayers/src/openlayersProvider.js +102 -2
- package/providers/beta/openlayers/src/openlayersProvider.test.js +95 -0
- package/providers/beta/openlayers/src/utils/highlightFeatures.js +20 -25
- package/providers/beta/openlayers/src/utils/highlightFeatures.test.js +73 -38
- package/providers/beta/openlayers/src/utils/patternImages.js +61 -0
- package/providers/beta/openlayers/src/utils/patternImages.test.js +74 -0
- package/providers/beta/openlayers/src/utils/symbolImages.js +103 -95
- package/providers/beta/openlayers/src/utils/symbolImages.test.js +136 -85
- package/providers/mapProvider.js +53 -1
- package/providers/mapProvider.test.js +6 -0
- package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
- package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
- package/providers/maplibre/dist/umd/index.js +1 -1
- package/providers/maplibre/src/maplibreProvider.js +50 -17
- package/providers/maplibre/src/maplibreProvider.test.js +74 -20
- package/providers/maplibre/src/utils/highlightFeatures.js +19 -14
- package/providers/maplibre/src/utils/highlightFeatures.test.js +20 -14
- package/providers/maplibre/src/utils/patternImages.js +46 -29
- package/providers/maplibre/src/utils/patternImages.test.js +52 -4
- package/providers/maplibre/src/utils/symbolImages.js +80 -39
- package/providers/maplibre/src/utils/symbolImages.test.js +157 -54
- package/src/App/components/Markers/Markers.jsx +16 -25
- package/src/App/components/Markers/Markers.test.jsx +34 -48
- package/src/App/components/Markers/SymbolLabelMarker.jsx +1 -1
- package/src/App/components/Markers/SymbolMarker.jsx +1 -1
- package/src/App/store/ServiceProvider.jsx +6 -3
- package/src/App/store/ServiceProvider.test.jsx +24 -0
- package/src/config/appConfig.js +1 -5
- package/src/config/mapSizeScales.js +8 -0
- package/src/config/symbolConfig.js +101 -25
- package/src/services/patternRegistry.js +97 -76
- package/src/services/patternRegistry.test.js +19 -1
- package/src/services/symbolComposer.js +184 -0
- package/src/services/symbolComposer.test.js +132 -0
- package/src/services/symbolRegistry.js +326 -155
- package/src/services/symbolRegistry.test.js +334 -32
- package/src/types.js +9 -3
- package/src/utils/hashString.js +23 -0
- package/src/utils/hashString.test.js +29 -0
- package/src/utils/imageDataCache.js +43 -0
- package/src/utils/imageDataCache.test.js +38 -0
- package/src/utils/pathBounds.js +330 -0
- package/src/utils/pathBounds.test.js +145 -0
- package/src/utils/patternUtils.js +0 -8
- package/src/utils/patternUtils.test.js +0 -25
- package/src/utils/recentImageSets.js +35 -0
- package/src/utils/recentImageSets.test.js +46 -0
- package/src/utils/symbolUtils.js +26 -29
- package/src/utils/symbolUtils.test.js +27 -41
- 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
|
+
})
|