From 8a42a3bf54a5fb749e258e170d53d6598f26cec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Thu, 3 Oct 2024 10:13:05 +0200 Subject: [PATCH] Fix justified text Fixes #90 --- example/example.gui_script | 10 +++++----- richtext/richtext.lua | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/example/example.gui_script b/example/example.gui_script index 2436faa..d6e46a5 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -88,16 +88,16 @@ local function create_align_example() local valign_middle = richtext.create("Align EVERYTHING to\nthe MIDDLEof each line", "Roboto-Regular", settings_valign_middle) local settings_align_justify = { position = vmath.vector3(10, 420, 0), align = richtext.ALIGN_JUSTIFY, width = 300 } - local justify = richtext.create("Justify this text\nDo it for both lines", "Roboto-Regular", settings_align_justify) + 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) - local settings_align_left = { position = vmath.vector3(10, 310, 0), align = richtext.ALIGN_LEFT } - local left = richtext.create("Left align this text\nDo it for both lines", "Roboto-Regular", settings_align_left) + local settings_align_left = { position = vmath.vector3(10, 200, 0), align = richtext.ALIGN_LEFT } + local left = richtext.create("Left align this text.\nDo it for both lines.", "Roboto-Regular", settings_align_left) local settings_align_right = { position = vmath.vector3(640, 200, 0), align = richtext.ALIGN_RIGHT } - local right = richtext.create("Right align this text\nDo it for both lines", "Roboto-Regular", settings_align_right) + local right = richtext.create("Right align this text.\nDo it for both lines.", "Roboto-Regular", settings_align_right) local settings_align_center = { position = vmath.vector3(320, 90, 0), align = richtext.ALIGN_CENTER } - local center = richtext.create("Center words around the specified position\nAnd these words as well", "Roboto-Regular", settings_align_center) + local center = richtext.create("Center words around the specified position.\nAnd these words as well.", "Roboto-Regular", settings_align_center) return merge(justify, left, right, center, valign_top, valign_middle, valign_bottom) end diff --git a/richtext/richtext.lua b/richtext/richtext.lua index f529f0c..d88b813 100755 --- a/richtext/richtext.lua +++ b/richtext/richtext.lua @@ -119,7 +119,7 @@ end -- position all words according to the line alignment and line width -- the list of words will be empty after this function is called -local function position_words(words, line_width, line_height, position, settings) +local function position_words(words, line_width, line_height, position, settings, line_break) if settings.align == M.ALIGN_RIGHT then position.x = position.x - line_width elseif settings.align == M.ALIGN_CENTER then @@ -127,7 +127,7 @@ local function position_words(words, line_width, line_height, position, settings end local spacing = 0 - if settings.align == M.ALIGN_JUSTIFY then + if settings.align == M.ALIGN_JUSTIFY and not line_break then local words_width = 0 local word_count = 0 for i=1,#words do @@ -481,7 +481,7 @@ function M.create(text, font, settings) text_metrics.height = text_metrics.height + (line_height * line_increment_before * settings.line_spacing) position.x = settings.position.x position.y = settings.position.y - text_metrics.height - position_words(line_words, line_width, line_height, position, settings) + position_words(line_words, line_width, line_height, position, settings, false) -- add the word that didn't fit to the next line instead line_words[#line_words + 1] = word @@ -524,7 +524,7 @@ function M.create(text, font, settings) text_metrics.height = text_metrics.height + (line_height * line_increment_before * settings.line_spacing) position.x = settings.position.x position.y = settings.position.y - text_metrics.height - position_words(line_words, line_width, line_height, position, settings) + position_words(line_words, line_width, line_height, position, settings, true) -- update text metrics 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) text_metrics.height = text_metrics.height + (line_height * line_increment_before * settings.line_spacing) position.x = settings.position.x position.y = settings.position.y - text_metrics.height - position_words(line_words, line_width, line_height, position, settings) + position_words(line_words, line_width, line_height, position, settings, true) text_metrics.height = text_metrics.height + (line_height * line_increment_after * settings.line_spacing) end