sp-mask.cpp revision 9916339c54c3b82435984fdfd645c561724fc25c
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#define __SP_MASK_C__
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan/*
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * SVG <mask> implementation
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * Authors:
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * Lauris Kaplinski <lauris@kaplinski.com>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * Copyright (C) 2003 authors
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * Released under GNU GPL, read the file 'COPYING' for more information
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan */
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "display/nr-arena.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "display/nr-arena-group.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include <xml/repr.h>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "enums.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "attributes.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "document.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "document-private.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "sp-item.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "sp-mask.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstruct SPMaskView {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan SPMaskView *next;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan unsigned int key;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NRArenaItem *arenaitem;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NRRect bbox;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan};
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_class_init (SPMaskClass *klass);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_init (SPMask *mask);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_release (SPObject * object);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_set (SPObject *object, unsigned int key, const gchar *value);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_update (SPObject *object, SPCtx *ctx, guint flags);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void sp_mask_modified (SPObject *object, guint flags);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic Inkscape::XML::Node *sp_mask_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark LoganSPMaskView *sp_mask_view_new_prepend (SPMaskView *list, unsigned int key, NRArenaItem *arenaitem);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark LoganSPMaskView *sp_mask_view_list_remove (SPMaskView *list, SPMaskView *view);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic SPObjectGroupClass *parent_class;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark LoganGType
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logansp_mask_get_type (void)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan{
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static GType type = 0;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan if (!type) {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan GTypeInfo info = {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sizeof (SPMaskClass),
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NULL, NULL,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan (GClassInitFunc) sp_mask_class_init,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NULL, NULL,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sizeof (SPMask),
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan 16,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan (GInstanceInitFunc) sp_mask_init,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NULL, /* value_table */
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan };
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan type = g_type_register_static (SP_TYPE_OBJECTGROUP, "SPMask", &info, (GTypeFlags)0);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan }
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan return type;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan}
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logansp_mask_class_init (SPMaskClass *klass)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan{
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan parent_class = (SPObjectGroupClass*) g_type_class_ref (SP_TYPE_OBJECTGROUP);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan SPObjectClass *sp_object_class = (SPObjectClass *) klass;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->build = sp_mask_build;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->release = sp_mask_release;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->set = sp_mask_set;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->child_added = sp_mask_child_added;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->update = sp_mask_update;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->modified = sp_mask_modified;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan sp_object_class->write = sp_mask_write;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan}
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logansp_mask_init (SPMask *mask)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan{
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mask->maskUnits_set = FALSE;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mask->maskUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mask->maskContentUnits_set = FALSE;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mask->maskContentUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mask->display = NULL;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan}
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganstatic void
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logansp_mask_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan{
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan if (((SPObjectClass *) parent_class)->build) {
((SPObjectClass *) parent_class)->build (object, document, repr);
}
sp_object_read_attr (object, "maskUnits");
sp_object_read_attr (object, "maskContentUnits");
/* Register ourselves */
sp_document_add_resource (document, "mask", object);
}
static void
sp_mask_release (SPObject * object)
{
if (SP_OBJECT_DOCUMENT (object)) {
/* Unregister ourselves */
sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "mask", object);
}
SPMask *cp = SP_MASK (object);
while (cp->display) {
/* We simply unref and let item manage this in handler */
cp->display = sp_mask_view_list_remove (cp->display, cp->display);
}
if (((SPObjectClass *) (parent_class))->release) {
((SPObjectClass *) parent_class)->release (object);
}
}
static void
sp_mask_set (SPObject *object, unsigned int key, const gchar *value)
{
SPMask *mask = SP_MASK (object);
switch (key) {
case SP_ATTR_MASKUNITS:
mask->maskUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
mask->maskUnits_set = FALSE;
if (value) {
if (!strcmp (value, "userSpaceOnUse")) {
mask->maskUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
mask->maskUnits_set = TRUE;
} else if (!strcmp (value, "objectBoundingBox")) {
mask->maskUnits_set = TRUE;
}
}
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_MASKCONTENTUNITS:
mask->maskContentUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
mask->maskContentUnits_set = FALSE;
if (value) {
if (!strcmp (value, "userSpaceOnUse")) {
mask->maskContentUnits_set = TRUE;
} else if (!strcmp (value, "objectBoundingBox")) {
mask->maskContentUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
mask->maskContentUnits_set = TRUE;
}
}
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
break;
default:
if (((SPObjectClass *) parent_class)->set)
((SPObjectClass *) parent_class)->set (object, key, value);
break;
}
}
static void
sp_mask_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
{
/* Invoke SPObjectGroup implementation */
((SPObjectClass *) (parent_class))->child_added (object, child, ref);
/* Show new object */
SPObject *ochild = SP_OBJECT_DOCUMENT (object)->getObjectByRepr(child);
if (SP_IS_ITEM (ochild)) {
SPMask *cp = SP_MASK (object);
for (SPMaskView *v = cp->display; v != NULL; v = v->next) {
NRArenaItem *ac = sp_item_invoke_show (SP_ITEM (ochild),
NR_ARENA_ITEM_ARENA (v->arenaitem),
v->key,
SP_ITEM_REFERENCE_FLAGS);
if (ac) {
nr_arena_item_add_child (v->arenaitem, ac, NULL);
nr_arena_item_unref (ac);
}
}
}
}
static void
sp_mask_update (SPObject *object, SPCtx *ctx, guint flags)
{
if (flags & SP_OBJECT_MODIFIED_FLAG) {
flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
}
flags &= SP_OBJECT_MODIFIED_CASCADE;
SPObjectGroup *og = SP_OBJECTGROUP (object);
GSList *l = NULL;
for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
g_object_ref (G_OBJECT (child));
l = g_slist_prepend (l, child);
}
l = g_slist_reverse (l);
while (l) {
SPObject *child = SP_OBJECT (l->data);
l = g_slist_remove (l, child);
if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
child->updateDisplay(ctx, flags);
}
g_object_unref (G_OBJECT (child));
}
SPMask *mask = SP_MASK (object);
for (SPMaskView *v = mask->display; v != NULL; v = v->next) {
if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
NRMatrix t;
nr_matrix_set_scale (&t, v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0);
t.c[4] = v->bbox.x0;
t.c[5] = v->bbox.y0;
nr_arena_group_set_child_transform (NR_ARENA_GROUP (v->arenaitem), &t);
} else {
nr_arena_group_set_child_transform (NR_ARENA_GROUP (v->arenaitem), NULL);
}
}
}
static void
sp_mask_modified (SPObject *object, guint flags)
{
if (flags & SP_OBJECT_MODIFIED_FLAG) {
flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
}
flags &= SP_OBJECT_MODIFIED_CASCADE;
SPObjectGroup *og = SP_OBJECTGROUP (object);
GSList *l = NULL;
for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
g_object_ref (G_OBJECT (child));
l = g_slist_prepend (l, child);
}
l = g_slist_reverse (l);
while (l) {
SPObject *child = SP_OBJECT (l->data);
l = g_slist_remove (l, child);
if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
child->emitModified(flags);
}
g_object_unref (G_OBJECT (child));
}
}
static Inkscape::XML::Node *
sp_mask_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
{
if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
repr = xml_doc->createElement("svg:mask");
}
if (((SPObjectClass *) (parent_class))->write)
((SPObjectClass *) (parent_class))->write (object, repr, flags);
return repr;
}
// Create a mask element (using passed elements), add it to <defs>
const gchar *
sp_mask_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
{
Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
Inkscape::XML::Node *repr = xml_doc->createElement("svg:mask");
repr->setAttribute("maskUnits", "userSpaceOnUse");
defsrepr->appendChild(repr);
const gchar *mask_id = repr->attribute("id");
SPObject *mask_object = document->getObjectById(mask_id);
for (GSList *it = reprs; it != NULL; it = it->next) {
Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
SPItem *item = SP_ITEM(mask_object->appendChildRepr(node));
if (NULL != applyTransform) {
NR::Matrix transform (item->transform);
transform *= (*applyTransform);
sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
}
}
if (repr != defsrepr->lastChild())
defsrepr->changeOrder(repr, defsrepr->lastChild()); // workaround for bug 989084
Inkscape::GC::release(repr);
return mask_id;
}
NRArenaItem *
sp_mask_show (SPMask *mask, NRArena *arena, unsigned int key)
{
g_return_val_if_fail (mask != NULL, NULL);
g_return_val_if_fail (SP_IS_MASK (mask), NULL);
g_return_val_if_fail (arena != NULL, NULL);
g_return_val_if_fail (NR_IS_ARENA (arena), NULL);
NRArenaItem *ai = NRArenaGroup::create(arena);
mask->display = sp_mask_view_new_prepend (mask->display, key, ai);
for (SPObject *child = sp_object_first_child(SP_OBJECT(mask)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
if (SP_IS_ITEM (child)) {
NRArenaItem *ac = sp_item_invoke_show (SP_ITEM (child), arena, key, SP_ITEM_REFERENCE_FLAGS);
if (ac) {
/* The order is not important in mask */
nr_arena_item_add_child (ai, ac, NULL);
nr_arena_item_unref (ac);
}
}
}
if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
NRMatrix t;
nr_matrix_set_scale (&t, mask->display->bbox.x1 - mask->display->bbox.x0, mask->display->bbox.y1 - mask->display->bbox.y0);
t.c[4] = mask->display->bbox.x0;
t.c[5] = mask->display->bbox.y0;
nr_arena_group_set_child_transform (NR_ARENA_GROUP (ai), &t);
}
return ai;
}
void
sp_mask_hide (SPMask *cp, unsigned int key)
{
g_return_if_fail (cp != NULL);
g_return_if_fail (SP_IS_MASK (cp));
for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)); child != NULL; child = SP_OBJECT_NEXT(child)) {
if (SP_IS_ITEM (child)) {
sp_item_invoke_hide (SP_ITEM (child), key);
}
}
for (SPMaskView *v = cp->display; v != NULL; v = v->next) {
if (v->key == key) {
/* We simply unref and let item to manage this in handler */
cp->display = sp_mask_view_list_remove (cp->display, v);
return;
}
}
g_assert_not_reached ();
}
void
sp_mask_set_bbox (SPMask *mask, unsigned int key, NRRect *bbox)
{
for (SPMaskView *v = mask->display; v != NULL; v = v->next) {
if (v->key == key) {
if (!NR_DF_TEST_CLOSE (v->bbox.x0, bbox->x0, NR_EPSILON) ||
!NR_DF_TEST_CLOSE (v->bbox.y0, bbox->y0, NR_EPSILON) ||
!NR_DF_TEST_CLOSE (v->bbox.x1, bbox->x1, NR_EPSILON) ||
!NR_DF_TEST_CLOSE (v->bbox.y1, bbox->y1, NR_EPSILON)) {
v->bbox = *bbox;
}
break;
}
}
}
/* Mask views */
SPMaskView *
sp_mask_view_new_prepend (SPMaskView *list, unsigned int key, NRArenaItem *arenaitem)
{
SPMaskView *new_mask_view = g_new (SPMaskView, 1);
new_mask_view->next = list;
new_mask_view->key = key;
new_mask_view->arenaitem = nr_arena_item_ref(arenaitem);
new_mask_view->bbox.x0 = new_mask_view->bbox.x1 = 0.0;
new_mask_view->bbox.y0 = new_mask_view->bbox.y1 = 0.0;
return new_mask_view;
}
SPMaskView *
sp_mask_view_list_remove (SPMaskView *list, SPMaskView *view)
{
if (view == list) {
list = list->next;
} else {
SPMaskView *prev;
prev = list;
while (prev->next != view) prev = prev->next;
prev->next = view->next;
}
nr_arena_item_unref (view->arenaitem);
g_free (view);
return list;
}
/*
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:encoding=utf-8:textwidth=99 :