pixi-silk 1.0.2 → 1.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,244 @@
1
+ import { INSTANCE_ATTRIBUTES } from "../constants.js";
2
+ import { toGlsl100 } from "./glsl100.js";
3
+ /** @internal Texels per instance in the instance texture (one vec4 attribute each). */
4
+ const TEXELS_PER_INSTANCE = 11;
5
+ /** @internal Width of the instance texture in texels. */
6
+ const INSTANCE_TEXTURE_WIDTH = 704;
7
+ /** @internal Most edges of a filled path, and most records of a union, the WebGL1 shader reads. */
8
+ const GL1_MAX_EDGES = 4096;
9
+ /** @internal Instance attributes the WebGL1 vertex stage still reads (8 attributes is the WebGL1 minimum). */
10
+ const GL1_VERTEX_ATTRIBUTES = /* @__PURE__ */ new Set([
11
+ "aBounds",
12
+ "aShape0",
13
+ "aShape2",
14
+ "aMeta",
15
+ "aFx"
16
+ ]);
17
+ const KEPT = GL1_VERTEX_ATTRIBUTES;
18
+ const silkVertexPatches = [
19
+ {
20
+ find: /^uniform highp sampler2D uBatch;\n/m,
21
+ replace: ""
22
+ },
23
+ {
24
+ find: /^vBase = 0\.0;\nint idx[\s\S]*?vBase = r1\.z;\n\}\n/m,
25
+ replace: ""
26
+ },
27
+ {
28
+ find: /^flat out .*\n/gm,
29
+ replace: ""
30
+ },
31
+ {
32
+ find: new RegExp(`^v(${[
33
+ "Shape0",
34
+ "Shape1",
35
+ "Shape2",
36
+ "Meta",
37
+ "Fill",
38
+ "Stroke",
39
+ "Grad",
40
+ "Paint",
41
+ "Dash",
42
+ "Fx",
43
+ "Tint",
44
+ "Inv",
45
+ "InvT",
46
+ "Base"
47
+ ].join("|")}) = .*\\n`, "gm"),
48
+ replace: ""
49
+ },
50
+ {
51
+ find: new RegExp(`^in vec4 (${INSTANCE_ATTRIBUTES.filter((a) => !KEPT.has(a)).join("|")});\\n`, "gm"),
52
+ replace: ""
53
+ },
54
+ {
55
+ find: "in vec2 aCorner;\n",
56
+ replace: "in vec2 aCorner;\nin float aIndex;\n"
57
+ },
58
+ {
59
+ find: "out vec2 vPos;\n",
60
+ replace: "out vec2 vPos;\nout float vIndex;\n"
61
+ },
62
+ {
63
+ find: "vPos = pos;\n",
64
+ replace: "vPos = pos;\nvIndex = aIndex;\n"
65
+ }
66
+ ];
67
+ function instanceSetup() {
68
+ const reads = INSTANCE_ATTRIBUTES.map((a, i) => a === "aBounds" ? "" : ` v${a.slice(1)} = instanceTexel(${i});\n`).join("");
69
+ return `// WebGL1: what the WebGL2 vertex stage passes in flat varyings, read here from uInstances
70
+ vec4 instanceTexel(int i)
71
+ {
72
+ int slot = int(vIndex + 0.5);
73
+ int row = slot / 64;
74
+ return texelFetch(uInstances, ivec2((slot - row * 64) * ${TEXELS_PER_INSTANCE} + i, row), 0);
75
+ }
76
+
77
+ void gl1Setup()
78
+ {
79
+ ${reads} vTint = uColor * uWorldColorAlpha;
80
+ vBase = 0.0;
81
+ mat3 m = uProjectionMatrix * uWorldTransformMatrix * uTransformMatrix;
82
+ float idet = 1.0 / (m[0].x * m[1].y - m[1].x * m[0].y);
83
+ mat2 ai = mat2(m[1].y, -m[0].y, -m[1].x, m[0].x) * idet;
84
+ vInv = vec4(ai[0], ai[1]);
85
+ vInvT = -(ai * m[2].xy);
86
+ }
87
+
88
+ `;
89
+ }
90
+ const silkFragmentPatches = [
91
+ {
92
+ find: /^flat in (vec4|vec2|float) (\w+);\n/gm,
93
+ replace: "$1 $2;\n"
94
+ },
95
+ {
96
+ find: "in float vProg;\n",
97
+ replace: `in float vProg;
98
+ in float vIndex;
99
+
100
+ uniform mat3 uProjectionMatrix;
101
+ uniform mat3 uWorldTransformMatrix;
102
+ uniform mat3 uTransformMatrix;
103
+ uniform vec4 uColor;
104
+ uniform vec4 uWorldColorAlpha;
105
+ uniform highp sampler2D uInstances;
106
+ `
107
+ },
108
+ {
109
+ find: /float sdPolygon6\(vec2 p, vec2 v\[6\], int n\)\n\{[\s\S]*?return s \* sqrt\(d\);\n\}/,
110
+ replace: `float sdPolygon6(vec2 p, vec2 v[6], int n)
111
+ {
112
+ // WebGL1 indexes arrays with loop indices only: walk the edges with the previous vertex in hand
113
+ vec2 prev = v[0];
114
+ for (int i = 0; i < 6; i++)
115
+ {
116
+ if (i == n - 1) prev = v[i];
117
+ }
118
+ float d = dot(p - v[0], p - v[0]);
119
+ float s = 1.0;
120
+ for (int i = 0; i < 6; i++)
121
+ {
122
+ if (i >= n) break;
123
+ vec2 vi = v[i];
124
+ vec2 e = prev - vi;
125
+ vec2 w = p - vi;
126
+ vec2 b = w - e * clamp(dot(w, e) / max(dot(e, e), 1e-12), 0.0, 1.0);
127
+ d = min(d, dot(b, b));
128
+ bvec3 c = bvec3(p.y >= vi.y, p.y < prev.y, e.x * w.y > e.y * w.x);
129
+ if (all(c) || all(not(c))) s = -s;
130
+ prev = vi;
131
+ }
132
+ return s * sqrt(d);
133
+ }`
134
+ },
135
+ {
136
+ find: ` P[k] = v + d1 * e + n1 * hw;
137
+ P[k + 1] = v + d1 * e + n1 * inner;
138
+ P[k + 2] = v - d0 * e + n0 * inner;
139
+ `,
140
+ replace: ` vec2 t0 = v + d1 * e + n1 * hw;
141
+ vec2 t1 = v + d1 * e + n1 * inner;
142
+ vec2 t2 = v - d0 * e + n0 * inner;
143
+ if (k == 2)
144
+ {
145
+ P[2] = t0;
146
+ P[3] = t1;
147
+ P[4] = t2;
148
+ P[5] = t2;
149
+ }
150
+ else
151
+ {
152
+ P[3] = t0;
153
+ P[4] = t1;
154
+ P[5] = t2;
155
+ }
156
+ `
157
+ },
158
+ {
159
+ find: "float closedSD(vec2 p, int type, float px)\n",
160
+ replace: `float gSt[8];
161
+
162
+ float stGet(int i)
163
+ {
164
+ float r = 0.0;
165
+ for (int j = 0; j < 8; j++)
166
+ {
167
+ if (j == i) r = gSt[j];
168
+ }
169
+ return r;
170
+ }
171
+
172
+ void stSet(int i, float v)
173
+ {
174
+ for (int j = 0; j < 8; j++)
175
+ {
176
+ if (j == i) gSt[j] = v;
177
+ }
178
+ }
179
+
180
+ float closedSD(vec2 p, int type, float px)
181
+ `
182
+ },
183
+ {
184
+ find: "float st[8];",
185
+ replace: ""
186
+ },
187
+ {
188
+ find: /\b(min|max)\((sp[^,()]*), (\d+)\)/g,
189
+ replace: "i$1($2, $3)"
190
+ },
191
+ {
192
+ find: /\bst\[([^\]]+)\] = ([^;]+);/g,
193
+ replace: "stSet($1, $2);"
194
+ },
195
+ {
196
+ find: /\bst\[([^\]]+)\]/g,
197
+ replace: "stGet($1)"
198
+ },
199
+ {
200
+ find: "void main(void)\n{",
201
+ replace: `${instanceSetup()}void main(void)\n{\ngl1Setup();`
202
+ }
203
+ ];
204
+ const rampPatches = [{
205
+ find: "uniform highp sampler2D uGradients;\n",
206
+ replace: `uniform highp sampler2D uGradients;\n\nvec4 rampLinear(float t, float row)
207
+ {
208
+ float x = t * 255.0;
209
+ float i = floor(x);
210
+ float v = (row + 0.5) / 256.0;
211
+ vec4 a = texture(uGradients, vec2((i + 0.5) / 256.0, v));
212
+ vec4 b = texture(uGradients, vec2((min(i + 1.0, 255.0) + 0.5) / 256.0, v));
213
+ return mix(a, b, x - i);
214
+ }
215
+ \n`
216
+ }, {
217
+ find: /textureLod\(uGradients, vec2\(\(t \* 255\.0 \+ 0\.5\) \/ 256\.0, \((\w+)\.y \+ 0\.5\) \/ 256\.0\), 0\.0\)/g,
218
+ replace: "rampLinear(t, $1.y)"
219
+ }];
220
+ /** Per-program conversion options, by program name. Programs not listed need only the general rules. */
221
+ const OPTIONS = {
222
+ "silk-sdf": {
223
+ vertex: { patches: silkVertexPatches },
224
+ fragment: {
225
+ patches: [...silkFragmentPatches, ...rampPatches],
226
+ caps: {
227
+ n: GL1_MAX_EDGES,
228
+ nAll: 256,
229
+ "s + cnt": 32
230
+ }
231
+ }
232
+ },
233
+ "silk-text": { fragment: { patches: rampPatches } }
234
+ };
235
+ /** @internal Converts the GLSL ES 3.00 stages of the program called `name` to GLSL ES 1.00. */
236
+ function gl1Sources(name, vertex, fragment) {
237
+ const options = OPTIONS[name] ?? {};
238
+ return {
239
+ vertex: toGlsl100(vertex, "vertex", options.vertex),
240
+ fragment: toGlsl100(fragment, "fragment", options.fragment)
241
+ };
242
+ }
243
+ //#endregion
244
+ export { GL1_MAX_EDGES, GL1_VERTEX_ATTRIBUTES, INSTANCE_TEXTURE_WIDTH, TEXELS_PER_INSTANCE, gl1Sources };
@@ -0,0 +1,41 @@
1
+ import { Renderer } from "pixi.js";
2
+ //#region src/webgl1.d.ts
3
+ /** Whether `renderer` is Pixi's WebGL renderer on a WebGL1 context. */
4
+ export declare function isWebGL1(renderer: Renderer): boolean;
5
+ /**
6
+ * Lets SilkGraphics, SilkText and Glass render on a WebGL1 context. Pixi falls back to WebGL1 when
7
+ * the browser has no WebGL2. This installs GLSL ES 1.00 versions of the shaders, which live in this
8
+ * module so that apps that leave it out do not ship them.
9
+ *
10
+ * Pass it to {@link createSilkApp} as `webgl1: enableWebGL1`, or call it with the renderer of an
11
+ * application you create yourself. It does nothing unless the renderer runs WebGL1. Call it before
12
+ * you create any SilkGraphics: objects created earlier keep the WebGL2 program and draw nothing.
13
+ *
14
+ * The device needs `OES_texture_float`, `OES_texture_half_float`, `OES_standard_derivatives` and
15
+ * highp in fragment shaders. Without one of them this logs an error, installs nothing and returns
16
+ * `false`: silk objects then draw nothing, and the app can show a fallback. Without
17
+ * `OES_texture_half_float_linear` the gradient ramps switch to nearest filtering, which the WebGL1
18
+ * shaders blend themselves, so they look the same.
19
+ *
20
+ * Limits on WebGL1: `enableSilkBatching()` has no effect, each object is one draw call. A filled
21
+ * path with more than 4096 edges, or a stroked path with more than 256 windows, draws in part with
22
+ * a warning. Programs are global: once enabled, objects on a WebGL2 renderer on the same page use
23
+ * the WebGL1 shaders too and do not batch. They give the same pixels.
24
+ *
25
+ * @param renderer - The renderer of the application.
26
+ * @returns `true` when WebGL1 support is on for this renderer. `false` when the renderer is not
27
+ * WebGL1, or the device lacks something the shaders need (an error is logged).
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { createSilkApp, SilkGraphics } from 'pixi-silk';
32
+ * import { enableWebGL1 } from 'pixi-silk/webgl1';
33
+ *
34
+ * const silk = await createSilkApp({ parent: document.body, webgl1: enableWebGL1 });
35
+ * silk.app.stage.addChild(new SilkGraphics().circle(50, 50, 40).fill(0x30d158));
36
+ * ```
37
+ */
38
+ export declare function enableWebGL1(renderer: Renderer): boolean;
39
+ /** Whether {@link enableWebGL1} has run on a WebGL1 renderer, so new SilkGraphics carry the WebGL1 program. */
40
+ export declare function isWebGL1Enabled(): boolean;
41
+ //#endregion
package/dist/webgl1.js ADDED
@@ -0,0 +1,104 @@
1
+ import { silkGl1 } from "./shader-slot.js";
2
+ import { createGl1Hooks } from "./webgl1/hooks.js";
3
+ //#region src/webgl1.ts
4
+ /** Whether `renderer` is Pixi's WebGL renderer on a WebGL1 context. */
5
+ function isWebGL1(renderer) {
6
+ return renderer.name === "webgl" && renderer.context.webGLVersion === 1;
7
+ }
8
+ /** `HALF_FLOAT_OES` of `OES_texture_half_float`. */
9
+ const HALF_FLOAT_OES = 36193;
10
+ /**
11
+ * The texture formats silk uploads, fixed for WebGL1. Pixi 8 maps them to WebGL2 formats
12
+ * (`RGBA32F`, `RGBA16F`, `R8`, `RED`), so the uploads fail on WebGL1. WebGL1 takes the base format
13
+ * as the internal format, with the float types of `OES_texture_float` and `OES_texture_half_float`.
14
+ * It has no one-channel `RED`, so the text atlas uploads as `LUMINANCE`. The shader reads it the
15
+ * same through `.r`. The tables are private to Pixi: returns `false` when they are not there.
16
+ */
17
+ function patchFormats(renderer) {
18
+ const gl = renderer.gl;
19
+ const t = renderer.texture;
20
+ const internal = t._mapFormatToInternalFormat;
21
+ const format = t._mapFormatToFormat;
22
+ const type = t._mapFormatToType;
23
+ if (!internal || !format || !type) return false;
24
+ internal.rgba32float = gl.RGBA;
25
+ internal.rgba16float = gl.RGBA;
26
+ type.rgba16float = HALF_FLOAT_OES;
27
+ internal.r8unorm = gl.LUMINANCE;
28
+ format.r8unorm = gl.LUMINANCE;
29
+ return true;
30
+ }
31
+ /** What the device lacks for the WebGL1 programs, or `null` when it has everything. */
32
+ function missing(renderer) {
33
+ const gl = renderer.gl;
34
+ const ext = renderer.context.extensions;
35
+ if (!ext.floatTexture) return "OES_texture_float";
36
+ if (!ext.textureHalfFloat) return "OES_texture_half_float";
37
+ if (!gl.getExtension("OES_standard_derivatives")) return "OES_standard_derivatives";
38
+ const high = gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.HIGH_FLOAT);
39
+ if (!high || high.precision === 0) return "highp precision in fragment shaders";
40
+ return null;
41
+ }
42
+ const prepared = /* @__PURE__ */ new WeakSet();
43
+ /**
44
+ * Lets SilkGraphics, SilkText and Glass render on a WebGL1 context. Pixi falls back to WebGL1 when
45
+ * the browser has no WebGL2. This installs GLSL ES 1.00 versions of the shaders, which live in this
46
+ * module so that apps that leave it out do not ship them.
47
+ *
48
+ * Pass it to {@link createSilkApp} as `webgl1: enableWebGL1`, or call it with the renderer of an
49
+ * application you create yourself. It does nothing unless the renderer runs WebGL1. Call it before
50
+ * you create any SilkGraphics: objects created earlier keep the WebGL2 program and draw nothing.
51
+ *
52
+ * The device needs `OES_texture_float`, `OES_texture_half_float`, `OES_standard_derivatives` and
53
+ * highp in fragment shaders. Without one of them this logs an error, installs nothing and returns
54
+ * `false`: silk objects then draw nothing, and the app can show a fallback. Without
55
+ * `OES_texture_half_float_linear` the gradient ramps switch to nearest filtering, which the WebGL1
56
+ * shaders blend themselves, so they look the same.
57
+ *
58
+ * Limits on WebGL1: `enableSilkBatching()` has no effect, each object is one draw call. A filled
59
+ * path with more than 4096 edges, or a stroked path with more than 256 windows, draws in part with
60
+ * a warning. Programs are global: once enabled, objects on a WebGL2 renderer on the same page use
61
+ * the WebGL1 shaders too and do not batch. They give the same pixels.
62
+ *
63
+ * @param renderer - The renderer of the application.
64
+ * @returns `true` when WebGL1 support is on for this renderer. `false` when the renderer is not
65
+ * WebGL1, or the device lacks something the shaders need (an error is logged).
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * import { createSilkApp, SilkGraphics } from 'pixi-silk';
70
+ * import { enableWebGL1 } from 'pixi-silk/webgl1';
71
+ *
72
+ * const silk = await createSilkApp({ parent: document.body, webgl1: enableWebGL1 });
73
+ * silk.app.stage.addChild(new SilkGraphics().circle(50, 50, 40).fill(0x30d158));
74
+ * ```
75
+ */
76
+ function enableWebGL1(renderer) {
77
+ if (!isWebGL1(renderer)) return false;
78
+ const r = renderer;
79
+ const lack = missing(r);
80
+ if (lack) {
81
+ console.error(`pixi-silk: this WebGL1 device lacks ${lack}, which SilkGraphics needs. Silk objects draw nothing.`);
82
+ return false;
83
+ }
84
+ if (!prepared.has(renderer)) {
85
+ if (!patchFormats(r)) {
86
+ console.error("pixi-silk: the format tables of Pixi texture system were not found (a Pixi version silk does not know). WebGL1 support is off.");
87
+ return false;
88
+ }
89
+ prepared.add(renderer);
90
+ r.runners.contextChange.add({ contextChange: () => {
91
+ r.gl.getExtension("OES_standard_derivatives");
92
+ patchFormats(r);
93
+ } });
94
+ }
95
+ silkGl1.hooks ??= createGl1Hooks();
96
+ if (!r.context.extensions.textureHalfFloatLinear) silkGl1.hooks.nearestRamps = true;
97
+ return true;
98
+ }
99
+ /** Whether {@link enableWebGL1} has run on a WebGL1 renderer, so new SilkGraphics carry the WebGL1 program. */
100
+ function isWebGL1Enabled() {
101
+ return silkGl1.hooks !== null;
102
+ }
103
+ //#endregion
104
+ export { enableWebGL1, isWebGL1, isWebGL1Enabled };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixi-silk",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "description": "Smooth vector graphics for PixiJS v8: analytic anti-aliased SDF primitives, squircles, arcs, polylines, dashes, OKLab gradients, glows and DPR-exact canvases, one draw call per object.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -167,6 +167,10 @@
167
167
  "types": "./dist/webgpu.d.ts",
168
168
  "default": "./dist/webgpu.js"
169
169
  },
170
+ "./webgl1": {
171
+ "types": "./dist/webgl1.d.ts",
172
+ "default": "./dist/webgl1.js"
173
+ },
170
174
  "./text": {
171
175
  "types": "./dist/text.d.ts",
172
176
  "default": "./dist/text.js"
@@ -191,15 +195,6 @@
191
195
  "publishConfig": {
192
196
  "access": "public"
193
197
  },
194
- "scripts": {
195
- "build": "tsdown",
196
- "test": "vitest run",
197
- "test:watch": "vitest",
198
- "bench": "node bench/primitives.mjs",
199
- "typecheck": "tsc && tsc -p tsconfig.build.json",
200
- "prepack": "node ../../scripts/stage-package-files.mjs",
201
- "postpack": "node ../../scripts/stage-package-files.mjs --clean"
202
- },
203
198
  "peerDependencies": {
204
199
  "pixi.js": "^8.6.0"
205
200
  },
@@ -242,5 +237,12 @@
242
237
  "retina",
243
238
  "hidpi",
244
239
  "typescript"
245
- ]
246
- }
240
+ ],
241
+ "scripts": {
242
+ "build": "tsdown",
243
+ "test": "vitest run",
244
+ "test:watch": "vitest",
245
+ "bench": "node bench/primitives.mjs",
246
+ "typecheck": "tsc && tsc -p tsconfig.build.json"
247
+ }
248
+ }
@@ -57,6 +57,7 @@ Import each from its own path. The root import does not re-export the others.
57
57
  |---|---|
58
58
  | `pixi-silk` | SilkGraphics, createSilkApp, gradients, colour and curve helpers, motion helpers (`damp`, `Spring`, `ease`), `enableSilkBatching`. |
59
59
  | `pixi-silk/webgpu` | `enableWebGPU` for your own WebGPU Application. |
60
+ | `pixi-silk/webgl1` | `enableWebGL1`, opt-in shaders for Pixi's WebGL1 fallback. |
60
61
  | `pixi-silk/glass` | `Glass`, a frosted pane. |
61
62
  | `pixi-silk/glow` | `Glow`, `Aurora`, `neon`, `halo`, rays. |
62
63
  | `pixi-silk/text` | `SilkText`, `SdfAtlas`, `silkLabel`, `layoutText`, caret helpers. |
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: pixi-silk-text
3
- description: "Use this skill when drawing text with pixi-silk in PixiJS v8: SilkText from pixi-silk/text. Covers SilkText options and setters (fontFamily, fontSize, fontWeight, fill colour or gradient, letterSpacing, lineHeight, align, wordWrap, tabularNumbers, kerning, outline, shadow, dilate, aaWidth, anchor), set(), the shared SdfAtlas and custom atlases (size, radius), silkLabel for widgets, caretAt, caretRect, rangeRects, layoutText. Triggers on: SilkText, sharp text, SDF text, distance field text, text blurry when zoomed, text outline, text glow, tabular numbers, SdfAtlas, silkLabel, pixi-silk/text."
3
+ description: "Use this skill when drawing text with pixi-silk in PixiJS v8: SilkText from pixi-silk/text. Covers SilkText options and setters (fontFamily, fontSize, fontWeight, fill colour or gradient, letterSpacing, lineHeight, align, wordWrap, tabularNumbers, kerning, outline, shadow, dilate, aaWidth, anchor, direction, wordBreak, inlines, maxLines, maxWidth, maxHeight, ellipsis, shrinkToFit, tabSize), set(), right-to-left and complex scripts (Arabic, Hebrew, Burmese, Thai, Indic, Chinese, Japanese, Korean), SilkRichText for sprites and icons inside text, the shared SdfAtlas and custom atlases (size, radius), silkLabel and silkRichLabel for widgets, caretAt, caretRect, rangeRects, layoutText. Triggers on: SilkText, sharp text, text overflow, ellipsis, shrink to fit, justify, SDF text, distance field text, text blurry when zoomed, text outline, text glow, tabular numbers, Arabic text, RTL, clipped marks, icon in text, sprite in text, SdfAtlas, silkLabel, pixi-silk/text."
4
4
  license: MIT
5
5
  ---
6
6
 
@@ -61,6 +61,52 @@ console.log(hit.index, caret.x, note.rangeRects(0, 5)); // selection boxes for c
61
61
 
62
62
  For an editable field, use `TextEditor` from `pixi-silk/ui/textedit` (see the pixi-silk-editor skill).
63
63
 
64
+ ### Icons and sprites inside text
65
+
66
+ ```ts
67
+ import { Sprite, Texture } from 'pixi.js';
68
+ import { SilkGraphics } from 'pixi-silk';
69
+ import { SilkRichText } from 'pixi-silk/text';
70
+
71
+ const status = new SilkRichText({
72
+ text: 'Synced {dot} 2 min ago, rated {star} 4.8',
73
+ fontSize: 24,
74
+ inlines: {
75
+ dot: { object: new Sprite(Texture.WHITE), height: 'cap', margin: 2 },
76
+ star: { object: () => new SilkGraphics().star(0, 0, 5, 10, 4).fill(0xffd60a), height: 'em', align: 'center' },
77
+ },
78
+ });
79
+
80
+ app.stage.addChild(status);
81
+ status.textObject.wordWrap = true; // style the text through textObject
82
+ ```
83
+
84
+ `{name}` takes a box that flows, wraps and follows right-to-left order like a character. `align` is `middle` (centred on capital letters and digits, the default), `center`, `baseline`, `top` or `bottom`. Pass a function to show an object more than once. `anchor` moves the objects with the text. A box sticks to the letters and digits next to it. `TextEditor` takes the same `inlines`: typed tokens become objects, and one Backspace deletes a token. Plain `SilkText` takes the same boxes as `inlines: { icon: { width, height } }` and reports them through `inlineRects()`.
85
+
86
+ ### Any script
87
+
88
+ ```ts
89
+ import { SilkText } from 'pixi-silk/text';
90
+
91
+ const ar = new SilkText({ text: 'السعر 1,234.56', fontSize: 20 }); // right to left, number stays in order
92
+ const my = new SilkText({ text: 'မင်္ဂလာပါ ကမ္ဘာ', fontSize: 20, wordWrap: true, wordWrapWidth: 200 });
93
+ const ko = new SilkText({ text: '변경 사항은 저장 버튼을 누른 뒤에 적용됩니다', wordBreak: 'keep-all', wordWrap: true, wordWrapWidth: 160 });
94
+ ```
95
+
96
+ Arabic, Hebrew, Burmese, Thai, Khmer, the Indic scripts and emoji sequences shape, and emoji keep their colours. Wrap inserted values of unknown direction in U+2068 and U+2069. Lines grow to fit stacked marks, so nothing is cut or overlaps the next line. Chinese and Japanese wrap between characters, Thai and Burmese between words.
97
+
98
+ ### Text that has to fit a box
99
+
100
+ ```ts
101
+ import { SilkText } from 'pixi-silk/text';
102
+
103
+ const title = new SilkText({ text: 'Settings', fontSize: 24, maxWidth: 160, shrinkToFit: true, minFontSize: 14 });
104
+ const note = new SilkText({ text: 'A long description', wordWrap: true, wordWrapWidth: 200, maxLines: 2 });
105
+ const body = new SilkText({ text: 'Paragraph', wordWrap: true, wordWrapWidth: 300, align: 'justify' });
106
+ ```
107
+
108
+ `maxLines`, `maxWidth` and `maxHeight` cut what does not fit and end it in `ellipsis` (`null` for no mark). `shrinkToFit` shrinks the font first, down to `minFontSize`. `renderedFontSize` and `truncated` report what happened. `justify` uses kashida in Arabic (`kashida: false` to turn it off). `tabSize` sets tab stops.
109
+
64
110
  ### Sharp labels in widgets
65
111
 
66
112
  ```ts
@@ -72,7 +118,7 @@ const pill = new ValuePill({ label: silkLabel({ tabularNumbers: true }) });
72
118
  pill.show('42 px');
73
119
  ```
74
120
 
75
- Widgets draw their labels with Pixi `Text` by default. `silkLabel()` swaps in SilkText, which stays sharp under a zooming camera.
121
+ Widgets draw their labels with Pixi `Text` by default. `silkLabel()` swaps in SilkText, which stays sharp under a zooming camera. `silkRichLabel({ up: { object, height: 'cap' } })` makes labels that can show `{up}` icons, and the widget sizes itself to them.
76
122
 
77
123
  ### A bigger atlas for large glows
78
124
 
@@ -115,6 +161,14 @@ world.scale.set(8); // still sharp
115
161
 
116
162
  Symptom: a large `shadow.blur` or `outline.width` is clipped to a hard edge. The default atlas field reaches about a quarter of the font size. Give the text an `SdfAtlas` with a larger `radius`.
117
163
 
164
+ ### [MEDIUM] Raising `lineHeight` or `padding` per language to stop clipped marks
165
+
166
+ Symptom: Burmese, Thai or Vietnamese marks cut at the top or overlapping the next line, with Pixi `Text`. SilkText finds the ink of every glyph and grows the line box to fit it, outlines included. Leave `lineHeight` at 0. A fixed `lineHeight` is kept as given, so tall stacks can then touch.
167
+
168
+ ### [MEDIUM] Adding a sprite as a child of SilkText
169
+
170
+ Symptom: the icon does not move when the text changes, or Pixi warns that a leaf cannot have children. Use `SilkRichText` with `{name}` tokens, which places the object in the text and moves it with every change.
171
+
118
172
  ### [MEDIUM] Expecting `\n` handling from `wordWrap` alone
119
173
 
120
174
  Symptom: one long line. `wordWrap` only breaks when `wordWrapWidth` is set to the width you want (default 100). `\n` always starts a new line.
@@ -123,5 +177,6 @@ Symptom: one long line. `wordWrap` only breaks when `wordWrapWidth` is set to th
123
177
 
124
178
  - [pixi-silk/text](https://pixi-silk.schmooky.dev/api/text.md)
125
179
  - [SilkText](https://pixi-silk.schmooky.dev/api/text/classes/SilkText.md), [SilkTextStyle](https://pixi-silk.schmooky.dev/api/text/interfaces/SilkTextStyle.md), [SilkTextOptions](https://pixi-silk.schmooky.dev/api/text/interfaces/SilkTextOptions.md)
180
+ - [SilkRichText](https://pixi-silk.schmooky.dev/api/text/classes/SilkRichText.md), [RichInline](https://pixi-silk.schmooky.dev/api/text/interfaces/RichInline.md)
126
181
  - [SdfAtlas](https://pixi-silk.schmooky.dev/api/text/classes/SdfAtlas.md), [silkLabel](https://pixi-silk.schmooky.dev/api/text/functions/silkLabel.md)
127
182
  - Guides: [Text](https://pixi-silk.schmooky.dev/docs/text.md), [Editing text](https://pixi-silk.schmooky.dev/docs/editing-text.md)