slight refactor

This commit is contained in:
jrkb 2023-04-12 10:58:53 +02:00
parent b16046e5b8
commit f6e4506d7f
8 changed files with 203 additions and 167 deletions

1
src/AtlasLayerCombo.cpp Normal file
View file

@ -0,0 +1 @@
// nothing yet

72
src/AtlasLayerCombo.h Normal file
View file

@ -0,0 +1,72 @@
#pragma once
#include "ofMain.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include "Layer.h"
#include "Utils.h"
#include <type_traits>
#include <unordered_map>
/*********
*
* 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> layer) = 0;
virtual const ComboIdentifier & getIdentifier() const = 0;
bool operator==(const AtlasLayerCombo & other) const {
return (this->getIdentifier() == other.getIdentifier());
}
};
}
namespace std {
template <>
struct hash <ofxVariableLab::ComboIdentifier> {
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 <ofxVariableLab::AtlasLayerCombo> {
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;
}
};
}

View file

@ -0,0 +1,18 @@
#include "GPUFontAtlasLayerCombo.h"
namespace ofxVariableLab {
void GPUFontAtlasLayerCombo::setup(const ComboIdentifier & identifier){
}
void GPUFontAtlasLayerCombo::update(){
}
void GPUFontAtlasLayerCombo::careForChild(shared_ptr <Layer> layer){
}
const ComboIdentifier & GPUFontAtlasLayerCombo::getIdentifier() const {
return identifier;
}
const vector <shared_ptr <GPUFontLayer> > & GPUFontAtlasLayerCombo::getLayers(){
return layers;
}
}

View file

@ -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> layer) override;
const ComboIdentifier & getIdentifier() const override;
const vector <shared_ptr <GPUFontLayer> > & getLayers();
shared_ptr <ofxGPUFont::Font> font;
private:
FT_Library library;
vector <shared_ptr <GPUFontLayer> > layers;
shared_ptr <ofShader> gpuFontShader;
ComboIdentifier identifier;
bool isDirty = false;
};
}

View file

@ -7,61 +7,6 @@
namespace ofxVariableLab { 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 <ofxMsdfgen::Atlas>();
msdfShader = make_shared <ofShader>();
#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> layer){
shared_ptr <MsdfLayer> msdfLayer = dynamic_pointer_cast <MsdfLayer>(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(){ void LayerComposition::setup(){
} }
@ -124,10 +69,6 @@ shared_ptr <Layer> LayerComposition::getLayer(const LayerID & layerID){
return layers[layerID]; return layers[layerID];
} }
const ofImage & LayerComposition::getAtlasImage(const ComboIdentifier & identifier){
return atlasLayerCombos[identifier]->getAtlasImage();
}
void LayerComposition::draw() const { void LayerComposition::draw() const {
for(const auto & layer_it : layers){ for(const auto & layer_it : layers){
layer_it.second->draw(); layer_it.second->draw();

View file

@ -1,124 +1,19 @@
#pragma once #pragma once
#include "Utils.h"
#include "ofMain.h" #include "ofMain.h"
#include "ofxMsdfgen.h" #include "AtlasLayerCombo.h"
#include "Layer.h" #include "MsdfAtlasLayerCombo.h"
#include "MsdfLayer.h" #include "MsdfLayer.h"
#include "GPUFontAtlasLayerCombo.h"
#include "GPUFontLayer.h" #include "GPUFontLayer.h"
#include "Utils.h"
#include <type_traits>
#include <unordered_map>
/*********
*
* plan:
* -> GPUFont has one font per layer
* -> use ofShader
* -> single glyph buffer in AtlasLayerCombo
* -> single curve buffer in AtlasLayerCombo
*
*/
namespace ofxVariableLab { 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> 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 <ofxVariableLab::ComboIdentifier> {
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 <ofxVariableLab::AtlasLayerCombo> {
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> layer) override;
const ComboIdentifier & getIdentifier() const override;
const ofImage & getAtlasImage() override;
string getAtlasImagePath() override;
shared_ptr <Layer> getLayer();
shared_ptr <ofxMsdfgen::Atlas> atlas;
private:
vector <shared_ptr <MsdfLayer> > layers;
vector <ofxMsdfgen::GlyphGeometry> glyphGeometries;
shared_ptr <ofShader> 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> layer) override;
const ComboIdentifier & getIdentifier() const override;
const ofImage & getAtlasImage() override;
string getAtlasImagePath() override;
shared_ptr <Layer> getLayer();
shared_ptr <ofxGPUFont::Atlas> atlas;
private:
vector <shared_ptr <GPUFontLayer> > layers;
shared_ptr <ofShader> gpuFontShader;
ComboIdentifier identifier;
bool isDirty = false;
};
class LayerComposition { class LayerComposition {
public: public:
void setup(); void setup();
void update(); void update();
void draw() const; void draw() const;
const ofImage & getAtlasImage(const ComboIdentifier & identifier);
LayerID addLayer(const ComboIdentifier & identifier, LayerID addLayer(const ComboIdentifier & identifier,
const string & text, const string & text,
const std::vector <FontVariation> & variations); const std::vector <FontVariation> & variations);

View file

@ -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 <ofxMsdfgen::Atlas>();
msdfShader = make_shared <ofShader>();
#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> layer){
shared_ptr <MsdfLayer> msdfLayer = dynamic_pointer_cast <MsdfLayer>(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();
}
}

25
src/MsdfAtlasLayerCombo.h Normal file
View file

@ -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> layer) override;
const ComboIdentifier & getIdentifier() const override;
const ofImage & getAtlasImage();
string getAtlasImagePath();
shared_ptr <Layer> getLayer(); // TODO: is this used
shared_ptr <ofxMsdfgen::Atlas> atlas;
private:
vector <shared_ptr <MsdfLayer> > layers;
vector <ofxMsdfgen::GlyphGeometry> glyphGeometries;
shared_ptr <ofShader> msdfShader;
ComboIdentifier identifier;
bool isDirty = false;
};
}