[gtk/label-sizing: 2/5] label: Never measure more than max-width-chars




commit ba44e7a228534ff066694ad97d25eaa23ec5f3af
Author: Benjamin Otte <otte redhat com>
Date:   Tue Oct 19 01:04:25 2021 +0200

    label: Never measure more than max-width-chars
    
    Even when we have tons of width available, still do the wrapping at
    max-width-chars.
    
    This is what happened in GTK3, too, but it happened automatically
    because GTK3 did for_size = MIN (for_size, nat_size) and GTK4 does not.
    
    So we do this manually in the label now.
    
    Fixes the label-sizing.ui reftest.

 gtk/gtklabel.c | 49 +++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 18 deletions(-)
---
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index e94615473d..661902a275 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -1017,6 +1017,23 @@ gtk_label_get_measuring_layout (GtkLabel    *self,
   return copy;
 }
 
+static int
+get_char_pixels (GtkWidget   *self,
+                 PangoLayout *layout)
+{
+  PangoContext *context;
+  PangoFontMetrics *metrics;
+  int char_width, digit_width;
+
+  context = pango_layout_get_context (layout);
+  metrics = pango_context_get_metrics (context, NULL, NULL);
+  char_width = pango_font_metrics_get_approximate_char_width (metrics);
+  digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
+  pango_font_metrics_unref (metrics);
+
+  return MAX (char_width, digit_width);
+}
+
 static void
 get_height_for_width (GtkLabel *self,
                       int       width,
@@ -1028,7 +1045,20 @@ get_height_for_width (GtkLabel *self,
   PangoLayout *layout;
   int text_height, baseline;
 
-  layout = gtk_label_get_measuring_layout (self, NULL, width * PANGO_SCALE);
+  width *= PANGO_SCALE;
+  if (self->max_width_chars > -1)
+    {
+      int char_pixels, width_chars;
+
+      layout = gtk_label_get_measuring_layout (self, NULL, -1);
+      char_pixels = get_char_pixels (GTK_WIDGET (self), layout);
+      if (self->width_chars > self->max_width_chars)
+        width_chars = self->width_chars;
+      else
+        width_chars = self->max_width_chars;
+      width = MIN (char_pixels * width_chars, width);
+    }
+  layout = gtk_label_get_measuring_layout (self, NULL, width);
 
   pango_layout_get_pixel_size (layout, NULL, &text_height);
 
@@ -1042,23 +1072,6 @@ get_height_for_width (GtkLabel *self,
   g_object_unref (layout);
 }
 
-static int
-get_char_pixels (GtkWidget   *self,
-                 PangoLayout *layout)
-{
-  PangoContext *context;
-  PangoFontMetrics *metrics;
-  int char_width, digit_width;
-
-  context = pango_layout_get_context (layout);
-  metrics = pango_context_get_metrics (context, NULL, NULL);
-  char_width = pango_font_metrics_get_approximate_char_width (metrics);
-  digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
-  pango_font_metrics_unref (metrics);
-
-  return MAX (char_width, digit_width);
-}
-
 static void
 gtk_label_get_preferred_layout_size (GtkLabel *self,
                                      PangoRectangle *smallest,


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