gtkcsstransformvalue.c 33.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* GTK - The GIMP Toolkit
 * Copyright (C) 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 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
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtkcssbgsizevalueprivate.h"

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

25
#include "gtkcsstransformvalueprivate.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#include "gtkcssnumbervalueprivate.h"

typedef union _GtkCssTransform GtkCssTransform;

typedef enum {
  GTK_CSS_TRANSFORM_NONE,
  GTK_CSS_TRANSFORM_MATRIX,
  GTK_CSS_TRANSFORM_TRANSLATE,
  GTK_CSS_TRANSFORM_ROTATE,
  GTK_CSS_TRANSFORM_SCALE,
  GTK_CSS_TRANSFORM_SKEW,
  GTK_CSS_TRANSFORM_SKEW_X,
  GTK_CSS_TRANSFORM_SKEW_Y
} GtkCssTransformType;

union _GtkCssTransform {
  GtkCssTransformType type;
  struct {
    GtkCssTransformType type;
    cairo_matrix_t      matrix;
  }                   matrix;
  struct {
    GtkCssTransformType type;
    GtkCssValue        *x;
    GtkCssValue        *y;
  }                   translate, scale, skew;
  struct {
    GtkCssTransformType type;
    GtkCssValue        *rotate;
  }                   rotate;
  struct {
    GtkCssTransformType type;
    GtkCssValue        *skew;
  }                   skew_x, skew_y;
};

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  guint             n_transforms;
  GtkCssTransform   transforms[1];
};

static GtkCssValue *    gtk_css_transform_value_alloc           (guint                  n_values);
static gboolean         gtk_css_transform_value_is_none         (const GtkCssValue     *value);

static void
gtk_css_transform_clear (GtkCssTransform *transform)
{
  switch (transform->type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      break;
    case GTK_CSS_TRANSFORM_TRANSLATE:
      _gtk_css_value_unref (transform->translate.x);
      _gtk_css_value_unref (transform->translate.y);
      break;
    case GTK_CSS_TRANSFORM_ROTATE:
      _gtk_css_value_unref (transform->rotate.rotate);
      break;
    case GTK_CSS_TRANSFORM_SCALE:
      _gtk_css_value_unref (transform->scale.x);
      _gtk_css_value_unref (transform->scale.y);
      break;
    case GTK_CSS_TRANSFORM_SKEW:
      _gtk_css_value_unref (transform->skew.x);
      _gtk_css_value_unref (transform->skew.y);
      break;
    case GTK_CSS_TRANSFORM_SKEW_X:
      _gtk_css_value_unref (transform->skew_x.skew);
      break;
    case GTK_CSS_TRANSFORM_SKEW_Y:
      _gtk_css_value_unref (transform->skew_y.skew);
      break;
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      break;
    }
}

static void
gtk_css_transform_init_identity (GtkCssTransform     *transform,
                                 GtkCssTransformType  type)
{
  switch (type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      cairo_matrix_init_identity (&transform->matrix.matrix);
      break;
    case GTK_CSS_TRANSFORM_TRANSLATE:
      transform->translate.x = _gtk_css_number_value_new (0, GTK_CSS_PX);
      transform->translate.y = _gtk_css_number_value_new (0, GTK_CSS_PX);
      break;
    case GTK_CSS_TRANSFORM_ROTATE:
      transform->rotate.rotate = _gtk_css_number_value_new (0, GTK_CSS_DEG);
      break;
    case GTK_CSS_TRANSFORM_SCALE:
      transform->scale.x = _gtk_css_number_value_new (1, GTK_CSS_NUMBER);
      transform->scale.y = _gtk_css_number_value_new (1, GTK_CSS_NUMBER);
      break;
    case GTK_CSS_TRANSFORM_SKEW:
      transform->skew.x = _gtk_css_number_value_new (0, GTK_CSS_DEG);
      transform->skew.y = _gtk_css_number_value_new (0, GTK_CSS_DEG);
      break;
    case GTK_CSS_TRANSFORM_SKEW_X:
      transform->skew_x.skew = _gtk_css_number_value_new (0, GTK_CSS_DEG);
      break;
    case GTK_CSS_TRANSFORM_SKEW_Y:
      transform->skew_y.skew = _gtk_css_number_value_new (0, GTK_CSS_DEG);
      break;
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      break;
    }

  transform->type = type;
}

static void
gtk_cairo_matrix_skew (cairo_matrix_t *matrix,
                       double          skew_x,
                       double          skew_y)
{
  cairo_matrix_t skew = { 1, tan (skew_x), tan (skew_y), 1, 0, 0 };

  cairo_matrix_multiply (matrix, &skew, matrix);
}

static void
gtk_css_transform_apply (const GtkCssTransform *transform,
                         cairo_matrix_t        *matrix)
{
  switch (transform->type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      cairo_matrix_multiply (matrix, &transform->matrix.matrix, matrix);
      break;
    case GTK_CSS_TRANSFORM_TRANSLATE:
      cairo_matrix_translate (matrix,
                              _gtk_css_number_value_get (transform->translate.x, 100),
                              _gtk_css_number_value_get (transform->translate.y, 100));
      break;
    case GTK_CSS_TRANSFORM_ROTATE:
      cairo_matrix_rotate (matrix,
                           _gtk_css_number_value_get (transform->rotate.rotate, 100) * (2 * G_PI) / 360);
      break;
    case GTK_CSS_TRANSFORM_SCALE:
      cairo_matrix_scale (matrix,
                          _gtk_css_number_value_get (transform->scale.x, 1),
                          _gtk_css_number_value_get (transform->scale.y, 1));
      break;
    case GTK_CSS_TRANSFORM_SKEW:
      gtk_cairo_matrix_skew (matrix,
                             _gtk_css_number_value_get (transform->skew.x, 100),
                             _gtk_css_number_value_get (transform->skew.y, 100));
      break;
    case GTK_CSS_TRANSFORM_SKEW_X:
      gtk_cairo_matrix_skew (matrix,
                             _gtk_css_number_value_get (transform->skew_x.skew, 100),
                             0);
      break;
    case GTK_CSS_TRANSFORM_SKEW_Y:
      gtk_cairo_matrix_skew (matrix,
                             0,
                             _gtk_css_number_value_get (transform->skew_y.skew, 100));
      break;
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      break;
    }
}

/* NB: The returned matrix may be invalid */
static void
202 203
gtk_css_transform_value_compute_matrix (const GtkCssValue *value,
                                        cairo_matrix_t    *matrix)
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 231 232 233
{
  guint i;

  cairo_matrix_init_identity (matrix);

  for (i = 0; i < value->n_transforms; i++)
    {
      gtk_css_transform_apply (&value->transforms[i], matrix);
    }
}

static void
gtk_css_value_transform_free (GtkCssValue *value)
{
  guint i;

  for (i = 0; i < value->n_transforms; i++)
    {
      gtk_css_transform_clear (&value->transforms[i]);
    }

  g_slice_free1 (sizeof (GtkCssValue) + sizeof (GtkCssTransform) * (value->n_transforms - 1), value);
}

/* returns TRUE if dest == src */
static gboolean
gtk_css_transform_compute (GtkCssTransform         *dest,
                           GtkCssTransform         *src,
                           guint                    property_id,
                           GtkStyleProviderPrivate *provider,
234 235
                           GtkCssStyle             *style,
                           GtkCssStyle             *parent_style)
236 237 238 239 240 241 242 243
{
  dest->type = src->type;

  switch (src->type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      return TRUE;
    case GTK_CSS_TRANSFORM_TRANSLATE:
244 245
      dest->translate.x = _gtk_css_value_compute (src->translate.x, property_id, provider, style, parent_style);
      dest->translate.y = _gtk_css_value_compute (src->translate.y, property_id, provider, style, parent_style);
246 247 248
      return dest->translate.x == src->translate.x
          && dest->translate.y == src->translate.y;
    case GTK_CSS_TRANSFORM_ROTATE:
249
      dest->rotate.rotate = _gtk_css_value_compute (src->rotate.rotate, property_id, provider, style, parent_style);
250 251
      return dest->rotate.rotate == src->rotate.rotate;
    case GTK_CSS_TRANSFORM_SCALE:
252 253
      dest->scale.x = _gtk_css_value_compute (src->scale.x, property_id, provider, style, parent_style);
      dest->scale.y = _gtk_css_value_compute (src->scale.y, property_id, provider, style, parent_style);
254 255 256
      return dest->scale.x == src->scale.x
          && dest->scale.y == src->scale.y;
    case GTK_CSS_TRANSFORM_SKEW:
257 258
      dest->skew.x = _gtk_css_value_compute (src->skew.x, property_id, provider, style, parent_style);
      dest->skew.y = _gtk_css_value_compute (src->skew.y, property_id, provider, style, parent_style);
259 260 261
      return dest->skew.x == src->skew.x
          && dest->skew.y == src->skew.y;
    case GTK_CSS_TRANSFORM_SKEW_X:
262
      dest->skew_x.skew = _gtk_css_value_compute (src->skew_x.skew, property_id, provider, style, parent_style);
263 264
      return dest->skew_x.skew == src->skew_x.skew;
    case GTK_CSS_TRANSFORM_SKEW_Y:
265
      dest->skew_y.skew = _gtk_css_value_compute (src->skew_y.skew, property_id, provider, style, parent_style);
266 267 268 269 270 271 272 273 274 275 276 277
      return dest->skew_y.skew == src->skew_y.skew;
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      return FALSE;
    }
}

static GtkCssValue *
gtk_css_value_transform_compute (GtkCssValue             *value,
                                 guint                    property_id,
                                 GtkStyleProviderPrivate *provider,
278
                                 GtkCssStyle             *style,
279
                                 GtkCssStyle             *parent_style)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
{
  GtkCssValue *result;
  gboolean changes;
  guint i;

  /* Special case the 99% case of "none" */
  if (gtk_css_transform_value_is_none (value))
    return _gtk_css_value_ref (value);

  changes = FALSE;
  result = gtk_css_transform_value_alloc (value->n_transforms);

  for (i = 0; i < value->n_transforms; i++)
    {
      changes |= !gtk_css_transform_compute (&result->transforms[i],
                                             &value->transforms[i],
                                             property_id,
                                             provider,
298
                                             style,
299
                                             parent_style);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    }

  if (!changes)
    {
      _gtk_css_value_unref (result);
      result = _gtk_css_value_ref (value);
    }

  return result;
}

static gboolean
gtk_css_transform_equal (const GtkCssTransform *transform1,
                         const GtkCssTransform *transform2)
{
315 316 317
  if (transform1->type != transform2->type)
    return FALSE;

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 479 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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  switch (transform1->type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      return transform1->matrix.matrix.xx == transform2->matrix.matrix.xx
          && transform1->matrix.matrix.xy == transform2->matrix.matrix.xy
          && transform1->matrix.matrix.yx == transform2->matrix.matrix.yx
          && transform1->matrix.matrix.yy == transform2->matrix.matrix.yy
          && transform1->matrix.matrix.x0 == transform2->matrix.matrix.x0
          && transform1->matrix.matrix.y0 == transform2->matrix.matrix.y0;
    case GTK_CSS_TRANSFORM_TRANSLATE:
      return _gtk_css_value_equal (transform1->translate.x, transform2->translate.x)
          && _gtk_css_value_equal (transform1->translate.y, transform2->translate.y);
    case GTK_CSS_TRANSFORM_ROTATE:
      return _gtk_css_value_equal (transform1->rotate.rotate, transform2->rotate.rotate);
    case GTK_CSS_TRANSFORM_SCALE:
      return _gtk_css_value_equal (transform1->scale.x, transform2->scale.x)
          && _gtk_css_value_equal (transform1->scale.y, transform2->scale.y);
    case GTK_CSS_TRANSFORM_SKEW:
      return _gtk_css_value_equal (transform1->skew.x, transform2->skew.x)
          && _gtk_css_value_equal (transform1->skew.y, transform2->skew.y);
    case GTK_CSS_TRANSFORM_SKEW_X:
      return _gtk_css_value_equal (transform1->skew_x.skew, transform2->skew_x.skew);
    case GTK_CSS_TRANSFORM_SKEW_Y:
      return _gtk_css_value_equal (transform1->skew_y.skew, transform2->skew_y.skew);
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      return FALSE;
    }
}

static gboolean
gtk_css_value_transform_equal (const GtkCssValue *value1,
                               const GtkCssValue *value2)
{
  const GtkCssValue *larger;
  guint i, n;

  n = MIN (value1->n_transforms, value2->n_transforms);
  for (i = 0; i < n; i++)
    {
      if (!gtk_css_transform_equal (&value1->transforms[i], &value2->transforms[i]))
        return FALSE;
    }

  larger = value1->n_transforms > value2->n_transforms ? value1 : value2;

  for (; i < larger->n_transforms; i++)
    {
      GtkCssTransform transform;

      gtk_css_transform_init_identity (&transform, larger->transforms[i].type);

      if (!gtk_css_transform_equal (&larger->transforms[i], &transform))
        {
          gtk_css_transform_clear (&transform);
          return FALSE;
        }

      gtk_css_transform_clear (&transform);
    }

  return TRUE;
}

typedef struct _DecomposedMatrix DecomposedMatrix;
struct _DecomposedMatrix {
  double translate[2];
  double scale[2];
  double angle;
  double m11;
  double m12;
  double m21;
  double m22;
};

static void
decomposed_init (DecomposedMatrix     *decomposed,
                 const cairo_matrix_t *matrix)
{
  double row0x = matrix->xx;
  double row0y = matrix->xy;
  double row1x = matrix->yx;
  double row1y = matrix->yy;
  double determinant;

  decomposed->translate[0] = matrix->x0;
  decomposed->translate[1] = matrix->y0;

  decomposed->scale[0] = sqrt (row0x * row0x + row0y * row0y);
  decomposed->scale[1] = sqrt (row1x * row1x + row1y * row1y);

  /* If determinant is negative, one axis was flipped. */
  determinant = row0x * row1y - row0y * row1x;
  
  if (determinant < 0)
    {
      /* Flip axis with minimum unit vector dot product. */
      if (row0x < row1y)
        decomposed->scale[0] = - decomposed->scale[0];
      else
        decomposed->scale[1] = - decomposed->scale[1];
    }
    
  /* Renormalize matrix to remove scale. */
  if (decomposed->scale[0])
    {
      row0x /= decomposed->scale[0];
      row0y /= decomposed->scale[0];
    }
  if (decomposed->scale[1])
    {
      row1x /= decomposed->scale[1];
      row1y /= decomposed->scale[1];
    }

  /* Compute rotation and renormalize matrix. */
  decomposed->angle = atan2(row0y, row0x);

  if (decomposed->angle)
    {
      /* Rotate(-angle) = [cos(angle), sin(angle), -sin(angle), cos(angle)]
       *                = [row0x, -row0y, row0y, row0x]
       * Thanks to the normalization above.
       */
      decomposed->m11 = row0x;
      decomposed->m12 = row0y;
      decomposed->m21 = row1x;
      decomposed->m22 = row1y;
    }
  else
    {
      decomposed->m11 = row0x * row0x - row0y * row1x;
      decomposed->m12 = row0x * row0y - row0y * row1y;
      decomposed->m21 = row0y * row0x + row0x * row1x;
      decomposed->m22 = row0y * row0y + row0x * row1y;
    }
  
  /* Convert into degrees because our rotation functions expect it. */
  decomposed->angle = decomposed->angle * 360 / (2 * G_PI);
}

static void
decomposed_interpolate (DecomposedMatrix       *result,
                        const DecomposedMatrix *start,
                        const DecomposedMatrix *end,
                        double                  progress)
{
  double start_angle, end_angle;

  result->translate[0] = start->translate[0] + (end->translate[0] - start->translate[0]) * progress;
  result->translate[1] = start->translate[1] + (end->translate[1] - start->translate[1]) * progress;
  result->m11 = start->m11 + (end->m11 - start->m11) * progress;
  result->m12 = start->m12 + (end->m12 - start->m12) * progress;
  result->m21 = start->m21 + (end->m21 - start->m21) * progress;
  result->m22 = start->m22 + (end->m22 - start->m22) * progress;

  /* If x-axis of one is flipped, and y-axis of the other,
   * convert to an unflipped rotation.
   */
  if ((start->scale[0] < 0 && end->scale[1] < 0) || (start->scale[1] < 0 && end->scale[0] < 0))
    {
      result->scale[0] = - start->scale[0];
      result->scale[1] = - start->scale[1];
      start_angle = start->angle < 0 ? start->angle + 180 : start->angle - 180;
      end_angle = end->angle;
    }
  else
    {
      result->scale[0] = start->scale[0];
      result->scale[1] = start->scale[1];
      start_angle = start->angle;
      end_angle = end->angle;
    }

  result->scale[0] = result->scale[0] + (end->scale[0] - result->scale[0]) * progress;
  result->scale[1] = result->scale[1] + (end->scale[1] - result->scale[1]) * progress;

  /* Don’t rotate the long way around. */
  if (start_angle == 0)
    start_angle = 360;
  if (end_angle == 0)
    end_angle = 360;

  if (ABS (start_angle - end_angle) > 180)
    {
      if (start_angle > end_angle)
        start_angle -= 360;
      else
        end_angle -= 360;
    }

  result->angle = start_angle + (end_angle - start_angle) * progress;
}

static void
decomposed_apply (const DecomposedMatrix *decomposed,
                  cairo_matrix_t         *matrix)
{
  matrix->xx = decomposed->m11;
  matrix->xy = decomposed->m12;
  matrix->yx = decomposed->m21;
  matrix->yy = decomposed->m22;
  matrix->x0 = 0;
  matrix->y0 = 0;

  /* Translate matrix. */
  cairo_matrix_translate (matrix, decomposed->translate[0], decomposed->translate[1]);
  
  /* Rotate matrix. */
  cairo_matrix_rotate (matrix, decomposed->angle * (2 * G_PI) / 360);
  
  /* Scale matrix. */
  cairo_matrix_scale (matrix, decomposed->scale[0], decomposed->scale[1]);
}

static void
gtk_css_transform_matrix_transition (cairo_matrix_t       *result,
                                     const cairo_matrix_t *start,
                                     const cairo_matrix_t *end,
                                     double                progress)
{
  DecomposedMatrix dresult, dstart, dend;

  decomposed_init (&dstart, start);
  decomposed_init (&dend, end);
  decomposed_interpolate (&dresult, &dstart, &dend, progress);
  decomposed_apply (&dresult, result);
}

static void
gtk_css_transform_transition (GtkCssTransform       *result,
                              const GtkCssTransform *start,
                              const GtkCssTransform *end,
                              guint                  property_id,
                              double                 progress)
{
  result->type = start->type;

  switch (start->type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      gtk_css_transform_matrix_transition (&result->matrix.matrix,
                                           &start->matrix.matrix,
                                           &end->matrix.matrix,
                                           progress);
      break;
    case GTK_CSS_TRANSFORM_TRANSLATE:
      result->translate.x = _gtk_css_value_transition (start->translate.x, end->translate.x, property_id, progress);
      result->translate.y = _gtk_css_value_transition (start->translate.y, end->translate.y, property_id, progress);
      break;
    case GTK_CSS_TRANSFORM_ROTATE:
      result->rotate.rotate = _gtk_css_value_transition (start->rotate.rotate, end->rotate.rotate, property_id, progress);
      break;
    case GTK_CSS_TRANSFORM_SCALE:
      result->scale.x = _gtk_css_value_transition (start->scale.x, end->scale.x, property_id, progress);
      result->scale.y = _gtk_css_value_transition (start->scale.y, end->scale.y, property_id, progress);
      break;
    case GTK_CSS_TRANSFORM_SKEW:
      result->skew.x = _gtk_css_value_transition (start->skew.x, end->skew.x, property_id, progress);
      result->skew.y = _gtk_css_value_transition (start->skew.y, end->skew.y, property_id, progress);
      break;
    case GTK_CSS_TRANSFORM_SKEW_X:
      result->skew_x.skew = _gtk_css_value_transition (start->skew_x.skew, end->skew_x.skew, property_id, progress);
      break;
    case GTK_CSS_TRANSFORM_SKEW_Y:
      result->skew_y.skew = _gtk_css_value_transition (start->skew_y.skew, end->skew_y.skew, property_id, progress);
      break;
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      break;
    }
}

static GtkCssValue *
gtk_css_value_transform_transition (GtkCssValue *start,
                                    GtkCssValue *end,
                                    guint        property_id,
                                    double       progress)
{
  GtkCssValue *result;
  guint i, n;

  if (gtk_css_transform_value_is_none (start))
    {
      if (gtk_css_transform_value_is_none (end))
        return _gtk_css_value_ref (start);

      n = 0;
    }
  else if (gtk_css_transform_value_is_none (end))
    {
      n = 0;
    }
  else
    {
      n = MIN (start->n_transforms, end->n_transforms);
    }

  /* Check transforms are compatible. If not, transition between
   * their result matrices.
   */
  for (i = 0; i < n; i++)
    {
      if (start->transforms[i].type != end->transforms[i].type)
        {
          cairo_matrix_t start_matrix, end_matrix;

          cairo_matrix_init_identity (&start_matrix);
628
          gtk_css_transform_value_compute_matrix (start, &start_matrix);
629
          cairo_matrix_init_identity (&end_matrix);
630
          gtk_css_transform_value_compute_matrix (end, &end_matrix);
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 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 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 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

          result = gtk_css_transform_value_alloc (1);
          result->transforms[0].type = GTK_CSS_TRANSFORM_MATRIX;
          gtk_css_transform_matrix_transition (&result->transforms[0].matrix.matrix, &start_matrix, &end_matrix, progress);

          return result;
        }
    }

  result = gtk_css_transform_value_alloc (MAX (start->n_transforms, end->n_transforms));

  for (i = 0; i < n; i++)
    {
      gtk_css_transform_transition (&result->transforms[i],
                                    &start->transforms[i],
                                    &end->transforms[i],
                                    property_id,
                                    progress);
    }

  for (; i < start->n_transforms; i++)
    {
      GtkCssTransform transform;

      gtk_css_transform_init_identity (&transform, start->transforms[i].type);
      gtk_css_transform_transition (&result->transforms[i],
                                    &start->transforms[i],
                                    &transform,
                                    property_id,
                                    progress);
      gtk_css_transform_clear (&transform);
    }
  for (; i < end->n_transforms; i++)
    {
      GtkCssTransform transform;

      gtk_css_transform_init_identity (&transform, end->transforms[i].type);
      gtk_css_transform_transition (&result->transforms[i],
                                    &transform,
                                    &end->transforms[i],
                                    property_id,
                                    progress);
      gtk_css_transform_clear (&transform);
    }

  g_assert (i == MAX (start->n_transforms, end->n_transforms));

  return result;
}

static void
gtk_css_transform_print (const GtkCssTransform *transform,
                         GString               *string)
{
  char buf[G_ASCII_DTOSTR_BUF_SIZE];

  switch (transform->type)
    {
    case GTK_CSS_TRANSFORM_MATRIX:
      g_string_append (string, "matrix(");
      g_ascii_dtostr (buf, sizeof (buf), transform->matrix.matrix.xx);
      g_string_append (string, buf);
      g_string_append (string, ", ");
      g_ascii_dtostr (buf, sizeof (buf), transform->matrix.matrix.xy);
      g_string_append (string, buf);
      g_string_append (string, ", ");
      g_ascii_dtostr (buf, sizeof (buf), transform->matrix.matrix.x0);
      g_string_append (string, buf);
      g_string_append (string, ", ");
      g_ascii_dtostr (buf, sizeof (buf), transform->matrix.matrix.yx);
      g_string_append (string, buf);
      g_string_append (string, ", ");
      g_ascii_dtostr (buf, sizeof (buf), transform->matrix.matrix.yy);
      g_string_append (string, buf);
      g_string_append (string, ", ");
      g_ascii_dtostr (buf, sizeof (buf), transform->matrix.matrix.y0);
      g_string_append (string, buf);
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_TRANSLATE:
      g_string_append (string, "translate(");
      _gtk_css_value_print (transform->translate.x, string);
      g_string_append (string, ", ");
      _gtk_css_value_print (transform->translate.y, string);
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_ROTATE:
      g_string_append (string, "rotate(");
      _gtk_css_value_print (transform->rotate.rotate, string);
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_SCALE:
      g_string_append (string, "scale(");
      _gtk_css_value_print (transform->scale.x, string);
      if (!_gtk_css_value_equal (transform->scale.x, transform->scale.y))
        {
          g_string_append (string, ", ");
          _gtk_css_value_print (transform->scale.y, string);
        }
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_SKEW:
      g_string_append (string, "skew(");
      _gtk_css_value_print (transform->skew.x, string);
      g_string_append (string, ", ");
      _gtk_css_value_print (transform->skew.y, string);
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_SKEW_X:
      g_string_append (string, "skewX(");
      _gtk_css_value_print (transform->skew_x.skew, string);
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_SKEW_Y:
      g_string_append (string, "skewY(");
      _gtk_css_value_print (transform->skew_y.skew, string);
      g_string_append (string, ")");
      break;
    case GTK_CSS_TRANSFORM_NONE:
    default:
      g_assert_not_reached ();
      break;
    }
}

static void
gtk_css_value_transform_print (const GtkCssValue *value,
                               GString           *string)
{
  guint i;

  if (gtk_css_transform_value_is_none (value))
    {
      g_string_append (string, "none");
      return;
    }

  for (i = 0; i < value->n_transforms; i++)
    {
      if (i > 0)
        g_string_append_c (string, ' ');

      gtk_css_transform_print (&value->transforms[i], string);
    }
}

static const GtkCssValueClass GTK_CSS_VALUE_TRANSFORM = {
  gtk_css_value_transform_free,
  gtk_css_value_transform_compute,
  gtk_css_value_transform_equal,
  gtk_css_value_transform_transition,
  gtk_css_value_transform_print
};

static GtkCssValue none_singleton = { &GTK_CSS_VALUE_TRANSFORM, 1, 0, {  { GTK_CSS_TRANSFORM_NONE } } };

static GtkCssValue *
gtk_css_transform_value_alloc (guint n_transforms)
{
  GtkCssValue *result;
           
  g_return_val_if_fail (n_transforms > 0, NULL);
         
  result = _gtk_css_value_alloc (&GTK_CSS_VALUE_TRANSFORM, sizeof (GtkCssValue) + sizeof (GtkCssTransform) * (n_transforms - 1));
  result->n_transforms = n_transforms;
            
  return result;
}

GtkCssValue *
_gtk_css_transform_value_new_none (void)
{
  return _gtk_css_value_ref (&none_singleton);
}

static gboolean
gtk_css_transform_value_is_none (const GtkCssValue *value)
{
  return value->n_transforms == 0;
}

static gboolean
gtk_css_transform_parse (GtkCssTransform *transform,
                         GtkCssParser    *parser)
{
  if (_gtk_css_parser_try (parser, "matrix(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_MATRIX;

      /* FIXME: Improve error handling here */
      if (!_gtk_css_parser_try_double (parser, &transform->matrix.matrix.xx)
          || !_gtk_css_parser_try (parser, ",", TRUE)
          || !_gtk_css_parser_try_double (parser, &transform->matrix.matrix.xy)
          || !_gtk_css_parser_try (parser, ",", TRUE)
          || !_gtk_css_parser_try_double (parser, &transform->matrix.matrix.x0)
          || !_gtk_css_parser_try (parser, ",", TRUE)
          || !_gtk_css_parser_try_double (parser, &transform->matrix.matrix.yx)
          || !_gtk_css_parser_try (parser, ",", TRUE)
          || !_gtk_css_parser_try_double (parser, &transform->matrix.matrix.yy)
          || !_gtk_css_parser_try (parser, ",", TRUE)
          || !_gtk_css_parser_try_double (parser, &transform->matrix.matrix.y0))
        {
          _gtk_css_parser_error (parser, "invalid syntax for matrix()");
          return FALSE;
        }
    }
  else if (_gtk_css_parser_try (parser, "translate(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_TRANSLATE;

      transform->translate.x = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_LENGTH);
      if (transform->translate.x == NULL)
        return FALSE;

      if (_gtk_css_parser_try (parser, ",", TRUE))
        {
          transform->translate.y = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_LENGTH);
          if (transform->translate.y == NULL)
            {
              _gtk_css_value_unref (transform->translate.x);
              return FALSE;
            }
        }
      else
        {
          transform->translate.y = _gtk_css_number_value_new (0, GTK_CSS_PX);
        }
    }
  else if (_gtk_css_parser_try (parser, "translateX(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_TRANSLATE;

      transform->translate.x = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_LENGTH);
      if (transform->translate.x == NULL)
        return FALSE;
      
      transform->translate.y = _gtk_css_number_value_new (0, GTK_CSS_PX);
    }
  else if (_gtk_css_parser_try (parser, "translateY(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_TRANSLATE;

      transform->translate.y = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_LENGTH);
      if (transform->translate.y == NULL)
        return FALSE;
      
      transform->translate.x = _gtk_css_number_value_new (0, GTK_CSS_PX);
    }
  else if (_gtk_css_parser_try (parser, "scale(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_SCALE;

      transform->scale.x = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER);
      if (transform->scale.x == NULL)
        return FALSE;

      if (_gtk_css_parser_try (parser, ",", TRUE))
        {
          transform->scale.y = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER);
          if (transform->scale.y == NULL)
            {
              _gtk_css_value_unref (transform->scale.x);
              return FALSE;
            }
        }
      else
        {
          transform->scale.y = _gtk_css_value_ref (transform->scale.x);
        }
    }
  else if (_gtk_css_parser_try (parser, "scaleX(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_SCALE;

      transform->scale.x = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER);
      if (transform->scale.x == NULL)
        return FALSE;
      
      transform->scale.y = _gtk_css_number_value_new (1, GTK_CSS_NUMBER);
    }
  else if (_gtk_css_parser_try (parser, "scaleY(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_SCALE;

      transform->scale.y = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER);
      if (transform->scale.y == NULL)
        return FALSE;
      
      transform->scale.x = _gtk_css_number_value_new (1, GTK_CSS_NUMBER);
    }
  else if (_gtk_css_parser_try (parser, "rotate(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_ROTATE;

      transform->rotate.rotate = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
      if (transform->rotate.rotate == NULL)
        return FALSE;
    }
  else if (_gtk_css_parser_try (parser, "skew(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_SKEW;

      transform->skew.x = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
      if (transform->skew.x == NULL)
        return FALSE;

      if (_gtk_css_parser_try (parser, ",", TRUE))
        {
          transform->skew.y = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
          if (transform->skew.y == NULL)
            {
              _gtk_css_value_unref (transform->skew.x);
              return FALSE;
            }
        }
      else
        {
          transform->skew.y = _gtk_css_number_value_new (0, GTK_CSS_DEG);
        }
    }
  else if (_gtk_css_parser_try (parser, "skewX(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_SKEW_X;

      transform->skew_x.skew = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
      if (transform->skew_x.skew == NULL)
        return FALSE;
    }
  else if (_gtk_css_parser_try (parser, "skewY(", TRUE))
    {
      transform->type = GTK_CSS_TRANSFORM_SKEW_Y;

      transform->skew_y.skew = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
      if (transform->skew_y.skew == NULL)
        return FALSE;
    }
  else
    {
      _gtk_css_parser_error (parser, "unknown syntax for transform");
      return FALSE;
    }

  if (!_gtk_css_parser_try (parser, ")", TRUE))
    {
      gtk_css_transform_clear (transform);
      _gtk_css_parser_error (parser, "Expected closing ')'");
      return FALSE;
    }

  return TRUE;
}

GtkCssValue *
_gtk_css_transform_value_parse (GtkCssParser *parser)
{
  GtkCssValue *value;
  GArray *array;
  guint i;

  if (_gtk_css_parser_try (parser, "none", TRUE))
    return _gtk_css_transform_value_new_none ();

  array = g_array_new (FALSE, FALSE, sizeof (GtkCssTransform));

  do {
    GtkCssTransform transform;

    if (!gtk_css_transform_parse (&transform, parser))
      {
        for (i = 0; i < array->len; i++)
          {
            gtk_css_transform_clear (&g_array_index (array, GtkCssTransform, i));
          }
        g_array_free (array, TRUE);
        return NULL;
      }
    g_array_append_val (array, transform);
  } while (!_gtk_css_parser_begins_with (parser, ';'));

  value = gtk_css_transform_value_alloc (array->len);
  memcpy (value->transforms, array->data, sizeof (GtkCssTransform) * array->len);

  g_array_free (array, TRUE);

  return value;
}

gboolean
1019 1020
_gtk_css_transform_value_get_matrix (const GtkCssValue *transform,
                                     cairo_matrix_t    *matrix)
1021
{
1022
  cairo_matrix_t invert;
1023 1024

  g_return_val_if_fail (transform->class == &GTK_CSS_VALUE_TRANSFORM, FALSE);
1025
  g_return_val_if_fail (matrix != NULL, FALSE);
1026
  
1027 1028 1029
  gtk_css_transform_value_compute_matrix (transform, &invert);

  *matrix = invert;
1030 1031 1032 1033 1034 1035 1036

  if (cairo_matrix_invert (&invert) != CAIRO_STATUS_SUCCESS)
    return FALSE;

  return TRUE;
}