diff --git a/frame/CMakeLists.txt b/frame/CMakeLists.txt index 268e92f..c7da05c 100644 --- a/frame/CMakeLists.txt +++ b/frame/CMakeLists.txt @@ -2,13 +2,16 @@ set(target frame) configure_file ( - "${PROJECT_SOURCE_DIR}/frame/src/Config.h.in" - "${PROJECT_SOURCE_DIR}/frame/src/Config.h" + "${PROJECT_SOURCE_DIR}/frame/src/BuildConfig.h.in" + "${PROJECT_SOURCE_DIR}/frame/src/BuildConfig.h" ) set(src src/main.cpp + src/Config.hpp + src/Config.cpp + src/Size.hpp src/Vector.hpp src/Rect.hpp @@ -69,6 +72,7 @@ endif (BUILD_VIRTUAL_DISPLAY) add_executable(${target} ${src}) target_link_libraries(${target} PRIVATE fmt::fmt + nlohmann_json::nlohmann_json ) if (BUILD_EPD) diff --git a/frame/src/BuildConfig.h b/frame/src/BuildConfig.h new file mode 100644 index 0000000..eaf6409 --- /dev/null +++ b/frame/src/BuildConfig.h @@ -0,0 +1,2 @@ +#define BUILD_VIRTUAL_DISPLAY +/* #undef BUILD_EPD */ diff --git a/frame/src/Config.h.in b/frame/src/BuildConfig.h.in similarity index 100% rename from frame/src/Config.h.in rename to frame/src/BuildConfig.h.in diff --git a/frame/src/Config.cpp b/frame/src/Config.cpp new file mode 100644 index 0000000..6c52bdc --- /dev/null +++ b/frame/src/Config.cpp @@ -0,0 +1,51 @@ +#include "Config.hpp" + +#include +#include +#include + +namespace fs = std::filesystem; + +#define PROPERTY(str, member) str.member = j.value(#member, str.member) + +Config Config::Load() +{ + fs::path const config_path{"config.json"}; + + if(fs::is_regular_file(config_path)) + { + std::fstream file(config_path.string(), std::ios::in); + + return json::parse(file); + } + + fmt::print("[INFO] Creating config file\n"); + Config config; + std::fstream file(config_path.string(), std::ios::out); + file << json(config).dump(); + + return config; +} + +// == to json == +void to_json(json& j, Config const& c) +{ + j = {{"font", c.font}}; +} + +void to_json(json& j, FontConfig const& fc) +{ + j = {{"base_path", fc.base_path}, {"fonts", fc.fonts}}; +} + +// == from json == +void from_json(json const& j, Config& c) +{ + PROPERTY(c, font); +} + +void from_json(json const& j, FontConfig& fc) +{ + PROPERTY(fc, base_path); + PROPERTY(fc, fonts); +} \ No newline at end of file diff --git a/frame/src/Config.hpp b/frame/src/Config.hpp new file mode 100644 index 0000000..2c03616 --- /dev/null +++ b/frame/src/Config.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include + +using json = nlohmann::json; + +struct FontConfig +{ + std::string base_path = "fonts/"; + std::unordered_map fonts{ + {"Fira Code", "FiraCode.json"}}; +}; + +struct Config +{ + FontConfig font{}; + + static Config Load(); +}; + +void to_json(json& j, Config const& c); +void to_json(json& j, FontConfig const& fc); + +void from_json(json const& j, Config& c); +void from_json(json const& j, FontConfig& fc); \ No newline at end of file diff --git a/frame/src/display/Display.cpp b/frame/src/display/Display.cpp index 4b84bbe..a0d57a6 100644 --- a/frame/src/display/Display.cpp +++ b/frame/src/display/Display.cpp @@ -1,6 +1,6 @@ #include "Display.hpp" -#include "../Config.h" +#include "../BuildConfig.h" #ifdef BUILD_EPD # include "EPD_7in5_V2.hpp" diff --git a/frame/src/font/FontRegistry.cpp b/frame/src/font/FontRegistry.cpp index 5023af3..0dd2191 100644 --- a/frame/src/font/FontRegistry.cpp +++ b/frame/src/font/FontRegistry.cpp @@ -1,7 +1,10 @@ #include "FontRegistry.hpp" +#include #include +namespace fs = std::filesystem; + namespace frame::font { std::shared_ptr GetFont(std::string_view name) @@ -14,11 +17,26 @@ namespace frame::font return Service::get()->Load(name); } + FontRegistry& LocateRegistry() + { + return *Service::get(); + } + FontRegistry::FontRegistry() : IService("FontRegistry") { } + void FontRegistry::LoadFromConfig(FontConfig const& config) + { + fs::path base{config.base_path}; + + for(auto const& [name, file] : config.fonts) + { + Load(name, (base / file).string()); + } + } + bool FontRegistry::Load(std::string_view name) { if(fonts.find(std::string{name}) != fonts.end()) @@ -43,6 +61,29 @@ namespace frame::font return false; } + bool FontRegistry::Load(std::string_view name, std::string_view path) + { + if(fonts.find(std::string{name}) != fonts.end()) + { + fmt::print("Font {} allready loaded!\n", name); + return true; + } + + fmt::print("Loading font \"{}\": ", name); + auto ptr = Font::LoadFromFile(path); + + if(ptr == nullptr) + { + fmt::print("Error not found!\n"); + } else + { + fonts[std::string{name}] = ptr; + fmt::print("OK\n"); + return true; + } + return false; + } + std::shared_ptr FontRegistry::Get(std::string_view name) { auto it = fonts.find({std::string{name}}); diff --git a/frame/src/font/FontRegistry.hpp b/frame/src/font/FontRegistry.hpp index 913867d..0b291d6 100644 --- a/frame/src/font/FontRegistry.hpp +++ b/frame/src/font/FontRegistry.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../Config.hpp" #include "../ServiceLocator.hpp" #include "Font.hpp" @@ -8,9 +9,11 @@ namespace frame::font { + class FontRegistry; std::shared_ptr GetFont(std::string_view name); bool LoadFont(std::string_view name); + FontRegistry& LocateRegistry(); class FontRegistry : public IService { @@ -19,7 +22,9 @@ namespace frame::font public: FontRegistry(); + void LoadFromConfig(FontConfig const& config); bool Load(std::string_view name); + bool Load(std::string_view name, std::string_view path); std::shared_ptr Get(std::string_view name); }; diff --git a/frame/src/main.cpp b/frame/src/main.cpp index 974a402..5b5d431 100644 --- a/frame/src/main.cpp +++ b/frame/src/main.cpp @@ -6,6 +6,7 @@ using namespace std::chrono_literals; +#include "Config.hpp" #include "ScreenManager.hpp" #include "display/Display.hpp" #include "font/FontRegistry.hpp" @@ -16,16 +17,22 @@ using namespace std::chrono_literals; int main() { + // == == Config == == + auto config = Config::Load(); + + // == == Display == == auto display = frame::display::Create(); if(!display) { + fmt::print("Error: Not display driver!"); return 0; } display->Init(); - frame::font::LoadFont("Fira Code"); + // frame::font::LoadFont("Fira Code"); + frame::font::LocateRegistry().LoadFromConfig(config.font); frame::ScreenManager screen(std::move(display));