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 <glibmm/i18n.h>
32 #include <livarot/Path.h>
33 #include "svg/stringstream.h"
34 #include "attributes.h"
35 #include "sp-use-reference.h"
36 #include "sp-tspan.h"
37 #include "sp-tref.h"
38 #include "sp-textpath.h"
39 #include "text-editing.h"
40 #include "style.h"
41 #include "libnr/nr-matrix-fns.h"
42 #include "xml/repr.h"
43 #include "document.h"
46 /*#####################################################
47 # SPTSPAN
48 #####################################################*/
50 static void sp_tspan_class_init(SPTSpanClass *classname);
51 static void sp_tspan_init(SPTSpan *tspan);
53 static void sp_tspan_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
54 static void sp_tspan_release(SPObject *object);
55 static void sp_tspan_set(SPObject *object, unsigned key, gchar const *value);
56 static void sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags);
57 static void sp_tspan_modified(SPObject *object, unsigned flags);
58 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
59 static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
60 static char *sp_tspan_description (SPItem *item);
62 static SPItemClass *tspan_parent_class;
64 /**
65 *
66 */
67 GType
68 sp_tspan_get_type()
69 {
70 static GType type = 0;
71 if (!type) {
72 GTypeInfo info = {
73 sizeof(SPTSpanClass),
74 NULL, /* base_init */
75 NULL, /* base_finalize */
76 (GClassInitFunc) sp_tspan_class_init,
77 NULL, /* class_finalize */
78 NULL, /* class_data */
79 sizeof(SPTSpan),
80 16, /* n_preallocs */
81 (GInstanceInitFunc) sp_tspan_init,
82 NULL, /* value_table */
83 };
84 type = g_type_register_static(SP_TYPE_ITEM, "SPTSpan", &info, (GTypeFlags)0);
85 }
86 return type;
87 }
89 static void
90 sp_tspan_class_init(SPTSpanClass *classname)
91 {
92 SPObjectClass * sp_object_class;
93 SPItemClass * item_class;
95 sp_object_class = (SPObjectClass *) classname;
96 item_class = (SPItemClass *) classname;
98 tspan_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
100 sp_object_class->build = sp_tspan_build;
101 sp_object_class->release = sp_tspan_release;
102 sp_object_class->set = sp_tspan_set;
103 sp_object_class->update = sp_tspan_update;
104 sp_object_class->modified = sp_tspan_modified;
105 sp_object_class->write = sp_tspan_write;
107 item_class->bbox = sp_tspan_bbox;
108 item_class->description = sp_tspan_description;
109 }
111 static void
112 sp_tspan_init(SPTSpan *tspan)
113 {
114 tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
115 new (&tspan->attributes) TextTagAttributes;
116 }
118 static void
119 sp_tspan_release(SPObject *object)
120 {
121 SPTSpan *tspan = SP_TSPAN(object);
123 tspan->attributes.~TextTagAttributes();
125 if (((SPObjectClass *) tspan_parent_class)->release)
126 ((SPObjectClass *) tspan_parent_class)->release(object);
127 }
129 static void
130 sp_tspan_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
131 {
132 //SPTSpan *tspan = SP_TSPAN(object);
134 sp_object_read_attr(object, "x");
135 sp_object_read_attr(object, "y");
136 sp_object_read_attr(object, "dx");
137 sp_object_read_attr(object, "dy");
138 sp_object_read_attr(object, "rotate");
139 sp_object_read_attr(object, "sodipodi:role");
141 if (((SPObjectClass *) tspan_parent_class)->build)
142 ((SPObjectClass *) tspan_parent_class)->build(object, doc, repr);
143 }
145 static void
146 sp_tspan_set(SPObject *object, unsigned key, gchar const *value)
147 {
148 SPTSpan *tspan = SP_TSPAN(object);
150 if (tspan->attributes.readSingleAttribute(key, value)) {
151 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
152 } else {
153 switch (key) {
154 case SP_ATTR_SODIPODI_ROLE:
155 if (value && (!strcmp(value, "line") || !strcmp(value, "paragraph"))) {
156 tspan->role = SP_TSPAN_ROLE_LINE;
157 } else {
158 tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
159 }
160 break;
161 default:
162 if (((SPObjectClass *) tspan_parent_class)->set)
163 (((SPObjectClass *) tspan_parent_class)->set)(object, key, value);
164 break;
165 }
166 }
167 }
169 static void
170 sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags)
171 {
172 if (((SPObjectClass *) tspan_parent_class)->update)
173 ((SPObjectClass *) tspan_parent_class)->update(object, ctx, flags);
175 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
176 flags &= SP_OBJECT_MODIFIED_CASCADE;
178 SPObject *ochild;
179 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
180 if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
181 ochild->updateDisplay(ctx, flags);
182 }
183 }
184 }
186 static void
187 sp_tspan_modified(SPObject *object, unsigned flags)
188 {
189 if (((SPObjectClass *) tspan_parent_class)->modified)
190 ((SPObjectClass *) tspan_parent_class)->modified(object, flags);
192 if (flags & SP_OBJECT_MODIFIED_FLAG)
193 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
194 flags &= SP_OBJECT_MODIFIED_CASCADE;
196 SPObject *ochild;
197 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
198 if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
199 ochild->emitModified(flags);
200 }
201 }
202 }
204 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
205 {
206 // find out the ancestor text which holds our layout
207 SPObject *parent_text = SP_OBJECT(item);
208 for (; parent_text != NULL && !SP_IS_TEXT(parent_text); parent_text = SP_OBJECT_PARENT (parent_text));
209 if (parent_text == NULL) return;
211 // get the bbox of our portion of the layout
212 SP_TEXT(parent_text)->layout.getBoundingBox(bbox, transform, sp_text_get_length_upto(parent_text, item), sp_text_get_length_upto(item, NULL) - 1);
214 // Add stroke width
215 SPStyle* style=SP_OBJECT_STYLE (item);
216 if (!style->stroke.isNone()) {
217 double const scale = expansion(transform);
218 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
219 double const width = MAX(0.125, style->stroke_width.computed * scale);
220 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
221 bbox->x0-=0.5*width;
222 bbox->x1+=0.5*width;
223 bbox->y0-=0.5*width;
224 bbox->y1+=0.5*width;
225 }
226 }
227 }
228 }
230 static Inkscape::XML::Node *
231 sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
232 {
233 SPTSpan *tspan = SP_TSPAN(object);
234 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
236 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
237 repr = xml_doc->createElement("svg:tspan");
238 }
240 tspan->attributes.writeTo(repr);
242 if ( flags&SP_OBJECT_WRITE_BUILD ) {
243 GSList *l = NULL;
244 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
245 Inkscape::XML::Node* c_repr=NULL;
246 if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
247 c_repr = child->updateRepr(NULL, flags);
248 } else if ( SP_IS_TEXTPATH(child) ) {
249 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
250 } else if ( SP_IS_STRING(child) ) {
251 c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
252 }
253 if ( c_repr ) l = g_slist_prepend(l, c_repr);
254 }
255 while ( l ) {
256 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
257 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
258 l = g_slist_remove(l, l->data);
259 }
260 } else {
261 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
262 if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
263 child->updateRepr(flags);
264 } else if ( SP_IS_TEXTPATH(child) ) {
265 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
266 } else if ( SP_IS_STRING(child) ) {
267 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
268 }
269 }
270 }
272 if (((SPObjectClass *) tspan_parent_class)->write)
273 ((SPObjectClass *) tspan_parent_class)->write(object, repr, flags);
275 return repr;
276 }
278 static char *
279 sp_tspan_description(SPItem *item)
280 {
281 g_return_val_if_fail(SP_IS_TSPAN(item), NULL);
283 return g_strdup(_("<b>Text span</b>"));
284 }
287 /*#####################################################
288 # SPTEXTPATH
289 #####################################################*/
291 static void sp_textpath_class_init(SPTextPathClass *classname);
292 static void sp_textpath_init(SPTextPath *textpath);
293 static void sp_textpath_finalize(GObject *obj);
295 static void sp_textpath_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
296 static void sp_textpath_release(SPObject *object);
297 static void sp_textpath_set(SPObject *object, unsigned key, gchar const *value);
298 static void sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags);
299 static void sp_textpath_modified(SPObject *object, unsigned flags);
300 static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
302 static SPItemClass *textpath_parent_class;
304 void refresh_textpath_source(SPTextPath* offset);
307 /**
308 *
309 */
310 GType
311 sp_textpath_get_type()
312 {
313 static GType type = 0;
314 if (!type) {
315 GTypeInfo info = {
316 sizeof(SPTextPathClass),
317 NULL, /* base_init */
318 NULL, /* base_finalize */
319 (GClassInitFunc) sp_textpath_class_init,
320 NULL, /* class_finalize */
321 NULL, /* class_data */
322 sizeof(SPTextPath),
323 16, /* n_preallocs */
324 (GInstanceInitFunc) sp_textpath_init,
325 NULL, /* value_table */
326 };
327 type = g_type_register_static(SP_TYPE_ITEM, "SPTextPath", &info, (GTypeFlags)0);
328 }
329 return type;
330 }
332 static void
333 sp_textpath_class_init(SPTextPathClass *classname)
334 {
335 GObjectClass *gobject_class = (GObjectClass *) classname;
336 SPObjectClass * sp_object_class;
337 SPItemClass * item_class;
339 sp_object_class = (SPObjectClass *) classname;
340 item_class = (SPItemClass *) classname;
342 textpath_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
344 gobject_class->finalize = sp_textpath_finalize;
346 sp_object_class->build = sp_textpath_build;
347 sp_object_class->release = sp_textpath_release;
348 sp_object_class->set = sp_textpath_set;
349 sp_object_class->update = sp_textpath_update;
350 sp_object_class->modified = sp_textpath_modified;
351 sp_object_class->write = sp_textpath_write;
352 }
354 static void
355 sp_textpath_init(SPTextPath *textpath)
356 {
357 new (&textpath->attributes) TextTagAttributes;
359 textpath->startOffset._set = false;
360 textpath->originalPath = NULL;
361 textpath->isUpdating=false;
362 // set up the uri reference
363 textpath->sourcePath = new SPUsePath(SP_OBJECT(textpath));
364 textpath->sourcePath->user_unlink = sp_textpath_to_text;
365 }
367 static void
368 sp_textpath_finalize(GObject *obj)
369 {
370 SPTextPath *textpath = (SPTextPath *) obj;
372 delete textpath->sourcePath;
373 }
375 static void
376 sp_textpath_release(SPObject *object)
377 {
378 SPTextPath *textpath = SP_TEXTPATH(object);
380 textpath->attributes.~TextTagAttributes();
382 if (textpath->originalPath) delete textpath->originalPath;
383 textpath->originalPath = NULL;
385 if (((SPObjectClass *) textpath_parent_class)->release)
386 ((SPObjectClass *) textpath_parent_class)->release(object);
387 }
389 static void
390 sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
391 {
392 //SPTextPath *textpath = SP_TEXTPATH(object);
394 sp_object_read_attr(object, "x");
395 sp_object_read_attr(object, "y");
396 sp_object_read_attr(object, "dx");
397 sp_object_read_attr(object, "dy");
398 sp_object_read_attr(object, "rotate");
399 sp_object_read_attr(object, "startOffset");
400 sp_object_read_attr(object, "xlink:href");
402 bool no_content=true;
403 for (Inkscape::XML::Node* rch = repr->firstChild() ; rch != NULL; rch = rch->next()) {
404 if ( rch->type() == Inkscape::XML::TEXT_NODE ) {no_content=false;break;}
405 }
407 if ( no_content ) {
408 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
409 Inkscape::XML::Node* rch = xml_doc->createTextNode("");
410 repr->addChild(rch, NULL);
411 }
413 if (((SPObjectClass *) textpath_parent_class)->build)
414 ((SPObjectClass *) textpath_parent_class)->build(object, doc, repr);
415 }
417 static void
418 sp_textpath_set(SPObject *object, unsigned key, gchar const *value)
419 {
420 SPTextPath *textpath = SP_TEXTPATH(object);
422 if (textpath->attributes.readSingleAttribute(key, value)) {
423 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
424 } else {
425 switch (key) {
426 case SP_ATTR_XLINK_HREF:
427 textpath->sourcePath->link((char*)value);
428 break;
429 case SP_ATTR_STARTOFFSET:
430 textpath->startOffset.readOrUnset(value);
431 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
432 break;
433 default:
434 if (((SPObjectClass *) textpath_parent_class)->set)
435 (((SPObjectClass *) textpath_parent_class)->set)(object, key, value);
436 break;
437 }
438 }
439 }
441 static void
442 sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags)
443 {
444 SPTextPath *textpath = SP_TEXTPATH(object);
446 textpath->isUpdating=true;
447 if ( textpath->sourcePath->sourceDirty ) refresh_textpath_source(textpath);
448 textpath->isUpdating=false;
450 if (((SPObjectClass *) textpath_parent_class)->update)
451 ((SPObjectClass *) textpath_parent_class)->update(object, ctx, flags);
453 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
454 flags &= SP_OBJECT_MODIFIED_CASCADE;
456 SPObject *ochild;
457 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
458 if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
459 ochild->updateDisplay(ctx, flags);
460 }
461 }
462 }
465 void refresh_textpath_source(SPTextPath* tp)
466 {
467 if ( tp == NULL ) return;
468 tp->sourcePath->refresh_source();
469 tp->sourcePath->sourceDirty=false;
471 // finalisons
472 if ( tp->sourcePath->originalPath ) {
473 if (tp->originalPath) {
474 delete tp->originalPath;
475 }
476 tp->originalPath = NULL;
478 tp->originalPath = new Path;
479 tp->originalPath->Copy(tp->sourcePath->originalPath);
480 tp->originalPath->ConvertWithBackData(0.01);
482 }
483 }
485 static void
486 sp_textpath_modified(SPObject *object, unsigned flags)
487 {
488 if (((SPObjectClass *) textpath_parent_class)->modified)
489 ((SPObjectClass *) textpath_parent_class)->modified(object, flags);
491 if (flags & SP_OBJECT_MODIFIED_FLAG)
492 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
493 flags &= SP_OBJECT_MODIFIED_CASCADE;
495 SPObject *ochild;
496 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
497 if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
498 ochild->emitModified(flags);
499 }
500 }
501 }
502 static Inkscape::XML::Node *
503 sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
504 {
505 SPTextPath *textpath = SP_TEXTPATH(object);
506 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
508 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
509 repr = xml_doc->createElement("svg:textPath");
510 }
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);
527 if ( flags&SP_OBJECT_WRITE_BUILD ) {
528 GSList *l = NULL;
529 for (SPObject* child = sp_object_first_child(object) ; 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(NULL, flags);
533 } else if ( SP_IS_TEXTPATH(child) ) {
534 //c_repr = child->updateRepr(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 = sp_object_first_child(object) ; 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(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 }
557 if (((SPObjectClass *) textpath_parent_class)->write)
558 ((SPObjectClass *) textpath_parent_class)->write(object, repr, flags);
560 return repr;
561 }
564 SPItem *
565 sp_textpath_get_path_item(SPTextPath *tp)
566 {
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;
573 }
575 void
576 sp_textpath_to_text(SPObject *tp)
577 {
578 SPObject *text = SP_OBJECT_PARENT(tp);
580 NRRect bbox;
581 sp_item_invoke_bbox(SP_ITEM(text), &bbox, sp_item_i2doc_affine(SP_ITEM(text)), TRUE);
582 NR::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[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
606 sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[NR::X]);
607 sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[NR::Y]);
608 }
609 }
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 :