2019-02-17 11:54:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Benjamin Otte
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-25 12:00:35 +00:00
|
|
|
* GskTransform: (ref-func gsk_transform_ref) (unref-func gsk_transform_unref)
|
|
|
|
*
|
|
|
|
* `GskTransform` is an object to describe transform matrices.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Unlike `graphene_matrix_t`, `GskTransform` retains the steps in how
|
|
|
|
* a transform was constructed, and allows inspecting them. It is modeled
|
|
|
|
* after the way CSS describes transforms.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* `GskTransform` objects are immutable and cannot be changed after creation.
|
2019-02-17 11:54:04 +00:00
|
|
|
* This means code can safely expose them as properties of objects without
|
|
|
|
* having to worry about others changing them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
#include "gsktransformprivate.h"
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
struct _GskTransformClass
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
gsize struct_size;
|
|
|
|
const char *type_name;
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
void (* finalize) (GskTransform *transform);
|
|
|
|
void (* to_matrix) (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix);
|
2019-03-04 18:33:04 +00:00
|
|
|
void (* apply_2d) (GskTransform *transform,
|
2019-03-01 05:24:33 +00:00
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy);
|
2019-03-04 18:33:04 +00:00
|
|
|
void (* apply_affine) (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
float *out_scale_x,
|
|
|
|
float *out_scale_y,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy);
|
2019-03-04 18:33:04 +00:00
|
|
|
void (* apply_translate) (GskTransform *transform,
|
2019-03-01 05:24:33 +00:00
|
|
|
float *out_dx,
|
|
|
|
float *out_dy);
|
2019-02-28 08:52:49 +00:00
|
|
|
void (* print) (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string);
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform * (* apply) (GskTransform *transform,
|
|
|
|
GskTransform *apply_to);
|
2019-03-03 16:57:11 +00:00
|
|
|
GskTransform * (* invert) (GskTransform *transform,
|
|
|
|
GskTransform *next);
|
2019-02-17 11:54:04 +00:00
|
|
|
/* both matrices have the same type */
|
2019-02-28 08:52:49 +00:00
|
|
|
gboolean (* equal) (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform);
|
2019-02-17 11:54:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
G_DEFINE_BOXED_TYPE (GskTransform, gsk_transform,
|
|
|
|
gsk_transform_ref,
|
|
|
|
gsk_transform_unref)
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
static gboolean
|
2019-03-01 05:48:30 +00:00
|
|
|
gsk_transform_is_identity (GskTransform *self);
|
2020-07-24 15:48:54 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_transform_matrix_with_category (GskTransform *next,
|
|
|
|
const graphene_matrix_t*matrix,
|
|
|
|
GskTransformCategory category);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-06-02 12:16:45 +00:00
|
|
|
static inline gboolean
|
|
|
|
gsk_transform_has_class (GskTransform *self,
|
|
|
|
const GskTransformClass *transform_class)
|
|
|
|
{
|
|
|
|
return self != NULL && self->transform_class == transform_class;
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/*< private >
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_alloc:
|
2019-02-17 11:54:04 +00:00
|
|
|
* @transform_class: class structure for this self
|
2019-03-04 08:20:58 +00:00
|
|
|
* @category: The category of this transform. Will be used to initialize
|
2021-05-18 21:05:26 +00:00
|
|
|
* the result's category together with &next's category
|
2021-05-21 00:45:06 +00:00
|
|
|
* @next: (transfer full) (nullable): Next transform to multiply with
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-05-20 03:40:54 +00:00
|
|
|
* Returns: (transfer full): the newly created `GskTransform`
|
2019-02-17 11:54:04 +00:00
|
|
|
*/
|
|
|
|
static gpointer
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_alloc (const GskTransformClass *transform_class,
|
2019-03-04 08:20:58 +00:00
|
|
|
GskTransformCategory category,
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *next)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *self;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (transform_class != NULL, NULL);
|
|
|
|
|
2019-03-22 11:45:57 +00:00
|
|
|
self = g_atomic_rc_box_alloc0 (transform_class->struct_size);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
self->transform_class = transform_class;
|
2019-03-04 18:33:04 +00:00
|
|
|
self->category = next ? MIN (category, next->category) : category;
|
2020-12-18 09:10:56 +00:00
|
|
|
if (gsk_transform_is_identity (next))
|
|
|
|
gsk_transform_unref (next);
|
|
|
|
else
|
|
|
|
self->next = next;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** IDENTITY ***/
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_finalize (GskTransform *transform)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_to_matrix (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
|
|
|
graphene_matrix_init_identity (out_matrix);
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_identity_transform_apply_2d (GskTransform *transform,
|
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_apply_affine (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
float *out_scale_x,
|
|
|
|
float *out_scale_y,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_identity_transform_apply_translate (GskTransform *transform,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_print (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string)
|
|
|
|
{
|
2019-06-03 16:19:57 +00:00
|
|
|
g_string_append (string, "none");
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_identity_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-03-03 16:57:11 +00:00
|
|
|
/* We do the following to make sure inverting a non-NULL transform
|
|
|
|
* will return a non-NULL transform.
|
|
|
|
*/
|
|
|
|
if (apply_to)
|
|
|
|
return apply_to;
|
|
|
|
else
|
|
|
|
return gsk_transform_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static GskTransform *
|
|
|
|
gsk_identity_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
/* We do the following to make sure inverting a non-NULL transform
|
|
|
|
* will return a non-NULL transform.
|
|
|
|
*/
|
|
|
|
if (next)
|
|
|
|
return next;
|
|
|
|
else
|
|
|
|
return gsk_transform_new ();
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static const GskTransformClass GSK_IDENTITY_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskTransform),
|
2020-12-18 09:12:28 +00:00
|
|
|
"GskIdentityTransform",
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_finalize,
|
|
|
|
gsk_identity_transform_to_matrix,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_identity_transform_apply_2d,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_apply_affine,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_identity_transform_apply_translate,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_print,
|
|
|
|
gsk_identity_transform_apply,
|
2019-03-03 16:57:11 +00:00
|
|
|
gsk_identity_transform_invert,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_identity_transform_equal,
|
2019-02-17 11:54:04 +00:00
|
|
|
};
|
|
|
|
|
2019-03-01 05:48:30 +00:00
|
|
|
/*<private>
|
|
|
|
* gsk_transform_is_identity:
|
2021-05-21 00:45:06 +00:00
|
|
|
* @transform: (nullable): A transform
|
2019-03-01 05:48:30 +00:00
|
|
|
*
|
|
|
|
* Checks if the transform is a representation of the identity
|
|
|
|
* transform.
|
|
|
|
*
|
|
|
|
* This is different from a transform like `scale(2) scale(0.5)`
|
|
|
|
* which just results in an identity transform when simplified.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if this transform is a representation of
|
2021-05-18 21:05:26 +00:00
|
|
|
* the identity transform
|
2019-03-01 05:48:30 +00:00
|
|
|
**/
|
|
|
|
static gboolean
|
|
|
|
gsk_transform_is_identity (GskTransform *self)
|
|
|
|
{
|
|
|
|
return self == NULL ||
|
|
|
|
(self->transform_class == &GSK_IDENTITY_TRANSFORM_CLASS && gsk_transform_is_identity (self->next));
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/*** MATRIX ***/
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
typedef struct _GskMatrixTransform GskMatrixTransform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
struct _GskMatrixTransform
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform parent;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_matrix_t matrix;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_finalize (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_to_matrix (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_matrix_init_from_matrix (out_matrix, &self->matrix);
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_matrix_transform_apply_2d (GskTransform *transform,
|
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2019-03-04 18:33:04 +00:00
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
|
|
|
graphene_matrix_t mat;
|
|
|
|
|
|
|
|
graphene_matrix_init_from_2d (&mat,
|
|
|
|
*out_xx, *out_yx,
|
|
|
|
*out_xy, *out_yy,
|
|
|
|
*out_dx, *out_dy);
|
|
|
|
graphene_matrix_multiply (&self->matrix, &mat, &mat);
|
|
|
|
|
|
|
|
/* not using graphene_matrix_to_2d() because it may
|
|
|
|
* fail the is_2d() check due to improper rounding */
|
|
|
|
*out_xx = graphene_matrix_get_value (&mat, 0, 0);
|
|
|
|
*out_yx = graphene_matrix_get_value (&mat, 0, 1);
|
|
|
|
*out_xy = graphene_matrix_get_value (&mat, 1, 0);
|
|
|
|
*out_yy = graphene_matrix_get_value (&mat, 1, 1);
|
|
|
|
*out_dx = graphene_matrix_get_value (&mat, 3, 0);
|
|
|
|
*out_dy = graphene_matrix_get_value (&mat, 3, 1);
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_apply_affine (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
float *out_scale_x,
|
|
|
|
float *out_scale_y,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-04 08:20:58 +00:00
|
|
|
switch (transform->category)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_UNKNOWN:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_ANY:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_3D:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D:
|
2019-02-17 11:54:04 +00:00
|
|
|
default:
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_AFFINE:
|
2019-03-22 13:54:29 +00:00
|
|
|
*out_dx += *out_scale_x * graphene_matrix_get_x_translation (&self->matrix);
|
|
|
|
*out_dy += *out_scale_y * graphene_matrix_get_y_translation (&self->matrix);
|
|
|
|
*out_scale_x *= graphene_matrix_get_x_scale (&self->matrix);
|
|
|
|
*out_scale_y *= graphene_matrix_get_y_scale (&self->matrix);
|
2019-03-04 18:33:04 +00:00
|
|
|
break;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_TRANSLATE:
|
2019-03-22 13:54:29 +00:00
|
|
|
*out_dx += *out_scale_x * graphene_matrix_get_x_translation (&self->matrix);
|
|
|
|
*out_dy += *out_scale_y * graphene_matrix_get_y_translation (&self->matrix);
|
2019-03-04 18:33:04 +00:00
|
|
|
break;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_IDENTITY:
|
2019-03-04 18:33:04 +00:00
|
|
|
break;
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_matrix_transform_apply_translate (GskTransform *transform,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
|
|
|
|
2019-03-04 08:20:58 +00:00
|
|
|
switch (transform->category)
|
2019-03-01 05:24:33 +00:00
|
|
|
{
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_UNKNOWN:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_ANY:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_3D:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_AFFINE:
|
2019-03-01 05:24:33 +00:00
|
|
|
default:
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2019-03-01 05:24:33 +00:00
|
|
|
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_TRANSLATE:
|
2019-03-22 13:54:29 +00:00
|
|
|
*out_dx += graphene_matrix_get_x_translation (&self->matrix);
|
|
|
|
*out_dy += graphene_matrix_get_y_translation (&self->matrix);
|
2019-03-04 18:33:04 +00:00
|
|
|
break;
|
2019-03-01 05:24:33 +00:00
|
|
|
|
2019-03-01 06:51:23 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_IDENTITY:
|
2019-03-04 18:33:04 +00:00
|
|
|
break;
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static void
|
|
|
|
string_append_double (GString *string,
|
|
|
|
double d)
|
|
|
|
{
|
|
|
|
char buf[G_ASCII_DTOSTR_BUF_SIZE];
|
|
|
|
|
|
|
|
g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%g", d);
|
|
|
|
g_string_append (string, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_print (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
guint i;
|
|
|
|
float f[16];
|
|
|
|
|
|
|
|
g_string_append (string, "matrix3d(");
|
|
|
|
graphene_matrix_to_float (&self->matrix, f);
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
string_append_double (string, f[i]);
|
|
|
|
}
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_matrix_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
return gsk_transform_matrix_with_category (apply_to,
|
2019-02-17 11:54:04 +00:00
|
|
|
&self->matrix,
|
2019-03-04 08:20:58 +00:00
|
|
|
transform->category);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 16:57:11 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_matrix_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
GskMatrixTransform *self = (GskMatrixTransform *) transform;
|
|
|
|
graphene_matrix_t inverse;
|
|
|
|
|
|
|
|
if (!graphene_matrix_inverse (&self->matrix, &inverse))
|
|
|
|
{
|
|
|
|
gsk_transform_unref (next);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gsk_transform_matrix_with_category (next,
|
|
|
|
&inverse,
|
2019-03-04 08:20:58 +00:00
|
|
|
transform->category);
|
2019-03-03 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static gboolean
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskMatrixTransform *first = (GskMatrixTransform *) first_transform;
|
|
|
|
GskMatrixTransform *second = (GskMatrixTransform *) second_transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-22 12:38:36 +00:00
|
|
|
if (graphene_matrix_equal_fast (&first->matrix, &second->matrix))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return graphene_matrix_equal (&first->matrix, &second->matrix);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static const GskTransformClass GSK_TRANSFORM_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskMatrixTransform),
|
|
|
|
"GskMatrixTransform",
|
|
|
|
gsk_matrix_transform_finalize,
|
|
|
|
gsk_matrix_transform_to_matrix,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_matrix_transform_apply_2d,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_apply_affine,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_matrix_transform_apply_translate,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_print,
|
|
|
|
gsk_matrix_transform_apply,
|
2019-03-03 16:57:11 +00:00
|
|
|
gsk_matrix_transform_invert,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_matrix_transform_equal,
|
2019-02-17 11:54:04 +00:00
|
|
|
};
|
|
|
|
|
2020-07-24 15:48:54 +00:00
|
|
|
static GskTransform *
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_matrix_with_category (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
const graphene_matrix_t *matrix,
|
2019-03-01 06:51:23 +00:00
|
|
|
GskTransformCategory category)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-03-04 08:20:58 +00:00
|
|
|
GskMatrixTransform *result = gsk_transform_alloc (&GSK_TRANSFORM_TRANSFORM_CLASS, category, next);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_matrix_init_from_matrix (&result->matrix, matrix);
|
|
|
|
|
|
|
|
return &result->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_matrix:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2019-02-17 11:54:04 +00:00
|
|
|
* @matrix: the matrix to multiply @next with
|
|
|
|
*
|
|
|
|
* Multiplies @next with the given @matrix.
|
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2019-02-17 11:54:04 +00:00
|
|
|
**/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_matrix (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
const graphene_matrix_t *matrix)
|
|
|
|
{
|
2019-03-01 06:51:23 +00:00
|
|
|
return gsk_transform_matrix_with_category (next, matrix, GSK_TRANSFORM_CATEGORY_UNKNOWN);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** TRANSLATE ***/
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
typedef struct _GskTranslateTransform GskTranslateTransform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
struct _GskTranslateTransform
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform parent;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_point3d_t point;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_finalize (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_to_matrix (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_matrix_init_translate (out_matrix, &self->point);
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_translate_transform_apply_2d (GskTransform *transform,
|
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert (self->point.z == 0.0);
|
2019-03-01 05:24:33 +00:00
|
|
|
|
|
|
|
*out_dx += *out_xx * self->point.x + *out_xy * self->point.y;
|
|
|
|
*out_dy += *out_yx * self->point.x + *out_yy * self->point.y;
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_apply_affine (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
float *out_scale_x,
|
|
|
|
float *out_scale_y,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert (self->point.z == 0.0);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
*out_dx += *out_scale_x * self->point.x;
|
|
|
|
*out_dy += *out_scale_y * self->point.y;
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_translate_transform_apply_translate (GskTransform *transform,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert (self->point.z == 0.0);
|
2019-03-01 05:24:33 +00:00
|
|
|
|
|
|
|
*out_dx += self->point.x;
|
|
|
|
*out_dy += self->point.y;
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_translate_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
return gsk_transform_translate_3d (apply_to, &self->point);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 16:57:11 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_translate_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_translate_3d (next, &GRAPHENE_POINT3D_INIT (-self->point.x, -self->point.y, -self->point.z));
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static gboolean
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTranslateTransform *first = (GskTranslateTransform *) first_transform;
|
|
|
|
GskTranslateTransform *second = (GskTranslateTransform *) second_transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2020-06-07 06:45:35 +00:00
|
|
|
return G_APPROX_VALUE (first->point.x, second->point.x, FLT_EPSILON) &&
|
|
|
|
G_APPROX_VALUE (first->point.y, second->point.y, FLT_EPSILON) &&
|
|
|
|
G_APPROX_VALUE (first->point.z, second->point.z, FLT_EPSILON);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_print (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTranslateTransform *self = (GskTranslateTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
if (self->point.z == 0)
|
|
|
|
g_string_append (string, "translate(");
|
|
|
|
else
|
|
|
|
g_string_append (string, "translate3d(");
|
|
|
|
|
|
|
|
string_append_double (string, self->point.x);
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
string_append_double (string, self->point.y);
|
|
|
|
if (self->point.z != 0)
|
|
|
|
{
|
|
|
|
g_string_append (string, ", ");
|
2019-03-02 03:35:44 +00:00
|
|
|
string_append_double (string, self->point.z);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static const GskTransformClass GSK_TRANSLATE_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskTranslateTransform),
|
|
|
|
"GskTranslateTransform",
|
|
|
|
gsk_translate_transform_finalize,
|
|
|
|
gsk_translate_transform_to_matrix,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_translate_transform_apply_2d,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_apply_affine,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_translate_transform_apply_translate,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_print,
|
|
|
|
gsk_translate_transform_apply,
|
2019-03-03 16:57:11 +00:00
|
|
|
gsk_translate_transform_invert,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_translate_transform_equal,
|
2019-02-17 11:54:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_translate:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2020-06-13 06:25:51 +00:00
|
|
|
* @point: the point to translate the transform by
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Translates @next in 2-dimensional space by @point.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2019-02-17 11:54:04 +00:00
|
|
|
**/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_translate (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
const graphene_point_t *point)
|
|
|
|
{
|
|
|
|
graphene_point3d_t point3d;
|
|
|
|
|
|
|
|
graphene_point3d_init (&point3d, point->x, point->y, 0);
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
return gsk_transform_translate_3d (next, &point3d);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_translate_3d:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2020-06-13 06:25:51 +00:00
|
|
|
* @point: the point to translate the transform by
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
|
|
|
* Translates @next by @point.
|
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2019-02-17 11:54:04 +00:00
|
|
|
**/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_translate_3d (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
const graphene_point3d_t *point)
|
|
|
|
{
|
2019-03-04 08:20:58 +00:00
|
|
|
GskTranslateTransform *result;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
|
|
|
if (graphene_point3d_equal (point, graphene_point3d_zero ()))
|
2019-07-06 10:56:28 +00:00
|
|
|
return next;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
2019-06-02 12:16:45 +00:00
|
|
|
if (gsk_transform_has_class (next, &GSK_TRANSLATE_TRANSFORM_CLASS))
|
|
|
|
{
|
|
|
|
GskTranslateTransform *t = (GskTranslateTransform *) next;
|
|
|
|
GskTransform *r = gsk_transform_translate_3d (gsk_transform_ref (next->next),
|
|
|
|
&GRAPHENE_POINT3D_INIT(t->point.x + point->x,
|
|
|
|
t->point.y + point->y,
|
|
|
|
t->point.z + point->z));
|
|
|
|
gsk_transform_unref (next);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-03-04 08:20:58 +00:00
|
|
|
result = gsk_transform_alloc (&GSK_TRANSLATE_TRANSFORM_CLASS,
|
|
|
|
point->z == 0.0 ? GSK_TRANSFORM_CATEGORY_2D_TRANSLATE
|
|
|
|
: GSK_TRANSFORM_CATEGORY_3D,
|
|
|
|
next);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_point3d_init_from_point (&result->point, point);
|
|
|
|
|
|
|
|
return &result->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** ROTATE ***/
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
typedef struct _GskRotateTransform GskRotateTransform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
struct _GskRotateTransform
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform parent;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
float angle;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_rotate_transform_finalize (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-19 11:30:08 +00:00
|
|
|
static inline void
|
|
|
|
_sincos (float deg,
|
|
|
|
float *out_s,
|
|
|
|
float *out_c)
|
|
|
|
{
|
|
|
|
if (deg == 90.0)
|
|
|
|
{
|
|
|
|
*out_c = 0.0;
|
|
|
|
*out_s = 1.0;
|
|
|
|
}
|
|
|
|
else if (deg == 180.0)
|
|
|
|
{
|
|
|
|
*out_c = -1.0;
|
|
|
|
*out_s = 0.0;
|
|
|
|
}
|
|
|
|
else if (deg == 270.0)
|
|
|
|
{
|
|
|
|
*out_c = 0.0;
|
|
|
|
*out_s = -1.0;
|
|
|
|
}
|
|
|
|
else if (deg == 0.0)
|
|
|
|
{
|
|
|
|
*out_c = 1.0;
|
|
|
|
*out_s = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-08 09:05:44 +00:00
|
|
|
float angle = deg * M_PI / 180.0;
|
|
|
|
|
|
|
|
#ifdef HAVE_SINCOSF
|
|
|
|
sincosf (angle, out_s, out_c);
|
|
|
|
#else
|
|
|
|
*out_s = sinf (angle);
|
|
|
|
*out_c = cosf (angle);
|
|
|
|
#endif
|
|
|
|
|
2019-12-19 11:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_rotate_transform_to_matrix (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskRotateTransform *self = (GskRotateTransform *) transform;
|
2019-12-19 11:30:08 +00:00
|
|
|
float c, s;
|
|
|
|
|
|
|
|
_sincos (self->angle, &s, &c);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-02 02:21:09 +00:00
|
|
|
graphene_matrix_init_from_2d (out_matrix,
|
|
|
|
c, s,
|
|
|
|
-s, c,
|
|
|
|
0, 0);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_rotate_transform_apply_2d (GskTransform *transform,
|
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2019-03-02 02:21:09 +00:00
|
|
|
GskRotateTransform *self = (GskRotateTransform *) transform;
|
2019-12-19 11:30:08 +00:00
|
|
|
float s, c, xx, xy, yx, yy;
|
2019-03-02 02:21:09 +00:00
|
|
|
|
2019-12-19 11:30:08 +00:00
|
|
|
_sincos (self->angle, &s, &c);
|
2019-03-02 02:21:09 +00:00
|
|
|
|
|
|
|
xx = c * *out_xx + s * *out_xy;
|
|
|
|
yx = c * *out_yx + s * *out_yy;
|
|
|
|
xy = -s * *out_xx + c * *out_xy;
|
|
|
|
yy = -s * *out_yx + c * *out_yy;
|
|
|
|
|
|
|
|
*out_xx = xx;
|
|
|
|
*out_yx = yx;
|
|
|
|
*out_xy = xy;
|
|
|
|
*out_yy = yy;
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_rotate_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskRotateTransform *self = (GskRotateTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-02 02:21:09 +00:00
|
|
|
return gsk_transform_rotate (apply_to, self->angle);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 16:57:11 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_rotate_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
GskRotateTransform *self = (GskRotateTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_rotate (next, - self->angle);
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static gboolean
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_rotate_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskRotateTransform *first = (GskRotateTransform *) first_transform;
|
|
|
|
GskRotateTransform *second = (GskRotateTransform *) second_transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-22 12:39:28 +00:00
|
|
|
return G_APPROX_VALUE (first->angle, second->angle, 0.01f);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_rotate_transform_print (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskRotateTransform *self = (GskRotateTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-02 02:21:09 +00:00
|
|
|
g_string_append (string, "rotate(");
|
|
|
|
string_append_double (string, self->angle);
|
|
|
|
g_string_append (string, ")");
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static const GskTransformClass GSK_ROTATE_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskRotateTransform),
|
|
|
|
"GskRotateTransform",
|
|
|
|
gsk_rotate_transform_finalize,
|
|
|
|
gsk_rotate_transform_to_matrix,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_rotate_transform_apply_2d,
|
2019-03-04 18:33:04 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_rotate_transform_print,
|
|
|
|
gsk_rotate_transform_apply,
|
2019-03-03 16:57:11 +00:00
|
|
|
gsk_rotate_transform_invert,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_rotate_transform_equal,
|
2019-02-17 11:54:04 +00:00
|
|
|
};
|
|
|
|
|
2019-12-19 11:29:33 +00:00
|
|
|
static inline float
|
|
|
|
normalize_angle (float angle)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (angle >= 0 && angle < 360)
|
|
|
|
return angle;
|
|
|
|
|
2020-02-11 09:00:34 +00:00
|
|
|
while (angle >= 360)
|
|
|
|
angle -= 360;
|
|
|
|
while (angle < 0)
|
|
|
|
angle += 360;
|
2019-12-19 11:29:33 +00:00
|
|
|
|
2020-02-11 09:00:34 +00:00
|
|
|
/* Due to precision issues we may end up with a result that is just
|
|
|
|
* past the allowed range when rounded. For example, something like
|
|
|
|
* -epsilon + 360 when rounded to a float may end up with 360.
|
|
|
|
* So, we handle these cases by returning the exact value 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (angle >= 360)
|
|
|
|
angle = 0;
|
2019-12-19 11:29:33 +00:00
|
|
|
|
2020-02-11 09:00:34 +00:00
|
|
|
g_assert (angle < 360.0);
|
|
|
|
g_assert (angle >= 0.0);
|
2020-02-02 12:20:40 +00:00
|
|
|
|
2020-02-11 09:00:34 +00:00
|
|
|
return angle;
|
2019-12-19 11:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_rotate:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2019-02-17 11:54:04 +00:00
|
|
|
* @angle: the rotation angle, in degrees (clockwise)
|
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Rotates @next @angle degrees in 2D - or in 3D-speak, around the z axis.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_rotate (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
float angle)
|
|
|
|
{
|
2019-06-02 12:16:45 +00:00
|
|
|
GskRotateTransform *result;
|
|
|
|
|
2019-06-03 14:20:12 +00:00
|
|
|
if (angle == 0.0f)
|
2019-07-06 10:56:28 +00:00
|
|
|
return next;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
2019-06-02 12:16:45 +00:00
|
|
|
if (gsk_transform_has_class (next, &GSK_ROTATE_TRANSFORM_CLASS))
|
|
|
|
{
|
|
|
|
GskTransform *r = gsk_transform_rotate (gsk_transform_ref (next->next),
|
|
|
|
((GskRotateTransform *) next)->angle + angle);
|
|
|
|
gsk_transform_unref (next);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = gsk_transform_alloc (&GSK_ROTATE_TRANSFORM_CLASS,
|
|
|
|
GSK_TRANSFORM_CATEGORY_2D,
|
|
|
|
next);
|
2019-03-02 02:21:09 +00:00
|
|
|
|
2019-12-19 11:29:33 +00:00
|
|
|
result->angle = normalize_angle (angle);
|
2019-03-02 02:21:09 +00:00
|
|
|
|
|
|
|
return &result->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** ROTATE 3D ***/
|
|
|
|
|
|
|
|
typedef struct _GskRotate3dTransform GskRotate3dTransform;
|
|
|
|
|
|
|
|
struct _GskRotate3dTransform
|
|
|
|
{
|
|
|
|
GskTransform parent;
|
|
|
|
|
|
|
|
float angle;
|
|
|
|
graphene_vec3_t axis;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_rotate3d_transform_finalize (GskTransform *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_rotate3d_transform_to_matrix (GskTransform *transform,
|
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
|
|
|
GskRotate3dTransform *self = (GskRotate3dTransform *) transform;
|
|
|
|
|
|
|
|
graphene_matrix_init_rotate (out_matrix, self->angle, &self->axis);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GskTransform *
|
|
|
|
gsk_rotate3d_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
|
|
|
{
|
|
|
|
GskRotate3dTransform *self = (GskRotate3dTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_rotate_3d (apply_to, self->angle, &self->axis);
|
|
|
|
}
|
|
|
|
|
2019-03-03 16:57:11 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_rotate3d_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
GskRotate3dTransform *self = (GskRotate3dTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_rotate_3d (next, - self->angle, &self->axis);
|
|
|
|
}
|
|
|
|
|
2019-03-02 02:21:09 +00:00
|
|
|
static gboolean
|
|
|
|
gsk_rotate3d_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
|
|
|
{
|
|
|
|
GskRotate3dTransform *first = (GskRotate3dTransform *) first_transform;
|
|
|
|
GskRotate3dTransform *second = (GskRotate3dTransform *) second_transform;
|
|
|
|
|
2019-03-22 12:39:28 +00:00
|
|
|
return G_APPROX_VALUE (first->angle, second->angle, 0.01f) &&
|
|
|
|
graphene_vec3_equal (&first->axis, &second->axis);
|
2019-03-02 02:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_rotate3d_transform_print (GskTransform *transform,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
GskRotate3dTransform *self = (GskRotate3dTransform *) transform;
|
|
|
|
float f[3];
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_string_append (string, "rotate3d(");
|
|
|
|
graphene_vec3_to_float (&self->axis, f);
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
string_append_double (string, f[i]);
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
}
|
|
|
|
string_append_double (string, self->angle);
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GskTransformClass GSK_ROTATE3D_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskRotate3dTransform),
|
|
|
|
"GskRotate3dTransform",
|
|
|
|
gsk_rotate3d_transform_finalize,
|
|
|
|
gsk_rotate3d_transform_to_matrix,
|
2019-03-04 18:33:04 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2019-03-02 02:21:09 +00:00
|
|
|
gsk_rotate3d_transform_print,
|
|
|
|
gsk_rotate3d_transform_apply,
|
2019-03-03 16:57:11 +00:00
|
|
|
gsk_rotate3d_transform_invert,
|
2019-03-02 02:21:09 +00:00
|
|
|
gsk_rotate3d_transform_equal,
|
|
|
|
};
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_rotate_3d:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2019-02-17 11:54:04 +00:00
|
|
|
* @angle: the rotation angle, in degrees (clockwise)
|
|
|
|
* @axis: The rotation axis
|
|
|
|
*
|
|
|
|
* Rotates @next @angle degrees around @axis.
|
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* For a rotation in 2D space, use [method@Gsk.Transform.rotate]
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_rotate_3d (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
float angle,
|
|
|
|
const graphene_vec3_t *axis)
|
|
|
|
{
|
2019-03-02 02:21:09 +00:00
|
|
|
GskRotate3dTransform *result;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
2019-03-02 02:21:09 +00:00
|
|
|
if (graphene_vec3_get_x (axis) == 0.0 && graphene_vec3_get_y (axis) == 0.0)
|
|
|
|
return gsk_transform_rotate (next, angle);
|
|
|
|
|
2019-06-03 14:20:12 +00:00
|
|
|
if (angle == 0.0f)
|
2019-07-06 10:56:28 +00:00
|
|
|
return next;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
2019-03-04 08:20:58 +00:00
|
|
|
result = gsk_transform_alloc (&GSK_ROTATE3D_TRANSFORM_CLASS,
|
|
|
|
GSK_TRANSFORM_CATEGORY_3D,
|
|
|
|
next);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-12-19 11:29:33 +00:00
|
|
|
result->angle = normalize_angle (angle);
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_vec3_init_from_vec3 (&result->axis, axis);
|
|
|
|
|
|
|
|
return &result->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** SCALE ***/
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
typedef struct _GskScaleTransform GskScaleTransform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
struct _GskScaleTransform
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform parent;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
float factor_x;
|
|
|
|
float factor_y;
|
|
|
|
float factor_z;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_finalize (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_to_matrix (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskScaleTransform *self = (GskScaleTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
graphene_matrix_init_scale (out_matrix, self->factor_x, self->factor_y, self->factor_z);
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_scale_transform_apply_2d (GskTransform *transform,
|
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
|
|
|
GskScaleTransform *self = (GskScaleTransform *) transform;
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert (self->factor_z == 1.0);
|
2019-03-01 05:24:33 +00:00
|
|
|
|
|
|
|
*out_xx *= self->factor_x;
|
|
|
|
*out_yx *= self->factor_x;
|
|
|
|
*out_xy *= self->factor_y;
|
|
|
|
*out_yy *= self->factor_y;
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_apply_affine (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
float *out_scale_x,
|
|
|
|
float *out_scale_y,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskScaleTransform *self = (GskScaleTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
g_assert (self->factor_z == 1.0);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
*out_scale_x *= self->factor_x;
|
|
|
|
*out_scale_y *= self->factor_y;
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_scale_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskScaleTransform *self = (GskScaleTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
return gsk_transform_scale_3d (apply_to, self->factor_x, self->factor_y, self->factor_z);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 16:57:11 +00:00
|
|
|
static GskTransform *
|
|
|
|
gsk_scale_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
GskScaleTransform *self = (GskScaleTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_scale_3d (next,
|
|
|
|
1.f / self->factor_x,
|
|
|
|
1.f / self->factor_y,
|
|
|
|
1.f / self->factor_z);
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
static gboolean
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskScaleTransform *first = (GskScaleTransform *) first_transform;
|
|
|
|
GskScaleTransform *second = (GskScaleTransform *) second_transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2020-01-04 07:54:58 +00:00
|
|
|
return G_APPROX_VALUE (first->factor_x, second->factor_x, FLT_EPSILON) &&
|
|
|
|
G_APPROX_VALUE (first->factor_y, second->factor_y, FLT_EPSILON) &&
|
|
|
|
G_APPROX_VALUE (first->factor_z, second->factor_z, FLT_EPSILON);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_print (GskTransform *transform,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
GskScaleTransform *self = (GskScaleTransform *) transform;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
if (self->factor_z == 1.0)
|
|
|
|
{
|
|
|
|
g_string_append (string, "scale(");
|
|
|
|
string_append_double (string, self->factor_x);
|
|
|
|
if (self->factor_x != self->factor_y)
|
|
|
|
{
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
string_append_double (string, self->factor_y);
|
|
|
|
}
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_string_append (string, "scale3d(");
|
|
|
|
string_append_double (string, self->factor_x);
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
string_append_double (string, self->factor_y);
|
|
|
|
g_string_append (string, ", ");
|
|
|
|
string_append_double (string, self->factor_z);
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
static const GskTransformClass GSK_SCALE_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskScaleTransform),
|
|
|
|
"GskScaleTransform",
|
|
|
|
gsk_scale_transform_finalize,
|
|
|
|
gsk_scale_transform_to_matrix,
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_scale_transform_apply_2d,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_apply_affine,
|
2019-03-04 18:33:04 +00:00
|
|
|
NULL,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_print,
|
|
|
|
gsk_scale_transform_apply,
|
2019-03-03 16:57:11 +00:00
|
|
|
gsk_scale_transform_invert,
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_scale_transform_equal,
|
2019-02-17 11:54:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_scale:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2019-02-17 11:54:04 +00:00
|
|
|
* @factor_x: scaling factor on the X axis
|
|
|
|
* @factor_y: scaling factor on the Y axis
|
|
|
|
*
|
|
|
|
* Scales @next in 2-dimensional space by the given factors.
|
2021-02-25 12:00:35 +00:00
|
|
|
*
|
|
|
|
* Use [method@Gsk.Transform.scale_3d] to scale in all 3 dimensions.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2019-02-17 11:54:04 +00:00
|
|
|
**/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_scale (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
float factor_x,
|
|
|
|
float factor_y)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
return gsk_transform_scale_3d (next, factor_x, factor_y, 1.0);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_scale_3d:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2019-02-17 11:54:04 +00:00
|
|
|
* @factor_x: scaling factor on the X axis
|
|
|
|
* @factor_y: scaling factor on the Y axis
|
|
|
|
* @factor_z: scaling factor on the Z axis
|
|
|
|
*
|
|
|
|
* Scales @next by the given factors.
|
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2019-02-17 11:54:04 +00:00
|
|
|
**/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_scale_3d (GskTransform *next,
|
2019-02-17 11:54:04 +00:00
|
|
|
float factor_x,
|
|
|
|
float factor_y,
|
|
|
|
float factor_z)
|
|
|
|
{
|
2019-03-04 08:20:58 +00:00
|
|
|
GskScaleTransform *result;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
|
|
|
if (factor_x == 1 && factor_y == 1 && factor_z == 1)
|
2019-07-06 10:56:28 +00:00
|
|
|
return next;
|
2019-06-03 14:20:12 +00:00
|
|
|
|
2019-06-02 12:16:45 +00:00
|
|
|
if (gsk_transform_has_class (next, &GSK_SCALE_TRANSFORM_CLASS))
|
|
|
|
{
|
|
|
|
GskScaleTransform *scale = (GskScaleTransform *) next;
|
|
|
|
GskTransform *r = gsk_transform_scale_3d (gsk_transform_ref (next->next),
|
|
|
|
scale->factor_x * factor_x,
|
|
|
|
scale->factor_y * factor_y,
|
|
|
|
scale->factor_z * factor_z);
|
|
|
|
gsk_transform_unref (next);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-03-04 08:20:58 +00:00
|
|
|
result = gsk_transform_alloc (&GSK_SCALE_TRANSFORM_CLASS,
|
|
|
|
factor_z != 1.0 ? GSK_TRANSFORM_CATEGORY_3D
|
|
|
|
: GSK_TRANSFORM_CATEGORY_2D_AFFINE,
|
|
|
|
next);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
result->factor_x = factor_x;
|
|
|
|
result->factor_y = factor_y;
|
|
|
|
result->factor_z = factor_z;
|
|
|
|
|
|
|
|
return &result->parent;
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:27:30 +00:00
|
|
|
/*** PERSPECTIVE ***/
|
|
|
|
|
|
|
|
typedef struct _GskPerspectiveTransform GskPerspectiveTransform;
|
|
|
|
|
|
|
|
struct _GskPerspectiveTransform
|
|
|
|
{
|
|
|
|
GskTransform parent;
|
|
|
|
|
|
|
|
float depth;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_perspective_transform_finalize (GskTransform *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_perspective_transform_to_matrix (GskTransform *transform,
|
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
|
|
|
GskPerspectiveTransform *self = (GskPerspectiveTransform *) transform;
|
|
|
|
float f[16] = { 1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f, 0.f,
|
2019-06-02 13:06:50 +00:00
|
|
|
0.f, 0.f, 1.f, self->depth ? -1.f / self->depth : 0.f,
|
2019-03-05 08:27:30 +00:00
|
|
|
0.f, 0.f, 0.f, 1.f };
|
|
|
|
|
|
|
|
graphene_matrix_init_from_float (out_matrix, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GskTransform *
|
|
|
|
gsk_perspective_transform_apply (GskTransform *transform,
|
|
|
|
GskTransform *apply_to)
|
|
|
|
{
|
|
|
|
GskPerspectiveTransform *self = (GskPerspectiveTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_perspective (apply_to, self->depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GskTransform *
|
|
|
|
gsk_perspective_transform_invert (GskTransform *transform,
|
|
|
|
GskTransform *next)
|
|
|
|
{
|
|
|
|
GskPerspectiveTransform *self = (GskPerspectiveTransform *) transform;
|
|
|
|
|
|
|
|
return gsk_transform_perspective (next, - self->depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_perspective_transform_equal (GskTransform *first_transform,
|
|
|
|
GskTransform *second_transform)
|
|
|
|
{
|
|
|
|
GskPerspectiveTransform *first = (GskPerspectiveTransform *) first_transform;
|
|
|
|
GskPerspectiveTransform *second = (GskPerspectiveTransform *) second_transform;
|
|
|
|
|
2019-03-22 12:39:28 +00:00
|
|
|
return G_APPROX_VALUE (first->depth, second->depth, 0.001f);
|
2019-03-05 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_perspective_transform_print (GskTransform *transform,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
GskPerspectiveTransform *self = (GskPerspectiveTransform *) transform;
|
|
|
|
|
|
|
|
g_string_append (string, "perspective(");
|
|
|
|
string_append_double (string, self->depth);
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GskTransformClass GSK_PERSPECTIVE_TRANSFORM_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskPerspectiveTransform),
|
|
|
|
"GskPerspectiveTransform",
|
|
|
|
gsk_perspective_transform_finalize,
|
|
|
|
gsk_perspective_transform_to_matrix,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
gsk_perspective_transform_print,
|
|
|
|
gsk_perspective_transform_apply,
|
|
|
|
gsk_perspective_transform_invert,
|
|
|
|
gsk_perspective_transform_equal,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_transform_perspective:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): the next transform
|
2019-03-05 08:27:30 +00:00
|
|
|
* @depth: distance of the z=0 plane. Lower values give a more
|
2021-05-18 21:05:26 +00:00
|
|
|
* flattened pyramid and therefore a more pronounced
|
|
|
|
* perspective effect.
|
2019-03-05 08:27:30 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Applies a perspective projection transform.
|
|
|
|
*
|
|
|
|
* This transform scales points in X and Y based on their Z value,
|
|
|
|
* scaling points with positive Z values away from the origin, and
|
2019-03-05 08:27:30 +00:00
|
|
|
* those with negative Z values towards the origin. Points
|
|
|
|
* on the z=0 plane are unchanged.
|
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-03-05 08:27:30 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_perspective (GskTransform *next,
|
|
|
|
float depth)
|
|
|
|
{
|
|
|
|
GskPerspectiveTransform *result;
|
2019-07-06 10:56:28 +00:00
|
|
|
|
2019-06-02 12:16:45 +00:00
|
|
|
if (gsk_transform_has_class (next, &GSK_PERSPECTIVE_TRANSFORM_CLASS))
|
|
|
|
{
|
|
|
|
GskTransform *r = gsk_transform_perspective (gsk_transform_ref (next->next),
|
|
|
|
((GskPerspectiveTransform *) next)->depth + depth);
|
|
|
|
gsk_transform_unref (next);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:27:30 +00:00
|
|
|
result = gsk_transform_alloc (&GSK_PERSPECTIVE_TRANSFORM_CLASS,
|
|
|
|
GSK_TRANSFORM_CATEGORY_ANY,
|
|
|
|
next);
|
|
|
|
|
|
|
|
result->depth = depth;
|
|
|
|
|
|
|
|
return &result->parent;
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/*** PUBLIC API ***/
|
|
|
|
|
|
|
|
static void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_finalize (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
self->transform_class->finalize (self);
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_unref (self->next);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_ref:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @self: (nullable): a `GskTransform`
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Acquires a reference on the given `GskTransform`.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Returns: (transfer none): the `GskTransform` with an additional reference
|
2019-02-17 11:54:04 +00:00
|
|
|
*/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_ref (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-03-22 11:45:57 +00:00
|
|
|
return g_atomic_rc_box_acquire (self);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_unref:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @self: (nullable): a `GskTransform`
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Releases a reference on the given `GskTransform`.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
|
|
|
* If the reference was the last, the resources associated to the @self are
|
|
|
|
* freed.
|
|
|
|
*/
|
|
|
|
void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_unref (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
if (self == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-03-22 11:45:57 +00:00
|
|
|
g_atomic_rc_box_release_full (self, (GDestroyNotify) gsk_transform_finalize);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_print:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @self: (nullable): a `GskTransform`
|
2019-02-17 11:54:04 +00:00
|
|
|
* @string: The string to print into
|
|
|
|
*
|
2019-03-21 04:52:41 +00:00
|
|
|
* Converts @self into a human-readable string representation suitable
|
2021-02-25 12:00:35 +00:00
|
|
|
* for printing.
|
|
|
|
*
|
|
|
|
* The result of this function can later be parsed with
|
|
|
|
* [func@Gsk.Transform.parse].
|
|
|
|
*/
|
2019-02-17 11:54:04 +00:00
|
|
|
void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_print (GskTransform *self,
|
2019-02-17 11:54:04 +00:00
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
g_return_if_fail (string != NULL);
|
|
|
|
|
|
|
|
if (self == NULL)
|
|
|
|
{
|
|
|
|
g_string_append (string, "none");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->next != NULL)
|
|
|
|
{
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_print (self->next, string);
|
2019-02-17 11:54:04 +00:00
|
|
|
g_string_append (string, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
self->transform_class->print (self, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_to_string:
|
2021-05-20 03:40:54 +00:00
|
|
|
* @self: (nullable): a `GskTransform`
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Converts a matrix into a string that is suitable for printing.
|
|
|
|
*
|
|
|
|
* The resulting string can be parsed with [func@Gsk.Transform.parse].
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* This is a wrapper around [method@Gsk.Transform.print].
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
|
|
|
* Returns: A new string for @self
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-17 11:54:04 +00:00
|
|
|
char *
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_to_string (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
GString *string;
|
|
|
|
|
|
|
|
string = g_string_new ("");
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_print (self, string);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
|
|
|
return g_string_free (string, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_to_matrix:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @self: (nullable): a `GskTransform`
|
2019-02-24 04:21:32 +00:00
|
|
|
* @out_matrix: (out caller-allocates): The matrix to set
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
|
|
|
* Computes the actual value of @self and stores it in @out_matrix.
|
2021-02-25 12:00:35 +00:00
|
|
|
*
|
2019-02-17 11:54:04 +00:00
|
|
|
* The previous value of @out_matrix will be ignored.
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-17 11:54:04 +00:00
|
|
|
void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_to_matrix (GskTransform *self,
|
2019-02-17 11:54:04 +00:00
|
|
|
graphene_matrix_t *out_matrix)
|
|
|
|
{
|
|
|
|
graphene_matrix_t m;
|
|
|
|
|
|
|
|
if (self == NULL)
|
|
|
|
{
|
|
|
|
graphene_matrix_init_identity (out_matrix);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_to_matrix (self->next, out_matrix);
|
2019-02-17 11:54:04 +00:00
|
|
|
self->transform_class->to_matrix (self, &m);
|
|
|
|
graphene_matrix_multiply (&m, out_matrix, out_matrix);
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:24:33 +00:00
|
|
|
/**
|
|
|
|
* gsk_transform_to_2d:
|
2021-05-20 03:40:54 +00:00
|
|
|
* @self: a 2D `GskTransform`
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_xx: (out): return location for the xx member
|
|
|
|
* @out_yx: (out): return location for the yx member
|
|
|
|
* @out_xy: (out): return location for the xy member
|
|
|
|
* @out_yy: (out): return location for the yy member
|
|
|
|
* @out_dx: (out): return location for the x0 member
|
|
|
|
* @out_dy: (out): return location for the y0 member
|
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Converts a `GskTransform` to a 2D transformation matrix.
|
|
|
|
*
|
2019-03-04 18:33:04 +00:00
|
|
|
* @self must be a 2D transformation. If you are not
|
2021-02-25 12:00:35 +00:00
|
|
|
* sure, use gsk_transform_get_category() >=
|
2019-03-04 18:33:04 +00:00
|
|
|
* %GSK_TRANSFORM_CATEGORY_2D to check.
|
2019-03-01 05:24:33 +00:00
|
|
|
*
|
|
|
|
* The returned values have the following layout:
|
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* ```
|
2019-03-01 05:24:33 +00:00
|
|
|
* | xx yx | | a b 0 |
|
|
|
|
* | xy yy | = | c d 0 |
|
2019-06-03 16:15:16 +00:00
|
|
|
* | dx dy | | tx ty 1 |
|
2021-02-25 12:00:35 +00:00
|
|
|
* ```
|
2019-03-01 05:24:33 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* This function can be used to convert between a `GskTransform`
|
2019-03-01 05:24:33 +00:00
|
|
|
* and a matrix type from other 2D drawing libraries, in particular
|
|
|
|
* Cairo.
|
|
|
|
*/
|
2019-03-04 18:33:04 +00:00
|
|
|
void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_transform_to_2d (GskTransform *self,
|
|
|
|
float *out_xx,
|
|
|
|
float *out_yx,
|
|
|
|
float *out_xy,
|
|
|
|
float *out_yy,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2021-07-24 14:51:08 +00:00
|
|
|
*out_xx = 1.0f;
|
|
|
|
*out_yx = 0.0f;
|
|
|
|
*out_xy = 0.0f;
|
|
|
|
*out_yy = 1.0f;
|
|
|
|
*out_dx = 0.0f;
|
|
|
|
*out_dy = 0.0f;
|
|
|
|
|
|
|
|
if (self == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (self->category < GSK_TRANSFORM_CATEGORY_2D))
|
2019-03-01 05:24:33 +00:00
|
|
|
{
|
2021-07-24 14:51:08 +00:00
|
|
|
char *s = gsk_transform_to_string (self);
|
|
|
|
g_warning ("Given transform \"%s\" is not a 2D transform.", s);
|
|
|
|
g_free (s);
|
2019-03-04 18:33:04 +00:00
|
|
|
return;
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
gsk_transform_to_2d (self->next,
|
|
|
|
out_xx, out_yx,
|
|
|
|
out_xy, out_yy,
|
|
|
|
out_dx, out_dy);
|
2019-03-01 05:24:33 +00:00
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
self->transform_class->apply_2d (self,
|
|
|
|
out_xx, out_yx,
|
|
|
|
out_xy, out_yy,
|
|
|
|
out_dx, out_dy);
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_transform_to_affine:
|
2021-02-25 12:00:35 +00:00
|
|
|
* @self: a `GskTransform`
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_scale_x: (out): return location for the scale
|
2021-05-18 21:05:26 +00:00
|
|
|
* factor in the x direction
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_scale_y: (out): return location for the scale
|
2021-05-18 21:05:26 +00:00
|
|
|
* factor in the y direction
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_dx: (out): return location for the translation
|
2021-05-18 21:05:26 +00:00
|
|
|
* in the x direction
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_dy: (out): return location for the translation
|
2021-05-18 21:05:26 +00:00
|
|
|
* in the y direction
|
2019-03-01 05:24:33 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Converts a `GskTransform` to 2D affine transformation factors.
|
|
|
|
*
|
2019-03-04 18:33:04 +00:00
|
|
|
* @self must be a 2D transformation. If you are not
|
2021-05-18 21:05:26 +00:00
|
|
|
* sure, use
|
|
|
|
*
|
|
|
|
* gsk_transform_get_category() >= %GSK_TRANSFORM_CATEGORY_2D_AFFINE
|
|
|
|
*
|
|
|
|
* to check.
|
2019-03-01 05:24:33 +00:00
|
|
|
*/
|
2019-03-04 18:33:04 +00:00
|
|
|
void
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_to_affine (GskTransform *self,
|
2019-02-17 11:54:04 +00:00
|
|
|
float *out_scale_x,
|
|
|
|
float *out_scale_y,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2021-07-24 14:51:08 +00:00
|
|
|
*out_scale_x = 1.0f;
|
|
|
|
*out_scale_y = 1.0f;
|
|
|
|
*out_dx = 0.0f;
|
|
|
|
*out_dy = 0.0f;
|
|
|
|
|
2020-07-24 16:33:33 +00:00
|
|
|
if (self == NULL)
|
2021-07-24 14:51:08 +00:00
|
|
|
return;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2020-07-24 16:33:33 +00:00
|
|
|
if (G_UNLIKELY (self->category < GSK_TRANSFORM_CATEGORY_2D_AFFINE))
|
|
|
|
{
|
|
|
|
char *s = gsk_transform_to_string (self);
|
|
|
|
g_warning ("Given transform \"%s\" is not an affine 2D transform.", s);
|
|
|
|
g_free (s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-24 14:51:08 +00:00
|
|
|
gsk_transform_to_affine (self->next,
|
|
|
|
out_scale_x, out_scale_y,
|
|
|
|
out_dx, out_dy);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-04 18:33:04 +00:00
|
|
|
self->transform_class->apply_affine (self,
|
|
|
|
out_scale_x, out_scale_y,
|
|
|
|
out_dx, out_dy);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:24:33 +00:00
|
|
|
/**
|
|
|
|
* gsk_transform_to_translate:
|
2021-02-25 12:00:35 +00:00
|
|
|
* @self: a `GskTransform`
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_dx: (out): return location for the translation
|
2021-05-18 21:05:26 +00:00
|
|
|
* in the x direction
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_dy: (out): return location for the translation
|
2021-05-18 21:05:26 +00:00
|
|
|
* in the y direction
|
2019-03-01 05:24:33 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Converts a `GskTransform` to a translation operation.
|
|
|
|
*
|
2019-03-04 18:33:04 +00:00
|
|
|
* @self must be a 2D transformation. If you are not
|
2021-05-18 21:05:26 +00:00
|
|
|
* sure, use
|
|
|
|
*
|
|
|
|
* gsk_transform_get_category() >= %GSK_TRANSFORM_CATEGORY_2D_TRANSLATE
|
|
|
|
*
|
|
|
|
* to check.
|
2019-03-01 05:24:33 +00:00
|
|
|
*/
|
2019-03-04 18:33:04 +00:00
|
|
|
void
|
2019-03-01 05:24:33 +00:00
|
|
|
gsk_transform_to_translate (GskTransform *self,
|
|
|
|
float *out_dx,
|
|
|
|
float *out_dy)
|
|
|
|
{
|
2021-07-24 14:51:08 +00:00
|
|
|
*out_dx = 0.0f;
|
|
|
|
*out_dy = 0.0f;
|
|
|
|
|
2020-07-24 16:33:33 +00:00
|
|
|
if (self == NULL)
|
2021-07-24 14:51:08 +00:00
|
|
|
return;
|
2019-03-01 05:24:33 +00:00
|
|
|
|
2020-07-24 16:33:33 +00:00
|
|
|
if (G_UNLIKELY (self->category < GSK_TRANSFORM_CATEGORY_2D_TRANSLATE))
|
|
|
|
{
|
|
|
|
char *s = gsk_transform_to_string (self);
|
|
|
|
g_warning ("Given transform \"%s\" is not an affine 2D translation.", s);
|
|
|
|
g_free (s);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-24 14:51:08 +00:00
|
|
|
gsk_transform_to_translate (self->next, out_dx, out_dy);
|
2019-03-01 05:24:33 +00:00
|
|
|
|
2021-07-24 14:51:08 +00:00
|
|
|
self->transform_class->apply_translate (self, out_dx, out_dy);
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_transform:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @next: (nullable) (transfer full): Transform to apply @other to
|
|
|
|
* @other: (nullable): Transform to apply
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Applies all the operations from @other to @next.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: The new transform
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_transform (GskTransform *next,
|
|
|
|
GskTransform *other)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
if (other == NULL)
|
|
|
|
return next;
|
|
|
|
|
2020-06-06 07:05:49 +00:00
|
|
|
if (next == NULL)
|
|
|
|
return gsk_transform_ref (other);
|
|
|
|
|
2020-02-19 23:02:19 +00:00
|
|
|
if (gsk_transform_is_identity (next))
|
|
|
|
{
|
2020-02-21 17:30:13 +00:00
|
|
|
/* ref before unref to avoid catastrophe when other == next */
|
|
|
|
other = gsk_transform_ref (other);
|
2020-02-19 23:02:19 +00:00
|
|
|
gsk_transform_unref (next);
|
2020-02-21 17:30:13 +00:00
|
|
|
return other;
|
2020-02-19 23:02:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:52:49 +00:00
|
|
|
next = gsk_transform_transform (next, other->next);
|
2019-02-17 11:54:04 +00:00
|
|
|
return other->transform_class->apply (other, next);
|
|
|
|
}
|
|
|
|
|
2019-03-03 16:57:11 +00:00
|
|
|
/**
|
|
|
|
* gsk_transform_invert:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @self: (nullable) (transfer full): Transform to invert
|
2019-03-03 16:57:11 +00:00
|
|
|
*
|
|
|
|
* Inverts the given transform.
|
|
|
|
*
|
|
|
|
* If @self is not invertible, %NULL is returned.
|
|
|
|
* Note that inverting %NULL also returns %NULL, which is
|
|
|
|
* the correct inverse of %NULL. If you need to differentiate
|
|
|
|
* between those cases, you should check @self is not %NULL
|
|
|
|
* before calling this function.
|
|
|
|
*
|
2021-05-18 21:05:26 +00:00
|
|
|
* Returns: (nullable): The inverted transform
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-03-03 16:57:11 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_invert (GskTransform *self)
|
|
|
|
{
|
|
|
|
GskTransform *result = NULL;
|
|
|
|
GskTransform *cur;
|
|
|
|
|
|
|
|
for (cur = self; cur; cur = cur->next)
|
|
|
|
{
|
|
|
|
result = cur->transform_class->invert (cur, result);
|
|
|
|
if (result == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsk_transform_unref (self);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-17 11:54:04 +00:00
|
|
|
/**
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_equal:
|
2020-12-20 20:22:15 +00:00
|
|
|
* @first: (nullable): the first transform
|
|
|
|
* @second: (nullable): the second transform
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-07 07:37:20 +00:00
|
|
|
* Checks two transforms for equality.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-05-18 21:05:26 +00:00
|
|
|
* Returns: %TRUE if the two transforms perform the same operation
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-17 11:54:04 +00:00
|
|
|
gboolean
|
2019-02-28 08:52:49 +00:00
|
|
|
gsk_transform_equal (GskTransform *first,
|
|
|
|
GskTransform *second)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
if (first == second)
|
|
|
|
return TRUE;
|
|
|
|
|
2020-02-21 17:25:05 +00:00
|
|
|
if (first == NULL)
|
|
|
|
return gsk_transform_is_identity (second);
|
|
|
|
|
|
|
|
if (second == NULL)
|
|
|
|
return gsk_transform_is_identity (first);
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-12-31 13:03:42 +00:00
|
|
|
if (first->transform_class != second->transform_class)
|
2019-02-17 11:54:04 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2019-12-31 13:03:42 +00:00
|
|
|
if (!gsk_transform_equal (first->next, second->next))
|
2019-02-17 11:54:04 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return first->transform_class->equal (first, second);
|
|
|
|
}
|
|
|
|
|
2019-03-01 06:51:23 +00:00
|
|
|
/**
|
|
|
|
* gsk_transform_get_category:
|
2021-05-20 03:40:54 +00:00
|
|
|
* @self: (nullable): A `GskTransform`
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2019-03-01 06:51:23 +00:00
|
|
|
* Returns the category this transform belongs to.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2019-03-01 06:51:23 +00:00
|
|
|
* Returns: The category of the transform
|
2019-02-17 11:54:04 +00:00
|
|
|
**/
|
2019-03-01 06:51:23 +00:00
|
|
|
GskTransformCategory
|
2021-02-11 00:04:20 +00:00
|
|
|
(gsk_transform_get_category) (GskTransform *self)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
|
|
|
if (self == NULL)
|
2019-03-01 06:51:23 +00:00
|
|
|
return GSK_TRANSFORM_CATEGORY_IDENTITY;
|
2019-02-17 11:54:04 +00:00
|
|
|
|
2019-03-04 08:20:58 +00:00
|
|
|
return self->category;
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-28 08:52:49 +00:00
|
|
|
* gsk_transform_new: (constructor):
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Creates a new identity transform.
|
|
|
|
*
|
|
|
|
* This function is meant to be used by language
|
2020-06-13 06:25:51 +00:00
|
|
|
* bindings. For C code, this is equivalent to using %NULL.
|
2019-02-17 11:54:04 +00:00
|
|
|
*
|
2020-06-13 06:25:51 +00:00
|
|
|
* Returns: A new identity transform
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-02-28 08:52:49 +00:00
|
|
|
GskTransform *
|
|
|
|
gsk_transform_new (void)
|
2019-02-17 11:54:04 +00:00
|
|
|
{
|
2019-03-04 08:20:58 +00:00
|
|
|
return gsk_transform_alloc (&GSK_IDENTITY_TRANSFORM_CLASS, GSK_TRANSFORM_CATEGORY_IDENTITY, NULL);
|
2019-02-17 11:54:04 +00:00
|
|
|
}
|
2019-03-01 05:24:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_transform_transform_bounds:
|
2021-02-25 12:00:35 +00:00
|
|
|
* @self: a `GskTransform`
|
|
|
|
* @rect: a `graphene_rect_t`
|
2019-03-01 05:24:33 +00:00
|
|
|
* @out_rect: (out caller-allocates): return location for the bounds
|
|
|
|
* of the transformed rectangle
|
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Transforms a `graphene_rect_t` using the given transform @self.
|
|
|
|
*
|
2019-08-21 08:56:20 +00:00
|
|
|
* The result is the bounding box containing the coplanar quad.
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-03-01 05:24:33 +00:00
|
|
|
void
|
|
|
|
gsk_transform_transform_bounds (GskTransform *self,
|
|
|
|
const graphene_rect_t *rect,
|
|
|
|
graphene_rect_t *out_rect)
|
|
|
|
{
|
2019-03-04 20:43:44 +00:00
|
|
|
switch (gsk_transform_get_category (self))
|
|
|
|
{
|
|
|
|
case GSK_TRANSFORM_CATEGORY_IDENTITY:
|
|
|
|
graphene_rect_init_from_rect (out_rect, rect);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_TRANSLATE:
|
|
|
|
{
|
|
|
|
float dx, dy;
|
|
|
|
|
|
|
|
gsk_transform_to_translate (self, &dx, &dy);
|
2020-09-28 15:23:51 +00:00
|
|
|
graphene_rect_init (out_rect,
|
|
|
|
rect->origin.x + dx,
|
|
|
|
rect->origin.y + dy,
|
|
|
|
rect->size.width,
|
|
|
|
rect->size.height);
|
2019-03-04 20:43:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-06-29 07:39:20 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_AFFINE:
|
|
|
|
{
|
|
|
|
float dx, dy, scale_x, scale_y;
|
|
|
|
|
|
|
|
gsk_transform_to_affine (self, &scale_x, &scale_y, &dx, &dy);
|
|
|
|
|
2020-09-28 15:23:51 +00:00
|
|
|
graphene_rect_init (out_rect,
|
|
|
|
(rect->origin.x * scale_x) + dx,
|
|
|
|
(rect->origin.y * scale_y) + dy,
|
|
|
|
rect->size.width * scale_x,
|
|
|
|
rect->size.height * scale_y);
|
2019-06-29 07:39:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-03-04 20:43:44 +00:00
|
|
|
case GSK_TRANSFORM_CATEGORY_UNKNOWN:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_ANY:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_3D:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
graphene_matrix_t mat;
|
2019-03-01 05:24:33 +00:00
|
|
|
|
2019-03-04 20:43:44 +00:00
|
|
|
gsk_transform_to_matrix (self, &mat);
|
2020-09-01 12:54:01 +00:00
|
|
|
gsk_matrix_transform_bounds (&mat, rect, out_rect);
|
2019-03-04 20:43:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-01 05:24:33 +00:00
|
|
|
}
|
2019-03-21 04:52:41 +00:00
|
|
|
|
2020-01-21 08:41:54 +00:00
|
|
|
/**
|
|
|
|
* gsk_transform_transform_point:
|
2021-02-25 12:00:35 +00:00
|
|
|
* @self: a `GskTransform`
|
|
|
|
* @point: a `graphene_point_t`
|
2020-01-21 08:41:54 +00:00
|
|
|
* @out_point: (out caller-allocates): return location for
|
|
|
|
* the transformed point
|
|
|
|
*
|
2021-02-25 12:00:35 +00:00
|
|
|
* Transforms a `graphene_point_t` using the given transform @self.
|
2020-01-21 08:41:54 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_transform_transform_point (GskTransform *self,
|
|
|
|
const graphene_point_t *point,
|
|
|
|
graphene_point_t *out_point)
|
|
|
|
{
|
|
|
|
switch (gsk_transform_get_category (self))
|
|
|
|
{
|
|
|
|
case GSK_TRANSFORM_CATEGORY_IDENTITY:
|
|
|
|
*out_point = *point;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_TRANSLATE:
|
|
|
|
{
|
|
|
|
float dx, dy;
|
|
|
|
|
|
|
|
gsk_transform_to_translate (self, &dx, &dy);
|
|
|
|
out_point->x = point->x + dx;
|
|
|
|
out_point->y = point->y + dy;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D_AFFINE:
|
|
|
|
{
|
|
|
|
float dx, dy, scale_x, scale_y;
|
|
|
|
|
|
|
|
gsk_transform_to_affine (self, &scale_x, &scale_y, &dx, &dy);
|
|
|
|
|
|
|
|
out_point->x = (point->x * scale_x) + dx;
|
|
|
|
out_point->y = (point->y * scale_y) + dy;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSK_TRANSFORM_CATEGORY_UNKNOWN:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_ANY:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_3D:
|
|
|
|
case GSK_TRANSFORM_CATEGORY_2D:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
graphene_matrix_t mat;
|
|
|
|
|
|
|
|
gsk_transform_to_matrix (self, &mat);
|
2020-09-01 12:54:01 +00:00
|
|
|
gsk_matrix_transform_point (&mat, point, out_point);
|
2020-01-21 08:41:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 04:52:41 +00:00
|
|
|
static guint
|
|
|
|
gsk_transform_parse_float (GtkCssParser *parser,
|
|
|
|
guint n,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
float *f = data;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_number (parser, &d))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
f[n] = d;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gsk_transform_parse_scale (GtkCssParser *parser,
|
|
|
|
guint n,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
float *f = data;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_number (parser, &d))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
f[n] = d;
|
|
|
|
f[1] = d;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gsk_transform_parser_parse (GtkCssParser *parser,
|
|
|
|
GskTransform **out_transform)
|
|
|
|
{
|
|
|
|
const GtkCssToken *token;
|
|
|
|
GskTransform *transform = NULL;
|
|
|
|
float f[16] = { 0, };
|
2019-06-03 15:36:31 +00:00
|
|
|
gboolean parsed_something = FALSE;
|
2019-03-21 04:52:41 +00:00
|
|
|
|
|
|
|
token = gtk_css_parser_get_token (parser);
|
|
|
|
if (gtk_css_token_is_ident (token, "none"))
|
|
|
|
{
|
|
|
|
gtk_css_parser_consume_token (parser);
|
|
|
|
*out_transform = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
if (gtk_css_token_is_function (token, "matrix"))
|
|
|
|
{
|
|
|
|
graphene_matrix_t matrix;
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 6, 6, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
graphene_matrix_init_from_2d (&matrix, f[0], f[1], f[2], f[3], f[4], f[5]);
|
|
|
|
transform = gsk_transform_matrix_with_category (transform,
|
|
|
|
&matrix,
|
|
|
|
GSK_TRANSFORM_CATEGORY_2D);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "matrix3d"))
|
|
|
|
{
|
|
|
|
graphene_matrix_t matrix;
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 16, 16, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
graphene_matrix_init_from_float (&matrix, f);
|
|
|
|
transform = gsk_transform_matrix (transform, &matrix);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "perspective"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_perspective (transform, f[0]);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "rotate") ||
|
|
|
|
gtk_css_token_is_function (token, "rotateZ"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_rotate (transform, f[0]);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "rotate3d"))
|
|
|
|
{
|
|
|
|
graphene_vec3_t axis;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 4, 4, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
graphene_vec3_init (&axis, f[0], f[1], f[2]);
|
|
|
|
transform = gsk_transform_rotate_3d (transform, f[3], &axis);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "rotateX"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_rotate_3d (transform, f[0], graphene_vec3_x_axis ());
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "rotateY"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_rotate_3d (transform, f[0], graphene_vec3_y_axis ());
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "scale"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 2, gsk_transform_parse_scale, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_scale (transform, f[0], f[1]);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "scale3d"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 3, 3, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_scale_3d (transform, f[0], f[1], f[2]);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "scaleX"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_scale (transform, f[0], 1.f);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "scaleY"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_scale (transform, 1.f, f[0]);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "scaleZ"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_scale_3d (transform, 1.f, 1.f, f[0]);
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "translate"))
|
|
|
|
{
|
|
|
|
f[1] = 0.f;
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 2, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_translate (transform, &GRAPHENE_POINT_INIT (f[0], f[1]));
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "translate3d"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 3, 3, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_translate_3d (transform, &GRAPHENE_POINT3D_INIT (f[0], f[1], f[2]));
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "translateX"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_translate (transform, &GRAPHENE_POINT_INIT (f[0], 0.f));
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "translateY"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_translate (transform, &GRAPHENE_POINT_INIT (0.f, f[0]));
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "translateZ"))
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
transform = gsk_transform_translate_3d (transform, &GRAPHENE_POINT3D_INIT (0.f, 0.f, f[0]));
|
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "skew"))
|
|
|
|
{
|
2019-06-29 05:12:10 +00:00
|
|
|
graphene_matrix_t matrix;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 2, 2, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
f[0] = f[0] / 180.0 * G_PI;
|
|
|
|
f[1] = f[1] / 180.0 * G_PI;
|
|
|
|
|
|
|
|
graphene_matrix_init_skew (&matrix, f[0], f[1]);
|
|
|
|
transform = gsk_transform_matrix (transform, &matrix);
|
2019-03-21 04:52:41 +00:00
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "skewX"))
|
|
|
|
{
|
2019-06-29 05:12:10 +00:00
|
|
|
graphene_matrix_t matrix;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
f[0] = f[0] / 180.0 * G_PI;
|
|
|
|
|
|
|
|
graphene_matrix_init_skew (&matrix, f[0], 0);
|
|
|
|
transform = gsk_transform_matrix (transform, &matrix);
|
2019-03-21 04:52:41 +00:00
|
|
|
}
|
|
|
|
else if (gtk_css_token_is_function (token, "skewY"))
|
|
|
|
{
|
2019-06-29 05:12:10 +00:00
|
|
|
graphene_matrix_t matrix;
|
|
|
|
|
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gsk_transform_parse_float, f))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
f[0] = f[0] / 180.0 * G_PI;
|
|
|
|
|
|
|
|
graphene_matrix_init_skew (&matrix, 0, f[0]);
|
|
|
|
transform = gsk_transform_matrix (transform, &matrix);
|
2019-03-21 04:52:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:36:31 +00:00
|
|
|
parsed_something = TRUE;
|
2019-03-21 04:52:41 +00:00
|
|
|
token = gtk_css_parser_get_token (parser);
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:36:31 +00:00
|
|
|
if (!parsed_something)
|
2019-03-21 04:52:41 +00:00
|
|
|
{
|
|
|
|
gtk_css_parser_error_syntax (parser, "Expected a transform");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_transform = transform;
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
gsk_transform_unref (transform);
|
|
|
|
*out_transform = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_transform_parse:
|
|
|
|
* @string: the string to parse
|
|
|
|
* @out_transform: (out): The location to put the transform in
|
|
|
|
*
|
|
|
|
* Parses the given @string into a transform and puts it in
|
2021-02-20 06:09:55 +00:00
|
|
|
* @out_transform.
|
|
|
|
*
|
|
|
|
* Strings printed via [method@Gsk.Transform.to_string]
|
2019-03-21 04:52:41 +00:00
|
|
|
* can be read in again successfully using this function.
|
|
|
|
*
|
|
|
|
* If @string does not describe a valid transform, %FALSE is
|
|
|
|
* returned and %NULL is put in @out_transform.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @string described a valid transform.
|
2021-02-25 12:00:35 +00:00
|
|
|
*/
|
2019-03-21 04:52:41 +00:00
|
|
|
gboolean
|
|
|
|
gsk_transform_parse (const char *string,
|
|
|
|
GskTransform **out_transform)
|
|
|
|
{
|
|
|
|
GtkCssParser *parser;
|
|
|
|
GBytes *bytes;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (string != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (out_transform != NULL, FALSE);
|
|
|
|
|
|
|
|
bytes = g_bytes_new_static (string, strlen (string));
|
|
|
|
parser = gtk_css_parser_new_for_bytes (bytes, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
result = gsk_transform_parser_parse (parser, out_transform);
|
|
|
|
|
|
|
|
if (result && !gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_EOF))
|
|
|
|
{
|
|
|
|
g_clear_pointer (out_transform, gsk_transform_unref);
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
gtk_css_parser_unref (parser);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-01 12:36:45 +00:00
|
|
|
|
|
|
|
/* Some of the graphene_matrix_transform apis yield unexpected
|
|
|
|
* results with projective matrices, since they silently drop
|
|
|
|
* the w component, so we provide working alternatives here.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gsk_matrix_transform_point (const graphene_matrix_t *m,
|
|
|
|
const graphene_point_t *p,
|
|
|
|
graphene_point_t *res)
|
|
|
|
{
|
|
|
|
graphene_vec4_t vec4;
|
|
|
|
float w;
|
|
|
|
|
|
|
|
graphene_vec4_init (&vec4, p->x, p->y, 0.0f, 1.0f);
|
|
|
|
graphene_matrix_transform_vec4 (m, &vec4, &vec4);
|
|
|
|
|
|
|
|
w = graphene_vec4_get_w (&vec4);
|
|
|
|
res->x = graphene_vec4_get_x (&vec4) / w;
|
|
|
|
res->y = graphene_vec4_get_y (&vec4) / w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_matrix_transform_point3d (const graphene_matrix_t *m,
|
|
|
|
const graphene_point3d_t *p,
|
|
|
|
graphene_point3d_t *res)
|
|
|
|
{
|
|
|
|
graphene_vec4_t vec4;
|
|
|
|
float w;
|
|
|
|
|
|
|
|
graphene_vec4_init (&vec4, p->x, p->y, 0.0f, 1.0f);
|
|
|
|
graphene_matrix_transform_vec4 (m, &vec4, &vec4);
|
|
|
|
|
|
|
|
w = graphene_vec4_get_w (&vec4);
|
|
|
|
res->x = graphene_vec4_get_x (&vec4) / w;
|
|
|
|
res->y = graphene_vec4_get_y (&vec4) / w;
|
|
|
|
res->z = graphene_vec4_get_z (&vec4) / w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-04 16:51:11 +00:00
|
|
|
gsk_matrix_transform_rect (const graphene_matrix_t *m,
|
|
|
|
const graphene_rect_t *r,
|
|
|
|
graphene_quad_t *res)
|
2020-09-01 12:36:45 +00:00
|
|
|
{
|
|
|
|
graphene_point_t ret[4];
|
|
|
|
graphene_rect_t rr;
|
|
|
|
|
|
|
|
graphene_rect_normalize_r (r, &rr);
|
|
|
|
|
|
|
|
#define TRANSFORM_POINT(matrix, rect, corner, out_p) do {\
|
|
|
|
graphene_vec4_t __s; \
|
|
|
|
graphene_point_t __p; \
|
|
|
|
float w; \
|
|
|
|
graphene_rect_get_ ## corner (rect, &__p); \
|
|
|
|
graphene_vec4_init (&__s, __p.x, __p.y, 0.f, 1.f); \
|
|
|
|
graphene_matrix_transform_vec4 (matrix, &__s, &__s); \
|
|
|
|
w = graphene_vec4_get_w (&__s); \
|
|
|
|
out_p.x = graphene_vec4_get_x (&__s) / w; \
|
|
|
|
out_p.y = graphene_vec4_get_y (&__s) / w; } while (0)
|
|
|
|
|
|
|
|
TRANSFORM_POINT (m, &rr, top_left, ret[0]);
|
|
|
|
TRANSFORM_POINT (m, &rr, top_right, ret[1]);
|
|
|
|
TRANSFORM_POINT (m, &rr, bottom_right, ret[2]);
|
|
|
|
TRANSFORM_POINT (m, &rr, bottom_left, ret[3]);
|
|
|
|
|
|
|
|
#undef TRANSFORM_POINT
|
|
|
|
|
2020-09-04 16:51:11 +00:00
|
|
|
graphene_quad_init (res, &ret[0], &ret[1], &ret[2], &ret[3]);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
gsk_matrix_transform_bounds (const graphene_matrix_t *m,
|
|
|
|
const graphene_rect_t *r,
|
|
|
|
graphene_rect_t *res)
|
|
|
|
{
|
|
|
|
graphene_quad_t q;
|
2020-09-01 12:36:45 +00:00
|
|
|
|
2020-09-04 16:51:11 +00:00
|
|
|
gsk_matrix_transform_rect (m, r, &q);
|
|
|
|
graphene_quad_bounds (&q, res);
|
2020-09-01 12:36:45 +00:00
|
|
|
}
|