From def8947f372a794755f8bcf3ba357ac5dfaa2013 Mon Sep 17 00:00:00 2001 From: Giuseppe Penone Date: Mon, 10 Apr 2023 19:53:00 +0100 Subject: [PATCH] fixed on Windows zooming the font reverts the font family to Sans (#2257) --- src/ct/ct_main_win.cc | 5 ++--- src/ct/ct_misc_utils.cc | 34 +++++++++++++++++++--------------- src/ct/ct_misc_utils.h | 4 ---- src/ct/ct_text_view.cc | 14 ++++++-------- tests/tests_misc_utils.cpp | 5 ++--- 5 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/ct/ct_main_win.cc b/src/ct/ct_main_win.cc index 2282ade02..4287ff695 100644 --- a/src/ct/ct_main_win.cc +++ b/src/ct/ct_main_win.cc @@ -650,11 +650,10 @@ void CtMainWin::config_switch_tree_side() void CtMainWin::_zoom_tree(bool is_increase) { Glib::RefPtr context = _uCtTreeview->get_style_context(); - Pango::FontDescription fontDesc = context->get_font(context->get_state()); + const Pango::FontDescription fontDesc = context->get_font(context->get_state()); int size = fontDesc.get_size() / Pango::SCALE + (is_increase ? 1 : -1); if (size < 6) size = 6; - fontDesc.set_size(size * Pango::SCALE); - _pCtConfig->treeFont = CtFontUtil::get_font_str(fontDesc); + _pCtConfig->treeFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->treeFont), size); signal_app_apply_for_each_window([](CtMainWin* win) { win->update_theme(); win->window_header_update(); }); } diff --git a/src/ct/ct_misc_utils.cc b/src/ct/ct_misc_utils.cc index 38127df6f..fdd6f879b 100644 --- a/src/ct/ct_misc_utils.cc +++ b/src/ct/ct_misc_utils.cc @@ -829,32 +829,36 @@ void CtStrUtil::convert_if_not_utf8(std::string& inOutText, const bool sanitise) Glib::ustring CtFontUtil::get_font_family(const Glib::ustring& fontStr) { + try { + std::vector fontStr_splitted = str::split(fontStr, CtConst::CHAR_SPACE); + fontStr_splitted.pop_back(); + Glib::ustring retVal = str::join(fontStr_splitted, CtConst::CHAR_SPACE); + return retVal; + } + catch (...) { + spdlog::warn("{} {}", __FUNCTION__, fontStr.raw()); + } return Pango::FontDescription(fontStr).get_family(); } -int CtFontUtil::get_font_size(const Pango::FontDescription& fontDesc) -{ - return fontDesc.get_size()/Pango::SCALE; -} - int CtFontUtil::get_font_size(const Glib::ustring& fontStr) { - return get_font_size(Pango::FontDescription(fontStr)); + try { + const std::vector fontStr_splitted = str::split(fontStr, CtConst::CHAR_SPACE); + const int retVal = std::stoi(fontStr_splitted.back()); + return retVal; + } + catch (...) { + spdlog::warn("{} {}", __FUNCTION__, fontStr.raw()); + } + return Pango::FontDescription(fontStr).get_size()/Pango::SCALE; } Glib::ustring CtFontUtil::get_font_str(const Glib::ustring& fontFamily, const int fontSize) { - return fontFamily + " " + std::to_string(fontSize); + return fontFamily + CtConst::CHAR_SPACE + std::to_string(fontSize); } -Glib::ustring CtFontUtil::get_font_str(const Pango::FontDescription& fontDesc) -{ - Glib::ustring font_family = fontDesc.get_family(); - auto font_family_splitted = str::split(font_family, ","); - return font_family_splitted.back() + " " + std::to_string(get_font_size(fontDesc)); -} - - void CtRgbUtil::set_rgb24str_from_rgb24int(guint32 rgb24Int, char* rgb24StrOut) { guint8 r = (rgb24Int >> 16) & 0xff; diff --git a/src/ct/ct_misc_utils.h b/src/ct/ct_misc_utils.h index 3a434de7d..cc4c5192f 100644 --- a/src/ct/ct_misc_utils.h +++ b/src/ct/ct_misc_utils.h @@ -259,14 +259,10 @@ namespace CtFontUtil { Glib::ustring get_font_family(const Glib::ustring& fontStr); -int get_font_size(const Pango::FontDescription& fontDesc); - int get_font_size(const Glib::ustring& fontStr); Glib::ustring get_font_str(const Glib::ustring& fontFamily, const int fontSize); -Glib::ustring get_font_str(const Pango::FontDescription& fontDesc); - } // namespace CtFontUtil namespace CtRgbUtil { diff --git a/src/ct/ct_text_view.cc b/src/ct/ct_text_view.cc index f7d562177..dba951ac9 100644 --- a/src/ct/ct_text_view.cc +++ b/src/ct/ct_text_view.cc @@ -626,33 +626,31 @@ void CtTextView::cursor_and_tooltips_reset() void CtTextView::zoom_text(const bool is_increase, const std::string& syntaxHighlighting) { Glib::RefPtr context = get_style_context(); - Pango::FontDescription fontDesc = context->get_font(context->get_state()); + const Pango::FontDescription fontDesc = context->get_font(context->get_state()); int size = fontDesc.get_size() / Pango::SCALE + (is_increase ? 1 : -1); if (size < 6) size = 6; - fontDesc.set_size(size * Pango::SCALE); if (syntaxHighlighting == CtConst::RICH_TEXT_ID or syntaxHighlighting == CtConst::TABLE_CELL_TEXT_ID) { - _pCtConfig->rtFont = CtFontUtil::get_font_str(fontDesc); + _pCtConfig->rtFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->rtFont), size); spdlog::debug("rtFont {}", _pCtConfig->rtFont); // also fix monospace font size if (_pCtConfig->msDedicatedFont and not _pCtConfig->monospaceFont.empty()) { - Pango::FontDescription monoFontDesc(_pCtConfig->monospaceFont); + const Pango::FontDescription monoFontDesc(_pCtConfig->monospaceFont); int monoSize = monoFontDesc.get_size() / Pango::SCALE + (is_increase ? 1 : -1); if (monoSize < 6) monoSize = 6; - monoFontDesc.set_size(monoSize * Pango::SCALE); - _pCtConfig->monospaceFont = CtFontUtil::get_font_str(monoFontDesc); + _pCtConfig->monospaceFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->monospaceFont), size); if (auto tag = get_buffer()->get_tag_table()->lookup(CtConst::TAG_ID_MONOSPACE)) { tag->property_font() = _pCtConfig->monospaceFont; } } } else if (syntaxHighlighting == CtConst::PLAIN_TEXT_ID) { - _pCtConfig->ptFont = CtFontUtil::get_font_str(fontDesc); + _pCtConfig->ptFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->ptFont), size); spdlog::debug("ptFont {}", _pCtConfig->ptFont); } else { - _pCtConfig->codeFont = CtFontUtil::get_font_str(fontDesc); + _pCtConfig->codeFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->codeFont), size); spdlog::debug("codeFont {}", _pCtConfig->codeFont); } _pCtMainWin->signal_app_apply_for_each_window([](CtMainWin* win) { win->update_theme(); }); diff --git a/tests/tests_misc_utils.cpp b/tests/tests_misc_utils.cpp index 2c3dd5bbb..e0e36c63b 100644 --- a/tests/tests_misc_utils.cpp +++ b/tests/tests_misc_utils.cpp @@ -1,7 +1,7 @@ /* * tests_misc_utils.cpp * - * Copyright 2009-2022 + * Copyright 2009-2023 * Giuseppe Penone * Evgenii Gurianov * @@ -173,7 +173,6 @@ TEST(MiscUtilsGroup, getFontMisc) ASSERT_STREQ("Noto Sans", CtFontUtil::get_font_family("Noto Sans 9").c_str()); ASSERT_EQ(9, CtFontUtil::get_font_size("Noto Sans 9")); ASSERT_STREQ("Noto Sans 9", CtFontUtil::get_font_str("Noto Sans", 9).c_str()); - ASSERT_STREQ("Noto Sans 9", CtFontUtil::get_font_str(Pango::FontDescription("Noto Sans 9")).c_str()); } TEST(MiscUtilsGroup, set_rgb24str_from_rgb24int) @@ -491,4 +490,4 @@ TEST(MiscUtilsGroup, gtk_pango_find_start_of_dir) ASSERT_EQ(-1, CtStrUtil::gtk_pango_find_start_of_dir("זה טקסט שנכתב בעברית, מימין לשמאל, עם סימני פיסוק! וגם; מספרים כמו", PANGO_DIRECTION_LTR)); ASSERT_EQ(8, CtStrUtil::gtk_pango_find_start_of_dir("Test 123" "עִבְרִית", PANGO_DIRECTION_RTL)); ASSERT_EQ(16, CtStrUtil::gtk_pango_find_start_of_dir("עִבְרִית" "Test 123", PANGO_DIRECTION_LTR)); -} \ No newline at end of file +}