diff --git a/README.md b/README.md index 2725170..fc78465 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,16 @@ Removes all gui text nodes created by `richtext.create()`. * `words` (table) - Table of words, as received from a call to `richtext.create()`. +### richtext.plaintext(words) +Returns the words created by `richtext.create()` as a plain text string without any formatting or tags. Linebreaks are included in the returned string. + +**PARAMETERS** +* `words` (table) - Table of words, as received from a call to `richtext.create()`. + +**RETURNS** +* `plaintext` (string) - Plain text version of the words. + + ### richtext.ALIGN_LEFT Left-align text. The words of a line starts at the specified position (see `richtext.create` settings above). diff --git a/example/example.gui_script b/example/example.gui_script index 499e284..d01d674 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -69,6 +69,7 @@ local function create_complex_example() local words, metrics = richtext.create(text, "Roboto", settings) print("The text consists of " .. tostring(metrics.char_count) .. " characters") + print("The plain-text is " .. richtext.plaintext(words)) -- adjust background to cover text gui.set_size(settings.parent, vmath.vector3(metrics.width, metrics.height, 0)) diff --git a/richtext/richtext.lua b/richtext/richtext.lua index f9106eb..032d290 100755 --- a/richtext/richtext.lua +++ b/richtext/richtext.lua @@ -721,5 +721,19 @@ function M.remove(words) end end +function M.plaintext(words) + local s = "" + for i=1,#words do + local word = words[i] + if word.text then + s = s .. word.text + if word.linebreak then + s = s .. "\n" + end + end + end + return s +end + return M