rsvg-paint-server.c 18 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 ts=4 expandtab: */
3 4 5 6 7 8
/* 
   rsvg-paint-server.c: Implement the SVG paint server abstraction.
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
9
   modify it under the terms of the GNU Library General Public License as
10 11 12 13 14 15
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
   Library General Public License for more details.
17
  
18
   You should have received a copy of the GNU Library General Public
19 20 21 22 23 24 25
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Author: Raph Levien <raph@artofcode.com>
*/

26
#include "config.h"
27
#include "rsvg-private.h"
28 29
#include "rsvg-defs.h"
#include "rsvg-paint-server.h"
30
#include "rsvg-styles.h"
31
#include "rsvg-image.h"
32

33
#include <glib.h>
34
#include <string.h>
35 36
#include <math.h>

37 38 39
#include "rsvg-css.h"

static RsvgPaintServer *
40
rsvg_paint_server_solid (guint32 argb)
41
{
42 43 44 45
    RsvgPaintServer *result = g_new (RsvgPaintServer, 1);

    result->refcnt = 1;
    result->type = RSVG_PAINT_SERVER_SOLID;
46 47 48
    result->core.color = g_new (RsvgSolidColor, 1);
    result->core.color->argb = argb;
    result->core.color->currentcolor = FALSE;
49 50

    return result;
51 52 53
}

static RsvgPaintServer *
54
rsvg_paint_server_solid_current_color (void)
55
{
56 57 58 59
    RsvgPaintServer *result = g_new (RsvgPaintServer, 1);

    result->refcnt = 1;
    result->type = RSVG_PAINT_SERVER_SOLID;
60 61
    result->core.color = g_new (RsvgSolidColor, 1);
    result->core.color->currentcolor = TRUE;
62 63

    return result;
64 65 66
}

static RsvgPaintServer *
67
rsvg_paint_server_iri (char *iri)
68
{
69 70 71
    RsvgPaintServer *result = g_new (RsvgPaintServer, 1);

    result->refcnt = 1;
72 73
    result->type = RSVG_PAINT_SERVER_IRI;
    result->core.iri = iri;
74 75

    return result;
76 77
}

78
/**
79
 * rsvg_paint_server_parse:
80 81 82 83 84
 * @str: The SVG paint specification string to parse.
 *
 * Parses the paint specification @str, creating a new paint server
 * object.
 *
85 86
 * Return value: (nullable): The newly created paint server, or %NULL
 *   on error.
87 88
 **/
RsvgPaintServer *
89
rsvg_paint_server_parse (gboolean * inherit, const char *str)
90
{
91
    char *name;
92
    guint32 argb;
93 94
    if (inherit != NULL)
        *inherit = 1;
95
    if (str == NULL || !strcmp (str, "none"))
96 97 98 99
        return NULL;

    name = rsvg_get_url_string (str);
    if (name) {
100
        return rsvg_paint_server_iri (name);
101 102 103 104 105 106
    } else if (!strcmp (str, "inherit")) {
        if (inherit != NULL)
            *inherit = 0;
        return rsvg_paint_server_solid (0);
    } else if (!strcmp (str, "currentColor")) {
        RsvgPaintServer *ps;
107
        ps = rsvg_paint_server_solid_current_color ();
108 109
        return ps;
    } else {
110 111
        argb = rsvg_css_parse_color (str, inherit);
        return rsvg_paint_server_solid (argb);
112
    }
113 114 115
}

/**
116
 * rsvg_paint_server_ref:
117
 * @ps: The paint server object to reference.
118 119
 *
 * Reference a paint server object.
120 121
 **/
void
122
rsvg_paint_server_ref (RsvgPaintServer * ps)
123
{
124 125 126
    if (ps == NULL)
        return;
    ps->refcnt++;
127 128 129
}

/**
130
 * rsvg_paint_server_unref:
131
 * @ps: The paint server object to unreference.
132 133
 *
 * Unreference a paint server object.
134 135
 **/
void
136
rsvg_paint_server_unref (RsvgPaintServer * ps)
137
{
138 139 140 141
    if (ps == NULL)
        return;
    if (--ps->refcnt == 0) {
        if (ps->type == RSVG_PAINT_SERVER_SOLID)
142
            g_free (ps->core.color);
143 144
        else if (ps->type == RSVG_PAINT_SERVER_IRI)
            g_free (ps->core.iri);
145 146
        g_free (ps);
    }
147
}
148

149
static void
150
rsvg_stop_set_atts (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag * atts)
151
{
152 153
    gboolean has_stop_color = FALSE;
    gboolean has_stop_opacity = FALSE;
154 155 156
    gboolean is_current_color = FALSE;
    const char *value;
    RsvgGradientStop *stop;
157
    RsvgState *state;
158 159
    int opacity;
    guint32 color;
160 161 162

    stop = (RsvgGradientStop *) self;

163 164 165
    if ((value = rsvg_property_bag_lookup (atts, "offset"))) {
        /* either a number [0,1] or a percentage */
        RsvgLength length = rsvg_length_parse (value, LENGTH_DIR_BOTH);
166

167 168
        if (length.unit == LENGTH_UNIT_DEFAULT || length.unit == LENGTH_UNIT_PERCENT) {
            double offset;
169

170
            offset = length.length;
171

172 173 174 175 176 177 178 179 180 181
            if (offset < 0.0)
                offset = 0.0;
            else if (offset > 1.0)
                offset = 1.0;

            stop->offset = offset;
            stop->is_valid = TRUE;
        } else {
            /* Only default and percent values are allowed */
            stop->is_valid = FALSE;
182
        }
183 184
    }
    if ((value = rsvg_property_bag_lookup (atts, "style")))
185
        rsvg_parse_style (ctx, rsvg_node_get_state (self), value);
186 187 188 189 190 191 192

    if ((value = rsvg_property_bag_lookup (atts, "stop-color"))) {
        has_stop_color = TRUE;

        if (!strcmp (value, "currentColor"))
            is_current_color = TRUE;
    }
193

194 195
    if ((value = rsvg_property_bag_lookup (atts, "stop-opacity"))) {
        has_stop_opacity = TRUE;
196
    }
197

198
    rsvg_parse_style_pairs (ctx, rsvg_node_get_state (self), atts);
199

200 201 202
    state = rsvg_state_new ();
    rsvg_state_reconstruct (state, self);

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    if (has_stop_color) {
        if (is_current_color)
            color = state->current_color;
        else {
            color = state->stop_color & 0x00ffffff;
        }
    } else {
        color = 0x0;
    }

    if (has_stop_opacity) {
        opacity = state->stop_opacity;
    } else {
        opacity = 0xff;
    }

    stop->rgba = (color << 8) | opacity;
220 221

    rsvg_state_free (state);
222 223
}

224
RsvgNode *
225
rsvg_new_stop (const char *element_name)
226
{
227
    RsvgGradientStop *stop = g_new (RsvgGradientStop, 1);
228 229 230 231 232 233 234 235
    RsvgNodeVtable vtable = {
        NULL,
        NULL,
        rsvg_stop_set_atts
    };

    _rsvg_node_init (&stop->super, RSVG_NODE_TYPE_STOP, &vtable);

236
    stop->offset = 0;
237
    stop->rgba = 0xff000000;
238
    stop->is_valid = FALSE;
239
    return &stop->super;
240 241 242
}

static void
243
rsvg_linear_gradient_set_atts (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag * atts)
244
{
245 246 247
    RsvgLinearGradient *grad = (RsvgLinearGradient *) self;
    const char *value;

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    if ((value = rsvg_property_bag_lookup (atts, "x1"))) {
        grad->x1 = rsvg_length_parse (value, LENGTH_DIR_HORIZONTAL);
        grad->hasx1 = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "y1"))) {
        grad->y1 = rsvg_length_parse (value, LENGTH_DIR_VERTICAL);
        grad->hasy1 = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "x2"))) {
        grad->x2 = rsvg_length_parse (value, LENGTH_DIR_HORIZONTAL);
        grad->hasx2 = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "y2"))) {
        grad->y2 = rsvg_length_parse (value, LENGTH_DIR_VERTICAL);
        grad->hasy2 = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "spreadMethod"))) {
        if (!strcmp (value, "pad")) {
            grad->spread = CAIRO_EXTEND_PAD;
        } else if (!strcmp (value, "reflect")) {
            grad->spread = CAIRO_EXTEND_REFLECT;
        } else if (!strcmp (value, "repeat")) {
            grad->spread = CAIRO_EXTEND_REPEAT;
271
        }
272 273 274 275 276 277 278 279 280 281 282 283 284 285
        grad->hasspread = TRUE;
    }
    g_free (grad->fallback);
    grad->fallback = g_strdup (rsvg_property_bag_lookup (atts, "xlink:href"));
    if ((value = rsvg_property_bag_lookup (atts, "gradientTransform"))) {
        rsvg_parse_transform (&grad->affine, value);
        grad->hastransform = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "gradientUnits"))) {
        if (!strcmp (value, "userSpaceOnUse"))
            grad->obj_bbox = FALSE;
        else if (!strcmp (value, "objectBoundingBox"))
            grad->obj_bbox = TRUE;
        grad->hasbbox = TRUE;
286
    }
287 288
}

289 290 291 292 293 294 295
static void
rsvg_linear_gradient_free (RsvgNode * node)
{
    RsvgLinearGradient *self = (RsvgLinearGradient *) node;
    g_free (self->fallback);
    _rsvg_node_free (node);
}
296 297

RsvgNode *
298
rsvg_new_linear_gradient (const char *element_name)
299
{
300
    RsvgLinearGradient *grad = NULL;
301 302 303 304 305 306
    RsvgNodeVtable vtable = {
        rsvg_linear_gradient_free,
        NULL,
        rsvg_linear_gradient_set_atts
    };

307
    grad = g_new (RsvgLinearGradient, 1);
308 309
    _rsvg_node_init (&grad->super, RSVG_NODE_TYPE_LINEAR_GRADIENT, &vtable);

Christian Persch's avatar
Christian Persch committed
310
    cairo_matrix_init_identity (&grad->affine);
311 312 313
    grad->x1 = rsvg_length_parse ("0", LENGTH_DIR_HORIZONTAL);
    grad->y1 = grad->y2 = rsvg_length_parse ("0", LENGTH_DIR_VERTICAL);
    grad->x2 = rsvg_length_parse ("1", LENGTH_DIR_HORIZONTAL);
314 315
    grad->fallback = NULL;
    grad->obj_bbox = TRUE;
316
    grad->spread = CAIRO_EXTEND_PAD;
317 318 319
    grad->hasx1 = grad->hasy1 = grad->hasx2 = grad->hasy2 = grad->hasbbox = grad->hasspread =
        grad->hastransform = FALSE;
    return &grad->super;
320 321
}

322
static void
323
rsvg_radial_gradient_set_atts (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag * atts)
324
{
325 326 327
    RsvgRadialGradient *grad = (RsvgRadialGradient *) self;
    const char *value;

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
    if ((value = rsvg_property_bag_lookup (atts, "cx"))) {
        grad->cx = rsvg_length_parse (value, LENGTH_DIR_HORIZONTAL);
        grad->hascx = TRUE;
        if (!grad->hasfx)
            grad->fx = grad->cx;
    }
    if ((value = rsvg_property_bag_lookup (atts, "cy"))) {
        grad->cy = rsvg_length_parse (value, LENGTH_DIR_VERTICAL);
        grad->hascy = TRUE;
        if (!grad->hasfy)
            grad->fy = grad->cy;
    }
    if ((value = rsvg_property_bag_lookup (atts, "r"))) {
        grad->r = rsvg_length_parse (value, LENGTH_DIR_BOTH);
        grad->hasr = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "fx"))) {
        grad->fx = rsvg_length_parse (value, LENGTH_DIR_HORIZONTAL);
        grad->hasfx = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "fy"))) {
        grad->fy = rsvg_length_parse (value, LENGTH_DIR_VERTICAL);
        grad->hasfy = TRUE;
    }
    g_free (grad->fallback);
    grad->fallback = g_strdup (rsvg_property_bag_lookup (atts, "xlink:href"));
    if ((value = rsvg_property_bag_lookup (atts, "gradientTransform"))) {
        rsvg_parse_transform (&grad->affine, value);
        grad->hastransform = TRUE;
357
    }
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    if ((value = rsvg_property_bag_lookup (atts, "spreadMethod"))) {
        if (!strcmp (value, "pad"))
            grad->spread = CAIRO_EXTEND_PAD;
        else if (!strcmp (value, "reflect"))
            grad->spread = CAIRO_EXTEND_REFLECT;
        else if (!strcmp (value, "repeat"))
            grad->spread = CAIRO_EXTEND_REPEAT;
        grad->hasspread = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "gradientUnits"))) {
        if (!strcmp (value, "userSpaceOnUse"))
            grad->obj_bbox = FALSE;
        else if (!strcmp (value, "objectBoundingBox"))
            grad->obj_bbox = TRUE;
        grad->hasbbox = TRUE;
    }
374 375
}

376 377 378 379 380 381 382 383
static void
rsvg_radial_gradient_free (RsvgNode * node)
{
    RsvgRadialGradient *self = (RsvgRadialGradient *) node;
    g_free (self->fallback);
    _rsvg_node_free (node);
}

384
RsvgNode *
385
rsvg_new_radial_gradient (const char *element_name)
386
{
387

388 389 390 391 392 393
    RsvgNodeVtable vtable = {
        rsvg_radial_gradient_free,
        NULL,
        rsvg_radial_gradient_set_atts
    };

394
    RsvgRadialGradient *grad = g_new (RsvgRadialGradient, 1);
395 396
    _rsvg_node_init (&grad->super, RSVG_NODE_TYPE_RADIAL_GRADIENT, &vtable);

Christian Persch's avatar
Christian Persch committed
397
    cairo_matrix_init_identity (&grad->affine);
398
    grad->obj_bbox = TRUE;
399
    grad->spread = CAIRO_EXTEND_PAD;
400
    grad->fallback = NULL;
401
    grad->cx = grad->cy = grad->r = grad->fx = grad->fy = rsvg_length_parse ("0.5", LENGTH_DIR_BOTH);
402 403 404
    grad->hascx = grad->hascy = grad->hasfx = grad->hasfy = grad->hasr = grad->hasbbox =
        grad->hasspread = grad->hastransform = FALSE;
    return &grad->super;
405 406
}

407
static void
408
rsvg_pattern_set_atts (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag * atts)
409
{
410 411 412
    RsvgPattern *pattern = (RsvgPattern *) self;
    const char *value;

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    if ((value = rsvg_property_bag_lookup (atts, "viewBox"))) {
        pattern->vbox = rsvg_css_parse_vbox (value);
        pattern->hasvbox = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "x"))) {
        pattern->x = rsvg_length_parse (value, LENGTH_DIR_HORIZONTAL);
        pattern->hasx = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "y"))) {
        pattern->y = rsvg_length_parse (value, LENGTH_DIR_VERTICAL);
        pattern->hasy = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "width"))) {
        pattern->width = rsvg_length_parse (value, LENGTH_DIR_HORIZONTAL);
        pattern->haswidth = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "height"))) {
        pattern->height = rsvg_length_parse (value, LENGTH_DIR_VERTICAL);
        pattern->hasheight = TRUE;
    }
    g_free (pattern->fallback);
    pattern->fallback = g_strdup (rsvg_property_bag_lookup (atts, "xlink:href"));
    if ((value = rsvg_property_bag_lookup (atts, "patternTransform"))) {
        rsvg_parse_transform (&pattern->affine, value);
        pattern->hastransform = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "patternUnits"))) {
        if (!strcmp (value, "userSpaceOnUse"))
            pattern->obj_bbox = FALSE;
        else if (!strcmp (value, "objectBoundingBox"))
            pattern->obj_bbox = TRUE;
        pattern->hasbbox = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "patternContentUnits"))) {
        if (!strcmp (value, "userSpaceOnUse"))
            pattern->obj_cbbox = FALSE;
        else if (!strcmp (value, "objectBoundingBox"))
            pattern->obj_cbbox = TRUE;
        pattern->hascbox = TRUE;
    }
    if ((value = rsvg_property_bag_lookup (atts, "preserveAspectRatio"))) {
        pattern->preserve_aspect_ratio = rsvg_css_parse_aspect_ratio (value);
        pattern->hasaspect = TRUE;
456
    }
457
}
458

459 460 461 462 463 464 465 466
static void
rsvg_pattern_free (RsvgNode * node)
{
    RsvgPattern *self = (RsvgPattern *) node;
    g_free (self->fallback);
    _rsvg_node_free (node);
}

467

468
RsvgNode *
469
rsvg_new_pattern (const char *element_name)
470
{
471
    RsvgPattern *pattern = g_new (RsvgPattern, 1);
472 473 474 475 476 477 478 479
    RsvgNodeVtable vtable = {
        rsvg_pattern_free,
        NULL,
        rsvg_pattern_set_atts
    };

    _rsvg_node_init (&pattern->super, RSVG_NODE_TYPE_PATTERN, &vtable);

Christian Persch's avatar
Christian Persch committed
480
    cairo_matrix_init_identity (&pattern->affine);
481 482
    pattern->obj_bbox = TRUE;
    pattern->obj_cbbox = FALSE;
483
    pattern->x = pattern->y = pattern->width = pattern->height = rsvg_length_parse ("0", LENGTH_DIR_BOTH);
484 485 486 487
    pattern->fallback = NULL;
    pattern->preserve_aspect_ratio = RSVG_ASPECT_RATIO_XMID_YMID;
    pattern->vbox.active = FALSE;
    pattern->hasx = pattern->hasy = pattern->haswidth = pattern->hasheight = pattern->hasbbox =
Vincent Untz's avatar
Vincent Untz committed
488
        pattern->hascbox = pattern->hasvbox = pattern->hasaspect = pattern->hastransform = FALSE;
489
    return &pattern->super;
490
}
491

492 493
typedef const char * (* GetFallbackFn) (RsvgNode *node);
typedef void (* ApplyFallbackFn) (RsvgNode *node, RsvgNode *fallback_node);
494 495 496 497 498

/* Some SVG paint servers can reference a "parent" or "fallback" paint server
 * through the xlink:href attribute (for example,
 * http://www.w3.org/TR/SVG11/pservers.html#LinearGradientElementHrefAttribute )
 * This is used to define a chain of properties to be resolved from each
499
 * fallback.
500 501
 */
static void
502
resolve_fallbacks (RsvgDrawingCtx *ctx,
503 504
                   RsvgNode *data,
                   RsvgNode *last_fallback,
505 506
                   GetFallbackFn get_fallback,
                   ApplyFallbackFn apply_fallback)
507
{
508 509
    RsvgNode *fallback;
    const char *fallback_id;
510

511 512 513
    fallback_id = get_fallback (last_fallback);
    if (fallback_id == NULL)
        return;
514
    fallback = rsvg_drawing_ctx_acquire_node (ctx, fallback_id);
515 516
    if (fallback == NULL)
      return;
517

518 519
    apply_fallback (data, fallback);
    resolve_fallbacks (ctx, data, fallback, get_fallback, apply_fallback);
520

521
    rsvg_drawing_ctx_release_node (ctx, fallback);
522 523
}

524
static const char *
525
pattern_get_fallback (RsvgNode *node)
526
{
527
    if (rsvg_node_type (node) == RSVG_NODE_TYPE_PATTERN) {
528 529 530 531 532
        RsvgPattern *pattern = (RsvgPattern *) node;

        return pattern->fallback;
    } else
        return NULL;
533 534 535
}

static void
536
pattern_apply_fallback (RsvgNode *pattern_node, RsvgNode *fallback_node)
537 538 539 540
{
    RsvgPattern *pattern;
    RsvgPattern *fallback;

541
    g_assert (rsvg_node_type (pattern_node) == RSVG_NODE_TYPE_PATTERN);
542

543
    if (rsvg_node_type (fallback_node) != RSVG_NODE_TYPE_PATTERN)
544 545 546 547
        return;

    pattern = (RsvgPattern *) pattern_node;
    fallback = (RsvgPattern *) fallback_node;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

    if (!pattern->hasx && fallback->hasx) {
        pattern->hasx = TRUE;
        pattern->x = fallback->x;
    }
    if (!pattern->hasy && fallback->hasy) {
        pattern->hasy = TRUE;
        pattern->y = fallback->y;
    }
    if (!pattern->haswidth && fallback->haswidth) {
        pattern->haswidth = TRUE;
        pattern->width = fallback->width;
    }
    if (!pattern->hasheight && fallback->hasheight) {
        pattern->hasheight = TRUE;
        pattern->height = fallback->height;
    }
    if (!pattern->hastransform && fallback->hastransform) {
        pattern->hastransform = TRUE;
        pattern->affine = fallback->affine;
    }
    if (!pattern->hasvbox && fallback->hasvbox) {
        pattern->vbox = fallback->vbox;
    }
    if (!pattern->hasaspect && fallback->hasaspect) {
        pattern->hasaspect = TRUE;
        pattern->preserve_aspect_ratio = fallback->preserve_aspect_ratio;
575
    }
576 577 578 579 580 581 582 583 584 585 586 587 588 589
    if (!pattern->hasbbox && fallback->hasbbox) {
        pattern->hasbbox = TRUE;
        pattern->obj_bbox = fallback->obj_bbox;
    }
    if (!pattern->hascbox && fallback->hascbox) {
        pattern->hascbox = TRUE;
        pattern->obj_cbbox = fallback->obj_cbbox;
    }
    if (!pattern->super.children->len && fallback->super.children->len) {
        pattern->super.children = fallback->super.children;
    }
}

void
590
rsvg_pattern_fix_fallback (RsvgDrawingCtx *ctx, RsvgPattern * pattern)
591
{
592
    resolve_fallbacks (ctx,
593 594
                       (RsvgNode *) pattern,
                       (RsvgNode *) pattern,
595 596
                       pattern_get_fallback,
                       pattern_apply_fallback);
597
}