msdfgen-ts 0.1.0 → 0.3.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,58 @@
1
+ /**
2
+ * Boolean ops on nonzero fills. unionShape removes overlaps: it rewrites a
3
+ * shape as the boundary of its nonzero fill, so overlapping contours
4
+ * (variable fonts, composite glyphs) no longer leave holes where msdfgen's
5
+ * overlapping-contour combiner gets their interior edges wrong. msdfgen
6
+ * itself does this step with Skia path ops (MSDFGEN_USE_SKIA:
7
+ * resolveShapeGeometry). clipShape intersects a shape with a clip region
8
+ * (SVG clip paths, the viewport), and subtractShape cuts other shapes out of
9
+ * one (SVG group opacity). This file is original.
10
+ *
11
+ * Every edge is split where it crosses another, each piece is kept iff the
12
+ * result's fill differs on its two sides, and the kept pieces are relinked
13
+ * into contours. Shapes the op doesn't change are returned untouched, and
14
+ * anything ambiguous (coincident curves, pieces that don't link up) also
15
+ * leaves the shape as it was — never worse than no preprocessing.
16
+ *
17
+ * unionShape runs on every shape generateShapeMSDF sees, overlapping or not,
18
+ * so the crossing search skips what can't matter: edge pairs whose bounds
19
+ * don't overlap (sweep over sorted bounds), and the endpoint consecutive
20
+ * edges share (see corner()). Module-scope scratch below is safe because the
21
+ * library is synchronous and single-threaded per worker.
22
+ */
23
+ import { type Contour } from "./contour.js";
24
+ import { type Shape } from "./shape.js";
25
+ /**
26
+ * Replaces `shape`'s contours with the boundary of their union (nonzero
27
+ * fill), in place. Leaves the shape untouched when it has no overlaps or
28
+ * when they can't be resolved cleanly. Expects em-normalized geometry,
29
+ * before `normalizeShape`.
30
+ *
31
+ * @param shape Shape to resolve (mutated in place).
32
+ */
33
+ export declare function unionShape(shape: Shape): void;
34
+ /**
35
+ * Clips `shape` in place to the union of the `clip` groups: what remains is
36
+ * the part of its nonzero fill that lies inside the nonzero fill of at least
37
+ * one group, as the boundary of that region (overlaps merged). Groups are
38
+ * unioned by fill, so their contours' orientations don't have to agree.
39
+ * Leaves the shape untouched when it already lies inside the clip, or when
40
+ * the intersection can't be resolved cleanly; an empty `clip` clips
41
+ * everything away. Expects em-normalized geometry, like unionShape. The
42
+ * clip's contours are only read.
43
+ *
44
+ * @param shape Shape to clip (mutated in place).
45
+ * @param clip Clip region: groups of closed contours, in `shape`'s space.
46
+ */
47
+ export declare function clipShape(shape: Shape, clip: Contour[][]): void;
48
+ /**
49
+ * Cuts the union of the `cut` groups out of `shape`, in place: what remains
50
+ * is the part of its nonzero fill outside every group's nonzero fill, as the
51
+ * boundary of that region (overlaps merged). Leaves the shape untouched when
52
+ * that boundary is its own, or when the difference can't be resolved cleanly. Expects em-normalized geometry, like unionShape. The
53
+ * cut's contours are only read.
54
+ *
55
+ * @param shape Shape to cut (mutated in place).
56
+ * @param cut Region to remove: groups of closed contours, in `shape`'s space.
57
+ */
58
+ export declare function subtractShape(shape: Shape, cut: Contour[][]): void;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Minimal SVG → Shape parser: the `svg/` twin of `font/`. No DOM.
3
+ *
4
+ * Supported: `<path>` (all commands, arcs converted to cubics) and the basic
5
+ * shapes (`<rect>` incl. rounded, `<circle>`, `<ellipse>`, `<polygon>`,
6
+ * `<polyline>`); `transform` on any element, composed through `<g>`; `fill`,
7
+ * `fill-rule`, `fill-opacity` (inherited) and `opacity` from attributes, CSS
8
+ * class rules in `<style>` (`.name` selectors only, later rules win) or inline
9
+ * `style`; group `opacity` applied to the group's composite (exact when its
10
+ * content is opaque, see parseSvgLayers); percentage lengths; linear/
11
+ * radial gradient fills (`gradientUnits`, `gradientTransform`; attributes and
12
+ * stops a gradient doesn't set come from the one its `href` links to; pad only,
13
+ * focal point ignored); `clip-path` on any element, composed through `<g>`
14
+ * (`<clipPath>` children: basic shapes, or `<use>` of one, with `clip-rule`;
15
+ * user space only), applied as geometry: each shape is intersected with its
16
+ * clip (clipShape); the root `viewBox` (or `width`/`height`), which also clips
17
+ * like a browser's viewport. `fill-rule="evenodd"` shapes are re-oriented so
18
+ * nonzero gives the same fill (msdfgen's orientContours; self-intersecting
19
+ * even-odd paths are approximate, as in msdfgen without Skia).
20
+ * Not supported: strokes, masks, filters, blend modes, `<use>` outside
21
+ * `<clipPath>`, `<text>`, nested `<svg>`, `clipPathUnits="objectBoundingBox"`
22
+ * (the clip is ignored). `<defs>`, `<clipPath>`, `<mask>`, `<symbol>`,
23
+ * `<pattern>` and `<marker>` contents are not drawn.
24
+ */
25
+ import { type Shape } from "../shape/shape.js";
26
+ /** A gradient fill, ready for a shader. Colours are raw CSS strings. */
27
+ export interface SvgGradient {
28
+ /** Linear: t = x'. Radial: t = length(x', y'). Clamped to [0, 1] (pad). */
29
+ radial: boolean;
30
+ /** Affine `[a, b, c, d, e, f]` (SVG `matrix()` order) from shape space
31
+ * to gradient space: x' = a·x + c·y + e, y' = b·x + d·y + f. */
32
+ matrix: number[];
33
+ stops: {
34
+ offset: number;
35
+ color: string;
36
+ opacity: number;
37
+ }[];
38
+ }
39
+ /** One filled `<path>`: its own shape (same space as {@link parseSvg}) and paint. */
40
+ export interface SvgLayer {
41
+ shape: Shape;
42
+ /** CSS colour of `fill`; unused when `gradient` is set. */
43
+ color: string;
44
+ /** `opacity × fill-opacity`. */
45
+ opacity: number;
46
+ gradient: SvgGradient | null;
47
+ }
48
+ /**
49
+ * Parses an SVG document into one filled {@link Shape}: y-up, origin at the
50
+ * viewBox's bottom-left, 1 unit = the viewBox height, clipped to the viewBox.
51
+ * All paths are unioned. The single-colour twin of {@link parseSvgLayers}.
52
+ *
53
+ * @param svg SVG source text.
54
+ * @returns The shape, ready for `Atlas.shapes` or `generateShapeMSDF`, plus
55
+ * the viewBox width (shape units; height is 1) for placing the icon.
56
+ */
57
+ export declare function parseSvg(svg: string): {
58
+ shape: Shape;
59
+ width: number;
60
+ };
61
+ /**
62
+ * Parses an SVG document into one {@link SvgLayer} per filled path, in paint
63
+ * order (back to front), for drawing with real colours and gradients. Each
64
+ * layer's shape is already clipped (clip paths, viewBox); a path clipped away
65
+ * entirely has no layer. A group's `opacity` applies to its composite, which
66
+ * layers drawn one by one can't do directly; when everything in the group is
67
+ * opaque, each member is cut down to the part no later member covers, so
68
+ * drawing the members with the group's opacity is exact. A group holding
69
+ * translucent content keeps that opacity multiplied into its members, an
70
+ * approximation where they overlap.
71
+ *
72
+ * @param svg SVG source text.
73
+ * @returns The layers, plus the viewBox width (shape units; height is 1).
74
+ */
75
+ export declare function parseSvgLayers(svg: string): {
76
+ layers: SvgLayer[];
77
+ width: number;
78
+ };
package/package.json CHANGED
@@ -1,31 +1,34 @@
1
1
  {
2
2
  "name": "msdfgen-ts",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Runtime MSDF font atlases for WebGL/WebGPU: a TypeScript port of msdfgen with a built-in TrueType parser. No WASM.",
5
5
  "keywords": [
6
+ "atlas",
7
+ "font",
6
8
  "msdf",
7
9
  "msdfgen",
8
10
  "sdf",
9
11
  "signed-distance-field",
10
- "font",
11
12
  "text",
12
13
  "truetype",
13
- "atlas",
14
14
  "webgl",
15
15
  "webgpu"
16
16
  ],
17
17
  "homepage": "https://github.com/w8r/msdfgen-ts#readme",
18
18
  "bugs": "https://github.com/w8r/msdfgen-ts/issues",
19
+ "license": "MIT",
20
+ "author": "Alexander Milevski",
19
21
  "repository": {
20
22
  "type": "git",
21
23
  "url": "git+https://github.com/w8r/msdfgen-ts.git"
22
24
  },
23
- "license": "MIT",
24
- "author": "Alexander Milevski",
25
- "type": "module",
26
25
  "files": [
27
26
  "dist"
28
27
  ],
28
+ "type": "module",
29
+ "sideEffects": [
30
+ "./dist/atlas-worker.js"
31
+ ],
29
32
  "main": "./dist/msdfgen-ts.js",
30
33
  "module": "./dist/msdfgen-ts.js",
31
34
  "types": "./dist/types/index.d.ts",
@@ -43,9 +46,6 @@
43
46
  "default": "./dist/atlas-worker.js"
44
47
  }
45
48
  },
46
- "sideEffects": [
47
- "./dist/atlas-worker.js"
48
- ],
49
49
  "scripts": {
50
50
  "gate:m0": "vitest run test/diff.test.ts",
51
51
  "gate:m1": "vitest run test/parser/",
@@ -55,43 +55,65 @@
55
55
  "gate:m3": "vitest run test/msdf/msdf.test.ts",
56
56
  "gate:m4": "vitest run test/atlas/",
57
57
  "gate:m5": "vitest run test/e2e.test.ts",
58
- "gate:m6": "vitest run test/size.test.ts && tsx --expose-gc tools/bench.mjs",
59
- "gate:all": "vitest run test/diff.test.ts test/parser/ test/shape/normalize.test.ts test/msdf/ test/atlas/ test/e2e.test.ts",
58
+ "gate:m6": "vitest run test/size.test.ts && tsx --expose-gc tools/bench.ts",
59
+ "gate:all": "vitest run test/diff.test.ts test/parser/ test/shape/normalize.test.ts test/msdf/ test/svg/ test/atlas/ test/layout/ test/e2e.test.ts",
60
+ "test:kerning": "vitest run test/layout/",
61
+ "bench:layout": "tsx tools/bench-layout.ts",
60
62
  "bench:vitest": "vitest bench test/bench/",
61
- "gen-golden": "node tools/gen-golden.mjs",
63
+ "gen-golden": "tsx tools/gen-golden.ts",
62
64
  "setup-reference": "bash tools/setup-reference.sh",
63
- "atlas-preview": "tsx tools/atlas-preview.mjs",
64
- "screenshot": "node tools/screenshot.mjs",
65
+ "atlas-preview": "tsx tools/atlas-preview.ts",
66
+ "screenshot": "tsx tools/screenshot.ts",
65
67
  "screenshot:setup": "playwright install chromium",
66
- "dev": "vite --open /demo/canvas/index.html",
68
+ "dev": "vite --open /demo/index.html",
67
69
  "dev:webgpu": "vite --open /demo/webgpu/index.html",
68
70
  "dev:webgl": "vite --open /demo/webgl/index.html",
69
71
  "dev:webgl-zoom": "vite --open /demo/webgl-zoom/index.html",
70
72
  "dev:webgpu-zoom": "vite --open /demo/webgpu-zoom/index.html",
71
73
  "dev:lucide": "vite --open /demo/lucide/index.html",
74
+ "dev:svg": "vite --open /demo/svg/index.html",
75
+ "dev:svg-bounce": "vite --open /demo/svg-bounce/index.html",
72
76
  "dev:bench": "vite --open /demo/bench/index.html",
73
77
  "build": "vite build && vite build --config vite.worker.config.ts && tsc -p tsconfig.build.json && tsx tools/fix-dts-extensions.ts",
74
78
  "build:demo": "vite build --config vite.demo.config.ts",
75
79
  "predeploy": "npm run build:demo",
76
80
  "deploy": "gh-pages -d demo-dist --nojekyll",
77
81
  "size": "vite build && gzip -c dist/msdfgen-ts.iife.js | wc -c | awk '{printf \"%.1f KB (iife, gzip)\\n\", $1/1024}'",
78
- "lint": "oxlint src/ test/",
82
+ "lint": "oxlint --deny-warnings .",
79
83
  "typecheck": "tsc --noEmit",
80
84
  "dev:hello-webgl": "vite --open /demo/hello-webgl/index.html",
85
+ "dev:hello-svg": "vite --open /demo/hello-svg/index.html",
81
86
  "dev:hello-webgpu": "vite --open /demo/hello-webgpu/index.html",
82
- "prepublishOnly": "npm run build"
87
+ "dev:three": "vite --open /demo/three/index.html",
88
+ "dev:r3f": "vite --open /demo/r3f/index.html",
89
+ "dev:pixi": "vite --open /demo/pixi/index.html",
90
+ "prepublishOnly": "npm run build",
91
+ "format": "oxfmt .",
92
+ "format:check": "oxfmt --check ."
83
93
  },
84
94
  "dependencies": {
85
95
  "potpack": "2.1.0"
86
96
  },
87
97
  "devDependencies": {
88
- "@types/node": "22.0.0",
98
+ "@chenglou/pretext": "1.0.0",
99
+ "@react-three/drei": "10.7.9",
100
+ "@react-three/fiber": "9.8.1",
101
+ "@types/node": "22.20.5",
89
102
  "@types/opentype.js": "1.3.10",
103
+ "@types/react": "19.3.0",
104
+ "@types/react-dom": "19.3.0",
105
+ "@types/three": "0.186.0",
90
106
  "@webgpu/types": "0.1.71",
91
107
  "gh-pages": "6.3.0",
92
108
  "opentype.js": "1.3.4",
109
+ "oxfmt": "0.71.0",
93
110
  "oxlint": "1.0.0",
111
+ "pixi.js": "8.22.0",
94
112
  "playwright": "1.62.1",
113
+ "react": "19.3.0",
114
+ "react-dom": "19.3.0",
115
+ "suspend-react": "0.1.3",
116
+ "three": "0.186.1",
95
117
  "tsx": "4.23.12",
96
118
  "typescript": "7.0.2",
97
119
  "vite": "6.3.0",