diff --git a/gtk/gtkinscription.c b/gtk/gtkinscription.c index c8937df9e9..f44937fa69 100644 --- a/gtk/gtkinscription.c +++ b/gtk/gtkinscription.c @@ -233,6 +233,24 @@ gtk_inscription_set_property (GObject *object, } } +static void +update_pango_alignment (GtkInscription *self) +{ + PangoAlignment align; + gboolean ltr; + + ltr = _gtk_widget_get_direction (GTK_WIDGET (self)) != GTK_TEXT_DIR_RTL; + + if (self->xalign < 0.33) + align = ltr ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT; + else if (self->xalign < 0.67) + align = PANGO_ALIGN_CENTER; + else + align = ltr ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT; + + pango_layout_set_alignment (self->layout, align); +} + static void gtk_inscription_update_layout_attributes (GtkInscription *self, PangoAttrList *css_attrs) @@ -267,6 +285,17 @@ gtk_inscription_css_changed (GtkWidget *widget, } } +static void +gtk_inscription_direction_changed (GtkWidget *widget, + GtkTextDirection previous_direction) +{ + GtkInscription *self = GTK_INSCRIPTION (widget); + + GTK_WIDGET_CLASS (gtk_inscription_parent_class)->direction_changed (widget, previous_direction); + + update_pango_alignment (self); +} + static PangoFontMetrics * gtk_inscription_get_font_metrics (GtkInscription *self) { @@ -395,7 +424,10 @@ gtk_inscription_get_layout_location (GtkInscription *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_allocated_baseline (widget); if (baseline != -1) @@ -444,7 +476,17 @@ gtk_inscription_allocate (GtkWidget *widget, PangoRectangle rect; pango_layout_iter_get_line_extents (iter, NULL, &rect); if (rect.y + rect.height > height * PANGO_SCALE) - pango_layout_set_width (self->layout, -1); + { + while (!pango_layout_line_is_paragraph_start (pango_layout_iter_get_line_readonly (iter))) + { + if (!pango_layout_iter_next_line (iter)) + break; + } + if (!pango_layout_line_is_paragraph_start (pango_layout_iter_get_line_readonly (iter))) + { + pango_layout_set_width (self->layout, -1); + } + } } } break; @@ -491,6 +533,7 @@ gtk_inscription_class_init (GtkInscriptionClass *klass) gobject_class->set_property = gtk_inscription_set_property; widget_class->css_changed = gtk_inscription_css_changed; + widget_class->direction_changed = gtk_inscription_direction_changed; widget_class->measure = gtk_inscription_measure; widget_class->size_allocate = gtk_inscription_allocate; widget_class->snapshot = gtk_inscription_snapshot; @@ -700,6 +743,7 @@ gtk_inscription_init (GtkInscription *self) self->layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), NULL); pango_layout_set_wrap (self->layout, PANGO_WRAP_WORD_CHAR); + update_pango_alignment (self); } /* for a11y */ @@ -988,6 +1032,8 @@ gtk_inscription_set_xalign (GtkInscription *self, self->xalign = xalign; + update_pango_alignment (self); + gtk_widget_queue_draw (GTK_WIDGET (self)); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_XALIGN]); diff --git a/testsuite/reftests/inscription-xalign-rtl.ref.ui b/testsuite/reftests/inscription-xalign-rtl.ref.ui new file mode 100644 index 0000000000..5d31f893ba --- /dev/null +++ b/testsuite/reftests/inscription-xalign-rtl.ref.ui @@ -0,0 +1,52 @@ + + + + 200 + 0 + + + vertical + + + עִברִית +English + 2 + 0.0 + + + + + עִברִית +English + 2 + 0.25 + + + + + עִברִית +English + 2 + 0.5 + + + + + עִברִית +English + 2 + 0.75 + + + + + עִברִית +English + 2 + 1.0 + + + + + + diff --git a/testsuite/reftests/inscription-xalign-rtl.ui b/testsuite/reftests/inscription-xalign-rtl.ui new file mode 100644 index 0000000000..a8e469f4c8 --- /dev/null +++ b/testsuite/reftests/inscription-xalign-rtl.ui @@ -0,0 +1,54 @@ + + + + 200 + 0 + + + vertical + + + + + עִברִית +English + 2 + 0.0 + + + + + עִברִית +English + 2 + 0.25 + + + + + עִברִית +English + 2 + 0.5 + + + + + עִברִית +English + 2 + 0.75 + + + + + עִברִית +English + 2 + 1.0 + + + + + + diff --git a/testsuite/reftests/inscription-xalign.ref.ui b/testsuite/reftests/inscription-xalign.ref.ui new file mode 100644 index 0000000000..08a8ec524b --- /dev/null +++ b/testsuite/reftests/inscription-xalign.ref.ui @@ -0,0 +1,42 @@ + + + + 200 + 0 + + + vertical + + + align +0.0 + word + 1 + 0 + left + + + + + align +0.5 + word + 1 + 0.5 + center + + + + + align +1.0 + word + 1 + 1 + right + + + + + + diff --git a/testsuite/reftests/inscription-xalign.ui b/testsuite/reftests/inscription-xalign.ui new file mode 100644 index 0000000000..d5ae41f3f5 --- /dev/null +++ b/testsuite/reftests/inscription-xalign.ui @@ -0,0 +1,39 @@ + + + + 200 + 0 + + + vertical + + + align +0.0 + word + 2 + 0 + + + + + align +0.5 + word + 2 + 0.5 + + + + + align +1.0 + word + 2 + 1 + + + + + + diff --git a/testsuite/reftests/meson.build b/testsuite/reftests/meson.build index eecb7f91ad..25fb13ab0d 100644 --- a/testsuite/reftests/meson.build +++ b/testsuite/reftests/meson.build @@ -375,6 +375,10 @@ testdata = [ 'inscription-overflow-multiline.ui', 'inscription-overflow-yalign.ref.ui', 'inscription-overflow-yalign.ui', + 'inscription-xalign.ref.ui', + 'inscription-xalign.ui', + 'inscription-xalign-rtl.ref.ui', + 'inscription-xalign-rtl.ui', 'label-attribute-preference.css', 'label-attribute-preference.ref.ui', 'label-attribute-preference.ui',