Skip to content

Commit b81e043

Browse files
committed
Added ALIGN_JUSTIFY
Fixes #53
1 parent 33cbfde commit b81e043

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ The `settings` table can contain the following values:
148148
* `color` (vector4) - The default color of text. Will be white if not specified.
149149
* `shadow` (vector4) - The default shadow color of text. Will be transparent if not specified.
150150
* `outline` (vector4) - The default outline color of text. Will be transparent if not specified.
151-
* `align` (hash) - One of `richtext.ALIGN_LEFT`, `richtext.ALIGN_CENTER` and `richtext.ALIGN_RIGHT`. Defaults to `richtext.ALIGN_LEFT`. Defines how the words of a line of text are positioned in relation the provided `position`.
151+
* `align` (hash) - One of `richtext.ALIGN_LEFT`, `richtext.ALIGN_CENTER`, `richtext.ALIGN_RIGHT` and `richtext.ALIGN_JUSTIFY`. Defaults to `richtext.ALIGN_LEFT`. Defines how the words of a line of text are positioned in relation the provided `position`. Width must be specified for `richtext.ALIGN_JUSTIFY`.
152152
* `line_spacing` (number) - Value to multiply line height with. Set to a value lower than 1.0 to reduce space between lines and a value higher than 1.0 to increase space between lines. Defaults to 1.0.
153153
* `image_pixel_grid_snap` (boolean) - Set to true to position image on full pixels (positions rounded to nearest integer) to avoid effects of anti-aliasing. Defaults to false.
154154
* `combine_words` (boolean) - Set to true to combine words with the same style on a line into a single node. This is useful for very long texts where the maximum number of nodes would exceed the limit set in the GUI. The disadvantage is that any per word operations will not work since the words have been combined.
@@ -276,6 +276,9 @@ Center text. The words of a line are centered on the specified position (see `ri
276276
### richtext.ALIGN_RIGHT
277277
Right-align text. The words of a line ends at the specified position (see `richtext.create` settings above).
278278

279+
### richtext.ALIGN_JUSTIFT
280+
Justify text. The words of a line start at the specified position and are spaced such that the last character of the last word ends at the right edge of the line bounds (see `richtext.create` settings above).
281+
279282

280283
# Credits
281284
* Smiley icons in example app from [Flaticons](https://www.flaticon.com/packs/smileys-3)

example/example.gui_script

+5-2
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ end
6464

6565

6666
local function create_align_example()
67+
local settings_align_justify = { position = vmath.vector3(10, 420, 0), align = richtext.ALIGN_JUSTIFY, width = 300 }
68+
local justify = richtext.create("Justify this text\nDo it for both lines", "Roboto-Regular", settings_align_justify)
69+
6770
local settings_align_left = { position = vmath.vector3(10, 310, 0), align = richtext.ALIGN_LEFT }
68-
local left = richtext.create("Left align this text\nDo it for both lines", "Roboto-Regular", settings_align_leftb)
71+
local left = richtext.create("Left align this text\nDo it for both lines", "Roboto-Regular", settings_align_left)
6972

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

7376
local settings_align_center = { position = vmath.vector3(320, 90, 0), align = richtext.ALIGN_CENTER }
7477
local center = richtext.create("Center words around the specified position\nAnd these words as well", "Roboto-Regular", settings_align_center)
75-
return merge(left, right, center)
78+
return merge(justify, left, right, center)
7679
end
7780

7881

richtext/richtext.lua

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local M = {}
66
M.ALIGN_CENTER = hash("ALIGN_CENTER")
77
M.ALIGN_LEFT = hash("ALIGN_LEFT")
88
M.ALIGN_RIGHT = hash("ALIGN_RIGHT")
9+
M.ALIGN_JUSTIFY = hash("ALIGN_JUSTIFY")
910

1011

1112
local V4_ZERO = vmath.vector4(0)
@@ -130,6 +131,15 @@ local function position_words(words, line_width, line_height, position, settings
130131
position.x = position.x - line_width / 2
131132
end
132133

134+
local spacing = 0
135+
if settings.align == M.ALIGN_JUSTIFY and #words > 1 then
136+
local words_width = 0
137+
for i=1,#words do
138+
local word = words[i]
139+
words_width = words_width + word.metrics.total_width
140+
end
141+
spacing = (settings.width - words_width) / (#words - 1)
142+
end
133143
for i=1,#words do
134144
local word = words[i]
135145
-- align spine animations to bottom of line since
@@ -143,7 +153,7 @@ local function position_words(words, line_width, line_height, position, settings
143153
else
144154
gui.set_position(word.node, position)
145155
end
146-
position.x = position.x + word.metrics.total_width
156+
position.x = position.x + word.metrics.total_width + spacing
147157
words[i] = nil
148158
end
149159
end
@@ -315,7 +325,10 @@ function M.create(text, font, settings)
315325
settings.line_spacing = settings.line_spacing or 1
316326
settings.image_pixel_grid_snap = settings.image_pixel_grid_snap or false
317327
settings.combine_words = settings.combine_words or false
318-
328+
if settings.align == M.ALIGN_JUSTIFY and not settings.width then
329+
error("Width must be specified if text should be justified")
330+
end
331+
319332
-- default settings for a word
320333
-- will be assigned to each word unless tags override the values
321334
local word_settings = {

0 commit comments

Comments
 (0)