]> git.sur5r.net Git - glabels/blob - glabels2/src/xml-label-04.c
2004-01-06 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / xml-label-04.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  xml-label-04.c:  GLabels xml label compat module
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 <config.h>
24
25 #include <gnome.h>
26
27 #include "xml-label-04.h"
28 #include "label-text.h"
29 #include "label-box.h"
30 #include "label-line.h"
31 #include "label-ellipse.h"
32 #include "label-image.h"
33 #include "label-barcode.h"
34 #include <libglabels/xml.h>
35
36 #include "debug.h"
37
38 static gboolean xml04_parse_media_description (xmlNodePtr node,
39                                                glLabel *label);
40 static void     xml04_parse_object            (xmlNodePtr node,
41                                                glLabelObject *object);
42 static void     xml04_parse_text_props        (xmlNodePtr node,
43                                                glLabelText *object);
44 static void     xml04_parse_box_props         (xmlNodePtr node,
45                                                glLabelBox *object);
46 static void     xml04_parse_line_props        (xmlNodePtr node,
47                                                glLabelLine *object);
48 static void     xml04_parse_ellipse_props     (xmlNodePtr node,
49                                                glLabelEllipse *object);
50 static void     xml04_parse_image_props       (xmlNodePtr node,
51                                                glLabelImage *object);
52 static void     xml04_parse_barcode_props     (xmlNodePtr node,
53                                                glLabelBarcode *object);
54 static void     xml04_parse_merge_properties  (xmlNodePtr node,
55                                                glLabel *label);
56
57 \f
58 /****************************************************************************/
59 /* PRIVATE.  Parse xml doc structure and create label.                      */
60 /****************************************************************************/
61 glLabel      *gl_xml_label_04_parse      (xmlNodePtr       root,
62                                           glXMLLabelStatus *status)
63 {
64         glLabel       *label;
65         xmlNodePtr    node;
66         xmlNsPtr      ns;
67         GObject       *object;
68         gboolean      rotate_flag;
69
70         gl_debug (DEBUG_XML, "START");
71
72         *status = XML_LABEL_OK;
73
74         if (!xmlStrEqual (root->name, "Label")) {
75                 g_warning (_("Bad root node = \"%s\""), root->name);
76                 *status = XML_LABEL_ERROR_OPEN_PARSE;
77                 return NULL;
78         }
79
80         label = GL_LABEL (gl_label_new ());
81
82         rotate_flag = gl_xml_get_prop_boolean (root, "rotate", FALSE);
83         gl_label_set_rotate_flag (label, rotate_flag);
84
85         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
86
87                 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
88
89                 if (!xmlNodeIsText (node)) {
90                         if (xmlStrEqual (node->name, "Media_Type")) {
91                                 if (!xml04_parse_media_description (node, label)) {
92                                         *status = XML_LABEL_UNKNOWN_MEDIA;
93                                 }
94                         } else if (xmlStrEqual (node->name, "Text")) {
95                                 object = gl_label_text_new (label);
96                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
97                                 xml04_parse_text_props (node, GL_LABEL_TEXT(object));
98                         } else if (xmlStrEqual (node->name, "Box")) {
99                                 object = gl_label_box_new (label);
100                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
101                                 xml04_parse_box_props (node, GL_LABEL_BOX(object));
102                         } else if (xmlStrEqual (node->name, "Line")) {
103                                 object = gl_label_line_new (label);
104                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
105                                 xml04_parse_line_props (node, GL_LABEL_LINE(object));
106                         } else if (xmlStrEqual (node->name, "Ellipse")) {
107                                 object = gl_label_ellipse_new (label);
108                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
109                                 xml04_parse_ellipse_props (node,
110                                                            GL_LABEL_ELLIPSE(object));
111                         } else if (xmlStrEqual (node->name, "Image")) {
112                                 object = gl_label_image_new (label);
113                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
114                                 xml04_parse_image_props (node, GL_LABEL_IMAGE(object));
115                         } else if (xmlStrEqual (node->name, "Barcode")) {
116                                 object = gl_label_barcode_new (label);
117                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
118                                 xml04_parse_barcode_props (node,
119                                                            GL_LABEL_BARCODE(object));
120                         } else if (xmlStrEqual (node->name, "Merge_Properties")) {
121                                 xml04_parse_merge_properties (node, label);
122                         } else {
123                                 g_warning (_("bad node =  \"%s\""), node->name);
124                         }
125                 }
126         }
127
128         gl_debug (DEBUG_XML, "END");
129
130         return label;
131 }
132
133 /*--------------------------------------------------------------------------*/
134 /* PRIVATE.  Parse Media Description node.                                  */
135 /*--------------------------------------------------------------------------*/
136 static gboolean
137 xml04_parse_media_description (xmlNodePtr node,
138                                glLabel    *label)
139 {
140         gchar      *template_name;
141         glTemplate *template;
142         gboolean   ret;
143
144         gl_debug (DEBUG_XML, "START");
145
146         template_name = xmlNodeGetContent (node);
147
148         template = gl_template_from_name (template_name);
149         if (template == NULL) {
150                 g_warning ("Undefined template \"%s\"", template_name);
151                 /* Get a default */
152                 template = gl_template_from_name (NULL);
153                 ret = FALSE;
154         } else {
155                 ret = TRUE;
156         }
157
158         gl_label_set_template (label, template);
159
160         gl_template_free (template);
161         g_free (template_name);
162
163         gl_debug (DEBUG_XML, "END");
164
165         return ret;
166 }
167
168 /*--------------------------------------------------------------------------*/
169 /* PRIVATE.  Parse XML Object Node                                          */
170 /*--------------------------------------------------------------------------*/
171 static void
172 xml04_parse_object (xmlNodePtr    object_node,
173                     glLabelObject *object)
174 {
175         gdouble x, y;
176
177         gl_debug (DEBUG_XML, "START");
178
179         x = gl_xml_get_prop_double (object_node, "x", 0);
180         y = gl_xml_get_prop_double (object_node, "y", 0);
181
182         gl_label_object_set_position (object, x, y);
183
184         gl_debug (DEBUG_XML, "END");
185 }
186
187 /*--------------------------------------------------------------------------*/
188 /* PRIVATE.  Parse XML Label->Text Node Properties                          */
189 /*--------------------------------------------------------------------------*/
190 static void
191 xml04_parse_text_props (xmlNodePtr    object_node,
192                         glLabelText   *object)
193 {
194         gchar            *font_family;
195         gdouble          font_size;
196         GnomeFontWeight  font_weight;
197         gboolean         font_italic_flag;
198         guint            color;
199         GtkJustification just;
200         xmlNodePtr       line_node, text_node;
201         glTextNode       *node_text;
202         GList            *nodes, *lines;
203         gdouble          w, h, x, y;
204         gchar            *string;
205
206         gl_debug (DEBUG_XML, "START");
207
208         font_family = xmlGetProp (object_node, "font_family");
209         font_size = gl_xml_get_prop_double (object_node, "font_size", 0);
210         string = xmlGetProp (object_node, "font_weight");
211         font_weight = gl_util_string_to_weight (string);
212         g_free (string);
213         font_italic_flag = gl_xml_get_prop_boolean (object_node, "font_italic", FALSE);
214
215         string = xmlGetProp (object_node, "justify");
216         just = gl_util_string_to_just (string);
217         g_free (string);
218
219         color = gl_xml_get_prop_uint (object_node, "color", 0);
220
221         gl_label_object_set_font_family (GL_LABEL_OBJECT(object), font_family);
222         gl_label_object_set_font_size (GL_LABEL_OBJECT(object), font_size);
223         gl_label_object_set_font_weight (GL_LABEL_OBJECT(object), font_weight);
224         gl_label_object_set_font_italic_flag (GL_LABEL_OBJECT(object), font_italic_flag);
225         gl_label_object_set_text_color (GL_LABEL_OBJECT(object), color);
226         gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), just);
227
228         lines = NULL;
229         for (line_node = object_node->xmlChildrenNode; line_node != NULL;
230              line_node = line_node->next) {
231
232                 if (xmlStrEqual (line_node->name, "Line")) {
233
234                         gl_debug (DEBUG_XML, "->Line node");
235
236                         nodes = NULL;
237                         for (text_node = line_node->xmlChildrenNode;
238                              text_node != NULL; text_node = text_node->next) {
239
240                                 if (xmlStrEqual (text_node->name, "Field")) {
241                                         gl_debug (DEBUG_XML, "->Line->Field node");
242                                         node_text = g_new0 (glTextNode, 1);
243                                         node_text->field_flag = TRUE;
244                                         node_text->data =
245                                             xmlGetProp (text_node, "name");
246                                         nodes =
247                                             g_list_append (nodes, node_text);
248                                 } else if (xmlNodeIsText (text_node)) {
249                                         gl_debug (DEBUG_XML, "->Line->\"literal\" node");
250                                         node_text = g_new0 (glTextNode, 1);
251                                         node_text->field_flag = FALSE;
252                                         node_text->data =
253                                             xmlNodeGetContent (text_node);
254                                         gl_debug (DEBUG_XML, "text = \"%s\"",
255                                                   node_text->data);
256                                         nodes =
257                                             g_list_append (nodes, node_text);
258                                 } else {
259                                         g_warning ("Unexpected Text Line child: \"%s\"",
260                                                    text_node->name);
261                                 }
262
263                         }
264                         lines = g_list_append (lines, nodes);
265
266                 } else if (!xmlNodeIsText (line_node)) {
267                         g_warning ("Unexpected Text child: \"%s\"",
268                                    line_node->name);
269                 }
270
271         }
272
273         gl_label_text_set_lines (object, lines);
274
275         gl_text_node_lines_free (&lines);
276         g_free (font_family);
277
278         /* Adjust location.  In 0.4.x, text was anchored at x,y */
279         gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
280         gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
281         switch (just) {
282         case GTK_JUSTIFY_LEFT:
283                 /* nothing */
284                 break;
285         case GTK_JUSTIFY_CENTER:
286                 x -= w/2.0;
287                 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
288                 break;
289         case GTK_JUSTIFY_RIGHT:
290                 x -= w;
291                 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
292                 break;
293         default:
294                 /* should not happen */
295                 break;
296         }
297
298         gl_debug (DEBUG_XML, "END");
299 }
300
301 /*--------------------------------------------------------------------------*/
302 /* PRIVATE.  Parse XML Label->Box Node Properties                           */
303 /*--------------------------------------------------------------------------*/
304 static void
305 xml04_parse_box_props (xmlNodePtr    node,
306                        glLabelBox    *object)
307 {
308         gdouble w, h, line_width;
309         guint line_color, fill_color;
310
311         gl_debug (DEBUG_XML, "START");
312
313         w = gl_xml_get_prop_double (node, "w", 0);
314         h = gl_xml_get_prop_double (node, "h", 0);
315
316         line_width = gl_xml_get_prop_double (node, "line_width", 0);
317
318         line_color = gl_xml_get_prop_uint (node, "line_color", 0);
319         fill_color = gl_xml_get_prop_uint (node, "fill_color", 0);
320
321         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
322         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
323         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color);
324         gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color);
325
326         gl_debug (DEBUG_XML, "END");
327 }
328
329 /*--------------------------------------------------------------------------*/
330 /* PRIVATE.  Parse XML Label->Line Node Properties                          */
331 /*--------------------------------------------------------------------------*/
332 static void
333 xml04_parse_line_props (xmlNodePtr    node,
334                         glLabelLine *object)
335 {
336         gdouble w, h, line_width;
337         guint line_color;
338
339         gl_debug (DEBUG_XML, "START");
340
341         w = gl_xml_get_prop_double (node, "dx", 0);
342         h = gl_xml_get_prop_double (node, "dy", 0);
343
344         line_width = gl_xml_get_prop_double (node, "line_width", 0);
345
346         line_color = gl_xml_get_prop_uint (node, "line_color", 0);
347
348         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
349         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
350         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color);
351
352         gl_debug (DEBUG_XML, "END");
353 }
354
355 /*--------------------------------------------------------------------------*/
356 /* PRIVATE.  Parse XML Label->Ellipse Node Properties                       */
357 /*--------------------------------------------------------------------------*/
358 static void
359 xml04_parse_ellipse_props (xmlNodePtr     node,
360                            glLabelEllipse *object)
361 {
362         gdouble w, h, line_width;
363         guint line_color, fill_color;
364
365         gl_debug (DEBUG_XML, "START");
366
367         w = gl_xml_get_prop_double (node, "w", 0);
368         h = gl_xml_get_prop_double (node, "h", 0);
369
370         line_width = gl_xml_get_prop_double (node, "line_width", 0);
371
372         line_color = gl_xml_get_prop_uint (node, "line_color", 0);
373         fill_color = gl_xml_get_prop_uint (node, "fill_color", 0);
374
375         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
376         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
377         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color);
378         gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color);
379
380         gl_debug (DEBUG_XML, "END");
381 }
382
383 /*--------------------------------------------------------------------------*/
384 /* PRIVATE.  Parse XML Label->Image Node Properties                         */
385 /*--------------------------------------------------------------------------*/
386 static void
387 xml04_parse_image_props (xmlNodePtr    node,
388                          glLabelImage  *object)
389 {
390         gdouble w, h;
391         glTextNode *filename;
392
393         gl_debug (DEBUG_XML, "START");
394
395         w = gl_xml_get_prop_double (node, "w", 0);
396         h = gl_xml_get_prop_double (node, "h", 0);
397         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
398
399         filename = g_new0 (glTextNode, 1);
400         filename->field_flag = FALSE;
401         filename->data = xmlGetProp (node, "filename");
402         gl_label_image_set_filename (object, filename);
403         gl_text_node_free (&filename);
404
405         gl_debug (DEBUG_XML, "END");
406 }
407
408 /*--------------------------------------------------------------------------*/
409 /* PRIVATE.  Parse XML Label->Barcode Node Properties                       */
410 /*--------------------------------------------------------------------------*/
411 static void
412 xml04_parse_barcode_props (xmlNodePtr    node,
413                            glLabelBarcode *object)
414 {
415         glBarcodeStyle style;
416         gboolean       text_flag;
417         guint          color;
418         gdouble        scale;
419         xmlNodePtr     child;
420         glTextNode     *text_node;
421         gchar          *string;
422
423         gl_debug (DEBUG_XML, "START");
424
425         color = gl_xml_get_prop_uint (node, "color", 0);
426
427         string = xmlGetProp (node, "style");
428         style = gl_barcode_text_to_style (string);
429         g_free (string);
430
431         text_flag = gl_xml_get_prop_boolean (node, "text", FALSE);
432         scale = gl_xml_get_prop_double (node, "scale", 1.0);
433         if (scale == 0.0) {
434                 scale = 0.5; /* Set to a valid value */
435         }
436         gl_label_barcode_set_props (object, style, text_flag, TRUE);
437         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color);
438
439         child = node->xmlChildrenNode;
440         text_node = g_new0 (glTextNode, 1);
441         if (xmlStrEqual (child->name, "Field")) {
442                 text_node->field_flag = TRUE;
443                 text_node->data       = xmlGetProp (child, "name");
444         } else if (xmlNodeIsText (child)) {
445                 text_node->field_flag = FALSE;
446                 text_node->data       = xmlNodeGetContent (child);
447         } else {
448                 g_warning ("Unexpected Barcode child: \"%s\"", child->name);
449         }
450         gl_label_barcode_set_data (object, text_node);
451
452         gl_text_node_free (&text_node);
453
454         gl_debug (DEBUG_XML, "END");
455 }
456
457 /*--------------------------------------------------------------------------*/
458 /* PRIVATE.  Parse XML merge properties tag.                                */
459 /*--------------------------------------------------------------------------*/
460 static void
461 xml04_parse_merge_properties (xmlNodePtr node,
462                               glLabel    *label)
463 {
464         glMerge                *merge;
465         xmlNodePtr             child;
466         gchar                  *string;
467
468         gl_debug (DEBUG_XML, "START");
469
470         string = xmlGetProp (node, "type");
471         merge = gl_merge_new (string);
472         g_free (string);
473
474         string = xmlGetProp (node, "src");
475         gl_merge_set_src (merge, string);
476         g_free (string);
477
478         gl_label_set_merge (label, merge);
479
480         g_object_unref (G_OBJECT(merge));
481
482         gl_debug (DEBUG_XML, "END");
483 }
484