): L.DivIcon {
const size = num(p["textSize"], 13);
const color = str(p["textColor"], "#111");
const halo = str(p["textHalo"], "#fff");
const bg = str(p["textBackground"]);
const border = str(p["textBorder"]);
const [pv, ph] = boxPadding(p["textBoxSize"]);
const radius = boxRadius(p["textBoxRadius"]);
const maxWidth = num(p["maxWidth"], 0);
const rot = num(p["rotation"], 0);
// Let CSS do the wrapping (this is HTML — no canvas maths needed). The trick is
// `width:max-content` + `max-width`: the box sizes to its longest line but is
// capped at `maxWidth`px. `pre-line` HONOURS the content's `\n` line breaks AND still
// wraps at that width. (Without `max-content` the 0×0 marker would force a wrap at every word.)
const css = [
"display:inline-block",
"white-space:pre-line",
"width:max-content",
maxWidth > 0 ? `max-width:${maxWidth}px` : "",
"text-align:center",
"line-height:1.2",
`font:${size}px sans-serif`,
`color:${color}`,
`text-shadow:-1px -1px 0 ${halo},1px -1px 0 ${halo},-1px 1px 0 ${halo},1px 1px 0 ${halo}`,
bg ? `background:${rgba(bg, 1)}` : "",
border ? `border:${textBoxBorderWidth(p["textBorderWidth"])}px solid ${border}` : "",
bg || border ? `padding:${pv}px ${ph}px;border-radius:${radius}px` : "",
// Centre the label on its anchor (like MapLibre's centred text-anchor).
`transform:translate(-50%,-50%)${rot ? ` rotate(${rot}deg)` : ""}`,
"transform-origin:center",
].filter(Boolean).join(";");
const html = `${escapeHtml(str(p["text"]))}
`;
// iconSize [0,0] ⇒ the marker's top-left sits on the anchor; the inner div's
// translate(-50%,-50%) then centres it there. Non-interactive so it never eats
// pointer events meant for the shape/handles beneath it.
return L.divIcon({ html, className: "draw-adapter-text", iconSize: [0, 0] });
}
}
function escapeHtml(s: string): string {
return s.replace(/[&<>"]/g, (c) => (c === "&" ? "&" : c === "<" ? "<" : c === ">" ? ">" : """));
}
// ── Geometric hit-test helpers (pure, pixel-space) ────────────────────────────
/** Flatten Leaflet's `getLatLngs()` (flat for a simple polyline, nested for multi/rings/holes)
* into a uniform list of LatLng arrays — one per line or ring. */
function normalizeLatLngs(latlngs: unknown): L.LatLng[][] {
if (!Array.isArray(latlngs) || latlngs.length === 0) return [];
if (latlngs[0] instanceof L.LatLng) return [latlngs as L.LatLng[]];
const out: L.LatLng[][] = [];
for (const sub of latlngs) out.push(...normalizeLatLngs(sub));
return out;
}
/** Distance (px) from point `p` to segment `a→b`. */
function distToSeg(p: L.Point, a: L.Point, b: L.Point): number {
const dx = b.x - a.x, dy = b.y - a.y;
const len2 = dx * dx + dy * dy;
let t = len2 ? ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2 : 0;
t = t < 0 ? 0 : t > 1 ? 1 : t;
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
}
/** Min distance (px) from `p` to a set of lines/rings. `closed` adds the last→first edge (polygons). */
function minSegDist(p: L.Point, lines: L.Point[][], closed: boolean): number {
let min = Infinity;
for (const line of lines) {
const n = line.length;
if (n === 0) continue;
if (n === 1) { min = Math.min(min, Math.hypot(p.x - line[0]!.x, p.y - line[0]!.y)); continue; }
const segs = closed ? n : n - 1;
for (let i = 0; i < segs; i++) min = Math.min(min, distToSeg(p, line[i]!, line[(i + 1) % n]!));
}
return min;
}
/** Even-odd point-in-polygon over all rings combined (honours holes). */
function pointInRings(p: L.Point, rings: L.Point[][]): boolean {
let inside = false;
for (const ring of rings) {
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
const a = ring[i]!, b = ring[j]!;
if ((a.y > p.y) !== (b.y > p.y) && p.x < ((b.x - a.x) * (p.y - a.y)) / (b.y - a.y) + a.x) inside = !inside;
}
}
return inside;
}