label: Let Pango align the layout
authorEmmanuele Bassi <ebassi@gnome.org>
Sun, 30 Jul 2023 10:26:27 +0000 (13:26 +0300)
committerEmmanuele Bassi <ebassi@gnome.org>
Sun, 30 Jul 2023 10:26:27 +0000 (13:26 +0300)
Just like GtkInscription does since commit 883011f2. The layout offsets
are maintained as floats, and only converted to integers when exposing
them to callers.

gtk/gtklabel.c

index f223bae0d350598928eaac47753996447c1d7d2e..9f1d993b0d1af2a98935ee87151042c10fedd868 100644 (file)
@@ -1313,8 +1313,8 @@ gtk_label_measure (GtkWidget      *widget,
 
 static void
 get_layout_location (GtkLabel  *self,
-                     int       *xp,
-                     int       *yp)
+                     float     *xp,
+                     float     *yp)
 {
   GtkWidget *widget = GTK_WIDGET (self);
   const int widget_width = gtk_widget_get_width (widget);
@@ -1322,7 +1322,7 @@ get_layout_location (GtkLabel  *self,
   PangoRectangle logical;
   float xalign;
   int baseline;
-  int x, y;
+  float x, y;
 
   g_assert (xp);
   g_assert (yp);
@@ -1333,7 +1333,10 @@ get_layout_location (GtkLabel  *self,
     xalign = 1.0 - xalign;
 
   pango_layout_get_pixel_extents (self->layout, NULL, &logical);
-  x = floor ((xalign * (widget_width - logical.width)) - logical.x);
+  if (pango_layout_get_width (self->layout) > 0)
+    x = 0.f;
+  else
+    x = floor ((xalign * (widget_width - logical.width)) - logical.x);
 
   baseline = gtk_widget_get_baseline (widget);
   if (baseline != -1)
@@ -1382,7 +1385,7 @@ gtk_label_snapshot (GtkWidget   *widget,
   GtkLabel *self = GTK_LABEL (widget);
   GtkLabelSelectionInfo *info;
   GtkCssStyle *style;
-  int lx, ly;
+  float lx, ly;
   int width, height;
   GtkCssBoxes boxes;
 
@@ -1705,7 +1708,7 @@ get_layout_index (GtkLabel *self,
   const char *cluster;
   const char *cluster_end;
   gboolean inside;
-  int lx, ly;
+  float lx, ly;
 
   *index = 0;
 
@@ -5247,17 +5250,17 @@ gtk_label_get_layout_offsets (GtkLabel *self,
                               int      *x,
                               int      *y)
 {
-  int local_x, local_y;
+  float local_x, local_y;
   g_return_if_fail (GTK_IS_LABEL (self));
 
   gtk_label_ensure_layout (self);
   get_layout_location (self, &local_x, &local_y);
 
   if (x)
-    *x = local_x;
+    *x = (int) local_x;
 
   if (y)
-    *y = local_y;
+    *y = (int) local_y;
 }
 
 /**