doc: update copyright line for 2021
[adg.git] / src / adg / adg-pango-style.c
blobc1b6f0d97ac44c14e3d1c8f4496fbbf037f08cd1
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 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_WITH_PRIVATE(AdgPangoStyle, adg_pango_style, ADG_TYPE_FONT_STYLE)
56 enum {
57 PROP_0,
58 PROP_SPACING
62 static void _adg_get_property (GObject *object,
63 guint prop_id,
64 GValue *value,
65 GParamSpec *pspec);
66 static void _adg_set_property (GObject *object,
67 guint prop_id,
68 const GValue *value,
69 GParamSpec *pspec);
70 static void _adg_invalidate (AdgStyle *style);
71 static void _adg_apply (AdgStyle *style,
72 AdgEntity *entity,
73 cairo_t *cr);
76 static void
77 adg_pango_style_class_init(AdgPangoStyleClass *klass)
79 GObjectClass *gobject_class;
80 AdgStyleClass *style_class;
81 GParamSpec *param;
83 gobject_class = (GObjectClass *) klass;
84 style_class = (AdgStyleClass *) klass;
86 gobject_class->get_property = _adg_get_property;
87 gobject_class->set_property = _adg_set_property;
89 style_class->invalidate = _adg_invalidate;
90 style_class->apply = _adg_apply;
92 param = g_param_spec_int("spacing",
93 P_("Spacing"),
94 P_("Amount of spacing between lines on multiline text"),
95 G_MININT, G_MAXINT, 0,
96 G_PARAM_READWRITE);
97 g_object_class_install_property(gobject_class, PROP_SPACING, param);
100 static void
101 adg_pango_style_init(AdgPangoStyle *pango_style)
103 AdgPangoStylePrivate *data = adg_pango_style_get_instance_private(pango_style);
104 data->font_description = NULL;
105 data->spacing = 0;
108 static void
109 _adg_get_property(GObject *object, guint prop_id,
110 GValue *value, GParamSpec *pspec)
112 AdgPangoStylePrivate *data = adg_pango_style_get_instance_private((AdgPangoStyle *) object);
114 switch (prop_id) {
115 case PROP_SPACING:
116 g_value_set_int(value, data->spacing);
117 break;
118 default:
119 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
120 break;
124 static void
125 _adg_set_property(GObject *object, guint prop_id,
126 const GValue *value, GParamSpec *pspec)
128 AdgPangoStylePrivate *data = adg_pango_style_get_instance_private((AdgPangoStyle *) object);
130 switch (prop_id) {
131 case PROP_SPACING:
132 data->spacing = g_value_get_int(value);
133 break;
134 default:
135 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
136 break;
142 * adg_pango_style_new:
144 * Constructs a new pango style initialized with default params.
146 * Returns: a newly created pango style
148 * Since: 1.0
150 AdgPangoStyle *
151 adg_pango_style_new(void)
153 return g_object_new(ADG_TYPE_PANGO_STYLE, NULL);
157 * adg_pango_style_get_description:
158 * @pango_style: an #AdgPangoStyle object
160 * Gets the #PangoFontDescription of @pango_style. The returned font is
161 * owned by @pango_style and must not be destroyed by the caller.
163 * Returns: the font description
165 * Since: 1.0
167 PangoFontDescription *
168 adg_pango_style_get_description(AdgPangoStyle *pango_style)
170 AdgPangoStylePrivate *data;
172 g_return_val_if_fail(ADG_IS_PANGO_STYLE(pango_style), NULL);
174 data = adg_pango_style_get_instance_private(pango_style);
176 if (data->font_description == NULL) {
177 AdgFontStyle *font_style;
178 const gchar *family;
179 cairo_font_slant_t slant;
180 cairo_font_weight_t weight;
181 gdouble size;
183 font_style = (AdgFontStyle *) pango_style;
184 family = adg_font_style_get_family(font_style);
185 slant = adg_font_style_get_slant(font_style);
186 weight = adg_font_style_get_weight(font_style);
187 size = adg_font_style_get_size(font_style);
189 data->font_description = pango_font_description_new();
191 pango_font_description_set_family(data->font_description, family);
193 switch (slant) {
194 case CAIRO_FONT_SLANT_NORMAL:
195 pango_font_description_set_style(data->font_description,
196 PANGO_STYLE_NORMAL);
197 break;
198 case CAIRO_FONT_SLANT_ITALIC:
199 pango_font_description_set_style(data->font_description,
200 PANGO_STYLE_ITALIC);
201 break;
202 case CAIRO_FONT_SLANT_OBLIQUE:
203 pango_font_description_set_style(data->font_description,
204 PANGO_STYLE_OBLIQUE);
205 break;
206 default:
207 g_warning(_("Unhandled slant value (%d)"), slant);
210 switch (weight) {
211 case CAIRO_FONT_WEIGHT_NORMAL:
212 pango_font_description_set_weight(data->font_description,
213 PANGO_WEIGHT_NORMAL);
214 break;
215 case CAIRO_FONT_WEIGHT_BOLD:
216 pango_font_description_set_weight(data->font_description,
217 PANGO_WEIGHT_BOLD);
218 break;
219 default:
220 g_warning(_("Unhandled weight value (%d)"), weight);
223 pango_font_description_set_size(data->font_description,
224 size * PANGO_SCALE);
227 return data->font_description;
231 * adg_pango_style_set_spacing:
232 * @pango_style: an #AdgPangoStyle object
233 * @spacing: the new spacing
235 * Sets the new spacing to @spacing on @pango_style.
237 * Since: 1.0
239 void
240 adg_pango_style_set_spacing(AdgPangoStyle *pango_style, gint spacing)
242 g_return_if_fail(ADG_IS_PANGO_STYLE(pango_style));
243 g_object_set(pango_style, "spacing", spacing, NULL);
247 * adg_pango_style_get_spacing:
248 * @pango_style: an #AdgPangoStyle object
250 * Gets the spacing of @pango_style.
252 * Returns: (type gint): the current spacing value.
254 * Since: 1.0
256 gint
257 adg_pango_style_get_spacing(AdgPangoStyle *pango_style)
259 AdgPangoStylePrivate *data;
261 g_return_val_if_fail(ADG_IS_PANGO_STYLE(pango_style), 0);
263 data = adg_pango_style_get_instance_private(pango_style);
264 return data->spacing;
268 static void
269 _adg_invalidate(AdgStyle *style)
271 AdgPangoStyle *pango_style = (AdgPangoStyle *) style;
272 AdgPangoStylePrivate *data = adg_pango_style_get_instance_private(pango_style);
274 if (data->font_description != NULL) {
275 pango_font_description_free(data->font_description);
276 data->font_description = NULL;
279 if (_ADG_OLD_STYLE_CLASS->invalidate != NULL)
280 _ADG_OLD_STYLE_CLASS->invalidate(style);
283 static void
284 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
286 AdgFontStyle *font_style;
287 AdgDress color_dress;
289 font_style = (AdgFontStyle *) style;
290 color_dress = adg_font_style_get_color_dress(font_style);
292 adg_entity_apply_dress(entity, color_dress, cr);