slight refactor

This commit is contained in:
jrkb 2023-04-11 17:45:20 +02:00
parent e1557285de
commit 470b831c61
9 changed files with 45 additions and 41 deletions

View file

@ -148,8 +148,8 @@ void ofApp::update(){
float animationSpeed = ofMap(sin(ofGetElapsedTimef() * 0.01), -1, 1, 0.0, 2); float animationSpeed = ofMap(sin(ofGetElapsedTimef() * 0.01), -1, 1, 0.0, 2);
float threshold = 5.0; float threshold = 5.0;
for(int i = 0; i < fontVariationAxes.size(); i++){ for(int i = 0; i < fontVariationAxes.size(); i++){
Font::FontVariationAxis & axis = fontVariationAxes[i]; ofxGPUFont::Font::FontVariationAxis & axis = fontVariationAxes[i];
Font::FontVariationAxisParameters & axisParams = fontVariationAxesParameters[i]; ofxGPUFont::Font::FontVariationAxisParameters & axisParams = fontVariationAxesParameters[i];
double newValue = ofMap(sin(ofGetElapsedTimef() * animationSpeed), double newValue = ofMap(sin(ofGetElapsedTimef() * animationSpeed),
-1, 1, -1, 1,
axisParams.minValue, axisParams.maxValue); axisParams.minValue, axisParams.maxValue);

View file

@ -2,39 +2,7 @@
#include "ofMain.h" #include "ofMain.h"
#include "ofxGPUFont.h" #include "ofxGPUFont.h"
#include "gpufont/font.hpp"
#include "gpufont/shader_catalog.hpp"
static std::unique_ptr <Font> loadFont(FT_Library & library, const std::string & filename, float worldSize = 1.0f, bool hinting = false){
std::string error;
FT_Face face = Font::loadFace(library, filename, error);
if(error != ""){
std::cerr << "[font] failed to load " << filename << ": " << error << std::endl;
return std::unique_ptr <Font>{};
}
return std::make_unique <Font>(face, worldSize, hinting);
}
static void tryUpdateMainFont(FT_Library & library,
const std::string & filename,
const string & mainText,
unique_ptr <Font> & mainFont,
Font::BoundingBox & bb){
{
auto font = loadFont(library, filename, 0.05f);
if(!font){
return;
}
font->dilation = 0.1f;
font->prepareGlyphsForText(mainText);
mainFont = std::move(font);
bb = mainFont->measure(0, 0, mainText);
}
}
class ofApp : public ofBaseApp { class ofApp : public ofBaseApp {
public: public:
@ -71,7 +39,7 @@ class ofApp : public ofBaseApp {
void exit(); void exit();
unique_ptr <Font> font; unique_ptr <ofxGPUFont::Font> font;
FT_Library library; FT_Library library;
// Empty VAO used when the vertex shader has no input and only uses gl_VertexID, // Empty VAO used when the vertex shader has no input and only uses gl_VertexID,
@ -82,7 +50,7 @@ class ofApp : public ofBaseApp {
//std::shared_ptr <ShaderCatalog::Entry> backgroundShader; //std::shared_ptr <ShaderCatalog::Entry> backgroundShader;
std::shared_ptr <ShaderCatalog::Entry> fontShader; std::shared_ptr <ShaderCatalog::Entry> fontShader;
Font::BoundingBox bb; ofxGPUFont::Font::BoundingBox bb;
Transform transform; Transform transform;
@ -152,7 +120,7 @@ class ofApp : public ofBaseApp {
"data/celines-fonts/Version-3-var.ttf" "data/celines-fonts/Version-3-var.ttf"
}; };
std::vector <Font::FontVariationAxisParameters> fontVariationAxesParameters; std::vector <ofxGPUFont::Font::FontVariationAxisParameters> fontVariationAxesParameters;
std::vector <Font::FontVariationAxis> fontVariationAxes; std::vector <ofxGPUFont::Font::FontVariationAxis> fontVariationAxes;
}; };

View file

@ -1,3 +1,4 @@
#pragma once
// Note: See "main.cpp" for headers. // Note: See "main.cpp" for headers.
// This file was extracted to improve the organization of the code, // This file was extracted to improve the organization of the code,
// but it is still compiled in the "main.cpp" translation unit, // but it is still compiled in the "main.cpp" translation unit,
@ -17,6 +18,8 @@
#define F16DOT16_TO_DOUBLE(x) (1 / 65536. * double(x)) #define F16DOT16_TO_DOUBLE(x) (1 / 65536. * double(x))
#define DOUBLE_TO_F16DOT16(x) FT_Fixed(65536. * x) #define DOUBLE_TO_F16DOT16(x) FT_Fixed(65536. * x)
namespace ofxGPUFont {
inline int32_t calculateUpperPowerOfTwo(int32_t v){ inline int32_t calculateUpperPowerOfTwo(int32_t v){
v--; v--;
v |= v >> 1; v |= v >> 1;
@ -32,7 +35,6 @@ inline bool isPowerOfTwo(int i){
return (i & (i - 1)) == 0; return (i & (i - 1)) == 0;
} }
class Font { class Font {
struct Glyph { struct Glyph {
FT_UInt index; FT_UInt index;
@ -318,7 +320,6 @@ class Font {
} }
void prepareGlyphsForText(const std::string & text, bool forceChange = false){ void prepareGlyphsForText(const std::string & text, bool forceChange = false){
// TODO: do not duplicate glyphs
bool changed = false; bool changed = false;
if(forceChange){ if(forceChange){
@ -916,3 +917,35 @@ class Font {
// anti-aliasing. Value is relative to emSize. // anti-aliasing. Value is relative to emSize.
float dilation = 0; float dilation = 0;
}; };
static std::unique_ptr <Font> loadFont(FT_Library & library, const std::string & filename, float worldSize = 1.0f, bool hinting = false){
std::string error;
FT_Face face = Font::loadFace(library, filename, error);
if(error != ""){
std::cerr << "[font] failed to load " << filename << ": " << error << std::endl;
return std::unique_ptr <Font>{};
}
return std::make_unique <Font>(face, worldSize, hinting);
}
static void tryUpdateMainFont(FT_Library & library,
const std::string & filename,
const string & mainText,
unique_ptr <Font> & mainFont,
Font::BoundingBox & bb){
{
auto font = loadFont(library, filename, 0.05f);
if(!font){
return;
}
font->dilation = 0.1f;
font->prepareGlyphsForText(mainText);
mainFont = std::move(font);
bb = mainFont->measure(0, 0, mainText);
}
}
}

View file

@ -1 +1,4 @@
#pragma one #pragma once
#include "gpufont/font.hpp"
#include "gpufont/shader_catalog.hpp"