kiosko 0.2.3 → 0.2.5

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
@@ -7,19 +7,41 @@ function setDefaultFamily(family) {
7
7
  defaultFamily = family;
8
8
  }
9
9
  function registerFontFiles(fonts) {
10
+ widths.clear();
10
11
  return Object.entries(fonts).filter(([family, path]) => !GlobalFonts.registerFromPath(path, family)).map(([, path]) => path);
11
12
  }
13
+ var widths = /* @__PURE__ */ new Map();
14
+ var MAX_FONTS = 400;
15
+ function widthOf(ctx, font, ch) {
16
+ let byChar = widths.get(font);
17
+ if (!byChar) {
18
+ if (widths.size >= MAX_FONTS) widths.clear();
19
+ byChar = /* @__PURE__ */ new Map();
20
+ widths.set(font, byChar);
21
+ }
22
+ let w = byChar.get(ch);
23
+ if (w === void 0) {
24
+ if (ctx.font !== font) ctx.font = font;
25
+ w = ctx.measureText(ch).width;
26
+ byChar.set(ch, w);
27
+ }
28
+ return w;
29
+ }
30
+ function clearTextCache() {
31
+ widths.clear();
32
+ }
12
33
  function glyphs(ctx, text, o) {
13
34
  const family = o.family ?? defaultFamily;
14
35
  const small = o.smallCaps === true;
15
36
  const capSize = o.size * (o.capRatio ?? 0.76);
16
37
  const out = [];
38
+ const font = `${o.size}px "${family}"`;
39
+ const capFont = `${capSize}px "${family}"`;
17
40
  for (const ch of text) {
18
41
  const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
19
42
  const size = isLower ? capSize : o.size;
20
43
  const draw = small ? ch.toUpperCase() : ch;
21
- ctx.font = `${size}px "${family}"`;
22
- out.push({ ch: draw, size, width: ctx.measureText(draw).width });
44
+ out.push({ ch: draw, size, width: widthOf(ctx, isLower ? capFont : font, draw) });
23
45
  }
24
46
  return out;
25
47
  }
@@ -38,8 +60,12 @@ function drawTracked(ctx, text, x, y, o) {
38
60
  let cursor = o.align === "right" ? x - total : o.align === "center" ? x - total / 2 : x;
39
61
  if (o.color) ctx.fillStyle = o.color;
40
62
  ctx.textBaseline = "alphabetic";
63
+ let size = -1;
41
64
  for (const g of gs) {
42
- ctx.font = `${g.size}px "${family}"`;
65
+ if (g.size !== size) {
66
+ ctx.font = `${g.size}px "${family}"`;
67
+ size = g.size;
68
+ }
43
69
  ctx.fillText(g.ch, cursor, y);
44
70
  cursor += g.width + tracking;
45
71
  }
@@ -373,6 +399,7 @@ export {
373
399
  TAU,
374
400
  bezierPts,
375
401
  clamp,
402
+ clearTextCache,
376
403
  css,
377
404
  drawClipped,
378
405
  drawMoonPhase,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kiosko",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
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",
package/sdk.js CHANGED
@@ -12,7 +12,7 @@ var CATEGORIES = [
12
12
  { id: "scene", label: { en: "Scenes", es: "Escenas" } },
13
13
  { id: "fun", label: { en: "Fun", es: "Diversi\xF3n" } }
14
14
  ];
15
- var SDK_VERSION = "0.2.3";
15
+ var SDK_VERSION = "0.2.5";
16
16
  function defineWidget(w) {
17
17
  return w;
18
18
  }