Skip to content

Commit 7882ce7

Browse files
committed
use ofNode as base
1 parent 2670307 commit 7882ce7

File tree

10 files changed

+334
-449
lines changed

10 files changed

+334
-449
lines changed

addons/ofxSvg/src/ofxSvg.cpp

Lines changed: 128 additions & 235 deletions
Large diffs are not rendered by default.

addons/ofxSvg/src/ofxSvg.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,20 @@ class ofxSvg : public ofxSvgGroup {
200200

201201
std::shared_ptr<ofxSvgImage> addImage( const of::filesystem::path& apath, const ofTexture& atex );
202202
std::shared_ptr<ofxSvgImage> addImage( const glm::vec2& apos, const of::filesystem::path& apath, const ofTexture& atex );
203-
std::shared_ptr<ofxSvgImage> addEmbeddedImage( const ofPixels& apixels );
203+
std::shared_ptr<ofxSvgImage> addEmbeddedImage( const glm::vec2& apos, const ofPixels& apixels );
204204

205205
// adapted from ofGLProgrammableRenderer for some sort of conformity
206-
void pushMatrix();
207-
bool popMatrix();
208-
void translate(float x, float y);
209-
void translate(const glm::vec2 & p);
210-
void scale(float xAmnt, float yAmnt);
211-
void rotateRadians(float radians);
212-
void rotateDegrees(float adegrees);
213-
void multMatrix(const glm::mat4 & m);
214-
void loadMatrix(const glm::mat4 & m);
215-
void loadIdentityMatrix();
206+
// this will be handled by the ofNode functionality
207+
// void pushMatrix();
208+
// bool popMatrix();
209+
// void translate(float x, float y);
210+
// void translate(const glm::vec2 & p);
211+
// void scale(float xAmnt, float yAmnt);
212+
// void rotateRadians(float radians);
213+
// void rotateDegrees(float adegrees);
214+
// void multMatrix(const glm::mat4 & m);
215+
// void loadMatrix(const glm::mat4 & m);
216+
// void loadIdentityMatrix();
216217

217218
virtual void drawDebug();
218219

@@ -242,11 +243,11 @@ class ofxSvg : public ofxSvgGroup {
242243
void _setNodeParentGroupStack( std::shared_ptr<ofxSvgElement> aele );
243244

244245
ofxSvgGroup* _getPushedGroup();
245-
bool _hasPushedMatrix();
246-
void _applyModelMatrixToElement( std::shared_ptr<ofxSvgElement> aele, glm::vec2 aDefaultPos );
247-
glm::vec2 _getPos2d(const glm::mat4& amat);
248-
glm::vec2 _getScale2d(const glm::mat4& amat);
249-
float _getZRotationRadians( const glm::mat4& amat );
246+
// bool _hasPushedMatrix();
247+
// void _applyModelMatrixToElement( std::shared_ptr<ofxSvgElement> aele, glm::vec2 aDefaultPos );
248+
// glm::vec2 _getPos2d(const glm::mat4& amat);
249+
// glm::vec2 _getScale2d(const glm::mat4& amat);
250+
// float _getZRotationRadians( const glm::mat4& amat );
250251

251252
ofxSvgCssClass& _addCssClassFromPath( std::shared_ptr<ofxSvgPath> aSvgPath );
252253
void _addCssClassFromPath( std::shared_ptr<ofxSvgPath> aSvgPath, ofXml& anode );

addons/ofxSvg/src/ofxSvgCss.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ std::string ofxSvgCssClass::getFontFamily( std::string aDefaultFontFamily ) {
308308
return getValue("font-family", aDefaultFontFamily);
309309
}
310310

311+
//--------------------------------------------------
312+
bool ofxSvgCssClass::setFontBold( bool ab ) {
313+
return addProperty("font-weight", ab ? "bold" : "regular" );
314+
}
315+
311316
//--------------------------------------------------
312317
bool ofxSvgCssClass::isFontBold() {
313318
bool bold = false;
@@ -317,6 +322,11 @@ bool ofxSvgCssClass::isFontBold() {
317322
return bold;
318323
}
319324

325+
//--------------------------------------------------
326+
bool ofxSvgCssClass::setFontItalic(bool ab) {
327+
return addProperty("font-style", ab ? "italic" : "regular" );
328+
}
329+
320330
//--------------------------------------------------
321331
bool ofxSvgCssClass::isFontItalic() {
322332
bool italic = false;

addons/ofxSvg/src/ofxSvgCss.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class ofxSvgCssClass {
5757
bool setFontFamily( std::string aFontFamily );
5858
std::string getFontFamily( std::string aDefaultFontFamily );
5959

60+
bool setFontBold( bool ab );
6061
bool isFontBold();
62+
63+
bool setFontItalic( bool ab );
6164
bool isFontItalic();
6265

6366
bool hasProperty( const std::string& akey );

addons/ofxSvg/src/ofxSvgElements.cpp

Lines changed: 62 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,6 @@ string ofxSvgElement::toString( int nlevel ) {
125125
return tstr;
126126
}
127127

128-
////--------------------------------------------------------------
129-
//glm::mat4 ofxSvgElement::getTransformMatrix() {
130-
// glm::mat4 rmat = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, pos.y, 0.0f));
131-
// if( rotation != 0.0f ) {
132-
//// glm::quat rq = glm::angleAxis(ofDegToRad(rotation), glm::vec3(0.f, 0.f, 1.0f ));
133-
//// rmat = rmat * glm::toMat4((const glm::quat&)rq);
134-
// rmat = glm::rotate(rmat, glm::radians(rotation), glm::vec3(0.f, 0.f, 1.f));
135-
// }
136-
// if( scale.x != 1.0f || scale.y != 1.0f ) {
137-
// rmat = glm::scale(rmat, glm::vec3(scale.x, scale.y, 1.0f));
138-
// }
139-
// return rmat;
140-
//};
141-
//
142-
////--------------------------------------------------------------
143-
//ofNode ofxSvgElement::getNodeTransform() {
144-
// ofNode tnode;// = ofxSvgBase::getNodeTransform();
145-
// tnode.setPosition(pos.x, pos.y, 0.0f);
146-
// if( rotation != 0.0f ) {
147-
// glm::quat rq = glm::angleAxis(ofDegToRad(rotation), glm::vec3(0.f, 0.f, 1.0f ));
148-
// tnode.setOrientation(rq);
149-
// }
150-
// tnode.setScale(1.0f);
151-
// if( scale.x != 1.0f || scale.y != 1.0f ) {
152-
// tnode.setScale(scale.x, scale.y, 1.f );
153-
// }
154-
// return tnode;
155-
//}
156-
157128
//--------------------------------------------------------------
158129
void ofxSvgPath::applyStyle(ofxSvgCssClass& aclass) {
159130
if( aclass.hasProperty("fill")) {
@@ -221,25 +192,13 @@ void ofxSvgPath::applyStyle(ofxSvgCssClass& aclass) {
221192

222193
//--------------------------------------------------------------
223194
void ofxSvgPath::customDraw() {
224-
// ofPushMatrix(); {
225-
// transformGL(); {
226195

227196
bool bHasOffset = mOffsetPos.x != 0.f || mOffsetPos.y != 0.f;
228197
if(bHasOffset) {
229198
ofPushMatrix();
230199
ofTranslate(mOffsetPos.x, mOffsetPos.y);
231200
}
232201

233-
ofSetColor( ofColor::orange );
234-
// ofDrawCircle(0, 0, 15);
235-
236-
// ofTranslate(pos.x, pos.y);
237-
238-
ofSetColor( ofColor::green );
239-
// ofDrawCircle(0, 0, 10);
240-
241-
// if( rotation != 0.0 ) ofRotateZDeg( rotation );
242-
// ofScale( scale.x, scale.y );
243202
if(isVisible()) {
244203
ofColor fillColor = getFillColor();
245204
ofColor strokeColor = getStrokeColor();
@@ -266,17 +225,9 @@ void ofxSvgPath::customDraw() {
266225
if(bHasOffset) {
267226
ofPopMatrix();
268227
}
269-
270-
// } restoreTransformGL();
271-
// } ofPopMatrix();
272228
}
273229

274230
#pragma mark - Image
275-
//--------------------------------------------------------------
276-
//ofRectangle ofxSvgImage::getRectangle() {
277-
// return ofRectangle(pos.x, pos.y, getWidth(), getHeight());
278-
//}
279-
280231
//--------------------------------------------------------------
281232
void ofxSvgImage::load() {
282233
if( !bTryLoad ) {
@@ -295,15 +246,10 @@ void ofxSvgImage::customDraw() {
295246

296247
if( isVisible() ) {
297248
if( img.isAllocated() ) {
298-
// ofPushMatrix(); {
299-
// transformGL(); {
300-
// ofTranslate( pos.x, pos.y );
301-
// if( rotation != 0.0 ) ofRotateZDeg( rotation );
302-
// ofScale( scale.x, scale.y );
303-
if(bUseShapeColor) ofSetColor( getColor() );
304-
img.draw( 0, 0 );
305-
// } ofPopMatrix();
306-
// } restoreTransformGL();
249+
if(bUseShapeColor) {
250+
ofSetColor( getColor() );
251+
}
252+
img.draw( 0, 0 );
307253
}
308254
}
309255
}
@@ -318,6 +264,7 @@ void ofxSvgImage::customDraw() {
318264

319265

320266
#pragma mark - Text
267+
//--------------------------------------------------------------
321268
std::vector<std::string> ofxSvgText::splitBySpanTags(const std::string& input) {
322269
std::vector<std::string> result;
323270
std::regex span_regex(R"(<[^>]+>[\s\S]*?</[^>]+>)"); // Match tags with content including newlines
@@ -349,11 +296,8 @@ std::vector<std::string> ofxSvgText::splitBySpanTags(const std::string& input) {
349296
return result;
350297
}
351298

352-
//struct SpanData {
353-
// std::string style;
354-
// std::string content;
355-
//};
356299

300+
//--------------------------------------------------------------
357301
ofxSvgText::SpanData ofxSvgText::extractSpanData(const std::string& spanTag) {
358302
SpanData data;
359303

@@ -375,6 +319,7 @@ ofxSvgText::SpanData ofxSvgText::extractSpanData(const std::string& spanTag) {
375319
return data;
376320
}
377321

322+
//--------------------------------------------------------------
378323
bool ofxSvgText::endsWithLineEnding(const std::string& astr) {
379324
if (astr.size() >= 2 && astr.substr(astr.size() - 2) == "\r\n") {
380325
// Windows line ending
@@ -386,6 +331,7 @@ bool ofxSvgText::endsWithLineEnding(const std::string& astr) {
386331
return false;
387332
}
388333

334+
//--------------------------------------------------------------
389335
std::vector<std::string> ofxSvgText::splitWordsAndLineEndings(const std::string& input) {
390336
std::vector<std::string> result;
391337

@@ -401,9 +347,18 @@ std::vector<std::string> ofxSvgText::splitWordsAndLineEndings(const std::string&
401347
return result;
402348
}
403349

404-
// build the text spans from a string and not from xml //
350+
// build the text spans from a string and not from xml / svg file structure //
405351
//--------------------------------------------------------------
406352
void ofxSvgText::setText( const std::string& astring, std::string aFontFamily, int aFontSize, float aMaxWidth ) {
353+
ofxSvgCssClass css;
354+
css.addProperty("font-family", aFontFamily);
355+
css.addProperty("font-size", aFontSize);
356+
css.addProperty("color", getColor() );
357+
setText( astring, css, aMaxWidth );
358+
}
359+
360+
//--------------------------------------------------------------
361+
void ofxSvgText::setText( const std::string& astring, const ofxSvgCssClass& aSvgCssClass, float aMaxWidth ) {
407362
meshes.clear();
408363
textSpans.clear();
409364

@@ -422,10 +377,15 @@ void ofxSvgText::setText( const std::string& astring, std::string aFontFamily, i
422377
bool bLastCharIsSpace = false;
423378

424379
// ofLogNotice("ofxSvgText") << "spanString: |" <<spanString<<"|";
425-
ofxSvgCssClass css;
426-
css.addProperty("font-family", aFontFamily);
427-
css.addProperty("font-size", aFontSize);
428-
css.addProperty("color", getColor() );
380+
ofxSvgCssClass css = mSvgCssClass;
381+
if( !css.hasProperty("color")) {
382+
// default to black
383+
css.setColor(ofColor(0));
384+
}
385+
css.setClassProperties( aSvgCssClass );
386+
// css.addProperty("font-family", aFontFamily);
387+
// css.addProperty("font-size", aFontSize);
388+
// css.addProperty("color", getColor() );
429389
if (spanString.find("<span") != std::string::npos) {
430390
SpanData data = extractSpanData(spanString);
431391
// std::cout << "Found <span> tag.\n";
@@ -645,6 +605,10 @@ void ofxSvgText::create() {
645605
vector< std::shared_ptr<TextSpan> > tspans = textSpans;
646606

647607
for( auto& tspan : textSpans ) {
608+
609+
// lets add any missing properties for the text spans from the ofxSvgText class
610+
tspan->mSvgCssClass.addMissingClassProperties(mSvgCssClass);
611+
648612
// auto tkey = ofxSvgFontBook::getFontKey(tspan->getFontFamily(), tspan->isBold(), tspan->isItalic() );
649613
if( !ofxSvgFontBook::hasFont(tspan->getFontFamily(), tspan->getFontSize(), tspan->isBold(), tspan->isItalic() )) {
650614
ofLogVerbose("ofxSvgText") << "Trying to load font " << tspan->getFontFamily() << " bold: " << tspan->isBold() << " italic: " << tspan->isItalic() << " | " << ofGetFrameNum();
@@ -742,65 +706,56 @@ void ofxSvgText::create() {
742706
// ofRectangle()
743707
// tempSpan->lineHeight = tfont.getStringBoundingBox("M", 0, 0).height;
744708
tempSpan->lineHeight = tfont.getLineHeight();
745-
// tempSpan.rect.x = tempSpan.rect.x - ogPos.x;
746-
// tempSpan.rect.y = tempSpan.rect.x - ogPos.x;
747-
//tempSpan.rect.y -= tempSpan.lineHeight;
748709
}
749710
}
750711
}
751712

752713
//--------------------------------------------------------------
753714
void ofxSvgText::customDraw() {
754715
if( !isVisible() ) return;
755-
716+
756717
if(bUseShapeColor) {
757-
ofSetColor( 255, 255, 255, 255.f * alpha );
758-
}
718+
ofSetColor( 255, 255, 255, 255.f * alpha );
719+
}
759720
std::map< string, std::map<int, ofMesh> >::iterator mainIt;
760721

761722
if(areTextSpansDirty()) {
762723
// sets textSpan->mBTextDirty to false;
763724
create();
764725
}
765726

766-
// transformGL(); {
767-
ofTexture* tex = NULL;
768-
for( mainIt = meshes.begin(); mainIt != meshes.end(); ++mainIt ) {
769-
string fontKey = mainIt->first;
770-
std::map< int, ofMesh >::iterator mIt;
771-
for( mIt = meshes[ fontKey ].begin(); mIt != meshes[ fontKey ].end(); ++mIt ) {
772-
int fontSize = mIt->first;
773-
// let's check to make sure that the texture is there, so that we can bind it //
774-
bool bHasTexture = false;
775-
// if( fonts.count( fontKey ) ) {
776-
if( ofxSvgFontBook::hasBookFont(fontKey)) {
777-
auto& fbook = ofxSvgFontBook::getBookFont(fontKey);
778-
if( fbook.textures.count( fontSize ) ) {
779-
bHasTexture = true;
780-
tex = &fbook.textures[ fontSize ];
781-
}
727+
ofTexture* tex = NULL;
728+
for( mainIt = meshes.begin(); mainIt != meshes.end(); ++mainIt ) {
729+
string fontKey = mainIt->first;
730+
std::map< int, ofMesh >::iterator mIt;
731+
for( mIt = meshes[ fontKey ].begin(); mIt != meshes[ fontKey ].end(); ++mIt ) {
732+
int fontSize = mIt->first;
733+
// let's check to make sure that the texture is there, so that we can bind it //
734+
bool bHasTexture = false;
735+
// if( fonts.count( fontKey ) ) {
736+
if( ofxSvgFontBook::hasBookFont(fontKey)) {
737+
auto& fbook = ofxSvgFontBook::getBookFont(fontKey);
738+
if( fbook.textures.count( fontSize ) ) {
739+
bHasTexture = true;
740+
tex = &fbook.textures[ fontSize ];
782741
}
783-
784-
if( bHasTexture ) tex->bind();
785-
ofMesh& tMeshMesh = mIt->second;
786-
if( bUseShapeColor ) {
787-
vector< ofFloatColor >& tcolors = tMeshMesh.getColors();
788-
for( auto& tc : tcolors ) {
789-
// if( bOverrideColor ) {
790-
// tc = _overrideColor;
791-
// } else {
792-
tc.a = alpha;
793-
// }
794-
}
795-
} else {
796-
tMeshMesh.disableColors();
742+
}
743+
744+
if( bHasTexture ) tex->bind();
745+
ofMesh& tMeshMesh = mIt->second;
746+
if( bUseShapeColor ) {
747+
vector< ofFloatColor >& tcolors = tMeshMesh.getColors();
748+
for( auto& tc : tcolors ) {
749+
tc.a = alpha;
797750
}
798-
tMeshMesh.draw();
799-
if( bHasTexture ) tex->unbind();
800-
tMeshMesh.enableColors();
751+
} else {
752+
tMeshMesh.disableColors();
801753
}
754+
tMeshMesh.draw();
755+
if( bHasTexture ) tex->unbind();
756+
tMeshMesh.enableColors();
802757
}
803-
// } restoreTransformGL();
758+
}
804759
}
805760

806761
////--------------------------------------------------------------
@@ -878,15 +833,6 @@ ofTrueTypeFont& ofxSvgText::getFont() {
878833
return ofxSvgFontBook::defaultFont;
879834
}
880835

881-
////--------------------------------------------------------------
882-
//ofColor ofxSvgText::getColor() {
883-
// if( textSpans.size() > 0 ) {
884-
// return textSpans[0]->getColor();
885-
// }
886-
// ofLogWarning("ofxSvgText") << __FUNCTION__ << " : no font detected from text spans, returning black.";
887-
// return ofColor(0,255);
888-
//}
889-
890836
// get the bounding rect for all of the text spans in this text element
891837
// should be called after create //
892838
//--------------------------------------------------------------

0 commit comments

Comments
 (0)