diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/Font.cpp | 10 | ||||
-rw-r--r-- | src/crepe/facade/Font.h | 12 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.cpp | 11 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.h | 8 |
4 files changed, 26 insertions, 15 deletions
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 2ad176a..b600b01 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,24 +1,28 @@ -#include "font.h" #include "../api/Config.h" + +#include "font.h" + using namespace std; using namespace crepe; void Font::load(unique_ptr<Asset> res){ const char* font_path = res->get_path(); - int font_size = Config::get_instance().font.font_size; this->font = std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)>( - TTF_OpenFont(font_path, font_size), &TTF_CloseFont); + TTF_OpenFont(font_path, this->default_font_size), &TTF_CloseFont); if (!font) { throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); } } + Font::Font(const char* src){ this->load(make_unique<Asset>(src)); } + Font::Font(std::unique_ptr<Asset> res){ this->load(std::move(res)); } + const TTF_Font& Font::get_font() const{ return this->font; } diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index e34ac63..8bf9fc9 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -9,17 +9,17 @@ namespace crepe { /** - * \brief Font resource facade + * \brief Resource for managing font creation and destruction * * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. */ class Font : public Resource{ public: - /**. - * \param src Asset with texture data to load. - * \param mediator use the SDLContext reference to load the image + /** + * \param src Asset with font data to load. + * \param mediator use the SDLContext reference to load text */ - Font(const Asset & src, Mediator & mediator); + Font( src, Mediator & mediator); ~Font() = default @@ -27,7 +27,7 @@ public: private: //! The SDL_ttf font object with custom deleter. std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font; - int default_font_size = Config::get_instance().font.font_size; + unsigned int default_font_size = Config::get_instance().font.size; }; } // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index a4be143..d7a0bff 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -7,16 +7,16 @@ using namespace crepe; using namespace std; SDLFontContext::SDLFontContext(){ - + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } } SDLFontContext::~SDLFontContext(){ - + FcFini(); } unique_ptr<Asset> SDLFontContext::get_font_asset(const std::string & font_family) { - if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); - } + // Create a pattern to search for the font family FcPattern* pattern = FcNameParse(reinterpret_cast<const FcChar8*>(font_family.c_str())); if (!pattern) { @@ -49,6 +49,5 @@ unique_ptr<Asset> SDLFontContext::get_font_asset(const std::string & font_family // Convert the file path to a std::string std::string font_file_path(reinterpret_cast<const char*>(file_path)); FcPatternDestroy(matched_pattern); - FcFini(); return std::move(make_unique<Asset>(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index cd91383..c890b2d 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -11,6 +11,14 @@ namespace crepe { public: SDLFontContext(); ~SDLFontContext(); + /** + * + * \brief Facade function to convert a font_family into an asset. + * + * This function uses the FontConfig library to convert a font family name (Arial, Inter, Helvetica) and converts it to the font source path. + * + * \param font_family Name of the font family name. + */ std::unique_ptr<Asset> get_font_asset(const std::string & font_family); private: }; |