Skip to content

Commit

Permalink
Add EmptyFontMgr when LAF_BACKEND=none
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Feb 20, 2024
1 parent a91b09f commit 2439471
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
3 changes: 3 additions & 0 deletions text/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ if(LAF_BACKEND STREQUAL "skia")
skia_font_mgr.cpp)
target_link_libraries(laf-text
skia skshaper skunicode)
else()
target_sources(laf-text PRIVATE
empty_font_mgr.cpp)
endif()

if(LAF_WITH_TESTS)
Expand Down
72 changes: 72 additions & 0 deletions text/empty_font_mgr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// LAF Text Library
// Copyright (c) 2024 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#include "text/font_mgr.h"

#include "text/font.h"
#include "text/font_style.h"
#include "text/font_style_set.h"
#include "text/typeface.h"

namespace text {

class EmptyTypeface : public Typeface {
public:
EmptyTypeface() { }
FontStyle fontStyle() const override { return FontStyle(); }
};

class EmptyFont : public Font {
public:
EmptyFont() { }
FontType type() override { return FontType::Unknown; }
int height() const override { return 0; }
int textLength(const std::string& str) const override { return 0; };
bool isScalable() const override { return false; }
void setSize(int size) override { }
void setAntialias(bool antialias) override { }
bool hasCodePoint(int codepoint) const override { return false; }
};

class EmptyFontStyleSet : public FontStyleSet {
public:
EmptyFontStyleSet() { }
int count() override { return 0; }
void getStyle(int index,
FontStyle& style,
std::string& name) override { }
TypefaceRef typeface(int index) override {
return base::make_ref<EmptyTypeface>();
}
TypefaceRef matchStyle(const FontStyle& style) override {
return base::make_ref<EmptyTypeface>();
}
};

class EmptyFontMgr : public FontMgr {
public:
EmptyFontMgr() { }
~EmptyFontMgr() { }
FontRef defaultFont(float size) const override {
return base::make_ref<EmptyFont>();
}
int countFamilies() const override { return 0; }
std::string familyName(int i) const override { return std::string(); }
FontStyleSetRef familyStyleSet(int i) const override {
return base::make_ref<EmptyFontStyleSet>();
}
FontStyleSetRef matchFamily(const std::string& familyName) const override {
return base::make_ref<EmptyFontStyleSet>();
}
};

// static
FontMgrRef FontMgr::Make()
{
return base::make_ref<EmptyFontMgr>();
}

} // namespace text
6 changes: 3 additions & 3 deletions text/font_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ namespace text {

class FontMgr : public base::RefCount {
public:
static base::Ref<FontMgr> Make();
static FontMgrRef Make();

FontRef loadSpriteSheetFont(const char* filename, int scale);
FontRef loadTrueTypeFont(const char* filename, int height);

virtual FontRef defaultFont(float size = 12) const = 0;
virtual int countFamilies() const = 0;
virtual std::string familyName(int index) const = 0;
virtual base::Ref<FontStyleSet> familyStyleSet(int index) const = 0;
virtual base::Ref<FontStyleSet> matchFamily(const std::string& familyName) const = 0;
virtual FontStyleSetRef familyStyleSet(int index) const = 0;
virtual FontStyleSetRef matchFamily(const std::string& familyName) const = 0;

protected:
FontMgr();
Expand Down
9 changes: 3 additions & 6 deletions text/font_style_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
#define LAF_TEXT_FONT_STYLE_SET_H_INCLUDED
#pragma once

#include "base/ref.h"
#include "text/typeface.h"
#include "text/fwd.h"

namespace text {

class FontStyle;

class FontStyleSet : public base::RefCount {
protected:
virtual ~FontStyleSet() { }
Expand All @@ -23,8 +20,8 @@ namespace text {
virtual void getStyle(int index,
FontStyle& style,
std::string& name) = 0;
virtual base::Ref<Typeface> typeface(int index) = 0;
virtual base::Ref<Typeface> matchStyle(const FontStyle& style) = 0;
virtual TypefaceRef typeface(int index) = 0;
virtual TypefaceRef matchStyle(const FontStyle& style) = 0;
};

} // namespace text
Expand Down
6 changes: 3 additions & 3 deletions text/skia_font_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ TypefaceRef SkiaFontStyleSet::matchStyle(const FontStyle& style)
// SkiaFontMgr

// static
base::Ref<FontMgr> FontMgr::Make()
FontMgrRef FontMgr::Make()
{
return base::make_ref<SkiaFontMgr>();
}
Expand Down Expand Up @@ -123,12 +123,12 @@ std::string SkiaFontMgr::familyName(int i) const
return std::string(name.c_str());
}

base::Ref<FontStyleSet> SkiaFontMgr::familyStyleSet(int i) const
FontStyleSetRef SkiaFontMgr::familyStyleSet(int i) const
{
return base::make_ref<SkiaFontStyleSet>(m_skFontMgr->createStyleSet(i));
}

base::Ref<FontStyleSet> SkiaFontMgr::matchFamily(const std::string& familyName) const
FontStyleSetRef SkiaFontMgr::matchFamily(const std::string& familyName) const
{
return base::make_ref<SkiaFontStyleSet>(m_skFontMgr->matchFamily(familyName.c_str()));
}
Expand Down
4 changes: 2 additions & 2 deletions text/skia_font_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SkiaFontMgr : public FontMgr {

int countFamilies() const override;
std::string familyName(int i) const override;
base::Ref<FontStyleSet> familyStyleSet(int i) const override;
base::Ref<FontStyleSet> matchFamily(const std::string& familyName) const override;
FontStyleSetRef familyStyleSet(int i) const override;
FontStyleSetRef matchFamily(const std::string& familyName) const override;

sk_sp<SkFontMgr> skFontMgr() const { return m_skFontMgr; }

Expand Down

0 comments on commit 2439471

Please sign in to comment.