sp-filter.cpp revision bbbcfcca035affdf7a344b211a9f53039077c7f7
4070N/A/** \file
4070N/A * SVG <filter> implementation.
6033N/A */
4070N/A/*
6033N/A * Authors:
6033N/A * Hugo Rodrigues <haa.rodrigues@gmail.com>
6033N/A * Niko Kiirala <niko@kiirala.com>
6033N/A * Jon A. Cruz <jon@joncruz.org>
6033N/A * Abhishek Sharma
6033N/A *
4070N/A * Copyright (C) 2006,2007 Authors
6033N/A *
6033N/A * Released under GNU GPL, read the file 'COPYING' for more information
4070N/A */
6033N/A
6033N/A#ifdef HAVE_CONFIG_H
6033N/A# include "config.h"
6033N/A#endif
6033N/A
6033N/A#include <map>
6033N/A#include <string.h>
4070N/Ausing std::map;
4070N/Ausing std::pair;
4070N/A
4070N/A#include <glibmm/stringutils.h>
6033N/A#include "attributes.h"
6033N/A#include "document.h"
6033N/A#include "sp-filter.h"
6033N/A#include "sp-filter-reference.h"
6033N/A#include "sp-filter-primitive.h"
4070N/A#include "uri.h"
4070N/A#include "xml/repr.h"
4070N/A#include <cstring>
4070N/A#include <string>
4070N/A
4070N/A#define SP_MACROS_SILENT
4070N/A#include "macros.h"
4070N/A
4070N/A#include "display/nr-filter.h"
4070N/A
4070N/Astatic void filter_ref_changed(SPObject *old_ref, SPObject *ref, SPFilter *filter);
4070N/Astatic void filter_ref_modified(SPObject *href, guint flags, SPFilter *filter);
4070N/A
4070N/A#include "sp-factory.h"
4070N/A
4070N/Anamespace {
4070N/A SPObject* createFilter() {
4070N/A return new SPFilter();
4070N/A }
4070N/A
4070N/A bool filterRegistered = SPFactory::instance().registerObject("svg:filter", createFilter);
4070N/A}
4070N/A
4070N/ASPFilter::SPFilter()
4070N/A : SPObject(), filterUnits(SP_FILTER_UNITS_OBJECTBOUNDINGBOX), filterUnits_set(FALSE),
4070N/A primitiveUnits(SP_FILTER_UNITS_USERSPACEONUSE), primitiveUnits_set(FALSE),
4070N/A filterRes(NumberOptNumber()),
4070N/A _renderer(NULL), _image_name(new std::map<gchar *, int, ltstr>), _image_number_next(0)
4070N/A{
4070N/A this->href = new SPFilterReference(this);
4070N/A this->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(filter_ref_changed), this));
4070N/A
4070N/A this->x = 0;
4070N/A this->y = 0;
4070N/A this->width = 0;
4070N/A this->height = 0;
4070N/A
4070N/A this->_image_name->clear();
4070N/A}
4070N/A
4070N/ASPFilter::~SPFilter() {
4070N/A}
4070N/A
4070N/A
4070N/A/**
4070N/A * Reads the Inkscape::XML::Node, and initializes SPFilter variables. For this to get called,
4070N/A * our name must be associated with a repr via "sp_object_type_register". Best done through
4070N/A * sp-object-repr.cpp's repr_name_entries array.
4070N/A */
4070N/Avoid SPFilter::build(SPDocument *document, Inkscape::XML::Node *repr) {
4070N/A //Read values of key attributes from XML nodes into object.
4070N/A this->readAttr( "style" ); // struct not derived from SPItem, we need to do this ourselves.
4070N/A this->readAttr( "filterUnits" );
4070N/A this->readAttr( "primitiveUnits" );
4070N/A this->readAttr( "x" );
4070N/A this->readAttr( "y" );
4070N/A this->readAttr( "width" );
4070N/A this->readAttr( "height" );
4070N/A this->readAttr( "filterRes" );
4070N/A this->readAttr( "xlink:href" );
4070N/A
4070N/A SPObject::build(document, repr);
4070N/A
4070N/A//is this necessary?
4070N/A document->addResource("filter", this);
4070N/A}
4070N/A
4070N/A/**
4070N/A * Drops any allocated memory.
4070N/A */
4070N/Avoid SPFilter::release() {
4070N/A if (this->document) {
4070N/A // Unregister ourselves
4070N/A this->document->removeResource("filter", this);
4070N/A }
4070N/A
4070N/A//TODO: release resources here
4070N/A
4070N/A //release href
4070N/A if (this->href) {
4070N/A this->modified_connection.disconnect();
4070N/A this->href->detach();
4070N/A delete this->href;
4070N/A this->href = NULL;
4070N/A }
4070N/A
4070N/A for (map<gchar *, int, ltstr>::const_iterator i = this->_image_name->begin() ; i != this->_image_name->end() ; i++) {
4070N/A g_free(i->first);
4070N/A }
4070N/A
4070N/A delete this->_image_name;
4070N/A
4070N/A SPObject::release();
4070N/A}
4070N/A
4070N/A/**
4070N/A * Sets a specific value in the SPFilter.
6033N/A */
6033N/Avoid SPFilter::set(unsigned int key, gchar const *value) {
4070N/A switch (key) {
4070N/A case SP_ATTR_FILTERUNITS:
6033N/A if (value) {
6033N/A if (!strcmp(value, "userSpaceOnUse")) {
6033N/A this->filterUnits = SP_FILTER_UNITS_USERSPACEONUSE;
4070N/A } else {
6033N/A this->filterUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
}
this->filterUnits_set = TRUE;
} else {
this->filterUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
this->filterUnits_set = FALSE;
}
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_PRIMITIVEUNITS:
if (value) {
if (!strcmp(value, "objectBoundingBox")) {
this->primitiveUnits = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
} else {
this->primitiveUnits = SP_FILTER_UNITS_USERSPACEONUSE;
}
this->primitiveUnits_set = TRUE;
} else {
this->primitiveUnits = SP_FILTER_UNITS_USERSPACEONUSE;
this->primitiveUnits_set = FALSE;
}
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_X:
this->x.readOrUnset(value);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_Y:
this->y.readOrUnset(value);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_WIDTH:
this->width.readOrUnset(value);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_HEIGHT:
this->height.readOrUnset(value);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_FILTERRES:
this->filterRes.set(value);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_XLINK_HREF:
if (value) {
try {
this->href->attach(Inkscape::URI(value));
} catch (Inkscape::BadURIException &e) {
g_warning("%s", e.what());
this->href->detach();
}
} else {
this->href->detach();
}
break;
default:
// See if any parents need this value.
SPObject::set(key, value);
break;
}
}
/**
* Receives update notifications.
*/
void SPFilter::update(SPCtx *ctx, guint flags) {
if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
/* do something to trigger redisplay, updates? */
}
SPObject::update(ctx, flags);
}
/**
* Writes its settings to an incoming repr object, if any.
*/
Inkscape::XML::Node* SPFilter::write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) {
// Original from sp-item-group.cpp
if (flags & SP_OBJECT_WRITE_BUILD) {
if (!repr) {
repr = doc->createElement("svg:this");
}
GSList *l = NULL;
for ( SPObject *child = this->firstChild(); child; child = child->getNext() ) {
Inkscape::XML::Node *crepr = child->updateRepr(doc, NULL, flags);
if (crepr) {
l = g_slist_prepend (l, crepr);
}
}
while (l) {
repr->addChild((Inkscape::XML::Node *) l->data, NULL);
Inkscape::GC::release((Inkscape::XML::Node *) l->data);
l = g_slist_remove (l, l->data);
}
} else {
for ( SPObject *child = this->firstChild() ; child; child = child->getNext() ) {
child->updateRepr(flags);
}
}
if ((flags & SP_OBJECT_WRITE_ALL) || this->filterUnits_set) {
switch (this->filterUnits) {
case SP_FILTER_UNITS_USERSPACEONUSE:
repr->setAttribute("filterUnits", "userSpaceOnUse");
break;
default:
repr->setAttribute("filterUnits", "objectBoundingBox");
break;
}
}
if ((flags & SP_OBJECT_WRITE_ALL) || this->primitiveUnits_set) {
switch (this->primitiveUnits) {
case SP_FILTER_UNITS_OBJECTBOUNDINGBOX:
repr->setAttribute("primitiveUnits", "objectBoundingBox");
break;
default:
repr->setAttribute("primitiveUnits", "userSpaceOnUse");
break;
}
}
if (this->x._set) {
sp_repr_set_svg_double(repr, "x", this->x.computed);
} else {
repr->setAttribute("x", NULL);
}
if (this->y._set) {
sp_repr_set_svg_double(repr, "y", this->y.computed);
} else {
repr->setAttribute("y", NULL);
}
if (this->width._set) {
sp_repr_set_svg_double(repr, "width", this->width.computed);
} else {
repr->setAttribute("width", NULL);
}
if (this->height._set) {
sp_repr_set_svg_double(repr, "height", this->height.computed);
} else {
repr->setAttribute("height", NULL);
}
if (this->filterRes.getNumber()>=0) {
gchar *tmp = this->filterRes.getValueString();
repr->setAttribute("filterRes", tmp);
g_free(tmp);
} else {
repr->setAttribute("filterRes", NULL);
}
if (this->href->getURI()) {
gchar *uri_string = this->href->getURI()->toString();
repr->setAttribute("xlink:href", uri_string);
g_free(uri_string);
}
SPObject::write(doc, repr, flags);
return repr;
}
/**
* Gets called when the filter is (re)attached to another filter.
*/
static void
filter_ref_changed(SPObject *old_ref, SPObject *ref, SPFilter *filter)
{
if (old_ref) {
filter->modified_connection.disconnect();
}
if ( SP_IS_FILTER(ref)
&& ref != filter )
{
filter->modified_connection =
ref->connectModified(sigc::bind(sigc::ptr_fun(&filter_ref_modified), filter));
}
filter_ref_modified(ref, 0, filter);
}
static void filter_ref_modified(SPObject */*href*/, guint /*flags*/, SPFilter *filter)
{
filter->requestModified(SP_OBJECT_MODIFIED_FLAG);
}
/**
* Callback for child_added event.
*/
void SPFilter::child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref) {
SPObject::child_added(child, ref);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
}
/**
* Callback for remove_child event.
*/
void SPFilter::remove_child(Inkscape::XML::Node *child) {
SPObject::remove_child(child);
this->requestModified(SP_OBJECT_MODIFIED_FLAG);
}
void sp_filter_build_renderer(SPFilter *sp_filter, Inkscape::Filters::Filter *nr_filter)
{
g_assert(sp_filter != NULL);
g_assert(nr_filter != NULL);
sp_filter->_renderer = nr_filter;
nr_filter->set_filter_units(sp_filter->filterUnits);
nr_filter->set_primitive_units(sp_filter->primitiveUnits);
nr_filter->set_x(sp_filter->x);
nr_filter->set_y(sp_filter->y);
nr_filter->set_width(sp_filter->width);
nr_filter->set_height(sp_filter->height);
if (sp_filter->filterRes.getNumber() >= 0) {
if (sp_filter->filterRes.getOptNumber() >= 0) {
nr_filter->set_resolution(sp_filter->filterRes.getNumber(),
sp_filter->filterRes.getOptNumber());
} else {
nr_filter->set_resolution(sp_filter->filterRes.getNumber());
}
}
nr_filter->clear_primitives();
SPObject *primitive_obj = sp_filter->children;
while (primitive_obj) {
if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
g_assert(primitive != NULL);
// if (((SPFilterPrimitiveClass*) G_OBJECT_GET_CLASS(primitive))->build_renderer) {
// ((SPFilterPrimitiveClass *) G_OBJECT_GET_CLASS(primitive))->build_renderer(primitive, nr_filter);
// } else {
// g_warning("Cannot build filter renderer: missing builder");
// } // CPPIFY: => FilterPrimitive should be abstract.
primitive->build_renderer(nr_filter);
}
primitive_obj = primitive_obj->next;
}
}
int sp_filter_primitive_count(SPFilter *filter) {
g_assert(filter != NULL);
int count = 0;
SPObject *primitive_obj = filter->children;
while (primitive_obj) {
if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) count++;
primitive_obj = primitive_obj->next;
}
return count;
}
int sp_filter_get_image_name(SPFilter *filter, gchar const *name) {
gchar *name_copy = strdup(name);
map<gchar *, int, ltstr>::iterator result = filter->_image_name->find(name_copy);
free(name_copy);
if (result == filter->_image_name->end()) return -1;
else return (*result).second;
}
int sp_filter_set_image_name(SPFilter *filter, gchar const *name) {
int value = filter->_image_number_next;
filter->_image_number_next++;
gchar *name_copy = strdup(name);
pair<gchar*,int> new_pair(name_copy, value);
pair<map<gchar*,int,ltstr>::iterator,bool> ret = filter->_image_name->insert(new_pair);
if (ret.second == false) {
return (*ret.first).second;
}
return value;
}
gchar const *sp_filter_name_for_image(SPFilter const *filter, int const image) {
switch (image) {
case Inkscape::Filters::NR_FILTER_SOURCEGRAPHIC:
return "SourceGraphic";
break;
case Inkscape::Filters::NR_FILTER_SOURCEALPHA:
return "SourceAlpha";
break;
case Inkscape::Filters::NR_FILTER_BACKGROUNDIMAGE:
return "BackgroundImage";
break;
case Inkscape::Filters::NR_FILTER_BACKGROUNDALPHA:
return "BackgroundAlpha";
break;
case Inkscape::Filters::NR_FILTER_STROKEPAINT:
return "StrokePaint";
break;
case Inkscape::Filters::NR_FILTER_FILLPAINT:
return "FillPaint";
break;
case Inkscape::Filters::NR_FILTER_SLOT_NOT_SET:
case Inkscape::Filters::NR_FILTER_UNNAMED_SLOT:
return 0;
break;
default:
for (map<gchar *, int, ltstr>::const_iterator i
= filter->_image_name->begin() ;
i != filter->_image_name->end() ; ++i) {
if (i->second == image) {
return i->first;
}
}
}
return 0;
}
Glib::ustring sp_filter_get_new_result_name(SPFilter *filter) {
g_assert(filter != NULL);
int largest = 0;
SPObject *primitive_obj = filter->children;
while (primitive_obj) {
if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
Inkscape::XML::Node *repr = primitive_obj->getRepr();
char const *result = repr->attribute("result");
int index;
if (result)
{
if (sscanf(result, "result%5d", &index) == 1)
{
if (index > largest)
{
largest = index;
}
}
}
}
primitive_obj = primitive_obj->next;
}
return "result" + Glib::Ascii::dtostr(largest + 1);
}
bool ltstr::operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
/*
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:fileencoding=utf-8:textwidth=99 :