[docs] Updated TODO.xml
[adg.git] / adg / adg-toy-text.c
blobaa3fe44250451a0114bfb346b759c5943d50e12d
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 local_changed (AdgEntity *entity);
67 static void invalidate (AdgEntity *entity);
68 static void arrange (AdgEntity *entity);
69 static void render (AdgEntity *entity,
70 cairo_t *cr);
71 static gboolean set_font_dress (AdgToyText *toy_text,
72 AdgDress dress);
73 static gboolean set_label (AdgToyText *toy_text,
74 const gchar *label);
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);
82 static void
83 adg_toy_text_class_init(AdgToyTextClass *klass)
85 GObjectClass *gobject_class;
86 AdgEntityClass *entity_class;
87 GParamSpec *param;
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",
105 P_("Font Dress"),
106 P_("The font dress to use for rendering this text"),
107 ADG_DRESS_TEXT,
108 G_PARAM_READWRITE);
109 g_object_class_install_property(gobject_class, PROP_FONT_DRESS, param);
111 param = g_param_spec_string("label",
112 P_("Label"),
113 P_("The label to display"),
114 NULL,
115 G_PARAM_READWRITE);
116 g_object_class_install_property(gobject_class, PROP_LABEL, param);
119 static void
120 adg_toy_text_init(AdgToyText *toy_text)
122 AdgToyTextPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(toy_text,
123 ADG_TYPE_TOY_TEXT,
124 AdgToyTextPrivate);
126 data->font_dress = ADG_DRESS_TEXT;
127 data->label = NULL;
128 data->glyphs = NULL;
130 toy_text->data = data;
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)
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. The
193 * #AdgEntity:local-method property is set by default to
194 * #ADG_LOCAL_NORMALIZED.
196 * Returns: the newly created toy text entity
198 AdgToyText *
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
214 * managing fonts.
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.
220 void
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
237 AdgDress
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.
257 void
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
275 const gchar *
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;
284 return data->label;
288 static void
289 global_changed(AdgEntity *entity)
291 if (PARENT_ENTITY_CLASS->global_changed)
292 PARENT_ENTITY_CLASS->global_changed(entity);
294 adg_entity_invalidate(entity);
297 static void
298 local_changed(AdgEntity *entity)
300 if (PARENT_ENTITY_CLASS->local_changed)
301 PARENT_ENTITY_CLASS->local_changed(entity);
303 adg_entity_invalidate(entity);
306 static void
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);
316 static void
317 arrange(AdgEntity *entity)
319 AdgToyText *toy_text;
320 AdgToyTextPrivate *data;
321 CpmlExtents extents;
323 toy_text = (AdgToyText *) entity;
324 data = toy_text->data;
326 if (data->font == NULL) {
327 AdgDress dress;
328 AdgFontStyle *font_style;
329 AdgMatrix ctm;
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) {
345 /* Cached result */
346 return;
347 } else {
348 cairo_status_t status;
349 cairo_text_extents_t cairo_extents;
351 status = cairo_scaled_font_text_to_glyphs(data->font, 0, 0,
352 data->label, -1,
353 &data->glyphs,
354 &data->num_glyphs,
355 NULL, NULL, NULL);
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));
361 return;
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);
375 static void
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);
391 static gboolean
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);
398 return TRUE;
401 return FALSE;
404 static gboolean
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)
411 return FALSE;
413 g_free(data->label);
414 data->label = g_strdup(label);
416 unset_glyphs(toy_text);
417 return TRUE;
420 static void
421 unset_font(AdgToyText *toy_text)
423 AdgToyTextPrivate *data = toy_text->data;
425 data->font = NULL;
428 static void
429 unset_glyphs(AdgToyText *toy_text)
431 AdgToyTextPrivate *data = toy_text->data;
433 if (data->glyphs != NULL) {
434 cairo_glyph_free(data->glyphs);
435 data->glyphs = NULL;
438 data->num_glyphs = 0;