summaryrefslogtreecommitdiffstats
path: root/src/object/sp-root.cpp
blob: 7980c0f9088dec3c962c357b1a7ac0b0df4b87b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// SPDX-License-Identifier: GPL-2.0-or-later
/** \file
 * SVG \<svg\> implementation.
 */
/*
 * Authors:
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   Jon A. Cruz <jon@joncruz.org>
 *   Abhishek Sharma
 *
 * Copyright (C) 1999-2002 Lauris Kaplinski
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <string>
#include <2geom/transforms.h>

#include "attributes.h"
#include "print.h"
#include "document.h"
#include "inkscape-version.h"
#include "sp-defs.h"
#include "sp-root.h"
#include "display/drawing-group.h"
#include "svg/stringstream.h"
#include "svg/svg.h"
#include "xml/repr.h"
#include "util/units.h"

SPRoot::SPRoot() : SPGroup(), SPViewBox()
{
    this->onload = nullptr;

    static Inkscape::Version const zero_version(0, 0);

    sp_version_from_string(SVG_VERSION, &this->original.svg);
    this->version.svg = zero_version;
    this->original.svg = zero_version;
    this->version.inkscape = zero_version;
    this->original.inkscape = zero_version;

    this->unset_x_and_y();
    this->width.unset(SVGLength::PERCENT, 1.0, 1.0);
    this->height.unset(SVGLength::PERCENT, 1.0, 1.0);

    this->defs = nullptr;
}

SPRoot::~SPRoot()
= default;

void SPRoot::unset_x_and_y()
{
    this->x.unset(SVGLength::PERCENT, 0.0, 0.0); // Ignored for root SVG element
    this->y.unset(SVGLength::PERCENT, 0.0, 0.0);
}

void SPRoot::build(SPDocument *document, Inkscape::XML::Node *repr)
{
    //XML Tree being used directly here while it shouldn't be.
    if (!this->getRepr()->attribute("version")) {
        repr->setAttribute("version", SVG_VERSION);
    }

    this->readAttr("version");
    this->readAttr("inkscape:version");
    /* It is important to parse these here, so objects will have viewport build-time */
    this->readAttr("x");
    this->readAttr("y");
    this->readAttr("width");
    this->readAttr("height");
    this->readAttr("viewBox");
    this->readAttr("preserveAspectRatio");
    this->readAttr("onload");

    SPGroup::build(document, repr);

    // Search for first <defs> node
    for (auto& o: children) {
        if (SP_IS_DEFS(&o)) {
            this->defs = SP_DEFS(&o);
            break;
        }
    }

    // clear transform, if any was read in - SVG does not allow transform= on <svg>
    SP_ITEM(this)->transform = Geom::identity();
}

void SPRoot::release()
{
    this->defs = nullptr;

    SPGroup::release();
}


void SPRoot::set(SPAttributeEnum key, const gchar *value)
{
    switch (key) {
    case SP_ATTR_VERSION:
        if (!sp_version_from_string(value, &this->version.svg)) {
            this->version.svg = this->original.svg;
        }
        break;

    case SP_ATTR_INKSCAPE_VERSION:
        if (!sp_version_from_string(value, &this->version.inkscape)) {
            this->version.inkscape = this->original.inkscape;
        }
        break;

    case SP_ATTR_X:
        /* Valid for non-root SVG elements; ex, em not handled correctly. */
        if (!this->x.read(value)) {
            this->x.unset(SVGLength::PERCENT, 0.0, 0.0);
        }

        /* fixme: I am almost sure these do not require viewport flag (Lauris) */
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
        break;

    case SP_ATTR_Y:
        /* Valid for non-root SVG elements; ex, em not handled correctly. */
        if (!this->y.read(value)) {
            this->y.unset(SVGLength::PERCENT, 0.0, 0.0);
        }

        /* fixme: I am almost sure these do not require viewport flag (Lauris) */
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
        break;

    case SP_ATTR_WIDTH:
        if (!this->width.read(value) || !(this->width.computed > 0.0)) {
            this->width.unset(SVGLength::PERCENT, 1.0, 1.0);
        }
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
        break;

    case SP_ATTR_HEIGHT:
        if (!this->height.read(value) || !(this->height.computed > 0.0)) {
            this->height.unset(SVGLength::PERCENT, 1.0, 1.0);
        }
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
        break;

    case SP_ATTR_VIEWBOX:
        set_viewBox( value );
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
        break;

    case SP_ATTR_PRESERVEASPECTRATIO:
        set_preserveAspectRatio( value );
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
        break;

    case SP_ATTR_ONLOAD:
        this->onload = (char *) value;
        break;

    default:
        /* Pass the set event to the parent */
        SPGroup::set(key, value);
        break;
    }
}

void SPRoot::child_added(Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
{
    SPGroup::child_added(child, ref);

    SPObject *co = this->document->getObjectByRepr(child);
    // NOTE: some XML nodes do not have corresponding SP objects,
    // for instance inkscape:clipboard used in the clipboard code.
    // See LP bug #1227827
    //g_assert (co != NULL || !strcmp("comment", child->name())); // comment repr node has no object

    if (co && SP_IS_DEFS(co)) {
        // We search for first <defs> node - it is not beautiful, but works
        for (auto& c: children) {
            if (SP_IS_DEFS(&c)) {
                this->defs = SP_DEFS(&c);
                break;
            }
        }
    }
}

void SPRoot::remove_child(Inkscape::XML::Node *child)
{
    if (this->defs && (this->defs->getRepr() == child)) {
        SPObject *iter = nullptr;

        // We search for first remaining <defs> node - it is not beautiful, but works
        for (auto& child: children) {
            iter = &child;
            if (SP_IS_DEFS(iter) && (SPDefs *)iter != this->defs) {
                this->defs = (SPDefs *)iter;
                break;
            }
        }

        if (!iter) {
            /* we should probably create a new <defs> here? */
            this->defs = nullptr;
        }
    }

    SPGroup::remove_child(child);
}

void SPRoot::setRootDimensions()
{
    /*
     * This is the root SVG element:
     *
     * x, y, width, and height apply to positioning the SVG element inside a parent.
     * For the root SVG in Inkscape there is no parent, thus special rules apply:
     *   If width, height not set, width = 100%, height = 100% (as always).
     *   If width and height are in percent, they are percent of viewBox width/height.
     *   If width, height, and viewBox are not set... pick "random" width/height.
     *   x, y are ignored.
     *   initial viewport = (0 0 width height)
     */
    if( this->viewBox_set ) {

        if( this->width._set ) {
            // Check if this is necessary
            if (this->width.unit == SVGLength::PERCENT) {
                this->width.computed  = this->width.value  * this->viewBox.width();
            }
        } else {
            this->width.set( SVGLength::PX, this->viewBox.width(),  this->viewBox.width()  );
        }

        if( this->height._set ) {
            if (this->height.unit == SVGLength::PERCENT) {
                this->height.computed = this->height.value * this->viewBox.height();
            }
        } else {
            this->height.set(SVGLength::PX, this->viewBox.height(), this->viewBox.height() );
        }

    } else {

        if( !this->width._set || this->width.unit == SVGLength::PERCENT) {
            this->width.set(  SVGLength::PX, 300,  300 ); // CSS/SVG default
        }

        if( !this->height._set || this->height.unit == SVGLength::PERCENT) {
            this->height.set( SVGLength::PX, 150,  150 ); // CSS/SVG default
        }
    }

    // Ignore x, y values for root element
    this->unset_x_and_y();
}

void SPRoot::update(SPCtx *ctx, guint flags)
{
    SPItemCtx const *ictx = (SPItemCtx const *) ctx;

    if( !this->parent ) {
        this->setRootDimensions();
    }

    // Calculate x, y, width, height from parent/initial viewport
    this->calcDimsFromParentViewport(ictx);

    // std::cout << "SPRoot::update: final:"
    //           << " x: " << x.computed
    //           << " y: " << y.computed
    //           << " width: " << width.computed
    //           << " height: " << height.computed << std::endl;

    // Calculate new viewport
    SPItemCtx rctx = *ictx;
    rctx.viewport = Geom::Rect::from_xywh( this->x.computed, this->y.computed,
                                           this->width.computed, this->height.computed );
    rctx = get_rctx( &rctx, Inkscape::Util::Quantity::convert(1, this->document->getDisplayUnit(), "px") );

    /* And invoke parent method */
    SPGroup::update((SPCtx *) &rctx, flags);

    /* As last step set additional transform of drawing group */
    for (SPItemView *v = this->display; v != nullptr; v = v->next) {
        Inkscape::DrawingGroup *g = dynamic_cast<Inkscape::DrawingGroup *>(v->arenaitem);
        g->setChildTransform(this->c2p);
    }
}

void SPRoot::modified(unsigned int flags)
{
    SPGroup::modified(flags);

    /* fixme: (Lauris) */
    if (!this->parent && (flags & SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
        this->document->emitResizedSignal(this->width.computed, this->height.computed);
    }
}


Inkscape::XML::Node *SPRoot::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
{
    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
        repr = xml_doc->createElement("svg:svg");
    }

    /* Only update version string on successful write to file. This is handled by 'file_save()'.
     * if (flags & SP_OBJECT_WRITE_EXT) {
     *   repr->setAttribute("inkscape:version", Inkscape::version_string);
     * }
     */

    if (!repr->attribute("version")) {
        gchar *myversion = sp_version_to_string(this->version.svg);
        repr->setAttribute("version", myversion);
        g_free(myversion);
    }

    if (fabs(this->x.computed) > 1e-9) {
        sp_repr_set_svg_double(repr, "x", this->x.computed);
    }

    if (fabs(this->y.computed) > 1e-9) {
        sp_repr_set_svg_double(repr, "y", this->y.computed);
    }

    /* Unlike all other SPObject, here we want to preserve absolute units too (and only here,
     * according to the recommendation in http://www.w3.org/TR/SVG11/coords.html#Units).
     */
    repr->setAttribute("width", sp_svg_length_write_with_units(this->width));
    repr->setAttribute("height", sp_svg_length_write_with_units(this->height));

    if (this->viewBox_set) {
        Inkscape::SVGOStringStream os;
        os << this->viewBox.left() << " " << this->viewBox.top() << " "
           << this->viewBox.width() << " " << this->viewBox.height();

        repr->setAttribute("viewBox", os.str());
    }

    SPGroup::write(xml_doc, repr, flags);

    return repr;
}

Inkscape::DrawingItem *SPRoot::show(Inkscape::Drawing &drawing, unsigned int key, unsigned int flags)
{
    Inkscape::DrawingItem *ai = SPGroup::show(drawing, key, flags);

    if (ai) {
        Inkscape::DrawingGroup *g = dynamic_cast<Inkscape::DrawingGroup *>(ai);
        g->setChildTransform(this->c2p);
    }

    // Uncomment to print out XML tree
    // getRepr()->recursivePrintTree(0);

    // Uncomment to print out SP Object tree
    // recursivePrintTree(0);

    // Uncomment to print out Display Item tree
    // ai->recursivePrintTree(0);

    return ai;
}

void SPRoot::print(SPPrintContext *ctx)
{
    ctx->bind(this->c2p, 1.0);

    SPGroup::print(ctx);

    ctx->release();
}

const char *SPRoot::displayName() const {
    return "SVG";  // Do not translate
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :