]> git.sur5r.net Git - glabels/blob - libglabels/template.c
Search both ~/.glabels and ${XDG_CONFIG_HOME}/libglabels/templates.
[glabels] / libglabels / template.c
1 /*
2  *  template.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of libglabels.
6  *
7  *  libglabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser 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  *  libglabels 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 Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with libglabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "template.h"
24
25 #include <glib/gi18n.h>
26 #include <glib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <math.h>
31
32 #include "libglabels-private.h"
33
34 #include "db.h"
35 #include "paper.h"
36
37 /*===========================================*/
38 /* Private macros and constants.             */
39 /*===========================================*/
40
41 /* Allowed error when comparing dimensions. (0.5pts ~= .007in ~= .2mm) */
42 #define EPSILON 0.5
43
44 /*===========================================*/
45 /* Private types                             */
46 /*===========================================*/
47
48
49 /*===========================================*/
50 /* Private globals                           */
51 /*===========================================*/
52
53
54 /*===========================================*/
55 /* Local function prototypes                 */
56 /*===========================================*/
57
58 static gint         compare_origins              (gconstpointer           a,
59                                                   gconstpointer           b,
60                                                   gpointer                user_data);
61
62 /*===========================================*/
63 /* Functions.                                */
64 /*===========================================*/
65
66 /**
67  * lgl_template_new:
68  *   @brand:        Template brand
69  *   @part:         Template part name/number
70  *   @description:  Template descriptions
71  *   @paper_id:     Page size id
72  *   @page_width:   Page width in points, set to zero unless paper_id="Other"
73  *   @page_height:  Page height in points, set to zero unless paper_id="Other"
74  *
75  * Create a new template structure, with the given top-level attributes.  The
76  * created template will have no initial categories, or frames associated with
77  * it.  See lgl_template_add_category() and lgl_template_add_frame() to add
78  * these.
79  *
80  * Returns: pointer to a newly allocated #lglTemplate structure.
81  *
82  */
83 lglTemplate *
84 lgl_template_new (const gchar         *brand,
85                   const gchar         *part,
86                   const gchar         *description,
87                   const gchar         *paper_id,
88                   gdouble              page_width,
89                   gdouble              page_height)
90 {
91         lglTemplate      *template;
92
93         template = g_new0 (lglTemplate,1);
94
95         template->brand       = g_strdup (brand);
96         template->part        = g_strdup (part);
97         template->description = g_strdup (description);
98         template->paper_id    = g_strdup (paper_id);
99         template->page_width  = page_width;
100         template->page_height = page_height;
101
102         return template;
103 }
104
105
106 /**
107  * lgl_template_new_from_equiv:
108  *   @brand:        Template brand
109  *   @part:         Template part name/number
110  *   @equiv_part:   Name of equivalent part to base template on
111  *
112  * Create a new template structure based on an existing template.  The
113  * created template will be a duplicate of the original template, except with
114  * the new part name/number.
115  *
116  * Returns: pointer to a newly allocated #lglTemplate structure.
117  *
118  */
119 lglTemplate *
120 lgl_template_new_from_equiv (const gchar          *brand,
121                              const gchar          *part,
122                              const gchar          *equiv_part)
123 {
124         lglTemplate      *template = NULL;
125
126         if ( lgl_db_does_template_exist (brand, equiv_part) )
127         {
128                 template = lgl_db_lookup_template_from_brand_part (brand, equiv_part);
129
130                 g_free (template->part);
131                 g_free (template->equiv_part);
132
133                 template->part       = g_strdup (part);
134                 template->equiv_part = g_strdup (equiv_part);
135         }
136         else
137         {
138                 g_message (_("Equivalent part (\"%s\") for \"%s\", not previously defined."),
139                            equiv_part, part);
140         }
141
142         return template;
143 }
144
145
146 /**
147  * lgl_template_get_name:
148  *   @template:  Pointer to template structure to test
149  *
150  * This function returns the name of the given template.  The name is the concetenation
151  * of the brand and part name/number.
152  *
153  * Returns:  A pointer to a newly allocated name string.  Should be freed with g_free().
154  *
155  */
156 gchar *
157 lgl_template_get_name (const lglTemplate  *template)
158 {
159         g_return_val_if_fail (template, NULL);
160
161         return g_strdup_printf ("%s %s", template->brand, template->part);
162 }
163
164
165 /**
166  * lgl_template_do_templates_match:
167  *   @template1:  Pointer to 1st template structure to test
168  *   @template2:  Pointer to 2nd template structure to test
169  *
170  * This function tests if the given templates match.  This is a simple test that only tests
171  * the brand and part name/number. It does not test if they are actually identical.
172  *
173  * Returns:  TRUE if the two templates match.
174  *
175  */
176 gboolean
177 lgl_template_do_templates_match (const lglTemplate  *template1,
178                                  const lglTemplate  *template2)
179 {
180         g_return_val_if_fail (template1, FALSE);
181         g_return_val_if_fail (template2, FALSE);
182
183         return (UTF8_EQUAL (template1->brand, template2->brand) &&
184                 UTF8_EQUAL (template1->part, template2->part));
185 }
186
187
188 /**
189  * lgl_template_does_brand_match:
190  *   @template:  Pointer to template structure to test
191  *   @brand:     Brand string
192  *
193  * This function tests if the brand of the template matches the given brand.
194  *
195  * Returns:  TRUE if the template matches the given brand.
196  *
197  */
198 gboolean
199 lgl_template_does_brand_match (const lglTemplate  *template,
200                                const gchar        *brand)
201 {
202         g_return_val_if_fail (template, FALSE);
203
204         /* NULL matches everything. */
205         if (brand == NULL)
206         {
207                 return TRUE;
208         }
209
210         return UTF8_EQUAL (template->brand, brand);
211 }
212
213
214 /**
215  * lgl_template_does_page_size_match:
216  *   @template:  Pointer to template structure to test
217  *   @paper_id:  Page size ID string
218  *
219  * This function tests if the page size of the template matches the given ID.
220  *
221  * Returns:  TRUE if the template matches the given page size ID.
222  *
223  */
224 gboolean
225 lgl_template_does_page_size_match (const lglTemplate  *template,
226                                    const gchar        *paper_id)
227 {
228         g_return_val_if_fail (template, FALSE);
229
230         /* NULL matches everything. */
231         if (paper_id == NULL)
232         {
233                 return TRUE;
234         }
235
236         return ASCII_EQUAL(paper_id, template->paper_id);
237 }
238
239
240 /**
241  * lgl_template_does_category_match:
242  *   @template:     Pointer to template structure to test
243  *   @category_id:  Category ID string
244  *
245  * This function tests if the given template belongs to the given category ID.
246  *
247  * Returns:  TRUE if the template matches the given category ID.
248  *
249  */
250 gboolean
251 lgl_template_does_category_match  (const lglTemplate  *template,
252                                    const gchar        *category_id)
253 {
254         GList *p;
255
256         g_return_val_if_fail (template, FALSE);
257
258         /* NULL matches everything. */
259         if (category_id == NULL)
260         {
261                 return TRUE;
262         }
263
264         for ( p=template->category_ids; p != NULL; p=p->next )
265         {
266                 if (ASCII_EQUAL(category_id, p->data))
267                 {
268                         return TRUE;
269                 }
270         }
271
272         return FALSE;
273 }
274
275
276 /**
277  * lgl_template_are_templates_identical:
278  *   @template1:  Pointer to 1st template structure to test
279  *   @template2:  Pointer to 2nd template structure to test
280  *
281  * This function tests if the given templates have identical size and layout properties.
282  *
283  * Returns:  TRUE if the two templates are identical.
284  *
285  */
286 gboolean
287 lgl_template_are_templates_identical (const lglTemplate   *template1,
288                                       const lglTemplate   *template2)
289 {
290         lglTemplateFrame  *frame1;
291         lglTemplateFrame  *frame2;
292         GList             *p1;
293         GList             *p2;
294         lglTemplateLayout *layout1;
295         lglTemplateLayout *layout2;
296         gboolean           match_found;
297
298
299         if (!UTF8_EQUAL (template1->paper_id, template2->paper_id) ||
300             (template1->page_width  != template2->page_width)      ||
301             (template1->page_height != template2->page_height))
302         {
303                 return FALSE;
304         }
305
306         frame1 = (lglTemplateFrame *)template1->frames->data;
307         frame2 = (lglTemplateFrame *)template2->frames->data;
308
309         if ( frame1->shape != frame2->shape )
310         {
311                 return FALSE;
312         }
313
314         switch ( frame1->shape )
315         {
316
317         case LGL_TEMPLATE_FRAME_SHAPE_RECT:
318                 if ( (fabs(frame1->rect.w - frame2->rect.w) > EPSILON) ||
319                      (fabs(frame1->rect.h - frame2->rect.h) > EPSILON) )
320                 {
321                         return FALSE;
322                 }
323                 break;
324
325         case LGL_TEMPLATE_FRAME_SHAPE_ELLIPSE:
326                 if ( (fabs(frame1->ellipse.w - frame2->ellipse.w) > EPSILON) ||
327                      (fabs(frame1->ellipse.h - frame2->ellipse.h) > EPSILON) )
328                 {
329                         return FALSE;
330                 }
331                 break;
332
333         case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
334                 if ( fabs(frame1->round.r - frame2->round.r) > EPSILON )
335                 {
336                         return FALSE;
337                 }
338                 break;
339
340         case LGL_TEMPLATE_FRAME_SHAPE_CD:
341                 if ( (fabs(frame1->cd.r1 - frame2->cd.r1) > EPSILON) ||
342                      (fabs(frame1->cd.r2 - frame2->cd.r2) > EPSILON) )
343                 {
344                         return FALSE;
345                 }
346         }
347
348         for ( p1 = frame1->all.layouts; p1; p1 = p1->next )
349         {
350                 layout1 = (lglTemplateLayout *)p1->data;
351
352                 match_found = FALSE;
353                 for ( p2 = frame2->all.layouts; p2 && !match_found; p2 = p2->next )
354                 {
355                         layout2 = (lglTemplateLayout *)p2->data;
356
357                         if ( (layout1->nx == layout2->nx) &&
358                              (layout1->ny == layout2->ny) &&
359                              (fabs(layout1->x0 - layout2->x0) < EPSILON) &&
360                              (fabs(layout1->y0 - layout2->y0) < EPSILON) &&
361                              (fabs(layout1->dx - layout2->dx) < EPSILON) &&
362                              (fabs(layout1->dy - layout2->dy) < EPSILON) )
363                         {
364                                 match_found = TRUE;
365                         }
366
367                 }
368                 if ( !match_found )
369                 {
370                         return FALSE;
371                 }
372         }
373
374         return TRUE;
375 }
376
377
378 /**
379  * lgl_template_add_frame:
380  *   @template:  Pointer to template structure
381  *   @frame:     Pointer to frame structure
382  *
383  * This function adds the given frame structure to the template.  Once added,
384  * the frame structure belongs to the given template; do not attempt to free
385  * it.
386  *
387  * Note: Currently glabels only supports a single frame per template.
388  *
389  */
390 void
391 lgl_template_add_frame (lglTemplate      *template,
392                         lglTemplateFrame *frame)
393 {
394         g_return_if_fail (template);
395         g_return_if_fail (frame);
396
397         template->frames = g_list_append (template->frames, frame);
398 }
399
400  
401 /**
402  * lgl_template_add_category:
403  *   @template:     Pointer to template structure
404  *   @category_id:  Category ID string
405  *
406  * This function adds the given category ID to a templates category list.
407  *
408  */
409 void
410 lgl_template_add_category (lglTemplate         *template,
411                            const gchar         *category_id)
412 {
413         g_return_if_fail (template);
414         g_return_if_fail (category_id);
415
416         template->category_ids = g_list_append (template->category_ids,
417                                                 g_strdup (category_id));
418 }
419
420  
421 /**
422  * lgl_template_frame_rect_new:
423  *   @id:      ID of frame.  (This should currently always be "0").
424  *   @w:       width of frame in points.
425  *   @h:       height of frame in points.
426  *   @r:       radius of rounded corners in points.  (Should be 0 for square corners.)
427  *   @x_waste: Amount of overprint to allow in the horizontal direction.
428  *   @y_waste: Amount of overprint to allow in the vertical direction.
429  *
430  * This function creates a new template frame for a rectangular label or card.
431  *
432  * Returns: Pointer to newly allocated #lglTemplateFrame structure.
433  *
434  */
435 lglTemplateFrame *
436 lgl_template_frame_rect_new  (const gchar         *id,
437                               gdouble              w,
438                               gdouble              h,
439                               gdouble              r,
440                               gdouble              x_waste,
441                               gdouble              y_waste)
442 {
443         lglTemplateFrame *frame;
444
445         frame = g_new0 (lglTemplateFrame, 1);
446
447         frame->shape = LGL_TEMPLATE_FRAME_SHAPE_RECT;
448         frame->rect.id = g_strdup (id);
449
450         frame->rect.w = w;
451         frame->rect.h = h;
452         frame->rect.r = r;
453         frame->rect.x_waste = x_waste;
454         frame->rect.y_waste = y_waste;
455
456         return frame;
457 }
458
459
460 /**
461  * lgl_template_frame_ellipse_new:
462  *   @id:      ID of frame.  (This should currently always be "0").
463  *   @w:       width of frame in points.
464  *   @h:       height of frame in points.
465  *   @waste:   Amount of overprint to allow in points.
466  *
467  * This function creates a new template frame for an elliptical label or card.
468  *
469  * Returns: Pointer to newly allocated #lglTemplateFrame structure.
470  *
471  */
472 lglTemplateFrame *
473 lgl_template_frame_ellipse_new  (const gchar         *id,
474                                  gdouble              w,
475                                  gdouble              h,
476                                  gdouble              waste)
477 {
478         lglTemplateFrame *frame;
479
480         frame = g_new0 (lglTemplateFrame, 1);
481
482         frame->shape = LGL_TEMPLATE_FRAME_SHAPE_ELLIPSE;
483         frame->ellipse.id = g_strdup (id);
484
485         frame->ellipse.w = w;
486         frame->ellipse.h = h;
487         frame->ellipse.waste = waste;
488
489         return frame;
490 }
491
492
493 /**
494  * lgl_template_frame_round_new:
495  *   @id:      ID of frame.  (This should currently always be "0").
496  *   @r:       radius of label in points.
497  *   @waste:   Amount of overprint to allow.
498  *
499  * This function creates a new template frame for a round label.
500  *
501  * Returns: Pointer to newly allocated #lglTemplateFrame structure.
502  *
503  */
504 lglTemplateFrame *
505 lgl_template_frame_round_new (const gchar         *id,
506                               gdouble              r,
507                               gdouble              waste)
508 {
509         lglTemplateFrame *frame;
510
511         frame = g_new0 (lglTemplateFrame, 1);
512
513         frame->shape = LGL_TEMPLATE_FRAME_SHAPE_ROUND;
514         frame->round.id = g_strdup (id);
515
516         frame->round.r = r;
517         frame->round.waste = waste;
518
519         return frame;
520 }
521
522                                                                                
523 /**
524  * lgl_template_frame_cd_new:
525  *   @id:      ID of frame.  (This should currently always be "0").
526  *   @r1:      outer radius of label in points.
527  *   @r2:      radius of center hole in points.
528  *   @w:       clip width of frame in points for business card CDs.  Should be 0 for no clipping.
529  *   @h:       clip height of frame in points for business card CDs.  Should be 0 for no clipping.
530  *   @waste:   Amount of overprint to allow.
531  *
532  * This function creates a new template frame for a CD/DVD label.
533  *
534  * Returns: Pointer to newly allocated #lglTemplateFrame structure.
535  *
536  */
537 lglTemplateFrame *
538 lgl_template_frame_cd_new (const gchar         *id,
539                            gdouble              r1,
540                            gdouble              r2,
541                            gdouble              w,
542                            gdouble              h,
543                            gdouble              waste)
544 {
545         lglTemplateFrame *frame;
546
547         frame = g_new0 (lglTemplateFrame, 1);
548
549         frame->shape = LGL_TEMPLATE_FRAME_SHAPE_CD;
550         frame->cd.id = g_strdup (id);
551
552         frame->cd.r1 = r1;
553         frame->cd.r2 = r2;
554         frame->cd.w  = w;
555         frame->cd.h  = h;
556         frame->cd.waste = waste;
557
558         return frame;
559 }
560
561
562 /**
563  * lgl_template_frame_get_size:
564  * @frame: #lglTemplateFrame structure to query
565  * @w: pointer to location to receive width of frame
566  * @h: pointer to location to receive height of frame
567  *
568  * Get size (width and height) of given #lglTemplateFrame in points.
569  *
570  */
571 void
572 lgl_template_frame_get_size (const lglTemplateFrame *frame,
573                              gdouble                *w,
574                              gdouble                *h)
575 {
576         g_return_if_fail (frame);
577
578         switch (frame->shape) {
579         case LGL_TEMPLATE_FRAME_SHAPE_RECT:
580                 *w = frame->rect.w;
581                 *h = frame->rect.h;
582                 break;
583         case LGL_TEMPLATE_FRAME_SHAPE_ELLIPSE:
584                 *w = frame->ellipse.w;
585                 *h = frame->ellipse.h;
586                 break;
587         case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
588                 *w = 2.0 * frame->round.r;
589                 *h = 2.0 * frame->round.r;
590                 break;
591         case LGL_TEMPLATE_FRAME_SHAPE_CD:
592                 if (frame->cd.w == 0.0) {
593                         *w = 2.0 * frame->cd.r1;
594                 } else {
595                         *w = frame->cd.w;
596                 }
597                 if (frame->cd.h == 0.0) {
598                         *h = 2.0 * frame->cd.r1;
599                 } else {
600                         *h = frame->cd.h;
601                 }
602                 break;
603         default:
604                 *w = 0.0;
605                 *h = 0.0;
606                 break;
607         }
608 }
609
610
611 /**
612  * lgl_template_frame_get_n_labels:
613  * @frame: #lglTemplateFrame structure to query
614  *
615  * Get total number of labels per sheet corresponding to the given frame.
616  *
617  * Returns: number of labels per sheet.
618  *
619  */
620 gint
621 lgl_template_frame_get_n_labels (const lglTemplateFrame *frame)
622 {
623         gint               n_labels = 0;
624         GList             *p;
625         lglTemplateLayout *layout;
626
627         g_return_val_if_fail (frame, 0);
628
629         for ( p=frame->all.layouts; p != NULL; p=p->next ) {
630                 layout = (lglTemplateLayout *)p->data;
631
632                 n_labels += layout->nx * layout->ny;
633         }
634
635         return n_labels;
636 }
637
638
639 /**
640  * lgl_template_frame_get_layout_description
641  * @frame: #lglTemplateFrame structure to query
642  *
643  * Get a description of the label layout including number of labels per sheet.
644  *
645  * Returns: a newly allocation description string.
646  *
647  */
648 gchar *
649 lgl_template_frame_get_layout_description (const lglTemplateFrame *frame)
650 {
651         gint                    n_labels;
652         gchar                  *string;
653         lglTemplateLayout      *layout;
654
655         n_labels = lgl_template_frame_get_n_labels (frame);
656
657         if ( frame->all.layouts && (frame->all.layouts->next == NULL) )
658         {
659                 layout = (lglTemplateLayout *)frame->all.layouts->data;
660                 /*
661                  * Translators: 1st %d = number of labels across a page,
662                  *              2nd %d = number of labels down a page,
663                  *              3rd %d = total number of labels on a page (sheet).
664                  */
665                 string = g_strdup_printf (_("%d Ã— %d (%d per sheet)"), layout->nx, layout->ny, n_labels);
666         }
667         else
668         {
669                 /* Translators: %d is the total number of labels on a page (sheet). */
670                 string = g_strdup_printf (_("%d per sheet"), n_labels);
671         }
672
673         return string;
674 }
675
676
677 /**
678  * lgl_template_frame_get_size_description
679  * @frame: #lglTemplateFrame structure to query
680  * @units: #lglUnits
681  *
682  * Get a description of the label size.
683  *
684  * Returns: a newly allocation description string.
685  *
686  */
687 gchar *
688 lgl_template_frame_get_size_description (const lglTemplateFrame *frame,
689                                          lglUnits                units)
690 {
691         const gchar               *units_string;
692         gdouble                    units_per_point;
693         gchar                     *string = NULL;
694
695         units_string    = lgl_units_get_name (units);
696         units_per_point = lgl_units_get_units_per_point (units);
697
698         switch (frame->shape) {
699         case LGL_TEMPLATE_FRAME_SHAPE_RECT:
700                 if ( units == LGL_UNITS_INCH ) {
701                         gchar *xstr, *ystr;
702
703                         xstr = lgl_str_format_fraction (frame->rect.w*units_per_point);
704                         ystr = lgl_str_format_fraction (frame->rect.h*units_per_point);
705                         string = g_strdup_printf ("%s Ã— %s %s",
706                                                   xstr, ystr, units_string);
707                         g_free (xstr);
708                         g_free (ystr);
709                 } else {
710                         string = g_strdup_printf ("%.5g Ã— %.5g %s",
711                                                   frame->rect.w*units_per_point,
712                                                   frame->rect.h*units_per_point,
713                                                   units_string);
714                 }
715                 break;
716         case LGL_TEMPLATE_FRAME_SHAPE_ELLIPSE:
717                 if ( units == LGL_UNITS_INCH ) {
718                         gchar *xstr, *ystr;
719
720                         xstr = lgl_str_format_fraction (frame->ellipse.w*units_per_point);
721                         ystr = lgl_str_format_fraction (frame->ellipse.h*units_per_point);
722                         string = g_strdup_printf ("%s Ã— %s %s",
723                                                   xstr, ystr, units_string);
724                         g_free (xstr);
725                         g_free (ystr);
726                 } else {
727                         string = g_strdup_printf ("%.5g Ã— %.5g %s",
728                                                   frame->ellipse.w*units_per_point,
729                                                   frame->ellipse.h*units_per_point,
730                                                   units_string);
731                 }
732                 break;
733         case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
734                 if ( units == LGL_UNITS_INCH ) {
735                         gchar *dstr;
736
737                         dstr = lgl_str_format_fraction (2.0*frame->round.r*units_per_point);
738                         string = g_strdup_printf ("%s %s %s",
739                                                   dstr, units_string,
740                                                   _("diameter"));
741                         g_free (dstr);
742                 } else {
743                         string = g_strdup_printf ("%.5g %s %s",
744                                                   2.0*frame->round.r*units_per_point,
745                                                   units_string,
746                                                   _("diameter"));
747                 }
748                 break;
749         case LGL_TEMPLATE_FRAME_SHAPE_CD:
750                 if ( units == LGL_UNITS_INCH ) {
751                         gchar *dstr;
752
753                         dstr = lgl_str_format_fraction (2.0*frame->cd.r1*units_per_point);
754                         string = g_strdup_printf ("%s %s %s",
755                                                   dstr, units_string,
756                                                   _("diameter"));
757                         g_free (dstr);
758                 } else {
759                         string = g_strdup_printf ("%.5g %s %s",
760                                                   2.0*frame->cd.r1*units_per_point,
761                                                   units_string,
762                                                   _("diameter"));
763                 }
764                 break;
765         default:
766                 break;
767         }
768
769         return string;
770 }
771
772
773 /**
774  * lgl_template_frame_get_origins:
775  * @frame: #lglTemplateFrame structure to query
776  *
777  * Get an array of label origins for the given frame.  These origins represent the
778  * upper left hand corner of each label on a page corresponding to the given frame.
779  * The origins will be ordered geometrically left to right and then top to bottom.
780  * The array should be freed using g_free().
781  *
782  * Returns: A newly allocated array of #lglTemplateOrigin structures.
783  *
784  */
785 lglTemplateOrigin *
786 lgl_template_frame_get_origins (const lglTemplateFrame *frame)
787 {
788         gint               i_label, n_labels, ix, iy;
789         lglTemplateOrigin *origins;
790         GList             *p;
791         lglTemplateLayout *layout;
792
793         g_return_val_if_fail (frame, NULL);
794
795         n_labels = lgl_template_frame_get_n_labels (frame);
796         origins = g_new0 (lglTemplateOrigin, n_labels);
797
798         i_label = 0;
799         for ( p=frame->all.layouts; p != NULL; p=p->next ) {
800                 layout = (lglTemplateLayout *)p->data;
801
802                 for (iy = 0; iy < layout->ny; iy++) {
803                         for (ix = 0; ix < layout->nx; ix++, i_label++) {
804                                 origins[i_label].x = ix*layout->dx + layout->x0;
805                                 origins[i_label].y = iy*layout->dy + layout->y0;
806                         }
807                 }
808         }
809
810         g_qsort_with_data (origins, n_labels, sizeof(lglTemplateOrigin),
811                            compare_origins, NULL);
812
813         return origins;
814 }
815
816
817 /**
818  * lgl_template_frame_add_layout:
819  *   @frame:  Pointer to template frame to add layout to.
820  *   @layout: Pointer to layout structure to add to frame.
821  *
822  * This function adds a layout structure to the given template frame.
823  *
824  */
825 void
826 lgl_template_frame_add_layout (lglTemplateFrame   *frame,
827                                lglTemplateLayout  *layout)
828 {
829         g_return_if_fail (frame);
830         g_return_if_fail (layout);
831
832         frame->all.layouts = g_list_append (frame->all.layouts, layout);
833 }
834  
835
836 /**
837  * lgl_template_frame_add_markup:
838  *   @frame:  Pointer to template frame to add markup to.
839  *   @markup: Pointer to markup structure to add to frame.
840  *
841  * This function adds a markup structure to the given template frame.
842  *
843  */
844 void
845 lgl_template_frame_add_markup (lglTemplateFrame   *frame,
846                                lglTemplateMarkup  *markup)
847 {
848         g_return_if_fail (frame);
849         g_return_if_fail (markup);
850
851         frame->all.markups = g_list_append (frame->all.markups, markup);
852 }
853  
854
855 /**
856  * lgl_template_layout_new:
857  *   @nx:  Number of labels across.
858  *   @ny:  Number of labels down.
859  *   @x0:  X coordinate of the top-left corner of the top-left label in the layout in points.
860  *   @y0:  Y coordinate of the top-left corner of the top-left label in the layout in points.
861  *   @dx:  Horizontal pitch in points.  This is the distance from left-edge to left-edge.
862  *   @dy:  Vertical pitch in points.  This is the distance from top-edge to top-edge.
863  *
864  * This function creates a new layout structure with the given parameters.
865  *
866  * Returns: a newly allocated #lglTemplateLayout structure.
867  *
868  */
869 lglTemplateLayout *
870 lgl_template_layout_new (gint    nx,
871                          gint    ny,
872                          gdouble x0,
873                          gdouble y0,
874                          gdouble dx,
875                          gdouble dy)
876 {
877         lglTemplateLayout *layout;
878
879         layout = g_new0 (lglTemplateLayout, 1);
880
881         layout->nx = nx;
882         layout->ny = ny;
883         layout->x0 = x0;
884         layout->y0 = y0;
885         layout->dx = dx;
886         layout->dy = dy;
887
888         return layout;
889 }
890
891
892 /**
893  * lgl_template_markup_margin_new:
894  *   @size: margin size in points.
895  *
896  * This function creates a new margin markup structure.
897  *
898  * Returns: a newly allocated #lglTemplateMarkup structure.
899  *
900  */
901 lglTemplateMarkup *
902 lgl_template_markup_margin_new (gdouble size)
903 {
904         lglTemplateMarkup *markup;
905
906         markup = g_new0 (lglTemplateMarkup, 1);
907
908         markup->type        = LGL_TEMPLATE_MARKUP_MARGIN;
909         markup->margin.size = size;
910
911         return markup;
912 }
913
914
915 /**
916  * lgl_template_markup_line_new:
917  *   @x1: x coordinate of first endpoint.
918  *   @y1: y coordinate of first endpoint.
919  *   @x2: x coordinate of second endpoint.
920  *   @y2: y coordinate of second endpoint.
921  *
922  * This function creates a new line markup structure.
923  *
924  * Returns: a newly allocated #lglTemplateMarkup structure.
925  *
926  */
927 lglTemplateMarkup *
928 lgl_template_markup_line_new (gdouble x1,
929                               gdouble y1,
930                               gdouble x2,
931                               gdouble y2)
932 {
933         lglTemplateMarkup *markup;
934
935         markup = g_new0 (lglTemplateMarkup, 1);
936
937         markup->type        = LGL_TEMPLATE_MARKUP_LINE;
938         markup->line.x1     = x1;
939         markup->line.y1     = y1;
940         markup->line.x2     = x2;
941         markup->line.y2     = y2;
942
943         return markup;
944 }
945
946
947 /**
948  * lgl_template_markup_circle_new:
949  *   @x0: x coordinate of center of circle.
950  *   @y0: y coordinate of center of circle.
951  *   @r:  radius of circle.
952  *
953  * This function creates a new circle markup structure.
954  *
955  * Returns: a newly allocated #lglTemplateMarkup structure.
956  *
957  */
958 lglTemplateMarkup *
959 lgl_template_markup_circle_new (gdouble x0,
960                                 gdouble y0,
961                                 gdouble r)
962 {
963         lglTemplateMarkup *markup;
964
965         markup = g_new0 (lglTemplateMarkup, 1);
966
967         markup->type        = LGL_TEMPLATE_MARKUP_CIRCLE;
968         markup->circle.x0   = x0;
969         markup->circle.y0   = y0;
970         markup->circle.r    = r;
971
972         return markup;
973 }
974
975
976 /**
977  * lgl_template_markup_rect_new:
978  *   @x1: x coordinate of top-left corner of rectangle.
979  *   @y1: y coordinate of top-left corner of rectangle.
980  *   @w:  width of rectangle.
981  *   @h:  height of rectangle.
982  *   @r:  radius of rounded corner.
983  *
984  * This function creates a new rectangle markup structure.
985  *
986  * Returns: a newly allocated #lglTemplateMarkup structure.
987  *
988  */
989 lglTemplateMarkup *
990 lgl_template_markup_rect_new (gdouble x1,
991                               gdouble y1,
992                               gdouble w,
993                               gdouble h,
994                               gdouble r)
995 {
996         lglTemplateMarkup *markup;
997
998         markup = g_new0 (lglTemplateMarkup, 1);
999
1000         markup->type        = LGL_TEMPLATE_MARKUP_RECT;
1001         markup->rect.x1     = x1;
1002         markup->rect.y1     = y1;
1003         markup->rect.w      = w;
1004         markup->rect.h      = h;
1005         markup->rect.r      = r;
1006
1007         return markup;
1008 }
1009
1010
1011 /**
1012  * lgl_template_markup_ellipse_new:
1013  *   @x1: x coordinate of top-left corner of ellipse.
1014  *   @y1: y coordinate of top-left corner of ellipse.
1015  *   @w:  width of ellipse.
1016  *   @h:  height of ellipse.
1017  *
1018  * This function creates a new ellipse markup structure.
1019  *
1020  * Returns: a newly allocated #lglTemplateMarkup structure.
1021  *
1022  */
1023 lglTemplateMarkup *
1024 lgl_template_markup_ellipse_new (gdouble x1,
1025                                  gdouble y1,
1026                                  gdouble w,
1027                                  gdouble h)
1028 {
1029         lglTemplateMarkup *markup;
1030
1031         markup = g_new0 (lglTemplateMarkup, 1);
1032
1033         markup->type        = LGL_TEMPLATE_MARKUP_ELLIPSE;
1034         markup->ellipse.x1     = x1;
1035         markup->ellipse.y1     = y1;
1036         markup->ellipse.w      = w;
1037         markup->ellipse.h      = h;
1038
1039         return markup;
1040 }
1041
1042
1043 /**
1044  * lgl_template_dup:
1045  *   @orig_template: Template to duplicate.
1046  *
1047  * This function duplicates a template structure.
1048  *
1049  * Returns:  a newly allocated #lglTemplate structure.
1050  *
1051  */
1052 lglTemplate *
1053 lgl_template_dup (const lglTemplate *orig_template)
1054 {
1055         lglTemplate         *template;
1056         GList               *p;
1057         lglTemplateFrame    *frame;
1058
1059         g_return_val_if_fail (orig_template, NULL);
1060
1061         template = lgl_template_new (orig_template->brand,
1062                                      orig_template->part,
1063                                      orig_template->description,
1064                                      orig_template->paper_id,
1065                                      orig_template->page_width,
1066                                      orig_template->page_height);
1067
1068         template->equiv_part  = g_strdup (orig_template->equiv_part);
1069         template->product_url = g_strdup (orig_template->product_url);
1070
1071
1072         for ( p=orig_template->category_ids; p != NULL; p=p->next )
1073         {
1074                 lgl_template_add_category (template, p->data);
1075         }
1076
1077         for ( p=orig_template->frames; p != NULL; p=p->next )
1078         {
1079                 frame = (lglTemplateFrame *)p->data;
1080
1081                 lgl_template_add_frame (template, lgl_template_frame_dup (frame));
1082         }
1083
1084         return template;
1085 }
1086
1087
1088 /**
1089  * lgl_template_free:
1090  *   @template: Template to free.
1091  *
1092  * This function frees all memory associated with given template structure.
1093  *
1094  */
1095 void
1096 lgl_template_free (lglTemplate *template)
1097 {
1098         GList            *p;
1099         lglTemplateFrame *frame;
1100
1101         if ( template != NULL ) {
1102
1103                 g_free (template->brand);
1104                 template->brand = NULL;
1105
1106                 g_free (template->part);
1107                 template->part = NULL;
1108
1109                 g_free (template->description);
1110                 template->description = NULL;
1111
1112                 g_free (template->paper_id);
1113                 template->paper_id = NULL;
1114
1115                 for ( p=template->category_ids; p != NULL; p=p->next ) {
1116
1117                         g_free (p->data);
1118                         p->data = NULL;
1119
1120                 }
1121                 g_list_free (template->category_ids);
1122                 template->category_ids = NULL;
1123
1124                 for ( p=template->frames; p != NULL; p=p->next ) {
1125
1126                         frame = (lglTemplateFrame *)p->data;
1127
1128                         lgl_template_frame_free (frame);
1129                         p->data = NULL;
1130                 }
1131                 g_list_free (template->frames);
1132                 template->frames = NULL;
1133
1134                 g_free (template);
1135
1136         }
1137
1138 }
1139
1140
1141 /**
1142  * lgl_template_frame_dup:
1143  *   @orig_frame: Frame to duplicate.
1144  *
1145  * This function duplicates a template frame structure.
1146  *
1147  * Returns:  a newly allocated #lglTemplateFrame structure.
1148  *
1149  */
1150 lglTemplateFrame *
1151 lgl_template_frame_dup (const lglTemplateFrame *orig_frame)
1152 {
1153         lglTemplateFrame    *frame;
1154         GList               *p;
1155         lglTemplateLayout   *layout;
1156         lglTemplateMarkup   *markup;
1157
1158         g_return_val_if_fail (orig_frame, NULL);
1159
1160         switch (orig_frame->shape) {
1161
1162         case LGL_TEMPLATE_FRAME_SHAPE_RECT:
1163                 frame =
1164                         lgl_template_frame_rect_new (orig_frame->all.id,
1165                                                      orig_frame->rect.w,
1166                                                      orig_frame->rect.h,
1167                                                      orig_frame->rect.r,
1168                                                      orig_frame->rect.x_waste,
1169                                                      orig_frame->rect.y_waste);
1170                 break;
1171
1172         case LGL_TEMPLATE_FRAME_SHAPE_ELLIPSE:
1173                 frame =
1174                         lgl_template_frame_ellipse_new (orig_frame->all.id,
1175                                                         orig_frame->ellipse.w,
1176                                                         orig_frame->ellipse.h,
1177                                                         orig_frame->ellipse.waste);
1178                 break;
1179
1180         case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
1181                 frame =
1182                         lgl_template_frame_round_new (orig_frame->all.id,
1183                                                       orig_frame->round.r,
1184                                                       orig_frame->round.waste);
1185                 break;
1186
1187         case LGL_TEMPLATE_FRAME_SHAPE_CD:
1188                 frame =
1189                         lgl_template_frame_cd_new (orig_frame->all.id,
1190                                                    orig_frame->cd.r1,
1191                                                    orig_frame->cd.r2,
1192                                                    orig_frame->cd.w,
1193                                                    orig_frame->cd.h,
1194                                                    orig_frame->cd.waste);
1195                 break;
1196
1197         default:
1198                 return NULL;
1199                 break;
1200         }
1201
1202         for ( p=orig_frame->all.layouts; p != NULL; p=p->next ) {
1203
1204                 layout = (lglTemplateLayout *)p->data;
1205
1206                 lgl_template_frame_add_layout (frame, lgl_template_layout_dup (layout));
1207         }
1208
1209         for ( p=orig_frame->all.markups; p != NULL; p=p->next ) {
1210
1211                 markup = (lglTemplateMarkup *)p->data;
1212
1213                 lgl_template_frame_add_markup (frame, lgl_template_markup_dup (markup));
1214         }
1215
1216         return frame;
1217 }
1218
1219
1220 /**
1221  * lgl_template_frame_free:
1222  *   @frame: Frame to free.
1223  *
1224  * This function frees all memory associated with given template frame structure.
1225  *
1226  */
1227 void
1228 lgl_template_frame_free (lglTemplateFrame *frame)
1229 {
1230         GList                *p;
1231         lglTemplateLayout    *layout;
1232         lglTemplateMarkup    *markup;
1233
1234         if ( frame != NULL ) {
1235
1236                 g_free (frame->all.id);
1237                 frame->all.id = NULL;
1238
1239                 for ( p=frame->all.layouts; p != NULL; p=p->next ) {
1240
1241                         layout = (lglTemplateLayout *)p->data;
1242
1243                         lgl_template_layout_free (layout);
1244                         p->data = NULL;
1245                 }
1246                 g_list_free (frame->all.layouts);
1247                 frame->all.layouts = NULL;
1248
1249                 for ( p=frame->all.markups; p != NULL; p=p->next ) {
1250
1251                         markup = (lglTemplateMarkup *)p->data;
1252
1253                         lgl_template_markup_free (markup);
1254                         p->data = NULL;
1255                 }
1256                 g_list_free (frame->all.markups);
1257                 frame->all.markups = NULL;
1258
1259                 g_free (frame);
1260
1261         }
1262
1263 }
1264
1265
1266 /**
1267  * lgl_template_layout_dup:
1268  *   @orig_layout: Layout to duplicate.
1269  *
1270  * This function duplicates a template layout structure.
1271  *
1272  * Returns:  a newly allocated #lglTemplateLayout structure.
1273  *
1274  */
1275 lglTemplateLayout *
1276 lgl_template_layout_dup (const lglTemplateLayout *orig_layout)
1277 {
1278         lglTemplateLayout *layout;
1279
1280         g_return_val_if_fail (orig_layout, NULL);
1281
1282         layout = g_new0 (lglTemplateLayout, 1);
1283
1284         /* copy contents */
1285         *layout = *orig_layout;
1286
1287         return layout;
1288 }
1289
1290
1291 /**
1292  * lgl_template_layout_free:
1293  *   @layout: Layout to free.
1294  *
1295  * This function frees all memory associated with given template layout structure.
1296  *
1297  */
1298 void
1299 lgl_template_layout_free (lglTemplateLayout *layout)
1300 {
1301         g_free (layout);
1302 }
1303
1304
1305 /**
1306  * lgl_template_markup_dup:
1307  *   @orig_markup: Markup to duplicate.
1308  *
1309  * This function duplicates a template markup structure.
1310  *
1311  * Returns:  a newly allocated #lglTemplateMarkup structure.
1312  *
1313  */
1314 lglTemplateMarkup *
1315 lgl_template_markup_dup (const lglTemplateMarkup *orig_markup)
1316 {
1317         lglTemplateMarkup *markup;
1318
1319         g_return_val_if_fail (orig_markup, NULL);
1320
1321         markup = g_new0 (lglTemplateMarkup, 1);
1322
1323         *markup = *orig_markup;
1324
1325         return markup;
1326 }
1327
1328
1329 /**
1330  * lgl_template_markup_free:
1331  *   @markup: Markup to free.
1332  *
1333  * This function frees all memory associated with given template markup structure.
1334  *
1335  */
1336 void
1337 lgl_template_markup_free (lglTemplateMarkup *markup)
1338 {
1339         g_free (markup);
1340 }
1341
1342
1343 static gint
1344 compare_origins (gconstpointer a,
1345                  gconstpointer b,
1346                  gpointer      user_data)
1347 {
1348         const lglTemplateOrigin *a_origin = a, *b_origin = b;
1349
1350         if ( a_origin->y < b_origin->y ) {
1351                 return -1;
1352         } else if ( a_origin->y > b_origin->y ) {
1353                 return +1;
1354         } else {
1355                 if ( a_origin->x < b_origin->x ) {
1356                         return -1;
1357                 } else if ( a_origin->x > b_origin->x ) {
1358                         return +1;
1359                 } else {
1360                         return 0; /* hopefully 2 labels won't have the same origin */
1361                 }
1362         }
1363 }
1364
1365
1366 /**
1367  * lgl_template_print:
1368  *   @template: template
1369  *
1370  * Print template details (for debugging purposes).
1371  *
1372  */
1373 void
1374 lgl_template_print (const lglTemplate *template)
1375 {
1376         g_print ("---- %s( TEMPLATE=%p ) ----\n", __FUNCTION__, template);
1377
1378         g_print("brand=\"%s\", part=\"%s\", description=\"%s\"\n",
1379                 template->brand, template->part, template->description);
1380
1381         g_print("paper_id=\"%s\", page_width=%g, page_height=%g\n",
1382                 template->paper_id, template->page_width, template->page_height);
1383
1384         g_print ("\n");
1385
1386 }
1387
1388
1389
1390 /*
1391  * Local Variables:       -- emacs
1392  * mode: C                -- emacs
1393  * c-basic-offset: 8      -- emacs
1394  * tab-width: 8           -- emacs
1395  * indent-tabs-mode: nil  -- emacs
1396  * End:                   -- emacs
1397  */