@andamento/svg 0.0.0-stage → 0.2.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.
@@ -0,0 +1,286 @@
1
+ import { Polygon, Layout, Cell, CellFill, ContentCells, ContentPlacement, Focus } from '@andamento/core';
2
+
3
+ /**
4
+ * Makes tiles look raised: a bevel round each tile's edges, lit from one side, and
5
+ * optionally a shadow each tile casts into the grout (the gaps) on the other.
6
+ */
7
+ interface ReliefOptions {
8
+ /** Bevel width in layout units. Defaults to 8% of a typical tile's size; 0 for none. */
9
+ bevel?: number;
10
+ /** Direction the light comes from: degrees clockwise from the top (default 315, top left). */
11
+ light?: number;
12
+ /** Opacity of the brightest highlight and the darkest bevel shadow, 0–1. Defaults to 0.4. */
13
+ strength?: number;
14
+ /** How far tiles' shadows fall into the grout, in layout units (default 0: no shadow). */
15
+ shadow?: number;
16
+ /** Blur of the cast shadow (standard deviation, layout units). Defaults to `shadow`. */
17
+ shadowBlur?: number;
18
+ /** Opacity of the cast shadow. Defaults to 0.35. */
19
+ shadowOpacity?: number;
20
+ }
21
+ /** One layer of a cell's bevel: all its faces of about the same shade, as one path. */
22
+ interface ReliefPath {
23
+ d: string;
24
+ /** "#fff" for highlights, "#000" for shadows. */
25
+ fill: string;
26
+ opacity: number;
27
+ }
28
+ /** The default bevel width for a layout: 8% of the square root of the mean tile area. */
29
+ declare function defaultBevel(layout: Layout): number;
30
+ /** One layer of a cell's bevel as polygons: all its faces of about the same shade. */
31
+ interface ReliefLayer {
32
+ polygons: Polygon[];
33
+ /** "#fff" for highlights, "#000" for shadows. */
34
+ fill: string;
35
+ opacity: number;
36
+ }
37
+ /**
38
+ * A cell's bevel grouped into a few shade levels, in layout coordinates, to draw over the
39
+ * cell's content. Pass the bevel width from `defaultBevel` if not set.
40
+ */
41
+ declare function reliefLayers(layout: Layout, cell: Cell, relief: ReliefOptions, bevel: number): ReliefLayer[];
42
+ /** `reliefLayers` as SVG paths, one per layer. */
43
+ declare function reliefPaths(layout: Layout, cell: Cell, relief: ReliefOptions, bevel: number, precision?: number): ReliefPath[];
44
+ /** Offset of the cast shadow (away from the light), or undefined for none. */
45
+ declare function shadowOffset(relief: ReliefOptions): {
46
+ dx: number;
47
+ dy: number;
48
+ blur: number;
49
+ opacity: number;
50
+ } | undefined;
51
+
52
+ declare const defaultPalette: string[];
53
+ /** A color, a palette (neighboring cells get different entries), or a color per cell. */
54
+ type FillSource = string | readonly string[] | CellFill;
55
+ interface CellStyle {
56
+ fill: string;
57
+ image?: string;
58
+ /** Which item of `images` the cell shows (-1 if none or if `images` is a function). */
59
+ slot: number;
60
+ }
61
+ /**
62
+ * Fill color per cell (by `cell.index`): a color, a palette (neighboring cells get
63
+ * different entries), or a function.
64
+ */
65
+ declare function resolveFills(layout: Layout, fill: FillSource): string[];
66
+ type StyleOptions = Pick<RenderOptions, "images" | "repeatImages" | "fill" | "contentCells" | "placement" | "imageAspects" | "choose">;
67
+ /** Resolve the fill color and image for every cell (shared by the SVG and React renderers). */
68
+ declare function resolveCellStyles(layout: Layout, opts?: StyleOptions): CellStyle[];
69
+ /** With `imageAlts`: a cell showing a described image is an image; the rest is decoration. */
70
+ declare function cellA11y(style: CellStyle, alts: readonly string[] | undefined): {
71
+ role?: "img";
72
+ "aria-label"?: string;
73
+ "aria-hidden"?: "true";
74
+ };
75
+
76
+ /**
77
+ * The parts of a 2D canvas context the renderer uses. A browser `CanvasRenderingContext2D`
78
+ * or `OffscreenCanvasRenderingContext2D` fits, and so do Node canvases such as
79
+ * `@napi-rs/canvas` or `skia-canvas`.
80
+ */
81
+ interface CanvasContextLike {
82
+ save(): void;
83
+ restore(): void;
84
+ setTransform(a: number, b: number, c: number, d: number, e: number, f: number): void;
85
+ beginPath(): void;
86
+ moveTo(x: number, y: number): void;
87
+ lineTo(x: number, y: number): void;
88
+ arc(x: number, y: number, radius: number, start: number, end: number, counterclockwise?: boolean): void;
89
+ closePath(): void;
90
+ fill(fillRule?: "nonzero" | "evenodd"): void;
91
+ clip(fillRule?: "nonzero" | "evenodd"): void;
92
+ stroke(): void;
93
+ clearRect(x: number, y: number, w: number, h: number): void;
94
+ drawImage(image: never, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
95
+ fillStyle: unknown;
96
+ strokeStyle: unknown;
97
+ lineWidth: number;
98
+ lineJoin: unknown;
99
+ globalAlpha: number;
100
+ shadowColor: string;
101
+ shadowBlur: number;
102
+ shadowOffsetX: number;
103
+ shadowOffsetY: number;
104
+ }
105
+ /** A loaded image: an HTMLImageElement, ImageBitmap, canvas, or a Node canvas image. */
106
+ type CanvasImage = {
107
+ width: number;
108
+ height: number;
109
+ };
110
+ interface CanvasRenderOptions extends Pick<RenderOptions, "images" | "repeatImages" | "contentCells" | "placement" | "imageAspects" | "choose" | "imageFit" | "containWithin" | "imageFocus" | "fill" | "stroke" | "strokeWidth" | "background"> {
111
+ /** Raised tiles, as for SVG. */
112
+ relief?: boolean | ReliefOptions;
113
+ /** One image across all the cells (a URL), as for SVG. */
114
+ backdrop?: string;
115
+ /**
116
+ * Loaded images by URL (see `loadImages`). Images not in it are skipped and their cells
117
+ * show `fill`. `renderToCanvas` and `renderToPng` load them for you.
118
+ */
119
+ loaded?: ReadonlyMap<string, CanvasImage>;
120
+ }
121
+ /**
122
+ * Draw a layout on a canvas context, scaled to fit `width` by `height` device pixels (the
123
+ * canvas size by default) and centerd, like SVG's default `preserveAspectRatio`. It draws
124
+ * what the SVG renderer does: background, cast shadows, fills, images, bevels and strokes.
125
+ * Synchronous: pass loaded images in `loaded`.
126
+ */
127
+ declare function drawLayout(ctx: CanvasContextLike, layout: Layout, options?: CanvasRenderOptions & {
128
+ width?: number;
129
+ height?: number;
130
+ }): void;
131
+ /** Add a polygon (with rounded corners) to the current path, as a closed subpath. */
132
+ declare function tracePolygon(ctx: Pick<CanvasContextLike, "moveTo" | "lineTo" | "arc" | "closePath">, poly: Polygon, radius: number): void;
133
+ /** The image URLs a layout would draw with these options (cell images and the backdrop). */
134
+ declare function imagesUsed(layout: Layout, options?: CanvasRenderOptions): string[];
135
+ /**
136
+ * Load images for drawing (in the browser, with `crossOrigin = "anonymous"` so the canvas
137
+ * can still be exported; the server must allow it). Images that fail are left out. Pass
138
+ * your own `load` elsewhere, e.g. `loadImage` from a Node canvas package.
139
+ */
140
+ declare function loadImages(urls: readonly string[], load?: (url: string) => Promise<CanvasImage>): Promise<Map<string, CanvasImage>>;
141
+ /** A canvas that can be drawn on and exported (browser or offscreen canvas, or your own). */
142
+ interface CanvasLike {
143
+ width: number;
144
+ height: number;
145
+ getContext(type: "2d"): unknown;
146
+ }
147
+ interface RasterOptions extends CanvasRenderOptions {
148
+ /** Output width in CSS pixels; the height follows the layout's shape. Defaults to the layout's width. */
149
+ width?: number;
150
+ /** Device pixels per CSS pixel, for sharp output on high-density screens. Defaults to 1. */
151
+ pixelRatio?: number;
152
+ /** Load an image (see `loadImages`). */
153
+ loadImage?: (url: string) => Promise<CanvasImage>;
154
+ }
155
+ /**
156
+ * Draw a layout onto `canvas`, sizing it to the layout (`width` × `pixelRatio`) and loading
157
+ * its images first. Resolves to the canvas.
158
+ */
159
+ declare function renderToCanvas<C extends CanvasLike>(layout: Layout, canvas: C, options?: RasterOptions): Promise<C>;
160
+ /** Pixel size of the raster for a layout: `width` (default the layout's) times `pixelRatio`. */
161
+ declare function rasterSize(layout: Layout, options?: Pick<RasterOptions, "width" | "pixelRatio">): {
162
+ w: number;
163
+ h: number;
164
+ };
165
+ /**
166
+ * Render a layout to a PNG (or JPEG / WebP with `type`) image, in the browser or a worker.
167
+ * Images must load (and, from other sites, allow cross-origin use) or their cells show
168
+ * their fill. Elsewhere, use `drawLayout` with your own canvas.
169
+ */
170
+ declare function renderToPng(layout: Layout, options?: RasterOptions & {
171
+ type?: string;
172
+ quality?: number;
173
+ createCanvas?: (w: number, h: number) => CanvasLike;
174
+ }): Promise<Blob>;
175
+
176
+ /**
177
+ * Renders a layout to an SVG string or element, to a canvas, or to a PNG.
178
+ *
179
+ * @module @andamento/svg
180
+ */
181
+
182
+ /** Soft neutrals for tiles without content: visible as tiles, but quiet next to photos. */
183
+ declare const neutralPalette: string[];
184
+ type ImageSource = readonly string[] | ((cell: Cell) => string | undefined);
185
+ interface RenderOptions {
186
+ /** Images: an array assigned in reading order, or a function per cell. */
187
+ images?: ImageSource;
188
+ /** Reuse the images array from the start when there are more cells than images. */
189
+ repeatImages?: boolean;
190
+ /** Which cells may hold an image from the `images` array (default "all"). */
191
+ contentCells?: ContentCells;
192
+ /** Where the images go when there are fewer than cells (default "reading"). */
193
+ placement?: ContentPlacement;
194
+ /**
195
+ * Width / height of each image in `images`. When given, each image goes in a cell of
196
+ * similar shape, to crop as little as possible (see `matchSlots`). Ignored with `repeatImages`.
197
+ */
198
+ imageAspects?: readonly number[];
199
+ /**
200
+ * How each image fills its cell: "cover" (default) fills the cell's bounding box and is
201
+ * cropped to the cell's shape; "contain" shows the whole image inside the cell's safe area
202
+ * (see `safeArea`), uncropped. Give `imageAspects` so the safe area has each image's shape.
203
+ */
204
+ imageFit?: "cover" | "contain";
205
+ /**
206
+ * With `imageFit: "contain"`, for cells an `edges: "window"` layout cuts: fit the image in
207
+ * the whole tile ("tile", the default; partly hidden like the tile) or in the part that
208
+ * shows ("visible"). See `SafeAreaOptions.within`.
209
+ */
210
+ containWithin?: "tile" | "visible";
211
+ /**
212
+ * Focal point of each image in `images` (by position; fractions of its width and height,
213
+ * see `estimateFocus`): what to keep in view when "cover" crops it. It goes as near the
214
+ * middle of what shows of its cell as covering the cell allows (see `coverRect`). Needs
215
+ * each image's shape, from `imageAspects` or the focus's `aspect` (the canvas renderers
216
+ * measure the images themselves).
217
+ */
218
+ imageFocus?: readonly (Focus | undefined)[];
219
+ /** With `imageAspects`: "shape" also picks which cells get images by their shape. */
220
+ choose?: "placement" | "shape";
221
+ /**
222
+ * Cell color when there is no image: a color, a palette (neighboring cells get
223
+ * different entries), or a function. Defaults to `defaultPalette`.
224
+ */
225
+ fill?: FillSource;
226
+ /**
227
+ * Raised tiles: a lit bevel round each tile, and optionally a shadow cast into the grout.
228
+ * `true` for the defaults. See `ReliefOptions`.
229
+ */
230
+ relief?: boolean | ReliefOptions;
231
+ /** Outline drawn on top of each cell. */
232
+ stroke?: string;
233
+ strokeWidth?: number;
234
+ /** Color painted over the whole region. Omit for transparent. */
235
+ background?: string;
236
+ /**
237
+ * One image across all the cells (a URL): drawn once over the layout bounds (cropped to
238
+ * cover them) and clipped to the cells, so each cell shows its piece. Cells' `fill` isn't
239
+ * drawn; per-cell `images` still go on top.
240
+ */
241
+ backdrop?: string;
242
+ /** Prefix for generated element ids; set it when several layouts share a page. */
243
+ idPrefix?: string;
244
+ /** Decimal places for coordinates. Defaults to 3. */
245
+ precision?: number;
246
+ /** Width/height attributes of the <svg>; it always gets a viewBox. */
247
+ width?: string | number;
248
+ height?: string | number;
249
+ /** Accessible title. */
250
+ title?: string;
251
+ /**
252
+ * Text alternative for each image in `images` (by position). Cells showing an image with
253
+ * one become labeled images to assistive technology, and the SVG a group of them; the
254
+ * other cells are hidden as decoration.
255
+ */
256
+ imageAlts?: readonly string[];
257
+ }
258
+ /** `d` attribute for a polygon. With `radius`, corners are rounded with circular arcs. */
259
+ declare function polygonToPath(p: Polygon, precision?: number, radius?: number): string;
260
+ /**
261
+ * `d` attribute for a cell's outline, including the layout's corner rounding and any holes
262
+ * (as extra subpaths; draw with `fill-rule="evenodd"`). For a window cell this is the
263
+ * whole tile (see `cellShape`); clip it to `viewPath`.
264
+ */
265
+ declare function cellPath(layout: Layout, cell: Cell, precision?: number): string;
266
+ /**
267
+ * `d` attribute for the area tiles show in (draw or clip with `evenodd`): `layout.view` in
268
+ * a window layout, else the region with its holes.
269
+ */
270
+ declare function viewPath(layout: Layout, precision?: number): string;
271
+ /** `d` attribute for the region with its holes (draw with `fill-rule="evenodd"`). */
272
+ declare function regionPath(layout: Layout, precision?: number): string;
273
+ /** Minimal element tree, serializable to a string or built into DOM nodes. */
274
+ interface SvgNode {
275
+ tag: string;
276
+ attrs: Record<string, string | number | undefined>;
277
+ children: (SvgNode | string)[];
278
+ }
279
+ declare function toSvgTree(layout: Layout, opts?: RenderOptions): SvgNode;
280
+ declare function serialize(node: SvgNode | string): string;
281
+ /** Render a layout to a standalone SVG document string. */
282
+ declare function renderToString(layout: Layout, opts?: RenderOptions): string;
283
+ /** Render a layout to a live <svg> element. */
284
+ declare function renderToElement(layout: Layout, opts?: RenderOptions, doc?: Document): SVGSVGElement;
285
+
286
+ export { type CellStyle as C, type CanvasContextLike, type CanvasImage, type CanvasLike, type CanvasRenderOptions, type FillSource, type ImageSource, type RasterOptions, type ReliefLayer, type ReliefOptions, type ReliefPath, type RenderOptions, type SvgNode, resolveFills as a, cellA11y as c, cellPath, defaultBevel, defaultPalette, drawLayout, imagesUsed, loadImages, neutralPalette, polygonToPath, resolveCellStyles as r, rasterSize, regionPath, reliefLayers, reliefPaths, renderToCanvas, renderToElement, renderToPng, renderToString, serialize, shadowOffset, toSvgTree, tracePolygon, viewPath };