(#348): Fix incorrect font sizing

Pango considers the font size in a PangoFontDescription to be in
points, and it converts to pixels by using the resolution set in
pango_cairo_context_set_resolution().

However, we were passing our plainly normalized Length values to
pango::FontDescription::set_size(), i.e. we were passing them in
pixels, not points.  Thus, Pango was applying extra scaling, and
messing up the final size.

This commit hardcodes a value of 72.0 for Pango's resolution.  This is
so that it will not apply any extra scaling on top of our
computations.  We are doing the inches-to-pixels conversion, instead
of letting Pango do it.

This commit adds 348-font-size-48dpi.svg which two pairs of squares
and text elements, which should both render at the same size:  one
pair is done in userspace pixels, and the other pair is done in
inches.

This commit also regenerates 310-font-size-at-48dpi-ref.png - it is
the only other file that uses absolute font sizes.

#348
parent d8a9824e
......@@ -516,7 +516,23 @@ impl<'a> DrawingCtx<'a> {
let cr = self.get_cairo_context();
pangocairo::functions::update_context(&cr, &context);
set_resolution(&context, self.dpi_y);
// Pango says this about pango_cairo_context_set_resolution():
//
// Sets the resolution for the context. This is a scale factor between
// points specified in a #PangoFontDescription and Cairo units. The
// default value is 96, meaning that a 10 point font will be 13
// units high. (10 * 96. / 72. = 13.3).
//
// I.e. Pango font sizes in a PangoFontDescription are in *points*, not pixels.
// However, we are normalizing everything to userspace units, which amount to
// pixels. So, we will use 72.0 here to make Pango not apply any further scaling
// to the size values we give it.
//
// An alternative would be to divide our font sizes by (dpi_y / 72) to effectively
// cancel out Pango's scaling, but it's probably better to deal with Pango-isms
// right here, instead of spreading them out through our Length normalization
// code.
set_resolution(&context, 72.0);
if self.is_testing {
let mut options = cairo::FontOptions::new();
......
<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">
<g fill="blue">
<rect x="48" y="48" width="48" height="48"/>
<text style="font-size: 48" x="96" y="96">Hello</text>
</g>
<g fill="green">
<rect x="1in" y="2.5in" width="1in" height="1in"/>
<text style="font-size: 1in" x="2in" y="3.5in">Hello</text>
</g>
</svg>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment