]> git.sur5r.net Git - glabels/blob - glabels2/src/label-text.c
Initial revision
[glabels] / glabels2 / src / label-text.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  label_text.c:  GLabels label text object
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <glib.h>
24 #include <libgnomeprint/gnome-glyphlist.h>
25
26 #include "label-text.h"
27
28 #include "pixmaps/checkerboard.xpm"
29
30 #include "debug.h"
31
32 /*========================================================*/
33 /* Private macros and constants.                          */
34 /*========================================================*/
35
36 #define DEFAULT_FONT_FAMILY      "Helvetica"
37 #define DEFAULT_FONT_SIZE        14.0
38 #define DEFAULT_FONT_WEIGHT      GNOME_FONT_BOOK
39 #define DEFAULT_FONT_ITALIC_FLAG FALSE
40 #define DEFAULT_JUST             GTK_JUSTIFY_LEFT
41 #define DEFAULT_COLOR            GNOME_CANVAS_COLOR (0,0,0)
42
43 /*========================================================*/
44 /* Private types.                                         */
45 /*========================================================*/
46
47 struct _glLabelTextPrivate {
48         GList           *lines; /* list of glLabelTextNode lists */
49         gchar           *font_family;
50         gdouble          font_size;
51         GnomeFontWeight  font_weight;
52         gboolean         font_italic_flag;
53         GtkJustification just;
54         guint            color;
55 };
56
57 /*========================================================*/
58 /* Private globals.                                       */
59 /*========================================================*/
60
61 static GObjectClass *parent_class = NULL;
62
63 static guint instance = 0;
64
65 /*========================================================*/
66 /* Private function prototypes.                           */
67 /*========================================================*/
68
69 static void gl_label_text_class_init (glLabelTextClass *klass);
70 static void gl_label_text_instance_init (glLabelText *ltext);
71 static void gl_label_text_finalize (GObject *object);
72
73 static void update_size (glLabelText *ltext);
74
75 \f
76 /*****************************************************************************/
77 /* Boilerplate object stuff.                                                 */
78 /*****************************************************************************/
79 GType
80 gl_label_text_get_type (void)
81 {
82         static GType type = 0;
83
84         if (!type) {
85                 GTypeInfo info = {
86                         sizeof (glLabelTextClass),
87                         NULL,
88                         NULL,
89                         (GClassInitFunc) gl_label_text_class_init,
90                         NULL,
91                         NULL,
92                         sizeof (glLabelText),
93                         0,
94                         (GInstanceInitFunc) gl_label_text_instance_init,
95                 };
96
97                 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
98                                                "glLabelText", &info, 0);
99         }
100
101         return type;
102 }
103
104 static void
105 gl_label_text_class_init (glLabelTextClass *klass)
106 {
107         GObjectClass *object_class = (GObjectClass *) klass;
108
109         parent_class = g_type_class_peek_parent (klass);
110
111         object_class->finalize = gl_label_text_finalize;
112 }
113
114 static void
115 gl_label_text_instance_init (glLabelText *ltext)
116 {
117         ltext->private = g_new0 (glLabelTextPrivate, 1);
118
119         ltext->private->font_family      = g_strdup(DEFAULT_FONT_FAMILY);
120         ltext->private->font_size        = DEFAULT_FONT_SIZE;
121         ltext->private->font_weight      = DEFAULT_FONT_WEIGHT;
122         ltext->private->font_italic_flag = DEFAULT_FONT_ITALIC_FLAG;
123         ltext->private->just             = DEFAULT_JUST;
124         ltext->private->color            = DEFAULT_COLOR;
125 }
126
127 static void
128 gl_label_text_finalize (GObject *object)
129 {
130         glLabelText *ltext;
131
132         g_return_if_fail (object && GL_IS_LABEL_TEXT (object));
133
134         ltext = GL_LABEL_TEXT (object);
135
136         g_free (ltext->private);
137
138         G_OBJECT_CLASS (parent_class)->finalize (object);
139 }
140
141 /*****************************************************************************/
142 /* NEW label "text" object.                                               */
143 /*****************************************************************************/
144 GObject *
145 gl_label_text_new (glLabel *label)
146 {
147         glLabelText *ltext;
148
149         ltext = g_object_new (gl_label_text_get_type(), NULL);
150
151         gl_label_object_set_parent (GL_LABEL_OBJECT(ltext), label);
152
153         return G_OBJECT (ltext);
154 }
155
156 /*****************************************************************************/
157 /* Duplicate object.                                                         */
158 /*****************************************************************************/
159 glLabelText *
160 gl_label_text_dup (glLabelText *ltext,
161                     glLabel *label)
162 {
163         glLabelText      *new_ltext;
164         gdouble          x, y, w, h;
165         GList            *lines;
166         gchar            *font_family;
167         gdouble          font_size;
168         GnomeFontWeight  font_weight;
169         gboolean         font_italic_flag;
170         guint            color;
171         GtkJustification just;
172
173         gl_debug (DEBUG_LABEL, "START");
174
175         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
176         g_return_if_fail (label && GL_IS_LABEL (label));
177
178         new_ltext = GL_LABEL_TEXT(gl_label_text_new (label));
179
180         gl_label_object_get_position (GL_LABEL_OBJECT(ltext), &x, &y);
181         gl_label_object_get_size     (GL_LABEL_OBJECT(ltext), &w, &h);
182
183         gl_label_object_set_position (GL_LABEL_OBJECT(new_ltext),  x,  y);
184         gl_label_object_set_size     (GL_LABEL_OBJECT(new_ltext),  w,  h);
185
186         lines = gl_label_text_get_lines (ltext);
187         gl_label_text_get_props (ltext,
188                                  &font_family, &font_size, &font_weight,
189                                  &font_italic_flag,
190                                  &color, &just);
191
192         gl_label_text_set_lines (new_ltext, lines);
193         gl_label_text_set_props (new_ltext,
194                                  font_family, font_size, font_weight,
195                                  font_italic_flag,
196                                  color, just);
197
198         gl_text_node_lines_free (&lines);
199         g_free (font_family);
200
201         gl_debug (DEBUG_LABEL, "END");
202
203         return new_ltext;
204 }
205
206
207 /*****************************************************************************/
208 /* Set object params.                                                        */
209 /*****************************************************************************/
210 void
211 gl_label_text_set_lines (glLabelText *ltext,
212                          GList *lines)
213 {
214         gl_debug (DEBUG_LABEL, "START");
215
216         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
217
218         gl_text_node_lines_free (&ltext->private->lines);
219         ltext->private->lines = gl_text_node_lines_dup (lines);
220
221         update_size (ltext);
222
223         gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
224
225         gl_debug (DEBUG_LABEL, "END");
226 }
227
228 void
229 gl_label_text_set_props (glLabelText *ltext,
230                          gchar *font_family,
231                          gdouble font_size,
232                          GnomeFontWeight font_weight,
233                          gboolean font_italic_flag,
234                          guint color,
235                          GtkJustification just)
236 {
237         GdkPixbuf *pixbuf;
238
239         gl_debug (DEBUG_LABEL, "START");
240
241         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
242
243         g_free (ltext->private->font_family);
244
245         ltext->private->font_family      = g_strdup (font_family);
246         ltext->private->font_size        = font_size;
247         ltext->private->font_weight      = font_weight;
248         ltext->private->font_italic_flag = font_italic_flag;
249         ltext->private->color            = color;
250         ltext->private->just             = just;
251
252         gl_debug (DEBUG_LABEL, "just = %d", ltext->private->just);
253
254         update_size (ltext);
255
256         gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
257
258         gl_debug (DEBUG_LABEL, "END");
259 }
260
261
262 /*****************************************************************************/
263 /* Get object params.                                                        */
264 /*****************************************************************************/
265 GList *
266 gl_label_text_get_lines (glLabelText *ltext)
267 {
268         g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
269
270         return gl_text_node_lines_dup (ltext->private->lines);
271 }
272
273 void
274 gl_label_text_get_props (glLabelText *ltext,
275                          gchar **font_family,
276                          gdouble *font_size,
277                          GnomeFontWeight *font_weight,
278                          gboolean *font_italic_flag,
279                          guint *color,
280                          GtkJustification *just)
281 {
282         g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
283
284         *font_family      = g_strdup (ltext->private->font_family);
285         *font_size        = ltext->private->font_size;
286         *font_weight      = ltext->private->font_weight;
287         *font_italic_flag = ltext->private->font_italic_flag;
288         *color            = ltext->private->color;
289         *just             = ltext->private->just;
290
291         gl_debug (DEBUG_LABEL, "just = %d", *just);
292 }
293
294 /*---------------------------------------------------------------------------*/
295 /* PRIVATE.  Update object size.                                             */
296 /*---------------------------------------------------------------------------*/
297 static void
298 update_size (glLabelText *ltext)
299 {
300         gdouble w, h;
301         GnomeFont *font;
302         gchar *text;
303         gchar **line;
304         gint i;
305         GnomeGlyphList *glyphlist;
306         ArtDRect bbox;
307         gdouble affine[6];
308
309         gl_debug (DEBUG_LABEL, "START");
310
311         font = gnome_font_find_closest_from_weight_slant (
312                 ltext->private->font_family,
313                 ltext->private->font_weight,
314                 ltext->private->font_italic_flag,
315                 ltext->private->font_size);
316
317         text = gl_text_node_lines_expand (ltext->private->lines, NULL);
318         line = g_strsplit (text, "\n", -1);
319         g_free (text);
320
321         art_affine_identity (affine);
322
323         w = 0.0;
324         h = 0.0;
325         for (i = 0; line[i] != NULL; i++) {
326
327                 glyphlist = gnome_glyphlist_from_text_dumb (font, 0,
328                                                             0.0, 0.0,
329                                                             line[i]);
330
331                 gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox);
332
333                 if ( bbox.x1 > w ) w = bbox.x1;
334
335                 h += ltext->private->font_size;
336
337         }
338
339         g_strfreev (line);
340
341         gl_label_object_set_size (GL_LABEL_OBJECT(ltext), w, h);
342
343         gl_debug (DEBUG_LABEL, "END");
344 }
345