---
 ChangeLog            |   11 ++++++
 pango/pango-layout.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 96 insertions(+), 1 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0568cd4..f65c809 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2007-04-26  Mathias Hasselmann  <mathias.hasselmann@gmx.de>
 
+	* pango/pango-layout.c (pango_layout_set_justify), 
+	(pango_layout_line_postprocess), (is_end_of_paragraph), 
+	(justify_words):
+	Add very primitive justification support. It does nothing but
+	expanding word spaces. It will fail when tabs are involved. It
+	probably also will fail for most non-latin scripts. Well, but better
+	some some half-assed implementation which covers the most common
+	latin use-cases, than no implementation at all.
+
+2007-04-26  Mathias Hasselmann  <mathias.hasselmann@gmx.de>
+
 	* pango-view/IPSUM.txt:
 	Adding pseudo text for testing justification on latin scripts.
 
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index 18b67e1..f62c9b6 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -622,7 +622,8 @@ pango_layout_get_font_description (PangoLayout *layout)
  * the justification may be done in more complex ways, like extending
  * the characters.
  *
- * Note that as of Pango-1.16, this functionality is not yet implemented.
+ * Note that as of Pango-1.16.4, this functionality only works for
+ * latin scripts.
  **/
 void
 pango_layout_set_justify (PangoLayout *layout,
@@ -4684,6 +4685,84 @@ adjust_line_letter_spacing (PangoLayoutLine *line)
     line->runs = g_slist_reverse (line->runs);
 }
 
+static gboolean
+is_end_of_paragraph (const PangoLayout    *layout,
+                     const PangoLayoutRun *run,
+                     gint                  offset)
+{
+  offset += run->glyphs->num_glyphs + 1;
+
+  return offset >= layout->n_chars 
+      || layout->log_attrs[offset].is_line_break;
+}
+
+static void
+justify_words (PangoLayoutLine *line,
+	       ParaBreakState  *state)
+{
+  int line_offset, offset, remaining;
+  int space_count, current_space, padding;
+  GSList *iter;
+
+  if (line->layout->width < 0)
+    return;
+
+  g_assert (NULL != line->layout->log_attrs);
+
+  space_count = 0;
+  remaining = line->layout->width;
+
+  line_offset = g_utf8_pointer_to_offset (line->layout->text,
+                                          line->layout->text + line->start_index);
+
+  for(iter = line->runs, offset = line_offset; iter; iter = iter->next)
+    {
+      PangoLayoutRun *run = iter->data;
+      int i;
+
+      if (is_end_of_paragraph (line->layout, run, offset))
+        continue;
+
+      for(i = 0; i < run->glyphs->num_glyphs; ++i) 
+        {
+          if (line->layout->log_attrs[offset + i].is_white)
+            ++space_count;
+      }
+
+      remaining -= pango_layout_run_get_width (run);
+      offset += run->glyphs->num_glyphs;
+    }
+
+  offset = line_offset;
+  current_space = 0;
+  padding = 0;
+
+  if (space_count)
+    {
+      for(iter = line->runs; iter; iter = iter->next)
+        {
+          PangoLayoutRun *run = iter->data;
+          PangoGlyphString *str = run->glyphs;
+          int i;
+
+          if (is_end_of_paragraph (line->layout, run, offset))
+            continue;
+
+          for(i = 0; i < str->num_glyphs; ++i) 
+            {
+              if (line->layout->log_attrs[offset + i].is_white)
+                {
+                  int next_padding = remaining * ++current_space / space_count;
+                  str->glyphs[i].geometry.width += next_padding - padding;
+                  padding = next_padding;
+                }
+            }
+
+          offset += str->num_glyphs;
+        }
+    }
+}
+
 static void
 pango_layout_line_postprocess (PangoLayoutLine *line,
 			       ParaBreakState  *state)
@@ -4707,6 +4786,11 @@ pango_layout_line_postprocess (PangoLayoutLine *line,
   /* Fixup letter spacing between runs
    */
   adjust_line_letter_spacing (line);
+
+  /* Fixup spacing between words when justification is requested
+   */
+  if (line->layout->justify)
+    justify_words (line, state);
 }
 
 static void
-- 
1.5.1.2