Code

A simple layout document as to what, why and how is cppification.
[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, Geom::Matrix const &transform, unsigned const flags);
61 static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Document *doc, 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     object->readAttr( "x");
137     object->readAttr( "y");
138     object->readAttr( "dx");
139     object->readAttr( "dy");
140     object->readAttr( "rotate");
141     object->readAttr( "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 = object->first_child() ; 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 = object->first_child() ; 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, Geom::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 = transform.descrim();
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::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
235     SPTSpan *tspan = SP_TSPAN(object);
237     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
238         repr = xml_doc->createElement("svg:tspan");
239     }
240         
241     tspan->attributes.writeTo(repr);
242         
243     if ( flags&SP_OBJECT_WRITE_BUILD ) {
244         GSList *l = NULL;
245         for (SPObject* child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
246             Inkscape::XML::Node* c_repr=NULL;
247             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
248                 c_repr = child->updateRepr(xml_doc, NULL, flags);
249             } else if ( SP_IS_TEXTPATH(child) ) {
250                 //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
251             } else if ( SP_IS_STRING(child) ) {
252                 c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
253             }
254             if ( c_repr ) l = g_slist_prepend(l, c_repr);
255         }
256         while ( l ) {
257             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
258             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
259             l = g_slist_remove(l, l->data);
260         }
261     } else {
262         for (SPObject* child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
263             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
264                 child->updateRepr(flags);
265             } else if ( SP_IS_TEXTPATH(child) ) {
266                 //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
267             } else if ( SP_IS_STRING(child) ) {
268                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
269             }
270         }
271     }
272         
273     if (((SPObjectClass *) tspan_parent_class)->write)
274         ((SPObjectClass *) tspan_parent_class)->write(object, xml_doc, repr, flags);
275         
276     return repr;
279 static char *
280 sp_tspan_description(SPItem *item)
282     g_return_val_if_fail(SP_IS_TSPAN(item), NULL);
284     return g_strdup(_("<b>Text span</b>"));
288 /*#####################################################
289 #  SPTEXTPATH
290 #####################################################*/
292 static void sp_textpath_class_init(SPTextPathClass *classname);
293 static void sp_textpath_init(SPTextPath *textpath);
294 static void sp_textpath_finalize(GObject *obj);
296 static void sp_textpath_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
297 static void sp_textpath_release(SPObject *object);
298 static void sp_textpath_set(SPObject *object, unsigned key, gchar const *value);
299 static void sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags);
300 static void sp_textpath_modified(SPObject *object, unsigned flags);
301 static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
303 static SPItemClass *textpath_parent_class;
305 void   refresh_textpath_source(SPTextPath* offset);
308 /**
309  *
310  */
311 GType
312 sp_textpath_get_type()
314     static GType type = 0;
315     if (!type) {
316         GTypeInfo info = {
317             sizeof(SPTextPathClass),
318             NULL,    /* base_init */
319             NULL,    /* base_finalize */
320             (GClassInitFunc) sp_textpath_class_init,
321             NULL,    /* class_finalize */
322             NULL,    /* class_data */
323             sizeof(SPTextPath),
324             16,    /* n_preallocs */
325             (GInstanceInitFunc) sp_textpath_init,
326             NULL,    /* value_table */
327         };
328         type = g_type_register_static(SP_TYPE_ITEM, "SPTextPath", &info, (GTypeFlags)0);
329     }
330     return type;
333 static void
334 sp_textpath_class_init(SPTextPathClass *classname)
336     GObjectClass  *gobject_class = (GObjectClass *) classname;
337     SPObjectClass * sp_object_class;
338     SPItemClass * item_class;
339         
340     sp_object_class = (SPObjectClass *) classname;
341     item_class = (SPItemClass *) classname;
342         
343     textpath_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
344         
345     gobject_class->finalize = sp_textpath_finalize;
346                 
347     sp_object_class->build = sp_textpath_build;
348     sp_object_class->release = sp_textpath_release;
349     sp_object_class->set = sp_textpath_set;
350     sp_object_class->update = sp_textpath_update;
351     sp_object_class->modified = sp_textpath_modified;
352     sp_object_class->write = sp_textpath_write;
355 static void
356 sp_textpath_init(SPTextPath *textpath)
358     new (&textpath->attributes) TextTagAttributes;
359         
360     textpath->startOffset._set = false;
361     textpath->originalPath = NULL;
362     textpath->isUpdating=false;
363     // set up the uri reference
364     textpath->sourcePath = new SPUsePath(SP_OBJECT(textpath));
365     textpath->sourcePath->user_unlink = sp_textpath_to_text;
368 static void
369 sp_textpath_finalize(GObject *obj)
371     SPTextPath *textpath = (SPTextPath *) obj;
372         
373     delete textpath->sourcePath;
376 static void
377 sp_textpath_release(SPObject *object)
379     SPTextPath *textpath = SP_TEXTPATH(object);
380         
381     textpath->attributes.~TextTagAttributes();
382         
383     if (textpath->originalPath) delete textpath->originalPath;
384     textpath->originalPath = NULL;
385                 
386     if (((SPObjectClass *) textpath_parent_class)->release)
387         ((SPObjectClass *) textpath_parent_class)->release(object);
390 static void
391 sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
393     //SPTextPath *textpath = SP_TEXTPATH(object);
394         
395     object->readAttr( "x");
396     object->readAttr( "y");
397     object->readAttr( "dx");
398     object->readAttr( "dy");
399     object->readAttr( "rotate");
400     object->readAttr( "startOffset");
401     object->readAttr( "xlink:href");
402         
403     bool  no_content=true;
404     for (Inkscape::XML::Node* rch = repr->firstChild() ; rch != NULL; rch = rch->next()) {
405         if ( rch->type() == Inkscape::XML::TEXT_NODE ) {no_content=false;break;}
406     }
407         
408     if ( no_content ) {
409         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
410         Inkscape::XML::Node* rch = xml_doc->createTextNode("");
411         repr->addChild(rch, NULL);
412     }
413         
414     if (((SPObjectClass *) textpath_parent_class)->build)
415         ((SPObjectClass *) textpath_parent_class)->build(object, doc, repr);
418 static void
419 sp_textpath_set(SPObject *object, unsigned key, gchar const *value)
421     SPTextPath *textpath = SP_TEXTPATH(object);
422         
423     if (textpath->attributes.readSingleAttribute(key, value)) {
424         object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
425     } else {
426         switch (key) {
427             case SP_ATTR_XLINK_HREF:
428                 textpath->sourcePath->link((char*)value);
429                 break;
430             case SP_ATTR_STARTOFFSET:
431                 textpath->startOffset.readOrUnset(value);
432                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
433                 break;
434             default:
435                 if (((SPObjectClass *) textpath_parent_class)->set)
436                     (((SPObjectClass *) textpath_parent_class)->set)(object, key, value);
437                 break;
438         }
439     }
442 static void
443 sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags)
445     SPTextPath *textpath = SP_TEXTPATH(object);
446         
447     textpath->isUpdating=true;
448     if ( textpath->sourcePath->sourceDirty ) refresh_textpath_source(textpath);
449     textpath->isUpdating=false;
450                 
451     if (((SPObjectClass *) textpath_parent_class)->update)
452         ((SPObjectClass *) textpath_parent_class)->update(object, ctx, flags);
453                 
454     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
455     flags &= SP_OBJECT_MODIFIED_CASCADE;
456                         
457     SPObject *ochild;
458     for ( ochild = object->first_child() ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
459         if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
460             ochild->updateDisplay(ctx, flags);
461         }
462     }
466 void   refresh_textpath_source(SPTextPath* tp)
468     if ( tp == NULL ) return;
469     tp->sourcePath->refresh_source();
470     tp->sourcePath->sourceDirty=false;
471         
472     // finalisons
473     if ( tp->sourcePath->originalPath ) {
474         if (tp->originalPath) {
475             delete tp->originalPath;
476         }
477         tp->originalPath = NULL;
478                 
479         tp->originalPath = new Path;
480         tp->originalPath->Copy(tp->sourcePath->originalPath);
481         tp->originalPath->ConvertWithBackData(0.01);
482                 
483     }
486 static void
487 sp_textpath_modified(SPObject *object, unsigned flags)
489     if (((SPObjectClass *) textpath_parent_class)->modified)
490         ((SPObjectClass *) textpath_parent_class)->modified(object, flags);
491         
492     if (flags & SP_OBJECT_MODIFIED_FLAG)
493         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
494     flags &= SP_OBJECT_MODIFIED_CASCADE;
495         
496     SPObject *ochild;
497     for ( ochild = object->first_child() ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
498         if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
499             ochild->emitModified(flags);
500         }
501     }
503 static Inkscape::XML::Node *
504 sp_textpath_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
506     SPTextPath *textpath = SP_TEXTPATH(object);
508     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
509         repr = xml_doc->createElement("svg:textPath");
510     }
511         
512     textpath->attributes.writeTo(repr);
513     if (textpath->startOffset._set) {
514         if (textpath->startOffset.unit == SVGLength::PERCENT) {
515                 Inkscape::SVGOStringStream os;
516             os << (textpath->startOffset.computed * 100.0) << "%";
517             SP_OBJECT_REPR(textpath)->setAttribute("startOffset", os.str().c_str());
518         } else {
519             /* FIXME: This logic looks rather undesirable if e.g. startOffset is to be
520                in ems. */
521             sp_repr_set_svg_double(repr, "startOffset", textpath->startOffset.computed);
522         }
523     }
525     if ( textpath->sourcePath->sourceHref ) repr->setAttribute("xlink:href", textpath->sourcePath->sourceHref);
526         
527     if ( flags&SP_OBJECT_WRITE_BUILD ) {
528         GSList *l = NULL;
529         for (SPObject* child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
530             Inkscape::XML::Node* c_repr=NULL;
531             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
532                 c_repr = child->updateRepr(xml_doc, NULL, flags);
533             } else if ( SP_IS_TEXTPATH(child) ) {
534                 //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
535             } else if ( SP_IS_STRING(child) ) {
536                 c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
537             }
538             if ( c_repr ) l = g_slist_prepend(l, c_repr);
539         }
540         while ( l ) {
541             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
542             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
543             l = g_slist_remove(l, l->data);
544         }
545     } else {
546         for (SPObject* child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
547             if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
548                 child->updateRepr(flags);
549             } else if ( SP_IS_TEXTPATH(child) ) {
550                 //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
551             } else if ( SP_IS_STRING(child) ) {
552                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
553             }
554         }
555     }
556         
557     if (((SPObjectClass *) textpath_parent_class)->write)
558         ((SPObjectClass *) textpath_parent_class)->write(object, xml_doc, repr, flags);
559         
560     return repr;
564 SPItem *
565 sp_textpath_get_path_item(SPTextPath *tp)
567     if (tp && tp->sourcePath) {
568         SPItem *refobj = tp->sourcePath->getObject();
569         if (SP_IS_ITEM(refobj))
570             return (SPItem *) refobj;
571     }
572     return NULL;
575 void
576 sp_textpath_to_text(SPObject *tp)
578     SPObject *text = SP_OBJECT_PARENT(tp);
580     NRRect bbox;
581     SP_ITEM(text)->invoke_bbox( &bbox, SP_ITEM(text)->i2doc_affine(), TRUE);
582     Geom::Point xy(bbox.x0, bbox.y0);
584     // make a list of textpath children
585     GSList *tp_reprs = NULL;
586     for (SPObject *o = SP_OBJECT(tp)->firstChild() ; o != NULL; o = o->next) {
587         tp_reprs = g_slist_prepend(tp_reprs, SP_OBJECT_REPR(o));
588     }
590     for ( GSList *i = tp_reprs ; i ; i = i->next ) {
591         // make a copy of each textpath child
592         Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate(SP_OBJECT_REPR(text)->document());
593         // remove the old repr from under textpath
594         SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data);
595         // put its copy under text
596         SP_OBJECT_REPR(text)->addChild(copy, NULL); // fixme: copy id
597     }
599     //remove textpath
600     tp->deleteObject();
601     g_slist_free(tp_reprs);
603     // set x/y on text
604     /* fixme: Yuck, is this really the right test? */
605     if (xy[Geom::X] != 1e18 && xy[Geom::Y] != 1e18) {
606         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[Geom::X]);
607         sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[Geom::Y]);
608     }
612 /*
613   Local Variables:
614   mode:c++
615   c-file-style:"stroustrup"
616   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
617   indent-tabs-mode:nil
618   fill-column:99
619   End:
620 */
621 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :