[build] Bumped version to 0.5.3
[adg.git] / adg / adg-toy-text.c
blobac36449eddd8c4ee66080dd16f5b60b167cfd463
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.
21 /**
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.
29 **/
31 /**
32 * AdgToyText:
34 * All fields are privates and should not be used directly.
35 * Use its public methods instead.
36 **/
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"
43 #include "adg-intl.h"
45 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_toy_text_parent_class)
46 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_toy_text_parent_class)
49 enum {
50 PROP_0,
51 PROP_FONT_DRESS,
52 PROP_LABEL
56 static void finalize (GObject *object);
57 static void get_property (GObject *object,
58 guint param_id,
59 GValue *value,
60 GParamSpec *pspec);
61 static void set_property (GObject *object,
62 guint param_id,
63 const GValue *value,
64 GParamSpec *pspec);
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,
69 cairo_t *cr);
70 static gboolean set_font_dress (AdgToyText *toy_text,
71 AdgDress dress);
72 static gboolean set_label (AdgToyText *toy_text,
73 const gchar *label);
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);
81 static void
82 adg_toy_text_class_init(AdgToyTextClass *klass)
84 GObjectClass *gobject_class;
85 AdgEntityClass *entity_class;
86 GParamSpec *param;
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",
103 P_("Font Dress"),
104 P_("The font dress to use for rendering this text"),
105 ADG_DRESS_TEXT,
106 G_PARAM_READWRITE);
107 g_object_class_install_property(gobject_class, PROP_FONT_DRESS, param);
109 param = g_param_spec_string("label",
110 P_("Label"),
111 P_("The label to display"),
112 NULL,
113 G_PARAM_READWRITE);
114 g_object_class_install_property(gobject_class, PROP_LABEL, param);
117 static void
118 adg_toy_text_init(AdgToyText *toy_text)
120 AdgToyTextPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(toy_text,
121 ADG_TYPE_TOY_TEXT,
122 AdgToyTextPrivate);
124 data->font_dress = ADG_DRESS_TEXT;
125 data->label = NULL;
126 data->glyphs = NULL;
128 toy_text->data = data;
130 adg_entity_set_normalized((AdgEntity *) toy_text, TRUE);
133 static void
134 finalize(GObject *object)
136 AdgToyText *toy_text;
137 AdgToyTextPrivate *data;
139 toy_text = (AdgToyText *) object;
140 data = toy_text->data;
142 g_free(data->label);
143 unset_font(toy_text);
144 unset_glyphs(toy_text);
146 if (PARENT_OBJECT_CLASS->finalize != NULL)
147 PARENT_OBJECT_CLASS->finalize(object);
150 static void
151 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
153 AdgToyTextPrivate *data = ((AdgToyText *) object)->data;
155 switch (prop_id) {
156 case PROP_FONT_DRESS:
157 g_value_set_int(value, data->font_dress);
158 break;
159 case PROP_LABEL:
160 g_value_set_string(value, data->label);
161 break;
162 default:
163 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
164 break;
168 static void
169 set_property(GObject *object, guint prop_id,
170 const GValue *value, GParamSpec *pspec)
172 AdgToyText *toy_text = (AdgToyText *) object;
174 switch (prop_id) {
175 case PROP_FONT_DRESS:
176 set_font_dress(toy_text, g_value_get_int(value));
177 break;
178 case PROP_LABEL:
179 set_label(toy_text, g_value_get_string(value));
180 break;
181 default:
182 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
183 break;
189 * adg_toy_text_new:
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
196 AdgToyText *
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
210 AdgDress
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
230 * managing fonts.
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.
236 void
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
254 const gchar *
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;
263 return data->label;
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.
274 void
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");
284 static void
285 global_changed(AdgEntity *entity)
287 PARENT_ENTITY_CLASS->global_changed(entity);
288 unset_font((AdgToyText *) entity);
291 static void
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);
301 static void
302 arrange(AdgEntity *entity)
304 AdgToyText *toy_text;
305 AdgToyTextPrivate *data;
306 AdgMatrix local;
307 CpmlExtents extents;
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);
323 return;
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,
331 data->label, -1,
332 &data->glyphs,
333 &data->num_glyphs,
334 NULL, NULL, NULL);
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));
340 return;
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);
354 static void
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);
370 static gboolean
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);
377 return TRUE;
380 return FALSE;
383 static gboolean
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)
390 return FALSE;
392 g_free(data->label);
393 data->label = g_strdup(label);
395 unset_glyphs(toy_text);
396 return TRUE;
399 static void
400 unset_font(AdgToyText *toy_text)
402 AdgToyTextPrivate *data = toy_text->data;
404 data->font = NULL;
407 static void
408 unset_glyphs(AdgToyText *toy_text)
410 AdgToyTextPrivate *data = toy_text->data;
412 if (data->glyphs != NULL) {
413 cairo_glyph_free(data->glyphs);
414 data->glyphs = NULL;
417 data->num_glyphs = 0;