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
312 * @ctm: the current transformation matrix
314 * Gets the scaled font of @font_style. The returned font is
315 * owned by @font_style and must not be destroyed by the caller.
317 * Returns: the scaled font
319 cairo_scaled_font_t
*
320 adg_font_style_font(AdgFontStyle
*font_style
, const AdgMatrix
*ctm
)
322 AdgFontStylePrivate
*data
;
323 cairo_font_options_t
*options
;
326 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), NULL
);
327 g_return_val_if_fail(ctm
!= NULL
, NULL
);
329 data
= font_style
->data
;
331 /* Check for cached font */
332 if (data
->font
!= NULL
) {
335 cairo_scaled_font_get_ctm(data
->font
, &font_ctm
);
337 /* The scaled font is valid only if the two ctm match */
338 if (ctm
->xx
== font_ctm
.xx
&& ctm
->yy
== font_ctm
.yy
&&
339 ctm
->xy
== font_ctm
.xy
&& ctm
->yx
== font_ctm
.yx
)
342 /* No valid cache found: rebuild the scaled font */
343 unset_font(font_style
);
346 if (data
->face
== NULL
) {
347 const gchar
*family
= data
->family
!= NULL
? data
->family
: "";
349 data
->face
= cairo_toy_font_face_create(family
, data
->slant
,
353 cairo_matrix_init_scale(&matrix
, data
->size
, data
->size
);
354 options
= cairo_font_options_create();
356 cairo_font_options_set_antialias(options
, data
->antialias
);
357 cairo_font_options_set_subpixel_order(options
, data
->subpixel_order
);
358 cairo_font_options_set_hint_style(options
, data
->hint_style
);
359 cairo_font_options_set_hint_metrics(options
, data
->hint_metrics
);
361 data
->font
= cairo_scaled_font_create(data
->face
, &matrix
, ctm
, options
);
363 cairo_font_options_destroy(options
);
369 * adg_font_style_get_color_dress:
370 * @font_style: an #AdgFontStyle
372 * Gets the color dress used by @font_style.
374 * Returns: the current color dress
377 adg_font_style_get_color_dress(AdgFontStyle
*font_style
)
379 AdgFontStylePrivate
*data
;
381 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), ADG_DRESS_UNDEFINED
);
383 data
= font_style
->data
;
385 return data
->color_dress
;
389 * adg_font_style_set_color_dress:
390 * @font_style: an #AdgFontStyle
391 * @dress: the new color dress to use
393 * Sets a new color dress on @font_style. The new dress
394 * should be related to the original dress: you cannot
395 * set a dress used for font styles to a dress managing
398 * The validation of the new dress is done by calling
399 * adg_dress_are_related() with @dress and the previous
400 * dress as arguments: check out its documentation for
401 * details on what is a related dress.
404 adg_font_style_set_color_dress(AdgFontStyle
*font_style
, AdgDress dress
)
406 AdgFontStylePrivate
*data
;
408 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
410 data
= font_style
->data
;
412 if (adg_dress_set(&data
->color_dress
, dress
))
413 g_object_notify((GObject
*) font_style
, "color-dress");
417 * adg_font_style_get_family:
418 * @font_style: an #AdgFontStyle object
420 * Gets the family of @font_style. The returned pointer refers to
421 * internally managed text that must not be modified or freed.
423 * Returns: the requested family
426 adg_font_style_get_family(AdgFontStyle
*font_style
)
428 AdgFontStylePrivate
*data
;
430 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), NULL
);
432 data
= font_style
->data
;
438 * adg_font_style_set_family:
439 * @font_style: an #AdgFontStyle object
440 * @family: 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
));
449 if (set_family(font_style
, family
))
450 g_object_notify((GObject
*) font_style
, "family");
454 * adg_font_style_get_slant:
455 * @font_style: an #AdgFontStyle object
457 * Gets the slant variant of @font_style.
459 * Returns: the slant variant
462 adg_font_style_get_slant(AdgFontStyle
*font_style
)
464 AdgFontStylePrivate
*data
;
466 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
467 CAIRO_FONT_SLANT_NORMAL
);
469 data
= font_style
->data
;
475 * adg_font_style_set_slant:
476 * @font_style: an #AdgFontStyle object
477 * @slant: the new slant
479 * Sets a new slant variant on @font_style.
482 adg_font_style_set_slant(AdgFontStyle
*font_style
,
483 cairo_font_slant_t slant
)
485 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
487 if (set_slant(font_style
, slant
))
488 g_object_notify((GObject
*) font_style
, "slant");
492 * adg_font_style_get_weight:
493 * @font_style: an #AdgFontStyle object
495 * Gets the weight variant of @font_style.
497 * Returns: the weight variant
500 adg_font_style_get_weight(AdgFontStyle
*font_style
)
502 AdgFontStylePrivate
*data
;
504 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
505 CAIRO_FONT_WEIGHT_NORMAL
);
507 data
= font_style
->data
;
513 * adg_font_style_set_weight:
514 * @font_style: an #AdgFontStyle object
515 * @weight: the new weight
517 * Sets a new weight variant on @font_style.
520 adg_font_style_set_weight(AdgFontStyle
*font_style
,
521 cairo_font_weight_t weight
)
523 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
525 if (set_weight(font_style
, weight
))
526 g_object_notify((GObject
*) font_style
, "weight");
530 * adg_font_style_get_size:
531 * @font_style: an #AdgFontStyle object
533 * Gets the size (in global space) of @font_style.
535 * Returns: the size variant
538 adg_font_style_get_size(AdgFontStyle
*font_style
)
540 AdgFontStylePrivate
*data
;
542 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), 0.);
544 data
= font_style
->data
;
550 * adg_font_style_set_size:
551 * @font_style: an #AdgFontStyle object
552 * @size: the new size
554 * Sets a new size (in global space) on @font_style.
557 adg_font_style_set_size(AdgFontStyle
*font_style
, gdouble size
)
559 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
561 if (set_size(font_style
, size
))
562 g_object_notify((GObject
*) font_style
, "size");
566 * adg_font_style_get_antialias:
567 * @font_style: an #AdgFontStyle object
569 * Gets the antialias mode used.
571 * Returns: the requested antialias mode
574 adg_font_style_get_antialias(AdgFontStyle
*font_style
)
576 AdgFontStylePrivate
*data
;
578 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
579 CAIRO_ANTIALIAS_DEFAULT
);
581 data
= font_style
->data
;
583 return data
->antialias
;
587 * adg_font_style_set_antialias:
588 * @font_style: an #AdgFontStyle object
589 * @antialias: the new antialias mode
591 * Sets a new antialias mode.
594 adg_font_style_set_antialias(AdgFontStyle
*font_style
,
595 cairo_antialias_t antialias
)
597 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
599 if (set_antialias(font_style
, antialias
))
600 g_object_notify((GObject
*) font_style
, "antialias");
604 * adg_font_style_get_subpixel_order:
605 * @font_style: an #AdgFontStyle object
607 * Gets the subpixel order mode used, that is the order of color elements
608 * within each pixel on the display device when rendering with an
609 * antialiasing mode of %CAIRO_ANTIALIAS_SUBPIXEL.
611 * Returns: the requested subpixel order mode
613 cairo_subpixel_order_t
614 adg_font_style_get_subpixel_order(AdgFontStyle
*font_style
)
616 AdgFontStylePrivate
*data
;
618 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
619 CAIRO_SUBPIXEL_ORDER_DEFAULT
);
621 data
= font_style
->data
;
623 return data
->subpixel_order
;
627 * adg_font_style_set_subpixel_order:
628 * @font_style: an #AdgFontStyle object
629 * @subpixel_order: the new subpixel order mode
631 * Sets a new subpixel order mode.
634 adg_font_style_set_subpixel_order(AdgFontStyle
*font_style
,
635 cairo_subpixel_order_t subpixel_order
)
637 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
639 if (set_subpixel_order(font_style
, subpixel_order
))
640 g_object_notify((GObject
*) font_style
, "subpixel-order");
644 * adg_font_style_get_hint_style:
645 * @font_style: an #AdgFontStyle object
647 * Gets the hint style mode used, that is how to fit outlines
648 * to the pixel grid in order to improve the appearance of the result.
650 * Returns: the requested hint style mode
653 adg_font_style_get_hint_style(AdgFontStyle
*font_style
)
655 AdgFontStylePrivate
*data
;
657 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
658 CAIRO_HINT_STYLE_DEFAULT
);
660 data
= font_style
->data
;
662 return data
->hint_style
;
666 * adg_font_style_set_hint_style:
667 * @font_style: an #AdgFontStyle object
668 * @hint_style: the new hint style mode
670 * Sets a new hint style mode.
673 adg_font_style_set_hint_style(AdgFontStyle
*font_style
,
674 cairo_hint_style_t hint_style
)
676 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
678 if (set_hint_style(font_style
, hint_style
))
679 g_object_notify((GObject
*) font_style
, "hint-style");
683 * adg_font_style_get_hint_metrics:
684 * @font_style: an #AdgFontStyle object
686 * Gets the state on whether to hint font metrics.
688 * Returns: the requested hint metrics state
691 adg_font_style_get_hint_metrics(AdgFontStyle
*font_style
)
693 AdgFontStylePrivate
*data
;
695 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
696 CAIRO_HINT_METRICS_DEFAULT
);
698 data
= font_style
->data
;
700 return data
->hint_metrics
;
704 * adg_font_style_set_hint_metrics:
705 * @font_style: an #AdgFontStyle object
706 * @hint_metrics: the new hint metrics state
708 * Sets a new hint metrics state.
711 adg_font_style_set_hint_metrics(AdgFontStyle
*font_style
,
712 cairo_hint_metrics_t hint_metrics
)
714 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
716 if (set_hint_metrics(font_style
, hint_metrics
))
717 g_object_notify((GObject
*) font_style
, "hint-metrics");
722 apply(AdgStyle
*style
, AdgEntity
*entity
, cairo_t
*cr
)
724 AdgFontStyle
*font_style
;
725 AdgFontStylePrivate
*data
;
727 cairo_scaled_font_t
*font
;
729 font_style
= (AdgFontStyle
*) style
;
730 data
= font_style
->data
;
732 adg_entity_apply_dress(entity
, data
->color_dress
, cr
);
734 cairo_get_matrix(cr
, &ctm
);
735 font
= adg_font_style_font((AdgFontStyle
*) style
, &ctm
);
737 cairo_set_scaled_font(cr
, font
);
741 set_family(AdgFontStyle
*font_style
, const gchar
*family
)
743 AdgFontStylePrivate
*data
= font_style
->data
;
745 if (adg_strcmp(family
, data
->family
) == 0)
748 g_free(data
->family
);
749 data
->family
= g_strdup(family
);
750 unset_face(font_style
);
756 set_slant(AdgFontStyle
*font_style
, cairo_font_slant_t slant
)
758 AdgFontStylePrivate
*data
= font_style
->data
;
760 if (slant
== data
->slant
)
764 unset_face(font_style
);
770 set_weight(AdgFontStyle
*font_style
, cairo_font_weight_t weight
)
772 AdgFontStylePrivate
*data
= font_style
->data
;
774 if (weight
== data
->weight
)
777 data
->weight
= weight
;
778 unset_face(font_style
);
784 set_size(AdgFontStyle
*font_style
, gdouble size
)
786 AdgFontStylePrivate
*data
= font_style
->data
;
788 if (size
== data
->size
)
792 unset_font(font_style
);
798 set_antialias(AdgFontStyle
*font_style
, cairo_antialias_t antialias
)
800 AdgFontStylePrivate
*data
= font_style
->data
;
802 if (antialias
== data
->antialias
)
805 data
->antialias
= antialias
;
806 unset_font(font_style
);
812 set_subpixel_order(AdgFontStyle
*font_style
,
813 cairo_subpixel_order_t subpixel_order
)
815 AdgFontStylePrivate
*data
= font_style
->data
;
817 if (subpixel_order
== data
->subpixel_order
)
820 data
->subpixel_order
= subpixel_order
;
821 unset_font(font_style
);
827 set_hint_style(AdgFontStyle
*font_style
, cairo_hint_style_t hint_style
)
829 AdgFontStylePrivate
*data
= font_style
->data
;
831 if (hint_style
== data
->hint_style
)
834 data
->hint_style
= hint_style
;
835 unset_font(font_style
);
841 set_hint_metrics(AdgFontStyle
*font_style
, cairo_hint_metrics_t hint_metrics
)
843 AdgFontStylePrivate
*data
= font_style
->data
;
845 if (hint_metrics
== data
->hint_metrics
)
848 data
->hint_metrics
= hint_metrics
;
849 unset_font(font_style
);
855 unset_face(AdgFontStyle
*font_style
)
857 AdgFontStylePrivate
*data
= font_style
->data
;
859 if (data
->face
== NULL
)
862 /* Changing the face invalidates the scaled font too */
863 unset_font(font_style
);
865 cairo_font_face_destroy(data
->face
);
870 unset_font(AdgFontStyle
*font_style
)
872 AdgFontStylePrivate
*data
= font_style
->data
;
874 if (data
->font
== NULL
)
877 cairo_scaled_font_destroy(data
->font
);