Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-tref.cpp
1 /** \file
2  * SVG <tref> implementation - All character data within the referenced
3  * element, including character data enclosed within additional markup,
4  * will be rendered.
5  *
6  * This file was created based on skeleton.cpp
7  */
8 /*
9  * Authors:
10  *   Gail Banaszkiewicz <Gail.Banaszkiewicz@gmail.com>
11  *   Jon A. Cruz <jon@joncruz.org>
12  *   Abhishek Sharma
13  *
14  * Copyright (C) 2007 Gail Banaszkiewicz
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <glibmm/i18n.h>
25 #include "attributes.h"
26 #include "document.h"
27 #include "sp-object-repr.h"
28 #include "sp-text.h"
29 #include "sp-tspan.h"
30 #include "sp-tref.h"
31 #include "style.h"
32 #include "text-editing.h"
33 #include "uri.h"
35 #include "display/nr-arena-group.h"
36 #include "libnr/nr-matrix-fns.h"
37 #include "xml/node.h"
38 #include "xml/repr.h"
41 //#define DEBUG_TREF
42 #ifdef DEBUG_TREF
43 # define debug(f, a...) { g_message("%s(%d) %s:", \
44                                   __FILE__,__LINE__,__FUNCTION__); \
45                           g_message(f, ## a); \
46                           g_message("\n"); \
47                         }
48 #else
49 # define debug(f, a...) /**/
50 #endif
53 static void build_string_from_root(Inkscape::XML::Node *root, Glib::ustring *retString);
55 /* TRef base class */
57 static void sp_tref_class_init(SPTRefClass *tref_class);
58 static void sp_tref_init(SPTRef *tref);
59 static void sp_tref_finalize(GObject *obj);
61 static void sp_tref_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
62 static void sp_tref_release(SPObject *object);
63 static void sp_tref_set(SPObject *object, unsigned int key, gchar const *value);
64 static void sp_tref_update(SPObject *object, SPCtx *ctx, guint flags);
65 static void sp_tref_modified(SPObject *object, guint flags);
66 static Inkscape::XML::Node *sp_tref_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
68 static void sp_tref_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
69 static gchar *sp_tref_description(SPItem *item);
71 static void sp_tref_href_changed(SPObject *old_ref, SPObject *ref, SPTRef *tref);
72 static void sp_tref_delete_self(SPObject *deleted, SPTRef *self);
74 static SPObjectClass *tref_parent_class;
76 GType
77 sp_tref_get_type()
78 {
79     static GType tref_type = 0;
81     if (!tref_type) {
82         GTypeInfo tref_info = {
83             sizeof(SPTRefClass),
84             NULL, NULL,
85             (GClassInitFunc) sp_tref_class_init,
86             NULL, NULL,
87             sizeof(SPTRef),
88             16,
89             (GInstanceInitFunc) sp_tref_init,
90             NULL,    /* value_table */
91         };
92         tref_type = g_type_register_static(SP_TYPE_ITEM, "SPTRef", &tref_info, (GTypeFlags)0);
93     }
94     return tref_type;
95 }
97 static void
98 sp_tref_class_init(SPTRefClass *tref_class)
99 {
100     GObjectClass *gobject_class = (GObjectClass *) tref_class;
101     SPObjectClass *sp_object_class = (SPObjectClass *)tref_class;
103     tref_parent_class = (SPObjectClass*)g_type_class_peek_parent(tref_class);
105     sp_object_class->build = sp_tref_build;
106     sp_object_class->release = sp_tref_release;
107     sp_object_class->write = sp_tref_write;
108     sp_object_class->set = sp_tref_set;
109     sp_object_class->update = sp_tref_update;
110     sp_object_class->modified = sp_tref_modified;
112     gobject_class->finalize = sp_tref_finalize;
114     SPItemClass *item_class = (SPItemClass *) tref_class;
116     item_class->bbox = sp_tref_bbox;
117     item_class->description = sp_tref_description;
120 static void
121 sp_tref_init(SPTRef *tref)
123     new (&tref->attributes) TextTagAttributes;
125     tref->href = NULL;
126     tref->uriOriginalRef = new SPTRefReference(SP_OBJECT(tref));
127     new (&tref->_delete_connection) sigc::connection();
128     new (&tref->_changed_connection) sigc::connection();
130     tref->_changed_connection =
131         tref->uriOriginalRef->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_tref_href_changed), tref));
135 static void
136 sp_tref_finalize(GObject *obj)
138     SPTRef *tref = (SPTRef *) obj;
140     delete tref->uriOriginalRef;
142     tref->_delete_connection.~connection();
143     tref->_changed_connection.~connection();
147 /**
148  * Reads the Inkscape::XML::Node, and initializes SPTRef variables.
149  */
150 static void
151 sp_tref_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
153     if (((SPObjectClass *) tref_parent_class)->build) {
154         ((SPObjectClass *) tref_parent_class)->build(object, document, repr);
155     }
157     object->readAttr( "xlink:href" );
158     object->readAttr( "x" );
159     object->readAttr( "y" );
160     object->readAttr( "dx" );
161     object->readAttr( "dy" );
162     object->readAttr( "rotate" );
165 /**
166  * Drops any allocated memory.
167  */
168 static void
169 sp_tref_release(SPObject *object)
171     SPTRef *tref = SP_TREF(object);
173     tref->attributes.~TextTagAttributes();
175     tref->_delete_connection.disconnect();
176     tref->_changed_connection.disconnect();
178     g_free(tref->href);
179     tref->href = NULL;
181     tref->uriOriginalRef->detach();
183     if (((SPObjectClass *) tref_parent_class)->release)
184         ((SPObjectClass *) tref_parent_class)->release(object);
187 /**
188  * Sets a specific value in the SPTRef.
189  */
190 static void
191 sp_tref_set(SPObject *object, unsigned int key, gchar const *value)
193     debug("0x%p %s(%u): '%s'",object,
194             sp_attribute_name(key),key,value ? value : "<no value>");
196     SPTRef *tref = SP_TREF(object);
198     if (tref->attributes.readSingleAttribute(key, value)) { // x, y, dx, dy, rotate
199         object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
200     } else if (key == SP_ATTR_XLINK_HREF) { // xlink:href
201         if ( !value ) {
202             // No value
203             g_free(tref->href);
204             tref->href = NULL;
205             tref->uriOriginalRef->detach();
206         } else if ((tref->href && strcmp(value, tref->href) != 0) || (!tref->href)) {
208             // Value has changed
210             if ( tref->href ) {
211                 g_free(tref->href);
212                 tref->href = NULL;
213             }
215             tref->href = g_strdup(value);
217             try {
218                 tref->uriOriginalRef->attach(Inkscape::URI(value));
219                 tref->uriOriginalRef->updateObserver();
220             } catch ( Inkscape::BadURIException &e ) {
221                 g_warning("%s", e.what());
222                 tref->uriOriginalRef->detach();
223             }
225             // No matter what happened, an update should be in order
226             SP_OBJECT(tref)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
227         }
229     } else { // default
230         if (((SPObjectClass *) tref_parent_class)->set) {
231             ((SPObjectClass *) tref_parent_class)->set(object, key, value);
232         }
233     }
238 /**
239  * Receives update notifications.  Code based on sp_use_update and sp_tspan_update.
240  */
241 static void
242 sp_tref_update(SPObject *object, SPCtx *ctx, guint flags)
244     debug("0x%p",object);
246     SPTRef *tref = SP_TREF(object);
248     if (((SPObjectClass *) tref_parent_class)->update) {
249         ((SPObjectClass *) tref_parent_class)->update(object, ctx, flags);
250     }
252     if (flags & SP_OBJECT_MODIFIED_FLAG) {
253         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
254     }
256     flags &= SP_OBJECT_MODIFIED_CASCADE;
258     SPObject *child = tref->stringChild;
259     if (child) {
260         if ( flags || ( child->uflags & SP_OBJECT_MODIFIED_FLAG )) {
261             child->updateDisplay(ctx, flags);
262         }
263     }
268 static void
269 sp_tref_modified(SPObject *object, guint flags)
271     SPTRef *tref_obj = SP_TREF(object);
273     if (flags & SP_OBJECT_MODIFIED_FLAG) {
274         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
275     }
277     flags &= SP_OBJECT_MODIFIED_CASCADE;
279     SPObject *child = tref_obj->stringChild;
280     if (child) {
281         g_object_ref(G_OBJECT(child));
282         if (flags || (child->mflags & SP_OBJECT_MODIFIED_FLAG)) {
283             child->emitModified(flags);
284         }
285         g_object_unref(G_OBJECT(child));
286     }
289 /**
290  * Writes its settings to an incoming repr object, if any.
291  */
292 static Inkscape::XML::Node *
293 sp_tref_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
295     debug("0x%p",object);
297     SPTRef *tref = SP_TREF(object);
299     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
300         repr = xml_doc->createElement("svg:tref");
301     }
303     tref->attributes.writeTo(repr);
305     if (tref->uriOriginalRef->getURI()) {
306         gchar *uri_string = tref->uriOriginalRef->getURI()->toString();
307         debug("uri_string=%s", uri_string);
308         repr->setAttribute("xlink:href", uri_string);
309         g_free(uri_string);
310     }
312     if (((SPObjectClass *) tref_parent_class)->write) {
313         ((SPObjectClass *) tref_parent_class)->write(object, xml_doc, repr, flags);
314     }
316     return repr;
319 /**
320  *  The code for this function is swiped from the tspan bbox code, since tref should work pretty much the same way
321  */
322 static void
323 sp_tref_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
325     // find out the ancestor text which holds our layout
326     SPObject *parent_text = SP_OBJECT(item);
327     for (; parent_text != NULL && !SP_IS_TEXT(parent_text); parent_text = SP_OBJECT_PARENT (parent_text)){};
328     if (parent_text == NULL) return;
330     // get the bbox of our portion of the layout
331     SP_TEXT(parent_text)->layout.getBoundingBox(
332         bbox, transform, sp_text_get_length_upto(parent_text, item), sp_text_get_length_upto(item, NULL) - 1);
334     // Add stroke width
335     SPStyle* style=SP_OBJECT_STYLE (item);
336     if (!style->stroke.isNone()) {
337         double const scale = transform.descrim();
338         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
339             double const width = MAX(0.125, style->stroke_width.computed * scale);
340             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
341                 bbox->x0-=0.5*width;
342                 bbox->x1+=0.5*width;
343                 bbox->y0-=0.5*width;
344                 bbox->y1+=0.5*width;
345             }
346         }
347     }
351 static gchar *
352 sp_tref_description(SPItem *item)
354     SPTRef *tref = SP_TREF(item);
356     SPObject *referred = tref->getObjectReferredTo();
358     if (tref && tref->getObjectReferredTo()) {
359         char *child_desc;
361         if (SP_IS_ITEM(referred)) {
362             child_desc = SP_ITEM(referred)->description();
363         } else {
364             child_desc = g_strdup("");
365         }
367         char *ret = g_strdup_printf(
368                 _("<b>Cloned character data</b>%s%s"),
369                 (SP_IS_ITEM(referred) ? _(" from ") : ""),
370                 child_desc);
371         g_free(child_desc);
372         return ret;
373     } else {
374         return g_strdup(_("<b>Orphaned cloned character data</b>"));
375     }
379 /* For the sigc::connection changes (i.e. when the object being refered to changes) */
380 static void
381 sp_tref_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPTRef *tref)
383     if (tref)
384     {
385         // Save a pointer to the original object being referred to
386         SPObject *refRoot = tref->getObjectReferredTo();
388         tref->_delete_connection.disconnect();
390         if (tref->stringChild) {
391             SP_OBJECT(tref)->detach(tref->stringChild);
392             tref->stringChild = NULL;
393         }
395         // Ensure that we are referring to a legitimate object
396         if (tref->href && refRoot && sp_tref_reference_allowed(tref, refRoot)) {
398             // Update the text being referred to (will create a new string child)
399             sp_tref_update_text(tref);
401             // Restore the delete connection now that we're done messing with stuff
402             tref->_delete_connection = SP_OBJECT(refRoot)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_tref_delete_self), tref));
403         }
405     }
409 /**
410  * Delete the tref object
411  */
412 static void
413 sp_tref_delete_self(SPObject */*deleted*/, SPTRef *self)
415     SP_OBJECT(self)->deleteObject();
418 /**
419  * Return the object referred to via the URI reference
420  */
421 SPObject * SPTRef::getObjectReferredTo(void)
423     SPObject *referredObject = NULL;
425     if (uriOriginalRef) {
426         referredObject = SP_OBJECT(uriOriginalRef->getObject());
427     }
429     return referredObject;
433 /**
434  * Returns true when the given tref is allowed to refer to a particular object
435  */
436 bool
437 sp_tref_reference_allowed(SPTRef *tref, SPObject *possible_ref)
439     bool allowed = false;
441     if (tref && possible_ref) {
442         if (tref != possible_ref) {
443             bool ancestor = false;
444             for (SPObject *obj = tref; obj; obj = SP_OBJECT_PARENT(obj)) {
445                 if (possible_ref == obj) {
446                     ancestor = true;
447                     break;
448                 }
449             }
450             allowed = !ancestor;
451         }
452     }
454     return allowed;
458 /**
459  * Returns true if a tref is fully contained in the confines of the given
460  * iterators and layout (or if there is no tref).
461  */
462 bool
463 sp_tref_fully_contained(SPObject *start_item, Glib::ustring::iterator &start,
464                              SPObject *end_item, Glib::ustring::iterator &end)
466     bool fully_contained = false;
468     if (start_item && end_item) {
470         // If neither the beginning or the end is a tref then we return true (whether there
471         // is a tref in the innards or not, because if there is one then it must be totally
472         // contained)
473         if (!(SP_IS_STRING(start_item) && SP_IS_TREF(SP_OBJECT_PARENT(start_item)))
474                 && !(SP_IS_STRING(end_item) && SP_IS_TREF(SP_OBJECT_PARENT(end_item)))) {
475             fully_contained = true;
476         }
478         // Both the beginning and end are trefs; but in this case, the string iterators
479         // must be at the right places
480         else if ((SP_IS_STRING(start_item) && SP_IS_TREF(SP_OBJECT_PARENT(start_item)))
481                 && (SP_IS_STRING(end_item) && SP_IS_TREF(SP_OBJECT_PARENT(end_item)))) {
482             if (start == SP_STRING(start_item)->string.begin()
483                     && end == SP_STRING(start_item)->string.end()) {
484                 fully_contained = true;
485             }
486         }
488         // If the beginning is a string that is a child of a tref, the iterator has to be
489         // at the beginning of the item
490         else if ((SP_IS_STRING(start_item) && SP_IS_TREF(SP_OBJECT_PARENT(start_item)))
491                     && !(SP_IS_STRING(end_item) && SP_IS_TREF(SP_OBJECT_PARENT(end_item)))) {
492             if (start == SP_STRING(start_item)->string.begin()) {
493                 fully_contained = true;
494             }
495         }
497         // Same, but the for the end
498         else if (!(SP_IS_STRING(start_item) && SP_IS_TREF(SP_OBJECT_PARENT(start_item)))
499                     && (SP_IS_STRING(end_item) && SP_IS_TREF(SP_OBJECT_PARENT(end_item)))) {
500             if (end == SP_STRING(start_item)->string.end()) {
501                 fully_contained = true;
502             }
503         }
504     }
506     return fully_contained;
510 void sp_tref_update_text(SPTRef *tref)
512     if (tref) {
513         // Get the character data that will be used with this tref
514         Glib::ustring charData = "";
515         build_string_from_root(SP_OBJECT_REPR(tref->getObjectReferredTo()), &charData);
517         if (tref->stringChild) {
518             SP_OBJECT(tref)->detach(tref->stringChild);
519             tref->stringChild = NULL;
520         }
522         // Create the node and SPString to be the tref's child
523         Inkscape::XML::Document *xml_doc = SP_OBJECT_DOCUMENT(tref)->getReprDoc();
525         Inkscape::XML::Node *newStringRepr = xml_doc->createTextNode(charData.c_str());
526         tref->stringChild = SP_OBJECT(g_object_new(sp_repr_type_lookup(newStringRepr), NULL));
528         // Add this SPString as a child of the tref
529         SP_OBJECT(tref)->attach(tref->stringChild, tref->lastChild());
530         sp_object_unref(tref->stringChild, NULL);
531         (tref->stringChild)->invoke_build(SP_OBJECT(tref)->document, newStringRepr, TRUE);
533         Inkscape::GC::release(newStringRepr);
534     }
539 /**
540  * Using depth-first search, build up a string by concatenating all SPStrings
541  * found in the tree starting at the root
542  */
543 static void
544 build_string_from_root(Inkscape::XML::Node *root, Glib::ustring *retString)
546     if (root && retString) {
548         // Stop and concatenate when a SPString is found
549         if (root->type() == Inkscape::XML::TEXT_NODE) {
550             *retString += (root->content());
552             debug("%s", retString->c_str());
554         // Otherwise, continue searching down the tree (with the assumption that no children nodes
555         // of a SPString are actually legal)
556         } else {
557             Inkscape::XML::Node *childNode;
558             for (childNode = root->firstChild(); childNode; childNode = childNode->next()) {
559                 build_string_from_root(childNode, retString);
560             }
561         }
562     }
565 /**
566  * This function will create a new tspan element with the same attributes as
567  * the tref had and add the same text as a child.  The tref is replaced in the
568  * tree with the new tspan.
569  * The code is based partially on sp_use_unlink
570  */
571 SPObject *
572 sp_tref_convert_to_tspan(SPObject *obj)
574     SPObject * new_tspan = NULL;
576     ////////////////////
577     // BASE CASE
578     ////////////////////
579     if (SP_IS_TREF(obj)) {
581         SPTRef *tref = SP_TREF(obj);
583         if (tref && tref->stringChild) {
584             Inkscape::XML::Node *tref_repr = SP_OBJECT_REPR(tref);
585             Inkscape::XML::Node *tref_parent = sp_repr_parent(tref_repr);
587             SPDocument *document = SP_OBJECT(tref)->document;
588             Inkscape::XML::Document *xml_doc = document->getReprDoc();
590             Inkscape::XML::Node *new_tspan_repr = xml_doc->createElement("svg:tspan");
592             // Add the new tspan element just after the current tref
593             tref_parent->addChild(new_tspan_repr, tref_repr);
594             Inkscape::GC::release(new_tspan_repr);
596             new_tspan = document->getObjectByRepr(new_tspan_repr);
598             // Create a new string child for the tspan
599             Inkscape::XML::Node *new_string_repr = SP_OBJECT_REPR(tref->stringChild)->duplicate(xml_doc);
600             new_tspan_repr->addChild(new_string_repr, NULL);
602             //SPObject * new_string_child = document->getObjectByRepr(new_string_repr);
604             // Merge style from the tref
605             SPStyle *new_tspan_sty = SP_OBJECT_STYLE(new_tspan);
606             SPStyle const *tref_sty = SP_OBJECT_STYLE(tref);
607             sp_style_merge_from_dying_parent(new_tspan_sty, tref_sty);
608             sp_style_merge_from_parent(new_tspan_sty, new_tspan->parent->style);
611             SP_OBJECT(new_tspan)->updateRepr();
613             // Hold onto our SPObject and repr for now.
614             sp_object_ref(SP_OBJECT(tref), NULL);
615             Inkscape::GC::anchor(tref_repr);
617             // Remove ourselves, not propagating delete events to avoid a
618             // chain-reaction with other elements that might reference us.
619             SP_OBJECT(tref)->deleteObject(false);
621             // Give the copy our old id and let go of our old repr.
622             new_tspan_repr->setAttribute("id", tref_repr->attribute("id"));
623             Inkscape::GC::release(tref_repr);
625             // Establish the succession and let go of our object.
626             SP_OBJECT(tref)->setSuccessor(new_tspan);
627             sp_object_unref(SP_OBJECT(tref), NULL);
628         }
629     }
630     ////////////////////
631     // RECURSIVE CASE
632     ////////////////////
633     else {
634         GSList *l = NULL;
635         for (SPObject *child = obj->firstChild() ; child != NULL ; child = child->getNext() ) {
636             sp_object_ref (SP_OBJECT (child), obj);
637             l = g_slist_prepend (l, child);
638         }
639         l = g_slist_reverse (l);
640         while (l) {
641             SPObject *child = SP_OBJECT (l->data);
642             l = g_slist_remove (l, child);
644             // Note that there may be more than one conversion happening here, so if it's not a
645             // tref being passed into this function, the returned value can't be specifically known
646             new_tspan = sp_tref_convert_to_tspan(child);
648             sp_object_unref (SP_OBJECT (child), obj);
649         }
650     }
652     return new_tspan;
656 /*
657   Local Variables:
658   mode:c++
659   c-file-style:"stroustrup"
660   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
661   indent-tabs-mode:nil
662   fill-column:99
663   End:
664 */
665 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :