Skip to content

refactor: move ofTtfSettings into ofTrueTypeFont #5681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions addons/ofxGui/src/ofxBaseGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void ofxGuiSetFont(const string & fontPath, int fontsize, bool _bAntiAliased, bo
ofxBaseGui::loadFont(fontPath, fontsize, _bAntiAliased, _bFullCharacterSet, dpi);
}

void ofxGuiSetFont(const ofTtfSettings & fontSettings){
void ofxGuiSetFont(const ofTrueTypeFont::Settings & fontSettings){
ofxBaseGui::loadFont(fontSettings);
}

Expand Down Expand Up @@ -97,7 +97,7 @@ void ofxBaseGui::loadFont(const std::string& filename, int fontsize, bool _bAnti
useTTF = true;
}

void ofxBaseGui::loadFont(const ofTtfSettings & fontSettings){
void ofxBaseGui::loadFont(const ofTrueTypeFont::Settings & fontSettings){
font.load(fontSettings);
fontLoaded = true;
useTTF = true;
Expand Down
2 changes: 1 addition & 1 deletion addons/ofxGui/src/ofxBaseGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ofxBaseGui {
static void setDefaultEventsPriority(ofEventOrder eventsPriority);

static void loadFont(const std::string& filename, int fontsize, bool _bAntiAliased = true, bool _bFullCharacterSet = false, int dpi = 0);
static void loadFont(const ofTtfSettings & fontSettings);
static void loadFont(const ofTrueTypeFont::Settings & fontSettings);
static void setUseTTF(bool bUseTTF);

void registerMouseEvents();
Expand Down
2 changes: 1 addition & 1 deletion addons/ofxGui/src/ofxGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "ofEvents.h"

void ofxGuiSetFont(const string & fontPath,int fontsize, bool _bAntiAliased=true, bool _bFullCharacterSet=true, int dpi=0);
void ofxGuiSetFont(const ofTtfSettings & fontSettings);
void ofxGuiSetFont(const ofTrueTypeFont::Settings & fontSettings);
void ofxGuiSetBitmapFont();

void ofxGuiSetHeaderColor(const ofColor & color);
Expand Down
16 changes: 8 additions & 8 deletions libs/openFrameworks/graphics/ofTrueTypeFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ ofTrueTypeFont::glyph ofTrueTypeFont::loadGlyph(uint32_t utf8) const{

//-----------------------------------------------------------
bool ofTrueTypeFont::load(const std::filesystem::path& filename, int fontSize, bool antialiased, bool fullCharacterSet, bool makeContours, float simplifyAmt, int dpi) {
ofTtfSettings settings(filename,fontSize);
ofTrueTypeFont::Settings settings(filename,fontSize);
settings.antialiased = antialiased;
settings.contours = makeContours;
settings.simplifyAmt = simplifyAmt;
Expand All @@ -777,7 +777,7 @@ bool ofTrueTypeFont::load(const std::filesystem::path& filename, int fontSize, b
return load(settings);
}

bool ofTrueTypeFont::load(const ofTtfSettings & _settings){
bool ofTrueTypeFont::load(const ofTrueTypeFont::Settings & _settings){
#if defined(TARGET_ANDROID)
ofAddListener(ofxAndroidEvents().unloadGL,this,&ofTrueTypeFont::unloadTextures);
ofAddListener(ofxAndroidEvents().reloadGL,this,&ofTrueTypeFont::reloadTextures);
Expand Down Expand Up @@ -1114,7 +1114,7 @@ void ofTrueTypeFont::iterateString(const string & str, float x, float y, bool vF
newLineDirection = -1;
}

int directionX = settings.direction == ofTtfSettings::LeftToRight?1:-1;
int directionX = settings.direction == Settings::Direction::LeftToRight?1:-1;

uint32_t prevC = 0;
for(auto c: ofUTF8Iterator(str)){
Expand All @@ -1124,7 +1124,7 @@ void ofTrueTypeFont::iterateString(const string & str, float x, float y, bool vF
pos.x = x ; //reset X Pos back to zero
prevC = 0;
} else if (c == '\t') {
if ( settings.direction == ofTtfSettings::LeftToRight ){
if ( settings.direction == Settings::Direction::LeftToRight ){
f( c, pos );
pos.x += getGlyphProperties( ' ' ).advance * TAB_WIDTH * letterSpacing * directionX;
} else{
Expand All @@ -1137,7 +1137,7 @@ void ofTrueTypeFont::iterateString(const string & str, float x, float y, bool vF
if(prevC>0){
pos.x += getKerning(c,prevC);// * directionX;
}
if(settings.direction == ofTtfSettings::LeftToRight){
if(settings.direction == Settings::Direction::LeftToRight){
f(c,pos);
pos.x += props.advance * letterSpacing * directionX;
}else{
Expand All @@ -1153,7 +1153,7 @@ void ofTrueTypeFont::iterateString(const string & str, float x, float y, bool vF
}

//-----------------------------------------------------------
void ofTrueTypeFont::setDirection(ofTtfSettings::Direction direction){
void ofTrueTypeFont::setDirection(ofTrueTypeFont::Settings::Direction direction){
settings.direction = direction;
}

Expand Down Expand Up @@ -1288,7 +1288,7 @@ glm::vec2 ofTrueTypeFont::getFirstGlyphPosForTexture(const std::string & str, bo
if(!str.empty()){
try{
auto c = *ofUTF8Iterator(str).begin();
if(settings.direction == ofTtfSettings::LeftToRight){
if(settings.direction == ofTrueTypeFont::Settings::Direction::LeftToRight){
if (c != '\n') {
auto g = loadGlyph(c);
return {-float(g.props.xmin), getLineHeight() + g.props.ymin + getDescenderHeight()};
Expand Down Expand Up @@ -1354,7 +1354,7 @@ ofTexture ofTrueTypeFont::getStringTexture(const std::string& str, bool vflip) c
totalPixels.set(1,0);
size_t i = 0;
for(auto & g: glyphs){
if(settings.direction == ofTtfSettings::LeftToRight){
if(settings.direction == Settings::Direction::LeftToRight){
g.pixels.blendInto(totalPixels, glyphPositions[i].x, glyphPositions[i].y + getLineHeight() + g.props.ymin + getDescenderHeight() );
}else{
g.pixels.blendInto(totalPixels, width-glyphPositions[i].x, glyphPositions[i].y + getLineHeight() + g.props.ymin + getDescenderHeight() );
Expand Down
61 changes: 27 additions & 34 deletions libs/openFrameworks/graphics/ofTrueTypeFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,45 +112,38 @@ class ofAlphabet{
static const std::initializer_list<ofUnicode::range> Cyrillic;
};




class ofTtfSettings{
friend class ofTrueTypeFont;
vector<ofUnicode::range> ranges;
class ofTrueTypeFont{

public:
ofTtfSettings(const std::filesystem::path & name, int size)
:fontName(name)
,fontSize(size){}

std::filesystem::path fontName;
int fontSize;
bool antialiased = true;
bool contours = false;
float simplifyAmt = 0.3f;
int dpi = 0;

enum Direction{
LeftToRight,
RightToLeft
};
Direction direction = LeftToRight;

void addRanges(std::initializer_list<ofUnicode::range> alphabet){
ranges.insert(ranges.end(), alphabet);
}
struct Settings{

void addRange(const ofUnicode::range & range){
ranges.push_back(range);
}
};
enum class Direction : uint32_t {
LeftToRight,
RightToLeft
};

std::filesystem::path fontName;
int fontSize = 0;
bool antialiased = true;
bool contours = false;
float simplifyAmt = 0.3f;
int dpi = 0;
Direction direction = Direction::LeftToRight;
vector<ofUnicode::range> ranges;

class ofTrueTypeFont{
Settings(const std::filesystem::path & name, int size)
:fontName(name)
,fontSize(size){}

public:
void addRanges(std::initializer_list<ofUnicode::range> alphabet){
ranges.insert(ranges.end(), alphabet);
}

void addRange(const ofUnicode::range & range){
ranges.push_back(range);
}
};

/// \brief Construct a default ofTrueTypeFont.
ofTrueTypeFont();
Expand Down Expand Up @@ -199,7 +192,7 @@ class ofTrueTypeFont{
float simplifyAmt=0.3f,
int dpi=0));

bool load(const ofTtfSettings & settings);
bool load(const Settings & settings);

/// \brief Has the font been loaded successfully?
/// \returns true if the font was loaded.
Expand Down Expand Up @@ -355,7 +348,7 @@ class ofTrueTypeFont{
bool isValidGlyph(uint32_t) const;
/// \}

void setDirection(ofTtfSettings::Direction direction);
void setDirection(Settings::Direction direction);
protected:
/// \cond INTERNAL

Expand Down Expand Up @@ -394,7 +387,7 @@ class ofTrueTypeFont{

vector<glyphProps> cps; // properties for each character

ofTtfSettings settings;
Settings settings;
unordered_map<uint32_t,size_t> glyphIndexMap;


Expand Down