render: handle cairo scaled font ourselves
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Eklöf · committed by Daniel Eklöf · parent bf6a3af · Browse files · 3 files·+50 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+50 added−26 removed
- other:+50 added−26 removed
Provenance
- Commit
bf7a675- Tree
2052632- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
bf6a3af
3 files changed+50 added−26 removed
modified:main.c+7 added-9 removed
73 unmodified lines74 75 struct context {76 bool keep_running;77 struct wayland wl;78+ struct render *render;79 80 struct prompt prompt;80-81 application_list_t applications;82 83 struct match *matches;84 size_t match_count;2 unmodified lines87 88 static const int width = 500;89 static const int height = 300;90 91-static double font_size;92-static cairo_scaled_font_t *scaled_font;93-91 static void refresh(const struct context *c);92 static void update_matches(struct context *c);93 94 static void582 unmodified lines677 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);678 cairo_rectangle(buf->cairo, 0, 0, width, height);679 cairo_stroke(buf->cairo);680 684- render_prompt(buf, scaled_font, font_size, &c->prompt);685- render_match_list(buf, scaled_font, font_size,686- c->matches, c->match_count, c->selected);681+ render_prompt(c->render, buf, &c->prompt);682+ render_match_list(c->render, buf, c->matches, c->match_count, c->selected);683 684 cairo_surface_flush(buf->cairo_surface);685 wl_surface_attach(c->wl.surface, buf->wl_buf, 0, 0);686 wl_surface_damage(c->wl.surface, 0, 0, buf->width, buf->height);105 unmodified lines792 /* Trigger a 'configure' event, after which we'll have the width */793 wl_surface_commit(c.wl.surface);794 wl_display_roundtrip(c.wl.display);795 800- const char *font_name = "Dina:pixelsize=9";801- scaled_font = font_from_name(font_name, &font_size);796+ const char *const font_name = "Dina:pixelsize=9";797+ c.render = render_init(font_name);798+799 refresh(&c);800 801 wl_display_dispatch_pending(c.wl.display);802 wl_display_flush(c.wl.display);13 unmodified lines816 wl_display_dispatch(c.wl.display);817 }818 819 out:820+ render_destroy(c.render);821 shm_fini();822 823 free(c.prompt.text);824 tll_foreach(c.applications, it) {46 unmodified linesmodified:render.c+35 added-12 removed
1 unmodified line2 3 #include <stdlib.h>4 #include <string.h>5 6+#include "font.h"7+8+struct render {9+ cairo_scaled_font_t *scaled_font;10+ double font_size;11+};12+13 void7-render_prompt(struct buffer *buf,8- cairo_scaled_font_t *scaled_font, double font_size,14+render_prompt(const struct render *render, struct buffer *buf,15 const struct prompt *prompt)16 {17 const size_t x = 20;18 const size_t y_base = 0;13- const size_t y_advance = font_size + 10; /* TODO: how much "extra" is "right"? */19+ const size_t y_advance = render->font_size + 10; /* TODO: how much "extra" is "right"? */20 size_t y = y_base;21 22 const char *const text = prompt->text;23 3 unmodified lines27 int num_glyphs = 0;28 int num_clusters = 0;29 30 cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(25- scaled_font, 0, 0, text, -1, &glyphs, &num_glyphs,31+ render->scaled_font, 0, 0, text, -1, &glyphs, &num_glyphs,32 &clusters, &num_clusters, &cluster_flags);33 assert(cr_status == CAIRO_STATUS_SUCCESS);34 35 cairo_text_extents_t extents;36 cairo_scaled_font_glyph_extents(31- scaled_font, glyphs, num_glyphs, &extents);37+ render->scaled_font, glyphs, num_glyphs, &extents);38 39 for (int i = 0; i < num_glyphs; i++) {40 glyphs[i].x += x;41 glyphs[i].y += y +42 ((double)y_advance - extents.height) / 2 - extents.y_bearing;43 }44 39- cairo_set_scaled_font(buf->cairo, scaled_font);45+ cairo_set_scaled_font(buf->cairo, render->scaled_font);46 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);47 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);48 cairo_show_text_glyphs(49 buf->cairo, text, -1, glyphs, num_glyphs,21 unmodified lines71 cairo_stroke(buf->cairo);72 }73 74 void69-render_match_list(struct buffer *buf,70- cairo_scaled_font_t *scaled_font, double font_size,75+render_match_list(const struct render *render, struct buffer *buf,76 const struct match matches[], size_t match_count,77 size_t selected)78 {79 assert(match_count == 0 || selected < match_count);80 81 const size_t x = 20;77- const size_t y_advance = font_size + 10; /* TODO: how much "extra" is "right"? */82+ const size_t y_advance = render->font_size + 10; /* TODO: how much "extra" is "right"? */83 const size_t y_base = 2 + y_advance;84 size_t y = y_base;85 86 for (size_t i = 0; i < match_count; i++) {6 unmodified lines93 int num_glyphs = 0;94 int num_clusters = 0;95 96 cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(92- scaled_font, 0, 0, text, -1, &glyphs, &num_glyphs,97+ render->scaled_font, 0, 0, text, -1, &glyphs, &num_glyphs,98 &clusters, &num_clusters, &cluster_flags);99 assert(cr_status == CAIRO_STATUS_SUCCESS);100 101 cairo_text_extents_t extents;102 cairo_scaled_font_glyph_extents(98- scaled_font, glyphs, num_glyphs, &extents);103+ render->scaled_font, glyphs, num_glyphs, &extents);104 105 for (int j = 0; j < num_glyphs; j++) {106 glyphs[j].x += x;107 glyphs[j].y += y +13 unmodified lines121 cairo_rectangle(buf->cairo, x - 2, y + 2, buf->width - 2 * (x - 2), y_advance - 4);122 cairo_fill(buf->cairo);123 }124 120- cairo_set_scaled_font(buf->cairo, scaled_font);125+ cairo_set_scaled_font(buf->cairo, render->scaled_font);126 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);127 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);128 cairo_show_text_glyphs(129 buf->cairo, text, -1, glyphs, num_glyphs,4 unmodified lines134 135 y += y_advance;136 }137 }138+139+struct render *140+render_init(const char *font_name)141+{142+ struct render *render = calloc(1, sizeof(*render));143+ render->scaled_font = font_from_name(font_name, &render->font_size);144+ return render;145+}146+147+void148+render_destroy(struct render *render)149+{150+ if (render == NULL)151+ return;152+153+ cairo_scaled_font_destroy(render->scaled_font);154+ free(render);155+}modified:render.h+8 added-5 removed
9 unmodified lines10 char *text;11 size_t cursor;12 };13 14-void render_prompt(struct buffer *buf,15- cairo_scaled_font_t *scaled_font, double font_size,16- const struct prompt *prompt);14+struct render;15+struct render *render_init(const char *font_name);16+void render_destroy(struct render *render);17 18-void render_match_list(struct buffer *buf,19- cairo_scaled_font_t *scaled_font, double font_size,18+void render_prompt(19+ const struct render *render, struct buffer *buf,20+ const struct prompt *prompt);21+22+void render_match_list(const struct render *render, struct buffer *buf,23 const struct match matches[], size_t match_count,24 size_t selected);