refactor previous commit’s new code into a function, add log message

This commit is contained in:
Michael Stapelberg 2013-11-18 23:17:44 +01:00
parent 1d6450f0e8
commit 5a4efd04c1
1 changed files with 18 additions and 15 deletions

View File

@ -30,6 +30,21 @@ static double pango_font_red;
static double pango_font_green; static double pango_font_green;
static double pango_font_blue; static double pango_font_blue;
static PangoLayout *create_layout_with_dpi(cairo_t *cr) {
PangoLayout *layout;
PangoContext *context;
context = pango_cairo_create_context(cr);
const double dpi = (double)root_screen->height_in_pixels * 25.4 /
(double)root_screen->height_in_millimeters;
LOG("X11 root window dictates %f DPI\n", dpi);
pango_cairo_context_set_resolution(context, dpi);
layout = pango_layout_new(context);
g_object_unref(context);
return layout;
}
/* /*
* Loads a Pango font description into an i3Font structure. Returns true * Loads a Pango font description into an i3Font structure. Returns true
* on success, false otherwise. * on success, false otherwise.
@ -56,10 +71,7 @@ static bool load_pango_font(i3Font *font, const char *desc) {
/* Create a dummy Pango layout to compute the font height */ /* Create a dummy Pango layout to compute the font height */
cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1); cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
cairo_t *cr = cairo_create(surface); cairo_t *cr = cairo_create(surface);
double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters; PangoLayout *layout = create_layout_with_dpi(cr);
PangoContext *pc = pango_cairo_create_context(cr);
pango_cairo_context_set_resolution(pc, ydpi);
PangoLayout *layout = pango_layout_new(pc);
pango_layout_set_font_description(layout, font->specific.pango_desc); pango_layout_set_font_description(layout, font->specific.pango_desc);
/* Get the font height */ /* Get the font height */
@ -69,7 +81,6 @@ static bool load_pango_font(i3Font *font, const char *desc) {
/* Free resources */ /* Free resources */
g_object_unref(layout); g_object_unref(layout);
g_object_unref(pc);
cairo_destroy(cr); cairo_destroy(cr);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
@ -89,10 +100,7 @@ static void draw_text_pango(const char *text, size_t text_len,
cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable, cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
root_visual_type, x + max_width, y + savedFont->height); root_visual_type, x + max_width, y + savedFont->height);
cairo_t *cr = cairo_create(surface); cairo_t *cr = cairo_create(surface);
double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters; PangoLayout *layout = create_layout_with_dpi(cr);
PangoContext *pc = pango_cairo_create_context(cr);
pango_cairo_context_set_resolution(pc, ydpi);
PangoLayout *layout = pango_layout_new(pc);
gint height; gint height;
pango_layout_set_font_description(layout, savedFont->specific.pango_desc); pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
@ -111,7 +119,6 @@ static void draw_text_pango(const char *text, size_t text_len,
/* Free resources */ /* Free resources */
g_object_unref(layout); g_object_unref(layout);
g_object_unref(pc);
cairo_destroy(cr); cairo_destroy(cr);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
} }
@ -125,10 +132,7 @@ static int predict_text_width_pango(const char *text, size_t text_len) {
/* root_visual_type is cached in load_pango_font */ /* root_visual_type is cached in load_pango_font */
cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1); cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
cairo_t *cr = cairo_create(surface); cairo_t *cr = cairo_create(surface);
double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters; PangoLayout *layout = create_layout_with_dpi(cr);
PangoContext *pc = pango_cairo_create_context(cr);
pango_cairo_context_set_resolution(pc, ydpi);
PangoLayout *layout = pango_layout_new(pc);
/* Get the font width */ /* Get the font width */
gint width; gint width;
@ -139,7 +143,6 @@ static int predict_text_width_pango(const char *text, size_t text_len) {
/* Free resources */ /* Free resources */
g_object_unref(layout); g_object_unref(layout);
g_object_unref(pc);
cairo_destroy(cr); cairo_destroy(cr);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);