[cairo] Scale font metrics to user space
authorBehdad Esfahbod <behdad@behdad.org>
Tue, 4 May 2010 17:40:35 +0000 (13:40 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Tue, 4 May 2010 17:40:35 +0000 (13:40 -0400)
Bug 457990 - font metrics are not converted to user space in cairo backend

pango/pangocairo-font.c
pango/pangocairo-private.h

index ace2a57..1f6382b 100644 (file)
@@ -22,6 +22,7 @@
 #include "config.h"
 
 #include <math.h>
+#include <string.h>
 
 #include "pangocairo.h"
 #include "pangocairo-private.h"
@@ -266,6 +267,10 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
       PangoLayout *layout;
       PangoRectangle extents;
       PangoFontDescription *desc;
+      cairo_scaled_font_t *scaled_font;
+      cairo_matrix_t cairo_matrix;
+      PangoMatrix pango_matrix;
+      PangoMatrix identity = PANGO_MATRIX_INIT;
 
       int height, shift;
 
@@ -281,17 +286,45 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
 
       info->sample_str = sample_str;
 
+      scaled_font = _pango_cairo_font_private_get_scaled_font (cf_priv);
+
       context = pango_font_map_create_context (fontmap);
       pango_context_set_language (context, language);
+
       font_options = cairo_font_options_create ();
-      cairo_scaled_font_get_font_options (_pango_cairo_font_private_get_scaled_font (cf_priv), font_options);
+      cairo_scaled_font_get_font_options (scaled_font, font_options);
       pango_cairo_context_set_font_options (context, font_options);
       cairo_font_options_destroy (font_options);
 
       info->metrics = (* PANGO_CAIRO_FONT_GET_IFACE (font)->create_base_metrics_for_context) (cfont, context);
 
-      /* Update approximate_*_width now */
+      /* We now need to adjust the base metrics for ctm */
+      cairo_scaled_font_get_ctm (scaled_font, &cairo_matrix);
+      pango_matrix.xx = cairo_matrix.xx;
+      pango_matrix.yx = cairo_matrix.yx;
+      pango_matrix.xy = cairo_matrix.xy;
+      pango_matrix.yy = cairo_matrix.yy;
+      pango_matrix.x0 = 0;
+      pango_matrix.y0 = 0;
+      if (G_UNLIKELY (0 != memcmp (&identity, &pango_matrix, 4 * sizeof (double))))
+        {
+         double xscale = pango_matrix_get_font_scale_factor (&pango_matrix);
+         if (xscale) xscale = 1 / xscale;
+
+         info->metrics->ascent *= xscale;
+         info->metrics->descent *= xscale;
+         info->metrics->underline_position *= xscale;
+         info->metrics->underline_thickness *= xscale;
+         info->metrics->strikethrough_position *= xscale;
+         info->metrics->strikethrough_thickness *= xscale;
+       }
 
+
+      /* Set the matrix on the context so we don't have to adjust the derived
+       * metrics. */
+      pango_context_set_matrix (context, &pango_matrix);
+
+      /* Update approximate_*_width now */
       layout = pango_layout_new (context);
       desc = pango_font_describe_with_absolute_size (font);
       pango_layout_set_font_description (layout, desc);
index 4e64f25..52a8b2f 100644 (file)
@@ -75,7 +75,7 @@ struct _PangoCairoFontPrivateScaledFontData
 struct _PangoCairoFontPrivate
 {
   PangoCairoFont *cfont;
-   
+
   PangoCairoFontPrivateScaledFontData *data;
 
   cairo_scaled_font_t *scaled_font;