Skip to content

Commit

Permalink
Fix bug ignoring bold/italic after we click another font name
Browse files Browse the repository at this point in the history
If we clicked bold/italic, and then choose another font family, we were
using the cached typeface inside the FontInfo instead of an update
typeface with the selected styles applied (bold/italic).

Now we don't cache the typeface inside FontInfo to avoid this.
  • Loading branch information
dacap committed Apr 15, 2024
1 parent 4981d44 commit 61ec7cd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
21 changes: 8 additions & 13 deletions src/app/font_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ FontInfo::FontInfo(Type type,
const std::string& name,
const float size,
const text::FontStyle style,
const bool antialias,
const text::TypefaceRef& typeface)
const bool antialias)
: m_type(type)
, m_name(name)
, m_size(size)
, m_style(style)
, m_antialias(antialias)
, m_typeface(typeface)
{
}

Expand All @@ -45,7 +43,6 @@ FontInfo::FontInfo(const FontInfo& other,
, m_size(size)
, m_style(style)
, m_antialias(antialias)
, m_typeface(other.typeface())
{
}

Expand All @@ -67,18 +64,16 @@ std::string FontInfo::thumbnailId() const
return std::string();
}

void FontInfo::findTypeface(const text::FontMgrRef& fontMgr) const
text::TypefaceRef FontInfo::findTypeface(const text::FontMgrRef& fontMgr) const
{
if (m_type != Type::System ||
m_typeface != nullptr) {
return;
}
if (m_type != Type::System)
return nullptr;

const text::FontStyleSetRef set = fontMgr->matchFamily(m_name);
if (set) {
if (auto newTypeface = set->matchStyle(m_style))
m_typeface = newTypeface;
}
if (set)
return set->matchStyle(m_style);

return nullptr;
}

} // namespace app
Expand Down
7 changes: 2 additions & 5 deletions src/app/font_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ namespace app {
const std::string& name = {},
float size = kDefaultSize,
text::FontStyle style = text::FontStyle(),
bool antialias = false,
const text::TypefaceRef& typeface = nullptr);
bool antialias = false);

FontInfo(const FontInfo& other,
float size,
Expand All @@ -60,8 +59,7 @@ namespace app {
text::FontStyle style() const { return m_style; }
bool antialias() const { return m_antialias; }

void findTypeface(const text::FontMgrRef& fontMgr) const;
text::TypefaceRef typeface() const { return m_typeface; }
text::TypefaceRef findTypeface(const text::FontMgrRef& fontMgr) const;

bool operator==(const FontInfo& other) const {
return (m_type == other.m_type &&
Expand All @@ -76,7 +74,6 @@ namespace app {
float m_size = kDefaultSize;
text::FontStyle m_style;
bool m_antialias = false;
mutable text::TypefaceRef m_typeface;
};

} // namespace app
Expand Down
9 changes: 3 additions & 6 deletions src/app/ui/font_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ class FontItem : public ListItem {

FontItem(const std::string& name,
const text::FontStyle& style,
const text::FontStyleSetRef& set,
const text::TypefaceRef& typeface)
const text::FontStyleSetRef& set)
: ListItem(name)
, m_fontInfo(FontInfo::Type::System, name,
FontInfo::kDefaultSize,
style, true, typeface)
FontInfo::kDefaultSize, style, true)
, m_set(set) {
getCachedThumbnail();
}
Expand Down Expand Up @@ -240,8 +238,7 @@ FontPopup::FontPopup(const FontInfo& fontInfo)
// weight, Upright slant, etc.)
auto typeface = set->matchStyle(text::FontStyle());
if (typeface) {
auto* item = new FontItem(name, typeface->fontStyle(),
set, typeface);
auto* item = new FontItem(name, typeface->fontStyle(), set);
item->ThumbnailGenerated.connect([this]{ onThumbnailGenerated(); });
m_listBox.addChild(item);
empty = false;
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/render_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ doc::ImageRef render_text(

if (fontInfo.type() == FontInfo::Type::System) {
// Just in case the typeface is not present in the FontInfo
fontInfo.findTypeface(fontMgr);
auto typeface = fontInfo.findTypeface(fontMgr);

const text::FontMgrRef fontMgr = theme->fontMgr();
font = fontMgr->makeFont(fontInfo.typeface());
font = fontMgr->makeFont(typeface);
if (!fontInfo.useDefaultSize())
font->setSize(fontInfo.size());
}
Expand Down

0 comments on commit 61ec7cd

Please sign in to comment.