Skip to content

Commit 5a4950a

Browse files
authored
Added support for manually sized images (#76)
* Added support for manually sized images * cleaned up the text parsing in the img tag * missed a couple close tags in the readme
1 parent 9a70d3c commit 5a4950a

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ The following tags are supported:
4848
| font | Change font | `<font=MyCoolFont>Foobar</font>` |
4949
| i | The text should be italic | `<i>Foobar</i>` |
5050
| img | Display image | `<img=texture:image/>` |
51+
| | Display image in fixed square | `<img=texture:image,size/>` |
52+
| | Display image in fixed rectangle | `<img=texture:image,width,height/>` |
5153
| nobr | Prevent the text from breaking | `Words <nobr>inside tag</nobr> won't break` |
5254
| size | Change text size, relative to default size | `<size=2>Twice as large</size>` |
5355
| spine | Display spine model | `<spine=scene:anim/>` |
5456
| p | Adds extra spacing below the line where this | `<p>A paragraph</p>\nSome other text` |
55-
| | tag ends. Adds a newline before its opening | `<p=2.5>This has 2.5 lines of spacing<p>` |
57+
| | tag ends. Adds a newline before its opening | `<p=2.5>This has 2.5 lines of spacing</p>` |
5658
| | tag if it doesn't already exist. | |
5759
| repeat | Repeat the text enclosed by the tag | `<repeat=5>Echo </repeat> five times` |
5860

richtext/richtext.lua

+19-2
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,29 @@ function M.length(text)
184184
end
185185

186186

187+
local size_vector = vmath.vector3()
187188
local function create_box_node(word)
188189
local node = gui.new_box_node(V3_ZERO, V3_ZERO)
190+
local word_image = word.image
191+
local image_width = word_image.width
192+
local image_height = word_image.height
189193
gui.set_id(node, new_id("box"))
190-
gui.set_size_mode(node, gui.SIZE_MODE_AUTO)
194+
if image_width then
195+
gui.set_size_mode(node, gui.SIZE_MODE_MANUAL)
196+
size_vector.x = image_width
197+
size_vector.y = image_height
198+
size_vector.z = 0
199+
gui.set_size(node, size_vector)
200+
else
201+
gui.set_size_mode(node, gui.SIZE_MODE_AUTO)
202+
end
191203
gui.set_texture(node, word.image.texture)
192-
gui.set_scale(node, vmath.vector3(word.size))
204+
local word_size = word.size
205+
size_vector.x = word_size
206+
size_vector.y = word_size
207+
size_vector.z = word_size
208+
gui.set_scale(node, size_vector)
209+
193210
gui.play_flipbook(node, hash(word.image.anim))
194211

195212
-- get metrics of node based on image size

richtext/tags.lua

+24-2
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,33 @@ M.register("nobr", function(params, settings)
6363
settings.nobr = true
6464
end)
6565

66+
-- Split string at first occurrence of token
67+
-- If the token doesn't exist the whole string is returned
68+
-- @param s The string to split
69+
-- @param token The token to split string on
70+
-- @return before The string before the token or the whole string if token doesn't exist
71+
-- @return after The string after the token or nul
72+
local function split(s, token)
73+
if not s then return nil, nil end
74+
local before, after = s:match("(.-)" .. token .. "(.*)")
75+
before = before or s
76+
return before, after
77+
end
78+
6679
M.register("img", function(params, settings)
67-
local texture, anim = params:match("(.-):(.*)")
80+
local texture_and_anim, params = split(params, ",")
81+
local width, params = split(params, ",")
82+
local height, params = split(params, ",")
83+
local texture, anim = split(texture_and_anim, ":")
84+
85+
width = width and tonumber(width)
86+
height = height and tonumber(height) or width
87+
6888
settings.image = {
6989
texture = texture,
70-
anim = anim
90+
anim = anim,
91+
width = width,
92+
height = height
7193
}
7294
end)
7395

0 commit comments

Comments
 (0)