gtkcssshadowvalue.c 31.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * Author: Cosimo Cecchi <cosimoc@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 */

#include "config.h"

22
#include "gtkcssshadowvalueprivate.h"
23

24
#include "gtkcairoblurprivate.h"
25
#include "gtkcsscolorvalueprivate.h"
26
#include "gtkcssnumbervalueprivate.h"
27
#include "gtkcssrgbavalueprivate.h"
28
#include "gtkstylecontextprivate.h"
29
#include "gtkrenderprivate.h"
30
#include "gtkpango.h"
31

32 33
#include <math.h>

34 35 36
struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  guint inset :1;
37

38 39 40 41
  GtkCssValue *hoffset;
  GtkCssValue *voffset;
  GtkCssValue *radius;
  GtkCssValue *spread;
42

43
  GtkCssValue *color;
44 45
};

46 47 48 49 50 51 52
static GtkCssValue *    gtk_css_shadow_value_new (GtkCssValue *hoffset,
                                                  GtkCssValue *voffset,
                                                  GtkCssValue *radius,
                                                  GtkCssValue *spread,
                                                  gboolean     inset,
                                                  GtkCssValue *color);

53 54
static void
gtk_css_value_shadow_free (GtkCssValue *shadow)
55
{
56 57 58 59
  _gtk_css_value_unref (shadow->hoffset);
  _gtk_css_value_unref (shadow->voffset);
  _gtk_css_value_unref (shadow->radius);
  _gtk_css_value_unref (shadow->spread);
60
  _gtk_css_value_unref (shadow->color);
61

62
  g_slice_free (GtkCssValue, shadow);
63
}
64

65
static GtkCssValue *
66 67 68
gtk_css_value_shadow_compute (GtkCssValue             *shadow,
                              guint                    property_id,
                              GtkStyleProviderPrivate *provider,
69
                              GtkCssStyle             *style,
70
                              GtkCssStyle             *parent_style)
71
{
72 73
  GtkCssValue *hoffset, *voffset, *radius, *spread, *color;

74 75 76 77 78
  hoffset = _gtk_css_value_compute (shadow->hoffset, property_id, provider, style, parent_style);
  voffset = _gtk_css_value_compute (shadow->voffset, property_id, provider, style, parent_style);
  radius = _gtk_css_value_compute (shadow->radius, property_id, provider, style, parent_style);
  spread = _gtk_css_value_compute (shadow->spread, property_id, provider, style, parent_style),
  color = _gtk_css_value_compute (shadow->color, property_id, provider, style, parent_style);
79 80

  return gtk_css_shadow_value_new (hoffset, voffset, radius, spread, shadow->inset, color);
81 82
}

83 84 85 86
static gboolean
gtk_css_value_shadow_equal (const GtkCssValue *shadow1,
                            const GtkCssValue *shadow2)
{
87 88 89 90 91 92
  return shadow1->inset == shadow2->inset
      && _gtk_css_value_equal (shadow1->hoffset, shadow2->hoffset)
      && _gtk_css_value_equal (shadow1->voffset, shadow2->voffset)
      && _gtk_css_value_equal (shadow1->radius, shadow2->radius)
      && _gtk_css_value_equal (shadow1->spread, shadow2->spread)
      && _gtk_css_value_equal (shadow1->color, shadow2->color);
93 94
}

95 96 97
static GtkCssValue *
gtk_css_value_shadow_transition (GtkCssValue *start,
                                 GtkCssValue *end,
98
                                 guint        property_id,
99 100
                                 double       progress)
{
101 102 103
  if (start->inset != end->inset)
    return NULL;

104 105 106 107
  return gtk_css_shadow_value_new (_gtk_css_value_transition (start->hoffset, end->hoffset, property_id, progress),
                                   _gtk_css_value_transition (start->voffset, end->voffset, property_id, progress),
                                   _gtk_css_value_transition (start->radius, end->radius, property_id, progress),
                                   _gtk_css_value_transition (start->spread, end->spread, property_id, progress),
108
                                   start->inset,
109
                                   _gtk_css_value_transition (start->color, end->color, property_id, progress));
110 111
}

112 113 114
static void
gtk_css_value_shadow_print (const GtkCssValue *shadow,
                            GString           *string)
115
{
116 117 118 119 120 121 122 123 124 125
  _gtk_css_value_print (shadow->hoffset, string);
  g_string_append_c (string, ' ');
  _gtk_css_value_print (shadow->voffset, string);
  g_string_append_c (string, ' ');
  if (_gtk_css_number_value_get (shadow->radius, 100) != 0 ||
      _gtk_css_number_value_get (shadow->spread, 100) != 0)
    {
      _gtk_css_value_print (shadow->radius, string);
      g_string_append_c (string, ' ');
    }
126

127 128 129 130 131
  if (_gtk_css_number_value_get (shadow->spread, 100) != 0)
    {
      _gtk_css_value_print (shadow->spread, string);
      g_string_append_c (string, ' ');
    }
132

133
  _gtk_css_value_print (shadow->color, string);
134

135 136
  if (shadow->inset)
    g_string_append (string, " inset");
137 138 139

}

140 141
static const GtkCssValueClass GTK_CSS_VALUE_SHADOW = {
  gtk_css_value_shadow_free,
142
  gtk_css_value_shadow_compute,
143
  gtk_css_value_shadow_equal,
144
  gtk_css_value_shadow_transition,
145 146 147
  gtk_css_value_shadow_print
};

148
static GtkCssValue *
149 150 151 152 153
gtk_css_shadow_value_new (GtkCssValue *hoffset,
                          GtkCssValue *voffset,
                          GtkCssValue *radius,
                          GtkCssValue *spread,
                          gboolean     inset,
154
                          GtkCssValue *color)
155
{
156 157 158 159 160 161 162 163 164
  GtkCssValue *retval;

  retval = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_SHADOW);

  retval->hoffset = hoffset;
  retval->voffset = voffset;
  retval->radius = radius;
  retval->spread = spread;
  retval->inset = inset;
165
  retval->color = color;
166 167

  return retval;
168
}
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
GtkCssValue *
_gtk_css_shadow_value_new_for_transition (GtkCssValue *target)
{
  GdkRGBA transparent = { 0, 0, 0, 0 };

  g_return_val_if_fail (target->class == &GTK_CSS_VALUE_SHADOW, NULL);

  return gtk_css_shadow_value_new (_gtk_css_number_value_new (0, GTK_CSS_PX),
                                   _gtk_css_number_value_new (0, GTK_CSS_PX),
                                   _gtk_css_number_value_new (0, GTK_CSS_PX),
                                   _gtk_css_number_value_new (0, GTK_CSS_PX),
                                   target->inset,
                                   _gtk_css_rgba_value_new_from_rgba (&transparent));
}

185 186 187 188 189 190 191 192 193
static gboolean
value_is_done_parsing (GtkCssParser *parser)
{
  return _gtk_css_parser_is_eof (parser) ||
         _gtk_css_parser_begins_with (parser, ',') ||
         _gtk_css_parser_begins_with (parser, ';') ||
         _gtk_css_parser_begins_with (parser, '}');
}

194
GtkCssValue *
195 196
_gtk_css_shadow_value_parse (GtkCssParser *parser,
                             gboolean      box_shadow_mode)
197
{
198 199 200 201 202 203 204 205 206 207
  enum {
    HOFFSET,
    VOFFSET,
    RADIUS,
    SPREAD,
    COLOR,
    N_VALUES
  };
  GtkCssValue *values[N_VALUES] = { NULL, };
  gboolean inset;
208 209
  guint i;

210 211
  if (box_shadow_mode)
    inset = _gtk_css_parser_try (parser, "inset", TRUE);
212 213
  else
    inset = FALSE;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

  do
  {
    if (values[HOFFSET] == NULL &&
         _gtk_css_parser_has_number (parser))
      {
        values[HOFFSET] = _gtk_css_number_value_parse (parser,
                                                       GTK_CSS_PARSE_LENGTH
                                                       | GTK_CSS_NUMBER_AS_PIXELS);
        if (values[HOFFSET] == NULL)
          goto fail;

        values[VOFFSET] = _gtk_css_number_value_parse (parser,
                                                       GTK_CSS_PARSE_LENGTH
                                                       | GTK_CSS_NUMBER_AS_PIXELS);
        if (values[VOFFSET] == NULL)
          goto fail;

        if (_gtk_css_parser_has_number (parser))
          {
            values[RADIUS] = _gtk_css_number_value_parse (parser,
                                                          GTK_CSS_PARSE_LENGTH
                                                          | GTK_CSS_POSITIVE_ONLY
                                                          | GTK_CSS_NUMBER_AS_PIXELS);
            if (values[RADIUS] == NULL)
              goto fail;
          }
        else
          values[RADIUS] = _gtk_css_number_value_new (0.0, GTK_CSS_PX);
243

244
        if (box_shadow_mode && _gtk_css_parser_has_number (parser))
245 246 247 248 249 250 251 252 253 254
          {
            values[SPREAD] = _gtk_css_number_value_parse (parser,
                                                          GTK_CSS_PARSE_LENGTH
                                                          | GTK_CSS_NUMBER_AS_PIXELS);
            if (values[SPREAD] == NULL)
              goto fail;
          }
        else
          values[SPREAD] = _gtk_css_number_value_new (0.0, GTK_CSS_PX);
      }
255
    else if (!inset && box_shadow_mode && _gtk_css_parser_try (parser, "inset", TRUE))
256 257 258 259 260 261 262 263
      {
        if (values[HOFFSET] == NULL)
          goto fail;
        inset = TRUE;
        break;
      }
    else if (values[COLOR] == NULL)
      {
264
        values[COLOR] = _gtk_css_color_value_parse (parser);
265

266
        if (values[COLOR] == NULL)
267 268 269 270 271 272 273 274 275 276 277 278 279
          goto fail;
      }
    else
      {
        /* We parsed everything and there's still stuff left?
         * Pretend we didn't notice and let the normal code produce
         * a 'junk at end of value' error */
        goto fail;
      }
  }
  while (values[HOFFSET] == NULL || !value_is_done_parsing (parser));

  if (values[COLOR] == NULL)
280
    values[COLOR] = _gtk_css_color_value_new_current_color ();
281 282 283 284 285 286 287

  return gtk_css_shadow_value_new (values[HOFFSET], values[VOFFSET],
                                   values[RADIUS], values[SPREAD],
                                   inset, values[COLOR]);

fail:
  for (i = 0; i < N_VALUES; i++)
288
    {
289 290
      if (values[i])
        _gtk_css_value_unref (values[i]);
291
    }
292

293
  return NULL;
294 295
}

296 297 298 299
static gboolean
needs_blur (const GtkCssValue *shadow)
{
  double radius = _gtk_css_number_value_get (shadow->radius, 0);
300 301 302 303

  /* The code doesn't actually do any blurring for radius 1, as it
   * ends up with box filter size 1 */
  if (radius <= 1.0)
304 305 306 307 308
    return FALSE;

  return TRUE;
}

309
static const cairo_user_data_key_t original_cr_key;
310 311 312

static cairo_t *
gtk_css_shadow_value_start_drawing (const GtkCssValue *shadow,
313 314
                                    cairo_t           *cr,
                                    GtkBlurFlags       blur_flags)
315
{
316
  cairo_rectangle_int_t clip_rect;
317 318
  cairo_surface_t *surface;
  cairo_t *blur_cr;
319
  gdouble radius, clip_radius;
320 321
  gboolean blur_x = (blur_flags & GTK_BLUR_X) != 0;
  gboolean blur_y = (blur_flags & GTK_BLUR_Y) != 0;
322

323
  if (!needs_blur (shadow))
324 325
    return cr;

326
  gdk_cairo_get_clip_rectangle (cr, &clip_rect);
327

328
  radius = _gtk_css_number_value_get (shadow->radius, 0);
329
  clip_radius = _gtk_cairo_blur_compute_pixels (radius);
330

331 332 333 334 335 336 337 338
  if (blur_flags & GTK_BLUR_REPEAT)
    {
      if (!blur_x)
        clip_rect.width = 1;
      if (!blur_y)
        clip_rect.height = 1;
    }

339
  /* Create a larger surface to center the blur. */
340 341
  surface = cairo_surface_create_similar_image (cairo_get_target (cr),
                                                CAIRO_FORMAT_A8,
342 343 344 345 346
                                                clip_rect.width + (blur_x ? 2 * clip_radius : 0),
                                                clip_rect.height + (blur_y ? 2 * clip_radius : 0));
  cairo_surface_set_device_offset (surface,
                                   (blur_x ? clip_radius : 0) - clip_rect.x,
                                   (blur_y ? clip_radius : 0) - clip_rect.y);
347
  blur_cr = cairo_create (surface);
348
  cairo_set_user_data (blur_cr, &original_cr_key, cairo_reference (cr), (cairo_destroy_func_t) cairo_destroy);
349 350 351 352

  if (cairo_has_current_point (cr))
    {
      double x, y;
353

354 355 356 357 358 359 360
      cairo_get_current_point (cr, &x, &y);
      cairo_move_to (blur_cr, x, y);
    }

  return blur_cr;
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374
void
mask_surface_repeat (cairo_t         *cr,
                     cairo_surface_t *surface)
{
    cairo_pattern_t *pattern;

    pattern = cairo_pattern_create_for_surface (surface);
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);

    cairo_mask (cr, pattern);

    cairo_pattern_destroy (pattern);
}

375 376
static cairo_t *
gtk_css_shadow_value_finish_drawing (const GtkCssValue *shadow,
377 378
                                     cairo_t           *cr,
                                     GtkBlurFlags       blur_flags)
379 380
{
  gdouble radius;
381 382
  cairo_t *original_cr;
  cairo_surface_t *surface;
383

384
  if (!needs_blur (shadow))
385 386
    return cr;

387
  original_cr = cairo_get_user_data (cr, &original_cr_key);
388 389

  /* Blur the surface. */
390 391
  surface = cairo_get_target (cr);
  radius = _gtk_css_number_value_get (shadow->radius, 0);
392
  _gtk_cairo_blur_surface (surface, radius, blur_flags);
393

394
  gdk_cairo_set_source_rgba (original_cr, _gtk_css_rgba_value_get_rgba (shadow->color));
395 396 397 398
  if (blur_flags & GTK_BLUR_REPEAT)
    mask_surface_repeat (original_cr, surface);
  else
    cairo_mask_surface (original_cr, surface, 0, 0);
399 400

  cairo_destroy (cr);
401

402
  cairo_surface_destroy (surface);
403 404 405 406

  return original_cr;
}

407 408 409 410 411 412 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
static const cairo_user_data_key_t radius_key;
static const cairo_user_data_key_t layout_serial_key;

G_DEFINE_QUARK (GtkCssShadowValue pango_cached_blurred_surface, pango_cached_blurred_surface)

static cairo_surface_t *
get_cached_pango_surface (PangoLayout       *layout,
                          const GtkCssValue *shadow)
{
  cairo_surface_t *cached_surface = g_object_get_qdata (G_OBJECT (layout), pango_cached_blurred_surface_quark ());
  guint cached_radius, cached_serial;
  guint radius, serial;

  if (!cached_surface)
    return NULL;

  radius = _gtk_css_number_value_get (shadow->radius, 0);
  cached_radius = GPOINTER_TO_UINT (cairo_surface_get_user_data (cached_surface, &radius_key));
  if (radius != cached_radius)
    return NULL;

  serial = pango_layout_get_serial (layout);
  cached_serial = GPOINTER_TO_UINT (cairo_surface_get_user_data (cached_surface, &layout_serial_key));
  if (serial != cached_serial)
    return NULL;

  return cached_surface;
}

static cairo_surface_t *
make_blurred_pango_surface (cairo_t           *existing_cr,
                            PangoLayout       *layout,
                            const GtkCssValue *shadow)
{
  cairo_surface_t *surface;
  cairo_t *cr;
  gdouble radius, clip_radius;
444
  gdouble x_scale, y_scale;
445 446 447 448 449 450
  PangoRectangle ink_rect;

  radius = _gtk_css_number_value_get (shadow->radius, 0);

  pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
  clip_radius = _gtk_cairo_blur_compute_pixels (radius);
451
  x_scale = y_scale = 1;
452
  cairo_surface_get_device_scale (cairo_get_target (existing_cr), &x_scale, &y_scale);
453 454 455

  surface = cairo_surface_create_similar_image (cairo_get_target (existing_cr),
                                                CAIRO_FORMAT_A8,
456 457 458
                                                x_scale * (ink_rect.width + 2 * clip_radius),
                                                y_scale * (ink_rect.height + 2 * clip_radius));
  cairo_surface_set_device_scale (surface, x_scale, y_scale);
459 460 461 462
  cairo_surface_set_device_offset (surface, -ink_rect.x + clip_radius, -ink_rect.y + clip_radius);
  cr = cairo_create (surface);
  cairo_move_to (cr, 0, 0);
  _gtk_pango_fill_layout (cr, layout);
463
  _gtk_cairo_blur_surface (surface, radius * x_scale, GTK_BLUR_X | GTK_BLUR_Y);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

  cairo_destroy (cr);

  return surface;
}

static cairo_surface_t *
get_blurred_pango_surface (cairo_t           *cr,
                           PangoLayout       *layout,
                           const GtkCssValue *shadow)
{
  cairo_surface_t *surface;
  guint radius, serial;

  surface = get_cached_pango_surface (layout, shadow);
  if (!surface)
    {
      surface = make_blurred_pango_surface (cr, layout, shadow);

      /* Cache the surface on the PangoLayout */
      radius = _gtk_css_number_value_get (shadow->radius, 0);
      cairo_surface_set_user_data (surface, &radius_key, GUINT_TO_POINTER (radius), NULL);

      serial = pango_layout_get_serial (layout);
      cairo_surface_set_user_data (surface, &layout_serial_key, GUINT_TO_POINTER (serial), NULL);

      g_object_set_qdata_full (G_OBJECT (layout), pango_cached_blurred_surface_quark (),
                               surface, (GDestroyNotify) cairo_surface_destroy);
    }

  return surface;
}

497
void
498 499 500
_gtk_css_shadow_value_paint_layout (const GtkCssValue *shadow,
                                    cairo_t           *cr,
                                    PangoLayout       *layout)
501
{
502
  g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
503

504 505 506
  if (!cairo_has_current_point (cr))
    cairo_move_to (cr, 0, 0);

507
  cairo_save (cr);
508

509 510 511 512 513 514 515 516 517
  if (needs_blur (shadow))
    {
      cairo_surface_t *blurred_surface = get_blurred_pango_surface (cr, layout, shadow);
      double x, y;
      cairo_get_current_point (cr, &x, &y);
      cairo_translate (cr, x, y);
      cairo_translate (cr,
                       _gtk_css_number_value_get (shadow->hoffset, 0),
                       _gtk_css_number_value_get (shadow->voffset, 0));
518

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
      gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
      cairo_mask_surface (cr, blurred_surface, 0, 0);
    }
  else
    {
      /* The no blur case -- just paint directly. */
      cairo_rel_move_to (cr,
                         _gtk_css_number_value_get (shadow->hoffset, 0),
                         _gtk_css_number_value_get (shadow->voffset, 0));
      gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
      _gtk_pango_fill_layout (cr, layout);
      cairo_rel_move_to (cr,
                         - _gtk_css_number_value_get (shadow->hoffset, 0),
                         - _gtk_css_number_value_get (shadow->voffset, 0));
    }
534

535
  cairo_restore (cr);
536 537
}

538
void
539 540
_gtk_css_shadow_value_paint_icon (const GtkCssValue *shadow,
			          cairo_t           *cr)
541 542 543
{
  cairo_pattern_t *pattern;

544
  g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
545

546 547
  cairo_save (cr);
  pattern = cairo_pattern_reference (cairo_get_source (cr));
548

549
  gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
550
  cr = gtk_css_shadow_value_start_drawing (shadow, cr, GTK_BLUR_X | GTK_BLUR_Y);
551

552 553 554
  cairo_translate (cr,
                   _gtk_css_number_value_get (shadow->hoffset, 0),
                   _gtk_css_number_value_get (shadow->voffset, 0));
555
  cairo_mask (cr, pattern);
556

557
  cr = gtk_css_shadow_value_finish_drawing (shadow, cr, GTK_BLUR_X | GTK_BLUR_Y);
558

559 560
  cairo_restore (cr);
  cairo_pattern_destroy (pattern);
561
}
562

563 564 565 566 567 568 569 570
gboolean
_gtk_css_shadow_value_get_inset (const GtkCssValue *shadow)
{
  g_return_val_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW, FALSE);

  return shadow->inset;
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
void
_gtk_css_shadow_value_get_geometry (const GtkCssValue *shadow,
                                    gdouble           *hoffset,
                                    gdouble           *voffset,
                                    gdouble           *radius,
                                    gdouble           *spread)
{
  g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);

  if (hoffset != NULL)
    *hoffset = _gtk_css_number_value_get (shadow->hoffset, 0);
  if (voffset != NULL)
    *voffset = _gtk_css_number_value_get (shadow->voffset, 0);

  if (radius != NULL)
    *radius = _gtk_css_number_value_get (shadow->radius, 0);
  if (spread != NULL)
    *spread = _gtk_css_number_value_get (shadow->spread, 0);
}

591 592 593 594 595 596 597 598 599
static gboolean
has_empty_clip (cairo_t *cr)
{
  double x1, y1, x2, y2;

  cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
  return x1 == x2 && y1 == y2;
}

600 601 602 603
static void
draw_shadow (const GtkCssValue   *shadow,
	     cairo_t             *cr,
	     GtkRoundedBox       *box,
604
	     GtkRoundedBox       *clip_box,
605
	     GtkBlurFlags         blur_flags)
606 607
{
  cairo_t *shadow_cr;
608
  gboolean do_blur;
609

610 611 612
  if (has_empty_clip (cr))
    return;

613
  gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
614 615 616
  do_blur = (blur_flags & (GTK_BLUR_X | GTK_BLUR_Y)) != 0;
  if (do_blur)
    shadow_cr = gtk_css_shadow_value_start_drawing (shadow, cr, blur_flags);
617 618
  else
    shadow_cr = cr;
619 620 621 622 623 624 625 626

  cairo_set_fill_rule (shadow_cr, CAIRO_FILL_RULE_EVEN_ODD);
  _gtk_rounded_box_path (box, shadow_cr);
  if (shadow->inset)
    _gtk_rounded_box_clip_path (clip_box, shadow_cr);

  cairo_fill (shadow_cr);

627 628
  if (do_blur)
    gtk_css_shadow_value_finish_drawing (shadow, shadow_cr, blur_flags);
629 630
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
typedef struct {
  double radius;
  GtkRoundedBoxCorner corner;
} CornerMask;

static guint
corner_mask_hash (CornerMask *mask)
{
  return ((guint)mask->radius << 24) ^
    ((guint)(mask->corner.horizontal*4)) << 12 ^
    ((guint)(mask->corner.vertical*4)) << 0;
}

static gboolean
corner_mask_equal (CornerMask *mask1,
                   CornerMask *mask2)
{
  return
    mask1->radius == mask2->radius &&
    mask1->corner.horizontal == mask2->corner.horizontal &&
    mask1->corner.vertical == mask2->corner.vertical;
}

654 655 656 657 658 659 660 661 662
static void
draw_shadow_corner (const GtkCssValue   *shadow,
                    cairo_t             *cr,
                    GtkRoundedBox       *box,
                    GtkRoundedBox       *clip_box,
                    GtkCssCorner         corner,
                    cairo_rectangle_int_t *drawn_rect)
{
  gdouble radius, clip_radius;
663 664 665 666 667 668 669 670 671 672 673
  int x1, x2, x3, y1, y2, y3, x, y;
  GtkRoundedBox corner_box;
  cairo_t *mask_cr;
  cairo_surface_t *mask;
  cairo_pattern_t *pattern;
  cairo_matrix_t matrix;
  double sx, sy;
  static GHashTable *corner_mask_cache = NULL;
  double max_other;
  CornerMask key;
  gboolean overlapped;
674 675 676 677

  radius = _gtk_css_number_value_get (shadow->radius, 0);
  clip_radius = _gtk_cairo_blur_compute_pixels (radius);

678
  overlapped = FALSE;
679 680 681 682
  if (corner == GTK_CSS_TOP_LEFT || corner == GTK_CSS_BOTTOM_LEFT)
    {
      x1 = floor (box->box.x - clip_radius);
      x2 = ceil (box->box.x + box->corner[corner].horizontal + clip_radius);
683 684 685 686 687 688
      x = x1;
      sx = 1;
      max_other = MAX(box->corner[GTK_CSS_TOP_RIGHT].horizontal, box->corner[GTK_CSS_TOP_RIGHT].horizontal);
      x3 = floor (box->box.x + box->box.width - max_other - clip_radius);
      if (x2 > x3)
        overlapped = TRUE;
689 690 691 692 693
    }
  else
    {
      x1 = floor (box->box.x + box->box.width - box->corner[corner].horizontal - clip_radius);
      x2 = ceil (box->box.x + box->box.width + clip_radius);
694 695 696 697 698 699
      x = x2;
      sx = -1;
      max_other = MAX(box->corner[GTK_CSS_TOP_LEFT].horizontal, box->corner[GTK_CSS_TOP_LEFT].horizontal);
      x3 = ceil (box->box.x + max_other + clip_radius);
      if (x3 > x1)
        overlapped = TRUE;
700 701 702 703 704 705
    }

  if (corner == GTK_CSS_TOP_LEFT || corner == GTK_CSS_TOP_RIGHT)
    {
      y1 = floor (box->box.y - clip_radius);
      y2 = ceil (box->box.y + box->corner[corner].vertical + clip_radius);
706 707 708 709 710 711
      y = y1;
      sy = 1;
      max_other = MAX(box->corner[GTK_CSS_BOTTOM_LEFT].vertical, box->corner[GTK_CSS_BOTTOM_RIGHT].vertical);
      y3 = floor (box->box.y + box->box.height - max_other - clip_radius);
      if (y2 > y3)
        overlapped = TRUE;
712 713 714 715 716
    }
  else
    {
      y1 = floor (box->box.y + box->box.height - box->corner[corner].vertical - clip_radius);
      y2 = ceil (box->box.y + box->box.height + clip_radius);
717 718 719 720 721 722
      y = y2;
      sy = -1;
      max_other = MAX(box->corner[GTK_CSS_TOP_LEFT].vertical, box->corner[GTK_CSS_TOP_RIGHT].vertical);
      y3 = ceil (box->box.y + max_other + clip_radius);
      if (y3 > y1)
        overlapped = TRUE;
723 724 725 726 727 728 729 730 731 732
    }

  drawn_rect->x = x1;
  drawn_rect->y = y1;
  drawn_rect->width = x2 - x1;
  drawn_rect->height = y2 - y1;

  cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1);
  cairo_clip (cr);

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
  if (shadow->inset || overlapped)
    {
      /* Fall back to generic path if inset or if the corner radius
         runs into each other */
      draw_shadow (shadow, cr, box, clip_box, GTK_BLUR_X | GTK_BLUR_Y);
      return;
    }

  if (has_empty_clip (cr))
    return;

  /* At this point we're drawing a blurred outset corner. The only
   * things that affect the output of the blurred mask in this case
   * is:
   *
   * What corner this is, which defines the orientation (sx,sy)
   * and position (x,y)
   *
   * The blur radius (which also defines the clip_radius)
   *
   * The the horizontal and vertical corner radius
   *
   * We apply the first position and orientation when drawing the
   * mask, so we cache rendered masks based on the blur radius and the
   * corner radius.
   */
  if (corner_mask_cache == NULL)
    corner_mask_cache = g_hash_table_new_full ((GHashFunc)corner_mask_hash,
                                               (GEqualFunc)corner_mask_equal,
                                               g_free, (GDestroyNotify)cairo_surface_destroy);

  key.radius = radius;
  key.corner = box->corner[corner];

  mask = g_hash_table_lookup (corner_mask_cache, &key);
  if (mask == NULL)
    {
      mask = cairo_surface_create_similar_image (cairo_get_target (cr), CAIRO_FORMAT_A8,
                                                 drawn_rect->width + clip_radius,
                                                 drawn_rect->height + clip_radius);
      mask_cr = cairo_create (mask);
      _gtk_rounded_box_init_rect (&corner_box, clip_radius, clip_radius, 2*drawn_rect->width, 2*drawn_rect->height);
      corner_box.corner[0] = box->corner[corner];
      _gtk_rounded_box_path (&corner_box, mask_cr);
      cairo_fill (mask_cr);
      _gtk_cairo_blur_surface (mask, radius, GTK_BLUR_X | GTK_BLUR_Y);
      cairo_destroy (mask_cr);
      g_hash_table_insert (corner_mask_cache, g_memdup (&key, sizeof (key)), mask);
    }

  gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
  pattern = cairo_pattern_create_for_surface (mask);
  cairo_matrix_init_identity (&matrix);
  cairo_matrix_scale (&matrix, sx, sy);
  cairo_matrix_translate (&matrix, -x, -y);
  cairo_pattern_set_matrix (pattern, &matrix);
  cairo_mask (cr, pattern);
  cairo_pattern_destroy (pattern);
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
}

static void
draw_shadow_side (const GtkCssValue   *shadow,
                  cairo_t             *cr,
                  GtkRoundedBox       *box,
                  GtkRoundedBox       *clip_box,
                  GtkCssSide           side,
                  cairo_rectangle_int_t *drawn_rect)
{
  GtkBlurFlags blur_flags = GTK_BLUR_REPEAT;
  gdouble radius, clip_radius;
  int x1, x2, y1, y2;

  radius = _gtk_css_number_value_get (shadow->radius, 0);
  clip_radius = _gtk_cairo_blur_compute_pixels (radius);

  if (side == GTK_CSS_TOP || side == GTK_CSS_BOTTOM)
    {
      blur_flags |= GTK_BLUR_Y;
      x1 = floor (box->box.x - clip_radius);
      x2 = ceil (box->box.x + box->box.width + clip_radius);
    }
  else if (side == GTK_CSS_LEFT)
    {
      x1 = floor (box->box.x -clip_radius);
      x2 = ceil (box->box.x + clip_radius);
    }
  else
    {
      x1 = floor (box->box.x + box->box.width -clip_radius);
      x2 = ceil (box->box.x + box->box.width + clip_radius);
    }

  if (side == GTK_CSS_LEFT || side == GTK_CSS_RIGHT)
    {
      blur_flags |= GTK_BLUR_X;
      y1 = floor (box->box.y - clip_radius);
      y2 = ceil (box->box.y + box->box.height + clip_radius);
    }
  else if (side == GTK_CSS_TOP)
    {
      y1 = floor (box->box.y -clip_radius);
      y2 = ceil (box->box.y + clip_radius);
    }
  else
    {
      y1 = floor (box->box.y + box->box.height -clip_radius);
      y2 = ceil (box->box.y + box->box.height + clip_radius);
    }

  drawn_rect->x = x1;
  drawn_rect->y = y1;
  drawn_rect->width = x2 - x1;
  drawn_rect->height = y2 - y1;

  cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1);
  cairo_clip (cr);
  draw_shadow (shadow, cr, box, clip_box, blur_flags);
}

852
void
853 854 855
_gtk_css_shadow_value_paint_box (const GtkCssValue   *shadow,
                                 cairo_t             *cr,
                                 const GtkRoundedBox *padding_box)
856
{
857
  GtkRoundedBox box, clip_box;
858
  double spread, radius, clip_radius, x, y, outside;
859
  double x1c, y1c, x2c, y2c;
860 861

  g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
862

863 864 865
  cairo_clip_extents (cr, &x1c, &y1c, &x2c, &y2c);
  if ((shadow->inset && !_gtk_rounded_box_intersects_rectangle (padding_box, x1c, y1c, x2c, y2c)) ||
      (!shadow->inset && _gtk_rounded_box_contains_rectangle (padding_box, x1c, y1c, x2c, y2c)))
866 867
    return;

868 869
  cairo_save (cr);

870 871
  spread = _gtk_css_number_value_get (shadow->spread, 0);
  radius = _gtk_css_number_value_get (shadow->radius, 0);
872
  clip_radius = _gtk_cairo_blur_compute_pixels (radius);
873 874 875
  x = _gtk_css_number_value_get (shadow->hoffset, 0);
  y = _gtk_css_number_value_get (shadow->voffset, 0);

876 877 878 879 880
  if (shadow->inset)
    {
      _gtk_rounded_box_path (padding_box, cr);
      cairo_clip (cr);
    }
881 882 883 884
  else
    {
      cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
      _gtk_rounded_box_path (padding_box, cr);
885
      outside = spread + clip_radius + MAX (fabs (x), fabs (y));
886 887 888
      clip_box = *padding_box;
      _gtk_rounded_box_grow (&clip_box, outside, outside, outside, outside);
      _gtk_rounded_box_clip_path (&clip_box, cr);
889

890 891
      cairo_clip (cr);
    }
892

893
  box = *padding_box;
894 895
  _gtk_rounded_box_move (&box, x, y);

896 897 898 899
  if (shadow->inset)
    _gtk_rounded_box_shrink (&box, spread, spread, spread, spread);
  else /* Outset */
    _gtk_rounded_box_grow (&box, spread, spread, spread, spread);
900

901
  clip_box = *padding_box;
902
  _gtk_rounded_box_shrink (&clip_box, -clip_radius, -clip_radius, -clip_radius, -clip_radius);
903

904
  if (!needs_blur (shadow))
905
    draw_shadow (shadow, cr, &box, &clip_box, GTK_BLUR_NONE);
906 907
  else
    {
908
      int i;
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
      cairo_region_t *remaining;
      cairo_rectangle_int_t r;

      /* For the blurred case we divide the rendering into 9 parts,
       * 4 of the corners, 4 for the horizonat/vertical lines and
       * one for the interior. We make the non-interior parts
       * large enought to fit the full radius of the blur, so that
       * the interior part can be drawn solidly.
       */

      if (shadow->inset)
	{
	  /* In the inset case we want to paint the whole clip-box.
	   * We could remove the part of "box" where the blur doesn't
	   * reach, but computing that is a bit tricky since the
	   * rounded corners are on the "inside" of it. */
	  r.x = floor (clip_box.box.x);
	  r.y = floor (clip_box.box.y);
	  r.width = ceil (clip_box.box.x + clip_box.box.width) - r.x;
	  r.height = ceil (clip_box.box.y + clip_box.box.height) - r.y;
	  remaining = cairo_region_create_rectangle (&r);
	}
      else
	{
	  /* In the outset case we want to paint the entire box, plus as far
	   * as the radius reaches from it */
935 936 937 938
	  r.x = floor (box.box.x - clip_radius);
	  r.y = floor (box.box.y - clip_radius);
	  r.width = ceil (box.box.x + box.box.width + clip_radius) - r.x;
	  r.height = ceil (box.box.y + box.box.height + clip_radius) - r.y;
939 940 941 942 943 944 945 946

	  remaining = cairo_region_create_rectangle (&r);
	}

      /* First do the corners of box */
      for (i = 0; i < 4; i++)
	{
	  cairo_save (cr);
947 948 949 950
          /* Always clip with remaining to ensure we never draw any area twice */
          gdk_cairo_region (cr, remaining);
          cairo_clip (cr);
	  draw_shadow_corner (shadow, cr, &box, &clip_box, i, &r);
951 952 953 954 955 956 957 958 959 960
	  cairo_restore (cr);

	  /* We drew the region, remove it from remaining */
	  cairo_region_subtract_rectangle (remaining, &r);
	}

      /* Then the sides */
      for (i = 0; i < 4; i++)
	{
	  cairo_save (cr);
961 962 963 964
          /* Always clip with remaining to ensure we never draw any area twice */
          gdk_cairo_region (cr, remaining);
          cairo_clip (cr);
	  draw_shadow_side (shadow, cr, &box, &clip_box, i, &r);
965 966 967 968 969 970 971 972 973 974 975
	  cairo_restore (cr);

	  /* We drew the region, remove it from remaining */
	  cairo_region_subtract_rectangle (remaining, &r);
	}

      /* Then the rest, which needs no blurring */

      cairo_save (cr);
      gdk_cairo_region (cr, remaining);
      cairo_clip (cr);
976
      draw_shadow (shadow, cr, &box, &clip_box, GTK_BLUR_NONE);
977
      cairo_restore (cr);
978 979

      cairo_region_destroy (remaining);
980
    }
981

982 983
  cairo_restore (cr);
}