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.
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.
29 #include "adg-toy-text.h"
30 #include "adg-toy-text-private.h"
31 #include "adg-translatable.h"
32 #include "adg-rotable.h"
33 #include "adg-font-style.h"
36 #define PARENT_CLASS ((AdgEntityClass *) adg_toy_text_parent_class)
46 static void translatable_init (AdgTranslatableIface
*iface
);
47 static void get_origin (AdgTranslatable
*translatable
,
49 static void set_origin (AdgTranslatable
*translatable
,
50 const AdgPoint
*origin
);
52 static void rotable_init (AdgRotableIface
*iface
);
53 static gdouble
get_angle (AdgRotable
*rotable
);
54 static void set_angle (AdgRotable
*rotable
,
57 static void finalize (GObject
*object
);
58 static void get_property (GObject
*object
,
62 static void set_property (GObject
*object
,
66 static void model_matrix_changed (AdgEntity
*entity
,
67 AdgMatrix
*parent_matrix
);
68 static void invalidate (AdgEntity
*entity
);
69 static void render (AdgEntity
*entity
,
71 static gboolean
update_origin_cache (AdgToyText
*toy_text
,
73 static gboolean
update_label_cache (AdgToyText
*toy_text
,
75 static void clear_label_cache (AdgToyText
*toy_text
);
76 static void clear_origin_cache (AdgToyText
*toy_text
);
79 G_DEFINE_TYPE_WITH_CODE(AdgToyText
, adg_toy_text
, ADG_TYPE_ENTITY
,
80 G_IMPLEMENT_INTERFACE(ADG_TYPE_TRANSLATABLE
,
82 G_IMPLEMENT_INTERFACE(ADG_TYPE_ROTABLE
, rotable_init
))
86 translatable_init(AdgTranslatableIface
*iface
)
88 iface
->get_origin
= get_origin
;
89 iface
->set_origin
= set_origin
;
93 get_origin(AdgTranslatable
*translatable
, AdgPoint
*dest
)
95 AdgToyText
*toy_text
= (AdgToyText
*) translatable
;
96 adg_point_copy(dest
, &toy_text
->priv
->origin
);
100 set_origin(AdgTranslatable
*translatable
, const AdgPoint
*origin
)
102 AdgToyText
*toy_text
= (AdgToyText
*) translatable
;
103 adg_point_copy(&toy_text
->priv
->origin
, origin
);
108 rotable_init(AdgRotableIface
*iface
)
110 iface
->get_angle
= get_angle
;
111 iface
->set_angle
= set_angle
;
115 get_angle(AdgRotable
*rotable
)
117 AdgToyText
*toy_text
= (AdgToyText
*) rotable
;
118 return toy_text
->priv
->angle
;
122 set_angle(AdgRotable
*rotable
, gdouble angle
)
124 AdgToyText
*toy_text
= (AdgToyText
*) rotable
;
125 toy_text
->priv
->angle
= angle
;
130 adg_toy_text_class_init(AdgToyTextClass
*klass
)
132 GObjectClass
*gobject_class
;
133 AdgEntityClass
*entity_class
;
136 gobject_class
= (GObjectClass
*) klass
;
137 entity_class
= (AdgEntityClass
*) klass
;
139 g_type_class_add_private(klass
, sizeof(AdgToyTextPrivate
));
141 gobject_class
->finalize
= finalize
;
142 gobject_class
->get_property
= get_property
;
143 gobject_class
->set_property
= set_property
;
145 entity_class
->model_matrix_changed
= model_matrix_changed
;
146 entity_class
->invalidate
= invalidate
;
147 entity_class
->render
= render
;
149 g_object_class_override_property(gobject_class
, PROP_ORIGIN
, "origin");
150 g_object_class_override_property(gobject_class
, PROP_ANGLE
, "angle");
152 param
= g_param_spec_string("label",
154 P_("The label to display"),
155 NULL
, G_PARAM_READWRITE
);
156 g_object_class_install_property(gobject_class
, PROP_LABEL
, param
);
160 adg_toy_text_init(AdgToyText
*toy_text
)
162 AdgToyTextPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE(toy_text
,
167 adg_point_unset(&priv
->origin
);
169 priv
->origin_cached
= FALSE
;
172 toy_text
->priv
= priv
;
176 finalize(GObject
*object
)
178 AdgToyText
*toy_text
= (AdgToyText
*) object
;
180 g_free(toy_text
->priv
->label
);
181 clear_label_cache(toy_text
);
182 clear_origin_cache(toy_text
);
184 ((GObjectClass
*) PARENT_CLASS
)->finalize(object
);
188 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
190 AdgToyText
*toy_text
= (AdgToyText
*) object
;
194 g_value_set_boxed(value
, &toy_text
->priv
->origin
);
197 g_value_set_double(value
, toy_text
->priv
->angle
);
200 g_value_set_string(value
, toy_text
->priv
->label
);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
209 set_property(GObject
*object
, guint prop_id
,
210 const GValue
*value
, GParamSpec
*pspec
)
212 AdgToyText
*toy_text
= (AdgToyText
*) object
;
216 adg_point_copy(&toy_text
->priv
->origin
,
217 (AdgPoint
*) g_value_get_boxed(value
));
218 clear_origin_cache(toy_text
);
221 toy_text
->priv
->angle
= g_value_get_double(value
);
222 clear_origin_cache(toy_text
);
225 g_free(toy_text
->priv
->label
);
226 toy_text
->priv
->label
= g_value_dup_string(value
);
227 clear_label_cache(toy_text
);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
238 * @label: the label text
240 * Creates a new toy text entity using @label as its text
242 * Return value: the new entity
245 adg_toy_text_new(const gchar
*label
)
247 return (AdgEntity
*) g_object_new(ADG_TYPE_TOY_TEXT
, "label", label
, NULL
);
251 * adg_toy_text_get_label:
252 * @toy_text: an #AdgToyText
254 * Gets the label text. The string is internally owned and
255 * must not be freed or modified.
257 * Return value: the label text
260 adg_toy_text_get_label(AdgToyText
*toy_text
)
262 g_return_val_if_fail(ADG_IS_TOY_TEXT(toy_text
), NULL
);
264 return toy_text
->priv
->label
;
268 * adg_toy_text_set_label:
269 * @toy_text: an #AdgToyText
270 * @label: the label text
272 * Explicitely sets the text to use as label.
275 adg_toy_text_set_label(AdgToyText
*toy_text
, const gchar
*label
)
277 g_return_if_fail(ADG_IS_TOY_TEXT(label
));
279 g_free(toy_text
->priv
->label
);
280 toy_text
->priv
->label
= g_strdup(label
);
281 g_object_notify((GObject
*) toy_text
, "label");
283 clear_label_cache(toy_text
);
288 render(AdgEntity
*entity
, cairo_t
*cr
)
290 AdgToyText
*toy_text
= (AdgToyText
*) entity
;
291 AdgToyTextPrivate
*priv
= toy_text
->priv
;
294 AdgStyle
*font_style
;
296 font_style
= adg_entity_get_style(entity
, ADG_SLOT_FONT_STYLE
);
299 cairo_set_matrix(cr
, adg_entity_get_paper_matrix(entity
));
300 adg_style_apply(font_style
, cr
);
301 cairo_rotate(cr
, priv
->angle
);
304 update_label_cache(toy_text
, cr
);
305 if (!priv
->origin_cached
)
306 update_origin_cache(toy_text
, cr
);
308 cairo_show_glyphs(cr
, priv
->glyphs
, priv
->num_glyphs
);
312 PARENT_CLASS
->render(entity
, cr
);
316 model_matrix_changed(AdgEntity
*entity
, AdgMatrix
*parent_matrix
)
318 clear_origin_cache((AdgToyText
*) entity
);
319 PARENT_CLASS
->model_matrix_changed(entity
, parent_matrix
);
323 invalidate(AdgEntity
*entity
)
325 AdgToyText
*toy_text
= (AdgToyText
*) entity
;
327 clear_label_cache(toy_text
);
328 clear_origin_cache(toy_text
);
330 PARENT_CLASS
->invalidate(entity
);
334 update_origin_cache(AdgToyText
*toy_text
, cairo_t
*cr
)
336 AdgToyTextPrivate
*priv
;
339 cairo_glyph_t
*glyph
;
343 priv
= toy_text
->priv
;
344 adg_point_copy(&point
, &priv
->origin
);
345 pair
= &priv
->origin_pair
;
346 glyph
= priv
->glyphs
;
347 cnt
= priv
->num_glyphs
;
349 /* On undefined label return error */
350 if (glyph
== NULL
|| cnt
<= 0)
353 if (priv
->angle
!= 0.) {
354 /* Following the less surprise rule, also the paper component
355 * of the origin point should rotate with the provided angle */
356 cairo_matrix_t rotation
;
357 cairo_matrix_init_rotate(&rotation
, priv
->angle
);
358 cpml_pair_transform(&point
.paper
, &rotation
);
361 adg_entity_point_to_pair((AdgEntity
*) toy_text
, &point
, pair
, cr
);
362 priv
->origin_cached
= TRUE
;
364 /* Check if the origin is still the same */
365 if (pair
->x
== glyph
->x
&& pair
->y
== glyph
->y
)
368 x
= pair
->x
- glyph
->x
;
369 y
= pair
->y
- glyph
->y
;
381 update_label_cache(AdgToyText
*toy_text
, cairo_t
*cr
)
383 AdgToyTextPrivate
*priv
= toy_text
->priv
;
384 cairo_status_t status
;
386 status
= cairo_scaled_font_text_to_glyphs(cairo_get_scaled_font(cr
),
387 0., 0., priv
->label
, -1,
392 if (status
!= CAIRO_STATUS_SUCCESS
) {
393 g_error("Unable to build glyphs (cairo message: %s)",
394 cairo_status_to_string(status
));
398 cairo_glyph_extents(cr
, priv
->glyphs
, priv
->num_glyphs
, &priv
->extents
);
400 clear_origin_cache(toy_text
);
405 clear_origin_cache(AdgToyText
*toy_text
)
407 toy_text
->priv
->origin_cached
= FALSE
;
411 clear_label_cache(AdgToyText
*toy_text
)
413 AdgToyTextPrivate
*priv
= toy_text
->priv
;
416 cairo_glyph_free(priv
->glyphs
);
420 priv
->num_glyphs
= 0;
421 memset(&priv
->extents
, 0, sizeof(priv
->extents
));