summaryrefslogtreecommitdiff
path: root/ccss/ccss-position.c
blob: 4beee7440f8018a17cdad995c50c87aab3d16c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* vim: set ts=8 sw=8 noexpandtab: */

/* The `C' CSS Library.
 * Copyright (C) 2008 Robert Staudinger
 *
 * This  library is free  software; you can  redistribute it and/or
 * modify it  under  the terms  of the  GNU Lesser  General  Public
 * License  as published  by the Free  Software  Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed  in the hope that it will be useful,
 * but  WITHOUT ANY WARRANTY; without even  the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License  along  with  this library;  if not,  write to  the Free
 * Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <glib.h>
#include "ccss-position-parser.h"
#include "config.h"

static const struct {
	char const		*name;
	ccss_position_type_t	 type;
	const double		 percentage;
} _position_map[] = {
  { "left",	CCSS_POSITION_LEFT,	0	},
  { "top",	CCSS_POSITION_TOP,	0	},
  { "right",	CCSS_POSITION_RIGHT,	100	},
  { "bottom",	CCSS_POSITION_BOTTOM,	100	},
  { "center",	CCSS_POSITION_CENTER,	50	},

  { "auto",	CCSS_POSITION_AUTO,	-1	},
  { "contain",	CCSS_POSITION_CONTAIN,	-1	},
  { "cover",	CCSS_POSITION_COVER,	-1	}
};

bool
ccss_position_parse (ccss_position_t	 *self,
		     uint32_t		  flags,
		     CRTerm const	**values)
{
	char const *name;

	if (!*values) {
		return false;
	}

	switch ((*values)->type) {
	case TERM_IDENT:
		name = (char const *) cr_string_peek_raw_str ((*values)->content.str);
		for (unsigned int i = 0; i < G_N_ELEMENTS (_position_map); i++) {
			if (_position_map[i].type & flags &&
			    0 == g_strcmp0 (_position_map[i].name, name)) {
				if (_position_map[i].percentage > -1) {
					self->type = CCSS_POSITION_PERCENTAGE;
					self->value = _position_map[i].percentage;
				} else {
					self->type =  _position_map[i].type;
					self->value = -1;
				}
				*values = (*values)->next;
				return true;
			}
		}
		break;
	case TERM_NUMBER:
		if (CCSS_POSITION_PERCENTAGE & flags &&
		    NUM_PERCENTAGE == (*values)->content.num->type) {
			self->type = CCSS_POSITION_PERCENTAGE;
			self->value = (*values)->content.num->val;
			*values = (*values)->next;
			return true;
		} else if (CCSS_POSITION_LENGTH & flags &&
			   NUM_GENERIC == (*values)->content.num->type) {
			self->type = CCSS_POSITION_LENGTH;
			self->value = (*values)->content.num->val;
			*values = (*values)->next;
			return true;
		} else {
			return false;
		}
		break;
	default:
		g_assert_not_reached ();
		/* Need some code here when building w/o assertions. */
		return false;
	}

	return false;
}

double
ccss_position_get_pos (ccss_position_t const	*self,
		       double			 extent,
		       double			 size)
{
	switch (self->type) {
	case CCSS_POSITION_LENGTH:
		return self->value;
	case CCSS_POSITION_PERCENTAGE:
		return self->value / 100. * (extent - size);
	case CCSS_POSITION_CONTAIN:
	case CCSS_POSITION_COVER:
	case CCSS_POSITION_AUTO:
	default:
		g_assert_not_reached ();
		/* Need some code here when building w/o assertions. */
		return 0;
	}

	return 0;
}

double
ccss_position_get_size (ccss_position_t const	*self,
		        double			 extent)
{
	switch (self->type) {
	case CCSS_POSITION_LENGTH:
		return self->value;
	case CCSS_POSITION_PERCENTAGE:
		return self->value / 100. * extent;
	case CCSS_POSITION_CONTAIN:
	case CCSS_POSITION_COVER:
	case CCSS_POSITION_AUTO:
	default:
		g_assert_not_reached ();
		/* Need some code here when building w/o assertions. */
		return 0;
	}

	return 0;
}

static void
contain (double	 extent_x,
	 double	 extent_y,
	 double	 width,
	 double	 height,
	 double	*x,
	 double	*y)
{
	/* Try to use full width. */
	*y = extent_x * height / width;
	if (*y <= extent_y) {
		*x = extent_x;
		return;
	}

	/* Use full height. */
	*x = extent_y * width / height;
	*y = extent_y;
}

static void
cover (double	 extent_x,
       double	 extent_y,
       double	 width,
       double	 height,
       double	*x,
       double	*y)
{
	/* Try to use full width. */
	*y = extent_x * height / width;
	if (*y >= extent_y) {
		*x = extent_x;
		return;
	}

	/* Use full height. */
	*x = extent_y * width / height;
	*y = extent_y;
}

double
ccss_position_get_hsize (ccss_position_t const	*self,
			 double			 extent_x,
			 double			 extent_y,
			 double			 width,
			 double			 height)
{
	double x;
	double y;

	switch (self->type) {
	case CCSS_POSITION_LENGTH:
		return self->value;
	case CCSS_POSITION_PERCENTAGE:
		return self->value * extent_x / 100.;
	case CCSS_POSITION_CONTAIN:
		contain (extent_x, extent_y, width, height, &x, &y);
		return x;
	case CCSS_POSITION_COVER:
		cover (extent_x, extent_y, width, height, &x, &y);
		return x;
	case CCSS_POSITION_AUTO:
		return width;
	default:
		g_assert_not_reached ();
		/* Need some code here when building w/o assertions. */
		return 0;
	}

	return 0;
}

double
ccss_position_get_vsize (ccss_position_t const	*self,
			 double			 extent_x,
			 double			 extent_y,
			 double			 width,
			 double			 height)
{
	double x;
	double y;

	switch (self->type) {
	case CCSS_POSITION_LENGTH:
		return self->value;
	case CCSS_POSITION_PERCENTAGE:
		return self->value * extent_y / 100.;
	case CCSS_POSITION_CONTAIN:
		contain (extent_x, extent_y, width, height, &x, &y);
		return y;
	case CCSS_POSITION_COVER:
		cover (extent_x, extent_y, width, height, &x, &y);
		return y;
	case CCSS_POSITION_AUTO:
		return height;
	default:
		g_assert_not_reached ();
		/* Need some code here when building w/o assertions. */
		return 0;
	}

	return 0;
}


char *
ccss_position_serialize (ccss_position_t const *self)
{
	g_return_val_if_fail (self, NULL);

	switch (self->type) {
	case CCSS_POSITION_LENGTH:
		return g_strdup_printf ("%.3f", self->value);
	case CCSS_POSITION_PERCENTAGE:
		return g_strdup_printf ("%.3fpx", self->value);
	case CCSS_POSITION_LEFT:
		return g_strdup ("left");
	case CCSS_POSITION_TOP:
		return g_strdup ("top");
	case CCSS_POSITION_RIGHT:
		return g_strdup ("right");
	case CCSS_POSITION_BOTTOM:
		return g_strdup ("bottom");
	case CCSS_POSITION_CENTER:
		return g_strdup ("center");
	case CCSS_POSITION_AUTO:
		return g_strdup ("auto");
	case CCSS_POSITION_CONTAIN:
		return g_strdup ("containe");
	case CCSS_POSITION_COVER:
		return g_strdup ("cover");
	case CCSS_POSITION_MASK_NUMERIC:
	case CCSS_POSITION_MASK_HORIZONTAL:
	case CCSS_POSITION_MASK_VERTICAL:
	case CCSS_POSITION_MASK_AUTO:
		/* Fall thru. */
		break;
	}

	return NULL;
}