diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-16 19:08:22 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-16 19:08:22 +0100 |
commit | 55f4aaf179cf723c5b703b0c4d848bf059ae190a (patch) | |
tree | 56b7034d665783384415abc1a0e9282c194ae1c3 /src/crepe/facade/FontFacade.cpp | |
parent | 7f7f1237532b4318361e69349cc38f34fe981803 (diff) |
font family implemented in the text component
Diffstat (limited to 'src/crepe/facade/FontFacade.cpp')
-rw-r--r-- | src/crepe/facade/FontFacade.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
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 <stdexcept> +#include <SDL2/SDL_ttf.h> +#include <fontconfig/fontconfig.h> + +#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<const FcChar8 *>(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<const char *>(file_path)); + FcPatternDestroy(matched_pattern); + FcFini(); + return Asset(font_file_path); +} |