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-internal.h"
40 #include "adg-toy-text.h"
41 #include "adg-toy-text-private.h"
42 #include "adg-dress-builtins.h"
43 #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 local_changed (AdgEntity
*entity
);
67 static void invalidate (AdgEntity
*entity
);
68 static void arrange (AdgEntity
*entity
);
69 static void render (AdgEntity
*entity
,
71 static gboolean
set_font_dress (AdgToyText
*toy_text
,
73 static gboolean
set_label (AdgToyText
*toy_text
,
75 static void unset_font (AdgToyText
*toy_text
);
76 static void unset_glyphs (AdgToyText
*toy_text
);
79 G_DEFINE_TYPE(AdgToyText
, adg_toy_text
, ADG_TYPE_ENTITY
);
83 adg_toy_text_class_init(AdgToyTextClass
*klass
)
85 GObjectClass
*gobject_class
;
86 AdgEntityClass
*entity_class
;
89 gobject_class
= (GObjectClass
*) klass
;
90 entity_class
= (AdgEntityClass
*) klass
;
92 g_type_class_add_private(klass
, sizeof(AdgToyTextPrivate
));
94 gobject_class
->finalize
= finalize
;
95 gobject_class
->get_property
= get_property
;
96 gobject_class
->set_property
= set_property
;
98 entity_class
->global_changed
= global_changed
;
99 entity_class
->local_changed
= local_changed
;
100 entity_class
->invalidate
= invalidate
;
101 entity_class
->arrange
= arrange
;
102 entity_class
->render
= render
;
104 param
= adg_param_spec_dress("font-dress",
106 P_("The font dress to use for rendering this text"),
109 g_object_class_install_property(gobject_class
, PROP_FONT_DRESS
, param
);
111 param
= g_param_spec_string("label",
113 P_("The label to display"),
116 g_object_class_install_property(gobject_class
, PROP_LABEL
, param
);
120 adg_toy_text_init(AdgToyText
*toy_text
)
122 AdgToyTextPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(toy_text
,
126 data
->font_dress
= ADG_DRESS_TEXT
;
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
)
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. The
193 * #AdgEntity:local-method property is set by default to
194 * #ADG_LOCAL_NORMALIZED.
196 * Returns: the newly created toy text entity
199 adg_toy_text_new(const gchar
*label
)
201 return g_object_new(ADG_TYPE_TOY_TEXT
,
202 "local-method", ADG_MIX_ANCESTORS_NORMALIZED
,
203 "label", label
, NULL
);
207 * adg_toy_text_set_font_dress:
208 * @toy_text: an #AdgToyText
209 * @dress: the new #AdgDress to use
211 * Sets a new font dress for rendering @toy_text. The new dress
212 * must be related to the original dress for this property:
213 * you cannot set a dress used for line styles to a dress
216 * The check is done by calling adg_dress_are_related() with
217 * @dress and the previous dress as arguments. Check out its
218 * documentation for details on what is a related dress.
221 adg_toy_text_set_font_dress(AdgToyText
*toy_text
, AdgDress dress
)
223 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
225 if (set_font_dress(toy_text
, dress
))
226 g_object_notify((GObject
*) toy_text
, "font-dress");
230 * adg_toy_text_get_font_dress:
231 * @toy_text: an #AdgToyText
233 * Gets the font dress to be used in rendering @toy_text.
235 * Returns: the current font dress
238 adg_toy_text_get_font_dress(AdgToyText
*toy_text
)
240 AdgToyTextPrivate
*data
;
242 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), ADG_DRESS_UNDEFINED
);
244 data
= toy_text
->data
;
246 return data
->font_dress
;
250 * adg_toy_text_set_label:
251 * @toy_text: an #AdgToyText
252 * @label: the label text
254 * Sets a new label for @toy_text. @label can be also %NULL,
255 * in which case will be treated as an empty string.
258 adg_toy_text_set_label(AdgToyText
*toy_text
, const gchar
*label
)
260 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
262 if (set_label(toy_text
, label
))
263 g_object_notify((GObject
*) toy_text
, "label");
267 * adg_toy_text_get_label:
268 * @toy_text: an #AdgToyText
270 * Gets the label text. The string is internally owned and
271 * must not be freed or modified.
273 * Returns: the label text
276 adg_toy_text_get_label(AdgToyText
*toy_text
)
278 AdgToyTextPrivate
*data
;
280 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), NULL
);
282 data
= toy_text
->data
;
289 global_changed(AdgEntity
*entity
)
291 if (PARENT_ENTITY_CLASS
->global_changed
)
292 PARENT_ENTITY_CLASS
->global_changed(entity
);
294 adg_entity_invalidate(entity
);
298 local_changed(AdgEntity
*entity
)
300 if (PARENT_ENTITY_CLASS
->local_changed
)
301 PARENT_ENTITY_CLASS
->local_changed(entity
);
303 adg_entity_invalidate(entity
);
307 invalidate(AdgEntity
*entity
)
309 unset_font((AdgToyText
*) entity
);
310 unset_glyphs((AdgToyText
*) entity
);
312 if (PARENT_ENTITY_CLASS
->invalidate
)
313 PARENT_ENTITY_CLASS
->invalidate(entity
);
317 arrange(AdgEntity
*entity
)
319 AdgToyText
*toy_text
;
320 AdgToyTextPrivate
*data
;
323 toy_text
= (AdgToyText
*) entity
;
324 data
= toy_text
->data
;
326 if (data
->font
== NULL
) {
328 AdgFontStyle
*font_style
;
331 dress
= data
->font_dress
;
332 font_style
= (AdgFontStyle
*) adg_entity_style(entity
, dress
);
334 adg_matrix_copy(&ctm
, adg_entity_get_global_matrix(entity
));
335 adg_matrix_transform(&ctm
, adg_entity_get_local_matrix(entity
),
336 ADG_TRANSFORM_BEFORE
);
338 data
->font
= adg_font_style_get_scaled_font(font_style
, &ctm
);
341 if (adg_is_empty(data
->label
)) {
342 /* Undefined label */
343 extents
.is_defined
= FALSE
;
344 } else if (data
->glyphs
!= NULL
) {
348 cairo_status_t status
;
349 cairo_text_extents_t cairo_extents
;
351 status
= cairo_scaled_font_text_to_glyphs(data
->font
, 0, 0,
357 if (status
!= CAIRO_STATUS_SUCCESS
) {
358 unset_glyphs(toy_text
);
359 g_error("Unable to build glyphs (cairo message: %s)",
360 cairo_status_to_string(status
));
364 cairo_scaled_font_glyph_extents(data
->font
, data
->glyphs
,
365 data
->num_glyphs
, &cairo_extents
);
366 cpml_extents_from_cairo_text(&extents
, &cairo_extents
);
367 cpml_extents_transform(&extents
, adg_entity_get_local_matrix(entity
));
368 cpml_extents_transform(&extents
, adg_entity_get_global_matrix(entity
));
372 adg_entity_set_extents(entity
, &extents
);
376 render(AdgEntity
*entity
, cairo_t
*cr
)
378 AdgToyText
*toy_text
;
379 AdgToyTextPrivate
*data
;
381 toy_text
= (AdgToyText
*) entity
;
382 data
= toy_text
->data
;
384 if (data
->glyphs
!= NULL
) {
385 adg_entity_apply_dress(entity
, data
->font_dress
, cr
);
386 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
387 cairo_show_glyphs(cr
, data
->glyphs
, data
->num_glyphs
);
392 set_font_dress(AdgToyText
*toy_text
, AdgDress dress
)
394 AdgToyTextPrivate
*data
= toy_text
->data
;
396 if (adg_dress_set(&data
->font_dress
, dress
)) {
397 unset_font(toy_text
);
405 set_label(AdgToyText
*toy_text
, const gchar
*label
)
407 AdgToyTextPrivate
*data
= toy_text
->data
;
409 /* Check if the new label differs from the current label */
410 if (adg_strcmp(label
, data
->label
) == 0)
414 data
->label
= g_strdup(label
);
416 unset_glyphs(toy_text
);
421 unset_font(AdgToyText
*toy_text
)
423 AdgToyTextPrivate
*data
= toy_text
->data
;
429 unset_glyphs(AdgToyText
*toy_text
)
431 AdgToyTextPrivate
*data
= toy_text
->data
;
433 if (data
->glyphs
!= NULL
) {
434 cairo_glyph_free(data
->glyphs
);
438 data
->num_glyphs
= 0;