kiosko 0.2.5 → 0.2.7

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/kit.js CHANGED
@@ -30,14 +30,50 @@ function widthOf(ctx, font, ch) {
30
30
  function clearTextCache() {
31
31
  widths.clear();
32
32
  }
33
+ var CJK = /[\u1100-\u11ff\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef]/;
34
+ function hasCjk(text) {
35
+ return CJK.test(text);
36
+ }
37
+ function trackingOf(text, o) {
38
+ return o.tracking && hasCjk(text) ? 0 : o.tracking ?? 0;
39
+ }
40
+ var minCjkPx = 0;
41
+ var enlarged = Infinity;
42
+ function setMinCjkPx(px) {
43
+ if (px !== minCjkPx) widths.clear();
44
+ minCjkPx = Math.max(0, px);
45
+ }
46
+ function takeCjkEnlarged() {
47
+ const v = enlarged;
48
+ enlarged = Infinity;
49
+ return Number.isFinite(v) ? Math.round(v) : null;
50
+ }
51
+ function scaleOf(ctx) {
52
+ const t = ctx.getTransform?.();
53
+ return t ? Math.hypot(t.a, t.b) || 1 : 1;
54
+ }
33
55
  function glyphs(ctx, text, o) {
34
56
  const family = o.family ?? defaultFamily;
35
- const small = o.smallCaps === true;
57
+ const cjk = hasCjk(text);
58
+ const small = o.smallCaps === true && !cjk;
36
59
  const capSize = o.size * (o.capRatio ?? 0.76);
37
60
  const out = [];
38
61
  const font = `${o.size}px "${family}"`;
39
62
  const capFont = `${capSize}px "${family}"`;
63
+ let cjkSize = o.size;
64
+ if (cjk && minCjkPx > 0) {
65
+ const scale = scaleOf(ctx);
66
+ if (o.size * scale < minCjkPx - 0.5) {
67
+ cjkSize = minCjkPx / scale;
68
+ enlarged = Math.min(enlarged, o.size * scale);
69
+ }
70
+ }
71
+ const cjkFont = `${cjkSize}px "${family}"`;
40
72
  for (const ch of text) {
73
+ if (cjkSize !== o.size && CJK.test(ch)) {
74
+ out.push({ ch, size: cjkSize, width: widthOf(ctx, cjkFont, ch) });
75
+ continue;
76
+ }
41
77
  const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
42
78
  const size = isLower ? capSize : o.size;
43
79
  const draw = small ? ch.toUpperCase() : ch;
@@ -50,11 +86,11 @@ function advance(gs, tracking) {
50
86
  return gs.reduce((a, g) => a + g.width, 0) + tracking * (gs.length - 1);
51
87
  }
52
88
  function measureTracked(ctx, text, o) {
53
- return advance(glyphs(ctx, text, o), o.tracking ?? 0);
89
+ return advance(glyphs(ctx, text, o), trackingOf(text, o));
54
90
  }
55
91
  function drawTracked(ctx, text, x, y, o) {
56
92
  const family = o.family ?? defaultFamily;
57
- const tracking = o.tracking ?? 0;
93
+ const tracking = trackingOf(text, o);
58
94
  const gs = glyphs(ctx, text, o);
59
95
  const total = advance(gs, tracking);
60
96
  let cursor = o.align === "right" ? x - total : o.align === "center" ? x - total / 2 : x;
@@ -92,17 +128,35 @@ function fitSize(ctx, text, maxWidth, o, minSize) {
92
128
  function drawClipped(ctx, text, x, y, maxWidth, o) {
93
129
  return drawTracked(ctx, truncate(ctx, text, maxWidth, o), x, y, o);
94
130
  }
131
+ var NO_START = /* @__PURE__ */ new Set([..."\u3001\u3002\uFF0C\uFF0E\uFF1A\uFF1B\uFF1F\uFF01\uFF09\u300D\u300F\u3011\u300B\u3009\u3015\uFF5D\u3017\u3019\u301B\u2019\u201D\u2026\u2025\u30FC\u3005\u309D\u309E\u30FD\u30FE\u3041\u3043\u3045\u3047\u3049\u3063\u3083\u3085\u3087\u308E\u30A1\u30A3\u30A5\u30A7\u30A9\u30C3\u30E3\u30E5\u30E7\u30EE\u30F5\u30F6,.;:?!)]}%"]);
132
+ var NO_END = /* @__PURE__ */ new Set([..."\uFF08\u300C\u300E\u3010\u300A\u3008\u3014\uFF5B\u3016\u3018\u301A\u2018\u201C([{$"]);
133
+ var isSpace = (c) => /\s/.test(c);
95
134
  function fitPrefix(ctx, text, maxWidth, o) {
96
135
  if (measureTracked(ctx, text, o) <= maxWidth) return text.length;
136
+ const chars = [...text];
97
137
  let lo = 1;
98
- let hi = text.length;
138
+ let hi = chars.length;
99
139
  while (lo < hi) {
100
140
  const mid = Math.ceil((lo + hi) / 2);
101
- if (measureTracked(ctx, text.slice(0, mid), o) <= maxWidth) lo = mid;
141
+ if (measureTracked(ctx, chars.slice(0, mid).join(""), o) <= maxWidth) lo = mid;
102
142
  else hi = mid - 1;
103
143
  }
104
- const space = text.lastIndexOf(" ", lo);
105
- return space > 0 ? space : lo;
144
+ const cjk = hasCjk(text);
145
+ const fits = (i) => !NO_START.has(chars[i]) && !NO_END.has(chars[i - 1]);
146
+ let cut = 0;
147
+ for (let i = lo; i >= 1; i--) {
148
+ const before = chars[i - 1];
149
+ const after = chars[i];
150
+ if ((isSpace(before) || isSpace(after) || CJK.test(before) || CJK.test(after)) && fits(i)) {
151
+ cut = i;
152
+ break;
153
+ }
154
+ }
155
+ if (!cut || cjk && cut < lo * 0.6) {
156
+ cut = lo;
157
+ while (cut > 1 && !fits(cut)) cut--;
158
+ }
159
+ return chars.slice(0, cut).join("").length;
106
160
  }
107
161
  function wrapLines(ctx, text, maxWidth, maxLines, o) {
108
162
  const clean = text.replace(/\s+/g, " ").trim();
@@ -407,6 +461,7 @@ export {
407
461
  drawWeatherIcon,
408
462
  ellipse,
409
463
  fitSize,
464
+ hasCjk,
410
465
  hashString,
411
466
  hex,
412
467
  linear,
@@ -417,8 +472,10 @@ export {
417
472
  registerFontFiles,
418
473
  rng,
419
474
  setDefaultFamily,
475
+ setMinCjkPx,
420
476
  smooth,
421
477
  sparkBars,
478
+ takeCjkEnlarged,
422
479
  truncate,
423
480
  wrapLines
424
481
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kiosko",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "A widget mosaic for small always-on USB LCD panels (Thermalright Trofeo Vision and friends), with a visual editor and plugins.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -14,6 +14,7 @@
14
14
  "*.js",
15
15
  "*.LEGAL.txt",
16
16
  "web",
17
+ "assets",
17
18
  "README.md",
18
19
  "LICENSE",
19
20
  "examples"