From b4c759e2de429c20bf07c9e16162d154152014d9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 2 Sep 2021 14:55:26 +0200 Subject: box-drawing: add braille characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- box-drawing.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'box-drawing.c') diff --git a/box-drawing.c b/box-drawing.c index f246fd01..a1940392 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -1953,6 +1953,92 @@ draw_quadrant(struct buf *buf, wchar_t wc) quad_lower_right(buf); } +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) { @@ -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: -- cgit v1.3 From ac2091f107cbcca546d9f62fc81c5a519aec67dc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 5 Sep 2021 10:05:35 +0200 Subject: box-drawing: NOINLINE braille --- box-drawing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'box-drawing.c') diff --git a/box-drawing.c b/box-drawing.c index a1940392..1c694627 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -1953,7 +1953,7 @@ draw_quadrant(struct buf *buf, wchar_t wc) quad_lower_right(buf); } -static void +static void NOINLINE draw_braille(struct buf *buf, wchar_t wc) { int w = min(buf->width / 4, buf->height / 8); -- cgit v1.3 From e426e77b1d3640868b521b65617525f76ab3b35c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 14 Sep 2021 10:20:07 +0200 Subject: box-drawing: braille: prefer increasing spacing over dot width Too large dots make them harder to distinguish, when the spacing between them is small. Prefer increasing the spacing, instead of increasing the dot size. This looks better at small font sizes in particular. --- box-drawing.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'box-drawing.c') diff --git a/box-drawing.c b/box-drawing.c index 1c694627..f1789f64 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -1962,8 +1962,8 @@ draw_braille(struct buf *buf, wchar_t wc) 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; + int x_px_left = buf->width - 2 * x_margin - x_spacing - 2 * w; + int y_px_left = buf->height - 2 * y_margin - 3 * y_spacing - 4 * w; LOG_DBG( "braille: before adjusting: " @@ -1972,27 +1972,30 @@ draw_braille(struct buf *buf, wchar_t wc) 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) { + if (x_px_left >= 2 && y_px_left >= 4 && w == 0) { w++; - x_pix_left -= 2; - y_pix_left -= 4; + x_px_left -= 2; + y_px_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_px_left >= 2 && x_margin == 0) { x_margin = 1; x_px_left -= 2; } + if (y_px_left >= 2 && y_margin == 0) { y_margin = 1; y_px_left -= 2; } - if (x_pix_left >= 2 && y_pix_left >= 4) { - w++; - x_pix_left -= 2; - y_pix_left -= 4; - } + /* Third, increase spacing */ + if (x_px_left >= 1) { x_spacing++; x_px_left--; } + if (y_px_left >= 3) { y_spacing++; y_px_left -= 3; } - if (x_pix_left >= 1) { x_spacing++; x_pix_left--; } - if (y_pix_left >= 3) { y_spacing++; y_pix_left -= 3; } + /* Fourth, margins (“spacing”, but on the sides) */ + if (x_px_left >= 2) { x_margin++; x_px_left -= 2; } + if (y_px_left >= 2) { y_margin++; y_px_left -= 2; } - if (x_pix_left >= 2) { x_margin++; x_pix_left -= 2; } - if (y_pix_left >= 2) { y_margin++; y_pix_left -= 2; } + /* Last - increase dot width */ + if (x_px_left >= 2 && y_px_left >= 4) { + w++; + x_px_left -= 2; + y_px_left -= 4; + } LOG_DBG( "braille: after adjusting: " @@ -2000,7 +2003,7 @@ draw_braille(struct buf *buf, wchar_t wc) 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(x_px_left <= 1 || y_px_left <= 1); xassert(2 * x_margin + 2 * w + x_spacing <= buf->width); xassert(2 * y_margin + 4 * w + 3 * y_spacing <= buf->height); -- cgit v1.3 From 705b5d786ca369f8c9d9ea2186ac77dffec6e809 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 19 Sep 2021 11:49:49 +0200 Subject: box-drawing: repair debug logs --- box-drawing.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'box-drawing.c') diff --git a/box-drawing.c b/box-drawing.c index f1789f64..887aa0c9 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -1969,7 +1969,7 @@ draw_braille(struct buf *buf, wchar_t wc) "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); + w, x_px_left, y_px_left); /* First, try hard to ensure the DOT width is non-zero */ if (x_px_left >= 2 && y_px_left >= 4 && w == 0) { @@ -2001,7 +2001,7 @@ draw_braille(struct buf *buf, wchar_t wc) "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); + w, x_px_left, y_px_left); xassert(x_px_left <= 1 || y_px_left <= 1); xassert(2 * x_margin + 2 * w + x_spacing <= buf->width); @@ -2889,8 +2889,7 @@ box_drawing(const struct terminal *term, wchar_t wc) }, }; - LOG_DBG("LIGHT=%d, HEAVY=%d", - _thickness(&buf, LIGHT), _thickness(&buf, HEAVY)); + LOG_DBG("LIGHT=%d, HEAVY=%d", buf.thickness[LIGHT], buf.thickness[HEAVY]); draw_glyph(&buf, wc); -- cgit v1.3 From fb77637eb96a71de48b49e40fbe7dd71ec85bcbf Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 19 Sep 2021 11:58:34 +0200 Subject: term: only scale using DPI if *all* monitors have a scaling factor or one With dpi-aware=auto (the default), scale fonts using DPI *only* if *all* available monitors have a scaling factor of one. The idea is this: if a user, with multiple monitors, have enabled scaling on *at least* one monitor, he/she has most likely done so to match the size of his/hers other monitors. For example, if the user has one monitor with a scaling factor of one, and another one with a scaling factor of two, he/she expects things to be twice as large on the second monitor. If we (foot) scale using DPI on the first monitor, and using the scaling factor on the second monitor, foot will *not* look twice as big on the second monitor (this was the old behavior of dpi-aware=auto). Part of #714 --- CHANGELOG.md | 3 +++ README.md | 6 +++--- box-drawing.c | 4 ++-- doc/foot.ini.5.scd | 6 ++++-- terminal.c | 59 +++++++++++++++++++++++++++++++++++++----------------- terminal.h | 3 +-- wayland.c | 5 +++++ 7 files changed, 59 insertions(+), 27 deletions(-) (limited to 'box-drawing.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index d229c4ec..c08b29c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,9 @@ still exported, like before. * The default install location for the terminfo definitions have been changed back to `${datadir}/terminfo`. +* `dpi-aware=auto`: fonts are now scaled using the monitor’s DPI only + when **all** monitors have a scaling factor of one + (https://codeberg.org/dnkl/foot/issues/714). ### Deprecated diff --git a/README.md b/README.md index 93d783c4..d90302f8 100644 --- a/README.md +++ b/README.md @@ -347,9 +347,9 @@ This is not how it is meant to be. Fonts are measured in _point sizes_ all mediums, be it printers or monitors, regardless of their DPI. Foot’s default behavior is to use the monitor’s DPI to size fonts when -output scaling has been disabled. On monitors where output scaling has -been enabled, fonts will instead be sized using the scaling -factor. +output scaling has been disabled on **all** monitors. If at least one +monitor has output scaling enabled, fonts will instead by sized using +the scaling factor. This can be changed to either **always** use the monitor’s DPI (regardless of scaling factor), or to **never** use it, with the diff --git a/box-drawing.c b/box-drawing.c index 887aa0c9..3f8a38ef 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -2837,8 +2837,8 @@ box_drawing(const struct terminal *term, wchar_t wc) abort(); } - double dpi = term_font_sized_by_dpi(term, term->scale) ? term->font_dpi : 96.; - double scale = term_font_sized_by_scale(term, term->scale) ? term->scale : 1.; + double dpi = term->font_is_sized_by_dpi ? term->font_dpi : 96.; + double scale = term->font_is_sized_by_dpi ? 1. : term->scale; double cell_size = sqrt(pow(term->cell_width, 2) + pow(term->cell_height, 2)); int base_thickness = diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index ea4779c5..6bc93cc4 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -161,8 +161,10 @@ in this order: scaling factor *does* double the font size. Finally, if set to *auto*, fonts will be sized using the monitor's - DPI on monitors with a scaling factor of 1, but otherwise using - the scaling factor. + DPI if _all_ monitors have a scaling factor of 1. If at least one + monitor as a scaling factor larger than 1 (regardless of whether + the foot window is mapped on that monitor or not), fonts will be + scaled using the scaling factor. Note that this option typically does not work with bitmap fonts, which only contains a pre-defined set of sizes, and cannot be diff --git a/terminal.c b/terminal.c index e6837977..2738291d 100644 --- a/terminal.c +++ b/terminal.c @@ -813,25 +813,48 @@ get_font_subpixel(const struct terminal *term) return FCFT_SUBPIXEL_DEFAULT; } -bool -term_font_sized_by_dpi(const struct terminal *term, int scale) +static bool +term_font_size_by_dpi(const struct terminal *term) { - return term->conf->dpi_aware == DPI_AWARE_YES || - (term->conf->dpi_aware == DPI_AWARE_AUTO && scale <= 1); -} + switch (term->conf->dpi_aware) { + case DPI_AWARE_YES: return true; + case DPI_AWARE_NO: return false; -bool -term_font_sized_by_scale(const struct terminal *term, int scale) -{ - return !term_font_sized_by_dpi(term, scale); + case DPI_AWARE_AUTO: + /* + * Scale using DPI if all monitors have a scaling factor or 1. + * + * The idea is this: if a user, with multiple monitors, have + * enabled scaling on at least one monitor, then he/she has + * most likely done so to match the size of his/hers other + * monitors. + * + * I.e. if the user has one monitor with a scaling factor of + * one, and another with a scaling factor of two, he/she + * expects things to be twice as large on the second + * monitor. + * + * If we (foot) scale using DPI on the first monitor, and + * using the scaling factor on the second monitor, foot will + * *not* look twice as big on the second monitor. + */ + tll_foreach(term->wl->monitors, it) { + const struct monitor *mon = &it->item; + if (mon->scale > 1) + return false; + } + return true; + } + + BUG("unhandled DPI awareness value"); } int term_pt_or_px_as_pixels(const struct terminal *term, const struct pt_or_px *pt_or_px) { - double scale = term_font_sized_by_scale(term, term->scale) ? term->scale : 1.; - double dpi = term_font_sized_by_dpi(term, term->scale) ? term->font_dpi : 96.; + double scale = !term->font_is_sized_by_dpi ? term->scale : 1.; + double dpi = term->font_is_sized_by_dpi ? term->font_dpi : 96.; return pt_or_px->px == 0 ? round(pt_or_px->pt * scale * dpi / 72) @@ -878,8 +901,7 @@ reload_fonts(struct terminal *term) bool use_px_size = term->font_sizes[i][j].px_size > 0; char size[64]; - const int scale = - term_font_sized_by_scale(term, term->scale) ? term->scale : 1; + const int scale = term->font_is_sized_by_dpi ? 1 : term->scale; if (use_px_size) snprintf(size, sizeof(size), ":pixelsize=%d", @@ -914,7 +936,7 @@ reload_fonts(struct terminal *term) const size_t count_bold_italic = custom_bold_italic ? counts[3] : counts[0]; const char **names_bold_italic = (const char **)(custom_bold_italic ? names[3] : names[0]); - const bool use_dpi = term_font_sized_by_dpi(term, term->scale); + const bool use_dpi = term->font_is_sized_by_dpi; char *attrs[4] = {NULL}; int attr_len[4] = {-1, -1, -1, -1}; /* -1, so that +1 (below) results in 0 */ @@ -2008,8 +2030,8 @@ term_font_dpi_changed(struct terminal *term, int old_scale) float dpi = get_font_dpi(term); xassert(term->scale > 0); - bool was_scaled_using_dpi = term_font_sized_by_dpi(term, old_scale); - bool will_scale_using_dpi = term_font_sized_by_dpi(term, term->scale); + bool was_scaled_using_dpi = term->font_is_sized_by_dpi; + bool will_scale_using_dpi = term_font_size_by_dpi(term); bool need_font_reload = was_scaled_using_dpi != will_scale_using_dpi || @@ -2019,15 +2041,16 @@ term_font_dpi_changed(struct terminal *term, int old_scale) if (need_font_reload) { LOG_DBG("DPI/scale change: DPI-awareness=%s, " - "DPI: %.2f -> %.2f, scale: %d, " + "DPI: %.2f -> %.2f, scale: %d -> %d, " "sizing font based on monitor's %s", term->conf->dpi_aware == DPI_AWARE_AUTO ? "auto" : term->conf->dpi_aware == DPI_AWARE_YES ? "yes" : "no", - term->font_dpi, dpi, term->scale, + term->font_dpi, dpi, old_scale, term->scale, will_scale_using_dpi ? "DPI" : "scaling factor"); } term->font_dpi = dpi; + term->font_is_sized_by_dpi = will_scale_using_dpi; if (!need_font_reload) return true; diff --git a/terminal.h b/terminal.h index 028b4844..b3b4a90e 100644 --- a/terminal.h +++ b/terminal.h @@ -329,6 +329,7 @@ struct terminal { struct config_font *font_sizes[4]; struct pt_or_px font_line_height; float font_dpi; + bool font_is_sized_by_dpi; int16_t font_x_ofs; int16_t font_y_ofs; enum fcft_subpixel font_subpixel; @@ -660,8 +661,6 @@ bool term_font_size_reset(struct terminal *term); bool term_font_dpi_changed(struct terminal *term, int old_scale); void term_font_subpixel_changed(struct terminal *term); -bool term_font_sized_by_dpi(const struct terminal *term, int scale); -bool term_font_sized_by_scale(const struct terminal *term, int scale); int term_pt_or_px_as_pixels( const struct terminal *term, const struct pt_or_px *pt_or_px); diff --git a/wayland.c b/wayland.c index 911694ff..49c8ed92 100644 --- a/wayland.c +++ b/wayland.c @@ -340,6 +340,11 @@ update_terms_on_monitor(struct monitor *mon) tll_foreach(wayl->terms, it) { struct terminal *term = it->item; + if (term->conf->dpi_aware == DPI_AWARE_AUTO) { + update_term_for_output_change(term); + continue; + } + tll_foreach(term->window->on_outputs, it2) { if (it2->item == mon) { update_term_for_output_change(term); -- cgit v1.3