gtkcssimagegradient.c 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2011 Red Hat Inc.
 *
 * 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.1 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
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

Benjamin Otte's avatar
Benjamin Otte committed
22 23
#define GDK_DISABLE_DEPRECATION_WARNINGS

24 25 26
#include "gtkcssimagegradientprivate.h"

#include "gtkcssprovider.h"
27 28 29

#include "deprecated/gtkgradientprivate.h"
#include "deprecated/gtksymboliccolorprivate.h"
30 31 32 33

G_DEFINE_TYPE (GtkCssImageGradient, _gtk_css_image_gradient, GTK_TYPE_CSS_IMAGE)

static GtkCssImage *
34 35 36
gtk_css_image_gradient_compute (GtkCssImage             *image,
                                guint                    property_id,
                                GtkStyleProviderPrivate *provider,
37
                                GtkCssStyle             *style,
38
                                GtkCssStyle             *parent_style)
39 40 41 42 43 44 45 46 47
{
  GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
  GtkCssImageGradient *copy;

  if (gradient->pattern)
    return g_object_ref (gradient);

  copy = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
  copy->gradient = gtk_gradient_ref (gradient->gradient);
48
  copy->pattern = _gtk_gradient_resolve_full (copy->gradient, provider, style, parent_style);
49 50 51 52

  return GTK_CSS_IMAGE (copy);
}

53
static cairo_pattern_t *
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
fade_pattern (cairo_pattern_t *pattern,
              double           opacity)
{
  double x0, y0, x1, y1, r0, r1;
  cairo_pattern_t *result;
  int i, n;

  switch (cairo_pattern_get_type (pattern))
    {
    case CAIRO_PATTERN_TYPE_LINEAR:
      cairo_pattern_get_linear_points (pattern, &x0, &y0, &x1, &y1);
      result = cairo_pattern_create_linear (x0, y0, x1, y1);
      break;
    case CAIRO_PATTERN_TYPE_RADIAL:
      cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
      result = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
      break;
    default:
      g_return_val_if_reached (NULL);
    }

  cairo_pattern_get_color_stop_count (pattern, &n);
  for (i = 0; i < n; i++)
    {
      double o, r, g, b, a;

      cairo_pattern_get_color_stop_rgba (pattern, i, &o, &r, &g, &b, &a);
      cairo_pattern_add_color_stop_rgba (result, o, r, g, b, a * opacity);
    }

84
  return result;
85 86
}

87
static cairo_pattern_t *
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
transition_pattern (cairo_pattern_t *start,
                    cairo_pattern_t *end,
                    double           progress)
{
  double sx0, sy0, sx1, sy1, sr0, sr1, ex0, ey0, ex1, ey1, er0, er1;
  cairo_pattern_t *result;
  int i, n;

  progress = CLAMP (progress, 0.0, 1.0);

  if (end == NULL)
    return fade_pattern (start, 1.0 - progress);

  g_assert (cairo_pattern_get_type (start) == cairo_pattern_get_type (end));

  switch (cairo_pattern_get_type (start))
    {
    case CAIRO_PATTERN_TYPE_LINEAR:
      cairo_pattern_get_linear_points (start, &sx0, &sy0, &sx1, &sy1);
107
      cairo_pattern_get_linear_points (end, &ex0, &ey0, &ex1, &ey1);
108 109 110 111 112 113 114
      result = cairo_pattern_create_linear ((1 - progress) * sx0 + progress * ex0,
                                            (1 - progress) * sx1 + progress * ex1,
                                            (1 - progress) * sy0 + progress * ey0,
                                            (1 - progress) * sy1 + progress * ey1);
      break;
    case CAIRO_PATTERN_TYPE_RADIAL:
      cairo_pattern_get_radial_circles (start, &sx0, &sy0, &sr0, &sx1, &sy1, &sr1);
115
      cairo_pattern_get_radial_circles (end, &ex0, &ey0, &er0, &ex1, &ey1, &er1);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      result = cairo_pattern_create_radial ((1 - progress) * sx0 + progress * ex0,
                                            (1 - progress) * sy0 + progress * ey0,
                                            (1 - progress) * sr0 + progress * er0,
                                            (1 - progress) * sx1 + progress * ex1,
                                            (1 - progress) * sy1 + progress * ey1,
                                            (1 - progress) * sr1 + progress * er1);
      break;
    default:
      g_return_val_if_reached (NULL);
    }

  cairo_pattern_get_color_stop_count (start, &n);
  for (i = 0; i < n; i++)
    {
      double so, sr, sg, sb, sa, eo, er, eg, eb, ea;

      cairo_pattern_get_color_stop_rgba (start, i, &so, &sr, &sg, &sb, &sa);
133
      cairo_pattern_get_color_stop_rgba (end, i, &eo, &er, &eg, &eb, &ea);
134 135 136 137 138 139 140 141 142 143 144 145

      cairo_pattern_add_color_stop_rgba (result,
                                         (1 - progress) * so + progress * eo,
                                         (1 - progress) * sr + progress * er,
                                         (1 - progress) * sg + progress * eg,
                                         (1 - progress) * sb + progress * eb,
                                         (1 - progress) * sa + progress * ea);
    }

  return result;
}

146
static GtkCssImage *
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
gtk_css_image_gradient_transition (GtkCssImage *start_image,
                                   GtkCssImage *end_image,
                                   guint        property_id,
                                   double       progress)
{
  GtkGradient *start_gradient, *end_gradient, *gradient;
  cairo_pattern_t *start_pattern, *end_pattern;
  GtkCssImageGradient *result;

  start_gradient = GTK_CSS_IMAGE_GRADIENT (start_image)->gradient;
  start_pattern = GTK_CSS_IMAGE_GRADIENT (start_image)->pattern;
  if (end_image == NULL)
    {
      end_gradient = NULL;
      end_pattern = NULL;
    }
  else
    {
      if (!GTK_IS_CSS_IMAGE_GRADIENT (end_image))
        return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);

      end_gradient = GTK_CSS_IMAGE_GRADIENT (end_image)->gradient;
      end_pattern = GTK_CSS_IMAGE_GRADIENT (end_image)->pattern;
    }

  gradient = _gtk_gradient_transition (start_gradient, end_gradient, property_id, progress);
  if (gradient == NULL)
    return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);

  result = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
  result->gradient = gradient;
  result->pattern = transition_pattern (start_pattern, end_pattern, progress);

  return GTK_CSS_IMAGE (result);
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static gboolean
gtk_css_image_gradient_draw_circle (GtkCssImageGradient *image,
                                    cairo_t              *cr,
                                    double               width,
                                    double               height)
{
  cairo_pattern_t *pattern = image->pattern;
  double x0, y0, x1, y1, r0, r1;
  GdkRGBA color0, color1;
  double offset0, offset1;
  int n_stops;

  if (cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_RADIAL)
    return FALSE;
  if (cairo_pattern_get_extend (pattern) != CAIRO_EXTEND_PAD)
    return FALSE;

  cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);

  if (x0 != x1 ||
      y0 != y1 ||
      r0 != 0.0)
    return FALSE;

  cairo_pattern_get_color_stop_count (pattern, &n_stops);
  if (n_stops != 2)
    return FALSE;

  cairo_pattern_get_color_stop_rgba (pattern, 0, &offset0, &color0.red, &color0.green, &color0.blue, &color0.alpha);
  cairo_pattern_get_color_stop_rgba (pattern, 1, &offset1, &color1.red, &color1.green, &color1.blue, &color1.alpha);
  if (offset0 != offset1)
    return FALSE;

  cairo_scale (cr, width, height);

  cairo_rectangle (cr, 0, 0, 1, 1);
  cairo_clip (cr);

  gdk_cairo_set_source_rgba (cr, &color1);
  cairo_paint (cr);

  gdk_cairo_set_source_rgba (cr, &color0);
  cairo_arc (cr, x1, y1, r1 * offset1, 0, 2 * G_PI);
  cairo_fill (cr);

  return TRUE;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244
static void
gtk_css_image_gradient_draw (GtkCssImage        *image,
                             cairo_t            *cr,
                             double              width,
                             double              height)
{
  GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);

  if (!gradient->pattern)
    {
      g_warning ("trying to paint unresolved gradient");
      return;
    }

245 246 247
  if (gtk_css_image_gradient_draw_circle (gradient, cr, width, height))
    return;

248 249 250 251 252 253 254 255 256
  cairo_scale (cr, width, height);

  cairo_rectangle (cr, 0, 0, 1, 1);
  cairo_set_source (cr, gradient->pattern);
  cairo_fill (cr);
}

static gboolean
gtk_css_image_gradient_parse (GtkCssImage  *image,
257
                              GtkCssParser *parser)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
{
  GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);

  gradient->gradient = _gtk_gradient_parse (parser);

  return gradient->gradient != NULL;
}

static void
gtk_css_image_gradient_print (GtkCssImage *image,
                              GString     *string)
{
  GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
  char *s;

  s = gtk_gradient_to_string (gradient->gradient);
  g_string_append (string, s);
  g_free (s);
}

static void
gtk_css_image_gradient_dispose (GObject *object)
{
  GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (object);

  if (gradient->gradient)
    {
      gtk_gradient_unref (gradient->gradient);
      gradient->gradient = NULL;
    }
  if (gradient->pattern)
    {
      cairo_pattern_destroy (gradient->pattern);
      gradient->pattern = NULL;
    }

  G_OBJECT_CLASS (_gtk_css_image_gradient_parent_class)->dispose (object);
}

static void
_gtk_css_image_gradient_class_init (GtkCssImageGradientClass *klass)
{
  GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  image_class->compute = gtk_css_image_gradient_compute;
304
  image_class->transition = gtk_css_image_gradient_transition;
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  image_class->draw = gtk_css_image_gradient_draw;
  image_class->parse = gtk_css_image_gradient_parse;
  image_class->print = gtk_css_image_gradient_print;

  object_class->dispose = gtk_css_image_gradient_dispose;
}

static void
_gtk_css_image_gradient_init (GtkCssImageGradient *image_gradient)
{
}

GtkGradient *
_gtk_gradient_parse (GtkCssParser *parser)
{
  GtkGradient *gradient;
  cairo_pattern_type_t type;
  gdouble coords[6];
  guint i;

  g_return_val_if_fail (parser != NULL, NULL);

  if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
    {
      _gtk_css_parser_error (parser,
                             "Expected '-gtk-gradient'");
      return NULL;
    }

  if (!_gtk_css_parser_try (parser, "(", TRUE))
    {
      _gtk_css_parser_error (parser,
                             "Expected '(' after '-gtk-gradient'");
      return NULL;
    }

  /* Parse gradient type */
  if (_gtk_css_parser_try (parser, "linear", TRUE))
    type = CAIRO_PATTERN_TYPE_LINEAR;
  else if (_gtk_css_parser_try (parser, "radial", TRUE))
    type = CAIRO_PATTERN_TYPE_RADIAL;
  else
    {
      _gtk_css_parser_error (parser,
                             "Gradient type must be 'radial' or 'linear'");
      return NULL;
    }

  /* Parse start/stop position parameters */
  for (i = 0; i < 2; i++)
    {
      if (! _gtk_css_parser_try (parser, ",", TRUE))
        {
          _gtk_css_parser_error (parser,
                                 "Expected ','");
          return NULL;
        }

      if (_gtk_css_parser_try (parser, "left", TRUE))
        coords[i * 3] = 0;
      else if (_gtk_css_parser_try (parser, "right", TRUE))
        coords[i * 3] = 1;
      else if (_gtk_css_parser_try (parser, "center", TRUE))
        coords[i * 3] = 0.5;
      else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
        {
          _gtk_css_parser_error (parser,
                                 "Expected a valid X coordinate");
          return NULL;
        }

      if (_gtk_css_parser_try (parser, "top", TRUE))
        coords[i * 3 + 1] = 0;
      else if (_gtk_css_parser_try (parser, "bottom", TRUE))
        coords[i * 3 + 1] = 1;
      else if (_gtk_css_parser_try (parser, "center", TRUE))
        coords[i * 3 + 1] = 0.5;
      else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
        {
          _gtk_css_parser_error (parser,
                                 "Expected a valid Y coordinate");
          return NULL;
        }

      if (type == CAIRO_PATTERN_TYPE_RADIAL)
        {
          /* Parse radius */
          if (! _gtk_css_parser_try (parser, ",", TRUE))
            {
              _gtk_css_parser_error (parser,
                                     "Expected ','");
              return NULL;
            }

          if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
            {
              _gtk_css_parser_error (parser,
                                     "Expected a numer for the radius");
              return NULL;
            }
        }
    }

  if (type == CAIRO_PATTERN_TYPE_LINEAR)
    gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
  else
    gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
                                        coords[3], coords[4], coords[5]);

  while (_gtk_css_parser_try (parser, ",", TRUE))
    {
      GtkSymbolicColor *color;
      gdouble position;

      if (_gtk_css_parser_try (parser, "from", TRUE))
        {
          position = 0;

          if (!_gtk_css_parser_try (parser, "(", TRUE))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
              return NULL;
            }

        }
      else if (_gtk_css_parser_try (parser, "to", TRUE))
        {
          position = 1;

          if (!_gtk_css_parser_try (parser, "(", TRUE))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
              return NULL;
            }

        }
      else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
        {
          if (!_gtk_css_parser_try (parser, "(", TRUE))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
              return NULL;
            }

          if (!_gtk_css_parser_try_double (parser, &position))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a valid number");
              return NULL;
            }

          if (!_gtk_css_parser_try (parser, ",", TRUE))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a comma");
              return NULL;
            }
        }
      else
        {
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Not a valid color-stop definition");
          return NULL;
        }

479
      color = _gtk_css_symbolic_value_new (parser);
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
      if (color == NULL)
        {
          gtk_gradient_unref (gradient);
          return NULL;
        }

      gtk_gradient_add_color_stop (gradient, position, color);
      gtk_symbolic_color_unref (color);

      if (!_gtk_css_parser_try (parser, ")", TRUE))
        {
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Expected ')'");
          return NULL;
        }
    }

  if (!_gtk_css_parser_try (parser, ")", TRUE))
    {
      gtk_gradient_unref (gradient);
      _gtk_css_parser_error (parser,
                             "Expected ')'");
      return NULL;
    }

  return gradient;
}