Merge branch 'wip/otte/for-main' into 'main'

label: word-char wrapping should word-wrap for natural size

Closes #4535

See merge request GNOME/gtk!4245
This commit is contained in:
Matthias Clasen 2021-12-17 02:11:50 +00:00
commit 47ac080565
4 changed files with 89 additions and 18 deletions

View File

@ -1158,6 +1158,34 @@ get_height_for_width (GtkLabel *self,
g_object_unref (layout);
}
static int
my_pango_layout_get_width_for_height (PangoLayout *layout,
int for_height,
int min,
int max)
{
int mid, text_width, text_height;
min = PANGO_PIXELS_CEIL (min);
max = PANGO_PIXELS_CEIL (max);
while (min < max)
{
mid = (min + max) / 2;
pango_layout_set_width (layout, mid * PANGO_SCALE);
pango_layout_get_size (layout, &text_width, &text_height);
text_width = PANGO_PIXELS_CEIL (text_width);
if (text_width > mid)
min = mid = text_width;
else if (text_height > for_height)
min = mid + 1;
else
max = mid;
}
return min * PANGO_SCALE;
}
static void
get_width_for_height (GtkLabel *self,
int height,
@ -1183,13 +1211,15 @@ get_width_for_height (GtkLabel *self,
}
else
{
int min, max, mid, text_width, text_height;
int min, max;
/* Can't use a measuring layout here, because we need to force
* ellipsizing mode */
gtk_label_ensure_layout (self);
layout = pango_layout_copy (self->layout);
pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_NONE);
if (self->wrap_mode == PANGO_WRAP_WORD_CHAR)
pango_layout_set_wrap (layout, PANGO_WRAP_WORD);
/* binary search for the smallest width where the height doesn't
* eclipse the given height */
@ -1198,23 +1228,7 @@ get_width_for_height (GtkLabel *self,
pango_layout_set_width (layout, -1);
pango_layout_get_size (layout, &max, NULL);
min = PANGO_PIXELS_CEIL (min);
max = PANGO_PIXELS_CEIL (max);
while (min < max)
{
mid = (min + max) / 2;
pango_layout_set_width (layout, mid * PANGO_SCALE);
pango_layout_get_size (layout, &text_width, &text_height);
text_width = PANGO_PIXELS_CEIL (text_width);
if (text_width > mid)
min = mid = text_width;
else if (text_height > height)
min = mid + 1;
else
max = mid;
}
*natural_width = min * PANGO_SCALE;
*natural_width = my_pango_layout_get_width_for_height (layout, height, min, max);
if (self->ellipsize != PANGO_ELLIPSIZE_NONE)
{
@ -1223,6 +1237,11 @@ get_width_for_height (GtkLabel *self,
pango_layout_get_size (layout, minimum_width, NULL);
*minimum_width = MAX (*minimum_width, minimum_default);
}
else if (self->wrap_mode == PANGO_WRAP_WORD_CHAR)
{
pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
*minimum_width = my_pango_layout_get_width_for_height (layout, height, min, *natural_width);
}
else
{
*minimum_width = *natural_width;

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkWindow">
<property name="default-width">300</property>
<property name="default-height">300</property>
<child>
<object class="GtkBox">
<property name="halign">center</property>
<property name="valign">center</property>
<child>
<object class="GtkLabel">
<property name="label">two
lines</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">unwrapped</property>
</object>
</child>
</object>
</child>
</object>
</interface>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkWindow">
<property name="default-width">300</property>
<property name="default-height">300</property>
<child>
<object class="GtkBox">
<property name="halign">center</property>
<property name="valign">center</property>
<child>
<object class="GtkLabel">
<property name="label">two
lines</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">unwrapped</property>
<property name="wrap">1</property>
<property name="wrap-mode">word-char</property>
</object>
</child>
</object>
</child>
</object>
</interface>

View File

@ -412,6 +412,8 @@ testdata = [
'label-width-chars-dont-shrink.ui',
'label-wrap-width-chars.ref.ui',
'label-wrap-width-chars.ui',
'label-wrap-word-char-natural-size.ref.ui',
'label-wrap-word-char-natural-size.ui',
'label-wrapped-huge-max-width-chars.ref.ui',
'label-wrapped-huge-max-width-chars.ui',
# this seems to make assumptions on text positioning