diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-10 09:47:46 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-10 09:47:46 +0100 |
commit | 29f2d4537cf4d4d4c74c16447a45a5d7034161f4 (patch) | |
tree | dd5ab4fbdf22ae9160a944faf283f3a853fd8281 /src/crepe | |
parent | 4d74c80d9c3c9fdca9b07d960a1f9fd3e38cdfad (diff) |
sdlfontcontext
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.cpp | 51 | ||||
-rw-r--r-- | src/crepe/facade/SDLFontContext.h | 15 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 4cc53bc..9b73323 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp + SDLFontContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -10,5 +11,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h + SDLFontContext.h ) diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp new file mode 100644 index 0000000..6ce5efd --- /dev/null +++ b/src/crepe/facade/SDLFontContext.cpp @@ -0,0 +1,51 @@ +#include "SDLFontContext.h" +#include <stdexcept> + +using namespace crepe; +using namespace std; + +SDLFontContext::SDLFontContext(){ + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } +} + +SDLFontContext::~SDLFontContext(){ + FcFini(); +} +Asset SDLFontContext::get_font_asset(const std::string & font_family) { + // Create a pattern to search for the font family + FcPattern* pattern = FcNameParse(reinterpret_cast<const FcChar8*>(font_family.c_str())); + if (!pattern) { + throw std::runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig* config = FcConfigGetCurrent(); + if (!config) { + FcPatternDestroy(pattern); + throw std::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) { + throw std::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) { + FcPatternDestroy(matched_pattern); + throw std::runtime_error("Failed to get font file path."); + } + + // Convert the file path to a std::string + std::string font_file_path(reinterpret_cast<const char*>(file_path)); + FcPatternDestroy(matched_pattern); + + return Asset(font_file_path); +} diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h new file mode 100644 index 0000000..3b2e162 --- /dev/null +++ b/src/crepe/facade/SDLFontContext.h @@ -0,0 +1,15 @@ +#pragma once + +#include <SDL2/SDL_ttf.h> +#include <fontconfig/fontconfig.h> +#include "../api/Asset.h" +namespace crepe { + class SDLFontContext{ + public: + SDLFontContext(); + ~SDLFontContext(); + Asset get_font_asset(const std::string & font_family); + private: + }; + +} |