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-dress-builtins.h"
42 #include "adg-font-style.h"
45 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_toy_text_parent_class)
46 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_toy_text_parent_class)
56 static void finalize (GObject
*object
);
57 static void get_property (GObject
*object
,
61 static void set_property (GObject
*object
,
65 static void global_changed (AdgEntity
*entity
);
66 static void invalidate (AdgEntity
*entity
);
67 static void arrange (AdgEntity
*entity
);
68 static void render (AdgEntity
*entity
,
70 static gboolean
set_font_dress (AdgToyText
*toy_text
,
72 static gboolean
set_label (AdgToyText
*toy_text
,
74 static void unset_font (AdgToyText
*toy_text
);
75 static void unset_glyphs (AdgToyText
*toy_text
);
78 G_DEFINE_TYPE(AdgToyText
, adg_toy_text
, ADG_TYPE_ENTITY
);
82 adg_toy_text_class_init(AdgToyTextClass
*klass
)
84 GObjectClass
*gobject_class
;
85 AdgEntityClass
*entity_class
;
88 gobject_class
= (GObjectClass
*) klass
;
89 entity_class
= (AdgEntityClass
*) klass
;
91 g_type_class_add_private(klass
, sizeof(AdgToyTextPrivate
));
93 gobject_class
->finalize
= finalize
;
94 gobject_class
->get_property
= get_property
;
95 gobject_class
->set_property
= set_property
;
97 entity_class
->global_changed
= global_changed
;
98 entity_class
->invalidate
= invalidate
;
99 entity_class
->arrange
= arrange
;
100 entity_class
->render
= render
;
102 param
= adg_param_spec_dress("font-dress",
104 P_("The font dress to use for rendering this text"),
107 g_object_class_install_property(gobject_class
, PROP_FONT_DRESS
, param
);
109 param
= g_param_spec_string("label",
111 P_("The label to display"),
114 g_object_class_install_property(gobject_class
, PROP_LABEL
, param
);
118 adg_toy_text_init(AdgToyText
*toy_text
)
120 AdgToyTextPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(toy_text
,
124 data
->font_dress
= ADG_DRESS_TEXT
;
128 toy_text
->data
= data
;
130 adg_entity_set_normalized((AdgEntity
*) toy_text
, TRUE
);
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
;
156 case PROP_FONT_DRESS
:
157 g_value_set_int(value
, data
->font_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
;
175 case PROP_FONT_DRESS
:
176 set_font_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_font_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_font_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
;
219 return data
->font_dress
;
223 * adg_toy_text_set_font_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_font_dress(AdgToyText
*toy_text
, AdgDress dress
)
239 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
241 if (set_font_dress(toy_text
, dress
))
242 g_object_notify((GObject
*) toy_text
, "font-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
= (AdgFontStyle
*)
314 adg_entity_style(entity
, data
->font_dress
);
316 data
->font
= adg_font_style_font(font_style
, adg_entity_ctm(entity
));
319 if (data
->label
== NULL
|| data
->label
[0] == '\0') {
320 /* Undefined label */
321 extents
.is_defined
= FALSE
;
322 adg_entity_set_extents(entity
, &extents
);
326 if (data
->glyphs
== NULL
) {
327 cairo_status_t status
;
328 cairo_text_extents_t cairo_extents
;
330 status
= cairo_scaled_font_text_to_glyphs(data
->font
, 0, 0,
336 if (status
!= CAIRO_STATUS_SUCCESS
) {
337 unset_glyphs(toy_text
);
338 g_error("Unable to build glyphs (cairo message: %s)",
339 cairo_status_to_string(status
));
343 cairo_scaled_font_glyph_extents(data
->font
, data
->glyphs
,
344 data
->num_glyphs
, &cairo_extents
);
345 cpml_extents_from_cairo_text(&extents
, &cairo_extents
);
346 adg_entity_get_local_map(entity
, &local
);
347 cairo_matrix_transform_point(&local
, &extents
.org
.x
, &extents
.org
.y
);
348 cairo_matrix_transform_distance(&local
, &extents
.size
.x
, &extents
.size
.y
);
350 adg_entity_set_extents(entity
, &extents
);
355 render(AdgEntity
*entity
, cairo_t
*cr
)
357 AdgToyText
*toy_text
;
358 AdgToyTextPrivate
*data
;
360 toy_text
= (AdgToyText
*) entity
;
361 data
= toy_text
->data
;
363 if (data
->glyphs
!= NULL
) {
364 adg_entity_apply_dress(entity
, data
->font_dress
, cr
);
365 cairo_set_matrix(cr
, adg_entity_ctm(entity
));
366 cairo_show_glyphs(cr
, data
->glyphs
, data
->num_glyphs
);
371 set_font_dress(AdgToyText
*toy_text
, AdgDress dress
)
373 AdgToyTextPrivate
*data
= toy_text
->data
;
375 if (adg_dress_set(&data
->font_dress
, dress
)) {
376 unset_font(toy_text
);
384 set_label(AdgToyText
*toy_text
, const gchar
*label
)
386 AdgToyTextPrivate
*data
= toy_text
->data
;
388 /* Check if the new label differs from the current label */
389 if (adg_strcmp(label
, data
->label
) == 0)
393 data
->label
= g_strdup(label
);
395 unset_glyphs(toy_text
);
400 unset_font(AdgToyText
*toy_text
)
402 AdgToyTextPrivate
*data
= toy_text
->data
;
408 unset_glyphs(AdgToyText
*toy_text
)
410 AdgToyTextPrivate
*data
= toy_text
->data
;
412 if (data
->glyphs
!= NULL
) {
413 cairo_glyph_free(data
->glyphs
);
417 data
->num_glyphs
= 0;