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"
55 static void get_property (GObject
*object
,
59 static void set_property (GObject
*object
,
63 static void apply (AdgStyle
*style
,
65 static void set_family (AdgFontStyle
*font_style
,
69 G_DEFINE_TYPE(AdgFontStyle
, adg_font_style
, ADG_TYPE_STYLE
);
73 adg_font_style_class_init(AdgFontStyleClass
*klass
)
75 GObjectClass
*gobject_class
;
76 AdgStyleClass
*style_class
;
79 gobject_class
= (GObjectClass
*) klass
;
80 style_class
= (AdgStyleClass
*) klass
;
82 g_type_class_add_private(klass
, sizeof(AdgFontStylePrivate
));
84 gobject_class
->get_property
= get_property
;
85 gobject_class
->set_property
= set_property
;
87 style_class
->apply
= apply
;
89 param
= g_param_spec_string("family",
91 P_("The font family name, encoded in UTF-8"),
94 g_object_class_install_property(gobject_class
, PROP_FAMILY
, param
);
96 param
= g_param_spec_int("slant",
98 P_("Variant of a font face based on its slant"),
99 G_MININT
, G_MAXINT
, CAIRO_FONT_SLANT_NORMAL
,
101 g_object_class_install_property(gobject_class
, PROP_SLANT
, param
);
103 param
= g_param_spec_int("weight",
105 P_("Variant of a font face based on its weight"),
106 G_MININT
, G_MAXINT
, CAIRO_FONT_WEIGHT_NORMAL
,
108 g_object_class_install_property(gobject_class
, PROP_WEIGHT
, param
);
110 param
= g_param_spec_double("size",
112 P_("Font size in user space units"),
115 g_object_class_install_property(gobject_class
, PROP_SIZE
, param
);
117 param
= g_param_spec_int("antialias",
118 P_("Font Antialiasing Mode"),
119 P_("Type of antialiasing to do when rendering text"),
120 G_MININT
, G_MAXINT
, CAIRO_ANTIALIAS_DEFAULT
,
122 g_object_class_install_property(gobject_class
, PROP_ANTIALIAS
, param
);
124 param
= g_param_spec_int("subpixel-order",
125 P_("Font Subpixel Order"),
126 P_("The order of color elements within each pixel on the display device when rendering with subpixel antialiasing mode"),
128 CAIRO_SUBPIXEL_ORDER_DEFAULT
,
130 g_object_class_install_property(gobject_class
, PROP_SUBPIXEL_ORDER
, param
);
132 param
= g_param_spec_int("hint-style",
133 P_("Type of Hinting"),
134 P_("How outlines must fit to the pixel grid in order to improve the glyph appearance"),
135 G_MININT
, G_MAXINT
, CAIRO_HINT_STYLE_DEFAULT
,
137 g_object_class_install_property(gobject_class
, PROP_HINT_STYLE
, param
);
139 param
= g_param_spec_int("hint-metrics",
140 P_("Font Metric Hinting"),
141 P_("Whether to hint font metrics, that is align them to integer values in device space"),
143 CAIRO_HINT_METRICS_DEFAULT
,
145 g_object_class_install_property(gobject_class
, PROP_HINT_METRICS
, param
);
149 adg_font_style_init(AdgFontStyle
*font_style
)
151 AdgFontStylePrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(font_style
,
153 AdgFontStylePrivate
);
156 data
->slant
= CAIRO_FONT_SLANT_NORMAL
;
157 data
->weight
= CAIRO_FONT_WEIGHT_NORMAL
;
159 data
->antialias
= CAIRO_ANTIALIAS_DEFAULT
;
160 data
->subpixel_order
= CAIRO_SUBPIXEL_ORDER_DEFAULT
;
161 data
->hint_style
= CAIRO_HINT_STYLE_DEFAULT
;
162 data
->hint_metrics
= CAIRO_HINT_METRICS_DEFAULT
;
164 font_style
->data
= data
;
168 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
170 AdgFontStylePrivate
*data
= ((AdgFontStyle
*) object
)->data
;
174 g_value_set_string(value
, data
->family
);
177 g_value_set_int(value
, data
->slant
);
180 g_value_set_int(value
, data
->weight
);
183 g_value_set_double(value
, data
->size
);
186 g_value_set_int(value
, data
->antialias
);
188 case PROP_SUBPIXEL_ORDER
:
189 g_value_set_int(value
, data
->subpixel_order
);
191 case PROP_HINT_STYLE
:
192 g_value_set_int(value
, data
->hint_style
);
194 case PROP_HINT_METRICS
:
195 g_value_set_int(value
, data
->hint_metrics
);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
204 set_property(GObject
*object
,
205 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
207 AdgFontStyle
*font_style
;
208 AdgFontStylePrivate
*data
;
210 font_style
= (AdgFontStyle
*) object
;
211 data
= font_style
->data
;
215 set_family(font_style
, g_value_get_string(value
));
218 data
->slant
= g_value_get_int(value
);
221 data
->weight
= g_value_get_int(value
);
224 data
->size
= g_value_get_double(value
);
227 data
->antialias
= g_value_get_int(value
);
229 case PROP_SUBPIXEL_ORDER
:
230 data
->subpixel_order
= g_value_get_int(value
);
232 case PROP_HINT_STYLE
:
233 data
->hint_style
= g_value_get_int(value
);
235 case PROP_HINT_METRICS
:
236 data
->hint_metrics
= g_value_get_int(value
);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
246 * adg_font_style_new:
248 * Constructs a new font style initialized with default params.
250 * Returns: a new font style
253 adg_font_style_new(void)
255 return g_object_new(ADG_TYPE_FONT_STYLE
, NULL
);
259 * adg_font_style_get_family:
260 * @font_style: an #AdgFontStyle object
262 * Gets the family of @font_style. The returned pointer refers to
263 * internally managed text that must not be modified or freed.
265 * Returns: the requested family
268 adg_font_style_get_family(AdgFontStyle
*font_style
)
270 AdgFontStylePrivate
*data
;
272 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), NULL
);
274 data
= font_style
->data
;
280 * adg_font_style_set_family:
281 * @font_style: an #AdgFontStyle object
282 * @family: the new family
287 adg_font_style_set_family(AdgFontStyle
*font_style
, const gchar
*family
)
289 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
291 set_family(font_style
, family
);
292 g_object_notify((GObject
*) font_style
, "family");
296 * adg_font_style_get_slant:
297 * @font_style: an #AdgFontStyle object
299 * Gets the slant variant of @font_style.
301 * Returns: the slant variant
304 adg_font_style_get_slant(AdgFontStyle
*font_style
)
306 AdgFontStylePrivate
*data
;
308 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
309 CAIRO_FONT_SLANT_NORMAL
);
311 data
= font_style
->data
;
317 * adg_font_style_set_slant:
318 * @font_style: an #AdgFontStyle object
319 * @slant: the new slant
321 * Sets a new slant variant on @font_style.
324 adg_font_style_set_slant(AdgFontStyle
*font_style
,
325 cairo_font_slant_t slant
)
327 AdgFontStylePrivate
*data
;
329 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
331 data
= font_style
->data
;
334 g_object_notify((GObject
*) font_style
, "slant");
338 * adg_font_style_get_weight:
339 * @font_style: an #AdgFontStyle object
341 * Gets the weight variant of @font_style.
343 * Returns: the weight variant
346 adg_font_style_get_weight(AdgFontStyle
*font_style
)
348 AdgFontStylePrivate
*data
;
350 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
351 CAIRO_FONT_WEIGHT_NORMAL
);
353 data
= font_style
->data
;
359 * adg_font_style_set_weight:
360 * @font_style: an #AdgFontStyle object
361 * @weight: the new weight
363 * Sets a new weight variant on @font_style.
366 adg_font_style_set_weight(AdgFontStyle
*font_style
,
367 cairo_font_weight_t weight
)
369 AdgFontStylePrivate
*data
;
371 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
373 data
= font_style
->data
;
374 data
->weight
= weight
;
376 g_object_notify((GObject
*) font_style
, "weight");
380 * adg_font_style_get_size:
381 * @font_style: an #AdgFontStyle object
383 * Gets the size (in global space) of @font_style.
385 * Returns: the size variant
388 adg_font_style_get_size(AdgFontStyle
*font_style
)
390 AdgFontStylePrivate
*data
;
392 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
), 0.);
394 data
= font_style
->data
;
400 * adg_font_style_set_size:
401 * @font_style: an #AdgFontStyle object
402 * @size: the new size
404 * Sets a new size (in global space) on @font_style.
407 adg_font_style_set_size(AdgFontStyle
*font_style
, gdouble size
)
409 AdgFontStylePrivate
*data
;
411 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
413 data
= font_style
->data
;
416 g_object_notify((GObject
*) font_style
, "size");
420 * adg_font_style_get_antialias:
421 * @font_style: an #AdgFontStyle object
423 * Gets the antialias mode used.
425 * Returns: the requested antialias mode
428 adg_font_style_get_antialias(AdgFontStyle
*font_style
)
430 AdgFontStylePrivate
*data
;
432 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
433 CAIRO_ANTIALIAS_DEFAULT
);
435 data
= font_style
->data
;
437 return data
->antialias
;
441 * adg_font_style_set_antialias:
442 * @font_style: an #AdgFontStyle object
443 * @antialias: the new antialias mode
445 * Sets a new antialias mode.
448 adg_font_style_set_antialias(AdgFontStyle
*font_style
,
449 cairo_antialias_t antialias
)
451 AdgFontStylePrivate
*data
;
453 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
455 data
= font_style
->data
;
456 data
->antialias
= antialias
;
458 g_object_notify((GObject
*) font_style
, "antialias");
462 * adg_font_style_get_subpixel_order:
463 * @font_style: an #AdgFontStyle object
465 * Gets the subpixel order mode used, that is the order of color elements
466 * within each pixel on the display device when rendering with an
467 * antialiasing mode of %CAIRO_ANTIALIAS_SUBPIXEL.
469 * Returns: the requested subpixel order mode
471 cairo_subpixel_order_t
472 adg_font_style_get_subpixel_order(AdgFontStyle
*font_style
)
474 AdgFontStylePrivate
*data
;
476 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
477 CAIRO_SUBPIXEL_ORDER_DEFAULT
);
479 data
= font_style
->data
;
481 return data
->subpixel_order
;
485 * adg_font_style_set_subpixel_order:
486 * @font_style: an #AdgFontStyle object
487 * @subpixel_order: the new subpixel order mode
489 * Sets a new subpixel order mode.
492 adg_font_style_set_subpixel_order(AdgFontStyle
*font_style
,
493 cairo_subpixel_order_t subpixel_order
)
495 AdgFontStylePrivate
*data
;
497 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
499 data
= font_style
->data
;
500 data
->subpixel_order
= subpixel_order
;
502 g_object_notify((GObject
*) font_style
, "subpixel-order");
506 * adg_font_style_get_hint_style:
507 * @font_style: an #AdgFontStyle object
509 * Gets the hint style mode used, that is how to fit outlines
510 * to the pixel grid in order to improve the appearance of the result.
512 * Returns: the requested hint style mode
515 adg_font_style_get_hint_style(AdgFontStyle
*font_style
)
517 AdgFontStylePrivate
*data
;
519 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
520 CAIRO_HINT_STYLE_DEFAULT
);
522 data
= font_style
->data
;
524 return data
->hint_style
;
528 * adg_font_style_set_hint_style:
529 * @font_style: an #AdgFontStyle object
530 * @hint_style: the new hint style mode
532 * Sets a new hint style mode.
535 adg_font_style_set_hint_style(AdgFontStyle
*font_style
,
536 cairo_hint_style_t hint_style
)
538 AdgFontStylePrivate
*data
;
540 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
542 data
= font_style
->data
;
543 data
->hint_style
= hint_style
;
545 g_object_notify((GObject
*) font_style
, "hint-style");
549 * adg_font_style_get_hint_metrics:
550 * @font_style: an #AdgFontStyle object
552 * Gets the state on whether to hint font metrics.
554 * Returns: the requested hint metrics state
557 adg_font_style_get_hint_metrics(AdgFontStyle
*font_style
)
559 AdgFontStylePrivate
*data
;
561 g_return_val_if_fail(ADG_IS_FONT_STYLE(font_style
),
562 CAIRO_HINT_METRICS_DEFAULT
);
564 data
= font_style
->data
;
566 return data
->hint_metrics
;
570 * adg_font_style_set_hint_metrics:
571 * @font_style: an #AdgFontStyle object
572 * @hint_metrics: the new hint metrics state
574 * Sets a new hint metrics state.
577 adg_font_style_set_hint_metrics(AdgFontStyle
*font_style
,
578 cairo_hint_metrics_t hint_metrics
)
580 AdgFontStylePrivate
*data
;
582 g_return_if_fail(ADG_IS_FONT_STYLE(font_style
));
584 data
= font_style
->data
;
585 data
->hint_metrics
= hint_metrics
;
587 g_object_notify((GObject
*) font_style
, "hint-metrics");
591 apply(AdgStyle
*style
, cairo_t
*cr
)
593 AdgFontStyle
*font_style
;
594 AdgFontStylePrivate
*data
;
595 cairo_font_options_t
*options
;
597 font_style
= (AdgFontStyle
*) style
;
598 data
= font_style
->data
;
601 cairo_select_font_face(cr
, data
->family
, data
->slant
, data
->weight
);
603 cairo_set_font_size(cr
, data
->size
);
605 options
= cairo_font_options_create();
607 cairo_font_options_set_antialias(options
, data
->antialias
);
608 cairo_font_options_set_subpixel_order(options
, data
->subpixel_order
);
609 cairo_font_options_set_hint_style(options
, data
->hint_style
);
610 cairo_font_options_set_hint_metrics(options
, data
->hint_metrics
);
611 cairo_set_font_options(cr
, options
);
613 cairo_font_options_destroy(options
);
617 set_family(AdgFontStyle
*font_style
, const gchar
*family
)
619 AdgFontStylePrivate
*data
= font_style
->data
;
621 g_free(data
->family
);
622 data
->family
= g_strdup(family
);