Masterwork From Distant Lands
ActivePublic

Authored by dkazakov on Oct 7 2016, 9:44 AM.
diff --git a/libs/flake/tests/TestSvgParser.cpp b/libs/flake/tests/TestSvgParser.cpp
index 7eec035..ea02372 100644
--- a/libs/flake/tests/TestSvgParser.cpp
+++ b/libs/flake/tests/TestSvgParser.cpp
@@ -482,4 +482,240 @@ void TestSvgParser::testParsePreserveAspectRatio()
}
}
+#if 1
+
+#include <boost/config/warning_disable.hpp>
+#include <boost/spirit/include/qi.hpp>
+#include <boost/spirit/include/phoenix_core.hpp>
+#include <boost/spirit/include/phoenix_operator.hpp>
+#include <boost/spirit/include/phoenix_object.hpp>
+#include <boost/fusion/include/adapt_struct.hpp>
+#include <boost/fusion/include/io.hpp>
+
+#include <boost/spirit/include/phoenix_fusion.hpp>
+#include <boost/spirit/include/phoenix_stl.hpp>
+
+namespace client
+{
+namespace qi = boost::spirit::qi;
+namespace ascii = boost::spirit::ascii;
+
+///////////////////////////////////////////////////////////////////////////
+// Our employee struct
+///////////////////////////////////////////////////////////////////////////
+//[tutorial_employee_struct
+
+struct matrix
+{
+ qreal a = 0;
+ qreal b = 0;
+ qreal c = 0;
+ qreal d = 0;
+ qreal e = 0;
+ qreal f = 0;
+};
+
+struct translate
+{
+ qreal tx = 0.0;
+ qreal ty = 0.0;
+};
+
+struct scale
+{
+ qreal sx = 0;
+ qreal sy = 0;
+};
+
+struct rotate
+{
+ qreal angle = 0;
+ qreal cx = 0;
+ qreal cy = 0;
+};
+
+struct skewX
+{
+ qreal angle = 0;
+};
+
+struct skewY
+{
+ qreal angle = 0;
+};
+
+struct transform_unit
+{
+ transform_unit() {}
+ transform_unit(const matrix &m) {}
+ transform_unit(const translate &t) {}
+ transform_unit(const scale &sc) {}
+ transform_unit(const rotate &r) {}
+ transform_unit(const skewX &sx) {}
+ transform_unit(const skewY &sy) {}
+ QTransform transform;
+};
+//]
+}
+
+// We need to tell fusion about our transform_unit struct
+// to make it a first-class fusion citizen. This has to
+// be in global scope.
+
+BOOST_FUSION_ADAPT_STRUCT(
+ client::matrix,
+ (qreal, a)
+ (qreal, b)
+ (qreal, c)
+ (qreal, d)
+ (qreal, e)
+ (qreal, f)
+ )
+
+BOOST_FUSION_ADAPT_STRUCT(
+ client::translate,
+ (qreal, tx)
+ (qreal, ty)
+ )
+
+BOOST_FUSION_ADAPT_STRUCT(
+ client::scale,
+ (qreal, sx)
+ (qreal, sy)
+ )
+
+BOOST_FUSION_ADAPT_STRUCT(
+ client::rotate,
+ (qreal, angle)
+ (qreal, cx)
+ (qreal, cy)
+ )
+
+BOOST_FUSION_ADAPT_STRUCT(
+ client::skewX,
+ (qreal, angle)
+ )
+
+BOOST_FUSION_ADAPT_STRUCT(
+ client::skewY,
+ (qreal, angle)
+ )
+
+namespace client
+{
+ template <typename Iterator>
+ struct transform_unit_parser : qi::grammar<Iterator, std::vector<transform_unit>(), ascii::space_type>
+ {
+ transform_unit_parser() : transform_unit_parser::base_type(start)
+ {
+ namespace phoenix = boost::phoenix;
+ using qi::lit;
+ using qi::double_;
+ using ascii::char_;
+ using qi::cntrl;
+ using phoenix::push_back;
+
+
+ comma %= -char_(',');
+
+ matrix_rule %=
+ lit("matrix")
+ >> '('
+ >> double_ >> comma
+ >> double_ >> comma
+ >> double_ >> comma
+ >> double_ >> comma
+ >> double_ >> comma
+ >> double_ >> comma
+ >> ')';
+
+ translate_rule %=
+ lit("translate")
+ >> '(' >> double_ >> comma >> -double_ >> ')';
+
+ scale_rule %=
+ lit("scale")
+ >> '(' >> double_ >> comma >> -double_ >> ')';
+
+ rotate_rule %=
+ lit("rotate")
+ >> '('
+ >> double_ >> comma
+ >> -(double_ >> comma >> double_)
+ >> ')';
+
+ skewX_rule %= lit("skewX") >> '(' >> double_ >> ')';
+ skewY_rule %= lit("skewY") >> '(' >> double_ >> ')';
+
+ start %=
+ (matrix_rule | translate_rule | scale_rule |
+ rotate_rule | skewX_rule | skewY_rule) %
+ (cntrl | comma);
+ }
+
+ qi::rule<Iterator, std::vector<transform_unit>(), ascii::space_type> start;
+ qi::rule<Iterator, translate(), ascii::space_type> translate_rule;
+ qi::rule<Iterator, matrix(), ascii::space_type> matrix_rule;
+ qi::rule<Iterator, scale(), ascii::space_type> scale_rule;
+ qi::rule<Iterator, rotate(), ascii::space_type> rotate_rule;
+ qi::rule<Iterator, skewX(), ascii::space_type> skewX_rule;
+ qi::rule<Iterator, skewY(), ascii::space_type> skewY_rule;
+ qi::rule<Iterator> comma;
+ };
+}
+
+void TestSvgParser::testParseTransform()
+{
+ using boost::spirit::ascii::space;
+ typedef std::string::const_iterator iterator_type;
+ typedef client::transform_unit_parser<iterator_type> transform_unit_parser;
+
+ transform_unit_parser g; // Our grammar
+ std::string str("translate(-111.0, 33) matrix (1 1 0 0 1, 3), translate(1)"
+ "scale(0.5) rotate(10) rotate(10, 3 3) skewX(1) skewY(2)");
+
+ std::vector<client::transform_unit> emp;
+ std::string::const_iterator iter = str.begin();
+ std::string::const_iterator end = str.end();
+ bool r = phrase_parse(iter, end, g, space, emp);
+
+ if (r && iter == end) {
+ qDebug() << ppVar(emp.size());
+ } else {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------\n";
+ }
+}
+
+#else
+void TestSvgParser::testParseTransform()
+{
+ QString str(" translate(-111.0, 33) translate(-111.0, 33) matrix (1 1 0 0 1, 3), translate(1)"
+ "scale(0.5) rotate(10) rotate(10, 3 3) skewX(1) skewY(2)");
+
+ QRegularExpression base("\\s*((matrix|translate|scale|rotate|skewX|skewY)\\s*\\(([-\\d\\.\\s,]+?)\\))[\\s,]*");
+
+ int startIndex = 0;
+ bool stringValid = false;
+ while (1) {
+ auto match = base.match(str, startIndex, QRegularExpression::NormalMatch, QRegularExpression::AnchoredMatchOption);
+ if (!match.hasMatch()) {
+ break;
+ }
+
+ qDebug() << ppVar(match.capturedTexts());
+
+ startIndex = match.capturedEnd(0);
+ if (startIndex == str.length()) {
+ qDebug() << "END" << ppVar(startIndex);
+ stringValid = true;
+ break;
+ }
+ }
+
+ qDebug() << ppVar(stringValid);
+}
+#endif
+
QTEST_GUILESS_MAIN(TestSvgParser)
diff --git a/libs/flake/tests/TestSvgParser.h b/libs/flake/tests/TestSvgParser.h
index c2eacb7..d1b9b4f 100644
--- a/libs/flake/tests/TestSvgParser.h
+++ b/libs/flake/tests/TestSvgParser.h
@@ -40,6 +40,7 @@ private Q_SLOTS:
void testScalingViewportResolution();
void testScalingViewportPercentInternal();
void testParsePreserveAspectRatio();
+ void testParseTransform();
};
dkazakov edited the content of this paste. (Show Details)Oct 7 2016, 9:44 AM
dkazakov changed the title of this paste from untitled to Masterwork From Distant Lands.
dkazakov updated the paste's language from autodetect to autodetect.