doc: update copyright line for 2021
[adg.git] / src / adg / adg-hatch.c
blob9bb15b6a075be0414faaabefae5debb96172ea49
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 Nicola Fontana <ntd at entidi.it>
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 of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-hatch
23 * @short_description: A hatched region
25 * The #AdgHatch object is used to fill a closed #AdgTrail model
26 * with some sort of pattern.
28 * Since: 1.0
29 **/
31 /**
32 * AdgHatch:
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
37 * Since: 1.0
38 **/
41 #include "adg-internal.h"
42 #include "adg-model.h"
43 #include "adg-trail.h"
44 #include "adg-stroke.h"
45 #include "adg-style.h"
46 #include "adg-fill-style.h"
47 #include "adg-dress.h"
48 #include "adg-param-dress.h"
50 #include "adg-hatch.h"
51 #include "adg-hatch-private.h"
54 G_DEFINE_TYPE_WITH_PRIVATE(AdgHatch, adg_hatch, ADG_TYPE_STROKE)
56 enum {
57 PROP_0,
58 PROP_FILL_DRESS
62 static void _adg_get_property (GObject *object,
63 guint param_id,
64 GValue *value,
65 GParamSpec *pspec);
66 static void _adg_set_property (GObject *object,
67 guint param_id,
68 const GValue *value,
69 GParamSpec *pspec);
70 static void _adg_render (AdgEntity *entity,
71 cairo_t *cr);
74 static void
75 adg_hatch_class_init(AdgHatchClass *klass)
77 GObjectClass *gobject_class;
78 AdgEntityClass *entity_class;
79 GParamSpec *param;
81 gobject_class = (GObjectClass *) klass;
82 entity_class = (AdgEntityClass *) klass;
84 gobject_class->get_property = _adg_get_property;
85 gobject_class->set_property = _adg_set_property;
87 entity_class->render = _adg_render;
89 param = adg_param_spec_dress("fill-dress",
90 P_("Fill Dress"),
91 P_("The dress to use for filling this entity"),
92 ADG_DRESS_FILL_HATCH,
93 G_PARAM_READWRITE);
94 g_object_class_install_property(gobject_class, PROP_FILL_DRESS, param);
97 static void
98 adg_hatch_init(AdgHatch *hatch)
100 AdgHatchPrivate *data = adg_hatch_get_instance_private(hatch);
101 data->fill_dress = ADG_DRESS_FILL_HATCH;
104 static void
105 _adg_get_property(GObject *object, guint prop_id,
106 GValue *value, GParamSpec *pspec)
108 AdgHatchPrivate *data = adg_hatch_get_instance_private((AdgHatch *) object);
110 switch (prop_id) {
111 case PROP_FILL_DRESS:
112 g_value_set_enum(value, data->fill_dress);
113 break;
114 default:
115 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
116 break;
120 static void
121 _adg_set_property(GObject *object, guint prop_id,
122 const GValue *value, GParamSpec *pspec)
124 AdgHatch *hatch = (AdgHatch *) object;
125 AdgHatchPrivate *data = adg_hatch_get_instance_private(hatch);
127 switch (prop_id) {
128 case PROP_FILL_DRESS:
129 data->fill_dress = g_value_get_enum(value);
130 break;
131 default:
132 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
133 break;
139 * adg_hatch_new:
140 * @trail: the #AdgTrail to hatch
142 * Creates a new hatch entity. @trail can be <constant>NULL</constant>,
143 * in which case an empty hatch is created.
145 * Returns: the newly created hatch entity
147 * Since: 1.0
149 AdgHatch *
150 adg_hatch_new(AdgTrail *trail)
152 return g_object_new(ADG_TYPE_HATCH, "trail", trail, NULL);
156 * adg_hatch_set_fill_dress:
157 * @hatch: an #AdgHatch
158 * @dress: the new #AdgDress to use
160 * Sets a new line dress for rendering @hatch. The new dress
161 * must be related to the original dress for this property:
162 * you cannot set a dress used for line styles to a dress
163 * managing fonts.
165 * The check is done by calling adg_dress_are_related() with
166 * @dress and the previous dress as arguments. Check out its
167 * documentation for details on what is a related dress.
169 * Since: 1.0
171 void
172 adg_hatch_set_fill_dress(AdgHatch *hatch, AdgDress dress)
174 g_return_if_fail(ADG_IS_HATCH(hatch));
175 g_object_set(hatch, "fill-dress", dress, NULL);
179 * adg_hatch_get_fill_dress:
180 * @hatch: an #AdgHatch
182 * Gets the line dress to be used in rendering @hatch.
184 * Returns: (transfer none): the current line dress.
186 * Since: 1.0
188 AdgDress
189 adg_hatch_get_fill_dress(AdgHatch *hatch)
191 AdgHatchPrivate *data;
193 g_return_val_if_fail(ADG_IS_HATCH(hatch), ADG_DRESS_UNDEFINED);
195 data = adg_hatch_get_instance_private(hatch);
196 return data->fill_dress;
200 static void
201 _adg_render(AdgEntity *entity, cairo_t *cr)
203 AdgHatch *hatch = (AdgHatch *) entity;
204 AdgStroke *stroke = (AdgStroke *) entity;
205 AdgTrail *trail = adg_stroke_get_trail(stroke);
206 const cairo_path_t *cairo_path = adg_trail_get_cairo_path(trail);
208 if (cairo_path != NULL) {
209 AdgHatchPrivate *data = adg_hatch_get_instance_private(hatch);
210 AdgFillStyle *fill_style =
211 (AdgFillStyle *) adg_entity_style(entity, data->fill_dress);
213 adg_fill_style_set_extents(fill_style, adg_entity_get_extents(entity));
215 cairo_save(cr);
216 cairo_transform(cr, adg_entity_get_global_matrix(entity));
217 cairo_transform(cr, adg_entity_get_local_matrix(entity));
218 cairo_append_path(cr, cairo_path);
219 cairo_restore(cr);
221 adg_style_apply((AdgStyle *) fill_style, entity, cr);
222 cairo_fill(cr);