adg: refactored _adg_read_cairo_path()
[adg.git] / src / adg / adg-pango-style.c
blobe9edd16c988576caaa5aebdeb1231af299814d57
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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-pango-style
23 * @short_description: A font style with pango support
25 * Adds pango support to the #AdgFontStyle class.
27 * Since: 1.0
30 /**
31 * AdgPangoStyle:
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
36 * Since: 1.0
37 **/
40 #include "adg-internal.h"
41 #include <pango/pango.h>
43 #include "adg-style.h"
44 #include "adg-dress.h"
45 #include "adg-dress-builtins.h"
46 #include "adg-font-style.h"
48 #include "adg-pango-style.h"
49 #include "adg-pango-style-private.h"
52 #define _ADG_OLD_STYLE_CLASS ((AdgStyleClass *) adg_pango_style_parent_class)
55 G_DEFINE_TYPE(AdgPangoStyle, adg_pango_style, ADG_TYPE_FONT_STYLE)
58 static void _adg_invalidate (AdgStyle *style);
59 static void _adg_apply (AdgStyle *style,
60 AdgEntity *entity,
61 cairo_t *cr);
64 static void
65 adg_pango_style_class_init(AdgPangoStyleClass *klass)
67 AdgStyleClass *style_class;
69 style_class = (AdgStyleClass *) klass;
71 g_type_class_add_private(klass, sizeof(AdgPangoStylePrivate));
73 style_class->invalidate = _adg_invalidate;
74 style_class->apply = _adg_apply;
77 static void
78 adg_pango_style_init(AdgPangoStyle *pango_style)
80 AdgPangoStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(pango_style,
81 ADG_TYPE_PANGO_STYLE,
82 AdgPangoStylePrivate);
84 data->font_description = NULL;
86 pango_style->data = data;
90 /**
91 * adg_pango_style_new:
93 * Constructs a new pango style initialized with default params.
95 * Returns: a newly created pango style
97 * Since: 1.0
98 **/
99 AdgPangoStyle *
100 adg_pango_style_new(void)
102 return g_object_new(ADG_TYPE_PANGO_STYLE, NULL);
106 * adg_pango_style_get_description:
107 * @pango_style: an #AdgPangoStyle object
109 * Gets the #PangoFontDescription of @pango_style. The returned font is
110 * owned by @pango_style and must not be destroyed by the caller.
112 * Returns: the font description
114 * Since: 1.0
116 PangoFontDescription *
117 adg_pango_style_get_description(AdgPangoStyle *pango_style)
119 AdgPangoStylePrivate *data;
121 g_return_val_if_fail(ADG_IS_PANGO_STYLE(pango_style), NULL);
123 data = pango_style->data;
125 if (data->font_description == NULL) {
126 AdgFontStyle *font_style;
127 const gchar *family;
128 cairo_font_slant_t slant;
129 cairo_font_weight_t weight;
130 gdouble size;
132 font_style = (AdgFontStyle *) pango_style;
133 family = adg_font_style_get_family(font_style);
134 slant = adg_font_style_get_slant(font_style);
135 weight = adg_font_style_get_weight(font_style);
136 size = adg_font_style_get_size(font_style);
138 data->font_description = pango_font_description_new();
140 pango_font_description_set_family(data->font_description, family);
142 switch (slant) {
143 case CAIRO_FONT_SLANT_NORMAL:
144 pango_font_description_set_style(data->font_description,
145 PANGO_STYLE_NORMAL);
146 break;
147 case CAIRO_FONT_SLANT_ITALIC:
148 pango_font_description_set_style(data->font_description,
149 PANGO_STYLE_ITALIC);
150 break;
151 case CAIRO_FONT_SLANT_OBLIQUE:
152 pango_font_description_set_style(data->font_description,
153 PANGO_STYLE_OBLIQUE);
154 break;
155 default:
156 g_warning(_("Unhandled slant value (%d)"), slant);
159 switch (weight) {
160 case CAIRO_FONT_WEIGHT_NORMAL:
161 pango_font_description_set_weight(data->font_description,
162 PANGO_WEIGHT_NORMAL);
163 break;
164 case CAIRO_FONT_WEIGHT_BOLD:
165 pango_font_description_set_weight(data->font_description,
166 PANGO_WEIGHT_BOLD);
167 break;
168 default:
169 g_warning(_("Unhandled weight value (%d)"), weight);
172 pango_font_description_set_size(data->font_description,
173 size * PANGO_SCALE);
176 return data->font_description;
180 static void
181 _adg_invalidate(AdgStyle *style)
183 AdgPangoStyle *pango_style;
184 AdgPangoStylePrivate *data;
186 pango_style = (AdgPangoStyle *) style;
187 data = pango_style->data;
189 if (data->font_description != NULL) {
190 pango_font_description_free(data->font_description);
191 data->font_description = NULL;
194 if (_ADG_OLD_STYLE_CLASS->invalidate != NULL)
195 _ADG_OLD_STYLE_CLASS->invalidate(style);
198 static void
199 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
201 AdgFontStyle *font_style;
202 AdgDress color_dress;
204 font_style = (AdgFontStyle *) style;
205 color_dress = adg_font_style_get_color_dress(font_style);
207 adg_entity_apply_dress(entity, color_dress, cr);