1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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.
32 * All fields are private and should not be used directly.
33 * Use its public methods instead.
37 #include "adg-font-style.h"
38 #include "adg-font-style-private.h"
39 #include "adg-dress-builtins.h"
43 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_font_style_parent_class)
60 static void dispose (GObject
*object
);
61 static void get_property (GObject
*object
,
65 static void set_property (GObject
*object
,
69 static void apply (AdgStyle
*style
,
72 static gboolean
set_family (AdgFontStyle
*font_style
,
74 static gboolean
set_slant (AdgFontStyle
*font_style
,
75 cairo_font_slant_t slant
);
76 static gboolean
set_weight (AdgFontStyle
*font_style
,
77 cairo_font_weight_t weight
);
78 static gboolean
set_size (AdgFontStyle
*font_style
,
80 static gboolean
set_antialias (AdgFontStyle
*font_style
,
81 cairo_antialias_t antialias
);
82 static gboolean
set_subpixel_order (AdgFontStyle
*font_style
,
83 cairo_subpixel_order_t subpixel_order
);
84 static gboolean
set_hint_style (AdgFontStyle
*font_style
,
85 cairo_hint_style_t hint_style
);
86 static gboolean
set_hint_metrics (AdgFontStyle
*font_style
,
87 cairo_hint_metrics_t hint_metrics
);
88 static void unset_face (AdgFontStyle
*font_style
);
89 static void unset_font (AdgFontStyle
*font_style
);
92 G_DEFINE_TYPE(AdgFontStyle
, adg_font_style
, ADG_TYPE_STYLE
);
96 adg_font_style_class_init(AdgFontStyleClass
*klass
)
98 GObjectClass
*gobject_class
;
99 AdgStyleClass
*style_class
;
102 gobject_class
= (GObjectClass
*) klass
;
103 style_class
= (AdgStyleClass
*) klass
;
105 g_type_class_add_private(klass
, sizeof(AdgFontStylePrivate
));
107 gobject_class
->dispose
= dispose
;
108 gobject_class
->get_property
= get_property
;
109 gobject_class
->set_property
= set_property
;
111 style_class
->apply
= apply
;
113 param
= adg_param_spec_dress("color-dress",
115 P_("The color dress to bind to this font style"),
118 g_object_class_install_property(gobject_class
, PROP_COLOR_DRESS
, param
);
120 param
= g_param_spec_string("family",
122 P_("The font family name, encoded in UTF-8"),
125 g_object_class_install_property(gobject_class
, PROP_FAMILY
, param
);
127 param
= g_param_spec_int("slant",
129 P_("Variant of a font face based on its slant"),
130 G_MININT
, G_MAXINT
, CAIRO_FONT_SLANT_NORMAL
,
132 g_object_class_install_property(gobject_class
, PROP_SLANT
, param
);
134 param
= g_param_spec_int("weight",
136 P_("Variant of a font face based on its weight"),
137 G_MININT
, G_MAXINT
, CAIRO_FONT_WEIGHT_NORMAL
,
139 g_object_class_install_property(gobject_class
, PROP_WEIGHT
, param
);
141 param
= g_param_spec_double("size",
143 P_("Font size in user space units"),
146 g_object_class_install_property(gobject_class
, PROP_SIZE
, param
);
148 param
= g_param_spec_int("antialias",
149 P_("Font Antialiasing Mode"),
150 P_("Type of antialiasing to do when rendering text"),
151 G_MININT
, G_MAXINT
, CAIRO_ANTIALIAS_DEFAULT
,
153 g_object_class_install_property(gobject_class
, PROP_ANTIALIAS
, param
);
155 param
= g_param_spec_int("subpixel-order",
156 P_("Font Subpixel Order"),
157 P_("The order of color elements within each pixel on the display device when rendering with subpixel antialiasing mode"),
159 CAIRO_SUBPIXEL_ORDER_DEFAULT
,
161 g_object_class_install_property(gobject_class
, PROP_SUBPIXEL_ORDER
, param
);
163 param
= g_param_spec_int("hint-style",
164 P_("Type of Hinting"),
165 P_("How outlines must fit to the pixel grid in order to improve the glyph appearance"),
166 G_MININT
, G_MAXINT
, CAIRO_HINT_STYLE_DEFAULT
,
168 g_object_class_install_property(gobject_class
, PROP_HINT_STYLE
, param
);
170 param
= g_param_spec_int("hint-metrics",
171 P_("Font Metric Hinting"),
172 P_("Whether to hint font metrics, that is align them to integer values in device space"),
174 CAIRO_HINT_METRICS_DEFAULT
,
176 g_object_class_install_property(gobject_class
, PROP_HINT_METRICS
, param
);
180 adg_font_style_init(AdgFontStyle
*font_style
)
182 AdgFontStylePrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(font_style
,
184 AdgFontStylePrivate
);
186 data
->color_dress
= ADG_DRESS_COLOR
;
188 data
->slant
= CAIRO_FONT_SLANT_NORMAL
;
189 data
->weight
= CAIRO_FONT_WEIGHT_NORMAL
;
191 data
->antialias
= CAIRO_ANTIALIAS_DEFAULT
;
192 data
->subpixel_order
= CAIRO_SUBPIXEL_ORDER_DEFAULT
;
193 data
->hint_style
= CAIRO_HINT_STYLE_DEFAULT
;
194 data
->hint_metrics
= CAIRO_HINT_METRICS_DEFAULT
;
197 font_style
->data
= data
;
201 dispose(GObject
*object
)
203 AdgFontStyle
*font_style
= (AdgFontStyle
*) object
;
205 unset_font(font_style
);
206 unset_face(font_style
);
208 if (PARENT_OBJECT_CLASS
->dispose
!= NULL
)
209 PARENT_OBJECT_CLASS
->dispose(object
);
213 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
215 AdgFontStylePrivate
*data
= ((AdgFontStyle
*) object
)->data
;
218 case PROP_COLOR_DRESS
:
219 g_value_set_int(value
, data
->color_dress
);
222 g_value_set_string(value
, data
->family
);
225 g_value_set_int(value
, data
->slant
);
228 g_value_set_int(value
, data
->weight
);
231 g_value_set_double(value
, data
->size
);
234 g_value_set_int(value
, data
->antialias
);
236 case PROP_SUBPIXEL_ORDER
:
237 g_value_set_int(value
, data
->subpixel_order
);
239 case PROP_HINT_STYLE
:
240 g_value_set_int(value
, data
->hint_style
);
242 case PROP_HINT_METRICS
:
243 g_value_set_int(value
, data
->hint_metrics
);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
252 set_property(GObject
*object
,
253 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
255 AdgFontStyle
*font_style
;
256 AdgFontStylePrivate
*data
;
258 font_style
= (AdgFontStyle
*) object
;
259 data
= font_style
->data
;
262 case PROP_COLOR_DRESS
:
263 adg_dress_set(&data
->color_dress
, g_value_get_int(value
));
266 set_family(font_style
, g_value_get_string(value
));
269 set_slant(font_style
, g_value_get_int(value
));
272 set_weight(font_style
, g_value_get_int(value
));
275 set_size(font_style
, g_value_get_double(value
));
278 set_antialias(font_style
, g_value_get_int(value
));
280 case PROP_SUBPIXEL_ORDER
:
281 set_subpixel_order(font_style
, g_value_get_int(value
));
283 case PROP_HINT_STYLE
:
284 set_hint_style(font_style
, g_value_get_int(value
));
286 case PROP_HINT_METRICS
:
287 set_hint_metrics(font_style
, g_value_get_int(value
));
290 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
297 * adg_font_style_new:
299 * Constructs a new font style initialized with default params.
301 * Returns: a new font style
304 adg_font_style_new(void)
306 return g_object_new(ADG_TYPE_FONT_STYLE
, NULL
);
310 * adg_font_style_font:
311 * @font_style: an #AdgFontStyle object
313 * Gets the scaled font of @font_style. The returned font is
314 * owned by @font_style and must not be destroyed by the caller.
316 * Returns: the scaled font
318 cairo_scaled_font_t
*
319 adg_font_style_font(AdgFontStyle
*font_style
, const AdgMatrix
*ctm
)
321 AdgFontStylePrivate
*data
;
322 cairo_font_options_t
*options
;
325 data
= font_style
->data
;
327 /* Check for cached font */
328 if (data
->font
!= NULL
) {
331 cairo_scaled_font_get_ctm(data
->font
, &font_ctm
);
333 /* The scaled font is valid only if the two ctm match */
334 if (ctm
->xx
== font_ctm
.xx
&& ctm
->yy
== font_ctm
.yy
&&
335 ctm
->xy
== font_ctm
.xy
&& ctm
->yx
== font_ctm
.yx
)
338 /* No valid cache found: rebuild the scaled font */
339 unset_font(font_style
);
342 if (data
->face
== NULL
) {
343 const gchar
*family
= data
->family
!= NULL
? data
->family
: "";
345 data
->face
= cairo_toy_font_face_create(family
, data
->slant
,
349 cairo_matrix_init_scale(&matrix
, data
->size
, data
->size
);
350 options
= cairo_font_options_create();
352 cairo_font_options_set_antialias(options
, data
->antialias
);
353 cairo_font_options_set_subpixel_order(options
, data
->subpixel_order
);
354 cairo_font_options_set_hint_style(options
, data
->hint_style
);
355 cairo_font_options_set_hint_metrics(options
, data
->hint_metrics
);
357 data
->font
= cairo_scaled_font_create(data
->face
, &matrix
, ctm
, options
);
359 cairo_font_options_destroy(options
);
365 * adg_font_style_get_color_dress:
366 * @font_style: an #AdgFontStyle
368 * Gets the color dress used by @font_style.
370 * Returns: the current color dress
373 adg_font_style_get_color_dress(AdgFontStyle
*font_style
)
375 AdgFontStylePrivate
*data
;
377 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), ADG_DRESS_UNDEFINED
);
379 data
= font_style
->data
;
381 return data
->color_dress
;
385 * adg_font_style_set_color_dress:
386 * @font_style: an #AdgFontStyle
387 * @dress: the new color dress to use
389 * Sets a new color dress on @font_style. The new dress
390 * should be related to the original dress: you cannot
391 * set a dress used for font styles to a dress managing
394 * The validation of the new dress is done by calling
395 * adg_dress_are_related() with @dress and the previous
396 * dress as arguments: check out its documentation for
397 * details on what is a related dress.
400 adg_font_style_set_color_dress(AdgFontStyle
*font_style
, AdgDress dress
)
402 AdgFontStylePrivate
*data
;
404 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
406 data
= font_style
->data
;
408 if (adg_dress_set(&data
->color_dress
, dress
))
409 g_object_notify((GObject
*) font_style
, "color-dress");
413 * adg_font_style_get_family:
414 * @font_style: an #AdgFontStyle object
416 * Gets the family of @font_style. The returned pointer refers to
417 * internally managed text that must not be modified or freed.
419 * Returns: the requested family
422 adg_font_style_get_family(AdgFontStyle
*font_style
)
424 AdgFontStylePrivate
*data
;
426 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), NULL
);
428 data
= font_style
->data
;
434 * adg_font_style_set_family:
435 * @font_style: an #AdgFontStyle object
436 * @family: the new family
441 adg_font_style_set_family(AdgFontStyle
*font_style
, const gchar
*family
)
443 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
445 if (set_family(font_style
, family
))
446 g_object_notify((GObject
*) font_style
, "family");
450 * adg_font_style_get_slant:
451 * @font_style: an #AdgFontStyle object
453 * Gets the slant variant of @font_style.
455 * Returns: the slant variant
458 adg_font_style_get_slant(AdgFontStyle
*font_style
)
460 AdgFontStylePrivate
*data
;
462 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
463 CAIRO_FONT_SLANT_NORMAL
);
465 data
= font_style
->data
;
471 * adg_font_style_set_slant:
472 * @font_style: an #AdgFontStyle object
473 * @slant: the new slant
475 * Sets a new slant variant on @font_style.
478 adg_font_style_set_slant(AdgFontStyle
*font_style
,
479 cairo_font_slant_t slant
)
481 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
483 if (set_slant(font_style
, slant
))
484 g_object_notify((GObject
*) font_style
, "slant");
488 * adg_font_style_get_weight:
489 * @font_style: an #AdgFontStyle object
491 * Gets the weight variant of @font_style.
493 * Returns: the weight variant
496 adg_font_style_get_weight(AdgFontStyle
*font_style
)
498 AdgFontStylePrivate
*data
;
500 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
501 CAIRO_FONT_WEIGHT_NORMAL
);
503 data
= font_style
->data
;
509 * adg_font_style_set_weight:
510 * @font_style: an #AdgFontStyle object
511 * @weight: the new weight
513 * Sets a new weight variant on @font_style.
516 adg_font_style_set_weight(AdgFontStyle
*font_style
,
517 cairo_font_weight_t weight
)
519 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
521 if (set_weight(font_style
, weight
))
522 g_object_notify((GObject
*) font_style
, "weight");
526 * adg_font_style_get_size:
527 * @font_style: an #AdgFontStyle object
529 * Gets the size (in global space) of @font_style.
531 * Returns: the size variant
534 adg_font_style_get_size(AdgFontStyle
*font_style
)
536 AdgFontStylePrivate
*data
;
538 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), 0.);
540 data
= font_style
->data
;
546 * adg_font_style_set_size:
547 * @font_style: an #AdgFontStyle object
548 * @size: the new size
550 * Sets a new size (in global space) on @font_style.
553 adg_font_style_set_size(AdgFontStyle
*font_style
, gdouble size
)
555 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
557 if (set_size(font_style
, size
))
558 g_object_notify((GObject
*) font_style
, "size");
562 * adg_font_style_get_antialias:
563 * @font_style: an #AdgFontStyle object
565 * Gets the antialias mode used.
567 * Returns: the requested antialias mode
570 adg_font_style_get_antialias(AdgFontStyle
*font_style
)
572 AdgFontStylePrivate
*data
;
574 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
575 CAIRO_ANTIALIAS_DEFAULT
);
577 data
= font_style
->data
;
579 return data
->antialias
;
583 * adg_font_style_set_antialias:
584 * @font_style: an #AdgFontStyle object
585 * @antialias: the new antialias mode
587 * Sets a new antialias mode.
590 adg_font_style_set_antialias(AdgFontStyle
*font_style
,
591 cairo_antialias_t antialias
)
593 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
595 if (set_antialias(font_style
, antialias
))
596 g_object_notify((GObject
*) font_style
, "antialias");
600 * adg_font_style_get_subpixel_order:
601 * @font_style: an #AdgFontStyle object
603 * Gets the subpixel order mode used, that is the order of color elements
604 * within each pixel on the display device when rendering with an
605 * antialiasing mode of %CAIRO_ANTIALIAS_SUBPIXEL.
607 * Returns: the requested subpixel order mode
609 cairo_subpixel_order_t
610 adg_font_style_get_subpixel_order(AdgFontStyle
*font_style
)
612 AdgFontStylePrivate
*data
;
614 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
615 CAIRO_SUBPIXEL_ORDER_DEFAULT
);
617 data
= font_style
->data
;
619 return data
->subpixel_order
;
623 * adg_font_style_set_subpixel_order:
624 * @font_style: an #AdgFontStyle object
625 * @subpixel_order: the new subpixel order mode
627 * Sets a new subpixel order mode.
630 adg_font_style_set_subpixel_order(AdgFontStyle
*font_style
,
631 cairo_subpixel_order_t subpixel_order
)
633 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
635 if (set_subpixel_order(font_style
, subpixel_order
))
636 g_object_notify((GObject
*) font_style
, "subpixel-order");
640 * adg_font_style_get_hint_style:
641 * @font_style: an #AdgFontStyle object
643 * Gets the hint style mode used, that is how to fit outlines
644 * to the pixel grid in order to improve the appearance of the result.
646 * Returns: the requested hint style mode
649 adg_font_style_get_hint_style(AdgFontStyle
*font_style
)
651 AdgFontStylePrivate
*data
;
653 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
654 CAIRO_HINT_STYLE_DEFAULT
);
656 data
= font_style
->data
;
658 return data
->hint_style
;
662 * adg_font_style_set_hint_style:
663 * @font_style: an #AdgFontStyle object
664 * @hint_style: the new hint style mode
666 * Sets a new hint style mode.
669 adg_font_style_set_hint_style(AdgFontStyle
*font_style
,
670 cairo_hint_style_t hint_style
)
672 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
674 if (set_hint_style(font_style
, hint_style
))
675 g_object_notify((GObject
*) font_style
, "hint-style");
679 * adg_font_style_get_hint_metrics:
680 * @font_style: an #AdgFontStyle object
682 * Gets the state on whether to hint font metrics.
684 * Returns: the requested hint metrics state
687 adg_font_style_get_hint_metrics(AdgFontStyle
*font_style
)
689 AdgFontStylePrivate
*data
;
691 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
692 CAIRO_HINT_METRICS_DEFAULT
);
694 data
= font_style
->data
;
696 return data
->hint_metrics
;
700 * adg_font_style_set_hint_metrics:
701 * @font_style: an #AdgFontStyle object
702 * @hint_metrics: the new hint metrics state
704 * Sets a new hint metrics state.
707 adg_font_style_set_hint_metrics(AdgFontStyle
*font_style
,
708 cairo_hint_metrics_t hint_metrics
)
710 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
712 if (set_hint_metrics(font_style
, hint_metrics
))
713 g_object_notify((GObject
*) font_style
, "hint-metrics");
718 apply(AdgStyle
*style
, AdgEntity
*entity
, cairo_t
*cr
)
720 AdgFontStyle
*font_style
;
721 AdgFontStylePrivate
*data
;
723 cairo_scaled_font_t
*font
;
725 font_style
= (AdgFontStyle
*) style
;
726 data
= font_style
->data
;
728 adg_entity_apply_dress(entity
, data
->color_dress
, cr
);
730 cairo_get_matrix(cr
, &ctm
);
731 font
= adg_font_style_font((AdgFontStyle
*) style
, &ctm
);
733 cairo_set_scaled_font(cr
, font
);
737 set_family(AdgFontStyle
*font_style
, const gchar
*family
)
739 AdgFontStylePrivate
*data
= font_style
->data
;
741 if (adg_strcmp(family
, data
->family
) == 0)
744 g_free(data
->family
);
745 data
->family
= g_strdup(family
);
746 unset_face(font_style
);
752 set_slant(AdgFontStyle
*font_style
, cairo_font_slant_t slant
)
754 AdgFontStylePrivate
*data
= font_style
->data
;
756 if (slant
== data
->slant
)
760 unset_face(font_style
);
766 set_weight(AdgFontStyle
*font_style
, cairo_font_weight_t weight
)
768 AdgFontStylePrivate
*data
= font_style
->data
;
770 if (weight
== data
->weight
)
773 data
->weight
= weight
;
774 unset_face(font_style
);
780 set_size(AdgFontStyle
*font_style
, gdouble size
)
782 AdgFontStylePrivate
*data
= font_style
->data
;
784 if (size
== data
->size
)
788 unset_font(font_style
);
794 set_antialias(AdgFontStyle
*font_style
, cairo_antialias_t antialias
)
796 AdgFontStylePrivate
*data
= font_style
->data
;
798 if (antialias
== data
->antialias
)
801 data
->antialias
= antialias
;
802 unset_font(font_style
);
808 set_subpixel_order(AdgFontStyle
*font_style
,
809 cairo_subpixel_order_t subpixel_order
)
811 AdgFontStylePrivate
*data
= font_style
->data
;
813 if (subpixel_order
== data
->subpixel_order
)
816 data
->subpixel_order
= subpixel_order
;
817 unset_font(font_style
);
823 set_hint_style(AdgFontStyle
*font_style
, cairo_hint_style_t hint_style
)
825 AdgFontStylePrivate
*data
= font_style
->data
;
827 if (hint_style
== data
->hint_style
)
830 data
->hint_style
= hint_style
;
831 unset_font(font_style
);
837 set_hint_metrics(AdgFontStyle
*font_style
, cairo_hint_metrics_t hint_metrics
)
839 AdgFontStylePrivate
*data
= font_style
->data
;
841 if (hint_metrics
== data
->hint_metrics
)
844 data
->hint_metrics
= hint_metrics
;
845 unset_font(font_style
);
851 unset_face(AdgFontStyle
*font_style
)
853 AdgFontStylePrivate
*data
= font_style
->data
;
855 if (data
->face
== NULL
)
858 /* Changing the face invalidates the scaled font too */
859 unset_font(font_style
);
861 cairo_font_face_destroy(data
->face
);
866 unset_font(AdgFontStyle
*font_style
)
868 AdgFontStylePrivate
*data
= font_style
->data
;
870 if (data
->font
== NULL
)
873 cairo_scaled_font_destroy(data
->font
);