aboutsummaryrefslogtreecommitdiff
path: root/box-drawing.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-09-02 12:55:26 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-09-12 17:22:12 +0000
commitb4c759e2de429c20bf07c9e16162d154152014d9 (patch)
treed174db12ec22d8a209f9d146190239fa9d27456e /box-drawing.c
parentMerge branch 'pgo-script' (diff)
box-drawing: add braille characters
Render braille ourselves, instead of using font glyphs. Decoding a braille character is easy enough; there are 256 codepoints, represented by an 8-bit integer (i.e. subtract the Unicode codepoint offset, 0x2800, and you’re left with an integer in the range 0-255). Each bit corresponds to a dot. The first 6 bits represent the upper 6 dots, while the two last bits represent the fourth (and last) row of dots. The hard part is sizing the dots and the spacing between them. The aim is to have the spacing between the dots be the same size as the dots themselves, and to have the margins on each side be half the size of the dots. In a perfectly sized cell, this means two braille characters next to each other will be evenly spaced. This is however almost never the case. The layout logic currently: * Set dot size to either the width / 4, or height / 8, depending on which one is smallest. * Horizontal spacing is initialized to the width / 4 * Vertical spacing is initialized to the height / 8 * Horizontal margins are initialized to the horizontal spacing / 2 * Vertical margins are initialized to the vertical spacing / 2. Next, we calculate the number of “remaining” pixels. That is, if we add the left margin, two dots and the spacing between, how many pixels are left on the horizontal axis? These pixels are distributed in the following order (we “stop” as soon as we run out of pixels): * If the dot size is 0 (happens for very small font sizes), increase it to 1. * If the margins are 0, increase them to 1. * If we have enough pixels (need at 2 horizontal and 4 vertical), increase the dot size. * Increase spacing. * Increase margins. Closes #702
Diffstat (limited to 'box-drawing.c')
-rw-r--r--box-drawing.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/box-drawing.c b/box-drawing.c
index f246fd01..a1940392 100644
--- a/box-drawing.c
+++ b/box-drawing.c
@@ -1954,6 +1954,92 @@ draw_quadrant(struct buf *buf, wchar_t wc)
}
static void
+draw_braille(struct buf *buf, wchar_t wc)
+{
+ int w = min(buf->width / 4, buf->height / 8);
+ int x_spacing = buf->width / 4;
+ int y_spacing = buf->height / 8;
+ int x_margin = x_spacing / 2;
+ int y_margin = y_spacing / 2;
+
+ int x_pix_left = buf->width - 2 * x_margin - x_spacing - 2 * w;
+ int y_pix_left = buf->height - 2 * y_margin - 3 * y_spacing - 4 * w;
+
+ LOG_DBG(
+ "braille: before adjusting: "
+ "cell: %dx%d, margin=%dx%d, spacing=%dx%d, width=%d, left=%dx%d",
+ buf->width, buf->height, x_margin, y_margin, x_spacing, y_spacing,
+ w, x_pix_left, y_pix_left);
+
+ /* First, try hard to ensure the DOT width is non-zero */
+ if (x_pix_left >= 2 && y_pix_left >= 4 && w == 0) {
+ w++;
+ x_pix_left -= 2;
+ y_pix_left -= 4;
+ }
+
+ /* Second, prefer a non-zero margin */
+ if (x_pix_left >= 2 && x_margin == 0) { x_margin = 1; x_pix_left -= 2; }
+ if (y_pix_left >= 2 && y_margin == 0) { y_margin = 1; y_pix_left -= 2; }
+
+ if (x_pix_left >= 2 && y_pix_left >= 4) {
+ w++;
+ x_pix_left -= 2;
+ y_pix_left -= 4;
+ }
+
+ if (x_pix_left >= 1) { x_spacing++; x_pix_left--; }
+ if (y_pix_left >= 3) { y_spacing++; y_pix_left -= 3; }
+
+ if (x_pix_left >= 2) { x_margin++; x_pix_left -= 2; }
+ if (y_pix_left >= 2) { y_margin++; y_pix_left -= 2; }
+
+ LOG_DBG(
+ "braille: after adjusting: "
+ "cell: %dx%d, margin=%dx%d, spacing=%dx%d, width=%d, left=%dx%d",
+ buf->width, buf->height, x_margin, y_margin, x_spacing, y_spacing,
+ w, x_pix_left, y_pix_left);
+
+ xassert(x_pix_left <= 1 || y_pix_left <= 1);
+ xassert(2 * x_margin + 2 * w + x_spacing <= buf->width);
+ xassert(2 * y_margin + 4 * w + 3 * y_spacing <= buf->height);
+
+ int x[2], y[4];
+ x[0] = x_margin;
+ x[1] = x_margin + w + x_spacing;
+ y[0] = y_margin;
+ y[1] = y[0] + w + y_spacing;
+ y[2] = y[1] + w + y_spacing;
+ y[3] = y[2] + w + y_spacing;
+
+ assert(wc >= 0x2800);
+ assert(wc <= 0x28ff);
+ uint8_t sym = wc - 0x2800;
+
+ /* Left side */
+ if (sym & 1)
+ rect(x[0], y[0], x[0] + w, y[0] + w);
+ if (sym & 2)
+ rect(x[0], y[1], x[0] + w, y[1] + w);
+ if (sym & 4)
+ rect(x[0], y[2], x[0] + w, y[2] + w);
+
+ /* Right side */
+ if (sym & 8)
+ rect(x[1], y[0], x[1] + w, y[0] + w);
+ if (sym & 16)
+ rect(x[1], y[1], x[1] + w, y[1] + w);
+ if (sym & 32)
+ rect(x[1], y[2], x[1] + w, y[2] + w);
+
+ /* 8-dot patterns */
+ if (sym & 64)
+ rect(x[0], y[3], x[0] + w, y[3] + w);
+ if (sym & 128)
+ rect(x[1], y[3], x[1] + w, y[3] + w);
+}
+
+static void
sextant_upper_left(struct buf *buf)
{
rect(0, 0, buf->x_halfs[0], buf->y_thirds[0]);
@@ -2653,6 +2739,8 @@ draw_glyph(struct buf *buf, wchar_t wc)
case 0x2595: draw_right_one_eighth_block(buf); break;
case 0x2596 ... 0x259f: draw_quadrant(buf, wc); break;
+ case 0x2800 ... 0x28ff: draw_braille(buf, wc); break;
+
case 0x1fb00 ... 0x1fb3b: draw_sextant(buf, wc); break;
case 0x1fb3c ... 0x1fb40: