From 4fa7e718a2cdd3e1d1028f01666136ae73ec22f7 Mon Sep 17 00:00:00 2001 From: poire-z Date: Thu, 11 May 2023 20:23:39 +0200 Subject: [PATCH] TextBoxWidget:getFontSizeToFitHeight(): allow for more accuracy By providing the font that will be used. As it's more expensive, use only when really needed. --- frontend/ui/widget/textboxwidget.lua | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/frontend/ui/widget/textboxwidget.lua b/frontend/ui/widget/textboxwidget.lua index 355b7004b..a3a13ce96 100644 --- a/frontend/ui/widget/textboxwidget.lua +++ b/frontend/ui/widget/textboxwidget.lua @@ -1084,7 +1084,7 @@ function TextBoxWidget:getVisibleHeightRatios() end -- Helper function to be used before intanstiating a TextBoxWidget instance -function TextBoxWidget:getFontSizeToFitHeight(height_px, nb_lines, line_height_em) +function TextBoxWidget:getFontSizeToFitHeight(height_px, nb_lines, line_height_em, font_face, font_bold) -- Get a font size that would fit nb_lines in height_px. -- A font with the returned size should then be provided -- to TextBoxWidget:new() (as well as the line_height_em given @@ -1102,6 +1102,32 @@ function TextBoxWidget:getFontSizeToFitHeight(height_px, nb_lines, line_height_e if Screen:scaleBySize(font_size) > face_size then -- be really sure we won't get it larger font_size = font_size - 1 end + -- Because of self.line_glyph_extra_height added to the final bb, the font_size we got + -- up to here can still generate a TextBoxWidget taller than the provided height_px, + -- which might be good enough for some usages. + -- If better accuracy is needed, provide (..., font_face, font_bold) so we can return + -- a better font_size for the font that will be used. + if font_face then + while true do + -- As done in TextBoxWidget:init(): + local line_height_px = Math.round( (1 + line_height_em) * Screen:scaleBySize(font_size) ) + local face = Font:getFace(font_face, font_size) + face = Font:getAdjustedFace(face, font_bold) + local face_height = face.ftface:getHeightAndAscender() + local line_heights_diff = math.floor(line_height_px - face_height) + if line_heights_diff >= 0 then + break + end + local line_glyph_extra_height = -line_heights_diff + if line_height_px * nb_lines + line_glyph_extra_height <= height_px then + break + end + if font_size <= 1 then + break + end + font_size = font_size - 1 + end + end return font_size end