ts-maps 0.3.2 → 0.3.4

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 (54) hide show
  1. package/dist/core-map/control/FullscreenControl.d.ts +39 -0
  2. package/dist/core-map/control/GeocoderControl.d.ts +61 -0
  3. package/dist/core-map/control/LocateControl.d.ts +5 -2
  4. package/dist/core-map/control/NavigationControl.d.ts +39 -0
  5. package/dist/core-map/control/index.d.ts +6 -0
  6. package/dist/core-map/game/TerritoryLog.d.ts +66 -0
  7. package/dist/core-map/game/TerritoryStore.d.ts +59 -0
  8. package/dist/core-map/game/index.d.ts +11 -0
  9. package/dist/core-map/game/loop.d.ts +61 -0
  10. package/dist/core-map/geo/area.d.ts +52 -0
  11. package/dist/core-map/geo/index.d.ts +30 -0
  12. package/dist/core-map/geo/index.js +826 -1
  13. package/dist/core-map/geo/polygonClip.d.ts +75 -0
  14. package/dist/core-map/geo/validate.d.ts +54 -0
  15. package/dist/core-map/index.d.ts +7 -2
  16. package/dist/core-map/layer/GeoJSONTileSource.d.ts +50 -0
  17. package/dist/core-map/layer/RunTrailLayer.d.ts +35 -0
  18. package/dist/core-map/layer/TerritoryLayer.d.ts +62 -0
  19. package/dist/core-map/layer/index.d.ts +6 -0
  20. package/dist/core-map/layer/tile/RasterDEMLayer.d.ts +4 -0
  21. package/dist/core-map/layer/tile/VectorTileMapLayer.d.ts +59 -11
  22. package/dist/core-map/layer/tile/urlTemplate.d.ts +1 -2
  23. package/dist/core-map/map/Map.d.ts +32 -1
  24. package/dist/core-map/mvt/DecodedTile.d.ts +20 -0
  25. package/dist/core-map/mvt/FlatTile.d.ts +25 -0
  26. package/dist/core-map/mvt/VectorTileFeature.d.ts +8 -0
  27. package/dist/core-map/style-spec/expressions/formatted.d.ts +36 -0
  28. package/dist/core-map/style-spec/expressions/operators/binding.d.ts +1 -0
  29. package/dist/core-map/style-spec/expressions/operators/geometry.d.ts +19 -0
  30. package/dist/core-map/style-spec/expressions/types.d.ts +3 -0
  31. package/dist/core-map/style-spec/index.js +459 -42
  32. package/dist/core-map/style-spec/schema.d.ts +43 -3
  33. package/dist/core-map/style-spec/types.d.ts +47 -7
  34. package/dist/core-map/styles/basemap.d.ts +41 -0
  35. package/dist/core-map/styles/index.d.ts +5 -0
  36. package/dist/core-map/styles/palette.d.ts +31 -0
  37. package/dist/core-map/symbols/CollisionIndex.d.ts +22 -5
  38. package/dist/core-map/symbols/GlyphAtlas.d.ts +6 -1
  39. package/dist/core-map/symbols/GlyphProvider.d.ts +21 -0
  40. package/dist/core-map/symbols/GlyphRenderer.d.ts +48 -0
  41. package/dist/core-map/symbols/IconAtlas.d.ts +10 -1
  42. package/dist/core-map/symbols/SymbolOverlay.d.ts +47 -0
  43. package/dist/core-map/symbols/index.d.ts +4 -0
  44. package/dist/core-map/symbols/index.js +1071 -67
  45. package/dist/core-map/symbols/loadGlyphs.d.ts +53 -0
  46. package/dist/core-map/symbols/loadSprite.d.ts +57 -0
  47. package/dist/core-map/symbols/placement.d.ts +67 -0
  48. package/dist/core-map/symbols/sdf.d.ts +37 -0
  49. package/dist/core-map/symbols/sections.d.ts +32 -0
  50. package/dist/core-map/workers/WorkerPool.d.ts +40 -0
  51. package/dist/core-map/workers/decodeMvtFlat.d.ts +52 -0
  52. package/dist/index.js +11548 -5996
  53. package/package.json +1 -1
  54. package/src/core-map/ts-maps.css +392 -99
@@ -0,0 +1,53 @@
1
+ /** Fill `{fontstack}` and `{range}` in a glyphs template. */
2
+ export declare function glyphUrl(template: string, fontstack: string, rangeStart: number): string;
3
+ /** The 256-codepoint block a character falls in. */
4
+ export declare function rangeStartFor(codePoint: number): number;
5
+ /** Decode one `glyphs.proto` message. */
6
+ export declare function decodeGlyphPbf(bytes: Uint8Array): GlyphRange;
7
+ /** The border Mapbox bakes around each glyph's distance field. */
8
+ export declare const GLYPH_BORDER: 3;
9
+ /**
10
+ * Load a style's glyph ranges.
11
+ *
12
+ * A style points at glyphs with a template — `"glyphs":
13
+ * "https://example.com/fonts/{fontstack}/{range}.pbf"` — serving signed
14
+ * distance fields in 256-codepoint blocks. ts-maps rasterises text from system
15
+ * fonts, which is sharper on a 2D canvas and needs no network at all, so this
16
+ * is not how labels are drawn. What it is for is the case that cannot be
17
+ * served locally: a style whose typeface the viewer does not have installed.
18
+ *
19
+ * Ranges are fetched on demand and cached, because a stack is 65,536 code
20
+ * points and a map shows a handful of blocks.
21
+ *
22
+ * Wire format (Mapbox `glyphs.proto`):
23
+ * 1 fontstack { 1 name, 2 range, 3 glyph { 1 id, 2 bitmap, 3 width,
24
+ * 4 height, 5 left, 6 top, 7 advance } }
25
+ */
26
+ export declare interface Glyph {
27
+ id: number
28
+ bitmap?: Uint8Array
29
+ width: number
30
+ height: number
31
+ left: number
32
+ top: number
33
+ advance: number
34
+ }
35
+ export declare interface LoadGlyphsOptions {
36
+ signal?: AbortSignal
37
+ fetch?: typeof globalThis.fetch
38
+ }
39
+ export type GlyphRange = Map<number, Glyph>;
40
+ /**
41
+ * Fetches glyph ranges and remembers them.
42
+ *
43
+ * One instance per map. A range in flight is shared rather than fetched twice,
44
+ * which matters when several tiles decode at once and all want the same block.
45
+ */
46
+ export declare class GlyphSource {
47
+ constructor(template: string, options?: LoadGlyphsOptions);
48
+ get(fontstack: string, codePoint: number): Glyph | undefined;
49
+ has(fontstack: string, codePoint: number): boolean;
50
+ load(fontstack: string, codePoint: number, signal?: AbortSignal): Promise<GlyphRange>;
51
+ loadForText(fontstack: string, text: string, signal?: AbortSignal): Promise<void>;
52
+ get size(): number;
53
+ }
@@ -0,0 +1,57 @@
1
+ import type { IconAtlas } from './IconAtlas';
2
+ /** `base` → `base@2x.png`, keeping any query string where it belongs. */
3
+ export declare function spriteUrl(base: string, extension: 'json' | 'png', pixelRatio: number): string;
4
+ /**
5
+ * Fetch the index and sheet for a sprite base URL.
6
+ *
7
+ * Falls back from `@2x` to 1x when the high-density sheet is missing, so a
8
+ * style that only publishes one density still works on a retina screen.
9
+ */
10
+ export declare function loadSprite(base: string, options?: LoadSpriteOptions): Promise<LoadedSprite>;
11
+ /**
12
+ * Push a loaded sprite's icons into an atlas.
13
+ *
14
+ * Entries carry their own `pixelRatio` in Mapbox's format; where one is
15
+ * missing the sheet's density stands in, so a `@2x` sheet does not render at
16
+ * double size.
17
+ *
18
+ * `prefix` namespaces the ids, for the multi-sheet form of `sprite`.
19
+ */
20
+ export declare function addSpriteToAtlas(atlas: IconAtlas, sprite: LoadedSprite, prefix?: string): number;
21
+ /**
22
+ * Load a style's sprite sheet.
23
+ *
24
+ * A style document points at a sprite with a base URL and no extension —
25
+ * `"sprite": "https://example.com/sprites/basic"` — from which two files are
26
+ * derived: a JSON index of named icons and a PNG holding their pixels. Until
27
+ * now `IconAtlas` required the caller to supply both by hand, which meant
28
+ * `icon-image` did nothing for any real-world style.
29
+ *
30
+ * Retina sheets follow the `@2x` convention. The higher-density sheet is
31
+ * preferred where the display can use it and quietly skipped where it cannot
32
+ * be fetched, because a style is not obliged to publish one.
33
+ */
34
+ /** The shape of an entry in a sprite JSON index. */
35
+ export declare interface SpriteIndexEntry {
36
+ x: number
37
+ y: number
38
+ width: number
39
+ height: number
40
+ pixelRatio?: number
41
+ sdf?: boolean
42
+ content?: [number, number, number, number]
43
+ stretchX?: Array<[number, number]>
44
+ stretchY?: Array<[number, number]>
45
+ }
46
+ export declare interface LoadSpriteOptions {
47
+ pixelRatio?: number
48
+ signal?: AbortSignal
49
+ fetch?: typeof globalThis.fetch
50
+ loadImage?: (url: string) => Promise<CanvasImageSource & { width: number, height: number }>
51
+ }
52
+ export declare interface LoadedSprite {
53
+ index: SpriteIndex
54
+ image: CanvasImageSource & { width: number, height: number }
55
+ pixelRatio: number
56
+ }
57
+ export type SpriteIndex = Record<string, SpriteIndexEntry>;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Where the text box's top-left corner goes, relative to the anchor point.
3
+ *
4
+ * `text-anchor` names the part of the label that touches the anchor, so
5
+ * `'left'` puts the label's left edge on the point and the text runs to the
6
+ * right — the opposite of what the name suggests at first glance.
7
+ */
8
+ export declare function anchorOffset(anchor: TextAnchor, width: number, height: number): Vec;
9
+ /** Normalise `text-offset` (ems, [x, y]) into pixels at a given text size. */
10
+ export declare function offsetPixels(offset: unknown, textSize: number): Vec;
11
+ /**
12
+ * The axis-aligned box covering a rectangle rotated about a point.
13
+ *
14
+ * Collision uses axis-aligned boxes, so a rotated label needs the bounds of
15
+ * its rotated corners rather than its unrotated ones — otherwise a label at
16
+ * 45° reserves a box far smaller than the ink it actually lays down.
17
+ */
18
+ export declare function rotatedBounds(x: number, y: number, width: number, height: number, angle: number, originX?: number, originY?: number): { minX: number, minY: number, maxX: number, maxY: number };
19
+ /** Total length of a polyline. */
20
+ export declare function lineLength(line: Vec[]): number;
21
+ /** The point and tangent angle at a distance along a polyline, or null past its end. */
22
+ export declare function pointAtDistance(line: Vec[], distance: number): (Vec & { angle: number }) | null;
23
+ /**
24
+ * Lay glyphs along a line, one per advance, each rotated to the local tangent.
25
+ *
26
+ * Returns null when the label does not fit, or bends past `maxAngle` — both
27
+ * are "don't draw this here" answers, and the caller is expected to try the
28
+ * next candidate position rather than force it.
29
+ */
30
+ export declare function placeGlyphsAlongLine(line: Vec[], options: LinePlacementOptions): PlacedGlyph[] | null;
31
+ /**
32
+ * Distances along a line at which to attempt a repeated label.
33
+ *
34
+ * A long road gets its name several times rather than once in the middle,
35
+ * which is what makes a street findable when only part of it is on screen.
36
+ */
37
+ export declare function repeatDistances(length: number, labelWidth: number, spacing: number): number[];
38
+ export declare interface Vec {
39
+ x: number
40
+ y: number
41
+ }
42
+ export declare interface PlacedGlyph {
43
+ x: number
44
+ y: number
45
+ angle: number
46
+ index: number
47
+ }
48
+ export declare interface LinePlacementOptions {
49
+ advances: number[]
50
+ start: number
51
+ maxAngle?: number
52
+ keepUpright?: boolean
53
+ }
54
+ // Symbol placement geometry.
55
+ //
56
+ // Pure functions, deliberately: placement is the part of label rendering most
57
+ // worth testing, and it is untestable if it only exists inside a canvas draw
58
+ // call. Everything here works in whatever pixel space the caller hands it.
59
+ export type TextAnchor = | 'center'
60
+ | 'left'
61
+ | 'right'
62
+ | 'top'
63
+ | 'bottom'
64
+ | 'top-left'
65
+ | 'top-right'
66
+ | 'bottom-left'
67
+ | 'bottom-right';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Colour one distance field.
3
+ *
4
+ * `field` is RGBA, of which only alpha is read. The result is RGBA the same
5
+ * size, ready to hand to `putImageData`.
6
+ */
7
+ export declare function renderSdfPixels(field: Uint8ClampedArray, width: number, height: number, options: SdfRenderOptions): Uint8ClampedArray;
8
+ /** Hermite interpolation between two edges, as in the GLSL builtin. */
9
+ export declare function smoothstep(edge0: number, edge1: number, x: number): number;
10
+ /**
11
+ * A CSS colour as RGB.
12
+ *
13
+ * Hex and `rgb()`/`rgba()` cover what styles actually write and are parsed
14
+ * directly. Anything else — a named colour, `hsl()` — goes through a canvas,
15
+ * which knows every notation CSS does and saves shipping a colour parser to
16
+ * handle the long tail.
17
+ */
18
+ export declare function parseColor(color: string): [number, number, number];
19
+ // Resolving a signed-distance icon to pixels.
20
+ //
21
+ // An SDF sprite stores distance from the shape's edge in its alpha channel
22
+ // rather than the icon's own colours, encoded so that 0.5 is the edge itself.
23
+ // Two things follow from that, and they are the reason the format is worth
24
+ // supporting: one grey shape can be drawn in any colour a style asks for, and
25
+ // the edge is recovered by thresholding rather than resampled from pixels, so
26
+ // it stays sharp however far the icon is scaled up.
27
+ //
28
+ // The work is kept here, apart from `IconAtlas`, because it is arithmetic over
29
+ // a buffer and nothing more. The atlas reads pixels out of a canvas and writes
30
+ // them back; that part needs a browser. This part can be tested against
31
+ // hand-built fields, which is where the behaviour worth checking actually
32
+ // lives.
33
+ export declare interface SdfRenderOptions {
34
+ color: string
35
+ haloColor?: string
36
+ haloWidth?: number
37
+ }
@@ -0,0 +1,32 @@
1
+ import type { FormattedSection } from '../style-spec/expressions/formatted';
2
+ import type { GlyphAtlas } from './GlyphAtlas';
3
+ /**
4
+ * Measure a formatted label as one box.
5
+ *
6
+ * Height is the tallest section's, so a small section next to a large one does
7
+ * not shrink the label's footprint and let something else overlap it.
8
+ */
9
+ export declare function measureSections(atlas: GlyphAtlas, sections: FormattedSection[], base: SectionBaseStyle): SectionLayout;
10
+ /**
11
+ * Draw a formatted label from `x` along a shared baseline at `y`.
12
+ *
13
+ * Sections sit on the baseline rather than being centred on it, which is what
14
+ * keeps a size change reading as one line of text instead of two runs that
15
+ * happen to be adjacent.
16
+ */
17
+ export declare function drawSections(ctx: CanvasRenderingContext2D, atlas: GlyphAtlas, sections: FormattedSection[], x: number, y: number, base: SectionBaseStyle): void;
18
+ export declare interface SectionBaseStyle {
19
+ size: number
20
+ color: string
21
+ italic?: boolean
22
+ bold?: boolean
23
+ family?: string
24
+ haloColor?: string
25
+ haloWidth?: number
26
+ }
27
+ export declare interface SectionLayout {
28
+ width: number
29
+ height: number
30
+ ascent: number
31
+ descent: number
32
+ }
@@ -0,0 +1,40 @@
1
+ export declare function registerWorkerHandler<T, R>(type: string, fn: WorkerHandler<T, R>, options?: RegisterOptions): void;
2
+ export declare function getWorkerHandler(type: string): WorkerHandler | undefined;
3
+ export declare interface WorkerTask<T = unknown, _R = unknown> {
4
+ type: string
5
+ payload: T
6
+ }
7
+ export declare interface WorkerPoolOptions {
8
+ size?: number
9
+ scriptUrl?: string
10
+ }
11
+ declare interface PendingTask {
12
+ resolve: (value: unknown) => void
13
+ reject: (reason: unknown) => void
14
+ }
15
+ export declare interface RegisterOptions {
16
+ deps?: Array<(...args: any[]) => any>
17
+ transfer?: (result: any) => ArrayBuffer[]
18
+ }
19
+ // eslint-disable-next-line no-unused-vars
20
+ export type WorkerHandler<T = unknown, R = unknown> = (task: WorkerTask<T, R>) => R | Promise<R>;
21
+ // ---------------------------------------------------------------------------
22
+ // Pool
23
+ // ---------------------------------------------------------------------------
24
+ export declare class WorkerPool {
25
+ _size: number;
26
+ _scriptUrl?: string;
27
+ _workers: Worker[];
28
+ _blobUrl?: string;
29
+ _nextId: number;
30
+ _pending: Map<number, PendingTask>;
31
+ _rr: number;
32
+ _usable: boolean;
33
+ constructor(opts?: WorkerPoolOptions);
34
+ size(): number;
35
+ _canUseWorkers(): boolean;
36
+ _spawn(): void;
37
+ _onMessage(e: MessageEvent): void;
38
+ run<T, R>(task: WorkerTask<T, R>): Promise<R>;
39
+ shutdown(): Promise<void>;
40
+ }
@@ -0,0 +1,52 @@
1
+ /** Every ArrayBuffer in a decoded tile, for a `postMessage` transfer list. */
2
+ export declare function flatTileBuffers(tile: FlatTile): ArrayBuffer[];
3
+ /**
4
+ * Decode a `.pbf` tile into flat arrays.
5
+ *
6
+ * Self-contained by construction — see the note at the top of the file.
7
+ */
8
+ export declare function decodeMvtFlat(bytes: Uint8Array): FlatTile;
9
+ // decodeMvtFlat — an MVT decoder that answers in typed arrays.
10
+ //
11
+ // This is deliberately a second decoder rather than a reuse of `Pbf` +
12
+ // `VectorTile`, for two reasons.
13
+ //
14
+ // **It has to survive being turned into a string.** WorkerPool ships its
15
+ // handlers by stringifying them into an inline worker, so anything a handler
16
+ // reaches for through an import is simply not there when it runs. This
17
+ // function closes over nothing at all — no imports, no module constants, no
18
+ // sibling helpers — which is a property the file is written to preserve, not
19
+ // an accident. Adding an import here breaks decoding in the worker while
20
+ // leaving every main-thread test passing, so don't.
21
+ //
22
+ // **It decodes to a different shape.** The output is flat typed arrays whose
23
+ // buffers are transferred to the main thread rather than copied. That is the
24
+ // entire point of the exercise: a tile decoded into `{x, y}` objects is
25
+ // hundreds of thousands of allocations that structured-clone must then walk
26
+ // and rebuild, which costs more than the decode it was meant to save. Moving
27
+ // work to a worker only helps when what crosses the boundary is cheap.
28
+ //
29
+ // Geometry stays lazy on the far side. The layout below is the tile's own —
30
+ // shared key and value tables, features as indices into them — so the main
31
+ // thread materialises a feature's points and properties when something asks
32
+ // for them, exactly as the Pbf path does.
33
+ //
34
+ // `test/mvt-flat.test.ts` decodes the same fixtures both ways and asserts the
35
+ // results match, which is what keeps the duplication honest.
36
+ export declare interface FlatLayer {
37
+ name: string
38
+ version: number
39
+ extent: number
40
+ keys: string[]
41
+ values: Array<string | number | boolean | null>
42
+ ids: Float64Array
43
+ types: Uint8Array
44
+ tagStart: Uint32Array
45
+ tags: Uint32Array
46
+ ringStart: Uint32Array
47
+ ringOffset: Uint32Array
48
+ coords: Int32Array
49
+ }
50
+ export declare interface FlatTile {
51
+ layers: FlatLayer[]
52
+ }