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
#include "rsvg-image.h"
40 41

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

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

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

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

59 60 61 62
    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);
63

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

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

    had_paint_server = FALSE;
79

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

84
        node = rsvg_drawing_ctx_acquire_node (ctx, ps->core.iri->iri_str);
85 86
        if (node == NULL) {
            use_alternate = TRUE;
87 88 89 90 91 92 93
        } 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;
            }
94
        } else if (rsvg_node_get_type (node) == RSVG_NODE_TYPE_PATTERN) {
95
            if (pattern_resolve_fallbacks_and_set_pattern (node, ctx, bbox)) {
96 97
                had_paint_server = TRUE;
            } else {
98 99 100 101 102 103 104 105
                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;
106 107
            }
        }
108

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

    return had_paint_server;
118 119
}

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

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

135
#ifdef HAVE_PANGOFT2
136 137 138 139 140 141 142 143 144 145 146 147 148
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;
}

149 150 151 152 153 154 155 156 157 158
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);
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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);

179 180 181 182 183
    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);
    }
184

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

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

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

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

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

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

219
    return context;
220 221
}

222 223 224
static void
setup_cr_for_stroke (cairo_t *cr, RsvgDrawingCtx *ctx, RsvgState *state)
{
225
    cairo_set_line_width (cr, rsvg_get_normalized_stroke_width (ctx));
226 227 228 229
    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,
230
                    rsvg_length_normalize (&state->dash.offset, ctx));
231 232
}

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

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

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

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

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

Christian Persch's avatar
Christian Persch committed
253
    rsvg_bbox_init (&bbox, &state->affine);
254 255 256 257 258 259 260 261 262 263 264
    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;
    }
265 266
    bbox.virgin = 0;

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

        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);
280 281

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

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

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

293 294 295 296 297 298 299
        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);
300 301

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

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

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

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

313 314


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

324
    rsvg_cairo_push_discrete_layer (ctx);
325 326 327

    cr = render->cr;

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

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

332
    setup_cr_for_stroke (cr, ctx, state);
333

334
    rsvg_path_builder_add_to_cairo_context (builder, cr);
335

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

338 339 340 341 342 343
    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? */

344 345 346 347 348 349 350 351 352 353 354 355 356
    /* 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.
     */

357 358 359 360 361 362 363 364 365
    /* 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.
     */
    {
366
        RsvgBbox fb;
Christian Persch's avatar
Christian Persch committed
367
        rsvg_bbox_init (&fb, &state->affine);
Christian Persch's avatar
Christian Persch committed
368 369 370
        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;
371 372 373
        fb.virgin = 0;
        rsvg_bbox_insert (&bbox, &fb);
    }
374 375

    /* Bounding box for stroke */
376 377
    if (state->stroke != NULL) {
        RsvgBbox sb;
Christian Persch's avatar
Christian Persch committed
378
        rsvg_bbox_init (&sb, &state->affine);
Christian Persch's avatar
Christian Persch committed
379 380 381
        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;
382 383 384 385
        sb.virgin = 0;
        rsvg_bbox_insert (&bbox, &sb);
    }

386 387
    cairo_set_tolerance (cr, backup_tolerance);

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

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

        cairo_set_fill_rule (cr, state->fill_rule);
394

395
        opacity = state->fill_opacity;
396

397 398 399 400 401 402 403 404 405
        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);
        }
406 407 408 409
    }

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

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

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

422
    rsvg_cairo_pop_discrete_layer (ctx);
423 424
}

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

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

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

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

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

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

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

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

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

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

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

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

482
    cairo_paint (render->cr);
483

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

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

    return render->cr;
}

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

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

539 540 541 542 543 544 545 546
    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);
547

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

551 552 553 554
    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);
555 556

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

    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
565 566 567 568
                                      sx * bbox->rect.width + bbox->rect.x,
                                      sy * bbox->rect.height + bbox->rect.y,
                                      sw * bbox->rect.width,
                                      sh * bbox->rect.height);
569 570 571 572 573
    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
574
        cairo_matrix_t bbtransform;
575 576
        RsvgState *mask_state;

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

        mask_state = rsvg_node_get_state (node_mask);

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

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

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

599
        rsvg_drawing_ctx_pop_view_box (ctx);
600

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

    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;
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
            /*
             *  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);
633 634 635 636 637
        }
    }

    cairo_destroy (mask_cr);

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

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

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

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

            default:
                g_assert_not_reached ();
                break;
            }

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

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

    if (!state->filter) {
684 685 686
        surface = cairo_surface_create_similar (cairo_get_target (render->cr),
                                                CAIRO_CONTENT_COLOR_ALPHA,
                                                render->width, render->height);
687
    } else {
688
        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
689 690 691 692 693 694 695 696 697 698
                                              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;
699
    }
700 701
#endif

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

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

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

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

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

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

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

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

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

752 753
    surface = cairo_get_target (child_cr);

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

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

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

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

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

774 775 776 777 778
    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);
779

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

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

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

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

800
    cairo_destroy (child_cr);
801

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

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

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

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

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

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

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

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

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

835 836
cairo_surface_t *
rsvg_cairo_get_surface_of_node (RsvgDrawingCtx *ctx,
837 838
                                RsvgNode *drawable,
                                double width,
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
                                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;

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

    cairo_destroy (cr);

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

    return surface;
}

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 893
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);
894
        return NULL;
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 957
    }

    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;
}
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 1070

/* 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;
}
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 1101

/* 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;
            }
        }
    }
}