Code

r10887@tres: ted | 2006-02-05 23:16:41 -0800
[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 "text-editing.h"
37 #include "style.h"
38 #include "libnr/nr-matrix-fns.h"
39 #include "xml/repr.h"
42 /*#####################################################
43 #  SPTSPAN
44 #####################################################*/
46 static void sp_tspan_class_init(SPTSpanClass *classname);
47 static void sp_tspan_init(SPTSpan *tspan);
49 static void sp_tspan_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
50 static void sp_tspan_release(SPObject *object);
51 static void sp_tspan_set(SPObject *object, unsigned key, gchar const *value);
52 static void sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags);
53 static void sp_tspan_modified(SPObject *object, unsigned flags);
54 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
55 static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
57 static SPItemClass *tspan_parent_class;
59 /**
60  *
61  */
62 GType
63 sp_tspan_get_type()
64 {
65     static GType type = 0;
66     if (!type) {
67         GTypeInfo info = {
68             sizeof(SPTSpanClass),
69             NULL,    /* base_init */
70             NULL,    /* base_finalize */
71             (GClassInitFunc) sp_tspan_class_init,
72             NULL,    /* class_finalize */
73             NULL,    /* class_data */
74             sizeof(SPTSpan),
75             16,    /* n_preallocs */
76             (GInstanceInitFunc) sp_tspan_init,
77             NULL,    /* value_table */
78         };
79         type = g_type_register_static(SP_TYPE_ITEM, "SPTSpan", &info, (GTypeFlags)0);
80     }
81     return type;
82 }
84 static void
85 sp_tspan_class_init(SPTSpanClass *classname)
86 {
87     SPObjectClass * sp_object_class;
88     SPItemClass * item_class;
89         
90     sp_object_class = (SPObjectClass *) classname;
91     item_class = (SPItemClass *) classname;
92         
93     tspan_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
94         
95     sp_object_class->build = sp_tspan_build;
96     sp_object_class->release = sp_tspan_release;
97     sp_object_class->set = sp_tspan_set;
98     sp_object_class->update = sp_tspan_update;
99     sp_object_class->modified = sp_tspan_modified;
100     sp_object_class->write = sp_tspan_write;
102     item_class->bbox = sp_tspan_bbox;
105 static void
106 sp_tspan_init(SPTSpan *tspan)
108     tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
109     new (&tspan->attributes) TextTagAttributes;
112 static void
113 sp_tspan_release(SPObject *object)
115     SPTSpan *tspan = SP_TSPAN(object);
117     tspan->attributes.~TextTagAttributes();
118         
119     if (((SPObjectClass *) tspan_parent_class)->release)
120         ((SPObjectClass *) tspan_parent_class)->release(object);
123 static void
124 sp_tspan_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
126     //SPTSpan *tspan = SP_TSPAN(object);
127         
128     sp_object_read_attr(object, "x");
129     sp_object_read_attr(object, "y");
130     sp_object_read_attr(object, "dx");
131     sp_object_read_attr(object, "dy");
132     sp_object_read_attr(object, "rotate");
133     sp_object_read_attr(object, "sodipodi:role");
134         
135     if (((SPObjectClass *) tspan_parent_class)->build)
136         ((SPObjectClass *) tspan_parent_class)->build(object, doc, repr);
139 static void
140 sp_tspan_set(SPObject *object, unsigned key, gchar const *value)
142     SPTSpan *tspan = SP_TSPAN(object);
143         
144     if (tspan->attributes.readSingleAttribute(key, value)) {
145         if (tspan->role != SP_TSPAN_ROLE_LINE)
146             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
147     } else {
148         switch (key) {
149             case SP_ATTR_SODIPODI_ROLE:
150                 if (value && (!strcmp(value, "line") || !strcmp(value, "paragraph"))) {
151                     tspan->role = SP_TSPAN_ROLE_LINE;
152                 } else {
153                     tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
154                 }
155                 break;
156             default:
157                 if (((SPObjectClass *) tspan_parent_class)->set)
158                     (((SPObjectClass *) tspan_parent_class)->set)(object, key, value);
159                 break;
160         }
161     }
164 static void
165 sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags)
167     if (((SPObjectClass *) tspan_parent_class)->update)
168         ((SPObjectClass *) tspan_parent_class)->update(object, ctx, flags);
169         
170     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
171     flags &= SP_OBJECT_MODIFIED_CASCADE;
172         
173     SPObject *ochild;
174     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
175         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
176             ochild->updateDisplay(ctx, flags);
177         }
178     }
181 static void
182 sp_tspan_modified(SPObject *object, unsigned flags)
184     if (((SPObjectClass *) tspan_parent_class)->modified)
185         ((SPObjectClass *) tspan_parent_class)->modified(object, flags);
186         
187     if (flags & SP_OBJECT_MODIFIED_FLAG)
188         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
189     flags &= SP_OBJECT_MODIFIED_CASCADE;
190         
191     SPObject *ochild;
192     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
193         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
194             ochild->emitModified(flags);
195         }
196     }
199 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
201     // find out the ancestor text which holds our layout
202     SPObject *parent_text = SP_OBJECT(item);
203     for (; parent_text != NULL && !SP_IS_TEXT(parent_text); parent_text = SP_OBJECT_PARENT (parent_text));
204     if (parent_text == NULL) return;
206     // get the bbox of our portion of the layout
207     SP_TEXT(parent_text)->layout.getBoundingBox(bbox, transform, sp_text_get_length_upto(parent_text, item), sp_text_get_length_upto(item, NULL) - 1);
209     // Add stroke width
210     SPStyle* style=SP_OBJECT_STYLE (item);
211     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
212         double const scale = expansion(transform);
213         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
214             double const width = MAX(0.125, style->stroke_width.computed * scale);
215             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
216                 bbox->x0-=0.5*width;
217                 bbox->x1+=0.5*width;
218                 bbox->y0-=0.5*width;
219                 bbox->y1+=0.5*width;
220             }
221         }
222     }
225 static Inkscape::XML::Node *
226 sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
228     SPTSpan *tspan = SP_TSPAN(object);
229         
230     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
231         repr = sp_repr_new("svg:tspan");
232     }
233         
234     tspan->attributes.writeTo(repr);
235         
236     if ( flags&SP_OBJECT_WRITE_BUILD ) {
237         GSList *l = NULL;
238         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
239             Inkscape::XML::Node* c_repr=NULL;
240             if ( SP_IS_TSPAN(child) ) {
241                 c_repr = child->updateRepr(NULL, flags);
242             } else if ( SP_IS_TEXTPATH(child) ) {
243                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
244             } else if ( SP_IS_STRING(child) ) {
245                 c_repr = sp_repr_new_text(SP_STRING(child)->string.c_str());
246             }
247             if ( c_repr ) l = g_slist_prepend(l, c_repr);
248         }
249         while ( l ) {
250             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
251             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
252             l = g_slist_remove(l, l->data);
253         }
254     } else {
255         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
256             if ( SP_IS_TSPAN(child) ) {
257                 child->updateRepr(flags);
258             } else if ( SP_IS_TEXTPATH(child) ) {
259                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
260             } else if ( SP_IS_STRING(child) ) {
261                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
262             }
263         }
264     }
265         
266     if (((SPObjectClass *) tspan_parent_class)->write)
267         ((SPObjectClass *) tspan_parent_class)->write(object, repr, flags);
268         
269     return repr;
272 /*#####################################################
273 #  SPTEXTPATH
274 #####################################################*/
276 static void sp_textpath_class_init(SPTextPathClass *classname);
277 static void sp_textpath_init(SPTextPath *textpath);
278 static void sp_textpath_finalize(GObject *obj);
280 static void sp_textpath_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
281 static void sp_textpath_release(SPObject *object);
282 static void sp_textpath_set(SPObject *object, unsigned key, gchar const *value);
283 static void sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags);
284 static void sp_textpath_modified(SPObject *object, unsigned flags);
285 static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
287 static SPItemClass *textpath_parent_class;
289 void   refresh_textpath_source(SPTextPath* offset);
292 /**
293  *
294  */
295 GType
296 sp_textpath_get_type()
298     static GType type = 0;
299     if (!type) {
300         GTypeInfo info = {
301             sizeof(SPTextPathClass),
302             NULL,    /* base_init */
303             NULL,    /* base_finalize */
304             (GClassInitFunc) sp_textpath_class_init,
305             NULL,    /* class_finalize */
306             NULL,    /* class_data */
307             sizeof(SPTextPath),
308             16,    /* n_preallocs */
309             (GInstanceInitFunc) sp_textpath_init,
310             NULL,    /* value_table */
311         };
312         type = g_type_register_static(SP_TYPE_ITEM, "SPTextPath", &info, (GTypeFlags)0);
313     }
314     return type;
317 static void
318 sp_textpath_class_init(SPTextPathClass *classname)
320     GObjectClass  *gobject_class = (GObjectClass *) classname;
321     SPObjectClass * sp_object_class;
322     SPItemClass * item_class;
323         
324     sp_object_class = (SPObjectClass *) classname;
325     item_class = (SPItemClass *) classname;
326         
327     textpath_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
328         
329     gobject_class->finalize = sp_textpath_finalize;
330                 
331     sp_object_class->build = sp_textpath_build;
332     sp_object_class->release = sp_textpath_release;
333     sp_object_class->set = sp_textpath_set;
334     sp_object_class->update = sp_textpath_update;
335     sp_object_class->modified = sp_textpath_modified;
336     sp_object_class->write = sp_textpath_write;
339 static void
340 sp_textpath_init(SPTextPath *textpath)
342     new (&textpath->attributes) TextTagAttributes;
343         
344     textpath->startOffset._set = false;
345     textpath->originalPath = NULL;
346     textpath->isUpdating=false;
347     // set up the uri reference
348     textpath->sourcePath = new SPUsePath(SP_OBJECT(textpath));
349     textpath->sourcePath->user_unlink = sp_textpath_to_text;
352 static void
353 sp_textpath_finalize(GObject *obj)
355     SPTextPath *textpath = (SPTextPath *) obj;
356         
357     delete textpath->sourcePath;
360 static void
361 sp_textpath_release(SPObject *object)
363     SPTextPath *textpath = SP_TEXTPATH(object);
364         
365     textpath->attributes.~TextTagAttributes();
366         
367     if (textpath->originalPath) delete textpath->originalPath;
368     textpath->originalPath = NULL;
369                 
370     if (((SPObjectClass *) textpath_parent_class)->release)
371         ((SPObjectClass *) textpath_parent_class)->release(object);
374 static void
375 sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
377     //SPTextPath *textpath = SP_TEXTPATH(object);
378         
379     sp_object_read_attr(object, "x");
380     sp_object_read_attr(object, "y");
381     sp_object_read_attr(object, "dx");
382     sp_object_read_attr(object, "dy");
383     sp_object_read_attr(object, "rotate");
384     sp_object_read_attr(object, "startOffset");
385     sp_object_read_attr(object, "xlink:href");
386         
387     bool  no_content=true;
388     for (Inkscape::XML::Node* rch = repr->firstChild() ; rch != NULL; rch = rch->next()) {
389         if ( rch->type() == Inkscape::XML::TEXT_NODE ) {no_content=false;break;}
390     }
391         
392     if ( no_content ) {
393         Inkscape::XML::Node* rch = sp_repr_new_text("");
394         repr->addChild(rch, NULL);
395     }
396         
397     if (((SPObjectClass *) textpath_parent_class)->build)
398         ((SPObjectClass *) textpath_parent_class)->build(object, doc, repr);
401 static void
402 sp_textpath_set(SPObject *object, unsigned key, gchar const *value)
404     SPTextPath *textpath = SP_TEXTPATH(object);
405         
406     if (textpath->attributes.readSingleAttribute(key, value)) {
407         object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
408     } else {
409         switch (key) {
410             case SP_ATTR_XLINK_HREF:
411                 textpath->sourcePath->link((char*)value);
412                 break;
413             case SP_ATTR_STARTOFFSET:
414                 textpath->startOffset.readOrUnset(value);
415                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
416                 break;
417             default:
418                 if (((SPObjectClass *) textpath_parent_class)->set)
419                     (((SPObjectClass *) textpath_parent_class)->set)(object, key, value);
420                 break;
421         }
422     }
425 static void
426 sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags)
428     SPTextPath *textpath = SP_TEXTPATH(object);
429         
430     textpath->isUpdating=true;
431     if ( textpath->sourcePath->sourceDirty ) refresh_textpath_source(textpath);
432     textpath->isUpdating=false;
433                 
434     if (((SPObjectClass *) textpath_parent_class)->update)
435         ((SPObjectClass *) textpath_parent_class)->update(object, ctx, flags);
436                 
437     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
438     flags &= SP_OBJECT_MODIFIED_CASCADE;
439                         
440     SPObject *ochild;
441     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
442         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
443             ochild->updateDisplay(ctx, flags);
444         }
445     }
449 void   refresh_textpath_source(SPTextPath* tp)
451     if ( tp == NULL ) return;
452     tp->sourcePath->refresh_source();
453     tp->sourcePath->sourceDirty=false;
454         
455     // finalisons
456     if ( tp->sourcePath->originalPath ) { 
457         if (tp->originalPath) {
458             delete tp->originalPath;
459         }
460         tp->originalPath = NULL;
461                 
462         tp->originalPath = new Path;
463         tp->originalPath->Copy(tp->sourcePath->originalPath);
464         tp->originalPath->ConvertWithBackData(0.01);
465                 
466     }
469 static void
470 sp_textpath_modified(SPObject *object, unsigned flags)
472     if (((SPObjectClass *) textpath_parent_class)->modified)
473         ((SPObjectClass *) textpath_parent_class)->modified(object, flags);
474         
475     if (flags & SP_OBJECT_MODIFIED_FLAG)
476         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
477     flags &= SP_OBJECT_MODIFIED_CASCADE;
478         
479     SPObject *ochild;
480     for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
481         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
482             ochild->emitModified(flags);
483         }
484     }
486 static Inkscape::XML::Node *
487 sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
489     SPTextPath *textpath = SP_TEXTPATH(object);
490         
491     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
492         repr = sp_repr_new("svg:textPath");
493     }
494         
495     textpath->attributes.writeTo(repr);
496     if (textpath->startOffset._set) {
497         if (textpath->startOffset.unit == SVGLength::PERCENT) {
498                 Inkscape::SVGOStringStream os;
499             os << (textpath->startOffset.computed * 100.0) << "%";
500             SP_OBJECT_REPR(textpath)->setAttribute("startOffset", os.str().c_str());
501         } else {
502             /* FIXME: This logic looks rather undesirable if e.g. startOffset is to be
503                in ems. */
504             sp_repr_set_svg_double(repr, "startOffset", textpath->startOffset.computed);
505         }
506     }
508     if ( textpath->sourcePath->sourceHref ) repr->setAttribute("xlink:href", textpath->sourcePath->sourceHref);
509         
510     if ( flags&SP_OBJECT_WRITE_BUILD ) {
511         GSList *l = NULL;
512         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
513             Inkscape::XML::Node* c_repr=NULL;
514             if ( SP_IS_TSPAN(child) ) {
515                 c_repr = child->updateRepr(NULL, flags);
516             } else if ( SP_IS_TEXTPATH(child) ) {
517                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
518             } else if ( SP_IS_STRING(child) ) {
519                 c_repr = sp_repr_new_text(SP_STRING(child)->string.c_str());
520             }
521             if ( c_repr ) l = g_slist_prepend(l, c_repr);
522         }
523         while ( l ) {
524             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
525             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
526             l = g_slist_remove(l, l->data);
527         }
528     } else {
529         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
530             if ( SP_IS_TSPAN(child) ) {
531                 child->updateRepr(flags);
532             } else if ( SP_IS_TEXTPATH(child) ) {
533                 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
534             } else if ( SP_IS_STRING(child) ) {
535                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
536             }
537         }
538     }
539         
540     if (((SPObjectClass *) textpath_parent_class)->write)
541         ((SPObjectClass *) textpath_parent_class)->write(object, repr, flags);
542         
543     return repr;
547 SPItem *
548 sp_textpath_get_path_item(SPTextPath *tp)
550     if (tp && tp->sourcePath) {
551         SPItem *refobj = tp->sourcePath->getObject();
552         if (SP_IS_ITEM(refobj))
553             return (SPItem *) refobj;
554     }
555     return NULL;
558 void
559 sp_textpath_to_text(SPObject *tp)
561     SPObject *text = SP_OBJECT_PARENT(tp);
563     NRRect bbox;
564     sp_item_invoke_bbox(SP_ITEM(text), &bbox, sp_item_i2doc_affine(SP_ITEM(text)), TRUE);
565     NR::Point xy(bbox.x0, bbox.y0);
567     // make a list of textpath children
568     GSList *tp_reprs = NULL;
569     for (SPObject *o = SP_OBJECT(tp)->firstChild() ; o != NULL; o = o->next) {
570         tp_reprs = g_slist_prepend(tp_reprs, SP_OBJECT_REPR(o));
571     }
573     for ( GSList *i = tp_reprs ; i ; i = i->next ) {
574         // make a copy of each textpath child
575         Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate();
576         // remove the old repr from under textpath
577         SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data); 
578         // put its copy into under textPath
579         SP_OBJECT_REPR(text)->addChild(copy, NULL); // fixme: copy id
580     }
582     //remove textpath
583     tp->deleteObject();
584     g_slist_free(tp_reprs);
586     // set x/y on text 
587     /* fixme: Yuck, is this really the right test? */
588     if (xy[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
589         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[NR::X]);
590         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[NR::Y]);
591     }
595 /*
596   Local Variables:
597   mode:c++
598   c-file-style:"stroustrup"
599   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
600   indent-tabs-mode:nil
601   fill-column:99
602   End:
603 */
604 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :