Skip to content

Commit

Permalink
Use Skia to load external (TTF/OTF) fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Apr 25, 2024
1 parent 4291c70 commit baa12a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions text/font_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ FontRef FontMgr::loadSpriteSheetFont(const char* filename, int scale)
return font;
}

FontRef FontMgr::loadTrueTypeFont(const char* filename, int height)
FontRef FontMgr::loadTrueTypeFont(const char* filename, float size)
{
#if LAF_FREETYPE
if (!m_ft)
m_ft.reset(new ft::Lib());
return FreeTypeFont::LoadFont(*m_ft.get(), filename, height);
return FreeTypeFont::LoadFont(*m_ft.get(), filename, size);
#else
return nullptr;
#endif
Expand Down
2 changes: 1 addition & 1 deletion text/font_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace text {
static FontMgrRef Make();

FontRef loadSpriteSheetFont(const char* filename, int scale);
FontRef loadTrueTypeFont(const char* filename, int height);
virtual FontRef loadTrueTypeFont(const char* filename, float size);
virtual FontRef makeFont(const TypefaceRef& typeface) = 0;
virtual FontRef makeFont(const TypefaceRef& typeface, float size) = 0;

Expand Down
13 changes: 13 additions & 0 deletions text/skia_font_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ SkiaFontMgr::~SkiaFontMgr()
{
}

FontRef SkiaFontMgr::loadTrueTypeFont(const char* filename, float size)
{
// Use the native impl from Skia to load the font file
sk_sp<SkTypeface> face = m_skFontMgr->makeFromFile(filename);
if (!face) {
// In other case try the FreeType impl
return FontMgr::loadTrueTypeFont(filename, size);
}

SkFont skFont(face, size);
return base::make_ref<SkiaFont>(skFont);
}

FontRef SkiaFontMgr::defaultFont(float size) const
{
sk_sp<SkTypeface> face =
Expand Down
2 changes: 2 additions & 0 deletions text/skia_font_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class SkiaFontMgr : public FontMgr {
SkiaFontMgr();
~SkiaFontMgr();

FontRef loadTrueTypeFont(const char* filename, float size) override;

FontRef defaultFont(float size) const override;
FontRef makeFont(const TypefaceRef& typeface) override;
FontRef makeFont(const TypefaceRef& typeface, float size) override;
Expand Down

0 comments on commit baa12a8

Please sign in to comment.