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
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** A declared set of property values: names as the C# properties (the JSX props of a control). */
|
|
2
|
+
export type ControlProps = Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Assigns the changed values of `next` straight onto the control (same names as the C# properties), as the React
|
|
5
|
+
* renderer does with JSX props; a value `prev` had and `next` lacks goes back to the control's default. Handler values
|
|
6
|
+
* (functions) are swapped without invalidating. Then the control is invalidated as little as the change needs: a size
|
|
7
|
+
* change re-measures, a look change redraws, a transform change only repaints the composition.
|
|
8
|
+
*/
|
|
9
|
+
export declare function ApplyProps(inst: {
|
|
10
|
+
Update(): void;
|
|
11
|
+
}, prev: ControlProps | null, next: ControlProps): void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { SkiaControl } from "./SkiaControl";
|
|
2
|
+
/** Not properties of the control: React's own keys. */
|
|
3
|
+
const SKIP = new Set(["children", "key", "ref"]);
|
|
4
|
+
/** Props that change how the subtree is composited, not what it contains: repaint, keep caches (DrawnUi RedrawCanvas). */
|
|
5
|
+
const REPAINT_ONLY = new Set(["TranslationX", "TranslationY", "TranslationZ", "Rotation", "RotationX", "RotationY", "RotationZ", "ScaleX", "ScaleY", "Scale", "SkewX", "SkewY", "AnchorX", "AnchorY", "Opacity"]);
|
|
6
|
+
/** Props that change only how a control looks, never its size (C# NeedDraw): an ImageComposite above redraws only its area. */
|
|
7
|
+
const isDrawOnly = (k) => k.endsWith("Color") || k === "FillGradient" || k === "StrokeGradient";
|
|
8
|
+
/**
|
|
9
|
+
* Assigns the changed values of `next` straight onto the control (same names as the C# properties), as the React
|
|
10
|
+
* renderer does with JSX props; a value `prev` had and `next` lacks goes back to the control's default. Handler values
|
|
11
|
+
* (functions) are swapped without invalidating. Then the control is invalidated as little as the change needs: a size
|
|
12
|
+
* change re-measures, a look change redraws, a transform change only repaints the composition.
|
|
13
|
+
*/
|
|
14
|
+
export function ApplyProps(inst, prev, next) {
|
|
15
|
+
let changed = false, repaint = false, layout = false;
|
|
16
|
+
for (const k in next) {
|
|
17
|
+
if (SKIP.has(k) || (prev && prev[k] === next[k]))
|
|
18
|
+
continue;
|
|
19
|
+
inst[k] = next[k];
|
|
20
|
+
if (typeof next[k] === "function")
|
|
21
|
+
continue;
|
|
22
|
+
if (REPAINT_ONLY.has(k))
|
|
23
|
+
repaint = true;
|
|
24
|
+
else {
|
|
25
|
+
changed = true;
|
|
26
|
+
if (!isDrawOnly(k))
|
|
27
|
+
layout = true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (prev)
|
|
31
|
+
for (const k in prev)
|
|
32
|
+
if (!SKIP.has(k) && !(k in next)) {
|
|
33
|
+
inst[k] = new inst.constructor()[k];
|
|
34
|
+
changed = layout = true;
|
|
35
|
+
}
|
|
36
|
+
if (changed && prev) {
|
|
37
|
+
if (layout || !(inst instanceof SkiaControl))
|
|
38
|
+
inst.Update();
|
|
39
|
+
else
|
|
40
|
+
inst.UpdateDraw();
|
|
41
|
+
}
|
|
42
|
+
else if (repaint && prev)
|
|
43
|
+
inst.RepaintComposition?.();
|
|
44
|
+
}
|
|
@@ -72,8 +72,14 @@ export declare class VelocityAccumulator {
|
|
|
72
72
|
private readonly samples;
|
|
73
73
|
private static readonly MaxSampleSize;
|
|
74
74
|
private static readonly ConsiderationTimeframeMs;
|
|
75
|
+
/** The clock (ms) the release is judged on, for checks that drive gestures on a virtual clock (C# ClockOverrideNanos). */
|
|
76
|
+
static Clock?: () => number;
|
|
75
77
|
Clear(): void;
|
|
76
78
|
CaptureVelocity(x: number, y: number, arrivedTimeNanos?: number): void;
|
|
79
|
+
/**
|
|
80
|
+
* The release velocity from the samples of the last 150 ms before NOW (as DrawnUi.Net): after a short app freeze a
|
|
81
|
+
* flick handled late does not fling (samples are dated by when their events happened).
|
|
82
|
+
*/
|
|
77
83
|
CalculateFinalVelocity(clampAbsolute?: number): {
|
|
78
84
|
X: number;
|
|
79
85
|
Y: number;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Mirrors DrawnUi scrolling physics: DecelerationTimingParameters + ScrollFlingAnimator (fling),
|
|
2
2
|
// Spring + SpringTimingParameters + SpringWithVelocityAnimator (bounce), VelocityAccumulator, RubberBandUtils.
|
|
3
3
|
import { SkiaValueAnimator } from "./Animators";
|
|
4
|
+
import { VelocityTrail } from "./Gestures";
|
|
4
5
|
/** UIKit-style exponential deceleration: value(t) = v0 * (rate^(1000 t) - 1) / k, k = 1000 ln(rate). */
|
|
5
6
|
export class DecelerationTimingParameters {
|
|
6
7
|
InitialValue;
|
|
@@ -198,15 +199,29 @@ export class VelocityAccumulator {
|
|
|
198
199
|
samples = [];
|
|
199
200
|
static MaxSampleSize = 5;
|
|
200
201
|
static ConsiderationTimeframeMs = 150;
|
|
202
|
+
/** The clock (ms) the release is judged on, for checks that drive gestures on a virtual clock (C# ClockOverrideNanos). */
|
|
203
|
+
static Clock;
|
|
201
204
|
Clear() { this.samples.length = 0; }
|
|
202
205
|
CaptureVelocity(x, y, arrivedTimeNanos = 0) {
|
|
203
206
|
const time = arrivedTimeNanos > 0 ? arrivedTimeNanos / 1e6 : performance.now();
|
|
207
|
+
// a move in the same burst as the last one replaces it: the first move of a burst carries only part of the burst's
|
|
208
|
+
// distance (DrawnUi.Rust VelocityAccumulator, 6d525f7)
|
|
209
|
+
const last = this.samples[this.samples.length - 1];
|
|
210
|
+
if (last && time - last.time < VelocityTrail.BurstMs) {
|
|
211
|
+
last.X = x;
|
|
212
|
+
last.Y = y;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
204
215
|
if (this.samples.length === VelocityAccumulator.MaxSampleSize)
|
|
205
216
|
this.samples.shift();
|
|
206
217
|
this.samples.push({ X: x, Y: y, time });
|
|
207
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* The release velocity from the samples of the last 150 ms before NOW (as DrawnUi.Net): after a short app freeze a
|
|
221
|
+
* flick handled late does not fling (samples are dated by when their events happened).
|
|
222
|
+
*/
|
|
208
223
|
CalculateFinalVelocity(clampAbsolute = 0) {
|
|
209
|
-
const now = performance.now();
|
|
224
|
+
const now = VelocityAccumulator.Clock?.() ?? performance.now();
|
|
210
225
|
const relevant = this.samples.filter((s) => now - s.time <= VelocityAccumulator.ConsiderationTimeframeMs);
|
|
211
226
|
if (relevant.length === 0)
|
|
212
227
|
return { X: 0, Y: 0 };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Font } from "canvaskit-wasm";
|
|
2
|
+
/**
|
|
3
|
+
* Text shaping (C# SkiaLabel DrawShapedText, HarfBuzzSharp there; DrawnUi.Rust shaping.rs, harfrust) for the text that
|
|
4
|
+
* needs it: complex scripts whose letters join or reorder (Arabic, Hebrew marks, Indic, Thai...) and emoji sequences
|
|
5
|
+
* (ZWJ families, skin tones, flags, keycaps). Everything else keeps the plain path: one glyph per character.
|
|
6
|
+
* HarfBuzz and ICU come with the CanvasKit paragraph module, already in the build (no extra wasm): a run is laid out as a
|
|
7
|
+
* one-font paragraph and its shaped glyphs are read back; the label keeps its own line breaking and font fallback.
|
|
8
|
+
*/
|
|
9
|
+
/** C# SkiaLabel.UnicodeNeedsShaping, plus the characters that make emoji sequences and the right-to-left scripts C# leaves out. */
|
|
10
|
+
export declare function NeedsShaping(cp: number): boolean;
|
|
11
|
+
/** Characters that belong to the glyph before them (an emoji sequence or a mark): they keep its font (C# JoinsPreviousGlyph). */
|
|
12
|
+
export declare function JoinsPrevious(cp: number): boolean;
|
|
13
|
+
/** Right-to-left letters: a paragraph with one of them gets the bidirectional pass. */
|
|
14
|
+
export declare function IsRtl(cp: number): boolean;
|
|
15
|
+
/** Whether a text has a character `test` accepts. */
|
|
16
|
+
export declare function HasCodePoint(text: string, test: (cp: number) => boolean): boolean;
|
|
17
|
+
/** One shaped run: glyphs from x = 0 on the baseline, in visual order, and the advance of every code point in logical order (a cluster's advance on its first code point). */
|
|
18
|
+
export interface Shaped {
|
|
19
|
+
Glyphs: Uint16Array;
|
|
20
|
+
Positions: Float32Array;
|
|
21
|
+
Advances: number[];
|
|
22
|
+
Width: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Shapes `text` with `font` in one direction (the caller splits runs by font and by bidirectional level), `spacing`
|
|
26
|
+
* pixels after every code point. Undefined when the font was not registered through the font registry (no shaping
|
|
27
|
+
* data): the caller keeps its plain glyphs.
|
|
28
|
+
*/
|
|
29
|
+
export declare function Shape(font: Font, text: string, rtl: boolean, spacing: number): Shaped | undefined;
|
|
30
|
+
/** The visual runs of a line: for every UTF-16 index, the rank of its run from the left and whether it runs right to left. */
|
|
31
|
+
export interface BidiMap {
|
|
32
|
+
Rank: Int32Array;
|
|
33
|
+
Rtl: Uint8Array;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Bidirectional runs of a line (Unicode bidirectional algorithm, ICU in CanvasKit), read from the visual order of a
|
|
37
|
+
* paragraph of that line: digits after Arabic in a left-to-right paragraph stay with the Arabic run (level 2), as the
|
|
38
|
+
* algorithm orders them. Undefined when no font was registered for shaping.
|
|
39
|
+
*/
|
|
40
|
+
export declare function BidiRuns(text: string, rtlParagraph: boolean): BidiMap | undefined;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { Super } from "./Super";
|
|
2
|
+
/**
|
|
3
|
+
* Text shaping (C# SkiaLabel DrawShapedText, HarfBuzzSharp there; DrawnUi.Rust shaping.rs, harfrust) for the text that
|
|
4
|
+
* needs it: complex scripts whose letters join or reorder (Arabic, Hebrew marks, Indic, Thai...) and emoji sequences
|
|
5
|
+
* (ZWJ families, skin tones, flags, keycaps). Everything else keeps the plain path: one glyph per character.
|
|
6
|
+
* HarfBuzz and ICU come with the CanvasKit paragraph module, already in the build (no extra wasm): a run is laid out as a
|
|
7
|
+
* one-font paragraph and its shaped glyphs are read back; the label keeps its own line breaking and font fallback.
|
|
8
|
+
*/
|
|
9
|
+
/** C# SkiaLabel.UnicodeNeedsShaping, plus the characters that make emoji sequences and the right-to-left scripts C# leaves out. */
|
|
10
|
+
export function NeedsShaping(cp) {
|
|
11
|
+
return (cp >= 0x0590 && cp <= 0x08FF) // Hebrew, Arabic, Syriac, Arabic Supplement, Thaana, NKo, Samaritan, Mandaic, Arabic Extended
|
|
12
|
+
|| (cp >= 0x0900 && cp <= 0x0DFF) // Devanagari .. Sinhala
|
|
13
|
+
|| (cp >= 0x0E00 && cp <= 0x0FFF) // Thai, Lao, Tibetan
|
|
14
|
+
|| (cp >= 0x1000 && cp <= 0x109F) // Myanmar
|
|
15
|
+
|| (cp >= 0x10A0 && cp <= 0x10FF) // Georgian
|
|
16
|
+
|| (cp >= 0x1100 && cp <= 0x11FF) // Hangul Jamo
|
|
17
|
+
|| (cp >= 0x1200 && cp <= 0x137F) // Ethiopic
|
|
18
|
+
|| (cp >= 0x1780 && cp <= 0x18AF) // Khmer, Mongolian
|
|
19
|
+
|| cp === 0x200C || cp === 0x200D // ZWNJ, ZWJ
|
|
20
|
+
|| cp === 0x20E3 // keycap
|
|
21
|
+
|| (cp >= 0x2600 && cp <= 0x27BF) // symbols that have emoji forms
|
|
22
|
+
|| (cp >= 0xFB1D && cp <= 0xFDFF) // Hebrew and Arabic presentation forms A
|
|
23
|
+
|| (cp >= 0xFE00 && cp <= 0xFE0F) // variation selectors (VS15 / VS16)
|
|
24
|
+
|| (cp >= 0xFE70 && cp <= 0xFEFF) // Arabic presentation forms B
|
|
25
|
+
|| (cp >= 0x1F000 && cp <= 0x1FAFF) // emoji, regional indicators (flags), skin tones
|
|
26
|
+
|| (cp >= 0xE0020 && cp <= 0xE007F); // tag sequences (subdivision flags)
|
|
27
|
+
}
|
|
28
|
+
/** Characters that belong to the glyph before them (an emoji sequence or a mark): they keep its font (C# JoinsPreviousGlyph). */
|
|
29
|
+
export function JoinsPrevious(cp) {
|
|
30
|
+
return cp === 0x200D || cp === 0x20E3 || (cp >= 0xFE00 && cp <= 0xFE0F) || (cp >= 0x1F3FB && cp <= 0x1F3FF) || (cp >= 0xE0020 && cp <= 0xE007F);
|
|
31
|
+
}
|
|
32
|
+
/** Right-to-left letters: a paragraph with one of them gets the bidirectional pass. */
|
|
33
|
+
export function IsRtl(cp) {
|
|
34
|
+
return (cp >= 0x0590 && cp <= 0x08FF) || (cp >= 0xFB1D && cp <= 0xFDFF) || (cp >= 0xFE70 && cp <= 0xFEFF) || (cp >= 0x10800 && cp <= 0x10FFF) || (cp >= 0x1E800 && cp <= 0x1EFFF);
|
|
35
|
+
}
|
|
36
|
+
/** Whether a text has a character `test` accepts. */
|
|
37
|
+
export function HasCodePoint(text, test) {
|
|
38
|
+
for (let i = 0; i < text.length; i++) {
|
|
39
|
+
const c = text.charCodeAt(i);
|
|
40
|
+
// below U+0590 nothing needs shaping or is right to left
|
|
41
|
+
if (c < 0x0590)
|
|
42
|
+
continue;
|
|
43
|
+
const cp = text.codePointAt(i);
|
|
44
|
+
if (test(cp))
|
|
45
|
+
return true;
|
|
46
|
+
if (cp > 0xFFFF)
|
|
47
|
+
i++;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const cache = new Map();
|
|
52
|
+
/** Bounds the shaping cache; a label re-measured with new text shapes again. */
|
|
53
|
+
const CacheLimit = 4000;
|
|
54
|
+
/**
|
|
55
|
+
* Shapes `text` with `font` in one direction (the caller splits runs by font and by bidirectional level), `spacing`
|
|
56
|
+
* pixels after every code point. Undefined when the font was not registered through the font registry (no shaping
|
|
57
|
+
* data): the caller keeps its plain glyphs.
|
|
58
|
+
*/
|
|
59
|
+
export function Shape(font, text, rtl, spacing) {
|
|
60
|
+
const family = Super.ShapingFamilyOf(font);
|
|
61
|
+
if (!family || !text)
|
|
62
|
+
return undefined;
|
|
63
|
+
const size = font.getSize();
|
|
64
|
+
const key = `${family}|${size}|${rtl ? 1 : 0}|${spacing}|${text}`;
|
|
65
|
+
const hit = cache.get(key);
|
|
66
|
+
if (hit !== undefined)
|
|
67
|
+
return hit ?? undefined;
|
|
68
|
+
const shaped = ShapeNow(font, family, size, text, rtl, spacing);
|
|
69
|
+
if (cache.size >= CacheLimit)
|
|
70
|
+
cache.clear();
|
|
71
|
+
cache.set(key, shaped ?? null);
|
|
72
|
+
return shaped;
|
|
73
|
+
}
|
|
74
|
+
/** Width a run is laid out in: one line, never wrapped; a right-to-left run ends at its right edge. */
|
|
75
|
+
const LayoutWidth = 1e6;
|
|
76
|
+
function Paragraph(family, size, text, rtl) {
|
|
77
|
+
const CK = Super.CK;
|
|
78
|
+
const style = new CK.ParagraphStyle({ textStyle: { fontFamilies: [family], fontSize: size }, textDirection: rtl ? CK.TextDirection.RTL : CK.TextDirection.LTR });
|
|
79
|
+
const builder = CK.ParagraphBuilder.MakeFromFontProvider(style, Super.ShapingFonts);
|
|
80
|
+
builder.addText(text);
|
|
81
|
+
const paragraph = builder.build();
|
|
82
|
+
builder.delete();
|
|
83
|
+
paragraph.layout(LayoutWidth);
|
|
84
|
+
return paragraph;
|
|
85
|
+
}
|
|
86
|
+
function ShapeNow(font, family, size, text, rtl, spacing) {
|
|
87
|
+
const paragraph = Paragraph(family, size, text, rtl);
|
|
88
|
+
try {
|
|
89
|
+
const lines = paragraph.getShapedLines();
|
|
90
|
+
if (lines.length === 0)
|
|
91
|
+
return undefined;
|
|
92
|
+
// the code point of every UTF-8 byte offset (the shaped runs report clusters in UTF-8)
|
|
93
|
+
const cpOfByte = [], u16OfCp = [];
|
|
94
|
+
let cps = 0, u16 = 0;
|
|
95
|
+
for (const ch of text) {
|
|
96
|
+
const c = ch.codePointAt(0), bytes = c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
|
|
97
|
+
for (let b = 0; b < bytes; b++)
|
|
98
|
+
cpOfByte.push(cps);
|
|
99
|
+
u16OfCp.push(u16);
|
|
100
|
+
cps++;
|
|
101
|
+
u16 += ch.length;
|
|
102
|
+
}
|
|
103
|
+
const cpOf = (byte) => cpOfByte[Math.min(byte, cpOfByte.length - 1)] ?? 0;
|
|
104
|
+
let count = 0;
|
|
105
|
+
for (const l of lines)
|
|
106
|
+
for (const r of l.runs)
|
|
107
|
+
count += r.glyphs.length;
|
|
108
|
+
const glyphs = new Uint16Array(count), positions = new Float32Array(count * 2), advances = new Array(cps).fill(0), clusters = new Array(count);
|
|
109
|
+
// one line (no width limit, no line breaks in a run); runs come in visual order. Glyph positions include their
|
|
110
|
+
// offsets (a mark sits off its pen position), so the run's left edge and width come from the paragraph itself.
|
|
111
|
+
const shapedWidth = paragraph.getMaxIntrinsicWidth();
|
|
112
|
+
const left = rtl ? LayoutWidth - shapedWidth : 0;
|
|
113
|
+
let k = 0;
|
|
114
|
+
for (const l of lines) {
|
|
115
|
+
for (const r of l.runs) {
|
|
116
|
+
for (let g = 0; g < r.glyphs.length; g++, k++) {
|
|
117
|
+
glyphs[k] = r.glyphs[g];
|
|
118
|
+
positions[k * 2] = r.positions[g * 2] - left;
|
|
119
|
+
positions[k * 2 + 1] = r.positions[g * 2 + 1] - l.baseline;
|
|
120
|
+
clusters[k] = cpOf(r.offsets[g]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// the advance of every grapheme on its first code point (caret boxes): the paragraph's own grapheme bounds, which
|
|
125
|
+
// follow the shaped pen advances (DrawnUi.Rust: HarfBuzz x_advance per cluster)
|
|
126
|
+
let sum = 0;
|
|
127
|
+
for (let i = 0; i < cps; i++) {
|
|
128
|
+
const info = paragraph.getGlyphInfoAt(u16OfCp[i]);
|
|
129
|
+
if (!info || info.graphemeClusterTextRange.start !== u16OfCp[i])
|
|
130
|
+
continue;
|
|
131
|
+
const b = info.graphemeLayoutBounds;
|
|
132
|
+
advances[i] = b[2] - b[0];
|
|
133
|
+
sum += advances[i];
|
|
134
|
+
}
|
|
135
|
+
if (sum <= 0) {
|
|
136
|
+
// no grapheme bounds: the glyphs' own widths, scaled to the shaped width
|
|
137
|
+
const glyphWidths = font.getGlyphWidths(glyphs);
|
|
138
|
+
for (let g = 0; g < count; g++) {
|
|
139
|
+
advances[clusters[g]] += glyphWidths[g];
|
|
140
|
+
sum += glyphWidths[g];
|
|
141
|
+
}
|
|
142
|
+
if (sum > 0)
|
|
143
|
+
for (let i = 0; i < advances.length; i++)
|
|
144
|
+
advances[i] *= shapedWidth / sum;
|
|
145
|
+
}
|
|
146
|
+
if (spacing !== 0) {
|
|
147
|
+
// visual order: the spacing of the code points between two clusters goes between them
|
|
148
|
+
let extra = 0, last = -1;
|
|
149
|
+
for (let g = 0; g < count; g++) {
|
|
150
|
+
if (last >= 0 && last !== clusters[g])
|
|
151
|
+
extra += spacing * Math.abs(last - clusters[g]);
|
|
152
|
+
last = clusters[g];
|
|
153
|
+
positions[g * 2] += extra;
|
|
154
|
+
}
|
|
155
|
+
for (let i = 0; i < advances.length; i++)
|
|
156
|
+
advances[i] += spacing;
|
|
157
|
+
}
|
|
158
|
+
const width = shapedWidth + spacing * cps;
|
|
159
|
+
return { Glyphs: glyphs, Positions: positions, Advances: advances, Width: width };
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
paragraph.delete();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Bidirectional runs of a line (Unicode bidirectional algorithm, ICU in CanvasKit), read from the visual order of a
|
|
167
|
+
* paragraph of that line: digits after Arabic in a left-to-right paragraph stay with the Arabic run (level 2), as the
|
|
168
|
+
* algorithm orders them. Undefined when no font was registered for shaping.
|
|
169
|
+
*/
|
|
170
|
+
export function BidiRuns(text, rtlParagraph) {
|
|
171
|
+
const family = Super.AnyShapingFamily();
|
|
172
|
+
if (!family || !text)
|
|
173
|
+
return undefined;
|
|
174
|
+
const CK = Super.CK;
|
|
175
|
+
const paragraph = Paragraph(family, 12, text, rtlParagraph);
|
|
176
|
+
try {
|
|
177
|
+
// UTF-16 index of every UTF-8 byte offset (the shaped runs report clusters in UTF-8)
|
|
178
|
+
const u16OfByte = [];
|
|
179
|
+
let u = 0;
|
|
180
|
+
for (const ch of text) {
|
|
181
|
+
const c = ch.codePointAt(0), bytes = c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
|
|
182
|
+
for (let k = 0; k < bytes; k++)
|
|
183
|
+
u16OfByte.push(u);
|
|
184
|
+
u += ch.length;
|
|
185
|
+
}
|
|
186
|
+
u16OfByte.push(u);
|
|
187
|
+
// every cluster start with the rank of its run; runs come left to right
|
|
188
|
+
const starts = [];
|
|
189
|
+
const firstOfRank = [];
|
|
190
|
+
let rank = 0;
|
|
191
|
+
for (const l of paragraph.getShapedLines()) {
|
|
192
|
+
for (const r of l.runs) {
|
|
193
|
+
let first = Infinity;
|
|
194
|
+
for (let g = 0; g < r.glyphs.length; g++) {
|
|
195
|
+
const at = u16OfByte[Math.min(r.offsets[g], u16OfByte.length - 1)];
|
|
196
|
+
starts.push({ At: at, Rank: rank });
|
|
197
|
+
first = Math.min(first, at);
|
|
198
|
+
}
|
|
199
|
+
firstOfRank.push(first);
|
|
200
|
+
rank++;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (starts.length === 0)
|
|
204
|
+
return undefined;
|
|
205
|
+
starts.sort((x, y) => x.At - y.At);
|
|
206
|
+
const map = { Rank: new Int32Array(text.length), Rtl: new Uint8Array(text.length) };
|
|
207
|
+
let k = 0, current = starts[0].Rank;
|
|
208
|
+
for (let i = 0; i < text.length; i++) {
|
|
209
|
+
while (k < starts.length && starts[k].At <= i)
|
|
210
|
+
current = starts[k++].Rank;
|
|
211
|
+
map.Rank[i] = current;
|
|
212
|
+
}
|
|
213
|
+
const rtlOfRank = firstOfRank.map((at) => {
|
|
214
|
+
const info = Number.isFinite(at) ? paragraph.getGlyphInfoAt(at) : null;
|
|
215
|
+
return info ? info.dir.value === CK.TextDirection.RTL.value : rtlParagraph;
|
|
216
|
+
});
|
|
217
|
+
for (let i = 0; i < text.length; i++)
|
|
218
|
+
map.Rtl[i] = rtlOfRank[map.Rank[i]] ? 1 : 0;
|
|
219
|
+
return map;
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
paragraph.delete();
|
|
223
|
+
}
|
|
224
|
+
}
|