[AdgDim] Invalidate also the quote value text
[adg.git] / src / adg / adg-toy-text.c
blobe69ab609667c869ba4260f00660edcd36cc2c4a3
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.
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-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)
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_label (AdgToyText *toy_text,
72 const gchar *label);
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);
80 static void
81 adg_toy_text_class_init(AdgToyTextClass *klass)
83 GObjectClass *gobject_class;
84 AdgEntityClass *entity_class;
85 GParamSpec *param;
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",
103 P_("Font Dress"),
104 P_("The font dress to use for rendering this text"),
105 ADG_DRESS_FONT_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_FONT_TEXT;
125 data->label = NULL;
126 data->glyphs = NULL;
128 toy_text->data = data;
131 static void
132 finalize(GObject *object)
134 AdgToyText *toy_text;
135 AdgToyTextPrivate *data;
137 toy_text = (AdgToyText *) object;
138 data = toy_text->data;
140 g_free(data->label);
141 unset_font(toy_text);
142 unset_glyphs(toy_text);
144 if (PARENT_OBJECT_CLASS->finalize)
145 PARENT_OBJECT_CLASS->finalize(object);
148 static void
149 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
151 AdgToyTextPrivate *data = ((AdgToyText *) object)->data;
153 switch (prop_id) {
154 case PROP_FONT_DRESS:
155 g_value_set_int(value, data->font_dress);
156 break;
157 case PROP_LABEL:
158 g_value_set_string(value, data->label);
159 break;
160 default:
161 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
162 break;
166 static void
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;
176 switch (prop_id) {
177 case PROP_FONT_DRESS:
178 data->font_dress = g_value_get_int(value);
179 unset_font(toy_text);
180 break;
181 case PROP_LABEL:
182 set_label(toy_text, g_value_get_string(value));
183 break;
184 default:
185 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
186 break;
192 * adg_toy_text_new:
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
201 AdgToyText *
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
217 * managing fonts.
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.
223 void
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
238 AdgDress
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.
258 void
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
276 const gchar *
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;
285 return data->label;
289 static void
290 global_changed(AdgEntity *entity)
292 if (PARENT_ENTITY_CLASS->global_changed)
293 PARENT_ENTITY_CLASS->global_changed(entity);
295 adg_entity_invalidate(entity);
298 static void
299 local_changed(AdgEntity *entity)
301 if (PARENT_ENTITY_CLASS->local_changed)
302 PARENT_ENTITY_CLASS->local_changed(entity);
304 adg_entity_invalidate(entity);
307 static void
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);
317 static void
318 arrange(AdgEntity *entity)
320 AdgToyText *toy_text;
321 AdgToyTextPrivate *data;
322 CpmlExtents extents;
324 toy_text = (AdgToyText *) entity;
325 data = toy_text->data;
327 if (data->font == NULL) {
328 AdgDress dress;
329 AdgFontStyle *font_style;
330 AdgMatrix ctm;
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) {
346 /* Cached result */
347 return;
348 } else {
349 cairo_status_t status;
350 cairo_text_extents_t cairo_extents;
352 status = cairo_scaled_font_text_to_glyphs(data->font, 0, 0,
353 data->label, -1,
354 &data->glyphs,
355 &data->num_glyphs,
356 NULL, NULL, NULL);
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));
362 return;
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);
376 static void
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);
392 static gboolean
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)
399 return FALSE;
401 g_free(data->label);
402 data->label = g_strdup(label);
404 unset_glyphs(toy_text);
405 return TRUE;
408 static void
409 unset_font(AdgToyText *toy_text)
411 AdgToyTextPrivate *data = toy_text->data;
413 data->font = NULL;
416 static void
417 unset_glyphs(AdgToyText *toy_text)
419 AdgToyTextPrivate *data = toy_text->data;
421 if (data->glyphs != NULL) {
422 cairo_glyph_free(data->glyphs);
423 data->glyphs = NULL;
426 data->num_glyphs = 0;