1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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.
22 * SECTION:adg-ruled-fill
23 * @short_description: A style composed of evenly spaced lines.
25 * The spacing between the lines could be changed using the
26 * adg_ruled_fill_set_spacing() method. The angle of the lines should
27 * be changed with adg_ruled_fill_set_angle().
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
38 #include "adg-internal.h"
39 #include "adg-ruled-fill.h"
40 #include "adg-ruled-fill-private.h"
41 #include "adg-dress-builtins.h"
44 #define PARENT_STYLE_CLASS ((AdgStyleClass *) adg_ruled_fill_parent_class)
45 #define PARENT_FILL_STYLE_CLASS ((AdgFillStyleClass *) adg_ruled_fill_parent_class)
56 static void get_property (GObject
*object
,
60 static void set_property (GObject
*object
,
64 static void apply (AdgStyle
*style
,
67 static void set_extents (AdgFillStyle
*fill_style
,
68 const CpmlExtents
*extents
);
69 static gboolean
set_spacing (AdgRuledFill
*ruled_fill
,
71 static gboolean
set_angle (AdgRuledFill
*ruled_fill
,
73 static cairo_pattern_t
*create_pattern (AdgRuledFill
*ruled_fill
,
76 static void draw_lines (const CpmlPair
*spacing
,
81 G_DEFINE_TYPE(AdgRuledFill
, adg_ruled_fill
, ADG_TYPE_FILL_STYLE
);
85 adg_ruled_fill_class_init(AdgRuledFillClass
*klass
)
87 GObjectClass
*gobject_class
;
88 AdgStyleClass
*style_class
;
89 AdgFillStyleClass
*fill_style_class
;
92 gobject_class
= (GObjectClass
*) klass
;
93 style_class
= (AdgStyleClass
*) klass
;
94 fill_style_class
= (AdgFillStyleClass
*) klass
;
96 g_type_class_add_private(klass
, sizeof(AdgRuledFillPrivate
));
98 gobject_class
->get_property
= get_property
;
99 gobject_class
->set_property
= set_property
;
101 style_class
->apply
= apply
;
103 fill_style_class
->set_extents
= set_extents
;
105 param
= adg_param_spec_dress("line-dress",
107 P_("Dress to be used for rendering the lines"),
108 ADG_DRESS_LINE_HATCH
,
110 g_object_class_install_property(gobject_class
, PROP_LINE_DRESS
, param
);
112 param
= g_param_spec_double("spacing",
114 P_("The spacing in global spaces between the lines"),
117 g_object_class_install_property(gobject_class
, PROP_SPACING
, param
);
119 param
= g_param_spec_double("angle",
121 P_("The angle (in radians) of the lines"),
124 g_object_class_install_property(gobject_class
, PROP_ANGLE
, param
);
128 adg_ruled_fill_init(AdgRuledFill
*ruled_fill
)
130 AdgRuledFillPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(ruled_fill
,
132 AdgRuledFillPrivate
);
134 data
->line_dress
= ADG_DRESS_LINE_HATCH
;
135 data
->angle
= G_PI_4
;
138 ruled_fill
->data
= data
;
142 get_property(GObject
*object
,
143 guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
145 AdgRuledFillPrivate
*data
= ((AdgRuledFill
*) object
)->data
;
148 case PROP_LINE_DRESS
:
149 g_value_set_int(value
, data
->line_dress
);
152 g_value_set_double(value
, data
->spacing
);
155 g_value_set_double(value
, data
->angle
);
158 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
164 set_property(GObject
*object
,
165 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
167 AdgRuledFill
*ruled_fill
;
168 AdgRuledFillPrivate
*data
;
170 ruled_fill
= (AdgRuledFill
*) object
;
171 data
= ruled_fill
->data
;
174 case PROP_LINE_DRESS
:
175 data
->line_dress
= g_value_get_int(value
);
178 set_spacing(ruled_fill
, g_value_get_double(value
));
181 set_angle(ruled_fill
, g_value_get_double(value
));
184 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
191 * adg_ruled_fill_new:
193 * Constructs a new empty ruled fill style initialized with default params.
195 * Returns: a newly created ruled fill style
198 adg_ruled_fill_new(void)
200 return g_object_new(ADG_TYPE_RULED_FILL
, NULL
);
204 * adg_ruled_fill_set_line_dress:
205 * @ruled_fill: an #AdgRuledFill object
206 * @dress: the new line dress
208 * Sets a new line dress on @ruled_fill.
211 adg_ruled_fill_set_line_dress(AdgRuledFill
*ruled_fill
, AdgDress dress
)
213 g_return_if_fail(ADG_IS_RULED_FILL(ruled_fill
));
214 g_object_set((GObject
*) ruled_fill
, "line-dress", dress
, NULL
);
218 * adg_ruled_fill_get_line_dress:
219 * @ruled_fill: an #AdgRuledFill object
221 * Gets the @ruled_fill dress to be used for rendering the lines.
223 * Returns: the line dress
226 adg_ruled_fill_get_line_dress(AdgRuledFill
*ruled_fill
)
228 AdgRuledFillPrivate
*data
;
230 g_return_val_if_fail(ADG_IS_RULED_FILL(ruled_fill
), ADG_DRESS_UNDEFINED
);
232 data
= ruled_fill
->data
;
234 return data
->line_dress
;
238 * adg_ruled_fill_set_spacing:
239 * @ruled_fill: an #AdgRuledFill
240 * @spacing: the new spacing
242 * Sets a new spacing on @ruled_fill.
245 adg_ruled_fill_set_spacing(AdgRuledFill
*ruled_fill
, gdouble spacing
)
247 g_return_if_fail(ADG_IS_RULED_FILL(ruled_fill
));
249 if (set_spacing(ruled_fill
, spacing
))
250 g_object_notify((GObject
*) ruled_fill
, "spacing");
254 * adg_ruled_fill_get_spacing:
255 * @ruled_fill: an #AdgRuledFill
257 * Gets the current spacing of @ruled_fill.
259 * Returns: the spacing (in global space)
262 adg_ruled_fill_get_spacing(AdgRuledFill
*ruled_fill
)
264 AdgRuledFillPrivate
*data
;
266 g_return_val_if_fail(ADG_IS_RULED_FILL(ruled_fill
), 0);
268 data
= ruled_fill
->data
;
270 return data
->spacing
;
274 * adg_ruled_fill_set_angle:
275 * @ruled_fill: an #AdgRuledFill
276 * @angle: the new angle
278 * Sets a new angle on @ruled_fill.
281 adg_ruled_fill_set_angle(AdgRuledFill
*ruled_fill
, gdouble angle
)
283 g_return_if_fail(ADG_IS_RULED_FILL(ruled_fill
));
285 if (set_angle(ruled_fill
, angle
))
286 g_object_notify((GObject
*) ruled_fill
, "angle");
290 * adg_ruled_fill_get_angle:
291 * @ruled_fill: an #AdgRuledFill
293 * Gets the current angle of @ruled_fill.
295 * Returns: the angle (in radians)
298 adg_ruled_fill_get_angle(AdgRuledFill
*ruled_fill
)
300 AdgRuledFillPrivate
*data
;
302 g_return_val_if_fail(ADG_IS_RULED_FILL(ruled_fill
), 0);
304 data
= ruled_fill
->data
;
311 apply(AdgStyle
*style
, AdgEntity
*entity
, cairo_t
*cr
)
313 AdgFillStyle
*fill_style
;
315 const CpmlExtents
*extents
;
316 cairo_matrix_t matrix
;
318 fill_style
= (AdgFillStyle
*) style
;
319 pattern
= adg_fill_style_get_pattern(fill_style
);
320 extents
= adg_fill_style_get_extents(fill_style
);
322 if (pattern
== NULL
) {
323 pattern
= create_pattern((AdgRuledFill
*) style
, entity
, cr
);
327 adg_fill_style_set_pattern(fill_style
, pattern
);
328 cairo_pattern_destroy(pattern
);
331 /* The extents are yet in identity space, so the ctm should be
332 * reset to avoid using local and global maps twice: only the
333 * translation (also in identity space) is applied */
334 cairo_matrix_init_translate(&matrix
, extents
->org
.x
, extents
->org
.y
);
335 cairo_set_matrix(cr
, &matrix
);
337 if (PARENT_STYLE_CLASS
->apply
)
338 PARENT_STYLE_CLASS
->apply(style
, entity
, cr
);
342 set_extents(AdgFillStyle
*fill_style
, const CpmlExtents
*extents
)
344 CpmlExtents old
, new;
346 cpml_extents_copy(&old
, adg_fill_style_get_extents(fill_style
));
348 /* The pattern is invalidated (and thus regenerated) only
349 * when the new extents are wider than the old ones */
350 if (old
.size
.x
>= extents
->size
.x
&& old
.size
.y
>= extents
->size
.y
) {
351 new.org
= extents
->org
;
354 cpml_extents_copy(&new, extents
);
355 adg_fill_style_set_pattern(fill_style
, NULL
);
358 if (PARENT_FILL_STYLE_CLASS
->set_extents
)
359 PARENT_FILL_STYLE_CLASS
->set_extents(fill_style
, &new);
363 set_spacing(AdgRuledFill
*ruled_fill
, gdouble spacing
)
365 AdgRuledFillPrivate
*data
= ruled_fill
->data
;
367 if (spacing
== data
->spacing
)
370 data
->spacing
= spacing
;
371 adg_fill_style_set_pattern((AdgFillStyle
*) ruled_fill
, NULL
);
377 set_angle(AdgRuledFill
*ruled_fill
, gdouble angle
)
379 AdgRuledFillPrivate
*data
= ruled_fill
->data
;
381 if (angle
== data
->angle
)
385 adg_fill_style_set_pattern((AdgFillStyle
*) ruled_fill
, NULL
);
390 static cairo_pattern_t
*
391 create_pattern(AdgRuledFill
*ruled_fill
, AdgEntity
*entity
, cairo_t
*cr
)
393 AdgFillStyle
*fill_style
;
394 const CpmlExtents
*extents
;
395 AdgRuledFillPrivate
*data
;
396 AdgStyle
*line_style
;
397 cairo_pattern_t
*pattern
;
398 cairo_surface_t
*surface
;
402 fill_style
= (AdgFillStyle
*) ruled_fill
;
403 extents
= adg_fill_style_get_extents(fill_style
);
405 /* Check for valid extents */
406 if (!extents
->is_defined
)
409 data
= ruled_fill
->data
;
410 line_style
= adg_entity_style(entity
, data
->line_dress
);
411 surface
= cairo_surface_create_similar(cairo_get_target(cr
),
412 CAIRO_CONTENT_COLOR_ALPHA
,
413 extents
->size
.x
, extents
->size
.y
);
414 pattern
= cairo_pattern_create_for_surface(surface
);
416 /* The pattern holds a reference to the surface, so
417 * there is no need to hold another reference here */
418 cairo_surface_destroy(surface
);
420 spacing
.x
= cos(data
->angle
) * data
->spacing
;
421 spacing
.y
= sin(data
->angle
) * data
->spacing
;
423 context
= cairo_create(surface
);
424 adg_style_apply(line_style
, entity
, context
);
425 draw_lines(&spacing
, &extents
->size
, context
);
426 cairo_destroy(context
);
432 draw_lines(const CpmlPair
*spacing
, const CpmlPair
*size
, cairo_t
*cr
)
434 CpmlPair step
, step1
, step2
;
437 /* There should be some sort of spacing and a destination area */
438 if ((spacing
->x
== 0 && spacing
->y
== 0) ||
439 (size
->x
<= 0 && size
->y
<= 0))
442 /* Revert spacings if needed to inspect only the x >= 0 cases */
443 cpml_pair_copy(&step
, spacing
);
444 if (spacing
->x
< 0 || (spacing
->x
== 0 && spacing
->y
< 0)) {
451 p1
.y
= step
.y
== 0 ? p2
.y
: 0;
452 p2
.x
= step
.x
== 0 ? p1
.x
: 0;
465 while (p1
.x
< size
->x
) {
466 if (p2
.y
<= 0 || p2
.y
>= size
->y
) {
470 cairo_move_to(cr
, p1
.x
, p1
.y
);
471 cairo_line_to(cr
, p2
.x
, p2
.y
);
483 while (p1
.y
>= 0 && p1
.y
<= size
->y
) {
484 if (p2
.y
<= 0 || p2
.y
>= size
->y
) {
488 cairo_move_to(cr
, p1
.x
, p1
.y
);
489 cairo_line_to(cr
, p2
.x
, p2
.y
);