~valavanisalex/ubuntu/precise/inkscape/fix-943984

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/sp-flowtext.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 */
 
3
 
 
4
#ifdef HAVE_CONFIG_H
 
5
# include "config.h"
 
6
#endif
 
7
#include <glibmm/i18n.h>
 
8
#include <cstring>
 
9
#include <string>
 
10
 
 
11
#include "attributes.h"
 
12
#include "xml/repr.h"
 
13
#include "style.h"
 
14
#include "inkscape.h"
 
15
#include "document.h"
 
16
#include "selection.h"
 
17
#include "desktop-handles.h"
 
18
#include "desktop.h"
 
19
 
 
20
#include "xml/repr.h"
 
21
 
 
22
#include "sp-flowdiv.h"
 
23
#include "sp-flowregion.h"
 
24
#include "sp-flowtext.h"
 
25
#include "sp-string.h"
 
26
#include "sp-use.h"
 
27
#include "sp-rect.h"
 
28
#include "text-tag-attributes.h"
 
29
#include "text-chemistry.h"
 
30
 
 
31
 
 
32
#include "livarot/Shape.h"
 
33
 
 
34
#include "display/nr-arena-glyphs.h"
 
35
 
 
36
 
 
37
static void sp_flowtext_class_init(SPFlowtextClass *klass);
 
38
static void sp_flowtext_init(SPFlowtext *group);
 
39
static void sp_flowtext_dispose(GObject *object);
 
40
 
 
41
static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
 
42
static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
 
43
static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
 
44
static void sp_flowtext_modified(SPObject *object, guint flags);
 
45
static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
46
static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
 
47
static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
 
48
 
 
49
static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
 
50
static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
 
51
static gchar *sp_flowtext_description(SPItem *item);
 
52
static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
 
53
static void sp_flowtext_hide(SPItem *item, unsigned key);
 
54
 
 
55
static SPItemClass *parent_class;
 
56
 
 
57
GType
 
58
sp_flowtext_get_type(void)
 
59
{
 
60
    static GType group_type = 0;
 
61
    if (!group_type) {
 
62
        GTypeInfo group_info = {
 
63
            sizeof(SPFlowtextClass),
 
64
            NULL,   /* base_init */
 
65
            NULL,   /* base_finalize */
 
66
            (GClassInitFunc) sp_flowtext_class_init,
 
67
            NULL,   /* class_finalize */
 
68
            NULL,   /* class_data */
 
69
            sizeof(SPFlowtext),
 
70
            16,     /* n_preallocs */
 
71
            (GInstanceInitFunc) sp_flowtext_init,
 
72
            NULL,   /* value_table */
 
73
        };
 
74
        group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
 
75
    }
 
76
    return group_type;
 
77
}
 
78
 
 
79
static void
 
80
sp_flowtext_class_init(SPFlowtextClass *klass)
 
81
{
 
82
    GObjectClass *object_class = (GObjectClass *) klass;
 
83
    SPObjectClass *sp_object_class = (SPObjectClass *) klass;
 
84
    SPItemClass *item_class = (SPItemClass *) klass;
 
85
 
 
86
    parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
 
87
 
 
88
    object_class->dispose = sp_flowtext_dispose;
 
89
 
 
90
    sp_object_class->child_added = sp_flowtext_child_added;
 
91
    sp_object_class->remove_child = sp_flowtext_remove_child;
 
92
    sp_object_class->update = sp_flowtext_update;
 
93
    sp_object_class->modified = sp_flowtext_modified;
 
94
    sp_object_class->write = sp_flowtext_write;
 
95
    sp_object_class->build = sp_flowtext_build;
 
96
    sp_object_class->set = sp_flowtext_set;
 
97
 
 
98
    item_class->bbox = sp_flowtext_bbox;
 
99
    item_class->print = sp_flowtext_print;
 
100
    item_class->description = sp_flowtext_description;
 
101
    item_class->show = sp_flowtext_show;
 
102
    item_class->hide = sp_flowtext_hide;
 
103
}
 
104
 
 
105
static void
 
106
sp_flowtext_init(SPFlowtext *group)
 
107
{
 
108
    group->par_indent = 0;
 
109
    new (&group->layout) Inkscape::Text::Layout();
 
110
}
 
111
 
 
112
static void
 
113
sp_flowtext_dispose(GObject *object)
 
114
{
 
115
    SPFlowtext *group = (SPFlowtext*)object;
 
116
 
 
117
    group->layout.~Layout();
 
118
}
 
119
 
 
120
static void
 
121
sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
 
122
{
 
123
    if (((SPObjectClass *) (parent_class))->child_added)
 
124
        (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
 
125
 
 
126
    object->requestModified(SP_OBJECT_MODIFIED_FLAG);
 
127
}
 
128
 
 
129
/* fixme: hide (Lauris) */
 
130
 
 
131
static void
 
132
sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
 
133
{
 
134
    if (((SPObjectClass *) (parent_class))->remove_child)
 
135
        (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
 
136
 
 
137
    object->requestModified(SP_OBJECT_MODIFIED_FLAG);
 
138
}
 
139
 
 
140
static void
 
141
sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
 
142
{
 
143
    SPFlowtext *group = SP_FLOWTEXT(object);
 
144
    SPItemCtx *ictx = (SPItemCtx *) ctx;
 
145
    SPItemCtx cctx = *ictx;
 
146
 
 
147
    if (((SPObjectClass *) (parent_class))->update)
 
148
        ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
 
149
 
 
150
    if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
 
151
    flags &= SP_OBJECT_MODIFIED_CASCADE;
 
152
 
 
153
    GSList *l = NULL;
 
154
    for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
 
155
        g_object_ref(G_OBJECT(child));
 
156
        l = g_slist_prepend(l, child);
 
157
    }
 
158
    l = g_slist_reverse(l);
 
159
    while (l) {
 
160
        SPObject *child = SP_OBJECT(l->data);
 
161
        l = g_slist_remove(l, child);
 
162
        if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
 
163
            if (SP_IS_ITEM(child)) {
 
164
                SPItem const &chi = *SP_ITEM(child);
 
165
                cctx.i2doc = chi.transform * ictx->i2doc;
 
166
                cctx.i2vp = chi.transform * ictx->i2vp;
 
167
                child->updateDisplay((SPCtx *)&cctx, flags);
 
168
            } else {
 
169
                child->updateDisplay(ctx, flags);
 
170
            }
 
171
        }
 
172
        g_object_unref(G_OBJECT(child));
 
173
    }
 
174
 
 
175
    group->rebuildLayout();
 
176
 
 
177
    NRRect paintbox;
 
178
    sp_item_invoke_bbox(group, &paintbox, Geom::identity(), TRUE);
 
179
    for (SPItemView *v = group->display; v != NULL; v = v->next) {
 
180
        group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
 
181
        nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
 
182
        // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
 
183
        group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
 
184
    }
 
185
}
 
186
 
 
187
static void
 
188
sp_flowtext_modified(SPObject *object, guint flags)
 
189
{
 
190
    SPObject *ft = SP_FLOWTEXT (object);
 
191
    SPObject *region = NULL;
 
192
 
 
193
    if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
 
194
    flags &= SP_OBJECT_MODIFIED_CASCADE;
 
195
 
 
196
    // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
 
197
    if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
 
198
        SPFlowtext *text = SP_FLOWTEXT(object);
 
199
        NRRect paintbox;
 
200
        sp_item_invoke_bbox(text, &paintbox, Geom::identity(), TRUE);
 
201
        for (SPItemView* v = text->display; v != NULL; v = v->next) {
 
202
            text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
 
203
            nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
 
204
            text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
 
205
        }
 
206
    }
 
207
 
 
208
    for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
 
209
        if (SP_IS_FLOWREGION(o)) {
 
210
            region = o;
 
211
            break;
 
212
        }
 
213
    }
 
214
 
 
215
    if (!region) return;
 
216
 
 
217
    if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
 
218
        region->emitModified(flags); // pass down to the region only
 
219
    }
 
220
}
 
221
 
 
222
static void
 
223
sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
 
224
{
 
225
    object->_requireSVGVersion(Inkscape::Version(1, 2));
 
226
 
 
227
    if (((SPObjectClass *) (parent_class))->build) {
 
228
        (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
 
229
    }
 
230
 
 
231
    sp_object_read_attr(object, "inkscape:layoutOptions");     // must happen after css has been read
 
232
}
 
233
 
 
234
static void
 
235
sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
 
236
{
 
237
    SPFlowtext *group = (SPFlowtext *) object;
 
238
 
 
239
    switch (key) {
 
240
        case SP_ATTR_LAYOUT_OPTIONS: {
 
241
            // deprecated attribute, read for backward compatibility only
 
242
            SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
 
243
            {
 
244
                gchar const *val = sp_repr_css_property(opts, "justification", NULL);
 
245
                if (val != NULL && !object->style->text_align.set) {
 
246
                    if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
 
247
                        object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
 
248
                    } else {
 
249
                        object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
 
250
                    }
 
251
                    object->style->text_align.set = TRUE;
 
252
                    object->style->text_align.inherit = FALSE;
 
253
                    object->style->text_align.computed = object->style->text_align.value;
 
254
                }
 
255
            }
 
256
            /* no equivalent css attribute for these two (yet)
 
257
            {
 
258
                gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
 
259
                if ( val == NULL ) {
 
260
                    group->algo = 0;
 
261
                } else {
 
262
                    if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
 
263
                        group->algo = 2;
 
264
                    } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
 
265
                        group->algo = 1;
 
266
                    } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
 
267
                        group->algo = 0;
 
268
                    }
 
269
                }
 
270
            }
 
271
            */
 
272
            {   // This would probably translate to padding-left, if SPStyle had it.
 
273
                gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
 
274
                if ( val == NULL ) {
 
275
                    group->par_indent = 0.0;
 
276
                } else {
 
277
                    sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
 
278
                }
 
279
            }
 
280
            sp_repr_css_attr_unref(opts);
 
281
            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
 
282
            break;
 
283
        }
 
284
        default:
 
285
            if (((SPObjectClass *) (parent_class))->set) {
 
286
                (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
 
287
            }
 
288
            break;
 
289
    }
 
290
}
 
291
 
 
292
static Inkscape::XML::Node *
 
293
sp_flowtext_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 
294
{
 
295
    if ( flags & SP_OBJECT_WRITE_BUILD ) {
 
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(xml_doc, 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
    }
 
317
 
 
318
    if (((SPObjectClass *) (parent_class))->write)
 
319
        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
 
320
 
 
321
    return repr;
 
322
}
 
323
 
 
324
static void
 
325
sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
 
326
{
 
327
    SPFlowtext *group = SP_FLOWTEXT(item);
 
328
    group->layout.getBoundingBox(bbox, transform);
 
329
 
 
330
    // Add stroke width
 
331
    SPStyle* style=SP_OBJECT_STYLE (item);
 
332
    if ( !style->stroke.isNone() ) {
 
333
        double const scale = transform.descrim();
 
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
}
 
345
 
 
346
static void
 
347
sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
 
348
{
 
349
    SPFlowtext *group = SP_FLOWTEXT(item);
 
350
 
 
351
    NRRect pbox;
 
352
    sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
 
353
    NRRect bbox;
 
354
    Geom::OptRect bbox_maybe = sp_item_bbox_desktop(item);
 
355
    if (!bbox_maybe) {
 
356
        return;
 
357
    }
 
358
    bbox = NRRect(from_2geom(*bbox_maybe));
 
359
 
 
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
    Geom::Matrix const ctm (sp_item_i2d_affine(item));
 
366
 
 
367
    group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
 
368
}
 
369
 
 
370
 
 
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
}
 
380
 
 
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);
 
387
 
 
388
    nr_arena_group_set_style(flowed, group->style);
 
389
 
 
390
    // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
 
391
    NRRect paintbox;
 
392
    sp_item_invoke_bbox(item, &paintbox, Geom::identity(), TRUE);
 
393
    group->layout.show(flowed, &paintbox);
 
394
 
 
395
    return flowed;
 
396
}
 
397
 
 
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
}
 
404
 
 
405
 
 
406
/*
 
407
 *
 
408
 */
 
409
 
 
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;
 
414
 
 
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
    }
 
430
 
 
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
    }
 
439
 
 
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) && !sp_repr_is_meta_element(child->repr))
 
466
            _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
 
467
    }
 
468
 
 
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
}
 
475
 
 
476
Shape* SPFlowtext::_buildExclusionShape() const
 
477
{
 
478
    Shape *shape = new Shape;
 
479
    Shape *shape_temp = new Shape;
 
480
 
 
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
}
 
497
 
 
498
void SPFlowtext::rebuildLayout()
 
499
{
 
500
    std::list<Shape> shapes;
 
501
 
 
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
}
 
510
 
 
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;
 
516
 
 
517
        nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
 
518
        nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
 
519
 
 
520
        child = nchild;
 
521
    }
 
522
}
 
523
 
 
524
Inkscape::XML::Node *
 
525
SPFlowtext::getAsText()
 
526
{
 
527
    if (!this->layout.outputExists()) return NULL;
 
528
 
 
529
    SPItem *item = SP_ITEM(this);
 
530
 
 
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
    Geom::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
 
536
    sp_repr_set_svg_double(repr, "x", anchor_point[Geom::X]);
 
537
    sp_repr_set_svg_double(repr, "y", anchor_point[Geom::Y]);
 
538
 
 
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");
 
542
 
 
543
        Inkscape::Text::Layout::iterator it_line_end = it;
 
544
        it_line_end.nextStartOfLine();
 
545
 
 
546
        while (it != it_line_end) {
 
547
 
 
548
            Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
 
549
            Geom::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.isIdentity()) {
 
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[Geom::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[Geom::Y]);
 
579
            if (line_tspan->childCount() == 0) {
 
580
                sp_repr_set_svg_double(line_tspan, "x", anchor_point[Geom::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
 
581
                sp_repr_set_svg_double(line_tspan, "y", anchor_point[Geom::Y]);
 
582
            }
 
583
 
 
584
            SPObject *source_obj = 0;
 
585
            void *rawptr = 0;
 
586
            Glib::ustring::iterator span_text_start_iter;
 
587
            this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
 
588
            source_obj = SP_OBJECT (rawptr);
 
589
            gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
 
590
            if (style_text && *style_text) {
 
591
                span_tspan->setAttribute("style", style_text);
 
592
                g_free(style_text);
 
593
            }
 
594
 
 
595
            if (SP_IS_STRING(source_obj)) {
 
596
                Glib::ustring *string = &SP_STRING(source_obj)->string;
 
597
                SPObject *span_end_obj = 0;
 
598
                void *rawptr = 0;
 
599
                Glib::ustring::iterator span_text_end_iter;
 
600
                this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
 
601
                span_end_obj = SP_OBJECT(rawptr);
 
602
                if (span_end_obj != source_obj) {
 
603
                    if (it_span_end == this->layout.end()) {
 
604
                        span_text_end_iter = span_text_start_iter;
 
605
                        for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
 
606
                            ++span_text_end_iter;
 
607
                    } else
 
608
                        span_text_end_iter = string->end();    // spans will never straddle a source boundary
 
609
                }
 
610
 
 
611
                if (span_text_start_iter != span_text_end_iter) {
 
612
                    Glib::ustring new_string;
 
613
                    while (span_text_start_iter != span_text_end_iter)
 
614
                        new_string += *span_text_start_iter++;    // grr. no substr() with iterators
 
615
                    Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
 
616
                    span_tspan->appendChild(new_text);
 
617
                    Inkscape::GC::release(new_text);
 
618
                }
 
619
            }
 
620
            it = it_span_end;
 
621
 
 
622
            line_tspan->appendChild(span_tspan);
 
623
            Inkscape::GC::release(span_tspan);
 
624
        }
 
625
        repr->appendChild(line_tspan);
 
626
        Inkscape::GC::release(line_tspan);
 
627
    }
 
628
 
 
629
    return repr;
 
630
}
 
631
 
 
632
SPItem *SPFlowtext::get_frame(SPItem *after)
 
633
{
 
634
    SPObject *ft = SP_OBJECT (this);
 
635
    SPObject *region = NULL;
 
636
 
 
637
    for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
 
638
        if (SP_IS_FLOWREGION(o)) {
 
639
            region = o;
 
640
            break;
 
641
        }
 
642
    }
 
643
 
 
644
    if (!region) return NULL;
 
645
 
 
646
    bool past = false;
 
647
    SPItem *frame = NULL;
 
648
 
 
649
    for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
 
650
        if (SP_IS_ITEM(o)) {
 
651
            if (after == NULL || past) {
 
652
                frame = SP_ITEM(o);
 
653
            } else {
 
654
                if (SP_ITEM(o) == after) {
 
655
                    past = true;
 
656
                }
 
657
            }
 
658
        }
 
659
    }
 
660
 
 
661
    if (!frame) return NULL;
 
662
 
 
663
    if (SP_IS_USE (frame)) {
 
664
        return sp_use_get_original(SP_USE(frame));
 
665
    } else {
 
666
        return frame;
 
667
    }
 
668
}
 
669
 
 
670
bool SPFlowtext::has_internal_frame()
 
671
{
 
672
    SPItem *frame = get_frame(NULL);
 
673
 
 
674
    return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
 
675
}
 
676
 
 
677
 
 
678
SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, Geom::Point p0, Geom::Point p1)
 
679
{
 
680
    SPDocument *doc = sp_desktop_document (desktop);
 
681
 
 
682
    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
 
683
    Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
 
684
    root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
 
685
    SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
 
686
    SPObject *root_object = doc->getObjectByRepr(root_repr);
 
687
    g_assert(SP_IS_FLOWTEXT(root_object));
 
688
 
 
689
    Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
 
690
    root_repr->appendChild(region_repr);
 
691
    SPObject *region_object = doc->getObjectByRepr(region_repr);
 
692
    g_assert(SP_IS_FLOWREGION(region_object));
 
693
 
 
694
    Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
 
695
    region_repr->appendChild(rect_repr);
 
696
 
 
697
    SPObject *rect = doc->getObjectByRepr(rect_repr);
 
698
 
 
699
    p0 *= desktop->dt2doc();
 
700
    p1 *= desktop->dt2doc();
 
701
    using Geom::X;
 
702
    using Geom::Y;
 
703
    Geom::Coord const x0 = MIN(p0[X], p1[X]);
 
704
    Geom::Coord const y0 = MIN(p0[Y], p1[Y]);
 
705
    Geom::Coord const x1 = MAX(p0[X], p1[X]);
 
706
    Geom::Coord const y1 = MAX(p0[Y], p1[Y]);
 
707
    Geom::Coord const w  = x1 - x0;
 
708
    Geom::Coord const h  = y1 - y0;
 
709
 
 
710
    sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
 
711
    SP_OBJECT(rect)->updateRepr();
 
712
 
 
713
    Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
 
714
    root_repr->appendChild(para_repr);
 
715
    SPObject *para_object = doc->getObjectByRepr(para_repr);
 
716
    g_assert(SP_IS_FLOWPARA(para_object));
 
717
 
 
718
    Inkscape::XML::Node *text = xml_doc->createTextNode("");
 
719
    para_repr->appendChild(text);
 
720
 
 
721
    Inkscape::GC::release(root_repr);
 
722
    Inkscape::GC::release(region_repr);
 
723
    Inkscape::GC::release(para_repr);
 
724
    Inkscape::GC::release(rect_repr);
 
725
 
 
726
    ft_item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
 
727
 
 
728
    return ft_item;
 
729
}
 
730
 
 
731
 
 
732
/*
 
733
  Local Variables:
 
734
  mode:c++
 
735
  c-file-style:"stroustrup"
 
736
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
737
  indent-tabs-mode:nil
 
738
  fill-column:99
 
739
  End:
 
740
*/
 
741
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :