]> git.sur5r.net Git - glabels/blobdiff - src/label-text.c
Workaround for pango kerning bug.
[glabels] / src / label-text.c
index b2a3231712af3efb0c83dc8c085645e58fd13a61..287d3cc15b8625fc39dd13e173163c12c38de3b8 100644 (file)
@@ -171,8 +171,10 @@ static gdouble         auto_shrink_font_size       (cairo_t          *cr,
                                                     gdouble           size,
                                                     PangoWeight       weight,
                                                     PangoStyle        style,
+                                                    gdouble           line_spacing,
                                                     gchar            *text,
-                                                    gdouble           width);
+                                                    gdouble           width,
+                                                    gdouble           height);
 
 static gboolean        object_at                   (glLabelObject    *object,
                                                     cairo_t          *cr,
@@ -529,9 +531,10 @@ get_size (glLabelObject *object,
 
        
        fontmap = pango_cairo_font_map_new ();
-       context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
+       context = pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
        options = cairo_font_options_create ();
         cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
+        cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
        pango_cairo_context_set_font_options (context, options);
        cairo_font_options_destroy (options);
 
@@ -1016,7 +1019,7 @@ gl_label_text_get_auto_shrink (glLabelText      *ltext)
 
 
 /*****************************************************************************/
-/* Automatically shrink text size to fit within horizontal width.            */
+/* Automatically shrink text size to fit within bounding box.                */
 /*****************************************************************************/
 static gdouble
 auto_shrink_font_size (cairo_t     *cr,
@@ -1024,14 +1027,16 @@ auto_shrink_font_size (cairo_t     *cr,
                        gdouble      size,
                        PangoWeight  weight,
                        PangoStyle   style,
+                       gdouble      line_spacing,
                        gchar       *text,
-                       gdouble      width)
+                       gdouble      width,
+                       gdouble      height)
 {
         PangoLayout          *layout;
         PangoFontDescription *desc;
         gint                  iw, ih;
-        gdouble               layout_width;
-        gdouble               new_size;
+        gdouble               layout_width, layout_height;
+        gdouble               new_wsize, new_hsize;
 
         layout = pango_cairo_create_layout (cr);
 
@@ -1044,35 +1049,50 @@ auto_shrink_font_size (cairo_t     *cr,
         pango_layout_set_font_description (layout, desc);
         pango_font_description_free       (desc);
 
-        pango_layout_set_text (layout, text, -1);
+        pango_layout_set_spacing (layout, size * (line_spacing-1) * PANGO_SCALE);
         pango_layout_set_width (layout, -1);
+        pango_layout_set_text (layout, text, -1);
         pango_layout_get_size (layout, &iw, &ih);
         layout_width = (gdouble)iw / (gdouble)PANGO_SCALE;
+        layout_height = (gdouble)ih / (gdouble)PANGO_SCALE;
 
         g_object_unref (layout);
 
         g_print ("Object w = %g, layout w = %g\n", width, layout_width);
+        g_print ("Object h = %g, layout h = %g\n", height, layout_height);
 
+        new_wsize = new_hsize = size;
         if ( layout_width > width )
         {
                 /* Scale down. */
-                new_size = size * (width-2*GL_LABEL_TEXT_MARGIN)/layout_width;
+                new_wsize = size * (width-2*GL_LABEL_TEXT_MARGIN) / layout_width;
 
                 /* Round down to nearest 1/2 point */
-                new_size = (int)(new_size*2.0) / 2.0;
+                new_wsize = (int)(new_wsize*2.0) / 2.0;
 
                 /* don't get ridiculously small. */
-                if (new_size < 1.0)
+                if (new_wsize < 1.0)
                 {
-                        new_size = 1.0;
+                        new_wsize = 1.0;
                 }
         }
-        else
+
+        if ( layout_height > height )
         {
-                new_size = size;
+                /* Scale down. */
+                new_hsize = size * height / layout_height;
+
+                /* Round down to nearest 1/2 point */
+                new_hsize = (int)(new_hsize*2.0) / 2.0;
+
+                /* don't get ridiculously small. */
+                if (new_hsize < 1.0)
+                {
+                        new_hsize = 1.0;
+                }
         }
 
-        return new_size;
+        return (new_wsize < new_hsize ? new_wsize : new_hsize);
 }
 
 
@@ -1095,13 +1115,24 @@ set_text_path (glLabelText      *this,
         PangoLayout          *layout;
         PangoStyle            style;
         PangoFontDescription *desc;
+        gdouble               scale_x, scale_y;
         cairo_font_options_t *font_options;
         PangoContext         *context;
 
 
         gl_debug (DEBUG_LABEL, "START");
 
+        /*
+         * Workaround for pango Bug#700592, which is a regression of Bug#341481.
+         * Render font at device scale and scale font size accordingly.
+         */
+        scale_x = 1.0;
+        scale_y = 1.0;
+        cairo_device_to_user_distance (cr, &scale_x, &scale_y);
+        scale_x = fabs (scale_x);
+        scale_y = fabs (scale_y);
         cairo_save (cr);
+        cairo_scale (cr, scale_x, scale_y);
 
         gl_label_object_get_size (GL_LABEL_OBJECT (this), &object_w, &object_h);
         gl_label_object_get_raw_size (GL_LABEL_OBJECT (this), &raw_w, &raw_h);
@@ -1111,17 +1142,19 @@ set_text_path (glLabelText      *this,
 
         style = this->priv->font_italic_flag ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
 
-        font_size   = this->priv->font_size;
+        font_size   = this->priv->font_size * FONT_SCALE;
         auto_shrink = gl_label_text_get_auto_shrink (this);
         if (!screen_flag && record && auto_shrink && (raw_w != 0.0))
         {
                 font_size = auto_shrink_font_size (cr,
                                                    this->priv->font_family,
-                                                   this->priv->font_size * FONT_SCALE,
+                                                   font_size,
                                                    this->priv->font_weight,
                                                    style,
+                                                   this->priv->line_spacing,
                                                    text,
-                                                   object_w);
+                                                   object_w,
+                                                   object_h);
         }
 
 
@@ -1129,6 +1162,7 @@ set_text_path (glLabelText      *this,
 
         font_options = cairo_font_options_create ();
         cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
+        cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
         context = pango_layout_get_context (layout);
         pango_cairo_context_set_font_options (context, font_options);
         cairo_font_options_destroy (font_options);
@@ -1136,20 +1170,20 @@ set_text_path (glLabelText      *this,
         desc = pango_font_description_new ();
         pango_font_description_set_family (desc, this->priv->font_family);
         pango_font_description_set_weight (desc, this->priv->font_weight);
-        pango_font_description_set_size   (desc, font_size * FONT_SCALE * PANGO_SCALE);
+        pango_font_description_set_size   (desc, font_size * PANGO_SCALE / scale_x);
         pango_font_description_set_style  (desc, style);
         pango_layout_set_font_description (layout, desc);
         pango_font_description_free       (desc);
 
         pango_layout_set_text (layout, text, -1);
-        pango_layout_set_spacing (layout, font_size * (this->priv->line_spacing-1) * PANGO_SCALE);
+        pango_layout_set_spacing (layout, font_size * (this->priv->line_spacing-1) * PANGO_SCALE / scale_x);
         if (raw_w == 0.0)
         {
                 pango_layout_set_width (layout, -1);
         }
         else
         {
-                pango_layout_set_width (layout, object_w * PANGO_SCALE);
+                pango_layout_set_width (layout, object_w * PANGO_SCALE / scale_x);
         }
         pango_layout_set_wrap (layout, PANGO_WRAP_WORD);
         pango_layout_set_alignment (layout, this->priv->align);
@@ -1158,17 +1192,17 @@ set_text_path (glLabelText      *this,
         switch (this->priv->valign)
         {
         case GL_VALIGN_VCENTER:
-                y = (object_h - ih) / 2;
+                y = (object_h/scale_x - ih) / 2;
                 break;
         case GL_VALIGN_BOTTOM:
-                y = object_h - ih;
+                y = object_h/scale_x - ih;
                 break;
         default:
                 y = 0;
                 break;
         }
 
-        cairo_move_to (cr, GL_LABEL_TEXT_MARGIN, y);
+        cairo_move_to (cr, GL_LABEL_TEXT_MARGIN/scale_x, y);
         pango_cairo_layout_path (cr, layout);
 
         g_object_unref (layout);