diff --git a/zathura/page-widget.c b/zathura/page-widget.c index a0d59ae..2be205a 100644 --- a/zathura/page-widget.c +++ b/zathura/page-widget.c @@ -297,12 +297,90 @@ zathura_page_widget_finalize(GObject* object) G_OBJECT_CLASS(zathura_page_widget_parent_class)->finalize(object); } +static void +set_font_from_property(cairo_t* cairo, zathura_t* zathura, cairo_font_weight_t weight) +{ + if (zathura == NULL) { + return; + } + + /* get user font description */ + char* font = NULL; + girara_setting_get(zathura->ui.session, "font", &font); + if (font == NULL) { + return; + } + + /* use pango to extract font family and size */ + PangoFontDescription* descr = pango_font_description_from_string(font); + + const char* family = pango_font_description_get_family(descr); + + /* get font size: can be points or absolute. + * absolute units: value = 10*PANGO_SCALE = 10 (unscaled) device units (logical pixels) + * point units: value = 10*PANGO_SCALE = 10 points = 10*(font dpi config / 72) device units */ + double size = pango_font_description_get_size(descr) / PANGO_SCALE; + + /* convert point size to device units */ + if (!pango_font_description_get_size_is_absolute(descr)) { + double font_dpi = 96.0; + if (zathura->ui.session != NULL) { + if (gtk_widget_has_screen(zathura->ui.session->gtk.view)) { + GdkScreen* screen = gtk_widget_get_screen(zathura->ui.session->gtk.view); + font_dpi = gdk_screen_get_resolution(screen); + } + } + size = size * font_dpi / 72; + } + + cairo_select_font_face(cairo, family, CAIRO_FONT_SLANT_NORMAL, weight); + cairo_set_font_size(cairo, size); + + pango_font_description_free(descr); + g_free(font); +} + +static cairo_text_extents_t +get_text_extents(const char* string, zathura_t* zathura, cairo_font_weight_t weight) { + cairo_text_extents_t text = {0,}; + + if (zathura == NULL) { + return text; + } + + /* make dummy surface to satisfy API requirements */ + cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 0, 0); + if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { + return text; + } + + cairo_t* cairo = cairo_create(surface); + if (cairo_status(cairo) != CAIRO_STATUS_SUCCESS) { + cairo_surface_destroy(surface); + return text; + } + + set_font_from_property(cairo, zathura, weight); + cairo_text_extents(cairo, string, &text); + + /* add some margin (for some reason the reported extents can be a bit short) */ + text.width += 6; + text.height += 2; + + cairo_destroy(cairo); + cairo_surface_destroy(surface); + + return text; +} + static void zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec) { ZathuraPage* pageview = ZATHURA_PAGE(object); zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(pageview); + cairo_text_extents_t text; + switch (prop_id) { case PROP_PAGE: priv->page = g_value_get_pointer(value); @@ -320,10 +398,19 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v } if (priv->links.retrieved == TRUE && priv->links.list != NULL) { + /* get size of text that should be large enough for every link hint */ + text = get_text_extents("888", priv->zathura, CAIRO_FONT_WEIGHT_BOLD); + GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) if (link != NULL) { + /* redraw link area */ zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link)); redraw_rect(pageview, &rectangle); + + /* also redraw area for link hint */ + rectangle.x2 = rectangle.x1 + text.width; + rectangle.y1 = rectangle.y2 - text.height; + redraw_rect(pageview, &rectangle); } GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link); } @@ -494,20 +581,12 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) return FALSE; } - /* draw rectangles */ - char* font = NULL; - girara_setting_get(priv->zathura->ui.session, "font", &font); + /* draw links */ + set_font_from_property(cairo, priv->zathura, CAIRO_FONT_WEIGHT_BOLD); float transparency = 0.5; girara_setting_get(priv->zathura->ui.session, "highlight-transparency", &transparency); - if (font != NULL) { - cairo_select_font_face(cairo, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); - } - - g_free(font); - - /* draw links */ if (priv->links.draw == true && priv->links.n != 0) { unsigned int link_counter = 0; GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link) @@ -523,7 +602,6 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) /* draw text */ cairo_set_source_rgba(cairo, 0, 0, 0, 1); - cairo_set_font_size(cairo, 10); cairo_move_to(cairo, rectangle.x1 + 1, rectangle.y2 - 1); char* link_number = g_strdup_printf("%i", priv->links.offset + ++link_counter); cairo_show_text(cairo, link_number);