diff options
author | Phil Jones <philj56@gmail.com> | 2022-06-23 14:48:35 +0100 |
---|---|---|
committer | Phil Jones <philj56@gmail.com> | 2022-06-23 14:48:35 +0100 |
commit | 128fa39bb62d1a1c627812f15030bf3ffb82100d (patch) | |
tree | e16020dd31faf4a2033ac45a3038981b8c54276c | |
parent | c57d9a2a65725dce8632bea6e539517f6b5e8f7d (diff) |
Remove Cairo scale factor.
The scale factor is now only used to scale font sizes, not all Cairo
drawing operations. This makes pixel-sized options correct.
-rw-r--r-- | src/config.c | 14 | ||||
-rw-r--r-- | src/entry.c | 21 | ||||
-rw-r--r-- | src/entry.h | 3 | ||||
-rw-r--r-- | src/entry_backend/harfbuzz.c | 27 | ||||
-rw-r--r-- | src/entry_backend/harfbuzz.h | 2 | ||||
-rw-r--r-- | src/entry_backend/pango.c | 2 | ||||
-rw-r--r-- | src/entry_backend/pango.h | 2 | ||||
-rw-r--r-- | src/image.h | 1 | ||||
-rw-r--r-- | src/main.c | 12 |
9 files changed, 32 insertions, 52 deletions
diff --git a/src/config.c b/src/config.c index 81f3f2a..79d0fae 100644 --- a/src/config.c +++ b/src/config.c @@ -100,7 +100,7 @@ void config_load(struct tofi *tofi, const char *filename) fclose(fp); config[size] = '\0'; - char *config_copy = strdup(config); + char *config_copy = xstrdup(config); if (!config_copy) { log_error("Failed to malloc second buffer for config file.\n"); goto CLEANUP_ALL; @@ -260,17 +260,17 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const } else if (strcasecmp(option, "selection-color") == 0) { tofi->window.entry.selection_color = parse_color(filename, lineno, value, &err); } else if (strcasecmp(option, "width") == 0) { - tofi->window.width = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width / tofi->window.scale); + tofi->window.width = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); } else if (strcasecmp(option, "height") == 0) { - tofi->window.height = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height / tofi->window.scale); + tofi->window.height = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); } else if (strcasecmp(option, "margin-top") == 0) { - tofi->window.margin_top = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height / tofi->window.scale); + tofi->window.margin_top = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); } else if (strcasecmp(option, "margin-bottom") == 0) { - tofi->window.margin_bottom = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height / tofi->window.scale); + tofi->window.margin_bottom = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); } else if (strcasecmp(option, "margin-left") == 0) { - tofi->window.margin_left = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width / tofi->window.scale); + tofi->window.margin_left = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); } else if (strcasecmp(option, "margin-right") == 0) { - tofi->window.margin_right = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width / tofi->window.scale); + tofi->window.margin_right = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); } else if (strcasecmp(option, "horizontal") == 0) { tofi->window.entry.horizontal = parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "hide-cursor") == 0) { diff --git a/src/entry.c b/src/entry.c index b476e36..b3292ce 100644 --- a/src/entry.c +++ b/src/entry.c @@ -24,11 +24,10 @@ static void rounded_rectangle(cairo_t *cr, uint32_t width, uint32_t height, uint cairo_close_path(cr); } -void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, uint32_t height, uint32_t scale) +void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, uint32_t height) { entry->image.width = width; entry->image.height = height; - entry->image.scale = scale; /* * Create the cairo surfaces and contexts we'll be using. @@ -46,7 +45,6 @@ void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, u height, width * sizeof(uint32_t) ); - cairo_surface_set_device_scale(surface, scale, scale); cairo_t *cr = cairo_create(surface); entry->cairo[0].surface = surface; @@ -59,16 +57,8 @@ void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, u height, width * sizeof(uint32_t) ); - cairo_surface_set_device_scale(entry->cairo[1].surface, scale, scale); entry->cairo[1].cr = cairo_create(entry->cairo[1].surface); - /* - * Cairo drawing operations take the scale into account, - * so we account for that here. - */ - width /= scale; - height /= scale; - /* Draw the background */ struct color color = entry->background_color; @@ -124,7 +114,7 @@ void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, u cairo_clip(cr); /* Setup the backend. */ - entry_backend_init(entry, &width, &height, scale); + entry_backend_init(entry, &width, &height); /* * To avoid performing all this drawing twice, we take a small @@ -181,10 +171,3 @@ void entry_update(struct entry *entry) entry->index = !entry->index; } - -void entry_set_scale(struct entry *entry, uint32_t scale) -{ - entry->image.scale = scale; - cairo_surface_set_device_scale(entry->cairo[0].surface, scale, scale); - cairo_surface_set_device_scale(entry->cairo[1].surface, scale, scale); -} diff --git a/src/entry.h b/src/entry.h index 7ceb215..f1187d3 100644 --- a/src/entry.h +++ b/src/entry.h @@ -59,9 +59,8 @@ struct entry { } border; }; -void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, uint32_t height, uint32_t scale); +void entry_init(struct entry *entry, uint8_t *restrict buffer, uint32_t width, uint32_t height); void entry_destroy(struct entry *entry); void entry_update(struct entry *entry); -void entry_set_scale(struct entry *entry, uint32_t scale); #endif /* ENTRY_H */ diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c index 51f08ea..0a46eed 100644 --- a/src/entry_backend/harfbuzz.c +++ b/src/entry_backend/harfbuzz.c @@ -56,7 +56,7 @@ static void setup_hb_buffer(hb_buffer_t *buffer) * Render a hb_buffer with Cairo, and return the width of the rendered area in * Cairo units. */ -static uint32_t render_hb_buffer(cairo_t *cr, hb_buffer_t *buffer, uint32_t scale) +static uint32_t render_hb_buffer(cairo_t *cr, hb_buffer_t *buffer) { cairo_save(cr); @@ -79,14 +79,12 @@ static uint32_t render_hb_buffer(cairo_t *cr, hb_buffer_t *buffer, uint32_t scal /* * The coordinates returned by HarfBuzz are in 26.6 fixed-point * format, so we divide by 64.0 (2^6) to get floats. - * We also divide by the display's scale factor, to counter - * Cairo's scaling of coordinates and sizes. */ cairo_glyphs[i].index = glyph_info[i].codepoint; - cairo_glyphs[i].x = x + glyph_pos[i].x_offset / 64.0 / scale; - cairo_glyphs[i].y = y - glyph_pos[i].y_offset / 64.0 / scale; - x += glyph_pos[i].x_advance / 64.0 / scale; - y -= glyph_pos[i].y_advance / 64.0 / scale; + cairo_glyphs[i].x = x + glyph_pos[i].x_offset / 64.0; + cairo_glyphs[i].y = y - glyph_pos[i].y_offset / 64.0; + x += glyph_pos[i].x_advance / 64.0; + y -= glyph_pos[i].y_advance / 64.0; } cairo_show_glyphs(cr, cairo_glyphs, glyph_count); @@ -97,7 +95,7 @@ static uint32_t render_hb_buffer(cairo_t *cr, hb_buffer_t *buffer, uint32_t scal double width = 0; for (unsigned int i=0; i < glyph_count; i++) { - width += glyph_pos[i].x_advance / 64.0 / scale; + width += glyph_pos[i].x_advance / 64.0; } return ceil(width); } @@ -105,8 +103,7 @@ static uint32_t render_hb_buffer(cairo_t *cr, hb_buffer_t *buffer, uint32_t scal void entry_backend_init( struct entry *entry, uint32_t *width, - uint32_t *height, - uint32_t scale) + uint32_t *height) { cairo_t *cr = entry->cairo[0].cr; @@ -132,8 +129,8 @@ void entry_backend_init( entry->backend.ft_face, entry->font_size * 64, entry->font_size * 64, - 96 * scale, - 96 * scale); + 96, + 96); if (err) { log_error("Error setting font size: %s\n", get_ft_error_string(err)); @@ -172,7 +169,7 @@ void entry_backend_init( log_debug("Drawing prompt.\n"); hb_buffer_add_utf8(entry->backend.hb_buffer, entry->prompt_text, -1, 0, -1); hb_shape(entry->backend.hb_font, entry->backend.hb_buffer, NULL, 0); - uint32_t prompt_width = render_hb_buffer(cr, buffer, scale); + uint32_t prompt_width = render_hb_buffer(cr, buffer); /* Move and clip so we don't draw over the prompt later */ cairo_translate(cr, prompt_width, 0); @@ -201,7 +198,7 @@ void entry_backend_update(struct entry *entry) setup_hb_buffer(buffer); hb_buffer_add_utf8(buffer, entry->input_mb, -1, 0, -1); hb_shape(entry->backend.hb_font, buffer, NULL, 0); - width = render_hb_buffer(cr, buffer, entry->image.scale); + width = render_hb_buffer(cr, buffer); cairo_font_extents_t font_extents; cairo_font_extents(cr, &font_extents); @@ -223,7 +220,7 @@ void entry_backend_update(struct entry *entry) struct color color = entry->selection_color; cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); } - width = render_hb_buffer(cr, buffer, entry->image.scale); + width = render_hb_buffer(cr, buffer); if (i == entry->selection) { cairo_restore(cr); } diff --git a/src/entry_backend/harfbuzz.h b/src/entry_backend/harfbuzz.h index 499e973..1917632 100644 --- a/src/entry_backend/harfbuzz.h +++ b/src/entry_backend/harfbuzz.h @@ -18,7 +18,7 @@ struct entry_backend { hb_buffer_t *hb_buffer; }; -void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height, uint32_t scale); +void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height); void entry_backend_destroy(struct entry *entry); void entry_backend_update(struct entry *entry); diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c index 990ce55..d645dc9 100644 --- a/src/entry_backend/pango.c +++ b/src/entry_backend/pango.c @@ -5,7 +5,7 @@ #include "../log.h" #include "../nelem.h" -void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height, uint32_t scale) +void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height) { cairo_t *cr = entry->cairo[0].cr; diff --git a/src/entry_backend/pango.h b/src/entry_backend/pango.h index 67b199f..769a992 100644 --- a/src/entry_backend/pango.h +++ b/src/entry_backend/pango.h @@ -10,7 +10,7 @@ struct entry_backend { PangoLayout *layout; }; -void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height, uint32_t scale); +void entry_backend_init(struct entry *entry, uint32_t *width, uint32_t *height); void entry_backend_destroy(struct entry *entry); void entry_backend_update(struct entry *entry); diff --git a/src/image.h b/src/image.h index 090f32d..e3cf86f 100644 --- a/src/image.h +++ b/src/image.h @@ -7,7 +7,6 @@ struct image { uint32_t width; uint32_t height; - uint32_t scale; struct { uint32_t x; uint32_t y; @@ -768,6 +768,9 @@ int main(int argc, char *argv[]) */ parse_args(&tofi, argc, argv); + /* Scale fonts to the correct size. */ + tofi.window.entry.font_size *= tofi.window.scale; + /* * If we were invoked as tofi-run, generate the command list. * Otherwise, just read standard input. @@ -834,8 +837,8 @@ int main(int argc, char *argv[]) -1); zwlr_layer_surface_v1_set_size( tofi.window.zwlr_layer_surface, - tofi.window.width, - tofi.window.height); + tofi.window.width / tofi.window.scale, + tofi.window.height / tofi.window.scale); zwlr_layer_surface_v1_set_margin( tofi.window.zwlr_layer_surface, tofi.window.margin_top, @@ -881,9 +884,8 @@ int main(int argc, char *argv[]) entry_init( &tofi.window.entry, tofi.window.surface.shm_pool_data, - tofi.window.surface.width, - tofi.window.surface.height, - tofi.window.scale); + tofi.window.width, + tofi.window.height); entry_update(&tofi.window.entry); log_unindent(); log_debug("Renderer initialised.\n"); |