@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/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
- * Draws a circle (filled or outline) at given center position and
6
- * radius and taking the current clip rect and format into account. If
7
- * `char` is not given, uses current style's `dot` char.
8
- *
9
- * @param canvas -
10
- * @param cx -
11
- * @param cy -
12
- * @param r -
13
- * @param char -
14
- * @param fill -
15
- */
16
- export const circle = (canvas, cx, cy, r, char, fill = false, format = canvas.format) => {
17
- if (r < 1)
18
- return;
19
- cx |= 0;
20
- cy |= 0;
21
- r |= 0;
22
- const { x1, y1, x2, y2, w: clipw, h: cliph } = peek(canvas.clipRects);
23
- if (!intersectRectCircle(x1, y1, clipw, cliph, cx, cy, r))
24
- return;
25
- char = charCode(char !== undefined ? char : peek(canvas.styles).dot, format);
26
- let x = 0;
27
- let y = r;
28
- let ymax = r * r;
29
- let sum = ymax + r;
30
- let dx2 = 1;
31
- let dy2 = 2 * r - 1;
32
- const { data, width } = canvas;
33
- const $ = (ox, oy) => ox >= x1 &&
34
- oy >= y1 &&
35
- ox < x2 &&
36
- oy < y2 &&
37
- (data[ox + oy * width] = char);
38
- while (x <= y) {
39
- if (fill) {
40
- hline(canvas, cx - y, cy + x, y << 1, char, char, char);
41
- x && hline(canvas, cx - y, cy - x, y << 1, char, char, char);
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
- else {
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
- * Returns string representation of canvas, optionally using given string
4
- * formatter. If none is given, returns plain string representation, ignoring
5
- * any character format data.
6
- *
7
- * @param canvas -
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
- else {
19
- for (let y = 0; y < height; y++) {
20
- res.push(formatNone(data, width, y * width));
21
- }
9
+ } else {
10
+ for (let y = 0; y < height; y++) {
11
+ res.push(formatNone(data, width, y * width));
22
12
  }
23
- return res.join("");
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
- * Draws horizontal line from `x`,`y`, taking the current clip rect,
5
- * format and style into account. The optional `s`, `e` (line endings)
6
- * and `m` (line middle) chars can be used to override the currently
7
- * active style.
8
- *
9
- * @param canvas -
10
- * @param x -
11
- * @param y -
12
- * @param len -
13
- * @param s -
14
- * @param e -
15
- * @param m -
16
- */
17
- export const hline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
18
- const { x1, y1, x2, y2 } = peek(canvas.clipRects);
19
- if (len < 1 || y < y1 || y >= y2 || x >= x2)
20
- return;
21
- _hvline(canvas.data, x, y, 1, canvas.width, x1, x2, len, format, peek(canvas.styles).hl, s, e, m);
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
- * Draws a vertical line from `x`,`y`, taking the current clip rect,
25
- * format and style into account. The optional `s`, `e` (line endings)
26
- * and `m` (line middle) chars can be used to override the currently
27
- * active style.
28
- *
29
- * @param canvas -
30
- * @param x -
31
- * @param y -
32
- * @param len -
33
- * @param s -
34
- * @param e -
35
- * @param m -
36
- */
37
- export const vline = (canvas, x, y, len, s, e, m, format = canvas.format) => {
38
- const { x1, x2, y1, y2 } = peek(canvas.clipRects);
39
- if (len < 1 || x < x1 || x >= x2 || y >= y2)
40
- return;
41
- _hvline(canvas.data, y, x, canvas.width, 1, y1, y2, len, format, peek(canvas.styles).vl, s, e, m);
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
- 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
- }
53
- else {
54
- idx = a * astride + b * bstride;
55
- buf[idx] = charCode(s !== undefined && len > 1 ? s : style, format);
56
- idx += astride;
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
- m = charCode(m != undefined ? m : style, format);
62
- for (let i = 0; i < len && a < amax; i++, a++, idx += astride) {
63
- buf[idx] = m;
64
- }
65
- if (len >= 0 && a < amax) {
66
- buf[idx] = charCode(e !== undefined ? e : style, format);
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
  };