mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
Add text:get_text_width function
This commit is contained in:
parent
e06337fdee
commit
1e213a5f30
@ -64,6 +64,18 @@ local function update_text_area_size(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- calculate space width with font
|
||||||
|
local function get_space_width(self, font)
|
||||||
|
if not self._space_width[font] then
|
||||||
|
local no_space = gui.get_text_metrics(font, "1", 0, false, 0, 0).width
|
||||||
|
local with_space = gui.get_text_metrics(font, " 1", 0, false, 0, 0).width
|
||||||
|
self._space_width[font] = with_space - no_space
|
||||||
|
end
|
||||||
|
|
||||||
|
return self._space_width[font]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Component init function
|
--- Component init function
|
||||||
-- @function text:init
|
-- @function text:init
|
||||||
-- @tparam node node Gui text node
|
-- @tparam node node Gui text node
|
||||||
@ -88,11 +100,34 @@ function M.init(self, node, value, no_adjust)
|
|||||||
self.on_set_pivot = Event()
|
self.on_set_pivot = Event()
|
||||||
self.on_update_text_scale = Event()
|
self.on_update_text_scale = Event()
|
||||||
|
|
||||||
|
self._space_width = {}
|
||||||
|
|
||||||
self:set_to(value or gui.get_text(self.node))
|
self:set_to(value or gui.get_text(self.node))
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Calculate text width with font with respect to trailing space
|
||||||
|
-- @function text:get_text_width
|
||||||
|
-- @tparam[opt] string text
|
||||||
|
function M.get_text_width(self, text)
|
||||||
|
text = text or self.last_value
|
||||||
|
local font = gui.get_font(self.node)
|
||||||
|
local scale = gui.get_scale(self.node)
|
||||||
|
local result = gui.get_text_metrics(font, text, 0, false, 0, 0).width
|
||||||
|
for i = #text, 1, -1 do
|
||||||
|
local c = string.sub(text, i, i)
|
||||||
|
if c ~= ' ' then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
result = result + get_space_width(self, font)
|
||||||
|
end
|
||||||
|
|
||||||
|
return result * scale.x
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Set text to text field
|
--- Set text to text field
|
||||||
-- @function text:set_to
|
-- @function text:set_to
|
||||||
-- @tparam string set_to Text for node
|
-- @tparam string set_to Text for node
|
||||||
|
1045
druid/system/utf8.lua
Normal file
1045
druid/system/utf8.lua
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user