drawnui-react 0.1.0-preview.3 → 0.1.0-preview.30

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 (91) hide show
  1. package/README.md +15 -1
  2. package/SKIPPED.md +32 -25
  3. package/dist/controls/AnimatedFramesRenderer.d.ts +7 -0
  4. package/dist/controls/AnimatedFramesRenderer.js +16 -1
  5. package/dist/controls/DrawnGame.d.ts +40 -0
  6. package/dist/controls/DrawnGame.js +64 -0
  7. package/dist/controls/GridStructure.d.ts +16 -2
  8. package/dist/controls/GridStructure.js +40 -4
  9. package/dist/controls/SkiaBackdrop.d.ts +8 -2
  10. package/dist/controls/SkiaBackdrop.js +42 -14
  11. package/dist/controls/SkiaButton.d.ts +15 -1
  12. package/dist/controls/SkiaButton.js +21 -4
  13. package/dist/controls/SkiaCarousel.d.ts +21 -2
  14. package/dist/controls/SkiaCarousel.js +49 -5
  15. package/dist/controls/SkiaCheckbox.d.ts +0 -1
  16. package/dist/controls/SkiaCheckbox.js +0 -6
  17. package/dist/controls/SkiaDrawer.d.ts +2 -0
  18. package/dist/controls/SkiaDrawer.js +2 -0
  19. package/dist/controls/SkiaEditor.d.ts +38 -1
  20. package/dist/controls/SkiaEditor.js +223 -49
  21. package/dist/controls/SkiaGif.d.ts +3 -0
  22. package/dist/controls/SkiaGif.js +12 -2
  23. package/dist/controls/SkiaImage.js +15 -10
  24. package/dist/controls/SkiaLabel.d.ts +78 -7
  25. package/dist/controls/SkiaLabel.js +379 -60
  26. package/dist/controls/SkiaLabelFps.d.ts +16 -0
  27. package/dist/controls/SkiaLabelFps.js +41 -0
  28. package/dist/controls/SkiaLayout.d.ts +97 -9
  29. package/dist/controls/SkiaLayout.js +387 -22
  30. package/dist/controls/SkiaLottie.js +4 -2
  31. package/dist/controls/SkiaProgress.d.ts +2 -1
  32. package/dist/controls/SkiaProgress.js +6 -3
  33. package/dist/controls/SkiaRadioButton.js +4 -3
  34. package/dist/controls/SkiaRichLabel.d.ts +1 -1
  35. package/dist/controls/SkiaRichLabel.js +11 -1
  36. package/dist/controls/SkiaScroll.d.ts +35 -5
  37. package/dist/controls/SkiaScroll.js +234 -23
  38. package/dist/controls/SkiaScrollBar.d.ts +27 -0
  39. package/dist/controls/SkiaScrollBar.js +51 -2
  40. package/dist/controls/SkiaShaderCarousel.d.ts +5 -0
  41. package/dist/controls/SkiaShaderCarousel.js +5 -0
  42. package/dist/controls/SkiaShape.d.ts +13 -2
  43. package/dist/controls/SkiaShape.js +50 -10
  44. package/dist/controls/SkiaSlider.d.ts +20 -2
  45. package/dist/controls/SkiaSlider.js +59 -5
  46. package/dist/controls/SkiaSprite.d.ts +1 -0
  47. package/dist/controls/SkiaSprite.js +4 -0
  48. package/dist/controls/SkiaSvg.js +1 -1
  49. package/dist/controls/SkiaSwitch.d.ts +0 -1
  50. package/dist/controls/SkiaSwitch.js +0 -6
  51. package/dist/controls/SkiaToggle.d.ts +25 -5
  52. package/dist/controls/SkiaToggle.js +58 -5
  53. package/dist/controls/SnappingLayout.d.ts +12 -1
  54. package/dist/controls/SnappingLayout.js +19 -1
  55. package/dist/core/Accessibility.d.ts +80 -0
  56. package/dist/core/Accessibility.js +421 -8
  57. package/dist/core/Animators.d.ts +37 -1
  58. package/dist/core/Animators.js +102 -9
  59. package/dist/core/Canvas.d.ts +108 -7
  60. package/dist/core/Canvas.js +353 -29
  61. package/dist/core/Easing.d.ts +12 -1
  62. package/dist/core/Easing.js +25 -1
  63. package/dist/core/Gestures.d.ts +34 -2
  64. package/dist/core/Gestures.js +84 -3
  65. package/dist/core/Hover.d.ts +47 -0
  66. package/dist/core/Hover.js +134 -0
  67. package/dist/core/KeyboardManager.d.ts +13 -0
  68. package/dist/core/KeyboardManager.js +18 -0
  69. package/dist/core/ScrollAnimators.js +9 -0
  70. package/dist/core/Shaping.d.ts +40 -0
  71. package/dist/core/Shaping.js +224 -0
  72. package/dist/core/SkiaControl.d.ts +209 -9
  73. package/dist/core/SkiaControl.js +500 -59
  74. package/dist/core/SkiaEffect.d.ts +17 -0
  75. package/dist/core/SkiaEffect.js +43 -8
  76. package/dist/core/SkiaImageManager.js +1 -1
  77. package/dist/core/Super.d.ts +22 -2
  78. package/dist/core/Super.js +47 -3
  79. package/dist/core/Types.d.ts +11 -1
  80. package/dist/core/Types.js +32 -1
  81. package/dist/core/ViewsAdapter.d.ts +7 -2
  82. package/dist/core/ViewsAdapter.js +10 -3
  83. package/dist/index.d.ts +3 -0
  84. package/dist/index.js +3 -0
  85. package/dist/react/SkiaShell.js +33 -14
  86. package/dist/react/index.d.ts +17 -4
  87. package/dist/react/index.js +103 -15
  88. package/dist/react/reconciler.js +33 -13
  89. package/dist/vite/index.d.ts +9 -0
  90. package/dist/vite/index.js +36 -3
  91. package/package.json +41 -3
@@ -3,6 +3,7 @@ import { ControlTappedEventArgs } from "../core/Gestures";
3
3
  import { Super } from "../core/Super";
4
4
  import { Colors, ScaledSize, SKRect, Thickness, } from "../core/Types";
5
5
  import { TextSpan } from "./TextSpan";
6
+ import { BidiRuns, HasCodePoint, IsRtl, JoinsPrevious, NeedsShaping, Shape } from "../core/Shaping";
6
7
  /**
7
8
  * Mirrors DrawnUi SkiaLabel: multi-line text with word wrapping, MaxLines + tail ellipsis, horizontal /
8
9
  * vertical alignment, LineSpacing / LineHeight, weights and attributes resolved through the font registry,
@@ -24,6 +25,8 @@ export class SkiaLabel extends SkiaControl {
24
25
  verticalTextAlignment = "Start";
25
26
  lineSpacing = 1;
26
27
  lineHeight = 1;
28
+ paragraphSpacing = 0;
29
+ characterSpacing = 1;
27
30
  textTransform = "None";
28
31
  padding = Thickness.Zero;
29
32
  /** Styled fragments; when not empty they replace `Text` (same precedence as C#). */
@@ -43,12 +46,17 @@ export class SkiaLabel extends SkiaControl {
43
46
  this[key] = v;
44
47
  this.Update();
45
48
  } }
49
+ /** A property that changes only the look (a color): no layout change, see UpdateDraw. */
50
+ SetLook(key, v) { if (this[key] !== v) {
51
+ this[key] = v;
52
+ this.UpdateDraw();
53
+ } }
46
54
  get Text() { return this.text; }
47
55
  set Text(v) { this.Set("text", v); }
48
56
  get FontSize() { return this.fontSize; }
49
57
  set FontSize(v) { this.Set("fontSize", v); }
50
58
  get TextColor() { return this.textColor; }
51
- set TextColor(v) { this.Set("textColor", v); }
59
+ set TextColor(v) { this.SetLook("textColor", v); }
52
60
  get FontFamily() { return this.fontFamily; }
53
61
  set FontFamily(v) { this.Set("fontFamily", v); }
54
62
  /**
@@ -74,10 +82,22 @@ export class SkiaLabel extends SkiaControl {
74
82
  /** Multiplier applied to the natural line height (ascent + descent). */
75
83
  get LineHeight() { return this.lineHeight; }
76
84
  set LineHeight(v) { this.Set("lineHeight", v); }
85
+ /**
86
+ * Space above every paragraph after the first, in lines (C# ParagraphSpacing; 0 here as DrawnUi.Rust, C# 0.25). A
87
+ * paragraph starts after "\n"; U+2028 breaks the line inside a paragraph.
88
+ */
89
+ get ParagraphSpacing() { return this.paragraphSpacing; }
90
+ set ParagraphSpacing(v) { this.Set("paragraphSpacing", v); }
91
+ /**
92
+ * C# CharacterSpacing: 1 = the font's own spacing; every glyph is followed by (CharacterSpacing - 1) points more
93
+ * (less below 1), the line measures and wraps at the width it draws (C# 7cf1007c, DrawnUi.Rust label).
94
+ */
95
+ get CharacterSpacing() { return this.characterSpacing; }
96
+ set CharacterSpacing(v) { this.Set("characterSpacing", v); }
77
97
  get TextTransform() { return this.textTransform; }
78
98
  set TextTransform(v) { this.Set("textTransform", v); }
79
99
  get Padding() { return this.padding; }
80
- set Padding(v) { this.Set("padding", v); }
100
+ set Padding(v) { this.Set("padding", Thickness.From(v)); }
81
101
  /** Number of laid-out lines after the last measure. */
82
102
  get LinesCount() { return this.lines.length; }
83
103
  // ---- spans as children (reconciler AddSubView / InsertSubView / RemoveSubView) ----
@@ -85,8 +105,14 @@ export class SkiaLabel extends SkiaControl {
85
105
  InsertSubView(index, control) {
86
106
  if (!(control instanceof TextSpan))
87
107
  throw new Error("DrawnUi: SkiaLabel children must be <TextSpan>");
108
+ const at = control.Parent === this ? this.Spans.indexOf(control) : -1; // already a span: a move, never a 2nd copy
109
+ if (at >= 0) {
110
+ this.Spans.splice(at, 1);
111
+ if (at < index)
112
+ index--;
113
+ }
88
114
  control.Parent = this;
89
- this.Spans.splice(index, 0, control);
115
+ this.Spans.splice(Math.min(Math.max(0, index), this.Spans.length), 0, control);
90
116
  this.Update();
91
117
  }
92
118
  RemoveSubView(control) {
@@ -102,6 +128,8 @@ export class SkiaLabel extends SkiaControl {
102
128
  mainFonts;
103
129
  fontsCache = new Map();
104
130
  runCache = new Map();
131
+ /** CharacterSpacing in pixels at the measured scale: added after every glyph of a run (its Width includes them). */
132
+ glyphSpacing = 0;
105
133
  ResolveFonts(family, weight, italic, sizePx) {
106
134
  const key = `${family}|${this.fontFamilyFallback}|${weight}|${italic}|${sizePx}`;
107
135
  let f = this.fontsCache.get(key);
@@ -131,15 +159,35 @@ export class SkiaLabel extends SkiaControl {
131
159
  const weight = span.FontWeight > 0 ? span.FontWeight : span.IsBold ? 700 : this.fontWeight > 0 ? this.fontWeight : bold ? 700 : 0;
132
160
  return this.ResolveFonts(span.FontFamily ?? this.fontFamily, weight, italic, (span.FontSize ?? this.fontSize) * scale);
133
161
  }
134
- static Advance(font, text) {
162
+ static NeedsShapingOrRtl(cp) { return NeedsShaping(cp) || IsRtl(cp); }
163
+ /** Width of a text in one font: the shaped glyphs where the text needs shaping (Arabic joins, emoji sequences), else glyph by glyph. */
164
+ Advance(font, text) {
165
+ if (HasCodePoint(text, SkiaLabel.NeedsShapingOrRtl)) {
166
+ const shaped = Shape(font, text, HasCodePoint(text, IsRtl), this.glyphSpacing);
167
+ if (shaped)
168
+ return shaped.Width;
169
+ }
135
170
  let w = 0;
136
171
  for (const adv of font.getGlyphWidths(font.getGlyphIDs(text)))
137
- w += adv;
172
+ w += adv + this.glyphSpacing;
138
173
  return w;
139
174
  }
175
+ static Ignorable = /[\u200B-\u200F\u2060-\u2064\uFE00-\uFE0F\u{E0000}-\u{E0FFF}]/u;
176
+ /**
177
+ * Default-ignorable code points (zero width space, joiners, direction marks, word joiners, variation selectors such
178
+ * as U+FE0F after an emoji, tags) that none of the fonts has draw nothing and take no room, never the missing-glyph
179
+ * box (DrawnUi.Rust label_ignorables; text is not shaped here, C# hides them by shaping).
180
+ */
181
+ static DropIgnorables(text, main, fallbacks) {
182
+ if (!SkiaLabel.Ignorable.test(text))
183
+ return text;
184
+ const has = (cp) => main.getGlyphIDs(cp)[0] !== 0 || fallbacks.some((f) => f.getGlyphIDs(cp)[0] !== 0);
185
+ return Array.from(text).filter((cp) => !SkiaLabel.Ignorable.test(cp) || has(cp)).join("");
186
+ }
140
187
  /**
141
188
  * Splits text into runs by glyph availability: the main font, or the first fallback that has a glyph where the
142
- * main font has glyph 0. Spaces always stay on the main font (fallback faces often carry very wide spaces).
189
+ * main font has glyph 0. Spaces always stay on the main font (fallback faces often carry very wide spaces). A joiner,
190
+ * a variation selector, a skin tone or a tag keeps the font of the glyph before it: an emoji sequence is one run.
143
191
  */
144
192
  Segment(text, fonts, span) {
145
193
  if (text.length === 0)
@@ -150,8 +198,13 @@ export class SkiaLabel extends SkiaControl {
150
198
  const main = fonts.Main;
151
199
  const fbs = fonts.Fallbacks;
152
200
  cached = [];
201
+ text = SkiaLabel.DropIgnorables(text, main, fbs);
202
+ if (text.length === 0) {
203
+ this.runCache.set(cacheKey, cached);
204
+ return cached;
205
+ }
153
206
  if (fbs.length === 0) {
154
- cached.push({ Text: text, Font: main, Width: SkiaLabel.Advance(main, text), Fonts: fonts });
207
+ cached.push({ Text: text, Font: main, Width: this.Advance(main, text), Fonts: fonts });
155
208
  }
156
209
  else {
157
210
  const cps = Array.from(text);
@@ -167,16 +220,16 @@ export class SkiaLabel extends SkiaControl {
167
220
  };
168
221
  let start = 0, current = fontFor(0);
169
222
  for (let i = 1; i < cps.length; i++) {
170
- const f = fontFor(i);
223
+ const f = JoinsPrevious(cps[i].codePointAt(0)) ? current : fontFor(i);
171
224
  if (f !== current) {
172
225
  const t = cps.slice(start, i).join("");
173
- cached.push({ Text: t, Font: current, Width: SkiaLabel.Advance(current, t), Fonts: fonts });
226
+ cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
174
227
  start = i;
175
228
  current = f;
176
229
  }
177
230
  }
178
231
  const t = cps.slice(start).join("");
179
- cached.push({ Text: t, Font: current, Width: SkiaLabel.Advance(current, t), Fonts: fonts });
232
+ cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
180
233
  }
181
234
  this.runCache.set(cacheKey, cached);
182
235
  }
@@ -184,6 +237,76 @@ export class SkiaLabel extends SkiaControl {
184
237
  }
185
238
  Width(runs) { let w = 0; for (const r of runs)
186
239
  w += r.Width; return w; }
240
+ /** A line never starts with these (closing punctuation, small kana, the long vowel mark): JIS X 4051 kinsoku (C#). */
241
+ static NoBreakBefore = "、。,.・:;?!゛゜ヽヾゝゞ々〻ー」』)〕]}〉》】〗〙〟⦆»’”‐゠–〜~ぁぃぅぇぉっゃゅょゎゕゖァィゥェォッャュョヮヵヶㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ。」、・ーァィゥェォャュョッ)]},.!?:;%";
242
+ /** A line never ends with these (opening brackets and quotes). */
243
+ static NoBreakAfter = "「『(〔[{〈《【〖〘〝⦅«‘“「([{";
244
+ /** Chinese / Japanese: ideographs, kana, CJK punctuation, full-width forms (C# IsCjkAt). Korean breaks at spaces only. */
245
+ static Cjk = /[\u3000-\u312F\u3190-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFF00-\uFFEF\u{20000}-\u{3FFFF}]/u;
246
+ static CanBreakBetween(before, after) {
247
+ return (SkiaLabel.Cjk.test(before) || SkiaLabel.Cjk.test(after)) && !SkiaLabel.NoBreakBefore.includes(after) && !SkiaLabel.NoBreakAfter.includes(before);
248
+ }
249
+ /**
250
+ * C# CanBreakInsideWord: whether a line may break between text[index - 1] and text[index] (UTF-16 indexes) inside a
251
+ * space-free run: next to a Chinese or Japanese character, never before closing punctuation or small kana, never
252
+ * after an opening bracket, never inside a surrogate pair.
253
+ */
254
+ static CanBreakInsideWord(text, index) {
255
+ if (index <= 0 || index >= text.length)
256
+ return false;
257
+ const hi = (k) => { const c = text.charCodeAt(k); return c >= 0xD800 && c <= 0xDBFF; };
258
+ if (hi(index - 1))
259
+ return false; // inside a surrogate pair
260
+ const before = String.fromCodePoint(text.codePointAt(hi(index - 2) && index >= 2 ? index - 2 : index - 1));
261
+ const after = String.fromCodePoint(text.codePointAt(index));
262
+ return SkiaLabel.CanBreakBetween(before, after);
263
+ }
264
+ /**
265
+ * C# FitAtBreak: the last place inside the word where a Chinese / Japanese line may break and `prefix` plus the word
266
+ * up to there still fits; code points from the word's start, 0 = none.
267
+ */
268
+ FitCjk(word, prefix, maxWidth) {
269
+ if (!word.some((r) => SkiaLabel.Cjk.test(r.Text)))
270
+ return 0;
271
+ const sp = this.glyphSpacing;
272
+ let x = prefix, best = 0, before = "", k = 0;
273
+ for (const r of word) {
274
+ const cps = Array.from(r.Text), widths = r.Font.getGlyphWidths(r.Font.getGlyphIDs(r.Text, cps.length));
275
+ for (let i = 0; i < cps.length; i++) {
276
+ if (k > 0 && SkiaLabel.CanBreakBetween(before, cps[i])) {
277
+ if (x - sp > maxWidth)
278
+ return best;
279
+ best = k;
280
+ }
281
+ x += widths[i] + sp;
282
+ before = cps[i];
283
+ k++;
284
+ }
285
+ }
286
+ return best;
287
+ }
288
+ /** The word cut after `count` code points: the head goes on a line, the rest follows. */
289
+ SplitWord(word, count) {
290
+ const head = [], rest = [];
291
+ let left = count;
292
+ for (const r of word) {
293
+ const cps = Array.from(r.Text);
294
+ if (left >= cps.length) {
295
+ head.push(r);
296
+ left -= cps.length;
297
+ continue;
298
+ }
299
+ if (left > 0) {
300
+ const a = cps.slice(0, left).join(""), b = cps.slice(left).join("");
301
+ head.push({ ...r, Text: a, Width: this.Advance(r.Font, a) });
302
+ rest.push({ ...r, Text: b, Width: this.Advance(r.Font, b) });
303
+ left = 0;
304
+ continue;
305
+ }
306
+ rest.push(r);
307
+ }
308
+ return [head, rest];
309
+ }
187
310
  Transform(text) {
188
311
  switch (this.textTransform) {
189
312
  case "Uppercase": return text.toUpperCase();
@@ -199,10 +322,16 @@ export class SkiaLabel extends SkiaControl {
199
322
  Tokenize(scale) {
200
323
  const paragraphs = [[]];
201
324
  const add = (text, fonts, span) => {
202
- const parts = this.Transform(text).split("\n");
325
+ // "\n" starts a paragraph, U+2028 breaks the line inside one
326
+ const pieces = this.Transform(text).split(/(\n|\u2028)/);
327
+ const parts = pieces.filter((_, i) => i % 2 === 0), seps = pieces.filter((_, i) => i % 2 === 1);
203
328
  for (let p = 0; p < parts.length; p++) {
204
- if (p > 0)
205
- paragraphs.push([]);
329
+ if (p > 0) {
330
+ const next = [];
331
+ if (seps[p - 1] === "\u2028")
332
+ next.LineBreak = true;
333
+ paragraphs.push(next);
334
+ }
206
335
  const para = paragraphs[paragraphs.length - 1];
207
336
  const words = parts[p].split(" ");
208
337
  for (let w = 0; w < words.length; w++) {
@@ -222,11 +351,11 @@ export class SkiaLabel extends SkiaControl {
222
351
  else if (this.text) {
223
352
  add(this.text, this.mainFonts);
224
353
  }
225
- // a fragment ending with a space puts the space before the NEXT token
354
+ // a fragment ending with a space puts that space, in its own style, before the NEXT token
226
355
  for (const para of paragraphs) {
227
356
  for (let i = 0; i < para.length; i++) {
228
357
  if (para[i].TrailingSpace && i + 1 < para.length)
229
- para[i + 1].SpaceBefore = true;
358
+ para[i + 1].Carried = { Fonts: para[i].Fonts, Span: para[i].Span };
230
359
  }
231
360
  }
232
361
  return paragraphs;
@@ -248,63 +377,100 @@ export class SkiaLabel extends SkiaControl {
248
377
  line.Descent = r.Fonts.Descent;
249
378
  }
250
379
  }
251
- /** Word-wraps tokens into lines that fit maxWidth (Infinity = no wrap), applies MaxLines with a tail ellipsis. */
252
- LayoutLines(maxWidth, scale) {
380
+ /**
381
+ * Word-wraps tokens into lines that fit maxWidth (Infinity = no wrap); keeps the lines that fit maxHeight (at least one)
382
+ * and MaxLines, the last kept one ending with a tail ellipsis (C# / DrawnUi.Rust: a label in a box too short for its
383
+ * text cuts it, not only MaxLines).
384
+ */
385
+ LayoutLines(maxWidth, scale, maxHeight = Infinity) {
253
386
  const wrap = this.lineBreakMode !== "NoWrap" && isFinite(maxWidth);
387
+ // run widths carry the gap after every glyph; the one after a line's last glyph is not drawn
388
+ const sp = this.glyphSpacing;
254
389
  const out = [];
255
390
  const empty = (fonts) => { const l = this.NewLine(); l.Ascent = fonts.Ascent; l.Descent = fonts.Descent; return l; };
256
391
  for (const para of this.Tokenize(scale)) {
257
392
  let line = this.NewLine();
393
+ if (out.length > 0 && !para.LineBreak)
394
+ line.Para = true;
258
395
  let lastFonts = this.mainFonts;
259
396
  for (let i = 0; i < para.length; i++) {
260
397
  const tok = para[i];
261
398
  lastFonts = tok.Fonts;
262
399
  // glued fragments (no space between spans) wrap as one word
263
400
  let word = this.Segment(tok.Text, tok.Fonts, tok.Span);
264
- while (i + 1 < para.length && !para[i + 1].SpaceBefore) {
401
+ while (i + 1 < para.length && !para[i + 1].SpaceBefore && !para[i + 1].Carried) {
265
402
  i++;
266
403
  word = word.concat(this.Segment(para[i].Text, para[i].Fonts, para[i].Span));
267
404
  }
268
- const space = tok.SpaceBefore && line.Runs.length > 0 ? this.Segment(" ", tok.Fonts, tok.Span) : [];
269
- const spaceW = this.Width(space), wordW = this.Width(word);
270
- if (!wrap || line.Width + spaceW + wordW <= maxWidth || (line.Runs.length === 0 && wordW <= maxWidth)) {
271
- this.Append(line, space);
272
- this.Append(line, word);
273
- continue;
274
- }
275
- if (line.Runs.length > 0) {
276
- out.push(line);
277
- line = this.NewLine();
278
- }
279
- if (wordW <= maxWidth) {
280
- this.Append(line, word);
281
- continue;
282
- }
283
- // word longer than the line: break by code points
284
- for (const r of word) {
285
- for (const ch of Array.from(r.Text)) {
286
- const run = this.Segment(ch, r.Fonts, r.Span);
287
- const chW = this.Width(run);
288
- if (line.Runs.length > 0 && line.Width + chW > maxWidth) {
289
- out.push(line);
290
- line = this.NewLine();
405
+ for (;;) {
406
+ // the space the fragment before ends with, then the word's own one, each in its style
407
+ const space = line.Runs.length === 0 ? [] : [
408
+ ...(tok.Carried ? this.Segment(" ", tok.Carried.Fonts, tok.Carried.Span) : []),
409
+ ...(tok.SpaceBefore ? this.Segment(" ", tok.Fonts, tok.Span) : []),
410
+ ];
411
+ const spaceW = this.Width(space), wordW = this.Width(word);
412
+ if (!wrap || line.Width + spaceW + wordW - sp <= maxWidth || (line.Runs.length === 0 && wordW - sp <= maxWidth)) {
413
+ this.Append(line, space);
414
+ this.Append(line, word);
415
+ break;
416
+ }
417
+ // Chinese / Japanese: the word fills the line up to its last break that fits (C# FitAtBreak)
418
+ const k = this.FitCjk(word, line.Width + spaceW, maxWidth);
419
+ if (k > 0) {
420
+ const [head, rest] = this.SplitWord(word, k);
421
+ this.Append(line, space);
422
+ this.Append(line, head);
423
+ out.push(line);
424
+ line = this.NewLine();
425
+ word = rest;
426
+ continue;
427
+ }
428
+ if (line.Runs.length > 0) {
429
+ out.push(line);
430
+ line = this.NewLine();
431
+ continue;
432
+ }
433
+ // word longer than the line: break by code points
434
+ for (const r of word) {
435
+ for (const ch of Array.from(r.Text)) {
436
+ const run = this.Segment(ch, r.Fonts, r.Span);
437
+ const chW = this.Width(run);
438
+ if (line.Runs.length > 0 && line.Width + chW - sp > maxWidth) {
439
+ out.push(line);
440
+ line = this.NewLine();
441
+ }
442
+ this.Append(line, run);
291
443
  }
292
- this.Append(line, run);
293
444
  }
445
+ break;
294
446
  }
295
447
  }
296
448
  out.push(line.Runs.length > 0 ? line : empty(lastFonts));
297
449
  }
298
- if (this.maxLines > 0 && out.length > this.maxLines) {
299
- out.length = this.maxLines;
450
+ let kept = this.maxLines > 0 ? Math.min(out.length, this.maxLines) : out.length;
451
+ if (isFinite(maxHeight)) {
452
+ let used = 0, fit = 0;
453
+ for (let i = 0; i < out.length; i++) {
454
+ const lh = this.LineHeightPx(out[i]);
455
+ // the block height of the first i + 1 lines: spacing after every line but the last, paragraph space above
456
+ const total = used + lh + (i > 0 ? this.ParagraphSpace(out[i]) : 0);
457
+ if (total > maxHeight && fit > 0)
458
+ break;
459
+ fit++;
460
+ used = total - lh + lh * this.lineSpacing;
461
+ }
462
+ kept = Math.min(kept, fit);
463
+ }
464
+ if (kept < out.length) {
465
+ out.length = kept;
300
466
  const truncates = this.lineBreakMode === "TailTruncation" || this.lineBreakMode === "HeadTruncation" || this.lineBreakMode === "MiddleTruncation";
301
467
  if (truncates) {
302
- const last = out[this.maxLines - 1];
468
+ const last = out[kept - 1];
303
469
  const tailRun = last.Runs[last.Runs.length - 1];
304
470
  const fonts = tailRun ? tailRun.Fonts : this.mainFonts;
305
471
  const ell = this.Segment("…", fonts, tailRun?.Span);
306
472
  const ellW = this.Width(ell);
307
- while (last.Runs.length > 0 && last.Width + ellW > maxWidth) {
473
+ while (last.Runs.length > 0 && last.Width + ellW - sp > maxWidth) {
308
474
  const r = last.Runs[last.Runs.length - 1];
309
475
  const cps = Array.from(r.Text);
310
476
  cps.pop();
@@ -316,27 +482,140 @@ export class SkiaLabel extends SkiaControl {
316
482
  continue;
317
483
  }
318
484
  r.Text = cps.join("");
319
- r.Width = SkiaLabel.Advance(r.Font, r.Text);
485
+ r.Width = this.Advance(r.Font, r.Text);
320
486
  last.Width += r.Width;
321
487
  }
322
488
  this.Append(last, ell);
323
489
  }
324
490
  }
491
+ if (sp !== 0)
492
+ for (const l of out)
493
+ if (l.Runs.length > 0)
494
+ l.Width -= sp;
325
495
  return out;
326
496
  }
497
+ /** A paragraph is right to left when its first strong letter is (DrawnUi.Rust split_bidi). */
498
+ static FirstStrongIsRtl(text) {
499
+ for (const ch of text) {
500
+ const cp = ch.codePointAt(0);
501
+ if (IsRtl(cp))
502
+ return true;
503
+ if (/\p{L}/u.test(ch))
504
+ return false;
505
+ }
506
+ return false;
507
+ }
508
+ /** The runs of a line cut where its visual run changes; `map` per UTF-16 index of the line text. */
509
+ SplitByRuns(runs, map) {
510
+ const out = [];
511
+ let at = 0;
512
+ for (const r of runs) {
513
+ const piece = (from, to) => {
514
+ const text = r.Text.slice(from, to);
515
+ out.push({ ...r, Text: text, Width: from === 0 && to === r.Text.length ? r.Width : this.Advance(r.Font, text), Level: map.Rtl[at + from] ? 1 : 0, Rank: map.Rank[at + from], At: at + from, Shaped: undefined, X: undefined });
516
+ };
517
+ let from = 0;
518
+ for (let i = 0; i < r.Text.length;) {
519
+ if (i > 0 && map.Rank[at + i] !== map.Rank[at + from]) {
520
+ piece(from, i);
521
+ from = i;
522
+ }
523
+ i += r.Text.codePointAt(i) > 0xFFFF ? 2 : 1;
524
+ }
525
+ if (r.Text.length > 0)
526
+ piece(from, r.Text.length);
527
+ at += r.Text.length;
528
+ }
529
+ return out;
530
+ }
531
+ /**
532
+ * After the layout, lines whose text needs shaping or runs right to left (DrawnUi.Rust build_blobs): with
533
+ * right-to-left text every run is cut where its bidirectional run changes (Unicode bidirectional algorithm, ICU, the
534
+ * paragraph direction from its first strong letter); runs that are right to left or need shaping get shaped glyphs;
535
+ * runs are placed in visual order. Lines without such text are left as they are (plain path).
536
+ */
537
+ ShapeLines(lines) {
538
+ const sp = this.glyphSpacing;
539
+ const lineText = (l) => l.Runs.map((r) => r.Text).join("");
540
+ const anyRtl = lines.some((l) => l.Runs.some((r) => HasCodePoint(r.Text, IsRtl)));
541
+ let rtlParagraph = false;
542
+ for (let li = 0; li < lines.length; li++) {
543
+ const line = lines[li];
544
+ let bidi = false;
545
+ if (anyRtl) {
546
+ if (li === 0 || line.Para) {
547
+ let end = li + 1;
548
+ while (end < lines.length && !lines[end].Para)
549
+ end++;
550
+ rtlParagraph = SkiaLabel.FirstStrongIsRtl(lines.slice(li, end).map(lineText).join(""));
551
+ }
552
+ const map = BidiRuns(lineText(line), rtlParagraph);
553
+ if (map) {
554
+ line.Runs = this.SplitByRuns(line.Runs, map);
555
+ bidi = line.Runs.some((r) => (r.Level ?? 0) > 0);
556
+ }
557
+ }
558
+ let shaped = false;
559
+ for (const run of line.Runs) {
560
+ const rtl = (run.Level ?? 0) % 2 === 1;
561
+ if (!rtl && !HasCodePoint(run.Text, NeedsShaping))
562
+ continue;
563
+ const s = Shape(run.Font, run.Text, rtl, sp);
564
+ if (s) {
565
+ run.Shaped = s;
566
+ run.Width = s.Width;
567
+ shaped = true;
568
+ }
569
+ }
570
+ if (!shaped && !bidi)
571
+ continue;
572
+ let width = 0;
573
+ for (const run of line.Runs)
574
+ width += run.Width;
575
+ line.Width = line.Runs.length > 0 ? width - sp : 0;
576
+ if (bidi)
577
+ SkiaLabel.PlaceRuns(line.Runs);
578
+ }
579
+ }
580
+ /**
581
+ * Where each run of a line with right-to-left text goes: visual runs left to right; inside a run, pieces (spans, fonts)
582
+ * in logical order, reversed in a right-to-left run.
583
+ */
584
+ static PlaceRuns(runs) {
585
+ const order = runs.map((_, i) => i).sort((a, b) => {
586
+ const ra = runs[a], rb = runs[b];
587
+ if ((ra.Rank ?? 0) !== (rb.Rank ?? 0))
588
+ return (ra.Rank ?? 0) - (rb.Rank ?? 0);
589
+ return ((ra.Level ?? 0) % 2 === 1 ? -1 : 1) * ((ra.At ?? 0) - (rb.At ?? 0));
590
+ });
591
+ let x = 0;
592
+ for (const k of order) {
593
+ runs[k].X = x;
594
+ x += runs[k].Width;
595
+ }
596
+ }
327
597
  LineHeightPx(line) { return (line.Ascent + line.Descent) * this.lineHeight; }
598
+ /** ParagraphSpacing above a line that starts a paragraph (after the first): that many of its lines, with their spacing. */
599
+ ParagraphSpace(line) { return line.Para && this.paragraphSpacing ? this.LineHeightPx(line) * this.lineSpacing * this.paragraphSpacing : 0; }
328
600
  BlockHeight() {
329
601
  let h = 0;
330
602
  for (let i = 0; i < this.lines.length; i++) {
331
603
  const lh = this.LineHeightPx(this.lines[i]);
332
- h += i < this.lines.length - 1 ? lh * this.lineSpacing : lh;
604
+ h += (i < this.lines.length - 1 ? lh * this.lineSpacing : lh) + this.ParagraphSpace(this.lines[i]);
333
605
  }
334
606
  return h;
335
607
  }
336
- MeasureAbsolute(widthConstraint, _heightConstraint, scale) {
608
+ MeasureAbsolute(widthConstraint, heightConstraint, scale) {
337
609
  this.mainFonts = this.ResolveMainFonts(scale);
610
+ const glyphSpacing = scale * (this.characterSpacing - 1);
611
+ if (glyphSpacing !== this.glyphSpacing) {
612
+ this.glyphSpacing = glyphSpacing;
613
+ this.runCache.clear();
614
+ }
338
615
  const px = this.padding.HorizontalThickness * scale, py = this.padding.VerticalThickness * scale;
339
- this.lines = this.text || this.Spans.length > 0 ? this.LayoutLines(widthConstraint - px, scale) : [];
616
+ const extraH = this.EffectsExtra(scale).H;
617
+ this.lines = this.text || this.Spans.length > 0 ? this.LayoutLines(widthConstraint - px, scale, heightConstraint - py - extraH) : [];
618
+ this.ShapeLines(this.lines);
340
619
  let width = 0;
341
620
  for (const l of this.lines)
342
621
  width = Math.max(width, l.Width);
@@ -367,6 +646,7 @@ export class SkiaLabel extends SkiaControl {
367
646
  y += extra.Stroke / 2;
368
647
  const out = [];
369
648
  for (const line of this.lines) {
649
+ y += this.ParagraphSpace(line);
370
650
  const lh = this.LineHeightPx(line);
371
651
  const lineW = line.Width + extra.W;
372
652
  let x = left;
@@ -412,16 +692,30 @@ export class SkiaLabel extends SkiaControl {
412
692
  let start = g.text.length ? this.text.indexOf(g.text, cursor) : cursor;
413
693
  if (start < 0)
414
694
  start = cursor;
695
+ // boxes in logical order, each at its visual place: a right-to-left run fills from its right edge
415
696
  let x = g.x, index = start;
697
+ const sp = this.glyphSpacing;
416
698
  for (const run of line.Runs) {
699
+ if (run.X !== undefined)
700
+ x = g.x + run.X;
417
701
  const cps = Array.from(run.Text);
418
- const widths = run.Font.getGlyphWidths(run.Font.getGlyphIDs(run.Text, cps.length));
702
+ const widths = run.Shaped ? run.Shaped.Advances.map((a) => a - sp) : run.Font.getGlyphWidths(run.Font.getGlyphIDs(run.Text, cps.length));
703
+ const rtl = (run.Level ?? 0) % 2 === 1;
704
+ let right = x + run.Width - sp;
419
705
  cps.forEach((cp, k) => {
420
706
  const w = widths[k] ?? 0;
421
- out.push({ Index: index, Length: cp.length, Line: li, Left: x, Top: g.y, Width: w, Height: g.h });
422
- x += w;
707
+ if (rtl) {
708
+ out.push({ Index: index, Length: cp.length, Line: li, Left: right - w, Top: g.y, Width: w, Height: g.h, Rtl: true });
709
+ right -= w + sp;
710
+ }
711
+ else {
712
+ out.push({ Index: index, Length: cp.length, Line: li, Left: x, Top: g.y, Width: w, Height: g.h });
713
+ x += w + sp;
714
+ }
423
715
  index += cp.length;
424
716
  });
717
+ if (rtl)
718
+ x += run.Width;
425
719
  }
426
720
  cursor = start + g.text.length;
427
721
  });
@@ -501,9 +795,29 @@ export class SkiaLabel extends SkiaControl {
501
795
  return paint;
502
796
  };
503
797
  const canvas = ctx.Context.Canvas;
798
+ const sp = this.glyphSpacing;
799
+ // spaced text: every glyph placed with the gap after it (C# MeasureLineGlyphs positions)
800
+ const drawRun = (run, rx, ry, paint) => {
801
+ if (run.Shaped) {
802
+ canvas.drawGlyphs(run.Shaped.Glyphs, run.Shaped.Positions, rx, ry, run.Font, paint);
803
+ return;
804
+ }
805
+ if (sp === 0) {
806
+ canvas.drawText(run.Text, rx, ry, paint, run.Font);
807
+ return;
808
+ }
809
+ const ids = run.Font.getGlyphIDs(run.Text), widths = run.Font.getGlyphWidths(ids);
810
+ const positions = new Float32Array(ids.length * 2);
811
+ for (let k = 0, gx = 0; k < ids.length; k++) {
812
+ positions[k * 2] = gx;
813
+ gx += widths[k] + sp;
814
+ }
815
+ canvas.drawGlyphs(ids, positions, rx, ry, run.Font, paint);
816
+ };
504
817
  const gradient = this.FillGradient;
505
818
  const textRect = new SKRect(left, top, right, bottom);
506
819
  for (const line of this.lines) {
820
+ y += this.ParagraphSpace(line);
507
821
  const lh = this.LineHeightPx(line);
508
822
  const lineW = line.Width + extra.W;
509
823
  let x = left;
@@ -517,24 +831,29 @@ export class SkiaLabel extends SkiaControl {
517
831
  const gradientRect = gradient ? (this.GradientByLines ? new SKRect(x, y, x + line.Width, y + lh) : textRect) : undefined;
518
832
  if (strokePaint && this.StrokeGradient)
519
833
  this.SetupGradient(strokePaint, this.StrokeGradient, gradientRect ?? new SKRect(x, y, x + line.Width, y + lh));
834
+ const lineX = x;
520
835
  for (const run of line.Runs) {
836
+ // a line with right-to-left text places its runs in visual order
837
+ if (run.X !== undefined)
838
+ x = lineX + run.X;
521
839
  const span = run.Span;
840
+ const drawnW = run.Width - sp; // the gap after the run's last glyph is not part of it
522
841
  if (span) {
523
- span.Rects.push(new SKRect(x - d.Left, y - d.Top, x + run.Width - d.Left, y + lh - d.Top));
842
+ span.Rects.push(new SKRect(x - d.Left, y - d.Top, x + drawnW - d.Left, y + lh - d.Top));
524
843
  if (span.BackgroundColor)
525
- canvas.drawRect(CK.LTRBRect(x, y, x + run.Width, y + lh), paintFor(span.BackgroundColor));
844
+ canvas.drawRect(CK.LTRBRect(x, y, x + drawnW, y + lh), paintFor(span.BackgroundColor));
526
845
  }
527
846
  const color = span?.TextColor ?? this.textColor;
528
847
  if (run.Text) {
529
848
  // C# DrawText order: drop shadow, stroke, fill
530
849
  if (shadowPaint)
531
- canvas.drawText(run.Text, x + shadowDx, baseline + shadowDy, shadowPaint, run.Font);
850
+ drawRun(run, x + shadowDx, baseline + shadowDy, shadowPaint);
532
851
  if (strokePaint)
533
- canvas.drawText(run.Text, x, baseline, strokePaint, run.Font);
852
+ drawRun(run, x, baseline, strokePaint);
534
853
  const paint = paintFor(color);
535
854
  if (gradient && gradientRect)
536
855
  this.SetupGradient(paint, gradient, gradientRect);
537
- canvas.drawText(run.Text, x, baseline, paint, run.Font);
856
+ drawRun(run, x, baseline, paint);
538
857
  }
539
858
  if (span?.HasDecorations) {
540
859
  // same geometry as C# DrawSpanDecorations; CanvasKit exposes no underline/strikeout/x-height metrics,
@@ -542,11 +861,11 @@ export class SkiaLabel extends SkiaControl {
542
861
  if (span.Underline && span.UnderlineWidth !== 0) {
543
862
  const w = span.UnderlineWidth > 0 ? span.UnderlineWidth * scale : -span.UnderlineWidth;
544
863
  const yl = Math.round(baseline + scale);
545
- canvas.drawLine(x, yl, x + run.Width, yl, paintFor(color, w));
864
+ canvas.drawLine(x, yl, x + drawnW, yl, paintFor(color, w));
546
865
  }
547
866
  if (span.Strikeout) {
548
867
  const yl = Math.round(baseline - (run.Fonts.SizePx * 0.52) / 2);
549
- canvas.drawLine(x, yl, x + run.Width, yl, paintFor(span.StrikeoutColor, span.StrikeoutWidth * scale));
868
+ canvas.drawLine(x, yl, x + drawnW, yl, paintFor(span.StrikeoutColor, span.StrikeoutWidth * scale));
550
869
  }
551
870
  }
552
871
  x += run.Width;