From 9a0740b27fcad7f06bae507e1957f4bfeb4f9dc7 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Sun, 5 Oct 2014 12:58:19 -0400 Subject: Rename marker.cpp -> sp-marker.cpp to reflect inclusion in SP-tree (bzr r13341.1.248) --- src/Makefile_insert | 2 +- src/extension/internal/cairo-renderer.cpp | 2 +- src/helper/stock-items.cpp | 2 +- src/marker.cpp | 541 ------------------------------ src/marker.h | 99 ------ src/selection-chemistry.cpp | 2 +- src/sp-marker.cpp | 541 ++++++++++++++++++++++++++++++ src/sp-marker.h | 99 ++++++ src/sp-shape.cpp | 2 +- src/splivarot.cpp | 2 +- src/widgets/stroke-marker-selector.cpp | 2 +- src/widgets/stroke-style.h | 2 +- 12 files changed, 648 insertions(+), 648 deletions(-) delete mode 100644 src/marker.cpp delete mode 100644 src/marker.h create mode 100644 src/sp-marker.cpp create mode 100644 src/sp-marker.h (limited to 'src') diff --git a/src/Makefile_insert b/src/Makefile_insert index 47ea28ebc..f8f0ac122 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -85,7 +85,6 @@ ink_common_sources += \ line-snapper.cpp line-snapper.h \ macros.h \ main-cmdlineact.cpp main-cmdlineact.h \ - marker.cpp marker.h \ media.cpp media.h \ menus-skeleton.h \ message-context.cpp message-context.h \ @@ -167,6 +166,7 @@ ink_common_sources += \ sp-line.cpp sp-line.h \ splivarot.cpp splivarot.h \ sp-lpe-item.cpp sp-lpe-item.h \ + sp-marker.cpp sp-marker.h \ sp-marker-loc.h \ sp-mask.cpp sp-mask.h \ sp-metadata.cpp sp-metadata.h \ diff --git a/src/extension/internal/cairo-renderer.cpp b/src/extension/internal/cairo-renderer.cpp index 0fec68c06..8196e34fb 100644 --- a/src/extension/internal/cairo-renderer.cpp +++ b/src/extension/internal/cairo-renderer.cpp @@ -42,7 +42,7 @@ #include "sp-item.h" #include "sp-item-group.h" #include "style.h" -#include "marker.h" +#include "sp-marker.h" #include "sp-linear-gradient.h" #include "sp-radial-gradient.h" #include "sp-root.h" diff --git a/src/helper/stock-items.cpp b/src/helper/stock-items.cpp index a12fa377a..582dcd2a8 100644 --- a/src/helper/stock-items.cpp +++ b/src/helper/stock-items.cpp @@ -28,7 +28,7 @@ #include "sp-gradient.h" #include "document-private.h" #include "sp-pattern.h" -#include "marker.h" +#include "sp-marker.h" #include "desktop-handles.h" #include "inkscape.h" diff --git a/src/marker.cpp b/src/marker.cpp deleted file mode 100644 index a4cbc30ca..000000000 --- a/src/marker.cpp +++ /dev/null @@ -1,541 +0,0 @@ -/* - * SVG implementation - * - * Authors: - * Lauris Kaplinski - * Bryce Harrington - * Abhishek Sharma - * Jon A. Cruz - * - * Copyright (C) 1999-2003 Lauris Kaplinski - * 2004-2006 Bryce Harrington - * 2008 Johan Engelen - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#include -#include -#include "config.h" - -#include <2geom/affine.h> -#include <2geom/transforms.h> -#include "svg/svg.h" -#include "display/drawing-group.h" -#include "xml/repr.h" -#include "attributes.h" -#include "marker.h" -#include "document.h" -#include "document-private.h" -#include "preferences.h" - -struct SPMarkerView { - SPMarkerView *next; - unsigned int key; - std::vector items; -}; - -static void sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems); - -#include "sp-factory.h" - -namespace { - SPObject* createMarker() { - return new SPMarker(); - } - - bool markerRegistered = SPFactory::instance().registerObject("svg:marker", createMarker); -} - -SPMarker::SPMarker() : SPGroup(), SPViewBox() { - - this->markerUnits = 0; - this->markerUnits_set = 0; - - this->orient_mode = MARKER_ORIENT_ANGLE; - this->orient_set = 0; - this->orient = 0; - - this->views = NULL; -} - -/** - * Initializes an SPMarker object. This notes the marker's viewBox is - * not set and initializes the marker's c2p identity matrix. - */ - -SPMarker::~SPMarker() { -} - -/** - * Virtual build callback for SPMarker. - * - * This is to be invoked immediately after creation of an SPMarker. This - * method fills an SPMarker object with its SVG attributes, and calls the - * parent class' build routine to attach the object to its document and - * repr. The result will be creation of the whole document tree. - * - * \see SPObject::build() - */ -void SPMarker::build(SPDocument *document, Inkscape::XML::Node *repr) { - this->readAttr( "markerUnits" ); - this->readAttr( "refX" ); - this->readAttr( "refY" ); - this->readAttr( "markerWidth" ); - this->readAttr( "markerHeight" ); - this->readAttr( "orient" ); - this->readAttr( "viewBox" ); - this->readAttr( "preserveAspectRatio" ); - - SPGroup::build(document, repr); -} - -void SPMarker::release() { - while (this->views) { - // Destroy all DrawingItems etc. - // Parent class ::hide method - //reinterpret_cast(parent_class)->hide(marker, marker->views->key); - // CPPIFY: correct one? - SPGroup::hide(this->views->key); - - - sp_marker_view_remove (this, this->views, TRUE); - } - - SPGroup::release(); -} - -/** - * Removes, releases and unrefs all children of object - * - * This is the inverse of sp_marker_build(). It must be invoked as soon - * as the marker is removed from the tree, even if it is still referenced - * by other objects. It hides and removes any views of the marker, then - * calls the parent classes' release function to deregister the object - * and release its SPRepr bindings. The result will be the destruction - * of the entire document tree. - * - * \see SPObject::release() - */ - -void SPMarker::set(unsigned int key, const gchar* value) { - switch (key) { - case SP_ATTR_MARKERUNITS: - this->markerUnits_set = FALSE; - this->markerUnits = SP_MARKER_UNITS_STROKEWIDTH; - - if (value) { - if (!strcmp (value, "strokeWidth")) { - this->markerUnits_set = TRUE; - } else if (!strcmp (value, "userSpaceOnUse")) { - this->markerUnits = SP_MARKER_UNITS_USERSPACEONUSE; - this->markerUnits_set = TRUE; - } - } - - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG); - break; - - case SP_ATTR_REFX: - this->refX.readOrUnset(value); - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); - break; - - case SP_ATTR_REFY: - this->refY.readOrUnset(value); - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); - break; - - case SP_ATTR_MARKERWIDTH: - this->markerWidth.readOrUnset(value, SVGLength::NONE, 3.0, 3.0); - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); - break; - - case SP_ATTR_MARKERHEIGHT: - this->markerHeight.readOrUnset(value, SVGLength::NONE, 3.0, 3.0); - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); - break; - - case SP_ATTR_ORIENT: - this->orient_set = FALSE; - this->orient_mode = MARKER_ORIENT_ANGLE; - this->orient = 0.0; - - if (value) { - if (!strcmp (value, "auto")) { - this->orient_mode = MARKER_ORIENT_AUTO; - this->orient_set = TRUE; - } else if (!strcmp (value, "auto-start-reverse")) { - this->orient_mode = MARKER_ORIENT_AUTO_START_REVERSE; - this->orient_set = TRUE; - } else if (sp_svg_number_read_f (value, &this->orient)) { - this->orient_mode = MARKER_ORIENT_ANGLE; - this->orient_set = TRUE; - } - } - - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); - break; - - case SP_ATTR_VIEWBOX: - set_viewBox( value ); - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG); - break; - - case SP_ATTR_PRESERVEASPECTRATIO: - set_preserveAspectRatio( value ); - this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG); - break; - - default: - SPGroup::set(key, value); - break; - } -} - -void SPMarker::update(SPCtx *ctx, guint flags) { - - SPItemCtx ictx; - - // Copy parent context - ictx.flags = ctx->flags; - - // Initialize transformations - ictx.i2doc = Geom::identity(); - ictx.i2vp = Geom::identity(); - - // Set up viewport - ictx.viewport = Geom::Rect::from_xywh(0, 0, this->markerWidth.computed, this->markerHeight.computed); - - SPItemCtx rctx = get_rctx( &ictx ); - - // Shift according to refX, refY - Geom::Point ref( this->refX.computed, this->refY.computed ); - ref *= c2p; - this->c2p = this->c2p * Geom::Translate( -ref ); - - // And invoke parent method - SPGroup::update((SPCtx *) &rctx, flags); - - // As last step set additional transform of drawing group - for (SPMarkerView *v = this->views; v != NULL; v = v->next) { - for (unsigned i = 0 ; i < v->items.size() ; i++) { - if (v->items[i]) { - Inkscape::DrawingGroup *g = dynamic_cast(v->items[i]); - g->setChildTransform(this->c2p); - } - } - } -} - -Inkscape::XML::Node* SPMarker::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) { - if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { - repr = xml_doc->createElement("svg:marker"); - } - - if (this->markerUnits_set) { - if (this->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) { - repr->setAttribute("markerUnits", "strokeWidth"); - } else { - repr->setAttribute("markerUnits", "userSpaceOnUse"); - } - } else { - repr->setAttribute("markerUnits", NULL); - } - - if (this->refX._set) { - sp_repr_set_svg_double(repr, "refX", this->refX.computed); - } else { - repr->setAttribute("refX", NULL); - } - - if (this->refY._set) { - sp_repr_set_svg_double (repr, "refY", this->refY.computed); - } else { - repr->setAttribute("refY", NULL); - } - - if (this->markerWidth._set) { - sp_repr_set_svg_double (repr, "markerWidth", this->markerWidth.computed); - } else { - repr->setAttribute("markerWidth", NULL); - } - - if (this->markerHeight._set) { - sp_repr_set_svg_double (repr, "markerHeight", this->markerHeight.computed); - } else { - repr->setAttribute("markerHeight", NULL); - } - - if (this->orient_set) { - if (this->orient_mode == MARKER_ORIENT_AUTO) { - repr->setAttribute("orient", "auto"); - } else if (this->orient_mode == MARKER_ORIENT_AUTO_START_REVERSE) { - repr->setAttribute("orient", "auto-start-reverse"); - } else { - sp_repr_set_css_double(repr, "orient", this->orient); - } - } else { - repr->setAttribute("orient", NULL); - } - - /* fixme: */ - //XML Tree being used directly here while it shouldn't be.... - repr->setAttribute("viewBox", this->getRepr()->attribute("viewBox")); - //XML Tree being used directly here while it shouldn't be.... - repr->setAttribute("preserveAspectRatio", this->getRepr()->attribute("preserveAspectRatio")); - - SPGroup::write(xml_doc, repr, flags); - - return repr; -} - -Inkscape::DrawingItem* SPMarker::show(Inkscape::Drawing &/*drawing*/, unsigned int /*key*/, unsigned int /*flags*/) { - // Markers in tree are never shown directly even if outside of . - return 0; -} - -Inkscape::DrawingItem* SPMarker::private_show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags) { - return SPGroup::show(drawing, key, flags); -} - -void SPMarker::hide(unsigned int key) { - // CPPIFY: correct? - SPGroup::hide(key); -} - -Geom::OptRect SPMarker::bbox(Geom::Affine const &/*transform*/, SPItem::BBoxType /*type*/) const { - return Geom::OptRect(); -} - -void SPMarker::print(SPPrintContext* /*ctx*/) { - -} - -/* fixme: Remove link if zero-sized (Lauris) */ - -/** - * Removes any SPMarkerViews that a marker has with a specific key. - * Set up the DrawingItem array's size in the specified SPMarker's SPMarkerView. - * This is called from sp_shape_update() for shapes that have markers. It - * removes the old view of the marker and establishes a new one, registering - * it with the marker's list of views for future updates. - * - * \param marker Marker to create views in. - * \param key Key to give each SPMarkerView. - * \param size Number of DrawingItems to put in the SPMarkerView. - */ -void -sp_marker_show_dimension (SPMarker *marker, unsigned int key, unsigned int size) -{ - SPMarkerView *view; - - for (view = marker->views; view != NULL; view = view->next) { - if (view->key == key) break; - } - if (view && (view->items.size() != size)) { - /* Free old view and allocate new */ - /* Parent class ::hide method */ - marker->hide(key); - - sp_marker_view_remove (marker, view, TRUE); - view = NULL; - } - if (!view) { - view = new SPMarkerView(); - view->items.clear(); - for (unsigned int i = 0; i < size; i++) { - view->items.push_back(NULL); - } - view->next = marker->views; - marker->views = view; - view->key = key; - } -} - -/** - * Shows an instance of a marker. This is called during sp_shape_update_marker_view() - * show and transform a child item in the drawing for all views with the given key. - */ -Inkscape::DrawingItem * -sp_marker_show_instance ( SPMarker *marker, Inkscape::DrawingItem *parent, - unsigned int key, unsigned int pos, - Geom::Affine const &base, float linewidth) -{ - // do not show marker if linewidth == 0 and markerUnits == strokeWidth - // otherwise Cairo will fail to render anything on the tile - // that contains the "degenerate" marker - if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH && linewidth == 0) { - return NULL; - } - - for (SPMarkerView *v = marker->views; v != NULL; v = v->next) { - if (v->key == key) { - if (pos >= v->items.size()) { - return NULL; - } - if (!v->items[pos]) { - /* Parent class ::show method */ - v->items[pos] = marker->private_show(parent->drawing(), key, SP_ITEM_REFERENCE_FLAGS); - - if (v->items[pos]) { - /* fixme: Position (Lauris) */ - parent->prependChild(v->items[pos]); - Inkscape::DrawingGroup *g = dynamic_cast(v->items[pos]); - if (g) g->setChildTransform(marker->c2p); - } - } - if (v->items[pos]) { - Geom::Affine m; - if (marker->orient_mode == MARKER_ORIENT_AUTO) { - m = base; - } else if (marker->orient_mode == MARKER_ORIENT_AUTO_START_REVERSE) { - m = Geom::Rotate::from_degrees( 180.0 ) * base; - m = base; - } else { - /* fixme: Orient units (Lauris) */ - m = Geom::Rotate::from_degrees(marker->orient); - m *= Geom::Translate(base.translation()); - } - if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) { - m = Geom::Scale(linewidth) * m; - } - v->items[pos]->setTransform(m); - } - return v->items[pos]; - } - } - - return NULL; -} - -/** - * Hides/removes all views of the given marker that have key 'key'. - * This replaces SPItem implementation because we have our own views - * \param key SPMarkerView key to hide. - */ -void -sp_marker_hide (SPMarker *marker, unsigned int key) -{ - SPMarkerView *v; - - v = marker->views; - while (v != NULL) { - SPMarkerView *next; - next = v->next; - if (v->key == key) { - /* Parent class ::hide method */ - marker->hide(key); - - sp_marker_view_remove (marker, v, TRUE); - return; - } - v = next; - } -} - -/** - * Removes a given view. Also will destroy sub-items in the view if destroyitems - * is set to a non-zero value. - */ -static void -sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems) -{ - if (view == marker->views) { - marker->views = view->next; - } else { - SPMarkerView *v; - for (v = marker->views; v->next != view; v = v->next) if (!v->next) return; - v->next = view->next; - } - if (destroyitems) { - for (unsigned int i = 0; i < view->items.size(); i++) { - /* We have to walk through the whole array because there may be hidden items */ - delete view->items[i]; - } - } - view->items.clear(); - delete view; -} - -const gchar *generate_marker(GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Point center, Geom::Affine move) -{ - Inkscape::XML::Document *xml_doc = document->getReprDoc(); - Inkscape::XML::Node *defsrepr = document->getDefs()->getRepr(); - - Inkscape::XML::Node *repr = xml_doc->createElement("svg:marker"); - - // Uncommenting this will make the marker fixed-size independent of stroke width. - // Commented out for consistency with standard markers which scale when you change - // stroke width: - //repr->setAttribute("markerUnits", "userSpaceOnUse"); - - sp_repr_set_svg_double(repr, "markerWidth", bounds.dimensions()[Geom::X]); - sp_repr_set_svg_double(repr, "markerHeight", bounds.dimensions()[Geom::Y]); - sp_repr_set_svg_double(repr, "refX", center[Geom::X]); - sp_repr_set_svg_double(repr, "refY", center[Geom::Y]); - - repr->setAttribute("orient", "auto"); - - defsrepr->appendChild(repr); - const gchar *mark_id = repr->attribute("id"); - SPObject *mark_object = document->getObjectById(mark_id); - - for (GSList *i = reprs; i != NULL; i = i->next) { - Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data); - SPItem *copy = SP_ITEM(mark_object->appendChildRepr(node)); - - Geom::Affine dup_transform; - if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform)) - dup_transform = Geom::identity(); - dup_transform *= move; - - copy->doWriteTransform(copy->getRepr(), dup_transform); - } - - Inkscape::GC::release(repr); - return mark_id; -} - -SPObject *sp_marker_fork_if_necessary(SPObject *marker) -{ - if (marker->hrefcount < 2) { - return marker; - } - - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - gboolean colorStock = prefs->getBool("/options/markers/colorStockMarkers", true); - gboolean colorCustom = prefs->getBool("/options/markers/colorCustomMarkers", false); - const gchar *stock = marker->getRepr()->attribute("inkscape:isstock"); - gboolean isStock = (!stock || !strcmp(stock,"true")); - - if (isStock ? !colorStock : !colorCustom) { - return marker; - } - - SPDocument *doc = marker->document; - Inkscape::XML::Document *xml_doc = doc->getReprDoc(); - // Turn off garbage-collectable or it might be collected before we can use it - marker->getRepr()->setAttribute("inkscape:collect", NULL); - Inkscape::XML::Node *mark_repr = marker->getRepr()->duplicate(xml_doc); - doc->getDefs()->getRepr()->addChild(mark_repr, NULL); - if (!mark_repr->attribute("inkscape:stockid")) { - mark_repr->setAttribute("inkscape:stockid", mark_repr->attribute("id")); - } - marker->getRepr()->setAttribute("inkscape:collect", "always"); - - SPObject *marker_new = static_cast(doc->getObjectByRepr(mark_repr)); - Inkscape::GC::release(mark_repr); - return marker_new; -} - -/* - Local Variables: - mode:c++ - c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) - indent-tabs-mode:nil - fill-column:99 - End: -*/ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : diff --git a/src/marker.h b/src/marker.h deleted file mode 100644 index b58523251..000000000 --- a/src/marker.h +++ /dev/null @@ -1,99 +0,0 @@ -#ifndef SEEN_SP_MARKER_H -#define SEEN_SP_MARKER_H - -/* - * SVG implementation - * - * Authors: - * Lauris Kaplinski - * - * Copyright (C) 1999-2003 Lauris Kaplinski - * Copyright (C) 2008 Johan Engelen - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ -/* - * This is quite similar in logic to - * Maybe we should merge them somehow (Lauris) - */ - -#define SP_TYPE_MARKER (sp_marker_get_type ()) -#define SP_MARKER(obj) (dynamic_cast((SPObject*)obj)) -#define SP_IS_MARKER(obj) (dynamic_cast((SPObject*)obj) != NULL) - -struct SPMarkerView; - -#include <2geom/rect.h> -#include <2geom/affine.h> - -#include "enums.h" -#include "svg/svg-length.h" -#include "sp-item-group.h" -#include "uri-references.h" -#include "viewbox.h" - -enum markerOrient { - MARKER_ORIENT_ANGLE, - MARKER_ORIENT_AUTO, - MARKER_ORIENT_AUTO_START_REVERSE -}; - -class SPMarker : public SPGroup, public SPViewBox { -public: - SPMarker(); - virtual ~SPMarker(); - - /* units */ - unsigned int markerUnits_set : 1; - unsigned int markerUnits : 1; - - /* reference point */ - SVGLength refX; - SVGLength refY; - - /* dimensions */ - SVGLength markerWidth; - SVGLength markerHeight; - - /* orient */ - unsigned int orient_set : 1; - markerOrient orient_mode : 2; - float orient; - - /* Private views */ - SPMarkerView *views; - - virtual void build(SPDocument *document, Inkscape::XML::Node *repr); - virtual void release(); - virtual void set(unsigned int key, gchar const* value); - virtual void update(SPCtx *ctx, guint flags); - virtual Inkscape::XML::Node* write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags); - - virtual Inkscape::DrawingItem* show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags); - virtual Inkscape::DrawingItem* private_show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags); - virtual void hide(unsigned int key); - - virtual Geom::OptRect bbox(Geom::Affine const &transform, SPItem::BBoxType type) const; - virtual void print(SPPrintContext *ctx); -}; - -class SPMarkerReference : public Inkscape::URIReference { - SPMarkerReference(SPObject *obj) : URIReference(obj) {} - SPMarker *getObject() const { - return static_cast(URIReference::getObject()); - } -protected: - virtual bool _acceptObject(SPObject *obj) const { - return SP_IS_MARKER(obj); - } -}; - -void sp_marker_show_dimension (SPMarker *marker, unsigned int key, unsigned int size); -Inkscape::DrawingItem *sp_marker_show_instance (SPMarker *marker, Inkscape::DrawingItem *parent, - unsigned int key, unsigned int pos, - Geom::Affine const &base, float linewidth); -void sp_marker_hide (SPMarker *marker, unsigned int key); -const char *generate_marker (GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Point center, Geom::Affine move); -SPObject *sp_marker_fork_if_necessary(SPObject *marker); - -#endif diff --git a/src/selection-chemistry.cpp b/src/selection-chemistry.cpp index 956e1719c..b87bc421f 100644 --- a/src/selection-chemistry.cpp +++ b/src/selection-chemistry.cpp @@ -41,7 +41,7 @@ SPCycleType SP_CYCLING = SP_CYCLE_FOCUS; #include "desktop-handles.h" #include "message-stack.h" #include "sp-item-transform.h" -#include "marker.h" +#include "sp-marker.h" #include "sp-use.h" #include "sp-textpath.h" #include "sp-tspan.h" diff --git a/src/sp-marker.cpp b/src/sp-marker.cpp new file mode 100644 index 000000000..e955594ab --- /dev/null +++ b/src/sp-marker.cpp @@ -0,0 +1,541 @@ +/* + * SVG implementation + * + * Authors: + * Lauris Kaplinski + * Bryce Harrington + * Abhishek Sharma + * Jon A. Cruz + * + * Copyright (C) 1999-2003 Lauris Kaplinski + * 2004-2006 Bryce Harrington + * 2008 Johan Engelen + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include +#include +#include "config.h" + +#include <2geom/affine.h> +#include <2geom/transforms.h> +#include "svg/svg.h" +#include "display/drawing-group.h" +#include "xml/repr.h" +#include "attributes.h" +#include "sp-marker.h" +#include "document.h" +#include "document-private.h" +#include "preferences.h" + +struct SPMarkerView { + SPMarkerView *next; + unsigned int key; + std::vector items; +}; + +static void sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems); + +#include "sp-factory.h" + +namespace { + SPObject* createMarker() { + return new SPMarker(); + } + + bool markerRegistered = SPFactory::instance().registerObject("svg:marker", createMarker); +} + +SPMarker::SPMarker() : SPGroup(), SPViewBox() { + + this->markerUnits = 0; + this->markerUnits_set = 0; + + this->orient_mode = MARKER_ORIENT_ANGLE; + this->orient_set = 0; + this->orient = 0; + + this->views = NULL; +} + +/** + * Initializes an SPMarker object. This notes the marker's viewBox is + * not set and initializes the marker's c2p identity matrix. + */ + +SPMarker::~SPMarker() { +} + +/** + * Virtual build callback for SPMarker. + * + * This is to be invoked immediately after creation of an SPMarker. This + * method fills an SPMarker object with its SVG attributes, and calls the + * parent class' build routine to attach the object to its document and + * repr. The result will be creation of the whole document tree. + * + * \see SPObject::build() + */ +void SPMarker::build(SPDocument *document, Inkscape::XML::Node *repr) { + this->readAttr( "markerUnits" ); + this->readAttr( "refX" ); + this->readAttr( "refY" ); + this->readAttr( "markerWidth" ); + this->readAttr( "markerHeight" ); + this->readAttr( "orient" ); + this->readAttr( "viewBox" ); + this->readAttr( "preserveAspectRatio" ); + + SPGroup::build(document, repr); +} + +void SPMarker::release() { + while (this->views) { + // Destroy all DrawingItems etc. + // Parent class ::hide method + //reinterpret_cast(parent_class)->hide(marker, marker->views->key); + // CPPIFY: correct one? + SPGroup::hide(this->views->key); + + + sp_marker_view_remove (this, this->views, TRUE); + } + + SPGroup::release(); +} + +/** + * Removes, releases and unrefs all children of object + * + * This is the inverse of sp_marker_build(). It must be invoked as soon + * as the marker is removed from the tree, even if it is still referenced + * by other objects. It hides and removes any views of the marker, then + * calls the parent classes' release function to deregister the object + * and release its SPRepr bindings. The result will be the destruction + * of the entire document tree. + * + * \see SPObject::release() + */ + +void SPMarker::set(unsigned int key, const gchar* value) { + switch (key) { + case SP_ATTR_MARKERUNITS: + this->markerUnits_set = FALSE; + this->markerUnits = SP_MARKER_UNITS_STROKEWIDTH; + + if (value) { + if (!strcmp (value, "strokeWidth")) { + this->markerUnits_set = TRUE; + } else if (!strcmp (value, "userSpaceOnUse")) { + this->markerUnits = SP_MARKER_UNITS_USERSPACEONUSE; + this->markerUnits_set = TRUE; + } + } + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG); + break; + + case SP_ATTR_REFX: + this->refX.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + break; + + case SP_ATTR_REFY: + this->refY.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + break; + + case SP_ATTR_MARKERWIDTH: + this->markerWidth.readOrUnset(value, SVGLength::NONE, 3.0, 3.0); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + break; + + case SP_ATTR_MARKERHEIGHT: + this->markerHeight.readOrUnset(value, SVGLength::NONE, 3.0, 3.0); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + break; + + case SP_ATTR_ORIENT: + this->orient_set = FALSE; + this->orient_mode = MARKER_ORIENT_ANGLE; + this->orient = 0.0; + + if (value) { + if (!strcmp (value, "auto")) { + this->orient_mode = MARKER_ORIENT_AUTO; + this->orient_set = TRUE; + } else if (!strcmp (value, "auto-start-reverse")) { + this->orient_mode = MARKER_ORIENT_AUTO_START_REVERSE; + this->orient_set = TRUE; + } else if (sp_svg_number_read_f (value, &this->orient)) { + this->orient_mode = MARKER_ORIENT_ANGLE; + this->orient_set = TRUE; + } + } + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + break; + + case SP_ATTR_VIEWBOX: + set_viewBox( value ); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG); + break; + + case SP_ATTR_PRESERVEASPECTRATIO: + set_preserveAspectRatio( value ); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG); + break; + + default: + SPGroup::set(key, value); + break; + } +} + +void SPMarker::update(SPCtx *ctx, guint flags) { + + SPItemCtx ictx; + + // Copy parent context + ictx.flags = ctx->flags; + + // Initialize transformations + ictx.i2doc = Geom::identity(); + ictx.i2vp = Geom::identity(); + + // Set up viewport + ictx.viewport = Geom::Rect::from_xywh(0, 0, this->markerWidth.computed, this->markerHeight.computed); + + SPItemCtx rctx = get_rctx( &ictx ); + + // Shift according to refX, refY + Geom::Point ref( this->refX.computed, this->refY.computed ); + ref *= c2p; + this->c2p = this->c2p * Geom::Translate( -ref ); + + // And invoke parent method + SPGroup::update((SPCtx *) &rctx, flags); + + // As last step set additional transform of drawing group + for (SPMarkerView *v = this->views; v != NULL; v = v->next) { + for (unsigned i = 0 ; i < v->items.size() ; i++) { + if (v->items[i]) { + Inkscape::DrawingGroup *g = dynamic_cast(v->items[i]); + g->setChildTransform(this->c2p); + } + } + } +} + +Inkscape::XML::Node* SPMarker::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) { + if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { + repr = xml_doc->createElement("svg:marker"); + } + + if (this->markerUnits_set) { + if (this->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) { + repr->setAttribute("markerUnits", "strokeWidth"); + } else { + repr->setAttribute("markerUnits", "userSpaceOnUse"); + } + } else { + repr->setAttribute("markerUnits", NULL); + } + + if (this->refX._set) { + sp_repr_set_svg_double(repr, "refX", this->refX.computed); + } else { + repr->setAttribute("refX", NULL); + } + + if (this->refY._set) { + sp_repr_set_svg_double (repr, "refY", this->refY.computed); + } else { + repr->setAttribute("refY", NULL); + } + + if (this->markerWidth._set) { + sp_repr_set_svg_double (repr, "markerWidth", this->markerWidth.computed); + } else { + repr->setAttribute("markerWidth", NULL); + } + + if (this->markerHeight._set) { + sp_repr_set_svg_double (repr, "markerHeight", this->markerHeight.computed); + } else { + repr->setAttribute("markerHeight", NULL); + } + + if (this->orient_set) { + if (this->orient_mode == MARKER_ORIENT_AUTO) { + repr->setAttribute("orient", "auto"); + } else if (this->orient_mode == MARKER_ORIENT_AUTO_START_REVERSE) { + repr->setAttribute("orient", "auto-start-reverse"); + } else { + sp_repr_set_css_double(repr, "orient", this->orient); + } + } else { + repr->setAttribute("orient", NULL); + } + + /* fixme: */ + //XML Tree being used directly here while it shouldn't be.... + repr->setAttribute("viewBox", this->getRepr()->attribute("viewBox")); + //XML Tree being used directly here while it shouldn't be.... + repr->setAttribute("preserveAspectRatio", this->getRepr()->attribute("preserveAspectRatio")); + + SPGroup::write(xml_doc, repr, flags); + + return repr; +} + +Inkscape::DrawingItem* SPMarker::show(Inkscape::Drawing &/*drawing*/, unsigned int /*key*/, unsigned int /*flags*/) { + // Markers in tree are never shown directly even if outside of . + return 0; +} + +Inkscape::DrawingItem* SPMarker::private_show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags) { + return SPGroup::show(drawing, key, flags); +} + +void SPMarker::hide(unsigned int key) { + // CPPIFY: correct? + SPGroup::hide(key); +} + +Geom::OptRect SPMarker::bbox(Geom::Affine const &/*transform*/, SPItem::BBoxType /*type*/) const { + return Geom::OptRect(); +} + +void SPMarker::print(SPPrintContext* /*ctx*/) { + +} + +/* fixme: Remove link if zero-sized (Lauris) */ + +/** + * Removes any SPMarkerViews that a marker has with a specific key. + * Set up the DrawingItem array's size in the specified SPMarker's SPMarkerView. + * This is called from sp_shape_update() for shapes that have markers. It + * removes the old view of the marker and establishes a new one, registering + * it with the marker's list of views for future updates. + * + * \param marker Marker to create views in. + * \param key Key to give each SPMarkerView. + * \param size Number of DrawingItems to put in the SPMarkerView. + */ +void +sp_marker_show_dimension (SPMarker *marker, unsigned int key, unsigned int size) +{ + SPMarkerView *view; + + for (view = marker->views; view != NULL; view = view->next) { + if (view->key == key) break; + } + if (view && (view->items.size() != size)) { + /* Free old view and allocate new */ + /* Parent class ::hide method */ + marker->hide(key); + + sp_marker_view_remove (marker, view, TRUE); + view = NULL; + } + if (!view) { + view = new SPMarkerView(); + view->items.clear(); + for (unsigned int i = 0; i < size; i++) { + view->items.push_back(NULL); + } + view->next = marker->views; + marker->views = view; + view->key = key; + } +} + +/** + * Shows an instance of a marker. This is called during sp_shape_update_marker_view() + * show and transform a child item in the drawing for all views with the given key. + */ +Inkscape::DrawingItem * +sp_marker_show_instance ( SPMarker *marker, Inkscape::DrawingItem *parent, + unsigned int key, unsigned int pos, + Geom::Affine const &base, float linewidth) +{ + // do not show marker if linewidth == 0 and markerUnits == strokeWidth + // otherwise Cairo will fail to render anything on the tile + // that contains the "degenerate" marker + if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH && linewidth == 0) { + return NULL; + } + + for (SPMarkerView *v = marker->views; v != NULL; v = v->next) { + if (v->key == key) { + if (pos >= v->items.size()) { + return NULL; + } + if (!v->items[pos]) { + /* Parent class ::show method */ + v->items[pos] = marker->private_show(parent->drawing(), key, SP_ITEM_REFERENCE_FLAGS); + + if (v->items[pos]) { + /* fixme: Position (Lauris) */ + parent->prependChild(v->items[pos]); + Inkscape::DrawingGroup *g = dynamic_cast(v->items[pos]); + if (g) g->setChildTransform(marker->c2p); + } + } + if (v->items[pos]) { + Geom::Affine m; + if (marker->orient_mode == MARKER_ORIENT_AUTO) { + m = base; + } else if (marker->orient_mode == MARKER_ORIENT_AUTO_START_REVERSE) { + m = Geom::Rotate::from_degrees( 180.0 ) * base; + m = base; + } else { + /* fixme: Orient units (Lauris) */ + m = Geom::Rotate::from_degrees(marker->orient); + m *= Geom::Translate(base.translation()); + } + if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) { + m = Geom::Scale(linewidth) * m; + } + v->items[pos]->setTransform(m); + } + return v->items[pos]; + } + } + + return NULL; +} + +/** + * Hides/removes all views of the given marker that have key 'key'. + * This replaces SPItem implementation because we have our own views + * \param key SPMarkerView key to hide. + */ +void +sp_marker_hide (SPMarker *marker, unsigned int key) +{ + SPMarkerView *v; + + v = marker->views; + while (v != NULL) { + SPMarkerView *next; + next = v->next; + if (v->key == key) { + /* Parent class ::hide method */ + marker->hide(key); + + sp_marker_view_remove (marker, v, TRUE); + return; + } + v = next; + } +} + +/** + * Removes a given view. Also will destroy sub-items in the view if destroyitems + * is set to a non-zero value. + */ +static void +sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems) +{ + if (view == marker->views) { + marker->views = view->next; + } else { + SPMarkerView *v; + for (v = marker->views; v->next != view; v = v->next) if (!v->next) return; + v->next = view->next; + } + if (destroyitems) { + for (unsigned int i = 0; i < view->items.size(); i++) { + /* We have to walk through the whole array because there may be hidden items */ + delete view->items[i]; + } + } + view->items.clear(); + delete view; +} + +const gchar *generate_marker(GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Point center, Geom::Affine move) +{ + Inkscape::XML::Document *xml_doc = document->getReprDoc(); + Inkscape::XML::Node *defsrepr = document->getDefs()->getRepr(); + + Inkscape::XML::Node *repr = xml_doc->createElement("svg:marker"); + + // Uncommenting this will make the marker fixed-size independent of stroke width. + // Commented out for consistency with standard markers which scale when you change + // stroke width: + //repr->setAttribute("markerUnits", "userSpaceOnUse"); + + sp_repr_set_svg_double(repr, "markerWidth", bounds.dimensions()[Geom::X]); + sp_repr_set_svg_double(repr, "markerHeight", bounds.dimensions()[Geom::Y]); + sp_repr_set_svg_double(repr, "refX", center[Geom::X]); + sp_repr_set_svg_double(repr, "refY", center[Geom::Y]); + + repr->setAttribute("orient", "auto"); + + defsrepr->appendChild(repr); + const gchar *mark_id = repr->attribute("id"); + SPObject *mark_object = document->getObjectById(mark_id); + + for (GSList *i = reprs; i != NULL; i = i->next) { + Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data); + SPItem *copy = SP_ITEM(mark_object->appendChildRepr(node)); + + Geom::Affine dup_transform; + if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform)) + dup_transform = Geom::identity(); + dup_transform *= move; + + copy->doWriteTransform(copy->getRepr(), dup_transform); + } + + Inkscape::GC::release(repr); + return mark_id; +} + +SPObject *sp_marker_fork_if_necessary(SPObject *marker) +{ + if (marker->hrefcount < 2) { + return marker; + } + + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + gboolean colorStock = prefs->getBool("/options/markers/colorStockMarkers", true); + gboolean colorCustom = prefs->getBool("/options/markers/colorCustomMarkers", false); + const gchar *stock = marker->getRepr()->attribute("inkscape:isstock"); + gboolean isStock = (!stock || !strcmp(stock,"true")); + + if (isStock ? !colorStock : !colorCustom) { + return marker; + } + + SPDocument *doc = marker->document; + Inkscape::XML::Document *xml_doc = doc->getReprDoc(); + // Turn off garbage-collectable or it might be collected before we can use it + marker->getRepr()->setAttribute("inkscape:collect", NULL); + Inkscape::XML::Node *mark_repr = marker->getRepr()->duplicate(xml_doc); + doc->getDefs()->getRepr()->addChild(mark_repr, NULL); + if (!mark_repr->attribute("inkscape:stockid")) { + mark_repr->setAttribute("inkscape:stockid", mark_repr->attribute("id")); + } + marker->getRepr()->setAttribute("inkscape:collect", "always"); + + SPObject *marker_new = static_cast(doc->getObjectByRepr(mark_repr)); + Inkscape::GC::release(mark_repr); + return marker_new; +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : diff --git a/src/sp-marker.h b/src/sp-marker.h new file mode 100644 index 000000000..b58523251 --- /dev/null +++ b/src/sp-marker.h @@ -0,0 +1,99 @@ +#ifndef SEEN_SP_MARKER_H +#define SEEN_SP_MARKER_H + +/* + * SVG implementation + * + * Authors: + * Lauris Kaplinski + * + * Copyright (C) 1999-2003 Lauris Kaplinski + * Copyright (C) 2008 Johan Engelen + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ +/* + * This is quite similar in logic to + * Maybe we should merge them somehow (Lauris) + */ + +#define SP_TYPE_MARKER (sp_marker_get_type ()) +#define SP_MARKER(obj) (dynamic_cast((SPObject*)obj)) +#define SP_IS_MARKER(obj) (dynamic_cast((SPObject*)obj) != NULL) + +struct SPMarkerView; + +#include <2geom/rect.h> +#include <2geom/affine.h> + +#include "enums.h" +#include "svg/svg-length.h" +#include "sp-item-group.h" +#include "uri-references.h" +#include "viewbox.h" + +enum markerOrient { + MARKER_ORIENT_ANGLE, + MARKER_ORIENT_AUTO, + MARKER_ORIENT_AUTO_START_REVERSE +}; + +class SPMarker : public SPGroup, public SPViewBox { +public: + SPMarker(); + virtual ~SPMarker(); + + /* units */ + unsigned int markerUnits_set : 1; + unsigned int markerUnits : 1; + + /* reference point */ + SVGLength refX; + SVGLength refY; + + /* dimensions */ + SVGLength markerWidth; + SVGLength markerHeight; + + /* orient */ + unsigned int orient_set : 1; + markerOrient orient_mode : 2; + float orient; + + /* Private views */ + SPMarkerView *views; + + virtual void build(SPDocument *document, Inkscape::XML::Node *repr); + virtual void release(); + virtual void set(unsigned int key, gchar const* value); + virtual void update(SPCtx *ctx, guint flags); + virtual Inkscape::XML::Node* write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags); + + virtual Inkscape::DrawingItem* show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags); + virtual Inkscape::DrawingItem* private_show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags); + virtual void hide(unsigned int key); + + virtual Geom::OptRect bbox(Geom::Affine const &transform, SPItem::BBoxType type) const; + virtual void print(SPPrintContext *ctx); +}; + +class SPMarkerReference : public Inkscape::URIReference { + SPMarkerReference(SPObject *obj) : URIReference(obj) {} + SPMarker *getObject() const { + return static_cast(URIReference::getObject()); + } +protected: + virtual bool _acceptObject(SPObject *obj) const { + return SP_IS_MARKER(obj); + } +}; + +void sp_marker_show_dimension (SPMarker *marker, unsigned int key, unsigned int size); +Inkscape::DrawingItem *sp_marker_show_instance (SPMarker *marker, Inkscape::DrawingItem *parent, + unsigned int key, unsigned int pos, + Geom::Affine const &base, float linewidth); +void sp_marker_hide (SPMarker *marker, unsigned int key); +const char *generate_marker (GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Point center, Geom::Affine move); +SPObject *sp_marker_fork_if_necessary(SPObject *marker); + +#endif diff --git a/src/sp-shape.cpp b/src/sp-shape.cpp index d76bd9780..2259088ba 100644 --- a/src/sp-shape.cpp +++ b/src/sp-shape.cpp @@ -35,7 +35,7 @@ #include "print.h" #include "document.h" #include "style.h" -#include "marker.h" +#include "sp-marker.h" #include "sp-path.h" #include "preferences.h" #include "attributes.h" diff --git a/src/splivarot.cpp b/src/splivarot.cpp index 8bb2a7150..1a75a6f29 100644 --- a/src/splivarot.cpp +++ b/src/splivarot.cpp @@ -25,7 +25,7 @@ #include "sp-path.h" #include "sp-shape.h" #include "sp-image.h" -#include "marker.h" +#include "sp-marker.h" #include "enums.h" #include "sp-text.h" #include "sp-flowtext.h" diff --git a/src/widgets/stroke-marker-selector.cpp b/src/widgets/stroke-marker-selector.cpp index 29c342f21..e80e01c22 100644 --- a/src/widgets/stroke-marker-selector.cpp +++ b/src/widgets/stroke-marker-selector.cpp @@ -31,7 +31,7 @@ #include "preferences.h" #include "path-prefix.h" #include "io/sys.h" -#include "marker.h" +#include "sp-marker.h" #include "sp-defs.h" #include "sp-root.h" #include "ui/cache/svg_preview_cache.h" diff --git a/src/widgets/stroke-style.h b/src/widgets/stroke-style.h index 464e7d2d1..15e394097 100644 --- a/src/widgets/stroke-style.h +++ b/src/widgets/stroke-style.h @@ -44,7 +44,7 @@ #include "helper/stock-items.h" #include "inkscape.h" #include "io/sys.h" -#include "marker.h" +#include "sp-marker.h" #include "preferences.h" #include "path-prefix.h" #include "selection.h" -- cgit v1.2.3