Skip to content

Commit

Permalink
Support font-family names with quotes #186
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Oct 18, 2024
1 parent 4628afe commit 3769b62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 1 addition & 3 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,10 @@ bool FontFaceCache::addFontFace(const std::string& family, bool bold, bool itali
return !face.isNull();
}

FontFace FontFaceCache::getFontFace(const std::string& family, bool bold, bool italic)
FontFace FontFaceCache::getFontFace(const std::string_view& family, bool bold, bool italic)
{
auto it = m_table.find(family);
if(it == m_table.end()) {
if(!family.empty())
return getFontFace(emptyString, bold, italic);
return FontFace();
}

Expand Down
5 changes: 2 additions & 3 deletions source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,12 @@ class FontFace {
class FontFaceCache {
public:
bool addFontFace(const std::string& family, bool bold, bool italic, const FontFace& face);

FontFace getFontFace(const std::string& family, bool bold, bool italic);
FontFace getFontFace(const std::string_view& family, bool bold, bool italic);

private:
FontFaceCache();
using FontFaceEntry = std::tuple<bool, bool, FontFace>;
std::map<std::string, std::vector<FontFaceEntry>> m_table;
std::map<std::string, std::vector<FontFaceEntry>, std::less<>> m_table;
friend FontFaceCache* fontFaceCache();
};

Expand Down
23 changes: 22 additions & 1 deletion source/svglayoutstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,28 @@ Font SVGLayoutState::font() const
{
auto bold = m_font_weight == FontWeight::Bold;
auto italic = m_font_style == FontStyle::Italic;
auto face = fontFaceCache()->getFontFace(m_font_family, bold, italic);

FontFace face;
std::string_view input(m_font_family);
while(!input.empty() && face.isNull()) {
auto family = input.substr(0, input.find(','));
input.remove_prefix(family.length());
if(!input.empty() && input.front() == ',')
input.remove_prefix(1);
stripLeadingAndTrailingSpaces(family);
if(!family.empty() && (family.front() == '\'' || family.front() == '"')) {
auto quote = family.front();
family.remove_prefix(1);
if(!family.empty() && family.back() == quote)
family.remove_suffix(1);
stripLeadingAndTrailingSpaces(family);
}

face = fontFaceCache()->getFontFace(family, bold, italic);
}

if(face.isNull())
face = fontFaceCache()->getFontFace(emptyString, bold, italic);
return Font(face, m_font_size);
}

Expand Down

0 comments on commit 3769b62

Please sign in to comment.