[pango/line-breaker: 23/31] Tweak PangoAlignment




commit 0f52943d6228646d2bc6c48426650fc318d1694c
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jan 23 22:06:28 2022 -0500

    Tweak PangoAlignment
    
    Remove the justify-all value, and drop the justify-last-line
    feature from PangoLayout. This can be done better using
    PangoLineBreaker now.
    
    On the flip side, add a 'natural' value, since that is what
    we want to fall back to for the last line in a justified
    paragraph. And CoreText has this value too.
    
    Make PANGO_ALIGN_NATURAL the default.

 pango/pango-layout.c | 41 +++++++++++++++++++++++++++++------------
 pango/pango-types.h  | 11 +++++------
 2 files changed, 34 insertions(+), 18 deletions(-)
---
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index f5587a6c..e2075afd 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -72,7 +72,7 @@ pango_layout_init (PangoLayout *layout)
   layout->height = -1;
   layout->indent = 0;
   layout->wrap = PANGO_WRAP_WORD;
-  layout->alignment = PANGO_ALIGN_LEFT;
+  layout->alignment = PANGO_ALIGN_NATURAL;
   layout->ellipsize = PANGO_ELLIPSIZE_NONE;
   layout->line_spacing = 0.0;
   layout->auto_dir = TRUE;
@@ -409,11 +409,11 @@ pango_layout_class_init (PangoLayoutClass *class)
    *
    * The alignment mode of this `PangoLayout.
    *
-   * The default value is `PANGO_ALIGNMENT_LEFT`.
+   * The default value is `PANGO_ALIGN_NATURAL`.
    */
   props[PROP_ALIGNMENT] = g_param_spec_enum ("alignment", "alignment", "alignment",
                                              PANGO_TYPE_ALIGNMENT,
-                                             PANGO_ALIGN_LEFT,
+                                             PANGO_ALIGN_NATURAL,
                                              G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
   /**
@@ -594,23 +594,40 @@ retry:
       offset = 0;
       switch (layout->alignment)
         {
+
         case PANGO_ALIGN_LEFT:
           break;
+
         case PANGO_ALIGN_CENTER:
           if (ext.width < width)
             offset = (width - ext.width) / 2;
           break;
+
+        case PANGO_ALIGN_JUSTIFY:
+          if (!pango_layout_line_ends_paragraph (line))
+            {
+              line = pango_layout_line_justify (line, width);
+              break;
+            }
+          G_GNUC_FALLTHROUGH;
+
+        case PANGO_ALIGN_NATURAL:
+          {
+            PangoLayoutLine *first_line;
+            if (pango_lines_get_line_count (layout->lines) > 0)
+              first_line = pango_lines_get_line (layout->lines, 0, NULL, NULL);
+            else
+              first_line = line;
+            if (pango_layout_line_get_resolved_direction (first_line) == PANGO_DIRECTION_LTR)
+              break;
+          }
+          G_GNUC_FALLTHROUGH;
+
         case PANGO_ALIGN_RIGHT:
           if (ext.width < width)
             offset = width - ext.width;
           break;
-        case PANGO_ALIGN_JUSTIFY:
-          if (!pango_layout_line_ends_paragraph (line))
-            line = pango_layout_line_justify (line, width);
-          break;
-        case PANGO_ALIGN_JUSTIFY_ALL:
-          line = pango_layout_line_justify (line, width);
-          break;
+
         default: g_assert_not_reached ();
         }
 
@@ -1269,7 +1286,7 @@ pango_layout_get_indent (PangoLayout *layout)
  * Sets the alignment for the layout: how short lines are
  * positioned within the horizontal space available.
  *
- * The default alignment is `PANGO_ALIGN_LEFT`.
+ * The default alignment is `PANGO_ALIGN_NATURAL`.
  */
 void
 pango_layout_set_alignment (PangoLayout    *layout,
@@ -1298,7 +1315,7 @@ pango_layout_set_alignment (PangoLayout    *layout,
 PangoAlignment
 pango_layout_get_alignment (PangoLayout *layout)
 {
-  g_return_val_if_fail (PANGO_IS_LAYOUT (layout), PANGO_ALIGN_LEFT);
+  g_return_val_if_fail (PANGO_IS_LAYOUT (layout), PANGO_ALIGN_NATURAL);
 
   return layout->alignment;
 }
diff --git a/pango/pango-types.h b/pango/pango-types.h
index 9643245e..82892928 100644
--- a/pango/pango-types.h
+++ b/pango/pango-types.h
@@ -245,10 +245,9 @@ void pango_extents_to_pixels (PangoRectangle *inclusive,
  * @PANGO_ALIGN_LEFT: Put all available space on the right
  * @PANGO_ALIGN_CENTER: Center the line within the available space
  * @PANGO_ALIGN_RIGHT: Put all available space on the left
- * @PANGO_ALIGN_JUSTIFY: Justify the content to fill the available
- *   space, unless the line ends the paragraph
- * @PANGO_ALIGN_JUSTIFY_ALL: Justify the content to fill the available
- *   space, even if the line ends the paragraph
+ * @PANGO_ALIGN_NATURAL: Use left or right alignment, depending
+ *   on the text direction of the paragraph
+ * @PANGO_ALIGN_JUSTIFY: Justify the content to fill the available space
  *
  * `PangoAlignment` describes how to align the lines of a `PangoLayout`
  * within the available space.
@@ -258,8 +257,8 @@ typedef enum
   PANGO_ALIGN_LEFT,
   PANGO_ALIGN_CENTER,
   PANGO_ALIGN_RIGHT,
-  PANGO_ALIGN_JUSTIFY,
-  PANGO_ALIGN_JUSTIFY_ALL,
+  PANGO_ALIGN_NATURAL,
+  PANGO_ALIGN_JUSTIFY
 } PangoAlignment;
 
 /**


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