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.
22 * SECTION:adg-pango-style
23 * @short_description: A font style with pango support
25 * Adds pango support to the #AdgFontStyle class.
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
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
)
62 static void _adg_get_property (GObject
*object
,
66 static void _adg_set_property (GObject
*object
,
70 static void _adg_invalidate (AdgStyle
*style
);
71 static void _adg_apply (AdgStyle
*style
,
77 adg_pango_style_class_init(AdgPangoStyleClass
*klass
)
79 GObjectClass
*gobject_class
;
80 AdgStyleClass
*style_class
;
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",
94 P_("Amount of spacing between lines on multiline text"),
95 G_MININT
, G_MAXINT
, 0,
97 g_object_class_install_property(gobject_class
, PROP_SPACING
, param
);
101 adg_pango_style_init(AdgPangoStyle
*pango_style
)
103 AdgPangoStylePrivate
*data
= adg_pango_style_get_instance_private(pango_style
);
104 data
->font_description
= NULL
;
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
);
116 g_value_set_int(value
, data
->spacing
);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
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
);
132 data
->spacing
= g_value_get_int(value
);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
142 * adg_pango_style_new:
144 * Constructs a new pango style initialized with default params.
146 * Returns: a newly created pango style
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
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
;
179 cairo_font_slant_t slant
;
180 cairo_font_weight_t weight
;
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
);
194 case CAIRO_FONT_SLANT_NORMAL
:
195 pango_font_description_set_style(data
->font_description
,
198 case CAIRO_FONT_SLANT_ITALIC
:
199 pango_font_description_set_style(data
->font_description
,
202 case CAIRO_FONT_SLANT_OBLIQUE
:
203 pango_font_description_set_style(data
->font_description
,
204 PANGO_STYLE_OBLIQUE
);
207 g_warning(_("Unhandled slant value (%d)"), slant
);
211 case CAIRO_FONT_WEIGHT_NORMAL
:
212 pango_font_description_set_weight(data
->font_description
,
213 PANGO_WEIGHT_NORMAL
);
215 case CAIRO_FONT_WEIGHT_BOLD
:
216 pango_font_description_set_weight(data
->font_description
,
220 g_warning(_("Unhandled weight value (%d)"), weight
);
223 pango_font_description_set_size(data
->font_description
,
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.
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.
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
;
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
);
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
);