Skip to content

Commit 3341af6

Browse files
committed
2 parents 6be4f7c + 0be5355 commit 3341af6

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The RichText library has limited support for HTML entities (reserved characters)
8989
* `>` converts into `>`
9090
* `&lt;` converts into `<`
9191
* `&nbsp;` converts into a non-breaking space between two words, ie `100&nbsp;km` becomes `100 km` with `100` and `km` acting as a single word with a space between.
92-
92+
* `&zwsp;` converts into a zero-width space character (`\226\128\139`). This is not an official HTML entity but it is provided as a convenient way to insert one into text.
9393

9494
# Usage
9595
The RichText library will create gui text nodes representing the markup in the text passed to the library. It will search for tags and split the entire text into words, where each word contains additional meta-data that is used to create and configure text nodes. This means that the library will create as many text nodes as there are words in the text.

example/example.gui_script

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ local function create_complex_example()
3535
[hash("spineboy")] = hash("spine-spineboy"),
3636
},
3737
},
38-
width = 410,
38+
width = 460,
3939
position = vmath.vector3(0, 0, 0),
4040
parent = gui.get_node("bg"),
4141
color = vmath.vector4(0.95, 0.95, 1.0, 1.0),
@@ -74,7 +74,7 @@ local function create_truncate_example()
7474
gui.set_pivot(cursor, gui.PIVOT_NW)
7575

7676
local settings = { position = vmath.vector3(0, 240, 0) }
77-
local words, metrics = richtext.create("This text should be shown one <img=smileys:cyclops/> at a time...", "Roboto-Regular", settings)
77+
local words, metrics = richtext.create("This text should be shown one <img=smileys:cyclops/> at a time...&zwsp;&zwsp;&zwsp;&zwsp;", "Roboto-Regular", settings)
7878
local length = 0
7979
local max_length = #words
8080
local options = { words = true }

richtext/parse.lua

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local utf8 = require("richtext.utf8")
44
local M = {}
55

66
local function parse_tag(tag, params)
7-
local settings = { tags = { [tag] = params } }
7+
local settings = { tags = { [tag] = params }, tag = tag }
88
if tag == "color" then
99
settings.color = color.parse(params)
1010
elseif tag == "shadow" then
@@ -38,7 +38,6 @@ local function parse_tag(tag, params)
3838
elseif tag == "nobr" then
3939
settings.nobr = true
4040
end
41-
settings.tag = tag
4241

4342
return settings
4443
end
@@ -47,10 +46,7 @@ end
4746
-- add a single word to the list of words
4847
local function add_word(text, settings, words)
4948
-- handle HTML entities
50-
text = text:gsub("&lt;", "<"):gsub("&gt;", ">")
51-
if text:find("&nbsp;") then
52-
text = text:gsub("&nbsp;", " ")
53-
end
49+
text = text:gsub("&lt;", "<"):gsub("&gt;", ">"):gsub("&nbsp;", " "):gsub("&zwsp;", "\226\128\139")
5450

5551
local data = { text = text }
5652
for k,v in pairs(settings) do

richtext/richtext.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function M.create(text, font, settings)
329329
local text_metrics = {
330330
width = 0,
331331
height = 0,
332-
char_count = parser.length(text),
332+
char_count = 0,
333333
}
334334
local line_words = {}
335335
local line_width = 0
@@ -338,6 +338,7 @@ function M.create(text, font, settings)
338338
local word_count = #words
339339
for i = 1, word_count do
340340
local word = words[i]
341+
text_metrics.char_count = text_metrics.char_count + parser.length(word.text)
341342
--print("word: [" .. word.text .. "]")
342343

343344
-- get font to use based on word tags

0 commit comments

Comments
 (0)