GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 76.5% 114 / 0 / 149
Functions: 95.0% 19 / 0 / 20
Branches: 47.3% 52 / 0 / 110

shumate/vector/shumate-vector-expression.c
Line Branch Exec Source
1 /*
2 * Copyright (C) 2021 James Westman <james@jwestman.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
16 */
17
18
19 #include "shumate-vector-renderer.h"
20 #include "shumate-vector-expression-private.h"
21 #include "shumate-vector-expression-filter-private.h"
22 #include "shumate-vector-expression-interpolate-private.h"
23 #include "shumate-vector-value-private.h"
24
25
26
4/6
shumate_vector_expression_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 8 times.
shumate_vector_expression_get_type:
✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 7 taken 1980 times.
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 7 not taken.
2004 G_DEFINE_TYPE (ShumateVectorExpression, shumate_vector_expression, G_TYPE_OBJECT)
27
28
29 ShumateVectorExpression *
30 620 shumate_vector_expression_from_json (JsonNode *json,
31 GError **error)
32 {
33
3/4
✓ Branch 2 → 3 taken 462 times.
✓ Branch 2 → 5 taken 158 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 462 times.
620 if (json == NULL || JSON_NODE_HOLDS_NULL (json))
34 158 return shumate_vector_expression_filter_from_literal (&SHUMATE_VECTOR_VALUE_INIT);
35
2/2
✓ Branch 8 → 9 taken 44 times.
✓ Branch 8 → 22 taken 418 times.
462 else if (JSON_NODE_HOLDS_VALUE (json))
36 {
37 44 g_auto(GValue) gvalue = G_VALUE_INIT;
38 44 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
39 44 const char *string;
40
41 44 json_node_get_value (json, &gvalue);
42
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 15 taken 44 times.
44 if (!shumate_vector_value_set_from_g_value (&value, &gvalue))
43 {
44 ✗ g_set_error (error,
45 SHUMATE_STYLE_ERROR,
46 SHUMATE_STYLE_ERROR_INVALID_EXPRESSION,
47 "Unsupported literal value in expression");
48 ✗ return NULL;
49 }
50
51
2/2
✓ Branch 16 → 17 taken 20 times.
✓ Branch 16 → 18 taken 24 times.
44 if (shumate_vector_value_get_string (&value, &string))
52 20 return shumate_vector_expression_filter_from_format (string, error);
53 else
54 24 return shumate_vector_expression_filter_from_literal (&value);
55 }
56
2/2
✓ Branch 23 → 24 taken 6 times.
✓ Branch 23 → 26 taken 412 times.
418 else if (JSON_NODE_HOLDS_OBJECT (json))
57 6 return shumate_vector_expression_interpolate_from_json_obj (json_node_get_object (json), error);
58
1/2
✓ Branch 27 → 28 taken 412 times.
✗ Branch 27 → 53 not taken.
412 else if (JSON_NODE_HOLDS_ARRAY (json))
59 {
60 412 JsonArray *array = json_node_get_array (json);
61
2/2
✓ Branch 30 → 31 taken 396 times.
✓ Branch 30 → 51 taken 16 times.
412 if (json_array_get_length (array) > 1)
62 {
63 396 JsonNode *first = json_array_get_element (array, 0);
64
3/4
✓ Branch 33 → 34 taken 396 times.
✗ Branch 33 → 51 not taken.
✓ Branch 35 → 36 taken 2 times.
✓ Branch 35 → 51 taken 394 times.
396 if (JSON_NODE_HOLDS_VALUE (first) && json_node_get_value_type (first) != G_TYPE_STRING)
65 {
66 /* If it's an array of numbers, return it as a literal */
67 2 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
68 2 shumate_vector_value_start_array (&value);
69
2/2
✓ Branch 47 → 38 taken 10 times.
✓ Branch 47 → 48 taken 2 times.
14 for (guint i = 0; i < json_array_get_length (array); i++)
70 {
71 10 g_auto(ShumateVectorValue) element = SHUMATE_VECTOR_VALUE_INIT;
72
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 43 taken 10 times.
10 if (!shumate_vector_value_set_from_json_literal (&element, json_array_get_element (array, i), error))
73 ✗ return NULL;
74 10 shumate_vector_value_array_append (&value, &element);
75 }
76
77 2 return shumate_vector_expression_filter_from_literal (&value);
78 }
79 }
80
81 410 return shumate_vector_expression_filter_from_json_array (json_node_get_array (json), NULL, error);
82 }
83 else
84 ✗ g_assert_not_reached ();
85 }
86
87
88 static gboolean
89 ✗ shumate_vector_expression_real_eval (ShumateVectorExpression *self,
90 ShumateVectorRenderScope *scope,
91 ShumateVectorValue *out)
92 {
93 ✗ g_assert_not_reached ();
94 }
95
96 /* A default implementation for eval_bitset() that calls the regular eval() method for each
97 feature. The mask parameter allows it to skip features that don't need to be calculated
98 (e.g. they have been excluded by a previous child of an ALL expression). */
99 static ShumateVectorIndexBitset *
100 2 shumate_vector_expression_real_eval_bitset (ShumateVectorExpression *self,
101 ShumateVectorRenderScope *scope,
102 ShumateVectorIndexBitset *mask)
103 {
104 2 VectorTile__Tile__Layer *layer = shumate_vector_reader_iter_get_layer_struct (scope->reader);
105 2 ShumateVectorIndexBitset *result = shumate_vector_index_bitset_new (layer->n_features);
106 2 int feature_idx = 0;
107
108
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 13 taken 2 times.
2 if (mask != NULL)
109 {
110 feature_idx = -1;
111 ✗ while ((feature_idx = shumate_vector_index_bitset_next (mask, feature_idx)) != -1)
112 {
113 ✗ shumate_vector_reader_iter_read_feature (scope->reader, feature_idx);
114 ✗ if (shumate_vector_expression_eval_boolean (self, scope, FALSE))
115 ✗ shumate_vector_index_bitset_set (result, feature_idx);
116 }
117 }
118 else
119 {
120 2 shumate_vector_reader_iter_read_feature (scope->reader, 0);
121 6 while (TRUE)
122 {
123
2/2
✓ Branch 15 → 16 taken 2 times.
✓ Branch 15 → 17 taken 2 times.
4 if (shumate_vector_expression_eval_boolean (self, scope, FALSE))
124 2 shumate_vector_index_bitset_set (result, feature_idx);
125
126
2/2
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 20 taken 2 times.
4 if (!shumate_vector_reader_iter_next_feature (scope->reader))
127 break;
128
129 2 feature_idx ++;
130 }
131 }
132
133 2 return result;
134 }
135
136
137 static void
138 8 shumate_vector_expression_class_init (ShumateVectorExpressionClass *klass)
139 {
140 8 klass->eval = shumate_vector_expression_real_eval;
141 8 klass->eval_bitset = shumate_vector_expression_real_eval_bitset;
142 8 klass->collect_indexes = NULL;
143 }
144
145
146 static void
147 1916 shumate_vector_expression_init (ShumateVectorExpression *self)
148 {
149 1916 }
150
151
152 gboolean
153 2020 shumate_vector_expression_eval (ShumateVectorExpression *self,
154 ShumateVectorRenderScope *scope,
155 ShumateVectorValue *out)
156 {
157
3/4
✓ Branch 2 → 3 taken 1942 times.
✓ Branch 2 → 6 taken 78 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1942 times.
2020 g_assert (self == NULL || SHUMATE_IS_VECTOR_EXPRESSION (self));
158
159
2/2
✓ Branch 6 → 7 taken 1942 times.
✓ Branch 6 → 8 taken 78 times.
2020 if (self == NULL)
160 return FALSE;
161 else
162 1942 return SHUMATE_VECTOR_EXPRESSION_GET_CLASS (self)->eval (self, scope, out);
163 }
164
165
166 /* Evaluates the expression as a boolean for every feature in the scope's current layer and
167 returns the result in a bitset. If the mask parameter is provided, only those features
168 need to be calculated. */
169 ShumateVectorIndexBitset *
170 14 shumate_vector_expression_eval_bitset (ShumateVectorExpression *self,
171 ShumateVectorRenderScope *scope,
172 ShumateVectorIndexBitset *mask)
173 {
174
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 14 times.
14 g_assert (SHUMATE_IS_VECTOR_EXPRESSION (self));
175 14 return SHUMATE_VECTOR_EXPRESSION_GET_CLASS (self)->eval_bitset (self, scope, mask);
176 }
177
178
179 /* Adds any indexes this expression uses to the index description. Indexes can only be used
180 in eval_bitset() implementations, not eval(). */
181 void
182 22 shumate_vector_expression_collect_indexes (ShumateVectorExpression *self,
183 const char *layer_name,
184 ShumateVectorIndexDescription *index_description)
185 {
186
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 22 times.
22 g_assert (SHUMATE_IS_VECTOR_EXPRESSION (self));
187
1/2
✓ Branch 5 → 6 taken 22 times.
✗ Branch 5 → 7 not taken.
22 if (SHUMATE_VECTOR_EXPRESSION_GET_CLASS (self)->collect_indexes != NULL)
188 22 SHUMATE_VECTOR_EXPRESSION_GET_CLASS (self)->collect_indexes (self, layer_name, index_description);
189 22 }
190
191
192 double
193 158 shumate_vector_expression_eval_number (ShumateVectorExpression *self,
194 ShumateVectorRenderScope *scope,
195 double default_val)
196 {
197 158 double result;
198 316 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
199
200 158 shumate_vector_expression_eval (self, scope, &value);
201
202
2/2
✓ Branch 4 → 5 taken 44 times.
✓ Branch 4 → 6 taken 114 times.
158 if (shumate_vector_value_get_number (&value, &result))
203 44 return result;
204 else
205 return default_val;
206 }
207
208
209 gboolean
210 450 shumate_vector_expression_eval_boolean (ShumateVectorExpression *self,
211 ShumateVectorRenderScope *scope,
212 gboolean default_val)
213 {
214 450 gboolean result;
215 900 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
216
217 450 shumate_vector_expression_eval (self, scope, &value);
218
219
2/2
✓ Branch 4 → 5 taken 368 times.
✓ Branch 4 → 6 taken 82 times.
450 if (shumate_vector_value_get_boolean (&value, &result))
220 368 return result;
221 else
222 return default_val;
223 }
224
225
226 char *
227 50 shumate_vector_expression_eval_string (ShumateVectorExpression *self,
228 ShumateVectorRenderScope *scope,
229 const char *default_val)
230 {
231 50 const char *result;
232 100 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
233
234 50 shumate_vector_expression_eval (self, scope, &value);
235
236
2/2
✓ Branch 4 → 5 taken 20 times.
✓ Branch 4 → 13 taken 30 times.
50 if (shumate_vector_value_get_string (&value, &result))
237
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 20 times.
20 return g_strdup (result);
238 else
239
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 30 times.
80 return g_strdup (default_val);
240 }
241
242
243 void
244 42 shumate_vector_expression_eval_color (ShumateVectorExpression *self,
245 ShumateVectorRenderScope *scope,
246 GdkRGBA *color)
247 {
248 84 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
249 42 shumate_vector_expression_eval (self, scope, &value);
250 42 shumate_vector_value_get_color (&value, color);
251 42 }
252
253 ShumateVectorSprite *
254 14 shumate_vector_expression_eval_image (ShumateVectorExpression *self,
255 ShumateVectorRenderScope *scope)
256 {
257 28 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
258 14 shumate_vector_expression_eval (self, scope, &value);
259
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 14 times.
14 if (value.type == SHUMATE_VECTOR_VALUE_TYPE_STRING)
260 {
261 ✗ const char *name;
262 ✗ shumate_vector_value_get_string (&value, &name);
263 ✗ return shumate_vector_sprite_sheet_get_sprite (scope->sprites, name, scope->scale_factor);
264 }
265
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 11 taken 14 times.
14 else if (value.type == SHUMATE_VECTOR_VALUE_TYPE_RESOLVED_IMAGE)
266 {
267 ✗ ShumateVectorSprite *sprite;
268 ✗ shumate_vector_value_get_image (&value, &sprite);
269 ✗ return g_object_ref (sprite);
270 }
271 else
272 return NULL;
273 }
274
275 ShumateVectorAlignment
276 20 shumate_vector_expression_eval_alignment (ShumateVectorExpression *self,
277 ShumateVectorRenderScope *scope)
278 {
279 40 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
280 20 const char *string;
281
282 20 shumate_vector_expression_eval (self, scope, &value);
283
284
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 12 taken 20 times.
20 if (shumate_vector_value_get_string (&value, &string))
285 {
286 ✗ if (g_strcmp0 (string, "map") == 0)
287 return SHUMATE_VECTOR_ALIGNMENT_MAP;
288 ✗ else if (g_strcmp0 (string, "viewport") == 0)
289 return SHUMATE_VECTOR_ALIGNMENT_VIEWPORT;
290 ✗ else if (g_strcmp0 (string, "viewport-glyph") == 0)
291 ✗ return SHUMATE_VECTOR_ALIGNMENT_VIEWPORT_GLYPH;
292 }
293
294 return SHUMATE_VECTOR_ALIGNMENT_AUTO;
295 }
296
297 ShumateVectorPlacement
298 10 shumate_vector_expression_eval_placement (ShumateVectorExpression *self,
299 ShumateVectorRenderScope *scope)
300 {
301 20 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
302 10 const char *string;
303
304 10 shumate_vector_expression_eval (self, scope, &value);
305
306
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 10 taken 10 times.
10 if (shumate_vector_value_get_string (&value, &string))
307 {
308 ✗ if (g_strcmp0 (string, "line") == 0)
309 return SHUMATE_VECTOR_PLACEMENT_LINE;
310 ✗ else if (g_strcmp0 (string, "line-center") == 0)
311 ✗ return SHUMATE_VECTOR_PLACEMENT_LINE_CENTER;
312 }
313
314 return SHUMATE_VECTOR_PLACEMENT_POINT;
315 }
316
317 ShumateVectorAnchor
318 20 shumate_vector_expression_eval_anchor (ShumateVectorExpression *self,
319 ShumateVectorRenderScope *scope)
320 {
321 40 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
322 20 const char *string;
323
324 20 shumate_vector_expression_eval (self, scope, &value);
325
326
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 22 taken 20 times.
20 if (shumate_vector_value_get_string (&value, &string))
327 {
328 ✗ if (g_strcmp0 (string, "top") == 0)
329 return SHUMATE_VECTOR_ANCHOR_TOP;
330 ✗ else if (g_strcmp0 (string, "bottom") == 0)
331 return SHUMATE_VECTOR_ANCHOR_BOTTOM;
332 ✗ else if (g_strcmp0 (string, "left") == 0)
333 return SHUMATE_VECTOR_ANCHOR_LEFT;
334 ✗ else if (g_strcmp0 (string, "right") == 0)
335 return SHUMATE_VECTOR_ANCHOR_RIGHT;
336 ✗ else if (g_strcmp0 (string, "top-left") == 0)
337 return SHUMATE_VECTOR_ANCHOR_TOP_LEFT;
338 ✗ else if (g_strcmp0 (string, "top-right") == 0)
339 return SHUMATE_VECTOR_ANCHOR_TOP_RIGHT;
340 ✗ else if (g_strcmp0 (string, "bottom-left") == 0)
341 return SHUMATE_VECTOR_ANCHOR_BOTTOM_LEFT;
342 ✗ else if (g_strcmp0 (string, "bottom-right") == 0)
343 ✗ return SHUMATE_VECTOR_ANCHOR_BOTTOM_RIGHT;
344 }
345
346 return SHUMATE_VECTOR_ANCHOR_CENTER;
347 }
348
349 ShumateVectorOverlap
350 20 shumate_vector_expression_eval_overlap (ShumateVectorExpression *self,
351 ShumateVectorExpression *allow_overlap,
352 ShumateVectorRenderScope *scope)
353 {
354 40 g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
355 20 const char *string;
356
357 20 shumate_vector_expression_eval (self, scope, &value);
358
359
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 11 taken 20 times.
20 if (shumate_vector_value_get_string (&value, &string))
360 {
361 ✗ if (g_strcmp0 (string, "always") == 0)
362 return SHUMATE_VECTOR_OVERLAP_ALWAYS;
363 ✗ else if (g_strcmp0 (string, "never") == 0)
364 return SHUMATE_VECTOR_OVERLAP_NEVER;
365 ✗ else if (g_strcmp0 (string, "cooperative") == 0)
366 return SHUMATE_VECTOR_OVERLAP_COOPERATIVE;
367 }
368
369
1/2
✓ Branch 12 → 13 taken 20 times.
✗ Branch 12 → 14 not taken.
20 if (shumate_vector_expression_eval_boolean (allow_overlap, scope, FALSE))
370 return SHUMATE_VECTOR_OVERLAP_ALWAYS;
371 else
372 20 return SHUMATE_VECTOR_OVERLAP_NEVER;
373 }
374
375 void
376 6 shumate_vector_expression_context_clear (ShumateVectorExpressionContext *ctx)
377 {
378
1/2
✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 4 not taken.
6 g_clear_pointer (&ctx->variables, g_hash_table_unref);
379 6 }
380
381