@loksly/lcd-canvas-kit 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/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;
package/dist/text.js CHANGED
@@ -15,20 +15,54 @@ 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
+ }
20
53
  function glyphs(ctx, text, o) {
21
54
  const family = o.family ?? defaultFamily;
22
55
  const small = o.smallCaps === true;
23
56
  const capSize = o.size * (o.capRatio ?? 0.76);
24
57
  const out = [];
58
+ const font = `${o.size}px "${family}"`;
59
+ const capFont = `${capSize}px "${family}"`;
25
60
  for (const ch of text) {
26
61
  // In small caps, a lower-case letter is drawn upper-case at a smaller size.
27
62
  const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
28
63
  const size = isLower ? capSize : o.size;
29
64
  const draw = small ? ch.toUpperCase() : ch;
30
- ctx.font = `${size}px "${family}"`;
31
- out.push({ ch: draw, size, width: ctx.measureText(draw).width });
65
+ out.push({ ch: draw, size, width: widthOf(ctx, isLower ? capFont : font, draw) });
32
66
  }
33
67
  return out;
34
68
  }
@@ -55,8 +89,12 @@ export function drawTracked(ctx, text, x, y, o) {
55
89
  if (o.color)
56
90
  ctx.fillStyle = o.color;
57
91
  ctx.textBaseline = 'alphabetic';
92
+ let size = -1;
58
93
  for (const g of gs) {
59
- ctx.font = `${g.size}px "${family}"`;
94
+ if (g.size !== size) {
95
+ ctx.font = `${g.size}px "${family}"`;
96
+ size = g.size;
97
+ }
60
98
  ctx.fillText(g.ch, cursor, y);
61
99
  cursor += g.width + tracking;
62
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loksly/lcd-canvas-kit",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
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.3"
45
+ "@loksly/weather-astro": "0.2.5"
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;
@@ -45,13 +79,14 @@ function glyphs(ctx: SKRSContext2D, text: string, o: TextOpts): Glyph[] {
45
79
  const small = o.smallCaps === true;
46
80
  const capSize = o.size * (o.capRatio ?? 0.76);
47
81
  const out: Glyph[] = [];
82
+ const font = `${o.size}px "${family}"`;
83
+ const capFont = `${capSize}px "${family}"`;
48
84
  for (const ch of text) {
49
85
  // In small caps, a lower-case letter is drawn upper-case at a smaller size.
50
86
  const isLower = small && ch !== ch.toUpperCase() && ch === ch.toLowerCase();
51
87
  const size = isLower ? capSize : o.size;
52
88
  const draw = small ? ch.toUpperCase() : ch;
53
- ctx.font = `${size}px "${family}"`;
54
- out.push({ ch: draw, size, width: ctx.measureText(draw).width });
89
+ out.push({ ch: draw, size, width: widthOf(ctx, isLower ? capFont : font, draw) });
55
90
  }
56
91
  return out;
57
92
  }
@@ -79,8 +114,12 @@ export function drawTracked(ctx: SKRSContext2D, text: string, x: number, y: numb
79
114
  let cursor = o.align === 'right' ? x - total : o.align === 'center' ? x - total / 2 : x;
80
115
  if (o.color) ctx.fillStyle = o.color;
81
116
  ctx.textBaseline = 'alphabetic';
117
+ let size = -1;
82
118
  for (const g of gs) {
83
- ctx.font = `${g.size}px "${family}"`;
119
+ if (g.size !== size) {
120
+ ctx.font = `${g.size}px "${family}"`;
121
+ size = g.size;
122
+ }
84
123
  ctx.fillText(g.ch, cursor, y);
85
124
  cursor += g.width + tracking;
86
125
  }