Code

NR::Maybe => boost::optional
[inkscape.git] / src / extension / internal / latex-pstricks.cpp
index e14d199291391c91dc3097afadcc10a487829c99..c1dcec361b0ca28d249dbbf856d8a10cefe98556 100644 (file)
 #include "libnr/nr-scale-translate-ops.h"
 #include "libnr/nr-translate-scale-ops.h"
 #include <libnr/nr-matrix-fns.h>
+#include <2geom/pathvector.h>
+#include <2geom/sbasis-to-bezier.h>
+#include <2geom/bezier-curve.h>
+#include <2geom/hvlinesegment.h>
+#include "helper/geom-curves.h"
 
-
-#include "libnr/n-art-bpath.h"
 #include "sp-item.h"
 
-
 #include "style.h"
 
 #include "latex-pstricks.h"
@@ -201,8 +203,8 @@ unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
 
 unsigned int
 PrintLatex::fill(Inkscape::Extension::Print *mod,
-                NRBPath const *bpath, NR::Matrix const *transform, SPStyle const *style,
-                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
+        Geom::PathVector const &pathv, NR::Matrix const *transform, SPStyle const *style,
+        NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
 
@@ -217,7 +219,7 @@ PrintLatex::fill(Inkscape::Extension::Print *mod,
 
         os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor]\n{\n";
 
-        print_bpath(os, bpath->path, transform);
+        print_pathvector(os, pathv, transform);
 
         os << "}\n}\n";
 
@@ -228,7 +230,7 @@ PrintLatex::fill(Inkscape::Extension::Print *mod,
 }
 
 unsigned int
-PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const NR::Matrix *transform, const SPStyle *style,
+PrintLatex::stroke (Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, const NR::Matrix *transform, const SPStyle *style,
                              const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
@@ -260,7 +262,7 @@ PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const
 
         os <<"]\n{\n";
 
-        print_bpath(os, bpath->path, transform);
+        print_pathvector(os, pathv, transform);
 
         os << "}\n}\n";
 
@@ -270,61 +272,57 @@ PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const
     return 0;
 }
 
+// FIXME: why is 'transform' argument not used?
 void
-PrintLatex::print_bpath(SVGOStringStream &os, const NArtBpath *bp, const NR::Matrix *transform)
+PrintLatex::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv_in, const NR::Matrix * /*transform*/)
 {
-    unsigned int closed;
-    NR::Matrix tf=*transform;
-    NR::Matrix tf_stack=m_tr_stack.top();
+    if (pathv_in.empty())
+        return;
+
+//    NR::Matrix tf=*transform;   // why was this here?
+    NR::Matrix tf_stack=m_tr_stack.top(); // and why is transform argument not used?
+    Geom::PathVector pathv = pathv_in * to_2geom(tf_stack); // generates new path, which is a bit slow, but this doesn't have to be performance optimized
 
     os << "\\newpath\n";
-    closed = FALSE;
-    while (bp->code != NR_END) {
-        using NR::X;
-        using NR::Y;
-
-//        NR::Point const p1(bp->c(1) * tf);
-//        NR::Point const p2(bp->c(2) * tf);
-//        NR::Point const p3(bp->c(3) * tf);
-
-        NR::Point const p1(bp->c(1) * tf_stack);
-        NR::Point const p2(bp->c(2) * tf_stack);
-        NR::Point const p3(bp->c(3) * tf_stack);
-
-        double const x1 = p1[X], y1 = p1[Y];
-        double const x2 = p2[X], y2 = p2[Y];
-        double const x3 = p3[X], y3 = p3[Y];
-        
-        switch (bp->code) {
-            case NR_MOVETO:
-                if (closed) {
-                    os << "\\closepath\n";
-                }
-                closed = TRUE;
-                os << "\\moveto(" << x3 << "," << y3 << ")\n";
-                break;
-            case NR_MOVETO_OPEN:
-                if (closed) {
-                    os << "\\closepath\n";
-                }
-                closed = FALSE;
-                os << "\\moveto(" << x3 << "," << y3 << ")\n";
-                break;
-            case NR_LINETO:
-                os << "\\lineto(" << x3 << "," << y3 << ")\n";
-                break;
-            case NR_CURVETO:
-                os << "\\curveto(" << x1 << "," << y1 << ")("
-                   << x2 << "," << y2 << ")("
-                   << x3 << "," << y3 << ")\n";
-                break;
-            default:
-                break;
+
+    for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
+
+        os << "\\moveto(" << it->initialPoint()[Geom::X] << "," << it->initialPoint()[Geom::Y] << ")\n";
+
+        for(Geom::Path::const_iterator cit = it->begin(); cit != it->end_open(); ++cit) {
+            print_2geomcurve(os, *cit);
+        }
+
+        if (it->closed()) {
+            os << "\\closepath\n";
         }
-        bp += 1;
+
     }
-    if (closed) {
-        os << "\\closepath\n";
+}
+
+void
+PrintLatex::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
+{
+    using Geom::X;
+    using Geom::Y;
+
+    if( is_straight_curve(c) )
+    {
+        os << "\\lineto(" << c.finalPoint()[X] << "," << c.finalPoint()[Y] << ")\n";
+    }
+    else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
+        std::vector<Geom::Point> points = cubic_bezier->points();
+        os << "\\curveto(" << points[1][X] << "," << points[1][Y] << ")("
+                           << points[2][X] << "," << points[2][Y] << ")("
+                           << points[3][X] << "," << points[3][Y] << ")\n";
+    }                                             
+    else {
+        //this case handles sbasis as well as all other curve types
+        Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
+
+        for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
+            print_2geomcurve(os, *iter);
+        }
     }
 }
 
@@ -347,7 +345,7 @@ PrintLatex::init (void)
                        "<name>" N_("LaTeX Print") "</name>\n"
                        "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
                        "<param name=\"destination\" type=\"string\"></param>\n"
-                        "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
+                        "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
                        "<print/>\n"
                "</inkscape-extension>", new PrintLatex());