drawnui-react 0.1.0-preview.8 → 1.0.1
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/README.md +229 -22
- package/SKIPPED.md +38 -29
- package/dist/controls/AnimatedFramesRenderer.d.ts +9 -0
- package/dist/controls/AnimatedFramesRenderer.js +18 -1
- package/dist/controls/DrawnGame.d.ts +44 -0
- package/dist/controls/DrawnGame.js +82 -0
- package/dist/controls/GridStructure.d.ts +25 -3
- package/dist/controls/GridStructure.js +81 -5
- package/dist/controls/SkiaBackdrop.d.ts +8 -2
- package/dist/controls/SkiaBackdrop.js +42 -14
- package/dist/controls/SkiaButton.d.ts +17 -3
- package/dist/controls/SkiaButton.js +26 -8
- package/dist/controls/SkiaCarousel.d.ts +22 -3
- package/dist/controls/SkiaCarousel.js +85 -9
- package/dist/controls/SkiaCheckbox.d.ts +0 -1
- package/dist/controls/SkiaCheckbox.js +2 -7
- package/dist/controls/SkiaDrawer.d.ts +15 -0
- package/dist/controls/SkiaDrawer.js +42 -7
- package/dist/controls/SkiaEditor.d.ts +51 -2
- package/dist/controls/SkiaEditor.js +292 -58
- package/dist/controls/SkiaGif.d.ts +7 -0
- package/dist/controls/SkiaGif.js +25 -4
- package/dist/controls/SkiaImage.d.ts +10 -0
- package/dist/controls/SkiaImage.js +80 -19
- package/dist/controls/SkiaLabel.d.ts +200 -11
- package/dist/controls/SkiaLabel.js +925 -93
- package/dist/controls/SkiaLabelFps.d.ts +12 -0
- package/dist/controls/SkiaLabelFps.js +35 -0
- package/dist/controls/SkiaLayout.d.ts +89 -9
- package/dist/controls/SkiaLayout.js +397 -23
- package/dist/controls/SkiaLottie.js +5 -3
- package/dist/controls/SkiaProgress.d.ts +2 -1
- package/dist/controls/SkiaProgress.js +8 -5
- package/dist/controls/SkiaRadioButton.d.ts +2 -0
- package/dist/controls/SkiaRadioButton.js +16 -4
- package/dist/controls/SkiaRichLabel.d.ts +1 -1
- package/dist/controls/SkiaRichLabel.js +11 -1
- package/dist/controls/SkiaScroll.d.ts +86 -7
- package/dist/controls/SkiaScroll.js +361 -40
- package/dist/controls/SkiaScrollBar.d.ts +27 -0
- package/dist/controls/SkiaScrollBar.js +51 -2
- package/dist/controls/SkiaShaderCarousel.d.ts +11 -1
- package/dist/controls/SkiaShaderCarousel.js +27 -5
- package/dist/controls/SkiaShape.d.ts +15 -2
- package/dist/controls/SkiaShape.js +57 -10
- package/dist/controls/SkiaShell.d.ts +282 -0
- package/dist/controls/SkiaShell.js +972 -0
- package/dist/controls/SkiaSlider.d.ts +20 -2
- package/dist/controls/SkiaSlider.js +61 -7
- package/dist/controls/SkiaSprite.d.ts +4 -0
- package/dist/controls/SkiaSprite.js +27 -11
- package/dist/controls/SkiaSvg.d.ts +4 -0
- package/dist/controls/SkiaSvg.js +28 -4
- package/dist/controls/SkiaSwitch.d.ts +0 -1
- package/dist/controls/SkiaSwitch.js +4 -8
- package/dist/controls/SkiaToggle.d.ts +25 -5
- package/dist/controls/SkiaToggle.js +58 -5
- package/dist/controls/SnappingLayout.d.ts +12 -1
- package/dist/controls/SnappingLayout.js +21 -2
- package/dist/controls/TooltipStyle.d.ts +52 -0
- package/dist/controls/TooltipStyle.js +88 -0
- package/dist/core/Accessibility.d.ts +162 -11
- package/dist/core/Accessibility.js +882 -22
- package/dist/core/Animators.d.ts +37 -1
- package/dist/core/Animators.js +102 -9
- package/dist/core/Canvas.d.ts +307 -8
- package/dist/core/Canvas.js +1202 -66
- package/dist/core/ControlStyle.d.ts +2 -0
- package/dist/core/ControlStyle.js +2 -0
- package/dist/core/DesignerActions.d.ts +32 -0
- package/dist/core/DesignerActions.js +68 -0
- package/dist/core/Easing.d.ts +12 -1
- package/dist/core/Easing.js +25 -1
- package/dist/core/Gestures.d.ts +86 -2
- package/dist/core/Gestures.js +184 -4
- package/dist/core/Hover.d.ts +51 -0
- package/dist/core/Hover.js +140 -0
- package/dist/core/ImageEffects.d.ts +2 -0
- package/dist/core/ImageEffects.js +2 -0
- package/dist/core/KeyboardManager.d.ts +40 -0
- package/dist/core/KeyboardManager.js +98 -8
- package/dist/core/PopupWindows.d.ts +94 -0
- package/dist/core/PopupWindows.js +207 -0
- package/dist/core/Props.d.ts +11 -0
- package/dist/core/Props.js +44 -0
- package/dist/core/ScrollAnimators.d.ts +6 -0
- package/dist/core/ScrollAnimators.js +16 -1
- package/dist/core/Shaping.d.ts +40 -0
- package/dist/core/Shaping.js +224 -0
- package/dist/core/SkiaControl.d.ts +306 -13
- package/dist/core/SkiaControl.js +681 -75
- package/dist/core/SkiaEffect.d.ts +29 -1
- package/dist/core/SkiaEffect.js +78 -16
- package/dist/core/SkiaImageManager.d.ts +101 -18
- package/dist/core/SkiaImageManager.js +318 -63
- package/dist/core/Styles.js +7 -1
- package/dist/core/Super.d.ts +33 -2
- package/dist/core/Super.js +58 -12
- package/dist/core/TextInputProxy.d.ts +31 -1
- package/dist/core/TextInputProxy.js +97 -15
- package/dist/core/Tooltips.d.ts +71 -0
- package/dist/core/Tooltips.js +205 -0
- package/dist/core/Types.d.ts +11 -1
- package/dist/core/Types.js +32 -1
- package/dist/core/ViewsAdapter.d.ts +7 -2
- package/dist/core/ViewsAdapter.js +10 -3
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/react/SkiaShell.d.ts +8 -74
- package/dist/react/SkiaShell.js +163 -475
- package/dist/react/index.d.ts +48 -4
- package/dist/react/index.js +210 -43
- package/dist/react/reconciler.d.ts +32 -0
- package/dist/react/reconciler.js +71 -40
- package/dist/vite/index.d.ts +9 -0
- package/dist/vite/index.js +36 -3
- package/package.json +103 -5
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { SkiaControl } from "../core/SkiaControl";
|
|
2
2
|
import { ControlTappedEventArgs } from "../core/Gestures";
|
|
3
|
+
import { KeyboardManager } from "../core/KeyboardManager";
|
|
3
4
|
import { Super } from "../core/Super";
|
|
4
5
|
import { Colors, ScaledSize, SKRect, Thickness, } from "../core/Types";
|
|
5
6
|
import { TextSpan } from "./TextSpan";
|
|
7
|
+
import { BidiRuns, HasCodePoint, IsRtl, JoinsPrevious, NeedsShaping, Shape } from "../core/Shaping";
|
|
6
8
|
/**
|
|
7
9
|
* Mirrors DrawnUi SkiaLabel: multi-line text with word wrapping, MaxLines + tail ellipsis, horizontal /
|
|
8
10
|
* vertical alignment, LineSpacing / LineHeight, weights and attributes resolved through the font registry,
|
|
@@ -24,12 +26,21 @@ export class SkiaLabel extends SkiaControl {
|
|
|
24
26
|
verticalTextAlignment = "Start";
|
|
25
27
|
lineSpacing = 1;
|
|
26
28
|
lineHeight = 1;
|
|
29
|
+
paragraphSpacing = 0.25;
|
|
30
|
+
lineHeightUniform = false;
|
|
31
|
+
/** The height every line takes under LineHeightUniform: the tallest laid-out line's (C# MeasuredLineHeight). */
|
|
32
|
+
uniformLineHeight = 0;
|
|
33
|
+
characterSpacing = 1;
|
|
27
34
|
textTransform = "None";
|
|
28
35
|
padding = Thickness.Zero;
|
|
29
36
|
/** Styled fragments; when not empty they replace `Text` (same precedence as C#). */
|
|
30
37
|
Spans = [];
|
|
31
38
|
/** Set to `Aria.RoleText` to expose every label to screen readers (React extension; C# is opt-in per control). */
|
|
32
39
|
static DefaultAccessibilityRole;
|
|
40
|
+
/** Highlight drawn over selected text (AccessibilityTextSelectable), for every label (C# SkiaLabel.TextSelectionColor). */
|
|
41
|
+
static TextSelectionColor = "rgba(13,110,253,0.353)";
|
|
42
|
+
/** Caption of the Copy button shown over a touch selection (C# SkiaLabel.CopyButtonText). */
|
|
43
|
+
static CopyButtonText = "Copy";
|
|
33
44
|
/** Like C# OnTextInternalChanged: the spoken label is the text (or the joined spans) unless AccessibilityLabel is set. */
|
|
34
45
|
DefaultAccessibilityLabel() {
|
|
35
46
|
return this.Spans.length > 0 ? this.Spans.map((s) => s.Text).join("") : this.text || undefined;
|
|
@@ -43,12 +54,18 @@ export class SkiaLabel extends SkiaControl {
|
|
|
43
54
|
this[key] = v;
|
|
44
55
|
this.Update();
|
|
45
56
|
} }
|
|
57
|
+
/** A property that changes only the look (a color): no layout change, see UpdateDraw. */
|
|
58
|
+
SetLook(key, v) { if (this[key] !== v) {
|
|
59
|
+
this[key] = v;
|
|
60
|
+
this.UpdateDraw();
|
|
61
|
+
} }
|
|
46
62
|
get Text() { return this.text; }
|
|
47
|
-
set Text(v) { this.
|
|
63
|
+
set Text(v) { if (v !== this.text && this.selLength > 0)
|
|
64
|
+
this.ClearSelection(); this.Set("text", v); }
|
|
48
65
|
get FontSize() { return this.fontSize; }
|
|
49
66
|
set FontSize(v) { this.Set("fontSize", v); }
|
|
50
67
|
get TextColor() { return this.textColor; }
|
|
51
|
-
set TextColor(v) { this.
|
|
68
|
+
set TextColor(v) { this.SetLook("textColor", v); }
|
|
52
69
|
get FontFamily() { return this.fontFamily; }
|
|
53
70
|
set FontFamily(v) { this.Set("fontFamily", v); }
|
|
54
71
|
/**
|
|
@@ -74,10 +91,27 @@ export class SkiaLabel extends SkiaControl {
|
|
|
74
91
|
/** Multiplier applied to the natural line height (ascent + descent). */
|
|
75
92
|
get LineHeight() { return this.lineHeight; }
|
|
76
93
|
set LineHeight(v) { this.Set("lineHeight", v); }
|
|
94
|
+
/**
|
|
95
|
+
* Space above every paragraph after the first, in lines of the label's font (C# ParagraphSpacing, default 0.25). A
|
|
96
|
+
* paragraph starts after "\n"; U+2028 breaks the line inside a paragraph.
|
|
97
|
+
*/
|
|
98
|
+
get ParagraphSpacing() { return this.paragraphSpacing; }
|
|
99
|
+
set ParagraphSpacing(v) { this.Set("paragraphSpacing", v); }
|
|
100
|
+
/** LineHeightUniform: true = every line as tall as the tallest one; false (default in every engine) = each line its own height. */
|
|
101
|
+
get LineHeightUniform() { return this.lineHeightUniform; }
|
|
102
|
+
set LineHeightUniform(v) { this.Set("lineHeightUniform", v); }
|
|
103
|
+
/**
|
|
104
|
+
* C# CharacterSpacing: 1 = the font's own spacing; every glyph is followed by (CharacterSpacing - 1) points more
|
|
105
|
+
* (less below 1), the line measures and wraps at the width it draws (C# 7cf1007c, DrawnUi.Rust label).
|
|
106
|
+
*/
|
|
107
|
+
get CharacterSpacing() { return this.characterSpacing; }
|
|
108
|
+
set CharacterSpacing(v) { this.Set("characterSpacing", v); }
|
|
77
109
|
get TextTransform() { return this.textTransform; }
|
|
78
110
|
set TextTransform(v) { this.Set("textTransform", v); }
|
|
79
111
|
get Padding() { return this.padding; }
|
|
80
|
-
set Padding(v) { this.Set("padding", v); }
|
|
112
|
+
set Padding(v) { this.Set("padding", Thickness.From(v)); }
|
|
113
|
+
/** A label with text, in Text or in its spans, shows text of its own (no accessibility-label tooltip). */
|
|
114
|
+
ShowsText() { return !!this.text || this.Spans.some((s) => !!s.Text); }
|
|
81
115
|
/** Number of laid-out lines after the last measure. */
|
|
82
116
|
get LinesCount() { return this.lines.length; }
|
|
83
117
|
// ---- spans as children (reconciler AddSubView / InsertSubView / RemoveSubView) ----
|
|
@@ -85,8 +119,14 @@ export class SkiaLabel extends SkiaControl {
|
|
|
85
119
|
InsertSubView(index, control) {
|
|
86
120
|
if (!(control instanceof TextSpan))
|
|
87
121
|
throw new Error("DrawnUi: SkiaLabel children must be <TextSpan>");
|
|
122
|
+
const at = control.Parent === this ? this.Spans.indexOf(control) : -1; // already a span: a move, never a 2nd copy
|
|
123
|
+
if (at >= 0) {
|
|
124
|
+
this.Spans.splice(at, 1);
|
|
125
|
+
if (at < index)
|
|
126
|
+
index--;
|
|
127
|
+
}
|
|
88
128
|
control.Parent = this;
|
|
89
|
-
this.Spans.splice(index, 0, control);
|
|
129
|
+
this.Spans.splice(Math.min(Math.max(0, index), this.Spans.length), 0, control);
|
|
90
130
|
this.Update();
|
|
91
131
|
}
|
|
92
132
|
RemoveSubView(control) {
|
|
@@ -101,7 +141,13 @@ export class SkiaLabel extends SkiaControl {
|
|
|
101
141
|
lines = [];
|
|
102
142
|
mainFonts;
|
|
103
143
|
fontsCache = new Map();
|
|
144
|
+
/** Runs of a text per style (Segment), and the same runs carrying a span; cleared when the glyph spacing changes. */
|
|
104
145
|
runCache = new Map();
|
|
146
|
+
spanRunCache = new WeakMap();
|
|
147
|
+
/** Wrap tokens of the text, kept while the label has not changed (NeedMeasure) at the same scale. */
|
|
148
|
+
tokens;
|
|
149
|
+
/** CharacterSpacing in pixels at the measured scale: added after every glyph of a run (its Width includes them). */
|
|
150
|
+
glyphSpacing = 0;
|
|
105
151
|
ResolveFonts(family, weight, italic, sizePx) {
|
|
106
152
|
const key = `${family}|${this.fontFamilyFallback}|${weight}|${italic}|${sizePx}`;
|
|
107
153
|
let f = this.fontsCache.get(key);
|
|
@@ -110,7 +156,7 @@ export class SkiaLabel extends SkiaControl {
|
|
|
110
156
|
const main = Super.GetFont(family, weight, italic, sizePx);
|
|
111
157
|
const m = main.getMetrics();
|
|
112
158
|
f = {
|
|
113
|
-
Key: key, Main: main, SizePx: sizePx, Ascent: -m.ascent, Descent: m.descent,
|
|
159
|
+
Key: key, Main: main, SizePx: sizePx, Ascent: -m.ascent, Descent: m.descent, Leading: m.leading,
|
|
114
160
|
Fallbacks: this.fontFamilyFallback
|
|
115
161
|
? this.fontFamilyFallback.split(",").map((a) => a.trim()).filter(Boolean).map((a) => Super.GetFont(a, weight, italic, sizePx))
|
|
116
162
|
: [],
|
|
@@ -131,27 +177,57 @@ export class SkiaLabel extends SkiaControl {
|
|
|
131
177
|
const weight = span.FontWeight > 0 ? span.FontWeight : span.IsBold ? 700 : this.fontWeight > 0 ? this.fontWeight : bold ? 700 : 0;
|
|
132
178
|
return this.ResolveFonts(span.FontFamily ?? this.fontFamily, weight, italic, (span.FontSize ?? this.fontSize) * scale);
|
|
133
179
|
}
|
|
134
|
-
static
|
|
180
|
+
static NeedsShapingOrRtl(cp) { return NeedsShaping(cp) || IsRtl(cp); }
|
|
181
|
+
/** Width of a text in one font: the shaped glyphs where the text needs shaping (Arabic joins, emoji sequences), else glyph by glyph. */
|
|
182
|
+
Advance(font, text) {
|
|
183
|
+
if (HasCodePoint(text, SkiaLabel.NeedsShapingOrRtl)) {
|
|
184
|
+
const shaped = Shape(font, text, HasCodePoint(text, IsRtl), this.glyphSpacing);
|
|
185
|
+
if (shaped)
|
|
186
|
+
return shaped.Width;
|
|
187
|
+
}
|
|
135
188
|
let w = 0;
|
|
136
189
|
for (const adv of font.getGlyphWidths(font.getGlyphIDs(text)))
|
|
137
|
-
w += adv;
|
|
190
|
+
w += adv + this.glyphSpacing;
|
|
138
191
|
return w;
|
|
139
192
|
}
|
|
193
|
+
static Ignorable = /[\u200B-\u200F\u2060-\u2064\uFE00-\uFE0F\u{E0000}-\u{E0FFF}]/u;
|
|
194
|
+
/**
|
|
195
|
+
* Default-ignorable code points (zero width space, joiners, direction marks, word joiners, variation selectors such
|
|
196
|
+
* as U+FE0F after an emoji, tags) that none of the fonts has draw nothing and take no room, never the missing-glyph
|
|
197
|
+
* box (DrawnUi.Rust label_ignorables; text is not shaped here, C# hides them by shaping).
|
|
198
|
+
*/
|
|
199
|
+
static DropIgnorables(text, main, fallbacks) {
|
|
200
|
+
if (!SkiaLabel.Ignorable.test(text))
|
|
201
|
+
return text;
|
|
202
|
+
const has = (cp) => main.getGlyphIDs(cp)[0] !== 0 || fallbacks.some((f) => f.getGlyphIDs(cp)[0] !== 0);
|
|
203
|
+
return Array.from(text).filter((cp) => !SkiaLabel.Ignorable.test(cp) || has(cp)).join("");
|
|
204
|
+
}
|
|
140
205
|
/**
|
|
141
206
|
* 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).
|
|
207
|
+
* main font has glyph 0. Spaces always stay on the main font (fallback faces often carry very wide spaces). A joiner,
|
|
208
|
+
* a variation selector, a skin tone or a tag keeps the font of the glyph before it: an emoji sequence is one run.
|
|
143
209
|
*/
|
|
144
210
|
Segment(text, fonts, span) {
|
|
145
211
|
if (text.length === 0)
|
|
146
212
|
return [];
|
|
147
|
-
|
|
148
|
-
|
|
213
|
+
let byText = this.runCache.get(fonts);
|
|
214
|
+
if (!byText) {
|
|
215
|
+
byText = new Map();
|
|
216
|
+
this.runCache.set(fonts, byText);
|
|
217
|
+
}
|
|
218
|
+
let cached = byText.get(text);
|
|
219
|
+
const cacheKey = text;
|
|
149
220
|
if (!cached) {
|
|
150
221
|
const main = fonts.Main;
|
|
151
222
|
const fbs = fonts.Fallbacks;
|
|
152
223
|
cached = [];
|
|
224
|
+
text = SkiaLabel.DropIgnorables(text, main, fbs);
|
|
225
|
+
if (text.length === 0) {
|
|
226
|
+
byText.set(cacheKey, cached);
|
|
227
|
+
return cached;
|
|
228
|
+
}
|
|
153
229
|
if (fbs.length === 0) {
|
|
154
|
-
cached.push({ Text: text, Font: main, Width:
|
|
230
|
+
cached.push({ Text: text, Font: main, Width: this.Advance(main, text), Fonts: fonts });
|
|
155
231
|
}
|
|
156
232
|
else {
|
|
157
233
|
const cps = Array.from(text);
|
|
@@ -167,23 +243,111 @@ export class SkiaLabel extends SkiaControl {
|
|
|
167
243
|
};
|
|
168
244
|
let start = 0, current = fontFor(0);
|
|
169
245
|
for (let i = 1; i < cps.length; i++) {
|
|
170
|
-
const f = fontFor(i);
|
|
246
|
+
const f = JoinsPrevious(cps[i].codePointAt(0)) ? current : fontFor(i);
|
|
171
247
|
if (f !== current) {
|
|
172
248
|
const t = cps.slice(start, i).join("");
|
|
173
|
-
cached.push({ Text: t, Font: current, Width:
|
|
249
|
+
cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
|
|
174
250
|
start = i;
|
|
175
251
|
current = f;
|
|
176
252
|
}
|
|
177
253
|
}
|
|
178
254
|
const t = cps.slice(start).join("");
|
|
179
|
-
cached.push({ Text: t, Font: current, Width:
|
|
255
|
+
cached.push({ Text: t, Font: current, Width: this.Advance(current, t), Fonts: fonts });
|
|
180
256
|
}
|
|
181
|
-
|
|
257
|
+
byText.set(cacheKey, cached);
|
|
258
|
+
}
|
|
259
|
+
if (!span)
|
|
260
|
+
return cached;
|
|
261
|
+
// the same runs carrying the span, made once per span, style and text
|
|
262
|
+
let bySpan = this.spanRunCache.get(span);
|
|
263
|
+
if (!bySpan) {
|
|
264
|
+
bySpan = new Map();
|
|
265
|
+
this.spanRunCache.set(span, bySpan);
|
|
266
|
+
}
|
|
267
|
+
let spanByText = bySpan.get(fonts);
|
|
268
|
+
if (!spanByText) {
|
|
269
|
+
spanByText = new Map();
|
|
270
|
+
bySpan.set(fonts, spanByText);
|
|
182
271
|
}
|
|
183
|
-
|
|
272
|
+
let withSpan = spanByText.get(cacheKey);
|
|
273
|
+
if (!withSpan) {
|
|
274
|
+
withSpan = cached.map((r) => ({ ...r, Span: span }));
|
|
275
|
+
spanByText.set(cacheKey, withSpan);
|
|
276
|
+
}
|
|
277
|
+
return withSpan;
|
|
184
278
|
}
|
|
185
279
|
Width(runs) { let w = 0; for (const r of runs)
|
|
186
280
|
w += r.Width; return w; }
|
|
281
|
+
/** A line never starts with these (closing punctuation, small kana, the long vowel mark): JIS X 4051 kinsoku (C#). */
|
|
282
|
+
static NoBreakBefore = "、。,.・:;?!゛゜ヽヾゝゞ々〻ー」』)〕]}〉》】〗〙〟⦆»’”‐゠–〜~ぁぃぅぇぉっゃゅょゎゕゖァィゥェォッャュョヮヵヶㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ。」、・ーァィゥェォャュョッ)]},.!?:;%";
|
|
283
|
+
/** A line never ends with these (opening brackets and quotes). */
|
|
284
|
+
static NoBreakAfter = "「『(〔[{〈《【〖〘〝⦅«‘“「([{";
|
|
285
|
+
/** Chinese / Japanese: ideographs, kana, CJK punctuation, full-width forms (C# IsCjkAt). Korean breaks at spaces only. */
|
|
286
|
+
static Cjk = /[\u3000-\u312F\u3190-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFF00-\uFFEF\u{20000}-\u{3FFFF}]/u;
|
|
287
|
+
static CanBreakBetween(before, after) {
|
|
288
|
+
return (SkiaLabel.Cjk.test(before) || SkiaLabel.Cjk.test(after)) && !SkiaLabel.NoBreakBefore.includes(after) && !SkiaLabel.NoBreakAfter.includes(before);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* C# CanBreakInsideWord: whether a line may break between text[index - 1] and text[index] (UTF-16 indexes) inside a
|
|
292
|
+
* space-free run: next to a Chinese or Japanese character, never before closing punctuation or small kana, never
|
|
293
|
+
* after an opening bracket, never inside a surrogate pair.
|
|
294
|
+
*/
|
|
295
|
+
static CanBreakInsideWord(text, index) {
|
|
296
|
+
if (index <= 0 || index >= text.length)
|
|
297
|
+
return false;
|
|
298
|
+
const hi = (k) => { const c = text.charCodeAt(k); return c >= 0xD800 && c <= 0xDBFF; };
|
|
299
|
+
if (hi(index - 1))
|
|
300
|
+
return false; // inside a surrogate pair
|
|
301
|
+
const before = String.fromCodePoint(text.codePointAt(hi(index - 2) && index >= 2 ? index - 2 : index - 1));
|
|
302
|
+
const after = String.fromCodePoint(text.codePointAt(index));
|
|
303
|
+
return SkiaLabel.CanBreakBetween(before, after);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* C# FitAtBreak: the last place inside the word where a Chinese / Japanese line may break and `prefix` plus the word
|
|
307
|
+
* up to there still fits; code points from the word's start, 0 = none.
|
|
308
|
+
*/
|
|
309
|
+
FitCjk(word, prefix, maxWidth) {
|
|
310
|
+
if (!word.some((r) => SkiaLabel.Cjk.test(r.Text)))
|
|
311
|
+
return 0;
|
|
312
|
+
const sp = this.glyphSpacing;
|
|
313
|
+
let x = prefix, best = 0, before = "", k = 0;
|
|
314
|
+
for (const r of word) {
|
|
315
|
+
const cps = Array.from(r.Text), widths = r.Font.getGlyphWidths(r.Font.getGlyphIDs(r.Text, cps.length));
|
|
316
|
+
for (let i = 0; i < cps.length; i++) {
|
|
317
|
+
if (k > 0 && SkiaLabel.CanBreakBetween(before, cps[i])) {
|
|
318
|
+
if (x - sp > maxWidth)
|
|
319
|
+
return best;
|
|
320
|
+
best = k;
|
|
321
|
+
}
|
|
322
|
+
x += widths[i] + sp;
|
|
323
|
+
before = cps[i];
|
|
324
|
+
k++;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return best;
|
|
328
|
+
}
|
|
329
|
+
/** The word cut after `count` code points: the head goes on a line, the rest follows. */
|
|
330
|
+
SplitWord(word, count) {
|
|
331
|
+
const head = [], rest = [];
|
|
332
|
+
let left = count;
|
|
333
|
+
for (const r of word) {
|
|
334
|
+
const cps = Array.from(r.Text);
|
|
335
|
+
if (left >= cps.length) {
|
|
336
|
+
head.push(r);
|
|
337
|
+
left -= cps.length;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (left > 0) {
|
|
341
|
+
const a = cps.slice(0, left).join(""), b = cps.slice(left).join("");
|
|
342
|
+
head.push({ ...r, Text: a, Width: this.Advance(r.Font, a) });
|
|
343
|
+
rest.push({ ...r, Text: b, Width: this.Advance(r.Font, b) });
|
|
344
|
+
left = 0;
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
rest.push(r);
|
|
348
|
+
}
|
|
349
|
+
return [head, rest];
|
|
350
|
+
}
|
|
187
351
|
Transform(text) {
|
|
188
352
|
switch (this.textTransform) {
|
|
189
353
|
case "Uppercase": return text.toUpperCase();
|
|
@@ -199,10 +363,16 @@ export class SkiaLabel extends SkiaControl {
|
|
|
199
363
|
Tokenize(scale) {
|
|
200
364
|
const paragraphs = [[]];
|
|
201
365
|
const add = (text, fonts, span) => {
|
|
202
|
-
|
|
366
|
+
// "\n" starts a paragraph, U+2028 breaks the line inside one
|
|
367
|
+
const pieces = this.Transform(text).split(/(\n|\u2028)/);
|
|
368
|
+
const parts = pieces.filter((_, i) => i % 2 === 0), seps = pieces.filter((_, i) => i % 2 === 1);
|
|
203
369
|
for (let p = 0; p < parts.length; p++) {
|
|
204
|
-
if (p > 0)
|
|
205
|
-
|
|
370
|
+
if (p > 0) {
|
|
371
|
+
const next = [];
|
|
372
|
+
if (seps[p - 1] === "\u2028")
|
|
373
|
+
next.LineBreak = true;
|
|
374
|
+
paragraphs.push(next);
|
|
375
|
+
}
|
|
206
376
|
const para = paragraphs[paragraphs.length - 1];
|
|
207
377
|
const words = parts[p].split(" ");
|
|
208
378
|
for (let w = 0; w < words.length; w++) {
|
|
@@ -222,11 +392,11 @@ export class SkiaLabel extends SkiaControl {
|
|
|
222
392
|
else if (this.text) {
|
|
223
393
|
add(this.text, this.mainFonts);
|
|
224
394
|
}
|
|
225
|
-
// a fragment ending with a space puts
|
|
395
|
+
// a fragment ending with a space puts that space, in its own style, before the NEXT token
|
|
226
396
|
for (const para of paragraphs) {
|
|
227
397
|
for (let i = 0; i < para.length; i++) {
|
|
228
398
|
if (para[i].TrailingSpace && i + 1 < para.length)
|
|
229
|
-
para[i + 1].
|
|
399
|
+
para[i + 1].Carried = { Fonts: para[i].Fonts, Span: para[i].Span };
|
|
230
400
|
}
|
|
231
401
|
}
|
|
232
402
|
return paragraphs;
|
|
@@ -248,63 +418,118 @@ export class SkiaLabel extends SkiaControl {
|
|
|
248
418
|
line.Descent = r.Fonts.Descent;
|
|
249
419
|
}
|
|
250
420
|
}
|
|
251
|
-
/**
|
|
252
|
-
|
|
421
|
+
/**
|
|
422
|
+
* Word-wraps tokens into lines that fit maxWidth (Infinity = no wrap); keeps the lines that fit maxHeight (at least one)
|
|
423
|
+
* and MaxLines, the last kept one ending with a tail ellipsis (C# / DrawnUi.Rust: a label in a box too short for its
|
|
424
|
+
* text cuts it, not only MaxLines).
|
|
425
|
+
*/
|
|
426
|
+
/** The last layout broke a line because the text did not fit: its max-content is wider than the constraint. */
|
|
427
|
+
wrapped = false;
|
|
428
|
+
/** The last layout cut lines (MaxLines, height, an ellipsis): its lines depend on the room it had. */
|
|
429
|
+
truncated = false;
|
|
430
|
+
/**
|
|
431
|
+
* Lines laid out without wrapping or cutting stay the same for any room that holds them: reused, while nothing in the
|
|
432
|
+
* label changed (NeedMeasure), for a later measure with at least that width and height (C# 5f24f577 label line reuse).
|
|
433
|
+
*/
|
|
434
|
+
reusable;
|
|
435
|
+
LayoutLines(maxWidth, scale, maxHeight = Infinity) {
|
|
436
|
+
this.wrapped = false;
|
|
437
|
+
this.truncated = false;
|
|
253
438
|
const wrap = this.lineBreakMode !== "NoWrap" && isFinite(maxWidth);
|
|
439
|
+
// run widths carry the gap after every glyph; the one after a line's last glyph is not drawn
|
|
440
|
+
const sp = this.glyphSpacing;
|
|
254
441
|
const out = [];
|
|
255
442
|
const empty = (fonts) => { const l = this.NewLine(); l.Ascent = fonts.Ascent; l.Descent = fonts.Descent; return l; };
|
|
256
|
-
|
|
443
|
+
// the text's wrap tokens change only with the label (NeedMeasure) or the scale
|
|
444
|
+
if (this.NeedMeasure || !this.tokens || this.tokens.Scale !== scale)
|
|
445
|
+
this.tokens = { Scale: scale, Paragraphs: this.Tokenize(scale) };
|
|
446
|
+
for (const para of this.tokens.Paragraphs) {
|
|
257
447
|
let line = this.NewLine();
|
|
448
|
+
if (out.length > 0 && !para.LineBreak)
|
|
449
|
+
line.Para = true;
|
|
258
450
|
let lastFonts = this.mainFonts;
|
|
259
451
|
for (let i = 0; i < para.length; i++) {
|
|
260
452
|
const tok = para[i];
|
|
261
453
|
lastFonts = tok.Fonts;
|
|
262
454
|
// glued fragments (no space between spans) wrap as one word
|
|
263
455
|
let word = this.Segment(tok.Text, tok.Fonts, tok.Span);
|
|
264
|
-
while (i + 1 < para.length && !para[i + 1].SpaceBefore) {
|
|
456
|
+
while (i + 1 < para.length && !para[i + 1].SpaceBefore && !para[i + 1].Carried) {
|
|
265
457
|
i++;
|
|
266
458
|
word = word.concat(this.Segment(para[i].Text, para[i].Fonts, para[i].Span));
|
|
267
459
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
460
|
+
for (;;) {
|
|
461
|
+
// the space the fragment before ends with, then the word's own one, each in its style
|
|
462
|
+
const space = line.Runs.length === 0 ? [] : [
|
|
463
|
+
...(tok.Carried ? this.Segment(" ", tok.Carried.Fonts, tok.Carried.Span) : []),
|
|
464
|
+
...(tok.SpaceBefore ? this.Segment(" ", tok.Fonts, tok.Span) : []),
|
|
465
|
+
];
|
|
466
|
+
const spaceW = this.Width(space), wordW = this.Width(word);
|
|
467
|
+
if (!wrap || line.Width + spaceW + wordW - sp <= maxWidth || (line.Runs.length === 0 && wordW - sp <= maxWidth)) {
|
|
468
|
+
this.Append(line, space);
|
|
469
|
+
this.Append(line, word);
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
// Chinese / Japanese: the word fills the line up to its last break that fits (C# FitAtBreak)
|
|
473
|
+
const k = this.FitCjk(word, line.Width + spaceW, maxWidth);
|
|
474
|
+
if (k > 0) {
|
|
475
|
+
const [head, rest] = this.SplitWord(word, k);
|
|
476
|
+
this.Append(line, space);
|
|
477
|
+
this.Append(line, head);
|
|
478
|
+
out.push(line);
|
|
479
|
+
line = this.NewLine();
|
|
480
|
+
word = rest;
|
|
481
|
+
this.wrapped = true;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (line.Runs.length > 0) {
|
|
485
|
+
out.push(line);
|
|
486
|
+
line = this.NewLine();
|
|
487
|
+
this.wrapped = true;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
// word longer than the line: break by code points
|
|
491
|
+
for (const r of word) {
|
|
492
|
+
for (const ch of Array.from(r.Text)) {
|
|
493
|
+
const run = this.Segment(ch, r.Fonts, r.Span);
|
|
494
|
+
const chW = this.Width(run);
|
|
495
|
+
if (line.Runs.length > 0 && line.Width + chW - sp > maxWidth) {
|
|
496
|
+
out.push(line);
|
|
497
|
+
line = this.NewLine();
|
|
498
|
+
this.wrapped = true;
|
|
499
|
+
}
|
|
500
|
+
this.Append(line, run);
|
|
291
501
|
}
|
|
292
|
-
this.Append(line, run);
|
|
293
502
|
}
|
|
503
|
+
break;
|
|
294
504
|
}
|
|
295
505
|
}
|
|
296
506
|
out.push(line.Runs.length > 0 ? line : empty(lastFonts));
|
|
297
507
|
}
|
|
298
|
-
|
|
299
|
-
|
|
508
|
+
let kept = this.maxLines > 0 ? Math.min(out.length, this.maxLines) : out.length;
|
|
509
|
+
if (isFinite(maxHeight)) {
|
|
510
|
+
let used = 0, fit = 0;
|
|
511
|
+
for (let i = 0; i < out.length; i++) {
|
|
512
|
+
const lh = this.OwnLineHeight(out[i]);
|
|
513
|
+
// the block height of the first i + 1 lines: spacing after every line but the last, paragraph space above
|
|
514
|
+
const total = used + lh + (i > 0 ? this.ParagraphSpace(out[i]) : 0);
|
|
515
|
+
if (total > maxHeight && fit > 0)
|
|
516
|
+
break;
|
|
517
|
+
fit++;
|
|
518
|
+
used = total + this.LineGap(lh);
|
|
519
|
+
}
|
|
520
|
+
kept = Math.min(kept, fit);
|
|
521
|
+
}
|
|
522
|
+
if (kept < out.length) {
|
|
523
|
+
this.truncated = true;
|
|
524
|
+
out.length = kept;
|
|
300
525
|
const truncates = this.lineBreakMode === "TailTruncation" || this.lineBreakMode === "HeadTruncation" || this.lineBreakMode === "MiddleTruncation";
|
|
301
526
|
if (truncates) {
|
|
302
|
-
const last = out[
|
|
527
|
+
const last = out[kept - 1];
|
|
303
528
|
const tailRun = last.Runs[last.Runs.length - 1];
|
|
304
529
|
const fonts = tailRun ? tailRun.Fonts : this.mainFonts;
|
|
305
530
|
const ell = this.Segment("…", fonts, tailRun?.Span);
|
|
306
531
|
const ellW = this.Width(ell);
|
|
307
|
-
while (last.Runs.length > 0 && last.Width + ellW > maxWidth) {
|
|
532
|
+
while (last.Runs.length > 0 && last.Width + ellW - sp > maxWidth) {
|
|
308
533
|
const r = last.Runs[last.Runs.length - 1];
|
|
309
534
|
const cps = Array.from(r.Text);
|
|
310
535
|
cps.pop();
|
|
@@ -316,33 +541,247 @@ export class SkiaLabel extends SkiaControl {
|
|
|
316
541
|
continue;
|
|
317
542
|
}
|
|
318
543
|
r.Text = cps.join("");
|
|
319
|
-
r.Width =
|
|
544
|
+
r.Width = this.Advance(r.Font, r.Text);
|
|
320
545
|
last.Width += r.Width;
|
|
321
546
|
}
|
|
322
547
|
this.Append(last, ell);
|
|
323
548
|
}
|
|
324
549
|
}
|
|
550
|
+
if (sp !== 0)
|
|
551
|
+
for (const l of out)
|
|
552
|
+
if (l.Runs.length > 0)
|
|
553
|
+
l.Width -= sp;
|
|
554
|
+
return out;
|
|
555
|
+
}
|
|
556
|
+
/** A paragraph is right to left when its first strong letter is (DrawnUi.Rust split_bidi). */
|
|
557
|
+
static FirstStrongIsRtl(text) {
|
|
558
|
+
for (const ch of text) {
|
|
559
|
+
const cp = ch.codePointAt(0);
|
|
560
|
+
if (IsRtl(cp))
|
|
561
|
+
return true;
|
|
562
|
+
if (/\p{L}/u.test(ch))
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
/** The runs of a line cut where its visual run changes; `map` per UTF-16 index of the line text. */
|
|
568
|
+
SplitByRuns(runs, map) {
|
|
569
|
+
const out = [];
|
|
570
|
+
let at = 0;
|
|
571
|
+
for (const r of runs) {
|
|
572
|
+
const piece = (from, to) => {
|
|
573
|
+
const text = r.Text.slice(from, to);
|
|
574
|
+
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 });
|
|
575
|
+
};
|
|
576
|
+
let from = 0;
|
|
577
|
+
for (let i = 0; i < r.Text.length;) {
|
|
578
|
+
if (i > 0 && map.Rank[at + i] !== map.Rank[at + from]) {
|
|
579
|
+
piece(from, i);
|
|
580
|
+
from = i;
|
|
581
|
+
}
|
|
582
|
+
i += r.Text.codePointAt(i) > 0xFFFF ? 2 : 1;
|
|
583
|
+
}
|
|
584
|
+
if (r.Text.length > 0)
|
|
585
|
+
piece(from, r.Text.length);
|
|
586
|
+
at += r.Text.length;
|
|
587
|
+
}
|
|
325
588
|
return out;
|
|
326
589
|
}
|
|
327
|
-
|
|
590
|
+
/**
|
|
591
|
+
* After the layout, lines whose text needs shaping or runs right to left (DrawnUi.Rust build_blobs): with
|
|
592
|
+
* right-to-left text every run is cut where its bidirectional run changes (Unicode bidirectional algorithm, ICU, the
|
|
593
|
+
* paragraph direction from its first strong letter); runs that are right to left or need shaping get shaped glyphs;
|
|
594
|
+
* runs are placed in visual order. Lines without such text are left as they are (plain path).
|
|
595
|
+
*/
|
|
596
|
+
ShapeLines(lines) {
|
|
597
|
+
const sp = this.glyphSpacing;
|
|
598
|
+
const lineText = (l) => l.Runs.map((r) => r.Text).join("");
|
|
599
|
+
const anyRtl = lines.some((l) => l.Runs.some((r) => HasCodePoint(r.Text, IsRtl)));
|
|
600
|
+
let rtlParagraph = false;
|
|
601
|
+
for (let li = 0; li < lines.length; li++) {
|
|
602
|
+
const line = lines[li];
|
|
603
|
+
let bidi = false;
|
|
604
|
+
if (anyRtl) {
|
|
605
|
+
if (li === 0 || line.Para) {
|
|
606
|
+
let end = li + 1;
|
|
607
|
+
while (end < lines.length && !lines[end].Para)
|
|
608
|
+
end++;
|
|
609
|
+
rtlParagraph = SkiaLabel.FirstStrongIsRtl(lines.slice(li, end).map(lineText).join(""));
|
|
610
|
+
}
|
|
611
|
+
line.Rtl = rtlParagraph;
|
|
612
|
+
const map = BidiRuns(lineText(line), rtlParagraph);
|
|
613
|
+
if (map) {
|
|
614
|
+
line.Runs = this.SplitByRuns(line.Runs, map);
|
|
615
|
+
bidi = line.Runs.some((r) => (r.Level ?? 0) > 0);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
let shaped = false;
|
|
619
|
+
for (const run of line.Runs) {
|
|
620
|
+
const rtl = (run.Level ?? 0) % 2 === 1;
|
|
621
|
+
if (!rtl && !HasCodePoint(run.Text, NeedsShaping))
|
|
622
|
+
continue;
|
|
623
|
+
const s = Shape(run.Font, run.Text, rtl, sp);
|
|
624
|
+
if (s) {
|
|
625
|
+
run.Shaped = s;
|
|
626
|
+
run.Width = s.Width;
|
|
627
|
+
shaped = true;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
if (!shaped && !bidi)
|
|
631
|
+
continue;
|
|
632
|
+
let width = 0;
|
|
633
|
+
for (const run of line.Runs)
|
|
634
|
+
width += run.Width;
|
|
635
|
+
line.Width = line.Runs.length > 0 ? width - sp : 0;
|
|
636
|
+
if (bidi)
|
|
637
|
+
SkiaLabel.PlaceRuns(line.Runs);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Where each run of a line with right-to-left text goes: visual runs left to right; inside a run, pieces (spans, fonts)
|
|
642
|
+
* in logical order, reversed in a right-to-left run.
|
|
643
|
+
*/
|
|
644
|
+
static PlaceRuns(runs) {
|
|
645
|
+
const order = runs.map((_, i) => i).sort((a, b) => {
|
|
646
|
+
const ra = runs[a], rb = runs[b];
|
|
647
|
+
if ((ra.Rank ?? 0) !== (rb.Rank ?? 0))
|
|
648
|
+
return (ra.Rank ?? 0) - (rb.Rank ?? 0);
|
|
649
|
+
return ((ra.Level ?? 0) % 2 === 1 ? -1 : 1) * ((ra.At ?? 0) - (rb.At ?? 0));
|
|
650
|
+
});
|
|
651
|
+
let x = 0;
|
|
652
|
+
for (const k of order) {
|
|
653
|
+
runs[k].X = x;
|
|
654
|
+
x += runs[k].Width;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
/** A line's own height: its spans' largest ascent plus largest descent, times LineHeight, rounded once to whole pixels. */
|
|
658
|
+
OwnLineHeight(line) { return Math.round((line.Ascent + line.Descent) * this.lineHeight); }
|
|
659
|
+
LineHeightPx(line) { return this.lineHeightUniform ? this.uniformLineHeight : this.OwnLineHeight(line); }
|
|
660
|
+
/** C# GetSpaceBetweenLines: the label font's Leading times LineSpacing when it has one, else (LineSpacing - 1) lines. */
|
|
661
|
+
LineGap(lh) {
|
|
662
|
+
const leading = this.mainFonts?.Leading ?? 0;
|
|
663
|
+
return leading > 0 ? leading * this.lineSpacing : lh * (this.lineSpacing - 1);
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* As DrawnUi.Net: the baseline sits at the line's bottom minus its descent, the extra space of LineHeight split above and
|
|
667
|
+
* below the text (a line of lh pixels holds lh / LineHeight of text).
|
|
668
|
+
*/
|
|
669
|
+
BaselineIn(line, lh) { return lh - (lh - lh / this.lineHeight) / 2 - line.Descent; }
|
|
670
|
+
/** ParagraphSpacing above a line that starts a paragraph (after the first): that many lines of the label's font, with their gap. */
|
|
671
|
+
ParagraphSpace(line) {
|
|
672
|
+
if (!line.Para || !this.paragraphSpacing)
|
|
673
|
+
return 0;
|
|
674
|
+
const f = this.mainFonts, lh = Math.round((f.Ascent + f.Descent) * this.lineHeight);
|
|
675
|
+
return (lh + this.LineGap(lh)) * this.paragraphSpacing;
|
|
676
|
+
}
|
|
328
677
|
BlockHeight() {
|
|
329
678
|
let h = 0;
|
|
330
679
|
for (let i = 0; i < this.lines.length; i++) {
|
|
331
680
|
const lh = this.LineHeightPx(this.lines[i]);
|
|
332
|
-
h += i < this.lines.length - 1 ? lh
|
|
681
|
+
h += lh + (i < this.lines.length - 1 ? this.LineGap(lh) : 0) + this.ParagraphSpace(this.lines[i]);
|
|
333
682
|
}
|
|
334
683
|
return h;
|
|
335
684
|
}
|
|
336
|
-
|
|
685
|
+
/** Pixels of ink outside the lines' advance boxes, at their left and right ends (side bearings, italic slant). */
|
|
686
|
+
inkLeft = 0;
|
|
687
|
+
inkRight = 0;
|
|
688
|
+
/** Ink drawn outside the label's box (a "j" hook, an italic slant) is recorded by caches too, as C# draws it whole. */
|
|
689
|
+
ComputeEffectsMargin(scale) {
|
|
690
|
+
const m = super.ComputeEffectsMargin(scale);
|
|
691
|
+
if (this.inkRight === 0 && this.inkLeft === 0)
|
|
692
|
+
return m;
|
|
693
|
+
return new Thickness(m.Left + this.inkLeft, m.Top, m.Right + this.inkRight, m.Bottom);
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* C# (after 106e768a): the ink bounds of the first glyph of each line's leftmost run and the last glyph of its
|
|
697
|
+
* rightmost run, beyond the advance box of that run. Two glyph bounds per line, at measure.
|
|
698
|
+
*/
|
|
699
|
+
MeasureInkOverhang() {
|
|
700
|
+
const sp = this.glyphSpacing;
|
|
701
|
+
let left = 0, right = 0;
|
|
702
|
+
for (const line of this.lines) {
|
|
703
|
+
const runs = line.Runs.filter((r) => r.Text.trim().length > 0);
|
|
704
|
+
if (runs.length === 0)
|
|
705
|
+
continue;
|
|
706
|
+
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));
|
|
707
|
+
const firstX = first.X ?? 0, lastEnd = (last.X ?? line.Width) + (last.X === undefined ? 0 : last.Width - sp);
|
|
708
|
+
// ink x of a run's visually first / last glyph, from the line start
|
|
709
|
+
const ends = (r, x0) => {
|
|
710
|
+
if (r.Shaped) {
|
|
711
|
+
const g = r.Shaped.Glyphs, n = g.length;
|
|
712
|
+
if (n === 0)
|
|
713
|
+
return [x0, x0];
|
|
714
|
+
const b = r.Font.getGlyphBounds([g[0], g[n - 1]]);
|
|
715
|
+
return [x0 + r.Shaped.Positions[0] + b[0], x0 + r.Shaped.Positions[(n - 1) * 2] + b[6]];
|
|
716
|
+
}
|
|
717
|
+
// only the first and the last character matter
|
|
718
|
+
const first = String.fromCodePoint(r.Text.codePointAt(0));
|
|
719
|
+
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);
|
|
720
|
+
const ids = r.Font.getGlyphIDs(first + r.Text.slice(lastAt));
|
|
721
|
+
const n = ids.length;
|
|
722
|
+
const b = r.Font.getGlyphBounds([ids[0], ids[n - 1]]);
|
|
723
|
+
const lastAdvance = r.Font.getGlyphWidths([ids[n - 1]])[0];
|
|
724
|
+
return [x0 + b[0], x0 + r.Width - sp - lastAdvance + b[6]];
|
|
725
|
+
};
|
|
726
|
+
// a line without placed runs (no bidi): its runs follow each other from x 0, the last ends at the line width
|
|
727
|
+
const lastX0 = last.X ?? (line.Width - (last.Width - sp));
|
|
728
|
+
left = Math.max(left, firstX - ends(first, firstX)[0]);
|
|
729
|
+
right = Math.max(right, ends(last, lastX0)[1] - (last.X === undefined ? line.Width : lastEnd));
|
|
730
|
+
}
|
|
731
|
+
return [Math.max(0, left), Math.max(0, right)];
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Where a line starts inside [left, right] (drawnui-cross 6am): Start and End follow the paragraph's direction (in a
|
|
735
|
+
* right-to-left paragraph Start is the right edge); Center is centered; Fill* lines are not stretched here and sit
|
|
736
|
+
* at the paragraph's start.
|
|
737
|
+
*/
|
|
738
|
+
LineX(line, left, right, lineW) {
|
|
739
|
+
const a = this.horizontalTextAlignment;
|
|
740
|
+
if (a === "Center")
|
|
741
|
+
return left + (right - left - lineW) / 2;
|
|
742
|
+
const atEnd = a === "End" ? !line.Rtl : !!line.Rtl;
|
|
743
|
+
return atEnd ? right - lineW : left;
|
|
744
|
+
}
|
|
745
|
+
MeasureAbsolute(widthConstraint, heightConstraint, scale) {
|
|
337
746
|
this.mainFonts = this.ResolveMainFonts(scale);
|
|
747
|
+
const glyphSpacing = scale * (this.characterSpacing - 1);
|
|
748
|
+
if (glyphSpacing !== this.glyphSpacing) {
|
|
749
|
+
this.glyphSpacing = glyphSpacing;
|
|
750
|
+
this.runCache = new Map();
|
|
751
|
+
this.spanRunCache = new WeakMap();
|
|
752
|
+
}
|
|
338
753
|
const px = this.padding.HorizontalThickness * scale, py = this.padding.VerticalThickness * scale;
|
|
339
|
-
|
|
754
|
+
const extraH = this.EffectsExtra(scale).H;
|
|
755
|
+
const roomW = widthConstraint - px, roomH = heightConstraint - py - extraH;
|
|
756
|
+
const r = this.reusable;
|
|
757
|
+
if (this.NeedMeasure || !r || r.Scale !== scale || r.Spacing !== glyphSpacing || roomW < r.Width - 0.01 || roomH < r.Height - 0.01) {
|
|
758
|
+
this.lines = this.text || this.Spans.length > 0 ? this.LayoutLines(roomW, scale, roomH) : [];
|
|
759
|
+
this.uniformLineHeight = 0;
|
|
760
|
+
for (const l of this.lines)
|
|
761
|
+
this.uniformLineHeight = Math.max(this.uniformLineHeight, this.OwnLineHeight(l));
|
|
762
|
+
this.ShapeLines(this.lines);
|
|
763
|
+
// ink past the lines' ends (a "j" hook, an italic slant) is drawing room for caches; widths unchanged
|
|
764
|
+
const [inkLeft, inkRight] = this.MeasureInkOverhang();
|
|
765
|
+
if (inkLeft !== this.inkLeft || inkRight !== this.inkRight) {
|
|
766
|
+
this.inkLeft = inkLeft;
|
|
767
|
+
this.inkRight = inkRight;
|
|
768
|
+
this.InvalidateEffectsMargin();
|
|
769
|
+
}
|
|
770
|
+
let widest = 0;
|
|
771
|
+
for (const l of this.lines)
|
|
772
|
+
widest = Math.max(widest, l.Width);
|
|
773
|
+
this.reusable = this.wrapped || this.truncated ? undefined : { Scale: scale, Spacing: glyphSpacing, Width: widest, Height: this.BlockHeight() };
|
|
774
|
+
}
|
|
340
775
|
let width = 0;
|
|
341
776
|
for (const l of this.lines)
|
|
342
777
|
width = Math.max(width, l.Width);
|
|
343
778
|
const extra = this.EffectsExtra(scale);
|
|
344
779
|
this.LayoutVersion++;
|
|
345
|
-
|
|
780
|
+
const size = ScaledSize.FromPixels(Math.ceil(width + extra.W) + px, Math.ceil(this.BlockHeight() + extra.H) + py, scale);
|
|
781
|
+
// fit-content (drawnui-cross 6an): text that had to wrap needs the whole width it was given (min of max-content and it)
|
|
782
|
+
if (this.wrapped && isFinite(widthConstraint))
|
|
783
|
+
this.fitContentPixels = { Width: widthConstraint, Height: size.Pixels.Height };
|
|
784
|
+
return size;
|
|
346
785
|
}
|
|
347
786
|
/** C# GradientByLines: FillGradient spans each line's bounds (default) instead of the whole text block. */
|
|
348
787
|
GradientByLines = true;
|
|
@@ -367,26 +806,16 @@ export class SkiaLabel extends SkiaControl {
|
|
|
367
806
|
y += extra.Stroke / 2;
|
|
368
807
|
const out = [];
|
|
369
808
|
for (const line of this.lines) {
|
|
809
|
+
y += this.ParagraphSpace(line);
|
|
370
810
|
const lh = this.LineHeightPx(line);
|
|
371
811
|
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;
|
|
812
|
+
let x = this.LineX(line, left, right, lineW);
|
|
377
813
|
x += extra.Stroke / 2;
|
|
378
814
|
out.push({ x, y, w: line.Width, h: lh, text: line.Runs.map((r) => r.Text).join("") });
|
|
379
|
-
y += lh
|
|
815
|
+
y += lh + this.LineGap(lh);
|
|
380
816
|
}
|
|
381
817
|
return out;
|
|
382
818
|
}
|
|
383
|
-
/** Lines as CSS text for the accessibility overlay (AccessibilityTextSelectable): font alias chain, weight and px size. */
|
|
384
|
-
GetAccessibilityTextLines(scale) {
|
|
385
|
-
const family = [this.fontFamily || Super.DefaultFontAlias, ...this.fontFamilyFallback.split(",").map((f) => f.trim()).filter(Boolean)].filter(Boolean).join(", ");
|
|
386
|
-
const weight = this.fontWeight > 0 ? this.fontWeight : this.fontAttributes === "Bold" || this.fontAttributes === "BoldItalic" ? 600 : 400;
|
|
387
|
-
const size = this.fontSize;
|
|
388
|
-
return this.LineGeometry().map((g) => ({ Text: g.text, Left: g.x / scale, Top: g.y / scale, Width: g.w / scale, Height: g.h / scale, FontFamily: family, FontWeight: weight, FontSize: size }));
|
|
389
|
-
}
|
|
390
819
|
/** Laid-out lines with their text index range (a wrapped-away space or line break sits between two lines). */
|
|
391
820
|
GetLineBoxes() {
|
|
392
821
|
const geo = this.LineGeometry();
|
|
@@ -412,16 +841,30 @@ export class SkiaLabel extends SkiaControl {
|
|
|
412
841
|
let start = g.text.length ? this.text.indexOf(g.text, cursor) : cursor;
|
|
413
842
|
if (start < 0)
|
|
414
843
|
start = cursor;
|
|
844
|
+
// boxes in logical order, each at its visual place: a right-to-left run fills from its right edge
|
|
415
845
|
let x = g.x, index = start;
|
|
846
|
+
const sp = this.glyphSpacing;
|
|
416
847
|
for (const run of line.Runs) {
|
|
848
|
+
if (run.X !== undefined)
|
|
849
|
+
x = g.x + run.X;
|
|
417
850
|
const cps = Array.from(run.Text);
|
|
418
|
-
const widths = run.Font.getGlyphWidths(run.Font.getGlyphIDs(run.Text, cps.length));
|
|
851
|
+
const widths = run.Shaped ? run.Shaped.Advances.map((a) => a - sp) : run.Font.getGlyphWidths(run.Font.getGlyphIDs(run.Text, cps.length));
|
|
852
|
+
const rtl = (run.Level ?? 0) % 2 === 1;
|
|
853
|
+
let right = x + run.Width - sp;
|
|
419
854
|
cps.forEach((cp, k) => {
|
|
420
855
|
const w = widths[k] ?? 0;
|
|
421
|
-
|
|
422
|
-
|
|
856
|
+
if (rtl) {
|
|
857
|
+
out.push({ Index: index, Length: cp.length, Line: li, Left: right - w, Top: g.y, Width: w, Height: g.h, Rtl: true });
|
|
858
|
+
right -= w + sp;
|
|
859
|
+
}
|
|
860
|
+
else {
|
|
861
|
+
out.push({ Index: index, Length: cp.length, Line: li, Left: x, Top: g.y, Width: w, Height: g.h });
|
|
862
|
+
x += w + sp;
|
|
863
|
+
}
|
|
423
864
|
index += cp.length;
|
|
424
865
|
});
|
|
866
|
+
if (rtl)
|
|
867
|
+
x += run.Width;
|
|
425
868
|
}
|
|
426
869
|
cursor = start + g.text.length;
|
|
427
870
|
});
|
|
@@ -445,8 +888,14 @@ export class SkiaLabel extends SkiaControl {
|
|
|
445
888
|
const sh = this.HasDropShadow() ? (this.DropShadowSize + this.DropShadowOffsetY) * scale : 0;
|
|
446
889
|
return { W: stroke + sw, H: stroke + sh, Stroke: stroke };
|
|
447
890
|
}
|
|
448
|
-
/**
|
|
449
|
-
|
|
891
|
+
/**
|
|
892
|
+
* FillGradient goes to one place (C# 9637994b, as DrawnUi.Rust): the background of a label that paints one (a
|
|
893
|
+
* BackgroundColor with alpha above 0), whose glyphs keep TextColor; else the glyphs.
|
|
894
|
+
*/
|
|
895
|
+
FillGradientPaintsBackground() { return this.HasBackground(); }
|
|
896
|
+
HasBackground() { return !!this.BackgroundColor && Super.ParseColor(this.BackgroundColor)[3] > 0; }
|
|
897
|
+
/** The gradient the glyphs are painted with: none when the label's background takes FillGradient. */
|
|
898
|
+
get TextFillGradient() { return this.HasBackground() ? undefined : this.FillGradient; }
|
|
450
899
|
Paint(ctx) {
|
|
451
900
|
if (this.lines.length === 0 || !this.mainFonts)
|
|
452
901
|
return;
|
|
@@ -501,40 +950,61 @@ export class SkiaLabel extends SkiaControl {
|
|
|
501
950
|
return paint;
|
|
502
951
|
};
|
|
503
952
|
const canvas = ctx.Context.Canvas;
|
|
504
|
-
const
|
|
953
|
+
const sp = this.glyphSpacing;
|
|
954
|
+
// spaced text: every glyph placed with the gap after it (C# MeasureLineGlyphs positions)
|
|
955
|
+
const drawRun = (run, rx, ry, paint) => {
|
|
956
|
+
if (run.Shaped) {
|
|
957
|
+
canvas.drawGlyphs(run.Shaped.Glyphs, run.Shaped.Positions, rx, ry, run.Font, paint);
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
if (sp === 0) {
|
|
961
|
+
canvas.drawText(run.Text, rx, ry, paint, run.Font);
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
const ids = run.Font.getGlyphIDs(run.Text), widths = run.Font.getGlyphWidths(ids);
|
|
965
|
+
const positions = new Float32Array(ids.length * 2);
|
|
966
|
+
for (let k = 0, gx = 0; k < ids.length; k++) {
|
|
967
|
+
positions[k * 2] = gx;
|
|
968
|
+
gx += widths[k] + sp;
|
|
969
|
+
}
|
|
970
|
+
canvas.drawGlyphs(ids, positions, rx, ry, run.Font, paint);
|
|
971
|
+
};
|
|
972
|
+
const gradient = this.TextFillGradient;
|
|
505
973
|
const textRect = new SKRect(left, top, right, bottom);
|
|
506
974
|
for (const line of this.lines) {
|
|
975
|
+
y += this.ParagraphSpace(line);
|
|
507
976
|
const lh = this.LineHeightPx(line);
|
|
508
977
|
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;
|
|
978
|
+
let x = this.LineX(line, left, right, lineW);
|
|
514
979
|
x += extra.Stroke / 2;
|
|
515
|
-
const baseline = y + line
|
|
980
|
+
const baseline = y + this.BaselineIn(line, lh);
|
|
516
981
|
// C# paintDefault gets the gradient over rectDraw, or over line.Bounds per line (GradientByLines)
|
|
517
982
|
const gradientRect = gradient ? (this.GradientByLines ? new SKRect(x, y, x + line.Width, y + lh) : textRect) : undefined;
|
|
518
983
|
if (strokePaint && this.StrokeGradient)
|
|
519
984
|
this.SetupGradient(strokePaint, this.StrokeGradient, gradientRect ?? new SKRect(x, y, x + line.Width, y + lh));
|
|
985
|
+
const lineX = x;
|
|
520
986
|
for (const run of line.Runs) {
|
|
987
|
+
// a line with right-to-left text places its runs in visual order
|
|
988
|
+
if (run.X !== undefined)
|
|
989
|
+
x = lineX + run.X;
|
|
521
990
|
const span = run.Span;
|
|
991
|
+
const drawnW = run.Width - sp; // the gap after the run's last glyph is not part of it
|
|
522
992
|
if (span) {
|
|
523
|
-
span.Rects.push(new SKRect(x - d.Left, y - d.Top, x +
|
|
993
|
+
span.Rects.push(new SKRect(x - d.Left, y - d.Top, x + drawnW - d.Left, y + lh - d.Top));
|
|
524
994
|
if (span.BackgroundColor)
|
|
525
|
-
canvas.drawRect(CK.LTRBRect(x, y, x +
|
|
995
|
+
canvas.drawRect(CK.LTRBRect(x, y, x + drawnW, y + lh), paintFor(span.BackgroundColor));
|
|
526
996
|
}
|
|
527
997
|
const color = span?.TextColor ?? this.textColor;
|
|
528
998
|
if (run.Text) {
|
|
529
999
|
// C# DrawText order: drop shadow, stroke, fill
|
|
530
1000
|
if (shadowPaint)
|
|
531
|
-
|
|
1001
|
+
drawRun(run, x + shadowDx, baseline + shadowDy, shadowPaint);
|
|
532
1002
|
if (strokePaint)
|
|
533
|
-
|
|
1003
|
+
drawRun(run, x, baseline, strokePaint);
|
|
534
1004
|
const paint = paintFor(color);
|
|
535
1005
|
if (gradient && gradientRect)
|
|
536
1006
|
this.SetupGradient(paint, gradient, gradientRect);
|
|
537
|
-
|
|
1007
|
+
drawRun(run, x, baseline, paint);
|
|
538
1008
|
}
|
|
539
1009
|
if (span?.HasDecorations) {
|
|
540
1010
|
// same geometry as C# DrawSpanDecorations; CanvasKit exposes no underline/strikeout/x-height metrics,
|
|
@@ -542,30 +1012,38 @@ export class SkiaLabel extends SkiaControl {
|
|
|
542
1012
|
if (span.Underline && span.UnderlineWidth !== 0) {
|
|
543
1013
|
const w = span.UnderlineWidth > 0 ? span.UnderlineWidth * scale : -span.UnderlineWidth;
|
|
544
1014
|
const yl = Math.round(baseline + scale);
|
|
545
|
-
canvas.drawLine(x, yl, x +
|
|
1015
|
+
canvas.drawLine(x, yl, x + drawnW, yl, paintFor(color, w));
|
|
546
1016
|
}
|
|
547
1017
|
if (span.Strikeout) {
|
|
548
1018
|
const yl = Math.round(baseline - (run.Fonts.SizePx * 0.52) / 2);
|
|
549
|
-
canvas.drawLine(x, yl, x +
|
|
1019
|
+
canvas.drawLine(x, yl, x + drawnW, yl, paintFor(span.StrikeoutColor, span.StrikeoutWidth * scale));
|
|
550
1020
|
}
|
|
551
1021
|
}
|
|
552
1022
|
x += run.Width;
|
|
553
1023
|
}
|
|
554
|
-
y += lh
|
|
1024
|
+
y += lh + this.LineGap(lh);
|
|
555
1025
|
}
|
|
556
1026
|
for (const paint of paints.values())
|
|
557
1027
|
paint.delete();
|
|
558
1028
|
strokePaint?.delete();
|
|
559
1029
|
shadowPaint?.delete();
|
|
1030
|
+
if (this.AccessibilityTextSelectable && this.selLength > 0)
|
|
1031
|
+
this.DrawTextSelection(ctx);
|
|
560
1032
|
}
|
|
561
1033
|
// ---- span taps (port of C# SkiaLabel.ProcessGestures) ----
|
|
562
1034
|
ProcessGestures(args, apply) {
|
|
1035
|
+
if (this.AccessibilityTextSelectable) {
|
|
1036
|
+
const consumed = this.ProcessTextSelection(args, apply);
|
|
1037
|
+
if (consumed)
|
|
1038
|
+
return consumed;
|
|
1039
|
+
}
|
|
563
1040
|
if (args.Type === "Tapped" && this.Spans.length > 0) {
|
|
564
1041
|
const x = apply.MappedLocation.X + apply.ChildOffset.X - this.DrawingRect.Left;
|
|
565
1042
|
const y = apply.MappedLocation.Y + apply.ChildOffset.Y - this.DrawingRect.Top;
|
|
566
1043
|
for (const span of this.Spans) {
|
|
567
1044
|
if (span.HasTapHandler && span.HitIsInside(x, y)) {
|
|
568
|
-
|
|
1045
|
+
if (SkiaControl.IsPrimaryPointer(args))
|
|
1046
|
+
this.PlayRippleAnimation(this.TouchEffectColor, x / this.RenderingScale, y / this.RenderingScale);
|
|
569
1047
|
return this.OnSpanTapped(span, args, apply);
|
|
570
1048
|
}
|
|
571
1049
|
}
|
|
@@ -586,4 +1064,358 @@ export class SkiaLabel extends SkiaControl {
|
|
|
586
1064
|
span.FireTap(new ControlTappedEventArgs(this, args, apply));
|
|
587
1065
|
return this;
|
|
588
1066
|
}
|
|
1067
|
+
// ---- text selection (port of C# SkiaLabel TEXT SELECTION, drawnui-cross 6cb / 6cc) ----
|
|
1068
|
+
get AccessibilityTextSelectable() { return super.AccessibilityTextSelectable; }
|
|
1069
|
+
/**
|
|
1070
|
+
* The text can be selected and copied, drawn by the label (C# SkiaLabel.AccessibilityTextSelectable). Mouse: drag, double
|
|
1071
|
+
* click for a word. Touch: long press picks a word, keep the finger down and drag to extend, a Copy button appears.
|
|
1072
|
+
* Ctrl+C (Cmd+C) copies and Ctrl+A selects all while the label holds the selection; a click elsewhere drops it. The label
|
|
1073
|
+
* takes the mouse press for this, so it no longer reaches controls under it. Free when off. Copies through
|
|
1074
|
+
* Super.SetClipboardText.
|
|
1075
|
+
*/
|
|
1076
|
+
set AccessibilityTextSelectable(v) { super.AccessibilityTextSelectable = v; if (!v)
|
|
1077
|
+
this.ClearSelection(); }
|
|
1078
|
+
selStart = 0;
|
|
1079
|
+
selLength = 0;
|
|
1080
|
+
anchorStart = 0;
|
|
1081
|
+
anchorEnd = 0;
|
|
1082
|
+
selecting = false;
|
|
1083
|
+
touchSelection = false;
|
|
1084
|
+
copyPressed = false;
|
|
1085
|
+
selectionKeysAttached = false;
|
|
1086
|
+
lastClickMs = 0;
|
|
1087
|
+
lastClickX = 0;
|
|
1088
|
+
lastClickY = 0;
|
|
1089
|
+
/** The drawn Copy button of a touch selection, relative to DrawingRect (pixels); empty when none. */
|
|
1090
|
+
copyButton = new SKRect(0, 0, 0, 0);
|
|
1091
|
+
selectionMap;
|
|
1092
|
+
/** Start of the selection in the selection's source: the label's text when the lines are taken from it verbatim, else the drawn lines joined. */
|
|
1093
|
+
get SelectionStart() { return this.selStart; }
|
|
1094
|
+
/** Length of the selection, 0 when nothing is selected. */
|
|
1095
|
+
get SelectionLength() { return this.selLength; }
|
|
1096
|
+
/** The selected text, empty when nothing is selected. */
|
|
1097
|
+
get SelectedText() {
|
|
1098
|
+
if (this.selLength <= 0)
|
|
1099
|
+
return "";
|
|
1100
|
+
const source = this.SelectionMap().Source;
|
|
1101
|
+
const start = Math.min(Math.max(this.selStart, 0), source.length);
|
|
1102
|
+
return source.substr(start, Math.min(Math.max(this.selLength, 0), source.length - start));
|
|
1103
|
+
}
|
|
1104
|
+
/** Selects a range of the text (see SelectionStart for what the indexes count). */
|
|
1105
|
+
Select(start, length) {
|
|
1106
|
+
const n = this.SelectionMap().Source.length;
|
|
1107
|
+
start = Math.min(Math.max(start, 0), n);
|
|
1108
|
+
length = Math.min(Math.max(length, 0), n - start);
|
|
1109
|
+
if (start === this.selStart && length === this.selLength)
|
|
1110
|
+
return;
|
|
1111
|
+
this.selStart = start;
|
|
1112
|
+
this.selLength = length;
|
|
1113
|
+
this.UpdateDraw();
|
|
1114
|
+
}
|
|
1115
|
+
/** Selects all of the text. */
|
|
1116
|
+
SelectAll() { this.Select(0, this.SelectionMap().Source.length); }
|
|
1117
|
+
/** Drops the selection. */
|
|
1118
|
+
ClearSelection() {
|
|
1119
|
+
this.selecting = false;
|
|
1120
|
+
this.copyPressed = false;
|
|
1121
|
+
if (this.selLength === 0 && !this.touchSelection)
|
|
1122
|
+
return;
|
|
1123
|
+
this.selLength = 0;
|
|
1124
|
+
this.touchSelection = false;
|
|
1125
|
+
this.UpdateDraw();
|
|
1126
|
+
}
|
|
1127
|
+
/** The app showed its own context menu for a touch selection: the drawn Copy button steps aside, the selection stays. */
|
|
1128
|
+
HideCopyButton() {
|
|
1129
|
+
if (!this.touchSelection)
|
|
1130
|
+
return;
|
|
1131
|
+
this.touchSelection = false;
|
|
1132
|
+
this.copyButton = new SKRect(0, 0, 0, 0);
|
|
1133
|
+
this.UpdateDraw();
|
|
1134
|
+
}
|
|
1135
|
+
/** Puts the selected text on the clipboard through Super.SetClipboardText. False when nothing is selected. */
|
|
1136
|
+
CopySelection() {
|
|
1137
|
+
const text = this.SelectedText;
|
|
1138
|
+
if (!text)
|
|
1139
|
+
return false;
|
|
1140
|
+
Super.SetClipboardText(text);
|
|
1141
|
+
return true;
|
|
1142
|
+
}
|
|
1143
|
+
/** A selectable label takes the canvas focus while text is selected and drops the selection when the focus moves elsewhere. */
|
|
1144
|
+
SetFrameworkFocus(focus) {
|
|
1145
|
+
if (!this.AccessibilityTextSelectable)
|
|
1146
|
+
return super.SetFrameworkFocus(focus);
|
|
1147
|
+
if (!focus)
|
|
1148
|
+
this.ClearSelection();
|
|
1149
|
+
this.AttachSelectionKeys(focus);
|
|
1150
|
+
return true;
|
|
1151
|
+
}
|
|
1152
|
+
OnDisposing() { this.AttachSelectionKeys(false); super.OnDisposing(); }
|
|
1153
|
+
AttachSelectionKeys(attach) {
|
|
1154
|
+
if (attach === this.selectionKeysAttached)
|
|
1155
|
+
return;
|
|
1156
|
+
this.selectionKeysAttached = attach;
|
|
1157
|
+
if (attach)
|
|
1158
|
+
KeyboardManager.Subscribe(this.onSelectionKey, SkiaLabel.noChar);
|
|
1159
|
+
else
|
|
1160
|
+
KeyboardManager.Unsubscribe(this.onSelectionKey, SkiaLabel.noChar);
|
|
1161
|
+
}
|
|
1162
|
+
static noChar = () => { };
|
|
1163
|
+
onSelectionKey = (key, e) => {
|
|
1164
|
+
// Ctrl+C in a page input or a focused element is that element's copy, not this selection's
|
|
1165
|
+
if (KeyboardManager.IsOwnedByElement(e))
|
|
1166
|
+
return;
|
|
1167
|
+
// a drag selected page text: the page copies it
|
|
1168
|
+
if (this.Superview?.OwnsTextSelectionKeys)
|
|
1169
|
+
return;
|
|
1170
|
+
if (!KeyboardManager.IsControlPressed)
|
|
1171
|
+
return;
|
|
1172
|
+
if (key === "KeyC")
|
|
1173
|
+
this.CopySelection();
|
|
1174
|
+
else if (key === "KeyA") {
|
|
1175
|
+
e.preventDefault();
|
|
1176
|
+
this.SelectAll();
|
|
1177
|
+
}
|
|
1178
|
+
};
|
|
1179
|
+
ClaimSelectionFocus() { this.Superview?.ReportFocus(this, this); this.AttachSelectionKeys(true); }
|
|
1180
|
+
/**
|
|
1181
|
+
* The drawn lines mapped onto a source string: the label's text when every line is found in it in order (spaces
|
|
1182
|
+
* dropped at wraps are then copied too), else the lines joined, a new line between paragraphs. Each line keeps its
|
|
1183
|
+
* caret slots (x of each text index, relative to DrawingRect) and its glyph boxes.
|
|
1184
|
+
*/
|
|
1185
|
+
SelectionMap() {
|
|
1186
|
+
const d = this.DrawingRect;
|
|
1187
|
+
const key = `${this.LayoutVersion}|${d.Width}|${d.Height}|${this.text}`;
|
|
1188
|
+
if (this.selectionMap?.Key === key)
|
|
1189
|
+
return this.selectionMap;
|
|
1190
|
+
const boxes = this.GetLineBoxes(), glyphs = this.GetGlyphBoxes();
|
|
1191
|
+
const texts = this.lines.map((l) => l.Runs.map((r) => r.Text).join(""));
|
|
1192
|
+
const starts = [];
|
|
1193
|
+
let source = this.text ?? "", position = 0, verbatim = true;
|
|
1194
|
+
for (const t of texts) {
|
|
1195
|
+
const at = source.indexOf(t, position);
|
|
1196
|
+
if (at < 0) {
|
|
1197
|
+
verbatim = false;
|
|
1198
|
+
break;
|
|
1199
|
+
}
|
|
1200
|
+
starts.push(at);
|
|
1201
|
+
position = at + t.length;
|
|
1202
|
+
}
|
|
1203
|
+
if (!verbatim) {
|
|
1204
|
+
starts.length = 0;
|
|
1205
|
+
source = "";
|
|
1206
|
+
texts.forEach((t, i) => { if (i > 0 && this.lines[i].Para)
|
|
1207
|
+
source += "\n"; starts.push(source.length); source += t; });
|
|
1208
|
+
}
|
|
1209
|
+
const lines = boxes.map((b, i) => {
|
|
1210
|
+
const own = glyphs.filter((g) => g.Line === i);
|
|
1211
|
+
const rtl = own.some((g) => g.Rtl);
|
|
1212
|
+
const slots = own.map((g) => ({ Offset: g.Index - b.Start, X: g.Rtl ? g.Left + g.Width : g.Left }));
|
|
1213
|
+
slots.push({ Offset: texts[i].length, X: this.lines[i].Rtl ? b.Left : b.Left + b.Width });
|
|
1214
|
+
return { Start: starts[i] ?? 0, Length: texts[i].length, Left: b.Left, Right: b.Left + b.Width, Top: b.Top, Bottom: b.Top + b.Height, Rtl: rtl, Slots: slots,
|
|
1215
|
+
Boxes: own.map((g) => ({ Offset: g.Index - b.Start, Length: g.Length, Left: g.Left, Right: g.Left + g.Width })) };
|
|
1216
|
+
});
|
|
1217
|
+
this.selectionMap = { Key: key, Source: source, Lines: lines };
|
|
1218
|
+
return this.selectionMap;
|
|
1219
|
+
}
|
|
1220
|
+
/** Text index under a point relative to DrawingRect (pixels). */
|
|
1221
|
+
IndexAt(x, y) {
|
|
1222
|
+
const lines = this.SelectionMap().Lines;
|
|
1223
|
+
if (lines.length === 0)
|
|
1224
|
+
return 0;
|
|
1225
|
+
const line = lines.find((l) => y <= l.Bottom) ?? lines[lines.length - 1];
|
|
1226
|
+
let best = 0, bestDistance = Infinity;
|
|
1227
|
+
for (const s of line.Slots) {
|
|
1228
|
+
const dx = Math.abs(s.X - x);
|
|
1229
|
+
if (dx < bestDistance) {
|
|
1230
|
+
bestDistance = dx;
|
|
1231
|
+
best = s.Offset;
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
return line.Start + best;
|
|
1235
|
+
}
|
|
1236
|
+
WordAt(index) {
|
|
1237
|
+
const text = this.SelectionMap().Source;
|
|
1238
|
+
if (text.length === 0)
|
|
1239
|
+
return [0, 0];
|
|
1240
|
+
index = Math.min(Math.max(index, 0), text.length - 1);
|
|
1241
|
+
const isWord = (c) => /[\p{L}\p{N}_']/u.test(c);
|
|
1242
|
+
if (!isWord(text[index]) && index > 0 && isWord(text[index - 1]))
|
|
1243
|
+
index--;
|
|
1244
|
+
if (!isWord(text[index]))
|
|
1245
|
+
return [index, index + 1];
|
|
1246
|
+
let start = index;
|
|
1247
|
+
while (start > 0 && isWord(text[start - 1]))
|
|
1248
|
+
start--;
|
|
1249
|
+
let end = index + 1;
|
|
1250
|
+
while (end < text.length && isWord(text[end]))
|
|
1251
|
+
end++;
|
|
1252
|
+
return [start, end];
|
|
1253
|
+
}
|
|
1254
|
+
/** Text index under a canvas point (pixels), for a selection a page drag makes across labels. */
|
|
1255
|
+
TextIndexAtCanvas(x, y) {
|
|
1256
|
+
const r = this.GetAccessibilityPixelRect();
|
|
1257
|
+
if (r.Width <= 0 || r.Height <= 0)
|
|
1258
|
+
return 0;
|
|
1259
|
+
return this.IndexAt(x - r.Left, y - r.Top);
|
|
1260
|
+
}
|
|
1261
|
+
/** Length of the text a selection counts in (see SelectionStart). */
|
|
1262
|
+
get SelectableLength() { return this.SelectionMap().Source.length; }
|
|
1263
|
+
SelectBetween(index) {
|
|
1264
|
+
const start = Math.min(this.anchorStart, index), end = Math.max(this.anchorEnd, index);
|
|
1265
|
+
this.Select(start, end - start);
|
|
1266
|
+
}
|
|
1267
|
+
/** Pointer handling of AccessibilityTextSelectable; null lets the gesture go on as usual. */
|
|
1268
|
+
ProcessTextSelection(args, apply) {
|
|
1269
|
+
const x = apply.MappedLocation.X + apply.ChildOffset.X - this.DrawingRect.Left;
|
|
1270
|
+
const y = apply.MappedLocation.Y + apply.ChildOffset.Y - this.DrawingRect.Top;
|
|
1271
|
+
const pointer = args.Event.Pointer;
|
|
1272
|
+
const mouse = pointer?.DeviceType === "Mouse";
|
|
1273
|
+
const onCopy = () => { const b = this.copyButton; return x >= b.Left && x < b.Right && y >= b.Top && y < b.Bottom; };
|
|
1274
|
+
switch (args.Type) {
|
|
1275
|
+
case "Down": {
|
|
1276
|
+
if (this.touchSelection && this.selLength > 0 && onCopy()) {
|
|
1277
|
+
this.copyPressed = true;
|
|
1278
|
+
return this;
|
|
1279
|
+
}
|
|
1280
|
+
if (!mouse)
|
|
1281
|
+
return null; // touch: a long press selects, a drag keeps scrolling
|
|
1282
|
+
// only the main button selects: a right click keeps the selection for its context menu
|
|
1283
|
+
if (pointer && pointer.Button !== "Left")
|
|
1284
|
+
return this.selLength > 0 ? this : null;
|
|
1285
|
+
const index = this.IndexAt(x, y);
|
|
1286
|
+
// when the click happened (stamped at arrival), not when a frame got to process it
|
|
1287
|
+
const now = args.Event.Timestamp;
|
|
1288
|
+
const second = now - this.lastClickMs < 450 && Math.hypot(x - this.lastClickX, y - this.lastClickY) < 8 * this.RenderingScale;
|
|
1289
|
+
this.lastClickMs = second ? 0 : now;
|
|
1290
|
+
this.lastClickX = x;
|
|
1291
|
+
this.lastClickY = y;
|
|
1292
|
+
[this.anchorStart, this.anchorEnd] = second ? this.WordAt(index) : [index, index];
|
|
1293
|
+
this.touchSelection = false;
|
|
1294
|
+
this.selecting = true;
|
|
1295
|
+
this.Select(this.anchorStart, this.anchorEnd - this.anchorStart);
|
|
1296
|
+
this.ClaimSelectionFocus();
|
|
1297
|
+
return this;
|
|
1298
|
+
}
|
|
1299
|
+
case "LongPressing": {
|
|
1300
|
+
if (mouse)
|
|
1301
|
+
return this.selecting ? this : null;
|
|
1302
|
+
const pressed = this.IndexAt(x, y);
|
|
1303
|
+
// a long press on the selection keeps it, for the context menu that follows
|
|
1304
|
+
if (this.selLength > 0 && pressed >= this.selStart && pressed < this.selStart + this.selLength) {
|
|
1305
|
+
[this.anchorStart, this.anchorEnd] = [this.selStart, this.selStart + this.selLength];
|
|
1306
|
+
this.touchSelection = true;
|
|
1307
|
+
this.selecting = true;
|
|
1308
|
+
this.ClaimSelectionFocus();
|
|
1309
|
+
this.UpdateDraw();
|
|
1310
|
+
return this;
|
|
1311
|
+
}
|
|
1312
|
+
[this.anchorStart, this.anchorEnd] = this.WordAt(pressed);
|
|
1313
|
+
this.touchSelection = true;
|
|
1314
|
+
this.selecting = true;
|
|
1315
|
+
this.Select(this.anchorStart, this.anchorEnd - this.anchorStart);
|
|
1316
|
+
this.ClaimSelectionFocus();
|
|
1317
|
+
this.UpdateDraw();
|
|
1318
|
+
return this;
|
|
1319
|
+
}
|
|
1320
|
+
case "Panning":
|
|
1321
|
+
if (!this.selecting)
|
|
1322
|
+
return null;
|
|
1323
|
+
this.SelectBetween(this.IndexAt(x, y));
|
|
1324
|
+
return this;
|
|
1325
|
+
case "Up":
|
|
1326
|
+
if (this.copyPressed) {
|
|
1327
|
+
this.copyPressed = false;
|
|
1328
|
+
if (onCopy()) {
|
|
1329
|
+
this.CopySelection();
|
|
1330
|
+
this.ClearSelection();
|
|
1331
|
+
}
|
|
1332
|
+
return this;
|
|
1333
|
+
}
|
|
1334
|
+
if (this.selecting) {
|
|
1335
|
+
this.selecting = false;
|
|
1336
|
+
return this;
|
|
1337
|
+
}
|
|
1338
|
+
return null;
|
|
1339
|
+
case "Tapped":
|
|
1340
|
+
if (this.copyPressed)
|
|
1341
|
+
return this; // the tap on the Copy button: its Up (after the tap) copies
|
|
1342
|
+
if (this.selLength <= 0)
|
|
1343
|
+
return null; // nothing selected: span links and Tapped still get the tap
|
|
1344
|
+
if (!mouse)
|
|
1345
|
+
this.ClearSelection(); // a tap drops a touch selection
|
|
1346
|
+
// a click that selected (double click) is the label's: unconsumed, the canvas would read it as a tap on empty
|
|
1347
|
+
// space and take the focus, and with it the selection
|
|
1348
|
+
return this;
|
|
1349
|
+
}
|
|
1350
|
+
return null;
|
|
1351
|
+
}
|
|
1352
|
+
/** Draws the selection over the lines just drawn, and the Copy button of a touch selection. */
|
|
1353
|
+
DrawTextSelection(ctx) {
|
|
1354
|
+
const CK = Super.CK, map = this.SelectionMap(), canvas = ctx.Context.Canvas, d = ctx.Destination, scale = ctx.Scale;
|
|
1355
|
+
const paint = new CK.Paint();
|
|
1356
|
+
paint.setAntiAlias(true);
|
|
1357
|
+
paint.setColor(Super.ParseColor(SkiaLabel.TextSelectionColor));
|
|
1358
|
+
const start = this.selStart, end = this.selStart + this.selLength;
|
|
1359
|
+
let first, last;
|
|
1360
|
+
map.Lines.forEach((line, i) => {
|
|
1361
|
+
const lineEnd = line.Start + line.Length;
|
|
1362
|
+
const a = Math.max(start, line.Start), b = Math.min(end, lineEnd);
|
|
1363
|
+
const continues = end > lineEnd && i < map.Lines.length - 1;
|
|
1364
|
+
if (a > b || (a === b && !continues))
|
|
1365
|
+
return;
|
|
1366
|
+
let rect;
|
|
1367
|
+
if (line.Rtl) {
|
|
1368
|
+
// right-to-left text: the selected characters' boxes, blocks across a direction change
|
|
1369
|
+
for (const g of line.Boxes) {
|
|
1370
|
+
if (g.Offset < a - line.Start || g.Offset >= b - line.Start)
|
|
1371
|
+
continue;
|
|
1372
|
+
const block = new SKRect(d.Left + g.Left, d.Top + line.Top, d.Left + g.Right, d.Top + line.Bottom);
|
|
1373
|
+
canvas.drawRect(CK.LTRBRect(block.Left, block.Top, block.Right, block.Bottom), paint);
|
|
1374
|
+
rect = rect ? new SKRect(Math.min(rect.Left, block.Left), block.Top, Math.max(rect.Right, block.Right), block.Bottom) : block;
|
|
1375
|
+
}
|
|
1376
|
+
if (!rect)
|
|
1377
|
+
return;
|
|
1378
|
+
}
|
|
1379
|
+
else {
|
|
1380
|
+
const slotX = (offset) => line.Slots.find((s) => s.Offset >= offset)?.X ?? line.Right;
|
|
1381
|
+
const x0 = slotX(a - line.Start), x1 = continues && b === lineEnd ? line.Right : slotX(b - line.Start);
|
|
1382
|
+
rect = new SKRect(d.Left + x0, d.Top + line.Top, d.Left + Math.max(x1, x0 + scale), d.Top + line.Bottom);
|
|
1383
|
+
canvas.drawRect(CK.LTRBRect(rect.Left, rect.Top, rect.Right, rect.Bottom), paint);
|
|
1384
|
+
}
|
|
1385
|
+
first ??= rect;
|
|
1386
|
+
last = rect;
|
|
1387
|
+
});
|
|
1388
|
+
this.copyButton = new SKRect(0, 0, 0, 0);
|
|
1389
|
+
if (!this.touchSelection || !first || !last) {
|
|
1390
|
+
paint.delete();
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
// the Copy button inside the label (its cache clips and its hit box is the label): above the selection, else below
|
|
1394
|
+
// it, else beside it on the same line, overlapping only when none fits
|
|
1395
|
+
const font = Super.GetFont(this.fontFamily || Super.DefaultFontAlias, 400, false, 14 * scale);
|
|
1396
|
+
const caption = SkiaLabel.CopyButtonText;
|
|
1397
|
+
const textWidth = font.getGlyphWidths(font.getGlyphIDs(caption)).reduce((s, w) => s + w, 0);
|
|
1398
|
+
const height = 32 * scale, width = textWidth + 28 * scale, gap = 6 * scale;
|
|
1399
|
+
const clamp = (v, lo, hi) => Math.min(Math.max(v, lo), Math.max(lo, hi));
|
|
1400
|
+
const centered = clamp((first.Left + first.Right) / 2 - width / 2, d.Left, d.Right - width);
|
|
1401
|
+
const middle = clamp((first.Top + first.Bottom) / 2 - height / 2, d.Top, d.Bottom - height);
|
|
1402
|
+
let bx, by;
|
|
1403
|
+
if (first.Top - gap - height >= d.Top)
|
|
1404
|
+
[bx, by] = [centered, first.Top - gap - height];
|
|
1405
|
+
else if (last.Bottom + gap + height <= d.Bottom)
|
|
1406
|
+
[bx, by] = [centered, last.Bottom + gap];
|
|
1407
|
+
else if (first.Right + gap + width <= d.Right)
|
|
1408
|
+
[bx, by] = [first.Right + gap, middle];
|
|
1409
|
+
else if (first.Left - gap - width >= d.Left)
|
|
1410
|
+
[bx, by] = [first.Left - gap - width, middle];
|
|
1411
|
+
else
|
|
1412
|
+
[bx, by] = [centered, Math.max(d.Top, d.Bottom - height)];
|
|
1413
|
+
paint.setColor(CK.Color(33, 37, 41, 235 / 255));
|
|
1414
|
+
canvas.drawRRect(CK.RRectXY(CK.LTRBRect(bx, by, bx + width, by + height), height / 2, height / 2), paint);
|
|
1415
|
+
paint.setColor(CK.WHITE);
|
|
1416
|
+
const m = font.getMetrics();
|
|
1417
|
+
canvas.drawText(caption, bx + width / 2 - textWidth / 2, by + height / 2 - (m.ascent + m.descent) / 2, paint, font);
|
|
1418
|
+
paint.delete();
|
|
1419
|
+
this.copyButton = new SKRect(bx - d.Left, by - d.Top, bx + width - d.Left, by + height - d.Top);
|
|
1420
|
+
}
|
|
589
1421
|
}
|