@loksly/lcd-canvas-kit 0.2.4 → 0.2.6
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/dist/text.d.ts +7 -0
- package/dist/text.js +131 -14
- package/package.json +2 -2
- package/src/text.ts +136 -14
package/dist/text.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export declare function setDefaultFamily(family: string): void;
|
|
|
12
12
|
* ("DejaVu Sans,DejaVu Sans Condensed"). Returns the paths that failed.
|
|
13
13
|
*/
|
|
14
14
|
export declare function registerFontFiles(fonts: Record<string, string>): string[];
|
|
15
|
+
/** Forgets the measured widths (tests, or fonts replaced under the same name). */
|
|
16
|
+
export declare function clearTextCache(): void;
|
|
15
17
|
export interface TextOpts {
|
|
16
18
|
size: number;
|
|
17
19
|
family?: string;
|
|
@@ -24,6 +26,11 @@ export interface TextOpts {
|
|
|
24
26
|
/** Height of the small caps relative to a capital letter. */
|
|
25
27
|
capRatio?: number;
|
|
26
28
|
}
|
|
29
|
+
/** Whether the text has Chinese, Japanese or Korean in it. */
|
|
30
|
+
export declare function hasCjk(text: string): boolean;
|
|
31
|
+
export declare function setMinCjkPx(px: number): void;
|
|
32
|
+
/** The smallest CJK size a widget asked for and got raised since the last call (null: none), and starts again. */
|
|
33
|
+
export declare function takeCjkEnlarged(): number | null;
|
|
27
34
|
/** Width the text would take with this tracking, without drawing it. */
|
|
28
35
|
export declare function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): number;
|
|
29
36
|
/**
|
package/dist/text.js
CHANGED
|
@@ -15,20 +15,105 @@ export function setDefaultFamily(family) {
|
|
|
15
15
|
* ("DejaVu Sans,DejaVu Sans Condensed"). Returns the paths that failed.
|
|
16
16
|
*/
|
|
17
17
|
export function registerFontFiles(fonts) {
|
|
18
|
+
// A family may now draw with another file: measure it again.
|
|
19
|
+
widths.clear();
|
|
18
20
|
return Object.entries(fonts).filter(([family, path]) => !GlobalFonts.registerFromPath(path, family)).map(([, path]) => path);
|
|
19
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Widths of single characters, by font. Measuring goes character by
|
|
24
|
+
* character (for tracking and small caps), and `measureText` with a font
|
|
25
|
+
* set before every character made wrapping a long paragraph cost hundreds
|
|
26
|
+
* of milliseconds (a news summary of 7 000 characters: 343 ms for three
|
|
27
|
+
* lines). A character's width only depends on its font and size.
|
|
28
|
+
*/
|
|
29
|
+
const widths = new Map();
|
|
30
|
+
/** Fonts kept (sizes × families); past it the cache starts again. */
|
|
31
|
+
const MAX_FONTS = 400;
|
|
32
|
+
function widthOf(ctx, font, ch) {
|
|
33
|
+
let byChar = widths.get(font);
|
|
34
|
+
if (!byChar) {
|
|
35
|
+
if (widths.size >= MAX_FONTS)
|
|
36
|
+
widths.clear();
|
|
37
|
+
byChar = new Map();
|
|
38
|
+
widths.set(font, byChar);
|
|
39
|
+
}
|
|
40
|
+
let w = byChar.get(ch);
|
|
41
|
+
if (w === undefined) {
|
|
42
|
+
if (ctx.font !== font)
|
|
43
|
+
ctx.font = font;
|
|
44
|
+
w = ctx.measureText(ch).width;
|
|
45
|
+
byChar.set(ch, w);
|
|
46
|
+
}
|
|
47
|
+
return w;
|
|
48
|
+
}
|
|
49
|
+
/** Forgets the measured widths (tests, or fonts replaced under the same name). */
|
|
50
|
+
export function clearTextCache() {
|
|
51
|
+
widths.clear();
|
|
52
|
+
}
|
|
53
|
+
const CJK = /[\u1100-\u11ff\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef]/;
|
|
54
|
+
/** Whether the text has Chinese, Japanese or Korean in it. */
|
|
55
|
+
export function hasCjk(text) {
|
|
56
|
+
return CJK.test(text);
|
|
57
|
+
}
|
|
58
|
+
/** No letter-spacing in a text with CJK: it would split it into single characters. */
|
|
59
|
+
function trackingOf(text, o) {
|
|
60
|
+
return o.tracking && hasCjk(text) ? 0 : (o.tracking ?? 0);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The smallest Chinese, Japanese or Korean text that can be read on the
|
|
64
|
+
* panel being painted, in its own pixels (0: no minimum). Kiosko sets it per
|
|
65
|
+
* screen from the panel's pixel size and how far its readers sit; the kit
|
|
66
|
+
* paints a CJK character asked smaller at this size instead (Latin is left
|
|
67
|
+
* alone: a dense hanzi turns into a blot first).
|
|
68
|
+
*/
|
|
69
|
+
let minCjkPx = 0;
|
|
70
|
+
/** The smallest CJK size asked for since `takeCjkEnlarged`, in panel pixels, when it was raised. */
|
|
71
|
+
let enlarged = Infinity;
|
|
72
|
+
export function setMinCjkPx(px) {
|
|
73
|
+
if (px !== minCjkPx)
|
|
74
|
+
widths.clear();
|
|
75
|
+
minCjkPx = Math.max(0, px);
|
|
76
|
+
}
|
|
77
|
+
/** The smallest CJK size a widget asked for and got raised since the last call (null: none), and starts again. */
|
|
78
|
+
export function takeCjkEnlarged() {
|
|
79
|
+
const v = enlarged;
|
|
80
|
+
enlarged = Infinity;
|
|
81
|
+
return Number.isFinite(v) ? Math.round(v) : null;
|
|
82
|
+
}
|
|
83
|
+
/** How much the context scales what it draws (a widget's zoom, the panel's turn). */
|
|
84
|
+
function scaleOf(ctx) {
|
|
85
|
+
const t = ctx.getTransform?.();
|
|
86
|
+
return t ? Math.hypot(t.a, t.b) || 1 : 1;
|
|
87
|
+
}
|
|
20
88
|
function glyphs(ctx, text, o) {
|
|
21
89
|
const family = o.family ?? defaultFamily;
|
|
22
|
-
|
|
90
|
+
// Han, kana and hangul have no capitals, and spaced out they read as separate words.
|
|
91
|
+
const cjk = hasCjk(text);
|
|
92
|
+
const small = o.smallCaps === true && !cjk;
|
|
23
93
|
const capSize = o.size * (o.capRatio ?? 0.76);
|
|
24
94
|
const out = [];
|
|
95
|
+
const font = `${o.size}px "${family}"`;
|
|
96
|
+
const capFont = `${capSize}px "${family}"`;
|
|
97
|
+
// CJK characters asked smaller than can be read on this panel are drawn at the minimum.
|
|
98
|
+
let cjkSize = o.size;
|
|
99
|
+
if (cjk && minCjkPx > 0) {
|
|
100
|
+
const scale = scaleOf(ctx);
|
|
101
|
+
if (o.size * scale < minCjkPx - 0.5) {
|
|
102
|
+
cjkSize = minCjkPx / scale;
|
|
103
|
+
enlarged = Math.min(enlarged, o.size * scale);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const cjkFont = `${cjkSize}px "${family}"`;
|
|
25
107
|
for (const ch of text) {
|
|
108
|
+
if (cjkSize !== o.size && CJK.test(ch)) {
|
|
109
|
+
out.push({ ch, size: cjkSize, width: widthOf(ctx, cjkFont, ch) });
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
26
112
|
// In small caps, a lower-case letter is drawn upper-case at a smaller size.
|
|
27
113
|
const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
|
|
28
114
|
const size = isLower ? capSize : o.size;
|
|
29
115
|
const draw = small ? ch.toUpperCase() : ch;
|
|
30
|
-
|
|
31
|
-
out.push({ ch: draw, size, width: ctx.measureText(draw).width });
|
|
116
|
+
out.push({ ch: draw, size, width: widthOf(ctx, isLower ? capFont : font, draw) });
|
|
32
117
|
}
|
|
33
118
|
return out;
|
|
34
119
|
}
|
|
@@ -40,7 +125,7 @@ function advance(gs, tracking) {
|
|
|
40
125
|
}
|
|
41
126
|
/** Width the text would take with this tracking, without drawing it. */
|
|
42
127
|
export function measureTracked(ctx, text, o) {
|
|
43
|
-
return advance(glyphs(ctx, text, o), o
|
|
128
|
+
return advance(glyphs(ctx, text, o), trackingOf(text, o));
|
|
44
129
|
}
|
|
45
130
|
/**
|
|
46
131
|
* Draws character by character to apply tracking and small caps, which the
|
|
@@ -48,15 +133,19 @@ export function measureTracked(ctx, text, o) {
|
|
|
48
133
|
*/
|
|
49
134
|
export function drawTracked(ctx, text, x, y, o) {
|
|
50
135
|
const family = o.family ?? defaultFamily;
|
|
51
|
-
const tracking = o
|
|
136
|
+
const tracking = trackingOf(text, o);
|
|
52
137
|
const gs = glyphs(ctx, text, o);
|
|
53
138
|
const total = advance(gs, tracking);
|
|
54
139
|
let cursor = o.align === 'right' ? x - total : o.align === 'center' ? x - total / 2 : x;
|
|
55
140
|
if (o.color)
|
|
56
141
|
ctx.fillStyle = o.color;
|
|
57
142
|
ctx.textBaseline = 'alphabetic';
|
|
143
|
+
let size = -1;
|
|
58
144
|
for (const g of gs) {
|
|
59
|
-
|
|
145
|
+
if (g.size !== size) {
|
|
146
|
+
ctx.font = `${g.size}px "${family}"`;
|
|
147
|
+
size = g.size;
|
|
148
|
+
}
|
|
60
149
|
ctx.fillText(g.ch, cursor, y);
|
|
61
150
|
cursor += g.width + tracking;
|
|
62
151
|
}
|
|
@@ -101,28 +190,56 @@ export function fitSize(ctx, text, maxWidth, o, minSize) {
|
|
|
101
190
|
export function drawClipped(ctx, text, x, y, maxWidth, o) {
|
|
102
191
|
return drawTracked(ctx, truncate(ctx, text, maxWidth, o), x, y, o);
|
|
103
192
|
}
|
|
193
|
+
/** Characters a line must not start with (Chinese and Japanese line breaking, "kinsoku"). */
|
|
194
|
+
const NO_START = new Set([...'、。,.:;?!)」』】》〉〕}〗〙〛’”…‥ー々ゝゞヽヾぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ,.;:?!)]}%']);
|
|
195
|
+
/** Characters a line must not end with. */
|
|
196
|
+
const NO_END = new Set([...'(「『【《〈〔{〖〘〚‘“([{$']);
|
|
197
|
+
const isSpace = (c) => /\s/.test(c);
|
|
104
198
|
/**
|
|
105
|
-
* Longest prefix of `text` that fits in `maxWidth`,
|
|
106
|
-
*
|
|
107
|
-
*
|
|
199
|
+
* Longest prefix of `text` that fits in `maxWidth`, as a string length.
|
|
200
|
+
* It breaks after a space, or between two characters when either is
|
|
201
|
+
* Chinese, Japanese or Korean (they have no spaces), never before a
|
|
202
|
+
* closing mark (,。」) nor after an opening one (「(). In Latin text it
|
|
203
|
+
* backs off to the last space however far; in text with CJK only if that
|
|
204
|
+
* keeps most of the line ("AI 模型在…" doesn't leave "AI" alone). If no
|
|
205
|
+
* break fits, it cuts hard: splitting a long path is better than an empty
|
|
206
|
+
* line.
|
|
108
207
|
*
|
|
109
208
|
* The search is binary because measuring is expensive: `measureTracked` walks
|
|
110
|
-
* character by character to apply tracking.
|
|
209
|
+
* character by character to apply tracking. It counts characters, not
|
|
210
|
+
* UTF-16 units, so it never splits one.
|
|
111
211
|
*/
|
|
112
212
|
function fitPrefix(ctx, text, maxWidth, o) {
|
|
113
213
|
if (measureTracked(ctx, text, o) <= maxWidth)
|
|
114
214
|
return text.length;
|
|
215
|
+
const chars = [...text];
|
|
115
216
|
let lo = 1;
|
|
116
|
-
let hi =
|
|
217
|
+
let hi = chars.length;
|
|
117
218
|
while (lo < hi) {
|
|
118
219
|
const mid = Math.ceil((lo + hi) / 2);
|
|
119
|
-
if (measureTracked(ctx,
|
|
220
|
+
if (measureTracked(ctx, chars.slice(0, mid).join(''), o) <= maxWidth)
|
|
120
221
|
lo = mid;
|
|
121
222
|
else
|
|
122
223
|
hi = mid - 1;
|
|
123
224
|
}
|
|
124
|
-
const
|
|
125
|
-
|
|
225
|
+
const cjk = hasCjk(text);
|
|
226
|
+
const fits = (i) => !NO_START.has(chars[i]) && !NO_END.has(chars[i - 1]);
|
|
227
|
+
let cut = 0;
|
|
228
|
+
for (let i = lo; i >= 1; i--) {
|
|
229
|
+
const before = chars[i - 1];
|
|
230
|
+
const after = chars[i];
|
|
231
|
+
if ((isSpace(before) || isSpace(after) || CJK.test(before) || CJK.test(after)) && fits(i)) {
|
|
232
|
+
cut = i;
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (!cut || (cjk && cut < lo * 0.6)) {
|
|
237
|
+
// Hard cut, still not starting the next line with a closing mark.
|
|
238
|
+
cut = lo;
|
|
239
|
+
while (cut > 1 && !fits(cut))
|
|
240
|
+
cut--;
|
|
241
|
+
}
|
|
242
|
+
return chars.slice(0, cut).join('').length;
|
|
126
243
|
}
|
|
127
244
|
/**
|
|
128
245
|
* Splits the text into at most `maxLines` lines that fit in `maxWidth`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loksly/lcd-canvas-kit",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Drawing helpers for small always-on LCD panels on @napi-rs/canvas: tracked text, small caps, word wrap, sparklines, pixel shift against image retention, weather and moon icons.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"check": "tsc -p tsconfig.json --noEmit"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@loksly/weather-astro": "0.2.
|
|
45
|
+
"@loksly/weather-astro": "0.2.6"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"@napi-rs/canvas": ">=0.1.65 <2"
|
package/src/text.ts
CHANGED
|
@@ -18,9 +18,43 @@ export function setDefaultFamily(family: string): void {
|
|
|
18
18
|
* ("DejaVu Sans,DejaVu Sans Condensed"). Returns the paths that failed.
|
|
19
19
|
*/
|
|
20
20
|
export function registerFontFiles(fonts: Record<string, string>): string[] {
|
|
21
|
+
// A family may now draw with another file: measure it again.
|
|
22
|
+
widths.clear();
|
|
21
23
|
return Object.entries(fonts).filter(([family, path]) => !GlobalFonts.registerFromPath(path, family)).map(([, path]) => path);
|
|
22
24
|
}
|
|
23
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Widths of single characters, by font. Measuring goes character by
|
|
28
|
+
* character (for tracking and small caps), and `measureText` with a font
|
|
29
|
+
* set before every character made wrapping a long paragraph cost hundreds
|
|
30
|
+
* of milliseconds (a news summary of 7 000 characters: 343 ms for three
|
|
31
|
+
* lines). A character's width only depends on its font and size.
|
|
32
|
+
*/
|
|
33
|
+
const widths = new Map<string, Map<string, number>>();
|
|
34
|
+
/** Fonts kept (sizes × families); past it the cache starts again. */
|
|
35
|
+
const MAX_FONTS = 400;
|
|
36
|
+
|
|
37
|
+
function widthOf(ctx: SKRSContext2D, font: string, ch: string): number {
|
|
38
|
+
let byChar = widths.get(font);
|
|
39
|
+
if (!byChar) {
|
|
40
|
+
if (widths.size >= MAX_FONTS) widths.clear();
|
|
41
|
+
byChar = new Map();
|
|
42
|
+
widths.set(font, byChar);
|
|
43
|
+
}
|
|
44
|
+
let w = byChar.get(ch);
|
|
45
|
+
if (w === undefined) {
|
|
46
|
+
if (ctx.font !== font) ctx.font = font;
|
|
47
|
+
w = ctx.measureText(ch).width;
|
|
48
|
+
byChar.set(ch, w);
|
|
49
|
+
}
|
|
50
|
+
return w;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Forgets the measured widths (tests, or fonts replaced under the same name). */
|
|
54
|
+
export function clearTextCache(): void {
|
|
55
|
+
widths.clear();
|
|
56
|
+
}
|
|
57
|
+
|
|
24
58
|
export interface TextOpts {
|
|
25
59
|
size: number;
|
|
26
60
|
family?: string;
|
|
@@ -34,24 +68,80 @@ export interface TextOpts {
|
|
|
34
68
|
capRatio?: number;
|
|
35
69
|
}
|
|
36
70
|
|
|
71
|
+
const CJK = /[\u1100-\u11ff\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef]/;
|
|
72
|
+
/** Whether the text has Chinese, Japanese or Korean in it. */
|
|
73
|
+
export function hasCjk(text: string): boolean {
|
|
74
|
+
return CJK.test(text);
|
|
75
|
+
}
|
|
76
|
+
/** No letter-spacing in a text with CJK: it would split it into single characters. */
|
|
77
|
+
function trackingOf(text: string, o: TextOpts): number {
|
|
78
|
+
return o.tracking && hasCjk(text) ? 0 : (o.tracking ?? 0);
|
|
79
|
+
}
|
|
80
|
+
|
|
37
81
|
interface Glyph {
|
|
38
82
|
ch: string;
|
|
39
83
|
size: number;
|
|
40
84
|
width: number;
|
|
41
85
|
}
|
|
42
86
|
|
|
87
|
+
/**
|
|
88
|
+
* The smallest Chinese, Japanese or Korean text that can be read on the
|
|
89
|
+
* panel being painted, in its own pixels (0: no minimum). Kiosko sets it per
|
|
90
|
+
* screen from the panel's pixel size and how far its readers sit; the kit
|
|
91
|
+
* paints a CJK character asked smaller at this size instead (Latin is left
|
|
92
|
+
* alone: a dense hanzi turns into a blot first).
|
|
93
|
+
*/
|
|
94
|
+
let minCjkPx = 0;
|
|
95
|
+
/** The smallest CJK size asked for since `takeCjkEnlarged`, in panel pixels, when it was raised. */
|
|
96
|
+
let enlarged = Infinity;
|
|
97
|
+
|
|
98
|
+
export function setMinCjkPx(px: number): void {
|
|
99
|
+
if (px !== minCjkPx) widths.clear();
|
|
100
|
+
minCjkPx = Math.max(0, px);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The smallest CJK size a widget asked for and got raised since the last call (null: none), and starts again. */
|
|
104
|
+
export function takeCjkEnlarged(): number | null {
|
|
105
|
+
const v = enlarged;
|
|
106
|
+
enlarged = Infinity;
|
|
107
|
+
return Number.isFinite(v) ? Math.round(v) : null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** How much the context scales what it draws (a widget's zoom, the panel's turn). */
|
|
111
|
+
function scaleOf(ctx: SKRSContext2D): number {
|
|
112
|
+
const t = (ctx as { getTransform?: () => { a: number; b: number } }).getTransform?.();
|
|
113
|
+
return t ? Math.hypot(t.a, t.b) || 1 : 1;
|
|
114
|
+
}
|
|
115
|
+
|
|
43
116
|
function glyphs(ctx: SKRSContext2D, text: string, o: TextOpts): Glyph[] {
|
|
44
117
|
const family = o.family ?? defaultFamily;
|
|
45
|
-
|
|
118
|
+
// Han, kana and hangul have no capitals, and spaced out they read as separate words.
|
|
119
|
+
const cjk = hasCjk(text);
|
|
120
|
+
const small = o.smallCaps === true && !cjk;
|
|
46
121
|
const capSize = o.size * (o.capRatio ?? 0.76);
|
|
47
122
|
const out: Glyph[] = [];
|
|
123
|
+
const font = `${o.size}px "${family}"`;
|
|
124
|
+
const capFont = `${capSize}px "${family}"`;
|
|
125
|
+
// CJK characters asked smaller than can be read on this panel are drawn at the minimum.
|
|
126
|
+
let cjkSize = o.size;
|
|
127
|
+
if (cjk && minCjkPx > 0) {
|
|
128
|
+
const scale = scaleOf(ctx);
|
|
129
|
+
if (o.size * scale < minCjkPx - 0.5) {
|
|
130
|
+
cjkSize = minCjkPx / scale;
|
|
131
|
+
enlarged = Math.min(enlarged, o.size * scale);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const cjkFont = `${cjkSize}px "${family}"`;
|
|
48
135
|
for (const ch of text) {
|
|
136
|
+
if (cjkSize !== o.size && CJK.test(ch)) {
|
|
137
|
+
out.push({ ch, size: cjkSize, width: widthOf(ctx, cjkFont, ch) });
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
49
140
|
// In small caps, a lower-case letter is drawn upper-case at a smaller size.
|
|
50
141
|
const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
|
|
51
142
|
const size = isLower ? capSize : o.size;
|
|
52
143
|
const draw = small ? ch.toUpperCase() : ch;
|
|
53
|
-
|
|
54
|
-
out.push({ ch: draw, size, width: ctx.measureText(draw).width });
|
|
144
|
+
out.push({ ch: draw, size, width: widthOf(ctx, isLower ? capFont : font, draw) });
|
|
55
145
|
}
|
|
56
146
|
return out;
|
|
57
147
|
}
|
|
@@ -64,7 +154,7 @@ function advance(gs: Glyph[], tracking: number): number {
|
|
|
64
154
|
|
|
65
155
|
/** Width the text would take with this tracking, without drawing it. */
|
|
66
156
|
export function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): number {
|
|
67
|
-
return advance(glyphs(ctx, text, o), o
|
|
157
|
+
return advance(glyphs(ctx, text, o), trackingOf(text, o));
|
|
68
158
|
}
|
|
69
159
|
|
|
70
160
|
/**
|
|
@@ -73,14 +163,18 @@ export function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): n
|
|
|
73
163
|
*/
|
|
74
164
|
export function drawTracked(ctx: SKRSContext2D, text: string, x: number, y: number, o: TextOpts): number {
|
|
75
165
|
const family = o.family ?? defaultFamily;
|
|
76
|
-
const tracking = o
|
|
166
|
+
const tracking = trackingOf(text, o);
|
|
77
167
|
const gs = glyphs(ctx, text, o);
|
|
78
168
|
const total = advance(gs, tracking);
|
|
79
169
|
let cursor = o.align === 'right' ? x - total : o.align === 'center' ? x - total / 2 : x;
|
|
80
170
|
if (o.color) ctx.fillStyle = o.color;
|
|
81
171
|
ctx.textBaseline = 'alphabetic';
|
|
172
|
+
let size = -1;
|
|
82
173
|
for (const g of gs) {
|
|
83
|
-
|
|
174
|
+
if (g.size !== size) {
|
|
175
|
+
ctx.font = `${g.size}px "${family}"`;
|
|
176
|
+
size = g.size;
|
|
177
|
+
}
|
|
84
178
|
ctx.fillText(g.ch, cursor, y);
|
|
85
179
|
cursor += g.width + tracking;
|
|
86
180
|
}
|
|
@@ -132,25 +226,53 @@ export function drawClipped(
|
|
|
132
226
|
return drawTracked(ctx, truncate(ctx, text, maxWidth, o), x, y, o);
|
|
133
227
|
}
|
|
134
228
|
|
|
229
|
+
/** Characters a line must not start with (Chinese and Japanese line breaking, "kinsoku"). */
|
|
230
|
+
const NO_START = new Set([...'、。,.:;?!)」』】》〉〕}〗〙〛’”…‥ー々ゝゞヽヾぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ,.;:?!)]}%']);
|
|
231
|
+
/** Characters a line must not end with. */
|
|
232
|
+
const NO_END = new Set([...'(「『【《〈〔{〖〘〚‘“([{$']);
|
|
233
|
+
const isSpace = (c: string) => /\s/.test(c);
|
|
234
|
+
|
|
135
235
|
/**
|
|
136
|
-
* Longest prefix of `text` that fits in `maxWidth`,
|
|
137
|
-
*
|
|
138
|
-
*
|
|
236
|
+
* Longest prefix of `text` that fits in `maxWidth`, as a string length.
|
|
237
|
+
* It breaks after a space, or between two characters when either is
|
|
238
|
+
* Chinese, Japanese or Korean (they have no spaces), never before a
|
|
239
|
+
* closing mark (,。」) nor after an opening one (「(). In Latin text it
|
|
240
|
+
* backs off to the last space however far; in text with CJK only if that
|
|
241
|
+
* keeps most of the line ("AI 模型在…" doesn't leave "AI" alone). If no
|
|
242
|
+
* break fits, it cuts hard: splitting a long path is better than an empty
|
|
243
|
+
* line.
|
|
139
244
|
*
|
|
140
245
|
* The search is binary because measuring is expensive: `measureTracked` walks
|
|
141
|
-
* character by character to apply tracking.
|
|
246
|
+
* character by character to apply tracking. It counts characters, not
|
|
247
|
+
* UTF-16 units, so it never splits one.
|
|
142
248
|
*/
|
|
143
249
|
function fitPrefix(ctx: SKRSContext2D, text: string, maxWidth: number, o: TextOpts): number {
|
|
144
250
|
if (measureTracked(ctx, text, o) <= maxWidth) return text.length;
|
|
251
|
+
const chars = [...text];
|
|
145
252
|
let lo = 1;
|
|
146
|
-
let hi =
|
|
253
|
+
let hi = chars.length;
|
|
147
254
|
while (lo < hi) {
|
|
148
255
|
const mid = Math.ceil((lo + hi) / 2);
|
|
149
|
-
if (measureTracked(ctx,
|
|
256
|
+
if (measureTracked(ctx, chars.slice(0, mid).join(''), o) <= maxWidth) lo = mid;
|
|
150
257
|
else hi = mid - 1;
|
|
151
258
|
}
|
|
152
|
-
const
|
|
153
|
-
|
|
259
|
+
const cjk = hasCjk(text);
|
|
260
|
+
const fits = (i: number) => !NO_START.has(chars[i]!) && !NO_END.has(chars[i - 1]!);
|
|
261
|
+
let cut = 0;
|
|
262
|
+
for (let i = lo; i >= 1; i--) {
|
|
263
|
+
const before = chars[i - 1]!;
|
|
264
|
+
const after = chars[i]!;
|
|
265
|
+
if ((isSpace(before) || isSpace(after) || CJK.test(before) || CJK.test(after)) && fits(i)) {
|
|
266
|
+
cut = i;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (!cut || (cjk && cut < lo * 0.6)) {
|
|
271
|
+
// Hard cut, still not starting the next line with a closing mark.
|
|
272
|
+
cut = lo;
|
|
273
|
+
while (cut > 1 && !fits(cut)) cut--;
|
|
274
|
+
}
|
|
275
|
+
return chars.slice(0, cut).join('').length;
|
|
154
276
|
}
|
|
155
277
|
|
|
156
278
|
/**
|