GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 510
Functions: 0.0% 0 / 0 / 23
Branches: 0.0% 0 / 0 / 218

shumate/vector/shumate-vector-symbol.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-symbol-private.h"
20 #include "shumate-vector-utils-private.h"
21 #include "shumate-vector-symbol-info-private.h"
22 #include "shumate-vector-symbol-container-private.h"
23 #include "shumate-layer.h"
24 #include "shumate-symbol-event-private.h"
25 #include "../shumate-vector-sprite.h"
26
27 #define RGBA_BLACK ((GdkRGBA){0, 0, 0, 1})
28
29 struct _ShumateVectorSymbol
30 {
31 GtkWidget parent_instance;
32
33 ShumateVectorSymbolInfo *symbol_info;
34
35 GArray *glyphs;
36
37 GskRenderNode *glyphs_node;
38 int layout_width, layout_height, baseline, layout_y;
39
40 graphene_rect_t bounds;
41 double x, y;
42
43 ShumateVectorPoint midpoint;
44 double midpoint_angle;
45 double line_length;
46
47 uint8_t show_text : 1;
48 uint8_t show_icon : 1;
49 };
50
51 ✗ G_DEFINE_TYPE (ShumateVectorSymbol, shumate_vector_symbol, GTK_TYPE_WIDGET)
52
53
54 enum {
55 PROP_0,
56 PROP_SYMBOL_INFO,
57 N_PROPS,
58 };
59
60 static GParamSpec *obj_properties[N_PROPS] = { NULL, };
61
62 enum {
63 CLICKED,
64 LAST_SIGNAL
65 };
66
67 static guint signals[LAST_SIGNAL] = { 0, };
68
69
70 typedef struct {
71 GskRenderNode *node;
72 ShumateVectorSprite *sprite;
73 double width;
74 GdkRGBA icon_color;
75 } Glyph;
76
77 static void
78 ✗ glyph_clear (Glyph *glyph)
79 {
80 ✗ g_clear_pointer (&glyph->node, gsk_render_node_unref);
81 ✗ }
82
83
84 ShumateVectorSymbol *
85 ✗ shumate_vector_symbol_new (ShumateVectorSymbolInfo *symbol_info)
86 {
87 ✗ return g_object_new (SHUMATE_TYPE_VECTOR_SYMBOL,
88 "symbol-info", symbol_info,
89 NULL);
90 }
91
92
93 static PangoAttrShape *
94 ✗ get_shape_from_glyph_item (PangoGlyphItem *item)
95 {
96 ✗ for (GSList *list = item->item->analysis.extra_attrs; list != NULL; list = list->next)
97 ✗ if (((PangoAttribute*)list->data)->klass->type == PANGO_ATTR_SHAPE)
98 return (PangoAttrShape*)list->data;
99 return NULL;
100 }
101
102 static void
103 ✗ get_color_from_glyph_item (PangoGlyphItem *item, GdkRGBA *color)
104 {
105 ✗ *color = RGBA_BLACK;
106
107 ✗ for (GSList *list = item->item->analysis.extra_attrs; list != NULL; list = list->next)
108 {
109 ✗ if (((PangoAttribute*)list->data)->klass->type == PANGO_ATTR_FOREGROUND)
110 {
111 ✗ PangoColor c = ((PangoAttrColor*)list->data)->color;
112 ✗ color->red = c.red / 65535.0;
113 ✗ color->green = c.green / 65535.0;
114 ✗ color->blue = c.blue / 65535.0;
115 }
116 ✗ else if (((PangoAttribute*)list->data)->klass->type == PANGO_ATTR_FOREGROUND_ALPHA)
117 ✗ color->alpha = ((PangoAttrInt*)list->data)->value / 65535.0;
118 }
119 ✗ }
120
121
122 static void
123 ✗ shumate_vector_symbol_constructed (GObject *object)
124 {
125 ✗ ShumateVectorSymbol *self = (ShumateVectorSymbol *)object;
126 ✗ g_autofree char *text = NULL;
127
128 ✗ if (self->symbol_info->details->formatted_text != NULL)
129 {
130 ✗ PangoContext *context = gtk_widget_get_pango_context (GTK_WIDGET (self));
131 ✗ g_autoptr(PangoLayout) layout = pango_layout_new (context);
132 ✗ g_autoptr(PangoAttrList) attrs = pango_attr_list_new ();
133 ✗ g_autoptr(GString) string = g_string_new ("");
134 ✗ PangoAttribute *attr;
135 ✗ gboolean any_images = FALSE;
136 ✗ PangoRectangle ink_rect;
137
138 ✗ if (self->symbol_info->details->text_font != NULL)
139 {
140 ✗ g_autoptr(PangoFontDescription) desc = pango_font_description_from_string (self->symbol_info->details->text_font);
141 ✗ attr = pango_attr_font_desc_new (desc);
142 ✗ pango_attr_list_insert (attrs, attr);
143 }
144
145 ✗ attr = pango_attr_letter_spacing_new (self->symbol_info->details->text_letter_spacing * self->symbol_info->details->text_size * PANGO_SCALE);
146 ✗ pango_attr_list_insert (attrs, attr);
147
148 ✗ attr = pango_attr_foreground_new (self->symbol_info->details->text_color.red * 65535,
149 ✗ self->symbol_info->details->text_color.green * 65535,
150 ✗ self->symbol_info->details->text_color.blue * 65535);
151 ✗ pango_attr_list_insert (attrs, attr);
152
153 ✗ attr = pango_attr_foreground_alpha_new (self->symbol_info->details->text_color.alpha * 65535);
154 ✗ pango_attr_list_insert (attrs, attr);
155
156 ✗ attr = pango_attr_size_new_absolute (self->symbol_info->details->text_size * PANGO_SCALE);
157 ✗ pango_attr_list_insert (attrs, attr);
158
159 ✗ pango_layout_set_attributes (layout, attrs);
160
161 ✗ for (int i = 0, n = self->symbol_info->details->formatted_text->len; i < n; i ++)
162 {
163 ✗ ShumateVectorFormatPart *part = g_ptr_array_index (self->symbol_info->details->formatted_text, i);
164
165 ✗ if (part->sprite != NULL)
166 {
167 ✗ int width = shumate_vector_sprite_get_width (part->sprite);
168 ✗ int height = shumate_vector_sprite_get_height (part->sprite);
169 /* For shapes, since we're overriding the ink and logical rects
170 of the glyph, we have to take letter spacing into account ourselves */
171 ✗ double spacing = self->symbol_info->details->text_letter_spacing * self->symbol_info->details->text_size;
172
173 ✗ PangoRectangle ink_rect = {
174 ✗ .x = spacing / 2 * PANGO_SCALE,
175 ✗ .y = -height * PANGO_SCALE,
176 ✗ .width = width * PANGO_SCALE,
177 .height = height * PANGO_SCALE,
178 };
179 ✗ PangoRectangle logical_rect = {
180 .x = 0,
181 .y = -height * PANGO_SCALE,
182 ✗ .width = (width + spacing) * PANGO_SCALE,
183 .height = height * PANGO_SCALE,
184 };
185
186 ✗ attr = pango_attr_shape_new_with_data (&ink_rect, &logical_rect, g_object_ref (part->sprite), (PangoAttrDataCopyFunc)g_object_ref, g_object_unref);
187 ✗ attr->start_index = string->len;
188 ✗ attr->end_index = string->len + strlen ("\uFFFC");
189 ✗ pango_attr_list_insert (attrs, attr);
190
191 ✗ if (spacing != 0)
192 {
193 ✗ attr = pango_attr_letter_spacing_new (0);
194 ✗ attr->start_index = string->len;
195 ✗ attr->end_index = string->len + strlen ("\uFFFC");
196 ✗ pango_attr_list_insert (attrs, attr);
197 }
198
199 ✗ g_string_append (string, "\uFFFC");
200
201 ✗ any_images = TRUE;
202 }
203 else
204 {
205 ✗ if (part->has_font_scale)
206 {
207 ✗ attr = pango_attr_size_new_absolute (part->font_scale * self->symbol_info->details->text_size * PANGO_SCALE);
208 ✗ attr->start_index = string->len;
209 ✗ attr->end_index = string->len + strlen (part->string);
210 ✗ pango_attr_list_insert (attrs, attr);
211 }
212
213 ✗ if (part->has_text_color)
214 {
215 ✗ attr = pango_attr_foreground_new (part->text_color.red * 65535,
216 ✗ part->text_color.green * 65535,
217 ✗ part->text_color.blue * 65535);
218 ✗ attr->start_index = string->len;
219 ✗ attr->end_index = string->len + strlen (part->string);
220 ✗ pango_attr_list_insert (attrs, attr);
221
222 ✗ attr = pango_attr_foreground_alpha_new (part->text_color.alpha * 65535);
223 ✗ attr->start_index = string->len;
224 ✗ attr->end_index = string->len + strlen (part->string);
225 ✗ pango_attr_list_insert (attrs, attr);
226 }
227
228 ✗ g_string_append (string, part->string);
229 }
230 }
231
232 ✗ text = g_string_free (g_steal_pointer (&string), FALSE);
233 ✗ pango_layout_set_text (layout, text, -1);
234
235 ✗ pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
236 ✗ self->layout_width = ink_rect.width;
237 ✗ self->layout_height = ink_rect.height;
238 ✗ self->layout_y = ink_rect.y;
239 ✗ self->baseline = pango_layout_get_baseline (layout) / (double) PANGO_SCALE;
240
241 ✗ if ((self->symbol_info->details->text_rotation_alignment == SHUMATE_VECTOR_ALIGNMENT_MAP
242 ✗ || self->symbol_info->details->text_rotation_alignment == SHUMATE_VECTOR_ALIGNMENT_VIEWPORT_GLYPH)
243 ✗ && (self->symbol_info->details->symbol_placement == SHUMATE_VECTOR_PLACEMENT_LINE
244 ✗ || self->symbol_info->details->symbol_placement == SHUMATE_VECTOR_PLACEMENT_LINE_CENTER))
245 {
246 ✗ g_autoptr(PangoLayoutIter) iter = NULL;
247 ✗ int i;
248 ✗ PangoGlyphItem *current_item;
249
250 ✗ self->glyphs = g_array_new (FALSE, FALSE, sizeof (Glyph));
251 ✗ g_array_set_clear_func (self->glyphs, (GDestroyNotify)glyph_clear);
252
253 ✗ iter = pango_layout_get_iter (layout);
254
255 ✗ do {
256 ✗ PangoAttrShape *shape = NULL;
257
258 ✗ current_item = pango_layout_iter_get_run (iter);
259
260 ✗ if (current_item == NULL)
261 ✗ continue;
262
263 ✗ shape = get_shape_from_glyph_item (current_item);
264
265 ✗ if (shape != NULL)
266 {
267 ✗ Glyph glyph = {
268 ✗ .sprite = g_object_ref (shape->data),
269 .node = NULL,
270 ✗ .width = shape->logical_rect.width / (double) PANGO_SCALE,
271 };
272 ✗ get_color_from_glyph_item (current_item, &glyph.icon_color);
273 ✗ g_array_append_vals (self->glyphs, &glyph, 1);
274 }
275 else
276 {
277 ✗ for (i = 0; i < current_item->glyphs->num_glyphs; i ++)
278 {
279 ✗ GskRenderNode *node;
280 ✗ Glyph glyph;
281 ✗ PangoGlyphString *glyph_string;
282 ✗ GdkRGBA color;
283
284 ✗ glyph_string = pango_glyph_string_new ();
285 ✗ pango_glyph_string_set_size (glyph_string, 1);
286 ✗ glyph_string->glyphs[0] = current_item->glyphs->glyphs[i];
287 ✗ glyph_string->log_clusters[0] = 0;
288
289 ✗ get_color_from_glyph_item (current_item, &color);
290
291 ✗ node =
292 ✗ gsk_text_node_new (current_item->item->analysis.font,
293 glyph_string,
294 &color,
295 ✗ &GRAPHENE_POINT_INIT (0, 0));
296
297 ✗ glyph.node = node;
298 ✗ glyph.sprite = NULL;
299 ✗ glyph.width = glyph_string->glyphs[0].geometry.width / (double) PANGO_SCALE;
300 ✗ g_array_append_vals (self->glyphs, &glyph, 1);
301
302 ✗ pango_glyph_string_free (glyph_string);
303 }
304 }
305 ✗ } while (pango_layout_iter_next_run (iter));
306 }
307 else
308 {
309 ✗ g_autoptr(GtkSnapshot) snapshot = gtk_snapshot_new ();
310
311 ✗ gtk_snapshot_append_layout (snapshot, layout, &RGBA_BLACK);
312
313 ✗ if (any_images)
314 {
315 ✗ g_autoptr(PangoLayoutIter) iter = NULL;
316
317 ✗ iter = pango_layout_get_iter (layout);
318
319 ✗ do {
320 ✗ PangoGlyphItem *current_item = pango_layout_iter_get_run (iter);
321 ✗ PangoAttrShape *shape;
322
323 ✗ if (current_item == NULL)
324 ✗ continue;
325
326 ✗ shape = get_shape_from_glyph_item (current_item);
327 ✗ if (shape != NULL)
328 {
329 ✗ ShumateVectorSprite *sprite = shape->data;
330 ✗ PangoRectangle extents;
331 ✗ GdkRGBA color;
332
333 ✗ pango_layout_iter_get_run_extents (iter, &extents, NULL);
334 ✗ get_color_from_glyph_item (current_item, &color);
335
336 ✗ gtk_snapshot_save (snapshot);
337 ✗ gtk_snapshot_translate (snapshot,
338 ✗ &GRAPHENE_POINT_INIT (
339 PANGO_PIXELS (extents.x),
340 PANGO_PIXELS (extents.y)
341 ));
342 ✗ gtk_symbolic_paintable_snapshot_symbolic (
343 GTK_SYMBOLIC_PAINTABLE (sprite),
344 snapshot,
345 ✗ PANGO_PIXELS (extents.width),
346 ✗ PANGO_PIXELS (extents.height),
347 &color,
348 1
349 );
350 ✗ gtk_snapshot_restore (snapshot);
351 }
352 ✗ } while (pango_layout_iter_next_run (iter));
353 }
354
355 ✗ self->glyphs_node = gtk_snapshot_free_to_node (g_steal_pointer (&snapshot));
356 }
357 }
358
359 ✗ if (self->symbol_info->details->cursor != NULL)
360 ✗ gtk_widget_set_cursor_from_name (GTK_WIDGET (self), self->symbol_info->details->cursor);
361
362 ✗ if (self->symbol_info->line != NULL)
363 {
364 ✗ ShumateVectorPointIter iter;
365
366 ✗ shumate_vector_point_iter_init (&iter, self->symbol_info->line);
367 ✗ shumate_vector_point_iter_advance (&iter, self->symbol_info->line_position);
368
369 ✗ shumate_vector_point_iter_get_current_point (&iter, &self->midpoint);
370 ✗ self->midpoint.x -= self->symbol_info->x;
371 ✗ self->midpoint.y -= self->symbol_info->y;
372
373 ✗ self->midpoint_angle = shumate_vector_point_iter_get_current_angle (&iter);
374
375 ✗ self->line_length = shumate_vector_line_string_length (self->symbol_info->line);
376 }
377
378 ✗ gtk_accessible_update_property (GTK_ACCESSIBLE (self),
379 GTK_ACCESSIBLE_PROPERTY_LABEL,
380 text,
381 -1);
382
383 ✗ G_OBJECT_CLASS (shumate_vector_symbol_parent_class)->constructed (object);
384 ✗ }
385
386
387 static gboolean
388 ✗ shumate_vector_symbol_contains (GtkWidget *widget,
389 double x,
390 double y)
391 {
392 ✗ GtkWidget *parent;
393 ✗ graphene_rect_t alloc;
394
395 ✗ if (x < 0 || y < 0)
396 return FALSE;
397
398 ✗ parent = gtk_widget_get_parent (widget);
399 ✗ if (!gtk_widget_compute_bounds (widget, parent, &alloc))
400 return FALSE;
401
402 ✗ if (x > alloc.size.width || y > alloc.size.height)
403 return FALSE;
404
405 ✗ if (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (parent))
406 {
407 ✗ ShumateVectorCollision *collision =
408 ✗ shumate_vector_symbol_container_get_collision ((ShumateVectorSymbolContainer *) (parent));
409
410 ✗ return shumate_vector_collision_query_point (collision,
411 ✗ alloc.origin.x + x + collision->delta_x,
412 ✗ alloc.origin.y + y + collision->delta_y,
413 widget);
414 }
415
416 return TRUE;
417 }
418
419
420 static void
421 ✗ shumate_vector_symbol_dispose (GObject *object)
422 {
423 ✗ ShumateVectorSymbol *self = (ShumateVectorSymbol *)object;
424 ✗ GtkWidget *child;
425
426 ✗ while ((child = gtk_widget_get_first_child (GTK_WIDGET (object))))
427 ✗ gtk_widget_unparent (child);
428
429 ✗ g_clear_pointer (&self->symbol_info, shumate_vector_symbol_info_unref);
430 ✗ g_clear_pointer (&self->glyphs, g_array_unref);
431 ✗ g_clear_pointer (&self->glyphs_node, gsk_render_node_unref);
432
433 ✗ G_OBJECT_CLASS (shumate_vector_symbol_parent_class)->dispose (object);
434 ✗ }
435
436
437 static void
438 ✗ shumate_vector_symbol_get_property (GObject *object,
439 guint property_id,
440 GValue *value,
441 GParamSpec *pspec)
442 {
443 ✗ ShumateVectorSymbol *self = (ShumateVectorSymbol *)object;
444
445 ✗ switch (property_id)
446 {
447 ✗ case PROP_SYMBOL_INFO:
448 ✗ g_value_set_boxed (value, self->symbol_info);
449 ✗ break;
450
451 ✗ default:
452 ✗ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
453 }
454 ✗ }
455
456
457 static void
458 ✗ shumate_vector_symbol_set_property (GObject *object,
459 guint property_id,
460 const GValue *value,
461 GParamSpec *pspec)
462 {
463 ✗ ShumateVectorSymbol *self = SHUMATE_VECTOR_SYMBOL (object);
464
465 ✗ switch (property_id)
466 {
467 ✗ case PROP_SYMBOL_INFO:
468 ✗ g_assert (self->symbol_info == NULL);
469 ✗ self->symbol_info = g_value_dup_boxed (value);
470 ✗ break;
471
472 ✗ default:
473 ✗ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
474 }
475 ✗ }
476
477
478 static double
479 ✗ positive_mod (double i, double n)
480 {
481 ✗ return fmod (fmod (i, n) + n, n);
482 }
483
484
485 static void
486 ✗ shumate_vector_symbol_measure (GtkWidget *widget,
487 GtkOrientation orientation,
488 int for_size,
489 int *minimum,
490 int *natural,
491 int *minimum_baseline,
492 int *natural_baseline)
493 {
494 ✗ ShumateVectorSymbol *self = SHUMATE_VECTOR_SYMBOL (widget);
495
496 ✗ if (self->symbol_info->line != NULL)
497 {
498 ✗ if (minimum)
499 ✗ *minimum = 0;
500 ✗ if (natural)
501 ✗ *natural = 0;
502 }
503 else
504 {
505 ✗ if (orientation == GTK_ORIENTATION_HORIZONTAL)
506 {
507 ✗ if (minimum)
508 ✗ *minimum = self->layout_width;
509 ✗ if (natural)
510 ✗ *natural = self->layout_width;
511 }
512 ✗ else if (orientation == GTK_ORIENTATION_VERTICAL)
513 {
514 ✗ if (minimum)
515 ✗ *minimum = self->layout_height;
516 ✗ if (natural)
517 ✗ *natural = self->layout_height;
518 }
519 }
520 ✗ }
521
522 static GtkSizeRequestMode
523 ✗ shumate_vector_symbol_get_request_mode (GtkWidget *widget)
524 {
525 ✗ return GTK_SIZE_REQUEST_CONSTANT_SIZE;
526 }
527
528
529 static void
530 ✗ add_anchor_offset (ShumateVectorAnchor anchor,
531 double *offset_x,
532 double *offset_y,
533 double width,
534 double height)
535 {
536 ✗ if (anchor == SHUMATE_VECTOR_ANCHOR_LEFT
537 ✗ || anchor == SHUMATE_VECTOR_ANCHOR_TOP_LEFT
538 || anchor == SHUMATE_VECTOR_ANCHOR_BOTTOM_LEFT)
539 ✗ *offset_x += width / 2.0;
540
541 ✗ if (anchor == SHUMATE_VECTOR_ANCHOR_RIGHT
542 ✗ || anchor == SHUMATE_VECTOR_ANCHOR_TOP_RIGHT
543 || anchor == SHUMATE_VECTOR_ANCHOR_BOTTOM_RIGHT)
544 ✗ *offset_x -= width / 2.0;
545
546 ✗ if (anchor == SHUMATE_VECTOR_ANCHOR_TOP
547 ✗ || anchor == SHUMATE_VECTOR_ANCHOR_TOP_LEFT
548 ✗ || anchor == SHUMATE_VECTOR_ANCHOR_TOP_RIGHT)
549 ✗ *offset_y += height / 2.0;
550
551 ✗ if (anchor == SHUMATE_VECTOR_ANCHOR_BOTTOM
552 ✗ || anchor == SHUMATE_VECTOR_ANCHOR_BOTTOM_LEFT
553 ✗ || anchor == SHUMATE_VECTOR_ANCHOR_BOTTOM_RIGHT)
554 ✗ *offset_y -= height / 2.0;
555 ✗ }
556
557
558 static void
559 ✗ shumate_vector_symbol_snapshot (GtkWidget *widget,
560 GtkSnapshot *snapshot)
561 {
562 ✗ ShumateVectorSymbol *self = SHUMATE_VECTOR_SYMBOL (widget);
563 ✗ double rotation = 0.0;
564 ✗ double tile_size_for_zoom = 512.0;
565 ✗ GtkWidget *parent = gtk_widget_get_parent (widget);
566 ✗ ShumateVectorPointIter iter;
567
568 ✗ if (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (parent))
569 {
570 ✗ ShumateViewport *viewport = shumate_layer_get_viewport (SHUMATE_LAYER (parent));
571 ✗ ShumateMapSource *map_source = shumate_vector_symbol_container_get_map_source (SHUMATE_VECTOR_SYMBOL_CONTAINER (parent));
572 ✗ double zoom_level = shumate_viewport_get_zoom_level (viewport);
573 ✗ tile_size_for_zoom = shumate_map_source_get_tile_size (map_source) * pow (2, zoom_level - self->symbol_info->details->tile_zoom_level);
574 ✗ rotation = shumate_viewport_get_rotation (viewport);
575 }
576
577 ✗ gtk_snapshot_save (snapshot);
578
579 /* Translate so the origin is at the origin point of the symbol */
580 ✗ gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (self->x - self->bounds.origin.x,
581 self->y - self->bounds.origin.y));
582
583 ✗ if (self->show_icon && self->symbol_info->details->icon_image && self->symbol_info->details->icon_opacity > 0.0)
584 {
585 ✗ double angle = 0;
586 ✗ double icon_width = shumate_vector_sprite_get_width (self->symbol_info->details->icon_image)
587 ✗ * self->symbol_info->details->icon_size;
588 ✗ double icon_height = shumate_vector_sprite_get_height (self->symbol_info->details->icon_image)
589 ✗ * self->symbol_info->details->icon_size;
590
591 ✗ double offset_x = self->symbol_info->details->icon_offset_x * self->symbol_info->details->icon_size;
592 ✗ double offset_y = self->symbol_info->details->icon_offset_y * self->symbol_info->details->icon_size;
593
594 ✗ add_anchor_offset (self->symbol_info->details->icon_anchor,
595 &offset_x, &offset_y,
596 icon_width, icon_height);
597
598 ✗ if (self->symbol_info->details->icon_rotation_alignment == SHUMATE_VECTOR_ALIGNMENT_MAP)
599 ✗ angle = self->midpoint_angle;
600 else
601 ✗ angle = -rotation;
602
603 ✗ angle += self->symbol_info->details->icon_rotate;
604
605 ✗ gtk_snapshot_save (snapshot);
606
607 ✗ gtk_snapshot_rotate (snapshot, rotation * 180 / G_PI);
608 ✗ gtk_snapshot_translate (snapshot,
609 ✗ &GRAPHENE_POINT_INIT (
610 self->midpoint.x * tile_size_for_zoom,
611 self->midpoint.y * tile_size_for_zoom
612 ));
613 ✗ gtk_snapshot_rotate (snapshot, angle * 180 / G_PI);
614 ✗ gtk_snapshot_translate (snapshot,
615 ✗ &GRAPHENE_POINT_INIT (
616 -icon_width / 2.0 + offset_x,
617 -icon_height / 2.0 + offset_y
618 ));
619
620 ✗ if (self->symbol_info->details->icon_opacity < 1.0)
621 ✗ gtk_snapshot_push_opacity (snapshot, self->symbol_info->details->icon_opacity);
622
623 ✗ gtk_symbolic_paintable_snapshot_symbolic (
624 ✗ GTK_SYMBOLIC_PAINTABLE (self->symbol_info->details->icon_image),
625 snapshot,
626 icon_width,
627 icon_height,
628 ✗ &self->symbol_info->details->icon_color,
629 1
630 );
631
632 ✗ if (self->symbol_info->details->icon_opacity < 1.0)
633 ✗ gtk_snapshot_pop (snapshot);
634
635 ✗ gtk_snapshot_restore (snapshot);
636 }
637
638 ✗ if (self->show_text && self->glyphs && self->symbol_info->details->text_opacity > 0.0)
639 ✗ {
640 ✗ double length = self->layout_width / tile_size_for_zoom;
641 ✗ double start_pos = MAX (0, self->symbol_info->line_position - (length / 2.0));
642 ✗ double avg_angle;
643
644 ✗ shumate_vector_point_iter_init (&iter, self->symbol_info->line);
645 ✗ shumate_vector_point_iter_advance (&iter, start_pos);
646
647 /* If the label is upside down on average, draw it the other way around */
648 ✗ if (self->symbol_info->details->text_keep_upright)
649 {
650 ✗ avg_angle = shumate_vector_point_iter_get_average_angle (&iter, self->layout_width / tile_size_for_zoom);
651 ✗ avg_angle = positive_mod (avg_angle + rotation, G_PI * 2.0);
652 ✗ if (avg_angle > G_PI / 2.0 && avg_angle < 3.0 * G_PI / 2.0)
653 {
654 ✗ iter.reversed = TRUE;
655 ✗ iter.current_point = iter.num_points - 1;
656 ✗ iter.distance = 0;
657 ✗ shumate_vector_point_iter_advance (&iter, self->line_length - start_pos - (self->layout_width / tile_size_for_zoom));
658 }
659 }
660
661 ✗ gtk_snapshot_save (snapshot);
662 ✗ gtk_snapshot_rotate (snapshot, rotation * 180 / G_PI);
663
664 ✗ if (self->symbol_info->details->text_opacity < 1.0)
665 ✗ gtk_snapshot_push_opacity (snapshot, self->symbol_info->details->text_opacity);
666
667 ✗ for (int i = 0; i < self->glyphs->len; i ++)
668 {
669 ✗ Glyph *glyph = &((Glyph *)self->glyphs->data)[i];
670 ✗ ShumateVectorPoint point;
671
672 /* Whitespace has no glyph, but still has a width that needs to be
673 * advanced in the point iter */
674 ✗ if (glyph->node != NULL || glyph->sprite != NULL)
675 ✗ {
676 ✗ double angle;
677
678 ✗ if (self->symbol_info->details->text_rotation_alignment == SHUMATE_VECTOR_ALIGNMENT_VIEWPORT_GLYPH)
679 ✗ angle = -rotation;
680 else
681 ✗ angle = shumate_vector_point_iter_get_average_angle (&iter, glyph->width / tile_size_for_zoom);
682
683 ✗ shumate_vector_point_iter_advance (&iter, glyph->width / tile_size_for_zoom / 2.0);
684 ✗ shumate_vector_point_iter_get_current_point (&iter, &point);
685 ✗ shumate_vector_point_iter_advance (&iter, glyph->width / tile_size_for_zoom / 2.0);
686
687 ✗ gtk_snapshot_save (snapshot);
688 ✗ gtk_snapshot_translate (snapshot,
689 ✗ &GRAPHENE_POINT_INIT (
690 (point.x - self->symbol_info->x) * tile_size_for_zoom,
691 (point.y - self->symbol_info->y) * tile_size_for_zoom
692 ));
693 ✗ gtk_snapshot_rotate (snapshot, angle * 180 / G_PI);
694 ✗ gtk_snapshot_translate (snapshot,
695 ✗ &GRAPHENE_POINT_INIT (-glyph->width / 2.0,
696 self->baseline - self->layout_y - self->layout_height / 2.0));
697
698 ✗ if (glyph->node != NULL)
699 ✗ gtk_snapshot_append_node (snapshot, glyph->node);
700 else
701 {
702 ✗ int width = shumate_vector_sprite_get_width (glyph->sprite);
703 ✗ int height = shumate_vector_sprite_get_height (glyph->sprite);
704 ✗ gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (0, -height));
705 ✗ gtk_symbolic_paintable_snapshot_symbolic (
706 ✗ GTK_SYMBOLIC_PAINTABLE (glyph->sprite),
707 snapshot,
708 width,
709 height,
710 ✗ &glyph->icon_color,
711 1
712 );
713 }
714
715 ✗ gtk_snapshot_restore (snapshot);
716 }
717 else
718 ✗ shumate_vector_point_iter_advance (&iter, glyph->width / tile_size_for_zoom);
719 }
720
721 ✗ if (self->symbol_info->details->text_opacity < 1.0)
722 ✗ gtk_snapshot_pop (snapshot);
723
724 ✗ gtk_snapshot_restore (snapshot);
725 }
726 ✗ else if (self->show_text && self->glyphs_node && self->symbol_info->details->text_opacity > 0.0)
727 {
728 ✗ double angle = 0;
729 ✗ double offset_x = self->symbol_info->details->text_offset_x * self->symbol_info->details->text_size;
730 ✗ double offset_y = self->symbol_info->details->text_offset_y * self->symbol_info->details->text_size;
731
732 ✗ add_anchor_offset (self->symbol_info->details->text_anchor,
733 &offset_x, &offset_y,
734 ✗ self->layout_width, self->layout_height);
735
736 ✗ if (self->symbol_info->details->text_rotation_alignment != SHUMATE_VECTOR_ALIGNMENT_MAP)
737 ✗ angle = -rotation;
738
739 ✗ gtk_snapshot_save (snapshot);
740
741 ✗ if (self->symbol_info->details->text_opacity < 1)
742 ✗ gtk_snapshot_push_opacity (snapshot, self->symbol_info->details->text_opacity);
743
744 ✗ gtk_snapshot_rotate (snapshot, rotation * 180 / G_PI);
745 ✗ gtk_snapshot_translate (snapshot,
746 ✗ &GRAPHENE_POINT_INIT (
747 self->midpoint.x * tile_size_for_zoom,
748 self->midpoint.y * tile_size_for_zoom
749 ));
750 ✗ gtk_snapshot_rotate (snapshot, angle * 180 / G_PI);
751
752 ✗ gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (
753 -self->layout_width / 2.0 + offset_x,
754 -self->layout_y - self->layout_height / 2.0 + offset_y
755 ));
756 ✗ gtk_snapshot_append_node (snapshot, self->glyphs_node);
757
758 ✗ if (self->symbol_info->details->text_opacity < 1)
759 ✗ gtk_snapshot_pop (snapshot);
760
761 ✗ gtk_snapshot_restore (snapshot);
762 }
763
764 ✗ gtk_snapshot_restore (snapshot);
765 ✗ }
766
767
768 static void
769 ✗ shumate_vector_symbol_class_init (ShumateVectorSymbolClass *klass)
770 {
771 ✗ GObjectClass *object_class = G_OBJECT_CLASS (klass);
772 ✗ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
773
774 ✗ object_class->constructed = shumate_vector_symbol_constructed;
775 ✗ object_class->dispose = shumate_vector_symbol_dispose;
776 ✗ object_class->get_property = shumate_vector_symbol_get_property;
777 ✗ object_class->set_property = shumate_vector_symbol_set_property;
778
779 ✗ widget_class->contains = shumate_vector_symbol_contains;
780 ✗ widget_class->get_request_mode = shumate_vector_symbol_get_request_mode;
781 ✗ widget_class->snapshot = shumate_vector_symbol_snapshot;
782 ✗ widget_class->measure = shumate_vector_symbol_measure;
783
784 /**
785 * ShumateVectorSymbol:symbol-info:
786 *
787 * The vector symbol information.
788 */
789 ✗ obj_properties[PROP_SYMBOL_INFO] =
790 ✗ g_param_spec_boxed ("symbol-info", NULL, NULL,
791 SHUMATE_TYPE_VECTOR_SYMBOL_INFO,
792 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
793
794 ✗ g_object_class_install_properties (object_class, N_PROPS, obj_properties);
795
796 ✗ signals[CLICKED] =
797 ✗ g_signal_new ("clicked",
798 G_OBJECT_CLASS_TYPE (object_class),
799 G_SIGNAL_RUN_LAST,
800 0, NULL, NULL,
801 g_cclosure_marshal_VOID__OBJECT,
802 G_TYPE_NONE,
803 1,
804 SHUMATE_TYPE_SYMBOL_EVENT);
805
806 ✗ gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_LABEL);
807 ✗ }
808
809 static void
810 ✗ on_clicked (ShumateVectorSymbol *self,
811 gint n_press,
812 double x,
813 double y,
814 GtkGestureClick *click)
815 {
816 ✗ g_autoptr(ShumateSymbolEvent) event =
817 ✗ shumate_symbol_event_new_with_n_press (self->symbol_info->details->layer,
818 ✗ self->symbol_info->details->source_layer,
819 ✗ self->symbol_info->details->feature_id,
820 ✗ self->symbol_info->details->tags,
821 n_press);
822
823 ✗ g_signal_emit (self, signals[CLICKED], 0, event);
824 ✗ }
825
826 static void
827 ✗ shumate_vector_symbol_init (ShumateVectorSymbol *self)
828 {
829 ✗ GtkGesture *click = gtk_gesture_click_new ();
830 ✗ g_signal_connect_object (click, "released", G_CALLBACK (on_clicked), self, G_CONNECT_SWAPPED);
831 ✗ gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (click));
832 ✗ }
833
834
835 ShumateVectorSymbolInfo *
836 ✗ shumate_vector_symbol_get_symbol_info (ShumateVectorSymbol *self)
837 {
838 ✗ g_return_val_if_fail (SHUMATE_IS_VECTOR_SYMBOL (self), NULL);
839
840 ✗ return self->symbol_info;
841 }
842
843 static void
844 ✗ rotate_around_center (double *x,
845 double *y,
846 double angle)
847 {
848 /* Rotate (x, y) around (0, 0) */
849
850 ✗ if (angle == 0)
851 return;
852
853 ✗ double old_x = *x;
854 ✗ double old_y = *y;
855
856 ✗ *x = cosf (angle) * old_x - sinf (angle) * old_y;
857 ✗ *y = sinf (angle) * old_x + cosf (angle) * old_y;
858 }
859
860
861 gboolean
862 ✗ shumate_vector_symbol_calculate_collision (ShumateVectorSymbol *self,
863 ShumateVectorCollision *collision,
864 double x,
865 double y,
866 double tile_size_for_zoom,
867 double rotation,
868 graphene_rect_t *bounds_out)
869 {
870 ✗ double yextent = self->symbol_info->details->text_size / 2.0;
871 ✗ gboolean check;
872 ✗ ShumateVectorPointIter iter;
873 ✗ ShumateVectorPoint point;
874 ✗ ShumateVectorPoint midpoint = {0};
875 ✗ int save = 0;
876
877 ✗ shumate_vector_collision_rollback_pending (collision, 0);
878
879 ✗ if (self->symbol_info->line != NULL)
880 {
881 ✗ midpoint = self->midpoint;
882
883 ✗ rotate_around_center (&midpoint.x, &midpoint.y, rotation);
884 ✗ midpoint.x *= tile_size_for_zoom;
885 ✗ midpoint.y *= tile_size_for_zoom;
886 }
887
888 ✗ if (self->glyphs != NULL)
889 {
890 ✗ double line_length = self->symbol_info->line_length;
891 ✗ double length = self->layout_width / tile_size_for_zoom;
892 ✗ double start_pos = MAX (0, self->symbol_info->line_position - (length / 2.0));
893
894 ✗ g_assert (self->symbol_info->line != NULL);
895
896 ✗ if (length > line_length - start_pos)
897 {
898 ✗ self->show_text = FALSE;
899 ✗ return FALSE;
900 }
901
902 ✗ shumate_vector_point_iter_init (&iter, self->symbol_info->line);
903 ✗ shumate_vector_point_iter_advance (&iter, start_pos);
904
905 ✗ self->show_text = TRUE;
906
907 ✗ do
908 {
909 ✗ double xextent = MIN (length, shumate_vector_point_iter_get_segment_length (&iter) - iter.distance) * tile_size_for_zoom / 2;
910
911 ✗ if (shumate_vector_point_iter_is_at_end (&iter))
912 return FALSE;
913
914 ✗ shumate_vector_point_iter_get_segment_center (&iter, length, &point);
915 ✗ point.x -= self->symbol_info->x;
916 ✗ point.y -= self->symbol_info->y;
917
918 ✗ rotate_around_center (&point.x, &point.y, rotation);
919 ✗ point.x *= tile_size_for_zoom;
920 ✗ point.y *= tile_size_for_zoom;
921
922 ✗ gboolean check = shumate_vector_collision_check (
923 collision,
924 ✗ x + point.x,
925 ✗ y + point.y,
926 xextent + self->symbol_info->details->text_padding,
927 ✗ yextent + self->symbol_info->details->text_padding,
928 ✗ rotation + shumate_vector_point_iter_get_current_angle (&iter),
929 self->symbol_info->details->text_overlap,
930 ✗ self->symbol_info->details->text_ignore_placement,
931 self
932 );
933
934 ✗ if (!check)
935 {
936 ✗ if (self->symbol_info->details->text_optional)
937 {
938 ✗ shumate_vector_collision_rollback_pending (collision, save);
939 ✗ self->show_text = FALSE;
940 ✗ break;
941 }
942 else
943 return FALSE;
944 }
945 }
946 ✗ while ((length -= shumate_vector_point_iter_next_segment (&iter)) > 0);
947 }
948 ✗ else if (self->glyphs_node != NULL)
949 {
950 ✗ ShumateVectorAnchor anchor = self->symbol_info->details->text_anchor;
951 ✗ double angle = 0;
952 ✗ double offset_x = self->symbol_info->details->text_offset_x * self->symbol_info->details->text_size;
953 ✗ double offset_y = self->symbol_info->details->text_offset_y * self->symbol_info->details->text_size;
954
955 ✗ add_anchor_offset (anchor, &offset_x, &offset_y, self->layout_width, self->layout_height);
956
957 ✗ if (self->symbol_info->details->text_rotation_alignment == SHUMATE_VECTOR_ALIGNMENT_MAP)
958 ✗ angle = rotation;
959
960 ✗ rotate_around_center (&offset_x, &offset_y, angle);
961
962 ✗ self->show_text = TRUE;
963
964 ✗ check = shumate_vector_collision_check (
965 collision,
966 ✗ x + midpoint.x + offset_x,
967 ✗ y + midpoint.y + offset_y,
968 ✗ self->layout_width / 2.0 + self->symbol_info->details->text_padding,
969 ✗ yextent + self->symbol_info->details->text_padding,
970 angle,
971 self->symbol_info->details->text_overlap,
972 ✗ self->symbol_info->details->text_ignore_placement,
973 self
974 );
975
976 ✗ if (!check)
977 {
978 ✗ if (self->symbol_info->details->text_optional)
979 {
980 ✗ shumate_vector_collision_rollback_pending (collision, save);
981 ✗ self->show_text = FALSE;
982 }
983 else
984 ✗ return FALSE;
985 }
986 }
987
988 ✗ save = shumate_vector_collision_save_pending (collision);
989
990 ✗ if (self->symbol_info->details->icon_image)
991 {
992 ✗ float angle = 0;
993 ✗ double icon_width = shumate_vector_sprite_get_width (self->symbol_info->details->icon_image)
994 ✗ * self->symbol_info->details->icon_size;
995 ✗ double icon_height = shumate_vector_sprite_get_height (self->symbol_info->details->icon_image)
996 ✗ * self->symbol_info->details->icon_size;
997
998 ✗ double offset_x = self->symbol_info->details->icon_offset_x * self->symbol_info->details->icon_size;
999 ✗ double offset_y = self->symbol_info->details->icon_offset_y * self->symbol_info->details->icon_size;
1000
1001 ✗ self->show_icon = TRUE;
1002
1003 ✗ add_anchor_offset (self->symbol_info->details->icon_anchor, &offset_x, &offset_y, icon_width, icon_height);
1004
1005 ✗ if (self->symbol_info->details->icon_rotation_alignment == SHUMATE_VECTOR_ALIGNMENT_MAP)
1006 ✗ angle = rotation + self->midpoint_angle;
1007 ✗ angle += self->symbol_info->details->icon_rotate;
1008 ✗ offset_x += (self->symbol_info->details->icon_padding_right - self->symbol_info->details->icon_padding_left) / 2.0;
1009 ✗ offset_y += (self->symbol_info->details->icon_padding_bottom - self->symbol_info->details->icon_padding_top) / 2.0;
1010 ✗ rotate_around_center (&offset_x, &offset_y, angle);
1011
1012 ✗ check = shumate_vector_collision_check (
1013 collision,
1014 ✗ x + midpoint.x + offset_x,
1015 ✗ y + midpoint.y + offset_y,
1016 ✗ (icon_width + self->symbol_info->details->icon_padding_left + self->symbol_info->details->icon_padding_right) / 2 ,
1017 ✗ (icon_height + self->symbol_info->details->icon_padding_top + self->symbol_info->details->icon_padding_bottom) / 2,
1018 angle,
1019 self->symbol_info->details->icon_overlap,
1020 ✗ self->symbol_info->details->icon_ignore_placement,
1021 self
1022 );
1023
1024 ✗ if (!check)
1025 {
1026 ✗ if (self->symbol_info->details->icon_optional)
1027 {
1028 ✗ shumate_vector_collision_rollback_pending (collision, save);
1029 ✗ self->show_icon = FALSE;
1030 }
1031 else
1032 ✗ return FALSE;
1033 }
1034 }
1035
1036 ✗ if (!self->show_icon && !self->show_text)
1037 return FALSE;
1038
1039 ✗ shumate_vector_collision_commit_pending (collision, &self->bounds);
1040 ✗ *bounds_out = self->bounds;
1041 ✗ self->x = x;
1042 ✗ self->y = y;
1043 ✗ return TRUE;
1044 }
1045
1046