Tested build process under ss-dev of OpenIndiana
[adg.git] / src / adg / adg-pango-style.c
blobfe4e4f638b203b36dda6db881ad6d7d0988370e2
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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.
28 /**
29 * AdgPangoStyle:
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-internal.h"
37 #include "adg-style.h"
38 #include "adg-dress.h"
39 #include "adg-dress-builtins.h"
40 #include "adg-font-style.h"
42 #include "adg-pango-style.h"
43 #include "adg-pango-style-private.h"
46 #define _ADG_OLD_STYLE_CLASS ((AdgStyleClass *) adg_pango_style_parent_class)
49 G_DEFINE_TYPE(AdgPangoStyle, adg_pango_style, ADG_TYPE_FONT_STYLE)
52 static void _adg_invalidate (AdgStyle *style);
53 static void _adg_apply (AdgStyle *style,
54 AdgEntity *entity,
55 cairo_t *cr);
58 static void
59 adg_pango_style_class_init(AdgPangoStyleClass *klass)
61 AdgStyleClass *style_class;
63 style_class = (AdgStyleClass *) klass;
65 g_type_class_add_private(klass, sizeof(AdgPangoStylePrivate));
67 style_class->invalidate = _adg_invalidate;
68 style_class->apply = _adg_apply;
71 static void
72 adg_pango_style_init(AdgPangoStyle *pango_style)
74 AdgPangoStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(pango_style,
75 ADG_TYPE_PANGO_STYLE,
76 AdgPangoStylePrivate);
78 data->font_description = NULL;
80 pango_style->data = data;
84 /**
85 * adg_pango_style_new:
87 * Constructs a new pango style initialized with default params.
89 * Returns: a newly created pango style
90 **/
91 AdgPangoStyle *
92 adg_pango_style_new(void)
94 return g_object_new(ADG_TYPE_PANGO_STYLE, NULL);
97 /**
98 * adg_pango_style_get_description:
99 * @pango_style: an #AdgPangoStyle object
101 * Gets the #PangoFontDescription of @pango_style. The returned font is
102 * owned by @pango_style and must not be destroyed by the caller.
104 * Returns: the font description
106 PangoFontDescription *
107 adg_pango_style_get_description(AdgPangoStyle *pango_style)
109 AdgPangoStylePrivate *data;
111 g_return_val_if_fail(ADG_IS_PANGO_STYLE(pango_style), NULL);
113 data = pango_style->data;
115 if (data->font_description == NULL) {
116 AdgFontStyle *font_style;
117 const gchar *family;
118 cairo_font_slant_t slant;
119 cairo_font_weight_t weight;
120 gdouble size;
122 font_style = (AdgFontStyle *) pango_style;
123 family = adg_font_style_get_family(font_style);
124 slant = adg_font_style_get_slant(font_style);
125 weight = adg_font_style_get_weight(font_style);
126 size = adg_font_style_get_size(font_style);
128 data->font_description = pango_font_description_new();
130 pango_font_description_set_family(data->font_description, family);
132 switch (slant) {
133 case CAIRO_FONT_SLANT_NORMAL:
134 pango_font_description_set_style(data->font_description,
135 PANGO_STYLE_NORMAL);
136 break;
137 case CAIRO_FONT_SLANT_ITALIC:
138 pango_font_description_set_style(data->font_description,
139 PANGO_STYLE_ITALIC);
140 break;
141 case CAIRO_FONT_SLANT_OBLIQUE:
142 pango_font_description_set_style(data->font_description,
143 PANGO_STYLE_OBLIQUE);
144 break;
145 default:
146 g_warning(_("Unhandled slant value (%d)"), slant);
149 switch (weight) {
150 case CAIRO_FONT_WEIGHT_NORMAL:
151 pango_font_description_set_weight(data->font_description,
152 PANGO_WEIGHT_NORMAL);
153 break;
154 case CAIRO_FONT_WEIGHT_BOLD:
155 pango_font_description_set_weight(data->font_description,
156 PANGO_WEIGHT_BOLD);
157 break;
158 default:
159 g_warning(_("Unhandled weight value (%d)"), weight);
162 pango_font_description_set_size(data->font_description,
163 size * PANGO_SCALE);
166 return data->font_description;
170 static void
171 _adg_invalidate(AdgStyle *style)
173 AdgPangoStyle *pango_style;
174 AdgPangoStylePrivate *data;
176 pango_style = (AdgPangoStyle *) style;
177 data = pango_style->data;
179 if (data->font_description != NULL) {
180 pango_font_description_free(data->font_description);
181 data->font_description = NULL;
184 if (_ADG_OLD_STYLE_CLASS->invalidate != NULL)
185 _ADG_OLD_STYLE_CLASS->invalidate(style);
188 static void
189 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
191 AdgFontStyle *font_style;
192 AdgDress color_dress;
194 font_style = (AdgFontStyle *) style;
195 color_dress = adg_font_style_get_color_dress(font_style);
197 adg_entity_apply_dress(entity, color_dress, cr);