From a0c359c24e8aacd16d8f9d4db35f50c985bfdf77 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 7 Jan 2021 11:17:23 +0100 Subject: term: set_fonts: use custom line-height+letter-spacing, if set If the user has configured a custom line-height and/or letter-spacing, use that instead of the font metrics. --- terminal.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 6ba04677..c7383ae2 100644 --- a/terminal.c +++ b/terminal.c @@ -629,10 +629,14 @@ term_set_fonts(struct terminal *term, struct fcft_font *fonts[static 4]) const int old_cell_width = term->cell_width; const int old_cell_height = term->cell_height; - term->cell_width = term->fonts[0]->space_advance.x > 0 - ? term->fonts[0]->space_advance.x : term->fonts[0]->max_advance.x; - term->cell_height = max(term->fonts[0]->height, - term->fonts[0]->ascent + term->fonts[0]->descent); + term->cell_width = term->conf->letter_spacing >= 0 + ? term->conf->letter_spacing + : (term->fonts[0]->space_advance.x > 0 + ? term->fonts[0]->space_advance.x : term->fonts[0]->max_advance.x); + term->cell_height = term->conf->line_height >= 0 + ? term->conf->line_height + : max(term->fonts[0]->height, + term->fonts[0]->ascent + term->fonts[0]->descent); LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); if (term->cell_width < old_cell_width || -- cgit v1.3 From 70cfcf11fb26a1e3954e72550e4c24e0fa43a8b2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 7 Jan 2021 11:46:18 +0100 Subject: config: letter-spacing: make this a relative value 0, the default, means no additional spacing; the cell width is defined by the font metrics. A positive value *adds* to the width from the font metrics, while a negative value *subtracts*. --- config.c | 17 +++++++++++++++-- foot.ini | 2 +- terminal.c | 9 +++++---- 3 files changed, 21 insertions(+), 7 deletions(-) (limited to 'terminal.c') diff --git a/config.c b/config.c index 6af8803b..8f0b334f 100644 --- a/config.c +++ b/config.c @@ -360,6 +360,19 @@ str_to_bool(const char *s) strtoul(s, NULL, 0) > 0; } +static bool +str_to_long(const char *s, int base, long *res) +{ + if (s == NULL) + return false; + + errno = 0; + char *end = NULL; + + *res = strtol(s, &end, base); + return errno == 0 && *end == '\0'; +} + static bool str_to_ulong(const char *s, int base, unsigned long *res) { @@ -567,8 +580,8 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else if (strcmp(key, "letter-spacing") == 0) { - unsigned long spacing; - if (!str_to_ulong(value, 10, &spacing)) { + long spacing; + if (!str_to_long(value, 10, &spacing)) { LOG_AND_NOTIFY_ERR( "%s:%d: [default]: letter-spacing: expected an integer, got '%s'", path, lineno, value); diff --git a/foot.ini b/foot.ini index ed9e5a72..f13d8e63 100644 --- a/foot.ini +++ b/foot.ini @@ -9,7 +9,7 @@ # font-italic= # font-bold-italic= # line-height= -# letter-spacing= +# letter-spacing=0 # horizontal-letter-offset=0 # vertical-letter-offset=0 # dpi-aware=yes diff --git a/terminal.c b/terminal.c index c7383ae2..c23450fb 100644 --- a/terminal.c +++ b/terminal.c @@ -629,10 +629,11 @@ term_set_fonts(struct terminal *term, struct fcft_font *fonts[static 4]) const int old_cell_width = term->cell_width; const int old_cell_height = term->cell_height; - term->cell_width = term->conf->letter_spacing >= 0 - ? term->conf->letter_spacing - : (term->fonts[0]->space_advance.x > 0 - ? term->fonts[0]->space_advance.x : term->fonts[0]->max_advance.x); + term->cell_width = (term->fonts[0]->space_advance.x > 0 + ? term->fonts[0]->space_advance.x + : term->fonts[0]->max_advance.x) + + term->conf->letter_spacing; + term->cell_height = term->conf->line_height >= 0 ? term->conf->line_height : max(term->fonts[0]->height, -- cgit v1.3 From a6fc8b5da4a3bfc9b1d3994797826518103879ac Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 7 Jan 2021 17:00:58 +0100 Subject: config: line-height, letter-spacing: values are in pt by default, but we allow px MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the value is specified without a unit, then the value is assumed to be in points, subject to DPI scaling. The value can optionally have a ‘px’ suffix, in which case the value is treated as a raw pixel count. --- box-drawing.c | 14 +++++++-- config.c | 93 ++++++++++++++++++++++++++++++----------------------------- config.h | 14 ++++++--- render.c | 8 ++--- terminal.c | 29 ++++++++++++++----- terminal.h | 2 ++ 6 files changed, 98 insertions(+), 62 deletions(-) (limited to 'terminal.c') diff --git a/box-drawing.c b/box-drawing.c index 0fa44cd3..6bfaef82 100644 --- a/box-drawing.c +++ b/box-drawing.c @@ -2064,13 +2064,23 @@ box_drawing(const struct terminal *term, wchar_t wc) }; draw_glyph(wc, &buf); + const struct config *conf = term->conf; + + int x_ofs = conf->horizontal_letter_offset.px > 0 + ? conf->horizontal_letter_offset.px + : conf->horizontal_letter_offset.pt * term->font_dpi / 72.; + int y_ofs = conf->vertical_letter_offset.px > 0 + ? conf->vertical_letter_offset.px + : conf->vertical_letter_offset.pt * term->font_dpi / 72.; + + struct fcft_glyph *glyph = xmalloc(sizeof(*glyph)); *glyph = (struct fcft_glyph){ .wc = wc, .cols = 1, .pix = pix, - .x = -term->conf->horizontal_letter_offset, - .y = term->conf->vertical_letter_offset + term->fonts[0]->ascent, + .x = -x_ofs, + .y = y_ofs + term->fonts[0]->ascent, .width = width, .height = height, .advance = { diff --git a/config.c b/config.c index 03474e49..51615389 100644 --- a/config.c +++ b/config.c @@ -360,19 +360,6 @@ str_to_bool(const char *s) strtoul(s, NULL, 0) > 0; } -static bool -str_to_long(const char *s, int base, long *res) -{ - if (s == NULL) - return false; - - errno = 0; - char *end = NULL; - - *res = strtol(s, &end, base); - return errno == 0 && *end == '\0'; -} - static bool str_to_ulong(const char *s, int base, unsigned long *res) { @@ -422,6 +409,40 @@ str_to_color(const char *s, uint32_t *color, bool allow_alpha, return true; } +static bool +str_to_pt_or_px(const char *s, union pt_or_px *res, struct config *conf, + const char *path, int lineno, const char *section, const char *key) +{ + size_t len = s != NULL ? strlen(s) : 0; + if (len >= 2 && s[len - 2] == 'p' && s[len - 1] == 'x') { + errno = 0; + char *end = NULL; + + long value = strtol(s, &end, 10); + if (!(errno == 0 && end == s + len - 2)) { + LOG_AND_NOTIFY_ERR( + "%s:%d: [%s]: %s: " + "expected an integer directly followed by 'px', got '%s'", + path, lineno, section, key, s); + return false; + } + res->pt = 0; + res->px = value; + } else { + double value; + if (!str_to_double(s, &value)) { + LOG_AND_NOTIFY_ERR( + "%s:%d: [%s]: %s: expected a decimal value, got '%s'", + path, lineno, section, key, s); + return false; + } + res->pt = value; + res->px = 0; + } + + return true; +} + static bool parse_section_main(const char *key, const char *value, struct config *conf, const char *path, unsigned lineno) @@ -572,47 +593,29 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else if (strcmp(key, "line-height") == 0) { - unsigned long height; - if (!str_to_ulong(value, 10, &height)) { - LOG_AND_NOTIFY_ERR( - "%s:%d: [default]: line-height: expected an integer, got '%s'", - path, lineno, value); + if (!str_to_pt_or_px(value, &conf->line_height, + conf, path, lineno, "default", "line-height")) return false; - } - conf->line_height = height; } else if (strcmp(key, "letter-spacing") == 0) { - long spacing; - if (!str_to_long(value, 10, &spacing)) { - LOG_AND_NOTIFY_ERR( - "%s:%d: [default]: letter-spacing: expected an integer, got '%s'", - path, lineno, value); + if (!str_to_pt_or_px(value, &conf->letter_spacing, + conf, path, lineno, "default", "letter-spacing")) return false; - } - conf->letter_spacing = spacing; } else if (strcmp(key, "horizontal-letter-offset") == 0) { - long offset; - if (!str_to_long(value, 10, &offset)) { - LOG_AND_NOTIFY_ERR( - "%s:%d: [default]: horizontal-letter-offset: " - "expected an integer, got '%s'", path, lineno, value); + if (!str_to_pt_or_px( + value, &conf->horizontal_letter_offset, + conf, path, lineno, "default", "horizontal-letter-offset")) return false; - } - conf->horizontal_letter_offset = offset; } else if (strcmp(key, "vertical-letter-offset") == 0) { - long offset; - if (!str_to_long(value, 10, &offset)) { - LOG_AND_NOTIFY_ERR( - "%s:%d: [default]: horizontal-letter-offset: " - "expected an integer, got '%s'", path, lineno, value); + if (!str_to_pt_or_px( + value, &conf->horizontal_letter_offset, + conf, path, lineno, "default", "vertical-letter-offset")) return false; - } - conf->vertical_letter_offset = offset; } else if (strcmp(key, "dpi-aware") == 0) { @@ -2039,10 +2042,10 @@ config_load(struct config *conf, const char *conf_path, .bell_action = BELL_ACTION_NONE, .startup_mode = STARTUP_WINDOWED, .fonts = {tll_init(), tll_init(), tll_init(), tll_init()}, - .line_height = -1, - .letter_spacing = -1, - .horizontal_letter_offset = 0, - .vertical_letter_offset = 0, + .line_height = { .pt = 0, .px = -1, }, + .letter_spacing = { .pt = 0, .px = 0, }, + .horizontal_letter_offset = {.pt = 0, .px = 0, }, + .vertical_letter_offset = {.pt = 0, .px = 0, }, .dpi_aware = DPI_AWARE_AUTO, /* DPI-aware when scaling-factor == 1 */ .scrollback = { .lines = 1000, diff --git a/config.h b/config.h index 9b33b37f..e9bc310f 100644 --- a/config.h +++ b/config.h @@ -54,6 +54,12 @@ struct config_mouse_binding { } pipe; }; +/* If px == -1, neither px nor pt is valid. If px == 0, pt is valid, else px */ +union pt_or_px { + int16_t px; + float pt; +}; + struct config { char *term; char *shell; @@ -85,12 +91,12 @@ struct config { config_font_list_t fonts[4]; /* Custom font metrics (-1 = use real font metrics) */ - int16_t line_height; - int16_t letter_spacing; + union pt_or_px line_height; + union pt_or_px letter_spacing; /* Adjusted letter x/y offsets */ - int16_t horizontal_letter_offset; - int16_t vertical_letter_offset; + union pt_or_px horizontal_letter_offset; + union pt_or_px vertical_letter_offset; struct { int lines; diff --git a/render.c b/render.c index 4e531207..51341365 100644 --- a/render.c +++ b/render.c @@ -256,7 +256,7 @@ color_dim_for_search(pixman_color_t *color) static inline int font_baseline(const struct terminal *term) { - return term->conf->vertical_letter_offset + term->fonts[0]->ascent; + return term->font_y_ofs + term->fonts[0]->ascent; } static void @@ -541,7 +541,7 @@ render_cell(struct terminal *term, pixman_image_t *pix, pixman_image_t *clr_pix = pixman_image_create_solid_fill(&fg); if (glyph != NULL) { - const int letter_x_ofs = term->conf->horizontal_letter_offset; + const int letter_x_ofs = term->font_x_ofs; if (unlikely(pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8)) { /* Glyph surface is a pre-rendered image (typically a color emoji...) */ @@ -1693,7 +1693,7 @@ render_osd(struct terminal *term, struct fcft_font *font = term->fonts[0]; pixman_color_t fg = color_hex_to_pixman(_fg); - const int x_ofs = term->conf->horizontal_letter_offset; + const int x_ofs = term->font_x_ofs; for (size_t i = 0; i < wcslen(text); i++) { const struct fcft_glyph *glyph = fcft_glyph_rasterize( @@ -2272,7 +2272,7 @@ render_search_box(struct terminal *term) struct fcft_font *font = term->fonts[0]; const int x_left = width - visible_width + margin; - const int x_ofs = term->conf->horizontal_letter_offset; + const int x_ofs = term->font_x_ofs; int x = x_left; int y = margin; pixman_color_t fg = color_hex_to_pixman(term->colors.table[0]); diff --git a/terminal.c b/terminal.c index c23450fb..676f0cb4 100644 --- a/terminal.c +++ b/terminal.c @@ -629,15 +629,30 @@ term_set_fonts(struct terminal *term, struct fcft_font *fonts[static 4]) const int old_cell_width = term->cell_width; const int old_cell_height = term->cell_height; - term->cell_width = (term->fonts[0]->space_advance.x > 0 - ? term->fonts[0]->space_advance.x - : term->fonts[0]->max_advance.x) - + term->conf->letter_spacing; - - term->cell_height = term->conf->line_height >= 0 - ? term->conf->line_height + const struct config *conf = term->conf; + + term->cell_width = + (term->fonts[0]->space_advance.x > 0 + ? term->fonts[0]->space_advance.x + : term->fonts[0]->max_advance.x) + + (conf->letter_spacing.px > 0 + ? conf->letter_spacing.px + : conf->letter_spacing.pt * term->font_dpi / 72.); + + term->cell_height = conf->line_height.px >= 0 + ? (conf->line_height.px > 0 + ? conf->line_height.px + : conf->line_height.pt * term->font_dpi / 72.) : max(term->fonts[0]->height, term->fonts[0]->ascent + term->fonts[0]->descent); + + term->font_x_ofs = conf->horizontal_letter_offset.px > 0 + ? conf->horizontal_letter_offset.px + : conf->horizontal_letter_offset.pt * term->font_dpi / 72.; + term->font_y_ofs = conf->vertical_letter_offset.px > 0 + ? conf->vertical_letter_offset.px + : conf->vertical_letter_offset.pt * term->font_dpi / 72.; + LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); if (term->cell_width < old_cell_width || diff --git a/terminal.h b/terminal.h index 128694e7..19fff0e2 100644 --- a/terminal.h +++ b/terminal.h @@ -265,6 +265,8 @@ struct terminal { struct config_font *font_sizes[4]; float font_dpi; int font_scale; + int16_t font_x_ofs; + int16_t font_y_ofs; enum fcft_subpixel font_subpixel; /* -- cgit v1.3 From 5ea37adaf407c63d8915a0ace2437bce46cf06cc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 7 Jan 2021 17:17:17 +0100 Subject: term: add pt_or_px_as_pixels() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function takes a ‘union pt_or_px’ and converts its value to a pixel value. --- terminal.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 676f0cb4..a9c98ed5 100644 --- a/terminal.c +++ b/terminal.c @@ -608,6 +608,15 @@ err_sem_destroy: return false; } +static int +pt_or_px_as_pixels(const struct terminal *term, + const union pt_or_px *pt_or_px) +{ + return pt_or_px->px == 0 + ? pt_or_px->pt * term->font_dpi / 72 + : pt_or_px->px; +} + static bool term_set_fonts(struct terminal *term, struct fcft_font *fonts[static 4]) { -- cgit v1.3 From 1830dfca55ff45398339ddd5493d55d02d990ab8 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 7 Jan 2021 17:19:34 +0100 Subject: term: use pt_or_px_as_pixels() --- terminal.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index a9c98ed5..a5537732 100644 --- a/terminal.c +++ b/terminal.c @@ -644,23 +644,15 @@ term_set_fonts(struct terminal *term, struct fcft_font *fonts[static 4]) (term->fonts[0]->space_advance.x > 0 ? term->fonts[0]->space_advance.x : term->fonts[0]->max_advance.x) - + (conf->letter_spacing.px > 0 - ? conf->letter_spacing.px - : conf->letter_spacing.pt * term->font_dpi / 72.); + + pt_or_px_as_pixels(term, &conf->letter_spacing); term->cell_height = conf->line_height.px >= 0 - ? (conf->line_height.px > 0 - ? conf->line_height.px - : conf->line_height.pt * term->font_dpi / 72.) + ? pt_or_px_as_pixels(term, &conf->line_height) : max(term->fonts[0]->height, term->fonts[0]->ascent + term->fonts[0]->descent); - term->font_x_ofs = conf->horizontal_letter_offset.px > 0 - ? conf->horizontal_letter_offset.px - : conf->horizontal_letter_offset.pt * term->font_dpi / 72.; - term->font_y_ofs = conf->vertical_letter_offset.px > 0 - ? conf->vertical_letter_offset.px - : conf->vertical_letter_offset.pt * term->font_dpi / 72.; + term->font_x_ofs = pt_or_px_as_pixels(term, &conf->horizontal_letter_offset); + term->font_y_ofs = pt_or_px_as_pixels(term, &conf->vertical_letter_offset); LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); -- cgit v1.3