From 55f4aaf179cf723c5b703b0c4d848bf059ae190a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 19:08:22 +0100 Subject: font family implemented in the text component --- src/crepe/api/Text.cpp | 8 ++++-- src/crepe/api/Text.h | 17 +++++++++--- src/crepe/facade/CMakeLists.txt | 4 +-- src/crepe/facade/FontFacade.cpp | 50 +++++++++++++++++++++++++++++++++ src/crepe/facade/FontFacade.h | 28 +++++++++++++++++++ src/crepe/facade/SDLFontContext.cpp | 55 ------------------------------------- src/crepe/facade/SDLFontContext.h | 29 ------------------- src/example/loadfont.cpp | 21 ++++---------- 8 files changed, 105 insertions(+), 107 deletions(-) create mode 100644 src/crepe/facade/FontFacade.cpp create mode 100644 src/crepe/facade/FontFacade.h delete mode 100644 src/crepe/facade/SDLFontContext.cpp delete mode 100644 src/crepe/facade/SDLFontContext.h diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 568af7f..e6db140 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,10 +1,14 @@ +#include "../facade/FontFacade.h" + #include "Text.h" + using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, const std::string & font_family, const Data & data) : UIObject(id, dimensions, offset), text(text), - data(data), - font_family(font_family) {} + data(data), + font(FontFacade::get_font_asset(font_family)) { +} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 64b2008..13b4375 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -5,6 +5,7 @@ #include "../Component.h" #include "Color.h" +#include "Asset.h" #include "UIObject.h" namespace crepe { @@ -39,13 +40,21 @@ public: }; public: + /** + * + * \param dimensions Width and height of the UIObject. + * \param offset Offset of the UIObject relative to its transform + * \param text The text to be displayed. + * \param font_family The font style name to be displayed. + * \param data Data struct containing extra text parameters. + */ Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string &, const std::string & font_family, const Data & data); - //! font family name such as (Arial,Helvetica,Inter) - std::string font_family = ""; + const std::string & text, const std::string & font_family, const Data & data); //! Label text. std::string text = ""; - // Data instance for data not gotten from constructor + //! Font asset variable + Asset font; + //! Data instance Data data; }; diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index e61b680..243ae46 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -4,7 +4,7 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp - SDLFontContext.cpp + FontFacade.cpp Font.cpp ) @@ -14,7 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h - SDLFontContext.h + FontFacade.h Font.h ) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp new file mode 100644 index 0000000..12e10ed --- /dev/null +++ b/src/crepe/facade/FontFacade.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +#include "FontFacade.h" + +using namespace crepe; +using namespace std; + +Asset FontFacade::get_font_asset(const string font_family) { + if (!FcInit()) { + throw runtime_error("Failed to initialize Fontconfig."); + } + // Create a pattern to search for the font family + FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); + if (pattern == NULL) { + throw runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig * config = FcConfigGetCurrent(); + if (config == NULL) { + FcPatternDestroy(pattern); + throw runtime_error("Failed to get current Fontconfig configuration."); + } + + // Match the font pattern + FcResult result; + FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); + FcPatternDestroy(pattern); + + if (matched_pattern == NULL) { + FcPatternDestroy(matched_pattern); + throw runtime_error("No matching font found."); + } + + // Extract the file path + FcChar8 * file_path = nullptr; + if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch + || file_path == NULL) { + FcPatternDestroy(matched_pattern); + throw runtime_error("Failed to get font file path."); + } + + // Convert the file path to a string + string font_file_path(reinterpret_cast(file_path)); + FcPatternDestroy(matched_pattern); + FcFini(); + return Asset(font_file_path); +} diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h new file mode 100644 index 0000000..1b835d4 --- /dev/null +++ b/src/crepe/facade/FontFacade.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "../api/Asset.h" + +namespace crepe { + +/** + * + * \brief Font facade class for converting font family names to absolute file paths + * + */ +class FontFacade { +public: + /** + * + * \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. + * This function is static so the member function can be used without create a FontFacade object. This way it can be used in a constructor as FontFacade::get_font_asset(). + * \param font_family Name of the font family name. + * \return Asset with filepath to the font. + */ + static Asset get_font_asset(const std::string font_family); +}; + +} // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp deleted file mode 100644 index 45d70cb..0000000 --- a/src/crepe/facade/SDLFontContext.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include - -#include "SDLFontContext.h" - -using namespace crepe; -using namespace std; - -SDLFontContext::SDLFontContext() { - if (!FcInit()) { - throw runtime_error("Failed to initialize Fontconfig."); - } -} - -SDLFontContext::~SDLFontContext() { FcFini(); } - -Asset SDLFontContext::get_font_asset(const string font_family) { - - // Create a pattern to search for the font family - FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (pattern == NULL) { - throw runtime_error("Failed to create font pattern."); - } - - // Default configuration - FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { - FcPatternDestroy(pattern); - throw runtime_error("Failed to get current Fontconfig configuration."); - } - - // Match the font pattern - FcResult result; - FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); - FcPatternDestroy(pattern); - - if (matched_pattern == NULL) { - FcPatternDestroy(matched_pattern); - throw runtime_error("No matching font found."); - } - - // Extract the file path - FcChar8 * file_path = nullptr; - if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch - || file_path == NULL) { - FcPatternDestroy(matched_pattern); - throw runtime_error("Failed to get font file path."); - } - - // Convert the file path to a string - string font_file_path(reinterpret_cast(file_path)); - FcPatternDestroy(matched_pattern); - return Asset(font_file_path); -} diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h deleted file mode 100644 index d15e1a3..0000000 --- a/src/crepe/facade/SDLFontContext.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - - -#include - -#include "../api/Asset.h" - -namespace crepe { -class SDLFontContext { -public: - SDLFontContext(); - ~SDLFontContext(); - SDLFontContext(const SDLFontContext &) = delete; - SDLFontContext(SDLFontContext &&) = delete; - SDLFontContext & operator=(const SDLFontContext &) = delete; - SDLFontContext & operator=(SDLFontContext &&) = delete; - /** - * - * \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. - */ - Asset get_font_asset(const std::string font_family); - -}; - -} // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index dae64bc..0ea6d86 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -9,26 +8,18 @@ using namespace crepe; int main() { - SDLFontContext font_facade; + // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); - Asset asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset.get_path() << std::endl; - std::unique_ptr font = std::make_unique(asset, mediator); - // Get the TTF_Font from the Font object - TTF_Font * ttf_font = font->get_font(); - //example if the asset is not correct for font - //std::unique_ptr fontThrow = std::make_unique(Asset("../help.txt"), mediator); - // Check if the font is loaded properly - if (ttf_font != nullptr) { - std::cout << "Font successfully loaded!" << std::endl; - } else { - std::cout << "Failed to load font." << std::endl; - } + std::cout << "Path: " << label->font.get_path() << std::endl; + + std::unique_ptr label2 = std::make_unique( + 1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::cout << "Path: " << label2->font.get_path() << std::endl; } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3