[pango/fix-small-caps-line-height] Fix line height with emulated small caps




commit a25f439fb509159b88a0d05da18d099894332884
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Dec 10 12:49:45 2021 -0500

    Fix line height with emulated small caps
    
    When we change font scale for emulating small caps,
    keep the original font around in the item, and use
    it for calculating the run height.
    
    This is reusing the unused lang_engine field in
    PangoAnalysis.
    
    Fixes: #622

 pango/itemize.c      | 21 +++++++++++++++++----
 pango/pango-item.c   |  6 ++++++
 pango/pango-item.h   |  4 ++--
 pango/pango-layout.c | 27 +++++++++++++++++++--------
 4 files changed, 44 insertions(+), 14 deletions(-)
---
diff --git a/pango/itemize.c b/pango/itemize.c
index 77e86507..ccbf8df4 100644
--- a/pango/itemize.c
+++ b/pango/itemize.c
@@ -1041,7 +1041,8 @@ collect_font_scale (PangoContext  *context,
                     GList        **stack,
                     PangoItem     *item,
                     PangoItem     *prev,
-                    double        *scale)
+                    double        *scale,
+                    gboolean      *is_small_caps)
 {
   gboolean retval = FALSE;
   GList *l;
@@ -1119,11 +1120,14 @@ collect_font_scale (PangoContext  *context,
      }
 
    *scale = 1.0;
+   *is_small_caps = TRUE;
 
    for (l = *stack; l; l = l->next)
      {
        ScaleItem *entry = l->data;
        *scale *= entry->scale;
+       if (((PangoAttrInt *)entry->attr)->value != PANGO_FONT_SCALE_SMALL_CAPS)
+         *is_small_caps = FALSE;
        retval = TRUE;
      }
 
@@ -1148,7 +1152,8 @@ collect_font_scale (PangoContext  *context,
 static void
 apply_scale_to_item (PangoContext *context,
                      PangoItem    *item,
-                     double        scale)
+                     double        scale,
+                     gboolean      is_small_caps)
 {
   PangoFontDescription *desc;
   double size;
@@ -1161,6 +1166,13 @@ apply_scale_to_item (PangoContext *context,
   else
     pango_font_description_set_size (desc, size);
 
+  if (is_small_caps)
+    {
+      if (item->analysis.lang_engine)
+        g_object_unref (item->analysis.lang_engine);
+      item->analysis.lang_engine = g_object_ref (item->analysis.font);
+    }
+
   g_object_unref (item->analysis.font);
   item->analysis.font = pango_font_map_load_font (context->font_map, context, desc);
 
@@ -1178,9 +1190,10 @@ apply_font_scale (PangoContext *context,
     {
       PangoItem *item = l->data;
       double scale;
+      gboolean is_small_caps;
 
-      if (collect_font_scale (context, &stack, item, prev, &scale))
-        apply_scale_to_item (context, item, scale);
+      if (collect_font_scale (context, &stack, item, prev, &scale, &is_small_caps))
+        apply_scale_to_item (context, item, scale, is_small_caps);
 
       prev = item;
     }
diff --git a/pango/pango-item.c b/pango/pango-item.c
index 2b2664c5..acda5eac 100644
--- a/pango/pango-item.c
+++ b/pango/pango-item.c
@@ -68,6 +68,9 @@ pango_item_copy (PangoItem *item)
     ((PangoItemPrivate *)result)->char_offset = ((PangoItemPrivate *)item)->char_offset;
 
   result->analysis = item->analysis;
+  if (result->analysis.lang_engine)
+    g_object_ref (result->analysis.lang_engine);
+
   if (result->analysis.font)
     g_object_ref (result->analysis.font);
 
@@ -102,6 +105,9 @@ pango_item_free (PangoItem *item)
       g_slist_free (item->analysis.extra_attrs);
     }
 
+  if (item->analysis.lang_engine)
+    g_object_unref (item->analysis.lang_engine);
+
   if (item->analysis.font)
     g_object_unref (item->analysis.font);
 
diff --git a/pango/pango-item.h b/pango/pango-item.h
index 8122be19..f0eebc01 100644
--- a/pango/pango-item.h
+++ b/pango/pango-item.h
@@ -61,8 +61,8 @@ typedef struct _PangoItem PangoItem;
 
 /**
  * PangoAnalysis:
- * @shape_engine: unused
- * @lang_engine: unused
+ * @shape_engine: unused, reserved
+ * @lang_engine: unused, reserved
  * @font: the font for this segment.
  * @level: the bidirectional level for this segment.
  * @gravity: the glyph orientation for this segment (A `PangoGravity`).
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index a3f8b552..6af480f6 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -5514,10 +5514,7 @@ pango_layout_get_empty_extents_and_height_at_index (PangoLayout    *layout,
                       free_font_desc = TRUE;
                     }
 
-                  pango_attr_iterator_get_font (&iter,
-                                                font_desc,
-                                                NULL,
-                                                NULL);
+                  pango_attr_iterator_get_font (&iter, font_desc, NULL, NULL);
 
                   attr = pango_attr_iterator_get (&iter, PANGO_ATTR_LINE_HEIGHT);
                   if (attr)
@@ -5685,11 +5682,25 @@ pango_layout_run_get_extents_and_height (PangoLayoutRun *run,
 
   if (height)
     {
-      if (!metrics)
-        metrics = pango_font_get_metrics (run->item->analysis.font,
-                                          run->item->analysis.language);
+      if (run->item->analysis.lang_engine)
+        {
+          PangoFontMetrics *height_metrics;
+
+          height_metrics = pango_font_get_metrics (PANGO_FONT (run->item->analysis.lang_engine),
+                                                   run->item->analysis.language);
+
+          *height = pango_font_metrics_get_height (height_metrics);
 
-      *height = pango_font_metrics_get_height (metrics);
+          pango_font_metrics_unref (height_metrics);
+        }
+      else
+        {
+          if (!metrics)
+            metrics = pango_font_get_metrics (run->item->analysis.font,
+                                              run->item->analysis.language);
+
+          *height = pango_font_metrics_get_height (metrics);
+        }
     }
 
   y_offset = run->y_offset;


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