]> git.sur5r.net Git - glabels/blob - src/xml-label-04.c
Imported Upstream version 3.0.0
[glabels] / src / xml-label-04.c
1 /*
2  *  xml-label-04.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "xml-label-04.h"
24
25 #include <glib/gi18n.h>
26
27 #include <libglabels.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 "bc-backends.h"
35 #include "str-util.h"
36
37 #include "debug.h"
38
39
40 static gboolean xml04_parse_media_description (xmlNodePtr node,
41                                                glLabel *label);
42 static void     xml04_parse_object            (xmlNodePtr node,
43                                                glLabelObject *object);
44 static void     xml04_parse_text_props        (xmlNodePtr node,
45                                                glLabelText *object);
46 static void     xml04_parse_box_props         (xmlNodePtr node,
47                                                glLabelBox *object);
48 static void     xml04_parse_line_props        (xmlNodePtr node,
49                                                glLabelLine *object);
50 static void     xml04_parse_ellipse_props     (xmlNodePtr node,
51                                                glLabelEllipse *object);
52 static void     xml04_parse_image_props       (xmlNodePtr node,
53                                                glLabelImage *object);
54 static void     xml04_parse_barcode_props     (xmlNodePtr node,
55                                                glLabelBarcode *object);
56 static void     xml04_parse_merge_properties  (xmlNodePtr node,
57                                                glLabel *label);
58
59
60 /****************************************************************************/
61 /* PRIVATE.  Parse xml doc structure and create label.                      */
62 /****************************************************************************/
63 glLabel      *gl_xml_label_04_parse      (xmlNodePtr       root,
64                                           glXMLLabelStatus *status)
65 {
66         glLabel       *label;
67         xmlNodePtr    node;
68         GObject       *object;
69         gboolean      rotate_flag;
70
71         gl_debug (DEBUG_XML, "START");
72
73         *status = XML_LABEL_OK;
74
75         if (!xmlStrEqual (root->name, (xmlChar *)"Label")) {
76                 g_message ("Bad root node = \"%s\"", root->name);
77                 *status = XML_LABEL_ERROR_OPEN_PARSE;
78                 return NULL;
79         }
80
81         label = GL_LABEL (gl_label_new ());
82
83         rotate_flag = lgl_xml_get_prop_boolean (root, "rotate", FALSE);
84         gl_label_set_rotate_flag (label, rotate_flag, FALSE);
85
86         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
87
88                 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
89
90                 if (!xmlNodeIsText (node)) {
91                         if (xmlStrEqual (node->name, (xmlChar *)"Media_Type")) {
92                                 if (!xml04_parse_media_description (node, label)) {
93                                         *status = XML_LABEL_UNKNOWN_MEDIA;
94                                 }
95                         } else if (xmlStrEqual (node->name, (xmlChar *)"Text")) {
96                                 object = gl_label_text_new (label, FALSE);
97                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
98                                 xml04_parse_text_props (node, GL_LABEL_TEXT(object));
99                         } else if (xmlStrEqual (node->name, (xmlChar *)"Box")) {
100                                 object = gl_label_box_new (label, FALSE);
101                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
102                                 xml04_parse_box_props (node, GL_LABEL_BOX(object));
103                         } else if (xmlStrEqual (node->name, (xmlChar *)"Line")) {
104                                 object = gl_label_line_new (label, FALSE);
105                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
106                                 xml04_parse_line_props (node, GL_LABEL_LINE(object));
107                         } else if (xmlStrEqual (node->name, (xmlChar *)"Ellipse")) {
108                                 object = gl_label_ellipse_new (label, FALSE);
109                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
110                                 xml04_parse_ellipse_props (node,
111                                                            GL_LABEL_ELLIPSE(object));
112                         } else if (xmlStrEqual (node->name, (xmlChar *)"Image")) {
113                                 object = gl_label_image_new (label, FALSE);
114                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
115                                 xml04_parse_image_props (node, GL_LABEL_IMAGE(object));
116                         } else if (xmlStrEqual (node->name, (xmlChar *)"Barcode")) {
117                                 object = gl_label_barcode_new (label, FALSE);
118                                 xml04_parse_object (node, GL_LABEL_OBJECT(object));
119                                 xml04_parse_barcode_props (node,
120                                                            GL_LABEL_BARCODE(object));
121                         } else if (xmlStrEqual (node->name, (xmlChar *)"Merge_Properties")) {
122                                 xml04_parse_merge_properties (node, label);
123                         } else {
124                                 g_message ("bad node =  \"%s\"", node->name);
125                         }
126                 }
127         }
128
129         gl_debug (DEBUG_XML, "END");
130
131         return label;
132 }
133
134
135 /*--------------------------------------------------------------------------*/
136 /* PRIVATE.  Parse Media Description node.                                  */
137 /*--------------------------------------------------------------------------*/
138 static gboolean
139 xml04_parse_media_description (xmlNodePtr node,
140                                glLabel    *label)
141 {
142         xmlChar     *template_name;
143         lglTemplate *template;
144         gboolean     ret;
145
146         gl_debug (DEBUG_XML, "START");
147
148         template_name = xmlNodeGetContent (node);
149
150         template = lgl_db_lookup_template_from_name ((gchar *)template_name);
151         if (template == NULL) {
152                 g_message ("Undefined template \"%s\"", template_name);
153                 /* Get a default */
154                 template = lgl_db_lookup_template_from_name (NULL);
155                 ret = FALSE;
156         } else {
157                 ret = TRUE;
158         }
159
160         gl_label_set_template (label, template, FALSE);
161
162         lgl_template_free (template);
163         xmlFree (template_name);
164
165         gl_debug (DEBUG_XML, "END");
166
167         return ret;
168 }
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, FALSE);
186
187         gl_debug (DEBUG_XML, "END");
188 }
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_str_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_str_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, FALSE);
227         gl_label_object_set_font_size (GL_LABEL_OBJECT(object), font_size, FALSE);
228         gl_label_object_set_font_weight (GL_LABEL_OBJECT(object), font_weight, FALSE);
229         gl_label_object_set_font_italic_flag (GL_LABEL_OBJECT(object), font_italic_flag, FALSE);
230         gl_label_object_set_text_color (GL_LABEL_OBJECT(object), color_node, FALSE);
231         gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), align, FALSE);
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, FALSE);
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, FALSE);
295                 break;
296         case PANGO_ALIGN_RIGHT:
297                 x -= w;
298                 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y, FALSE);
299                 break;
300         default:
301                 /* should not happen */
302                 break;
303         }
304
305         gl_debug (DEBUG_XML, "END");
306 }
307
308
309 /*--------------------------------------------------------------------------*/
310 /* PRIVATE.  Parse XML Label->Box Node Properties                           */
311 /*--------------------------------------------------------------------------*/
312 static void
313 xml04_parse_box_props (xmlNodePtr    node,
314                        glLabelBox    *object)
315 {
316         gdouble w, h, line_width;
317         glColorNode *line_color_node;
318         glColorNode *fill_color_node;
319
320         gl_debug (DEBUG_XML, "START");
321
322         w = lgl_xml_get_prop_double (node, "w", 0);
323         h = lgl_xml_get_prop_double (node, "h", 0);
324
325         line_width = lgl_xml_get_prop_double (node, "line_width", 0);
326
327         line_color_node = gl_color_node_new_default ();
328         line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
329         
330         fill_color_node = gl_color_node_new_default ();
331         fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
332
333         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
334         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width, FALSE);
335         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node, FALSE);
336         gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node, FALSE);
337
338         gl_color_node_free (&line_color_node);
339         gl_color_node_free (&fill_color_node);
340         gl_debug (DEBUG_XML, "END");
341 }
342
343
344 /*--------------------------------------------------------------------------*/
345 /* PRIVATE.  Parse XML Label->Line Node Properties                          */
346 /*--------------------------------------------------------------------------*/
347 static void
348 xml04_parse_line_props (xmlNodePtr    node,
349                         glLabelLine *object)
350 {
351         gdouble w, h, line_width;
352         glColorNode *line_color_node;
353
354         gl_debug (DEBUG_XML, "START");
355
356         w = lgl_xml_get_prop_double (node, "dx", 0);
357         h = lgl_xml_get_prop_double (node, "dy", 0);
358
359         line_width = lgl_xml_get_prop_double (node, "line_width", 0);
360
361         line_color_node = gl_color_node_new_default ();
362         line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
363
364         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
365         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width, FALSE);
366         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node, FALSE);
367
368         gl_color_node_free (&line_color_node);
369         
370         gl_debug (DEBUG_XML, "END");
371 }
372
373
374 /*--------------------------------------------------------------------------*/
375 /* PRIVATE.  Parse XML Label->Ellipse Node Properties                       */
376 /*--------------------------------------------------------------------------*/
377 static void
378 xml04_parse_ellipse_props (xmlNodePtr     node,
379                            glLabelEllipse *object)
380 {
381         gdouble w, h, line_width;
382         glColorNode *line_color_node;
383         glColorNode *fill_color_node;
384
385         gl_debug (DEBUG_XML, "START");
386
387         w = lgl_xml_get_prop_double (node, "w", 0);
388         h = lgl_xml_get_prop_double (node, "h", 0);
389
390         line_width = lgl_xml_get_prop_double (node, "line_width", 0);
391
392         line_color_node = gl_color_node_new_default ();
393         line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
394         
395         fill_color_node = gl_color_node_new_default ();
396         fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
397
398         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
399         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width, FALSE);
400         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node, FALSE);
401         gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node, FALSE);
402
403         gl_color_node_free (&line_color_node);
404         gl_color_node_free (&fill_color_node);
405         gl_debug (DEBUG_XML, "END");
406 }
407
408
409 /*--------------------------------------------------------------------------*/
410 /* PRIVATE.  Parse XML Label->Image Node Properties                         */
411 /*--------------------------------------------------------------------------*/
412 static void
413 xml04_parse_image_props (xmlNodePtr    node,
414                          glLabelImage  *object)
415 {
416         gdouble w, h;
417         glTextNode *filename;
418
419         gl_debug (DEBUG_XML, "START");
420
421         filename = g_new0 (glTextNode, 1);
422         filename->field_flag = FALSE;
423         filename->data = (gchar *)xmlGetProp (node, (xmlChar *)"filename");
424         gl_label_image_set_filename (object, filename, FALSE);
425         gl_text_node_free (&filename);
426
427         w = lgl_xml_get_prop_double (node, "w", 0);
428         h = lgl_xml_get_prop_double (node, "h", 0);
429         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
430
431         gl_debug (DEBUG_XML, "END");
432 }
433
434
435 /*--------------------------------------------------------------------------*/
436 /* PRIVATE.  Parse XML Label->Barcode Node Properties                       */
437 /*--------------------------------------------------------------------------*/
438 static void
439 xml04_parse_barcode_props (xmlNodePtr    node,
440                            glLabelBarcode *object)
441 {
442         gchar               *id;
443         const gchar         *backend_id;
444         glLabelBarcodeStyle *style;
445         glColorNode         *color_node;
446         xmlNodePtr           child;
447         glTextNode          *text_node;
448
449         gl_debug (DEBUG_XML, "START");
450
451         color_node = gl_color_node_new_default ();
452         color_node->color = lgl_xml_get_prop_uint (node, "color", 0);
453
454         style = gl_label_barcode_style_new ();
455
456         id = lgl_xml_get_prop_string (node, "style", "POSTNET");
457         backend_id = gl_barcode_backends_guess_backend_id (id);
458         gl_label_barcode_style_set_backend_id (style, backend_id);
459         gl_label_barcode_style_set_style_id (style, id);
460
461         style->text_flag     = lgl_xml_get_prop_boolean (node, "text", FALSE);
462         style->checksum_flag = TRUE;
463         style->format_digits = 0;
464
465         gl_label_barcode_set_style (object, style, FALSE);
466
467         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color_node, FALSE);
468
469         child = node->xmlChildrenNode;
470         text_node = g_new0 (glTextNode, 1);
471         if (xmlStrEqual (child->name, (xmlChar *)"Field")) {
472                 text_node->field_flag = TRUE;
473                 text_node->data       = (gchar *)xmlGetProp (child, (xmlChar *)"name");
474         } else if (xmlNodeIsText (child)) {
475                 text_node->field_flag = FALSE;
476                 text_node->data       = (gchar *)xmlNodeGetContent (child);
477         } else {
478                 g_message ("Unexpected Barcode child: \"%s\"", child->name);
479         }
480         gl_label_barcode_set_data (object, text_node, FALSE);
481
482         gl_color_node_free (&color_node);
483         gl_text_node_free (&text_node);
484         g_free (id);
485         gl_label_barcode_style_free (style);
486
487         gl_debug (DEBUG_XML, "END");
488 }
489
490
491 /*--------------------------------------------------------------------------*/
492 /* PRIVATE.  Parse XML merge properties tag.                                */
493 /*--------------------------------------------------------------------------*/
494 static void
495 xml04_parse_merge_properties (xmlNodePtr node,
496                               glLabel    *label)
497 {
498         glMerge               *merge;
499         xmlChar               *string;
500
501         gl_debug (DEBUG_XML, "START");
502
503         string = xmlGetProp (node, (xmlChar *)"type");
504         merge = gl_merge_new ((gchar *)string);
505         xmlFree (string);
506
507         string = xmlGetProp (node, (xmlChar *)"src");
508         gl_merge_set_src (merge, (gchar *)string);
509         xmlFree (string);
510
511         gl_label_set_merge (label, merge, FALSE);
512
513         g_object_unref (G_OBJECT(merge));
514
515         gl_debug (DEBUG_XML, "END");
516 }
517
518
519
520 /*
521  * Local Variables:       -- emacs
522  * mode: C                -- emacs
523  * c-basic-offset: 8      -- emacs
524  * tab-width: 8           -- emacs
525  * indent-tabs-mode: nil  -- emacs
526  * End:                   -- emacs
527  */