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.
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.
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
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
)
69 static void _adg_get_property (GObject
*object
,
73 static void _adg_set_property (GObject
*object
,
77 static void _adg_invalidate (AdgStyle
*style
);
78 static void _adg_apply (AdgStyle
*style
,
84 adg_font_style_class_init(AdgFontStyleClass
*klass
)
86 GObjectClass
*gobject_class
;
87 AdgStyleClass
*style_class
;
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",
103 P_("The fallback color dress to bind to this style"),
106 g_object_class_install_property(gobject_class
, PROP_COLOR_DRESS
, param
);
108 param
= g_param_spec_string("family",
110 P_("The font family name, encoded in UTF-8"),
113 g_object_class_install_property(gobject_class
, PROP_FAMILY
, param
);
115 param
= g_param_spec_int("slant",
117 P_("Variant of a font face based on its slant"),
118 G_MININT
, G_MAXINT
, CAIRO_FONT_SLANT_NORMAL
,
120 g_object_class_install_property(gobject_class
, PROP_SLANT
, param
);
122 param
= g_param_spec_int("weight",
124 P_("Variant of a font face based on its weight"),
125 G_MININT
, G_MAXINT
, CAIRO_FONT_WEIGHT_NORMAL
,
127 g_object_class_install_property(gobject_class
, PROP_WEIGHT
, param
);
129 param
= g_param_spec_double("size",
131 P_("Font size in user space units"),
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
,
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"),
147 CAIRO_SUBPIXEL_ORDER_DEFAULT
,
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
,
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"),
162 CAIRO_HINT_METRICS_DEFAULT
,
164 g_object_class_install_property(gobject_class
, PROP_HINT_METRICS
, param
);
168 adg_font_style_init(AdgFontStyle
*font_style
)
170 AdgFontStylePrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(font_style
,
172 AdgFontStylePrivate
);
174 data
->color_dress
= ADG_DRESS_COLOR
;
176 data
->slant
= CAIRO_FONT_SLANT_NORMAL
;
177 data
->weight
= CAIRO_FONT_WEIGHT_NORMAL
;
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
;
185 font_style
->data
= data
;
189 _adg_get_property(GObject
*object
, guint prop_id
,
190 GValue
*value
, GParamSpec
*pspec
)
192 AdgFontStylePrivate
*data
= ((AdgFontStyle
*) object
)->data
;
195 case PROP_COLOR_DRESS
:
196 g_value_set_int(value
, data
->color_dress
);
199 g_value_set_string(value
, data
->family
);
202 g_value_set_int(value
, data
->slant
);
205 g_value_set_int(value
, data
->weight
);
208 g_value_set_double(value
, data
->size
);
211 g_value_set_int(value
, data
->antialias
);
213 case PROP_SUBPIXEL_ORDER
:
214 g_value_set_int(value
, data
->subpixel_order
);
216 case PROP_HINT_STYLE
:
217 g_value_set_int(value
, data
->hint_style
);
219 case PROP_HINT_METRICS
:
220 g_value_set_int(value
, data
->hint_metrics
);
223 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
229 _adg_set_property(GObject
*object
, guint prop_id
,
230 const GValue
*value
, GParamSpec
*pspec
)
233 AdgFontStyle
*font_style
;
234 AdgFontStylePrivate
*data
;
236 style
= (AdgStyle
*) object
;
237 font_style
= (AdgFontStyle
*) object
;
238 data
= font_style
->data
;
241 case PROP_COLOR_DRESS
:
242 data
->color_dress
= g_value_get_int(value
);
245 g_free(data
->family
);
246 data
->family
= g_value_dup_string(value
);
247 adg_style_invalidate(style
);
250 data
->slant
= g_value_get_int(value
);
251 adg_style_invalidate(style
);
254 data
->weight
= g_value_get_int(value
);
255 adg_style_invalidate(style
);
258 data
->size
= g_value_get_double(value
);
259 adg_style_invalidate(style
);
262 data
->antialias
= g_value_get_int(value
);
263 adg_style_invalidate(style
);
265 case PROP_SUBPIXEL_ORDER
:
266 data
->subpixel_order
= g_value_get_int(value
);
267 adg_style_invalidate(style
);
269 case PROP_HINT_STYLE
:
270 data
->hint_style
= g_value_get_int(value
);
271 adg_style_invalidate(style
);
273 case PROP_HINT_METRICS
:
274 data
->hint_metrics
= g_value_get_int(value
);
275 adg_style_invalidate(style
);
278 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
285 * adg_font_style_new:
287 * Constructs a new font style initialized with default params.
289 * Returns: (transfer full): a newly created font style
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.
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
);
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
);
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.
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
;
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
) {
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
)
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
,
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
);
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
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.
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.
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
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.
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
;
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.
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.
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
;
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.
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.
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
;
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.
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.
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
;
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.
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.
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.
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.
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.
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.
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.
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.
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
;
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
);
770 if (data
->face
== NULL
) {
771 cairo_font_face_destroy(data
->face
);
777 _adg_apply(AdgStyle
*style
, AdgEntity
*entity
, cairo_t
*cr
)
779 AdgFontStyle
*font_style
;
780 AdgFontStylePrivate
*data
;
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
);