Skip to content

Commit 597eea9

Browse files
committed
fix for fill-opacity and populating css values even if they wont be used.
1 parent 187642e commit 597eea9

2 files changed

Lines changed: 60 additions & 40 deletions

File tree

addons/ofxSvg/src/ofxSvg.cpp

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ std::vector<std::shared_ptr<ofxSvgElement>> ofxSvg::deepCopyVector(const std::ve
8484
if (ptr) {
8585
copy.push_back(clone(ptr));
8686
} else {
87-
ofLogNotice("ofxSvg") << "deepCopyVector :: nullptr";
87+
ofLogVerbose("ofxSvg") << "deepCopyVector :: nullptr";
8888
copy.push_back(std::shared_ptr<ofxSvgElement>()); // Preserve nullptr entries
8989
}
9090
}
@@ -95,7 +95,6 @@ void ofxSvg::deepCopyFrom( const ofxSvg & mom ) {
9595
if( mom.mChildren.size() > 0 ) {
9696
mChildren = deepCopyVector(mom.mChildren);
9797
}
98-
ofLogNotice("ofxSvg") << "deepCopyFrom mom num children: " << mom.mChildren.size() << " my size: " << mChildren.size();
9998
if( mom.mDefElements.size() > 0 ) {
10099
mDefElements = deepCopyVector(mom.mDefElements);
101100
}
@@ -164,15 +163,15 @@ void ofxSvg::moveFrom( ofxSvg&& mom ) {
164163
//--------------------------------------------------------------
165164
ofxSvg::ofxSvg(const ofxSvg & mom) {
166165
clear();
167-
ofLogNotice("ofxSvg") << "ofxSvg(const ofxSvg & mom)";
166+
ofLogVerbose("ofxSvg") << "ofxSvg(const ofxSvg & mom)";
168167
deepCopyFrom(mom);
169168
}
170169

171170
// Copy assignment operator (deep copy)
172171
//--------------------------------------------------------------
173172
ofxSvg& ofxSvg::operator=(const ofxSvg& mom) {
174173
if (this != &mom) {
175-
ofLogNotice("ofxSvg") << "ofxSvg::operator=(const ofxSvg& mom)";
174+
ofLogVerbose("ofxSvg") << "ofxSvg::operator=(const ofxSvg& mom)";
176175
clear();
177176
deepCopyFrom(mom);
178177
}
@@ -182,15 +181,15 @@ ofxSvg& ofxSvg::operator=(const ofxSvg& mom) {
182181
// Move constructor
183182
//--------------------------------------------------------------
184183
ofxSvg::ofxSvg(ofxSvg && mom) {
185-
ofLogNotice("ofxSvg") << "ofxSvg(ofxSvg && mom)";
184+
ofLogVerbose("ofxSvg") << "ofxSvg(ofxSvg && mom)";
186185
clear();
187186
moveFrom(std::move(mom));
188187
}
189188

190189
// Move assignment operator
191190
ofxSvg& ofxSvg::operator=(ofxSvg&& mom) {
192191
if (this != &mom) {
193-
ofLogNotice("ofxSvg") << "ofxSvg::operator=(ofxSvg&& mom)";
192+
ofLogVerbose("ofxSvg") << "ofxSvg::operator=(ofxSvg&& mom)";
194193
clear();
195194
moveFrom(std::move(mom));
196195
}
@@ -1018,7 +1017,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
10181017
'A', 'a' // elliptical arc
10191018
};
10201019
std::string ostring = dattr.getValue();
1021-
// ofLogNotice(moduleName()) << __FUNCTION__ << " dattr: " << ostring;
1020+
// ofLogNotice("ofxSvg") << __FUNCTION__ << " dattr: " << ostring;
10221021

10231022
if( ostring.empty() ) {
10241023
ofLogError("ofxSvg") << __FUNCTION__ << " there is no data in the d string.";
@@ -1097,14 +1096,14 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
10971096
break;
10981097
}
10991098

1100-
// ofLogNotice(moduleName()) << " o : ["<< ostring[index] <<"]";
1099+
ofLogVerbose("ofxSvg") << " o : ["<< ostring[index] <<"]";
11011100

11021101
// up to next valid character //
11031102
std::string currentString;
11041103
bool bFoundValidNextChar = false;
11051104
auto pos = index+1;
11061105
if( pos >= ostring.size() ) {
1107-
ofLogVerbose("svgParser") << "pos is greater than string size: " << pos << " / " << ostring.size();
1106+
ofLogVerbose("ofxSvg") << "pos is greater than string size: " << pos << " / " << ostring.size();
11081107
// break;
11091108
breakMe = true;
11101109
}
@@ -1127,7 +1126,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
11271126

11281127

11291128
if( currentString.empty() ) {
1130-
ofLogVerbose("svgParser") << "currentString is empty: " << cchar;
1129+
ofLogVerbose("ofxSvg") << "currentString is empty: " << cchar;
11311130
// break;
11321131
}
11331132

@@ -1171,7 +1170,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
11711170
}
11721171
npositions = parsePoints(currentString);
11731172
// for( auto& np : npositions ) {
1174-
// ofLogNotice(moduleName()) << cchar << " line to: " << np;
1173+
// ofLogVerbose("ofxSvg") << cchar << " line to: " << np;
11751174
// }
11761175
ctype = ofPath::Command::lineTo;
11771176
} else if( cchar == 'z' || cchar == 'Z' ) {
@@ -1184,7 +1183,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
11841183
ctype = ofPath::Command::bezierTo;
11851184
npositions = parsePoints(currentString);
11861185
// for( auto& np : npositions ) {
1187-
// ofLogNotice(moduleName()) << cchar << " bezier to: " << np;
1186+
// ofLogVerbose("ofxSvg") << cchar << " bezier to: " << np;
11881187
// }
11891188
} else if( cchar == 'Q' || cchar == 'q' || cchar == 'T' || cchar == 't' ) {
11901189
if( cchar == 'q' ) {
@@ -1195,7 +1194,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
11951194
npositions = parsePoints(currentString);
11961195

11971196
// for( auto& np : npositions ) {
1198-
// ofLogNotice(moduleName()) << " Quad bezier to: " << np;
1197+
// ofLogNotice("ofxSvg") << " Quad bezier to: " << np;
11991198
// }
12001199
} else if(cchar == 'a' || cchar == 'A' ) {
12011200
if( cchar == 'a' ) {
@@ -1220,14 +1219,14 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
12201219

12211220
}
12221221
// for( auto& np : npositions ) {
1223-
// ofLogNotice(moduleName()) << " arc parsed positions: " << np;
1222+
// ofLogNotice("ofxSvg") << " arc parsed positions: " << np;
12241223
// }
12251224
}
12261225

12271226
if( ctype.has_value() ) {
12281227

12291228
// for( auto& np : npositions ) {
1230-
// ofLogNotice(moduleName()) << cchar << " position: " << np;
1229+
// ofLogNotice("ofxSvg") << cchar << " position: " << np;
12311230
// }
12321231

12331232
auto prevPos = currentPos;
@@ -1253,15 +1252,15 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
12531252

12541253
} else {
12551254
// if( commandT == ofPath::Command::moveTo ) {
1256-
// ofLogNotice("ofxSvgParser") << "before current pos is altered: move to: " << npositions[0] << " current Pos: " << currentPos << " relative: " << bRelative;
1255+
// ofLogNotice("ofxSvg") << "before current pos is altered: move to: " << npositions[0] << " current Pos: " << currentPos << " relative: " << bRelative;
12571256
// }
12581257
if( npositions.size() > 0 && commandT != ofPath::Command::close ) {
12591258
currentPos = convertToAbsolute(bRelative, currentPos, npositions );
12601259
}
12611260
}
12621261

12631262
// if( npositions.size() > 0 ) {
1264-
// ofLogNotice("ofxSvgParser") << "before current pos is altered: move to: " << npositions[0] << " current Pos: " << currentPos << " relative: " << bRelative;
1263+
// ofLogNotice("ofxSvg") << "before current pos is altered: move to: " << npositions[0] << " current Pos: " << currentPos << " relative: " << bRelative;
12651264
// }
12661265

12671266
if( commandT != ofPath::Command::bezierTo ) {
@@ -1276,6 +1275,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
12761275
} else if( commandT == ofPath::Command::lineTo ) {
12771276
aSvgPath->path.lineTo(currentPos);
12781277
} else if( commandT == ofPath::Command::close ) {
1278+
// ofLogNotice("ofxSvg") << "Closing the path";
12791279
aSvgPath->path.close();
12801280
} else if( commandT == ofPath::Command::bezierTo ) {
12811281

@@ -1471,7 +1471,7 @@ void ofxSvg::_parsePath( ofXml& tnode, std::shared_ptr<ofxSvgPath> aSvgPath ) {
14711471
// mCPoints.insert( mCPoints.end(), npositions.begin(), npositions.end() );
14721472
}
14731473

1474-
// ofLogNotice(moduleName()) << "["<<cchar<<"]: " << currentString;
1474+
// ofLogNotice("ofxSvg") << "["<<cchar<<"]: " << currentString;
14751475

14761476

14771477
justInCase++;
@@ -1485,6 +1485,9 @@ ofxSvgCssClass ofxSvg::_parseStyle( ofXml& anode ) {
14851485
if( mCurrentSvgCss ) {
14861486
// apply first if we have a global style //
14871487
for( auto& tprop : mCurrentSvgCss->properties ) {
1488+
if( tprop.first.empty() ) {
1489+
ofLogNotice("ofxSvg") << "First prop is empty";
1490+
}
14881491
css.addProperty(tprop.first, tprop.second);
14891492
}
14901493
}
@@ -1494,10 +1497,10 @@ ofxSvgCssClass ofxSvg::_parseStyle( ofXml& anode ) {
14941497
if( auto classAttr = anode.getAttribute("class") ) {
14951498
// get a list of classes, is this separated by commas?
14961499
auto classList = ofSplitString(classAttr.getValue(), ",");
1497-
// ofLogNotice("ofx::svg::Parser") << " going to try and parse style classes string: " << classAttr.getValue();
1500+
// ofLogNotice("ofxSvg") << " going to try and parse style classes string: " << classAttr.getValue();
14981501
for( auto& className : classList ) {
14991502
if( mSvgCss.hasClass(className) ) {
1500-
// ofLogNotice("ofx::svg::Parser") << " has class " << className;
1503+
// ofLogNotice("ofxSvg") << " has class " << className;
15011504
// now lets try to apply it to the path
15021505
auto& tCss = mSvgCss.getClass(className);
15031506
for( auto& tprop : tCss.properties ) {
@@ -1509,15 +1512,26 @@ ofxSvgCssClass ofxSvg::_parseStyle( ofXml& anode ) {
15091512

15101513
// locally set on node overrides the class listing
15111514
// are there any properties on the node?
1512-
if( auto fillAttr = anode.getAttribute("fill")) {
1513-
css.addProperty("fill", fillAttr.getValue());
1514-
}
1515-
if( auto strokeAttr = anode.getAttribute("stroke")) {
1516-
css.addProperty("stroke", strokeAttr.getValue());
1517-
}
15181515

1519-
if( auto strokeWidthAttr = anode.getAttribute("stroke-width")) {
1520-
css.addProperty("stroke-width", strokeWidthAttr.getValue());
1516+
// avoid the following
1517+
std::vector<std::string> reservedAtts = {
1518+
"d", "id", "xlink:href", "width", "height", "rx", "ry", "cx", "cy", "r", "style", "font-family",
1519+
"x","y","x1","y1","x2","y2"
1520+
};
1521+
1522+
// lets try to do this a better way
1523+
for( auto& att : anode.getAttributes() ) {
1524+
auto atName = ofToLower(att.getName());
1525+
bool bFileIt = true;
1526+
for( auto& rattName : reservedAtts ) {
1527+
if( atName == rattName ) {
1528+
bFileIt=false;
1529+
break;
1530+
}
1531+
}
1532+
if( bFileIt ) {
1533+
css.addProperty(att.getName(), att.getValue());
1534+
}
15211535
}
15221536

15231537
if( auto ffattr = anode.getAttribute("font-family") ) {
@@ -1526,29 +1540,19 @@ ofxSvgCssClass ofxSvg::_parseStyle( ofXml& anode ) {
15261540
css.addProperty("font-family", tFontFam);
15271541
}
15281542

1529-
if( auto fsattr = anode.getAttribute("font-size") ) {
1530-
css.addProperty("font-size", fsattr.getValue() );
1531-
}
1532-
15331543
// and lastly style
15341544
if( auto styleAttr = anode.getAttribute("style") ) {
15351545
css.addProperties(styleAttr.getValue());
15361546
}
15371547

1538-
// override anything else if set directly on the node
1539-
if( auto disAttr = anode.getAttribute("display") ) {
1540-
css.addProperties(disAttr.getValue());
1541-
}
1542-
15431548
return css;
15441549
}
15451550

15461551
//--------------------------------------------------------------
15471552
void ofxSvg::_applyStyleToElement( ofXml& tnode, std::shared_ptr<ofxSvgElement> aEle ) {
15481553
auto css = _parseStyle(tnode);
1549-
// ofLogNotice("_applyStyleToElement" ) << " " << aEle->name << " -----";
15501554
if( css.hasAndIsNone("display")) {
1551-
// ofLogNotice("parser") << "setting element to invisible: " << aEle->name;
1555+
ofLogVerbose("ofxSvg") << "setting element to invisible: " << aEle->name;
15521556
aEle->setVisible(false);
15531557
}
15541558
}
@@ -1574,6 +1578,21 @@ void ofxSvg::_applyStyleToPath( ofxSvgCssClass& aclass, std::shared_ptr<ofxSvgPa
15741578
aSvgPath->path.setFillColor(ofColor(0));
15751579
}
15761580

1581+
if( aclass.hasProperty("fill-opacity")) {
1582+
if( aclass.isNone("fill-opacity")) {
1583+
aSvgPath->path.setFilled(false);
1584+
} else {
1585+
float val = aclass.getFloatValue("fill-opacity", 1.0f);
1586+
if( val <= 0.0001f ) {
1587+
aSvgPath->path.setFilled(false);
1588+
} else {
1589+
auto pcolor = aSvgPath->path.getFillColor();
1590+
pcolor.a = val;
1591+
aSvgPath->path.setFillColor(pcolor);
1592+
}
1593+
}
1594+
}
1595+
15771596
if( !aclass.isNone("stroke") ) {
15781597
aSvgPath->path.setStrokeColor(aclass.getColor("stroke"));
15791598
}
@@ -1707,7 +1726,7 @@ bool ofxSvg::setTransformFromSvgMatrixString( string aStr, std::shared_ptr<ofxSv
17071726
} else {
17081727
mat = mat * glm::toMat4((const glm::quat&)glm::angleAxis(glm::radians(aele->rotation), glm::vec3(0.f, 0.f, 1.f)));
17091728
}
1710-
// ofLogNotice("svg parser") << "rcenter: " << rcenter.x << ", " << rcenter.y;
1729+
// ofLogNotice("ofxSvg") << "rcenter: " << rcenter.x << ", " << rcenter.y;
17111730
}
17121731
ofLogVerbose("ofxSvg") << __FUNCTION__ << " arotation: " << aele->rotation;
17131732
}

addons/ofxSvg/src/ofxSvgCss.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ ofColor ofxSvgCssClass::sGetColor(const std::string& astr ) {
118118
ofColor tcolor(255);
119119
int hint = ofHexToInt(cstr);
120120
tcolor.setHex(hint);
121+
// ofLogNotice("ofxSvgCssClass") << "color: " << cstr << " ofColor: " << tcolor;
121122
return tcolor;
122123
} else if( !astr.empty() ) {
123124
if( sCommonColors.count(cstr)) {

0 commit comments

Comments
 (0)