@loksly/lcd-canvas-kit 0.2.5 → 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 +5 -0
- package/dist/text.js +90 -11
- package/package.json +2 -2
- package/src/text.ts +94 -11
package/dist/text.d.ts
CHANGED
|
@@ -26,6 +26,11 @@ export interface TextOpts {
|
|
|
26
26
|
/** Height of the small caps relative to a capital letter. */
|
|
27
27
|
capRatio?: number;
|
|
28
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;
|
|
29
34
|
/** Width the text would take with this tracking, without drawing it. */
|
|
30
35
|
export declare function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): number;
|
|
31
36
|
/**
|
package/dist/text.js
CHANGED
|
@@ -50,14 +50,65 @@ function widthOf(ctx, font, ch) {
|
|
|
50
50
|
export function clearTextCache() {
|
|
51
51
|
widths.clear();
|
|
52
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
|
+
}
|
|
53
88
|
function glyphs(ctx, text, o) {
|
|
54
89
|
const family = o.family ?? defaultFamily;
|
|
55
|
-
|
|
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;
|
|
56
93
|
const capSize = o.size * (o.capRatio ?? 0.76);
|
|
57
94
|
const out = [];
|
|
58
95
|
const font = `${o.size}px "${family}"`;
|
|
59
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}"`;
|
|
60
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
|
+
}
|
|
61
112
|
// In small caps, a lower-case letter is drawn upper-case at a smaller size.
|
|
62
113
|
const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
|
|
63
114
|
const size = isLower ? capSize : o.size;
|
|
@@ -74,7 +125,7 @@ function advance(gs, tracking) {
|
|
|
74
125
|
}
|
|
75
126
|
/** Width the text would take with this tracking, without drawing it. */
|
|
76
127
|
export function measureTracked(ctx, text, o) {
|
|
77
|
-
return advance(glyphs(ctx, text, o), o
|
|
128
|
+
return advance(glyphs(ctx, text, o), trackingOf(text, o));
|
|
78
129
|
}
|
|
79
130
|
/**
|
|
80
131
|
* Draws character by character to apply tracking and small caps, which the
|
|
@@ -82,7 +133,7 @@ export function measureTracked(ctx, text, o) {
|
|
|
82
133
|
*/
|
|
83
134
|
export function drawTracked(ctx, text, x, y, o) {
|
|
84
135
|
const family = o.family ?? defaultFamily;
|
|
85
|
-
const tracking = o
|
|
136
|
+
const tracking = trackingOf(text, o);
|
|
86
137
|
const gs = glyphs(ctx, text, o);
|
|
87
138
|
const total = advance(gs, tracking);
|
|
88
139
|
let cursor = o.align === 'right' ? x - total : o.align === 'center' ? x - total / 2 : x;
|
|
@@ -139,28 +190,56 @@ export function fitSize(ctx, text, maxWidth, o, minSize) {
|
|
|
139
190
|
export function drawClipped(ctx, text, x, y, maxWidth, o) {
|
|
140
191
|
return drawTracked(ctx, truncate(ctx, text, maxWidth, o), x, y, o);
|
|
141
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);
|
|
142
198
|
/**
|
|
143
|
-
* Longest prefix of `text` that fits in `maxWidth`,
|
|
144
|
-
*
|
|
145
|
-
*
|
|
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.
|
|
146
207
|
*
|
|
147
208
|
* The search is binary because measuring is expensive: `measureTracked` walks
|
|
148
|
-
* 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.
|
|
149
211
|
*/
|
|
150
212
|
function fitPrefix(ctx, text, maxWidth, o) {
|
|
151
213
|
if (measureTracked(ctx, text, o) <= maxWidth)
|
|
152
214
|
return text.length;
|
|
215
|
+
const chars = [...text];
|
|
153
216
|
let lo = 1;
|
|
154
|
-
let hi =
|
|
217
|
+
let hi = chars.length;
|
|
155
218
|
while (lo < hi) {
|
|
156
219
|
const mid = Math.ceil((lo + hi) / 2);
|
|
157
|
-
if (measureTracked(ctx,
|
|
220
|
+
if (measureTracked(ctx, chars.slice(0, mid).join(''), o) <= maxWidth)
|
|
158
221
|
lo = mid;
|
|
159
222
|
else
|
|
160
223
|
hi = mid - 1;
|
|
161
224
|
}
|
|
162
|
-
const
|
|
163
|
-
|
|
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;
|
|
164
243
|
}
|
|
165
244
|
/**
|
|
166
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
|
@@ -68,20 +68,75 @@ export interface TextOpts {
|
|
|
68
68
|
capRatio?: number;
|
|
69
69
|
}
|
|
70
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
|
+
|
|
71
81
|
interface Glyph {
|
|
72
82
|
ch: string;
|
|
73
83
|
size: number;
|
|
74
84
|
width: number;
|
|
75
85
|
}
|
|
76
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
|
+
|
|
77
116
|
function glyphs(ctx: SKRSContext2D, text: string, o: TextOpts): Glyph[] {
|
|
78
117
|
const family = o.family ?? defaultFamily;
|
|
79
|
-
|
|
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;
|
|
80
121
|
const capSize = o.size * (o.capRatio ?? 0.76);
|
|
81
122
|
const out: Glyph[] = [];
|
|
82
123
|
const font = `${o.size}px "${family}"`;
|
|
83
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}"`;
|
|
84
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
|
+
}
|
|
85
140
|
// In small caps, a lower-case letter is drawn upper-case at a smaller size.
|
|
86
141
|
const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
|
|
87
142
|
const size = isLower ? capSize : o.size;
|
|
@@ -99,7 +154,7 @@ function advance(gs: Glyph[], tracking: number): number {
|
|
|
99
154
|
|
|
100
155
|
/** Width the text would take with this tracking, without drawing it. */
|
|
101
156
|
export function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): number {
|
|
102
|
-
return advance(glyphs(ctx, text, o), o
|
|
157
|
+
return advance(glyphs(ctx, text, o), trackingOf(text, o));
|
|
103
158
|
}
|
|
104
159
|
|
|
105
160
|
/**
|
|
@@ -108,7 +163,7 @@ export function measureTracked(ctx: SKRSContext2D, text: string, o: TextOpts): n
|
|
|
108
163
|
*/
|
|
109
164
|
export function drawTracked(ctx: SKRSContext2D, text: string, x: number, y: number, o: TextOpts): number {
|
|
110
165
|
const family = o.family ?? defaultFamily;
|
|
111
|
-
const tracking = o
|
|
166
|
+
const tracking = trackingOf(text, o);
|
|
112
167
|
const gs = glyphs(ctx, text, o);
|
|
113
168
|
const total = advance(gs, tracking);
|
|
114
169
|
let cursor = o.align === 'right' ? x - total : o.align === 'center' ? x - total / 2 : x;
|
|
@@ -171,25 +226,53 @@ export function drawClipped(
|
|
|
171
226
|
return drawTracked(ctx, truncate(ctx, text, maxWidth, o), x, y, o);
|
|
172
227
|
}
|
|
173
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
|
+
|
|
174
235
|
/**
|
|
175
|
-
* Longest prefix of `text` that fits in `maxWidth`,
|
|
176
|
-
*
|
|
177
|
-
*
|
|
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.
|
|
178
244
|
*
|
|
179
245
|
* The search is binary because measuring is expensive: `measureTracked` walks
|
|
180
|
-
* 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.
|
|
181
248
|
*/
|
|
182
249
|
function fitPrefix(ctx: SKRSContext2D, text: string, maxWidth: number, o: TextOpts): number {
|
|
183
250
|
if (measureTracked(ctx, text, o) <= maxWidth) return text.length;
|
|
251
|
+
const chars = [...text];
|
|
184
252
|
let lo = 1;
|
|
185
|
-
let hi =
|
|
253
|
+
let hi = chars.length;
|
|
186
254
|
while (lo < hi) {
|
|
187
255
|
const mid = Math.ceil((lo + hi) / 2);
|
|
188
|
-
if (measureTracked(ctx,
|
|
256
|
+
if (measureTracked(ctx, chars.slice(0, mid).join(''), o) <= maxWidth) lo = mid;
|
|
189
257
|
else hi = mid - 1;
|
|
190
258
|
}
|
|
191
|
-
const
|
|
192
|
-
|
|
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;
|
|
193
276
|
}
|
|
194
277
|
|
|
195
278
|
/**
|