+static void
+xr_measure_fonts (cairo_t *cairo, const struct xr_font fonts[XR_N_FONTS],
+ int *char_width, int *char_height)
+{
+ *char_width = 0;
+ *char_height = 0;
+ for (int i = 0; i < XR_N_FONTS; i++)
+ {
+ PangoLayout *layout = pango_cairo_create_layout (cairo);
+ pango_layout_set_font_description (layout, fonts[i].desc);
+
+ pango_layout_set_text (layout, "0", 1);
+
+ int cw, ch;
+ pango_layout_get_size (layout, &cw, &ch);
+ *char_width = MAX (*char_width, pango_to_xr (cw));
+ *char_height = MAX (*char_height, pango_to_xr (ch));
+
+ g_object_unref (G_OBJECT (layout));
+ }
+}
+
+static int
+get_layout_height (PangoLayout *layout)
+{
+ int w, h;
+ pango_layout_get_size (layout, &w, &h);
+ return h;
+}
+
+static int
+xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
+ const struct page_heading *ph, int page_number,
+ int width, bool draw, int base_y)
+{
+ PangoLayout *layout = pango_cairo_create_layout (cairo);
+ pango_layout_set_font_description (layout, font);
+
+ int y = 0;
+ for (size_t i = 0; i < ph->n; i++)
+ {
+ const struct page_paragraph *pp = &ph->paragraphs[i];
+
+ char *markup = output_driver_substitute_heading_vars (pp->markup,
+ page_number);
+ pango_layout_set_markup (layout, markup, -1);
+ free (markup);
+
+ pango_layout_set_alignment (
+ layout,
+ (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
+ : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
+ : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
+ : PANGO_ALIGN_RIGHT));
+ pango_layout_set_width (layout, xr_to_pango (width));
+ if (draw)
+ {
+ cairo_save (cairo);
+ cairo_translate (cairo, 0, xr_to_pt (y + base_y));
+ pango_cairo_show_layout (cairo, layout);
+ cairo_restore (cairo);
+ }
+
+ y += pango_to_xr (get_layout_height (layout));
+ }
+
+ g_object_unref (G_OBJECT (layout));
+
+ return y;
+}
+
+static int
+xr_measure_headings (cairo_surface_t *surface,
+ const PangoFontDescription *font,
+ const struct page_heading headings[2],
+ int width, int object_spacing, int height[2])
+{
+ cairo_t *cairo = cairo_create (surface);
+ int total = 0;
+ for (int i = 0; i < 2; i++)
+ {
+ int h = xr_render_page_heading (cairo, font, &headings[i], -1,
+ width, false, 0);
+
+ /* If the top heading is nonempty, add some space below it. */
+ if (h && i == 0)
+ h += object_spacing;
+
+ if (height)
+ height[i] = h;
+ total += h;
+ }
+ cairo_destroy (cairo);
+ return total;
+}
+