[librsvg: 3/4] (#348): Fix incorrect font sizing



commit dc4f58459dfa72ecc8a3c9b952a4b8cab8d75387
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Sep 27 10:43:47 2018 -0500

    (#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.
    
    https://gitlab.gnome.org/GNOME/librsvg/issues/348

 rsvg_internals/src/drawing_ctx.rs                     |  18 +++++++++++++++++-
 .../reftests/bugs/310-font-size-at-48dpi-ref.png      | Bin 5818 -> 6692 bytes
 .../reftests/bugs/348-font-size-48dpi-ref.png         | Bin 0 -> 3682 bytes
 tests/fixtures/reftests/bugs/348-font-size-48dpi.svg  |  12 ++++++++++++
 4 files changed, 29 insertions(+), 1 deletion(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 32b942b1..b5a98300 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -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();
diff --git a/tests/fixtures/reftests/bugs/310-font-size-at-48dpi-ref.png 
b/tests/fixtures/reftests/bugs/310-font-size-at-48dpi-ref.png
index dd7d7eaf..3f93fbbc 100644
Binary files a/tests/fixtures/reftests/bugs/310-font-size-at-48dpi-ref.png and 
b/tests/fixtures/reftests/bugs/310-font-size-at-48dpi-ref.png differ
diff --git a/tests/fixtures/reftests/bugs/348-font-size-48dpi-ref.png 
b/tests/fixtures/reftests/bugs/348-font-size-48dpi-ref.png
new file mode 100644
index 00000000..d40ee906
Binary files /dev/null and b/tests/fixtures/reftests/bugs/348-font-size-48dpi-ref.png differ
diff --git a/tests/fixtures/reftests/bugs/348-font-size-48dpi.svg 
b/tests/fixtures/reftests/bugs/348-font-size-48dpi.svg
new file mode 100644
index 00000000..d36eceaf
--- /dev/null
+++ b/tests/fixtures/reftests/bugs/348-font-size-48dpi.svg
@@ -0,0 +1,12 @@
+<?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>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]