Moved AdgPair to CpmlPair
[adg.git] / src / adg / adg-font-style.c
blob169446b391935fd3c4772aca7fddfcfa623eeaef
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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-font-style
23 * @short_description: Font style related stuff
25 * Contains parameters on how to draw texts such as font family, slanting,
26 * weight, hinting and so on.
28 * Since: 1.0
31 /**
32 * AdgFontStyle:
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-style.h"
43 #include "adg-dress.h"
44 #include "adg-dress-builtins.h"
46 #include "adg-font-style.h"
47 #include "adg-font-style-private.h"
50 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_font_style_parent_class)
53 G_DEFINE_TYPE(AdgFontStyle, adg_font_style, ADG_TYPE_STYLE)
55 enum {
56 PROP_0,
57 PROP_COLOR_DRESS,
58 PROP_FAMILY,
59 PROP_SLANT,
60 PROP_WEIGHT,
61 PROP_SIZE,
62 PROP_ANTIALIAS,
63 PROP_SUBPIXEL_ORDER,
64 PROP_HINT_STYLE,
65 PROP_HINT_METRICS
69 static void _adg_get_property (GObject *object,
70 guint prop_id,
71 GValue *value,
72 GParamSpec *pspec);
73 static void _adg_set_property (GObject *object,
74 guint prop_id,
75 const GValue *value,
76 GParamSpec *pspec);
77 static void _adg_invalidate (AdgStyle *style);
78 static void _adg_apply (AdgStyle *style,
79 AdgEntity *entity,
80 cairo_t *cr);
83 static void
84 adg_font_style_class_init(AdgFontStyleClass *klass)
86 GObjectClass *gobject_class;
87 AdgStyleClass *style_class;
88 GParamSpec *param;
90 gobject_class = (GObjectClass *) klass;
91 style_class = (AdgStyleClass *) klass;
93 g_type_class_add_private(klass, sizeof(AdgFontStylePrivate));
95 gobject_class->get_property = _adg_get_property;
96 gobject_class->set_property = _adg_set_property;
98 style_class->invalidate = _adg_invalidate;
99 style_class->apply = _adg_apply;
101 param = adg_param_spec_dress("color-dress",
102 P_("Color Dress"),
103 P_("The fallback color dress to bind to this style"),
104 ADG_DRESS_COLOR,
105 G_PARAM_READWRITE);
106 g_object_class_install_property(gobject_class, PROP_COLOR_DRESS, param);
108 param = g_param_spec_string("family",
109 P_("Font Family"),
110 P_("The font family name, encoded in UTF-8"),
111 NULL,
112 G_PARAM_READWRITE);
113 g_object_class_install_property(gobject_class, PROP_FAMILY, param);
115 param = g_param_spec_int("slant",
116 P_("Font Slant"),
117 P_("Variant of a font face based on its slant"),
118 G_MININT, G_MAXINT, CAIRO_FONT_SLANT_NORMAL,
119 G_PARAM_READWRITE);
120 g_object_class_install_property(gobject_class, PROP_SLANT, param);
122 param = g_param_spec_int("weight",
123 P_("Font Weight"),
124 P_("Variant of a font face based on its weight"),
125 G_MININT, G_MAXINT, CAIRO_FONT_WEIGHT_NORMAL,
126 G_PARAM_READWRITE);
127 g_object_class_install_property(gobject_class, PROP_WEIGHT, param);
129 param = g_param_spec_double("size",
130 P_("Font Size"),
131 P_("Font size in user space units"),
132 0, G_MAXDOUBLE, 10,
133 G_PARAM_READWRITE);
134 g_object_class_install_property(gobject_class, PROP_SIZE, param);
136 param = g_param_spec_int("antialias",
137 P_("Font Antialiasing Mode"),
138 P_("Type of antialiasing to do when rendering text"),
139 G_MININT, G_MAXINT, CAIRO_ANTIALIAS_DEFAULT,
140 G_PARAM_READWRITE);
141 g_object_class_install_property(gobject_class, PROP_ANTIALIAS, param);
143 param = g_param_spec_int("subpixel-order",
144 P_("Font Subpixel Order"),
145 P_("The order of color elements within each pixel on the display device when rendering with subpixel antialiasing mode"),
146 G_MININT, G_MAXINT,
147 CAIRO_SUBPIXEL_ORDER_DEFAULT,
148 G_PARAM_READWRITE);
149 g_object_class_install_property(gobject_class, PROP_SUBPIXEL_ORDER, param);
151 param = g_param_spec_int("hint-style",
152 P_("Type of Hinting"),
153 P_("How outlines must fit to the pixel grid in order to improve the glyph appearance"),
154 G_MININT, G_MAXINT, CAIRO_HINT_STYLE_DEFAULT,
155 G_PARAM_READWRITE);
156 g_object_class_install_property(gobject_class, PROP_HINT_STYLE, param);
158 param = g_param_spec_int("hint-metrics",
159 P_("Font Metric Hinting"),
160 P_("Whether to hint font metrics, that is align them to integer values in device space"),
161 G_MININT, G_MAXINT,
162 CAIRO_HINT_METRICS_DEFAULT,
163 G_PARAM_READWRITE);
164 g_object_class_install_property(gobject_class, PROP_HINT_METRICS, param);
167 static void
168 adg_font_style_init(AdgFontStyle *font_style)
170 AdgFontStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(font_style,
171 ADG_TYPE_FONT_STYLE,
172 AdgFontStylePrivate);
174 data->color_dress = ADG_DRESS_COLOR;
175 data->family = NULL;
176 data->slant = CAIRO_FONT_SLANT_NORMAL;
177 data->weight = CAIRO_FONT_WEIGHT_NORMAL;
178 data->size = 10;
179 data->antialias = CAIRO_ANTIALIAS_DEFAULT;
180 data->subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
181 data->hint_style = CAIRO_HINT_STYLE_DEFAULT;
182 data->hint_metrics = CAIRO_HINT_METRICS_DEFAULT;
183 data->font = NULL;
185 font_style->data = data;
188 static void
189 _adg_get_property(GObject *object, guint prop_id,
190 GValue *value, GParamSpec *pspec)
192 AdgFontStylePrivate *data = ((AdgFontStyle *) object)->data;
194 switch (prop_id) {
195 case PROP_COLOR_DRESS:
196 g_value_set_int(value, data->color_dress);
197 break;
198 case PROP_FAMILY:
199 g_value_set_string(value, data->family);
200 break;
201 case PROP_SLANT:
202 g_value_set_int(value, data->slant);
203 break;
204 case PROP_WEIGHT:
205 g_value_set_int(value, data->weight);
206 break;
207 case PROP_SIZE:
208 g_value_set_double(value, data->size);
209 break;
210 case PROP_ANTIALIAS:
211 g_value_set_int(value, data->antialias);
212 break;
213 case PROP_SUBPIXEL_ORDER:
214 g_value_set_int(value, data->subpixel_order);
215 break;
216 case PROP_HINT_STYLE:
217 g_value_set_int(value, data->hint_style);
218 break;
219 case PROP_HINT_METRICS:
220 g_value_set_int(value, data->hint_metrics);
221 break;
222 default:
223 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
224 break;
228 static void
229 _adg_set_property(GObject *object, guint prop_id,
230 const GValue *value, GParamSpec *pspec)
232 AdgStyle *style;
233 AdgFontStyle *font_style;
234 AdgFontStylePrivate *data;
236 style = (AdgStyle *) object;
237 font_style = (AdgFontStyle *) object;
238 data = font_style->data;
240 switch (prop_id) {
241 case PROP_COLOR_DRESS:
242 data->color_dress = g_value_get_int(value);
243 break;
244 case PROP_FAMILY:
245 g_free(data->family);
246 data->family = g_value_dup_string(value);
247 adg_style_invalidate(style);
248 break;
249 case PROP_SLANT:
250 data->slant = g_value_get_int(value);
251 adg_style_invalidate(style);
252 break;
253 case PROP_WEIGHT:
254 data->weight = g_value_get_int(value);
255 adg_style_invalidate(style);
256 break;
257 case PROP_SIZE:
258 data->size = g_value_get_double(value);
259 adg_style_invalidate(style);
260 break;
261 case PROP_ANTIALIAS:
262 data->antialias = g_value_get_int(value);
263 adg_style_invalidate(style);
264 break;
265 case PROP_SUBPIXEL_ORDER:
266 data->subpixel_order = g_value_get_int(value);
267 adg_style_invalidate(style);
268 break;
269 case PROP_HINT_STYLE:
270 data->hint_style = g_value_get_int(value);
271 adg_style_invalidate(style);
272 break;
273 case PROP_HINT_METRICS:
274 data->hint_metrics = g_value_get_int(value);
275 adg_style_invalidate(style);
276 break;
277 default:
278 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
279 break;
285 * adg_font_style_new:
287 * Constructs a new font style initialized with default params.
289 * Returns: (transfer full): a newly created font style
291 * Since: 1.0
293 AdgFontStyle *
294 adg_font_style_new(void)
296 return g_object_new(ADG_TYPE_FONT_STYLE, NULL);
300 * adg_font_style_new_options:
301 * @font_style: an #AdgFontStyle object
303 * Creates a new set of #cairo_font_options_t filled with the values
304 * picked from @font_style. The returned value must be freed with
305 * cairo_font_options_destroy().
307 * Returns: (transfer full): a newly allocated list of cairo font options.
309 * Since: 1.0
311 cairo_font_options_t *
312 adg_font_style_new_options(AdgFontStyle *font_style)
314 AdgFontStylePrivate *data;
315 cairo_font_options_t *options;
317 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style), NULL);
319 data = font_style->data;
320 options = cairo_font_options_create();
322 /* Check for cached font */
323 if (data->font != NULL) {
324 cairo_scaled_font_get_font_options(data->font, options);
325 } else {
326 cairo_font_options_set_antialias(options, data->antialias);
327 cairo_font_options_set_subpixel_order(options, data->subpixel_order);
328 cairo_font_options_set_hint_style(options, data->hint_style);
329 cairo_font_options_set_hint_metrics(options, data->hint_metrics);
332 return options;
336 * adg_font_style_get_scaled_font:
337 * @font_style: an #AdgFontStyle object
338 * @ctm: the current transformation matrix
340 * Gets the scaled font of @font_style. The returned font is
341 * owned by @font_style and must not be destroyed by the caller.
343 * Returns: (transfer none): the scaled font.
345 * Since: 1.0
347 cairo_scaled_font_t *
348 adg_font_style_get_scaled_font(AdgFontStyle *font_style, const AdgMatrix *ctm)
350 AdgFontStylePrivate *data;
351 cairo_font_options_t *options;
352 AdgMatrix matrix;
354 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style), NULL);
355 g_return_val_if_fail(ctm != NULL, NULL);
357 data = font_style->data;
359 /* Check for cached font */
360 if (data->font != NULL) {
361 AdgMatrix font_ctm;
363 cairo_scaled_font_get_ctm(data->font, &font_ctm);
365 /* The scaled font is valid only if the two ctm match */
366 if (ctm->xx == font_ctm.xx && ctm->yy == font_ctm.yy &&
367 ctm->xy == font_ctm.xy && ctm->yx == font_ctm.yx)
368 return data->font;
370 /* No valid cache found: rebuild the scaled font */
371 adg_style_invalidate((AdgStyle *) font_style);
374 if (data->face == NULL) {
375 const gchar *family = data->family != NULL ? data->family : "";
377 data->face = cairo_toy_font_face_create(family, data->slant,
378 data->weight);
381 cairo_matrix_init_scale(&matrix, data->size, data->size);
382 options = adg_font_style_new_options(font_style);
383 data->font = cairo_scaled_font_create(data->face, &matrix, ctm, options);
384 cairo_font_options_destroy(options);
386 return data->font;
390 * adg_font_style_set_color_dress:
391 * @font_style: an #AdgFontStyle
392 * @dress: (transfer none): the new color dress to use
394 * Sets a new color dress on @font_style. The new dress
395 * should be related to the original dress: you cannot
396 * set a dress used for font styles to a dress managing
397 * fonts.
399 * The validation of the new dress is done by calling
400 * adg_dress_are_related() with @dress and the previous
401 * dress as arguments: check out its documentation for
402 * details on what is a related dress.
404 * Since: 1.0
406 void
407 adg_font_style_set_color_dress(AdgFontStyle *font_style, AdgDress dress)
409 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
410 g_object_set(font_style, "color-dress", dress, NULL);
414 * adg_font_style_get_color_dress:
415 * @font_style: an #AdgFontStyle
417 * Gets the color dress used by @font_style.
419 * Returns: (transfer none): the current color dress.
421 * Since: 1.0
423 AdgDress
424 adg_font_style_get_color_dress(AdgFontStyle *font_style)
426 AdgFontStylePrivate *data;
428 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style), ADG_DRESS_UNDEFINED);
430 data = font_style->data;
432 return data->color_dress;
436 * adg_font_style_set_family:
437 * @font_style: an #AdgFontStyle object
438 * @family: (transfer none): the new family
440 * Sets a new family.
442 * Since: 1.0
444 void
445 adg_font_style_set_family(AdgFontStyle *font_style, const gchar *family)
447 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
448 g_object_set(font_style, "family", family, NULL);
452 * adg_font_style_get_family:
453 * @font_style: an #AdgFontStyle object
455 * Gets the family of @font_style. The returned pointer refers to
456 * internally managed text that must not be modified or freed.
458 * Returns: (transfer none): the requested family.
460 * Since: 1.0
462 const gchar *
463 adg_font_style_get_family(AdgFontStyle *font_style)
465 AdgFontStylePrivate *data;
467 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style), NULL);
469 data = font_style->data;
471 return data->family;
475 * adg_font_style_set_slant:
476 * @font_style: an #AdgFontStyle object
477 * @slant: (type gint): the new slant
479 * Sets a new slant variant on @font_style.
481 * Since: 1.0
483 void
484 adg_font_style_set_slant(AdgFontStyle *font_style,
485 cairo_font_slant_t slant)
487 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
488 g_object_set(font_style, "slant", slant, NULL);
492 * adg_font_style_get_slant:
493 * @font_style: an #AdgFontStyle object
495 * Gets the slant variant of @font_style.
497 * Returns: (type gint) (transfer none): the slant variant.
499 * Since: 1.0
501 cairo_font_slant_t
502 adg_font_style_get_slant(AdgFontStyle *font_style)
504 AdgFontStylePrivate *data;
506 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style),
507 CAIRO_FONT_SLANT_NORMAL);
509 data = font_style->data;
511 return data->slant;
515 * adg_font_style_set_weight:
516 * @font_style: an #AdgFontStyle object
517 * @weight: (type gint): the new weight
519 * Sets a new weight variant on @font_style.
521 * Since: 1.0
523 void
524 adg_font_style_set_weight(AdgFontStyle *font_style,
525 cairo_font_weight_t weight)
527 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
528 g_object_set(font_style, "weight", weight, NULL);
532 * adg_font_style_get_weight:
533 * @font_style: an #AdgFontStyle object
535 * Gets the weight variant of @font_style.
537 * Returns: (type gint) (transfer none): the weight variant.
539 * Since: 1.0
541 cairo_font_weight_t
542 adg_font_style_get_weight(AdgFontStyle *font_style)
544 AdgFontStylePrivate *data;
546 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style),
547 CAIRO_FONT_WEIGHT_NORMAL);
549 data = font_style->data;
551 return data->weight;
555 * adg_font_style_set_size:
556 * @font_style: an #AdgFontStyle object
557 * @size: the new size
559 * Sets a new size (in global space) on @font_style.
561 * Since: 1.0
563 void
564 adg_font_style_set_size(AdgFontStyle *font_style, gdouble size)
566 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
567 g_object_set(font_style, "size", size, NULL);
571 * adg_font_style_get_size:
572 * @font_style: an #AdgFontStyle object
574 * Gets the size (in global space) of @font_style.
576 * Returns: the size variant.
578 * Since: 1.0
580 gdouble
581 adg_font_style_get_size(AdgFontStyle *font_style)
583 AdgFontStylePrivate *data;
585 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style), 0.);
587 data = font_style->data;
589 return data->size;
593 * adg_font_style_set_antialias:
594 * @font_style: an #AdgFontStyle object
595 * @antialias: (type gint): the new antialias mode
597 * Sets a new antialias mode.
599 * Since: 1.0
601 void
602 adg_font_style_set_antialias(AdgFontStyle *font_style,
603 cairo_antialias_t antialias)
605 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
606 g_object_set(font_style, "antialias", antialias, NULL);
610 * adg_font_style_get_antialias:
611 * @font_style: an #AdgFontStyle object
613 * Gets the antialias mode used.
615 * Returns: (type gint) (transfer none): the requested antialias mode.
617 * Since: 1.0
619 cairo_antialias_t
620 adg_font_style_get_antialias(AdgFontStyle *font_style)
622 AdgFontStylePrivate *data;
624 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style),
625 CAIRO_ANTIALIAS_DEFAULT);
627 data = font_style->data;
629 return data->antialias;
633 * adg_font_style_set_subpixel_order:
634 * @font_style: an #AdgFontStyle object
635 * @subpixel_order: (type gint): the new subpixel order mode
637 * Sets a new subpixel order mode.
639 * Since: 1.0
641 void
642 adg_font_style_set_subpixel_order(AdgFontStyle *font_style,
643 cairo_subpixel_order_t subpixel_order)
645 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
646 g_object_set(font_style, "subpixel-order", subpixel_order, NULL);
650 * adg_font_style_get_subpixel_order:
651 * @font_style: an #AdgFontStyle object
653 * Gets the subpixel order mode used, that is the order of color elements
654 * within each pixel on the display device when rendering with an
655 * antialiasing mode of %CAIRO_ANTIALIAS_SUBPIXEL.
657 * Returns: (type gint) (transfer none): the requested subpixel order mode.
659 * Since: 1.0
661 cairo_subpixel_order_t
662 adg_font_style_get_subpixel_order(AdgFontStyle *font_style)
664 AdgFontStylePrivate *data;
666 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style),
667 CAIRO_SUBPIXEL_ORDER_DEFAULT);
669 data = font_style->data;
671 return data->subpixel_order;
675 * adg_font_style_set_hint_style:
676 * @font_style: an #AdgFontStyle object
677 * @hint_style: (type gint): the new hint style mode
679 * Sets a new hint style mode.
681 * Since: 1.0
683 void
684 adg_font_style_set_hint_style(AdgFontStyle *font_style,
685 cairo_hint_style_t hint_style)
687 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
688 g_object_set(font_style, "hint-style", hint_style, NULL);
692 * adg_font_style_get_hint_style:
693 * @font_style: an #AdgFontStyle object
695 * Gets the hint style mode used, that is how to fit outlines
696 * to the pixel grid in order to improve the appearance of the result.
698 * Returns: (type gint) (transfer none): the requested hint style mode.
700 * Since: 1.0
702 cairo_hint_style_t
703 adg_font_style_get_hint_style(AdgFontStyle *font_style)
705 AdgFontStylePrivate *data;
707 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style),
708 CAIRO_HINT_STYLE_DEFAULT);
710 data = font_style->data;
712 return data->hint_style;
716 * adg_font_style_set_hint_metrics:
717 * @font_style: an #AdgFontStyle object
718 * @hint_metrics: (type gint): the new hint metrics state
720 * Sets a new hint metrics state.
722 * Since: 1.0
724 void
725 adg_font_style_set_hint_metrics(AdgFontStyle *font_style,
726 cairo_hint_metrics_t hint_metrics)
728 g_return_if_fail(ADG_IS_FONT_STYLE(font_style));
729 g_object_set(font_style, "hint-metrics", hint_metrics, NULL);
733 * adg_font_style_get_hint_metrics:
734 * @font_style: an #AdgFontStyle object
736 * Gets the state on whether to hint font metrics.
738 * Returns: (type gint) (transfer none): the requested hint metrics state.
740 * Since: 1.0
742 cairo_hint_metrics_t
743 adg_font_style_get_hint_metrics(AdgFontStyle *font_style)
745 AdgFontStylePrivate *data;
747 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style),
748 CAIRO_HINT_METRICS_DEFAULT);
750 data = font_style->data;
752 return data->hint_metrics;
756 static void
757 _adg_invalidate(AdgStyle *style)
759 AdgFontStyle *font_style;
760 AdgFontStylePrivate *data;
762 font_style = (AdgFontStyle *) style;
763 data = font_style->data;
765 if (data->font != NULL) {
766 cairo_scaled_font_destroy(data->font);
767 data->font = NULL;
770 if (data->face == NULL) {
771 cairo_font_face_destroy(data->face);
772 data->face = NULL;
776 static void
777 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
779 AdgFontStyle *font_style;
780 AdgFontStylePrivate *data;
781 AdgMatrix ctm;
782 cairo_scaled_font_t *font;
784 font_style = (AdgFontStyle *) style;
785 data = font_style->data;
787 adg_entity_apply_dress(entity, data->color_dress, cr);
789 cairo_get_matrix(cr, &ctm);
790 font = adg_font_style_get_scaled_font((AdgFontStyle *) style, &ctm);
792 cairo_set_scaled_font(cr, font);