aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/CMakeLists.txt2
-rw-r--r--src/crepe/facade/Font.cpp24
-rw-r--r--src/crepe/facade/Font.h33
-rw-r--r--src/crepe/facade/SDLFontContext.cpp54
-rw-r--r--src/crepe/facade/SDLFontContext.h18
5 files changed, 131 insertions, 0 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index 0598e16..f4d71ff 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -4,6 +4,7 @@ target_sources(crepe PUBLIC
SoundContext.cpp
SDLContext.cpp
DB.cpp
+ SDLFontContext.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -12,5 +13,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
SoundContext.h
SDLContext.h
DB.h
+ SDLFontContext.h
)
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
new file mode 100644
index 0000000..2ad176a
--- /dev/null
+++ b/src/crepe/facade/Font.cpp
@@ -0,0 +1,24 @@
+#include "font.h"
+#include "../api/Config.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);
+
+ 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
new file mode 100644
index 0000000..e34ac63
--- /dev/null
+++ b/src/crepe/facade/Font.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <memory>
+#include <SDL2/SDL_ttf.h>
+
+#include "../api/Asset.h"
+#include "../api/Config.h"
+
+namespace crepe {
+
+/**
+ * \brief Font resource facade
+ *
+ * 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
+ */
+ Font(const Asset & src, Mediator & mediator);
+
+
+ ~Font() = default
+ const TTF_Font& get_font() const;
+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;
+};
+
+} // namespace crepe
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));
+}
diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h
new file mode 100644
index 0000000..cd91383
--- /dev/null
+++ b/src/crepe/facade/SDLFontContext.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <memory>
+#include <SDL2/SDL_ttf.h>
+#include <fontconfig/fontconfig.h>
+
+#include "../api/Asset.h"
+
+namespace crepe {
+ class SDLFontContext{
+ public:
+ SDLFontContext();
+ ~SDLFontContext();
+ std::unique_ptr<Asset> get_font_asset(const std::string & font_family);
+ private:
+ };
+
+}