1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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.
25 * @short_description: The ADG way to associate styles to entities
27 * The dress is a virtualization of an #AdgStyle instance. #AdgEntity
28 * objects do not directly refer to #AdgStyle but use #AdgDress values
29 * instead. This allows some advanced operations, such as overriding
30 * a dress only in a specific entity branch of the hierarchy or
31 * customize multiple entities at once.
38 * @ADG_DRESS_UNDEFINED: undefined dress, used for notifying invalid
40 * @ADG_DRESS_COLOR: default built-in color. This is a
41 * pass-through dress, that is it does not
42 * change the cairo context when it is
43 * applied. This dress will be resolved to an
44 * #AdgColorStyle instance.
45 * @ADG_DRESS_COLOR_BACKGROUND: default built-in color to be used as the
46 * #AdgCanvas background. This dress will be
47 * resolved to an #AdgColorStyle instance.
48 * @ADG_DRESS_COLOR_STROKE: default built-in color for #AdgStroke
49 * entities. This dress will be resolved to
50 * an #AdgColorStyle instance.
51 * @ADG_DRESS_COLOR_DIMENSION: built-in color used by default in
52 * #AdgDimStyle. This dress will be resolved
53 * to an #AdgColorStyle instance.
54 * @ADG_DRESS_COLOR_ANNOTATION: built-in color used for rendering
55 * helper entities such as #AdgToyText,
56 * #AdgTable and #AdgTitleBlock. This dress
57 * will be resolved to an #AdgColorStyle
59 * @ADG_DRESS_COLOR_FILL: built-in color used by default by
60 * #AdgFillStyle based styles. This dress
61 * will be resolved to an #AdgColorStyle
63 * @ADG_DRESS_COLOR_AXIS: default built-in color for stroking
64 * #ADG_DRESS_LINE_AXIS lines. This dress
65 * will be resolved to an #AdgColorStyle
67 * @ADG_DRESS_COLOR_HIDDEN: default built-in color for stroking
68 * #ADG_DRESS_LINE_HIDDEN lines. This dress
69 * will be resolved to an #AdgColorStyle
71 * @ADG_DRESS_LINE: default built-in line. This is a
72 * pass-through dress, that is it does not
73 * change the cairo context when it is
74 * applied. This dress will be resolved to
75 * an #AdgLineStyle instance.
76 * @ADG_DRESS_LINE_STROKE: built-in line type to be used by default
77 * for rendering #AdgStroke entities.
78 * This dress will be resolved to an
79 * #AdgLineStyle instance.
80 * @ADG_DRESS_LINE_DIMENSION: built-in line type used by default for
81 * rendering base and extension lines of
82 * dimensions. This dress will be resolved
83 * to an #AdgLineStyle instance.
84 * @ADG_DRESS_LINE_FILL: built-in line type used by #AdgFillStyle.
85 * This dress will be resolved to an
86 * #AdgLineStyle instance.
87 * @ADG_DRESS_LINE_GRID: built-in line type used for rendering
88 * the grid of #AdgTable entities, that is
89 * the frame of the cells. This dress will
90 * be resolved to an #AdgLineStyle instance.
91 * @ADG_DRESS_LINE_FRAME: built-in line type used for rendering the
92 * frame of #AdgTable entities, that is the
93 * frame around the whole table. This dress
94 * will be resolved to an #AdgLineStyle
96 * @ADG_DRESS_LINE_AXIS: built-in line type used for rendering axis
97 * and centerlines. This dress will be
98 * resolved to an #AdgLineStyle instance.
99 * @ADG_DRESS_LINE_HIDDEN: built-in line type used for rendering
100 * hidden lines and edges. This dress will be
101 * resolved to an #AdgLineStyle instance.
102 * @ADG_DRESS_FONT: default built-in font. This dress will be
103 * resolved to an #AdgFontStyle instance.
104 * @ADG_DRESS_FONT_TEXT: built-in font used by default for
105 * rendering common text such as #AdgToyText
106 * or the value of #AdgTable entities. This
107 * dress will be resolved to an #AdgFontStyle
109 * @ADG_DRESS_FONT_ANNOTATION: built-in font used for rendering auxiliary
110 * text, such as the titles on #AdgTable
111 * entities. This dress will be resolved to
112 * an #AdgFontStyle instance.
113 * @ADG_DRESS_FONT_QUOTE_TEXT: built-in font used for rendering regular
114 * text on dimension entities, such as the
115 * nominal value and the notes of a quote.
116 * This dress will be resolved to an
117 * #AdgFontStyle instance.
118 * @ADG_DRESS_FONT_QUOTE_ANNOTATION: built-in font used for rendering auxiliary
119 * text on dimension entities, such as the
120 * min and max limits of a quote. This dress
121 * will be resolved to an #AdgFontStyle
123 * @ADG_DRESS_DIMENSION: default built-in for dimensions. This
124 * dress will be resolved to an #AdgDimStyle
126 * @ADG_DRESS_FILL: default built-in for filling. This is a
127 * pass-through dress, that is it does not
128 * change the cairo context when it is
129 * applied. This dress will be resolved to an
130 * #AdgFillStyle derived instance.
131 * @ADG_DRESS_FILL_HATCH: built-in dress used by default by
132 * #AdgHatch instances. This dress will be
133 * resolved to an #AdgFillStyle derived
135 * @ADG_DRESS_TABLE: default built-in for tables. This dress
136 * will be resolved to an #AdgTableStyle
139 * An index representing a virtual #AdgStyle. The ADG comes equipped
140 * with some built-in dress.
148 * The type used to express a dress index. It is defined only for GObject
149 * internal and should not be used directly (at least, as far as I know).
155 * ADG_VALUE_HOLDS_DRESS:
158 * Checks whether a #GValue is actually holding an #AdgDress value or not.
160 * Returns: %TRUE is @value is holding an #AdgDress, %FALSE otherwise
166 #include "adg-internal.h"
167 #include "adg-dash.h"
168 #include "adg-model.h"
169 #include "adg-trail.h"
170 #include "adg-marker.h"
171 #include "adg-arrow.h"
172 #include "adg-style.h"
173 #include "adg-color-style.h"
174 #include "adg-line-style.h"
175 #include "adg-text-internal.h"
176 #include "adg-dim-style.h"
177 #include "adg-fill-style.h"
178 #include "adg-ruled-fill.h"
179 #include "adg-table-style.h"
181 #include "adg-dress.h"
182 #include "adg-dress-private.h"
184 #define MM *2.83464566927
187 static GArray
* _adg_data_array (void);
188 static void _adg_data_register (AdgDress dress
,
190 GType ancestor_type
);
191 static void _adg_data_register_builtins (void);
192 static AdgDressPrivate
*_adg_data_lookup (AdgDress dress
);
196 * adg_dress_from_name:
197 * @name: the name of a dress
199 * Gets the dress bound to a @name string. No warnings are raised
200 * if the dress is not found.
202 * Returns: the #AdgDress value or #ADG_DRESS_UNDEFINED if not found.
207 adg_dress_from_name(const gchar
*name
)
209 GEnumClass
*dress_class
= g_type_class_ref(ADG_TYPE_DRESS
);
210 GEnumValue
*enum_value
= g_enum_get_value_by_name(dress_class
, name
);
211 g_type_class_unref(dress_class
);
212 return enum_value
!= NULL
? enum_value
->value
: ADG_DRESS_UNDEFINED
;
216 * adg_dress_are_related:
217 * @dress1: an #AdgDress
218 * @dress2: another #AdgDress
220 * Checks whether @dress1 and @dress2 are related, that is
221 * if they have the same ancestor type as returned by
222 * adg_dress_get_ancestor_type().
224 * Returns: %TRUE if the dresses are related, %FALSE otherwise.
229 adg_dress_are_related(AdgDress dress1
, AdgDress dress2
)
231 GType ancestor_type1
, ancestor_type2
;
233 ancestor_type1
= adg_dress_get_ancestor_type(dress1
);
234 if (ancestor_type1
<= 0)
237 ancestor_type2
= adg_dress_get_ancestor_type(dress2
);
238 if (ancestor_type2
<= 0)
241 return ancestor_type1
== ancestor_type2
;
246 * @dress: a pointer to an #AdgDress
247 * @src: the source dress
249 * Copies @src in @dress. This operation can be succesful only if
250 * @dress is #ADG_DRESS_UNDEFINED or if it contains a dress related
251 * to @src, that is adg_dress_are_related() returns %TRUE.
253 * Returns: %TRUE on copy done, %FALSE on copy failed or not needed.
258 adg_dress_set(AdgDress
*dress
, AdgDress src
)
263 if (*dress
!= ADG_DRESS_UNDEFINED
&& !adg_dress_are_related(*dress
, src
))
271 * adg_dress_get_name:
272 * @dress: an #AdgDress
274 * Gets the name associated to @dress. No warnings are raised if
275 * @dress is not found.
277 * Returns: the requested name or %NULL if not found.
282 adg_dress_get_name(AdgDress dress
)
284 GEnumClass
*dress_class
= g_type_class_ref(ADG_TYPE_DRESS
);
285 GEnumValue
*enum_value
= g_enum_get_value(dress_class
, dress
);
286 g_type_class_unref(dress_class
);
287 return enum_value
!= NULL
? enum_value
->value_name
: NULL
;
291 * adg_dress_get_ancestor_type:
292 * @dress: an #AdgDress
294 * Gets the base type that should be present in every #AdgStyle
295 * acceptable by @dress. No warnings are raised if @dress
298 * Returns: the ancestor type or %0 on errors.
303 adg_dress_get_ancestor_type(AdgDress dress
)
305 AdgDressPrivate
*data
= _adg_data_lookup(dress
);
306 return data
!= NULL
? data
->ancestor_type
: 0;
310 * adg_dress_set_fallback:
311 * @dress: an #AdgDress
312 * @fallback: (transfer full): the new fallback style
314 * Associates a new @fallback style to @dress. If the dress does
315 * not exist (it was not previously created by adg_dress_new()),
316 * a warning message is raised and the function fails.
318 * @fallback is checked for compatibily with @dress. Any dress holds
319 * an ancestor type: if this type is not found in the @fallback
320 * hierarchy, a warning message is raised and the function fails.
322 * After a succesfull call, the reference to the previous fallback
323 * (if any) is dropped while a new reference to @fallback is added.
328 adg_dress_set_fallback(AdgDress dress
, AdgStyle
*fallback
)
330 AdgDressPrivate
*data
= _adg_data_lookup(dress
);
332 g_return_if_fail(data
!= NULL
);
334 if (data
->fallback
== fallback
)
337 /* Check if the new fallback style is compatible with this dress */
338 if (fallback
!= NULL
&& !adg_dress_style_is_compatible(dress
, fallback
)) {
339 g_warning(_("%s: the fallback style of `%d' (%s) must be a `%s' derived type, but a `%s' has been provided"),
340 G_STRLOC
, dress
, adg_dress_get_name(dress
),
341 g_type_name(data
->ancestor_type
),
342 g_type_name(G_TYPE_FROM_INSTANCE(fallback
)));
346 if (data
->fallback
!= NULL
)
347 g_object_unref(data
->fallback
);
349 data
->fallback
= fallback
;
351 if (data
->fallback
!= NULL
)
352 g_object_ref(data
->fallback
);
356 * adg_dress_get_fallback:
357 * @dress: an #AdgDress
359 * Gets the fallback style associated to @dress. No warnings
360 * are raised if the dress is not found. The returned style
361 * is owned by dress and should not be freed or modified.
363 * Returns: (transfer none): the requested #AdgStyle derived instance or %NULL if not set.
368 adg_dress_get_fallback(AdgDress dress
)
370 AdgDressPrivate
*data
= _adg_data_lookup(dress
);
371 return data
!= NULL
? data
->fallback
: NULL
;
375 * adg_dress_style_is_compatible:
376 * @dress: an #AdgDress
377 * @style: (transfer none): the #AdgStyle to check
379 * Checks whether @style is compatible with @dress, that is if
380 * @style has the ancestor style type (as returned by
381 * adg_dress_get_ancestor_type()) in its hierarchy.
383 * Returns: %TRUE if @dress can accept @style, %FALSE otherwise.
388 adg_dress_style_is_compatible(AdgDress dress
, AdgStyle
*style
)
390 GType ancestor_type
= adg_dress_get_ancestor_type(dress
);
392 g_return_val_if_fail(ancestor_type
> 0, FALSE
);
393 g_return_val_if_fail(ADG_IS_STYLE(style
), FALSE
);
395 return G_TYPE_CHECK_INSTANCE_TYPE(style
, ancestor_type
);
400 _adg_data_array(void)
402 /* The following register keeps track of the metadata bound to every
403 * #AdgDress value, such as the fallback style and the ancestor type.
405 * The AdgDress value is cohincident with the index of its metadata
406 * inside this register, that is if %ADG_DRESS_COLOR_BACKGROUND is 2,
407 * array->data[2] will contain its metadata.
409 static GArray
*array
= NULL
;
411 if (G_UNLIKELY(array
== NULL
)) {
412 array
= g_array_new(FALSE
, FALSE
, sizeof(AdgDressPrivate
));
413 _adg_data_register_builtins();
420 _adg_data_register(AdgDress dress
, AdgStyle
*fallback
, GType ancestor_type
)
422 GArray
*array
= _adg_data_array();
423 AdgDressPrivate data
;
425 data
.fallback
= fallback
;
426 data
.ancestor_type
= ancestor_type
;
428 g_array_insert_vals(array
, dress
, g_memdup(&data
, sizeof(data
)), 1);
432 _adg_data_register_builtins(void)
435 AdgMarker
*arrow1
, *arrow2
;
437 _adg_data_register(ADG_DRESS_UNDEFINED
,
442 /* Predefined colors */
444 _adg_data_register(ADG_DRESS_COLOR
,
446 ADG_TYPE_COLOR_STYLE
);
448 _adg_data_register(ADG_DRESS_COLOR_BACKGROUND
,
449 g_object_new(ADG_TYPE_COLOR_STYLE
,
454 ADG_TYPE_COLOR_STYLE
);
456 _adg_data_register(ADG_DRESS_COLOR_STROKE
,
457 g_object_new(ADG_TYPE_COLOR_STYLE
,
459 ADG_TYPE_COLOR_STYLE
);
461 _adg_data_register(ADG_DRESS_COLOR_DIMENSION
,
462 g_object_new(ADG_TYPE_COLOR_STYLE
,
467 ADG_TYPE_COLOR_STYLE
);
469 _adg_data_register(ADG_DRESS_COLOR_ANNOTATION
,
470 g_object_new(ADG_TYPE_COLOR_STYLE
,
475 ADG_TYPE_COLOR_STYLE
);
477 _adg_data_register(ADG_DRESS_COLOR_FILL
,
478 g_object_new(ADG_TYPE_COLOR_STYLE
,
483 ADG_TYPE_COLOR_STYLE
);
485 _adg_data_register(ADG_DRESS_COLOR_AXIS
,
486 g_object_new(ADG_TYPE_COLOR_STYLE
,
491 ADG_TYPE_COLOR_STYLE
);
493 _adg_data_register(ADG_DRESS_COLOR_HIDDEN
,
494 g_object_new(ADG_TYPE_COLOR_STYLE
,
499 ADG_TYPE_COLOR_STYLE
);
502 /* Predefined lines */
504 _adg_data_register(ADG_DRESS_LINE
,
506 ADG_TYPE_LINE_STYLE
);
508 _adg_data_register(ADG_DRESS_LINE_STROKE
,
509 g_object_new(ADG_TYPE_LINE_STYLE
,
510 "color-dress", ADG_DRESS_COLOR_STROKE
,
513 ADG_TYPE_LINE_STYLE
);
515 _adg_data_register(ADG_DRESS_LINE_DIMENSION
,
516 g_object_new(ADG_TYPE_LINE_STYLE
,
519 ADG_TYPE_LINE_STYLE
);
521 _adg_data_register(ADG_DRESS_LINE_FILL
,
522 g_object_new(ADG_TYPE_LINE_STYLE
,
523 "color-dress", ADG_DRESS_COLOR_FILL
,
526 ADG_TYPE_LINE_STYLE
);
528 _adg_data_register(ADG_DRESS_LINE_GRID
,
529 g_object_new(ADG_TYPE_LINE_STYLE
,
530 "antialias", CAIRO_ANTIALIAS_NONE
,
533 ADG_TYPE_LINE_STYLE
);
535 _adg_data_register(ADG_DRESS_LINE_FRAME
,
536 g_object_new(ADG_TYPE_LINE_STYLE
,
537 "color-dress", ADG_DRESS_COLOR_ANNOTATION
,
538 "antialias", CAIRO_ANTIALIAS_NONE
,
541 ADG_TYPE_LINE_STYLE
);
544 dash
= adg_dash_new_with_dashes(4, 2 MM
, 2 MM
, 10 MM
, 2 MM
);
545 _adg_data_register(ADG_DRESS_LINE_AXIS
,
546 g_object_new(ADG_TYPE_LINE_STYLE
,
547 "color-dress", ADG_DRESS_COLOR_AXIS
,
551 ADG_TYPE_LINE_STYLE
);
552 adg_dash_destroy(dash
);
554 dash
= adg_dash_new_with_dashes(2, 6 MM
, 6 MM
);
555 _adg_data_register(ADG_DRESS_LINE_HIDDEN
,
556 g_object_new(ADG_TYPE_LINE_STYLE
,
557 "color-dress", ADG_DRESS_COLOR_HIDDEN
,
561 ADG_TYPE_LINE_STYLE
);
562 adg_dash_destroy(dash
);
565 /* Predefined fonts */
567 _adg_data_register(ADG_DRESS_FONT
,
568 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
572 ADG_TYPE_BEST_FONT_STYLE
);
574 _adg_data_register(ADG_DRESS_FONT_TEXT
,
575 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
576 "color-dress", ADG_DRESS_COLOR_ANNOTATION
,
578 "weight", CAIRO_FONT_WEIGHT_BOLD
,
581 ADG_TYPE_BEST_FONT_STYLE
);
583 _adg_data_register(ADG_DRESS_FONT_ANNOTATION
,
584 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
585 "color-dress", ADG_DRESS_COLOR_ANNOTATION
,
589 ADG_TYPE_BEST_FONT_STYLE
);
591 _adg_data_register(ADG_DRESS_FONT_QUOTE_TEXT
,
592 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
594 "weight", CAIRO_FONT_WEIGHT_BOLD
,
597 ADG_TYPE_BEST_FONT_STYLE
);
599 _adg_data_register(ADG_DRESS_FONT_QUOTE_ANNOTATION
,
600 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
604 ADG_TYPE_BEST_FONT_STYLE
);
607 /* Predefined dimension styles */
609 arrow1
= (AdgMarker
*) adg_arrow_new();
610 arrow2
= (AdgMarker
*) adg_arrow_new();
611 adg_marker_set_pos(arrow2
, 1);
612 _adg_data_register(ADG_DRESS_DIMENSION
,
613 g_object_new(ADG_TYPE_DIM_STYLE
,
618 g_object_unref(arrow1
);
619 g_object_unref(arrow2
);
622 /* Predefined fill styles */
624 _adg_data_register(ADG_DRESS_FILL
,
626 ADG_TYPE_FILL_STYLE
);
628 _adg_data_register(ADG_DRESS_FILL_HATCH
,
629 g_object_new(ADG_TYPE_RULED_FILL
,
630 "line-dress", ADG_DRESS_LINE_FILL
,
632 ADG_TYPE_FILL_STYLE
);
635 /* Predefined table styles */
637 _adg_data_register(ADG_DRESS_TABLE
,
638 g_object_new(ADG_TYPE_TABLE_STYLE
, NULL
),
639 ADG_TYPE_TABLE_STYLE
);
642 static AdgDressPrivate
*
643 _adg_data_lookup(AdgDress dress
)
645 GArray
*array
= _adg_data_array();
647 if (dress
>= array
->len
)
650 return ((AdgDressPrivate
*) array
->data
) + dress
;