Skip to content

Commit

Permalink
fixed on Windows zooming the font reverts the font family to Sans (#2257
Browse files Browse the repository at this point in the history
)
  • Loading branch information
giuspen committed Apr 10, 2023
1 parent 5778331 commit def8947
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/ct/ct_main_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,10 @@ void CtMainWin::config_switch_tree_side()
void CtMainWin::_zoom_tree(bool is_increase)
{
Glib::RefPtr<Gtk::StyleContext> 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(); });
}

Expand Down
34 changes: 19 additions & 15 deletions src/ct/ct_misc_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Glib::ustring> 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<Glib::ustring> 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;
Expand Down
4 changes: 0 additions & 4 deletions src/ct/ct_misc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions src/ct/ct_text_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -626,33 +626,31 @@ void CtTextView::cursor_and_tooltips_reset()
void CtTextView::zoom_text(const bool is_increase, const std::string& syntaxHighlighting)
{
Glib::RefPtr<Gtk::StyleContext> 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(); });
Expand Down
5 changes: 2 additions & 3 deletions tests/tests_misc_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* tests_misc_utils.cpp
*
* Copyright 2009-2022
* Copyright 2009-2023
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
}
}

0 comments on commit def8947

Please sign in to comment.