aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Text.cpp6
-rw-r--r--src/crepe/api/Text.h3
-rw-r--r--src/crepe/facade/FontFacade.cpp48
-rw-r--r--src/example/loadfont.cpp7
4 files changed, 34 insertions, 30 deletions
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
index 58dc6c6..6c632f3 100644
--- a/src/crepe/api/Text.cpp
+++ b/src/crepe/api/Text.cpp
@@ -5,10 +5,8 @@
using namespace crepe;
Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::string & font_family, const Data & data, const std::string & text,
- std::optional<Asset> font)
+ const std::string & font_family, const Data & data, const std::string & text)
: UIObject(id, dimensions, offset),
text(text),
data(data),
- font_family(font_family),
- font(font) {}
+ font_family(font_family){}
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
index 92cca18..c30dc80 100644
--- a/src/crepe/api/Text.h
+++ b/src/crepe/api/Text.h
@@ -51,8 +51,7 @@ public:
* \param font Optional font asset that can be passed or left empty.
*/
Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::string & font_family, const Data & data, const std::string & text = "",
- std::optional<Asset> font = std::nullopt);
+ const std::string & font_family, const Data & data, const std::string & text = "");
//! Label text.
std::string text = "";
diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp
index 7edfeb8..5382f1a 100644
--- a/src/crepe/facade/FontFacade.cpp
+++ b/src/crepe/facade/FontFacade.cpp
@@ -1,51 +1,55 @@
#include <fontconfig/fontconfig.h>
#include <stdexcept>
+#include <memory>
+#include <functional>
+#include <string>
#include "FontFacade.h"
-using namespace crepe;
using namespace std;
+using namespace crepe;
FontFacade::FontFacade() {
if (!FcInit()) {
throw runtime_error("Failed to initialize Fontconfig.");
}
}
+
FontFacade::~FontFacade() { FcFini(); }
-Asset FontFacade::get_font_asset(const string & font_family) {
+Asset FontFacade::get_font_asset(const 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 == NULL) {
+ FcPattern* raw_pattern = FcNameParse(reinterpret_cast<const FcChar8*>(font_family.c_str()));
+ if (!raw_pattern) {
throw runtime_error("Failed to create font pattern.");
}
- // Default configuration
- FcConfig * config = FcConfigGetCurrent();
- if (config == NULL) {
- // FcPatternDestroy(pattern);
+ std::unique_ptr<FcPattern, std::function<void(FcPattern*)>> pattern(
+ raw_pattern,
+ [](FcPattern* p) { FcPatternDestroy(p); }
+ );
+
+ FcConfig* config = FcConfigGetCurrent();
+ if (!config) {
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);
+ FcPattern* raw_matched_pattern = FcFontMatch(config, pattern.get(), &result);
+ if (!raw_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);
+
+ std::unique_ptr<FcPattern, std::function<void(FcPattern*)>> matched_pattern(
+ raw_matched_pattern,
+ [](FcPattern* p) { FcPatternDestroy(p); }
+ );
+
+ FcChar8* file_path = nullptr;
+ if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch || !file_path) {
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);
+ string font_file_path = reinterpret_cast<const char*>(file_path);
return Asset(font_file_path);
}
diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp
index 78b3adb..788fac4 100644
--- a/src/example/loadfont.cpp
+++ b/src/example/loadfont.cpp
@@ -3,6 +3,7 @@
#include <crepe/api/Text.h>
#include <crepe/facade/Font.h>
#include <crepe/facade/SDLContext.h>
+#include <crepe/facade/FontFacade.h>
#include <crepe/manager/Mediator.h>
#include <crepe/manager/ResourceManager.h>
#include <exception>
@@ -14,15 +15,17 @@ int main() {
// SDLFontContext font_facade;
Mediator mediator;
+ FontFacade font_facade{};
SDLContext sdl_context{mediator};
// ComponentManager component_manager{mediator};
ResourceManager resource_manager{mediator};
try {
// Correct way to create a unique pointer for Text
std::unique_ptr<Text> label = std::make_unique<Text>(
- 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text", Asset(""));
+ 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text");
// std::cout << "Path: " << label->font.get_path() << std::endl;
-
+ Asset asset1 = font_facade.get_font_asset("OpenSymbol");
+ std::cout << asset1.get_path() << std::endl;
std::unique_ptr<Text> label2 = std::make_unique<Text>(
1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{});
Asset asset = Asset("test test");