@thi.ng/text-canvas 2.6.21 → 2.6.23

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**: 2023-12-03T12:13:31Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
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.
package/README.md CHANGED
@@ -68,7 +68,7 @@ For Node.js REPL:
68
68
  const textCanvas = await import("@thi.ng/text-canvas");
69
69
  ```
70
70
 
71
- Package sizes (brotli'd, pre-treeshake): ESM: 5.51 KB
71
+ Package sizes (brotli'd, pre-treeshake): ESM: 5.56 KB
72
72
 
73
73
  ## Dependencies
74
74
 
package/api.js CHANGED
@@ -1,85 +1,101 @@
1
- export var Align;
2
- (function (Align) {
3
- Align[Align["LEFT"] = 0] = "LEFT";
4
- Align[Align["RIGHT"] = 1] = "RIGHT";
5
- Align[Align["CENTER"] = 2] = "CENTER";
6
- })(Align || (Align = {}));
7
- export var Border;
8
- (function (Border) {
9
- Border[Border["NONE"] = 0] = "NONE";
10
- Border[Border["H"] = 1] = "H";
11
- Border[Border["V"] = 2] = "V";
12
- Border[Border["ALL"] = 3] = "ALL";
13
- Border[Border["FRAME"] = 4] = "FRAME";
14
- Border[Border["FRAME_H"] = 5] = "FRAME_H";
15
- Border[Border["FRAME_V"] = 6] = "FRAME_V";
16
- })(Border || (Border = {}));
17
- // https://en.wikipedia.org/wiki/Box-drawing_character
18
- export const STYLE_ASCII = {
19
- hl: "-",
20
- vl: "|",
21
- tl: "+",
22
- tr: "+",
23
- bl: "+",
24
- br: "+",
25
- tjl: "+",
26
- tjr: "+",
27
- tjt: "+",
28
- tjb: "+",
29
- jct: "+",
30
- dot: ".",
1
+ var Align = /* @__PURE__ */ ((Align2) => {
2
+ Align2[Align2["LEFT"] = 0] = "LEFT";
3
+ Align2[Align2["RIGHT"] = 1] = "RIGHT";
4
+ Align2[Align2["CENTER"] = 2] = "CENTER";
5
+ return Align2;
6
+ })(Align || {});
7
+ var Border = /* @__PURE__ */ ((Border2) => {
8
+ Border2[Border2["NONE"] = 0] = "NONE";
9
+ Border2[Border2["H"] = 1] = "H";
10
+ Border2[Border2["V"] = 2] = "V";
11
+ Border2[Border2["ALL"] = 3] = "ALL";
12
+ Border2[Border2["FRAME"] = 4] = "FRAME";
13
+ Border2[Border2["FRAME_H"] = 5] = "FRAME_H";
14
+ Border2[Border2["FRAME_V"] = 6] = "FRAME_V";
15
+ return Border2;
16
+ })(Border || {});
17
+ const STYLE_ASCII = {
18
+ hl: "-",
19
+ vl: "|",
20
+ tl: "+",
21
+ tr: "+",
22
+ bl: "+",
23
+ br: "+",
24
+ tjl: "+",
25
+ tjr: "+",
26
+ tjt: "+",
27
+ tjb: "+",
28
+ jct: "+",
29
+ dot: "."
31
30
  };
32
- export const STYLE_THIN = {
33
- hl: "",
34
- vl: "",
35
- tl: "",
36
- tr: "",
37
- bl: "",
38
- br: "",
39
- tjl: "",
40
- tjr: "",
41
- tjt: "",
42
- tjb: "",
43
- jct: "",
44
- dot: "",
31
+ const STYLE_THIN = {
32
+ hl: "\u2500",
33
+ vl: "\u2502",
34
+ tl: "\u250C",
35
+ tr: "\u2510",
36
+ bl: "\u2514",
37
+ br: "\u2518",
38
+ tjl: "\u251C",
39
+ tjr: "\u2524",
40
+ tjt: "\u252C",
41
+ tjb: "\u2534",
42
+ jct: "\u253C",
43
+ dot: "\u2022"
45
44
  };
46
- export const STYLE_THIN_ROUNDED = {
47
- ...STYLE_THIN,
48
- tl: "",
49
- tr: "",
50
- bl: "",
51
- br: "",
45
+ const STYLE_THIN_ROUNDED = {
46
+ ...STYLE_THIN,
47
+ tl: "\u256D",
48
+ tr: "\u256E",
49
+ bl: "\u2570",
50
+ br: "\u256F"
52
51
  };
53
- export const STYLE_DASHED = {
54
- ...STYLE_THIN,
55
- hl: "",
56
- vl: "",
52
+ const STYLE_DASHED = {
53
+ ...STYLE_THIN,
54
+ hl: "\u254C",
55
+ vl: "\u2506"
57
56
  };
58
- export const STYLE_DASHED_ROUNDED = {
59
- ...STYLE_DASHED,
60
- tl: "",
61
- tr: "",
62
- bl: "",
63
- br: "",
57
+ const STYLE_DASHED_ROUNDED = {
58
+ ...STYLE_DASHED,
59
+ tl: "\u256D",
60
+ tr: "\u256E",
61
+ bl: "\u2570",
62
+ br: "\u256F"
64
63
  };
65
- export const STYLE_DOUBLE = {
66
- hl: "",
67
- vl: "",
68
- tl: "",
69
- tr: "",
70
- bl: "",
71
- br: "",
72
- tjl: "",
73
- tjr: "",
74
- tjt: "",
75
- tjb: "",
76
- jct: "",
77
- dot: "",
64
+ const STYLE_DOUBLE = {
65
+ hl: "\u2550",
66
+ vl: "\u2551",
67
+ tl: "\u2554",
68
+ tr: "\u2557",
69
+ bl: "\u255A",
70
+ br: "\u255D",
71
+ tjl: "\u2560",
72
+ tjr: "\u2563",
73
+ tjt: "\u2566",
74
+ tjb: "\u2569",
75
+ jct: "\u256C",
76
+ dot: "\u2022"
77
+ };
78
+ const ENDINGS = "()[]{}<>^v\u25C0\u25B6\u25B2\u25BC\u2022\u25CF";
79
+ const BARS_H = " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588";
80
+ const BARS_V = " \u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588";
81
+ const SHADES_ASCII_10 = " .-:=+*#%@";
82
+ const SHADES_ASCII_16 = " .,-:+=il3GXOQW0";
83
+ const SHADES_BLOCK = " \u2591\u2592\u2593\u2588";
84
+ const SHADES_BLOCK_ALT = " \u2596\u259E\u259F\u2588";
85
+ export {
86
+ Align,
87
+ BARS_H,
88
+ BARS_V,
89
+ Border,
90
+ ENDINGS,
91
+ SHADES_ASCII_10,
92
+ SHADES_ASCII_16,
93
+ SHADES_BLOCK,
94
+ SHADES_BLOCK_ALT,
95
+ STYLE_ASCII,
96
+ STYLE_DASHED,
97
+ STYLE_DASHED_ROUNDED,
98
+ STYLE_DOUBLE,
99
+ STYLE_THIN,
100
+ STYLE_THIN_ROUNDED
78
101
  };
79
- export const ENDINGS = "()[]{}<>^v◀▶▲▼•●";
80
- export const BARS_H = " ▏▎▍▌▋▊▉█";
81
- export const BARS_V = " ▁▂▃▄▅▆▇█";
82
- export const SHADES_ASCII_10 = " .-:=+*#%@";
83
- export const SHADES_ASCII_16 = " .,-:+=il3GXOQW0";
84
- export const SHADES_BLOCK = " ░▒▓█";
85
- export const SHADES_BLOCK_ALT = " ▖▞▟█";
package/bars.js CHANGED
@@ -8,39 +8,45 @@ import { map } from "@thi.ng/transducers/map";
8
8
  import { max as $max } from "@thi.ng/transducers/max";
9
9
  import { min as $min } from "@thi.ng/transducers/min";
10
10
  import { BARS_H, BARS_V } from "./api.js";
11
- export const barChartHLines = (height, vals, min, max) => {
12
- const $vals = ensureArray(vals);
13
- min = min !== undefined ? min : $min($vals);
14
- max = max !== undefined ? max : $max($vals);
15
- const bars = [...map((x) => barVertical(height, x, min, max, ""), $vals)];
16
- const num = bars.length;
17
- const res = [];
18
- for (let i = 0; i < height; i++) {
19
- let line = "";
20
- for (let j = 0; j < num; j++) {
21
- line += bars[j][i];
22
- }
23
- res.push(line);
11
+ const barChartHLines = (height, vals, min, max) => {
12
+ const $vals = ensureArray(vals);
13
+ min = min !== void 0 ? min : $min($vals);
14
+ max = max !== void 0 ? max : $max($vals);
15
+ const bars = [...map((x) => barVertical(height, x, min, max, ""), $vals)];
16
+ const num = bars.length;
17
+ const res = [];
18
+ for (let i = 0; i < height; i++) {
19
+ let line = "";
20
+ for (let j = 0; j < num; j++) {
21
+ line += bars[j][i];
24
22
  }
25
- return res;
23
+ res.push(line);
24
+ }
25
+ return res;
26
26
  };
27
- export const barChartHStr = (height, vals, min, max) => barChartHLines(height, vals, min, max).join("\n");
28
- export const barChartVLines = (width, vals, min, max) => {
29
- const $vals = ensureArray(vals);
30
- min = min !== undefined ? min : $min($vals);
31
- max = max !== undefined ? max : $max($vals);
32
- return [...map((x) => barHorizontal(width, x, min, max), $vals)];
27
+ const barChartHStr = (height, vals, min, max) => barChartHLines(height, vals, min, max).join("\n");
28
+ const barChartVLines = (width, vals, min, max) => {
29
+ const $vals = ensureArray(vals);
30
+ min = min !== void 0 ? min : $min($vals);
31
+ max = max !== void 0 ? max : $max($vals);
32
+ return [...map((x) => barHorizontal(width, x, min, max), $vals)];
33
33
  };
34
- export const barChartVStr = (width, vals, min, max) => barChartVLines(width, vals, min, max).join("\n");
35
- export const barHorizontal = (width, x, min = 0, max = 1) => bar(BARS_H, width, false, x, min, max, "");
36
- export const barVertical = (height, x, min = 0, max = 1, delim = "\n") => bar(BARS_V, height, true, x, min, max, delim);
34
+ const barChartVStr = (width, vals, min, max) => barChartVLines(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
37
  const bar = (chars, size, left, x, min, max, delim) => {
38
- x = fitClamped(x, min, max, 0, size);
39
- const f = (fract(x) * 9) | 0;
40
- const full = repeat(chars[8] + delim, x | 0);
41
- const partial = f > 0 ? chars[f] + delim : "";
42
- size += size * delim.length;
43
- return left
44
- ? padLeft(size, " ")(partial + full)
45
- : padRight(size, " ")(full + partial);
38
+ x = fitClamped(x, min, max, 0, size);
39
+ const f = fract(x) * 9 | 0;
40
+ const full = repeat(chars[8] + delim, x | 0);
41
+ const partial = f > 0 ? chars[f] + delim : "";
42
+ size += size * delim.length;
43
+ return left ? padLeft(size, " ")(partial + full) : padRight(size, " ")(full + partial);
44
+ };
45
+ export {
46
+ barChartHLines,
47
+ barChartHStr,
48
+ barChartVLines,
49
+ barChartVStr,
50
+ barHorizontal,
51
+ barVertical
46
52
  };
package/canvas.js CHANGED
@@ -1,6 +1,17 @@
1
- var Canvas_1;
2
- import { __decorate } from "tslib";
3
- import { nomixin, } from "@thi.ng/api";
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
12
+ import {
13
+ nomixin
14
+ } from "@thi.ng/api";
4
15
  import { IGrid2DMixin } from "@thi.ng/api/mixins/igrid";
5
16
  import { peek } from "@thi.ng/arrays/peek";
6
17
  import { assert } from "@thi.ng/errors/assert";
@@ -11,147 +22,165 @@ import { max } from "@thi.ng/transducers/max";
11
22
  import { transduce } from "@thi.ng/transducers/transduce";
12
23
  import { STYLE_ASCII } from "./api.js";
13
24
  import { charCode, intersectRect } from "./utils.js";
14
- let Canvas = Canvas_1 = class Canvas {
15
- data;
16
- size;
17
- stride;
18
- format;
19
- defaultFormat;
20
- styles;
21
- clipRects;
22
- constructor(width, height, format = NONE, style = STYLE_ASCII) {
23
- this.size = [width, height];
24
- this.stride = [1, this.width];
25
- this.format = this.defaultFormat = format;
26
- this.data = new Uint32Array(width * height).fill(charCode(0x20, format));
27
- this.styles = [style];
28
- this.clipRects = [
29
- { x1: 0, y1: 0, x2: width, y2: height, w: width, h: height },
30
- ];
31
- }
32
- get width() {
33
- return this.size[0];
34
- }
35
- get height() {
36
- return this.size[1];
37
- }
38
- get offset() {
39
- return 0;
40
- }
41
- get dim() {
42
- return 2;
43
- }
44
- copy() {
45
- const res = new Canvas_1(this.width, this.height, this.format);
46
- res.data.set(this.data);
47
- res.stride = this.stride.slice();
48
- res.styles = this.styles.slice();
49
- res.clipRects = this.clipRects.slice();
50
- return res;
51
- }
52
- // @ts-ignore mixin
53
- order() { }
54
- // @ts-ignore mixin
55
- includes(d0, d1) { }
56
- // @ts-ignore mixin
57
- indexAt(d0, d1) { }
58
- // @ts-ignore mixin
59
- indexAtUnsafe(d0, d1) { }
60
- // @ts-ignore mixin
61
- getAt(x, y) { }
62
- // @ts-ignore mixin
63
- getAtUnsafe(x, y) { }
64
- setAt(x, y, val) {
65
- return this.includes(x, y)
66
- ? ((this.data[this.indexAtUnsafe(x, y)] = charCode(val, this.format)),
67
- true)
68
- : false;
69
- }
70
- // @ts-ignore mixin
71
- setAtUnsafe(x, y, col) { }
25
+ let Canvas = class {
26
+ data;
27
+ size;
28
+ stride;
29
+ format;
30
+ defaultFormat;
31
+ styles;
32
+ clipRects;
33
+ constructor(width, height, format = NONE, style = STYLE_ASCII) {
34
+ this.size = [width, height];
35
+ this.stride = [1, this.width];
36
+ this.format = this.defaultFormat = format;
37
+ this.data = new Uint32Array(width * height).fill(
38
+ charCode(32, format)
39
+ );
40
+ this.styles = [style];
41
+ this.clipRects = [
42
+ { x1: 0, y1: 0, x2: width, y2: height, w: width, h: height }
43
+ ];
44
+ }
45
+ get width() {
46
+ return this.size[0];
47
+ }
48
+ get height() {
49
+ return this.size[1];
50
+ }
51
+ get offset() {
52
+ return 0;
53
+ }
54
+ get dim() {
55
+ return 2;
56
+ }
57
+ copy() {
58
+ const res = new Canvas(this.width, this.height, this.format);
59
+ res.data.set(this.data);
60
+ res.stride = this.stride.slice();
61
+ res.styles = this.styles.slice();
62
+ res.clipRects = this.clipRects.slice();
63
+ return res;
64
+ }
65
+ // @ts-ignore mixin
66
+ order() {
67
+ }
68
+ // @ts-ignore mixin
69
+ includes(d0, d1) {
70
+ }
71
+ // @ts-ignore mixin
72
+ indexAt(d0, d1) {
73
+ }
74
+ // @ts-ignore mixin
75
+ indexAtUnsafe(d0, d1) {
76
+ }
77
+ // @ts-ignore mixin
78
+ getAt(x, y) {
79
+ }
80
+ // @ts-ignore mixin
81
+ getAtUnsafe(x, y) {
82
+ }
83
+ setAt(x, y, val) {
84
+ return this.includes(x, y) ? (this.data[this.indexAtUnsafe(x, y)] = charCode(
85
+ val,
86
+ this.format
87
+ ), true) : false;
88
+ }
89
+ // @ts-ignore mixin
90
+ setAtUnsafe(x, y, col) {
91
+ }
72
92
  };
73
- __decorate([
74
- nomixin
75
- ], Canvas.prototype, "setAt", null);
76
- Canvas = Canvas_1 = __decorate([
77
- IGrid2DMixin
93
+ __decorateClass([
94
+ nomixin
95
+ ], Canvas.prototype, "setAt", 1);
96
+ Canvas = __decorateClass([
97
+ IGrid2DMixin
78
98
  ], Canvas);
79
- export { Canvas };
80
- export const canvas = (width, height, format, style) => new Canvas(width, height, format, style);
81
- /**
82
- * Creates and returns a new {@link Canvas} from given string/lines array and
83
- * optional default `format` and/or initial `fill` value.
84
- *
85
- * @remarks
86
- * The canvas will use the longest line width as its width and the length of the
87
- * source array as height.
88
- *
89
- * @param lines
90
- * @param format
91
- * @param fill
92
- */
93
- export const canvasFromText = (lines, format = NONE, fill) => {
94
- const height = lines.length;
95
- assert(height > 0, "require at least 1 line of text");
96
- const width = transduce(map((x) => x.length), max(), lines);
97
- const res = canvas(width, height, format);
98
- fill !== undefined && res.data.fill(charCode(fill, format));
99
- format <<= 16;
100
- for (let y = 0; y < height; y++) {
101
- const line = lines[y];
102
- for (let x = 0, i = y * width, n = Math.min(width, line.length); x < n; x++, i++) {
103
- res.data[i] = line.charCodeAt(x) | format;
104
- }
99
+ const canvas = (width, height, format, style) => new Canvas(width, height, format, style);
100
+ const canvasFromText = (lines, format = NONE, fill) => {
101
+ const height = lines.length;
102
+ assert(height > 0, "require at least 1 line of text");
103
+ const width = transduce(
104
+ map((x) => x.length),
105
+ max(),
106
+ lines
107
+ );
108
+ const res = canvas(width, height, format);
109
+ fill !== void 0 && res.data.fill(charCode(fill, format));
110
+ format <<= 16;
111
+ for (let y = 0; y < height; y++) {
112
+ const line = lines[y];
113
+ for (let x = 0, i = y * width, n = Math.min(width, line.length); x < n; x++, i++) {
114
+ res.data[i] = line.charCodeAt(x) | format;
105
115
  }
106
- return res;
116
+ }
117
+ return res;
107
118
  };
108
- export const beginClip = (canvas, x, y, w, h) => {
109
- x |= 0;
110
- y |= 0;
111
- w |= 0;
112
- h |= 0;
113
- const { width, height } = canvas;
114
- const x2 = clamp(x + w, 0, width);
115
- const y2 = clamp(y + h, 0, height);
116
- const x1 = clamp(x, 0, width);
117
- const y1 = clamp(y, 0, height);
118
- canvas.clipRects.push(intersectRect({ x1, y1, x2, y2, w: x2 - x1, h: y2 - y1 }, peek(canvas.clipRects)));
119
+ const beginClip = (canvas2, x, y, w, h) => {
120
+ x |= 0;
121
+ y |= 0;
122
+ w |= 0;
123
+ h |= 0;
124
+ const { width, height } = canvas2;
125
+ const x2 = clamp(x + w, 0, width);
126
+ const y2 = clamp(y + h, 0, height);
127
+ const x1 = clamp(x, 0, width);
128
+ const y1 = clamp(y, 0, height);
129
+ canvas2.clipRects.push(
130
+ intersectRect(
131
+ { x1, y1, x2, y2, w: x2 - x1, h: y2 - y1 },
132
+ peek(canvas2.clipRects)
133
+ )
134
+ );
119
135
  };
120
136
  const pop = (stack) => stack.length > 1 && stack.pop();
121
- export const endClip = (canvas) => pop(canvas.clipRects);
122
- export const withClip = (canvas, x, y, w, h, fn) => {
123
- beginClip(canvas, x, y, w, h);
124
- fn();
125
- canvas.clipRects.pop();
137
+ const endClip = (canvas2) => pop(canvas2.clipRects);
138
+ const withClip = (canvas2, x, y, w, h, fn) => {
139
+ beginClip(canvas2, x, y, w, h);
140
+ fn();
141
+ canvas2.clipRects.pop();
142
+ };
143
+ const beginStyle = (canvas2, style) => {
144
+ canvas2.styles.push(style);
126
145
  };
127
- export const beginStyle = (canvas, style) => {
128
- canvas.styles.push(style);
146
+ const endStyle = (canvas2) => pop(canvas2.styles);
147
+ const withStyle = (canvas2, style, fn) => {
148
+ canvas2.styles.push(style);
149
+ fn();
150
+ canvas2.styles.pop();
129
151
  };
130
- export const endStyle = (canvas) => pop(canvas.styles);
131
- export const withStyle = (canvas, style, fn) => {
132
- canvas.styles.push(style);
133
- fn();
134
- canvas.styles.pop();
152
+ const setFormat = (canvas2, format) => canvas2.format = format;
153
+ const withFormat = (canvas2, format, fn) => {
154
+ const prev = canvas2.format;
155
+ canvas2.format = format;
156
+ fn();
157
+ canvas2.format = prev;
135
158
  };
136
- export const setFormat = (canvas, format) => (canvas.format = format);
137
- export const withFormat = (canvas, format, fn) => {
138
- const prev = canvas.format;
139
- canvas.format = format;
140
- fn();
141
- canvas.format = prev;
159
+ const getAt = (canvas2, x, y) => {
160
+ x |= 0;
161
+ y |= 0;
162
+ return x >= 0 && y >= 0 && x < canvas2.width && y < canvas2.height ? canvas2.data[x + y * canvas2.width] : 0;
142
163
  };
143
- export const getAt = (canvas, x, y) => {
144
- x |= 0;
145
- y |= 0;
146
- return x >= 0 && y >= 0 && x < canvas.width && y < canvas.height
147
- ? canvas.data[x + y * canvas.width]
148
- : 0;
164
+ const setAt = (canvas2, x, y, code, format = canvas2.format) => {
165
+ x |= 0;
166
+ y |= 0;
167
+ const { x1, y1, x2, y2 } = peek(canvas2.clipRects);
168
+ if (x < x1 || y < y1 || x >= x2 || y >= y2)
169
+ return;
170
+ canvas2.data[x + y * canvas2.width] = charCode(code, format);
149
171
  };
150
- export const setAt = (canvas, x, y, code, format = canvas.format) => {
151
- x |= 0;
152
- y |= 0;
153
- const { x1, y1, x2, y2 } = peek(canvas.clipRects);
154
- if (x < x1 || y < y1 || x >= x2 || y >= y2)
155
- return;
156
- canvas.data[x + y * canvas.width] = charCode(code, format);
172
+ export {
173
+ Canvas,
174
+ beginClip,
175
+ beginStyle,
176
+ canvas,
177
+ canvasFromText,
178
+ endClip,
179
+ endStyle,
180
+ getAt,
181
+ setAt,
182
+ setFormat,
183
+ withClip,
184
+ withFormat,
185
+ withStyle
157
186
  };