@thi.ng/text-canvas 3.0.22 → 3.0.24

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-04-23T07:02:18Z
3
+ - **Last updated**: 2024-06-21T19:34:38Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [3.0.24](https://github.com/thi-ng/umbrella/tree/@thi.ng/text-canvas@3.0.24) (2024-06-21)
13
+
14
+ #### ♻️ Refactoring
15
+
16
+ - enforce uniform naming convention of internal functions ([56992b2](https://github.com/thi-ng/umbrella/commit/56992b2))
17
+
12
18
  ### [3.0.3](https://github.com/thi-ng/umbrella/tree/@thi.ng/text-canvas@3.0.3) (2024-02-22)
13
19
 
14
20
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 192 standalone projects, maintained as part
10
+ > This is one of 193 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -81,7 +81,7 @@ For Node.js REPL:
81
81
  const tc = await import("@thi.ng/text-canvas");
82
82
  ```
83
83
 
84
- Package sizes (brotli'd, pre-treeshake): ESM: 6.24 KB
84
+ Package sizes (brotli'd, pre-treeshake): ESM: 6.23 KB
85
85
 
86
86
  ## Dependencies
87
87
 
package/bars.js CHANGED
@@ -32,9 +32,9 @@ const barChartHLines = (width, vals, min, max) => {
32
32
  return [...map((x) => barHorizontal(width, x, min, max), $vals)];
33
33
  };
34
34
  const barChartHStr = (width, vals, min, max) => barChartHLines(width, vals, min, max).join("\n");
35
- const barHorizontal = (width, x, min = 0, max = 1) => bar(BARS_H, width, false, x, min, max, "");
36
- const barVertical = (height, x, min = 0, max = 1, delim = "\n") => bar(BARS_V, height, true, x, min, max, delim);
37
- const bar = (chars, size, left, x, min, max, delim) => {
35
+ const barHorizontal = (width, x, min = 0, max = 1) => __bar(BARS_H, width, false, x, min, max, "");
36
+ const barVertical = (height, x, min = 0, max = 1, delim = "\n") => __bar(BARS_V, height, true, x, min, max, delim);
37
+ const __bar = (chars, size, left, x, min, max, delim) => {
38
38
  x = fitClamped(x, min, max, 0, size);
39
39
  const f = fract(x) * 9 | 0;
40
40
  const full = repeat(chars[8] + delim, x | 0);
package/canvas.js CHANGED
@@ -5,8 +5,7 @@ var __decorateClass = (decorators, target, key, kind) => {
5
5
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
6
  if (decorator = decorators[i])
7
7
  result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
- if (kind && result)
9
- __defProp(target, key, result);
8
+ if (kind && result) __defProp(target, key, result);
10
9
  return result;
11
10
  };
12
11
  import {
@@ -139,8 +138,8 @@ const beginClip = (canvas2, x, y, w, h) => {
139
138
  )
140
139
  );
141
140
  };
142
- const pop = (stack) => stack.length > 1 && stack.pop();
143
- const endClip = (canvas2) => pop(canvas2.clipRects);
141
+ const __pop = (stack) => stack.length > 1 && stack.pop();
142
+ const endClip = (canvas2) => __pop(canvas2.clipRects);
144
143
  const withClip = (canvas2, x, y, w, h, fn) => {
145
144
  beginClip(canvas2, x, y, w, h);
146
145
  fn();
@@ -149,7 +148,7 @@ const withClip = (canvas2, x, y, w, h, fn) => {
149
148
  const beginStyle = (canvas2, style) => {
150
149
  canvas2.styles.push(style);
151
150
  };
152
- const endStyle = (canvas2) => pop(canvas2.styles);
151
+ const endStyle = (canvas2) => __pop(canvas2.styles);
153
152
  const withStyle = (canvas2, style, fn) => {
154
153
  canvas2.styles.push(style);
155
154
  fn();
@@ -171,8 +170,7 @@ const setAt = (canvas2, x, y, code, format = canvas2.format) => {
171
170
  x |= 0;
172
171
  y |= 0;
173
172
  const { x1, y1, x2, y2 } = peek(canvas2.clipRects);
174
- if (x < x1 || y < y1 || x >= x2 || y >= y2)
175
- return;
173
+ if (x < x1 || y < y1 || x >= x2 || y >= y2) return;
176
174
  canvas2.data[x + y * canvas2.width] = charCode(code, format);
177
175
  };
178
176
  export {
package/circle.js CHANGED
@@ -2,14 +2,12 @@ import { peek } from "@thi.ng/arrays/peek";
2
2
  import { hline } from "./hvline.js";
3
3
  import { charCode, intersectRectCircle } from "./utils.js";
4
4
  const circle = (canvas, cx, cy, r, char, fill = false, format = canvas.format) => {
5
- if (r < 1)
6
- return;
5
+ if (r < 1) return;
7
6
  cx |= 0;
8
7
  cy |= 0;
9
8
  r |= 0;
10
9
  const { x1, y1, x2, y2, w: clipw, h: cliph } = peek(canvas.clipRects);
11
- if (!intersectRectCircle(x1, y1, clipw, cliph, cx, cy, r))
12
- return;
10
+ if (!intersectRectCircle(x1, y1, clipw, cliph, cx, cy, r)) return;
13
11
  char = charCode(
14
12
  char !== void 0 ? char : peek(canvas.styles).dot,
15
13
  format
package/hvline.js CHANGED
@@ -2,8 +2,7 @@ import { peek } from "@thi.ng/arrays/peek";
2
2
  import { charCode } from "./utils.js";
3
3
  const hline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
4
4
  const { x1, y1, x2, y2 } = peek(canvas.clipRects);
5
- if (len < 1 || y < y1 || y >= y2 || x >= x2)
6
- return;
5
+ if (len < 1 || y < y1 || y >= y2 || x >= x2) return;
7
6
  _hvline(
8
7
  canvas.data,
9
8
  x,
@@ -22,8 +21,7 @@ const hline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
22
21
  };
23
22
  const vline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
24
23
  const { x1, x2, y1, y2 } = peek(canvas.clipRects);
25
- if (len < 1 || x < x1 || x >= x2 || y >= y2)
26
- return;
24
+ if (len < 1 || x < x1 || x >= x2 || y >= y2) return;
27
25
  _hvline(
28
26
  canvas.data,
29
27
  y,
package/image.d.ts CHANGED
@@ -16,7 +16,7 @@ export declare const blit: (dest: Canvas, src: Canvas, x?: number, y?: number) =
16
16
  * pixels/chars will be copied. Supports region clipping.
17
17
  *
18
18
  * @example
19
- * ```ts
19
+ * ```ts tangle:../export/blit-mask.ts
20
20
  * import {
21
21
  * blitMask, canvas, canvasFromText, clear, formatCanvas
22
22
  * } from "@thi.ng/text-canvas";
@@ -30,17 +30,19 @@ export declare const blit: (dest: Canvas, src: Canvas, x?: number, y?: number) =
30
30
  * "###==###",
31
31
  * ]);
32
32
  *
33
+ * console.log(formatCanvas(a));
34
+ *
33
35
  * // destination canvas (filled w/ "-")
34
36
  * const b = canvas(12,7);
35
37
  * clear(b, true, "-");
36
38
  *
37
39
  * // paste `a` several times into `b` using "#" as mask
38
- * blitMask(b, -4, -2, a, "#"); // top-left (partially outside)
39
- * blitMask(b, 2, 1, a, "#"); // center
40
- * blitMask(b, 8, 4, a, "#"); // bottom-right (part outside)
40
+ * blitMask(b, a, -4, -2, "#"); // top-left (partially outside)
41
+ * blitMask(b, a, 2, 1, "#"); // center
42
+ * blitMask(b, a, 8, 4, "#"); // bottom-right (part outside)
41
43
  *
42
44
  * // show result
43
- * console.log(formatCanvas(b))
45
+ * console.log(formatCanvas(b));
44
46
  * // ===---------
45
47
  * // ==---==-----
46
48
  * // =---====----
package/image.js CHANGED
@@ -41,16 +41,14 @@ const __initBlit = (dest, x, y, src) => {
41
41
  { x1: x, y1: y, x2: x + sw, y2: y + sh, w: sw, h: sh },
42
42
  peek(dest.clipRects)
43
43
  );
44
- if (!iw || !ih)
45
- return;
44
+ if (!iw || !ih) return;
46
45
  const sx = clamp0(x1 - x);
47
46
  const sy = clamp0(y1 - y);
48
47
  return { sbuf, dbuf, sw, dw, x, y, x1, y1, y2, iw, ih, sx, sy };
49
48
  };
50
49
  const blit = (dest, src, x = 0, y = 0) => {
51
50
  const state = __initBlit(dest, x, y, src);
52
- if (!state)
53
- return;
51
+ if (!state) return;
54
52
  const { sbuf, dbuf, x1, y1, y2, sx, sy, iw, sw, dw } = state;
55
53
  for (let yy = sy, dy = y1; dy < y2; yy++, dy++) {
56
54
  let sidx = sx + yy * sw;
@@ -60,8 +58,7 @@ const blit = (dest, src, x = 0, y = 0) => {
60
58
  };
61
59
  const blitMask = (dest, src, x = 0, y = 0, mask = 32) => {
62
60
  const state = __initBlit(dest, x, y, src);
63
- if (!state)
64
- return;
61
+ if (!state) return;
65
62
  const { sbuf, dbuf, x1, y1, y2, sx, sy, iw, sw, dw } = state;
66
63
  mask = charCode(mask, 0);
67
64
  for (let yy = sy, dy = y1; dy < y2; yy++, dy++) {
@@ -72,8 +69,7 @@ const blitMask = (dest, src, x = 0, y = 0, mask = 32) => {
72
69
  };
73
70
  const blitBarsV = (dest, src, x = 0, y = 0, blend = blendBarsVAdd) => {
74
71
  const state = __initBlit(dest, x, y, src);
75
- if (!state)
76
- return;
72
+ if (!state) return;
77
73
  const { sbuf, dbuf, x1, y1, y2, sx, sy, iw, sw, dw } = state;
78
74
  for (let yy = sy, dy = y1; dy < y2; yy++, dy++) {
79
75
  let sidx = sx + yy * sw;
@@ -251,8 +247,7 @@ const BLEND_ADD = {
251
247
  }
252
248
  };
253
249
  const resize = (canvas2, newWidth, newHeight) => {
254
- if (canvas2.width === newWidth && canvas2.height === newHeight)
255
- return;
250
+ if (canvas2.width === newWidth && canvas2.height === newHeight) return;
256
251
  const dest = new Canvas(newWidth, newHeight);
257
252
  dest.data.fill(charCode(32, canvas2.format));
258
253
  blit(dest, canvas2);
@@ -302,9 +297,8 @@ const image = (canvas2, x, y, w, h, pixels, opts = {}) => {
302
297
  sy,
303
298
  w: iw,
304
299
  h: ih
305
- } = imgRect(canvas2, x, y, w, h);
306
- if (!iw || !ih)
307
- return;
300
+ } = __imgRect(canvas2, x, y, w, h);
301
+ if (!iw || !ih) return;
308
302
  const {
309
303
  chars = SHADES_BLOCK,
310
304
  format = canvas2.format,
@@ -341,9 +335,8 @@ const imageRaw = (canvas2, x, y, w, h, pixels, char = "\u2588") => {
341
335
  sy,
342
336
  w: iw,
343
337
  h: ih
344
- } = imgRect(canvas2, x, y, w, h);
345
- if (!iw || !ih)
346
- return;
338
+ } = __imgRect(canvas2, x, y, w, h);
339
+ if (!iw || !ih) return;
347
340
  const code = char.charCodeAt(0);
348
341
  for (let yy = sy, dy = y1; dy < y2; yy++, dy++) {
349
342
  let sidx = sx + yy * w;
@@ -368,9 +361,8 @@ const imageRawFmtOnly = (canvas2, x, y, w, h, pixels) => {
368
361
  sy,
369
362
  w: iw,
370
363
  h: ih
371
- } = imgRect(canvas2, x, y, w, h);
372
- if (!iw || !ih)
373
- return;
364
+ } = __imgRect(canvas2, x, y, w, h);
365
+ if (!iw || !ih) return;
374
366
  for (let yy = sy, dy = y1; dy < y2; yy++, dy++) {
375
367
  let sidx = sx + yy * w;
376
368
  let didx = x1 + dy * width;
@@ -396,9 +388,8 @@ const imageBraille = (canvas2, x, y, w, h, pixels, thresh, format) => {
396
388
  sy,
397
389
  w: iw,
398
390
  h: ih
399
- } = imgRect(canvas2, x, y, w >> 1, h >> 2);
400
- if (!iw || !ih)
401
- return;
391
+ } = __imgRect(canvas2, x, y, w >> 1, h >> 2);
392
+ if (!iw || !ih) return;
402
393
  const w2 = w * 2;
403
394
  const w3 = w * 3;
404
395
  const braille = (i) => (pixels[i] >= thresh ? 1 : 0) | (pixels[i + w] >= thresh ? 2 : 0) | (pixels[i + w2] >= thresh ? 4 : 0) | (pixels[i + w3] >= thresh ? 8 : 0) | (pixels[i + 1] >= thresh ? 16 : 0) | (pixels[i + w + 1] >= thresh ? 32 : 0) | (pixels[i + w2 + 1] >= thresh ? 64 : 0) | (pixels[i + w3 + 1] >= thresh ? 128 : 0) | 10240;
@@ -422,7 +413,7 @@ const imageCanvas565 = (src, char) => {
422
413
  return dest;
423
414
  };
424
415
  const imageString565 = (src, char, fmt = FMT_ANSI565) => formatCanvas(imageCanvas565(src, char), fmt);
425
- const imgRect = (canvas2, x, y, w, h) => {
416
+ const __imgRect = (canvas2, x, y, w, h) => {
426
417
  const rect = intersectRect(
427
418
  { x1: x, y1: y, x2: x + w, y2: y + h, w, h },
428
419
  peek(canvas2.clipRects)
package/line.js CHANGED
@@ -5,8 +5,7 @@ import { charCode } from "./utils.js";
5
5
  const line = (canvas, ax, ay, bx, by, char, format = canvas.format) => {
6
6
  const { x1, y1, x2, y2 } = peek(canvas.clipRects);
7
7
  const clipped = liangBarsky2Raw(ax, ay, bx, by, x1, y1, x2, y2);
8
- if (!clipped)
9
- return;
8
+ if (!clipped) return;
10
9
  ax = clipped[0] | 0;
11
10
  ay = clipped[1] | 0;
12
11
  bx = clipped[2] | 0;
@@ -23,8 +22,7 @@ const line = (canvas, ax, ay, bx, by, char, format = canvas.format) => {
23
22
  );
24
23
  while (true) {
25
24
  canvas.data[ax + ay * w] = char;
26
- if (ax === bx && ay === by)
27
- return;
25
+ if (ax === bx && ay === by) return;
28
26
  let t = err << 1;
29
27
  if (t < dx) {
30
28
  err += dx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/text-canvas",
3
- "version": "3.0.22",
3
+ "version": "3.0.24",
4
4
  "description": "Text based canvas, drawing, plotting, tables with arbitrary formatting (incl. ANSI/HTML)",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -10,7 +10,7 @@
10
10
  "type": "git",
11
11
  "url": "https://github.com/thi-ng/umbrella.git"
12
12
  },
13
- "homepage": "https://github.com/thi-ng/umbrella/tree/develop/packages/text-canvas#readme",
13
+ "homepage": "https://thi.ng/text-canvas",
14
14
  "funding": [
15
15
  {
16
16
  "type": "github",
@@ -36,22 +36,22 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.11.1",
40
- "@thi.ng/arrays": "^2.9.5",
41
- "@thi.ng/checks": "^3.6.3",
42
- "@thi.ng/errors": "^2.5.6",
43
- "@thi.ng/geom-clip-line": "^2.3.88",
44
- "@thi.ng/math": "^5.10.12",
45
- "@thi.ng/strings": "^3.7.32",
46
- "@thi.ng/text-format": "^2.2.4",
47
- "@thi.ng/transducers": "^9.0.4"
39
+ "@thi.ng/api": "^8.11.3",
40
+ "@thi.ng/arrays": "^2.9.7",
41
+ "@thi.ng/checks": "^3.6.5",
42
+ "@thi.ng/errors": "^2.5.8",
43
+ "@thi.ng/geom-clip-line": "^2.3.90",
44
+ "@thi.ng/math": "^5.11.0",
45
+ "@thi.ng/strings": "^3.7.34",
46
+ "@thi.ng/text-format": "^2.2.6",
47
+ "@thi.ng/transducers": "^9.0.6"
48
48
  },
49
49
  "devDependencies": {
50
- "@microsoft/api-extractor": "^7.43.0",
51
- "esbuild": "^0.20.2",
52
- "tslib": "^2.6.2",
53
- "typedoc": "^0.25.12",
54
- "typescript": "^5.4.3"
50
+ "@microsoft/api-extractor": "^7.47.0",
51
+ "esbuild": "^0.21.5",
52
+ "tslib": "^2.6.3",
53
+ "typedoc": "^0.25.13",
54
+ "typescript": "^5.5.2"
55
55
  },
56
56
  "keywords": [
57
57
  "4bit",
@@ -147,5 +147,5 @@
147
147
  ],
148
148
  "year": 2020
149
149
  },
150
- "gitHead": "aed3421c21044c005fbcb7cc37965ccf85a870d2\n"
150
+ "gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n"
151
151
  }
package/plot.js CHANGED
@@ -34,18 +34,15 @@ const plotLineChart = (canvas, x, y, height, vals, min, max, format = canvas.for
34
34
  const { x1, x2 } = peek(canvas.clipRects);
35
35
  for (let i = 0, n = $vals.length - 1; i < n; i++) {
36
36
  const xx = x + i;
37
- if (xx < x1)
38
- continue;
39
- if (xx > x2)
40
- break;
37
+ if (xx < x1) continue;
38
+ if (xx > x2) break;
41
39
  const ya = Math.round(fitClamped($vals[i], min, max, height, 0)) + y;
42
40
  const yb = Math.round(fitClamped($vals[i + 1], min, max, height, 0)) + y;
43
41
  if (ya === yb) {
44
42
  canvas.setAt(xx, ya, 9472 | format);
45
43
  } else {
46
44
  let [y1, y2] = minMax(ya, yb);
47
- while (++y1 < y2)
48
- canvas.setAt(xx, y1, 9474 | format);
45
+ while (++y1 < y2) canvas.setAt(xx, y1, 9474 | format);
49
46
  canvas.setAt(xx, ya, (ya < yb ? 9582 : 9583) | format);
50
47
  canvas.setAt(xx, yb, (ya < yb ? 9584 : 9581) | format);
51
48
  }
package/rect.js CHANGED
@@ -18,8 +18,7 @@ const clear = (canvas, reset = false, code = 32) => {
18
18
  };
19
19
  const clearFormat = ({ data }, format = NONE) => {
20
20
  format <<= 16;
21
- for (let i = data.length; i-- > 0; )
22
- data[i] = data[i] & 65535 | format;
21
+ for (let i = data.length; i-- > 0; ) data[i] = data[i] & 65535 | format;
23
22
  };
24
23
  const fillRect = (canvas, x, y, w, h, char, format = canvas.format) => {
25
24
  x |= 0;
@@ -36,8 +35,7 @@ const fillRect = (canvas, x, y, w, h, char, format = canvas.format) => {
36
35
  y = y1;
37
36
  }
38
37
  const { data, width } = canvas;
39
- if (w < 1 || h < 1 || x >= x2 || y >= y2)
40
- return;
38
+ if (w < 1 || h < 1 || x >= x2 || y >= y2) return;
41
39
  w = Math.min(w, x2 - x);
42
40
  h = Math.min(h, y2 - y);
43
41
  char = charCode(char, format);
@@ -49,8 +47,7 @@ const fillRect = (canvas, x, y, w, h, char, format = canvas.format) => {
49
47
  const strokeRect = (canvas, x, y, w, h, format = canvas.format) => {
50
48
  w |= 0;
51
49
  h |= 0;
52
- if (w < 2 || h < 2)
53
- return;
50
+ if (w < 2 || h < 2) return;
54
51
  const style = peek(canvas.styles);
55
52
  hline(canvas, x, y, w, style.tl, style.tr, style.hl, format);
56
53
  hline(canvas, x, y + h - 1, w, style.bl, style.br, style.hl, format);
package/text.js CHANGED
@@ -12,8 +12,7 @@ const textLine = (canvas, x, y, line, format = canvas.format) => {
12
12
  x |= 0;
13
13
  y |= 0;
14
14
  const { x1, y1, x2, y2 } = peek(canvas.clipRects);
15
- if (y < y1 || y >= y2 || x >= x2)
16
- return;
15
+ if (y < y1 || y >= y2 || x >= x2) return;
17
16
  let i = 0;
18
17
  if (x < x1) {
19
18
  i = x1 - x;
@@ -41,8 +40,7 @@ const textColumn = (canvas, x, y, width, txt, format = canvas.format, hard = fal
41
40
  for (let line of wordWrapLines(txt, { width, hard })) {
42
41
  textLine(canvas, x, y, line.toString(), format);
43
42
  y++;
44
- if (y >= height)
45
- break;
43
+ if (y >= height) break;
46
44
  }
47
45
  return y;
48
46
  };
package/utils.js CHANGED
@@ -6,8 +6,8 @@ const intersectRect = (a, b) => {
6
6
  const y2 = Math.min(a.y2, b.y2);
7
7
  return { x1, y1, x2, y2, w: Math.max(x2 - x1, 0), h: Math.max(y2 - y1, 0) };
8
8
  };
9
- const axis = (a, b, c) => (a < b ? a - b : a > b + c ? a - b - c : 0) ** 2;
10
- const intersectRectCircle = (x, y, w, h, cx, cy, r) => axis(cx, x, w) + axis(cy, y, h) <= r * r;
9
+ const __axis = (a, b, c) => (a < b ? a - b : a > b + c ? a - b - c : 0) ** 2;
10
+ const intersectRectCircle = (x, y, w, h, cx, cy, r) => __axis(cx, x, w) + __axis(cy, y, h) <= r * r;
11
11
  export {
12
12
  charCode,
13
13
  intersectRect,