drawnui-react 0.1.0-preview.4 → 0.1.0-preview.40

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 (105) hide show
  1. package/README.md +119 -14
  2. package/SKIPPED.md +36 -26
  3. package/dist/controls/AnimatedFramesRenderer.d.ts +9 -0
  4. package/dist/controls/AnimatedFramesRenderer.js +18 -1
  5. package/dist/controls/DrawnGame.d.ts +44 -0
  6. package/dist/controls/DrawnGame.js +82 -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 +17 -3
  12. package/dist/controls/SkiaButton.js +26 -8
  13. package/dist/controls/SkiaCarousel.d.ts +22 -3
  14. package/dist/controls/SkiaCarousel.js +85 -9
  15. package/dist/controls/SkiaCheckbox.d.ts +0 -1
  16. package/dist/controls/SkiaCheckbox.js +2 -7
  17. package/dist/controls/SkiaDrawer.d.ts +15 -0
  18. package/dist/controls/SkiaDrawer.js +42 -7
  19. package/dist/controls/SkiaEditor.d.ts +48 -2
  20. package/dist/controls/SkiaEditor.js +268 -53
  21. package/dist/controls/SkiaGif.d.ts +7 -0
  22. package/dist/controls/SkiaGif.js +25 -4
  23. package/dist/controls/SkiaImage.d.ts +5 -0
  24. package/dist/controls/SkiaImage.js +48 -14
  25. package/dist/controls/SkiaLabel.d.ts +117 -9
  26. package/dist/controls/SkiaLabel.js +526 -80
  27. package/dist/controls/SkiaLabelFps.d.ts +16 -0
  28. package/dist/controls/SkiaLabelFps.js +41 -0
  29. package/dist/controls/SkiaLayout.d.ts +89 -9
  30. package/dist/controls/SkiaLayout.js +364 -21
  31. package/dist/controls/SkiaLottie.js +5 -3
  32. package/dist/controls/SkiaProgress.d.ts +2 -1
  33. package/dist/controls/SkiaProgress.js +8 -5
  34. package/dist/controls/SkiaRadioButton.d.ts +2 -0
  35. package/dist/controls/SkiaRadioButton.js +16 -4
  36. package/dist/controls/SkiaRichLabel.d.ts +1 -1
  37. package/dist/controls/SkiaRichLabel.js +11 -1
  38. package/dist/controls/SkiaScroll.d.ts +35 -5
  39. package/dist/controls/SkiaScroll.js +252 -32
  40. package/dist/controls/SkiaScrollBar.d.ts +27 -0
  41. package/dist/controls/SkiaScrollBar.js +51 -2
  42. package/dist/controls/SkiaShaderCarousel.d.ts +11 -1
  43. package/dist/controls/SkiaShaderCarousel.js +27 -5
  44. package/dist/controls/SkiaShape.d.ts +13 -2
  45. package/dist/controls/SkiaShape.js +50 -10
  46. package/dist/controls/SkiaSlider.d.ts +20 -2
  47. package/dist/controls/SkiaSlider.js +61 -7
  48. package/dist/controls/SkiaSprite.d.ts +4 -0
  49. package/dist/controls/SkiaSprite.js +22 -10
  50. package/dist/controls/SkiaSvg.d.ts +4 -0
  51. package/dist/controls/SkiaSvg.js +28 -4
  52. package/dist/controls/SkiaSwitch.d.ts +0 -1
  53. package/dist/controls/SkiaSwitch.js +4 -8
  54. package/dist/controls/SkiaToggle.d.ts +25 -5
  55. package/dist/controls/SkiaToggle.js +58 -5
  56. package/dist/controls/SnappingLayout.d.ts +12 -1
  57. package/dist/controls/SnappingLayout.js +21 -2
  58. package/dist/controls/TooltipStyle.d.ts +52 -0
  59. package/dist/controls/TooltipStyle.js +88 -0
  60. package/dist/core/Accessibility.d.ts +134 -0
  61. package/dist/core/Accessibility.js +675 -9
  62. package/dist/core/Animators.d.ts +37 -1
  63. package/dist/core/Animators.js +102 -9
  64. package/dist/core/Canvas.d.ts +222 -10
  65. package/dist/core/Canvas.js +698 -37
  66. package/dist/core/ControlStyle.d.ts +2 -0
  67. package/dist/core/ControlStyle.js +2 -0
  68. package/dist/core/Easing.d.ts +12 -1
  69. package/dist/core/Easing.js +25 -1
  70. package/dist/core/Gestures.d.ts +81 -2
  71. package/dist/core/Gestures.js +177 -3
  72. package/dist/core/Hover.d.ts +51 -0
  73. package/dist/core/Hover.js +140 -0
  74. package/dist/core/ImageEffects.d.ts +2 -0
  75. package/dist/core/ImageEffects.js +2 -0
  76. package/dist/core/KeyboardManager.d.ts +40 -0
  77. package/dist/core/KeyboardManager.js +98 -8
  78. package/dist/core/PopupWindows.d.ts +94 -0
  79. package/dist/core/PopupWindows.js +205 -0
  80. package/dist/core/ScrollAnimators.js +9 -0
  81. package/dist/core/Shaping.d.ts +40 -0
  82. package/dist/core/Shaping.js +224 -0
  83. package/dist/core/SkiaControl.d.ts +279 -10
  84. package/dist/core/SkiaControl.js +621 -64
  85. package/dist/core/SkiaEffect.d.ts +17 -0
  86. package/dist/core/SkiaEffect.js +43 -8
  87. package/dist/core/SkiaImageManager.js +1 -1
  88. package/dist/core/Super.d.ts +29 -2
  89. package/dist/core/Super.js +54 -3
  90. package/dist/core/Tooltips.d.ts +71 -0
  91. package/dist/core/Tooltips.js +205 -0
  92. package/dist/core/Types.d.ts +11 -1
  93. package/dist/core/Types.js +32 -1
  94. package/dist/core/ViewsAdapter.d.ts +7 -2
  95. package/dist/core/ViewsAdapter.js +10 -3
  96. package/dist/index.d.ts +6 -0
  97. package/dist/index.js +6 -0
  98. package/dist/react/SkiaShell.js +65 -26
  99. package/dist/react/index.d.ts +33 -4
  100. package/dist/react/index.js +157 -18
  101. package/dist/react/reconciler.d.ts +20 -0
  102. package/dist/react/reconciler.js +69 -14
  103. package/dist/vite/index.d.ts +9 -0
  104. package/dist/vite/index.js +36 -3
  105. package/package.json +61 -2
@@ -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,24 @@ 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)); }
101
+ /** A label with text, in Text or in its spans, shows text of its own (no accessibility-label tooltip). */
102
+ ShowsText() { return !!this.text || this.Spans.some((s) => !!s.Text); }
81
103
  /** Number of laid-out lines after the last measure. */
82
104
  get LinesCount() { return this.lines.length; }
83
105
  // ---- spans as children (reconciler AddSubView / InsertSubView / RemoveSubView) ----
@@ -85,8 +107,14 @@ export class SkiaLabel extends SkiaControl {
85
107
  InsertSubView(index, control) {
86
108
  if (!(control instanceof TextSpan))
87
109
  throw new Error("DrawnUi: SkiaLabel children must be <TextSpan>");
110
+ const at = control.Parent === this ? this.Spans.indexOf(control) : -1; // already a span: a move, never a 2nd copy
111
+ if (at >= 0) {
112
+ this.Spans.splice(at, 1);
113
+ if (at < index)
114
+ index--;
115
+ }
88
116
  control.Parent = this;
89
- this.Spans.splice(index, 0, control);
117
+ this.Spans.splice(Math.min(Math.max(0, index), this.Spans.length), 0, control);
90
118
  this.Update();
91
119
  }
92
120
  RemoveSubView(control) {
@@ -101,7 +129,13 @@ export class SkiaLabel extends SkiaControl {
101
129
  lines = [];
102
130
  mainFonts;
103
131
  fontsCache = new Map();
132
+ /** Runs of a text per style (Segment), and the same runs carrying a span; cleared when the glyph spacing changes. */
104
133
  runCache = new Map();
134
+ spanRunCache = new WeakMap();
135
+ /** Wrap tokens of the text, kept while the label has not changed (NeedMeasure) at the same scale. */
136
+ tokens;
137
+ /** CharacterSpacing in pixels at the measured scale: added after every glyph of a run (its Width includes them). */
138
+ glyphSpacing = 0;
105
139
  ResolveFonts(family, weight, italic, sizePx) {
106
140
  const key = `${family}|${this.fontFamilyFallback}|${weight}|${italic}|${sizePx}`;
107
141
  let f = this.fontsCache.get(key);
@@ -131,27 +165,57 @@ export class SkiaLabel extends SkiaControl {
131
165
  const weight = span.FontWeight > 0 ? span.FontWeight : span.IsBold ? 700 : this.fontWeight > 0 ? this.fontWeight : bold ? 700 : 0;
132
166
  return this.ResolveFonts(span.FontFamily ?? this.fontFamily, weight, italic, (span.FontSize ?? this.fontSize) * scale);
133
167
  }
134
- static Advance(font, text) {
168
+ static NeedsShapingOrRtl(cp) { return NeedsShaping(cp) || IsRtl(cp); }
169
+ /** Width of a text in one font: the shaped glyphs where the text needs shaping (Arabic joins, emoji sequences), else glyph by glyph. */
170
+ Advance(font, text) {
171
+ if (HasCodePoint(text, SkiaLabel.NeedsShapingOrRtl)) {
172
+ const shaped = Shape(font, text, HasCodePoint(text, IsRtl), this.glyphSpacing);
173
+ if (shaped)
174
+ return shaped.Width;
175
+ }
135
176
  let w = 0;
136
177
  for (const adv of font.getGlyphWidths(font.getGlyphIDs(text)))
137
- w += adv;
178
+ w += adv + this.glyphSpacing;
138
179
  return w;
139
180
  }
181
+ static Ignorable = /[\u200B-\u200F\u2060-\u2064\uFE00-\uFE0F\u{E0000}-\u{E0FFF}]/u;
182
+ /**
183
+ * Default-ignorable code points (zero width space, joiners, direction marks, word joiners, variation selectors such
184
+ * as U+FE0F after an emoji, tags) that none of the fonts has draw nothing and take no room, never the missing-glyph
185
+ * box (DrawnUi.Rust label_ignorables; text is not shaped here, C# hides them by shaping).
186
+ */
187
+ static DropIgnorables(text, main, fallbacks) {
188
+ if (!SkiaLabel.Ignorable.test(text))
189
+ return text;
190
+ const has = (cp) => main.getGlyphIDs(cp)[0] !== 0 || fallbacks.some((f) => f.getGlyphIDs(cp)[0] !== 0);
191
+ return Array.from(text).filter((cp) => !SkiaLabel.Ignorable.test(cp) || has(cp)).join("");
192
+ }
140
193
  /**
141
194
  * 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).
195
+ * main font has glyph 0. Spaces always stay on the main font (fallback faces often carry very wide spaces). A joiner,
196
+ * a variation selector, a skin tone or a tag keeps the font of the glyph before it: an emoji sequence is one run.
143
197
  */
144
198
  Segment(text, fonts, span) {
145
199
  if (text.length === 0)
146
200
  return [];
147
- const cacheKey = fonts.Key + " " + text;
148
- let cached = this.runCache.get(cacheKey);
201
+ let byText = this.runCache.get(fonts);
202
+ if (!byText) {
203
+ byText = new Map();
204
+ this.runCache.set(fonts, byText);
205
+ }
206
+ let cached = byText.get(text);
207
+ const cacheKey = text;
149
208
  if (!cached) {
150
209
  const main = fonts.Main;
151
210
  const fbs = fonts.Fallbacks;
152
211
  cached = [];
212
+ text = SkiaLabel.DropIgnorables(text, main, fbs);
213
+ if (text.length === 0) {
214
+ byText.set(cacheKey, cached);
215
+ return cached;
216
+ }
153
217
  if (fbs.length === 0) {
154
- cached.push({ Text: text, Font: main, Width: SkiaLabel.Advance(main, text), Fonts: fonts });
218
+ cached.push({ Text: text, Font: main, Width: this.Advance(main, text), Fonts: fonts });
155
219
  }
156
220
  else {
157
221
  const cps = Array.from(text);
@@ -167,23 +231,111 @@ export class SkiaLabel extends SkiaControl {
167
231
  };
168
232
  let start = 0, current = fontFor(0);
169
233
  for (let i = 1; i < cps.length; i++) {
170
- const f = fontFor(i);
234
+ const f = JoinsPrevious(cps[i].codePointAt(0)) ? current : fontFor(i);
171
235
  if (f !== current) {
172
236
  const t = cps.slice(start, i).join("");
173
- cached.push({ Text: t, Font: current, Width: SkiaLabel.Advance(current, t), Fonts: fonts });
237
+ cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
174
238
  start = i;
175
239
  current = f;
176
240
  }
177
241
  }
178
242
  const t = cps.slice(start).join("");
179
- cached.push({ Text: t, Font: current, Width: SkiaLabel.Advance(current, t), Fonts: fonts });
243
+ cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
180
244
  }
181
- this.runCache.set(cacheKey, cached);
245
+ byText.set(cacheKey, cached);
246
+ }
247
+ if (!span)
248
+ return cached;
249
+ // the same runs carrying the span, made once per span, style and text
250
+ let bySpan = this.spanRunCache.get(span);
251
+ if (!bySpan) {
252
+ bySpan = new Map();
253
+ this.spanRunCache.set(span, bySpan);
254
+ }
255
+ let spanByText = bySpan.get(fonts);
256
+ if (!spanByText) {
257
+ spanByText = new Map();
258
+ bySpan.set(fonts, spanByText);
182
259
  }
183
- return span ? cached.map((r) => ({ ...r, Span: span })) : cached;
260
+ let withSpan = spanByText.get(cacheKey);
261
+ if (!withSpan) {
262
+ withSpan = cached.map((r) => ({ ...r, Span: span }));
263
+ spanByText.set(cacheKey, withSpan);
264
+ }
265
+ return withSpan;
184
266
  }
185
267
  Width(runs) { let w = 0; for (const r of runs)
186
268
  w += r.Width; return w; }
269
+ /** A line never starts with these (closing punctuation, small kana, the long vowel mark): JIS X 4051 kinsoku (C#). */
270
+ static NoBreakBefore = "、。,.・:;?!゛゜ヽヾゝゞ々〻ー」』)〕]}〉》】〗〙〟⦆»’”‐゠–〜~ぁぃぅぇぉっゃゅょゎゕゖァィゥェォッャュョヮヵヶㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ。」、・ーァィゥェォャュョッ)]},.!?:;%";
271
+ /** A line never ends with these (opening brackets and quotes). */
272
+ static NoBreakAfter = "「『(〔[{〈《【〖〘〝⦅«‘“「([{";
273
+ /** Chinese / Japanese: ideographs, kana, CJK punctuation, full-width forms (C# IsCjkAt). Korean breaks at spaces only. */
274
+ static Cjk = /[\u3000-\u312F\u3190-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFF00-\uFFEF\u{20000}-\u{3FFFF}]/u;
275
+ static CanBreakBetween(before, after) {
276
+ return (SkiaLabel.Cjk.test(before) || SkiaLabel.Cjk.test(after)) && !SkiaLabel.NoBreakBefore.includes(after) && !SkiaLabel.NoBreakAfter.includes(before);
277
+ }
278
+ /**
279
+ * C# CanBreakInsideWord: whether a line may break between text[index - 1] and text[index] (UTF-16 indexes) inside a
280
+ * space-free run: next to a Chinese or Japanese character, never before closing punctuation or small kana, never
281
+ * after an opening bracket, never inside a surrogate pair.
282
+ */
283
+ static CanBreakInsideWord(text, index) {
284
+ if (index <= 0 || index >= text.length)
285
+ return false;
286
+ const hi = (k) => { const c = text.charCodeAt(k); return c >= 0xD800 && c <= 0xDBFF; };
287
+ if (hi(index - 1))
288
+ return false; // inside a surrogate pair
289
+ const before = String.fromCodePoint(text.codePointAt(hi(index - 2) && index >= 2 ? index - 2 : index - 1));
290
+ const after = String.fromCodePoint(text.codePointAt(index));
291
+ return SkiaLabel.CanBreakBetween(before, after);
292
+ }
293
+ /**
294
+ * C# FitAtBreak: the last place inside the word where a Chinese / Japanese line may break and `prefix` plus the word
295
+ * up to there still fits; code points from the word's start, 0 = none.
296
+ */
297
+ FitCjk(word, prefix, maxWidth) {
298
+ if (!word.some((r) => SkiaLabel.Cjk.test(r.Text)))
299
+ return 0;
300
+ const sp = this.glyphSpacing;
301
+ let x = prefix, best = 0, before = "", k = 0;
302
+ for (const r of word) {
303
+ const cps = Array.from(r.Text), widths = r.Font.getGlyphWidths(r.Font.getGlyphIDs(r.Text, cps.length));
304
+ for (let i = 0; i < cps.length; i++) {
305
+ if (k > 0 && SkiaLabel.CanBreakBetween(before, cps[i])) {
306
+ if (x - sp > maxWidth)
307
+ return best;
308
+ best = k;
309
+ }
310
+ x += widths[i] + sp;
311
+ before = cps[i];
312
+ k++;
313
+ }
314
+ }
315
+ return best;
316
+ }
317
+ /** The word cut after `count` code points: the head goes on a line, the rest follows. */
318
+ SplitWord(word, count) {
319
+ const head = [], rest = [];
320
+ let left = count;
321
+ for (const r of word) {
322
+ const cps = Array.from(r.Text);
323
+ if (left >= cps.length) {
324
+ head.push(r);
325
+ left -= cps.length;
326
+ continue;
327
+ }
328
+ if (left > 0) {
329
+ const a = cps.slice(0, left).join(""), b = cps.slice(left).join("");
330
+ head.push({ ...r, Text: a, Width: this.Advance(r.Font, a) });
331
+ rest.push({ ...r, Text: b, Width: this.Advance(r.Font, b) });
332
+ left = 0;
333
+ continue;
334
+ }
335
+ rest.push(r);
336
+ }
337
+ return [head, rest];
338
+ }
187
339
  Transform(text) {
188
340
  switch (this.textTransform) {
189
341
  case "Uppercase": return text.toUpperCase();
@@ -199,10 +351,16 @@ export class SkiaLabel extends SkiaControl {
199
351
  Tokenize(scale) {
200
352
  const paragraphs = [[]];
201
353
  const add = (text, fonts, span) => {
202
- const parts = this.Transform(text).split("\n");
354
+ // "\n" starts a paragraph, U+2028 breaks the line inside one
355
+ const pieces = this.Transform(text).split(/(\n|\u2028)/);
356
+ const parts = pieces.filter((_, i) => i % 2 === 0), seps = pieces.filter((_, i) => i % 2 === 1);
203
357
  for (let p = 0; p < parts.length; p++) {
204
- if (p > 0)
205
- paragraphs.push([]);
358
+ if (p > 0) {
359
+ const next = [];
360
+ if (seps[p - 1] === "\u2028")
361
+ next.LineBreak = true;
362
+ paragraphs.push(next);
363
+ }
206
364
  const para = paragraphs[paragraphs.length - 1];
207
365
  const words = parts[p].split(" ");
208
366
  for (let w = 0; w < words.length; w++) {
@@ -222,11 +380,11 @@ export class SkiaLabel extends SkiaControl {
222
380
  else if (this.text) {
223
381
  add(this.text, this.mainFonts);
224
382
  }
225
- // a fragment ending with a space puts the space before the NEXT token
383
+ // a fragment ending with a space puts that space, in its own style, before the NEXT token
226
384
  for (const para of paragraphs) {
227
385
  for (let i = 0; i < para.length; i++) {
228
386
  if (para[i].TrailingSpace && i + 1 < para.length)
229
- para[i + 1].SpaceBefore = true;
387
+ para[i + 1].Carried = { Fonts: para[i].Fonts, Span: para[i].Span };
230
388
  }
231
389
  }
232
390
  return paragraphs;
@@ -248,63 +406,118 @@ export class SkiaLabel extends SkiaControl {
248
406
  line.Descent = r.Fonts.Descent;
249
407
  }
250
408
  }
251
- /** Word-wraps tokens into lines that fit maxWidth (Infinity = no wrap), applies MaxLines with a tail ellipsis. */
252
- LayoutLines(maxWidth, scale) {
409
+ /**
410
+ * Word-wraps tokens into lines that fit maxWidth (Infinity = no wrap); keeps the lines that fit maxHeight (at least one)
411
+ * and MaxLines, the last kept one ending with a tail ellipsis (C# / DrawnUi.Rust: a label in a box too short for its
412
+ * text cuts it, not only MaxLines).
413
+ */
414
+ /** The last layout broke a line because the text did not fit: its max-content is wider than the constraint. */
415
+ wrapped = false;
416
+ /** The last layout cut lines (MaxLines, height, an ellipsis): its lines depend on the room it had. */
417
+ truncated = false;
418
+ /**
419
+ * Lines laid out without wrapping or cutting stay the same for any room that holds them: reused, while nothing in the
420
+ * label changed (NeedMeasure), for a later measure with at least that width and height (C# 5f24f577 label line reuse).
421
+ */
422
+ reusable;
423
+ LayoutLines(maxWidth, scale, maxHeight = Infinity) {
424
+ this.wrapped = false;
425
+ this.truncated = false;
253
426
  const wrap = this.lineBreakMode !== "NoWrap" && isFinite(maxWidth);
427
+ // run widths carry the gap after every glyph; the one after a line's last glyph is not drawn
428
+ const sp = this.glyphSpacing;
254
429
  const out = [];
255
430
  const empty = (fonts) => { const l = this.NewLine(); l.Ascent = fonts.Ascent; l.Descent = fonts.Descent; return l; };
256
- for (const para of this.Tokenize(scale)) {
431
+ // the text's wrap tokens change only with the label (NeedMeasure) or the scale
432
+ if (this.NeedMeasure || !this.tokens || this.tokens.Scale !== scale)
433
+ this.tokens = { Scale: scale, Paragraphs: this.Tokenize(scale) };
434
+ for (const para of this.tokens.Paragraphs) {
257
435
  let line = this.NewLine();
436
+ if (out.length > 0 && !para.LineBreak)
437
+ line.Para = true;
258
438
  let lastFonts = this.mainFonts;
259
439
  for (let i = 0; i < para.length; i++) {
260
440
  const tok = para[i];
261
441
  lastFonts = tok.Fonts;
262
442
  // glued fragments (no space between spans) wrap as one word
263
443
  let word = this.Segment(tok.Text, tok.Fonts, tok.Span);
264
- while (i + 1 < para.length && !para[i + 1].SpaceBefore) {
444
+ while (i + 1 < para.length && !para[i + 1].SpaceBefore && !para[i + 1].Carried) {
265
445
  i++;
266
446
  word = word.concat(this.Segment(para[i].Text, para[i].Fonts, para[i].Span));
267
447
  }
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();
448
+ for (;;) {
449
+ // the space the fragment before ends with, then the word's own one, each in its style
450
+ const space = line.Runs.length === 0 ? [] : [
451
+ ...(tok.Carried ? this.Segment(" ", tok.Carried.Fonts, tok.Carried.Span) : []),
452
+ ...(tok.SpaceBefore ? this.Segment(" ", tok.Fonts, tok.Span) : []),
453
+ ];
454
+ const spaceW = this.Width(space), wordW = this.Width(word);
455
+ if (!wrap || line.Width + spaceW + wordW - sp <= maxWidth || (line.Runs.length === 0 && wordW - sp <= maxWidth)) {
456
+ this.Append(line, space);
457
+ this.Append(line, word);
458
+ break;
459
+ }
460
+ // Chinese / Japanese: the word fills the line up to its last break that fits (C# FitAtBreak)
461
+ const k = this.FitCjk(word, line.Width + spaceW, maxWidth);
462
+ if (k > 0) {
463
+ const [head, rest] = this.SplitWord(word, k);
464
+ this.Append(line, space);
465
+ this.Append(line, head);
466
+ out.push(line);
467
+ line = this.NewLine();
468
+ word = rest;
469
+ this.wrapped = true;
470
+ continue;
471
+ }
472
+ if (line.Runs.length > 0) {
473
+ out.push(line);
474
+ line = this.NewLine();
475
+ this.wrapped = true;
476
+ continue;
477
+ }
478
+ // word longer than the line: break by code points
479
+ for (const r of word) {
480
+ for (const ch of Array.from(r.Text)) {
481
+ const run = this.Segment(ch, r.Fonts, r.Span);
482
+ const chW = this.Width(run);
483
+ if (line.Runs.length > 0 && line.Width + chW - sp > maxWidth) {
484
+ out.push(line);
485
+ line = this.NewLine();
486
+ this.wrapped = true;
487
+ }
488
+ this.Append(line, run);
291
489
  }
292
- this.Append(line, run);
293
490
  }
491
+ break;
294
492
  }
295
493
  }
296
494
  out.push(line.Runs.length > 0 ? line : empty(lastFonts));
297
495
  }
298
- if (this.maxLines > 0 && out.length > this.maxLines) {
299
- out.length = this.maxLines;
496
+ let kept = this.maxLines > 0 ? Math.min(out.length, this.maxLines) : out.length;
497
+ if (isFinite(maxHeight)) {
498
+ let used = 0, fit = 0;
499
+ for (let i = 0; i < out.length; i++) {
500
+ const lh = this.LineHeightPx(out[i]);
501
+ // the block height of the first i + 1 lines: spacing after every line but the last, paragraph space above
502
+ const total = used + lh + (i > 0 ? this.ParagraphSpace(out[i]) : 0);
503
+ if (total > maxHeight && fit > 0)
504
+ break;
505
+ fit++;
506
+ used = total - lh + lh * this.lineSpacing;
507
+ }
508
+ kept = Math.min(kept, fit);
509
+ }
510
+ if (kept < out.length) {
511
+ this.truncated = true;
512
+ out.length = kept;
300
513
  const truncates = this.lineBreakMode === "TailTruncation" || this.lineBreakMode === "HeadTruncation" || this.lineBreakMode === "MiddleTruncation";
301
514
  if (truncates) {
302
- const last = out[this.maxLines - 1];
515
+ const last = out[kept - 1];
303
516
  const tailRun = last.Runs[last.Runs.length - 1];
304
517
  const fonts = tailRun ? tailRun.Fonts : this.mainFonts;
305
518
  const ell = this.Segment("…", fonts, tailRun?.Span);
306
519
  const ellW = this.Width(ell);
307
- while (last.Runs.length > 0 && last.Width + ellW > maxWidth) {
520
+ while (last.Runs.length > 0 && last.Width + ellW - sp > maxWidth) {
308
521
  const r = last.Runs[last.Runs.length - 1];
309
522
  const cps = Array.from(r.Text);
310
523
  cps.pop();
@@ -316,33 +529,227 @@ export class SkiaLabel extends SkiaControl {
316
529
  continue;
317
530
  }
318
531
  r.Text = cps.join("");
319
- r.Width = SkiaLabel.Advance(r.Font, r.Text);
532
+ r.Width = this.Advance(r.Font, r.Text);
320
533
  last.Width += r.Width;
321
534
  }
322
535
  this.Append(last, ell);
323
536
  }
324
537
  }
538
+ if (sp !== 0)
539
+ for (const l of out)
540
+ if (l.Runs.length > 0)
541
+ l.Width -= sp;
325
542
  return out;
326
543
  }
544
+ /** A paragraph is right to left when its first strong letter is (DrawnUi.Rust split_bidi). */
545
+ static FirstStrongIsRtl(text) {
546
+ for (const ch of text) {
547
+ const cp = ch.codePointAt(0);
548
+ if (IsRtl(cp))
549
+ return true;
550
+ if (/\p{L}/u.test(ch))
551
+ return false;
552
+ }
553
+ return false;
554
+ }
555
+ /** The runs of a line cut where its visual run changes; `map` per UTF-16 index of the line text. */
556
+ SplitByRuns(runs, map) {
557
+ const out = [];
558
+ let at = 0;
559
+ for (const r of runs) {
560
+ const piece = (from, to) => {
561
+ const text = r.Text.slice(from, to);
562
+ 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 });
563
+ };
564
+ let from = 0;
565
+ for (let i = 0; i < r.Text.length;) {
566
+ if (i > 0 && map.Rank[at + i] !== map.Rank[at + from]) {
567
+ piece(from, i);
568
+ from = i;
569
+ }
570
+ i += r.Text.codePointAt(i) > 0xFFFF ? 2 : 1;
571
+ }
572
+ if (r.Text.length > 0)
573
+ piece(from, r.Text.length);
574
+ at += r.Text.length;
575
+ }
576
+ return out;
577
+ }
578
+ /**
579
+ * After the layout, lines whose text needs shaping or runs right to left (DrawnUi.Rust build_blobs): with
580
+ * right-to-left text every run is cut where its bidirectional run changes (Unicode bidirectional algorithm, ICU, the
581
+ * paragraph direction from its first strong letter); runs that are right to left or need shaping get shaped glyphs;
582
+ * runs are placed in visual order. Lines without such text are left as they are (plain path).
583
+ */
584
+ ShapeLines(lines) {
585
+ const sp = this.glyphSpacing;
586
+ const lineText = (l) => l.Runs.map((r) => r.Text).join("");
587
+ const anyRtl = lines.some((l) => l.Runs.some((r) => HasCodePoint(r.Text, IsRtl)));
588
+ let rtlParagraph = false;
589
+ for (let li = 0; li < lines.length; li++) {
590
+ const line = lines[li];
591
+ let bidi = false;
592
+ if (anyRtl) {
593
+ if (li === 0 || line.Para) {
594
+ let end = li + 1;
595
+ while (end < lines.length && !lines[end].Para)
596
+ end++;
597
+ rtlParagraph = SkiaLabel.FirstStrongIsRtl(lines.slice(li, end).map(lineText).join(""));
598
+ }
599
+ line.Rtl = rtlParagraph;
600
+ const map = BidiRuns(lineText(line), rtlParagraph);
601
+ if (map) {
602
+ line.Runs = this.SplitByRuns(line.Runs, map);
603
+ bidi = line.Runs.some((r) => (r.Level ?? 0) > 0);
604
+ }
605
+ }
606
+ let shaped = false;
607
+ for (const run of line.Runs) {
608
+ const rtl = (run.Level ?? 0) % 2 === 1;
609
+ if (!rtl && !HasCodePoint(run.Text, NeedsShaping))
610
+ continue;
611
+ const s = Shape(run.Font, run.Text, rtl, sp);
612
+ if (s) {
613
+ run.Shaped = s;
614
+ run.Width = s.Width;
615
+ shaped = true;
616
+ }
617
+ }
618
+ if (!shaped && !bidi)
619
+ continue;
620
+ let width = 0;
621
+ for (const run of line.Runs)
622
+ width += run.Width;
623
+ line.Width = line.Runs.length > 0 ? width - sp : 0;
624
+ if (bidi)
625
+ SkiaLabel.PlaceRuns(line.Runs);
626
+ }
627
+ }
628
+ /**
629
+ * Where each run of a line with right-to-left text goes: visual runs left to right; inside a run, pieces (spans, fonts)
630
+ * in logical order, reversed in a right-to-left run.
631
+ */
632
+ static PlaceRuns(runs) {
633
+ const order = runs.map((_, i) => i).sort((a, b) => {
634
+ const ra = runs[a], rb = runs[b];
635
+ if ((ra.Rank ?? 0) !== (rb.Rank ?? 0))
636
+ return (ra.Rank ?? 0) - (rb.Rank ?? 0);
637
+ return ((ra.Level ?? 0) % 2 === 1 ? -1 : 1) * ((ra.At ?? 0) - (rb.At ?? 0));
638
+ });
639
+ let x = 0;
640
+ for (const k of order) {
641
+ runs[k].X = x;
642
+ x += runs[k].Width;
643
+ }
644
+ }
327
645
  LineHeightPx(line) { return (line.Ascent + line.Descent) * this.lineHeight; }
646
+ /** ParagraphSpacing above a line that starts a paragraph (after the first): that many of its lines, with their spacing. */
647
+ ParagraphSpace(line) { return line.Para && this.paragraphSpacing ? this.LineHeightPx(line) * this.lineSpacing * this.paragraphSpacing : 0; }
328
648
  BlockHeight() {
329
649
  let h = 0;
330
650
  for (let i = 0; i < this.lines.length; i++) {
331
651
  const lh = this.LineHeightPx(this.lines[i]);
332
- h += i < this.lines.length - 1 ? lh * this.lineSpacing : lh;
652
+ h += (i < this.lines.length - 1 ? lh * this.lineSpacing : lh) + this.ParagraphSpace(this.lines[i]);
333
653
  }
334
654
  return h;
335
655
  }
336
- MeasureAbsolute(widthConstraint, _heightConstraint, scale) {
656
+ /** Pixels of ink outside the lines' advance boxes, at their left and right ends (side bearings, italic slant). */
657
+ inkLeft = 0;
658
+ inkRight = 0;
659
+ /** Ink drawn outside the label's box (a "j" hook, an italic slant) is recorded by caches too, as C# draws it whole. */
660
+ ComputeEffectsMargin(scale) {
661
+ const m = super.ComputeEffectsMargin(scale);
662
+ if (this.inkRight === 0 && this.inkLeft === 0)
663
+ return m;
664
+ return new Thickness(m.Left + this.inkLeft, m.Top, m.Right + this.inkRight, m.Bottom);
665
+ }
666
+ /**
667
+ * C# (after 106e768a): the ink bounds of the first glyph of each line's leftmost run and the last glyph of its
668
+ * rightmost run, beyond the advance box of that run. Two glyph bounds per line, at measure.
669
+ */
670
+ MeasureInkOverhang() {
671
+ const sp = this.glyphSpacing;
672
+ let left = 0, right = 0;
673
+ for (const line of this.lines) {
674
+ const runs = line.Runs.filter((r) => r.Text.trim().length > 0);
675
+ if (runs.length === 0)
676
+ continue;
677
+ const first = runs.reduce((a, b) => ((b.X ?? 0) < (a.X ?? 0) ? b : a)), last = runs.reduce((a, b) => ((b.X ?? 0) >= (a.X ?? 0) ? b : a));
678
+ const firstX = first.X ?? 0, lastEnd = (last.X ?? line.Width) + (last.X === undefined ? 0 : last.Width - sp);
679
+ // ink x of a run's visually first / last glyph, from the line start
680
+ const ends = (r, x0) => {
681
+ if (r.Shaped) {
682
+ const g = r.Shaped.Glyphs, n = g.length;
683
+ if (n === 0)
684
+ return [x0, x0];
685
+ const b = r.Font.getGlyphBounds([g[0], g[n - 1]]);
686
+ return [x0 + r.Shaped.Positions[0] + b[0], x0 + r.Shaped.Positions[(n - 1) * 2] + b[6]];
687
+ }
688
+ // only the first and the last character matter
689
+ const first = String.fromCodePoint(r.Text.codePointAt(0));
690
+ const lastAt = r.Text.length - (r.Text.length > 1 && r.Text.charCodeAt(r.Text.length - 1) >= 0xDC00 && r.Text.charCodeAt(r.Text.length - 1) <= 0xDFFF ? 2 : 1);
691
+ const ids = r.Font.getGlyphIDs(first + r.Text.slice(lastAt));
692
+ const n = ids.length;
693
+ const b = r.Font.getGlyphBounds([ids[0], ids[n - 1]]);
694
+ const lastAdvance = r.Font.getGlyphWidths([ids[n - 1]])[0];
695
+ return [x0 + b[0], x0 + r.Width - sp - lastAdvance + b[6]];
696
+ };
697
+ // a line without placed runs (no bidi): its runs follow each other from x 0, the last ends at the line width
698
+ const lastX0 = last.X ?? (line.Width - (last.Width - sp));
699
+ left = Math.max(left, firstX - ends(first, firstX)[0]);
700
+ right = Math.max(right, ends(last, lastX0)[1] - (last.X === undefined ? line.Width : lastEnd));
701
+ }
702
+ return [Math.max(0, left), Math.max(0, right)];
703
+ }
704
+ /**
705
+ * Where a line starts inside [left, right] (drawnui-cross 6am): Start and End follow the paragraph's direction (in a
706
+ * right-to-left paragraph Start is the right edge); Center is centered; Fill* lines are not stretched here and sit
707
+ * at the paragraph's start.
708
+ */
709
+ LineX(line, left, right, lineW) {
710
+ const a = this.horizontalTextAlignment;
711
+ if (a === "Center")
712
+ return left + (right - left - lineW) / 2;
713
+ const atEnd = a === "End" ? !line.Rtl : !!line.Rtl;
714
+ return atEnd ? right - lineW : left;
715
+ }
716
+ MeasureAbsolute(widthConstraint, heightConstraint, scale) {
337
717
  this.mainFonts = this.ResolveMainFonts(scale);
718
+ const glyphSpacing = scale * (this.characterSpacing - 1);
719
+ if (glyphSpacing !== this.glyphSpacing) {
720
+ this.glyphSpacing = glyphSpacing;
721
+ this.runCache = new Map();
722
+ this.spanRunCache = new WeakMap();
723
+ }
338
724
  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) : [];
725
+ const extraH = this.EffectsExtra(scale).H;
726
+ const roomW = widthConstraint - px, roomH = heightConstraint - py - extraH;
727
+ const r = this.reusable;
728
+ if (this.NeedMeasure || !r || r.Scale !== scale || r.Spacing !== glyphSpacing || roomW < r.Width - 0.01 || roomH < r.Height - 0.01) {
729
+ this.lines = this.text || this.Spans.length > 0 ? this.LayoutLines(roomW, scale, roomH) : [];
730
+ this.ShapeLines(this.lines);
731
+ // ink past the lines' ends (a "j" hook, an italic slant) is drawing room for caches; widths unchanged
732
+ const [inkLeft, inkRight] = this.MeasureInkOverhang();
733
+ if (inkLeft !== this.inkLeft || inkRight !== this.inkRight) {
734
+ this.inkLeft = inkLeft;
735
+ this.inkRight = inkRight;
736
+ this.InvalidateEffectsMargin();
737
+ }
738
+ let widest = 0;
739
+ for (const l of this.lines)
740
+ widest = Math.max(widest, l.Width);
741
+ this.reusable = this.wrapped || this.truncated ? undefined : { Scale: scale, Spacing: glyphSpacing, Width: widest, Height: this.BlockHeight() };
742
+ }
340
743
  let width = 0;
341
744
  for (const l of this.lines)
342
745
  width = Math.max(width, l.Width);
343
746
  const extra = this.EffectsExtra(scale);
344
747
  this.LayoutVersion++;
345
- return ScaledSize.FromPixels(Math.ceil(width + extra.W) + px, Math.ceil(this.BlockHeight() + extra.H) + py, scale);
748
+ const size = ScaledSize.FromPixels(Math.ceil(width + extra.W) + px, Math.ceil(this.BlockHeight() + extra.H) + py, scale);
749
+ // fit-content (drawnui-cross 6an): text that had to wrap needs the whole width it was given (min of max-content and it)
750
+ if (this.wrapped && isFinite(widthConstraint))
751
+ this.fitContentPixels = { Width: widthConstraint, Height: size.Pixels.Height };
752
+ return size;
346
753
  }
347
754
  /** C# GradientByLines: FillGradient spans each line's bounds (default) instead of the whole text block. */
348
755
  GradientByLines = true;
@@ -367,13 +774,10 @@ export class SkiaLabel extends SkiaControl {
367
774
  y += extra.Stroke / 2;
368
775
  const out = [];
369
776
  for (const line of this.lines) {
777
+ y += this.ParagraphSpace(line);
370
778
  const lh = this.LineHeightPx(line);
371
779
  const lineW = line.Width + extra.W;
372
- let x = left;
373
- if (this.horizontalTextAlignment === "Center")
374
- x = left + (right - left - lineW) / 2;
375
- else if (this.horizontalTextAlignment === "End")
376
- x = right - lineW;
780
+ let x = this.LineX(line, left, right, lineW);
377
781
  x += extra.Stroke / 2;
378
782
  out.push({ x, y, w: line.Width, h: lh, text: line.Runs.map((r) => r.Text).join("") });
379
783
  y += lh * this.lineSpacing;
@@ -412,16 +816,30 @@ export class SkiaLabel extends SkiaControl {
412
816
  let start = g.text.length ? this.text.indexOf(g.text, cursor) : cursor;
413
817
  if (start < 0)
414
818
  start = cursor;
819
+ // boxes in logical order, each at its visual place: a right-to-left run fills from its right edge
415
820
  let x = g.x, index = start;
821
+ const sp = this.glyphSpacing;
416
822
  for (const run of line.Runs) {
823
+ if (run.X !== undefined)
824
+ x = g.x + run.X;
417
825
  const cps = Array.from(run.Text);
418
- const widths = run.Font.getGlyphWidths(run.Font.getGlyphIDs(run.Text, cps.length));
826
+ const widths = run.Shaped ? run.Shaped.Advances.map((a) => a - sp) : run.Font.getGlyphWidths(run.Font.getGlyphIDs(run.Text, cps.length));
827
+ const rtl = (run.Level ?? 0) % 2 === 1;
828
+ let right = x + run.Width - sp;
419
829
  cps.forEach((cp, k) => {
420
830
  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;
831
+ if (rtl) {
832
+ out.push({ Index: index, Length: cp.length, Line: li, Left: right - w, Top: g.y, Width: w, Height: g.h, Rtl: true });
833
+ right -= w + sp;
834
+ }
835
+ else {
836
+ out.push({ Index: index, Length: cp.length, Line: li, Left: x, Top: g.y, Width: w, Height: g.h });
837
+ x += w + sp;
838
+ }
423
839
  index += cp.length;
424
840
  });
841
+ if (rtl)
842
+ x += run.Width;
425
843
  }
426
844
  cursor = start + g.text.length;
427
845
  });
@@ -445,8 +863,14 @@ export class SkiaLabel extends SkiaControl {
445
863
  const sh = this.HasDropShadow() ? (this.DropShadowSize + this.DropShadowOffsetY) * scale : 0;
446
864
  return { W: stroke + sw, H: stroke + sh, Stroke: stroke };
447
865
  }
448
- /** C# SkiaLabel.SetupBackgroundPaint: no BackgroundColor = no background, the gradient goes on the glyphs. */
449
- FillGradientPaintsBackground() { return !!this.BackgroundColor; }
866
+ /**
867
+ * FillGradient goes to one place (C# 9637994b, as DrawnUi.Rust): the background of a label that paints one (a
868
+ * BackgroundColor with alpha above 0), whose glyphs keep TextColor; else the glyphs.
869
+ */
870
+ FillGradientPaintsBackground() { return this.HasBackground(); }
871
+ HasBackground() { return !!this.BackgroundColor && Super.ParseColor(this.BackgroundColor)[3] > 0; }
872
+ /** The gradient the glyphs are painted with: none when the label's background takes FillGradient. */
873
+ get TextFillGradient() { return this.HasBackground() ? undefined : this.FillGradient; }
450
874
  Paint(ctx) {
451
875
  if (this.lines.length === 0 || !this.mainFonts)
452
876
  return;
@@ -501,40 +925,61 @@ export class SkiaLabel extends SkiaControl {
501
925
  return paint;
502
926
  };
503
927
  const canvas = ctx.Context.Canvas;
504
- const gradient = this.FillGradient;
928
+ const sp = this.glyphSpacing;
929
+ // spaced text: every glyph placed with the gap after it (C# MeasureLineGlyphs positions)
930
+ const drawRun = (run, rx, ry, paint) => {
931
+ if (run.Shaped) {
932
+ canvas.drawGlyphs(run.Shaped.Glyphs, run.Shaped.Positions, rx, ry, run.Font, paint);
933
+ return;
934
+ }
935
+ if (sp === 0) {
936
+ canvas.drawText(run.Text, rx, ry, paint, run.Font);
937
+ return;
938
+ }
939
+ const ids = run.Font.getGlyphIDs(run.Text), widths = run.Font.getGlyphWidths(ids);
940
+ const positions = new Float32Array(ids.length * 2);
941
+ for (let k = 0, gx = 0; k < ids.length; k++) {
942
+ positions[k * 2] = gx;
943
+ gx += widths[k] + sp;
944
+ }
945
+ canvas.drawGlyphs(ids, positions, rx, ry, run.Font, paint);
946
+ };
947
+ const gradient = this.TextFillGradient;
505
948
  const textRect = new SKRect(left, top, right, bottom);
506
949
  for (const line of this.lines) {
950
+ y += this.ParagraphSpace(line);
507
951
  const lh = this.LineHeightPx(line);
508
952
  const lineW = line.Width + extra.W;
509
- let x = left;
510
- if (this.horizontalTextAlignment === "Center")
511
- x = left + (right - left - lineW) / 2;
512
- else if (this.horizontalTextAlignment === "End")
513
- x = right - lineW;
953
+ let x = this.LineX(line, left, right, lineW);
514
954
  x += extra.Stroke / 2;
515
955
  const baseline = y + line.Ascent;
516
956
  // C# paintDefault gets the gradient over rectDraw, or over line.Bounds per line (GradientByLines)
517
957
  const gradientRect = gradient ? (this.GradientByLines ? new SKRect(x, y, x + line.Width, y + lh) : textRect) : undefined;
518
958
  if (strokePaint && this.StrokeGradient)
519
959
  this.SetupGradient(strokePaint, this.StrokeGradient, gradientRect ?? new SKRect(x, y, x + line.Width, y + lh));
960
+ const lineX = x;
520
961
  for (const run of line.Runs) {
962
+ // a line with right-to-left text places its runs in visual order
963
+ if (run.X !== undefined)
964
+ x = lineX + run.X;
521
965
  const span = run.Span;
966
+ const drawnW = run.Width - sp; // the gap after the run's last glyph is not part of it
522
967
  if (span) {
523
- span.Rects.push(new SKRect(x - d.Left, y - d.Top, x + run.Width - d.Left, y + lh - d.Top));
968
+ span.Rects.push(new SKRect(x - d.Left, y - d.Top, x + drawnW - d.Left, y + lh - d.Top));
524
969
  if (span.BackgroundColor)
525
- canvas.drawRect(CK.LTRBRect(x, y, x + run.Width, y + lh), paintFor(span.BackgroundColor));
970
+ canvas.drawRect(CK.LTRBRect(x, y, x + drawnW, y + lh), paintFor(span.BackgroundColor));
526
971
  }
527
972
  const color = span?.TextColor ?? this.textColor;
528
973
  if (run.Text) {
529
974
  // C# DrawText order: drop shadow, stroke, fill
530
975
  if (shadowPaint)
531
- canvas.drawText(run.Text, x + shadowDx, baseline + shadowDy, shadowPaint, run.Font);
976
+ drawRun(run, x + shadowDx, baseline + shadowDy, shadowPaint);
532
977
  if (strokePaint)
533
- canvas.drawText(run.Text, x, baseline, strokePaint, run.Font);
978
+ drawRun(run, x, baseline, strokePaint);
534
979
  const paint = paintFor(color);
535
980
  if (gradient && gradientRect)
536
981
  this.SetupGradient(paint, gradient, gradientRect);
537
- canvas.drawText(run.Text, x, baseline, paint, run.Font);
982
+ drawRun(run, x, baseline, paint);
538
983
  }
539
984
  if (span?.HasDecorations) {
540
985
  // same geometry as C# DrawSpanDecorations; CanvasKit exposes no underline/strikeout/x-height metrics,
@@ -542,11 +987,11 @@ export class SkiaLabel extends SkiaControl {
542
987
  if (span.Underline && span.UnderlineWidth !== 0) {
543
988
  const w = span.UnderlineWidth > 0 ? span.UnderlineWidth * scale : -span.UnderlineWidth;
544
989
  const yl = Math.round(baseline + scale);
545
- canvas.drawLine(x, yl, x + run.Width, yl, paintFor(color, w));
990
+ canvas.drawLine(x, yl, x + drawnW, yl, paintFor(color, w));
546
991
  }
547
992
  if (span.Strikeout) {
548
993
  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));
994
+ canvas.drawLine(x, yl, x + drawnW, yl, paintFor(span.StrikeoutColor, span.StrikeoutWidth * scale));
550
995
  }
551
996
  }
552
997
  x += run.Width;
@@ -565,7 +1010,8 @@ export class SkiaLabel extends SkiaControl {
565
1010
  const y = apply.MappedLocation.Y + apply.ChildOffset.Y - this.DrawingRect.Top;
566
1011
  for (const span of this.Spans) {
567
1012
  if (span.HasTapHandler && span.HitIsInside(x, y)) {
568
- this.PlayRippleAnimation(this.TouchEffectColor, x / this.RenderingScale, y / this.RenderingScale);
1013
+ if (SkiaControl.IsPrimaryPointer(args))
1014
+ this.PlayRippleAnimation(this.TouchEffectColor, x / this.RenderingScale, y / this.RenderingScale);
569
1015
  return this.OnSpanTapped(span, args, apply);
570
1016
  }
571
1017
  }