1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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_DIMENSION_ANGULAR: default built-in for angular dimensions.
127 * This dress will be resolved to an
128 * #AdgDimStyle instance.
129 * @ADG_DRESS_DIMENSION_RADIUS: default built-in for radius dimensions.
130 * This dress will be resolved to an
131 * #AdgDimStyle instance.
132 * @ADG_DRESS_DIMENSION_DIAMETER: default built-in for diameter dimensions.
133 * This dress will be resolved to an
134 * #AdgDimStyle instance.
135 * @ADG_DRESS_FILL: default built-in for filling. This is a
136 * pass-through dress, that is it does not
137 * change the cairo context when it is
138 * applied. This dress will be resolved to an
139 * #AdgFillStyle derived instance.
140 * @ADG_DRESS_FILL_HATCH: built-in dress used by default by
141 * #AdgHatch instances. This dress will be
142 * resolved to an #AdgFillStyle derived
144 * @ADG_DRESS_TABLE: default built-in for tables. This dress
145 * will be resolved to an #AdgTableStyle
148 * An index representing a virtual #AdgStyle. The ADG comes equipped
149 * with some built-in dress.
157 * The type used to express a dress index. It is defined only for GObject
158 * internal and should not be used directly (at least, as far as I know).
164 * ADG_VALUE_HOLDS_DRESS:
167 * Checks whether a #GValue is actually holding an #AdgDress value or not.
169 * Returns: <constant>TRUE</constant> is @value is holding an #AdgDress, <constant>FALSE</constant> otherwise.
175 #include "adg-internal.h"
176 #include "adg-dash.h"
177 #include "adg-model.h"
178 #include "adg-trail.h"
179 #include "adg-marker.h"
180 #include "adg-arrow.h"
181 #include "adg-style.h"
182 #include "adg-color-style.h"
183 #include "adg-line-style.h"
184 #include "adg-text-internal.h"
185 #include "adg-dim-style.h"
186 #include "adg-fill-style.h"
187 #include "adg-ruled-fill.h"
188 #include "adg-table-style.h"
190 #include "adg-dress.h"
191 #include "adg-dress-private.h"
193 #define MM *2.83464566927
196 static GArray
* _adg_data_array (void);
197 static void _adg_data_register (AdgDress dress
,
199 GType ancestor_type
);
200 static void _adg_data_register_builtins (void);
201 static AdgDressPrivate
*_adg_data_lookup (AdgDress dress
);
205 * adg_dress_from_name:
206 * @name: the name of a dress
208 * Gets the dress bound to a @name string. No warnings are raised
209 * if the dress is not found.
211 * Returns: the #AdgDress value or #ADG_DRESS_UNDEFINED if not found.
216 adg_dress_from_name(const gchar
*name
)
218 GEnumClass
*dress_class
= g_type_class_ref(ADG_TYPE_DRESS
);
219 GEnumValue
*enum_value
= g_enum_get_value_by_name(dress_class
, name
);
220 g_type_class_unref(dress_class
);
221 return enum_value
!= NULL
? enum_value
->value
: ADG_DRESS_UNDEFINED
;
225 * adg_dress_are_related:
226 * @dress1: an #AdgDress
227 * @dress2: another #AdgDress
229 * Checks whether @dress1 and @dress2 are related, that is
230 * if they have the same ancestor type as returned by
231 * adg_dress_get_ancestor_type().
233 * Returns: <constant>TRUE</constant> if the dresses are related, <constant>FALSE</constant> otherwise.
238 adg_dress_are_related(AdgDress dress1
, AdgDress dress2
)
240 GType ancestor_type1
, ancestor_type2
;
242 ancestor_type1
= adg_dress_get_ancestor_type(dress1
);
243 if (ancestor_type1
<= 0)
246 ancestor_type2
= adg_dress_get_ancestor_type(dress2
);
247 if (ancestor_type2
<= 0)
250 return ancestor_type1
== ancestor_type2
;
255 * @dress: a pointer to an #AdgDress
256 * @src: the source dress
258 * Copies @src in @dress. This operation can be successful only if
259 * @dress is #ADG_DRESS_UNDEFINED or if it contains a dress related
260 * to @src, i.e. adg_dress_are_related() returns <constant>TRUE</constant>.
262 * Returns: <constant>TRUE</constant> on copy done, <constant>FALSE</constant> on copy failed or not needed.
267 adg_dress_set(AdgDress
*dress
, AdgDress src
)
272 if (*dress
!= ADG_DRESS_UNDEFINED
&& !adg_dress_are_related(*dress
, src
))
280 * adg_dress_get_name:
281 * @dress: an #AdgDress
283 * Gets the name associated to @dress. No warnings are raised if
284 * @dress is not found.
286 * Returns: the requested name or <constant>NULL</constant> if not found.
291 adg_dress_get_name(AdgDress dress
)
293 GEnumClass
*dress_class
= g_type_class_ref(ADG_TYPE_DRESS
);
294 GEnumValue
*enum_value
= g_enum_get_value(dress_class
, dress
);
295 g_type_class_unref(dress_class
);
296 return enum_value
!= NULL
? enum_value
->value_name
: NULL
;
300 * adg_dress_get_ancestor_type:
301 * @dress: an #AdgDress
303 * Gets the base type that should be present in every #AdgStyle
304 * acceptable by @dress. No warnings are raised if @dress
307 * Returns: the ancestor type or 0 on errors.
312 adg_dress_get_ancestor_type(AdgDress dress
)
314 AdgDressPrivate
*data
= _adg_data_lookup(dress
);
315 return data
!= NULL
? data
->ancestor_type
: 0;
319 * adg_dress_set_fallback:
320 * @dress: an #AdgDress
321 * @fallback: (transfer full): the new fallback style
323 * Associates a new @fallback style to @dress. If the dress is
324 * not valid, a warning message is raised and the function fails.
326 * @fallback is checked for compatibily with @dress. Any dress holds
327 * an ancestor type: if this type is not found in the @fallback
328 * hierarchy, a warning message is raised and the function fails.
330 * After a succesfull call, the reference to the previous fallback
331 * (if any) is dropped while a new reference to @fallback is added.
336 adg_dress_set_fallback(AdgDress dress
, AdgStyle
*fallback
)
338 AdgDressPrivate
*data
= _adg_data_lookup(dress
);
340 g_return_if_fail(data
!= NULL
);
342 if (data
->fallback
== fallback
)
345 /* Check if the new fallback style is compatible with this dress */
346 if (fallback
!= NULL
&& !adg_dress_style_is_compatible(dress
, fallback
)) {
347 g_warning(_("%s: the fallback style of '%s' dress (%d) must be a '%s' derived type, but a '%s' has been provided"),
348 G_STRLOC
, adg_dress_get_name(dress
), dress
,
349 g_type_name(data
->ancestor_type
),
350 g_type_name(G_TYPE_FROM_INSTANCE(fallback
)));
354 if (data
->fallback
!= NULL
)
355 g_object_unref(data
->fallback
);
357 data
->fallback
= fallback
;
359 if (data
->fallback
!= NULL
)
360 g_object_ref(data
->fallback
);
364 * adg_dress_get_fallback:
365 * @dress: an #AdgDress
367 * Gets the fallback style associated to @dress. No warnings
368 * are raised if the dress is not found. The returned style
369 * is owned by dress and should not be freed or modified.
371 * Returns: (transfer none): the requested #AdgStyle derived instance or <constant>NULL</constant> if not set.
376 adg_dress_get_fallback(AdgDress dress
)
378 AdgDressPrivate
*data
= _adg_data_lookup(dress
);
379 return data
!= NULL
? data
->fallback
: NULL
;
383 * adg_dress_style_is_compatible:
384 * @dress: an #AdgDress
385 * @style: (transfer none): the #AdgStyle to check
387 * Checks whether @style is compatible with @dress, that is if
388 * @style has the ancestor style type (as returned by
389 * adg_dress_get_ancestor_type()) in its hierarchy.
391 * Returns: <constant>TRUE</constant> if @dress can accept @style, <constant>FALSE</constant> otherwise.
396 adg_dress_style_is_compatible(AdgDress dress
, AdgStyle
*style
)
398 GType ancestor_type
= adg_dress_get_ancestor_type(dress
);
400 g_return_val_if_fail(ancestor_type
> 0, FALSE
);
401 g_return_val_if_fail(ADG_IS_STYLE(style
), FALSE
);
403 return G_TYPE_CHECK_INSTANCE_TYPE(style
, ancestor_type
);
408 _adg_data_array(void)
410 /* The following register keeps track of the metadata bound to every
411 * #AdgDress value, such as the fallback style and the ancestor type.
413 * The AdgDress value is cohincident with the index of its metadata
414 * inside this register, that is if %ADG_DRESS_COLOR_BACKGROUND is 2,
415 * array->data[2] will contain its metadata.
417 static GArray
*array
= NULL
;
419 if (G_UNLIKELY(array
== NULL
)) {
420 array
= g_array_new(FALSE
, FALSE
, sizeof(AdgDressPrivate
));
421 _adg_data_register_builtins();
428 _adg_data_register(AdgDress dress
, AdgStyle
*fallback
, GType ancestor_type
)
430 GArray
*array
= _adg_data_array();
431 AdgDressPrivate data
;
433 data
.fallback
= fallback
;
434 data
.ancestor_type
= ancestor_type
;
436 g_array_insert_vals(array
, dress
, g_memdup(&data
, sizeof(data
)), 1);
440 _adg_data_register_builtins(void)
443 AdgMarker
*arrow1
, *arrow2
;
445 _adg_data_register(ADG_DRESS_UNDEFINED
,
450 /* Predefined colors */
452 _adg_data_register(ADG_DRESS_COLOR
,
454 ADG_TYPE_COLOR_STYLE
);
456 _adg_data_register(ADG_DRESS_COLOR_BACKGROUND
,
457 g_object_new(ADG_TYPE_COLOR_STYLE
,
462 ADG_TYPE_COLOR_STYLE
);
464 _adg_data_register(ADG_DRESS_COLOR_STROKE
,
465 g_object_new(ADG_TYPE_COLOR_STYLE
,
467 ADG_TYPE_COLOR_STYLE
);
469 _adg_data_register(ADG_DRESS_COLOR_DIMENSION
,
470 g_object_new(ADG_TYPE_COLOR_STYLE
,
475 ADG_TYPE_COLOR_STYLE
);
477 _adg_data_register(ADG_DRESS_COLOR_ANNOTATION
,
478 g_object_new(ADG_TYPE_COLOR_STYLE
,
483 ADG_TYPE_COLOR_STYLE
);
485 _adg_data_register(ADG_DRESS_COLOR_FILL
,
486 g_object_new(ADG_TYPE_COLOR_STYLE
,
491 ADG_TYPE_COLOR_STYLE
);
493 _adg_data_register(ADG_DRESS_COLOR_AXIS
,
494 g_object_new(ADG_TYPE_COLOR_STYLE
,
499 ADG_TYPE_COLOR_STYLE
);
501 _adg_data_register(ADG_DRESS_COLOR_HIDDEN
,
502 g_object_new(ADG_TYPE_COLOR_STYLE
,
507 ADG_TYPE_COLOR_STYLE
);
510 /* Predefined lines */
512 _adg_data_register(ADG_DRESS_LINE
,
514 ADG_TYPE_LINE_STYLE
);
516 _adg_data_register(ADG_DRESS_LINE_STROKE
,
517 g_object_new(ADG_TYPE_LINE_STYLE
,
518 "color-dress", ADG_DRESS_COLOR_STROKE
,
521 ADG_TYPE_LINE_STYLE
);
523 _adg_data_register(ADG_DRESS_LINE_DIMENSION
,
524 g_object_new(ADG_TYPE_LINE_STYLE
,
527 ADG_TYPE_LINE_STYLE
);
529 _adg_data_register(ADG_DRESS_LINE_FILL
,
530 g_object_new(ADG_TYPE_LINE_STYLE
,
531 "color-dress", ADG_DRESS_COLOR_FILL
,
534 ADG_TYPE_LINE_STYLE
);
536 _adg_data_register(ADG_DRESS_LINE_GRID
,
537 g_object_new(ADG_TYPE_LINE_STYLE
,
538 "antialias", CAIRO_ANTIALIAS_NONE
,
541 ADG_TYPE_LINE_STYLE
);
543 _adg_data_register(ADG_DRESS_LINE_FRAME
,
544 g_object_new(ADG_TYPE_LINE_STYLE
,
545 "color-dress", ADG_DRESS_COLOR_ANNOTATION
,
546 "antialias", CAIRO_ANTIALIAS_NONE
,
549 ADG_TYPE_LINE_STYLE
);
552 dash
= adg_dash_new_with_dashes(4, 2 MM
, 2 MM
, 10 MM
, 2 MM
);
553 _adg_data_register(ADG_DRESS_LINE_AXIS
,
554 g_object_new(ADG_TYPE_LINE_STYLE
,
555 "color-dress", ADG_DRESS_COLOR_AXIS
,
559 ADG_TYPE_LINE_STYLE
);
560 adg_dash_destroy(dash
);
562 dash
= adg_dash_new_with_dashes(2, 6 MM
, 6 MM
);
563 _adg_data_register(ADG_DRESS_LINE_HIDDEN
,
564 g_object_new(ADG_TYPE_LINE_STYLE
,
565 "color-dress", ADG_DRESS_COLOR_HIDDEN
,
569 ADG_TYPE_LINE_STYLE
);
570 adg_dash_destroy(dash
);
573 /* Predefined fonts */
575 _adg_data_register(ADG_DRESS_FONT
,
576 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
580 ADG_TYPE_BEST_FONT_STYLE
);
582 _adg_data_register(ADG_DRESS_FONT_TEXT
,
583 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
584 "color-dress", ADG_DRESS_COLOR_ANNOTATION
,
586 "weight", CAIRO_FONT_WEIGHT_BOLD
,
589 ADG_TYPE_BEST_FONT_STYLE
);
591 _adg_data_register(ADG_DRESS_FONT_ANNOTATION
,
592 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
593 "color-dress", ADG_DRESS_COLOR_ANNOTATION
,
597 ADG_TYPE_BEST_FONT_STYLE
);
599 _adg_data_register(ADG_DRESS_FONT_QUOTE_TEXT
,
600 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
602 "weight", CAIRO_FONT_WEIGHT_BOLD
,
605 ADG_TYPE_BEST_FONT_STYLE
);
607 _adg_data_register(ADG_DRESS_FONT_QUOTE_ANNOTATION
,
608 g_object_new(ADG_TYPE_BEST_FONT_STYLE
,
612 ADG_TYPE_BEST_FONT_STYLE
);
615 /* Predefined dimension styles */
617 arrow1
= (AdgMarker
*) adg_arrow_new();
618 arrow2
= (AdgMarker
*) adg_arrow_new();
619 adg_marker_set_pos(arrow2
, 1);
620 _adg_data_register(ADG_DRESS_DIMENSION
,
621 g_object_new(ADG_TYPE_DIM_STYLE
,
626 g_object_unref(arrow1
);
627 g_object_unref(arrow2
);
630 /* Predefined angular dimension styles */
632 arrow1
= (AdgMarker
*) adg_arrow_new();
633 arrow2
= (AdgMarker
*) adg_arrow_new();
634 adg_marker_set_pos(arrow2
, 1);
635 _adg_data_register(ADG_DRESS_DIMENSION_ANGULAR
,
636 g_object_new(ADG_TYPE_DIM_STYLE
,
641 "number-arguments", "Dm",
642 "number-format", "%g°(%g')",
645 g_object_unref(arrow1
);
646 g_object_unref(arrow2
);
649 /* Predefined radius dimension styles */
651 arrow1
= (AdgMarker
*) adg_arrow_new();
652 arrow2
= (AdgMarker
*) adg_arrow_new();
653 adg_marker_set_pos(arrow2
, 1);
654 _adg_data_register(ADG_DRESS_DIMENSION_RADIUS
,
655 g_object_new(ADG_TYPE_DIM_STYLE
,
658 "number-arguments", "d",
659 "number-format", "R%g",
662 g_object_unref(arrow1
);
663 g_object_unref(arrow2
);
666 /* Predefined diameter dimension styles */
668 arrow1
= (AdgMarker
*) adg_arrow_new();
669 arrow2
= (AdgMarker
*) adg_arrow_new();
670 adg_marker_set_pos(arrow2
, 1);
671 _adg_data_register(ADG_DRESS_DIMENSION_DIAMETER
,
672 g_object_new(ADG_TYPE_DIM_STYLE
,
675 "number-arguments", "d",
676 "number-format", ADG_UTF8_DIAMETER
"%g",
679 g_object_unref(arrow1
);
680 g_object_unref(arrow2
);
683 /* Predefined fill styles */
685 _adg_data_register(ADG_DRESS_FILL
,
687 ADG_TYPE_FILL_STYLE
);
689 _adg_data_register(ADG_DRESS_FILL_HATCH
,
690 g_object_new(ADG_TYPE_RULED_FILL
,
691 "line-dress", ADG_DRESS_LINE_FILL
,
693 ADG_TYPE_FILL_STYLE
);
696 /* Predefined table styles */
698 _adg_data_register(ADG_DRESS_TABLE
,
699 g_object_new(ADG_TYPE_TABLE_STYLE
, NULL
),
700 ADG_TYPE_TABLE_STYLE
);
703 static AdgDressPrivate
*
704 _adg_data_lookup(AdgDress dress
)
706 GArray
*array
= _adg_data_array();
708 if (dress
>= array
->len
)
711 return ((AdgDressPrivate
*) array
->data
) + dress
;