Skip to content

Commit

Permalink
Solve #217 Fix error font argument, remove deprecated gui.get_text_me…
Browse files Browse the repository at this point in the history
…trics
  • Loading branch information
Insality committed Dec 1, 2022
1 parent 5d3625e commit 7f5be3f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 4 additions & 3 deletions druid/base/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

local Event = require("druid.event")
local const = require("druid.const")
local helper = require("druid.helper")
local utf8 = require("druid.system.utf8")
local component = require("druid.component")

Expand Down Expand Up @@ -77,7 +78,7 @@ local function update_text_area_size(self)
local max_width = self.text_area.x
local max_height = self.text_area.y

local metrics = gui.get_text_metrics_from_node(self.node)
local metrics = helper.get_text_metrics_from_node(self.node)

local scale_modifier = max_width / metrics.width
scale_modifier = math.min(scale_modifier, self.start_scale.x)
Expand Down Expand Up @@ -133,8 +134,8 @@ 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
local no_space = resource.get_text_metrics(font, "1").width
local with_space = resource.get_text_metrics(font, " 1").width
self._space_width[font] = with_space - no_space
end

Expand Down
19 changes: 18 additions & 1 deletion druid/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local M = {}
--- Text node or icon node can be nil
local function get_text_width(text_node)
if text_node then
local text_metrics = gui.get_text_metrics_from_node(text_node)
local text_metrics = M.get_text_metrics_from_node(text_node)
local text_scale = gui.get_scale(text_node).x
return text_metrics.width * text_scale
end
Expand Down Expand Up @@ -280,6 +280,23 @@ function M.get_border(node, offset)
end


function M.get_text_metrics_from_node(node)
local font_resource = gui.get_font_resource(gui.get_font(node))
local options = {
tracking = gui.get_tracking(node),
line_break = gui.get_line_break(node),
}

-- Gather other options only if it used in node
if options.line_break then
options.width = gui.get_size(node).x
options.leading = gui.get_leading(node)
end

return resource.get_text_metrics(font_resource, gui.get_text(node), options)
end


--- Show deprecated message. Once time per message
-- @function helper.deprecated
-- @tparam string message The deprecated message
Expand Down

0 comments on commit 7f5be3f

Please sign in to comment.