From a303321365bf5ae640715eb9e033f6f01cb085c8 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sat, 5 Apr 2014 08:15:20 +0200 Subject: Allow command line options -i and -j to effect plain-svg output, this involves cropping and reducing. (bzr r13268) --- src/main.cpp | 16 ++++++++++++++++ src/sp-object.cpp | 17 +++++++++++++++++ src/sp-object.h | 5 +++++ 3 files changed, 38 insertions(+) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index c2377b42c..6f4add4b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1177,6 +1177,22 @@ static int sp_process_file_list(GSList *fl) g_slist_free (selected); g_slist_free (to_select); } + if(sp_export_id) { + doc->ensureUpToDate(); + + // "crop" the document to the specified object, cleaning as we go. + SPObject *obj = doc->getObjectById(sp_export_id); + Geom::OptRect const bbox(SP_ITEM(obj)->visualBounds()); + + if (bbox) { + doc->fitToRect(*bbox, false); + } + + if (sp_export_id_only) { + // If -j then remove all other objects to complete the "crop" + doc->getRoot()->cropToObject(obj); + } + } Inkscape::Extension::save(Inkscape::Extension::db.get("org.inkscape.output.svg.plain"), doc, sp_export_svg, false, false, false, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY); diff --git a/src/sp-object.cpp b/src/sp-object.cpp index be3a1ab9d..c904705fb 100644 --- a/src/sp-object.cpp +++ b/src/sp-object.cpp @@ -460,6 +460,23 @@ void SPObject::deleteObject(bool propagate, bool propagate_descendants) sp_object_unref(this, NULL); } +void SPObject::cropToObject(SPObject *except) +{ + GSList *toDelete = NULL; + for ( SPObject *child = this->firstChild(); child; child = child->getNext() ) { + if (SP_IS_ITEM(child)) { + if (child->isAncestorOf(except)) { + child->cropToObject(except); + } else if(child != except) { + toDelete = g_slist_append(toDelete, child); + } + } + } + for (GSList *item = toDelete; item; item = item->next) { + SP_OBJECT(item->data)->deleteObject(true, true); + } +} + void SPObject::attach(SPObject *object, SPObject *prev) { //g_return_if_fail(parent != NULL); diff --git a/src/sp-object.h b/src/sp-object.h index cf18d4523..9ce5629e6 100644 --- a/src/sp-object.h +++ b/src/sp-object.h @@ -464,6 +464,11 @@ public: deleteObject(propagate, propagate); } + /** + * Removes all children except for the given object, it's children and it's ancesstors. + */ + void cropToObject(SPObject *except); + /** * Connects a slot to be called when an object is deleted. * -- cgit v1.2.3