Code

moving trunk for module inkscape
[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 <livarot/Path.h>
31 #include "svg/stringstream.h"
32 #include "attributes.h"
33 #include "sp-use-reference.h"
34 #include "sp-tspan.h"
35 #include "sp-textpath.h"
36 #include "xml/repr.h"
39 /*#####################################################
40 #  SPTSPAN
41 #####################################################*/
43 static void sp_tspan_class_init(SPTSpanClass *classname);
44 static void sp_tspan_init(SPTSpan *tspan);
46 static void sp_tspan_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
47 static void sp_tspan_release(SPObject *object);
48 static void sp_tspan_set(SPObject *object, unsigned key, gchar const *value);
49 static void sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags);
50 static void sp_tspan_modified(SPObject *object, unsigned flags);
51 static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
53 static SPItemClass *tspan_parent_class;
55 /**
56  *
57  */
58 GType
59 sp_tspan_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SPTSpanClass),
65             NULL,    /* base_init */
66             NULL,    /* base_finalize */
67             (GClassInitFunc) sp_tspan_class_init,
68             NULL,    /* class_finalize */
69             NULL,    /* class_data */
70             sizeof(SPTSpan),
71             16,    /* n_preallocs */
72             (GInstanceInitFunc) sp_tspan_init,
73             NULL,    /* value_table */
74         };
75         type = g_type_register_static(SP_TYPE_ITEM, "SPTSpan", &info, (GTypeFlags)0);
76     }
77     return type;
78 }
80 static void
81 sp_tspan_class_init(SPTSpanClass *classname)
82 {
83     SPObjectClass * sp_object_class;
84     SPItemClass * item_class;
85         
86     sp_object_class = (SPObjectClass *) classname;
87     item_class = (SPItemClass *) classname;
88         
89     tspan_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
90         
91     sp_object_class->build = sp_tspan_build;
92     sp_object_class->release = sp_tspan_release;
93     sp_object_class->set = sp_tspan_set;
94     sp_object_class->update = sp_tspan_update;
95     sp_object_class->modified = sp_tspan_modified;
96     sp_object_class->write = sp_tspan_write;
97 }
99 static void
100 sp_tspan_init(SPTSpan *tspan)
102     tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
103     new (&tspan->attributes) TextTagAttributes;
106 static void
107 sp_tspan_release(SPObject *object)
109     SPTSpan *tspan = SP_TSPAN(object);
111     tspan->attributes.~TextTagAttributes();
112         
113     if (((SPObjectClass *) tspan_parent_class)->release)
114         ((SPObjectClass *) tspan_parent_class)->release(object);
117 static void
118 sp_tspan_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
120     //SPTSpan *tspan = SP_TSPAN(object);
121         
122     sp_object_read_attr(object, "x");
123     sp_object_read_attr(object, "y");
124     sp_object_read_attr(object, "dx");
125     sp_object_read_attr(object, "dy");
126     sp_object_read_attr(object, "rotate");
127     sp_object_read_attr(object, "sodipodi:role");
128         
129     if (((SPObjectClass *) tspan_parent_class)->build)
130         ((SPObjectClass *) tspan_parent_class)->build(object, doc, repr);
133 static void
134 sp_tspan_set(SPObject *object, unsigned key, gchar const *value)
136     SPTSpan *tspan = SP_TSPAN(object);
137         
138     if (tspan->attributes.readSingleAttribute(key, value)) {
139         if (tspan->role != SP_TSPAN_ROLE_LINE)
140             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
141     } else {
142         switch (key) {
143             case SP_ATTR_SODIPODI_ROLE:
144                 if (value && (!strcmp(value, "line") || !strcmp(value, "paragraph"))) {
145                     tspan->role = SP_TSPAN_ROLE_LINE;
146                 } else {
147                     tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
148                 }
149                 break;
150             default:
151                 if (((SPObjectClass *) tspan_parent_class)->set)
152                     (((SPObjectClass *) tspan_parent_class)->set)(object, key, value);
153                 break;
154         }
155     }
158 static void
159 sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags)
161     if (((SPObjectClass *) tspan_parent_class)->update)
162         ((SPObjectClass *) tspan_parent_class)->update(object, ctx, flags);
163         
164     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
165     flags &= SP_OBJECT_MODIFIED_CASCADE;
166         
167     SPObject *ochild;
168     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
169         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
170             ochild->updateDisplay(ctx, flags);
171         }
172     }
175 static void
176 sp_tspan_modified(SPObject *object, unsigned flags)
178     if (((SPObjectClass *) tspan_parent_class)->modified)
179         ((SPObjectClass *) tspan_parent_class)->modified(object, flags);
180         
181     if (flags & SP_OBJECT_MODIFIED_FLAG)
182         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
183     flags &= SP_OBJECT_MODIFIED_CASCADE;
184         
185     SPObject *ochild;
186     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
187         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
188             ochild->emitModified(flags);
189         }
190     }
193 static Inkscape::XML::Node *
194 sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
196     SPTSpan *tspan = SP_TSPAN(object);
197         
198     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
199         repr = sp_repr_new("svg:tspan");
200     }
201         
202     tspan->attributes.writeTo(repr);
203         
204     if ( flags&SP_OBJECT_WRITE_BUILD ) {
205         GSList *l = NULL;
206         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
207             Inkscape::XML::Node* c_repr=NULL;
208             if ( SP_IS_TSPAN(child) ) {
209                 c_repr = child->updateRepr(NULL, flags);
210             } else if ( SP_IS_TEXTPATH(child) ) {
211                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
212             } else if ( SP_IS_STRING(child) ) {
213                 c_repr = sp_repr_new_text(SP_STRING(child)->string.c_str());
214             }
215             if ( c_repr ) l = g_slist_prepend(l, c_repr);
216         }
217         while ( l ) {
218             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
219             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
220             l = g_slist_remove(l, l->data);
221         }
222     } else {
223         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
224             if ( SP_IS_TSPAN(child) ) {
225                 child->updateRepr(flags);
226             } else if ( SP_IS_TEXTPATH(child) ) {
227                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
228             } else if ( SP_IS_STRING(child) ) {
229                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
230             }
231         }
232     }
233         
234     if (((SPObjectClass *) tspan_parent_class)->write)
235         ((SPObjectClass *) tspan_parent_class)->write(object, repr, flags);
236         
237     return repr;
240 /*#####################################################
241 #  SPTEXTPATH
242 #####################################################*/
244 static void sp_textpath_class_init(SPTextPathClass *classname);
245 static void sp_textpath_init(SPTextPath *textpath);
246 static void sp_textpath_finalize(GObject *obj);
248 static void sp_textpath_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
249 static void sp_textpath_release(SPObject *object);
250 static void sp_textpath_set(SPObject *object, unsigned key, gchar const *value);
251 static void sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags);
252 static void sp_textpath_modified(SPObject *object, unsigned flags);
253 static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
255 static SPItemClass *textpath_parent_class;
257 void   refresh_textpath_source(SPTextPath* offset);
260 /**
261  *
262  */
263 GType
264 sp_textpath_get_type()
266     static GType type = 0;
267     if (!type) {
268         GTypeInfo info = {
269             sizeof(SPTextPathClass),
270             NULL,    /* base_init */
271             NULL,    /* base_finalize */
272             (GClassInitFunc) sp_textpath_class_init,
273             NULL,    /* class_finalize */
274             NULL,    /* class_data */
275             sizeof(SPTextPath),
276             16,    /* n_preallocs */
277             (GInstanceInitFunc) sp_textpath_init,
278             NULL,    /* value_table */
279         };
280         type = g_type_register_static(SP_TYPE_ITEM, "SPTextPath", &info, (GTypeFlags)0);
281     }
282     return type;
285 static void
286 sp_textpath_class_init(SPTextPathClass *classname)
288     GObjectClass  *gobject_class = (GObjectClass *) classname;
289     SPObjectClass * sp_object_class;
290     SPItemClass * item_class;
291         
292     sp_object_class = (SPObjectClass *) classname;
293     item_class = (SPItemClass *) classname;
294         
295     textpath_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
296         
297     gobject_class->finalize = sp_textpath_finalize;
298                 
299     sp_object_class->build = sp_textpath_build;
300     sp_object_class->release = sp_textpath_release;
301     sp_object_class->set = sp_textpath_set;
302     sp_object_class->update = sp_textpath_update;
303     sp_object_class->modified = sp_textpath_modified;
304     sp_object_class->write = sp_textpath_write;
307 static void
308 sp_textpath_init(SPTextPath *textpath)
310     new (&textpath->attributes) TextTagAttributes;
311         
312     textpath->startOffset._set = false;
313     textpath->originalPath = NULL;
314     textpath->isUpdating=false;
315     // set up the uri reference
316     textpath->sourcePath = new SPUsePath(SP_OBJECT(textpath));
317     textpath->sourcePath->user_unlink = sp_textpath_to_text;
320 static void
321 sp_textpath_finalize(GObject *obj)
323     SPTextPath *textpath = (SPTextPath *) obj;
324         
325     delete textpath->sourcePath;
328 static void
329 sp_textpath_release(SPObject *object)
331     SPTextPath *textpath = SP_TEXTPATH(object);
332         
333     textpath->attributes.~TextTagAttributes();
334         
335     if (textpath->originalPath) delete textpath->originalPath;
336     textpath->originalPath = NULL;
337                 
338     if (((SPObjectClass *) textpath_parent_class)->release)
339         ((SPObjectClass *) textpath_parent_class)->release(object);
342 static void
343 sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
345     //SPTextPath *textpath = SP_TEXTPATH(object);
346         
347     sp_object_read_attr(object, "x");
348     sp_object_read_attr(object, "y");
349     sp_object_read_attr(object, "dx");
350     sp_object_read_attr(object, "dy");
351     sp_object_read_attr(object, "rotate");
352     sp_object_read_attr(object, "startOffset");
353     sp_object_read_attr(object, "xlink:href");
354         
355     bool  no_content=true;
356     for (Inkscape::XML::Node* rch = repr->firstChild() ; rch != NULL; rch = rch->next()) {
357         if ( rch->type() == Inkscape::XML::TEXT_NODE ) {no_content=false;break;}
358     }
359         
360     if ( no_content ) {
361         Inkscape::XML::Node* rch = sp_repr_new_text("");
362         repr->addChild(rch, NULL);
363     }
364         
365     if (((SPObjectClass *) textpath_parent_class)->build)
366         ((SPObjectClass *) textpath_parent_class)->build(object, doc, repr);
369 static void
370 sp_textpath_set(SPObject *object, unsigned key, gchar const *value)
372     SPTextPath *textpath = SP_TEXTPATH(object);
373         
374     if (textpath->attributes.readSingleAttribute(key, value)) {
375         object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
376     } else {
377         switch (key) {
378             case SP_ATTR_XLINK_HREF:
379                 textpath->sourcePath->link((char*)value);
380                 break;
381             case SP_ATTR_STARTOFFSET:
382                 textpath->startOffset.readOrUnset(value);
383                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
384                 break;
385             default:
386                 if (((SPObjectClass *) textpath_parent_class)->set)
387                     (((SPObjectClass *) textpath_parent_class)->set)(object, key, value);
388                 break;
389         }
390     }
393 static void
394 sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags)
396     SPTextPath *textpath = SP_TEXTPATH(object);
397         
398     textpath->isUpdating=true;
399     if ( textpath->sourcePath->sourceDirty ) refresh_textpath_source(textpath);
400     textpath->isUpdating=false;
401                 
402     if (((SPObjectClass *) textpath_parent_class)->update)
403         ((SPObjectClass *) textpath_parent_class)->update(object, ctx, flags);
404                 
405     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
406     flags &= SP_OBJECT_MODIFIED_CASCADE;
407                         
408     SPObject *ochild;
409     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
410         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
411             ochild->updateDisplay(ctx, flags);
412         }
413     }
417 void   refresh_textpath_source(SPTextPath* tp)
419     if ( tp == NULL ) return;
420     tp->sourcePath->refresh_source();
421     tp->sourcePath->sourceDirty=false;
422         
423     // finalisons
424     if ( tp->sourcePath->originalPath ) { 
425         if (tp->originalPath) {
426             delete tp->originalPath;
427         }
428         tp->originalPath = NULL;
429                 
430         tp->originalPath = new Path;
431         tp->originalPath->Copy(tp->sourcePath->originalPath);
432         tp->originalPath->ConvertWithBackData(0.01);
433                 
434     }
437 static void
438 sp_textpath_modified(SPObject *object, unsigned flags)
440     if (((SPObjectClass *) textpath_parent_class)->modified)
441         ((SPObjectClass *) textpath_parent_class)->modified(object, flags);
442         
443     if (flags & SP_OBJECT_MODIFIED_FLAG)
444         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
445     flags &= SP_OBJECT_MODIFIED_CASCADE;
446         
447     SPObject *ochild;
448     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
449         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
450             ochild->emitModified(flags);
451         }
452     }
454 static Inkscape::XML::Node *
455 sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
457     SPTextPath *textpath = SP_TEXTPATH(object);
458         
459     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
460         repr = sp_repr_new("svg:textPath");
461     }
462         
463     textpath->attributes.writeTo(repr);
464     if (textpath->startOffset._set) {
465         if (textpath->startOffset.unit == SVGLength::PERCENT) {
466                 Inkscape::SVGOStringStream os;
467             os << (textpath->startOffset.computed * 100.0) << "%";
468             SP_OBJECT_REPR(textpath)->setAttribute("startOffset", os.str().c_str());
469         } else {
470             /* FIXME: This logic looks rather undesirable if e.g. startOffset is to be
471                in ems. */
472             sp_repr_set_svg_double(repr, "startOffset", textpath->startOffset.computed);
473         }
474     }
476     if ( textpath->sourcePath->sourceHref ) repr->setAttribute("xlink:href", textpath->sourcePath->sourceHref);
477         
478     if ( flags&SP_OBJECT_WRITE_BUILD ) {
479         GSList *l = NULL;
480         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
481             Inkscape::XML::Node* c_repr=NULL;
482             if ( SP_IS_TSPAN(child) ) {
483                 c_repr = child->updateRepr(NULL, flags);
484             } else if ( SP_IS_TEXTPATH(child) ) {
485                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
486             } else if ( SP_IS_STRING(child) ) {
487                 c_repr = sp_repr_new_text(SP_STRING(child)->string.c_str());
488             }
489             if ( c_repr ) l = g_slist_prepend(l, c_repr);
490         }
491         while ( l ) {
492             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
493             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
494             l = g_slist_remove(l, l->data);
495         }
496     } else {
497         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
498             if ( SP_IS_TSPAN(child) ) {
499                 child->updateRepr(flags);
500             } else if ( SP_IS_TEXTPATH(child) ) {
501                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
502             } else if ( SP_IS_STRING(child) ) {
503                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
504             }
505         }
506     }
507         
508     if (((SPObjectClass *) textpath_parent_class)->write)
509         ((SPObjectClass *) textpath_parent_class)->write(object, repr, flags);
510         
511     return repr;
515 SPItem *
516 sp_textpath_get_path_item(SPTextPath *tp)
518     if (tp && tp->sourcePath) {
519         SPItem *refobj = tp->sourcePath->getObject();
520         if (SP_IS_ITEM(refobj))
521             return (SPItem *) refobj;
522     }
523     return NULL;
526 void
527 sp_textpath_to_text(SPObject *tp)
529     SPObject *text = SP_OBJECT_PARENT(tp);
531     NRRect bbox;
532     sp_item_invoke_bbox(SP_ITEM(text), &bbox, sp_item_i2doc_affine(SP_ITEM(text)), TRUE);
533     NR::Point xy(bbox.x0, bbox.y0);
535     // make a list of textpath children
536     GSList *tp_reprs = NULL;
537     for (SPObject *o = SP_OBJECT(tp)->firstChild() ; o != NULL; o = o->next) {
538         tp_reprs = g_slist_prepend(tp_reprs, SP_OBJECT_REPR(o));
539     }
541     for ( GSList *i = tp_reprs ; i ; i = i->next ) {
542         // make a copy of each textpath child
543         Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate();
544         // remove the old repr from under textpath
545         SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data); 
546         // put its copy into under textPath
547         SP_OBJECT_REPR(text)->addChild(copy, NULL); // fixme: copy id
548     }
550     //remove textpath
551     tp->deleteObject();
552     g_slist_free(tp_reprs);
554     // set x/y on text 
555     /* fixme: Yuck, is this really the right test? */
556     if (xy[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
557         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[NR::X]);
558         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[NR::Y]);
559     }
563 /*
564   Local Variables:
565   mode:c++
566   c-file-style:"stroustrup"
567   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
568   indent-tabs-mode:nil
569   fill-column:99
570   End:
571 */
572 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :