Skip to content

ofTrueTypeFont mesh insert at once #8432

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions libs/openFrameworks/3d/of3dPrimitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const glm::vec4& of3dPrimitive::getTexCoords() const{

//----------------------------------------------------------
vector<ofIndexType> of3dPrimitive::getIndices( int startIndex, int endIndex ) const {

vector<ofIndexType> indices;
indices.assign( getMesh().getIndices().begin()+startIndex, getMesh().getIndices().begin()+endIndex );
return indices;
Expand Down
68 changes: 36 additions & 32 deletions libs/openFrameworks/graphics/ofTrueTypeFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void initWindows(){
char value_data_char[2048];
string fontsDir = ofGetEnv("windir");
fontsDir += "\\Fonts\\";

for (DWORD i = 0; i < value_count; ++i)
{
DWORD name_len = 2048;
Expand Down Expand Up @@ -689,7 +689,7 @@ ofTrueTypeFont::glyph ofTrueTypeFont::loadGlyph(uint32_t utf8) const{

//-----------------------------------------------------------
bool ofTrueTypeFont::load(const of::filesystem::path & filename, int fontSize, bool antialiased, bool fullCharacterSet, bool makeContours, float simplifyAmt, int dpi) {

ofTrueTypeFontSettings settings(filename,fontSize);
settings.antialiased = antialiased;
settings.contours = makeContours;
Expand Down Expand Up @@ -987,10 +987,10 @@ void ofTrueTypeFont::drawChar(uint32_t c, float x, float y, bool vFlipped) const

auto props = getGlyphProperties(c);

float xmin = props.xmin+x;
float ymin = props.ymin;
float xmax = props.xmax+x;
float ymax = props.ymax;
float xmin = props.xmin+x;
float ymin = props.ymin;
float xmax = props.xmax+x;
float ymax = props.ymax;

if(!vFlipped){
ymin *= -1.0;
Expand All @@ -1000,26 +1000,30 @@ void ofTrueTypeFont::drawChar(uint32_t c, float x, float y, bool vFlipped) const
ymin += y;
ymax += y;

ofIndexType firstIndex = stringQuads.getVertices().size();

stringQuads.addVertex(glm::vec3(xmin,ymin,0.f));
stringQuads.addVertex(glm::vec3(xmax,ymin,0.f));
stringQuads.addVertex(glm::vec3(xmax,ymax,0.f));
stringQuads.addVertex(glm::vec3(xmin,ymax,0.f));

stringQuads.addTexCoord(glm::vec2(props.t1,props.v1));
stringQuads.addTexCoord(glm::vec2(props.t2,props.v1));
stringQuads.addTexCoord(glm::vec2(props.t2,props.v2));
stringQuads.addTexCoord(glm::vec2(props.t1,props.v2));

stringQuads.addIndex(firstIndex);
stringQuads.addIndex(firstIndex+1);
stringQuads.addIndex(firstIndex+2);
stringQuads.addIndex(firstIndex+2);
stringQuads.addIndex(firstIndex+3);
stringQuads.addIndex(firstIndex);


ofIndexType firstIndex { static_cast<ofIndexType>(stringQuads.getVertices().size()) };

stringQuads.addVertices({
{ xmin, ymin, 0.f },
{ xmax, ymin, 0.f },
{ xmax, ymax, 0.f },
{ xmin, ymax, 0.f },
});

stringQuads.addTexCoords({
{ props.t1, props.v1 },
{ props.t2, props.v1 },
{ props.t2, props.v2 },
{ props.t1, props.v2 }
});

stringQuads.addIndices({
firstIndex,
static_cast<ofIndexType>(firstIndex + 1),
static_cast<ofIndexType>(firstIndex + 2),
static_cast<ofIndexType>(firstIndex + 2),
static_cast<ofIndexType>(firstIndex + 3),
firstIndex
});
}

//-----------------------------------------------------------
Expand Down Expand Up @@ -1188,11 +1192,11 @@ ofRectangle ofTrueTypeFont::getStringBoundingBox(const string& c, float x, float
// Calculate bounding box by iterating over glyph properties
// Meaning of props can be deduced from illustration at top of:
// https://www.freetype.org/freetype2/docs/tutorial/step2.html
//
//
// We deliberately not generate a mesh and iterate over its
// vertices, as this would not correctly return spacing for
// blank characters.

float w = 0;
iterateString( c, x, y, vflip, [&]( uint32_t c, glm::vec2 pos ){
auto props = getGlyphProperties( c );
Expand Down Expand Up @@ -1300,22 +1304,22 @@ ofTexture ofTrueTypeFont::getStringTexture(const string& str, bool vflip) const{
try{
if (c != '\n') {
auto g = loadGlyph(c);

if (c == '\t'){
auto temp = loadGlyph(' ');
glyphs.push_back(temp);
}else{
glyphs.push_back(g);
}

int x = pos.x + g.props.xmin;
int y = pos.y;
glyphPositions.emplace_back(x, y);

if(c == '\t')lineWidth += g.props.advance + getGlyphProperties(' ').advance * spaceSize * TAB_WIDTH;
else if(c == ' ')lineWidth += g.props.advance + getGlyphProperties(' ').advance * spaceSize;
else if(isValidGlyph(c))lineWidth += g.props.advance + getGlyphProperties(' ').advance * (letterSpacing - 1.f);

width = max(width, lineWidth);
y += g.props.ymax;
height = max(height, y + getLineHeight());
Expand Down
Loading