3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of libglabels.
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.
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.
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/>.
25 #include <glib/gi18n.h>
29 #include <sys/types.h>
31 #include "libglabels-private.h"
36 /*===========================================*/
38 /*===========================================*/
41 /*===========================================*/
43 /*===========================================*/
46 /*===========================================*/
47 /* Local function prototypes */
48 /*===========================================*/
50 static gint compare_origins (gconstpointer a,
54 /*===========================================*/
56 /*===========================================*/
60 * @brand: Template brand
61 * @part: Template part name/number
62 * @description: Template descriptions
63 * @paper_id: Page size id
64 * @page_width: Page width in points, set to zero unless paper_id="Other"
65 * @page_height: Page height in points, set to zero unless paper_id="Other"
67 * Create a new template structure, with the given top-level attributes. The
68 * created template will have no initial aliases, categories, or frames
69 * associated with it. See lgl_template_add_alias(), lgl_template_add_category(),
70 * and lgl_template_add_frame() to add these.
72 * Returns: pointer to a newly allocated #lglTemplate structure.
76 lgl_template_new (const gchar *brand,
78 const gchar *description,
79 const gchar *paper_id,
83 lglTemplate *template;
84 lglTemplateAlias *alias;
86 template = g_new0 (lglTemplate,1);
88 template->brand = g_strdup (brand);
89 template->part = g_strdup (part);
90 template->description = g_strdup (description);
91 template->paper_id = g_strdup (paper_id);
92 template->page_width = page_width;
93 template->page_height = page_height;
95 /* Always include primary name in alias list. */
96 template->aliases = NULL;
97 alias = lgl_template_alias_new (brand, part);
98 lgl_template_add_alias (template, alias);
105 * lgl_template_new_from_equiv:
106 * @brand: Template brand
107 * @part: Template part name/number
108 * @equiv_part: Name of equivalent part to base template on
110 * Create a new template structure based on an existing template. The
111 * created template will be a duplicate of the original template, except with
112 * the new part name/number.
114 * Returns: pointer to a newly allocated #lglTemplate structure.
118 lgl_template_new_from_equiv (const gchar *brand,
120 const gchar *equiv_part)
122 lglTemplate *template;
124 lglTemplateAlias *alias;
126 template = lgl_db_lookup_template_from_brand_part (brand, equiv_part);
129 g_free (template->part);
130 g_free (template->equiv_part);
132 template->part = g_strdup (part);
133 template->equiv_part = g_strdup (equiv_part);
135 for ( p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next )
137 alias = (lglTemplateAlias *)p_alias->data;
138 lgl_template_alias_free (alias);
140 g_list_free (template->aliases);
141 template->aliases = NULL;
143 alias = lgl_template_alias_new (brand, part);
144 lgl_template_add_alias (template, alias);
148 g_message (_("Equivalent part (\"%s\") for \"%s\", not previously defined."),
157 * lgl_template_get_name:
158 * @template: Pointer to template structure to test
160 * This function returns the name of the given template. The name is the concetenation
161 * of the brand and part name/number.
163 * Returns: A pointer to a newly allocated name string. Should be freed with g_free().
167 lgl_template_get_name (const lglTemplate *template)
169 g_return_val_if_fail (template, NULL);
171 return g_strdup_printf ("%s %s", template->brand, template->part);
176 * lgl_template_do_templates_match:
177 * @template1: Pointer to 1st template structure to test
178 * @template2: Pointer to 2nd template structure to test
180 * This function tests if the given templates match. This is a simple test that only tests
181 * the brand and part name/number. It does not test if they are actually identical.
183 * Returns: TRUE if the two template matche.
187 lgl_template_do_templates_match (const lglTemplate *template1,
188 const lglTemplate *template2)
190 g_return_val_if_fail (template1, FALSE);
191 g_return_val_if_fail (template2, FALSE);
193 return (UTF8_EQUAL (template1->brand, template2->brand) &&
194 UTF8_EQUAL (template1->part, template2->part));
199 * lgl_template_does_brand_match:
200 * @template: Pointer to template structure to test
201 * @brand: Brand string
203 * This function tests if the brand of the template matches the given brand.
205 * Returns: TRUE if the template matches the given brand.
209 lgl_template_does_brand_match (const lglTemplate *template,
212 g_return_val_if_fail (template, FALSE);
214 /* NULL matches everything. */
220 return UTF8_EQUAL (template->brand, brand);
225 * lgl_template_does_page_size_match:
226 * @template: Pointer to template structure to test
227 * @paper_id: Page size ID string
229 * This function tests if the page size of the template matches the given ID.
231 * Returns: TRUE if the template matches the given page size ID.
235 lgl_template_does_page_size_match (const lglTemplate *template,
236 const gchar *paper_id)
238 g_return_val_if_fail (template, FALSE);
240 /* NULL matches everything. */
241 if (paper_id == NULL)
246 return ASCII_EQUAL(paper_id, template->paper_id);
251 * lgl_template_does_category_match:
252 * @template: Pointer to template structure to test
253 * @category_id: Category ID string
255 * This function tests if the given template belongs to the given category ID.
257 * Returns: TRUE if the template matches the given category ID.
261 lgl_template_does_category_match (const lglTemplate *template,
262 const gchar *category_id)
266 g_return_val_if_fail (template, FALSE);
268 /* NULL matches everything. */
269 if (category_id == NULL)
274 for ( p=template->category_ids; p != NULL; p=p->next )
276 if (ASCII_EQUAL(category_id, p->data))
287 * lgl_template_alias_new:
288 * @brand: Alias brand
289 * @part: Alias part name/number
291 * Create a new template alias structure, with the given brand and part number.
293 * Returns: pointer to a newly allocated #lglTemplateAlias structure.
297 lgl_template_alias_new (const gchar *brand,
300 lglTemplateAlias *alias;
302 alias = g_new0 (lglTemplateAlias,1);
304 alias->brand = g_strdup (brand);
305 alias->part = g_strdup (part);
312 * lgl_template_add_alias:
313 * @template: Pointer to template structure
314 * @alias: Alias string
316 * This function adds the given alias to a templates list of aliases.
320 lgl_template_add_alias (lglTemplate *template,
321 lglTemplateAlias *alias)
323 g_return_if_fail (template);
324 g_return_if_fail (alias);
326 template->aliases = g_list_append (template->aliases, alias);
331 * lgl_template_add_frame:
332 * @template: Pointer to template structure
333 * @frame: Pointer to frame structure
335 * This function adds the given frame structure to the template. Once added,
336 * the frame structure belongs to the given template; do not attempt to free
339 * Note: Currently glabels only supports a single frame per template.
343 lgl_template_add_frame (lglTemplate *template,
344 lglTemplateFrame *frame)
346 g_return_if_fail (template);
347 g_return_if_fail (frame);
349 template->frames = g_list_append (template->frames, frame);
354 * lgl_template_add_category:
355 * @template: Pointer to template structure
356 * @category_id: Category ID string
358 * This function adds the given category ID to a templates category list.
362 lgl_template_add_category (lglTemplate *template,
363 const gchar *category_id)
365 g_return_if_fail (template);
366 g_return_if_fail (category_id);
368 template->category_ids = g_list_append (template->category_ids,
369 g_strdup (category_id));
374 * lgl_template_frame_rect_new:
375 * @id: ID of frame. (This should currently always be "0").
376 * @w: width of frame in points.
377 * @h: height of frame in points.
378 * @r: radius of rounded corners in points. (Should be 0 for square corners.)
379 * @x_waste: Amount of overprint to allow in the horizontal direction.
380 * @y_waste: Amount of overprint to allow in the vertical direction.
382 * This function creates a new template frame for a rectangular label or card.
384 * Returns: Pointer to newly allocated #lglTemplateFrame structure.
388 lgl_template_frame_rect_new (const gchar *id,
395 lglTemplateFrame *frame;
397 frame = g_new0 (lglTemplateFrame, 1);
399 frame->shape = LGL_TEMPLATE_FRAME_SHAPE_RECT;
400 frame->rect.id = g_strdup (id);
405 frame->rect.x_waste = x_waste;
406 frame->rect.y_waste = y_waste;
413 * lgl_template_frame_round_new:
414 * @id: ID of frame. (This should currently always be "0").
415 * @r: radius of label in points.
416 * @waste: Amount of overprint to allow.
418 * This function creates a new template frame for a round label.
420 * Returns: Pointer to newly allocated #lglTemplateFrame structure.
424 lgl_template_frame_round_new (const gchar *id,
428 lglTemplateFrame *frame;
430 frame = g_new0 (lglTemplateFrame, 1);
432 frame->shape = LGL_TEMPLATE_FRAME_SHAPE_ROUND;
433 frame->round.id = g_strdup (id);
436 frame->round.waste = waste;
443 * lgl_template_frame_cd_new:
444 * @id: ID of frame. (This should currently always be "0").
445 * @r1: outer radius of label in points.
446 * @r2: radius of center hole in points.
447 * @w: clip width of frame in points for business card CDs. Should be 0 for no clipping.
448 * @h: clip height of frame in points for business card CDs. Should be 0 for no clipping.
449 * @waste: Amount of overprint to allow.
451 * This function creates a new template frame for a CD/DVD label.
453 * Returns: Pointer to newly allocated #lglTemplateFrame structure.
457 lgl_template_frame_cd_new (const gchar *id,
464 lglTemplateFrame *frame;
466 frame = g_new0 (lglTemplateFrame, 1);
468 frame->shape = LGL_TEMPLATE_FRAME_SHAPE_CD;
469 frame->cd.id = g_strdup (id);
475 frame->cd.waste = waste;
482 * lgl_template_frame_get_size:
483 * @frame: #lglTemplateFrame structure to query
484 * @w: pointer to location to receive width of frame
485 * @h: pointer to location to receive height of frame
487 * Get size (width and height) of given #lglTemplateFrame in points.
491 lgl_template_frame_get_size (const lglTemplateFrame *frame,
495 g_return_if_fail (frame);
497 switch (frame->shape) {
498 case LGL_TEMPLATE_FRAME_SHAPE_RECT:
502 case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
503 *w = 2.0 * frame->round.r;
504 *h = 2.0 * frame->round.r;
506 case LGL_TEMPLATE_FRAME_SHAPE_CD:
507 if (frame->cd.w == 0.0) {
508 *w = 2.0 * frame->cd.r1;
512 if (frame->cd.h == 0.0) {
513 *h = 2.0 * frame->cd.r1;
527 * lgl_template_frame_get_n_labels:
528 * @frame: #lglTemplateFrame structure to query
530 * Get total number of labels per sheet corresponding to the given frame.
532 * Returns: number of labels per sheet.
536 lgl_template_frame_get_n_labels (const lglTemplateFrame *frame)
540 lglTemplateLayout *layout;
542 g_return_val_if_fail (frame, 0);
544 for ( p=frame->all.layouts; p != NULL; p=p->next ) {
545 layout = (lglTemplateLayout *)p->data;
547 n_labels += layout->nx * layout->ny;
555 * lgl_template_frame_get_origins:
556 * @frame: #lglTemplateFrame structure to query
558 * Get an array of label origins for the given frame. These origins represent the
559 * upper left hand corner of each label on a page corresponding to the given frame.
560 * The origins will be ordered geometrically left to right and then top to bottom.
561 * The array should be freed using g_free().
563 * Returns: A newly allocated array of #lglTemplateOrigin structures.
567 lgl_template_frame_get_origins (const lglTemplateFrame *frame)
569 gint i_label, n_labels, ix, iy;
570 lglTemplateOrigin *origins;
572 lglTemplateLayout *layout;
574 g_return_val_if_fail (frame, NULL);
576 n_labels = lgl_template_frame_get_n_labels (frame);
577 origins = g_new0 (lglTemplateOrigin, n_labels);
580 for ( p=frame->all.layouts; p != NULL; p=p->next ) {
581 layout = (lglTemplateLayout *)p->data;
583 for (iy = 0; iy < layout->ny; iy++) {
584 for (ix = 0; ix < layout->nx; ix++, i_label++) {
585 origins[i_label].x = ix*layout->dx + layout->x0;
586 origins[i_label].y = iy*layout->dy + layout->y0;
591 g_qsort_with_data (origins, n_labels, sizeof(lglTemplateOrigin),
592 compare_origins, NULL);
599 * lgl_template_frame_add_layout:
600 * @frame: Pointer to template frame to add layout to.
601 * @layout: Pointer to layout structure to add to frame.
603 * This function adds a layout structure to the given template frame.
607 lgl_template_frame_add_layout (lglTemplateFrame *frame,
608 lglTemplateLayout *layout)
610 g_return_if_fail (frame);
611 g_return_if_fail (layout);
613 frame->all.layouts = g_list_append (frame->all.layouts, layout);
618 * lgl_template_frame_add_markup:
619 * @frame: Pointer to template frame to add markup to.
620 * @markup: Pointer to markup structure to add to frame.
622 * This function adds a markup structure to the given template frame.
626 lgl_template_frame_add_markup (lglTemplateFrame *frame,
627 lglTemplateMarkup *markup)
629 g_return_if_fail (frame);
630 g_return_if_fail (markup);
632 frame->all.markups = g_list_append (frame->all.markups, markup);
637 * lgl_template_layout_new:
638 * @nx: Number of labels across.
639 * @ny: Number of labels down.
640 * @x0: X coordinate of the top-left corner of the top-left label in the layout in points.
641 * @y0: Y coordinate of the top-left corner of the top-left label in the layout in points.
642 * @dx: Horizontal pitch in points. This is the distance from left-edge to left-edge.
643 * @dy: Vertical pitch in points. This is the distance from top-edge to top-edge.
645 * This function creates a new layout structure with the given parameters.
647 * Returns: a newly allocated #lglTemplateLayout structure.
651 lgl_template_layout_new (gint nx,
658 lglTemplateLayout *layout;
660 layout = g_new0 (lglTemplateLayout, 1);
674 * lgl_template_markup_margin_new:
675 * @size: margin size in points.
677 * This function creates a new margin markup structure.
679 * Returns: a newly allocated #lglTemplateMarkup structure.
683 lgl_template_markup_margin_new (gdouble size)
685 lglTemplateMarkup *markup;
687 markup = g_new0 (lglTemplateMarkup, 1);
689 markup->type = LGL_TEMPLATE_MARKUP_MARGIN;
690 markup->margin.size = size;
697 * lgl_template_markup_line_new:
698 * @x1: x coordinate of first endpoint.
699 * @y1: y coordinate of first endpoint.
700 * @x2: x coordinate of second endpoint.
701 * @y2: y coordinate of second endpoint.
703 * This function creates a new line markup structure.
705 * Returns: a newly allocated #lglTemplateMarkup structure.
709 lgl_template_markup_line_new (gdouble x1,
714 lglTemplateMarkup *markup;
716 markup = g_new0 (lglTemplateMarkup, 1);
718 markup->type = LGL_TEMPLATE_MARKUP_LINE;
719 markup->line.x1 = x1;
720 markup->line.y1 = y1;
721 markup->line.x2 = x2;
722 markup->line.y2 = y2;
729 * lgl_template_markup_circle_new:
730 * @x0: x coordinate of center of circle.
731 * @y0: y coordinate of center of circle.
732 * @r: radius of circle.
734 * This function creates a new circle markup structure.
736 * Returns: a newly allocated #lglTemplateMarkup structure.
740 lgl_template_markup_circle_new (gdouble x0,
744 lglTemplateMarkup *markup;
746 markup = g_new0 (lglTemplateMarkup, 1);
748 markup->type = LGL_TEMPLATE_MARKUP_CIRCLE;
749 markup->circle.x0 = x0;
750 markup->circle.y0 = y0;
751 markup->circle.r = r;
758 * lgl_template_markup_rect_new:
759 * @x1: x coordinate of top-left corner of rectangle.
760 * @y1: y coordinate of top-left corner of rectangle.
761 * @w: width of rectangle.
762 * @h: height of rectangle.
763 * @r: radius of rounded corner.
765 * This function creates a new rectangle markup structure.
767 * Returns: a newly allocated #lglTemplateMarkup structure.
771 lgl_template_markup_rect_new (gdouble x1,
777 lglTemplateMarkup *markup;
779 markup = g_new0 (lglTemplateMarkup, 1);
781 markup->type = LGL_TEMPLATE_MARKUP_RECT;
782 markup->rect.x1 = x1;
783 markup->rect.y1 = y1;
794 * @orig_template: Template to duplicate.
796 * This function duplicates a template structure.
798 * Returns: a newly allocated #lglTemplate structure.
802 lgl_template_dup (const lglTemplate *orig_template)
804 lglTemplate *template;
805 lglTemplateAlias *alias;
807 lglTemplateFrame *frame;
809 g_return_val_if_fail (orig_template, NULL);
811 template = lgl_template_new (orig_template->brand,
813 orig_template->description,
814 orig_template->paper_id,
815 orig_template->page_width,
816 orig_template->page_height);
818 template->equiv_part = g_strdup (orig_template->equiv_part);
819 template->product_url = g_strdup (orig_template->product_url);
821 for ( p=orig_template->aliases; p != NULL; p=p->next )
823 alias = (lglTemplateAlias *)p->data;
825 if ( !(UTF8_EQUAL (template->brand, alias->brand) &&
826 UTF8_EQUAL (template->part, alias->part)) )
828 lgl_template_add_alias (template, lgl_template_alias_dup (alias));
833 for ( p=orig_template->category_ids; p != NULL; p=p->next )
835 lgl_template_add_category (template, p->data);
838 for ( p=orig_template->frames; p != NULL; p=p->next )
840 frame = (lglTemplateFrame *)p->data;
842 lgl_template_add_frame (template, lgl_template_frame_dup (frame));
851 * @template: Template to free.
853 * This function frees all memory associated with given template structure.
857 lgl_template_free (lglTemplate *template)
860 lglTemplateFrame *frame;
862 if ( template != NULL ) {
864 g_free (template->brand);
865 template->brand = NULL;
867 g_free (template->part);
868 template->part = NULL;
870 g_free (template->description);
871 template->description = NULL;
873 g_free (template->paper_id);
874 template->paper_id = NULL;
876 for ( p=template->aliases; p != NULL; p=p->next ) {
878 lgl_template_alias_free (p->data);
882 g_list_free (template->aliases);
883 template->aliases = NULL;
885 for ( p=template->category_ids; p != NULL; p=p->next ) {
891 g_list_free (template->category_ids);
892 template->category_ids = NULL;
894 for ( p=template->frames; p != NULL; p=p->next ) {
896 frame = (lglTemplateFrame *)p->data;
898 lgl_template_frame_free (frame);
901 g_list_free (template->frames);
902 template->frames = NULL;
912 * lgl_template_alias_dup:
913 * @orig_alias: Alias to duplicate.
915 * This function duplicates a template alias structure.
917 * Returns: a newly allocated #lglTemplateAlias structure.
921 lgl_template_alias_dup (const lglTemplateAlias *orig_alias)
923 g_return_val_if_fail (orig_alias, NULL);
925 return lgl_template_alias_new (orig_alias->brand, orig_alias->part);
930 * lgl_template_alias_free:
931 * @alias: Alias to free.
933 * This function frees all memory associated with given template alias structure.
937 lgl_template_alias_free (lglTemplateAlias *alias)
942 g_free (alias->brand);
945 g_free (alias->part);
954 * lgl_template_frame_dup:
955 * @orig_frame: Frame to duplicate.
957 * This function duplicates a template frame structure.
959 * Returns: a newly allocated #lglTemplateFrame structure.
963 lgl_template_frame_dup (const lglTemplateFrame *orig_frame)
965 lglTemplateFrame *frame;
967 lglTemplateLayout *layout;
968 lglTemplateMarkup *markup;
970 g_return_val_if_fail (orig_frame, NULL);
972 switch (orig_frame->shape) {
974 case LGL_TEMPLATE_FRAME_SHAPE_RECT:
976 lgl_template_frame_rect_new (orig_frame->all.id,
980 orig_frame->rect.x_waste,
981 orig_frame->rect.y_waste);
984 case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
986 lgl_template_frame_round_new (orig_frame->all.id,
988 orig_frame->round.waste);
991 case LGL_TEMPLATE_FRAME_SHAPE_CD:
993 lgl_template_frame_cd_new (orig_frame->all.id,
998 orig_frame->cd.waste);
1006 for ( p=orig_frame->all.layouts; p != NULL; p=p->next ) {
1008 layout = (lglTemplateLayout *)p->data;
1010 lgl_template_frame_add_layout (frame, lgl_template_layout_dup (layout));
1013 for ( p=orig_frame->all.markups; p != NULL; p=p->next ) {
1015 markup = (lglTemplateMarkup *)p->data;
1017 lgl_template_frame_add_markup (frame, lgl_template_markup_dup (markup));
1025 * lgl_template_frame_free:
1026 * @frame: Frame to free.
1028 * This function frees all memory associated with given template frame structure.
1032 lgl_template_frame_free (lglTemplateFrame *frame)
1035 lglTemplateLayout *layout;
1036 lglTemplateMarkup *markup;
1038 if ( frame != NULL ) {
1040 g_free (frame->all.id);
1041 frame->all.id = NULL;
1043 for ( p=frame->all.layouts; p != NULL; p=p->next ) {
1045 layout = (lglTemplateLayout *)p->data;
1047 lgl_template_layout_free (layout);
1050 g_list_free (frame->all.layouts);
1051 frame->all.layouts = NULL;
1053 for ( p=frame->all.markups; p != NULL; p=p->next ) {
1055 markup = (lglTemplateMarkup *)p->data;
1057 lgl_template_markup_free (markup);
1060 g_list_free (frame->all.markups);
1061 frame->all.markups = NULL;
1071 * lgl_template_layout_dup:
1072 * @orig_layout: Layout to duplicate.
1074 * This function duplicates a template layout structure.
1076 * Returns: a newly allocated #lglTemplateLayout structure.
1080 lgl_template_layout_dup (const lglTemplateLayout *orig_layout)
1082 lglTemplateLayout *layout;
1084 g_return_val_if_fail (orig_layout, NULL);
1086 layout = g_new0 (lglTemplateLayout, 1);
1089 *layout = *orig_layout;
1096 * lgl_template_layout_free:
1097 * @layout: Layout to free.
1099 * This function frees all memory associated with given template layout structure.
1103 lgl_template_layout_free (lglTemplateLayout *layout)
1110 * lgl_template_markup_dup:
1111 * @orig_markup: Markup to duplicate.
1113 * This function duplicates a template markup structure.
1115 * Returns: a newly allocated #lglTemplateMarkup structure.
1119 lgl_template_markup_dup (const lglTemplateMarkup *orig_markup)
1121 lglTemplateMarkup *markup;
1123 g_return_val_if_fail (orig_markup, NULL);
1125 markup = g_new0 (lglTemplateMarkup, 1);
1127 *markup = *orig_markup;
1134 * lgl_template_markup_free:
1135 * @markup: Markup to free.
1137 * This function frees all memory associated with given template markup structure.
1141 lgl_template_markup_free (lglTemplateMarkup *markup)
1148 compare_origins (gconstpointer a,
1152 const lglTemplateOrigin *a_origin = a, *b_origin = b;
1154 if ( a_origin->y < b_origin->y ) {
1156 } else if ( a_origin->y > b_origin->y ) {
1159 if ( a_origin->x < b_origin->x ) {
1161 } else if ( a_origin->x > b_origin->x ) {
1164 return 0; /* hopefully 2 labels won't have the same origin */
1172 * Local Variables: -- emacs
1174 * c-basic-offset: 8 -- emacs
1175 * tab-width: 8 -- emacs
1176 * indent-tabs-mode: nil -- emacs