1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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-toy-text
23 * @short_description: Simple text entity that use the cairo "toy" text API
25 * The #AdgToyText class is a basic class to show simple text. It internally
26 * uses the so called cairo "toy" API and it shares the same limitations.
28 * The toy text entity is not subject to the local matrix, only its origin is.
34 * All fields are privates and should not be used directly.
35 * Use its public methods instead.
39 #include "adg-toy-text.h"
40 #include "adg-toy-text-private.h"
41 #include "adg-font-style.h"
44 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_toy_text_parent_class)
45 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_toy_text_parent_class)
55 static void finalize (GObject
*object
);
56 static void get_property (GObject
*object
,
60 static void set_property (GObject
*object
,
64 static void global_changed (AdgEntity
*entity
);
65 static void invalidate (AdgEntity
*entity
);
66 static void arrange (AdgEntity
*entity
);
67 static void render (AdgEntity
*entity
,
69 static gboolean
set_dress (AdgToyText
*toy_text
,
71 static gboolean
set_label (AdgToyText
*toy_text
,
73 static void unset_font (AdgToyText
*toy_text
);
74 static void unset_glyphs (AdgToyText
*toy_text
);
77 G_DEFINE_TYPE(AdgToyText
, adg_toy_text
, ADG_TYPE_ENTITY
);
81 adg_toy_text_class_init(AdgToyTextClass
*klass
)
83 GObjectClass
*gobject_class
;
84 AdgEntityClass
*entity_class
;
87 gobject_class
= (GObjectClass
*) klass
;
88 entity_class
= (AdgEntityClass
*) klass
;
90 g_type_class_add_private(klass
, sizeof(AdgToyTextPrivate
));
92 gobject_class
->finalize
= finalize
;
93 gobject_class
->get_property
= get_property
;
94 gobject_class
->set_property
= set_property
;
96 entity_class
->global_changed
= global_changed
;
97 entity_class
->invalidate
= invalidate
;
98 entity_class
->arrange
= arrange
;
99 entity_class
->render
= render
;
101 param
= adg_param_spec_dress("dress",
103 P_("The font dress to use for rendering this text"),
104 ADG_DRESS_TEXT_REGULAR
,
106 g_object_class_install_property(gobject_class
, PROP_DRESS
, param
);
108 param
= g_param_spec_string("label",
110 P_("The label to display"),
113 g_object_class_install_property(gobject_class
, PROP_LABEL
, param
);
117 adg_toy_text_init(AdgToyText
*toy_text
)
119 AdgToyTextPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(toy_text
,
123 data
->dress
= ADG_DRESS_TEXT_REGULAR
;
127 adg_entity_set_local_mode((AdgEntity
*) toy_text
,
128 ADG_TRANSFORM_BEFORE_NORMALIZED
);
130 toy_text
->data
= data
;
134 finalize(GObject
*object
)
136 AdgToyText
*toy_text
;
137 AdgToyTextPrivate
*data
;
139 toy_text
= (AdgToyText
*) object
;
140 data
= toy_text
->data
;
143 unset_font(toy_text
);
144 unset_glyphs(toy_text
);
146 if (PARENT_OBJECT_CLASS
->finalize
!= NULL
)
147 PARENT_OBJECT_CLASS
->finalize(object
);
151 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
153 AdgToyTextPrivate
*data
= ((AdgToyText
*) object
)->data
;
157 g_value_set_int(value
, data
->dress
);
160 g_value_set_string(value
, data
->label
);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
169 set_property(GObject
*object
, guint prop_id
,
170 const GValue
*value
, GParamSpec
*pspec
)
172 AdgToyText
*toy_text
= (AdgToyText
*) object
;
176 set_dress(toy_text
, g_value_get_int(value
));
179 set_label(toy_text
, g_value_get_string(value
));
182 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
190 * @label: the label text
192 * Creates a new toy text entity using @label as its text
194 * Returns: the newly created toy text entity
197 adg_toy_text_new(const gchar
*label
)
199 return g_object_new(ADG_TYPE_TOY_TEXT
, "label", label
, NULL
);
203 * adg_toy_text_get_dress:
204 * @toy_text: an #AdgToyText
206 * Gets the font dress to be used in rendering @toy_text.
208 * Returns: the current font dress
211 adg_toy_text_get_dress(AdgToyText
*toy_text
)
213 AdgToyTextPrivate
*data
;
215 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), ADG_DRESS_UNDEFINED
);
217 data
= toy_text
->data
;
223 * adg_toy_text_set_dress:
224 * @toy_text: an #AdgToyText
225 * @dress: the new #AdgDress to use
227 * Sets a new font dress for rendering @toy_text. The new dress
228 * must be related to the original dress for this property:
229 * you cannot set a dress used for line styles to a dress
232 * The check is done by calling adg_dress_are_related() with
233 * @dress and the previous dress as arguments. Check out its
234 * documentation for details on what is a related dress.
237 adg_toy_text_set_dress(AdgToyText
*toy_text
, AdgDress dress
)
239 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
241 if (set_dress(toy_text
, dress
))
242 g_object_notify((GObject
*) toy_text
, "dress");
246 * adg_toy_text_get_label:
247 * @toy_text: an #AdgToyText
249 * Gets the label text. The string is internally owned and
250 * must not be freed or modified.
252 * Returns: the label text
255 adg_toy_text_get_label(AdgToyText
*toy_text
)
257 AdgToyTextPrivate
*data
;
259 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), NULL
);
261 data
= toy_text
->data
;
267 * adg_toy_text_set_label:
268 * @toy_text: an #AdgToyText
269 * @label: the label text
271 * Sets a new label for @toy_text. @label can be also %NULL,
272 * in which case will be treated as an empty string.
275 adg_toy_text_set_label(AdgToyText
*toy_text
, const gchar
*label
)
277 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
279 if (set_label(toy_text
, label
))
280 g_object_notify((GObject
*) toy_text
, "label");
285 global_changed(AdgEntity
*entity
)
287 PARENT_ENTITY_CLASS
->global_changed(entity
);
288 unset_font((AdgToyText
*) entity
);
292 invalidate(AdgEntity
*entity
)
294 unset_font((AdgToyText
*) entity
);
295 unset_glyphs((AdgToyText
*) entity
);
297 if (PARENT_ENTITY_CLASS
->invalidate
!= NULL
)
298 return PARENT_ENTITY_CLASS
->invalidate(entity
);
302 arrange(AdgEntity
*entity
)
304 AdgToyText
*toy_text
;
305 AdgToyTextPrivate
*data
;
309 toy_text
= (AdgToyText
*) entity
;
310 data
= toy_text
->data
;
312 if (data
->font
== NULL
) {
313 AdgFontStyle
*font_style
;
316 font_style
= (AdgFontStyle
*) adg_entity_style(entity
, data
->dress
);
317 adg_entity_get_ctm(entity
, &ctm
);
319 data
->font
= adg_font_style_font(font_style
, &ctm
);
322 if (data
->label
== NULL
|| data
->label
[0] == '\0') {
323 /* Undefined label */
324 extents
.is_defined
= FALSE
;
325 adg_entity_set_extents(entity
, &extents
);
329 if (data
->glyphs
== NULL
) {
330 cairo_status_t status
;
331 cairo_text_extents_t cairo_extents
;
333 status
= cairo_scaled_font_text_to_glyphs(data
->font
, 0, 0,
339 if (status
!= CAIRO_STATUS_SUCCESS
) {
340 unset_glyphs(toy_text
);
341 g_error("Unable to build glyphs (cairo message: %s)",
342 cairo_status_to_string(status
));
346 cairo_scaled_font_glyph_extents(data
->font
, data
->glyphs
,
347 data
->num_glyphs
, &cairo_extents
);
348 cpml_extents_from_cairo_text(&extents
, &cairo_extents
);
349 adg_entity_get_local_map(entity
, &local
);
350 cairo_matrix_transform_point(&local
, &extents
.org
.x
, &extents
.org
.y
);
351 cairo_matrix_transform_distance(&local
, &extents
.size
.x
, &extents
.size
.y
);
353 adg_entity_set_extents(entity
, &extents
);
358 render(AdgEntity
*entity
, cairo_t
*cr
)
360 AdgToyText
*toy_text
;
361 AdgToyTextPrivate
*data
;
363 toy_text
= (AdgToyText
*) entity
;
364 data
= toy_text
->data
;
366 if (data
->glyphs
!= NULL
) {
369 adg_entity_get_ctm(entity
, &ctm
);
371 cairo_set_scaled_font(cr
, data
->font
);
372 cairo_set_matrix(cr
, &ctm
);
373 cairo_show_glyphs(cr
, data
->glyphs
, data
->num_glyphs
);
378 set_dress(AdgToyText
*toy_text
, AdgDress dress
)
380 AdgToyTextPrivate
*data
= toy_text
->data
;
382 if (adg_dress_set(&data
->dress
, dress
)) {
383 unset_font(toy_text
);
391 set_label(AdgToyText
*toy_text
, const gchar
*label
)
393 AdgToyTextPrivate
*data
= toy_text
->data
;
395 /* Check if the new label differs from the current label */
396 if (adg_strcmp(label
, data
->label
) == 0)
400 data
->label
= g_strdup(label
);
402 unset_glyphs(toy_text
);
407 unset_font(AdgToyText
*toy_text
)
409 AdgToyTextPrivate
*data
= toy_text
->data
;
415 unset_glyphs(AdgToyText
*toy_text
)
417 AdgToyTextPrivate
*data
= toy_text
->data
;
419 if (data
->glyphs
!= NULL
) {
420 cairo_glyph_free(data
->glyphs
);
424 data
->num_glyphs
= 0;