shumate/vector/shumate-vector-line-layer.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 | #include <gtk/gtk.h> | ||
| 19 | #include "shumate-vector-expression-private.h" | ||
| 20 | #include "shumate-vector-line-layer-private.h" | ||
| 21 | #include "shumate-vector-utils-private.h" | ||
| 22 | #include "shumate-vector-value-private.h" | ||
| 23 | |||
| 24 | struct _ShumateVectorLineLayer | ||
| 25 | { | ||
| 26 | ShumateVectorLayer parent_instance; | ||
| 27 | |||
| 28 | ShumateVectorExpression *color; | ||
| 29 | ShumateVectorExpression *opacity; | ||
| 30 | ShumateVectorExpression *width; | ||
| 31 | ShumateVectorExpression *cap; | ||
| 32 | ShumateVectorExpression *join; | ||
| 33 | |||
| 34 | double *dashes; | ||
| 35 | int num_dashes; | ||
| 36 | }; | ||
| 37 | |||
| 38 |
4/6shumate_vector_line_layer_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4 times.
shumate_vector_line_layer_get_type:
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 7 taken 2 times.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 7 not taken.
|
14 | G_DEFINE_TYPE (ShumateVectorLineLayer, shumate_vector_line_layer, SHUMATE_TYPE_VECTOR_LAYER) |
| 39 | |||
| 40 | |||
| 41 | ShumateVectorLayer * | ||
| 42 | 6 | shumate_vector_line_layer_create_from_json (JsonObject *object, GError **error) | |
| 43 | { | ||
| 44 | 6 | ShumateVectorLineLayer *layer = g_object_new (SHUMATE_TYPE_VECTOR_LINE_LAYER, NULL); | |
| 45 | 6 | JsonNode *paint_node, *layout_node; | |
| 46 | |||
| 47 |
1/2✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 33 not taken.
|
6 | if ((paint_node = json_object_get_member (object, "paint"))) |
| 48 | { | ||
| 49 | 6 | JsonObject *paint; | |
| 50 | |||
| 51 |
1/2✓ Branch 7 → 8 taken 6 times.
✗ Branch 7 → 32 not taken.
|
6 | if (!shumate_vector_json_get_object (paint_node, &paint, error)) |
| 52 | ✗ | return NULL; | |
| 53 | |||
| 54 |
1/2✓ Branch 10 → 11 taken 6 times.
✗ Branch 10 → 32 not taken.
|
6 | if (!(layer->color = shumate_vector_expression_from_json (json_object_get_member (paint, "line-color"), error))) |
| 55 | return NULL; | ||
| 56 | |||
| 57 |
1/2✓ Branch 13 → 14 taken 6 times.
✗ Branch 13 → 32 not taken.
|
6 | if (!(layer->opacity = shumate_vector_expression_from_json (json_object_get_member (paint, "line-opacity"), error))) |
| 58 | return NULL; | ||
| 59 | |||
| 60 |
1/2✓ Branch 16 → 17 taken 6 times.
✗ Branch 16 → 32 not taken.
|
6 | if (!(layer->width = shumate_vector_expression_from_json (json_object_get_member (paint, "line-width"), error))) |
| 61 | return NULL; | ||
| 62 | |||
| 63 | { | ||
| 64 | 6 | JsonArray *dasharray; | |
| 65 | |||
| 66 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 6 times.
|
6 | if (!shumate_vector_json_get_array_member (paint, "line-dasharray", &dasharray, error)) |
| 67 | ✗ | return NULL; | |
| 68 | |||
| 69 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 31 taken 6 times.
|
6 | if (dasharray != NULL) |
| 70 | { | ||
| 71 | ✗ | int i; | |
| 72 | ✗ | layer->num_dashes = json_array_get_length (dasharray); | |
| 73 | ✗ | layer->dashes = g_new (double, layer->num_dashes); | |
| 74 | ✗ | for (i = 0; i < layer->num_dashes; i ++) | |
| 75 | ✗ | layer->dashes[i] = json_node_get_double (json_array_get_element (dasharray, i)); | |
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 |
1/2✗ Branch 34 → 35 not taken.
✓ Branch 34 → 45 taken 6 times.
|
6 | if ((layout_node = json_object_get_member (object, "layout"))) |
| 81 | { | ||
| 82 | ✗ | JsonObject *layout; | |
| 83 | |||
| 84 | ✗ | if (!shumate_vector_json_get_object (layout_node, &layout, error)) | |
| 85 | ✗ | return NULL; | |
| 86 | |||
| 87 | ✗ | if (!(layer->cap = shumate_vector_expression_from_json (json_object_get_member (layout, "line-cap"), error))) | |
| 88 | return NULL; | ||
| 89 | |||
| 90 | ✗ | if (!(layer->join = shumate_vector_expression_from_json (json_object_get_member (layout, "line-join"), error))) | |
| 91 | return NULL; | ||
| 92 | } | ||
| 93 | |||
| 94 | return (ShumateVectorLayer *)layer; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | static void | ||
| 99 | 6 | shumate_vector_line_layer_finalize (GObject *object) | |
| 100 | { | ||
| 101 | 6 | ShumateVectorLineLayer *self = SHUMATE_VECTOR_LINE_LAYER (object); | |
| 102 | |||
| 103 |
1/2✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 4 not taken.
|
6 | g_clear_object (&self->color); |
| 104 |
1/2✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 6 not taken.
|
6 | g_clear_object (&self->opacity); |
| 105 |
1/2✓ Branch 6 → 7 taken 6 times.
✗ Branch 6 → 8 not taken.
|
6 | g_clear_object (&self->width); |
| 106 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 6 times.
|
6 | g_clear_object (&self->cap); |
| 107 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 6 times.
|
6 | g_clear_object (&self->join); |
| 108 | |||
| 109 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 6 times.
|
6 | g_clear_pointer (&self->dashes, g_free); |
| 110 | |||
| 111 | 6 | G_OBJECT_CLASS (shumate_vector_line_layer_parent_class)->finalize (object); | |
| 112 | 6 | } | |
| 113 | |||
| 114 | |||
| 115 | static void | ||
| 116 | 4 | shumate_vector_line_layer_render (ShumateVectorLayer *layer, ShumateVectorRenderScope *scope) | |
| 117 | { | ||
| 118 | 4 | ShumateVectorLineLayer *self = SHUMATE_VECTOR_LINE_LAYER (layer); | |
| 119 | 4 | GdkRGBA color = SHUMATE_VECTOR_COLOR_BLACK; | |
| 120 | 4 | double opacity; | |
| 121 | 4 | double width; | |
| 122 | 4 | g_autofree char *cap = NULL; | |
| 123 | 4 | g_autofree char *join = NULL; | |
| 124 | |||
| 125 | 4 | shumate_vector_expression_eval_color (self->color, scope, &color); | |
| 126 | 4 | opacity = shumate_vector_expression_eval_number (self->opacity, scope, 1.0); | |
| 127 | 4 | width = shumate_vector_expression_eval_number (self->width, scope, 1.0); | |
| 128 | 4 | cap = shumate_vector_expression_eval_string (self->cap, scope, NULL); | |
| 129 | 4 | join = shumate_vector_expression_eval_string (self->join, scope, NULL); | |
| 130 | |||
| 131 | 4 | shumate_vector_render_scope_exec_geometry (scope); | |
| 132 | |||
| 133 | 4 | cairo_set_source_rgba (scope->cr, color.red, color.green, color.blue, color.alpha * opacity); | |
| 134 | 4 | cairo_set_line_width (scope->cr, width * scope->scale); | |
| 135 | |||
| 136 |
1/2✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 4 times.
|
4 | if (g_strcmp0 (cap, "round") == 0) |
| 137 | ✗ | cairo_set_line_cap (scope->cr, CAIRO_LINE_CAP_ROUND); | |
| 138 |
1/2✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 4 times.
|
4 | else if (g_strcmp0 (cap, "square") == 0) |
| 139 | ✗ | cairo_set_line_cap (scope->cr, CAIRO_LINE_CAP_SQUARE); | |
| 140 | else | ||
| 141 | 4 | cairo_set_line_cap (scope->cr, CAIRO_LINE_CAP_BUTT); | |
| 142 | |||
| 143 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 4 times.
|
4 | if (g_strcmp0 (join, "bevel") == 0) |
| 144 | ✗ | cairo_set_line_join (scope->cr, CAIRO_LINE_JOIN_BEVEL); | |
| 145 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 4 times.
|
4 | else if (g_strcmp0 (join, "round") == 0) |
| 146 | ✗ | cairo_set_line_join (scope->cr, CAIRO_LINE_JOIN_ROUND); | |
| 147 | else | ||
| 148 | 4 | cairo_set_line_join (scope->cr, CAIRO_LINE_JOIN_MITER); | |
| 149 | |||
| 150 |
1/2✓ Branch 24 → 25 taken 4 times.
✗ Branch 24 → 26 not taken.
|
4 | if (self->dashes == NULL) |
| 151 | 4 | cairo_set_dash (scope->cr, NULL, 0, 0); | |
| 152 | else | ||
| 153 | { | ||
| 154 | ✗ | int i; | |
| 155 | ✗ | g_autofree double *dasharray = g_new (double, self->num_dashes); | |
| 156 | ✗ | gboolean any_nonzero = FALSE; | |
| 157 | ✗ | gboolean all_positive = TRUE; | |
| 158 | |||
| 159 | ✗ | for (i = 0; i < self->num_dashes; i ++) | |
| 160 | { | ||
| 161 | ✗ | dasharray[i] = self->dashes[i] * width * scope->scale; | |
| 162 | |||
| 163 | ✗ | if (dasharray[i] < 0) | |
| 164 | { | ||
| 165 | all_positive = FALSE; | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | ✗ | else if (dasharray[i] != 0) | |
| 169 | ✗ | any_nonzero = TRUE; | |
| 170 | } | ||
| 171 | |||
| 172 | /* make sure the dasharray is valid */ | ||
| 173 | ✗ | if (any_nonzero && all_positive) | |
| 174 | ✗ | cairo_set_dash (scope->cr, dasharray, self->num_dashes, 0); | |
| 175 | else | ||
| 176 | ✗ | cairo_set_dash (scope->cr, NULL, 0, 0); | |
| 177 | } | ||
| 178 | |||
| 179 | 4 | cairo_stroke (scope->cr); | |
| 180 | 4 | } | |
| 181 | |||
| 182 | |||
| 183 | static void | ||
| 184 | 4 | shumate_vector_line_layer_class_init (ShumateVectorLineLayerClass *klass) | |
| 185 | { | ||
| 186 | 4 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
| 187 | 4 | ShumateVectorLayerClass *layer_class = SHUMATE_VECTOR_LAYER_CLASS (klass); | |
| 188 | |||
| 189 | 4 | object_class->finalize = shumate_vector_line_layer_finalize; | |
| 190 | 4 | layer_class->render = shumate_vector_line_layer_render; | |
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | static void | ||
| 195 | 6 | shumate_vector_line_layer_init (ShumateVectorLineLayer *self) | |
| 196 | { | ||
| 197 | 6 | } | |
| 198 |