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.
23 * @short_description: A pango based text entity
25 * The #AdgText class is the main class for showing text. It works
26 * the same way as #AdgToyText but uses pango instead of the so
27 * called cairo "toy" API.
29 * The text entity is not subject to the local matrix, only its origin is.
35 * All fields are privates and should not be used directly.
36 * Use its public methods instead.
40 #include "adg-internal.h"
41 #include <pango/pangocairo.h>
43 #include "adg-dress.h"
44 #include "adg-dress-builtins.h"
45 #include "adg-style.h"
46 #include "adg-font-style.h"
47 #include "adg-pango-style.h"
48 #include "adg-textual.h"
51 #include "adg-text-private.h"
54 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_text_parent_class)
55 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_text_parent_class)
58 static void _adg_iface_init (AdgTextualIface
*iface
);
61 G_DEFINE_TYPE_WITH_CODE(AdgText
, adg_text
, ADG_TYPE_ENTITY
,
62 G_IMPLEMENT_INTERFACE(ADG_TYPE_TEXTUAL
, _adg_iface_init
))
71 static void _adg_dispose (GObject
*object
);
72 static void _adg_finalize (GObject
*object
);
73 static void _adg_get_property (GObject
*object
,
77 static void _adg_set_property (GObject
*object
,
81 static void _adg_global_changed (AdgEntity
*entity
);
82 static void _adg_local_changed (AdgEntity
*entity
);
83 static void _adg_invalidate (AdgEntity
*entity
);
84 static void _adg_arrange (AdgEntity
*entity
);
85 static void _adg_render (AdgEntity
*entity
,
87 static void _adg_set_font_dress (AdgTextual
*textual
,
89 static AdgDress
_adg_get_font_dress (AdgTextual
*textual
);
90 static void _adg_set_text (AdgTextual
*textual
,
92 static gchar
* _adg_dup_text (AdgTextual
*textual
);
93 static void _adg_refresh_extents (AdgText
*text
);
94 static void _adg_clear_layout (AdgText
*text
);
98 adg_text_class_init(AdgTextClass
*klass
)
100 GObjectClass
*gobject_class
;
101 AdgEntityClass
*entity_class
;
103 gobject_class
= (GObjectClass
*) klass
;
104 entity_class
= (AdgEntityClass
*) klass
;
106 g_type_class_add_private(klass
, sizeof(AdgTextPrivate
));
108 gobject_class
->dispose
= _adg_dispose
;
109 gobject_class
->finalize
= _adg_finalize
;
110 gobject_class
->get_property
= _adg_get_property
;
111 gobject_class
->set_property
= _adg_set_property
;
113 entity_class
->global_changed
= _adg_global_changed
;
114 entity_class
->local_changed
= _adg_local_changed
;
115 entity_class
->invalidate
= _adg_invalidate
;
116 entity_class
->arrange
= _adg_arrange
;
117 entity_class
->render
= _adg_render
;
119 g_object_class_override_property(gobject_class
, PROP_FONT_DRESS
, "font-dress");
120 g_object_class_override_property(gobject_class
, PROP_TEXT
, "text");
124 _adg_iface_init(AdgTextualIface
*iface
)
126 iface
->set_font_dress
= _adg_set_font_dress
;
127 iface
->get_font_dress
= _adg_get_font_dress
;
128 iface
->set_text
= _adg_set_text
;
129 iface
->dup_text
= _adg_dup_text
;
130 iface
->text_changed
= NULL
;
134 adg_text_init(AdgText
*text
)
136 AdgTextPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(text
, ADG_TYPE_TEXT
,
139 data
->font_dress
= ADG_DRESS_FONT_TEXT
;
147 _adg_dispose(GObject
*object
)
149 AdgText
*text
= (AdgText
*) object
;
151 _adg_clear_layout(text
);
153 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
154 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
158 _adg_finalize(GObject
*object
)
161 AdgTextPrivate
*data
;
163 text
= (AdgText
*) object
;
168 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
169 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
173 _adg_get_property(GObject
*object
, guint prop_id
,
174 GValue
*value
, GParamSpec
*pspec
)
176 AdgTextPrivate
*data
= ((AdgText
*) object
)->data
;
179 case PROP_FONT_DRESS
:
180 g_value_set_int(value
, data
->font_dress
);
183 g_value_set_string(value
, data
->text
);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
192 _adg_set_property(GObject
*object
, guint prop_id
,
193 const GValue
*value
, GParamSpec
*pspec
)
196 AdgTextPrivate
*data
;
198 text
= (AdgText
*) object
;
202 case PROP_FONT_DRESS
:
203 data
->font_dress
= g_value_get_int(value
);
204 _adg_clear_layout(text
);
208 data
->text
= g_value_dup_string(value
);
209 _adg_clear_layout(text
);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
222 * Creates a new text entity using @text as its content.
223 * The #AdgEntity:local-method property is set by default to
224 * #ADG_LOCAL_NORMALIZED.
226 * Returns: the newly created text entity
229 adg_text_new(const gchar
*text
)
231 return g_object_new(ADG_TYPE_TEXT
,
232 "local-method", ADG_MIX_ANCESTORS_NORMALIZED
,
238 _adg_global_changed(AdgEntity
*entity
)
240 if (_ADG_OLD_ENTITY_CLASS
->global_changed
!= NULL
)
241 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
243 _adg_refresh_extents((AdgText
*) entity
);
247 _adg_local_changed(AdgEntity
*entity
)
249 if (_ADG_OLD_ENTITY_CLASS
->local_changed
!= NULL
)
250 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
252 _adg_refresh_extents((AdgText
*) entity
);
256 _adg_invalidate(AdgEntity
*entity
)
258 _adg_clear_layout((AdgText
*) entity
);
260 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
261 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
265 _adg_arrange(AdgEntity
*entity
)
268 AdgTextPrivate
*data
;
271 text
= (AdgText
*) entity
;
274 if (adg_is_string_empty(data
->text
)) {
276 CpmlExtents new_extents
;
277 new_extents
.is_defined
= FALSE
;
278 adg_entity_set_extents(entity
, &new_extents
);
280 } else if (data
->layout
!= NULL
) {
285 if (data
->layout
== NULL
) {
287 AdgPangoStyle
*pango_style
;
288 PangoFontMap
*font_map
;
289 PangoFontDescription
*font_description
;
290 cairo_font_options_t
*options
;
291 PangoContext
*context
;
293 dress
= data
->font_dress
;
294 pango_style
= (AdgPangoStyle
*) adg_entity_style(entity
, dress
);
295 font_map
= pango_cairo_font_map_new();
296 font_description
= adg_pango_style_get_description(pango_style
);
298 context
= pango_context_new();
299 pango_context_set_font_map(context
, font_map
);
300 g_object_unref(font_map
);
301 pango_cairo_context_set_resolution(context
, 72);
303 options
= adg_font_style_new_options((AdgFontStyle
*) pango_style
);
304 pango_cairo_context_set_font_options(context
, options
);
305 cairo_font_options_destroy(options
);
307 data
->layout
= pango_layout_new(context
);
308 g_object_unref(context
);
310 pango_layout_set_text(data
->layout
, data
->text
, -1);
311 pango_layout_set_font_description(data
->layout
, font_description
);
314 pango_layout_get_extents(data
->layout
, NULL
, &size
);
316 data
->raw_extents
.org
.x
= pango_units_to_double(size
.x
);
317 data
->raw_extents
.org
.y
= pango_units_to_double(size
.y
);
318 data
->raw_extents
.size
.x
= pango_units_to_double(size
.width
);
319 data
->raw_extents
.size
.y
= pango_units_to_double(size
.height
);
320 data
->raw_extents
.is_defined
= TRUE
;
322 _adg_refresh_extents((AdgText
*) entity
);
326 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
329 AdgTextPrivate
*data
;
331 text
= (AdgText
*) entity
;
334 if (data
->layout
!= NULL
) {
335 adg_entity_apply_dress(entity
, data
->font_dress
, cr
);
336 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
337 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
339 /* Realign the text to follow the cairo toy text convention:
340 * use bottom/left corner as reference (pango uses top/left). */
341 cairo_translate(cr
, 0, -data
->raw_extents
.size
.y
);
343 pango_cairo_update_layout(cr
, data
->layout
);
344 pango_cairo_show_layout(cr
, data
->layout
);
349 _adg_set_font_dress(AdgTextual
*textual
, AdgDress dress
)
351 g_object_set(textual
, "font-dress", dress
, NULL
);
355 _adg_get_font_dress(AdgTextual
*textual
)
357 AdgTextPrivate
*data
= ((AdgText
*) textual
)->data
;
358 return data
->font_dress
;
362 _adg_set_text(AdgTextual
*textual
, const gchar
*text
)
364 g_object_set(textual
, "text", text
, NULL
);
368 _adg_dup_text(AdgTextual
*textual
)
370 AdgTextPrivate
*data
= ((AdgText
*) textual
)->data
;
371 return g_strdup(data
->text
);
375 _adg_refresh_extents(AdgText
*text
)
377 AdgTextPrivate
*data
;
380 CpmlExtents new_extents
;
384 if (! data
->raw_extents
.is_defined
)
387 entity
= (AdgEntity
*) text
;
389 adg_matrix_copy(&ctm
, adg_entity_get_global_matrix(entity
));
390 adg_matrix_transform(&ctm
, adg_entity_get_local_matrix(entity
),
391 ADG_TRANSFORM_AFTER
);
392 cpml_extents_copy(&new_extents
, &data
->raw_extents
);
394 /* Realign the text to follow the cairo toy text convention:
395 * use bottom/left corner as reference (pango uses top/left). */
396 new_extents
.org
.y
-= new_extents
.size
.y
;
398 cpml_extents_transform(&new_extents
, &ctm
);
399 adg_entity_set_extents(entity
, &new_extents
);
403 _adg_clear_layout(AdgText
*text
)
405 AdgTextPrivate
*data
= text
->data
;
407 if (data
->layout
!= NULL
) {
408 g_object_unref(data
->layout
);