Skip to content

Commit

Permalink
[Textbox] Use FontMetrics font height (if available) to get height
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveDavenport committed Oct 3, 2024
1 parent f15aae9 commit 186b0e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
30 changes: 27 additions & 3 deletions source/widgets/textbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "helper.h"
#include "keyb.h"
#include "mode.h"
#include "timings.h"
#include "view.h"
#include "widgets/textbox.h"
#include <ctype.h>
Expand Down Expand Up @@ -140,6 +141,22 @@ static void textbox_initialize_font(textbox *tb) {
PangoRectangle rect;
pango_layout_get_pixel_extents(layout, NULL, &rect);
tbfc->height = rect.y + rect.height;

// Try to find height from font. Might be slow?
TICK_N("Get font height");
PangoFont *font = pango_context_load_font(p_context, tbfc->pfd);
if (font) {
PangoFontMetrics *fm = pango_font_get_metrics(font, NULL);
if (fm) {
int h = pango_font_metrics_get_height(fm) / PANGO_SCALE;
if (h > 0) {
tbfc->height = h;
}
pango_font_metrics_unref(fm);
}
g_object_unref(font);
}
TICK_N("Get font height");
g_object_unref(layout);

// Cast away consts. (*yuck*) because table_insert does not know it is
Expand Down Expand Up @@ -502,9 +519,10 @@ static void textbox_draw(widget *wid, cairo_t *draw) {
// use text color as fallback for themes that don't specify the cursor color
rofi_theme_get_color(WIDGET(tb), "text-color", draw);

{ int rem =
MAX(0, tb->widget.w - widget_padding_get_padding_width(WIDGET(tb)) -
line_width - dot_offset);
{
int rem =
MAX(0, tb->widget.w - widget_padding_get_padding_width(WIDGET(tb)) -
line_width - dot_offset);
switch (pango_layout_get_alignment(tb->layout)) {
case PANGO_ALIGN_CENTER:
x = rem * (tb->xalign - 0.5);
Expand Down Expand Up @@ -942,6 +960,12 @@ void textbox_set_pango_context(const char *font, PangoContext *p) {
PangoRectangle rect;
pango_layout_get_pixel_extents(layout, NULL, &rect);
tbfc->height = rect.y + rect.height;
if (tbfc->metrics) {
int h = pango_font_metrics_get_height(tbfc->metrics) / PANGO_SCALE;
if (h > 0) {
tbfc->height = h;
}
}
g_object_unref(layout);
tbfc_default = tbfc;

Expand Down
6 changes: 6 additions & 0 deletions test/textbox-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ int rofi_is_in_dmenu_mode = 0;

ThemeWidget *rofi_configuration = NULL;

void rofi_timings_tick(G_GNUC_UNUSED const char *file,
G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line,
G_GNUC_UNUSED char const *msg);
void rofi_timings_tick(G_GNUC_UNUSED const char *file,
G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line,
G_GNUC_UNUSED char const *msg) {}
uint32_t rofi_icon_fetcher_query(G_GNUC_UNUSED const char *name,
G_GNUC_UNUSED const int size) {
return 0;
Expand Down
7 changes: 7 additions & 0 deletions test/widget-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include "display.h"
#include "glibconfig.h"
#include "rofi-icon-fetcher.h"
#include "rofi.h"
#include "xcb.h"
Expand Down Expand Up @@ -58,6 +59,12 @@ uint32_t rofi_icon_fetcher_query_advanced(G_GNUC_UNUSED const char *name,
G_GNUC_UNUSED const int hsize) {
return 0;
}
void rofi_timings_tick(G_GNUC_UNUSED const char *file,
G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line,
G_GNUC_UNUSED char const *msg);
void rofi_timings_tick(G_GNUC_UNUSED const char *file,
G_GNUC_UNUSED char const *str, G_GNUC_UNUSED int line,
G_GNUC_UNUSED char const *msg) {}

cairo_surface_t *rofi_icon_fetcher_get(G_GNUC_UNUSED const uint32_t uid) {
return NULL;
Expand Down

0 comments on commit 186b0e7

Please sign in to comment.