@thi.ng/text-canvas 2.6.22 → 2.6.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 +1 -1
- package/README.md +1 -1
- package/api.js +95 -79
- package/bars.js +37 -31
- package/canvas.js +164 -135
- package/circle.js +55 -66
- package/format.js +14 -20
- package/hvline.js +64 -61
- package/image.js +231 -310
- package/line.js +66 -73
- package/package.json +15 -12
- package/rect.js +66 -88
- package/style.js +30 -26
- package/table.js +133 -118
- package/text.js +86 -108
- package/utils.js +13 -8
package/circle.js
CHANGED
|
@@ -1,72 +1,61 @@
|
|
|
1
1
|
import { peek } from "@thi.ng/arrays/peek";
|
|
2
2
|
import { hline } from "./hvline.js";
|
|
3
3
|
import { charCode, intersectRectCircle } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
(
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
const circle = (canvas, cx, cy, r, char, fill = false, format = canvas.format) => {
|
|
5
|
+
if (r < 1)
|
|
6
|
+
return;
|
|
7
|
+
cx |= 0;
|
|
8
|
+
cy |= 0;
|
|
9
|
+
r |= 0;
|
|
10
|
+
const { x1, y1, x2, y2, w: clipw, h: cliph } = peek(canvas.clipRects);
|
|
11
|
+
if (!intersectRectCircle(x1, y1, clipw, cliph, cx, cy, r))
|
|
12
|
+
return;
|
|
13
|
+
char = charCode(
|
|
14
|
+
char !== void 0 ? char : peek(canvas.styles).dot,
|
|
15
|
+
format
|
|
16
|
+
);
|
|
17
|
+
let x = 0;
|
|
18
|
+
let y = r;
|
|
19
|
+
let ymax = r * r;
|
|
20
|
+
let sum = ymax + r;
|
|
21
|
+
let dx2 = 1;
|
|
22
|
+
let dy2 = 2 * r - 1;
|
|
23
|
+
const { data, width } = canvas;
|
|
24
|
+
const $ = (ox, oy) => ox >= x1 && oy >= y1 && ox < x2 && oy < y2 && (data[ox + oy * width] = char);
|
|
25
|
+
while (x <= y) {
|
|
26
|
+
if (fill) {
|
|
27
|
+
hline(canvas, cx - y, cy + x, y << 1, char, char, char);
|
|
28
|
+
x && hline(canvas, cx - y, cy - x, y << 1, char, char, char);
|
|
29
|
+
} else {
|
|
30
|
+
$(cx - y, cy + x);
|
|
31
|
+
y && $(cx + y, cy + x);
|
|
32
|
+
if (x) {
|
|
33
|
+
$(cx - y, cy - x);
|
|
34
|
+
y && $(cx + y, cy - x);
|
|
35
|
+
}
|
|
36
|
+
if (x !== y) {
|
|
37
|
+
$(cx - x, cy - y);
|
|
38
|
+
x && $(cx + x, cy - y);
|
|
39
|
+
if (y) {
|
|
40
|
+
$(cx - x, cy + y);
|
|
41
|
+
x && $(cx + x, cy + y);
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
$(cx - y, cy + x);
|
|
45
|
-
y && $(cx + y, cy + x);
|
|
46
|
-
if (x) {
|
|
47
|
-
$(cx - y, cy - x);
|
|
48
|
-
y && $(cx + y, cy - x);
|
|
49
|
-
}
|
|
50
|
-
if (x !== y) {
|
|
51
|
-
$(cx - x, cy - y);
|
|
52
|
-
x && $(cx + x, cy - y);
|
|
53
|
-
if (y) {
|
|
54
|
-
$(cx - x, cy + y);
|
|
55
|
-
x && $(cx + x, cy + y);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
sum -= dx2;
|
|
60
|
-
if (sum <= ymax) {
|
|
61
|
-
if (fill && x !== y) {
|
|
62
|
-
hline(canvas, cx - x, cy - y, x << 1, char, char, char);
|
|
63
|
-
y && hline(canvas, cx - x, cy + y, x << 1, char, char, char);
|
|
64
|
-
}
|
|
65
|
-
y--;
|
|
66
|
-
ymax -= dy2;
|
|
67
|
-
dy2 -= 2;
|
|
68
|
-
}
|
|
69
|
-
x++;
|
|
70
|
-
dx2 += 2;
|
|
43
|
+
}
|
|
71
44
|
}
|
|
45
|
+
sum -= dx2;
|
|
46
|
+
if (sum <= ymax) {
|
|
47
|
+
if (fill && x !== y) {
|
|
48
|
+
hline(canvas, cx - x, cy - y, x << 1, char, char, char);
|
|
49
|
+
y && hline(canvas, cx - x, cy + y, x << 1, char, char, char);
|
|
50
|
+
}
|
|
51
|
+
y--;
|
|
52
|
+
ymax -= dy2;
|
|
53
|
+
dy2 -= 2;
|
|
54
|
+
}
|
|
55
|
+
x++;
|
|
56
|
+
dx2 += 2;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
export {
|
|
60
|
+
circle
|
|
72
61
|
};
|
package/format.js
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
1
|
import { format, formatNone } from "@thi.ng/text-format/format";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @param fmt -
|
|
9
|
-
*/
|
|
10
|
-
export const formatCanvas = (canvas, fmt) => {
|
|
11
|
-
const { data, width, height } = canvas;
|
|
12
|
-
const res = [];
|
|
13
|
-
if (fmt) {
|
|
14
|
-
for (let y = 0; y < height; y++) {
|
|
15
|
-
res.push(format(fmt, data, width, y * width));
|
|
16
|
-
}
|
|
2
|
+
const formatCanvas = (canvas, fmt) => {
|
|
3
|
+
const { data, width, height } = canvas;
|
|
4
|
+
const res = [];
|
|
5
|
+
if (fmt) {
|
|
6
|
+
for (let y = 0; y < height; y++) {
|
|
7
|
+
res.push(format(fmt, data, width, y * width));
|
|
17
8
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
9
|
+
} else {
|
|
10
|
+
for (let y = 0; y < height; y++) {
|
|
11
|
+
res.push(formatNone(data, width, y * width));
|
|
22
12
|
}
|
|
23
|
-
|
|
13
|
+
}
|
|
14
|
+
return res.join("");
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
formatCanvas
|
|
24
18
|
};
|
package/hvline.js
CHANGED
|
@@ -1,68 +1,71 @@
|
|
|
1
1
|
import { peek } from "@thi.ng/arrays/peek";
|
|
2
2
|
import { charCode } from "./utils.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
3
|
+
const hline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
|
|
4
|
+
const { x1, y1, x2, y2 } = peek(canvas.clipRects);
|
|
5
|
+
if (len < 1 || y < y1 || y >= y2 || x >= x2)
|
|
6
|
+
return;
|
|
7
|
+
_hvline(
|
|
8
|
+
canvas.data,
|
|
9
|
+
x,
|
|
10
|
+
y,
|
|
11
|
+
1,
|
|
12
|
+
canvas.width,
|
|
13
|
+
x1,
|
|
14
|
+
x2,
|
|
15
|
+
len,
|
|
16
|
+
format,
|
|
17
|
+
peek(canvas.styles).hl,
|
|
18
|
+
s,
|
|
19
|
+
e,
|
|
20
|
+
m
|
|
21
|
+
);
|
|
22
22
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
23
|
+
const vline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
|
|
24
|
+
const { x1, x2, y1, y2 } = peek(canvas.clipRects);
|
|
25
|
+
if (len < 1 || x < x1 || x >= x2 || y >= y2)
|
|
26
|
+
return;
|
|
27
|
+
_hvline(
|
|
28
|
+
canvas.data,
|
|
29
|
+
y,
|
|
30
|
+
x,
|
|
31
|
+
canvas.width,
|
|
32
|
+
1,
|
|
33
|
+
y1,
|
|
34
|
+
y2,
|
|
35
|
+
len,
|
|
36
|
+
format,
|
|
37
|
+
peek(canvas.styles).vl,
|
|
38
|
+
s,
|
|
39
|
+
e,
|
|
40
|
+
m
|
|
41
|
+
);
|
|
42
42
|
};
|
|
43
43
|
const _hvline = (buf, a, b, astride, bstride, amin, amax, len, format, style, s, e, m) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
a++;
|
|
58
|
-
len--;
|
|
59
|
-
}
|
|
44
|
+
a |= 0;
|
|
45
|
+
b |= 0;
|
|
46
|
+
len |= 0;
|
|
47
|
+
let idx;
|
|
48
|
+
if (a < amin) {
|
|
49
|
+
len += a - amin;
|
|
50
|
+
a = amin;
|
|
51
|
+
idx = a * astride + b * bstride;
|
|
52
|
+
} else {
|
|
53
|
+
idx = a * astride + b * bstride;
|
|
54
|
+
buf[idx] = charCode(s !== void 0 && len > 1 ? s : style, format);
|
|
55
|
+
idx += astride;
|
|
56
|
+
a++;
|
|
60
57
|
len--;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
}
|
|
59
|
+
len--;
|
|
60
|
+
m = charCode(m != void 0 ? m : style, format);
|
|
61
|
+
for (let i = 0; i < len && a < amax; i++, a++, idx += astride) {
|
|
62
|
+
buf[idx] = m;
|
|
63
|
+
}
|
|
64
|
+
if (len >= 0 && a < amax) {
|
|
65
|
+
buf[idx] = charCode(e !== void 0 ? e : style, format);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
export {
|
|
69
|
+
hline,
|
|
70
|
+
vline
|
|
68
71
|
};
|