[refactoring] TextWidget.getTextWidth() (#16387)

This commit is contained in:
hius07 authored and GitHub committed 2026-11-13 18:34:08 +02:00
1 parent 7c38cf104c
commit 5ce66bad64
12 files changed
+66 -144

No files matched your search

@@ -2221,14 +2221,7 @@ function ReaderFooter:insertDynamicFillers(info, filler1_idx, filler2_idx)
end
function ReaderFooter:getTextWidth(text)
local tmp = TextWidget:new{
text = text,
face = self.footer_text_face,
bold = self.settings.text_font_bold,
}
local text_width = tmp:getSize().w
tmp:free()
return text_width
return TextWidget.getTextWidth(text, self.footer_text_face, self.settings.text_font_bold)
end
function ReaderFooter:setTocMarkers(reset)
@@ -140,14 +140,8 @@ function ReaderPageMap:resetLayout()
-- Get some metric for label min width
self.label_face = Font:getFace(self.label_font_face, self.label_font_size)
local textw = TextWidget:new{
text = " ",
face = self.label_face,
}
self.space_width = textw:getWidth()
textw:setText("8")
self.number_width = textw:getWidth()
textw:free()
self.space_width = TextWidget.getTextWidth(" ", self.label_face)
self.number_width = TextWidget.getTextWidth("8", self.label_face)
self.min_label_width = self.space_width * 2 + self.number_width
end
+1 -6
View File
@@ -826,12 +826,7 @@ function ReaderToc:onShowToc()
end
end
-- Have the width of 4 spaces be the unit of indentation
local tmp = TextWidget:new{
text = " ",
face = Font:getFace("smallinfofont", items_font_size),
}
self.toc_indent = tmp:getSize().w
tmp:free()
self.toc_indent = TextWidget.getTextWidth(" ", Font:getFace("smallinfofont", items_font_size))
self:updateChaptersDisplayedTitlePageNumber()
end
+8 -19
View File
@@ -1,7 +1,6 @@
local CheckButton = require("ui/widget/checkbutton")
local CheckMark = require("ui/widget/checkmark")
local Device = require("device")
local FFIUtil = require("ffi/util")
local Font = require("ui/font")
local InfoMessage = require("ui/widget/infomessage")
local Language = require("ui/language")
@@ -9,9 +8,10 @@ local Size = require("ui/size")
local TextWidget = require("ui/widget/textwidget")
local UIManager = require("ui/uimanager")
local VirtualKeyboard = require("ui/widget/virtualkeyboard")
local Screen = Device.screen
local T = FFIUtil.template
local ffiUtil = require("ffi/util")
local util = require("util")
local Screen = Device.screen
local T = ffiUtil.template
local _ = require("gettext")
local input_dialog, check_button_bold, check_button_border, check_button_compact
@@ -47,14 +47,11 @@ end
local function genKeyboardLayoutsSubmenu()
local item_table = {}
for lang, keyboard_layout in FFIUtil.orderedPairs(VirtualKeyboard.lang_to_keyboard_layout) do
for lang, keyboard_layout in ffiUtil.orderedPairs(VirtualKeyboard.lang_to_keyboard_layout) do
table.insert(item_table, {
text_func = function()
local text = T("%1 (%2)", Language:getLanguageName(lang), lang:sub(1, 2))
if G_reader_settings:readSetting("keyboard_layout_default") == lang then
text = text .. " ★"
end
return text
text = T("%1 (%2)", Language:getLanguageName(lang), lang:sub(1, 2)),
text_star_func = function()
return G_reader_settings:readSetting("keyboard_layout_default") == lang
end,
checked_func = function()
local keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts", {})
@@ -126,21 +123,13 @@ local sub_item_table = {
text_func = function()
local activated_keyboards, nb_keyboards = getActivatedKeyboardsStringCount()
local item_text = T(_("Keyboard layouts: %1"), activated_keyboards)
-- get width of text
local tmp = TextWidget:new{
text = item_text,
face = Font:getFace("cfont"),
}
local item_text_w = tmp:getSize().w
tmp:free()
local item_text_w = TextWidget.getTextWidth(item_text, Font:getFace("cfont"))
local checked_widget = CheckMark:new{ -- for layout, to :getSize()
checked = true,
}
if item_text_w >= Screen:getWidth() - 2*Size.padding.default - checked_widget:getSize().w then
item_text = T(_("Keyboard layouts (%1)"), nb_keyboards)
end
return item_text
end,
sub_item_table_func = genKeyboardLayoutsSubmenu,
+1 -6
View File
@@ -795,12 +795,7 @@ function BookMapWidget:init()
self.toc_span_font_name = "infofont"
self.toc_span_font_size = 14
self.toc_span_face = Font:getFace(self.toc_span_font_name, self.toc_span_font_size)
local test_w = TextWidget:new{
text = "z",
face = self.toc_span_face,
}
self.span_height = test_w:getSize().h + BookMapRow.toc_span_border
test_w:free()
self.span_height = TextWidget.getTextHeight("z", self.toc_span_face) + BookMapRow.toc_span_border
-- Reference font size for flat TOC items, as set (or default) in ReaderToc
self.reader_toc_font_size = G_reader_settings:readSetting("toc_items_font_size")
+1 -6
View File
@@ -136,12 +136,7 @@ function Button:init()
if not self.label_widget:isTruncated() then
local checkmark_width = 0
if self.checked_func and not self.checked_func() then
local tmp = TextWidget:new{
text = self.checkmark,
face = face,
}
checkmark_width = tmp:getSize().w
tmp:free()
checkmark_width = TextWidget.getTextWidth(self.checkmark, face, self.text_font_bold)
end
self._min_needed_width = self.label_widget:getSize().w + checkmark_width + outer_pad_width
end
+4 -17
View File
@@ -471,12 +471,7 @@ function MenuItem:getDotsText(face)
if not _dots_cached_info or _dots_cached_info.screen_width ~= screen_w
or _dots_cached_info.face ~= face then
local unit = "."
local tmp = TextWidget:new{
text = unit,
face = face,
}
local unit_w = tmp:getSize().w
tmp:free()
local unit_w = TextWidget.getTextWidth(unit, face)
-- (We assume/expect no kerning will happen between consecutive units)
local nb_units = math.ceil(screen_w / unit_w)
local min_width = unit_w * 3 -- have it not shown if smaller than this
@@ -1270,11 +1265,7 @@ function Menu:setupItemHeights()
}:getSize().h
local infont_size = self.items_mandatory_font_size or (self.font_size - 4)
local infont_face = Font:getFace("infont", infont_size)
local infont_char_width = TextWidget:new{
text = "0",
face = infont_face,
bold = true,
}:getSize().w
local infont_char_width = TextWidget.getTextWidth("0", infont_face, true) -- bold
local available_width = self.inner_dimen.w
if self.is_enable_shortcut then
available_width = available_width - line_height - Size.span.horizontal_default
@@ -1288,13 +1279,9 @@ function Menu:setupItemHeights()
-- exact item height can be calculated by building the TextBoxWidget for item text,
-- but it is slow, so estimate the number of lines by building the TextWidget
-- empirical 8% is added to consider unjustified unhyphenated multilines text layout
local item_text_width = TextWidget:new{
text = item.text,
face = face,
bold = item.bold,
}:getSize().w * 1.08
local item_text_w = TextWidget.getTextWidth(item.text, face, item.bold) * 1.08
local item_available_width = available_width - infont_char_width * (item.mandatory and #tostring(item.mandatory) or 0)
local lines_nb = math.min(math.ceil(item_text_width / item_available_width), self.items_max_lines)
local lines_nb = math.min(math.ceil(item_text_w / item_available_width), self.items_max_lines)
item.height = lines_nb * line_height + 2 * Size.span.vertical_default + self.linesize
item.shortcut_icon_width = line_height -- letter shortcuts of fixed size (1 line)
+2 -12
View File
@@ -141,12 +141,7 @@ function PageBrowserWidget:init()
self.toc_span_font_name = "infofont"
self.toc_span_font_size = 14
self.toc_span_face = Font:getFace(self.toc_span_font_name, self.toc_span_font_size)
local test_w = TextWidget:new{
text = "z",
face = self.toc_span_face,
}
self.span_height = test_w:getSize().h + BookMapRow.toc_span_border
test_w:free()
self.span_height = TextWidget.getTextHeight("z", self.toc_span_face) + BookMapRow.toc_span_border
-- For page numbers alongside thumbnails, use the same font size
-- we use for them in the ribbon
@@ -155,12 +150,7 @@ function PageBrowserWidget:init()
-- We'll be displaying the number vertically, so get the width we'd need
-- to display some wide single char (this will influence side and inter
-- thumbnails margins).
test_w = TextWidget:new{
text = "W",
face = self.page_num_font_face,
}
self.page_num_width = test_w:getWidth()
test_w:free()
self.page_num_width = TextWidget.getTextWidth("W", self.page_num_font_face)
end
self.min_nb_rows = 1
+26
View File
@@ -314,6 +314,32 @@ function TextWidget:getBaseline()
return self._baseline_h
end
function TextWidget.getTextWidth(text, face, bold, padding)
local tmp = TextWidget:new{
text = text,
face = face,
bold = bold,
padding = padding,
}
tmp:updateSize()
local w = tmp._length
tmp:free()
return w
end
function TextWidget.getTextHeight(text, face, bold, padding)
local tmp = TextWidget:new{
text = text,
face = face,
bold = bold,
padding = padding,
}
tmp:updateSize()
local h = tmp._height
tmp:free()
return h
end
function TextWidget:setText(text)
if text == self.text then
return
+8 -24
View File
@@ -6,13 +6,12 @@ Plugin for setting screen warmth based on the sun position and/or a time schedul
local CheckButton = require("ui/widget/checkbutton")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local DateTimeWidget = require("ui/widget/datetimewidget")
local DoubleSpinWidget = require("ui/widget/doublespinwidget")
local Device = require("device")
local DeviceListener = require("device/devicelistener")
local Dispatcher = require("dispatcher")
local DoubleSpinWidget = require("ui/widget/doublespinwidget")
local Event = require("ui/event")
local FFIUtil = require("ffi/util")
local Font = require("ui/font")
local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog")
@@ -23,13 +22,13 @@ local SunTime = require("suntime")
local TextWidget = require("ui/widget/textwidget")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local datetime = require("datetime")
local logger = require("logger")
local Powerd = Device.powerd
local Screen = Device.screen
local _ = require("gettext")
local C_ = _.pgettext
local Powerd = Device.powerd
local T = FFIUtil.template
local Screen = require("device").screen
local datetime = require("datetime")
local T = require("ffi/util").template
local activate_sun = 1
local activate_schedule = 2
@@ -1232,23 +1231,8 @@ function AutoWarmth:showTimesInfo(title, location, activator, request_easy)
-- t .. times
-- num .. index in times
local function info_line(indent, text, time, num, face, easy)
-- get width of space
local unit = " "
local tmp = TextWidget:new{
text = unit,
face = face,
}
local space_w = tmp:getSize().w
-- get width of text
unit = text
tmp = TextWidget:new{
text = unit,
face = face,
}
local text_w = tmp:getSize().w
tmp:free()
local space_w = TextWidget.getTextWidth(" ", face)
local text_w = TextWidget.getTextWidth(text, face)
-- width of text in spaces
local str_len = math.floor(text_w / space_w + 0.5)
+3 -19
View File
@@ -666,13 +666,7 @@ function CalendarDayView:init()
self.footer_height = self.page_info:getSize().h
self.items_per_page = 5
local temp_text = TextWidget:new{
text = " ",
face = BookDailyItem.face
}
self.book_item_height = temp_text:getSize().h + 2 * Size.padding.small
temp_text:free()
self.book_item_height = TextWidget.getTextHeight(" ", BookDailyItem.face) + 2 * Size.padding.small
self.book_items = VerticalGroup:new{}
self.timeline = OverlapGroup:new{
@@ -750,22 +744,12 @@ function CalendarDayView:setupView()
local font_size = TextWidget:getFontSizeToFitHeight("cfont", self.time_text_width, Size.padding.small)
self.time_text_face = Font:getFace("cfont", font_size)
local temp_text = TextWidget:new{
text = "00:00",
face = self.time_text_face
}
local time_text_width = temp_text:getSize().w
local time_text_width = TextWidget.getTextWidth("00:00", self.time_text_face)
while time_text_width > self.time_text_width * 0.8 do
font_size = font_size * 0.8
self.time_text_face = Font:getFace("cfont", font_size)
temp_text:free()
temp_text = TextWidget:new{
text = "00:00",
face = self.time_text_face
}
time_text_width = temp_text:getWidth()
time_text_width = TextWidget.getTextWidth("00:00", self.time_text_face)
end
temp_text:free()
self.pages = #self.kv_pairs <= self.items_per_page+1 and 1 or math.ceil(#self.kv_pairs / self.items_per_page)
self.footer_container[1] = self.pages > 1 and self.page_info or VerticalSpan:new{ w = 0 }
+9 -19
View File
@@ -104,17 +104,13 @@ end
function MenuDialog:setupPluginMenu()
local size = Screen:getSize()
local width = math.floor(size.w * 0.9)
local face = Font:getFace("xx_smallinfofont")
-- Switch text translations could be long
local temp_text_widget = TextWidget:new{
text = _("Auto add new words"),
face = Font:getFace("xx_smallinfofont")
}
local switch_guide_width = temp_text_widget:getSize().w
temp_text_widget:setText(_("Save context"))
switch_guide_width = math.max(switch_guide_width, temp_text_widget:getSize().w)
local w1 = TextWidget.getTextWidth(_("Auto add new words"), face)
local w2 = TextWidget.getTextWidth(_("Save context"), face)
local switch_guide_width = math.max(w1, w2)
switch_guide_width = math.min(math.max(switch_guide_width, math.ceil(width*0.39)), math.ceil(width*0.61))
temp_text_widget:free()
local switch_width = width - switch_guide_width - Size.padding.fullscreen - Size.padding.default
@@ -302,7 +298,7 @@ function MenuDialog:setupPluginMenu()
dimen = Geom:new{w = switch_guide_width, h = switch:getSize().h },
TextWidget:new{
text = _("Auto add new words"),
face = Font:getFace("xx_smallinfofont"),
face = face,
max_width = switch_guide_width
}
},
@@ -315,7 +311,7 @@ function MenuDialog:setupPluginMenu()
dimen = Geom:new{w = switch_guide_width, h = switch:getSize().h},
TextWidget:new{
text = _("Save context"),
face = Font:getFace("xx_smallinfofont"),
face = face,
max_width = switch_guide_width
}
},
@@ -523,13 +519,7 @@ function WordInfoDialog:init()
end
if not word_info_dialog_width then
local temp_text = TextWidget:new{
text = self.dates,
padding = Size.padding.fullscreen,
face = Font:getFace("cfont", 14)
}
local dates_width = temp_text:getSize().w
temp_text:free()
local dates_width = TextWidget.getTextWidth(self.dates, Font:getFace("cfont", 14), nil, Size.padding.fullscreen)
local screen_width = math.min(Screen:getWidth(), Screen:getHeight())
word_info_dialog_width = math.floor(math.max(screen_width * 0.6, math.min(screen_width * 0.8, dates_width)))
end
@@ -777,8 +767,8 @@ local VocabItemWidget = InputContainer:extend{
local point_widget_height = point_widget:getSize().h
local point_widget_width = point_widget:getSize().w
local word_height = TextWidget:new{text = " ", face = word_face}:getSize().h
local subtitle_height = TextWidget:new{text = " ", face = subtitle_face}:getSize().h
local word_height = TextWidget.getTextHeight(" ", word_face)
local subtitle_height = TextWidget.getTextHeight(" ", subtitle_face)
function VocabItemWidget:init()