Skip to content

Commit

Permalink
Made UTF Text work without...
Browse files Browse the repository at this point in the history
Made UTF Text work without user manually wrapping text in [|characters]

Fixed some cases where "|" was used in code, insead of special_character[5]
  • Loading branch information
sysl-dev committed Feb 8, 2021
1 parent ba717ed commit 398cc2d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
35 changes: 27 additions & 8 deletions example/library/slog-text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ]] ----------------------------------------------------
Expand All @@ -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,


Expand Down
21 changes: 4 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions slog-text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ]] ----------------------------------------------------
Expand All @@ -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,


Expand Down

0 comments on commit 398cc2d

Please sign in to comment.