2

这很长,有很多代码,所以我希望 Stack Overflow 可以应付它。:P

我正在尝试使用 Boost Spirit 编写 SVG 解析器。我有一个用“轮廓”填充向量的语法,这些向量是“BezierPoints”的向量,它可以表示常规点或带有贝塞尔控件的点。

到目前为止,我有这个(还没有处理相关的绘制命令):

#ifndef SVG_PARSER_HPP
#define SVG_PARSER_HPP

#include <vector>

#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix.hpp"

#include "boost/fusion/include/adapt_struct.hpp"
#include "boost/fusion/include/std_pair.hpp"

namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;

struct Point
{
    Point(const double nx = 0.0, const double ny = 0.0) : x(nx), y(ny)
    {}

    double x;
    double y;
};

BOOST_FUSION_ADAPT_STRUCT(
    Point,
    (double, x)
    (double, y)
)

struct BezierPoint
{
    BezierPoint(const double x = 0.0, const double y = 0.0) :
      point(x, y), control1(0.0, 0.0), control2(0.0, 0.0) {}

    BezierPoint(const Point &p) : point(p), control1(0.0, 0.0),
      control2(0.0, 0.0) {}

    Point point; // End point.  Start point is in the BezierPoint that
                 // came before it.

    // Todo: Set these to be coincident with point for non-curve points.
    Point control1;
    Point control2;
};

BOOST_FUSION_ADAPT_STRUCT(
    BezierPoint,
    (Point, control1)
    (Point, control2)
    (Point, point)
)

typedef std::vector<BezierPoint> BezierVec;
typedef std::vector<BezierVec> Contours;

template <typename Iterator>
struct PathGrammar : qi::grammar<Iterator, Contours()>
{
    ///////////////////////////
    // SVG is a damn monster //
    ///////////////////////////

    PathGrammar() : PathGrammar::base_type(path_data)
    {
        using qi::char_;
        using qi::double_;
        using qi::_val;
        using qi::_1;

        using phoenix::push_back;
        using phoenix::insert;
        using phoenix::begin;
        using phoenix::end;
        using phoenix::construct;
        using phoenix::val;

        using ascii::space;

        path_data = *space >> -moveto_drawto_command_groups
          >> *space;

        moveto_drawto_command_groups = moveto_drawto_command_group
          % *space;
        moveto_drawto_command_group = moveto[
           insert(_val, end(_val), begin(_1), end(_1))
          ] >> *space
          >> -drawto_commands[
           insert(_val, end(_val), begin(_1), end(_1))
          ];

        // Draw commands are (optionally) followed by a closepath
        // command.
        drawto_commands = (drawto_command[
           insert(_val, end(_val), begin(_1), end(_1))
          ] % *space) >> *space >> -closepath;
        drawto_command =  lineto | horizontal_lineto
          | vertical_lineto | curveto | smooth_curveto;

        moveto = ( char_('M') | char_('m') ) >> *space
          >> lineto_argument_sequence;

        closepath = (char_('Z') | char_('z'));

        lineto = ( char_('L') | char_('l') ) >> *space
          >> lineto_argument_sequence;
        lineto_argument_sequence = coordinate_pair[
           push_back(_val, construct<BezierPoint>(_1))
          ] % -comma_space;

        horizontal_lineto = ( char_('H') | char_('h') ) >> *space
          >> horizontal_lineto_argument_sequence;
        horizontal_lineto_argument_sequence = coordinate[
           push_back(_val, construct<BezierPoint>(_1, val(0.0)))
          ] % -comma_space;

        vertical_lineto = ( char_('V') | char_('v') ) >> *space
          >> vertical_lineto_argument_sequence;
        vertical_lineto_argument_sequence = coordinate[
           push_back(_val, construct<BezierPoint>(val(0.0), _1))
          ] % -comma_space;

        curveto = ( char_('C') | char_('c') ) >> *space
          >> curveto_argument_sequence;
        curveto_argument_sequence = curveto_argument % -comma_space;
        curveto_argument = coordinate_pair >> -comma_space
          >> coordinate_pair >> -comma_space >> coordinate_pair;

        smooth_curveto = ( char_('S') | char_('s') ) >> *space
          >> smooth_curveto_argument_sequence;
        smooth_curveto_argument_sequence = smooth_curveto_argument
          % -comma_space;
        smooth_curveto_argument = coordinate_pair >> -comma_space
          >> coordinate_pair;

        coordinate_pair = (double_ >> -comma_space >> double_);
        coordinate = double_;
        comma_space = (+space >> -char_(',') >> *space)
          | (char_(',') >> *space);
    }

    // Quadratic curves are not supported

    qi::rule<Iterator, Contours()> path_data;

    qi::rule<Iterator, Contours()> moveto_drawto_command_groups;
    qi::rule<Iterator, BezierVec()> moveto_drawto_command_group;

    qi::rule<Iterator, BezierVec()> drawto_commands;
    qi::rule<Iterator, BezierVec()> drawto_command;

    qi::rule<Iterator, BezierVec()> moveto;
    qi::rule<Iterator, BezierVec()> moveto_argument_sequence;

    qi::rule<Iterator> closepath;

    qi::rule<Iterator, BezierVec()> lineto;
    qi::rule<Iterator, BezierVec()> lineto_argument_sequence;

    qi::rule<Iterator, BezierVec()> horizontal_lineto;
    qi::rule<Iterator, BezierVec()>
      horizontal_lineto_argument_sequence;

    qi::rule<Iterator, BezierVec()> vertical_lineto;
    qi::rule<Iterator, BezierVec()> vertical_lineto_argument_sequence;

    qi::rule<Iterator, BezierVec()> curveto;
    qi::rule<Iterator, BezierVec()> curveto_argument_sequence;
    qi::rule<Iterator, BezierPoint()> curveto_argument;

    qi::rule<Iterator, BezierVec()> smooth_curveto;
    qi::rule<Iterator, BezierVec()> smooth_curveto_argument_sequence;
    qi::rule<Iterator, BezierPoint()> smooth_curveto_argument;

    qi::rule<Iterator, Point()> coordinate_pair;
    qi::rule<Iterator, double()> coordinate;
    qi::rule<Iterator> comma_space;
};

#endif

语法是这样调用的:

typedef string::const_iterator StrItr;

PathGrammar<StrItr> grammar;

Contours paths;

StrItr startIt = pathData.begin();
StrItr endIt = pathData.end();
qi::parse(startIt, endIt, grammar, paths);

BOOST_FOREACH(BezierVec v, paths)
{
    cout << "Path:" << endl;
    BOOST_FOREACH(BezierPoint p, v)
    {
        cout << '\t' << p.point.x << ", " << p.point.y << endl;
    }
}

这是我当前的测试字符串:

M26.591,0L0,22.348l25.46,23.479L12.306,100l36.067-23.619L85.008,28.43L26.591,0z M30.553,34.23
    l-8.487-10.467l9.052-5.234l25.601,8.77l-3.109,12.729L30.553,34.23z

重新格式化字符串以使其更易于阅读:

M 26.591,   0
L  0    ,  22.348
l 25.46 ,  23.479
L 12.306, 100
l 36.067, -23.619
L 85.008,  28.43
L 26.591,   0
z
M 30.553,  34.23
l -8.487, -10.467
l  9.052,  -5.234
l 25.601,   8.77
l -3.109,  12.729
L 30.553,  34.23
z

这是输出:

Path:
    77, 0
    26.591, 0
    76, 0
    0, 22.348
    108, 0
    25.46, 23.479
    76, 0
    12.306, 100
    108, 0
    36.067, -23.619
    76, 0
    85.008, 28.43
    76, 0
    26.591, 0
Path:
    77, 0
    30.553, 34.23
    108, 0
    -8.487, -10.467
    108, 0
    9.052, -5.234
    108, 0
    25.601, 8.77
    108, 0
    -3.109, 12.729
    76, 0
    30.553, 34.23

语法看到了要点,但它不断添加所有这些额外的点,我不知道它们来自哪里。

附言

我也想知道我的一些规则。首先有这个规则:

qi::rule<Iterator, BezierVec()> drawto_commands;
qi::rule<Iterator, BezierVec()> drawto_command;

...

drawto_commands = (drawto_command[
   insert(_val, end(_val), begin(_1), end(_1))
  ] % *space) >> *space >> -closepath;

我希望将结果(drawto_command % *space)作为单个向量而不是向量向量。据我所知,我必须使用 Phoenix 手动执行此操作。是这样吗?

我的 moveto 规则也有类似的情况:

qi::rule<Iterator, BezierVec()> moveto_drawto_command_group;
qi::rule<Iterator, BezierVec()> moveto;
qi::rule<Iterator, BezierVec()> moveto_argument_sequence;

...

moveto_drawto_command_group = moveto[
   insert(_val, end(_val), begin(_1), end(_1))
  ] >> *space
  >> -drawto_commands[
   insert(_val, end(_val), begin(_1), end(_1))
  ];

我有两条规则给出了 BezierVec,我想将它们组合成一个 BezierVec 作为第三条规则。到目前为止,这样做的唯一方法似乎是使用 Phoenix 手动插入。没有更简单的方法吗?

4

1 回答 1

5

输出中的附加值是从 ASCII 字符'M' == 77'L' == 76'l' == 108等生成的。当您使用char_('M')等匹配那些时会发生这种情况,这会将匹配的值作为char属性公开。编译器很乐意将它分配给double输出数组中的值。为了避免这种情况,请使用lit('M'),或仅使用'M'。这些都没有公开任何属性,使得表达式在生成的值方面是中立的。

可以改进的第二件事是*space从所有地方删除构造并切换到短语解析(请参阅此处phrase_parse的API 函数文档)。如果您提供一个解析器组件作为skipper 参数并将一个skipper 模板类型参数添加到您的所有规则中,您将获得与散布在语法中的构造相同的效果。例如:spacespace_type*space

qi::rule<Iterator, Contours(), space_type> path_data;

如果您有部分输入不允许包含空格,则这些仍然可以嵌入到lexeme[]指令中。请参阅此处了解更多信息。


你的 PS:

为了组合从drawto_command您返回的所有向量,可以使用强制 Spirit.Qi 附加到提供的(左侧属性)向量的技巧:

 drawto_commands = +drawto_command >> -closepath;

这已经假设您切换到短语解析,因此我删除了这些*space结构。为什么这行得通?好吧,Spirit.Qi 对序列有一个特殊的属性传播规则,如果该序列的所有元素都暴露了该属性类型或那些属性类型的容器,则强制它将其元素暴露的所有属性附加到提供的容器中。这里不需要语义动作。请注意,这仅适用于序列,而不适用于单个元素右侧构造。

您的第二个相关问题可以用类似的方式解决:

moveto_drawto_command_group = moveto >> -drawto_commands;

同样,不需要语义动作。

于 2010-07-02T00:50:43.467 回答