]> git.sur5r.net Git - glabels/blob - glabels2/src/xml-label-04.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / 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/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 "str-util.h"
35
36 #include "debug.h"
37
38
39 static gboolean xml04_parse_media_description (xmlNodePtr node,
40                                                glLabel *label);
41 static void     xml04_parse_object            (xmlNodePtr node,
42                                                glLabelObject *object);
43 static void     xml04_parse_text_props        (xmlNodePtr node,
44                                                glLabelText *object);
45 static void     xml04_parse_box_props         (xmlNodePtr node,
46                                                glLabelBox *object);
47 static void     xml04_parse_line_props        (xmlNodePtr node,
48                                                glLabelLine *object);
49 static void     xml04_parse_ellipse_props     (xmlNodePtr node,
50                                                glLabelEllipse *object);
51 static void     xml04_parse_image_props       (xmlNodePtr node,
52                                                glLabelImage *object);
53 static void     xml04_parse_barcode_props     (xmlNodePtr node,
54                                                glLabelBarcode *object);
55 static void     xml04_parse_merge_properties  (xmlNodePtr node,
56                                                glLabel *label);
57
58
59 /****************************************************************************/
60 /* PRIVATE.  Parse xml doc structure and create label.                      */
61 /****************************************************************************/
62 glLabel      *gl_xml_label_04_parse      (xmlNodePtr       root,
63                                           glXMLLabelStatus *status)
64 {
65         glLabel       *label;
66         xmlNodePtr    node;
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, (xmlChar *)"Label")) {
75                 g_message (_("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 = lgl_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, (xmlChar *)"Media_Type")) {
91                                 if (!xml04_parse_media_description (node, label)) {
92                                         *status = XML_LABEL_UNKNOWN_MEDIA;
93                                 }
94                         } else if (xmlStrEqual (node->name, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"Merge_Properties")) {
121                                 xml04_parse_merge_properties (node, label);
122                         } else {
123                                 g_message (_("bad node =  \"%s\""), node->name);
124                         }
125                 }
126         }
127
128         gl_debug (DEBUG_XML, "END");
129
130         return label;
131 }
132
133
134 /*--------------------------------------------------------------------------*/
135 /* PRIVATE.  Parse Media Description node.                                  */
136 /*--------------------------------------------------------------------------*/
137 static gboolean
138 xml04_parse_media_description (xmlNodePtr node,
139                                glLabel    *label)
140 {
141         xmlChar     *template_name;
142         lglTemplate *template;
143         gboolean     ret;
144
145         gl_debug (DEBUG_XML, "START");
146
147         template_name = xmlNodeGetContent (node);
148
149         template = lgl_db_lookup_template_from_name ((gchar *)template_name);
150         if (template == NULL) {
151                 g_message ("Undefined template \"%s\"", template_name);
152                 /* Get a default */
153                 template = lgl_db_lookup_template_from_name (NULL);
154                 ret = FALSE;
155         } else {
156                 ret = TRUE;
157         }
158
159         gl_label_set_template (label, template);
160
161         lgl_template_free (template);
162         xmlFree (template_name);
163
164         gl_debug (DEBUG_XML, "END");
165
166         return ret;
167 }
168
169
170 /*--------------------------------------------------------------------------*/
171 /* PRIVATE.  Parse XML Object Node                                          */
172 /*--------------------------------------------------------------------------*/
173 static void
174 xml04_parse_object (xmlNodePtr    object_node,
175                     glLabelObject *object)
176 {
177         gdouble x, y;
178
179         gl_debug (DEBUG_XML, "START");
180
181         x = lgl_xml_get_prop_double (object_node, "x", 0);
182         y = lgl_xml_get_prop_double (object_node, "y", 0);
183
184         gl_label_object_set_position (object, x, y);
185
186         gl_debug (DEBUG_XML, "END");
187 }
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_str_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_str_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 /*--------------------------------------------------------------------------*/
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 /*--------------------------------------------------------------------------*/
344 /* PRIVATE.  Parse XML Label->Line Node Properties                          */
345 /*--------------------------------------------------------------------------*/
346 static void
347 xml04_parse_line_props (xmlNodePtr    node,
348                         glLabelLine *object)
349 {
350         gdouble w, h, line_width;
351         glColorNode *line_color_node;
352
353         gl_debug (DEBUG_XML, "START");
354
355         w = lgl_xml_get_prop_double (node, "dx", 0);
356         h = lgl_xml_get_prop_double (node, "dy", 0);
357
358         line_width = lgl_xml_get_prop_double (node, "line_width", 0);
359
360         line_color_node = gl_color_node_new_default ();
361         line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
362
363         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
364         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
365         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
366
367         gl_color_node_free (&line_color_node);
368         
369         gl_debug (DEBUG_XML, "END");
370 }
371
372
373 /*--------------------------------------------------------------------------*/
374 /* PRIVATE.  Parse XML Label->Ellipse Node Properties                       */
375 /*--------------------------------------------------------------------------*/
376 static void
377 xml04_parse_ellipse_props (xmlNodePtr     node,
378                            glLabelEllipse *object)
379 {
380         gdouble w, h, line_width;
381         glColorNode *line_color_node;
382         glColorNode *fill_color_node;
383
384         gl_debug (DEBUG_XML, "START");
385
386         w = lgl_xml_get_prop_double (node, "w", 0);
387         h = lgl_xml_get_prop_double (node, "h", 0);
388
389         line_width = lgl_xml_get_prop_double (node, "line_width", 0);
390
391         line_color_node = gl_color_node_new_default ();
392         line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
393         
394         fill_color_node = gl_color_node_new_default ();
395         fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
396
397         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
398         gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
399         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
400         gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node);
401
402         gl_color_node_free (&line_color_node);
403         gl_color_node_free (&fill_color_node);
404         gl_debug (DEBUG_XML, "END");
405 }
406
407
408 /*--------------------------------------------------------------------------*/
409 /* PRIVATE.  Parse XML Label->Image Node Properties                         */
410 /*--------------------------------------------------------------------------*/
411 static void
412 xml04_parse_image_props (xmlNodePtr    node,
413                          glLabelImage  *object)
414 {
415         gdouble w, h;
416         glTextNode *filename;
417
418         gl_debug (DEBUG_XML, "START");
419
420         filename = g_new0 (glTextNode, 1);
421         filename->field_flag = FALSE;
422         filename->data = (gchar *)xmlGetProp (node, (xmlChar *)"filename");
423         gl_label_image_set_filename (object, filename);
424         gl_text_node_free (&filename);
425
426         w = lgl_xml_get_prop_double (node, "w", 0);
427         h = lgl_xml_get_prop_double (node, "h", 0);
428         gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
429
430         gl_debug (DEBUG_XML, "END");
431 }
432
433
434 /*--------------------------------------------------------------------------*/
435 /* PRIVATE.  Parse XML Label->Barcode Node Properties                       */
436 /*--------------------------------------------------------------------------*/
437 static void
438 xml04_parse_barcode_props (xmlNodePtr    node,
439                            glLabelBarcode *object)
440 {
441         xmlChar       *id;
442         gboolean       text_flag;
443         glColorNode   *color_node;
444         gdouble        scale;
445         xmlNodePtr     child;
446         glTextNode    *text_node;
447
448         gl_debug (DEBUG_XML, "START");
449
450         color_node = gl_color_node_new_default ();
451         color_node->color = lgl_xml_get_prop_uint (node, "color", 0);
452
453         id = xmlGetProp (node, (xmlChar *)"style");
454
455         text_flag = lgl_xml_get_prop_boolean (node, "text", FALSE);
456         scale = lgl_xml_get_prop_double (node, "scale", 1.0);
457         if (scale == 0.0) {
458                 scale = 0.5; /* Set to a valid value */
459         }
460         gl_label_barcode_set_props (object, (gchar *)id, text_flag, TRUE, 0);
461         gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color_node);
462
463         child = node->xmlChildrenNode;
464         text_node = g_new0 (glTextNode, 1);
465         if (xmlStrEqual (child->name, (xmlChar *)"Field")) {
466                 text_node->field_flag = TRUE;
467                 text_node->data       = (gchar *)xmlGetProp (child, (xmlChar *)"name");
468         } else if (xmlNodeIsText (child)) {
469                 text_node->field_flag = FALSE;
470                 text_node->data       = (gchar *)xmlNodeGetContent (child);
471         } else {
472                 g_message ("Unexpected Barcode child: \"%s\"", child->name);
473         }
474         gl_label_barcode_set_data (object, text_node);
475
476         gl_color_node_free (&color_node);
477         gl_text_node_free (&text_node);
478         xmlFree (id);
479
480         gl_debug (DEBUG_XML, "END");
481 }
482
483
484 /*--------------------------------------------------------------------------*/
485 /* PRIVATE.  Parse XML merge properties tag.                                */
486 /*--------------------------------------------------------------------------*/
487 static void
488 xml04_parse_merge_properties (xmlNodePtr node,
489                               glLabel    *label)
490 {
491         glMerge               *merge;
492         xmlChar               *string;
493
494         gl_debug (DEBUG_XML, "START");
495
496         string = xmlGetProp (node, (xmlChar *)"type");
497         merge = gl_merge_new ((gchar *)string);
498         xmlFree (string);
499
500         string = xmlGetProp (node, (xmlChar *)"src");
501         gl_merge_set_src (merge, (gchar *)string);
502         xmlFree (string);
503
504         gl_label_set_merge (label, merge);
505
506         g_object_unref (G_OBJECT(merge));
507
508         gl_debug (DEBUG_XML, "END");
509 }
510
511
512
513 /*
514  * Local Variables:       -- emacs
515  * mode: C                -- emacs
516  * c-basic-offset: 8      -- emacs
517  * tab-width: 8           -- emacs
518  * indent-tabs-mode: nil  -- emacs
519  * End:                   -- emacs
520  */