1 /*
2 */
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7 #include <glibmm/i18n.h>
9 #include "attributes.h"
10 #include "xml/repr.h"
11 #include "style.h"
12 #include "inkscape.h"
13 #include "document.h"
14 #include "selection.h"
15 #include "desktop-handles.h"
16 #include "desktop.h"
17 #include "desktop-affine.h"
19 #include "xml/repr.h"
21 #include "sp-flowdiv.h"
22 #include "sp-flowregion.h"
23 #include "sp-flowtext.h"
24 #include "sp-string.h"
25 #include "sp-use.h"
26 #include "sp-rect.h"
27 #include "text-tag-attributes.h"
28 #include "text-chemistry.h"
31 #include "livarot/Shape.h"
33 #include "display/nr-arena-glyphs.h"
36 static void sp_flowtext_class_init(SPFlowtextClass *klass);
37 static void sp_flowtext_init(SPFlowtext *group);
38 static void sp_flowtext_dispose(GObject *object);
40 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
41 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
42 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
43 static void sp_flowtext_modified(SPObject *object, guint flags);
44 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
45 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
46 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
48 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
49 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
50 static gchar *sp_flowtext_description(SPItem *item);
51 static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
52 static void sp_flowtext_hide(SPItem *item, unsigned key);
54 static SPItemClass *parent_class;
56 GType
57 sp_flowtext_get_type(void)
58 {
59 static GType group_type = 0;
60 if (!group_type) {
61 GTypeInfo group_info = {
62 sizeof(SPFlowtextClass),
63 NULL, /* base_init */
64 NULL, /* base_finalize */
65 (GClassInitFunc) sp_flowtext_class_init,
66 NULL, /* class_finalize */
67 NULL, /* class_data */
68 sizeof(SPFlowtext),
69 16, /* n_preallocs */
70 (GInstanceInitFunc) sp_flowtext_init,
71 NULL, /* value_table */
72 };
73 group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
74 }
75 return group_type;
76 }
78 static void
79 sp_flowtext_class_init(SPFlowtextClass *klass)
80 {
81 GObjectClass *object_class = (GObjectClass *) klass;
82 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
83 SPItemClass *item_class = (SPItemClass *) klass;
85 parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
87 object_class->dispose = sp_flowtext_dispose;
89 sp_object_class->child_added = sp_flowtext_child_added;
90 sp_object_class->remove_child = sp_flowtext_remove_child;
91 sp_object_class->update = sp_flowtext_update;
92 sp_object_class->modified = sp_flowtext_modified;
93 sp_object_class->write = sp_flowtext_write;
94 sp_object_class->build = sp_flowtext_build;
95 sp_object_class->set = sp_flowtext_set;
97 item_class->bbox = sp_flowtext_bbox;
98 item_class->print = sp_flowtext_print;
99 item_class->description = sp_flowtext_description;
100 item_class->show = sp_flowtext_show;
101 item_class->hide = sp_flowtext_hide;
102 }
104 static void
105 sp_flowtext_init(SPFlowtext *group)
106 {
107 group->par_indent = 0;
108 new (&group->layout) Inkscape::Text::Layout();
109 }
111 static void
112 sp_flowtext_dispose(GObject *object)
113 {
114 SPFlowtext *group = (SPFlowtext*)object;
116 group->layout.~Layout();
117 }
119 static void
120 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
121 {
122 if (((SPObjectClass *) (parent_class))->child_added)
123 (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
125 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
126 }
128 /* fixme: hide (Lauris) */
130 static void
131 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
132 {
133 if (((SPObjectClass *) (parent_class))->remove_child)
134 (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
136 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
137 }
139 static void
140 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
141 {
142 SPFlowtext *group = SP_FLOWTEXT(object);
143 SPItemCtx *ictx = (SPItemCtx *) ctx;
144 SPItemCtx cctx = *ictx;
146 if (((SPObjectClass *) (parent_class))->update)
147 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
149 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
150 flags &= SP_OBJECT_MODIFIED_CASCADE;
152 GSList *l = NULL;
153 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
154 g_object_ref(G_OBJECT(child));
155 l = g_slist_prepend(l, child);
156 }
157 l = g_slist_reverse(l);
158 while (l) {
159 SPObject *child = SP_OBJECT(l->data);
160 l = g_slist_remove(l, child);
161 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
162 if (SP_IS_ITEM(child)) {
163 SPItem const &chi = *SP_ITEM(child);
164 cctx.i2doc = chi.transform * ictx->i2doc;
165 cctx.i2vp = chi.transform * ictx->i2vp;
166 child->updateDisplay((SPCtx *)&cctx, flags);
167 } else {
168 child->updateDisplay(ctx, flags);
169 }
170 }
171 g_object_unref(G_OBJECT(child));
172 }
174 group->rebuildLayout();
176 NRRect paintbox;
177 sp_item_invoke_bbox(group, &paintbox, NR::identity(), TRUE);
178 for (SPItemView *v = group->display; v != NULL; v = v->next) {
179 group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
180 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
181 // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
182 group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
183 }
184 }
186 static void
187 sp_flowtext_modified(SPObject *object, guint flags)
188 {
189 SPObject *ft = SP_FLOWTEXT (object);
190 SPObject *region = NULL;
192 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
193 flags &= SP_OBJECT_MODIFIED_CASCADE;
195 // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
196 if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
197 SPFlowtext *text = SP_FLOWTEXT(object);
198 NRRect paintbox;
199 sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
200 for (SPItemView* v = text->display; v != NULL; v = v->next) {
201 text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
202 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
203 text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
204 }
205 }
207 for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
208 if (SP_IS_FLOWREGION(o)) {
209 region = o;
210 break;
211 }
212 }
214 if (!region) return;
216 if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
217 region->emitModified(flags); // pass down to the region only
218 }
219 }
221 static void
222 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
223 {
224 object->_requireSVGVersion(Inkscape::Version(1, 2));
226 if (((SPObjectClass *) (parent_class))->build) {
227 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
228 }
230 sp_object_read_attr(object, "inkscape:layoutOptions"); // must happen after css has been read
231 }
233 static void
234 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
235 {
236 SPFlowtext *group = (SPFlowtext *) object;
238 switch (key) {
239 case SP_ATTR_LAYOUT_OPTIONS: {
240 // deprecated attribute, read for backward compatibility only
241 SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
242 {
243 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
244 if (val != NULL && !object->style->text_align.set) {
245 if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
246 object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
247 } else {
248 object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
249 }
250 object->style->text_align.set = TRUE;
251 object->style->text_align.inherit = FALSE;
252 object->style->text_align.computed = object->style->text_align.value;
253 }
254 }
255 /* no equivalent css attribute for these two (yet)
256 {
257 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
258 if ( val == NULL ) {
259 group->algo = 0;
260 } else {
261 if ( strcmp(val, "better") == 0 ) { // knuth-plass, never worked for general cases
262 group->algo = 2;
263 } else if ( strcmp(val, "simple") == 0 ) { // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
264 group->algo = 1;
265 } else if ( strcmp(val, "default") == 0 ) { // the same one we use, a standard greedy
266 group->algo = 0;
267 }
268 }
269 }
270 */
271 { // This would probably translate to padding-left, if SPStyle had it.
272 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
273 if ( val == NULL ) {
274 group->par_indent = 0.0;
275 } else {
276 sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
277 }
278 }
279 sp_repr_css_attr_unref(opts);
280 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
281 break;
282 }
283 default:
284 if (((SPObjectClass *) (parent_class))->set) {
285 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
286 }
287 break;
288 }
289 }
291 static Inkscape::XML::Node *
292 sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
293 {
294 if ( flags & SP_OBJECT_WRITE_BUILD ) {
295 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
296 if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
297 GSList *l = NULL;
298 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
299 Inkscape::XML::Node *c_repr = NULL;
300 if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
301 c_repr = child->updateRepr(NULL, flags);
302 }
303 if ( c_repr ) l = g_slist_prepend(l, c_repr);
304 }
305 while ( l ) {
306 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
307 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
308 l = g_slist_remove(l, l->data);
309 }
310 } else {
311 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
312 if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
313 child->updateRepr(flags);
314 }
315 }
316 }
318 if (((SPObjectClass *) (parent_class))->write)
319 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
321 return repr;
322 }
324 static void
325 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
326 {
327 SPFlowtext *group = SP_FLOWTEXT(item);
328 group->layout.getBoundingBox(bbox, transform);
330 // Add stroke width
331 SPStyle* style=SP_OBJECT_STYLE (item);
332 if ( !style->stroke.isNone() ) {
333 double const scale = expansion(transform);
334 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
335 double const width = MAX(0.125, style->stroke_width.computed * scale);
336 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
337 bbox->x0-=0.5*width;
338 bbox->x1+=0.5*width;
339 bbox->y0-=0.5*width;
340 bbox->y1+=0.5*width;
341 }
342 }
343 }
344 }
346 static void
347 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
348 {
349 SPFlowtext *group = SP_FLOWTEXT(item);
351 NRRect pbox;
352 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
353 NRRect bbox;
354 NR::Maybe<NR::Rect> bbox_maybe = sp_item_bbox_desktop(item);
355 if (!bbox_maybe) {
356 return;
357 }
358 bbox = NRRect(*bbox_maybe);
360 NRRect dbox;
361 dbox.x0 = 0.0;
362 dbox.y0 = 0.0;
363 dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
364 dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
365 NR::Matrix const ctm = sp_item_i2d_affine(item);
367 group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
368 }
371 static gchar *sp_flowtext_description(SPItem *item)
372 {
373 Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
374 int const nChars = layout.iteratorToCharIndex(layout.end());
375 if (SP_FLOWTEXT(item)->has_internal_frame())
376 return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
377 else
378 return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
379 }
381 static NRArenaItem *
382 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
383 {
384 SPFlowtext *group = (SPFlowtext *) item;
385 NRArenaGroup *flowed = NRArenaGroup::create(arena);
386 nr_arena_group_set_transparent(flowed, FALSE);
388 nr_arena_group_set_style(flowed, group->style);
390 // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
391 NRRect paintbox;
392 sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
393 group->layout.show(flowed, &paintbox);
395 return flowed;
396 }
398 static void
399 sp_flowtext_hide(SPItem *item, unsigned int key)
400 {
401 if (((SPItemClass *) parent_class)->hide)
402 ((SPItemClass *) parent_class)->hide(item, key);
403 }
406 /*
407 *
408 */
410 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
411 {
412 Inkscape::Text::Layout::OptionalTextTagAttrs pi;
413 bool with_indent = false;
415 if (SP_IS_FLOWPARA(root)) {
416 // emulate par-indent with the first char's kern
417 SPObject *t = root;
418 for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
419 if (SP_IS_FLOWTEXT(t)) {
420 double indent = SP_FLOWTEXT(t)->par_indent;
421 if (indent != 0) {
422 with_indent = true;
423 SVGLength sl;
424 sl.value = sl.computed = indent;
425 sl._set = true;
426 pi.dx.push_back(sl);
427 }
428 }
429 }
431 if (*pending_line_break_object) {
432 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
433 layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
434 } else {
435 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
436 }
437 *pending_line_break_object = NULL;
438 }
440 for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
441 if (SP_IS_STRING(child)) {
442 if (*pending_line_break_object) {
443 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
444 layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
445 else {
446 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
447 }
448 *pending_line_break_object = NULL;
449 }
450 if (with_indent)
451 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
452 else
453 layout.appendText(SP_STRING(child)->string, root->style, child);
454 } else if (SP_IS_FLOWREGION(child)) {
455 std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
456 for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
457 shapes->push_back(Shape());
458 if (exclusion_shape->hasEdges())
459 shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
460 else
461 shapes->back().Copy(*it);
462 layout.appendWrapShape(&shapes->back());
463 }
464 }
465 else if (!SP_IS_FLOWREGIONEXCLUDE(child))
466 _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
467 }
469 if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
470 if (!root->hasChildren())
471 layout.appendText("", root->style, root);
472 *pending_line_break_object = root;
473 }
474 }
476 Shape* SPFlowtext::_buildExclusionShape() const
477 {
478 Shape *shape = new Shape;
479 Shape *shape_temp = new Shape;
481 for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
482 // RH: is it right that this shouldn't be recursive?
483 if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
484 SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
485 if (c_child->computed == NULL || !c_child->computed->hasEdges())
486 continue;
487 if (shape->hasEdges()) {
488 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
489 std::swap(shape, shape_temp);
490 } else
491 shape->Copy(c_child->computed);
492 }
493 }
494 delete shape_temp;
495 return shape;
496 }
498 void SPFlowtext::rebuildLayout()
499 {
500 std::list<Shape> shapes;
502 layout.clear();
503 Shape *exclusion_shape = _buildExclusionShape();
504 SPObject *pending_line_break_object = NULL;
505 _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
506 delete exclusion_shape;
507 layout.calculateFlow();
508 //g_print(layout.dumpAsText().c_str());
509 }
511 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
512 {
513 nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
514 for (NRArenaItem *child = in_arena->children; child != NULL; ) {
515 NRArenaItem *nchild = child->next;
517 nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
518 nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
520 child = nchild;
521 }
522 }
524 Inkscape::XML::Node *
525 SPFlowtext::getAsText()
526 {
527 if (!this->layout.outputExists()) return NULL;
529 SPItem *item = SP_ITEM(this);
531 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
532 Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
533 repr->setAttribute("xml:space", "preserve");
534 repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
535 NR::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
536 sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
537 sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
539 for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
540 Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
541 line_tspan->setAttribute("sodipodi:role", "line");
543 Inkscape::Text::Layout::iterator it_line_end = it;
544 it_line_end.nextStartOfLine();
546 while (it != it_line_end) {
548 Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
549 NR::Point anchor_point = this->layout.characterAnchorPoint(it);
550 // use kerning to simulate justification and whatnot
551 Inkscape::Text::Layout::iterator it_span_end = it;
552 it_span_end.nextStartOfSpan();
553 Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
554 this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
555 // set x,y attributes only when we need to
556 bool set_x = false;
557 bool set_y = false;
558 if (!item->transform.test_identity()) {
559 set_x = set_y = true;
560 } else {
561 Inkscape::Text::Layout::iterator it_chunk_start = it;
562 it_chunk_start.thisStartOfChunk();
563 if (it == it_chunk_start) {
564 set_x = true;
565 // don't set y so linespacing adjustments and things will still work
566 }
567 Inkscape::Text::Layout::iterator it_shape_start = it;
568 it_shape_start.thisStartOfShape();
569 if (it == it_shape_start)
570 set_y = true;
571 }
572 if (set_x && !attrs.dx.empty())
573 attrs.dx[0] = 0.0;
574 TextTagAttributes(attrs).writeTo(span_tspan);
575 if (set_x)
576 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]); // FIXME: this will pick up the wrong end of counter-directional runs
577 if (set_y)
578 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
580 SPObject *source_obj = 0;
581 void *rawptr = 0;
582 Glib::ustring::iterator span_text_start_iter;
583 this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
584 source_obj = SP_OBJECT (rawptr);
585 gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
586 if (style_text && *style_text) {
587 span_tspan->setAttribute("style", style_text);
588 g_free(style_text);
589 }
591 if (SP_IS_STRING(source_obj)) {
592 Glib::ustring *string = &SP_STRING(source_obj)->string;
593 SPObject *span_end_obj = 0;
594 void *rawptr = 0;
595 Glib::ustring::iterator span_text_end_iter;
596 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
597 span_end_obj = SP_OBJECT(rawptr);
598 if (span_end_obj != source_obj) {
599 if (it_span_end == this->layout.end()) {
600 span_text_end_iter = span_text_start_iter;
601 for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
602 ++span_text_end_iter;
603 } else
604 span_text_end_iter = string->end(); // spans will never straddle a source boundary
605 }
607 if (span_text_start_iter != span_text_end_iter) {
608 Glib::ustring new_string;
609 while (span_text_start_iter != span_text_end_iter)
610 new_string += *span_text_start_iter++; // grr. no substr() with iterators
611 Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
612 span_tspan->appendChild(new_text);
613 Inkscape::GC::release(new_text);
614 }
615 }
616 it = it_span_end;
618 line_tspan->appendChild(span_tspan);
619 Inkscape::GC::release(span_tspan);
620 }
621 repr->appendChild(line_tspan);
622 Inkscape::GC::release(line_tspan);
623 }
625 return repr;
626 }
628 SPItem *SPFlowtext::get_frame(SPItem *after)
629 {
630 SPObject *ft = SP_OBJECT (this);
631 SPObject *region = NULL;
633 for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
634 if (SP_IS_FLOWREGION(o)) {
635 region = o;
636 break;
637 }
638 }
640 if (!region) return NULL;
642 bool past = false;
643 SPItem *frame = NULL;
645 for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
646 if (SP_IS_ITEM(o)) {
647 if (after == NULL || past) {
648 frame = SP_ITEM(o);
649 } else {
650 if (SP_ITEM(o) == after) {
651 past = true;
652 }
653 }
654 }
655 }
657 if (!frame) return NULL;
659 if (SP_IS_USE (frame)) {
660 return sp_use_get_original(SP_USE(frame));
661 } else {
662 return frame;
663 }
664 }
666 bool SPFlowtext::has_internal_frame()
667 {
668 SPItem *frame = get_frame(NULL);
670 return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
671 }
674 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
675 {
676 SPDocument *doc = sp_desktop_document (desktop);
678 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
679 Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
680 root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
681 SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
682 SPObject *root_object = doc->getObjectByRepr(root_repr);
683 g_assert(SP_IS_FLOWTEXT(root_object));
685 Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
686 root_repr->appendChild(region_repr);
687 SPObject *region_object = doc->getObjectByRepr(region_repr);
688 g_assert(SP_IS_FLOWREGION(region_object));
690 Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
691 region_repr->appendChild(rect_repr);
693 SPObject *rect = doc->getObjectByRepr(rect_repr);
695 p0 = sp_desktop_dt2root_xy_point(desktop, p0);
696 p1 = sp_desktop_dt2root_xy_point(desktop, p1);
697 using NR::X;
698 using NR::Y;
699 NR::Coord const x0 = MIN(p0[X], p1[X]);
700 NR::Coord const y0 = MIN(p0[Y], p1[Y]);
701 NR::Coord const x1 = MAX(p0[X], p1[X]);
702 NR::Coord const y1 = MAX(p0[Y], p1[Y]);
703 NR::Coord const w = x1 - x0;
704 NR::Coord const h = y1 - y0;
706 sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
707 SP_OBJECT(rect)->updateRepr();
709 Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
710 root_repr->appendChild(para_repr);
711 SPObject *para_object = doc->getObjectByRepr(para_repr);
712 g_assert(SP_IS_FLOWPARA(para_object));
714 Inkscape::XML::Node *text = xml_doc->createTextNode("");
715 para_repr->appendChild(text);
717 Inkscape::GC::release(root_repr);
718 Inkscape::GC::release(region_repr);
719 Inkscape::GC::release(para_repr);
720 Inkscape::GC::release(rect_repr);
722 return ft_item;
723 }
726 /*
727 Local Variables:
728 mode:c++
729 c-file-style:"stroustrup"
730 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
731 indent-tabs-mode:nil
732 fill-column:99
733 End:
734 */
735 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :