Code

Applying fixes for gcc 4.3 build issues (closes LP: #169115)
[inkscape.git] / src / sp-tspan.cpp
1 #define __SP_TSPAN_C__
3 /*
4  * SVG <text> and <tspan> implementation
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 1999-2002 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 /*
17  * fixme:
18  *
19  * These subcomponents should not be items, or alternately
20  * we have to invent set of flags to mark, whether standard
21  * attributes are applicable to given item (I even like this
22  * idea somewhat - Lauris)
23  *
24  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <cstring>
31 #include <string>
32 #include <glibmm/i18n.h>
34 #include <livarot/Path.h>
35 #include "svg/stringstream.h"
36 #include "attributes.h"
37 #include "sp-use-reference.h"
38 #include "sp-tspan.h"
39 #include "sp-tref.h"
40 #include "sp-textpath.h"
41 #include "text-editing.h"
42 #include "style.h"
43 #include "libnr/nr-matrix-fns.h"
44 #include "xml/repr.h"
45 #include "document.h"
48 /*#####################################################
49 #  SPTSPAN
50 #####################################################*/
52 static void sp_tspan_class_init(SPTSpanClass *classname);
53 static void sp_tspan_init(SPTSpan *tspan);
55 static void sp_tspan_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
56 static void sp_tspan_release(SPObject *object);
57 static void sp_tspan_set(SPObject *object, unsigned key, gchar const *value);
58 static void sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags);
59 static void sp_tspan_modified(SPObject *object, unsigned flags);
60 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
61 static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
62 static char *sp_tspan_description (SPItem *item);
64 static SPItemClass *tspan_parent_class;
66 /**
67  *
68  */
69 GType
70 sp_tspan_get_type()
71 {
72     static GType type = 0;
73     if (!type) {
74         GTypeInfo info = {
75             sizeof(SPTSpanClass),
76             NULL,    /* base_init */
77             NULL,    /* base_finalize */
78             (GClassInitFunc) sp_tspan_class_init,
79             NULL,    /* class_finalize */
80             NULL,    /* class_data */
81             sizeof(SPTSpan),
82             16,    /* n_preallocs */
83             (GInstanceInitFunc) sp_tspan_init,
84             NULL,    /* value_table */
85         };
86         type = g_type_register_static(SP_TYPE_ITEM, "SPTSpan", &info, (GTypeFlags)0);
87     }
88     return type;
89 }
91 static void
92 sp_tspan_class_init(SPTSpanClass *classname)
93 {
94     SPObjectClass * sp_object_class;
95     SPItemClass * item_class;
96         
97     sp_object_class = (SPObjectClass *) classname;
98     item_class = (SPItemClass *) classname;
99         
100     tspan_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
101         
102     sp_object_class->build = sp_tspan_build;
103     sp_object_class->release = sp_tspan_release;
104     sp_object_class->set = sp_tspan_set;
105     sp_object_class->update = sp_tspan_update;
106     sp_object_class->modified = sp_tspan_modified;
107     sp_object_class->write = sp_tspan_write;
109     item_class->bbox = sp_tspan_bbox;
110     item_class->description = sp_tspan_description;
113 static void
114 sp_tspan_init(SPTSpan *tspan)
116     tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
117     new (&tspan->attributes) TextTagAttributes;
120 static void
121 sp_tspan_release(SPObject *object)
123     SPTSpan *tspan = SP_TSPAN(object);
125     tspan->attributes.~TextTagAttributes();
126         
127     if (((SPObjectClass *) tspan_parent_class)->release)
128         ((SPObjectClass *) tspan_parent_class)->release(object);
131 static void
132 sp_tspan_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
134     //SPTSpan *tspan = SP_TSPAN(object);
135         
136     sp_object_read_attr(object, "x");
137     sp_object_read_attr(object, "y");
138     sp_object_read_attr(object, "dx");
139     sp_object_read_attr(object, "dy");
140     sp_object_read_attr(object, "rotate");
141     sp_object_read_attr(object, "sodipodi:role");
142         
143     if (((SPObjectClass *) tspan_parent_class)->build)
144         ((SPObjectClass *) tspan_parent_class)->build(object, doc, repr);
147 static void
148 sp_tspan_set(SPObject *object, unsigned key, gchar const *value)
150     SPTSpan *tspan = SP_TSPAN(object);
151         
152     if (tspan->attributes.readSingleAttribute(key, value)) {
153         object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
154     } else {
155         switch (key) {
156             case SP_ATTR_SODIPODI_ROLE:
157                 if (value && (!strcmp(value, "line") || !strcmp(value, "paragraph"))) {
158                     tspan->role = SP_TSPAN_ROLE_LINE;
159                 } else {
160                     tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
161                 }
162                 break;
163             default:
164                 if (((SPObjectClass *) tspan_parent_class)->set)
165                     (((SPObjectClass *) tspan_parent_class)->set)(object, key, value);
166                 break;
167         }
168     }
171 static void
172 sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags)
174     if (((SPObjectClass *) tspan_parent_class)->update)
175         ((SPObjectClass *) tspan_parent_class)->update(object, ctx, flags);
176         
177     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
178     flags &= SP_OBJECT_MODIFIED_CASCADE;
179         
180     SPObject *ochild;
181     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
182         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
183             ochild->updateDisplay(ctx, flags);
184         }
185     }
188 static void
189 sp_tspan_modified(SPObject *object, unsigned flags)
191     if (((SPObjectClass *) tspan_parent_class)->modified)
192         ((SPObjectClass *) tspan_parent_class)->modified(object, flags);
193         
194     if (flags & SP_OBJECT_MODIFIED_FLAG)
195         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
196     flags &= SP_OBJECT_MODIFIED_CASCADE;
197         
198     SPObject *ochild;
199     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
200         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
201             ochild->emitModified(flags);
202         }
203     }
206 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
208     // find out the ancestor text which holds our layout
209     SPObject *parent_text = SP_OBJECT(item);
210     for (; parent_text != NULL && !SP_IS_TEXT(parent_text); parent_text = SP_OBJECT_PARENT (parent_text));
211     if (parent_text == NULL) return;
213     // get the bbox of our portion of the layout
214     SP_TEXT(parent_text)->layout.getBoundingBox(bbox, transform, sp_text_get_length_upto(parent_text, item), sp_text_get_length_upto(item, NULL) - 1);
216     // Add stroke width
217     SPStyle* style=SP_OBJECT_STYLE (item);
218     if (!style->stroke.isNone()) {
219         double const scale = expansion(transform);
220         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
221             double const width = MAX(0.125, style->stroke_width.computed * scale);
222             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
223                 bbox->x0-=0.5*width;
224                 bbox->x1+=0.5*width;
225                 bbox->y0-=0.5*width;
226                 bbox->y1+=0.5*width;
227             }
228         }
229     }
232 static Inkscape::XML::Node *
233 sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
235     SPTSpan *tspan = SP_TSPAN(object);
236     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
237         
238     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
239         repr = xml_doc->createElement("svg:tspan");
240     }
241         
242     tspan->attributes.writeTo(repr);
243         
244     if ( flags&SP_OBJECT_WRITE_BUILD ) {
245         GSList *l = NULL;
246         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
247             Inkscape::XML::Node* c_repr=NULL;
248             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
249                 c_repr = child->updateRepr(NULL, flags);
250             } else if ( SP_IS_TEXTPATH(child) ) {
251                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
252             } else if ( SP_IS_STRING(child) ) {
253                 c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
254             }
255             if ( c_repr ) l = g_slist_prepend(l, c_repr);
256         }
257         while ( l ) {
258             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
259             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
260             l = g_slist_remove(l, l->data);
261         }
262     } else {
263         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
264             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
265                 child->updateRepr(flags);
266             } else if ( SP_IS_TEXTPATH(child) ) {
267                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
268             } else if ( SP_IS_STRING(child) ) {
269                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
270             }
271         }
272     }
273         
274     if (((SPObjectClass *) tspan_parent_class)->write)
275         ((SPObjectClass *) tspan_parent_class)->write(object, repr, flags);
276         
277     return repr;
280 static char *
281 sp_tspan_description(SPItem *item)
283     g_return_val_if_fail(SP_IS_TSPAN(item), NULL);
285     return g_strdup(_("<b>Text span</b>"));
289 /*#####################################################
290 #  SPTEXTPATH
291 #####################################################*/
293 static void sp_textpath_class_init(SPTextPathClass *classname);
294 static void sp_textpath_init(SPTextPath *textpath);
295 static void sp_textpath_finalize(GObject *obj);
297 static void sp_textpath_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
298 static void sp_textpath_release(SPObject *object);
299 static void sp_textpath_set(SPObject *object, unsigned key, gchar const *value);
300 static void sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags);
301 static void sp_textpath_modified(SPObject *object, unsigned flags);
302 static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
304 static SPItemClass *textpath_parent_class;
306 void   refresh_textpath_source(SPTextPath* offset);
309 /**
310  *
311  */
312 GType
313 sp_textpath_get_type()
315     static GType type = 0;
316     if (!type) {
317         GTypeInfo info = {
318             sizeof(SPTextPathClass),
319             NULL,    /* base_init */
320             NULL,    /* base_finalize */
321             (GClassInitFunc) sp_textpath_class_init,
322             NULL,    /* class_finalize */
323             NULL,    /* class_data */
324             sizeof(SPTextPath),
325             16,    /* n_preallocs */
326             (GInstanceInitFunc) sp_textpath_init,
327             NULL,    /* value_table */
328         };
329         type = g_type_register_static(SP_TYPE_ITEM, "SPTextPath", &info, (GTypeFlags)0);
330     }
331     return type;
334 static void
335 sp_textpath_class_init(SPTextPathClass *classname)
337     GObjectClass  *gobject_class = (GObjectClass *) classname;
338     SPObjectClass * sp_object_class;
339     SPItemClass * item_class;
340         
341     sp_object_class = (SPObjectClass *) classname;
342     item_class = (SPItemClass *) classname;
343         
344     textpath_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
345         
346     gobject_class->finalize = sp_textpath_finalize;
347                 
348     sp_object_class->build = sp_textpath_build;
349     sp_object_class->release = sp_textpath_release;
350     sp_object_class->set = sp_textpath_set;
351     sp_object_class->update = sp_textpath_update;
352     sp_object_class->modified = sp_textpath_modified;
353     sp_object_class->write = sp_textpath_write;
356 static void
357 sp_textpath_init(SPTextPath *textpath)
359     new (&textpath->attributes) TextTagAttributes;
360         
361     textpath->startOffset._set = false;
362     textpath->originalPath = NULL;
363     textpath->isUpdating=false;
364     // set up the uri reference
365     textpath->sourcePath = new SPUsePath(SP_OBJECT(textpath));
366     textpath->sourcePath->user_unlink = sp_textpath_to_text;
369 static void
370 sp_textpath_finalize(GObject *obj)
372     SPTextPath *textpath = (SPTextPath *) obj;
373         
374     delete textpath->sourcePath;
377 static void
378 sp_textpath_release(SPObject *object)
380     SPTextPath *textpath = SP_TEXTPATH(object);
381         
382     textpath->attributes.~TextTagAttributes();
383         
384     if (textpath->originalPath) delete textpath->originalPath;
385     textpath->originalPath = NULL;
386                 
387     if (((SPObjectClass *) textpath_parent_class)->release)
388         ((SPObjectClass *) textpath_parent_class)->release(object);
391 static void
392 sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
394     //SPTextPath *textpath = SP_TEXTPATH(object);
395         
396     sp_object_read_attr(object, "x");
397     sp_object_read_attr(object, "y");
398     sp_object_read_attr(object, "dx");
399     sp_object_read_attr(object, "dy");
400     sp_object_read_attr(object, "rotate");
401     sp_object_read_attr(object, "startOffset");
402     sp_object_read_attr(object, "xlink:href");
403         
404     bool  no_content=true;
405     for (Inkscape::XML::Node* rch = repr->firstChild() ; rch != NULL; rch = rch->next()) {
406         if ( rch->type() == Inkscape::XML::TEXT_NODE ) {no_content=false;break;}
407     }
408         
409     if ( no_content ) {
410         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
411         Inkscape::XML::Node* rch = xml_doc->createTextNode("");
412         repr->addChild(rch, NULL);
413     }
414         
415     if (((SPObjectClass *) textpath_parent_class)->build)
416         ((SPObjectClass *) textpath_parent_class)->build(object, doc, repr);
419 static void
420 sp_textpath_set(SPObject *object, unsigned key, gchar const *value)
422     SPTextPath *textpath = SP_TEXTPATH(object);
423         
424     if (textpath->attributes.readSingleAttribute(key, value)) {
425         object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
426     } else {
427         switch (key) {
428             case SP_ATTR_XLINK_HREF:
429                 textpath->sourcePath->link((char*)value);
430                 break;
431             case SP_ATTR_STARTOFFSET:
432                 textpath->startOffset.readOrUnset(value);
433                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
434                 break;
435             default:
436                 if (((SPObjectClass *) textpath_parent_class)->set)
437                     (((SPObjectClass *) textpath_parent_class)->set)(object, key, value);
438                 break;
439         }
440     }
443 static void
444 sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags)
446     SPTextPath *textpath = SP_TEXTPATH(object);
447         
448     textpath->isUpdating=true;
449     if ( textpath->sourcePath->sourceDirty ) refresh_textpath_source(textpath);
450     textpath->isUpdating=false;
451                 
452     if (((SPObjectClass *) textpath_parent_class)->update)
453         ((SPObjectClass *) textpath_parent_class)->update(object, ctx, flags);
454                 
455     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
456     flags &= SP_OBJECT_MODIFIED_CASCADE;
457                         
458     SPObject *ochild;
459     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
460         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
461             ochild->updateDisplay(ctx, flags);
462         }
463     }
467 void   refresh_textpath_source(SPTextPath* tp)
469     if ( tp == NULL ) return;
470     tp->sourcePath->refresh_source();
471     tp->sourcePath->sourceDirty=false;
472         
473     // finalisons
474     if ( tp->sourcePath->originalPath ) {
475         if (tp->originalPath) {
476             delete tp->originalPath;
477         }
478         tp->originalPath = NULL;
479                 
480         tp->originalPath = new Path;
481         tp->originalPath->Copy(tp->sourcePath->originalPath);
482         tp->originalPath->ConvertWithBackData(0.01);
483                 
484     }
487 static void
488 sp_textpath_modified(SPObject *object, unsigned flags)
490     if (((SPObjectClass *) textpath_parent_class)->modified)
491         ((SPObjectClass *) textpath_parent_class)->modified(object, flags);
492         
493     if (flags & SP_OBJECT_MODIFIED_FLAG)
494         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
495     flags &= SP_OBJECT_MODIFIED_CASCADE;
496         
497     SPObject *ochild;
498     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
499         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
500             ochild->emitModified(flags);
501         }
502     }
504 static Inkscape::XML::Node *
505 sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
507     SPTextPath *textpath = SP_TEXTPATH(object);
508     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
509         
510     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
511         repr = xml_doc->createElement("svg:textPath");
512     }
513         
514     textpath->attributes.writeTo(repr);
515     if (textpath->startOffset._set) {
516         if (textpath->startOffset.unit == SVGLength::PERCENT) {
517                 Inkscape::SVGOStringStream os;
518             os << (textpath->startOffset.computed * 100.0) << "%";
519             SP_OBJECT_REPR(textpath)->setAttribute("startOffset", os.str().c_str());
520         } else {
521             /* FIXME: This logic looks rather undesirable if e.g. startOffset is to be
522                in ems. */
523             sp_repr_set_svg_double(repr, "startOffset", textpath->startOffset.computed);
524         }
525     }
527     if ( textpath->sourcePath->sourceHref ) repr->setAttribute("xlink:href", textpath->sourcePath->sourceHref);
528         
529     if ( flags&SP_OBJECT_WRITE_BUILD ) {
530         GSList *l = NULL;
531         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
532             Inkscape::XML::Node* c_repr=NULL;
533             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
534                 c_repr = child->updateRepr(NULL, flags);
535             } else if ( SP_IS_TEXTPATH(child) ) {
536                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
537             } else if ( SP_IS_STRING(child) ) {
538                 c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
539             }
540             if ( c_repr ) l = g_slist_prepend(l, c_repr);
541         }
542         while ( l ) {
543             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
544             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
545             l = g_slist_remove(l, l->data);
546         }
547     } else {
548         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
549             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
550                 child->updateRepr(flags);
551             } else if ( SP_IS_TEXTPATH(child) ) {
552                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
553             } else if ( SP_IS_STRING(child) ) {
554                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
555             }
556         }
557     }
558         
559     if (((SPObjectClass *) textpath_parent_class)->write)
560         ((SPObjectClass *) textpath_parent_class)->write(object, repr, flags);
561         
562     return repr;
566 SPItem *
567 sp_textpath_get_path_item(SPTextPath *tp)
569     if (tp && tp->sourcePath) {
570         SPItem *refobj = tp->sourcePath->getObject();
571         if (SP_IS_ITEM(refobj))
572             return (SPItem *) refobj;
573     }
574     return NULL;
577 void
578 sp_textpath_to_text(SPObject *tp)
580     SPObject *text = SP_OBJECT_PARENT(tp);
582     NRRect bbox;
583     sp_item_invoke_bbox(SP_ITEM(text), &bbox, sp_item_i2doc_affine(SP_ITEM(text)), TRUE);
584     NR::Point xy(bbox.x0, bbox.y0);
586     // make a list of textpath children
587     GSList *tp_reprs = NULL;
588     for (SPObject *o = SP_OBJECT(tp)->firstChild() ; o != NULL; o = o->next) {
589         tp_reprs = g_slist_prepend(tp_reprs, SP_OBJECT_REPR(o));
590     }
592     for ( GSList *i = tp_reprs ; i ; i = i->next ) {
593         // make a copy of each textpath child
594         Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate(SP_OBJECT_REPR(text)->document());
595         // remove the old repr from under textpath
596         SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data);
597         // put its copy under text
598         SP_OBJECT_REPR(text)->addChild(copy, NULL); // fixme: copy id
599     }
601     //remove textpath
602     tp->deleteObject();
603     g_slist_free(tp_reprs);
605     // set x/y on text
606     /* fixme: Yuck, is this really the right test? */
607     if (xy[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
608         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[NR::X]);
609         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[NR::Y]);
610     }
614 /*
615   Local Variables:
616   mode:c++
617   c-file-style:"stroustrup"
618   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
619   indent-tabs-mode:nil
620   fill-column:99
621   End:
622 */
623 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :