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 if (tspan->role != SP_TSPAN_ROLE_LINE)
152 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
153 } else {
154 switch (key) {
155 case SP_ATTR_SODIPODI_ROLE:
156 if (value && (!strcmp(value, "line") || !strcmp(value, "paragraph"))) {
157 tspan->role = SP_TSPAN_ROLE_LINE;
158 } else {
159 tspan->role = SP_TSPAN_ROLE_UNSPECIFIED;
160 }
161 break;
162 default:
163 if (((SPObjectClass *) tspan_parent_class)->set)
164 (((SPObjectClass *) tspan_parent_class)->set)(object, key, value);
165 break;
166 }
167 }
168 }
170 static void
171 sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags)
172 {
173 if (((SPObjectClass *) tspan_parent_class)->update)
174 ((SPObjectClass *) tspan_parent_class)->update(object, ctx, flags);
176 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
177 flags &= SP_OBJECT_MODIFIED_CASCADE;
179 SPObject *ochild;
180 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
181 if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
182 ochild->updateDisplay(ctx, flags);
183 }
184 }
185 }
187 static void
188 sp_tspan_modified(SPObject *object, unsigned flags)
189 {
190 if (((SPObjectClass *) tspan_parent_class)->modified)
191 ((SPObjectClass *) tspan_parent_class)->modified(object, flags);
193 if (flags & SP_OBJECT_MODIFIED_FLAG)
194 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
195 flags &= SP_OBJECT_MODIFIED_CASCADE;
197 SPObject *ochild;
198 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
199 if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
200 ochild->emitModified(flags);
201 }
202 }
203 }
205 static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
206 {
207 // find out the ancestor text which holds our layout
208 SPObject *parent_text = SP_OBJECT(item);
209 for (; parent_text != NULL && !SP_IS_TEXT(parent_text); parent_text = SP_OBJECT_PARENT (parent_text));
210 if (parent_text == NULL) return;
212 // get the bbox of our portion of the layout
213 SP_TEXT(parent_text)->layout.getBoundingBox(bbox, transform, sp_text_get_length_upto(parent_text, item), sp_text_get_length_upto(item, NULL) - 1);
215 // Add stroke width
216 SPStyle* style=SP_OBJECT_STYLE (item);
217 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
218 double const scale = expansion(transform);
219 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
220 double const width = MAX(0.125, style->stroke_width.computed * scale);
221 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
222 bbox->x0-=0.5*width;
223 bbox->x1+=0.5*width;
224 bbox->y0-=0.5*width;
225 bbox->y1+=0.5*width;
226 }
227 }
228 }
229 }
231 static Inkscape::XML::Node *
232 sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
233 {
234 SPTSpan *tspan = SP_TSPAN(object);
235 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
237 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
238 repr = xml_doc->createElement("svg:tspan");
239 }
241 tspan->attributes.writeTo(repr);
243 if ( flags&SP_OBJECT_WRITE_BUILD ) {
244 GSList *l = NULL;
245 for (SPObject* child = sp_object_first_child(object) ; 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(NULL, flags);
249 } else if ( SP_IS_TEXTPATH(child) ) {
250 //c_repr = child->updateRepr(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 = sp_object_first_child(object) ; 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(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 }
273 if (((SPObjectClass *) tspan_parent_class)->write)
274 ((SPObjectClass *) tspan_parent_class)->write(object, repr, flags);
276 return repr;
277 }
279 static char *
280 sp_tspan_description(SPItem *item)
281 {
282 g_return_val_if_fail(SP_IS_TSPAN(item), NULL);
284 return g_strdup(_("<b>Text span</b>"));
285 }
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::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()
313 {
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;
331 }
333 static void
334 sp_textpath_class_init(SPTextPathClass *classname)
335 {
336 GObjectClass *gobject_class = (GObjectClass *) classname;
337 SPObjectClass * sp_object_class;
338 SPItemClass * item_class;
340 sp_object_class = (SPObjectClass *) classname;
341 item_class = (SPItemClass *) classname;
343 textpath_parent_class = (SPItemClass*)g_type_class_ref(SP_TYPE_ITEM);
345 gobject_class->finalize = sp_textpath_finalize;
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;
353 }
355 static void
356 sp_textpath_init(SPTextPath *textpath)
357 {
358 new (&textpath->attributes) TextTagAttributes;
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;
366 }
368 static void
369 sp_textpath_finalize(GObject *obj)
370 {
371 SPTextPath *textpath = (SPTextPath *) obj;
373 delete textpath->sourcePath;
374 }
376 static void
377 sp_textpath_release(SPObject *object)
378 {
379 SPTextPath *textpath = SP_TEXTPATH(object);
381 textpath->attributes.~TextTagAttributes();
383 if (textpath->originalPath) delete textpath->originalPath;
384 textpath->originalPath = NULL;
386 if (((SPObjectClass *) textpath_parent_class)->release)
387 ((SPObjectClass *) textpath_parent_class)->release(object);
388 }
390 static void
391 sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
392 {
393 //SPTextPath *textpath = SP_TEXTPATH(object);
395 sp_object_read_attr(object, "x");
396 sp_object_read_attr(object, "y");
397 sp_object_read_attr(object, "dx");
398 sp_object_read_attr(object, "dy");
399 sp_object_read_attr(object, "rotate");
400 sp_object_read_attr(object, "startOffset");
401 sp_object_read_attr(object, "xlink:href");
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 }
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 }
414 if (((SPObjectClass *) textpath_parent_class)->build)
415 ((SPObjectClass *) textpath_parent_class)->build(object, doc, repr);
416 }
418 static void
419 sp_textpath_set(SPObject *object, unsigned key, gchar const *value)
420 {
421 SPTextPath *textpath = SP_TEXTPATH(object);
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 }
440 }
442 static void
443 sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags)
444 {
445 SPTextPath *textpath = SP_TEXTPATH(object);
447 textpath->isUpdating=true;
448 if ( textpath->sourcePath->sourceDirty ) refresh_textpath_source(textpath);
449 textpath->isUpdating=false;
451 if (((SPObjectClass *) textpath_parent_class)->update)
452 ((SPObjectClass *) textpath_parent_class)->update(object, ctx, flags);
454 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
455 flags &= SP_OBJECT_MODIFIED_CASCADE;
457 SPObject *ochild;
458 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
459 if ( flags || ( ochild->uflags & SP_OBJECT_MODIFIED_FLAG )) {
460 ochild->updateDisplay(ctx, flags);
461 }
462 }
463 }
466 void refresh_textpath_source(SPTextPath* tp)
467 {
468 if ( tp == NULL ) return;
469 tp->sourcePath->refresh_source();
470 tp->sourcePath->sourceDirty=false;
472 // finalisons
473 if ( tp->sourcePath->originalPath ) {
474 if (tp->originalPath) {
475 delete tp->originalPath;
476 }
477 tp->originalPath = NULL;
479 tp->originalPath = new Path;
480 tp->originalPath->Copy(tp->sourcePath->originalPath);
481 tp->originalPath->ConvertWithBackData(0.01);
483 }
484 }
486 static void
487 sp_textpath_modified(SPObject *object, unsigned flags)
488 {
489 if (((SPObjectClass *) textpath_parent_class)->modified)
490 ((SPObjectClass *) textpath_parent_class)->modified(object, flags);
492 if (flags & SP_OBJECT_MODIFIED_FLAG)
493 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
494 flags &= SP_OBJECT_MODIFIED_CASCADE;
496 SPObject *ochild;
497 for ( ochild = sp_object_first_child(object) ; ochild ; ochild = SP_OBJECT_NEXT(ochild) ) {
498 if (flags || (ochild->mflags & SP_OBJECT_MODIFIED_FLAG)) {
499 ochild->emitModified(flags);
500 }
501 }
502 }
503 static Inkscape::XML::Node *
504 sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
505 {
506 SPTextPath *textpath = SP_TEXTPATH(object);
507 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
509 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
510 repr = xml_doc->createElement("svg:textPath");
511 }
513 textpath->attributes.writeTo(repr);
514 if (textpath->startOffset._set) {
515 if (textpath->startOffset.unit == SVGLength::PERCENT) {
516 Inkscape::SVGOStringStream os;
517 os << (textpath->startOffset.computed * 100.0) << "%";
518 SP_OBJECT_REPR(textpath)->setAttribute("startOffset", os.str().c_str());
519 } else {
520 /* FIXME: This logic looks rather undesirable if e.g. startOffset is to be
521 in ems. */
522 sp_repr_set_svg_double(repr, "startOffset", textpath->startOffset.computed);
523 }
524 }
526 if ( textpath->sourcePath->sourceHref ) repr->setAttribute("xlink:href", textpath->sourcePath->sourceHref);
528 if ( flags&SP_OBJECT_WRITE_BUILD ) {
529 GSList *l = NULL;
530 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
531 Inkscape::XML::Node* c_repr=NULL;
532 if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
533 c_repr = child->updateRepr(NULL, flags);
534 } else if ( SP_IS_TEXTPATH(child) ) {
535 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
536 } else if ( SP_IS_STRING(child) ) {
537 c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
538 }
539 if ( c_repr ) l = g_slist_prepend(l, c_repr);
540 }
541 while ( l ) {
542 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
543 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
544 l = g_slist_remove(l, l->data);
545 }
546 } else {
547 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
548 if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
549 child->updateRepr(flags);
550 } else if ( SP_IS_TEXTPATH(child) ) {
551 //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
552 } else if ( SP_IS_STRING(child) ) {
553 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
554 }
555 }
556 }
558 if (((SPObjectClass *) textpath_parent_class)->write)
559 ((SPObjectClass *) textpath_parent_class)->write(object, repr, flags);
561 return repr;
562 }
565 SPItem *
566 sp_textpath_get_path_item(SPTextPath *tp)
567 {
568 if (tp && tp->sourcePath) {
569 SPItem *refobj = tp->sourcePath->getObject();
570 if (SP_IS_ITEM(refobj))
571 return (SPItem *) refobj;
572 }
573 return NULL;
574 }
576 void
577 sp_textpath_to_text(SPObject *tp)
578 {
579 SPObject *text = SP_OBJECT_PARENT(tp);
581 NRRect bbox;
582 sp_item_invoke_bbox(SP_ITEM(text), &bbox, sp_item_i2doc_affine(SP_ITEM(text)), TRUE);
583 NR::Point xy(bbox.x0, bbox.y0);
585 // make a list of textpath children
586 GSList *tp_reprs = NULL;
587 for (SPObject *o = SP_OBJECT(tp)->firstChild() ; o != NULL; o = o->next) {
588 tp_reprs = g_slist_prepend(tp_reprs, SP_OBJECT_REPR(o));
589 }
591 for ( GSList *i = tp_reprs ; i ; i = i->next ) {
592 // make a copy of each textpath child
593 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate(SP_OBJECT_REPR(text)->document());
594 // remove the old repr from under textpath
595 SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data);
596 // put its copy into under textPath
597 SP_OBJECT_REPR(text)->addChild(copy, NULL); // fixme: copy id
598 }
600 //remove textpath
601 tp->deleteObject();
602 g_slist_free(tp_reprs);
604 // set x/y on text
605 /* fixme: Yuck, is this really the right test? */
606 if (xy[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
607 sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[NR::X]);
608 sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[NR::Y]);
609 }
610 }
613 /*
614 Local Variables:
615 mode:c++
616 c-file-style:"stroustrup"
617 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
618 indent-tabs-mode:nil
619 fill-column:99
620 End:
621 */
622 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :