Skip to content

Commit

Permalink
Add check for symbolic font in fontconfig code
Browse files Browse the repository at this point in the history
  • Loading branch information
AdenKoperczak committed Dec 31, 2024
1 parent 6bf3ae8 commit f34a3e2
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions scwx-qt/source/scwx/qt/manager/font_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ FontManager::Impl::MatchFontFile(const std::string& family,
FcPatternAddString(pattern,
FC_FONTFORMAT,
reinterpret_cast<const FcChar8*>(kFcTrueType_.c_str()));
FcPatternAddBool(pattern, FC_SYMBOL, FcFalse);

if (!styles.empty())
{
Expand All @@ -485,29 +486,51 @@ FontManager::Impl::MatchFontFile(const std::string& family,
FcDefaultSubstitute(pattern);

// Find matching font
FcResult result;
FcPattern* match = FcFontMatch(nullptr, pattern, &result);
FcResult result {};
FcFontSet* matches = FcFontSort(nullptr, pattern, FcFalse, nullptr, &result);
FontRecord record {};

if (match != nullptr)
if (matches != nullptr)
{
FcChar8* fcFamily;
FcChar8* fcStyle;
FcChar8* fcFile;

// Match was found, get properties
if (FcPatternGetString(match, FC_FAMILY, 0, &fcFamily) == FcResultMatch &&
FcPatternGetString(match, FC_STYLE, 0, &fcStyle) == FcResultMatch &&
FcPatternGetString(match, FC_FILE, 0, &fcFile) == FcResultMatch)
for (int i = 0; i < matches->nfont; i++)
{
record.family_ = reinterpret_cast<char*>(fcFamily);
record.style_ = reinterpret_cast<char*>(fcStyle);
record.filename_ = reinterpret_cast<char*>(fcFile);

logger_->debug("Found matching font: {}:{} ({})",
record.family_,
record.style_,
record.filename_);
FcPattern* match =
// Using C code requires pointer arithmetic
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
FcFontRenderPrepare(nullptr, pattern, matches->fonts[i]);
if (match == nullptr)
{
continue;
}
FcChar8* fcFamily = nullptr;
FcChar8* fcStyle = nullptr;
FcChar8* fcFile = nullptr;
FcBool fcSymbol = FcFalse;

// Match was found, get properties
if (FcPatternGetString(match, FC_FAMILY, 0, &fcFamily) ==
FcResultMatch &&
FcPatternGetString(match, FC_STYLE, 0, &fcStyle) ==
FcResultMatch &&
FcPatternGetString(match, FC_FILE, 0, &fcFile) == FcResultMatch &&
FcPatternGetBool(match, FC_SYMBOL, 0, &fcSymbol) ==
FcResultMatch &&
fcSymbol == FcFalse /*Must check fcSymbol manually*/)
{
record.family_ = reinterpret_cast<char*>(fcFamily);
record.style_ = reinterpret_cast<char*>(fcStyle);
record.filename_ = reinterpret_cast<char*>(fcFile);

logger_->debug("Found matching font: {}:{} ({}) {}",
record.family_,
record.style_,
record.filename_,
fcSymbol);
FcPatternDestroy(match);
break;
}

FcPatternDestroy(match);
}
}

Expand All @@ -517,7 +540,7 @@ FontManager::Impl::MatchFontFile(const std::string& family,
}

// Cleanup
FcPatternDestroy(match);
FcFontSetDestroy(matches);
FcPatternDestroy(pattern);

return record;
Expand Down

0 comments on commit f34a3e2

Please sign in to comment.