GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 59.1% 39 / 0 / 66
Functions: 87.5% 7 / 0 / 8
Branches: 50.0% 13 / 0 / 26

shumate/vector/shumate-vector-fill-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-fill-layer-private.h"
21 #include "shumate-vector-sprite-sheet.h"
22 #include "shumate-vector-utils-private.h"
23 #include "shumate-vector-value-private.h"
24
25 struct _ShumateVectorFillLayer
26 {
27 ShumateVectorLayer parent_instance;
28
29 ShumateVectorExpression *color;
30 ShumateVectorExpression *opacity;
31 ShumateVectorExpression *pattern;
32 };
33
34
4/6
shumate_vector_fill_layer_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4 times.
shumate_vector_fill_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 (ShumateVectorFillLayer, shumate_vector_fill_layer, SHUMATE_TYPE_VECTOR_LAYER)
35
36
37 ShumateVectorLayer *
38 6 shumate_vector_fill_layer_create_from_json (JsonObject *object, GError **error)
39 {
40 6 ShumateVectorFillLayer *layer = g_object_new (SHUMATE_TYPE_VECTOR_FILL_LAYER, NULL);
41 6 JsonNode *paint_node;
42
43
1/2
✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 19 not taken.
6 if ((paint_node = json_object_get_member (object, "paint")))
44 {
45 6 JsonObject *paint;
46
47
1/2
✓ Branch 7 → 8 taken 6 times.
✗ Branch 7 → 18 not taken.
6 if (!shumate_vector_json_get_object (paint_node, &paint, error))
48 ✗ return NULL;
49
50
1/2
✓ Branch 10 → 11 taken 6 times.
✗ Branch 10 → 18 not taken.
6 if (!(layer->color = shumate_vector_expression_from_json (json_object_get_member (paint, "fill-color"), error)))
51 return NULL;
52
53
1/2
✓ Branch 13 → 14 taken 6 times.
✗ Branch 13 → 18 not taken.
6 if (!(layer->opacity = shumate_vector_expression_from_json (json_object_get_member (paint, "fill-opacity"), error)))
54 return NULL;
55
56
1/2
✓ Branch 16 → 17 taken 6 times.
✗ Branch 16 → 18 not taken.
6 if (!(layer->pattern = shumate_vector_expression_from_json (json_object_get_member (paint, "fill-pattern"), error)))
57 return NULL;
58 }
59
60 return (ShumateVectorLayer *)layer;
61 }
62
63 static cairo_pattern_t *
64 ✗ create_pattern (ShumateVectorSprite *sprite,
65 ShumateVectorRenderScope *scope)
66 {
67 ✗ int width = shumate_vector_sprite_get_width (sprite) * scope->scale_factor;
68 ✗ int height = shumate_vector_sprite_get_height (sprite) * scope->scale_factor;
69 ✗ cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
70 ✗ cairo_t *source_cr = cairo_create (surface);
71 ✗ GtkSnapshot *snapshot = gtk_snapshot_new ();
72 ✗ g_autoptr(GskRenderNode) node = NULL;
73 ✗ cairo_pattern_t *pattern;
74 ✗ cairo_matrix_t matrix;
75
76 ✗ gdk_paintable_snapshot (GDK_PAINTABLE (sprite), snapshot, width, height);
77 ✗ node = gtk_snapshot_free_to_node (snapshot);
78
79 ✗ gsk_render_node_draw (node, source_cr);
80
81 ✗ pattern = cairo_pattern_create_for_surface (surface);
82 ✗ cairo_matrix_init_scale (&matrix, 1 / scope->scale * scope->scale_factor, 1 / scope->scale * scope->scale_factor);
83 ✗ cairo_pattern_set_matrix (pattern, &matrix);
84 ✗ cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
85
86 ✗ cairo_surface_destroy (surface);
87 ✗ cairo_destroy (source_cr);
88
89 ✗ return pattern;
90 }
91
92
93 static void
94 4 shumate_vector_fill_layer_render (ShumateVectorLayer *layer, ShumateVectorRenderScope *scope)
95 {
96 4 ShumateVectorFillLayer *self = SHUMATE_VECTOR_FILL_LAYER (layer);
97 4 GdkRGBA color = SHUMATE_VECTOR_COLOR_BLACK;
98 4 double opacity;
99 8 g_autoptr(ShumateVectorSprite) pattern_sprite = NULL;
100
101 4 shumate_vector_expression_eval_color (self->color, scope, &color);
102 4 opacity = shumate_vector_expression_eval_number (self->opacity, scope, 1.0);
103 4 pattern_sprite = shumate_vector_expression_eval_image (self->pattern, scope);
104
105 4 shumate_vector_render_scope_exec_geometry (scope);
106
107
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 14 taken 4 times.
4 if (pattern_sprite != NULL)
108 {
109 ✗ cairo_pattern_t *pattern = create_pattern (pattern_sprite, scope);
110
111 ✗ cairo_set_source (scope->cr, pattern);
112
113 /* Use cairo_paint_with_alpha so that we can set fill-opacity correctly. */
114 ✗ cairo_save (scope->cr);
115 ✗ cairo_clip (scope->cr);
116 ✗ cairo_paint_with_alpha (scope->cr, opacity);
117 ✗ cairo_restore (scope->cr);
118
119 ✗ cairo_pattern_destroy (pattern);
120 }
121 else
122 {
123 4 cairo_set_source_rgba (scope->cr, color.red, color.green, color.blue, color.alpha * opacity);
124 4 cairo_fill (scope->cr);
125 }
126 4 }
127
128
129 static void
130 6 shumate_vector_fill_layer_finalize (GObject *object)
131 {
132 6 ShumateVectorFillLayer *self = SHUMATE_VECTOR_FILL_LAYER (object);
133
134
1/2
✓ Branch 2 → 3 taken 6 times.
✗ Branch 2 → 4 not taken.
6 g_clear_object (&self->color);
135
1/2
✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 6 not taken.
6 g_clear_object (&self->opacity);
136
1/2
✓ Branch 6 → 7 taken 6 times.
✗ Branch 6 → 8 not taken.
6 g_clear_object (&self->pattern);
137
138 6 G_OBJECT_CLASS (shumate_vector_fill_layer_parent_class)->finalize (object);
139 6 }
140
141
142 static void
143 4 shumate_vector_fill_layer_class_init (ShumateVectorFillLayerClass *klass)
144 {
145 4 GObjectClass *object_class = G_OBJECT_CLASS (klass);
146 4 ShumateVectorLayerClass *layer_class = SHUMATE_VECTOR_LAYER_CLASS (klass);
147
148 4 object_class->finalize = shumate_vector_fill_layer_finalize;
149 4 layer_class->render = shumate_vector_fill_layer_render;
150 }
151
152
153 static void
154 6 shumate_vector_fill_layer_init (ShumateVectorFillLayer *self)
155 {
156 6 }
157