Code

add pdf import filter via poppler-cairo
[inkscape.git] / src / extension / internal / grid.cpp
index cf5c7529db0e06bdb0740a7f747d18a8a9c73c4d..a19ab75388c49e36a1d4ab0a44f63d4487481752 100644 (file)
@@ -1,11 +1,12 @@
 /**
     \file grid.cpp
+
     A plug-in to add a grid creation effect into Inkscape.
 */
 /*
  * Copyright (C) 2004-2005  Ted Gould <ted@gould.cx>
  * Copyright (C) 2007  MenTaLguY <mental@rydia.net>
+ *   Abhishek Sharma
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
@@ -20,6 +21,8 @@
 #include "sp-object.h"
 #include "util/glib-list-iterators.h"
 
+#include "svg/path-string.h"
+
 #include "extension/effect.h"
 #include "extension/system.h"
 
@@ -36,7 +39,7 @@ namespace Internal {
     \return Whether the load was sucessful
 */
 bool
-Grid::load (Inkscape::Extension::Extension *module)
+Grid::load (Inkscape::Extension::Extension */*module*/)
 {
     // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
     return TRUE;
@@ -44,34 +47,29 @@ Grid::load (Inkscape::Extension::Extension *module)
 
 namespace {
 
-Glib::ustring build_op(char op, NR::Point p) {
-    gchar *floatstring;
-    // FIXME: locale formatting issues?
-    floatstring = g_strdup_printf("%c%f,%f ", op, p[NR::X], p[NR::Y]);
-    Glib::ustring result(floatstring);
-    g_free(floatstring);
-    return result;
-}
-
-Glib::ustring build_lines(int axis, NR::Rect bounding_area,
-                          float offset, float spacing)
+Glib::ustring build_lines(Geom::Rect bounding_area,
+                          float offset[], float spacing[])
 {
-    NR::Point point_offset(0.0, 0.0);
-    point_offset[axis] = offset;
+    Geom::Point point_offset(0.0, 0.0);
 
-    Glib::ustring path_data("");
-    for (NR::Point start_point = bounding_area.min();
-            start_point[axis] + offset <= (bounding_area.max())[axis];
-            start_point[axis] += spacing) {
-        NR::Point end_point = start_point;
-        end_point[1-axis] = (bounding_area.max())[1-axis];
-
-        path_data += build_op('M', start_point + point_offset)
-                   + build_op('L', end_point + point_offset);
-    }
+    SVG::PathString path_data;
 
-    return path_data;
-}
+    for ( int axis = 0 ; axis < 2 ; ++axis ) {
+        point_offset[axis] = offset[axis];
+
+        for (Geom::Point start_point = bounding_area.min();
+                start_point[axis] + offset[axis] <= (bounding_area.max())[axis];
+                start_point[axis] += spacing[axis]) {
+            Geom::Point end_point = start_point;
+            end_point[1-axis] = (bounding_area.max())[1-axis];
+
+            path_data.moveTo(start_point + point_offset)
+                     .lineTo(end_point + point_offset);
+        }
+    }
+        // std::cout << "Path data:" << path_data.c_str() << std::endl;
+        return path_data;
+    }
 
 }
 
@@ -81,23 +79,25 @@ Glib::ustring build_lines(int axis, NR::Rect bounding_area,
     \param  document What should be edited.
 */
 void
-Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document)
+Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
 {
     Inkscape::Selection * selection     = ((SPDesktop *)document)->selection;
 
-    NR::Rect bounding_area = NR::Rect(NR::Point(0,0), NR::Point(100,100));
+    Geom::Rect bounding_area = Geom::Rect(Geom::Point(0,0), Geom::Point(100,100));
     if (selection->isEmpty()) {
         /* get page size */
         SPDocument * doc = document->doc();
-        bounding_area = NR::Rect(NR::Point(0,0),
-                                 NR::Point(sp_document_width(doc),
-                                           sp_document_height(doc)));
+        bounding_area = Geom::Rect(  Geom::Point(0,0),
+                                     Geom::Point(doc->getWidth(), doc->getHeight())  );
     } else {
-        bounding_area = selection->bounds();
+        Geom::OptRect bounds = selection->bounds();
+        if (bounds) {
+            bounding_area = *bounds;
+        }
 
-        gdouble doc_height  =  sp_document_height(document->doc());
-        NR::Rect temprec = NR::Rect(NR::Point(bounding_area.min()[NR::X], doc_height - bounding_area.min()[NR::Y]),
-                                    NR::Point(bounding_area.max()[NR::X], doc_height - bounding_area.max()[NR::Y]));
+        gdouble doc_height  =  (document->doc())->getHeight();
+        Geom::Rect temprec = Geom::Rect(Geom::Point(bounding_area.min()[Geom::X], doc_height - bounding_area.min()[Geom::Y]),
+                                    Geom::Point(bounding_area.max()[Geom::X], doc_height - bounding_area.max()[Geom::Y]));
 
         bounding_area = temprec;
     }
@@ -109,13 +109,13 @@ Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *doc
                          module->get_param_float("yoffset") };
 
     Glib::ustring path_data("");
-    for ( int axis = 0 ; axis < 2 ; ++axis ) {
-        path_data += build_lines(axis, bounding_area,
-                                 offsets[axis], spacings[axis]);
-    }
 
-    Inkscape::XML::Document * xml_doc = sp_document_repr_doc(document->doc());
-    Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->repr;
+    path_data = build_lines(bounding_area,
+                                 offsets, spacings);
+    Inkscape::XML::Document * xml_doc = document->doc()->getReprDoc();
+
+    //XML Tree being used directly here while it shouldn't be.
+    Inkscape::XML::Node * current_layer = static_cast<SPDesktop *>(document)->currentLayer()->getRepr();
     Inkscape::XML::Node * path = xml_doc->createElement("svg:path");
 
     path->setAttribute("d", path_data.c_str());
@@ -123,12 +123,15 @@ Grid::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *doc
     Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
     style += ";stroke-width:";
     gchar floatstring[64];
-    sprintf(floatstring, "%f", line_width);
+    std::ostringstream stringstream;
+    stringstream << line_width;
+    sprintf(floatstring, "%s", stringstream.str().c_str());
     style += floatstring;
     style += "pt";
     path->setAttribute("style", style.c_str());
 
     current_layer->appendChild(path);
+               Inkscape::GC::release(path);
 
     return;
 }
@@ -144,7 +147,7 @@ public:
                 describing the parameter. */
     PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
             Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
-        this->set_value(_ext->get_param_float(_pref)); 
+        this->set_value(_ext->get_param_float(_pref));
         this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
         return;
     };
@@ -173,17 +176,17 @@ PrefAdjustment::val_changed (void)
     Uses AutoGUI for creating the GUI.
 */
 Gtk::Widget *
-Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view)
+Grid::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
 {
     SPDocument * current_document = view->doc();
 
     using Inkscape::Util::GSListConstIterator;
     GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
     Inkscape::XML::Node * first_select = NULL;
-    if (selected != NULL) 
+    if (selected != NULL)
         first_select = SP_OBJECT_REPR(*selected);
 
-    return module->autogui(current_document, first_select);
+    return module->autogui(current_document, first_select, changeSignal);
 }
 
 #include "clear-n_.h"
@@ -192,14 +195,14 @@ void
 Grid::init (void)
 {
     Inkscape::Extension::build_from_mem(
-        "<inkscape-extension>\n"
+        "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
             "<name>" N_("Grid") "</name>\n"
             "<id>org.inkscape.effect.grid</id>\n"
-            "<param name=\"lineWidth\" gui-text=\"" N_("Line Width") "\" type=\"float\">1.0</param>\n"
-            "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing") "\" type=\"float\">10.0</param>\n"
-            "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing") "\" type=\"float\">10.0</param>\n"
-            "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset") "\" type=\"float\">5.0</param>\n"
-            "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset") "\" type=\"float\">5.0</param>\n"
+            "<param name=\"lineWidth\" gui-text=\"" N_("Line Width:") "\" type=\"float\">1.0</param>\n"
+            "<param name=\"xspacing\" gui-text=\"" N_("Horizontal Spacing:") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
+            "<param name=\"yspacing\" gui-text=\"" N_("Vertical Spacing:") "\" type=\"float\" min=\"0.1\" max=\"1000\">10.0</param>\n"
+            "<param name=\"xoffset\" gui-text=\"" N_("Horizontal Offset:") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
+            "<param name=\"yoffset\" gui-text=\"" N_("Vertical Offset:") "\" type=\"float\" min=\"0.0\" max=\"1000\">0.0</param>\n"
             "<effect>\n"
                 "<object-type>all</object-type>\n"
                 "<effects-menu>\n"