From c077d8e4d02703486ac3348123d351a440b71337 Mon Sep 17 00:00:00 2001 From: "xin.zhang" Date: Wed, 19 Feb 2025 18:25:14 +0800 Subject: [PATCH] FIX: get suitable font size to draw the texts jira: [STUDIO-10067] Change-Id: I589fd6a271ae177e4630e403b64c18090aab9471 (cherry picked from commit d036c92a3793e8c0c758432b52f2af8ffeff751f) --- src/slic3r/GUI/SelectMachinePop.cpp | 1 + src/slic3r/GUI/Widgets/AMSItem.cpp | 31 +++++++++++++--------- src/slic3r/GUI/Widgets/SwitchButton.cpp | 8 +++--- src/slic3r/Utils/WxFontUtils.cpp | 34 +++++++++++++++++++++++++ src/slic3r/Utils/WxFontUtils.hpp | 2 ++ 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/slic3r/GUI/SelectMachinePop.cpp b/src/slic3r/GUI/SelectMachinePop.cpp index 0ed6d6261d..92942e4267 100644 --- a/src/slic3r/GUI/SelectMachinePop.cpp +++ b/src/slic3r/GUI/SelectMachinePop.cpp @@ -1029,6 +1029,7 @@ PinCodePanel::PinCodePanel(wxWindow* parent, int type, wxWindowID winid /*= wxID if (m_type == 0) {txt = _L("Bind with Pin Code");} else if (m_type == 1) {txt = _L("Bind with Access Code");} + WxFontUtils::get_suitable_font_size(0.5 * size.GetHeight(), dc); auto txt_size = dc.GetTextExtent(txt); dc.DrawText(txt, wxPoint(FromDIP(28), (size.y - txt_size.y) / 2)); diff --git a/src/slic3r/GUI/Widgets/AMSItem.cpp b/src/slic3r/GUI/Widgets/AMSItem.cpp index 66b23554f6..92ce182a55 100644 --- a/src/slic3r/GUI/Widgets/AMSItem.cpp +++ b/src/slic3r/GUI/Widgets/AMSItem.cpp @@ -3,6 +3,7 @@ #include "../BitmapCache.hpp" #include "../I18N.hpp" #include "../GUI_App.hpp" +#include "../Utils/WxFontUtils.hpp" #include #include @@ -229,9 +230,11 @@ void AMSExtText::doRender(wxDC& dc) auto size = GetSize(); dc.SetPen(wxPen(StateColor::darkModeColorFor(AMS_CONTROL_GRAY800), 2, wxPENSTYLE_SOLID)); - auto tsize = dc.GetMultiLineTextExtent(_L("Ext")); - dc.SetFont(Label::Body_13); dc.SetTextForeground(StateColor::darkModeColorFor(AMS_CONTROL_GRAY800)); + dc.SetFont(Label::Body_13); + WxFontUtils::get_suitable_font_size(0.7 * size.GetHeight(), dc); + + auto tsize = dc.GetMultiLineTextExtent(_L("Ext")); wxPoint pot(FromDIP((size.x - tsize.x) / 2), FromDIP((size.y - tsize.y) / 2)); dc.DrawText(_L("Ext"), pot); } @@ -2873,24 +2876,28 @@ void AMSHumidity::doRender(wxDC& dc) pot = wxPoint(FromDIP(5), ((size.y - hum_img.GetBmpSize().y) / 2)); dc.DrawBitmap(hum_img.bmp(), pot); + pot.x += hum_img.GetBmpSize().x + FromDIP(3); // percentage wxString hum_percentage(std::to_string(m_amsinfo.humidity_raw)); - auto tsize = dc.GetMultiLineTextExtent(hum_percentage); dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); dc.SetFont(Label::Body_14); dc.SetTextForeground(StateColor::darkModeColorFor(AMS_CONTROL_BLACK_COLOUR)); - // pot = wxPoint(FromDIP(size.x * 0.3), FromDIP((size.y - tsize.y) / 2)); - pot.x = pot.x + hum_img.GetBmpSize().x + FromDIP(3); - dc.DrawText(hum_percentage, pot); - pot.x += (tsize.x + FromDIP(5)); + WxFontUtils::get_suitable_font_size(0.7 * size.GetHeight(), dc); + auto tsize1 = dc.GetMultiLineTextExtent(hum_percentage); + pot.y = (size.y - tsize1.y) / 2; + dc.DrawText(hum_percentage, pot); + pot.x += (tsize1.x + FromDIP(3)); + + // percentage sign dc.SetFont(Label::Body_12); - tsize = dc.GetMultiLineTextExtent(_L("%")); - pot.y += (tsize.y / 2 - FromDIP(4)); + WxFontUtils::get_suitable_font_size(0.5 * size.GetHeight(), dc); + auto tsize2 = dc.GetMultiLineTextExtent(_L("%")); + pot.y = pot.y + ((tsize1.y - tsize2.y) / 2) + FromDIP(2); dc.DrawText(_L("%"), pot); - pot.x = pot.x + tsize.x + FromDIP(2); + pot.x += tsize2.x + FromDIP(3); } else /*image with number*/ { @@ -2916,8 +2923,8 @@ void AMSHumidity::doRender(wxDC& dc) //sun image /*pot.x = FromDIP(size.x * 0.69); pot.y = FromDIP((size.y - ams_sun_img.GetBmpHeight()) / 2);*/ - pot.x = pot.x + FromDIP(ams_sun_img.GetBmpWidth() / 2); - pot.y = FromDIP((size.y - ams_sun_img.GetBmpHeight()) / 2); + pot.x = pot.x + (ams_sun_img.GetBmpWidth() / 2); + pot.y = (size.y - ams_sun_img.GetBmpHeight()) / 2; dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); dc.DrawBitmap(ams_sun_img.bmp(), pot); diff --git a/src/slic3r/GUI/Widgets/SwitchButton.cpp b/src/slic3r/GUI/Widgets/SwitchButton.cpp index 3a2285b65e..fefbbbce49 100644 --- a/src/slic3r/GUI/Widgets/SwitchButton.cpp +++ b/src/slic3r/GUI/Widgets/SwitchButton.cpp @@ -4,6 +4,7 @@ #include "../wxExtensions.hpp" #include "../Utils/MacDarkMode.hpp" +#include "../Utils/WxFontUtils.hpp" #include #include @@ -239,14 +240,16 @@ void SwitchBoard::doRender(wxDC &dc) dc.DrawRoundedRectangle(0, 0, GetSize().x / 2, GetSize().y, 8); } - auto left_txt_size = dc.GetTextExtent(leftLabel); - dc.SetFont(::Label::Body_13); if (switch_left) { dc.SetTextForeground(*wxWHITE); } else { dc.SetTextForeground(0x333333); } + dc.SetFont(::Label::Body_13); + Slic3r::GUI::WxFontUtils::get_suitable_font_size(0.6 * GetSize().GetHeight(), dc); + + auto left_txt_size = dc.GetTextExtent(leftLabel); dc.DrawText(leftLabel, wxPoint((GetSize().x / 2 - left_txt_size.x) / 2, (GetSize().y - left_txt_size.y) / 2)); /*right*/ @@ -257,7 +260,6 @@ void SwitchBoard::doRender(wxDC &dc) } auto right_txt_size = dc.GetTextExtent(rightLabel); - dc.SetFont(::Label::Body_13); if (switch_right) { dc.SetTextForeground(*wxWHITE); } else { diff --git a/src/slic3r/Utils/WxFontUtils.cpp b/src/slic3r/Utils/WxFontUtils.cpp index deb95de942..a536e65912 100644 --- a/src/slic3r/Utils/WxFontUtils.cpp +++ b/src/slic3r/Utils/WxFontUtils.cpp @@ -297,6 +297,40 @@ bool WxFontUtils::is_bold(const wxFont &font) { return wx_weight != wxFONTWEIGHT_NORMAL; } +void Slic3r::GUI::WxFontUtils::get_suitable_font_size(int max_height, wxDC &dc) +{ + wxFont font = dc.GetFont(); + if (!font.IsOk()) return; + + int font_size = font.GetPointSize(); + int height = dc.GetFontMetrics().height; + if (height < max_height) /*go smaller*/ + { + while (height < max_height) { + font_size++; + font.SetPointSize(font_size); + dc.SetFont(font); + height = dc.GetFontMetrics().height; + } + + if (height > max_height) + { + font_size--; + font.SetPointSize(font_size); + dc.SetFont(font); + } + } + else if (height > max_height) /*go bigger*/ + { + while (height > max_height && font_size > 1) { + font_size--; + font.SetPointSize(font_size); + dc.SetFont(font); + height = dc.GetFontMetrics().height; + } + } +} + std::unique_ptr WxFontUtils::set_italic(wxFont &font, const Emboss::FontFile &font_file) { static std::vector italic_styles = { diff --git a/src/slic3r/Utils/WxFontUtils.hpp b/src/slic3r/Utils/WxFontUtils.hpp index 3845a1085c..6c9769ae71 100644 --- a/src/slic3r/Utils/WxFontUtils.hpp +++ b/src/slic3r/Utils/WxFontUtils.hpp @@ -41,6 +41,8 @@ public: static bool is_italic(const wxFont &font); static bool is_bold(const wxFont &font); + static void get_suitable_font_size(int max_height, wxDC &dc); + /// /// Set italic into wx font /// When italic font is same as original return nullptr.