X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libi3%2Ffont.c;h=df478fb4fc87f69e6a7d659b4bec7ab8cee4a10d;hb=3c5df50c54ab6451098c2175a21470312952000e;hp=8239b1f4414e8cc2ea6974c35da66c8042a7b6a4;hpb=a1691a08ba33bba654d45e80afab0dcf03cc0cfa;p=i3%2Fi3 diff --git a/libi3/font.c b/libi3/font.c index 8239b1f4..df478fb4 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -30,6 +30,30 @@ static double pango_font_red; static double pango_font_green; static double pango_font_blue; +/* Necessary to track whether the dpi changes and trigger a LOG() message, + * which is more easily visible to users. */ +static double logged_dpi = 0.0; + +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; + if (logged_dpi != dpi) { + logged_dpi = dpi; + LOG("X11 root window dictates %f DPI\n", dpi); + } else { + DLOG("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 * on success, false otherwise. @@ -45,8 +69,7 @@ static bool load_pango_font(i3Font *font, const char *desc) { LOG("Using Pango font %s, size %d\n", pango_font_description_get_family(font->specific.pango_desc), - pango_font_description_get_size(font->specific.pango_desc) / PANGO_SCALE - ); + pango_font_description_get_size(font->specific.pango_desc) / PANGO_SCALE); /* We cache root_visual_type here, since you must call * load_pango_font before any other pango function @@ -56,7 +79,7 @@ static bool load_pango_font(i3Font *font, const char *desc) { /* 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_t *cr = cairo_create(surface); - PangoLayout *layout = pango_cairo_create_layout(cr); + PangoLayout *layout = create_layout_with_dpi(cr); pango_layout_set_font_description(layout, font->specific.pango_desc); /* Get the font height */ @@ -79,23 +102,31 @@ static bool load_pango_font(i3Font *font, const char *desc) { * */ static void draw_text_pango(const char *text, size_t text_len, - xcb_drawable_t drawable, int x, int y, int max_width) { + xcb_drawable_t drawable, int x, int y, + int max_width, bool is_markup) { /* Create the Pango layout */ /* root_visual_type is cached in load_pango_font */ 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); - PangoLayout *layout = pango_cairo_create_layout(cr); + PangoLayout *layout = create_layout_with_dpi(cr); + gint height; + pango_layout_set_font_description(layout, savedFont->specific.pango_desc); pango_layout_set_width(layout, max_width * PANGO_SCALE); pango_layout_set_wrap(layout, PANGO_WRAP_CHAR); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); + if (is_markup) + pango_layout_set_markup(layout, text, text_len); + else + pango_layout_set_text(layout, text, text_len); + /* Do the drawing */ cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue); - cairo_move_to(cr, x, y); - pango_layout_set_text(layout, text, text_len); pango_cairo_update_layout(cr, layout); + pango_layout_get_pixel_size(layout, NULL, &height); + cairo_move_to(cr, x, y - 0.5 * (height - savedFont->height)); pango_cairo_show_layout(cr, layout); /* Free resources */ @@ -108,17 +139,22 @@ static void draw_text_pango(const char *text, size_t text_len, * Calculate the text width using Pango rendering. * */ -static int predict_text_width_pango(const char *text, size_t text_len) { +static int predict_text_width_pango(const char *text, size_t text_len, bool is_markup) { /* Create a dummy Pango layout */ /* 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_t *cr = cairo_create(surface); - PangoLayout *layout = pango_cairo_create_layout(cr); + PangoLayout *layout = create_layout_with_dpi(cr); /* Get the font width */ gint width; pango_layout_set_font_description(layout, savedFont->specific.pango_desc); - pango_layout_set_text(layout, text, text_len); + + if (is_markup) + pango_layout_set_markup(layout, text, text_len); + else + pango_layout_set_text(layout, text, text_len); + pango_cairo_update_layout(cr, layout); pango_layout_get_pixel_size(layout, &width, NULL); @@ -133,13 +169,23 @@ static int predict_text_width_pango(const char *text, size_t text_len) { /* * Loads a font for usage, also getting its metrics. If fallback is true, - * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. + * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. If any + * font was previously loaded, it will be freed. * */ i3Font load_font(const char *pattern, const bool fallback) { + /* if any font was previously loaded, free it now */ + free_font(); + i3Font font; font.type = FONT_TYPE_NONE; + /* No XCB connction, return early because we're just validating the + * configuration file. */ + if (conn == NULL) { + return font; + } + #if PANGO_SUPPORT /* Try to load a pango font if specified */ if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) { @@ -160,7 +206,7 @@ i3Font load_font(const char *pattern, const bool fallback) { /* Send all our requests first */ font.specific.xcb.id = xcb_generate_id(conn); xcb_void_cookie_t font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id, - strlen(pattern), pattern); + strlen(pattern), pattern); xcb_query_font_cookie_t info_cookie = xcb_query_font(conn, font.specific.xcb.id); /* Check for errors. If errors, fall back to default font. */ @@ -173,7 +219,7 @@ i3Font load_font(const char *pattern, const bool fallback) { pattern, error->error_code); pattern = "fixed"; font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id, - strlen(pattern), pattern); + strlen(pattern), pattern); info_cookie = xcb_query_font(conn, font.specific.xcb.id); /* Check if we managed to open 'fixed' */ @@ -184,12 +230,13 @@ i3Font load_font(const char *pattern, const bool fallback) { ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n"); pattern = "-misc-*"; font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id, - strlen(pattern), pattern); + strlen(pattern), pattern); info_cookie = xcb_query_font(conn, font.specific.xcb.id); if ((error = xcb_request_check(conn, font_cookie)) != NULL) errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks " - "(fixed or -misc-*): X11 error %d", error->error_code); + "(fixed or -misc-*): X11 error %d", + error->error_code); } } @@ -223,10 +270,15 @@ void set_font(i3Font *font) { } /* - * Frees the resources taken by the current font. + * Frees the resources taken by the current font. If no font was previously + * loaded, it simply returns. * */ void free_font(void) { + /* if there is no saved font, simply return */ + if (savedFont == NULL) + return; + free(savedFont->pattern); switch (savedFont->type) { case FONT_TYPE_NONE: @@ -249,6 +301,8 @@ void free_font(void) { assert(false); break; } + + savedFont = NULL; } /* @@ -265,7 +319,7 @@ void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background case FONT_TYPE_XCB: { /* Change the font and colors in the GC */ uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT; - uint32_t values[] = { foreground, background, savedFont->specific.xcb.id }; + uint32_t values[] = {foreground, background, savedFont->specific.xcb.id}; xcb_change_gc(conn, gc, mask, values); break; } @@ -286,7 +340,7 @@ void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len); static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable, - xcb_gcontext_t gc, int x, int y, int max_width) { + xcb_gcontext_t gc, int x, int y, int max_width) { /* X11 coordinates for fonts start at the baseline */ int pos_y = y + savedFont->specific.xcb.info->font_ascent; @@ -332,13 +386,13 @@ void draw_text(i3String *text, xcb_drawable_t drawable, return; case FONT_TYPE_XCB: draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text), - drawable, gc, x, y, max_width); + drawable, gc, x, y, max_width); break; #if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Render the text using Pango */ draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text), - drawable, x, y, max_width); + drawable, x, y, max_width, i3string_is_markup(text)); return; #endif default: @@ -351,15 +405,14 @@ void draw_text(i3String *text, xcb_drawable_t drawable, * */ void draw_text_ascii(const char *text, xcb_drawable_t drawable, - xcb_gcontext_t gc, int x, int y, int max_width) { + xcb_gcontext_t gc, int x, int y, int max_width) { assert(savedFont != NULL); switch (savedFont->type) { case FONT_TYPE_NONE: /* Nothing to do */ return; - case FONT_TYPE_XCB: - { + case FONT_TYPE_XCB: { size_t text_len = strlen(text); if (text_len > 255) { /* The text is too long to draw it directly to X */ @@ -378,7 +431,7 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable, case FONT_TYPE_PANGO: /* Render the text using Pango */ draw_text_pango(text, strlen(text), - drawable, x, y, max_width); + drawable, x, y, max_width, false); return; #endif default: @@ -397,9 +450,9 @@ static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) { /* Query the text width */ xcb_generic_error_t *error; xcb_query_text_extents_cookie_t cookie = xcb_query_text_extents(conn, - savedFont->specific.xcb.id, text_len, (xcb_char2b_t*)text); + savedFont->specific.xcb.id, text_len, (xcb_char2b_t *)text); xcb_query_text_extents_reply_t *reply = xcb_query_text_extents_reply(conn, - cookie, &error); + cookie, &error); if (reply == NULL) { /* We return a safe estimate because a rendering error is better than * a crash. Plus, the user will see the error in his log. */ @@ -441,14 +494,14 @@ static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) { /* Don't you ask me, how this one works… (Merovius) */ info = &font_table[((row - font_info->min_byte1) * - (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) + - (col - font_info->min_char_or_byte2)]; + (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) + + (col - font_info->min_char_or_byte2)]; if (info->character_width != 0 || - (info->right_side_bearing | - info->left_side_bearing | - info->ascent | - info->descent) != 0) { + (info->right_side_bearing | + info->left_side_bearing | + info->ascent | + info->descent) != 0) { width += info->character_width; } } @@ -474,7 +527,8 @@ int predict_text_width(i3String *text) { #if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Calculate extents using Pango */ - return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text)); + return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text), + i3string_is_markup(text)); #endif default: assert(false);