1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * xml-label-04.c: GLabels xml label compat module
8 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
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.
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.
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
27 #include "xml-label-04.h"
29 #include <glib/gi18n.h>
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>
42 static gboolean xml04_parse_media_description (xmlNodePtr node,
44 static void xml04_parse_object (xmlNodePtr node,
45 glLabelObject *object);
46 static void xml04_parse_text_props (xmlNodePtr node,
48 static void xml04_parse_box_props (xmlNodePtr node,
50 static void xml04_parse_line_props (xmlNodePtr node,
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,
62 /****************************************************************************/
63 /* PRIVATE. Parse xml doc structure and create label. */
64 /****************************************************************************/
65 glLabel *gl_xml_label_04_parse (xmlNodePtr root,
66 glXMLLabelStatus *status)
73 gl_debug (DEBUG_XML, "START");
75 *status = XML_LABEL_OK;
77 if (!xmlStrEqual (root->name, (xmlChar *)"Label")) {
78 g_message (_("Bad root node = \"%s\""), root->name);
79 *status = XML_LABEL_ERROR_OPEN_PARSE;
83 label = GL_LABEL (gl_label_new ());
85 rotate_flag = lgl_xml_get_prop_boolean (root, "rotate", FALSE);
86 gl_label_set_rotate_flag (label, rotate_flag);
88 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
90 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
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;
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);
126 g_message (_("bad node = \"%s\""), node->name);
131 gl_debug (DEBUG_XML, "END");
136 /*--------------------------------------------------------------------------*/
137 /* PRIVATE. Parse Media Description node. */
138 /*--------------------------------------------------------------------------*/
140 xml04_parse_media_description (xmlNodePtr node,
143 xmlChar *template_name;
144 lglTemplate *template;
147 gl_debug (DEBUG_XML, "START");
149 template_name = xmlNodeGetContent (node);
151 template = lgl_template_from_name ((gchar *)template_name);
152 if (template == NULL) {
153 g_message ("Undefined template \"%s\"", template_name);
155 template = lgl_template_from_name (NULL);
161 gl_label_set_template (label, template);
163 lgl_template_free (template);
164 xmlFree (template_name);
166 gl_debug (DEBUG_XML, "END");
171 /*--------------------------------------------------------------------------*/
172 /* PRIVATE. Parse XML Object Node */
173 /*--------------------------------------------------------------------------*/
175 xml04_parse_object (xmlNodePtr object_node,
176 glLabelObject *object)
180 gl_debug (DEBUG_XML, "START");
182 x = lgl_xml_get_prop_double (object_node, "x", 0);
183 y = lgl_xml_get_prop_double (object_node, "y", 0);
185 gl_label_object_set_position (object, x, y);
187 gl_debug (DEBUG_XML, "END");
190 /*--------------------------------------------------------------------------*/
191 /* PRIVATE. Parse XML Label->Text Node Properties */
192 /*--------------------------------------------------------------------------*/
194 xml04_parse_text_props (xmlNodePtr object_node,
197 xmlChar *font_family;
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;
209 gl_debug (DEBUG_XML, "START");
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);
216 font_italic_flag = lgl_xml_get_prop_boolean (object_node, "font_italic", FALSE);
218 string = xmlGetProp (object_node, (xmlChar *)"justify");
219 align = gl_util_string_to_align ((gchar *)string);
222 color_node = gl_color_node_new_default ();
223 color_node->color = lgl_xml_get_prop_uint (object_node, "color", 0);
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);
232 gl_color_node_free (&color_node);
235 for (line_node = object_node->xmlChildrenNode; line_node != NULL;
236 line_node = line_node->next) {
238 if (xmlStrEqual (line_node->name, (xmlChar *)"Line")) {
240 gl_debug (DEBUG_XML, "->Line node");
243 for (text_node = line_node->xmlChildrenNode;
244 text_node != NULL; text_node = text_node->next) {
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;
251 (gchar *)xmlGetProp (text_node, (xmlChar *)"name");
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;
259 (gchar *)xmlNodeGetContent (text_node);
260 gl_debug (DEBUG_XML, "text = \"%s\"",
263 g_list_append (nodes, node_text);
265 g_message ("Unexpected Text Line child: \"%s\"",
270 lines = g_list_append (lines, nodes);
272 } else if (!xmlNodeIsText (line_node)) {
273 g_message ("Unexpected Text child: \"%s\"",
279 gl_label_text_set_lines (object, lines);
281 gl_text_node_lines_free (&lines);
282 xmlFree (font_family);
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);
288 case PANGO_ALIGN_LEFT:
291 case PANGO_ALIGN_CENTER:
293 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
295 case PANGO_ALIGN_RIGHT:
297 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
300 /* should not happen */
304 gl_debug (DEBUG_XML, "END");
307 /*--------------------------------------------------------------------------*/
308 /* PRIVATE. Parse XML Label->Box Node Properties */
309 /*--------------------------------------------------------------------------*/
311 xml04_parse_box_props (xmlNodePtr node,
314 gdouble w, h, line_width;
315 glColorNode *line_color_node;
316 glColorNode *fill_color_node;
318 gl_debug (DEBUG_XML, "START");
320 w = lgl_xml_get_prop_double (node, "w", 0);
321 h = lgl_xml_get_prop_double (node, "h", 0);
323 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
325 line_color_node = gl_color_node_new_default ();
326 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
328 fill_color_node = gl_color_node_new_default ();
329 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
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);
336 gl_color_node_free (&line_color_node);
337 gl_color_node_free (&fill_color_node);
338 gl_debug (DEBUG_XML, "END");
341 /*--------------------------------------------------------------------------*/
342 /* PRIVATE. Parse XML Label->Line Node Properties */
343 /*--------------------------------------------------------------------------*/
345 xml04_parse_line_props (xmlNodePtr node,
348 gdouble w, h, line_width;
349 glColorNode *line_color_node;
351 gl_debug (DEBUG_XML, "START");
353 w = lgl_xml_get_prop_double (node, "dx", 0);
354 h = lgl_xml_get_prop_double (node, "dy", 0);
356 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
358 line_color_node = gl_color_node_new_default ();
359 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
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);
365 gl_color_node_free (&line_color_node);
367 gl_debug (DEBUG_XML, "END");
370 /*--------------------------------------------------------------------------*/
371 /* PRIVATE. Parse XML Label->Ellipse Node Properties */
372 /*--------------------------------------------------------------------------*/
374 xml04_parse_ellipse_props (xmlNodePtr node,
375 glLabelEllipse *object)
377 gdouble w, h, line_width;
378 glColorNode *line_color_node;
379 glColorNode *fill_color_node;
381 gl_debug (DEBUG_XML, "START");
383 w = lgl_xml_get_prop_double (node, "w", 0);
384 h = lgl_xml_get_prop_double (node, "h", 0);
386 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
388 line_color_node = gl_color_node_new_default ();
389 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
391 fill_color_node = gl_color_node_new_default ();
392 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
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);
399 gl_color_node_free (&line_color_node);
400 gl_color_node_free (&fill_color_node);
401 gl_debug (DEBUG_XML, "END");
404 /*--------------------------------------------------------------------------*/
405 /* PRIVATE. Parse XML Label->Image Node Properties */
406 /*--------------------------------------------------------------------------*/
408 xml04_parse_image_props (xmlNodePtr node,
409 glLabelImage *object)
412 glTextNode *filename;
414 gl_debug (DEBUG_XML, "START");
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);
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);
426 gl_debug (DEBUG_XML, "END");
429 /*--------------------------------------------------------------------------*/
430 /* PRIVATE. Parse XML Label->Barcode Node Properties */
431 /*--------------------------------------------------------------------------*/
433 xml04_parse_barcode_props (xmlNodePtr node,
434 glLabelBarcode *object)
438 glColorNode *color_node;
441 glTextNode *text_node;
443 gl_debug (DEBUG_XML, "START");
445 color_node = gl_color_node_new_default ();
446 color_node->color = lgl_xml_get_prop_uint (node, "color", 0);
448 id = xmlGetProp (node, (xmlChar *)"style");
450 text_flag = lgl_xml_get_prop_boolean (node, "text", FALSE);
451 scale = lgl_xml_get_prop_double (node, "scale", 1.0);
453 scale = 0.5; /* Set to a valid value */
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);
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);
467 g_message ("Unexpected Barcode child: \"%s\"", child->name);
469 gl_label_barcode_set_data (object, text_node);
471 gl_color_node_free (&color_node);
472 gl_text_node_free (&text_node);
475 gl_debug (DEBUG_XML, "END");
478 /*--------------------------------------------------------------------------*/
479 /* PRIVATE. Parse XML merge properties tag. */
480 /*--------------------------------------------------------------------------*/
482 xml04_parse_merge_properties (xmlNodePtr node,
488 gl_debug (DEBUG_XML, "START");
490 string = xmlGetProp (node, (xmlChar *)"type");
491 merge = gl_merge_new ((gchar *)string);
494 string = xmlGetProp (node, (xmlChar *)"src");
495 gl_merge_set_src (merge, (gchar *)string);
498 gl_label_set_merge (label, merge);
500 g_object_unref (G_OBJECT(merge));
502 gl_debug (DEBUG_XML, "END");