adg: "transfer none" on get_cairo_path()
[adg.git] / src / adg / adg-pango-style.c
blob6a7ac603953fd6fb532539d9063dbed42466b675
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-font-style.h"
47 #include "adg-pango-style.h"
48 #include "adg-pango-style-private.h"
51 #define _ADG_OLD_STYLE_CLASS ((AdgStyleClass *) adg_pango_style_parent_class)
54 G_DEFINE_TYPE(AdgPangoStyle, adg_pango_style, ADG_TYPE_FONT_STYLE)
57 static void _adg_invalidate (AdgStyle *style);
58 static void _adg_apply (AdgStyle *style,
59 AdgEntity *entity,
60 cairo_t *cr);
63 static void
64 adg_pango_style_class_init(AdgPangoStyleClass *klass)
66 AdgStyleClass *style_class;
68 style_class = (AdgStyleClass *) klass;
70 g_type_class_add_private(klass, sizeof(AdgPangoStylePrivate));
72 style_class->invalidate = _adg_invalidate;
73 style_class->apply = _adg_apply;
76 static void
77 adg_pango_style_init(AdgPangoStyle *pango_style)
79 AdgPangoStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(pango_style,
80 ADG_TYPE_PANGO_STYLE,
81 AdgPangoStylePrivate);
83 data->font_description = NULL;
85 pango_style->data = data;
89 /**
90 * adg_pango_style_new:
92 * Constructs a new pango style initialized with default params.
94 * Returns: a newly created pango style
96 * Since: 1.0
97 **/
98 AdgPangoStyle *
99 adg_pango_style_new(void)
101 return g_object_new(ADG_TYPE_PANGO_STYLE, NULL);
105 * adg_pango_style_get_description:
106 * @pango_style: an #AdgPangoStyle object
108 * Gets the #PangoFontDescription of @pango_style. The returned font is
109 * owned by @pango_style and must not be destroyed by the caller.
111 * Returns: the font description
113 * Since: 1.0
115 PangoFontDescription *
116 adg_pango_style_get_description(AdgPangoStyle *pango_style)
118 AdgPangoStylePrivate *data;
120 g_return_val_if_fail(ADG_IS_PANGO_STYLE(pango_style), NULL);
122 data = pango_style->data;
124 if (data->font_description == NULL) {
125 AdgFontStyle *font_style;
126 const gchar *family;
127 cairo_font_slant_t slant;
128 cairo_font_weight_t weight;
129 gdouble size;
131 font_style = (AdgFontStyle *) pango_style;
132 family = adg_font_style_get_family(font_style);
133 slant = adg_font_style_get_slant(font_style);
134 weight = adg_font_style_get_weight(font_style);
135 size = adg_font_style_get_size(font_style);
137 data->font_description = pango_font_description_new();
139 pango_font_description_set_family(data->font_description, family);
141 switch (slant) {
142 case CAIRO_FONT_SLANT_NORMAL:
143 pango_font_description_set_style(data->font_description,
144 PANGO_STYLE_NORMAL);
145 break;
146 case CAIRO_FONT_SLANT_ITALIC:
147 pango_font_description_set_style(data->font_description,
148 PANGO_STYLE_ITALIC);
149 break;
150 case CAIRO_FONT_SLANT_OBLIQUE:
151 pango_font_description_set_style(data->font_description,
152 PANGO_STYLE_OBLIQUE);
153 break;
154 default:
155 g_warning(_("Unhandled slant value (%d)"), slant);
158 switch (weight) {
159 case CAIRO_FONT_WEIGHT_NORMAL:
160 pango_font_description_set_weight(data->font_description,
161 PANGO_WEIGHT_NORMAL);
162 break;
163 case CAIRO_FONT_WEIGHT_BOLD:
164 pango_font_description_set_weight(data->font_description,
165 PANGO_WEIGHT_BOLD);
166 break;
167 default:
168 g_warning(_("Unhandled weight value (%d)"), weight);
171 pango_font_description_set_size(data->font_description,
172 size * PANGO_SCALE);
175 return data->font_description;
179 static void
180 _adg_invalidate(AdgStyle *style)
182 AdgPangoStyle *pango_style;
183 AdgPangoStylePrivate *data;
185 pango_style = (AdgPangoStyle *) style;
186 data = pango_style->data;
188 if (data->font_description != NULL) {
189 pango_font_description_free(data->font_description);
190 data->font_description = NULL;
193 if (_ADG_OLD_STYLE_CLASS->invalidate != NULL)
194 _ADG_OLD_STYLE_CLASS->invalidate(style);
197 static void
198 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
200 AdgFontStyle *font_style;
201 AdgDress color_dress;
203 font_style = (AdgFontStyle *) style;
204 color_dress = adg_font_style_get_color_dress(font_style);
206 adg_entity_apply_dress(entity, color_dress, cr);