Hi! I was able to proceed further on this. You can take a look at this small C sample based only on pangocairo that exhibits the problem. This code shows what Gtk3 does internally. The integer scaling factor (gdk_scale) is set on the CairoSurface with cairo_surface_set_device_scale. The text scaling factor (gdk_dpi_scale) instead is set on the PangoCairoContext using pango_cairo_context_set_resolution. You can compile and run the following snippet and see the output.png file:
/*
* FILE: test_pango.c
* COMMENTS: compile with: gcc -o test_pango test_pango.c $(pkg-config --cflags --libs pangocairo)
*/
#include <pango/pangocairo.h>
const int gdk_scale = 5;
const double gdk_dpi_scale = 1.0 / 5.0;
void draw_text (cairo_t *cr) {
PangoFontMap *font_map = pango_cairo_font_map_get_default ();
PangoContext *pango_cairo_context = pango_font_map_create_context (font_map);
pango_cairo_context_set_resolution (pango_cairo_context, 96.0 * gdk_dpi_scale);
PangoLayout *pango_layout = pango_layout_new (pango_cairo_context);
pango_layout_set_text (pango_layout, "Hello, world", -1);
PangoFontDescription *pango_font = pango_font_description_from_string ("Segoe UI 9");
pango_layout_set_font_description (pango_layout, pango_font);
pango_cairo_show_layout (cr, pango_layout);
pango_font_description_free (pango_font);
g_object_unref (pango_layout);
g_object_unref (pango_cairo_context);
}
void clear_to_white_background (cairo_t *cr) {
cairo_rectangle (cr, 0, 0, 500, 100);
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
cairo_fill (cr);
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
}
int main () {
cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 500, 100);
cr = cairo_create (surface);
clear_to_white_background (cr);
cairo_surface_set_device_scale (surface, gdk_scale, gdk_scale);
draw_text (cr);
cairo_surface_write_to_png (surface, "output.png");
cairo_destroy (cr);
cairo_surface_destroy (surface);
return 0;
}
Using both cairo_surface_set_device_scale and pango_cairo_context_set_resolution causes a bad interaction. I don’t know if it is a sign of a problem in Pango or if we are instead ‘abusing’ the APIs (and asking for troubles) 