Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / extension / internal / ps.cpp
index 8cf2845363923a99e273f13095d60ad0d6fc77a0..15b0bd8f40d95bc57e8a592c1fea40ec64df9d9f 100644 (file)
 #include <signal.h>
 #include <errno.h>
 
-#include <libnr/n-art-bpath.h>
+#include <libnr/nr-matrix-fns.h>
 
 #include <glib/gmem.h>
+#include <glib/gstrfuncs.h>
 #include <gtk/gtkstock.h>
 #include <gtk/gtkvbox.h>
 #include <gtk/gtkframe.h>
 #include <string>
 #include <iostream>
 #include <fstream>
+#include <cstring>
+#include <cstdio>
+#include <cstdlib>
+#include <cmath>
 
-#ifdef SOLARIS_2_8
-#include <unistd.h>
-#endif
+#include <2geom/sbasis-to-bezier.h>
+#include <2geom/bezier-curve.h>
+#include <2geom/hvlinesegment.h>
+#include "helper/geom-curves.h"
 
+/*
+using std::atof;
+using std::ceil;
+using std::fclose;
+using std::ferror;
+using std::fflush;
+using std::fgetc;
+using std::fprintf;  --> this line will result  'std::libintl_fprintf' has not been declared
+using std::fputc;
+using std::fseek;
+using std::ifstream;
+using std::ios;
+using std::memset;
+using std::strchr;
+using std::strcmp;
+using std::strerror;
+using std::tmpfile;
+*/
 using namespace std;
 
 namespace Inkscape {
@@ -277,7 +301,7 @@ PrintPS::begin(Inkscape::Extension::Print *mod, SPDocument *doc)
     if (fn != NULL) {
         if (*fn == '|') {
             fn += 1;
-            while (isspace(*fn)) fn += 1;
+            while (g_ascii_isspace(*fn)) fn += 1;
 #ifndef WIN32
             osp = popen(fn, "w");
 #else
@@ -529,13 +553,13 @@ PrintPS::finish(Inkscape::Extension::Print *mod)
         int const width = (int) (_width * dots_per_pt + 0.5);
         int const height = (int) (_height * dots_per_pt + 0.5);
 
-        NRMatrix affine;
-        affine.c[0] = width / ((x1 - x0) * PX_PER_PT);
-        affine.c[1] = 0.0;
-        affine.c[2] = 0.0;
-        affine.c[3] = height / ((y1 - y0) * PX_PER_PT);
-        affine.c[4] = -affine.c[0] * x0;
-        affine.c[5] = -affine.c[3] * y0;
+        NR::Matrix affine;
+        affine[0] = width / ((x1 - x0) * PX_PER_PT);
+        affine[1] = 0.0;
+        affine[2] = 0.0;
+        affine[3] = height / ((y1 - y0) * PX_PER_PT);
+        affine[4] = -affine[0] * x0;
+        affine[5] = -affine[3] * y0;
 
         nr_arena_item_set_transform(mod->root, &affine);
 
@@ -551,7 +575,7 @@ PrintPS::finish(Inkscape::Extension::Print *mod)
 
             /* Update to renderable state. */
             NRGC gc(NULL);
-            nr_matrix_set_identity(&gc.transform);
+            gc.transform.set_identity();
             nr_arena_item_invoke_update(mod->root, &bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
             /* Render */
             /* This should take guchar* instead of unsigned char*) */
@@ -562,13 +586,13 @@ PrintPS::finish(Inkscape::Extension::Print *mod)
             memset(px, 0xff, 4 * width * 64);
             nr_arena_item_invoke_render(NULL, mod->root, &bbox, &pb, 0);
             /* Blitter goes here */
-            NRMatrix imgt;
-            imgt.c[0] = (bbox.x1 - bbox.x0) / dots_per_pt;
-            imgt.c[1] = 0.0;
-            imgt.c[2] = 0.0;
-            imgt.c[3] = (bbox.y1 - bbox.y0) / dots_per_pt;
-            imgt.c[4] = 0.0;
-            imgt.c[5] = _height - y / dots_per_pt - (bbox.y1 - bbox.y0) / dots_per_pt;
+            NR::Matrix imgt;
+            imgt[0] = (bbox.x1 - bbox.x0) / dots_per_pt;
+            imgt[1] = 0.0;
+            imgt[2] = 0.0;
+            imgt[3] = (bbox.y1 - bbox.y0) / dots_per_pt;
+            imgt[4] = 0.0;
+            imgt[5] = _height - y / dots_per_pt - (bbox.y1 - bbox.y0) / dots_per_pt;
 
             print_image(_stream, px, bbox.x1 - bbox.x0, bbox.y1 - bbox.y0, 4 * width, &imgt);
         }
@@ -609,24 +633,24 @@ PrintPS::finish(Inkscape::Extension::Print *mod)
 }
 
 unsigned int
-PrintPS::bind(Inkscape::Extension::Print *mod, NRMatrix const *transform, float opacity)
+PrintPS::bind(Inkscape::Extension::Print */*mod*/, NR::Matrix const *transform, float /*opacity*/)
 {
     if (!_stream) return 0;  // XXX: fixme, returning -1 as unsigned.
     if (_bitmap) return 0;
 
     Inkscape::SVGOStringStream os;
-    os << "gsave [" << transform->c[0] << " "
-       << transform->c[1] << " "
-       << transform->c[2] << " "
-       << transform->c[3] << " "
-       << transform->c[4] << " "
-       << transform->c[5] << "] concat\n";
+    os << "gsave [" << (*transform)[0] << " "
+       << (*transform)[1] << " "
+       << (*transform)[2] << " "
+       << (*transform)[3] << " "
+       << (*transform)[4] << " "
+       << (*transform)[5] << "] concat\n";
 
     return fprintf(_stream, "%s", os.str().c_str());
 }
 
 unsigned int
-PrintPS::release(Inkscape::Extension::Print *mod)
+PrintPS::release(Inkscape::Extension::Print */*mod*/)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
     if (_bitmap) return 0;
@@ -635,7 +659,7 @@ PrintPS::release(Inkscape::Extension::Print *mod)
 }
 
 unsigned int
-PrintPS::comment(Inkscape::Extension::Print *mod, char const *comment)
+PrintPS::comment(Inkscape::Extension::Print */*mod*/, char const *comment)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
     if (_bitmap) return 0;
@@ -646,18 +670,18 @@ PrintPS::comment(Inkscape::Extension::Print *mod, char const *comment)
 void
 PrintPS::print_fill_style(SVGOStringStream &os, SPStyle const *const style, NRRect const *pbox)
 {
-    g_return_if_fail( style->fill.type == SP_PAINT_TYPE_COLOR
-                      || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+    g_return_if_fail( style->fill.isColor()
+                      || ( style->fill.isPaintserver()
                            && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) );
 
-    if (style->fill.type == SP_PAINT_TYPE_COLOR) {
+    if (style->fill.isColor()) {
         float rgb[3];
         sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
 
         os << rgb[0] << " " << rgb[1] << " " << rgb[2] << " setrgbcolor\n";
 
     } else {
-        g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+        g_assert( style->fill.isPaintserver()
                   && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
 
         if (SP_IS_LINEARGRADIENT(SP_STYLE_FILL_SERVER(style))) {
@@ -714,7 +738,7 @@ PrintPS::print_fill_style(SVGOStringStream &os, SPStyle const *const style, NRRe
                                            pbox->x0, pbox->y0);
                 c *= bbox2user;
                 f *= bbox2user;
-                r *= bbox2user.expansion();
+                r *= NR::expansion(bbox2user);
             }
 
             os << "<<\n/ShadingType 3\n/ColorSpace /DeviceRGB\n";
@@ -793,14 +817,14 @@ PrintPS::print_stroke_style(SVGOStringStream &os, SPStyle const *style)
 
 
 unsigned int
-PrintPS::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *const style,
+PrintPS::fill(Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, NR::Matrix const *ctm, SPStyle const *const style,
               NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
     if (_bitmap) return 0;
 
-    if ( style->fill.type == SP_PAINT_TYPE_COLOR
-         || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+    if ( style->fill.isColor()
+         || ( style->fill.isPaintserver()
               && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) )
     {
         Inkscape::SVGOStringStream os;
@@ -809,13 +833,13 @@ PrintPS::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix co
 
         print_fill_style(os, style, pbox);
 
-        print_bpath(os, bpath->path);
+        print_pathvector(os, pathv);
 
-        if (style->fill_rule.value == SP_WIND_RULE_EVENODD) {
-            if (style->fill.type == SP_PAINT_TYPE_COLOR) {
+        if (style->fill_rule.computed == SP_WIND_RULE_EVENODD) {
+            if (style->fill.isColor()) {
                 os << "eofill\n";
             } else {
-                g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+                g_assert( style->fill.isPaintserver()
                           && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
                 SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));
                 os << "eoclip\n";
@@ -830,10 +854,10 @@ PrintPS::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix co
                 }
             }
         } else {
-            if (style->fill.type == SP_PAINT_TYPE_COLOR) {
+            if (style->fill.isColor()) {
                 os << "fill\n";
             } else {
-                g_assert( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+                g_assert( style->fill.isPaintserver()
                           && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) );
                 SPGradient const *g = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));
                 os << "clip\n";
@@ -859,18 +883,18 @@ PrintPS::fill(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix co
 
 
 unsigned int
-PrintPS::stroke(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix const *ctm, SPStyle const *style,
+PrintPS::stroke(Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, NR::Matrix const *ctm, SPStyle const *style,
                 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
     if (_bitmap) return 0;
 
-    if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
+    if (style->stroke.isColor()) {
         Inkscape::SVGOStringStream os;
 
         print_stroke_style(os, style);
 
-        print_bpath(os, bpath->path);
+        print_pathvector(os, pathv);
 
         os << "stroke\n";
 
@@ -882,7 +906,7 @@ PrintPS::stroke(Inkscape::Extension::Print *mod, NRBPath const *bpath, NRMatrix
 
 unsigned int
 PrintPS::image(Inkscape::Extension::Print *mod, guchar *px, unsigned int w, unsigned int h, unsigned int rs,
-               NRMatrix const *transform, SPStyle const *style)
+               NR::Matrix const *transform, SPStyle const *style)
 {
     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
     if (_bitmap) return 0;
@@ -894,7 +918,7 @@ PrintPS::image(Inkscape::Extension::Print *mod, guchar *px, unsigned int w, unsi
 char const *
 PrintPS::PSFontName(SPStyle const *style)
 {
-    font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
+    font_instance *tf = font_factory::Default()->FaceFromStyle(style);
 
     char const *n;
     char name_buf[256];
@@ -1254,7 +1278,8 @@ PrintPS::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,
     * that's why using PSFontName() method just to get the PS fontname
     * is not enough and not appropriate
     */
-    font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
+    font_instance *tf = font_factory::Default()->FaceFromStyle(style);
+    
     const gchar *fn = NULL;
     char name_buf[256];
 
@@ -1367,8 +1392,8 @@ PrintPS::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,
    //The commented line beneath causes Inkscape to crash under Linux but not under Windows
     //g_free((void*) fn);
 
-    if ( style->fill.type == SP_PAINT_TYPE_COLOR
-         || ( style->fill.type == SP_PAINT_TYPE_PAINTSERVER
+    if ( style->fill.isColor()
+         || ( style->fill.isPaintserver()
               && SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)) ) )
     {
         // set fill style
@@ -1385,7 +1410,7 @@ PrintPS::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,
         os << ") show\n";
     }
 
-    if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
+    if (style->stroke.isColor()) {
 
         // set stroke style
         print_stroke_style(os, style);
@@ -1413,44 +1438,55 @@ PrintPS::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,
 /* PostScript helpers */
 
 void
-PrintPS::print_bpath(SVGOStringStream &os, NArtBpath const *bp)
+PrintPS::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv)
 {
+    if (pathv.empty())
+        return;
+
     os << "newpath\n";
-    bool closed = false;
-    while (bp->code != NR_END) {
-        switch (bp->code) {
-            case NR_MOVETO:
-                if (closed) {
-                    os << "closepath\n";
-                }
-                closed = true;
-                os << bp->x3 << " " << bp->y3 << " moveto\n";
-                break;
-            case NR_MOVETO_OPEN:
-                if (closed) {
-                    os << "closepath\n";
-                }
-                closed = false;
-                os << bp->x3 << " " << bp->y3 << " moveto\n";
-                break;
-            case NR_LINETO:
-                os << bp->x3 << " " << bp->y3 << " lineto\n";
-                break;
-            case NR_CURVETO:
-                os << bp->x1 << " " << bp->y1 << " "
-                   << bp->x2 << " " << bp->y2 << " "
-                   << bp->x3 << " " << bp->y3 << " curveto\n";
-                break;
-            default:
-                break;
+
+    for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
+
+        os << it->initialPoint()[Geom::X] << " " << it->initialPoint()[Geom::Y] << " moveto\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";
+        }
+
     }
-    if (closed) {
-        os << "closepath\n";
+}
+
+void
+PrintPS::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
+{
+    using Geom::X;
+    using Geom::Y;
+
+    if( is_straight_curve(c) )
+    {
+        os << c.finalPoint()[X] << " " << c.finalPoint()[Y] << " lineto\n";
+    }
+    else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
+        std::vector<Geom::Point> points = cubic_bezier->points();
+        os << points[1][X] << " " << points[1][Y] << " "
+           << points[2][X] << " " << points[2][Y] << " "
+           << points[3][X] << " " << points[3][Y] << " curveto\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);
+        }
     }
 }
 
+
 /* The following code is licensed under GNU GPL.
 ** The packbits, ascii85 and imaging printing code
 ** is from the gimp's postscript.c.
@@ -1614,18 +1650,18 @@ PrintPS::ascii85_done(SVGOStringStream &os)
 
 unsigned int
 PrintPS::print_image(FILE *ofp, guchar *px, unsigned int width, unsigned int height, unsigned int rs,
-                     NRMatrix const *transform)
+                     NR::Matrix const *transform)
 {
     Inkscape::SVGOStringStream os;
 
     os << "gsave\n";
 
-    os << "[" << transform->c[0] << " "
-       << transform->c[1] << " "
-       << transform->c[2] << " "
-       << transform->c[3] << " "
-       << transform->c[4] << " "
-       << transform->c[5] << "] concat\n";
+    os << "[" << (*transform)[0] << " "
+       << (*transform)[1] << " "
+       << (*transform)[2] << " "
+       << (*transform)[3] << " "
+       << (*transform)[4] << " "
+       << (*transform)[5] << "] concat\n";
 
     /* Write read image procedure */
     os << "<<\n";
@@ -1728,15 +1764,15 @@ PrintPS::init(void)
 {
     /* SVG in */
     (void) Inkscape::Extension::build_from_mem(
-        "<inkscape-extension>\n"
+        "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
         "<name>" N_("Postscript Print") "</name>\n"
         "<id>" SP_MODULE_KEY_PRINT_PS "</id>\n"
-        "<param name=\"bitmap\" type=\"boolean\">FALSE</param>\n"
+        "<param name=\"bitmap\" type=\"boolean\">false</param>\n"
         "<param name=\"resolution\" type=\"string\">72</param>\n"
         "<param name=\"destination\" type=\"string\">| lp</param>\n"
-        "<param name=\"pageBoundingBox\" type=\"boolean\">TRUE</param>\n"
-        "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
-        "<param name=\"fontEmbedded\" type=\"boolean\">FALSE</param>\n"
+        "<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
+        "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
+        "<param name=\"fontEmbedded\" type=\"boolean\">false</param>\n"
         "<print/>\n"
         "</inkscape-extension>", new PrintPS());
 }