Skip to content

Commit 8743f7f

Browse files
authored
ofTrueTypeFont mesh insert at once (#8432)
1 parent 9d272ad commit 8743f7f

2 files changed

Lines changed: 37 additions & 32 deletions

File tree

libs/openFrameworks/3d/of3dPrimitives.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const glm::vec4& of3dPrimitive::getTexCoords() const{
102102

103103
//----------------------------------------------------------
104104
vector<ofIndexType> of3dPrimitive::getIndices( int startIndex, int endIndex ) const {
105+
105106
vector<ofIndexType> indices;
106107
indices.assign( getMesh().getIndices().begin()+startIndex, getMesh().getIndices().begin()+endIndex );
107108
return indices;

libs/openFrameworks/graphics/ofTrueTypeFont.cpp

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void initWindows(){
298298
char value_data_char[2048];
299299
string fontsDir = ofGetEnv("windir");
300300
fontsDir += "\\Fonts\\";
301-
301+
302302
for (DWORD i = 0; i < value_count; ++i)
303303
{
304304
DWORD name_len = 2048;
@@ -689,7 +689,7 @@ ofTrueTypeFont::glyph ofTrueTypeFont::loadGlyph(uint32_t utf8) const{
689689

690690
//-----------------------------------------------------------
691691
bool ofTrueTypeFont::load(const of::filesystem::path & filename, int fontSize, bool antialiased, bool fullCharacterSet, bool makeContours, float simplifyAmt, int dpi) {
692-
692+
693693
ofTrueTypeFontSettings settings(filename,fontSize);
694694
settings.antialiased = antialiased;
695695
settings.contours = makeContours;
@@ -987,10 +987,10 @@ void ofTrueTypeFont::drawChar(uint32_t c, float x, float y, bool vFlipped) const
987987

988988
auto props = getGlyphProperties(c);
989989

990-
float xmin = props.xmin+x;
991-
float ymin = props.ymin;
992-
float xmax = props.xmax+x;
993-
float ymax = props.ymax;
990+
float xmin = props.xmin+x;
991+
float ymin = props.ymin;
992+
float xmax = props.xmax+x;
993+
float ymax = props.ymax;
994994

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

1003-
ofIndexType firstIndex = stringQuads.getVertices().size();
1004-
1005-
stringQuads.addVertex(glm::vec3(xmin,ymin,0.f));
1006-
stringQuads.addVertex(glm::vec3(xmax,ymin,0.f));
1007-
stringQuads.addVertex(glm::vec3(xmax,ymax,0.f));
1008-
stringQuads.addVertex(glm::vec3(xmin,ymax,0.f));
1009-
1010-
stringQuads.addTexCoord(glm::vec2(props.t1,props.v1));
1011-
stringQuads.addTexCoord(glm::vec2(props.t2,props.v1));
1012-
stringQuads.addTexCoord(glm::vec2(props.t2,props.v2));
1013-
stringQuads.addTexCoord(glm::vec2(props.t1,props.v2));
1014-
1015-
stringQuads.addIndex(firstIndex);
1016-
stringQuads.addIndex(firstIndex+1);
1017-
stringQuads.addIndex(firstIndex+2);
1018-
stringQuads.addIndex(firstIndex+2);
1019-
stringQuads.addIndex(firstIndex+3);
1020-
stringQuads.addIndex(firstIndex);
1021-
1022-
1003+
ofIndexType firstIndex { static_cast<ofIndexType>(stringQuads.getVertices().size()) };
1004+
1005+
stringQuads.addVertices({
1006+
{ xmin, ymin, 0.f },
1007+
{ xmax, ymin, 0.f },
1008+
{ xmax, ymax, 0.f },
1009+
{ xmin, ymax, 0.f },
1010+
});
1011+
1012+
stringQuads.addTexCoords({
1013+
{ props.t1, props.v1 },
1014+
{ props.t2, props.v1 },
1015+
{ props.t2, props.v2 },
1016+
{ props.t1, props.v2 }
1017+
});
1018+
1019+
stringQuads.addIndices({
1020+
firstIndex,
1021+
static_cast<ofIndexType>(firstIndex + 1),
1022+
static_cast<ofIndexType>(firstIndex + 2),
1023+
static_cast<ofIndexType>(firstIndex + 2),
1024+
static_cast<ofIndexType>(firstIndex + 3),
1025+
firstIndex
1026+
});
10231027
}
10241028

10251029
//-----------------------------------------------------------
@@ -1188,11 +1192,11 @@ ofRectangle ofTrueTypeFont::getStringBoundingBox(const string& c, float x, float
11881192
// Calculate bounding box by iterating over glyph properties
11891193
// Meaning of props can be deduced from illustration at top of:
11901194
// https://www.freetype.org/freetype2/docs/tutorial/step2.html
1191-
//
1195+
//
11921196
// We deliberately not generate a mesh and iterate over its
11931197
// vertices, as this would not correctly return spacing for
11941198
// blank characters.
1195-
1199+
11961200
float w = 0;
11971201
iterateString( c, x, y, vflip, [&]( uint32_t c, glm::vec2 pos ){
11981202
auto props = getGlyphProperties( c );
@@ -1300,22 +1304,22 @@ ofTexture ofTrueTypeFont::getStringTexture(const string& str, bool vflip) const{
13001304
try{
13011305
if (c != '\n') {
13021306
auto g = loadGlyph(c);
1303-
1307+
13041308
if (c == '\t'){
13051309
auto temp = loadGlyph(' ');
13061310
glyphs.push_back(temp);
13071311
}else{
13081312
glyphs.push_back(g);
13091313
}
1310-
1314+
13111315
int x = pos.x + g.props.xmin;
13121316
int y = pos.y;
13131317
glyphPositions.emplace_back(x, y);
1314-
1318+
13151319
if(c == '\t')lineWidth += g.props.advance + getGlyphProperties(' ').advance * spaceSize * TAB_WIDTH;
13161320
else if(c == ' ')lineWidth += g.props.advance + getGlyphProperties(' ').advance * spaceSize;
13171321
else if(isValidGlyph(c))lineWidth += g.props.advance + getGlyphProperties(' ').advance * (letterSpacing - 1.f);
1318-
1322+
13191323
width = max(width, lineWidth);
13201324
y += g.props.ymax;
13211325
height = max(height, y + getLineHeight());

0 commit comments

Comments
 (0)