aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade/SDLFontContext.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade/SDLFontContext.cpp')
-rw-r--r--src/crepe/facade/SDLFontContext.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp
new file mode 100644
index 0000000..a4be143
--- /dev/null
+++ b/src/crepe/facade/SDLFontContext.cpp
@@ -0,0 +1,54 @@
+#include <stdexcept>
+
+#include "SDLFontContext.h"
+
+
+using namespace crepe;
+using namespace std;
+
+SDLFontContext::SDLFontContext(){
+
+}
+
+SDLFontContext::~SDLFontContext(){
+
+}
+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) {
+ 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);
+ FcFini();
+ return std::move(make_unique<Asset>(font_file_path));
+}