@babylonjs/lite-gl 1.19.0 → 1.20.0-preview

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 (52) hide show
  1. package/CHANGELOG.md +1350 -0
  2. package/LICENSE +201 -0
  3. package/README.md +31 -0
  4. package/index.d.ts +13 -0
  5. package/index.js +7 -0
  6. package/index.js.map +1 -1
  7. package/package.json +9 -3
  8. package/text/_gpu/text-effect.d.ts +1 -0
  9. package/text/_gpu/text-effect.js +70 -0
  10. package/text/_gpu/text-effect.js.map +1 -0
  11. package/text/_gpu/text-instances.d.ts +1 -0
  12. package/text/_gpu/text-instances.js +57 -0
  13. package/text/_gpu/text-instances.js.map +1 -0
  14. package/text/_gpu/text-textures.d.ts +1 -0
  15. package/text/_gpu/text-textures.js +82 -0
  16. package/text/_gpu/text-textures.js.map +1 -0
  17. package/text/_gpu/text-vao.d.ts +1 -0
  18. package/text/_gpu/text-vao.js +49 -0
  19. package/text/_gpu/text-vao.js.map +1 -0
  20. package/text/default-text-data.d.ts +28 -0
  21. package/text/default-text-data.js +103 -0
  22. package/text/default-text-data.js.map +1 -0
  23. package/text/font.d.ts +16 -0
  24. package/text/font.js +24 -0
  25. package/text/font.js.map +1 -0
  26. package/text/glyph-extraction.d.ts +19 -0
  27. package/text/glyph-extraction.js +115 -0
  28. package/text/glyph-extraction.js.map +1 -0
  29. package/text/glyph-storage.d.ts +68 -0
  30. package/text/glyph-storage.js +298 -0
  31. package/text/glyph-storage.js.map +1 -0
  32. package/text/layout.d.ts +18 -0
  33. package/text/layout.js +219 -0
  34. package/text/layout.js.map +1 -0
  35. package/text/set-font-weight-offset.d.ts +5 -0
  36. package/text/set-font-weight-offset.js +67 -0
  37. package/text/set-font-weight-offset.js.map +1 -0
  38. package/text/shaders/slug-shader.d.ts +1 -0
  39. package/text/shaders/slug-shader.js +181 -0
  40. package/text/shaders/slug-shader.js.map +1 -0
  41. package/text/shaders/text-shader-fragment.d.ts +1 -0
  42. package/text/shaders/text-shader-fragment.js +2 -0
  43. package/text/shaders/text-shader-fragment.js.map +1 -0
  44. package/text/shaders/weight-shader-fragment.d.ts +1 -0
  45. package/text/shaders/weight-shader-fragment.js +75 -0
  46. package/text/shaders/weight-shader-fragment.js.map +1 -0
  47. package/text/text-data.d.ts +109 -0
  48. package/text/text-data.js +723 -0
  49. package/text/text-data.js.map +1 -0
  50. package/text/text-renderer.d.ts +68 -0
  51. package/text/text-renderer.js +238 -0
  52. package/text/text-renderer.js.map +1 -0
@@ -0,0 +1,49 @@
1
+ import { TEXT_INSTANCE_BYTES } from "../text-data.js";
2
+ /** @internal */
3
+ export function disposeTextVao(engine, vao) {
4
+ if (!engine._isLost && !engine._disposed) {
5
+ if (engine._state.boundVao === vao._handle) {
6
+ engine.gl.bindVertexArray(null);
7
+ engine._state.boundVao = null;
8
+ engine._state.boundElementBuffer = null;
9
+ }
10
+ engine.gl.deleteVertexArray(vao._handle);
11
+ }
12
+ }
13
+ /** @internal */
14
+ export function bindTextVao(engine, quad, instances, slotStart, previous) {
15
+ const gl = engine.gl;
16
+ const state = engine._state;
17
+ let vao = previous;
18
+ if (!vao || vao._quad !== quad.handle || vao._instances !== instances.handle || vao._slotStart !== slotStart) {
19
+ const handle = gl.createVertexArray();
20
+ if (!handle) {
21
+ throw new Error("lite-gl: gl.createVertexArray returned null (text)");
22
+ }
23
+ if (vao) {
24
+ disposeTextVao(engine, vao);
25
+ }
26
+ vao = { _handle: handle, _quad: quad.handle, _instances: instances.handle, _slotStart: slotStart };
27
+ gl.bindVertexArray(handle);
28
+ gl.bindBuffer(gl.ARRAY_BUFFER, quad.handle);
29
+ gl.enableVertexAttribArray(0);
30
+ gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 8, 0);
31
+ gl.bindBuffer(gl.ARRAY_BUFFER, instances.handle);
32
+ gl.enableVertexAttribArray(1);
33
+ gl.vertexAttribPointer(1, 2, gl.FLOAT, false, TEXT_INSTANCE_BYTES, slotStart * TEXT_INSTANCE_BYTES);
34
+ gl.vertexAttribDivisor(1, 1);
35
+ gl.enableVertexAttribArray(2);
36
+ gl.vertexAttribIPointer(2, 1, gl.UNSIGNED_INT, TEXT_INSTANCE_BYTES, slotStart * TEXT_INSTANCE_BYTES + 8);
37
+ gl.vertexAttribDivisor(2, 1);
38
+ state.boundArrayBuffer = instances.handle;
39
+ state.boundVao = handle;
40
+ state.boundElementBuffer = null;
41
+ }
42
+ else if (state.boundVao !== vao._handle) {
43
+ gl.bindVertexArray(vao._handle);
44
+ state.boundVao = vao._handle;
45
+ state.boundElementBuffer = null;
46
+ }
47
+ return vao;
48
+ }
49
+ //# sourceMappingURL=text-vao.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-vao.js","sourceRoot":"","sources":["../../../src/text/_gpu/text-vao.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AActD,gBAAgB;AAChB,MAAM,UAAU,cAAc,CAAC,MAAuB,EAAE,GAAY;IAChE,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACvC,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,KAAK,GAAG,CAAC,OAAO,EAAE,CAAC;YACzC,MAAM,CAAC,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;YAChC,MAAM,CAAC,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC;YAC9B,MAAM,CAAC,MAAM,CAAC,kBAAkB,GAAG,IAAI,CAAC;QAC5C,CAAC;QACD,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;AACL,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,WAAW,CAAC,MAAuB,EAAE,IAAoB,EAAE,SAAyB,EAAE,SAAiB,EAAE,QAAkB;IACvI,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACr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pG,EAAE,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC7B,EAAE,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC;QAC9B,EAAE,CAAC,oBAAoB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,YAAY,EAAE,mBAAmB,EAAE,SAAS,GAAG,mBAAmB,GAAG,CAAC,CAAC,CAAC;QACzG,EAAE,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC7B,KAAK,CAAC,gBAAgB,GAAG,SAAS,CAAC,MAAM,CAAC;QAC1C,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC;QACxB,KAAK,CAAC,kBAAkB,GAAG,IAAI,CAAC;IACpC,CAAC;SAAM,IAAI,KAAK,CAAC,QAAQ,KAAK,GAAG,CAAC,OAAO,EAAE,CAAC;QACxC,EAAE,CAAC,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAChC,KAAK,CAAC,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC;QAC7B,KAAK,CAAC,kBAAkB,GAAG,IAAI,CAAC;IACpC,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC","sourcesContent":["import type { GLEngineContext } from \"../../context.js\";\nimport type { GLVertexBuffer } from \"../../mesh.js\";\nimport { TEXT_INSTANCE_BYTES } from \"../text-data.js\";\n\n/** @internal WebGL2 has no base-instance draw; cache the group's attribute offsets in a VAO. */\nexport interface TextVao {\n /** @internal */\n _handle: WebGLVertexArrayObject;\n /** @internal */\n _quad: WebGLBuffer;\n /** @internal */\n _instances: WebGLBuffer;\n /** @internal */\n _slotStart: number;\n}\n\n/** @internal */\nexport function disposeTextVao(engine: GLEngineContext, vao: TextVao): void {\n if (!engine._isLost && !engine._disposed) {\n if (engine._state.boundVao === vao._handle) {\n engine.gl.bindVertexArray(null);\n engine._state.boundVao = null;\n engine._state.boundElementBuffer = null;\n }\n engine.gl.deleteVertexArray(vao._handle);\n }\n}\n\n/** @internal */\nexport function bindTextVao(engine: GLEngineContext, quad: GLVertexBuffer, instances: GLVertexBuffer, slotStart: number, previous?: TextVao): TextVao {\n const gl = engine.gl;\n const state = engine._state;\n let vao = previous;\n if (!vao || vao._quad !== quad.handle || vao._instances !== instances.handle || vao._slotStart !== slotStart) {\n const handle = gl.createVertexArray();\n if (!handle) {\n throw new Error(\"lite-gl: gl.createVertexArray returned null (text)\");\n }\n if (vao) {\n disposeTextVao(engine, vao);\n }\n vao = { _handle: handle, _quad: quad.handle, _instances: instances.handle, _slotStart: slotStart };\n gl.bindVertexArray(handle);\n gl.bindBuffer(gl.ARRAY_BUFFER, quad.handle);\n gl.enableVertexAttribArray(0);\n gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 8, 0);\n gl.bindBuffer(gl.ARRAY_BUFFER, instances.handle);\n gl.enableVertexAttribArray(1);\n gl.vertexAttribPointer(1, 2, gl.FLOAT, false, TEXT_INSTANCE_BYTES, slotStart * TEXT_INSTANCE_BYTES);\n gl.vertexAttribDivisor(1, 1);\n gl.enableVertexAttribArray(2);\n gl.vertexAttribIPointer(2, 1, gl.UNSIGNED_INT, TEXT_INSTANCE_BYTES, slotStart * TEXT_INSTANCE_BYTES + 8);\n gl.vertexAttribDivisor(2, 1);\n state.boundArrayBuffer = instances.handle;\n state.boundVao = handle;\n state.boundElementBuffer = null;\n } else if (state.boundVao !== vao._handle) {\n gl.bindVertexArray(vao._handle);\n state.boundVao = vao._handle;\n state.boundElementBuffer = null;\n }\n return vao;\n}\n"]}
@@ -0,0 +1,28 @@
1
+ /** Default convenience GLTextData: shapes text + extracts curves into a fresh GLGlyphStorage. */
2
+ import type { GLFont } from "./font.js";
3
+ import type { GLTextData } from "./text-data.js";
4
+ import type { GLTextLayoutOptions } from "./layout.js";
5
+ declare const defaultTextDataBrand: unique symbol;
6
+ /** Convenience text-data variant that owns its `GLGlyphStorage` and exposes the laid-out
7
+ * pixel-space `width` / `height` of the text block. Produced by `createDefaultTextData`. */
8
+ export interface GLDefaultTextData extends GLTextData {
9
+ readonly [defaultTextDataBrand]: true;
10
+ /** Pixel-space width of the laid-out run (max line width). */
11
+ readonly width: number;
12
+ /** Pixel-space height of the laid-out run (lines × line-height). */
13
+ readonly height: number;
14
+ }
15
+ /** Shape `text` with the default layout, extract glyph curves, and bundle into a
16
+ * `GLDefaultTextData`. `textColor` is applied as the run's defaultColor (per-glyph color
17
+ * overrides remain available via direct `updateTextData(replaceRun)` calls).
18
+ *
19
+ * The returned `GLDefaultTextData` owns its underlying `GLGlyphStorage` — release both with
20
+ * `disposeDefaultTextData(data)`. */
21
+ export declare function createDefaultTextData(font: GLFont, fontSizePx: number, text: string, textColor?: readonly [number, number, number, number], options?: GLTextLayoutOptions): GLDefaultTextData;
22
+ /** Re-shape `text` and apply the new run via `updateTextData(replaceRun)`. New glyphs are
23
+ * added to the storage in place. When `textColor` is omitted, the live run's existing
24
+ * `defaultColor` is preserved (so any caller-driven color override survives a text re-shape). */
25
+ export declare function updateDefaultTextData(data: GLDefaultTextData, text: string, textColor?: readonly [number, number, number, number]): void;
26
+ /** Release the per-block GPU resources AND the underlying `GLGlyphStorage` owned by `data`. */
27
+ export declare function disposeDefaultTextData(data: GLDefaultTextData): void;
28
+ export {};
@@ -0,0 +1,103 @@
1
+ /** Default convenience GLTextData: shapes text + extracts curves into a fresh GLGlyphStorage. */
2
+ import { extractGlyphCurves } from "./glyph-extraction.js";
3
+ import { createGlyphStorage, disposeGlyphStorage, updateGlyphStorage } from "./glyph-storage.js";
4
+ import { createTextData, disposeTextData, updateTextData } from "./text-data.js";
5
+ import { layoutText } from "./layout.js";
6
+ import { getFontFamily } from "text-shaper";
7
+ /** Mark every glyph in `glyphs` as seen, returning only the ids not seen before, or `null`
8
+ * when there are none.
9
+ *
10
+ * Re-shaped text almost always reuses the repertoire it already had — an 840-cell grid of
11
+ * changing numbers is ~37 distinct glyphs across 5,466 placements — so the common answer is
12
+ * "nothing new". Answering that with a typed-array probe per glyph keeps the steady state
13
+ * allocation free and lets the caller skip extraction and storage update entirely. That
14
+ * matters more than it looks: `extractGlyphCurves` only early-outs on ids already in the
15
+ * target map, so passing it a fresh map every call made it re-walk the whole repertoire, and
16
+ * glyphs with no outline (space) were re-extracted forever because an absent outline is
17
+ * never cached.
18
+ *
19
+ * A glyph id at or beyond `seen.length` reads `undefined`, which is not `=== 0`, so it is
20
+ * treated as already seen and never reaches extraction. That matches the old behaviour
21
+ * observably: `extractGlyphCurves` asks `extractOne` for such an id, gets `null`, and skips
22
+ * it without recording anything. */
23
+ function collectNewGlyphs(seen, glyphs) {
24
+ let added = null;
25
+ for (const g of glyphs) {
26
+ const id = g.glyphId;
27
+ if (seen[id] === 0) {
28
+ seen[id] = 1;
29
+ (added ??= new Set()).add(id);
30
+ }
31
+ }
32
+ return added;
33
+ }
34
+ /** Derive the curve-set id from the font's family name (e.g. "Inter", "Roboto"). Falls back
35
+ * to `"font"` for fonts that lack a usable name table. */
36
+ function familyCurveSetId(font) {
37
+ return (font._font.name && getFontFamily(font._font.name)) || "font";
38
+ }
39
+ /** Shape `text` with the default layout, extract glyph curves, and bundle into a
40
+ * `GLDefaultTextData`. `textColor` is applied as the run's defaultColor (per-glyph color
41
+ * overrides remain available via direct `updateTextData(replaceRun)` calls).
42
+ *
43
+ * The returned `GLDefaultTextData` owns its underlying `GLGlyphStorage` — release both with
44
+ * `disposeDefaultTextData(data)`. */
45
+ export function createDefaultTextData(font, fontSizePx, text, textColor, options) {
46
+ const laid = layoutText(font, text, fontSizePx, options);
47
+ const innerCurves = new Map();
48
+ const seenGlyphs = new Uint8Array(font._font.numGlyphs);
49
+ const ids = collectNewGlyphs(seenGlyphs, laid._glyphs);
50
+ if (ids) {
51
+ extractGlyphCurves(font, ids, innerCurves);
52
+ }
53
+ const curveSetId = familyCurveSetId(font);
54
+ const storage = createGlyphStorage(new Map([[curveSetId, innerCurves]]));
55
+ const run = {
56
+ curveSet: curveSetId,
57
+ glyphs: laid._glyphs,
58
+ pixelsPerFontUnit: laid._pixelsPerFontUnit,
59
+ defaultColor: textColor,
60
+ };
61
+ return Object.assign(createTextData(storage, [run]), {
62
+ width: laid._width,
63
+ height: laid._height,
64
+ _font: font,
65
+ _fontSizePx: fontSizePx,
66
+ _options: options,
67
+ _curveSetId: curveSetId,
68
+ _storage: storage,
69
+ _seenGlyphs: seenGlyphs,
70
+ });
71
+ }
72
+ /** Re-shape `text` and apply the new run via `updateTextData(replaceRun)`. New glyphs are
73
+ * added to the storage in place. When `textColor` is omitted, the live run's existing
74
+ * `defaultColor` is preserved (so any caller-driven color override survives a text re-shape). */
75
+ export function updateDefaultTextData(data, text, textColor) {
76
+ const laid = layoutText(data._font, text, data._fontSizePx, data._options);
77
+ // Extract any glyph outlines this data has not offered to the storage before. Text that
78
+ // reuses its existing repertoire — the overwhelmingly common case — does no work here.
79
+ const ids = collectNewGlyphs(data._seenGlyphs, laid._glyphs);
80
+ if (ids) {
81
+ const innerCurves = new Map();
82
+ extractGlyphCurves(data._font, ids, innerCurves);
83
+ updateGlyphStorage(data._storage, data._curveSetId, innerCurves);
84
+ }
85
+ // Always use the current `data.runs[0]` as the previous reference; a GLDefaultTextData
86
+ // owns exactly one run and the caller may have swapped it out via their own ops.
87
+ const previousRun = data.runs[0];
88
+ const newRun = {
89
+ curveSet: data._curveSetId,
90
+ glyphs: laid._glyphs,
91
+ pixelsPerFontUnit: laid._pixelsPerFontUnit,
92
+ defaultColor: textColor ?? previousRun.defaultColor,
93
+ };
94
+ updateTextData(data, { update: "replaceRun", previous: previousRun, run: newRun });
95
+ // Refresh the cached width/height on the branded object.
96
+ Object.assign(data, { width: laid._width, height: laid._height });
97
+ }
98
+ /** Release the per-block GPU resources AND the underlying `GLGlyphStorage` owned by `data`. */
99
+ export function disposeDefaultTextData(data) {
100
+ disposeTextData(data);
101
+ disposeGlyphStorage(data._storage);
102
+ }
103
+ //# sourceMappingURL=default-text-data.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-text-data.js","source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,GAAG,GAAG,gBAAgB,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7D,IAAI,GAAG,EAAE,CAAC;QACN,MAAM,WAAW,GAAG,IAAI,GAAG,EAAyB,CAAC;QACrD,kBAAkB,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC;QACjD,kBAAkB,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;IACrE,CAAC;IACD,uFAAuF;IACvF,iFAAiF;IACjF,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAE,CAAC;IAClC,MAAM,MAAM,GAAe;QACvB,QAAQ,EAAE,IAAI,CAAC,WAAW;QAC1B,MAAM,EAAE,IAAI,CAAC,OAAO;QACpB,iBAAiB,EAAE,IAAI,CAAC,kBAAkB;QAC1C,YAAY,EAAE,SAAS,IAAI,WAAW,CAAC,YAAY;KACtD,CAAC;IACF,cAAc,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;IACnF,yDAAyD;IACzD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;AACtE,CAAC;AAED,+FAA+F;AAC/F,MAAM,UAAU,sBAAsB,CAAC,IAAuB;IAC1D,eAAe,CAAC,IAAI,CAAC,CAAC;IACtB,mBAAmB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACvC,CAAC","sourcesContent":["/** Default convenience GLTextData: shapes text + extracts curves into a fresh GLGlyphStorage. */\n\nimport type { GLFont } from \"./font.js\";\nimport { extractGlyphCurves } from \"./glyph-extraction.js\";\nimport type { GLCurveSetId, GLGlyphCurves, GLGlyphStorage } from \"./glyph-storage.js\";\nimport { createGlyphStorage, disposeGlyphStorage, updateGlyphStorage } from \"./glyph-storage.js\";\nimport type { GLGlyphRun, GLPlacedGlyph, GLTextData } from \"./text-data.js\";\nimport { createTextData, disposeTextData, updateTextData } from \"./text-data.js\";\nimport type { GLTextLayoutOptions } from \"./layout.js\";\nimport { layoutText } from \"./layout.js\";\nimport { getFontFamily } from \"text-shaper\";\n\ndeclare const defaultTextDataBrand: unique symbol;\n\n/** Convenience text-data variant that owns its `GLGlyphStorage` and exposes the laid-out\n * pixel-space `width` / `height` of the text block. Produced by `createDefaultTextData`. */\nexport interface GLDefaultTextData extends GLTextData {\n readonly [defaultTextDataBrand]: true;\n /** Pixel-space width of the laid-out run (max line width). */\n readonly width: number;\n /** Pixel-space height of the laid-out run (lines × line-height). */\n readonly height: number;\n /** @internal GLFont used to shape this data. */\n readonly _font: GLFont;\n /** @internal GLFont size in pixels used to shape this data. */\n readonly _fontSizePx: number;\n /** @internal Layout options captured at create-time. */\n readonly _options: GLTextLayoutOptions | undefined;\n /** @internal Curve-set id derived from the font's family name. */\n readonly _curveSetId: GLCurveSetId;\n /** @internal GLGlyphStorage owned by this GLDefaultTextData. Disposed by `disposeDefaultTextData`. */\n readonly _storage: GLGlyphStorage;\n /** @internal Glyph ids already offered to `_storage`, indexed by glyph id. Sized to the\n * font's glyph count, and necessarily per-instance because `_storage` is per-instance —\n * so this costs `numGlyphs` bytes per `GLDefaultTextData` (~3 KB for a Latin font, ~65 KB\n * for a full CJK one). */\n readonly _seenGlyphs: Uint8Array;\n}\n\n/** Mark every glyph in `glyphs` as seen, returning only the ids not seen before, or `null`\n * when there are none.\n *\n * Re-shaped text almost always reuses the repertoire it already had — an 840-cell grid of\n * changing numbers is ~37 distinct glyphs across 5,466 placements — so the common answer is\n * \"nothing new\". Answering that with a typed-array probe per glyph keeps the steady state\n * allocation free and lets the caller skip extraction and storage update entirely. That\n * matters more than it looks: `extractGlyphCurves` only early-outs on ids already in the\n * target map, so passing it a fresh map every call made it re-walk the whole repertoire, and\n * glyphs with no outline (space) were re-extracted forever because an absent outline is\n * never cached.\n *\n * A glyph id at or beyond `seen.length` reads `undefined`, which is not `=== 0`, so it is\n * treated as already seen and never reaches extraction. That matches the old behaviour\n * observably: `extractGlyphCurves` asks `extractOne` for such an id, gets `null`, and skips\n * it without recording anything. */\nfunction collectNewGlyphs(seen: Uint8Array, glyphs: readonly GLPlacedGlyph[]): Set<number> | null {\n let added: Set<number> | null = null;\n for (const g of glyphs) {\n const id = g.glyphId;\n if (seen[id] === 0) {\n seen[id] = 1;\n (added ??= new Set()).add(id);\n }\n }\n return added;\n}\n\n/** Derive the curve-set id from the font's family name (e.g. \"Inter\", \"Roboto\"). Falls back\n * to `\"font\"` for fonts that lack a usable name table. */\nfunction familyCurveSetId(font: GLFont): GLCurveSetId {\n return (font._font.name && getFontFamily(font._font.name)) || \"font\";\n}\n\n/** Shape `text` with the default layout, extract glyph curves, and bundle into a\n * `GLDefaultTextData`. `textColor` is applied as the run's defaultColor (per-glyph color\n * overrides remain available via direct `updateTextData(replaceRun)` calls).\n *\n * The returned `GLDefaultTextData` owns its underlying `GLGlyphStorage` — release both with\n * `disposeDefaultTextData(data)`. */\nexport function createDefaultTextData(\n font: GLFont,\n fontSizePx: number,\n text: string,\n textColor?: readonly [number, number, number, number],\n options?: GLTextLayoutOptions\n): GLDefaultTextData {\n const laid = layoutText(font, text, fontSizePx, options);\n const innerCurves = new Map<number, GLGlyphCurves>();\n const seenGlyphs = new Uint8Array(font._font.numGlyphs);\n const ids = collectNewGlyphs(seenGlyphs, laid._glyphs);\n if (ids) {\n extractGlyphCurves(font, ids, innerCurves);\n }\n const curveSetId = familyCurveSetId(font);\n const storage = createGlyphStorage(new Map([[curveSetId, innerCurves]]));\n const run: GLGlyphRun = {\n curveSet: curveSetId,\n glyphs: laid._glyphs,\n pixelsPerFontUnit: laid._pixelsPerFontUnit,\n defaultColor: textColor,\n };\n return Object.assign(createTextData(storage, [run]), {\n width: laid._width,\n height: laid._height,\n _font: font,\n _fontSizePx: fontSizePx,\n _options: options,\n _curveSetId: curveSetId,\n _storage: storage,\n _seenGlyphs: seenGlyphs,\n }) as GLDefaultTextData;\n}\n\n/** Re-shape `text` and apply the new run via `updateTextData(replaceRun)`. New glyphs are\n * added to the storage in place. When `textColor` is omitted, the live run's existing\n * `defaultColor` is preserved (so any caller-driven color override survives a text re-shape). */\nexport function updateDefaultTextData(data: GLDefaultTextData, text: string, textColor?: readonly [number, number, number, number]): void {\n const laid = layoutText(data._font, text, data._fontSizePx, data._options);\n // Extract any glyph outlines this data has not offered to the storage before. Text that\n // reuses its existing repertoire — the overwhelmingly common case — does no work here.\n const ids = collectNewGlyphs(data._seenGlyphs, laid._glyphs);\n if (ids) {\n const innerCurves = new Map<number, GLGlyphCurves>();\n extractGlyphCurves(data._font, ids, innerCurves);\n updateGlyphStorage(data._storage, data._curveSetId, innerCurves);\n }\n // Always use the current `data.runs[0]` as the previous reference; a GLDefaultTextData\n // owns exactly one run and the caller may have swapped it out via their own ops.\n const previousRun = data.runs[0]!;\n const newRun: GLGlyphRun = {\n curveSet: data._curveSetId,\n glyphs: laid._glyphs,\n pixelsPerFontUnit: laid._pixelsPerFontUnit,\n defaultColor: textColor ?? previousRun.defaultColor,\n };\n updateTextData(data, { update: \"replaceRun\", previous: previousRun, run: newRun });\n // Refresh the cached width/height on the branded object.\n Object.assign(data, { width: laid._width, height: laid._height });\n}\n\n/** Release the per-block GPU resources AND the underlying `GLGlyphStorage` owned by `data`. */\nexport function disposeDefaultTextData(data: GLDefaultTextData): void {\n disposeTextData(data);\n disposeGlyphStorage(data._storage);\n}\n"]}
package/text/font.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /** GLFont loading. Wraps text-shaper behind a branded opaque type.
2
+ *
3
+ * A `GLFont` is the boundary between Babylon Lite and the text-shaper library. It is
4
+ * consumed by the default-layout pipeline (`layoutText` in `layout.ts`) and by the
5
+ * default-curve-extraction pipeline (`extractGlyphCurves` in `glyph-extraction.ts`).
6
+ * Callers driving their own text layout and outline extraction never need a `GLFont`. */
7
+ declare const fontBrand: unique symbol;
8
+ /** Opaque handle for a loaded TrueType/OpenType font used by the default text layout and glyph extraction helpers. */
9
+ export interface GLFont {
10
+ readonly [fontBrand]: true;
11
+ }
12
+ /** Load a TTF or OTF font from a URL. */
13
+ export declare function loadFont(url: string): Promise<GLFont>;
14
+ /** Build a `GLFont` from an in-memory TTF/OTF buffer. */
15
+ export declare function createFontFromBuffer(data: ArrayBuffer): GLFont;
16
+ export {};
package/text/font.js ADDED
@@ -0,0 +1,24 @@
1
+ /** GLFont loading. Wraps text-shaper behind a branded opaque type.
2
+ *
3
+ * A `GLFont` is the boundary between Babylon Lite and the text-shaper library. It is
4
+ * consumed by the default-layout pipeline (`layoutText` in `layout.ts`) and by the
5
+ * default-curve-extraction pipeline (`extractGlyphCurves` in `glyph-extraction.ts`).
6
+ * Callers driving their own text layout and outline extraction never need a `GLFont`. */
7
+ import { Font as TextShaperFont } from "text-shaper";
8
+ /** Load a TTF or OTF font from a URL. */
9
+ export async function loadFont(url) {
10
+ const response = await fetch(url);
11
+ if (!response.ok) {
12
+ throw new Error(`loadFont: failed to fetch ${url} (${response.status})`);
13
+ }
14
+ const data = await response.arrayBuffer();
15
+ return createFontFromBuffer(data);
16
+ }
17
+ /** Build a `GLFont` from an in-memory TTF/OTF buffer. */
18
+ export function createFontFromBuffer(data) {
19
+ return {
20
+ _font: TextShaperFont.load(data),
21
+ _curvesCache: null,
22
+ };
23
+ }
24
+ //# sourceMappingURL=font.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"font.js","sourceRoot":"","sources":["../../src/text/font.ts"],"names":[],"mappings":"AAAA;;;;;0FAK0F;AAE1F,OAAO,EAAE,IAAI,IAAI,cAAc,EAAE,MAAM,aAAa,CAAC;AAcrD,yCAAyC;AACzC,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,GAAW;IACtC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,6BAA6B,GAAG,KAAK,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IAC7E,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC;IAC1C,OAAO,oBAAoB,CAAC,IAAI,CAAC,CAAC;AACtC,CAAC;AAED,yDAAyD;AACzD,MAAM,UAAU,oBAAoB,CAAC,IAAiB;IAClD,OAAO;QACH,KAAK,EAAE,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC;QAChC,YAAY,EAAE,IAAI;KACA,CAAC;AAC3B,CAAC","sourcesContent":["/** GLFont loading. Wraps text-shaper behind a branded opaque type.\n *\n * A `GLFont` is the boundary between Babylon Lite and the text-shaper library. It is\n * consumed by the default-layout pipeline (`layoutText` in `layout.ts`) and by the\n * default-curve-extraction pipeline (`extractGlyphCurves` in `glyph-extraction.ts`).\n * Callers driving their own text layout and outline extraction never need a `GLFont`. */\n\nimport { Font as TextShaperFont } from \"text-shaper\";\nimport type { GLGlyphCurves } from \"./glyph-storage.js\";\n\ndeclare const fontBrand: unique symbol;\n\n/** Opaque handle for a loaded TrueType/OpenType font used by the default text layout and glyph extraction helpers. */\nexport interface GLFont {\n readonly [fontBrand]: true;\n /** @internal Underlying text-shaper font handle. */\n readonly _font: TextShaperFont;\n /** @internal Lazily-allocated per-font glyph-curves cache. */\n _curvesCache: Map<number, GLGlyphCurves> | null;\n}\n\n/** Load a TTF or OTF font from a URL. */\nexport async function loadFont(url: string): Promise<GLFont> {\n const response = await fetch(url);\n if (!response.ok) {\n throw new Error(`loadFont: failed to fetch ${url} (${response.status})`);\n }\n const data = await response.arrayBuffer();\n return createFontFromBuffer(data);\n}\n\n/** Build a `GLFont` from an in-memory TTF/OTF buffer. */\nexport function createFontFromBuffer(data: ArrayBuffer): GLFont {\n return {\n _font: TextShaperFont.load(data),\n _curvesCache: null,\n } as unknown as GLFont;\n}\n"]}
@@ -0,0 +1,19 @@
1
+ /** Glyph outline extraction: the default `text-shaper`-backed pipeline that
2
+ * produces `GLGlyphCurves` values from a `GLFont`'s glyph paths.
3
+ *
4
+ * The outline value types themselves (`GLQuadCurve`, `GLGlyphBounds`, `GLGlyphCurves`)
5
+ * live in `glyph-storage.ts` because storage is what owns and persists them.
6
+ * Callers using their own outline source (DirectWrite, FreeType, hand-rolled)
7
+ * import those types directly from `glyph-storage.js` and never pull this
8
+ * module — so `text-shaper` stays out of their bundle. The only helper they
9
+ * may want is `cubicToQuadratics`, which has no dependency on text-shaper. */
10
+ import type { GLFont } from "./font.js";
11
+ import type { GLGlyphCurves, GLQuadCurve } from "./glyph-storage.js";
12
+ /** Approximate a cubic Bézier with two quadratics using the "3/4 rule" (matches Slug reference).
13
+ * Exposed as a public helper so callers that ingest cubic outlines from their own font sources
14
+ * (e.g. DirectWrite, FreeType) can convert into the quadratic-only format `GLGlyphCurves` expects. */
15
+ export declare function cubicToQuadratics(p0x: number, p0y: number, c1x: number, c1y: number, c2x: number, c2y: number, p1x: number, p1y: number): [GLQuadCurve, GLQuadCurve];
16
+ /** Extract outlines for the requested glyph ids and add them to `target`.
17
+ * Skips ids already present in `target`. Glyphs with no outline are silently skipped.
18
+ * Mutates `target` directly — no allocation when no new glyphs appear. */
19
+ export declare function extractGlyphCurves(font: GLFont, glyphIds: ReadonlySet<number>, target: Map<number, GLGlyphCurves>): void;
@@ -0,0 +1,115 @@
1
+ /** Glyph outline extraction: the default `text-shaper`-backed pipeline that
2
+ * produces `GLGlyphCurves` values from a `GLFont`'s glyph paths.
3
+ *
4
+ * The outline value types themselves (`GLQuadCurve`, `GLGlyphBounds`, `GLGlyphCurves`)
5
+ * live in `glyph-storage.ts` because storage is what owns and persists them.
6
+ * Callers using their own outline source (DirectWrite, FreeType, hand-rolled)
7
+ * import those types directly from `glyph-storage.js` and never pull this
8
+ * module — so `text-shaper` stays out of their bundle. The only helper they
9
+ * may want is `cubicToQuadratics`, which has no dependency on text-shaper. */
10
+ import { getGlyphPath } from "text-shaper";
11
+ /** Approximate a cubic Bézier with two quadratics using the "3/4 rule" (matches Slug reference).
12
+ * Exposed as a public helper so callers that ingest cubic outlines from their own font sources
13
+ * (e.g. DirectWrite, FreeType) can convert into the quadratic-only format `GLGlyphCurves` expects. */
14
+ export function cubicToQuadratics(p0x, p0y, c1x, c1y, c2x, c2y, p1x, p1y) {
15
+ const q0cx = p0x + (c1x - p0x) * 0.75;
16
+ const q0cy = p0y + (c1y - p0y) * 0.75;
17
+ const q1cx = p1x + (c2x - p1x) * 0.75;
18
+ const q1cy = p1y + (c2y - p1y) * 0.75;
19
+ const midx = (q0cx + q1cx) * 0.5;
20
+ const midy = (q0cy + q1cy) * 0.5;
21
+ return [
22
+ { p0x, p0y, p1x: q0cx, p1y: q0cy, p2x: midx, p2y: midy },
23
+ { p0x: midx, p0y: midy, p1x: q1cx, p1y: q1cy, p2x: p1x, p2y: p1y },
24
+ ];
25
+ }
26
+ function extractOne(font, glyphId) {
27
+ const path = getGlyphPath(font._font, glyphId);
28
+ if (!path || !path.bounds) {
29
+ return null;
30
+ }
31
+ const curves = [];
32
+ let curX = 0;
33
+ let curY = 0;
34
+ let startX = 0;
35
+ let startY = 0;
36
+ for (const cmd of path.commands) {
37
+ switch (cmd.type) {
38
+ case "M":
39
+ curX = cmd.x;
40
+ curY = cmd.y;
41
+ startX = cmd.x;
42
+ startY = cmd.y;
43
+ break;
44
+ case "L": {
45
+ const dx = cmd.x - curX;
46
+ const dy = cmd.y - curY;
47
+ if (Math.abs(dx) >= 0.1 || Math.abs(dy) >= 0.1) {
48
+ const mx = (curX + cmd.x) * 0.5;
49
+ const my = (curY + cmd.y) * 0.5;
50
+ curves.push({ p0x: curX, p0y: curY, p1x: mx, p1y: my, p2x: cmd.x, p2y: cmd.y });
51
+ }
52
+ curX = cmd.x;
53
+ curY = cmd.y;
54
+ break;
55
+ }
56
+ case "Q":
57
+ curves.push({ p0x: curX, p0y: curY, p1x: cmd.x1, p1y: cmd.y1, p2x: cmd.x, p2y: cmd.y });
58
+ curX = cmd.x;
59
+ curY = cmd.y;
60
+ break;
61
+ case "C": {
62
+ const [q1, q2] = cubicToQuadratics(curX, curY, cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);
63
+ curves.push(q1, q2);
64
+ curX = cmd.x;
65
+ curY = cmd.y;
66
+ break;
67
+ }
68
+ case "Z": {
69
+ const cdx = startX - curX;
70
+ const cdy = startY - curY;
71
+ if (Math.abs(cdx) > 0.1 || Math.abs(cdy) > 0.1) {
72
+ const mx = (curX + startX) * 0.5;
73
+ const my = (curY + startY) * 0.5;
74
+ curves.push({ p0x: curX, p0y: curY, p1x: mx, p1y: my, p2x: startX, p2y: startY });
75
+ }
76
+ curX = startX;
77
+ curY = startY;
78
+ break;
79
+ }
80
+ }
81
+ }
82
+ if (curves.length === 0) {
83
+ return null;
84
+ }
85
+ const bounds = {
86
+ xMin: path.bounds.xMin,
87
+ yMin: path.bounds.yMin,
88
+ xMax: path.bounds.xMax,
89
+ yMax: path.bounds.yMax,
90
+ };
91
+ return { glyphId, curves, bounds };
92
+ }
93
+ /** Extract outlines for the requested glyph ids and add them to `target`.
94
+ * Skips ids already present in `target`. Glyphs with no outline are silently skipped.
95
+ * Mutates `target` directly — no allocation when no new glyphs appear. */
96
+ export function extractGlyphCurves(font, glyphIds, target) {
97
+ const cache = (font._curvesCache ??= new Map());
98
+ for (const id of glyphIds) {
99
+ if (target.has(id)) {
100
+ continue;
101
+ }
102
+ let entry = cache.get(id);
103
+ if (entry === undefined) {
104
+ const extracted = extractOne(font, id);
105
+ if (extracted == null) {
106
+ // Sentinel: cache the absence by skipping target; future calls re-skip via target.has.
107
+ continue;
108
+ }
109
+ entry = extracted;
110
+ cache.set(id, entry);
111
+ }
112
+ target.set(id, entry);
113
+ }
114
+ }
115
+ //# sourceMappingURL=glyph-extraction.js.map
@@ -0,0 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M,SAAS,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YACvC,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;gBACpB,uFAAuF;gBACvF,SAAS;YACb,CAAC;YACD,KAAK,GAAG,SAAS,CAAC;YAClB,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QACzB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IAC1B,CAAC;AACL,CAAC","sourcesContent":["/** Glyph outline extraction: the default `text-shaper`-backed pipeline that\n * produces `GLGlyphCurves` values from a `GLFont`'s glyph paths.\n *\n * The outline value types themselves (`GLQuadCurve`, `GLGlyphBounds`, `GLGlyphCurves`)\n * live in `glyph-storage.ts` because storage is what owns and persists them.\n * Callers using their own outline source (DirectWrite, FreeType, hand-rolled)\n * import those types directly from `glyph-storage.js` and never pull this\n * module — so `text-shaper` stays out of their bundle. The only helper they\n * may want is `cubicToQuadratics`, which has no dependency on text-shaper. */\n\nimport { getGlyphPath } from \"text-shaper\";\nimport type { GLFont } from \"./font.js\";\nimport type { GLGlyphBounds, GLGlyphCurves, GLQuadCurve } from \"./glyph-storage.js\";\n\n/** Approximate a cubic Bézier with two quadratics using the \"3/4 rule\" (matches Slug reference).\n * Exposed as a public helper so callers that ingest cubic outlines from their own font sources\n * (e.g. DirectWrite, FreeType) can convert into the quadratic-only format `GLGlyphCurves` expects. */\nexport function cubicToQuadratics(p0x: number, p0y: number, c1x: number, c1y: number, c2x: number, c2y: number, p1x: number, p1y: number): [GLQuadCurve, GLQuadCurve] {\n const q0cx = p0x + (c1x - p0x) * 0.75;\n const q0cy = p0y + (c1y - p0y) * 0.75;\n const q1cx = p1x + (c2x - p1x) * 0.75;\n const q1cy = p1y + (c2y - p1y) * 0.75;\n const midx = (q0cx + q1cx) * 0.5;\n const midy = (q0cy + q1cy) * 0.5;\n return [\n { p0x, p0y, p1x: q0cx, p1y: q0cy, p2x: midx, p2y: midy },\n { p0x: midx, p0y: midy, p1x: q1cx, p1y: q1cy, p2x: p1x, p2y: p1y },\n ];\n}\n\nfunction extractOne(font: GLFont, glyphId: number): GLGlyphCurves | null {\n const path = getGlyphPath(font._font, glyphId);\n if (!path || !path.bounds) {\n return null;\n }\n const curves: GLQuadCurve[] = [];\n let curX = 0;\n let curY = 0;\n let startX = 0;\n let startY = 0;\n for (const cmd of path.commands) {\n switch (cmd.type) {\n case \"M\":\n curX = cmd.x;\n curY = cmd.y;\n startX = cmd.x;\n startY = cmd.y;\n break;\n case \"L\": {\n const dx = cmd.x - curX;\n const dy = cmd.y - curY;\n if (Math.abs(dx) >= 0.1 || Math.abs(dy) >= 0.1) {\n const mx = (curX + cmd.x) * 0.5;\n const my = (curY + cmd.y) * 0.5;\n curves.push({ p0x: curX, p0y: curY, p1x: mx, p1y: my, p2x: cmd.x, p2y: cmd.y });\n }\n curX = cmd.x;\n curY = cmd.y;\n break;\n }\n case \"Q\":\n curves.push({ p0x: curX, p0y: curY, p1x: cmd.x1, p1y: cmd.y1, p2x: cmd.x, p2y: cmd.y });\n curX = cmd.x;\n curY = cmd.y;\n break;\n case \"C\": {\n const [q1, q2] = cubicToQuadratics(curX, curY, cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);\n curves.push(q1, q2);\n curX = cmd.x;\n curY = cmd.y;\n break;\n }\n case \"Z\": {\n const cdx = startX - curX;\n const cdy = startY - curY;\n if (Math.abs(cdx) > 0.1 || Math.abs(cdy) > 0.1) {\n const mx = (curX + startX) * 0.5;\n const my = (curY + startY) * 0.5;\n curves.push({ p0x: curX, p0y: curY, p1x: mx, p1y: my, p2x: startX, p2y: startY });\n }\n curX = startX;\n curY = startY;\n break;\n }\n }\n }\n if (curves.length === 0) {\n return null;\n }\n const bounds: GLGlyphBounds = {\n xMin: path.bounds.xMin,\n yMin: path.bounds.yMin,\n xMax: path.bounds.xMax,\n yMax: path.bounds.yMax,\n };\n return { glyphId, curves, bounds };\n}\n\n/** Extract outlines for the requested glyph ids and add them to `target`.\n * Skips ids already present in `target`. Glyphs with no outline are silently skipped.\n * Mutates `target` directly — no allocation when no new glyphs appear. */\nexport function extractGlyphCurves(font: GLFont, glyphIds: ReadonlySet<number>, target: Map<number, GLGlyphCurves>): void {\n const cache = (font._curvesCache ??= new Map());\n for (const id of glyphIds) {\n if (target.has(id)) {\n continue;\n }\n let entry = cache.get(id);\n if (entry === undefined) {\n const extracted = extractOne(font, id);\n if (extracted == null) {\n // Sentinel: cache the absence by skipping target; future calls re-skip via target.has.\n continue;\n }\n entry = extracted;\n cache.set(id, entry);\n }\n target.set(id, entry);\n }\n}\n"]}
@@ -0,0 +1,68 @@
1
+ /** Glyph storage: per-curve-set CPU outline catalog plus the GPU atlas packed from it.
2
+ *
3
+ * Layered:
4
+ * - `GLGlyphStorage` is the opaque public handle. Holds one or more curve-sets keyed by
5
+ * `GLCurveSetId` (typically a font family name). Each curve-set owns its glyph
6
+ * outlines plus the `SharedAtlas` packed from them.
7
+ * - `SharedAtlas` stages float32 curve control points and uint32 band headers/
8
+ * curve-reference lists in append-only RGBA and RG arrays, respectively.
9
+ * - Atlas packing (`packAppendGlyph`) and spatial-band partitioning (`buildGlyphBands`)
10
+ * live here as internal helpers — they implement the storage's invariants and are
11
+ * not callable from outside the module.
12
+ * - GPU creation/upload (`SharedAtlasGpu`, `ensureSharedAtlasGpu`) lives in
13
+ * `_gpu/text-textures.ts`; the shared types are exported from here. GPU teardown
14
+ * is performed inline in `disposeGlyphStorage` to avoid a circular import edge.
15
+ *
16
+ * Lifetime is caller-owned (matches `Texture2D` semantics):
17
+ * - `createGlyphStorage(initial?)` allocates a fresh storage, optionally seeded.
18
+ * - `updateGlyphStorage(storage, curveSetId, curves)` adds glyphs (creating the
19
+ * curve-set on demand). Glyph ids already present are skipped.
20
+ * - `disposeGlyphStorage(storage)` releases every atlas. The caller must ensure no
21
+ * `GLTextData` is still drawing from it — using a disposed storage is undefined
22
+ * behavior. Idempotent.
23
+ */
24
+ declare const glyphStorageBrand: unique symbol;
25
+ /** Quadratic Bézier curve describing one segment of a glyph outline in font units. */
26
+ export type GLQuadCurve = {
27
+ readonly p0x: number;
28
+ readonly p0y: number;
29
+ readonly p1x: number;
30
+ readonly p1y: number;
31
+ readonly p2x: number;
32
+ readonly p2y: number;
33
+ };
34
+ /** Axis-aligned glyph extents in font units, used to size the rendered quad and spatial curve bands. */
35
+ export type GLGlyphBounds = {
36
+ readonly xMin: number;
37
+ readonly yMin: number;
38
+ readonly xMax: number;
39
+ readonly yMax: number;
40
+ };
41
+ /** Complete outline data for one glyph id, ready to be packed into a `GLGlyphStorage` atlas. */
42
+ export type GLGlyphCurves = {
43
+ readonly glyphId: number;
44
+ readonly curves: readonly GLQuadCurve[];
45
+ readonly bounds: GLGlyphBounds;
46
+ };
47
+ /** Identifier for a curve set (a font's glyph-curves map). Strings let callers use a
48
+ * human-readable key (e.g. the font face name) for easy debugging. */
49
+ export type GLCurveSetId = string;
50
+ /** Opaque bundle of glyph outlines (organized by curve-set) and the GPU atlases packed
51
+ * from them. Holds an arbitrary number of curve-sets — each curve-set gets its own atlas.
52
+ * Shared by reference across any number of `GLTextData`s that need the same glyph catalog. */
53
+ export interface GLGlyphStorage {
54
+ readonly [glyphStorageBrand]: true;
55
+ }
56
+ /** Build a `GLGlyphStorage`. If `initial` is provided, each curve-set is packed into its
57
+ * own atlas synchronously. The passed inner maps are *adopted* by the storage — the
58
+ * caller must not mutate them directly afterward (use `updateGlyphStorage` instead). */
59
+ export declare function createGlyphStorage(initial?: Map<GLCurveSetId, Map<number, GLGlyphCurves>>): GLGlyphStorage;
60
+ /** Add glyphs to the named curve-set, creating it if it doesn't exist yet. Glyph ids
61
+ * already present in the curve-set are skipped (the existing outline + atlas slot wins).
62
+ * Safe to call between frames: the atlas grows in place and the next render uploads the
63
+ * new glyphs. */
64
+ export declare function updateGlyphStorage(storage: GLGlyphStorage, curveSetId: GLCurveSetId, curves: ReadonlyMap<number, GLGlyphCurves>): void;
65
+ /** Release every GPU atlas owned by `storage`. Idempotent. The caller is responsible for
66
+ * ensuring no `GLTextData` is still drawing from this storage. */
67
+ export declare function disposeGlyphStorage(storage: GLGlyphStorage): void;
68
+ export {};