Code

2bd76e6234ba8bd5cfc920cfd80bcbdd3c19236b
[inkscape.git] / src / libnr / n-art-bpath-2geom.cpp
1 #define SEEN_LIBNR_N_ART_BPATH_2GEOM_CPP
3 /** \file
4  * Contains functions to convert from NArtBpath to 2geom's Path
5  *
6  * Copyright (C) Johan Engelen 2007-2008 <j.b.c.engelen@utwente.nl>
7  *
8  * Released under GNU GPL, read the file 'COPYING' for more information
9  */
10  
11 #include "libnr/n-art-bpath-2geom.h"
13 #include "svg/svg.h"
14 #include <glib.h>
15 #include <2geom/path.h>
16 #include <2geom/svg-path.h>
17 #include <2geom/svg-path-parser.h>
18 #include <typeinfo>
20 std::vector<Geom::Path>
21 BPath_to_2GeomPath(NArtBpath const * bpath)
22 {
23     std::vector<Geom::Path> pathv;
24     if (!bpath) {
25         return pathv;
26     }
28     NArtBpath const *bp = bpath;   // points to element within bpath
29     Geom::Path * current = NULL;   // points to current path
30     while (bp->code != NR_END) {
31         if ( current &&
32              ( (bp->code == NR_MOVETO) || (bp->code == NR_MOVETO_OPEN) )
33             )
34         {   // about to start a new path, correct the current path: nartbpath manually adds the closing line segment so erase it for closed path.
35             if (current->closed() && !current->empty()) {
36                 // but only remove this last segment if it is a *linesegment*:
37                 if ( dynamic_cast<Geom::LineSegment const *>(&current->back()) ) {
38                     current->erase_last();
39                 }
40             }
41         }
43         switch(bp->code) {
44             case NR_MOVETO:
45                 pathv.push_back( Geom::Path() );  // for some reason Geom::Path(Point) does not work...
46                 current = &pathv.back();
47                 current->start( Geom::Point(bp->x3, bp->y3) );
48                 current->close(true);
49             break;
51             case NR_MOVETO_OPEN:
52                 pathv.push_back( Geom::Path() );  // for some reason Geom::Path(Point) does not work...
53                 current = &pathv.back();
54                 current->start( Geom::Point(bp->x3, bp->y3) );
55                 current->close(false);
56             break;
58             case NR_LINETO:
59                 current->appendNew<Geom::LineSegment>( Geom::Point(bp->x3, bp->y3) );
60             break;
62             case NR_CURVETO:
63                 current->appendNew<Geom::CubicBezier> ( Geom::Point(bp->x1, bp->y1), Geom::Point(bp->x2, bp->y2), Geom::Point(bp->x3, bp->y3) );
64             break;
66             case NR_END:
67                 g_error("BPath_to_2GeomPath: logical error");
68             break;
69         }
70         ++bp;
71     }
72     if ( current && current->closed() && !current->empty() ) {
73         // correct the current path: nartbpath manually adds the closing line segment so erase it for closed path.
74         // but only remove this last segment if it is a *linesegment*:
75         if ( dynamic_cast<Geom::LineSegment const *>(&current->back()) ) {
76             current->erase_last();
77         }
78     }
80     return pathv;
81 }
83 NArtBpath *
84 BPath_from_2GeomPath(std::vector<Geom::Path> const & path)
85 {
86     char * svgd = sp_svg_write_path(path);
87     NArtBpath *bpath = sp_svg_read_path(svgd);
88     g_free(svgd);
89     return bpath;
90 }
94 /*
95   Local Variables:
96   mode:c++
97   c-file-style:"stroustrup"
98   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
99   indent-tabs-mode:nil
100   fill-column:99
101   End:
102 */
103 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :