pixi-silk 1.0.1 → 1.1.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # pixi-silk
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - SilkText: text that fits its box, full bidi, colour emoji. `maxLines`, `maxWidth` and `maxHeight` cut what does not fit and end it in `ellipsis`, and `shrinkToFit` shrinks the font first, down to `minFontSize` (`renderedFontSize` and `truncated` report it). `align: 'justify'` stretches wrapped lines, with kashida in Arabic. Tabs line up on `tabSize` stops. Letter spacing is not applied inside Arabic words or words of shaped scripts. The bidi pass now handles isolates, embeddings and overrides, and pairs brackets. A caret can stand at both places an offset has where the direction changes, and word jumps follow the screen. Emoji keep their colours, from a second texture. Thai and Lao are baked a cluster at a time, and `atlas.usage` reports how full the atlas is. Icons centre on the letters in text without capitals.
8
+
9
+ - Inline objects and editing polish. `silkRichLabel()` gives widgets labels that hold icons. `SilkRichText` has an `anchor`, and its objects follow the anchor and the text object's position and scale. `TextEditor` takes `inlines`: typed `{name}` tokens show their object, the caret steps over a token and one Backspace deletes it. The arrow keys move by what is on screen in lines that mix directions (`caretVisual`). Lines break beside inline boxes next to Chinese, Japanese or Korean text, and a box stays with the letters and digits next to it. `tabularNumbers` covers every decimal digit set, and digits of Burmese, Thai and the Indic scripts are clusters of their own.
10
+
11
+ - SilkText lays out right-to-left text. Arabic and Hebrew paragraphs are reordered with the Unicode bidi algorithm, so numbers, prices, percentages and Latin words inside them keep reading left to right, and brackets are mirrored. Joined Arabic letters are baked as one glyph per run, so they shape with the font. New `direction` style (`ltr`, `rtl`, `auto`), and `align` takes `start` and `end` (the default is now `start`, which is `left` for left-to-right text). Carets, hit testing and selection boxes follow the visual order, and the left arrow in `TextEditor` moves forward in a right-to-left paragraph.
12
+
13
+ - SilkText handles every script without clipping. Glyphs are baked from the ink found in the pixels, not the `measureText` box, so stacked Burmese, Thai and Vietnamese marks are never cut. Lines grow to fit the tallest ink and the outline. Burmese, Thai, Khmer, Lao, the Indic scripts, emoji sequences and combining accents are baked a word or a cluster at a time so Canvas shapes them. Lines wrap between Chinese and Japanese characters (with kinsoku), Korean syllables (or only at spaces with the new `wordBreak: 'keep-all'`) and Thai or Burmese words. Outlines are drawn for every glyph before any fill, so they never cover a neighbouring letter. New inline objects: `{name}` tokens with `inlines` reserve aligned boxes (`inlineRects()`), and the new `SilkRichText` places sprites, SilkGraphics or any container in them.
14
+
15
+ ## 1.0.2
16
+
17
+ ### Patch Changes
18
+
19
+ - [#14](https://github.com/schmooky/pixi-silk/pull/14) [`2bc3024`](https://github.com/schmooky/pixi-silk/commit/2bc30243a749b18c02baeb07bc1c9a9c6120c878) Thanks [@igaming-bulochka](https://github.com/igaming-bulochka)! - `TransformGizmo.grab()` now ends its drag on the next release wherever it happens. Before, a drag started from a press on another object only ended when the release reached the gizmo itself, so the target kept following the pointer after the button was let go. The gizmo now listens for `pointerup`, `pointerupoutside` and `pointercancel` on the pressed object for the length of the drag, and also ends it when the window loses focus or cancels the pointer.
20
+
3
21
  ## 1.0.1
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -58,8 +58,7 @@ may change in a minor release.
58
58
  `createSilkApp`, or call `enableWebGPU()` from `pixi-silk/webgpu` before the first
59
59
  `SilkGraphics` in your own Pixi app
60
60
 
61
- It does not do texture fills or SVG documents, and `SilkText` has no shaping for right-to-left
62
- scripts. The [FAQ](https://pixi-silk.schmooky.dev/faq/) lists the other limits.
61
+ It does not do texture fills or SVG documents. The [FAQ](https://pixi-silk.schmooky.dev/faq/) lists the other limits.
63
62
 
64
63
  ## For AI agents
65
64
 
@@ -1,5 +1,7 @@
1
1
  import { Gradient } from "../gradient.js";
2
2
  import { SdfAtlas } from "./atlas.js";
3
+ import { TextDirection } from "./bidi.js";
4
+ import { WordBreak } from "./cluster.js";
3
5
  import { CaretHit, CaretRect, LayoutLine, TextAlign, TextLayout, TextRect } from "./layout.js";
4
6
  import { Bounds, Buffer, ColorSource, DestroyOptions, Geometry, Mesh, ObservablePoint, PointData, Shader } from "pixi.js";
5
7
  //#region src/text/SilkText.d.ts
@@ -41,8 +43,14 @@ export interface SilkTextStyle {
41
43
  letterSpacing?: number;
42
44
  /** Distance between baselines. 0 uses the font's ascent plus descent, like PixiJS `Text`. Default 0. */
43
45
  lineHeight?: number;
44
- /** Alignment of the lines. Default `left`. */
46
+ /** Alignment of the lines. `start` is left for left-to-right paragraphs and right for right-to-left ones. Default `start`. */
45
47
  align?: TextAlign;
48
+ /**
49
+ * Base direction of each paragraph: `ltr`, `rtl`, or `auto` to take it from the first strong
50
+ * character. Arabic and Hebrew read right to left either way. Numbers and Latin words inside them
51
+ * keep their order. Default `auto`.
52
+ */
53
+ direction?: TextDirection;
46
54
  /** Breaks lines at spaces to fit `wordWrapWidth`. Default false. */
47
55
  wordWrap?: boolean;
48
56
  /** Width that `wordWrap` breaks lines at, in local units. Default 100. */
@@ -61,6 +69,75 @@ export interface SilkTextStyle {
61
69
  dilate?: number;
62
70
  /** Width of the anti-aliasing ramp in device pixels. Larger is softer. Default 1. */
63
71
  aaWidth?: number;
72
+ /**
73
+ * Where lines may break without spaces. `normal` breaks between Chinese, Japanese and Korean
74
+ * characters, `keep-all` keeps them together up to the next space. Default `normal`.
75
+ */
76
+ wordBreak?: WordBreak;
77
+ /**
78
+ * Room for inline objects, by name. `{icon}` in the text, with `icon` listed here, becomes a box
79
+ * that flows with the text. Read the boxes back with {@link SilkText.inlineRects}, or use
80
+ * {@link SilkRichText} to place sprites and graphics in them. Default none.
81
+ */
82
+ inlines?: Record<string, TextInline>;
83
+ /** Most lines to show. Past it the text is cut and the last line ends in `ellipsis`. 0 for no limit. Default 0. */
84
+ maxLines?: number;
85
+ /**
86
+ * Widest the text may be, in local units, when `wordWrap` is off. A longer line is cut and ends
87
+ * in `ellipsis`, or with `shrinkToFit` the font shrinks first. 0 for no limit. Default 0.
88
+ */
89
+ maxWidth?: number;
90
+ /** Tallest the text box may be, in local units. Lines past it are cut, or the font shrinks first. 0 for no limit. Default 0. */
91
+ maxHeight?: number;
92
+ /** What a cut line ends with. `null` cuts without a mark. Default an ellipsis, U+2026. */
93
+ ellipsis?: string | null;
94
+ /**
95
+ * Shrinks the font, down to `minFontSize`, until the text fits `maxWidth`, `maxHeight` and
96
+ * `maxLines`. What still does not fit is cut. Wrapping moves with the size. Default false.
97
+ */
98
+ shrinkToFit?: boolean;
99
+ /** Smallest size `shrinkToFit` goes down to, in local units. 0 for half the font size. Default 0. */
100
+ minFontSize?: number;
101
+ /** Tab stops every this many space widths, measured from the start of the line. Default 8. */
102
+ tabSize?: number;
103
+ }
104
+ /** How an inline box sits on the line. */
105
+ export type InlineAlign = "baseline" | "middle" | "center" | "top" | "bottom";
106
+ /** A box for an inline object, as {@link SilkTextStyle.inlines} lists them. Sizes are in local units. */
107
+ export interface TextInline {
108
+ /** Width of the object. */
109
+ width: number;
110
+ /** Height of the object. */
111
+ height: number;
112
+ /**
113
+ * Vertical placement. `middle` centres the box on the middle of capital letters and digits, the
114
+ * place for icons next to numbers. In text with neither, such as Arabic, Chinese or lower case,
115
+ * it centres on the middle of the letters. `center` centres it on the line box. `baseline` puts
116
+ * its bottom on the baseline, `top` and `bottom` align it with the font's ascent and descent.
117
+ * Default `middle`.
118
+ */
119
+ align?: InlineAlign;
120
+ /** Moves the box down (positive) or up after alignment. Default 0. */
121
+ offsetY?: number;
122
+ /** Space left and right of the box. Default 0. */
123
+ margin?: number;
124
+ }
125
+ /** Where an inline object goes, as {@link SilkText.inlineRects} returns them, in local units. */
126
+ export interface InlineRect {
127
+ /** Name of the inline, as written between braces. */
128
+ name: string;
129
+ /** Left edge of the object, margin excluded. */
130
+ x: number;
131
+ /** Top edge of the object. */
132
+ y: number;
133
+ /** Width of the object. */
134
+ width: number;
135
+ /** Height of the object. */
136
+ height: number;
137
+ /** Line it sits on. */
138
+ line: number;
139
+ /** Offset of its `{name}` token in the text. */
140
+ index: number;
64
141
  }
65
142
  /** Options of a {@link SilkText}: the style plus the text, anchor and atlas. */
66
143
  export interface SilkTextOptions extends SilkTextStyle {
@@ -102,9 +179,12 @@ export declare class SilkTextGeometry extends Geometry {
102
179
  * `measureText`, so line breaks and widths match PixiJS `Text`. Style changes relayout once, on the
103
180
  * next render or the next read of {@link SilkText.layout}.
104
181
  *
105
- * It has no rich text, per-character styles or right-to-left shaping. Glyphs combine code points
106
- * one by one, so complex scripts and emoji sequences do not shape. Works with Pixi's WebGL2 and
107
- * WebGPU renderers.
182
+ * Any common script works. Arabic and Hebrew paragraphs are reordered with the Unicode bidi
183
+ * algorithm, and numbers and Latin words inside them keep reading left to right. Words of scripts
184
+ * that need shaping (Arabic, Burmese, Thai, the Indic scripts) are baked whole, so Canvas shapes
185
+ * them, and lines grow to fit stacked marks. `{name}` tokens make room for inline objects, see
186
+ * {@link SilkRichText}. Emoji keep their colours. It has no per-character styles. Works with Pixi's
187
+ * WebGL2 and WebGPU renderers.
108
188
  *
109
189
  * @example
110
190
  * ```ts
@@ -127,6 +207,9 @@ export declare class SilkText extends Mesh<SilkTextGeometry, Shader> {
127
207
  private _version;
128
208
  private _f;
129
209
  private _count;
210
+ private _passes;
211
+ private _size;
212
+ private _inlineBoxes;
130
213
  private readonly _uniforms;
131
214
  private _gradKey;
132
215
  private readonly _scratch;
@@ -175,6 +258,10 @@ export declare class SilkText extends Mesh<SilkTextGeometry, Shader> {
175
258
  get align(): TextAlign;
176
259
  /** Sets {@link SilkText.align}. */
177
260
  set align(v: TextAlign);
261
+ /** Base direction of each paragraph: `ltr`, `rtl` or `auto` (from the first strong character). */
262
+ get direction(): TextDirection;
263
+ /** Sets {@link SilkText.direction}. */
264
+ set direction(v: TextDirection);
178
265
  /** Breaks lines at spaces to fit {@link SilkText.wordWrapWidth}. */
179
266
  get wordWrap(): boolean;
180
267
  /** Sets {@link SilkText.wordWrap}. */
@@ -223,14 +310,62 @@ export declare class SilkText extends Mesh<SilkTextGeometry, Shader> {
223
310
  * take, for example to grapheme cluster boundaries.
224
311
  */
225
312
  caretAt(point: PointData, isStop?: (index: number) => boolean): CaretHit;
226
- /** Where a caret at `index` sits, in local units. `upstream` picks the end of a wrapped line. */
227
- caretRect(index: number, upstream?: boolean): CaretRect;
313
+ /**
314
+ * Where a caret at `index` sits, in local units. `upstream` picks the end of a wrapped line,
315
+ * `leading` the place before the next character where the direction changes.
316
+ */
317
+ caretRect(index: number, upstream?: boolean, leading?: boolean): CaretRect;
228
318
  /** Highlight boxes of the text from `from` to `to`, one per line, in local units. */
229
319
  rangeRects(from: number, to: number): TextRect[];
230
320
  /** The laid-out lines: text, width and baseline of each, in layout units (see {@link SilkText.layout}). Relayouts first if needed. */
231
321
  get lines(): readonly LayoutLine[];
232
322
  /** Glyph instances drawn: characters with ink. Spaces, shadows and outlines are not counted. */
233
323
  get glyphCount(): number;
324
+ /**
325
+ * Where the inline objects of the text go, one rectangle per `{name}` token, in local units.
326
+ * Bidi and wrapping move them with the text. See {@link SilkTextStyle.inlines}.
327
+ */
328
+ inlineRects(): InlineRect[];
329
+ /** Room for inline objects, by name. Set a new object to change it, mutating this one does nothing. */
330
+ get inlines(): Record<string, TextInline>;
331
+ /** Sets {@link SilkText.inlines}. */
332
+ set inlines(v: Record<string, TextInline>);
333
+ /** Most lines to show, 0 for no limit. See {@link SilkTextStyle.maxLines}. */
334
+ get maxLines(): number;
335
+ /** Sets {@link SilkText.maxLines}. */
336
+ set maxLines(v: number);
337
+ /** Widest the text may be when it does not wrap, 0 for no limit. See {@link SilkTextStyle.maxWidth}. */
338
+ get maxWidth(): number;
339
+ /** Sets {@link SilkText.maxWidth}. */
340
+ set maxWidth(v: number);
341
+ /** Tallest the text box may be, 0 for no limit. See {@link SilkTextStyle.maxHeight}. */
342
+ get maxHeight(): number;
343
+ /** Sets {@link SilkText.maxHeight}. */
344
+ set maxHeight(v: number);
345
+ /** What a cut line ends with, or `null` for no mark. */
346
+ get ellipsis(): string | null;
347
+ /** Sets {@link SilkText.ellipsis}. */
348
+ set ellipsis(v: string | null);
349
+ /** Shrinks the font to fit `maxWidth`, `maxHeight` and `maxLines`. See {@link SilkTextStyle.shrinkToFit}. */
350
+ get shrinkToFit(): boolean;
351
+ /** Sets {@link SilkText.shrinkToFit}. */
352
+ set shrinkToFit(v: boolean);
353
+ /** Smallest size `shrinkToFit` goes to, 0 for half the font size. */
354
+ get minFontSize(): number;
355
+ /** Sets {@link SilkText.minFontSize}. */
356
+ set minFontSize(v: number);
357
+ /** Tab stops every this many space widths. */
358
+ get tabSize(): number;
359
+ /** Sets {@link SilkText.tabSize}. */
360
+ set tabSize(v: number);
361
+ /** The font size the text is drawn at: `fontSize`, or smaller when `shrinkToFit` had to shrink it. */
362
+ get renderedFontSize(): number;
363
+ /** True when the text did not fit its limits and was cut. */
364
+ get truncated(): boolean;
365
+ /** Where lines may break without spaces: `normal` or `keep-all`. */
366
+ get wordBreak(): WordBreak;
367
+ /** Sets {@link SilkText.wordBreak}. */
368
+ set wordBreak(v: WordBreak);
234
369
  override get bounds(): Bounds;
235
370
  /** Hit test against the text box (local coordinates). */
236
371
  override containsPoint(point: PointData): boolean;
@@ -240,6 +375,8 @@ export declare class SilkText extends Mesh<SilkTextGeometry, Shader> {
240
375
  */
241
376
  updateText(): void;
242
377
  override destroy(options?: DestroyOptions): void;
378
+ /** The inline boxes in layout terms: advance with margins, and the height above and below the baseline. */
379
+ private _resolveInlines;
243
380
  private _set;
244
381
  private _invalidate;
245
382
  private _build;
@@ -90,9 +90,12 @@ const premultiply = (out, color, alpha) => {
90
90
  * `measureText`, so line breaks and widths match PixiJS `Text`. Style changes relayout once, on the
91
91
  * next render or the next read of {@link SilkText.layout}.
92
92
  *
93
- * It has no rich text, per-character styles or right-to-left shaping. Glyphs combine code points
94
- * one by one, so complex scripts and emoji sequences do not shape. Works with Pixi's WebGL2 and
95
- * WebGPU renderers.
93
+ * Any common script works. Arabic and Hebrew paragraphs are reordered with the Unicode bidi
94
+ * algorithm, and numbers and Latin words inside them keep reading left to right. Words of scripts
95
+ * that need shaping (Arabic, Burmese, Thai, the Indic scripts) are baked whole, so Canvas shapes
96
+ * them, and lines grow to fit stacked marks. `{name}` tokens make room for inline objects, see
97
+ * {@link SilkRichText}. Emoji keep their colours. It has no per-character styles. Works with Pixi's
98
+ * WebGL2 and WebGPU renderers.
96
99
  *
97
100
  * @example
98
101
  * ```ts
@@ -111,7 +114,8 @@ var SilkText = class SilkText extends Mesh {
111
114
  fill: 16777215,
112
115
  letterSpacing: 0,
113
116
  lineHeight: 0,
114
- align: "left",
117
+ align: "start",
118
+ direction: "auto",
115
119
  wordWrap: false,
116
120
  wordWrapWidth: 100,
117
121
  breakWords: false,
@@ -120,7 +124,16 @@ var SilkText = class SilkText extends Mesh {
120
124
  outline: null,
121
125
  shadow: null,
122
126
  dilate: 0,
123
- aaWidth: 1
127
+ aaWidth: 1,
128
+ wordBreak: "normal",
129
+ inlines: {},
130
+ maxLines: 0,
131
+ maxWidth: 0,
132
+ maxHeight: 0,
133
+ ellipsis: "…",
134
+ shrinkToFit: false,
135
+ minFontSize: 0,
136
+ tabSize: 8
124
137
  };
125
138
  /** Point of the text box placed at the position: (0, 0) top left, (0.5, 0.5) centre, (1, 1) bottom right. Changing it relayouts. */
126
139
  anchor;
@@ -133,6 +146,9 @@ var SilkText = class SilkText extends Mesh {
133
146
  _version = -1;
134
147
  _f;
135
148
  _count = 0;
149
+ _passes = 1;
150
+ _size = 0;
151
+ _inlineBoxes = {};
136
152
  _uniforms;
137
153
  _gradKey = null;
138
154
  _scratch = /* @__PURE__ */ new Float32Array(4);
@@ -142,7 +158,7 @@ var SilkText = class SilkText extends Mesh {
142
158
  const opts = typeof options === "string" ? { text: options } : options;
143
159
  const atlas = opts.atlas ?? SdfAtlas.shared;
144
160
  const geometry = new SilkTextGeometry();
145
- const shader = createTextShader(atlas.source);
161
+ const shader = createTextShader(atlas.source, atlas.colorSource);
146
162
  super({
147
163
  geometry,
148
164
  shader,
@@ -252,6 +268,14 @@ var SilkText = class SilkText extends Mesh {
252
268
  set align(v) {
253
269
  this._set("align", v);
254
270
  }
271
+ /** Base direction of each paragraph: `ltr`, `rtl` or `auto` (from the first strong character). */
272
+ get direction() {
273
+ return this._style.direction;
274
+ }
275
+ /** Sets {@link SilkText.direction}. */
276
+ set direction(v) {
277
+ this._set("direction", v);
278
+ }
255
279
  /** Breaks lines at spaces to fit {@link SilkText.wordWrapWidth}. */
256
280
  get wordWrap() {
257
281
  return this._style.wordWrap;
@@ -348,9 +372,12 @@ var SilkText = class SilkText extends Mesh {
348
372
  const layout = this.layout;
349
373
  return caretAtPoint(layout, point.x - this._ox, point.y - this._oy, isStop);
350
374
  }
351
- /** Where a caret at `index` sits, in local units. `upstream` picks the end of a wrapped line. */
352
- caretRect(index, upstream = false) {
353
- const r = caretRect(this.layout, index, upstream);
375
+ /**
376
+ * Where a caret at `index` sits, in local units. `upstream` picks the end of a wrapped line,
377
+ * `leading` the place before the next character where the direction changes.
378
+ */
379
+ caretRect(index, upstream = false, leading = false) {
380
+ const r = caretRect(this.layout, index, upstream, leading);
354
381
  return {
355
382
  x: r.x + this._ox,
356
383
  top: r.top + this._oy,
@@ -375,7 +402,110 @@ var SilkText = class SilkText extends Mesh {
375
402
  /** Glyph instances drawn: characters with ink. Spaces, shadows and outlines are not counted. */
376
403
  get glyphCount() {
377
404
  this.updateText();
378
- return this._style.shadow ? this._count / 2 : this._count;
405
+ return this._count / this._passes;
406
+ }
407
+ /**
408
+ * Where the inline objects of the text go, one rectangle per `{name}` token, in local units.
409
+ * Bidi and wrapping move them with the text. See {@link SilkTextStyle.inlines}.
410
+ */
411
+ inlineRects() {
412
+ const layout = this.layout;
413
+ const out = [];
414
+ for (const g of layout.glyphs) {
415
+ const box = g.inline ? this._inlineBoxes[g.inline] : void 0;
416
+ if (!g.inline || !box) continue;
417
+ out.push({
418
+ name: g.inline,
419
+ x: this._ox + g.x + box.margin,
420
+ y: this._oy + g.y - box.top,
421
+ width: box.width,
422
+ height: box.height,
423
+ line: g.line,
424
+ index: g.index
425
+ });
426
+ }
427
+ return out;
428
+ }
429
+ /** Room for inline objects, by name. Set a new object to change it, mutating this one does nothing. */
430
+ get inlines() {
431
+ return this._style.inlines;
432
+ }
433
+ /** Sets {@link SilkText.inlines}. */
434
+ set inlines(v) {
435
+ this._set("inlines", v);
436
+ }
437
+ /** Most lines to show, 0 for no limit. See {@link SilkTextStyle.maxLines}. */
438
+ get maxLines() {
439
+ return this._style.maxLines;
440
+ }
441
+ /** Sets {@link SilkText.maxLines}. */
442
+ set maxLines(v) {
443
+ this._set("maxLines", v);
444
+ }
445
+ /** Widest the text may be when it does not wrap, 0 for no limit. See {@link SilkTextStyle.maxWidth}. */
446
+ get maxWidth() {
447
+ return this._style.maxWidth;
448
+ }
449
+ /** Sets {@link SilkText.maxWidth}. */
450
+ set maxWidth(v) {
451
+ this._set("maxWidth", v);
452
+ }
453
+ /** Tallest the text box may be, 0 for no limit. See {@link SilkTextStyle.maxHeight}. */
454
+ get maxHeight() {
455
+ return this._style.maxHeight;
456
+ }
457
+ /** Sets {@link SilkText.maxHeight}. */
458
+ set maxHeight(v) {
459
+ this._set("maxHeight", v);
460
+ }
461
+ /** What a cut line ends with, or `null` for no mark. */
462
+ get ellipsis() {
463
+ return this._style.ellipsis;
464
+ }
465
+ /** Sets {@link SilkText.ellipsis}. */
466
+ set ellipsis(v) {
467
+ this._set("ellipsis", v);
468
+ }
469
+ /** Shrinks the font to fit `maxWidth`, `maxHeight` and `maxLines`. See {@link SilkTextStyle.shrinkToFit}. */
470
+ get shrinkToFit() {
471
+ return this._style.shrinkToFit;
472
+ }
473
+ /** Sets {@link SilkText.shrinkToFit}. */
474
+ set shrinkToFit(v) {
475
+ this._set("shrinkToFit", v);
476
+ }
477
+ /** Smallest size `shrinkToFit` goes to, 0 for half the font size. */
478
+ get minFontSize() {
479
+ return this._style.minFontSize;
480
+ }
481
+ /** Sets {@link SilkText.minFontSize}. */
482
+ set minFontSize(v) {
483
+ this._set("minFontSize", v);
484
+ }
485
+ /** Tab stops every this many space widths. */
486
+ get tabSize() {
487
+ return this._style.tabSize;
488
+ }
489
+ /** Sets {@link SilkText.tabSize}. */
490
+ set tabSize(v) {
491
+ this._set("tabSize", v);
492
+ }
493
+ /** The font size the text is drawn at: `fontSize`, or smaller when `shrinkToFit` had to shrink it. */
494
+ get renderedFontSize() {
495
+ this.updateText();
496
+ return this._size;
497
+ }
498
+ /** True when the text did not fit its limits and was cut. */
499
+ get truncated() {
500
+ return this.layout.truncated;
501
+ }
502
+ /** Where lines may break without spaces: `normal` or `keep-all`. */
503
+ get wordBreak() {
504
+ return this._style.wordBreak;
505
+ }
506
+ /** Sets {@link SilkText.wordBreak}. */
507
+ set wordBreak(v) {
508
+ this._set("wordBreak", v);
379
509
  }
380
510
  get bounds() {
381
511
  this.updateText();
@@ -417,6 +547,53 @@ var SilkText = class SilkText extends Mesh {
417
547
  if (shader) this.atlas.untrack(shader);
418
548
  shader?.destroy();
419
549
  }
550
+ /** The inline boxes in layout terms: advance with margins, and the height above and below the baseline. */
551
+ _resolveInlines(font, k, ascent, descent) {
552
+ const out = {};
553
+ const names = Object.keys(this._style.inlines);
554
+ this._inlineBoxes = {};
555
+ if (!names.length) return out;
556
+ const plain = this._text.replace(/\{[\w-]+\}/g, "").replace(/\s+/g, "");
557
+ let middle = (this.atlas.ink(font, "H").ascent * k || ascent * .7) / 2;
558
+ if (plain && !/[\p{Lu}\p{Nd}]/u.test(plain)) {
559
+ const m = this.atlas.measure(font, plain);
560
+ if (m.ascent + m.descent > 0) middle = (m.ascent - m.descent) / 2 * k;
561
+ }
562
+ for (const name of names) {
563
+ const d = this._style.inlines[name];
564
+ const h = Math.max(0, d.height);
565
+ const margin = d.margin ?? 0;
566
+ let top;
567
+ switch (d.align ?? "middle") {
568
+ case "baseline":
569
+ top = h;
570
+ break;
571
+ case "center":
572
+ top = (ascent - descent) / 2 + h / 2;
573
+ break;
574
+ case "top":
575
+ top = ascent;
576
+ break;
577
+ case "bottom":
578
+ top = h - descent;
579
+ break;
580
+ default: top = middle + h / 2;
581
+ }
582
+ top -= d.offsetY ?? 0;
583
+ this._inlineBoxes[name] = {
584
+ width: Math.max(0, d.width),
585
+ height: h,
586
+ margin,
587
+ top
588
+ };
589
+ out[name] = {
590
+ width: Math.max(0, d.width) + 2 * margin,
591
+ ascent: top,
592
+ descent: h - top
593
+ };
594
+ }
595
+ return out;
596
+ }
420
597
  _set(key, value) {
421
598
  if (this._style[key] === value) return;
422
599
  this._style[key] = value;
@@ -435,43 +612,97 @@ var SilkText = class SilkText extends Mesh {
435
612
  weight: s.fontWeight,
436
613
  style: s.fontStyle
437
614
  };
438
- const k = s.fontSize / atlas.size;
439
615
  const fm = atlas.metrics(font);
440
- const layout = layoutText(this._text, {
441
- ascent: fm.ascent * k,
442
- descent: fm.descent * k,
443
- letterSpacing: s.letterSpacing,
444
- lineHeight: s.lineHeight > 0 ? s.lineHeight : void 0,
445
- align: s.align,
446
- wordWrap: s.wordWrap,
447
- wordWrapWidth: s.wordWrapWidth,
448
- breakWords: s.breakWords,
449
- tabularNumbers: s.tabularNumbers,
450
- kerning: s.kerning
451
- }, {
452
- advance: (c) => atlas.advance(font, c) * k,
453
- kerning: (a, b) => atlas.kerning(font, a, b) * k
454
- });
616
+ const reach = Math.max(0, s.dilate) + (s.outline && (s.outline.alpha ?? 1) > 0 ? Math.max(0, s.outline.width) : 0);
617
+ const lay = (size, maxLines, cut) => {
618
+ const k = size / atlas.size;
619
+ const f = size / s.fontSize;
620
+ return layoutText(this._text, {
621
+ ascent: fm.ascent * k,
622
+ descent: fm.descent * k,
623
+ letterSpacing: s.letterSpacing * f,
624
+ lineHeight: s.lineHeight > 0 ? s.lineHeight * f : void 0,
625
+ align: s.align,
626
+ direction: s.direction,
627
+ wordWrap: s.wordWrap,
628
+ wordWrapWidth: s.wordWrapWidth,
629
+ breakWords: s.breakWords,
630
+ tabularNumbers: s.tabularNumbers,
631
+ kerning: s.kerning,
632
+ wordBreak: s.wordBreak,
633
+ tabSize: s.tabSize,
634
+ inlines: this._resolveInlines(font, k, fm.ascent * k, fm.descent * k),
635
+ maxLines: cut ? maxLines : 0,
636
+ maxWidth: cut && s.maxWidth > 0 ? s.maxWidth : void 0,
637
+ ellipsis: s.ellipsis
638
+ }, {
639
+ advance: (c) => atlas.advance(font, c) * k,
640
+ kerning: (a, b) => atlas.kerning(font, a, b) * k,
641
+ ink: (c) => {
642
+ const i = atlas.ink(font, c);
643
+ return {
644
+ ascent: i.ascent * k + reach,
645
+ descent: i.descent * k + reach
646
+ };
647
+ }
648
+ });
649
+ };
650
+ const fits = (l) => (s.maxWidth <= 0 || s.wordWrap || l.width <= s.maxWidth + 1e-6) && (s.maxHeight <= 0 || l.height <= s.maxHeight + 1e-6) && (s.maxLines <= 0 || l.lines.length <= s.maxLines);
651
+ let size = s.fontSize;
652
+ let layout = lay(size, 0, false);
653
+ if (s.shrinkToFit && !fits(layout)) {
654
+ let lo = Math.min(s.fontSize, s.minFontSize > 0 ? s.minFontSize : s.fontSize / 2);
655
+ let hi = s.fontSize;
656
+ if (fits(lay(lo, 0, false))) for (let i = 0; i < 8 && hi - lo > .05; i++) {
657
+ const mid = (lo + hi) / 2;
658
+ if (fits(lay(mid, 0, false))) lo = mid;
659
+ else hi = mid;
660
+ }
661
+ size = lo;
662
+ layout = lay(size, 0, false);
663
+ }
664
+ if (!fits(layout)) {
665
+ let lines = s.maxLines > 0 ? s.maxLines : Number.POSITIVE_INFINITY;
666
+ if (s.maxHeight > 0 && layout.height > s.maxHeight) {
667
+ const font = layout.ascent + layout.descent;
668
+ lines = Math.min(lines, Math.max(1, Math.floor((s.maxHeight - font) / layout.lineHeight + 1e-6) + 1));
669
+ }
670
+ layout = lay(size, Number.isFinite(lines) ? lines : 0, true);
671
+ }
672
+ const k = size / atlas.size;
673
+ this._size = size;
455
674
  const ox = -this.anchor.x * layout.width;
456
675
  const oy = -this.anchor.y * layout.height;
457
676
  const shadow = s.shadow && (s.shadow.alpha ?? 1) > 0 ? s.shadow : null;
677
+ const outline = s.outline && s.outline.width > 0 && (s.outline.alpha ?? 1) > 0 ? s.outline : null;
458
678
  const glyphs = layout.glyphs;
459
- const layers = shadow ? 2 : 1;
679
+ const passes = [];
680
+ if (shadow) passes.push([1, 0]);
681
+ if (outline) passes.push([0, 1], [0, 2]);
682
+ else passes.push([0, 0]);
460
683
  this._layout = layout;
461
684
  this._ox = ox;
462
685
  this._oy = oy;
463
- this._ensure(glyphs.length * layers);
686
+ this._passes = passes.length;
687
+ this._ensure(glyphs.length * passes.length);
464
688
  const f = this._f;
689
+ const bounds = this.geometry.localBounds;
690
+ const pad = atlas.pad * k;
465
691
  let n = 0;
466
- for (let layer = layers - 1; layer >= 0; layer--) {
692
+ bounds.clear();
693
+ bounds.addFrame(ox, oy, ox + layout.width, oy + layout.height);
694
+ for (const [layer, mode] of passes) {
467
695
  const dx = layer ? shadow?.offsetX ?? 0 : 0;
468
696
  const dy = layer ? shadow?.offsetY ?? 0 : 0;
697
+ const reach = Math.max(0, s.dilate) + (outline ? outline.width : 0) + (layer ? shadow?.blur ?? 4 : 0);
469
698
  for (const pg of glyphs) {
699
+ if (pg.inline) continue;
470
700
  const g = atlas.glyph(font, pg.char);
471
701
  if (g.w === 0) continue;
472
702
  const o = n * STRIDE;
473
703
  const x0 = ox + pg.x + g.left * k + dx;
474
704
  const y0 = oy + pg.y + g.top * k + dy;
705
+ bounds.addFrame(x0 + pad - reach, y0 + pad - reach, x0 + g.w * k - pad + reach, y0 + g.h * k - pad + reach);
475
706
  f[o] = x0;
476
707
  f[o + 1] = y0;
477
708
  f[o + 2] = x0 + g.w * k;
@@ -481,16 +712,14 @@ var SilkText = class SilkText extends Mesh {
481
712
  f[o + 6] = g.x + g.w;
482
713
  f[o + 7] = g.y + g.h;
483
714
  f[o + 8] = layer;
484
- f[o + 9] = 0;
485
- f[o + 10] = 0;
486
- f[o + 11] = 0;
715
+ f[o + 9] = mode;
716
+ f[o + 10] = g.color ? g.color.x : -1;
717
+ f[o + 11] = g.color ? g.color.y : -1;
487
718
  n++;
488
719
  }
489
720
  }
490
721
  this._count = n;
491
722
  const geometry = this.geometry;
492
- geometry.localBounds.clear();
493
- geometry.localBounds.addFrame(ox, oy, ox + layout.width, oy + layout.height);
494
723
  geometry.instanceCount = n;
495
724
  geometry.instanceBuffer.update(Math.max(n, 1) * STRIDE * 4);
496
725
  this._paint(s, k, ox, oy, layout);
@@ -546,7 +775,7 @@ var SilkText = class SilkText extends Mesh {
546
775
  atlas.radius,
547
776
  atlas.cutoff,
548
777
  k,
549
- s.fontSize
778
+ this._size
550
779
  ]);
551
780
  u.uFx.set([
552
781
  s.dilate,