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