From 7f5be3ffb0ed1157c5cfe5937c17a232c1c698f9 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 1 Dec 2022 21:02:31 +0200 Subject: [PATCH] Solve #217 Fix error font argument, remove deprecated gui.get_text_metrics --- druid/base/text.lua | 7 ++++--- druid/helper.lua | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/druid/base/text.lua b/druid/base/text.lua index 8981ccc2..098ae938 100755 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -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") @@ -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) @@ -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 diff --git a/druid/helper.lua b/druid/helper.lua index c7849d11..c9594262 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -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 @@ -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