rsvg-cairo-draw.c 34.2 KB
Newer Older
1 2
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 expandtab: */
3
/*
Caleb Michael Moore's avatar
Caleb Michael Moore committed
4
   rsvg-shapes.c: Draw shapes with cairo
5

Caleb Michael Moore's avatar
Caleb Michael Moore committed
6 7
   Copyright (C) 2005 Dom Lachowicz <cinamod@hotmail.com>
   Copyright (C) 2005 Caleb Moore <c.moore@student.unsw.edu.au>
8
   Copyright (C) 2005 Red Hat, Inc.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

25
   Authors: Dom Lachowicz <cinamod@hotmail.com>,
26
            Caleb Moore <c.moore@student.unsw.edu.au>
27
            Carl Worth <cworth@cworth.org>
28 29
*/

30 31
#include "config.h"

32 33
#include "rsvg-cairo-draw.h"
#include "rsvg-cairo-render.h"
Caleb Michael Moore's avatar
Caleb Michael Moore committed
34
#include "rsvg-cairo-clip.h"
35
#include "rsvg-styles.h"
36
#include "rsvg-path-builder.h"
37
#include "rsvg-filter.h"
38
#include "rsvg-structure.h"
39 40

#include <math.h>
41
#include <string.h>
42

43
#include <pango/pangocairo.h>
44
#include <pango/pangofc-fontmap.h>
45

46
static void
47 48
_set_source_rsvg_solid_color (RsvgDrawingCtx * ctx,
                              RsvgSolidColor * color, guint8 opacity, guint32 current_color)
49
{
50 51
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
    cairo_t *cr = render->cr;
52
    guint32 argb = color->argb;
53
    double r, g, b, a;
54

55 56
    if (color->currentcolor)
        argb = current_color;
57

58 59 60 61
    r = ((argb >> 16) & 0xff) / 255.0;
    g = ((argb >>  8) & 0xff) / 255.0;
    b = ((argb >>  0) & 0xff) / 255.0;
    a =  (argb >> 24) / 255.0 * (opacity / 255.0);
62

63
    cairo_set_source_rgba (cr, r, g, b, a);
64 65
}

66
/* note: _set_source_rsvg_paint_server does not change cairo's CTM */
67
static gboolean
68 69 70
_set_source_rsvg_paint_server (RsvgDrawingCtx * ctx,
                               guint32 current_color_rgb,
                               RsvgPaintServer * ps,
71
                               guint8 opacity, RsvgBbox bbox, guint32 current_color)
72
{
73
    RsvgNode *node;
74
    gboolean had_paint_server;
75
    gboolean use_alternate;
76 77

    had_paint_server = FALSE;
78

79
    switch (ps->type) {
80
    case RSVG_PAINT_SERVER_IRI:
81 82
        use_alternate = FALSE;

83
        node = rsvg_drawing_ctx_acquire_node (ctx, ps->core.iri->iri_str);
84 85
        if (node == NULL) {
            use_alternate = TRUE;
86 87 88 89 90 91 92
        } else if (rsvg_node_get_type (node) == RSVG_NODE_TYPE_LINEAR_GRADIENT
                   || rsvg_node_get_type (node) == RSVG_NODE_TYPE_RADIAL_GRADIENT) {
            if (gradient_resolve_fallbacks_and_set_pattern (node, ctx, opacity, bbox)) {
                had_paint_server = TRUE;
            } else {
                use_alternate = TRUE;
            }
93
        } else if (rsvg_node_get_type (node) == RSVG_NODE_TYPE_PATTERN) {
94
            if (pattern_resolve_fallbacks_and_set_pattern (node, ctx, bbox)) {
95 96
                had_paint_server = TRUE;
            } else {
97 98 99 100 101 102 103 104
                use_alternate = TRUE;
            }
        }

        if (use_alternate) {
            if (ps->core.iri->has_alternate) {
                _set_source_rsvg_solid_color (ctx, &ps->core.iri->alternate, opacity, current_color);
                had_paint_server = TRUE;
105 106
            }
        }
107

108
        rsvg_drawing_ctx_release_node (ctx, node);
109 110
        break;
    case RSVG_PAINT_SERVER_SOLID:
111
        _set_source_rsvg_solid_color (ctx, ps->core.color, opacity, current_color);
112
        had_paint_server = TRUE;
113 114
        break;
    }
115 116

    return had_paint_server;
117 118
}

119
static void
Christian Persch's avatar
Christian Persch committed
120
_set_rsvg_affine (RsvgCairoRender * render, cairo_matrix_t *affine)
121
{
122
    cairo_t * cr = render->cr;
123
    cairo_matrix_t matrix;
124
    gboolean nest = cr != render->initial_cr;
125

126
    cairo_matrix_init (&matrix,
Christian Persch's avatar
Christian Persch committed
127 128 129 130
                       affine->xx, affine->yx,
                       affine->xy, affine->yy,
                       affine->x0 + (nest ? 0 : render->offset_x),
                       affine->y0 + (nest ? 0 : render->offset_y));
131
    cairo_set_matrix (cr, &matrix);
132 133
}

134
#ifdef HAVE_PANGOFT2
135 136 137 138 139 140 141 142 143 144 145 146 147
static cairo_font_options_t *
get_font_options_for_testing (void)
{
    cairo_font_options_t *options;

    options = cairo_font_options_create ();
    cairo_font_options_set_antialias (options, CAIRO_ANTIALIAS_GRAY);
    cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_FULL);
    cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);

    return options;
}

148 149 150 151 152 153 154 155 156 157
static void
set_font_options_for_testing (PangoContext *context)
{
    cairo_font_options_t *font_options;

    font_options = get_font_options_for_testing ();
    pango_cairo_context_set_font_options (context, font_options);
    cairo_font_options_destroy (font_options);
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static void
create_font_config_for_testing (RsvgCairoRender *render)
{
    const char *font_path = SRCDIR "/tests/resources/LiberationSans-Regular.ttf";

    if (render->font_config_for_testing != NULL)
        return;

    render->font_config_for_testing = FcConfigCreate ();

    if (!FcConfigAppFontAddFile (render->font_config_for_testing, (const FcChar8 *) font_path)) {
        g_error ("Could not load font file \"%s\" for tests; aborting", font_path);
    }
}

static PangoFontMap *
get_font_map_for_testing (RsvgCairoRender *render)
{
    create_font_config_for_testing (render);

178 179 180 181 182
    if (!render->font_map_for_testing) {
        render->font_map_for_testing = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
        pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (render->font_map_for_testing),
                                      render->font_config_for_testing);
    }
183

184
    return render->font_map_for_testing;
185
}
186
#endif
187

188
PangoContext *
189
rsvg_cairo_create_pango_context (RsvgDrawingCtx * ctx)
190
{
191 192
    PangoFontMap *fontmap;
    PangoContext *context;
193
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
194
    double dpi_y;
195

196
#ifdef HAVE_PANGOFT2
197 198 199
    if (ctx->is_testing) {
        fontmap = get_font_map_for_testing (render);
    } else {
200
#endif
201
        fontmap = pango_cairo_font_map_get_default ();
202
#ifdef HAVE_PANGOFT2
203
    }
204
#endif
205

206
    context = pango_font_map_create_context (fontmap);
207
    pango_cairo_update_context (render->cr, context);
208 209 210 211

    rsvg_drawing_ctx_get_dpi (ctx, NULL, &dpi_y);
    pango_cairo_context_set_resolution (context, dpi_y);

212
#ifdef HAVE_PANGOFT2
213
    if (ctx->is_testing) {
214
        set_font_options_for_testing (context);
215
    }
216
#endif
217

218
    return context;
219 220
}

221 222 223
static void
setup_cr_for_stroke (cairo_t *cr, RsvgDrawingCtx *ctx, RsvgState *state)
{
224
    cairo_set_line_width (cr, rsvg_get_normalized_stroke_width (ctx));
225 226 227 228
    cairo_set_miter_limit (cr, state->miter_limit);
    cairo_set_line_cap (cr, (cairo_line_cap_t) state->cap);
    cairo_set_line_join (cr, (cairo_line_join_t) state->join);
    cairo_set_dash (cr, state->dash.dash, state->dash.n_dash,
229
                    rsvg_length_normalize (&state->dash.offset, ctx));
230 231
}

232
void
233
rsvg_cairo_render_pango_layout (RsvgDrawingCtx * ctx, PangoLayout * layout, double x, double y)
234
{
235
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
236
    RsvgState *state = rsvg_current_state (ctx);
237
    PangoRectangle ink;
238
    RsvgBbox bbox;
239 240
    PangoGravity gravity = pango_context_get_gravity (pango_layout_get_context (layout));
    double rotation;
241

242
    pango_layout_get_extents(layout, &ink, NULL);
243 244 245 246 247

    if (ink.width == 0 || ink.height == 0) {
        return;
    }

248
    cairo_set_antialias (render->cr, state->text_rendering_type);
249

Christian Persch's avatar
Christian Persch committed
250
    _set_rsvg_affine (render, &state->affine);
251

Christian Persch's avatar
Christian Persch committed
252
    rsvg_bbox_init (&bbox, &state->affine);
253 254 255 256 257 258 259 260 261 262 263
    if (PANGO_GRAVITY_IS_VERTICAL (gravity)) {
        bbox.rect.x = x + (ink.x - ink.height) / (double)PANGO_SCALE;
        bbox.rect.y = y + ink.y / (double)PANGO_SCALE;
        bbox.rect.width = ink.height / (double)PANGO_SCALE;
        bbox.rect.height = ink.width / (double)PANGO_SCALE;
    } else {
        bbox.rect.x = x + ink.x / (double)PANGO_SCALE;
        bbox.rect.y = y + ink.y / (double)PANGO_SCALE;
        bbox.rect.width = ink.width / (double)PANGO_SCALE;
        bbox.rect.height = ink.height / (double)PANGO_SCALE;
    }
264 265
    bbox.virgin = 0;

266
    rotation = pango_gravity_to_rotation (gravity);
267
    if (state->fill) {
268
        cairo_save (render->cr);
Hiroyuki Ikezoe's avatar
Hiroyuki Ikezoe committed
269 270
        cairo_move_to (render->cr, x, y);
        rsvg_bbox_insert (&render->bbox, &bbox);
271 272 273 274 275 276 277 278

        if (_set_source_rsvg_paint_server (ctx,
                                           state->current_color,
                                           state->fill,
                                           state->fill_opacity,
                                           bbox, rsvg_current_state (ctx)->current_color)) {
            if (rotation != 0.)
                cairo_rotate (render->cr, -rotation);
279 280

            pango_cairo_update_layout (render->cr, layout);
281 282 283
            pango_cairo_show_layout (render->cr, layout);
        }

284
        cairo_restore (render->cr);
285 286 287
    }

    if (state->stroke) {
288
        cairo_save (render->cr);
289 290
        cairo_move_to (render->cr, x, y);
        rsvg_bbox_insert (&render->bbox, &bbox);
291

292 293 294 295 296 297 298
        if (_set_source_rsvg_paint_server (ctx,
                                           state->current_color,
                                           state->stroke,
                                           state->stroke_opacity,
                                           bbox, rsvg_current_state (ctx)->current_color)) {
            if (rotation != 0.)
                cairo_rotate (render->cr, -rotation);
299 300

            pango_cairo_update_layout (render->cr, layout);
301
            pango_cairo_layout_path (render->cr, layout);
302

303
            setup_cr_for_stroke (render->cr, ctx, state);
304

305 306
            cairo_stroke (render->cr);
        }
307

308
        cairo_restore (render->cr);
309
    }
310 311
}

312 313


314
void
315
rsvg_cairo_render_path_builder (RsvgDrawingCtx * ctx, RsvgPathBuilder *builder)
316
{
317
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
318
    RsvgState *state = rsvg_current_state (ctx);
319 320
    cairo_t *cr;
    RsvgBbox bbox;
321
    double backup_tolerance;
322

323
    rsvg_cairo_push_discrete_layer (ctx);
324 325 326

    cr = render->cr;

327
    cairo_set_antialias (cr, state->shape_rendering_type);
328

Christian Persch's avatar
Christian Persch committed
329
    _set_rsvg_affine (render, &state->affine);
330

331
    setup_cr_for_stroke (cr, ctx, state);
332

333
    rsvg_path_builder_add_to_cairo_context (builder, cr);
334

Christian Persch's avatar
Christian Persch committed
335
    rsvg_bbox_init (&bbox, &state->affine);
336

337 338 339 340 341 342
    backup_tolerance = cairo_get_tolerance (cr);
    cairo_set_tolerance (cr, 1.0);
    /* dropping the precision of cairo's bezier subdivision, yielding 2x
       _rendering_ time speedups, are these rather expensive operations
       really needed here? */

343 344 345 346 347 348 349 350 351 352 353 354 355
    /* FIXME: See https://www.w3.org/TR/SVG/coords.html#ObjectBoundingBox for
     * discussion on how to compute bounding boxes to be used for viewports and
     * clipping.  It looks like we should be using cairo_path_extents() for
     * that, not cairo_fill_extents().
     *
     * We may need to maintain *two* sets of bounding boxes - one for
     * viewports/clipping, and one for user applications like a
     * rsvg_compute_ink_rect() function in the future.
     *
     * See https://bugzilla.gnome.org/show_bug.cgi?id=760112 for discussion of a
     * public API to get the ink rectangle.
     */

356 357 358 359 360 361 362 363 364
    /* Bounding box for fill
     *
     * Unlike the case for stroke, for fills we always compute the bounding box.
     * In GNOME we have SVGs for symbolic icons where each icon has a bounding
     * rectangle with no fill and no stroke, and inside it there are the actual
     * paths for the icon's shape.  We need to be able to compute the bounding
     * rectangle's extents, even when it has no fill nor stroke.
     */
    {
365
        RsvgBbox fb;
Christian Persch's avatar
Christian Persch committed
366
        rsvg_bbox_init (&fb, &state->affine);
Christian Persch's avatar
Christian Persch committed
367 368 369
        cairo_fill_extents (cr, &fb.rect.x, &fb.rect.y, &fb.rect.width, &fb.rect.height);
        fb.rect.width -= fb.rect.x;
        fb.rect.height -= fb.rect.y;
370 371 372
        fb.virgin = 0;
        rsvg_bbox_insert (&bbox, &fb);
    }
373 374

    /* Bounding box for stroke */
375 376
    if (state->stroke != NULL) {
        RsvgBbox sb;
Christian Persch's avatar
Christian Persch committed
377
        rsvg_bbox_init (&sb, &state->affine);
Christian Persch's avatar
Christian Persch committed
378 379 380
        cairo_stroke_extents (cr, &sb.rect.x, &sb.rect.y, &sb.rect.width, &sb.rect.height);
        sb.rect.width -= sb.rect.x;
        sb.rect.height -= sb.rect.y;
381 382 383 384
        sb.virgin = 0;
        rsvg_bbox_insert (&bbox, &sb);
    }

385 386
    cairo_set_tolerance (cr, backup_tolerance);

387 388 389 390
    rsvg_bbox_insert (&render->bbox, &bbox);

    if (state->fill != NULL) {
        int opacity;
391 392

        cairo_set_fill_rule (cr, state->fill_rule);
393

394
        opacity = state->fill_opacity;
395

396 397 398 399 400 401 402 403 404
        if (_set_source_rsvg_paint_server (ctx,
                                           state->current_color,
                                           state->fill,
                                           opacity, bbox, rsvg_current_state (ctx)->current_color)) {
            if (state->stroke != NULL)
                cairo_fill_preserve (cr);
            else
                cairo_fill (cr);
        }
405 406 407 408
    }

    if (state->stroke != NULL) {
        int opacity;
409
        opacity = state->stroke_opacity;
410

411 412 413 414 415 416
        if (_set_source_rsvg_paint_server (ctx,
                                           state->current_color,
                                           state->stroke,
                                           opacity, bbox, rsvg_current_state (ctx)->current_color)) {
            cairo_stroke (cr);
        }
417 418
    }

419 420
    cairo_new_path (cr); /* clear the path in case stroke == fill == NULL; otherwise we leave it around from computing the bounding box */

421
    rsvg_cairo_pop_discrete_layer (ctx);
422 423
}

424
void
425
rsvg_cairo_render_surface (RsvgDrawingCtx *ctx,
426
                           cairo_surface_t *surface,
427 428 429
                           double src_x,
                           double src_y,
                           double w,
430
                           double h)
431
{
432
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
433
    RsvgState *state = rsvg_current_state (ctx);
434

435 436
    int width, height;
    double dwidth, dheight;
437 438
    RsvgBbox bbox;

439
    if (surface == NULL)
440 441
        return;

442
    g_return_if_fail (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE);
443

444 445 446
    dwidth = width = cairo_image_surface_get_width (surface);
    dheight = height = cairo_image_surface_get_height (surface);
    if (width == 0 || height == 0)
447 448
        return;

Christian Persch's avatar
Christian Persch committed
449
    rsvg_bbox_init (&bbox, &state->affine);
450 451
    bbox.rect.x = src_x;
    bbox.rect.y = src_y;
Christian Persch's avatar
Christian Persch committed
452 453
    bbox.rect.width = w;
    bbox.rect.height = h;
454
    bbox.virgin = 0;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
455

Christian Persch's avatar
Christian Persch committed
456
    _set_rsvg_affine (render, &state->affine);
457
    cairo_scale (render->cr, w / dwidth, h / dheight);
458 459
    src_x *= dwidth / w;
    src_y *= dheight / h;
460

461
    cairo_set_operator (render->cr, state->comp_op);
462

463
#if 1
464
    cairo_set_source_surface (render->cr, surface, src_x, src_y);
465
#else
466 467 468
    {
        cairo_pattern_t *pattern;
        cairo_matrix_t matrix;
469

470 471
        pattern = cairo_pattern_create_for_surface (surface);
        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
472

473
        cairo_matrix_init_translate (&matrix, -src_x, -src_y);
474
        cairo_pattern_set_matrix (pattern, &matrix);
475

476 477 478
        cairo_set_source (render->cr, pattern);
        cairo_pattern_destroy (pattern);
    }
479 480
#endif

481
    cairo_paint (render->cr);
482

483
    rsvg_bbox_insert (&render->bbox, &bbox);
484
}
485

486 487 488 489 490 491 492 493
cairo_t *
rsvg_cairo_get_cairo_context (RsvgDrawingCtx *ctx)
{
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);

    return render->cr;
}

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/* FIXME: Usage of this function is more less a hack.  Some code does this:
 *
 *   save_cr = rsvg_cairo_get_cairo_context (ctx);
 *
 *   some_surface = create_surface ();
 *
 *   cr = cairo_create (some_surface);
 *
 *   rsvg_cairo_set_cairo_context (ctx, cr);
 *
 *   ... draw with ctx but to that temporary surface
 *
 *   rsvg_cairo_set_cairo_context (ctx, save_cr);
 *
 * It would be better to have an explicit push/pop for the cairo_t, or
 * pushing a temporary surface, or something that does not involve
 * monkeypatching the cr directly.
 */
void
rsvg_cairo_set_cairo_context (RsvgDrawingCtx *ctx, cairo_t *cr)
{
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);

    render->cr = cr;
}

Dom Lachowicz's avatar
Dom Lachowicz committed
520
static void
521
rsvg_cairo_generate_mask (cairo_t * cr, RsvgNode *node_mask, RsvgDrawingCtx *ctx, RsvgBbox *bbox)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
522
{
523
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
524 525
    cairo_surface_t *surface;
    cairo_t *mask_cr, *save_cr;
526
    RsvgState *state = rsvg_current_state (ctx);
527 528 529
    guint8 *pixels;
    guint32 width = render->width, height = render->height;
    guint32 rowstride = width * 4, row, i;
Christian Persch's avatar
Christian Persch committed
530
    cairo_matrix_t affinesave;
531
    double sx, sy, sw, sh;
532
    gboolean nest = cr != render->initial_cr;
533 534 535 536
    RsvgMask *self;

    g_assert (rsvg_node_get_type (node_mask) == RSVG_NODE_TYPE_MASK);
    self = rsvg_rust_cnode_get_impl (node_mask);
537

538 539 540 541 542 543 544 545
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
        cairo_surface_destroy (surface);
        return;
    }

    pixels = cairo_image_surface_get_data (surface);
    rowstride = cairo_image_surface_get_stride (surface);
546

547
    if (self->maskunits == objectBoundingBox)
548
        rsvg_drawing_ctx_push_view_box (ctx, 1, 1);
549

550 551 552 553
    sx = rsvg_length_normalize (&self->x, ctx);
    sy = rsvg_length_normalize (&self->y, ctx);
    sw = rsvg_length_normalize (&self->width, ctx);
    sh = rsvg_length_normalize (&self->height, ctx);
554 555

    if (self->maskunits == objectBoundingBox)
556
        rsvg_drawing_ctx_pop_view_box (ctx);
557 558 559 560 561 562 563

    mask_cr = cairo_create (surface);
    save_cr = render->cr;
    render->cr = mask_cr;

    if (self->maskunits == objectBoundingBox)
        rsvg_cairo_add_clipping_rect (ctx,
Christian Persch's avatar
Christian Persch committed
564 565 566 567
                                      sx * bbox->rect.width + bbox->rect.x,
                                      sy * bbox->rect.height + bbox->rect.y,
                                      sw * bbox->rect.width,
                                      sh * bbox->rect.height);
568 569 570 571 572
    else
        rsvg_cairo_add_clipping_rect (ctx, sx, sy, sw, sh);

    /* Horribly dirty hack to have the bbox premultiplied to everything */
    if (self->contentunits == objectBoundingBox) {
Christian Persch's avatar
Christian Persch committed
573
        cairo_matrix_t bbtransform;
574 575
        RsvgState *mask_state;

Christian Persch's avatar
Christian Persch committed
576 577 578 579 580 581 582
        cairo_matrix_init (&bbtransform,
                           bbox->rect.width,
                           0,
                           0,
                           bbox->rect.height,
                           bbox->rect.x,
                           bbox->rect.y);
583 584 585 586 587

        mask_state = rsvg_node_get_state (node_mask);

        affinesave = mask_state->affine;
        cairo_matrix_multiply (&mask_state->affine, &bbtransform, &mask_state->affine);
588
        rsvg_drawing_ctx_push_view_box (ctx, 1, 1);
589 590 591
    }

    rsvg_state_push (ctx);
592
    rsvg_node_draw_children (node_mask, ctx, 0);
593 594 595
    rsvg_state_pop (ctx);

    if (self->contentunits == objectBoundingBox) {
596
        RsvgState *mask_state;
597

598
        rsvg_drawing_ctx_pop_view_box (ctx);
599

600 601
        mask_state = rsvg_node_get_state (node_mask);
        mask_state->affine = affinesave;
602 603 604 605 606 607 608 609
    }

    render->cr = save_cr;

    for (row = 0; row < height; row++) {
        guint8 *row_data = (pixels + (row * rowstride));
        for (i = 0; i < width; i++) {
            guint32 *pixel = (guint32 *) row_data + i;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            /*
             *  Assuming, the pixel is linear RGB (not sRGB)
             *  y = luminance
             *  Y = 0.2126 R + 0.7152 G + 0.0722 B
             *  1.0 opacity = 255
             *
             *  When Y = 1.0, pixel for mask should be 0xFFFFFFFF
             *  	(you get 1.0 luminance from 255 from R, G and B)
             *
             *	r_mult = 0xFFFFFFFF / (255.0 * 255.0) * .2126 = 14042.45  ~= 14042
             *	g_mult = 0xFFFFFFFF / (255.0 * 255.0) * .7152 = 47239.69  ~= 47240
             *	b_mult = 0xFFFFFFFF / (255.0 * 255.0) * .0722 =  4768.88  ~= 4769
             *
             * 	This allows for the following expected behaviour:
             *  (we only care about the most sig byte)
             *	if pixel = 0x00FFFFFF, pixel' = 0xFF......
             *	if pixel = 0x00020202, pixel' = 0x02......
             *	if pixel = 0x00000000, pixel' = 0x00......
             */
            *pixel = ((((*pixel & 0x00ff0000) >> 16) * 14042 +
                       ((*pixel & 0x0000ff00) >>  8) * 47240 +
                       ((*pixel & 0x000000ff)      ) * 4769    ) * state->opacity);
632 633 634 635 636
        }
    }

    cairo_destroy (mask_cr);

637 638 639 640
    cairo_identity_matrix (cr);
    cairo_mask_surface (cr, surface,
                        nest ? 0 : render->offset_x,
                        nest ? 0 : render->offset_y);
641
    cairo_surface_destroy (surface);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
642 643 644
}

static void
645
rsvg_cairo_push_render_stack (RsvgDrawingCtx * ctx)
646
{
647
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
648 649 650
    cairo_surface_t *surface;
    cairo_t *child_cr;
    RsvgBbox *bbox;
651
    RsvgState *state = rsvg_current_state (ctx);
652 653
    gboolean lateclip = FALSE;

654 655
    if (rsvg_current_state (ctx)->clip_path) {
        RsvgNode *node;
656
        node = rsvg_drawing_ctx_acquire_node_of_type (ctx, rsvg_current_state (ctx)->clip_path, RSVG_NODE_TYPE_CLIP_PATH);
657
        if (node) {
658
            RsvgClipPath *clip_path = rsvg_rust_cnode_get_impl (node);
659 660 661

            switch (clip_path->units) {
            case userSpaceOnUse:
662
                rsvg_cairo_clip (ctx, node, NULL);
663 664 665 666 667 668 669 670 671 672
                break;
            case objectBoundingBox:
                lateclip = TRUE;
                break;

            default:
                g_assert_not_reached ();
                break;
            }

673
            rsvg_drawing_ctx_release_node (ctx, node);
674 675
        }
    }
676 677

    if (state->opacity == 0xFF
678
        && !state->filter && !state->mask && !lateclip && (state->comp_op == CAIRO_OPERATOR_OVER)
679 680
        && (state->enable_background == RSVG_ENABLE_BACKGROUND_ACCUMULATE))
        return;
681 682

    if (!state->filter) {
683 684 685
        surface = cairo_surface_create_similar (cairo_get_target (render->cr),
                                                CAIRO_CONTENT_COLOR_ALPHA,
                                                render->width, render->height);
686
    } else {
687
        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
688 689 690 691 692 693 694 695 696 697
                                              render->width, render->height);

        /* The surface reference is owned by the child_cr created below and put on the cr_stack! */
        render->surfaces_stack = g_list_prepend (render->surfaces_stack, surface);
    }

#if 0
    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
        cairo_surface_destroy (surface);
        return;
698
    }
699 700
#endif

701 702 703 704 705 706
    child_cr = cairo_create (surface);
    cairo_surface_destroy (surface);

    render->cr_stack = g_list_prepend (render->cr_stack, render->cr);
    render->cr = child_cr;

707
    bbox = g_new0 (RsvgBbox, 1);
708 709
    *bbox = render->bbox;
    render->bb_stack = g_list_prepend (render->bb_stack, bbox);
Christian Persch's avatar
Christian Persch committed
710
    rsvg_bbox_init (&render->bbox, &state->affine);
711 712 713
}

void
714
rsvg_cairo_push_discrete_layer (RsvgDrawingCtx * ctx)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
715
{
716 717 718
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);

    cairo_save (render->cr);
719
    rsvg_cairo_push_render_stack (ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
720 721 722
}

static void
723
rsvg_cairo_pop_render_stack (RsvgDrawingCtx * ctx)
724
{
725
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
726
    cairo_t *child_cr = render->cr;
727
    RsvgNode *lateclip = NULL;
728
    cairo_surface_t *surface = NULL;
729
    RsvgState *state = rsvg_current_state (ctx);
730
    gboolean nest, needs_destroy = FALSE;
731

732 733
    if (rsvg_current_state (ctx)->clip_path) {
        RsvgNode *node;
734
        node = rsvg_drawing_ctx_acquire_node_of_type (ctx, rsvg_current_state (ctx)->clip_path, RSVG_NODE_TYPE_CLIP_PATH);
735 736 737 738 739 740 741 742 743
        if (node) {
            RsvgClipPath *cp = rsvg_rust_cnode_get_impl (node);

            if (cp->units == objectBoundingBox) {
                lateclip = node;
            } else {
                rsvg_drawing_ctx_release_node (ctx, node);
            }
        }
744
    }
745 746

    if (state->opacity == 0xFF
747
        && !state->filter && !state->mask && !lateclip && (state->comp_op == CAIRO_OPERATOR_OVER)
748 749
        && (state->enable_background == RSVG_ENABLE_BACKGROUND_ACCUMULATE))
        return;
750

751 752
    surface = cairo_get_target (child_cr);

753
    if (state->filter) {
754
        RsvgNode *filter;
755
        cairo_surface_t *output;
756

757 758
        output = render->surfaces_stack->data;
        render->surfaces_stack = g_list_delete_link (render->surfaces_stack, render->surfaces_stack);
759

760
        filter = rsvg_drawing_ctx_acquire_node_of_type (ctx, state->filter, RSVG_NODE_TYPE_FILTER);
761
        if (filter) {
762
            needs_destroy = TRUE;
763
            surface = rsvg_filter_render (filter, output, ctx, &render->bbox, "2103");
764
            rsvg_drawing_ctx_release_node (ctx, filter);
765

766 767
            /* Don't destroy the output surface, it's owned by child_cr */
        }
768
    }
769 770

    render->cr = (cairo_t *) render->cr_stack->data;
771
    render->cr_stack = g_list_delete_link (render->cr_stack, render->cr_stack);
772

773 774 775 776 777
    nest = render->cr != render->initial_cr;
    cairo_identity_matrix (render->cr);
    cairo_set_source_surface (render->cr, surface,
                              nest ? 0 : render->offset_x,
                              nest ? 0 : render->offset_y);
778

779
    if (lateclip) {
780
        rsvg_cairo_clip (ctx, lateclip, &render->bbox);
781
        rsvg_drawing_ctx_release_node (ctx, lateclip);
782
    }
783

784
    cairo_set_operator (render->cr, state->comp_op);
785

786
    if (state->mask) {
787 788
        RsvgNode *mask;

789
        mask = rsvg_drawing_ctx_acquire_node_of_type (ctx, state->mask, RSVG_NODE_TYPE_MASK);
790
        if (mask) {
791
            rsvg_cairo_generate_mask (render->cr, mask, ctx, &render->bbox);
792
            rsvg_drawing_ctx_release_node (ctx, mask);
793
        }
794 795
    } else if (state->opacity != 0xFF)
        cairo_paint_with_alpha (render->cr, (double) state->opacity / 255.0);
796
    else
797
        cairo_paint (render->cr);
798

799
    cairo_destroy (child_cr);
800

801
    rsvg_bbox_insert ((RsvgBbox *) render->bb_stack->data, &render->bbox);
802

803
    render->bbox = *((RsvgBbox *) render->bb_stack->data);
804

805
    g_free (render->bb_stack->data);
806
    render->bb_stack = g_list_delete_link (render->bb_stack, render->bb_stack);
807

808
    if (needs_destroy) {
809
        cairo_surface_destroy (surface);
810
    }
811 812
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
813
void
814
rsvg_cairo_pop_discrete_layer (RsvgDrawingCtx * ctx)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
815
{
816 817
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);

818
    rsvg_cairo_pop_render_stack (ctx);
819
    cairo_restore (render->cr);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
820 821
}

822 823
void
rsvg_cairo_add_clipping_rect (RsvgDrawingCtx * ctx, double x, double y, double w, double h)
824
{
825
    RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
826
    cairo_t *cr = render->cr;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
827

Christian Persch's avatar
Christian Persch committed
828
    _set_rsvg_affine (render, &rsvg_current_state (ctx)->affine);
829

830 831
    cairo_rectangle (cr, x, y, w, h);
    cairo_clip (cr);
832 833
}

834 835
cairo_surface_t *
rsvg_cairo_get_surface_of_node (RsvgDrawingCtx *ctx,
836 837
                                RsvgNode *drawable,
                                double width,
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
                                double height)
{
    cairo_surface_t *surface;
    cairo_t *cr;

    RsvgCairoRender *save_render = (RsvgCairoRender *) ctx->render;
    RsvgCairoRender *render;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
        cairo_surface_destroy (surface);
        return NULL;
    }

    cr = cairo_create (surface);

    render = rsvg_cairo_render_new (cr, width, height);
    ctx->render = (RsvgRender *) render;

857
    rsvg_drawing_ctx_draw_node_from_stack (ctx, drawable, 0);
858 859 860 861 862 863 864 865 866

    cairo_destroy (cr);

    rsvg_render_free (ctx->render);
    ctx->render = (RsvgRender *) save_render;

    return surface;
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
cairo_surface_t *
rsvg_cairo_surface_from_pixbuf (const GdkPixbuf *pixbuf)
{
    gint width, height, gdk_rowstride, n_channels, cairo_rowstride;
    guchar *gdk_pixels, *cairo_pixels;
    cairo_format_t format;
    cairo_surface_t *surface;
    int j;

    if (pixbuf == NULL)
        return NULL;

    width = gdk_pixbuf_get_width (pixbuf);
    height = gdk_pixbuf_get_height (pixbuf);
    gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
    gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
    n_channels = gdk_pixbuf_get_n_channels (pixbuf);

    if (n_channels == 3)
        format = CAIRO_FORMAT_RGB24;
    else
        format = CAIRO_FORMAT_ARGB32;

    surface = cairo_image_surface_create (format, width, height);
    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
        cairo_surface_destroy (surface);
893
        return NULL;
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    }

    cairo_pixels = cairo_image_surface_get_data (surface);
    cairo_rowstride = cairo_image_surface_get_stride (surface);

    if (n_channels == 3) {
        for (j = height; j; j--) {
            guchar *p = gdk_pixels;
            guchar *q = cairo_pixels;
            guchar *end = p + 3 * width;

            while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
                q[0] = p[2];
                q[1] = p[1];
                q[2] = p[0];
#else
                q[1] = p[0];
                q[2] = p[1];
                q[3] = p[2];
#endif
                p += 3;
                q += 4;
            }

            gdk_pixels += gdk_rowstride;
            cairo_pixels += cairo_rowstride;
        }
    } else {
        for (j = height; j; j--) {
            guchar *p = gdk_pixels;
            guchar *q = cairo_pixels;
            guchar *end = p + 4 * width;
            guint t1, t2, t3;

#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END

            while (p < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
                MULT (q[0], p[2], p[3], t1);
                MULT (q[1], p[1], p[3], t2);
                MULT (q[2], p[0], p[3], t3);
                q[3] = p[3];
#else
                q[0] = p[3];
                MULT (q[1], p[0], p[3], t1);
                MULT (q[2], p[1], p[3], t2);
                MULT (q[3], p[2], p[3], t3);
#endif

                p += 4;
                q += 4;
            }

#undef MULT
            gdk_pixels += gdk_rowstride;
            cairo_pixels += cairo_rowstride;
        }
    }

    cairo_surface_mark_dirty (surface);
    return surface;
}
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

/* Copied from gtk+/gdk/gdkpixbuf-drawable.c, LGPL 2+.
 *
 * Copyright (C) 1999 Michael Zucchi
 *
 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
 *          Cody Russell <bratsche@dfw.net>
 *          Federico Mena-Quintero <federico@gimp.org>
 */

static void
convert_alpha (guchar *dest_data,
               int     dest_stride,
               guchar *src_data,
               int     src_stride,
               int     src_x,
               int     src_y,
               int     width,
               int     height)
{
    int x, y;

    src_data += src_stride * src_y + src_x * 4;

    for (y = 0; y < height; y++) {
        guint32 *src = (guint32 *) src_data;

        for (x = 0; x < width; x++) {
          guint alpha = src[x] >> 24;

          if (alpha == 0) {
              dest_data[x * 4 + 0] = 0;
              dest_data[x * 4 + 1] = 0;
              dest_data[x * 4 + 2] = 0;
          } else {
              dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
              dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >>  8) * 255 + alpha / 2) / alpha;
              dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >>  0) * 255 + alpha / 2) / alpha;
          }
          dest_data[x * 4 + 3] = alpha;
      }

      src_data += src_stride;
      dest_data += dest_stride;
    }
}

static void
convert_no_alpha (guchar *dest_data,
                  int     dest_stride,
                  guchar *src_data,
                  int     src_stride,
                  int     src_x,
                  int     src_y,
                  int     width,
                  int     height)
{
    int x, y;

    src_data += src_stride * src_y + src_x * 4;

    for (y = 0; y < height; y++) {
        guint32 *src = (guint32 *) src_data;

        for (x = 0; x < width; x++) {
            dest_data[x * 3 + 0] = src[x] >> 16;
            dest_data[x * 3 + 1] = src[x] >>  8;
            dest_data[x * 3 + 2] = src[x];
        }

        src_data += src_stride;
        dest_data += dest_stride;
    }
}

GdkPixbuf *
rsvg_cairo_surface_to_pixbuf (cairo_surface_t *surface)
{
    cairo_content_t content;
    GdkPixbuf *dest;
    int width, height;

    /* General sanity checks */
    g_assert (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE);

    width = cairo_image_surface_get_width (surface);
    height = cairo_image_surface_get_height (surface);
    if (width == 0 || height == 0)
        return NULL;

    content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
    dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                          !!(content & CAIRO_CONTENT_ALPHA),
                          8,
                          width, height);

    if (gdk_pixbuf_get_has_alpha (dest))
      convert_alpha (gdk_pixbuf_get_pixels (dest),
                    gdk_pixbuf_get_rowstride (dest),
                    cairo_image_surface_get_data (surface),
                    cairo_image_surface_get_stride (surface),
                    0, 0,
                    width, height);
    else
      convert_no_alpha (gdk_pixbuf_get_pixels (dest),
                        gdk_pixbuf_get_rowstride (dest),
                        cairo_image_surface_get_data (surface),
                        cairo_image_surface_get_stride (surface),
                        0, 0,
                        width, height);

    return dest;
}
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

/* This is unused but still exists for ABI compat. See bug #666868. */
void rsvg_cairo_to_pixbuf (guint8 * pixels, int rowstride, int height);

void
rsvg_cairo_to_pixbuf (guint8 * pixels, int rowstride, int height)
{
    int row;
    /* un-premultiply data */
    for (row = 0; row < height; row++) {
        guint8 *row_data = (pixels + (row * rowstride));
        int i;

        for (i = 0; i < rowstride; i += 4) {
            guint8 *b = &row_data[i];
            guint32 pixel;
            guint8 alpha;

            memcpy (&pixel, b, sizeof (guint32));
            alpha = (pixel & 0xff000000) >> 24;
            if (alpha == 0) {
                b[0] = b[1] = b[2] = b[3] = 0;
            } else {
                b[0] = (((pixel & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
                b[1] = (((pixel & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
                b[2] = (((pixel & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
                b[3] = alpha;
            }
        }
    }
}