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