Code

Filters. New custom predefined filters (all ABCs, Neon draw, Color shilf, Silhouette...
[inkscape.git] / src / extension / internal / latex-pstricks.cpp
index 3c3cf6682dc25ce1f5288ac848810f44f568e888..44b64c5f8f999fdb3da78ec39b81ce0846d696d0 100644 (file)
@@ -1,10 +1,9 @@
-#define __SP_LATEX_C__
-
 /*
  * LaTeX Printing
  *
  * Author:
  *  Michael Forbes <miforbes@mbhs.edu>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2004 Authors
  * 
 #include <signal.h>
 #include <errno.h>
 
-#include "libnr/n-art-bpath.h"
-#include "sp-item.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 "sp-item.h"
 
 #include "style.h"
 
@@ -67,6 +70,8 @@ PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
     FILE *osf, *osp;
     const gchar * fn;
 
+    os.setf(std::ios::fixed);
+
     fn = mod->get_param_string("destination");
 
     osf = NULL;
@@ -122,59 +127,22 @@ PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
     }
 
     // width and height in pt
-    _width = sp_document_width(doc) * PT_PER_PX;
-    _height = sp_document_height(doc) * PT_PER_PX;
-
-    NRRect d;
-    bool   pageLandscape;
-    // printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");
-/*    if (pageBoundingBox) {
-       d.x0 = d.y0 = 0;
-        d.x1 = ceil(_width);
-        d.y1 = ceil(_height);
-    } else */{ // no bounding boxes for now
-        SPItem* doc_item = SP_ITEM(sp_document_root(doc));
-        sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
-        // convert from px to pt
-        d.x0 *= PT_PER_PX;
-        d.x1 *= PT_PER_PX;
-        d.y0 *= PT_PER_PX;
-        d.y1 *= PT_PER_PX;
-    }
-
+    _width = doc->getWidth() * PT_PER_PX;
+    _height = doc->getHeight() * PT_PER_PX;
 
     if (res >= 0) {
 
         os << "%%Creator: " << PACKAGE_STRING << "\n";
        os << "%%Please note this file requires PSTricks extensions\n";
 
-        // 2004 Dec 10, BFC:
-        // The point of the following code is (1) to do the thing that's expected by users
-        // who have done File>New>A4_landscape or ...letter_landscape (i.e., rotate
-        // the output), while (2) not messing up users who simply want their output wider
-        // than it is tall (e.g., small figures for inclusion in LaTeX).
-        // The original patch by WQ only had the w>h condition.
-        {
-             double w = (d.x1 - d.x0); // width and height of bounding box, in pt
-             double h = (d.y1 - d.y0);
-             pageLandscape = (
-                 (w > 0. && h > 0.) // empty documents fail this sanity check, have w<0, h<0
-                 && (w > h)   // implies, but does not prove, the user wanted landscape
-                 && (w > 600) // approximate maximum printable width of an A4
-             )
-             ? true : false;
-        }
-
-        if (pageLandscape) {
-            os << "\\rotate{90}\n";
-        }
-
         os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
         // from now on we can output px, but they will be treated as pt
     
-        os << "\\begin{pspicture}(" << sp_document_width(doc) << "," << sp_document_height(doc) << ")\n";
+        os << "\\begin{pspicture}(" << doc->getWidth() << "," << doc->getHeight() << ")\n";
     }
 
+    m_tr_stack.push( Geom::Scale(1, -1) * Geom::Translate(0, doc->getHeight()));
+
     return fprintf(_stream, "%s", os.str().c_str());
 }
 
@@ -195,6 +163,27 @@ PrintLatex::finish (Inkscape::Extension::Print *mod)
     return 0;
 }
 
+unsigned int
+PrintLatex::bind(Inkscape::Extension::Print *mod, Geom::Matrix const *transform, float opacity)
+{
+    Geom::Matrix tr = *transform;
+    
+    if(m_tr_stack.size()){
+        Geom::Matrix tr_top = m_tr_stack.top();
+        m_tr_stack.push(tr * tr_top);
+    }else
+        m_tr_stack.push(tr);
+
+    return 1;
+}
+
+unsigned int
+PrintLatex::release(Inkscape::Extension::Print *mod)
+{
+    m_tr_stack.pop();
+    return 1;
+}
+
 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
                                  const char * comment)
 {
@@ -205,21 +194,29 @@ unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
 
 unsigned int
 PrintLatex::fill(Inkscape::Extension::Print *mod,
-                NRBPath const *bpath, NRMatrix const *transform, SPStyle const *style,
-                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
+        Geom::PathVector const &pathv, Geom::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.
 
-    if (style->fill.type == SP_PAINT_TYPE_COLOR) {
+    if (style->fill.isColor()) {
         Inkscape::SVGOStringStream os;
         float rgb[3];
+        float fill_opacity;
 
+        os.setf(std::ios::fixed);
+
+        fill_opacity=SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
         sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
         os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
+        os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor";
+        if (fill_opacity!=1.0) {
+            os << ",opacity="<<fill_opacity;
+        }
 
-        os << "\\pscustom[fillstyle=solid,fillcolor=curcolor]\n{\n";
+        os << "]\n{\n";
 
-        print_bpath(os, bpath->path, transform);
+        print_pathvector(os, pathv, transform);
 
         os << "}\n}\n";
 
@@ -230,19 +227,28 @@ PrintLatex::fill(Inkscape::Extension::Print *mod,
 }
 
 unsigned int
-PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const NRMatrix *transform, const SPStyle *style,
+PrintLatex::stroke (Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, const Geom::Matrix *transform, const SPStyle *style,
                              const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
 
-    if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
+    if (style->stroke.isColor()) {
         Inkscape::SVGOStringStream os;
         float rgb[3];
+        float stroke_opacity;
+        Geom::Matrix tr_stack = m_tr_stack.top();
+        double const scale = tr_stack.descrim();
+        os.setf(std::ios::fixed);
 
+        stroke_opacity=SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
         sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
         os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
 
-        os << "\\pscustom[linewidth=" << style->stroke_width.computed<< ",linecolor=curcolor";
+        os << "\\pscustom[linewidth=" << style->stroke_width.computed*scale<< ",linecolor=curcolor";
+        
+        if (stroke_opacity!=1.0) {
+            os<<",strokeopacity="<<stroke_opacity;
+        }
 
         if (style->stroke_dasharray_set &&
                 style->stroke_dash.n_dash &&
@@ -259,7 +265,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";
 
@@ -269,55 +275,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 NRMatrix *transform)
+PrintLatex::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv_in, const Geom::Matrix * /*transform*/)
 {
-    unsigned int closed;
-    NR::Matrix tf=*transform;
+    if (pathv_in.empty())
+        return;
 
+//    Geom::Matrix tf=*transform;   // why was this here?
+    Geom::Matrix tf_stack=m_tr_stack.top(); // and why is transform argument not used?
+    Geom::PathVector pathv = pathv_in * 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);
-        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);
         }
-        bp += 1;
+
+        if (it->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";
     }
-    if (closed) {
-        os << "\\closepath\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);
+        }
     }
 }
 
@@ -336,11 +344,11 @@ PrintLatex::init (void)
        
        /* SVG in */
     ext = Inkscape::Extension::build_from_mem(
-               "<inkscape-extension>\n"
+               "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
                        "<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());
 
@@ -360,5 +368,5 @@ PrintLatex::init (void)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :