doc: update copyright line for 2019
[adg.git] / src / adg / adg-dress.c
blobed6e96241553167b388e2490dd10fa9f2dd43151
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2019 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-dress
23 * @Section_Id:Dresses
24 * @title: AdgDress
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.
33 * Since: 1.0
34 **/
36 /**
37 * AdgDress:
38 * @ADG_DRESS_UNDEFINED: undefined dress, used for notifying invalid
39 * dresses.
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
58 * instance.
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
62 * instance.
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
66 * instance.
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
70 * instance.
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
95 * instance.
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
108 * instance.
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
122 * instance.
123 * @ADG_DRESS_DIMENSION: default built-in for dimensions. This
124 * dress will be resolved to an #AdgDimStyle
125 * instance.
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
134 * instance.
135 * @ADG_DRESS_TABLE: default built-in for tables. This dress
136 * will be resolved to an #AdgTableStyle
137 * derived instance.
139 * An index representing a virtual #AdgStyle. The ADG comes equipped
140 * with some built-in dress.
142 * Since: 1.0
146 * ADG_TYPE_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).
151 * Since: 1.0
155 * ADG_VALUE_HOLDS_DRESS:
156 * @value: a #GValue
158 * Checks whether a #GValue is actually holding an #AdgDress value or not.
160 * Returns: <constant>TRUE</constant> is @value is holding an #AdgDress, <constant>FALSE</constant> otherwise.
162 * Since: 1.0
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,
189 AdgStyle *fallback,
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.
204 * Since: 1.0
206 AdgDress
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: <constant>TRUE</constant> if the dresses are related, <constant>FALSE</constant> otherwise.
226 * Since: 1.0
228 gboolean
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)
235 return FALSE;
237 ancestor_type2 = adg_dress_get_ancestor_type(dress2);
238 if (ancestor_type2 <= 0)
239 return FALSE;
241 return ancestor_type1 == ancestor_type2;
245 * adg_dress_set:
246 * @dress: a pointer to an #AdgDress
247 * @src: the source dress
249 * Copies @src in @dress. This operation can be successful only if
250 * @dress is #ADG_DRESS_UNDEFINED or if it contains a dress related
251 * to @src, i.e. adg_dress_are_related() returns <constant>TRUE</constant>.
253 * Returns: <constant>TRUE</constant> on copy done, <constant>FALSE</constant> on copy failed or not needed.
255 * Since: 1.0
257 gboolean
258 adg_dress_set(AdgDress *dress, AdgDress src)
260 if (*dress == src)
261 return FALSE;
263 if (*dress != ADG_DRESS_UNDEFINED && !adg_dress_are_related(*dress, src))
264 return FALSE;
266 *dress = src;
267 return TRUE;
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 <constant>NULL</constant> if not found.
279 * Since: 1.0
281 const gchar *
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
296 * is not found.
298 * Returns: the ancestor type or 0 on errors.
300 * Since: 1.0
302 GType
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 is
315 * not valid, a warning message is raised and the function fails.
317 * @fallback is checked for compatibily with @dress. Any dress holds
318 * an ancestor type: if this type is not found in the @fallback
319 * hierarchy, a warning message is raised and the function fails.
321 * After a succesfull call, the reference to the previous fallback
322 * (if any) is dropped while a new reference to @fallback is added.
324 * Since: 1.0
326 void
327 adg_dress_set_fallback(AdgDress dress, AdgStyle *fallback)
329 AdgDressPrivate *data = _adg_data_lookup(dress);
331 g_return_if_fail(data != NULL);
333 if (data->fallback == fallback)
334 return;
336 /* Check if the new fallback style is compatible with this dress */
337 if (fallback != NULL && !adg_dress_style_is_compatible(dress, fallback)) {
338 g_warning(_("%s: the fallback style of '%s' dress (%d) must be a '%s' derived type, but a '%s' has been provided"),
339 G_STRLOC, adg_dress_get_name(dress), dress,
340 g_type_name(data->ancestor_type),
341 g_type_name(G_TYPE_FROM_INSTANCE(fallback)));
342 return;
345 if (data->fallback != NULL)
346 g_object_unref(data->fallback);
348 data->fallback = fallback;
350 if (data->fallback != NULL)
351 g_object_ref(data->fallback);
355 * adg_dress_get_fallback:
356 * @dress: an #AdgDress
358 * Gets the fallback style associated to @dress. No warnings
359 * are raised if the dress is not found. The returned style
360 * is owned by dress and should not be freed or modified.
362 * Returns: (transfer none): the requested #AdgStyle derived instance or <constant>NULL</constant> if not set.
364 * Since: 1.0
366 AdgStyle *
367 adg_dress_get_fallback(AdgDress dress)
369 AdgDressPrivate *data = _adg_data_lookup(dress);
370 return data != NULL ? data->fallback : NULL;
374 * adg_dress_style_is_compatible:
375 * @dress: an #AdgDress
376 * @style: (transfer none): the #AdgStyle to check
378 * Checks whether @style is compatible with @dress, that is if
379 * @style has the ancestor style type (as returned by
380 * adg_dress_get_ancestor_type()) in its hierarchy.
382 * Returns: <constant>TRUE</constant> if @dress can accept @style, <constant>FALSE</constant> otherwise.
384 * Since: 1.0
386 gboolean
387 adg_dress_style_is_compatible(AdgDress dress, AdgStyle *style)
389 GType ancestor_type = adg_dress_get_ancestor_type(dress);
391 g_return_val_if_fail(ancestor_type > 0, FALSE);
392 g_return_val_if_fail(ADG_IS_STYLE(style), FALSE);
394 return G_TYPE_CHECK_INSTANCE_TYPE(style, ancestor_type);
398 static GArray *
399 _adg_data_array(void)
401 /* The following register keeps track of the metadata bound to every
402 * #AdgDress value, such as the fallback style and the ancestor type.
404 * The AdgDress value is cohincident with the index of its metadata
405 * inside this register, that is if %ADG_DRESS_COLOR_BACKGROUND is 2,
406 * array->data[2] will contain its metadata.
408 static GArray *array = NULL;
410 if (G_UNLIKELY(array == NULL)) {
411 array = g_array_new(FALSE, FALSE, sizeof(AdgDressPrivate));
412 _adg_data_register_builtins();
415 return array;
418 static void
419 _adg_data_register(AdgDress dress, AdgStyle *fallback, GType ancestor_type)
421 GArray *array = _adg_data_array();
422 AdgDressPrivate data;
424 data.fallback = fallback;
425 data.ancestor_type = ancestor_type;
427 g_array_insert_vals(array, dress, g_memdup(&data, sizeof(data)), 1);
430 static void
431 _adg_data_register_builtins(void)
433 AdgDash *dash;
434 AdgMarker *arrow1, *arrow2;
436 _adg_data_register(ADG_DRESS_UNDEFINED,
437 NULL,
438 G_TYPE_INVALID);
441 /* Predefined colors */
443 _adg_data_register(ADG_DRESS_COLOR,
444 NULL,
445 ADG_TYPE_COLOR_STYLE);
447 _adg_data_register(ADG_DRESS_COLOR_BACKGROUND,
448 g_object_new(ADG_TYPE_COLOR_STYLE,
449 "blue", 1.,
450 "green", 1.,
451 "red", 1.,
452 NULL),
453 ADG_TYPE_COLOR_STYLE);
455 _adg_data_register(ADG_DRESS_COLOR_STROKE,
456 g_object_new(ADG_TYPE_COLOR_STYLE,
457 NULL),
458 ADG_TYPE_COLOR_STYLE);
460 _adg_data_register(ADG_DRESS_COLOR_DIMENSION,
461 g_object_new(ADG_TYPE_COLOR_STYLE,
462 "red", 0.,
463 "green", 0.4,
464 "blue", 0.6,
465 NULL),
466 ADG_TYPE_COLOR_STYLE);
468 _adg_data_register(ADG_DRESS_COLOR_ANNOTATION,
469 g_object_new(ADG_TYPE_COLOR_STYLE,
470 "red", 0.4,
471 "green", 0.4,
472 "blue", 0.2,
473 NULL),
474 ADG_TYPE_COLOR_STYLE);
476 _adg_data_register(ADG_DRESS_COLOR_FILL,
477 g_object_new(ADG_TYPE_COLOR_STYLE,
478 "red", 0.25,
479 "green", 0.25,
480 "blue", 0.25,
481 NULL),
482 ADG_TYPE_COLOR_STYLE);
484 _adg_data_register(ADG_DRESS_COLOR_AXIS,
485 g_object_new(ADG_TYPE_COLOR_STYLE,
486 "red", 0.,
487 "green", 0.75,
488 "blue", 0.25,
489 NULL),
490 ADG_TYPE_COLOR_STYLE);
492 _adg_data_register(ADG_DRESS_COLOR_HIDDEN,
493 g_object_new(ADG_TYPE_COLOR_STYLE,
494 "red", 0.5,
495 "green", 0.5,
496 "blue", 0.5,
497 NULL),
498 ADG_TYPE_COLOR_STYLE);
501 /* Predefined lines */
503 _adg_data_register(ADG_DRESS_LINE,
504 NULL,
505 ADG_TYPE_LINE_STYLE);
507 _adg_data_register(ADG_DRESS_LINE_STROKE,
508 g_object_new(ADG_TYPE_LINE_STYLE,
509 "color-dress", ADG_DRESS_COLOR_STROKE,
510 "width", 1.5,
511 NULL),
512 ADG_TYPE_LINE_STYLE);
514 _adg_data_register(ADG_DRESS_LINE_DIMENSION,
515 g_object_new(ADG_TYPE_LINE_STYLE,
516 "width", 0.5,
517 NULL),
518 ADG_TYPE_LINE_STYLE);
520 _adg_data_register(ADG_DRESS_LINE_FILL,
521 g_object_new(ADG_TYPE_LINE_STYLE,
522 "color-dress", ADG_DRESS_COLOR_FILL,
523 "width", 0.5,
524 NULL),
525 ADG_TYPE_LINE_STYLE);
527 _adg_data_register(ADG_DRESS_LINE_GRID,
528 g_object_new(ADG_TYPE_LINE_STYLE,
529 "antialias", CAIRO_ANTIALIAS_NONE,
530 "width", 1.,
531 NULL),
532 ADG_TYPE_LINE_STYLE);
534 _adg_data_register(ADG_DRESS_LINE_FRAME,
535 g_object_new(ADG_TYPE_LINE_STYLE,
536 "color-dress", ADG_DRESS_COLOR_ANNOTATION,
537 "antialias", CAIRO_ANTIALIAS_NONE,
538 "width", 2.,
539 NULL),
540 ADG_TYPE_LINE_STYLE);
543 dash = adg_dash_new_with_dashes(4, 2 MM, 2 MM, 10 MM, 2 MM);
544 _adg_data_register(ADG_DRESS_LINE_AXIS,
545 g_object_new(ADG_TYPE_LINE_STYLE,
546 "color-dress", ADG_DRESS_COLOR_AXIS,
547 "width", 0.25 MM,
548 "dash", dash,
549 NULL),
550 ADG_TYPE_LINE_STYLE);
551 adg_dash_destroy(dash);
553 dash = adg_dash_new_with_dashes(2, 6 MM, 3 MM);
554 _adg_data_register(ADG_DRESS_LINE_HIDDEN,
555 g_object_new(ADG_TYPE_LINE_STYLE,
556 "color-dress", ADG_DRESS_COLOR_HIDDEN,
557 "width", 0.25 MM,
558 "dash", dash,
559 NULL),
560 ADG_TYPE_LINE_STYLE);
561 adg_dash_destroy(dash);
564 /* Predefined fonts */
566 _adg_data_register(ADG_DRESS_FONT,
567 g_object_new(ADG_TYPE_BEST_FONT_STYLE,
568 "family", "Serif",
569 "size", 14.,
570 NULL),
571 ADG_TYPE_BEST_FONT_STYLE);
573 _adg_data_register(ADG_DRESS_FONT_TEXT,
574 g_object_new(ADG_TYPE_BEST_FONT_STYLE,
575 "color-dress", ADG_DRESS_COLOR_ANNOTATION,
576 "family", "Sans",
577 "weight", CAIRO_FONT_WEIGHT_BOLD,
578 "size", 12.,
579 NULL),
580 ADG_TYPE_BEST_FONT_STYLE);
582 _adg_data_register(ADG_DRESS_FONT_ANNOTATION,
583 g_object_new(ADG_TYPE_BEST_FONT_STYLE,
584 "color-dress", ADG_DRESS_COLOR_ANNOTATION,
585 "family", "Sans",
586 "size", 8.,
587 NULL),
588 ADG_TYPE_BEST_FONT_STYLE);
590 _adg_data_register(ADG_DRESS_FONT_QUOTE_TEXT,
591 g_object_new(ADG_TYPE_BEST_FONT_STYLE,
592 "family", "Sans",
593 "weight", CAIRO_FONT_WEIGHT_BOLD,
594 "size", 12.,
595 NULL),
596 ADG_TYPE_BEST_FONT_STYLE);
598 _adg_data_register(ADG_DRESS_FONT_QUOTE_ANNOTATION,
599 g_object_new(ADG_TYPE_BEST_FONT_STYLE,
600 "family", "Sans",
601 "size", 8.,
602 NULL),
603 ADG_TYPE_BEST_FONT_STYLE);
606 /* Predefined dimension styles */
608 arrow1 = (AdgMarker *) adg_arrow_new();
609 arrow2 = (AdgMarker *) adg_arrow_new();
610 adg_marker_set_pos(arrow2, 1);
611 _adg_data_register(ADG_DRESS_DIMENSION,
612 g_object_new(ADG_TYPE_DIM_STYLE,
613 "marker1", arrow1,
614 "marker2", arrow2,
615 NULL),
616 ADG_TYPE_DIM_STYLE);
617 g_object_unref(arrow1);
618 g_object_unref(arrow2);
621 /* Predefined fill styles */
623 _adg_data_register(ADG_DRESS_FILL,
624 NULL,
625 ADG_TYPE_FILL_STYLE);
627 _adg_data_register(ADG_DRESS_FILL_HATCH,
628 g_object_new(ADG_TYPE_RULED_FILL,
629 "line-dress", ADG_DRESS_LINE_FILL,
630 NULL),
631 ADG_TYPE_FILL_STYLE);
634 /* Predefined table styles */
636 _adg_data_register(ADG_DRESS_TABLE,
637 g_object_new(ADG_TYPE_TABLE_STYLE, NULL),
638 ADG_TYPE_TABLE_STYLE);
641 static AdgDressPrivate *
642 _adg_data_lookup(AdgDress dress)
644 GArray *array = _adg_data_array();
646 if (dress >= array->len)
647 return NULL;
649 return ((AdgDressPrivate *) array->data) + dress;