Skip to content

Commit

Permalink
Refactor FontFaceCache
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Sep 7, 2024
1 parent 4b4e7f0 commit dad72ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 53 deletions.
74 changes: 29 additions & 45 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ void PathIterator::next()
m_index += m_elements[m_index].header.length;
}

const std::string emptyString;

FontFace::FontFace(plutovg_font_face_t* face)
: m_face(face)
{
Expand Down Expand Up @@ -424,68 +426,30 @@ plutovg_font_face_t* FontFace::release()
return std::exchange(m_face, nullptr);
}

FontFace FontFaceCache::addFontFace(const std::string& family, bool bold, bool italic, const std::string& filename)
bool FontFaceCache::addFontFace(const std::string& family, bool bold, bool italic, const std::string& filename)
{
return addFontFace(family, bold, italic, FontFace(filename));
}

FontFace FontFaceCache::addFontFace(const std::string& family, bool bold, bool italic, const void* data, size_t length)
bool FontFaceCache::addFontFace(const std::string& family, bool bold, bool italic, const void* data, size_t length)
{
return addFontFace(family, bold, italic, FontFace(data, length));
}

FontFace FontFaceCache::addFontFace(const std::string& family, bool bold, bool italic, const FontFace& face)
bool FontFaceCache::addFontFace(const std::string& family, bool bold, bool italic, const FontFace& face)
{
if(!face.isNull())
m_table[family].emplace_back(bold, italic, face);
return face;
}

#ifdef _WIN32
#define ARIAL_REGULAR "C:/Windows/Fonts/arial.ttf"
#define ARIAL_BOLD "C:/Windows/Fonts/arialbd.ttf"
#define ARIAL_ITALIC "C:/Windows/Fonts/ariali.ttf"
#define ARIAL_BOLD_ITALIC "C:/Windows/Fonts/arialbi.ttf"
#elif __APPLE__
#define ARIAL_REGULAR "/Library/Fonts/Arial.ttf"
#define ARIAL_BOLD "/Library/Fonts/Arial Bold.ttf"
#define ARIAL_ITALIC "/Library/Fonts/Arial Italic.ttf"
#define ARIAL_BOLD_ITALIC "/Library/Fonts/Arial Bold Italic.ttf"
#else
#define ARIAL_REGULAR "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
#define ARIAL_BOLD "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
#define ARIAL_ITALIC "/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf"
#define ARIAL_BOLD_ITALIC "/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf"
#endif

FontFace FontFaceCache::getFallbackFontFace(bool bold, bool italic)
{
if(!bold && !italic)
return addFontFace(emptyString, false, false, ARIAL_REGULAR);
if(bold && !italic)
return addFontFace(emptyString, true, false, ARIAL_BOLD);
if(!bold && italic)
return addFontFace(emptyString, false, true, ARIAL_ITALIC);
return addFontFace(emptyString, true, true, ARIAL_BOLD_ITALIC);
return !face.isNull();
}

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

if(family.empty()) {
for(const auto& item : it->second) {
if(bold == std::get<0>(item) && italic == std::get<1>(item)) {
return std::get<2>(item);
}
}

return getFallbackFontFace(bold, italic);
if(!family.empty())
return getFontFace(emptyString, bold, italic);
return FontFace();
}

auto select = [bold, italic](const FontFaceEntry& a, const FontFaceEntry& b) {
Expand All @@ -506,6 +470,26 @@ FontFace FontFaceCache::getFontFace(const std::string& family, bool bold, bool i
return std::get<2>(entry);
}

FontFaceCache::FontFaceCache()
{
#ifdef _WIN32
addFontFace(emptyString, false, false, "C:/Windows/Fonts/arial.ttf");
addFontFace(emptyString, true, false, "C:/Windows/Fonts/arialbd.ttf");
addFontFace(emptyString, false, true, "C:/Windows/Fonts/ariali.ttf");
addFontFace(emptyString, true, true, "C:/Windows/Fonts/arialbi.ttf");
#elif __APPLE__
addFontFace(emptyString, false, false, "/Library/Fonts/Arial.ttf");
addFontFace(emptyString, true, false, "/Library/Fonts/Arial Bold.ttf");
addFontFace(emptyString, false, true, "/Library/Fonts/Arial Italic.ttf");
addFontFace(emptyString, true, true, "/Library/Fonts/Arial Bold Italic.ttf");
#elif __linux__
addFontFace(emptyString, false, false, "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
addFontFace(emptyString, true, false, "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf");
addFontFace(emptyString, false, true, "/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf");
addFontFace(emptyString, true, true, "/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf");
#endif
}

FontFaceCache* fontFaceCache()
{
thread_local FontFaceCache cache;
Expand Down
11 changes: 5 additions & 6 deletions source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class PathIterator {
int m_index;
};

static std::string emptyString;
extern const std::string emptyString;

class FontFace {
public:
Expand All @@ -421,15 +421,14 @@ class FontFace {

class FontFaceCache {
public:
FontFace addFontFace(const std::string& family, bool bold, bool italic, const std::string& filename);
FontFace addFontFace(const std::string& family, bool bold, bool italic, const void* data, size_t length);
FontFace addFontFace(const std::string& family, bool bold, bool italic, const FontFace& face);
bool addFontFace(const std::string& family, bool bold, bool italic, const std::string& filename);
bool addFontFace(const std::string& family, bool bold, bool italic, const void* data, size_t length);
bool addFontFace(const std::string& family, bool bold, bool italic, const FontFace& face);

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

private:
FontFaceCache() = default;
FontFaceCache();
using FontFaceEntry = std::tuple<bool, bool, FontFace>;
std::map<std::string, std::vector<FontFaceEntry>> m_table;
friend FontFaceCache* fontFaceCache();
Expand Down
4 changes: 2 additions & 2 deletions source/lunasvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,12 @@ std::unique_ptr<Document> Document::loadFromData(const char* data, size_t length

bool Document::addFontFace(const std::string& family, bool bold, bool italic, const std::string& filename)
{
return !fontFaceCache()->addFontFace(family, bold, italic, filename).isNull();
return fontFaceCache()->addFontFace(family, bold, italic, filename);
}

bool Document::addFontFace(const std::string& family, bool bold, bool italic, const void* data, size_t length)
{
return !fontFaceCache()->addFontFace(family, bold, italic, data, length).isNull();
return fontFaceCache()->addFontFace(family, bold, italic, data, length);
}

float Document::width() const
Expand Down

0 comments on commit dad72ce

Please sign in to comment.