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