Skip to content

Commit 8a42a3b

Browse files
committed
Fix justified text
Fixes #90
1 parent 20a7fbb commit 8a42a3b

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

example/example.gui_script

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ local function create_align_example()
8888
local valign_middle = richtext.create("Align <size=2>EVERYTHING</size> to\nthe <size=0.5>MIDDLE</size>of each line", "Roboto-Regular", settings_valign_middle)
8989

9090
local settings_align_justify = { position = vmath.vector3(10, 420, 0), align = richtext.ALIGN_JUSTIFY, width = 300 }
91-
local justify = richtext.create("Justify this text\nDo it for both lines", "Roboto-Regular", settings_align_justify)
91+
local justify = richtext.create("Justify this multi-line and very long text but ignore the line with the line-break.\nAlso ignore last line.", "Roboto-Regular", settings_align_justify)
9292

93-
local settings_align_left = { position = vmath.vector3(10, 310, 0), align = richtext.ALIGN_LEFT }
94-
local left = richtext.create("Left align this text\nDo it for both lines", "Roboto-Regular", settings_align_left)
93+
local settings_align_left = { position = vmath.vector3(10, 200, 0), align = richtext.ALIGN_LEFT }
94+
local left = richtext.create("Left align this text.\nDo it for both lines.", "Roboto-Regular", settings_align_left)
9595

9696
local settings_align_right = { position = vmath.vector3(640, 200, 0), align = richtext.ALIGN_RIGHT }
97-
local right = richtext.create("Right align this text\nDo it for both lines", "Roboto-Regular", settings_align_right)
97+
local right = richtext.create("Right align this text.\nDo it for both lines.", "Roboto-Regular", settings_align_right)
9898

9999
local settings_align_center = { position = vmath.vector3(320, 90, 0), align = richtext.ALIGN_CENTER }
100-
local center = richtext.create("Center words around the specified position\nAnd these words as well", "Roboto-Regular", settings_align_center)
100+
local center = richtext.create("Center words around the specified position.\nAnd these words as well.", "Roboto-Regular", settings_align_center)
101101
return merge(justify, left, right, center, valign_top, valign_middle, valign_bottom)
102102
end
103103

richtext/richtext.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ end
119119

120120
-- position all words according to the line alignment and line width
121121
-- the list of words will be empty after this function is called
122-
local function position_words(words, line_width, line_height, position, settings)
122+
local function position_words(words, line_width, line_height, position, settings, line_break)
123123
if settings.align == M.ALIGN_RIGHT then
124124
position.x = position.x - line_width
125125
elseif settings.align == M.ALIGN_CENTER then
126126
position.x = position.x - line_width / 2
127127
end
128128

129129
local spacing = 0
130-
if settings.align == M.ALIGN_JUSTIFY then
130+
if settings.align == M.ALIGN_JUSTIFY and not line_break then
131131
local words_width = 0
132132
local word_count = 0
133133
for i=1,#words do
@@ -481,7 +481,7 @@ function M.create(text, font, settings)
481481
text_metrics.height = text_metrics.height + (line_height * line_increment_before * settings.line_spacing)
482482
position.x = settings.position.x
483483
position.y = settings.position.y - text_metrics.height
484-
position_words(line_words, line_width, line_height, position, settings)
484+
position_words(line_words, line_width, line_height, position, settings, false)
485485

486486
-- add the word that didn't fit to the next line instead
487487
line_words[#line_words + 1] = word
@@ -524,7 +524,7 @@ function M.create(text, font, settings)
524524
text_metrics.height = text_metrics.height + (line_height * line_increment_before * settings.line_spacing)
525525
position.x = settings.position.x
526526
position.y = settings.position.y - text_metrics.height
527-
position_words(line_words, line_width, line_height, position, settings)
527+
position_words(line_words, line_width, line_height, position, settings, true)
528528

529529
-- update text metrics
530530
text_metrics.height = text_metrics.height + (line_height * line_increment_after * settings.line_spacing) + paragraph_spacing
@@ -541,7 +541,7 @@ function M.create(text, font, settings)
541541
text_metrics.height = text_metrics.height + (line_height * line_increment_before * settings.line_spacing)
542542
position.x = settings.position.x
543543
position.y = settings.position.y - text_metrics.height
544-
position_words(line_words, line_width, line_height, position, settings)
544+
position_words(line_words, line_width, line_height, position, settings, true)
545545
text_metrics.height = text_metrics.height + (line_height * line_increment_after * settings.line_spacing)
546546
end
547547

0 commit comments

Comments
 (0)