From f6e4506d7f1b97677ef79078fe66507788035105 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Wed, 12 Apr 2023 10:58:53 +0200 Subject: [PATCH] slight refactor --- src/AtlasLayerCombo.cpp | 1 + src/AtlasLayerCombo.h | 72 +++++++++++++++++++++ src/GPUFontAtlasLayerCombo.cpp | 18 ++++++ src/GPUFontAtlasLayerCombo.h | 25 ++++++++ src/LayerComposition.cpp | 59 ------------------ src/LayerComposition.h | 111 +-------------------------------- src/MsdfAtlasLayerCombo.cpp | 59 ++++++++++++++++++ src/MsdfAtlasLayerCombo.h | 25 ++++++++ 8 files changed, 203 insertions(+), 167 deletions(-) create mode 100644 src/AtlasLayerCombo.cpp create mode 100644 src/AtlasLayerCombo.h create mode 100644 src/GPUFontAtlasLayerCombo.cpp create mode 100644 src/GPUFontAtlasLayerCombo.h create mode 100644 src/MsdfAtlasLayerCombo.cpp create mode 100644 src/MsdfAtlasLayerCombo.h diff --git a/src/AtlasLayerCombo.cpp b/src/AtlasLayerCombo.cpp new file mode 100644 index 0000000..8f9537e --- /dev/null +++ b/src/AtlasLayerCombo.cpp @@ -0,0 +1 @@ +// nothing yet diff --git a/src/AtlasLayerCombo.h b/src/AtlasLayerCombo.h new file mode 100644 index 0000000..8e37f7a --- /dev/null +++ b/src/AtlasLayerCombo.h @@ -0,0 +1,72 @@ +#pragma once + +#include "ofMain.h" +#include +#include FT_FREETYPE_H +#include "Layer.h" +#include "Utils.h" +#include +#include +/********* + * + * plan: + * -> GPUFont has one font per layer + * -> use ofShader + * -> single glyph buffer in AtlasLayerCombo + * -> single curve buffer in AtlasLayerCombo + * + */ + +namespace ofxVariableLab { + +struct ComboIdentifier { + std::string fontPath = "data/celines-fonts/Version-2-var.ttf"; + Layer::Type type = Layer::MSDFGEN; + + bool operator==(const ComboIdentifier & other) const { + return (this->fontPath == other.fontPath + && this->type == other.type); + } +}; + +class AtlasLayerCombo { + public: + virtual void setup(const ComboIdentifier & identifier) = 0; + virtual void update() = 0; + virtual void careForChild(shared_ptr layer) = 0; + virtual const ComboIdentifier & getIdentifier() const = 0; + + bool operator==(const AtlasLayerCombo & other) const { + return (this->getIdentifier() == other.getIdentifier()); + } +}; +} +namespace std { +template <> +struct hash { + std::size_t operator()(const ofxVariableLab::ComboIdentifier & k) const { + using std::size_t; + using std::hash; + using std::string; + + size_t seed = 0; + ofxVariableLab::hash_combine(seed, k.fontPath); + ofxVariableLab::hash_combine(seed, k.type); + + return seed; + } +}; +template <> +struct hash { + std::size_t operator()(const ofxVariableLab::AtlasLayerCombo & k) const { + using std::size_t; + using std::hash; + using std::string; + + size_t seed = 0; + ofxVariableLab::hash_combine(seed, k.getIdentifier()); + + return seed; + } +}; +} diff --git a/src/GPUFontAtlasLayerCombo.cpp b/src/GPUFontAtlasLayerCombo.cpp new file mode 100644 index 0000000..01306d6 --- /dev/null +++ b/src/GPUFontAtlasLayerCombo.cpp @@ -0,0 +1,18 @@ +#include "GPUFontAtlasLayerCombo.h" + +namespace ofxVariableLab { + +void GPUFontAtlasLayerCombo::setup(const ComboIdentifier & identifier){ +} +void GPUFontAtlasLayerCombo::update(){ +} +void GPUFontAtlasLayerCombo::careForChild(shared_ptr layer){ +} +const ComboIdentifier & GPUFontAtlasLayerCombo::getIdentifier() const { + return identifier; +} +const vector > & GPUFontAtlasLayerCombo::getLayers(){ + return layers; +} + +} diff --git a/src/GPUFontAtlasLayerCombo.h b/src/GPUFontAtlasLayerCombo.h new file mode 100644 index 0000000..3bd2dce --- /dev/null +++ b/src/GPUFontAtlasLayerCombo.h @@ -0,0 +1,25 @@ +#pragma once + +#include "ofMain.h" +#include "AtlasLayerCombo.h" +#include "GPUFontLayer.h" + +namespace ofxVariableLab { +class GPUFontAtlasLayerCombo : public AtlasLayerCombo { + public: + void setup(const ComboIdentifier & identifier) override; + void update() override; + void careForChild(shared_ptr layer) override; + const ComboIdentifier & getIdentifier() const override; + const vector > & getLayers(); + shared_ptr font; + + private: + FT_Library library; + vector > layers; + shared_ptr gpuFontShader; + ComboIdentifier identifier; + bool isDirty = false; +}; + +} diff --git a/src/LayerComposition.cpp b/src/LayerComposition.cpp index 8c6c821..6fc3f07 100644 --- a/src/LayerComposition.cpp +++ b/src/LayerComposition.cpp @@ -7,61 +7,6 @@ namespace ofxVariableLab { -void MsdfAtlasLayerCombo::setup(const ComboIdentifier & layerIdentifier){ - this->identifier = layerIdentifier; - ofxMsdfgen::AtlasSettings atlasSettings; - //settings.characters = "ABCDEFGHIJKL"; - atlasSettings.scale = 64; - atlasSettings.minimumScale = 64; - atlasSettings.characters = ""; - atlasSettings.maxInterpolationStepSize = 50.0; - - atlas = make_shared (); - - msdfShader = make_shared (); - #ifdef TARGET_EMSCRIPTEN - msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader"); - #else - msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader"); - #endif - - atlas->setup(this->identifier.fontPath, atlasSettings); -} - -void MsdfAtlasLayerCombo::update(){ - if(isDirty){ - atlas->generate(); - isDirty = false; - } -} - -void MsdfAtlasLayerCombo::careForChild(shared_ptr layer){ - shared_ptr msdfLayer = dynamic_pointer_cast (layer); - msdfLayer->setAtlas(this->atlas); - msdfLayer->setShader(msdfShader); - auto & as = atlas->settings; - for(const char c : layer->getProps().text){ - if(as.characters.find(c) == std::string::npos){ - // not found - as.characters += c; - isDirty = true; - } - } - layers.push_back(msdfLayer); -} - -const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const { - return identifier; -} - -const ofImage & MsdfAtlasLayerCombo::getAtlasImage(){ - return atlas->getAtlasImage(); -} - -string MsdfAtlasLayerCombo::getAtlasImagePath(){ - return atlas->getAtlasPath(); -} - void LayerComposition::setup(){ } @@ -124,10 +69,6 @@ shared_ptr LayerComposition::getLayer(const LayerID & layerID){ return layers[layerID]; } -const ofImage & LayerComposition::getAtlasImage(const ComboIdentifier & identifier){ - return atlasLayerCombos[identifier]->getAtlasImage(); -} - void LayerComposition::draw() const { for(const auto & layer_it : layers){ layer_it.second->draw(); diff --git a/src/LayerComposition.h b/src/LayerComposition.h index e98f046..6c7a489 100644 --- a/src/LayerComposition.h +++ b/src/LayerComposition.h @@ -1,124 +1,19 @@ #pragma once -#include "Utils.h" #include "ofMain.h" -#include "ofxMsdfgen.h" -#include "Layer.h" +#include "AtlasLayerCombo.h" +#include "MsdfAtlasLayerCombo.h" #include "MsdfLayer.h" +#include "GPUFontAtlasLayerCombo.h" #include "GPUFontLayer.h" -#include "Utils.h" -#include -#include -/********* - * - * plan: - * -> GPUFont has one font per layer - * -> use ofShader - * -> single glyph buffer in AtlasLayerCombo - * -> single curve buffer in AtlasLayerCombo - * - */ namespace ofxVariableLab { -struct ComboIdentifier { - std::string fontPath = "data/celines-fonts/Version-2-var.ttf"; - Layer::Type type = Layer::MSDFGEN; - - bool operator==(const ComboIdentifier & other) const { - return (this->fontPath == other.fontPath - && this->type == other.type); - } -}; - -class AtlasLayerCombo { - public: - virtual void setup(const ComboIdentifier & identifier) = 0; - virtual void update() = 0; - virtual void careForChild(shared_ptr layer) = 0; - virtual const ComboIdentifier & getIdentifier() const = 0; - virtual const ofImage & getAtlasImage() = 0; - virtual string getAtlasImagePath() = 0; - - bool operator==(const AtlasLayerCombo & other) const { - return (this->getIdentifier() == other.getIdentifier()); - } -}; -} -namespace std { -template <> -struct hash { - std::size_t operator()(const ofxVariableLab::ComboIdentifier & k) const { - using std::size_t; - using std::hash; - using std::string; - - size_t seed = 0; - ofxVariableLab::hash_combine(seed, k.fontPath); - ofxVariableLab::hash_combine(seed, k.type); - - return seed; - } -}; -template <> -struct hash { - std::size_t operator()(const ofxVariableLab::AtlasLayerCombo & k) const { - using std::size_t; - using std::hash; - using std::string; - - size_t seed = 0; - ofxVariableLab::hash_combine(seed, k.getIdentifier()); - - return seed; - } -}; -} -namespace ofxVariableLab { - -class MsdfAtlasLayerCombo : public AtlasLayerCombo { - public: - void setup(const ComboIdentifier & identifier) override; - void update() override; - void careForChild(shared_ptr layer) override; - const ComboIdentifier & getIdentifier() const override; - const ofImage & getAtlasImage() override; - string getAtlasImagePath() override; - shared_ptr getLayer(); - shared_ptr atlas; - - private: - vector > layers; - vector glyphGeometries; - shared_ptr msdfShader; - ComboIdentifier identifier; - bool isDirty = false; -}; - -class GPUFontAtlasLayerCombo : public AtlasLayerCombo { - public: - void setup(const ComboIdentifier & identifier) override; - void update() override; - void careForChild(shared_ptr layer) override; - const ComboIdentifier & getIdentifier() const override; - const ofImage & getAtlasImage() override; - string getAtlasImagePath() override; - shared_ptr getLayer(); - shared_ptr atlas; - - private: - vector > layers; - shared_ptr gpuFontShader; - ComboIdentifier identifier; - bool isDirty = false; -}; - class LayerComposition { public: void setup(); void update(); void draw() const; - const ofImage & getAtlasImage(const ComboIdentifier & identifier); LayerID addLayer(const ComboIdentifier & identifier, const string & text, const std::vector & variations); diff --git a/src/MsdfAtlasLayerCombo.cpp b/src/MsdfAtlasLayerCombo.cpp new file mode 100644 index 0000000..7cc3a15 --- /dev/null +++ b/src/MsdfAtlasLayerCombo.cpp @@ -0,0 +1,59 @@ +#include "MsdfAtlasLayerCombo.h" + +namespace ofxVariableLab { + +void MsdfAtlasLayerCombo::setup(const ComboIdentifier & layerIdentifier){ + this->identifier = layerIdentifier; + ofxMsdfgen::AtlasSettings atlasSettings; + //settings.characters = "ABCDEFGHIJKL"; + atlasSettings.scale = 64; + atlasSettings.minimumScale = 64; + atlasSettings.characters = ""; + atlasSettings.maxInterpolationStepSize = 50.0; + + atlas = make_shared (); + + msdfShader = make_shared (); + #ifdef TARGET_EMSCRIPTEN + msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader"); + #else + msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader"); + #endif + + atlas->setup(this->identifier.fontPath, atlasSettings); +} + +void MsdfAtlasLayerCombo::update(){ + if(isDirty){ + atlas->generate(); + isDirty = false; + } +} + +void MsdfAtlasLayerCombo::careForChild(shared_ptr layer){ + shared_ptr msdfLayer = dynamic_pointer_cast (layer); + msdfLayer->setAtlas(this->atlas); + msdfLayer->setShader(msdfShader); + auto & as = atlas->settings; + for(const char c : layer->getProps().text){ + if(as.characters.find(c) == std::string::npos){ + // not found + as.characters += c; + isDirty = true; + } + } + layers.push_back(msdfLayer); +} + +const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const { + return identifier; +} + +const ofImage & MsdfAtlasLayerCombo::getAtlasImage(){ + return atlas->getAtlasImage(); +} + +string MsdfAtlasLayerCombo::getAtlasImagePath(){ + return atlas->getAtlasPath(); +} +} diff --git a/src/MsdfAtlasLayerCombo.h b/src/MsdfAtlasLayerCombo.h new file mode 100644 index 0000000..a07d1c8 --- /dev/null +++ b/src/MsdfAtlasLayerCombo.h @@ -0,0 +1,25 @@ +#pragma once + +#include "AtlasLayerCombo.h" +#include "MsdfLayer.h" + +namespace ofxVariableLab { +class MsdfAtlasLayerCombo : public AtlasLayerCombo { + public: + void setup(const ComboIdentifier & identifier) override; + void update() override; + void careForChild(shared_ptr layer) override; + const ComboIdentifier & getIdentifier() const override; + const ofImage & getAtlasImage(); + string getAtlasImagePath(); + shared_ptr getLayer(); // TODO: is this used + shared_ptr atlas; + + private: + vector > layers; + vector glyphGeometries; + shared_ptr msdfShader; + ComboIdentifier identifier; + bool isDirty = false; +}; +}