Skip to content

Commit c4eefd0

Browse files
authored
group fix bounding box. Add load image function. Hack for path bounding box to get outlines using setPathWidth(); (#8500)
1 parent c93980b commit c4eefd0

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

addons/ofxSvg/src/ofxSvgElements.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ void ofxSvgImage::load() {
238238
}
239239
}
240240

241+
//--------------------------------------------------------------
242+
bool ofxSvgImage::load( const of::filesystem::path& afilePath ) {
243+
filepath = afilePath;
244+
bTryLoad = false;
245+
load();
246+
return isLoaded();
247+
}
248+
241249
//--------------------------------------------------------------
242250
void ofxSvgImage::customDraw() {
243251
if( !bTryLoad ) {

addons/ofxSvg/src/ofxSvgElements.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ class ofxSvgPath : public ofxSvgElement {
213213
virtual ofRectangle getBoundingBox() override {
214214
if(mBBoxNeedsRecalc) {
215215
mBBoxNeedsRecalc = false;
216+
217+
// Note: Hack to force outlines by setting a stroke if there is none.
218+
float prevStrokeWidth = path.getStrokeWidth();
219+
if( prevStrokeWidth < 1.0f ) {
220+
path.setStrokeWidth(1.f);
221+
}
222+
216223
// ofRectangle brect;
217224
const auto& outlines = path.getOutline();
218225
if( outlines.size() > 0 ) {
@@ -232,6 +239,8 @@ class ofxSvgPath : public ofxSvgElement {
232239
}
233240
}
234241
}
242+
243+
path.setStrokeWidth(prevStrokeWidth);
235244
}
236245
return mBounds;
237246
};
@@ -392,6 +401,7 @@ class ofxSvgImage : public ofxSvgElement {
392401
};
393402

394403
void load();
404+
bool load( const of::filesystem::path& afilePath );
395405
bool isLoaded() {
396406
return (img.isAllocated() && img.getWidth() > 0 && img.getHeight() > 0);
397407
}

addons/ofxSvg/src/ofxSvgGroup.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ class ofxSvgGroup : public ofxSvgElement {
8484
bool bFirstRect = true;
8585
auto allEs = getAllElements(false);
8686
for( auto& ele : allEs ) {
87-
auto erect = ele->getBoundingBox();
87+
// Grab the global bounding box so that we can put it into the group space later
88+
// If we just grab the element local bounding box, it doesn't account for position, rotation, etc.
89+
auto erect = ele->getGlobalBoundingBox();
8890
if( erect.width > 0 && erect.height > 0 ) {
8991
if(bFirstRect) {
9092
rrect = erect;
@@ -94,6 +96,20 @@ class ofxSvgGroup : public ofxSvgElement {
9496
bFirstRect = false;
9597
}
9698
}
99+
100+
if( !bFirstRect ) {
101+
// Now lets put the rectangle into the group transform space.
102+
auto gmat = getGlobalTransformMatrix();
103+
auto gmatInv = glm::inverse(gmat);
104+
105+
std::vector<glm::vec3> rverts = { rrect.getTopLeft(), rrect.getTopRight(), rrect.getBottomRight(), rrect.getBottomLeft()};
106+
107+
for( auto& rv : rverts ) {
108+
rv = glm::vec3(gmatInv * glm::vec4(rv, 1.f));
109+
}
110+
return _calculateExtents( rverts );
111+
}
112+
97113
return rrect;
98114
};
99115

0 commit comments

Comments
 (0)