1/* graphene-size.c: Size
2 *
3 * SPDX-License-Identifier: MIT
4 *
5 * Copyright 2014 Emmanuele Bassi
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26/**
27 * SECTION:graphene-size
28 * @Title: Size
29 * @short_description: Size representation
30 *
31 * #graphene_size_t represents a size composed of a @graphene_size_t.width
32 * by a @graphene_size_t.height.
33 */
34
35#include "graphene-private.h"
36#include "graphene-size.h"
37
38#include <math.h>
39
40/**
41 * graphene_size_alloc: (constructor)
42 *
43 * Allocates a new #graphene_size_t.
44 *
45 * The contents of the returned value are undefined.
46 *
47 * Returns: (transfer full): the newly allocated #graphene_size_t
48 *
49 * Since: 1.0
50 */
51graphene_size_t *
52graphene_size_alloc (void)
53{
54 return calloc (nmemb: 1, size: sizeof (graphene_size_t));
55}
56
57/**
58 * graphene_size_free:
59 * @s: a #graphene_size_t
60 *
61 * Frees the resources allocated by graphene_size_alloc().
62 *
63 * Since: 1.0
64 */
65void
66graphene_size_free (graphene_size_t *s)
67{
68 free (ptr: s);
69}
70
71/**
72 * graphene_size_init:
73 * @s: a #graphene_size_t
74 * @width: the width
75 * @height: the height
76 *
77 * Initializes a #graphene_size_t using the given @width and @height.
78 *
79 * Returns: (transfer none): the initialized #graphene_size_t
80 *
81 * Since: 1.0
82 */
83graphene_size_t *
84graphene_size_init (graphene_size_t *s,
85 float width,
86 float height)
87{
88 s->width = width;
89 s->height = height;
90
91 return s;
92}
93
94/**
95 * graphene_size_init_from_size:
96 * @s: a #graphene_size_t
97 * @src: a #graphene_size_t
98 *
99 * Initializes a #graphene_size_t using the width and height of
100 * the given @src.
101 *
102 * Returns: (transfer none): the initialized #graphene_size_t
103 *
104 * Since: 1.0
105 */
106graphene_size_t *
107graphene_size_init_from_size (graphene_size_t *s,
108 const graphene_size_t *src)
109{
110 *s = *src;
111
112 return s;
113}
114
115static bool
116size_equal (const void *p1,
117 const void *p2)
118{
119 const graphene_size_t *a = p1;
120 const graphene_size_t *b = p2;
121
122 return graphene_approx_val (a: a->width, b: b->width) &&
123 graphene_approx_val (a: a->height, b: b->height);
124}
125
126/**
127 * graphene_size_equal:
128 * @a: a #graphene_size_t
129 * @b: a #graphene_size_t
130 *
131 * Checks whether the two give #graphene_size_t are equal.
132 *
133 * Returns: `true` if the sizes are equal
134 *
135 * Since: 1.0
136 */
137bool
138graphene_size_equal (const graphene_size_t *a,
139 const graphene_size_t *b)
140{
141 return graphene_pointer_equal (p1: a, p2: b, func: size_equal);
142}
143
144/**
145 * graphene_size_scale:
146 * @s: a #graphene_size_t
147 * @factor: the scaling factor
148 * @res: (out caller-allocates): return location for the scaled size
149 *
150 * Scales the components of a #graphene_size_t using the given @factor.
151 *
152 * Since: 1.0
153 */
154void
155graphene_size_scale (const graphene_size_t *s,
156 float factor,
157 graphene_size_t *res)
158{
159 *res = *s;
160
161 res->width *= factor;
162 res->height *= factor;
163}
164
165/**
166 * graphene_size_interpolate:
167 * @a: a #graphene_size_t
168 * @b: a #graphene_size_t
169 * @factor: the linear interpolation factor
170 * @res: (out caller-allocates): return location for the interpolated size
171 *
172 * Linearly interpolates the two given #graphene_size_t using the given
173 * interpolation @factor.
174 *
175 * Since: 1.0
176 */
177void
178graphene_size_interpolate (const graphene_size_t *a,
179 const graphene_size_t *b,
180 double factor,
181 graphene_size_t *res)
182{
183 res->width = graphene_lerp (a: a->width, b: b->width, factor);
184 res->height = graphene_lerp (a: a->height, b: b->height, factor);
185}
186
187static const graphene_size_t _graphene_size_zero;
188
189/**
190 * graphene_size_zero:
191 *
192 * A constant pointer to a zero #graphene_size_t, useful for
193 * equality checks and interpolations.
194 *
195 * Returns: (transfer none): a constant size
196 *
197 * Since: 1.0
198 */
199const graphene_size_t *
200graphene_size_zero (void)
201{
202 return &_graphene_size_zero;
203}
204

source code of gtk/subprojects/graphene/src/graphene-size.c