Skip to content

Commit

Permalink
Fix justified text
Browse files Browse the repository at this point in the history
Fixes #90
  • Loading branch information
britzl committed Oct 3, 2024
1 parent 20a7fbb commit 8a42a3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions example/example.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ local function create_align_example()
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)

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

Expand Down
10 changes: 5 additions & 5 deletions richtext/richtext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ 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
position.x = position.x - line_width / 2
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 8a42a3b

Please sign in to comment.