FIX: get suitable font size to draw the texts

jira: [STUDIO-10067]
Change-Id: I589fd6a271ae177e4630e403b64c18090aab9471
(cherry picked from commit d036c92a3793e8c0c758432b52f2af8ffeff751f)
This commit is contained in:
xin.zhang
2025-02-19 18:25:14 +08:00
committed by Noisyfox
parent 3dd369ad4e
commit c077d8e4d0
5 changed files with 61 additions and 15 deletions

View File

@@ -1029,6 +1029,7 @@ PinCodePanel::PinCodePanel(wxWindow* parent, int type, wxWindowID winid /*= wxID
if (m_type == 0) {txt = _L("Bind with Pin Code");} if (m_type == 0) {txt = _L("Bind with Pin Code");}
else if (m_type == 1) {txt = _L("Bind with Access 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); auto txt_size = dc.GetTextExtent(txt);
dc.DrawText(txt, wxPoint(FromDIP(28), (size.y - txt_size.y) / 2)); dc.DrawText(txt, wxPoint(FromDIP(28), (size.y - txt_size.y) / 2));

View File

@@ -3,6 +3,7 @@
#include "../BitmapCache.hpp" #include "../BitmapCache.hpp"
#include "../I18N.hpp" #include "../I18N.hpp"
#include "../GUI_App.hpp" #include "../GUI_App.hpp"
#include "../Utils/WxFontUtils.hpp"
#include <wx/simplebook.h> #include <wx/simplebook.h>
#include <wx/dcgraph.h> #include <wx/dcgraph.h>
@@ -229,9 +230,11 @@ void AMSExtText::doRender(wxDC& dc)
auto size = GetSize(); auto size = GetSize();
dc.SetPen(wxPen(StateColor::darkModeColorFor(AMS_CONTROL_GRAY800), 2, wxPENSTYLE_SOLID)); 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.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)); wxPoint pot(FromDIP((size.x - tsize.x) / 2), FromDIP((size.y - tsize.y) / 2));
dc.DrawText(_L("Ext"), pot); 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)); pot = wxPoint(FromDIP(5), ((size.y - hum_img.GetBmpSize().y) / 2));
dc.DrawBitmap(hum_img.bmp(), pot); dc.DrawBitmap(hum_img.bmp(), pot);
pot.x += hum_img.GetBmpSize().x + FromDIP(3);
// percentage // percentage
wxString hum_percentage(std::to_string(m_amsinfo.humidity_raw)); wxString hum_percentage(std::to_string(m_amsinfo.humidity_raw));
auto tsize = dc.GetMultiLineTextExtent(hum_percentage);
dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); dc.SetPen(wxPen(*wxTRANSPARENT_PEN));
dc.SetFont(Label::Body_14); dc.SetFont(Label::Body_14);
dc.SetTextForeground(StateColor::darkModeColorFor(AMS_CONTROL_BLACK_COLOUR)); 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); dc.SetFont(Label::Body_12);
tsize = dc.GetMultiLineTextExtent(_L("%")); WxFontUtils::get_suitable_font_size(0.5 * size.GetHeight(), dc);
pot.y += (tsize.y / 2 - FromDIP(4)); auto tsize2 = dc.GetMultiLineTextExtent(_L("%"));
pot.y = pot.y + ((tsize1.y - tsize2.y) / 2) + FromDIP(2);
dc.DrawText(_L("%"), pot); dc.DrawText(_L("%"), pot);
pot.x = pot.x + tsize.x + FromDIP(2); pot.x += tsize2.x + FromDIP(3);
} }
else /*image with number*/ else /*image with number*/
{ {
@@ -2916,8 +2923,8 @@ void AMSHumidity::doRender(wxDC& dc)
//sun image //sun image
/*pot.x = FromDIP(size.x * 0.69); /*pot.x = FromDIP(size.x * 0.69);
pot.y = FromDIP((size.y - ams_sun_img.GetBmpHeight()) / 2);*/ pot.y = FromDIP((size.y - ams_sun_img.GetBmpHeight()) / 2);*/
pot.x = pot.x + FromDIP(ams_sun_img.GetBmpWidth() / 2); pot.x = pot.x + (ams_sun_img.GetBmpWidth() / 2);
pot.y = FromDIP((size.y - ams_sun_img.GetBmpHeight()) / 2); pot.y = (size.y - ams_sun_img.GetBmpHeight()) / 2;
dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); dc.SetPen(wxPen(*wxTRANSPARENT_PEN));
dc.DrawBitmap(ams_sun_img.bmp(), pot); dc.DrawBitmap(ams_sun_img.bmp(), pot);

View File

@@ -4,6 +4,7 @@
#include "../wxExtensions.hpp" #include "../wxExtensions.hpp"
#include "../Utils/MacDarkMode.hpp" #include "../Utils/MacDarkMode.hpp"
#include "../Utils/WxFontUtils.hpp"
#include <wx/dcmemory.h> #include <wx/dcmemory.h>
#include <wx/dcclient.h> #include <wx/dcclient.h>
@@ -239,14 +240,16 @@ void SwitchBoard::doRender(wxDC &dc)
dc.DrawRoundedRectangle(0, 0, GetSize().x / 2, GetSize().y, 8); 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) { if (switch_left) {
dc.SetTextForeground(*wxWHITE); dc.SetTextForeground(*wxWHITE);
} else { } else {
dc.SetTextForeground(0x333333); 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)); dc.DrawText(leftLabel, wxPoint((GetSize().x / 2 - left_txt_size.x) / 2, (GetSize().y - left_txt_size.y) / 2));
/*right*/ /*right*/
@@ -257,7 +260,6 @@ void SwitchBoard::doRender(wxDC &dc)
} }
auto right_txt_size = dc.GetTextExtent(rightLabel); auto right_txt_size = dc.GetTextExtent(rightLabel);
dc.SetFont(::Label::Body_13);
if (switch_right) { if (switch_right) {
dc.SetTextForeground(*wxWHITE); dc.SetTextForeground(*wxWHITE);
} else { } else {

View File

@@ -297,6 +297,40 @@ bool WxFontUtils::is_bold(const wxFont &font) {
return wx_weight != wxFONTWEIGHT_NORMAL; 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<Emboss::FontFile> WxFontUtils::set_italic(wxFont &font, const Emboss::FontFile &font_file) std::unique_ptr<Emboss::FontFile> WxFontUtils::set_italic(wxFont &font, const Emboss::FontFile &font_file)
{ {
static std::vector<wxFontStyle> italic_styles = { static std::vector<wxFontStyle> italic_styles = {

View File

@@ -41,6 +41,8 @@ public:
static bool is_italic(const wxFont &font); static bool is_italic(const wxFont &font);
static bool is_bold(const wxFont &font); static bool is_bold(const wxFont &font);
static void get_suitable_font_size(int max_height, wxDC &dc);
/// <summary> /// <summary>
/// Set italic into wx font /// Set italic into wx font
/// When italic font is same as original return nullptr. /// When italic font is same as original return nullptr.