Skip to content
Myr

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

3 files+50 added−26 removed

modified:main.c+7 added-9 removed
73 unmodified lines
7475struct context {76 bool keep_running;77 struct wayland wl;78 struct render *render;7980 struct prompt prompt;8081 application_list_t applications;8283 struct match *matches;84 size_t match_count;
2 unmodified lines
8788static const int width = 500;89static const int height = 300;9091static double font_size;92static cairo_scaled_font_t *scaled_font;9391static void refresh(const struct context *c);92static void update_matches(struct context *c);9394static void
582 unmodified lines
677 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);678 cairo_rectangle(buf->cairo, 0, 0, width, height);679 cairo_stroke(buf->cairo);680684 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);683684 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 lines
792 /* 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);795800 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);798799 refresh(&c);800801 wl_display_dispatch_pending(c.wl.display);802 wl_display_flush(c.wl.display);
13 unmodified lines
816 wl_display_dispatch(c.wl.display);817 }818819out:820 render_destroy(c.render);821 shm_fini();822823 free(c.prompt.text);824 tll_foreach(c.applications, it) {
46 unmodified lines
modified:render.c+35 added-12 removed
1 unmodified line
23#include <stdlib.h>4#include <string.h>56#include "font.h"78struct render {9 cairo_scaled_font_t *scaled_font;10 double font_size;11};1213void7render_prompt(struct buffer *buf,8 cairo_scaled_font_t *scaled_font, double font_size,14render_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;2122 const char *const text = prompt->text;23
3 unmodified lines
27 int num_glyphs = 0;28 int num_clusters = 0;2930 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);3435 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);3839 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 }4439 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 lines
71 cairo_stroke(buf->cairo);72}7374void69render_match_list(struct buffer *buf,70 cairo_scaled_font_t *scaled_font, double font_size,75render_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);8081 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;8586 for (size_t i = 0; i < match_count; i++) {
6 unmodified lines
93 int num_glyphs = 0;94 int num_clusters = 0;9596 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);100101 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);104105 for (int j = 0; j < num_glyphs; j++) {106 glyphs[j].x += x;107 glyphs[j].y += y +
13 unmodified lines
121 cairo_rectangle(buf->cairo, x - 2, y + 2, buf->width - 2 * (x - 2), y_advance - 4);122 cairo_fill(buf->cairo);123 }124120 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 lines
134135 y += y_advance;136 }137}138139struct render *140render_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}146147void148render_destroy(struct render *render)149{150 if (render == NULL)151 return;152153 cairo_scaled_font_destroy(render->scaled_font);154 free(render);155}
modified:render.h+8 added-5 removed
9 unmodified lines
10 char *text;11 size_t cursor;12};1314void render_prompt(struct buffer *buf,15 cairo_scaled_font_t *scaled_font, double font_size,16 const struct prompt *prompt);14struct render;15struct render *render_init(const char *font_name);16void render_destroy(struct render *render);1718void render_match_list(struct buffer *buf,19 cairo_scaled_font_t *scaled_font, double font_size,18void render_prompt(19 const struct render *render, struct buffer *buf,20 const struct prompt *prompt);2122void render_match_list(const struct render *render, struct buffer *buf,23 const struct match matches[], size_t match_count,24 size_t selected);