From 398cc2db7235469c30c24cd1cccf3f33ef51a539 Mon Sep 17 00:00:00 2001 From: SysL Date: Mon, 8 Feb 2021 16:07:32 -0700 Subject: [PATCH] Made UTF Text work without... Made UTF Text work without user manually wrapping text in [|characters] Fixed some cases where "|" was used in code, insead of special_character[5] --- example/library/slog-text.lua | 35 +++++++++++++++++++++++++++-------- readme.md | 21 ++++----------------- slog-text.lua | 35 +++++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/example/library/slog-text.lua b/example/library/slog-text.lua index 71892ca..02d1cf5 100644 --- a/example/library/slog-text.lua +++ b/example/library/slog-text.lua @@ -36,6 +36,7 @@ local m = { ----------------------------------------------------------------------------------------------------]]-- local unpack = unpack local love = love +local utf8 = require("utf8") --[[---------------------------------------------------------------------------------------------------- Debug Print - Confirms Loaded when m.debug is true. @@ -115,7 +116,7 @@ local function convert_special_character(char) end --[[ Get character Width ]]----------------------------------------------------------------------------- --- Get the width of the current character width the current font. -- L2R? +-- Get the width of the current character width the current font. local function get_character_width(character) return love.graphics.getFont():getWidth(character) end @@ -254,6 +255,18 @@ end SEND - Sends a string to be drawn by the current textbox. ----------------------------------------------------------------------------------------------------]]-- function M:send(text, wrap_num, show_all) + + -- This whole things just fixes UTF8 characters. + local words = {} + for _, c in utf8.codes(text) do + if #utf8.char(c) > 1 then + table.insert(words, special_character[1] .. special_character[5] .. utf8.char(c) .. special_character[2]) + else + table.insert(words, utf8.char(c)) + end + end + text = table.concat(words) + self.current_character = #self.table_string self.current_print_speed = self.default_print_speed self.waitforinput = false @@ -312,14 +325,14 @@ function M:send(text, wrap_num, show_all) local space_count = 0 local fulljustspace = {} for i=1, #self.table_string do - if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. "|" then + if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then -- Keep track of the last space. if self.table_string[i] == " " then last_space = i pixel_count_last_space = 0 end -- Count the pixels for the characters - if self.table_string[i]:sub(1,2) == special_character[1] .. "|" then -- Have to count wrapped strings + if self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then -- Have to count wrapped strings pixel_count = pixel_count + get_character_width(self.table_string[i]:sub(3, #self.table_string[i]-1)) --print(get_character_width(self.table_string[i]:sub(3, #self.table_string[i]-1))) else @@ -354,7 +367,9 @@ function M:send(text, wrap_num, show_all) if true and font_table ~= nil and self.table_string[i] ~= special_character[1] .. "/font" .. special_character[2] then --print(self.table_string[i]:sub(7, #self.table_string[i]-1)) local font_table = string_to_table(font_table) - love.graphics.setFont(font_table[self.table_string[i]:sub(7, #self.table_string[i]-1)]) + if font_table[self.table_string[i]:sub(7, #self.table_string[i]-1)] then + love.graphics.setFont(font_table[self.table_string[i]:sub(7, #self.table_string[i]-1)]) + end else love.graphics.setFont(self.default_font) end @@ -364,8 +379,8 @@ function M:send(text, wrap_num, show_all) if self.rendering ~= "left" then pixel_count = 0 for i=1, #self.table_string do - if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. "|" then -- Have to count wrapped strings - if self.table_string[i]:sub(1,2) == special_character[1] .. "|" then + if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then -- Have to count wrapped strings + if self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then pixel_count = pixel_count + get_character_width(self.table_string[i]:sub(3, #self.table_string[i]-1)) else pixel_count = pixel_count + get_character_width(self.table_string[i]) @@ -993,7 +1008,7 @@ M.command_table = { if image_table[_mod1] and image_table[_mod1][_mod2] then love.graphics.draw(image_table[_mod1][_mod2], self.tx + self.cursor.x, self.ty + self.cursor.y) self.cursor.x = self.cursor.x + image_table[_mod1][_mod2]:getWidth() - elseif image_table[_mod1] then + elseif image_table[_mod1] and type(image_table[_mod1]) ~= "table" then love.graphics.draw(image_table[_mod1], self.tx + self.cursor.x, self.ty + self.cursor.y) self.cursor.x = self.cursor.x + image_table[_mod1]:getWidth() else @@ -1059,6 +1074,8 @@ M.command_table = { local _mod2 = self.command_modifer[3] if audio_table[_mod1] and audio_table[_mod1][_mod2] then audio_table[_mod1][_mod2]:play() + elseif type(audio_table[_mod1]) ~= "table" then + audio_table[_mod1]:play() end end, --[[ Stop a sound ]] ---------------------------------------------------- @@ -1071,7 +1088,9 @@ M.command_table = { local _mod2 = self.command_modifer[3] if audio_table[_mod1] and audio_table[_mod1][_mod2] then audio_table[_mod1][_mod2]:stop() - end + elseif type(audio_table[_mod1]) ~= "table" then + audio_table[_mod1]:stop() + end end, diff --git a/readme.md b/readme.md index b8877cc..4dc2810 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,8 @@ ![Quick Demo of Examples](/screenshots/ex.gif?raw=true "Quick Demo of Examples") +# Update Notes +* It is no longer required to wrap UTF8 characters. You can send them without the wrapping and the library will scan and wrap extended characters without any manual input. # Setup ## Adding the library to your project @@ -32,29 +34,14 @@ my_cool_textbox:draw(0,0) my_cool_textbox:update(dt) ``` -# UTF8 Text Note -UTF8 characters need to be wrapped in ```[|t][|a][|g][|s]``` to work. -You could also write something to preprocess your text before sending it to SYSL-Text. - -## Quick text wrapper in Notepad++ replace command -``` -//Find: -(.) - -//Replace -[|$1] -``` - - - # Tags -SYSL-Text supports tags in ```[these brackets]``` and will use them to apply effects to your rendered text. SYSL-Text also supports defining default styles per textbox. Please take a look at the examples below. +SYSL-Text supports tags in ```[these brackets]``` and will use them to apply effects to your rendered text. SYSL-Text also supports defining default styles per textbox. Please take a look at the examples below. **Please note that effects and style are only examples, the library does not provide backgrounds or textboxes.** ## Tags with Screenshot Examples ![Example 1](/screenshots/1.gif?raw=true "Example of Library") ```lua example2box = Text.new("left", { color = {1,1,1,1}, shadow_color = {0.5,0.5,1,0.4}, font = Fonts.golden_apple, character_sound = true}) -example2box:send("[|•] Do you like eggs?[newline][|•] I think they are [pad=6]eggzelent![audio=sfx=laugh]", 100, false) +example2box:send("• Do you like eggs?[newline]• I think they are [pad=6]eggzelent![audio=sfx=laugh]", 100, false) ``` ![Example 2](/screenshots/2.gif?raw=true "Example of Library") ```lua diff --git a/slog-text.lua b/slog-text.lua index 71892ca..02d1cf5 100644 --- a/slog-text.lua +++ b/slog-text.lua @@ -36,6 +36,7 @@ local m = { ----------------------------------------------------------------------------------------------------]]-- local unpack = unpack local love = love +local utf8 = require("utf8") --[[---------------------------------------------------------------------------------------------------- Debug Print - Confirms Loaded when m.debug is true. @@ -115,7 +116,7 @@ local function convert_special_character(char) end --[[ Get character Width ]]----------------------------------------------------------------------------- --- Get the width of the current character width the current font. -- L2R? +-- Get the width of the current character width the current font. local function get_character_width(character) return love.graphics.getFont():getWidth(character) end @@ -254,6 +255,18 @@ end SEND - Sends a string to be drawn by the current textbox. ----------------------------------------------------------------------------------------------------]]-- function M:send(text, wrap_num, show_all) + + -- This whole things just fixes UTF8 characters. + local words = {} + for _, c in utf8.codes(text) do + if #utf8.char(c) > 1 then + table.insert(words, special_character[1] .. special_character[5] .. utf8.char(c) .. special_character[2]) + else + table.insert(words, utf8.char(c)) + end + end + text = table.concat(words) + self.current_character = #self.table_string self.current_print_speed = self.default_print_speed self.waitforinput = false @@ -312,14 +325,14 @@ function M:send(text, wrap_num, show_all) local space_count = 0 local fulljustspace = {} for i=1, #self.table_string do - if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. "|" then + if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then -- Keep track of the last space. if self.table_string[i] == " " then last_space = i pixel_count_last_space = 0 end -- Count the pixels for the characters - if self.table_string[i]:sub(1,2) == special_character[1] .. "|" then -- Have to count wrapped strings + if self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then -- Have to count wrapped strings pixel_count = pixel_count + get_character_width(self.table_string[i]:sub(3, #self.table_string[i]-1)) --print(get_character_width(self.table_string[i]:sub(3, #self.table_string[i]-1))) else @@ -354,7 +367,9 @@ function M:send(text, wrap_num, show_all) if true and font_table ~= nil and self.table_string[i] ~= special_character[1] .. "/font" .. special_character[2] then --print(self.table_string[i]:sub(7, #self.table_string[i]-1)) local font_table = string_to_table(font_table) - love.graphics.setFont(font_table[self.table_string[i]:sub(7, #self.table_string[i]-1)]) + if font_table[self.table_string[i]:sub(7, #self.table_string[i]-1)] then + love.graphics.setFont(font_table[self.table_string[i]:sub(7, #self.table_string[i]-1)]) + end else love.graphics.setFont(self.default_font) end @@ -364,8 +379,8 @@ function M:send(text, wrap_num, show_all) if self.rendering ~= "left" then pixel_count = 0 for i=1, #self.table_string do - if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. "|" then -- Have to count wrapped strings - if self.table_string[i]:sub(1,2) == special_character[1] .. "|" then + if #self.table_string[i] == 1 or self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then -- Have to count wrapped strings + if self.table_string[i]:sub(1,2) == special_character[1] .. special_character[5] then pixel_count = pixel_count + get_character_width(self.table_string[i]:sub(3, #self.table_string[i]-1)) else pixel_count = pixel_count + get_character_width(self.table_string[i]) @@ -993,7 +1008,7 @@ M.command_table = { if image_table[_mod1] and image_table[_mod1][_mod2] then love.graphics.draw(image_table[_mod1][_mod2], self.tx + self.cursor.x, self.ty + self.cursor.y) self.cursor.x = self.cursor.x + image_table[_mod1][_mod2]:getWidth() - elseif image_table[_mod1] then + elseif image_table[_mod1] and type(image_table[_mod1]) ~= "table" then love.graphics.draw(image_table[_mod1], self.tx + self.cursor.x, self.ty + self.cursor.y) self.cursor.x = self.cursor.x + image_table[_mod1]:getWidth() else @@ -1059,6 +1074,8 @@ M.command_table = { local _mod2 = self.command_modifer[3] if audio_table[_mod1] and audio_table[_mod1][_mod2] then audio_table[_mod1][_mod2]:play() + elseif type(audio_table[_mod1]) ~= "table" then + audio_table[_mod1]:play() end end, --[[ Stop a sound ]] ---------------------------------------------------- @@ -1071,7 +1088,9 @@ M.command_table = { local _mod2 = self.command_modifer[3] if audio_table[_mod1] and audio_table[_mod1][_mod2] then audio_table[_mod1][_mod2]:stop() - end + elseif type(audio_table[_mod1]) ~= "table" then + audio_table[_mod1]:stop() + end end,