Skip to content
Myr

render: use *font* extents, not *text* extents

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 07ee6ed · Browse files · 2 files+99 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+99 added−54 removed

  • other:+99 added−54 removed

2 files+99 added−54 removed

modified:main.c+1 added-1 removed
839 unmodified lines
840 cairo_paint(buf->cairo);841842 /* Border */843 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);844 cairo_set_line_width(buf->cairo, 2);844 cairo_set_line_width(buf->cairo, 2 * 1);845 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);846 cairo_rectangle(buf->cairo, 0, 0, width, height);847 cairo_stroke(buf->cairo);848
271 unmodified lines
modified:render.c+98 added-53 removed
11 unmodified lines
12 cairo_scaled_font_t *regular_font;13 double font_size;14};1516static const double x_margin = 20;17static const double y_margin = 4;18static const double border_size = 1;1920void21render_prompt(const struct render *render, struct buffer *buf,22 const struct prompt *prompt)23{20 const size_t x = 20;21 const size_t y_base = 0;22 const size_t y_advance = render->font_size + 10; /* TODO: how much "extra" is "right"? */23 size_t y = y_base;24 cairo_set_scaled_font(buf->cairo, render->regular_font);2526 cairo_font_extents_t fextents;27 cairo_font_extents(buf->cairo, &fextents);2829 const char *const text = prompt->text;3031 cairo_glyph_t *glyphs = NULL;32 cairo_text_cluster_t *clusters = NULL;
13 unmodified lines
46 cairo_scaled_font_glyph_extents(47 render->regular_font, glyphs, num_glyphs, &extents);4849 for (int i = 0; i < num_glyphs; i++) {46 glyphs[i].x += x;47 glyphs[i].y += y +48 ((double)y_advance - extents.height) / 2 - extents.y_bearing;50 glyphs[i].x += border_size + x_margin;51 glyphs[i].y += border_size + y_margin + fextents.height - fextents.descent;52 }5351 cairo_set_scaled_font(buf->cairo, render->regular_font);54 cairo_set_source_rgba(buf->cairo, 1.0, 1.0, 1.0, 1.0);55 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);56 cairo_show_text_glyphs(57 buf->cairo, text, -1, glyphs, num_glyphs,
16 unmodified lines
74 }7576 int cursor_x;77 if (cursor_at_glyph == 0)76 cursor_x = x;78 cursor_x = x_margin;79 else if (cursor_at_glyph >= num_glyphs)78 cursor_x = x + extents.x_advance;80 cursor_x = x_margin + extents.x_advance;81 else82 cursor_x = glyphs[cursor_at_glyph].x;8384 cairo_set_line_width(buf->cairo, 1);83 cairo_move_to(buf->cairo, cursor_x - 0.5, y_base + 4);84 cairo_line_to(buf->cairo, cursor_x - 0.5, y_base + y_advance - 4);85 cairo_move_to(buf->cairo, cursor_x - 0.5, border_size + y_margin);86 cairo_line_to(buf->cairo, cursor_x - 0.5, border_size + y_margin + fextents.height);87 cairo_stroke(buf->cairo);8889 cairo_glyph_free(glyphs);90 cairo_text_cluster_free(clusters);9190 cairo_set_line_width(buf->cairo, 1);91 cairo_move_to(buf->cairo, 0, y_base + y_advance + 0.5);92 cairo_line_to(buf->cairo, buf->width, y_base + y_advance + 0.5);92 cairo_set_line_width(buf->cairo, border_size);93 cairo_move_to(buf->cairo, 0, border_size + 2 * y_margin + fextents.height + border_size / 2);94 cairo_line_to(buf->cairo, buf->width, border_size + 2 * y_margin + fextents.height + border_size / 2);95 cairo_stroke(buf->cairo);96}9798static void97render_text(struct buffer *buf, int *x, int y, double y_advance,98 const char *text, size_t len, cairo_scaled_font_t *font, uint32_t color)99render_glyphs(struct buffer *buf, uint32_t color, const cairo_glyph_t *glyphs, int num_glyphs)100{101 double red, green, blue, alpha;102 red = (double)((color >> 24) & 0xff) / 255.0;103 green = (double)((color >> 16) & 0xff) / 255.0;104 blue = (double)((color >> 8) & 0xff) / 255.0;105 alpha = (double)((color >> 0) & 0xff) / 255.0;106107 cairo_set_source_rgba(buf->cairo, red, green, blue, alpha);108 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);109 cairo_show_glyphs(buf->cairo, glyphs, num_glyphs);110}111112static void113render_match_text(struct buffer *buf, double *x, double y,114 const char *text, ssize_t start, size_t length,115 cairo_scaled_font_t *font,116 uint32_t regular_color, uint32_t match_color)117{118 cairo_set_scaled_font(buf->cairo, font);119120 cairo_glyph_t *glyphs = NULL;121 cairo_text_cluster_t *clusters = NULL;122 cairo_text_cluster_flags_t cluster_flags;123 int num_glyphs = 0;124 int num_clusters = 0;125126 cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(107 font, 0, 0, text, len, &glyphs, &num_glyphs,127 font, 0, 0, text, -1, &glyphs, &num_glyphs,128 &clusters, &num_clusters, &cluster_flags);129130 if (cr_status != CAIRO_STATUS_SUCCESS) {131 LOG_ERR("cairo failure: TODO: error message");
4 unmodified lines
136 cairo_scaled_font_glyph_extents(font, glyphs, num_glyphs, &extents);137138 for (int j = 0; j < num_glyphs; j++) {139 glyphs[j].x += *x;120 glyphs[j].y += y + (y_advance - extents.height) / 2 - extents.y_bearing;140 glyphs[j].y += y;141 }142123 double red, green, blue, alpha;124 red = (double)((color >> 24) & 0xff) / 255.0;125 green = (double)((color >> 16) & 0xff) / 255.0;126 blue = (double)((color >> 8) & 0xff) / 255.0;127 alpha = (double)((color >> 0) & 0xff) / 255.0;143 if (start >= 0) {144129 cairo_set_scaled_font(buf->cairo, font);130 cairo_set_source_rgba(buf->cairo, red, green, blue, alpha);131 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);132 cairo_show_text_glyphs(133 buf->cairo, text, len, glyphs, num_glyphs,134 clusters, num_clusters, cluster_flags);145 /*146 * start/length are in bytes, but we need to know which147 * *glyph* the match starts at, and how many *glyphs* the match148 * is. For regular ascii, these will always be the same, but149 * for multibyte UTF-8 characters not so.150 */151152 /* Calculate at which *glyph* the matching part starts */153 size_t idx = 0;154 size_t glyph_start = 0;155 while (idx < start) {156 int clen = mblen(&text[idx], MB_CUR_MAX);157 if (clen < 0) {158 LOG_ERRNO("match text: %s", text);159 break;160 }161162 glyph_start++;163 idx += clen;164 }165166 /* Find out how many *glyphs* that match */167 size_t glyph_match_count = 0;168 while (idx < start + length) {169 int clen = mblen(&text[idx], MB_CUR_MAX);170 if (clen < 0) {171 LOG_ERRNO("match text: %s", text);172 break;173 }174175 glyph_match_count++;176 idx += clen;177 }178179 render_glyphs(buf, regular_color, &glyphs[0], glyph_start);180 render_glyphs(buf, match_color, &glyphs[start], glyph_match_count);181 render_glyphs(buf, regular_color, &glyphs[start + length], num_glyphs - glyph_start - glyph_match_count);182 } else183 render_glyphs(buf, regular_color, glyphs, num_glyphs);184185 cairo_glyph_free(glyphs);186 cairo_text_cluster_free(clusters);187188 *x += extents.width;189}190142static void143render_match_text(struct buffer *buf, int *x, int y, double y_advance,144 const char *text, ssize_t start, size_t length,145 cairo_scaled_font_t *font,146 uint32_t regular_color, uint32_t match_color)147{148 if (start >= 0) {149 render_text(buf, x, y, y_advance, &text[0], start, font, regular_color);150 render_text(buf, x, y, y_advance, &text[start], length, font, match_color);151 render_text(buf, x, y, y_advance, &text[start + length],152 strlen(text) - (start + length), font, regular_color);153 } else154 render_text(buf, x, y, y_advance, text, strlen(text), font, regular_color);155}156191void192render_match_list(const struct render *render, struct buffer *buf,193 const struct match matches[], size_t match_count,194 size_t match_length, size_t selected)195{196 assert(match_count == 0 || selected < match_count);197164 const size_t x = 20;165 const size_t y_advance = render->font_size + 10; /* TODO: how much "extra" is "right"? */166 const size_t y_base = 2 + y_advance;167 size_t y = y_base;198199 cairo_set_scaled_font(buf->cairo, render->regular_font);200201 cairo_font_extents_t fextents;202 cairo_font_extents(buf->cairo, &fextents);203204 const double row_height = 2 * y_margin + fextents.height;205 const double first_row = 2 * border_size + 2 * y_margin + fextents.height;206 const double sel_margin = x_margin / 3;207208 double y = first_row + y_margin + fextents.height - fextents.descent;209210 for (size_t i = 0; i < match_count; i++) {211 const struct match *match = &matches[i];212213 /* Hightlight selected entry */214 if (i == selected) {215 cairo_set_source_rgba(buf->cairo, 0.157, 0.157, 0.157, 0.9);216 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);176 cairo_rectangle(buf->cairo, x - 2, y + 2, buf->width - 2 * (x - 2), y_advance - 4);217 cairo_rectangle(buf->cairo,218 x_margin - sel_margin,219 first_row + i * row_height,220 buf->width - 2 * (x_margin - sel_margin),221 fextents.height + 2 * y_margin);222 cairo_fill(buf->cairo);223 }224225 /* Application title */181 int cur_x = x;226 double cur_x = border_size + x_margin;227 render_match_text(183 buf, &cur_x, y, y_advance,228 buf, &cur_x, y,229 match->application->title, match->start_title, match_length,230 render->regular_font, 0xffffffff, 0xcc9393ff);231187 y += y_advance;232 y += 2 * y_margin + fextents.height;233 }234}235236struct render *
16 unmodified lines