[AdgLDim] Quote management throught maps
[adg.git] / adg / adg-font-style.c
blob4992dd33cbbf65fbb167d039e8da2c9991d234d4
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.
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.
29 /**
30 * AdgFontStyle:
32 * All fields are private and should not be used directly.
33 * Use its public methods instead.
34 **/
37 #include "adg-font-style.h"
38 #include "adg-font-style-private.h"
39 #include "adg-intl.h"
42 enum {
43 PROP_0,
44 PROP_FAMILY,
45 PROP_SLANT,
46 PROP_WEIGHT,
47 PROP_SIZE,
48 PROP_ANTIALIAS,
49 PROP_SUBPIXEL_ORDER,
50 PROP_HINT_STYLE,
51 PROP_HINT_METRICS
55 static void get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint prop_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void apply (AdgStyle *style,
64 cairo_t *cr);
65 static void set_family (AdgFontStyle *font_style,
66 const gchar *family);
69 G_DEFINE_TYPE(AdgFontStyle, adg_font_style, ADG_TYPE_STYLE);
72 static void
73 adg_font_style_class_init(AdgFontStyleClass *klass)
75 GObjectClass *gobject_class;
76 AdgStyleClass *style_class;
77 GParamSpec *param;
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",
90 P_("Font Family"),
91 P_("The font family name, encoded in UTF-8"),
92 NULL,
93 G_PARAM_READWRITE);
94 g_object_class_install_property(gobject_class, PROP_FAMILY, param);
96 param = g_param_spec_int("slant",
97 P_("Font Slant"),
98 P_("Variant of a font face based on its slant"),
99 G_MININT, G_MAXINT, CAIRO_FONT_SLANT_NORMAL,
100 G_PARAM_READWRITE);
101 g_object_class_install_property(gobject_class, PROP_SLANT, param);
103 param = g_param_spec_int("weight",
104 P_("Font Weight"),
105 P_("Variant of a font face based on its weight"),
106 G_MININT, G_MAXINT, CAIRO_FONT_WEIGHT_NORMAL,
107 G_PARAM_READWRITE);
108 g_object_class_install_property(gobject_class, PROP_WEIGHT, param);
110 param = g_param_spec_double("size",
111 P_("Font Size"),
112 P_("Font size in user space units"),
113 0, G_MAXDOUBLE, 10,
114 G_PARAM_READWRITE);
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,
121 G_PARAM_READWRITE);
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"),
127 G_MININT, G_MAXINT,
128 CAIRO_SUBPIXEL_ORDER_DEFAULT,
129 G_PARAM_READWRITE);
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,
136 G_PARAM_READWRITE);
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"),
142 G_MININT, G_MAXINT,
143 CAIRO_HINT_METRICS_DEFAULT,
144 G_PARAM_READWRITE);
145 g_object_class_install_property(gobject_class, PROP_HINT_METRICS, param);
148 static void
149 adg_font_style_init(AdgFontStyle *font_style)
151 AdgFontStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(font_style,
152 ADG_TYPE_FONT_STYLE,
153 AdgFontStylePrivate);
155 data->family = NULL;
156 data->slant = CAIRO_FONT_SLANT_NORMAL;
157 data->weight = CAIRO_FONT_WEIGHT_NORMAL;
158 data->size = 10;
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;
167 static void
168 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
170 AdgFontStylePrivate *data = ((AdgFontStyle *) object)->data;
172 switch (prop_id) {
173 case PROP_FAMILY:
174 g_value_set_string(value, data->family);
175 break;
176 case PROP_SLANT:
177 g_value_set_int(value, data->slant);
178 break;
179 case PROP_WEIGHT:
180 g_value_set_int(value, data->weight);
181 break;
182 case PROP_SIZE:
183 g_value_set_double(value, data->size);
184 break;
185 case PROP_ANTIALIAS:
186 g_value_set_int(value, data->antialias);
187 break;
188 case PROP_SUBPIXEL_ORDER:
189 g_value_set_int(value, data->subpixel_order);
190 break;
191 case PROP_HINT_STYLE:
192 g_value_set_int(value, data->hint_style);
193 break;
194 case PROP_HINT_METRICS:
195 g_value_set_int(value, data->hint_metrics);
196 break;
197 default:
198 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
199 break;
203 static void
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;
213 switch (prop_id) {
214 case PROP_FAMILY:
215 set_family(font_style, g_value_get_string(value));
216 break;
217 case PROP_SLANT:
218 data->slant = g_value_get_int(value);
219 break;
220 case PROP_WEIGHT:
221 data->weight = g_value_get_int(value);
222 break;
223 case PROP_SIZE:
224 data->size = g_value_get_double(value);
225 break;
226 case PROP_ANTIALIAS:
227 data->antialias = g_value_get_int(value);
228 break;
229 case PROP_SUBPIXEL_ORDER:
230 data->subpixel_order = g_value_get_int(value);
231 break;
232 case PROP_HINT_STYLE:
233 data->hint_style = g_value_get_int(value);
234 break;
235 case PROP_HINT_METRICS:
236 data->hint_metrics = g_value_get_int(value);
237 break;
238 default:
239 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
240 break;
246 * adg_font_style_new:
248 * Constructs a new font style initialized with default params.
250 * Returns: a new font style
252 AdgStyle *
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
267 const gchar *
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;
276 return data->family;
280 * adg_font_style_set_family:
281 * @font_style: an #AdgFontStyle object
282 * @family: the new family
284 * Sets a new family.
286 void
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
303 cairo_font_slant_t
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;
313 return data->slant;
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.
323 void
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;
332 data->slant = slant;
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
345 cairo_font_weight_t
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;
355 return data->weight;
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.
365 void
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
387 gdouble
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;
396 return data->size;
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.
406 void
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;
414 data->size = size;
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
427 cairo_antialias_t
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.
447 void
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.
491 void
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
514 cairo_hint_style_t
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.
534 void
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
556 cairo_hint_metrics_t
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.
576 void
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");
590 static void
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;
600 if (data->family)
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);
616 static void
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);