0

I have the following sample code

#include <cairo/cairo-pdf.h>
#include <pango/pangocairo.h>

#define MM_TO_PT (72.0 / 25.4)
#define PAGE_WIDTH_A5 148  // mm
#define PAGE_HEIGHT_A5 210 // mm

void draw(cairo_t* cr, double scale, double x, double y);

int main()
{
    cairo_surface_t *surface = cairo_pdf_surface_create("test.pdf", 
        PAGE_WIDTH_A5 * MM_TO_PT, PAGE_HEIGHT_A5 * MM_TO_PT);
    cairo_t* cr = cairo_create(surface);

    double scale = MM_TO_PT;
    draw(cr, scale * 2., 20. / 2, 20. / 2);
    draw(cr, scale, 20., 40.);
    draw(cr, 1, 20. * scale, 60. * scale);

    cairo_destroy(cr);
    cairo_surface_finish(surface);
    cairo_surface_destroy(surface);
}

void draw(cairo_t* cr, double scale, double x, double y)
{
    cairo_scale(cr, scale, scale);
    PangoLayout* layout = pango_cairo_create_layout(cr);
    pango_cairo_update_layout(cr, layout);
    pango_layout_set_text(layout, "Sample text\non two lines", -1);
    PangoFontDescription* desc = pango_font_description_from_string("Calibri, 12");
    pango_font_description_set_size(desc, gint(10.0 * (double)PANGO_SCALE / scale));
    pango_layout_set_font_description(layout, desc);
    pango_font_description_free(desc);
    cairo_move_to(cr, x, y);
    pango_cairo_show_layout(cr, layout);
    g_object_unref(layout);
    cairo_scale(cr, 1 / scale, 1/ scale);
}

The 3 draw lines from the main function should draw the same sized text at different heights. However the first text is very poor with horizontal spacing messed up. The result of the second statement is a bit better, but still unacceptable. The final statement draws the text well. If the size of the text is smaller, then the result becomes worse.

The output of the above program

My idea was to use metric coordinates (mm) in all my drawing functions to be able to design the layout using mm. For this I used the approach from the second draw function. This has worked well until recently when I updated pango-cairo library. Does anyone know what has changed that made rendering worse? I found the scaling capability of pango-cairo useful, but with this change I need to implement my own scaling. Any help or comments would be appreciated.

0 Answers0