[AdgLDim] Using the new clear API
[adg.git] / adg / adg-toy-text.c
blob1cc01b53ebc684740a789200538a1f246dfd67b4
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-font-style.h"
42 #include "adg-intl.h"
44 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_toy_text_parent_class)
45 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_toy_text_parent_class)
48 enum {
49 PROP_0,
50 PROP_DRESS,
51 PROP_LABEL
55 static void finalize (GObject *object);
56 static void get_property (GObject *object,
57 guint param_id,
58 GValue *value,
59 GParamSpec *pspec);
60 static void set_property (GObject *object,
61 guint param_id,
62 const GValue *value,
63 GParamSpec *pspec);
64 static void global_changed (AdgEntity *entity);
65 static void invalidate (AdgEntity *entity);
66 static void arrange (AdgEntity *entity);
67 static void render (AdgEntity *entity,
68 cairo_t *cr);
69 static gboolean set_dress (AdgToyText *toy_text,
70 AdgDress dress);
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->invalidate = invalidate;
98 entity_class->arrange = arrange;
99 entity_class->render = render;
101 param = adg_param_spec_dress("dress",
102 P_("Font Dress"),
103 P_("The font dress to use for rendering this text"),
104 ADG_DRESS_TEXT_REGULAR,
105 G_PARAM_READWRITE);
106 g_object_class_install_property(gobject_class, PROP_DRESS, param);
108 param = g_param_spec_string("label",
109 P_("Label"),
110 P_("The label to display"),
111 NULL,
112 G_PARAM_READWRITE);
113 g_object_class_install_property(gobject_class, PROP_LABEL, param);
116 static void
117 adg_toy_text_init(AdgToyText *toy_text)
119 AdgToyTextPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(toy_text,
120 ADG_TYPE_TOY_TEXT,
121 AdgToyTextPrivate);
123 data->dress = ADG_DRESS_TEXT_REGULAR;
124 data->label = NULL;
125 data->glyphs = NULL;
127 adg_entity_set_local_mode((AdgEntity *) toy_text,
128 ADG_TRANSFORM_BEFORE_NORMALIZED);
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 != 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_DRESS:
157 g_value_set_int(value, data->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_DRESS:
176 set_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_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_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->dress;
223 * adg_toy_text_set_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_dress(AdgToyText *toy_text, AdgDress dress)
239 g_return_if_fail(ADG_IS_TOY_TEXT(toy_text));
241 if (set_dress(toy_text, dress))
242 g_object_notify((GObject *) toy_text, "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;
314 AdgMatrix ctm;
316 font_style = (AdgFontStyle *) adg_entity_style(entity, data->dress);
317 adg_entity_get_ctm(entity, &ctm);
319 data->font = adg_font_style_font(font_style, &ctm);
322 if (data->label == NULL || data->label[0] == '\0') {
323 /* Undefined label */
324 extents.is_defined = FALSE;
325 adg_entity_set_extents(entity, &extents);
326 return;
329 if (data->glyphs == NULL) {
330 cairo_status_t status;
331 cairo_text_extents_t cairo_extents;
333 status = cairo_scaled_font_text_to_glyphs(data->font, 0, 0,
334 data->label, -1,
335 &data->glyphs,
336 &data->num_glyphs,
337 NULL, NULL, NULL);
339 if (status != CAIRO_STATUS_SUCCESS) {
340 unset_glyphs(toy_text);
341 g_error("Unable to build glyphs (cairo message: %s)",
342 cairo_status_to_string(status));
343 return;
346 cairo_scaled_font_glyph_extents(data->font, data->glyphs,
347 data->num_glyphs, &cairo_extents);
348 cpml_extents_from_cairo_text(&extents, &cairo_extents);
349 adg_entity_get_local_map(entity, &local);
350 cairo_matrix_transform_point(&local, &extents.org.x, &extents.org.y);
351 cairo_matrix_transform_distance(&local, &extents.size.x, &extents.size.y);
353 adg_entity_set_extents(entity, &extents);
357 static void
358 render(AdgEntity *entity, cairo_t *cr)
360 AdgToyText *toy_text;
361 AdgToyTextPrivate *data;
363 toy_text = (AdgToyText *) entity;
364 data = toy_text->data;
366 if (data->glyphs != NULL) {
367 AdgMatrix ctm;
369 adg_entity_get_ctm(entity, &ctm);
371 cairo_set_scaled_font(cr, data->font);
372 cairo_set_matrix(cr, &ctm);
373 cairo_show_glyphs(cr, data->glyphs, data->num_glyphs);
377 static gboolean
378 set_dress(AdgToyText *toy_text, AdgDress dress)
380 AdgToyTextPrivate *data = toy_text->data;
382 if (adg_dress_set(&data->dress, dress)) {
383 unset_font(toy_text);
384 return TRUE;
387 return FALSE;
390 static gboolean
391 set_label(AdgToyText *toy_text, const gchar *label)
393 AdgToyTextPrivate *data = toy_text->data;
395 /* Check if the new label differs from the current label */
396 if (adg_strcmp(label, data->label) == 0)
397 return FALSE;
399 g_free(data->label);
400 data->label = g_strdup(label);
402 unset_glyphs(toy_text);
403 return TRUE;
406 static void
407 unset_font(AdgToyText *toy_text)
409 AdgToyTextPrivate *data = toy_text->data;
411 data->font = NULL;
414 static void
415 unset_glyphs(AdgToyText *toy_text)
417 AdgToyTextPrivate *data = toy_text->data;
419 if (data->glyphs != NULL) {
420 cairo_glyph_free(data->glyphs);
421 data->glyphs = NULL;
424 data->num_glyphs = 0;