+#if PANGO_SUPPORT
+static xcb_visualtype_t *root_visual_type;
+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.
+ *
+ */
+static bool load_pango_font(i3Font *font, const char *desc) {
+ /* Load the font description */
+ font->specific.pango_desc = pango_font_description_from_string(desc);
+ if (!font->specific.pango_desc) {
+ ELOG("Could not open font %s with Pango, fallback to X font.\n", desc);
+ return false;
+ }
+
+ 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);
+
+ /* We cache root_visual_type here, since you must call
+ * load_pango_font before any other pango function
+ * that would need root_visual_type */
+ root_visual_type = get_visualtype(root_screen);
+
+ /* 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 = create_layout_with_dpi(cr);
+ pango_layout_set_font_description(layout, font->specific.pango_desc);
+
+ /* Get the font height */
+ gint height;
+ pango_layout_get_pixel_size(layout, NULL, &height);
+ font->height = height;
+
+ /* Free resources */
+ g_object_unref(layout);
+ cairo_destroy(cr);
+ cairo_surface_destroy(surface);
+
+ /* Set the font type and return successfully */
+ font->type = FONT_TYPE_PANGO;
+ return true;
+}
+
+/*
+ * Draws text using Pango rendering.
+ *
+ */
+static void draw_text_pango(const char *text, size_t text_len,
+ 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);
+ cairo_t *cr = cairo_create(surface);
+ 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);
+ 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 */
+ g_object_unref(layout);
+ cairo_destroy(cr);
+ cairo_surface_destroy(surface);
+}
+
+/*
+ * Calculate the text width using Pango rendering.
+ *
+ */
+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 = create_layout_with_dpi(cr);
+
+ /* Get the font width */
+ gint width;
+ pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
+
+ 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);
+
+ /* Free resources */
+ g_object_unref(layout);
+ cairo_destroy(cr);
+ cairo_surface_destroy(surface);
+
+ return width;
+}
+#endif
+