1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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_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
->local_changed
= local_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_FONT_TEXT
;
128 toy_text
->data
= data
;
132 finalize(GObject
*object
)
134 AdgToyText
*toy_text
;
135 AdgToyTextPrivate
*data
;
137 toy_text
= (AdgToyText
*) object
;
138 data
= toy_text
->data
;
141 unset_font(toy_text
);
142 unset_glyphs(toy_text
);
144 if (PARENT_OBJECT_CLASS
->finalize
)
145 PARENT_OBJECT_CLASS
->finalize(object
);
149 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
151 AdgToyTextPrivate
*data
= ((AdgToyText
*) object
)->data
;
154 case PROP_FONT_DRESS
:
155 g_value_set_int(value
, data
->font_dress
);
158 g_value_set_string(value
, data
->label
);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
167 set_property(GObject
*object
, guint prop_id
,
168 const GValue
*value
, GParamSpec
*pspec
)
170 AdgToyText
*toy_text
;
171 AdgToyTextPrivate
*data
;
173 toy_text
= (AdgToyText
*) object
;
174 data
= toy_text
->data
;
177 case PROP_FONT_DRESS
:
178 data
->font_dress
= g_value_get_int(value
);
179 unset_font(toy_text
);
182 set_label(toy_text
, g_value_get_string(value
));
185 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
193 * @label: the label text
195 * Creates a new toy text entity using @label as its text. The
196 * #AdgEntity:local-method property is set by default to
197 * #ADG_LOCAL_NORMALIZED.
199 * Returns: the newly created toy text entity
202 adg_toy_text_new(const gchar
*label
)
204 return g_object_new(ADG_TYPE_TOY_TEXT
,
205 "local-method", ADG_MIX_ANCESTORS_NORMALIZED
,
206 "label", label
, NULL
);
210 * adg_toy_text_set_font_dress:
211 * @toy_text: an #AdgToyText
212 * @dress: the new #AdgDress to use
214 * Sets a new font dress for rendering @toy_text. The new dress
215 * must be related to the original dress for this property:
216 * you cannot set a dress used for line styles to a dress
219 * The check is done by calling adg_dress_are_related() with
220 * @dress and the previous dress as arguments. Check out its
221 * documentation for details on what is a related dress.
224 adg_toy_text_set_font_dress(AdgToyText
*toy_text
, AdgDress dress
)
226 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
227 g_object_set((GObject
*) toy_text
, "font-dress", dress
, NULL
);
231 * adg_toy_text_get_font_dress:
232 * @toy_text: an #AdgToyText
234 * Gets the font dress to be used in rendering @toy_text.
236 * Returns: the current font dress
239 adg_toy_text_get_font_dress(AdgToyText
*toy_text
)
241 AdgToyTextPrivate
*data
;
243 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), ADG_DRESS_UNDEFINED
);
245 data
= toy_text
->data
;
247 return data
->font_dress
;
251 * adg_toy_text_set_label:
252 * @toy_text: an #AdgToyText
253 * @label: the label text
255 * Sets a new label for @toy_text. @label can be also %NULL,
256 * in which case will be treated as an empty string.
259 adg_toy_text_set_label(AdgToyText
*toy_text
, const gchar
*label
)
261 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text
));
263 if (set_label(toy_text
, label
))
264 g_object_notify((GObject
*) toy_text
, "label");
268 * adg_toy_text_get_label:
269 * @toy_text: an #AdgToyText
271 * Gets the label text. The string is internally owned and
272 * must not be freed or modified.
274 * Returns: the label text
277 adg_toy_text_get_label(AdgToyText
*toy_text
)
279 AdgToyTextPrivate
*data
;
281 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), NULL
);
283 data
= toy_text
->data
;
290 global_changed(AdgEntity
*entity
)
292 if (PARENT_ENTITY_CLASS
->global_changed
)
293 PARENT_ENTITY_CLASS
->global_changed(entity
);
295 adg_entity_invalidate(entity
);
299 local_changed(AdgEntity
*entity
)
301 if (PARENT_ENTITY_CLASS
->local_changed
)
302 PARENT_ENTITY_CLASS
->local_changed(entity
);
304 adg_entity_invalidate(entity
);
308 invalidate(AdgEntity
*entity
)
310 unset_font((AdgToyText
*) entity
);
311 unset_glyphs((AdgToyText
*) entity
);
313 if (PARENT_ENTITY_CLASS
->invalidate
)
314 PARENT_ENTITY_CLASS
->invalidate(entity
);
318 arrange(AdgEntity
*entity
)
320 AdgToyText
*toy_text
;
321 AdgToyTextPrivate
*data
;
324 toy_text
= (AdgToyText
*) entity
;
325 data
= toy_text
->data
;
327 if (data
->font
== NULL
) {
329 AdgFontStyle
*font_style
;
332 dress
= data
->font_dress
;
333 font_style
= (AdgFontStyle
*) adg_entity_style(entity
, dress
);
335 adg_matrix_copy(&ctm
, adg_entity_get_global_matrix(entity
));
336 adg_matrix_transform(&ctm
, adg_entity_get_local_matrix(entity
),
337 ADG_TRANSFORM_BEFORE
);
339 data
->font
= adg_font_style_get_scaled_font(font_style
, &ctm
);
342 if (adg_is_string_empty(data
->label
)) {
343 /* Undefined label */
344 extents
.is_defined
= FALSE
;
345 } else if (data
->glyphs
!= NULL
) {
349 cairo_status_t status
;
350 cairo_text_extents_t cairo_extents
;
352 status
= cairo_scaled_font_text_to_glyphs(data
->font
, 0, 0,
358 if (status
!= CAIRO_STATUS_SUCCESS
) {
359 unset_glyphs(toy_text
);
360 g_error("Unable to build glyphs (cairo message: %s)",
361 cairo_status_to_string(status
));
365 cairo_scaled_font_glyph_extents(data
->font
, data
->glyphs
,
366 data
->num_glyphs
, &cairo_extents
);
367 cpml_extents_from_cairo_text(&extents
, &cairo_extents
);
368 cpml_extents_transform(&extents
, adg_entity_get_local_matrix(entity
));
369 cpml_extents_transform(&extents
, adg_entity_get_global_matrix(entity
));
373 adg_entity_set_extents(entity
, &extents
);
377 render(AdgEntity
*entity
, cairo_t
*cr
)
379 AdgToyText
*toy_text
;
380 AdgToyTextPrivate
*data
;
382 toy_text
= (AdgToyText
*) entity
;
383 data
= toy_text
->data
;
385 if (data
->glyphs
!= NULL
) {
386 adg_entity_apply_dress(entity
, data
->font_dress
, cr
);
387 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
388 cairo_show_glyphs(cr
, data
->glyphs
, data
->num_glyphs
);
393 set_label(AdgToyText
*toy_text
, const gchar
*label
)
395 AdgToyTextPrivate
*data
= toy_text
->data
;
397 /* Check if the new label differs from the current label */
398 if (g_strcmp0(label
, data
->label
) == 0)
402 data
->label
= g_strdup(label
);
404 unset_glyphs(toy_text
);
409 unset_font(AdgToyText
*toy_text
)
411 AdgToyTextPrivate
*data
= toy_text
->data
;
417 unset_glyphs(AdgToyText
*toy_text
)
419 AdgToyTextPrivate
*data
= toy_text
->data
;
421 if (data
->glyphs
!= NULL
) {
422 cairo_glyph_free(data
->glyphs
);
426 data
->num_glyphs
= 0;