2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * print.c: Print module
6 * Copyright (C) 2001 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
38 #include <libglabels/template.h>
43 #define ARC_FINE 2 /* Resolution in degrees of large arcs */
44 #define ARC_COURSE 5 /* Resolution in degrees of small arcs */
46 #define TICK_OFFSET 2.25
47 #define TICK_LENGTH 18.0
49 /*=========================================================================*/
51 /*=========================================================================*/
53 typedef struct _PrintInfo {
54 /* gnome print context */
55 GnomePrintContext *pc;
57 /* gnome print configuration */
58 GnomePrintConfig *config;
60 /* gLabels Template */
62 gboolean label_rotate_flag;
73 /*=========================================================================*/
74 /* Private function prototypes. */
75 /*=========================================================================*/
76 static PrintInfo *print_info_new (GnomePrintJob *job,
79 static void print_info_free (PrintInfo **pi);
82 static void print_page_begin (PrintInfo *pi);
84 static void print_page_end (PrintInfo *pi);
86 static void print_crop_marks (PrintInfo *pi);
88 static void print_label (PrintInfo *pi,
92 glMergeRecord *record,
93 gboolean outline_flag,
94 gboolean reverse_flag);
97 static void draw_label (PrintInfo *pi,
99 glMergeRecord *record);
102 static void draw_object (PrintInfo *pi,
103 glLabelObject *object,
104 glMergeRecord *record);
106 static void draw_text_object (PrintInfo *pi,
108 glMergeRecord *record);
110 static void draw_box_object (PrintInfo *pi,
113 static void draw_line_object (PrintInfo *pi,
114 glLabelLine *object);
116 static void draw_ellipse_object (PrintInfo *pi,
117 glLabelEllipse *object);
119 static void draw_image_object (PrintInfo *pi,
120 glLabelImage *object,
121 glMergeRecord *record);
123 static void draw_barcode_object (PrintInfo *pi,
124 glLabelBarcode *object,
125 glMergeRecord *record);
128 static void draw_outline (PrintInfo *pi,
131 static void clip_to_outline (PrintInfo *pi,
134 static void clip_punchouts (PrintInfo *pi,
138 static void create_rectangle_path (GnomePrintContext *pc,
144 static void create_ellipse_path (GnomePrintContext *pc,
150 static void create_rounded_rectangle_path (GnomePrintContext *pc,
157 static void create_clipped_circle_path (GnomePrintContext *pc,
164 #ifndef NO_ALPHA_HACK
165 static guchar * get_pixels_as_rgb (const GdkPixbuf *pixbuf);
169 /*****************************************************************************/
170 /* Simple (no merge data) print command. */
171 /*****************************************************************************/
173 gl_print_simple (GnomePrintJob *job,
181 const glTemplateLabelType *label_type;
182 gint i_sheet, i_label;
183 glTemplateOrigin *origins;
185 gl_debug (DEBUG_PRINT, "START");
187 pi = print_info_new (job, label);
188 label_type = gl_template_get_first_label_type (pi->template);
190 origins = gl_template_get_origins (label_type);
192 for (i_sheet = 0; i_sheet < n_sheets; i_sheet++) {
194 print_page_begin (pi);
195 if (flags->crop_marks) {
196 print_crop_marks (pi);
199 for (i_label = first - 1; i_label < last; i_label++) {
201 print_label (pi, label,
202 origins[i_label].x, origins[i_label].y,
203 NULL, flags->outline, flags->reverse);
212 print_info_free (&pi);
214 gl_debug (DEBUG_PRINT, "END");
217 /*****************************************************************************/
218 /* Merge print command (collated copies) */
219 /*****************************************************************************/
221 gl_print_merge_collated (GnomePrintJob *job,
228 const GList *record_list;
230 const glTemplateLabelType *label_type;
231 gint i_sheet, i_label, n_labels_per_page, i_copy;
232 glMergeRecord *record;
234 glTemplateOrigin *origins;
236 gl_debug (DEBUG_PRINT, "START");
238 merge = gl_label_get_merge (label);
239 record_list = gl_merge_get_record_list (merge);
241 pi = print_info_new (job, label);
242 label_type = gl_template_get_first_label_type (pi->template);
244 n_labels_per_page = gl_template_get_n_labels (label_type);
245 origins = gl_template_get_origins (label_type);
250 for ( p=(GList *)record_list; p!=NULL; p=p->next ) {
251 record = (glMergeRecord *)p->data;
253 if ( record->select_flag ) {
254 for (i_copy = 0; i_copy < n_copies; i_copy++) {
256 if ((i_label == 0) || (i_sheet == 0)) {
258 print_page_begin (pi);
259 if (flags->crop_marks) {
260 print_crop_marks (pi);
264 print_label (pi, label,
268 flags->outline, flags->reverse);
270 i_label = (i_label + 1) % n_labels_per_page;
284 print_info_free (&pi);
286 gl_debug (DEBUG_PRINT, "END");
289 /*****************************************************************************/
290 /* Merge print command (uncollated copies) */
291 /*****************************************************************************/
293 gl_print_merge_uncollated (GnomePrintJob *job,
300 const GList *record_list;
302 const glTemplateLabelType *label_type;
303 gint i_sheet, i_label, n_labels_per_page, i_copy;
304 glMergeRecord *record;
306 glTemplateOrigin *origins;
308 gl_debug (DEBUG_PRINT, "START");
310 merge = gl_label_get_merge (label);
311 record_list = gl_merge_get_record_list (merge);
313 pi = print_info_new (job, label);
314 label_type = gl_template_get_first_label_type (pi->template);
316 n_labels_per_page = gl_template_get_n_labels (label_type);
317 origins = gl_template_get_origins (label_type);
322 for (i_copy = 0; i_copy < n_copies; i_copy++) {
324 for ( p=(GList *)record_list; p!=NULL; p=p->next ) {
325 record = (glMergeRecord *)p->data;
327 if ( record->select_flag ) {
330 if ((i_label == 0) || (i_sheet == 0)) {
332 print_page_begin (pi);
333 if (flags->crop_marks) {
334 print_crop_marks (pi);
338 print_label (pi, label,
342 flags->outline, flags->reverse);
344 i_label = (i_label + 1) % n_labels_per_page;
358 print_info_free (&pi);
360 gl_debug (DEBUG_PRINT, "END");
363 /*****************************************************************************/
364 /* Batch print. Call appropriate function above. */
365 /*****************************************************************************/
367 gl_print_batch (GnomePrintJob *job,
374 glTemplate *template;
375 const glTemplateLabelType *label_type;
378 gl_debug (DEBUG_PRINT, "START");
380 merge = gl_label_get_merge (label);
381 template = gl_label_get_template (label);
382 label_type = gl_template_get_first_label_type (template);
384 if ( merge == NULL ) {
385 n_per_page = gl_template_get_n_labels(label_type);
387 gl_print_simple (job, label, n_sheets, 1, n_per_page, flags);
389 gl_print_merge_collated (job, label, n_copies, 1, flags);
391 gl_template_free (template);
393 gl_debug (DEBUG_PRINT, "END");
396 /*---------------------------------------------------------------------------*/
397 /* PRIVATE. new print info structure */
398 /*---------------------------------------------------------------------------*/
400 print_info_new (GnomePrintJob *job,
403 PrintInfo *pi = g_new0 (PrintInfo, 1);
404 glTemplate *template;
406 gl_debug (DEBUG_PRINT, "START");
408 g_return_val_if_fail (job && GNOME_IS_PRINT_JOB (job), NULL);
409 g_return_val_if_fail (label && GL_IS_LABEL (label), NULL);
411 template = gl_label_get_template (label);
413 g_return_val_if_fail (template, NULL);
414 g_return_val_if_fail (template->page_size, NULL);
415 g_return_val_if_fail (template->page_width > 0, NULL);
416 g_return_val_if_fail (template->page_height > 0, NULL);
418 pi->pc = gnome_print_job_get_context (job);
419 pi->config = gnome_print_job_get_config (job);
421 gl_debug (DEBUG_PRINT,
422 "setting page size = \"%s\"", template->page_size);
424 gnome_print_config_set_length (pi->config,
425 GNOME_PRINT_KEY_PAPER_WIDTH,
426 template->page_width,
427 GNOME_PRINT_PS_UNIT);
428 gnome_print_config_set_length (pi->config,
429 GNOME_PRINT_KEY_PAPER_HEIGHT,
430 template->page_height,
431 GNOME_PRINT_PS_UNIT);
433 pi->page_width = template->page_width;
434 pi->page_height = template->page_height;
436 pi->template = template;
437 pi->label_rotate_flag = gl_label_get_rotate_flag (label);
441 gl_debug (DEBUG_PRINT, "END");
446 /*---------------------------------------------------------------------------*/
447 /* PRIVATE. free print info structure */
448 /*---------------------------------------------------------------------------*/
450 print_info_free (PrintInfo **pi)
452 gl_debug (DEBUG_PRINT, "START");
454 gl_template_free ((*pi)->template);
455 (*pi)->template = NULL;
457 gnome_print_context_close ((*pi)->pc);
462 gl_debug (DEBUG_PRINT, "END");
465 /*---------------------------------------------------------------------------*/
466 /* PRIVATE. Begin a new page. */
467 /*---------------------------------------------------------------------------*/
469 print_page_begin (PrintInfo *pi)
473 gl_debug (DEBUG_PRINT, "START");
477 str = g_strdup_printf ("sheet%02d", pi->sheet);
478 gnome_print_beginpage (pi->pc, str);
481 /* Translate and scale, so that our origin is at the upper left. */
482 gnome_print_translate (pi->pc, 0.0, pi->page_height);
483 gnome_print_scale (pi->pc, 1.0, -1.0);
485 gl_debug (DEBUG_PRINT, "END");
488 /*---------------------------------------------------------------------------*/
489 /* PRIVATE. End a page. */
490 /*---------------------------------------------------------------------------*/
492 print_page_end (PrintInfo *pi)
494 gl_debug (DEBUG_PRINT, "START");
496 gnome_print_showpage (pi->pc);
498 gl_debug (DEBUG_PRINT, "END");
501 /*---------------------------------------------------------------------------*/
502 /* PRIVATE. Print crop tick marks. */
503 /*---------------------------------------------------------------------------*/
505 print_crop_marks (PrintInfo *pi)
507 const glTemplateLabelType *label_type;
508 gdouble w, h, page_w, page_h;
510 glTemplateLayout *layout;
511 gdouble xmin, ymin, xmax, ymax, dx, dy;
512 gdouble x1, y1, x2, y2, x3, y3, x4, y4;
515 gl_debug (DEBUG_PRINT, "START");
517 label_type = gl_template_get_first_label_type (pi->template);
519 gl_template_get_label_size (label_type, &w, &h);
521 page_w = pi->page_width;
522 page_h = pi->page_height;
524 gnome_print_setrgbcolor (pi->pc, 0.25, 0.25, 0.25);
525 gnome_print_setopacity (pi->pc, 1.0);
526 gnome_print_setlinewidth (pi->pc, 0.25);
528 for (p=label_type->layouts; p != NULL; p=p->next) {
530 layout = (glTemplateLayout *)p->data;
534 xmax = layout->x0 + layout->dx*(layout->nx - 1) + w;
535 ymax = layout->y0 + layout->dy*(layout->ny - 1) + h;
543 for (ix=0; ix < nx; ix++) {
548 y1 = MAX((ymin - TICK_OFFSET), 0.0);
549 y2 = MAX((y1 - TICK_LENGTH), 0.0);
551 y3 = MIN((ymax + TICK_OFFSET), page_h);
552 y4 = MIN((y3 + TICK_LENGTH), page_h);
554 gnome_print_moveto (pi->pc, x1, y1);
555 gnome_print_lineto (pi->pc, x1, y2);
556 gnome_print_stroke (pi->pc);
558 gnome_print_moveto (pi->pc, x2, y1);
559 gnome_print_lineto (pi->pc, x2, y2);
560 gnome_print_stroke (pi->pc);
562 gnome_print_moveto (pi->pc, x1, y3);
563 gnome_print_lineto (pi->pc, x1, y4);
564 gnome_print_stroke (pi->pc);
566 gnome_print_moveto (pi->pc, x2, y3);
567 gnome_print_lineto (pi->pc, x2, y4);
568 gnome_print_stroke (pi->pc);
572 for (iy=0; iy < ny; iy++) {
577 x1 = MAX((xmin - TICK_OFFSET), 0.0);
578 x2 = MAX((x1 - TICK_LENGTH), 0.0);
580 x3 = MIN((xmax + TICK_OFFSET), page_w);
581 x4 = MIN((x3 + TICK_LENGTH), page_w);
583 gnome_print_moveto (pi->pc, x1, y1);
584 gnome_print_lineto (pi->pc, x2, y1);
585 gnome_print_stroke (pi->pc);
587 gnome_print_moveto (pi->pc, x1, y2);
588 gnome_print_lineto (pi->pc, x2, y2);
589 gnome_print_stroke (pi->pc);
591 gnome_print_moveto (pi->pc, x3, y1);
592 gnome_print_lineto (pi->pc, x4, y1);
593 gnome_print_stroke (pi->pc);
595 gnome_print_moveto (pi->pc, x3, y2);
596 gnome_print_lineto (pi->pc, x4, y2);
597 gnome_print_stroke (pi->pc);
603 gl_debug (DEBUG_PRINT, "END");
606 /*---------------------------------------------------------------------------*/
607 /* PRIVATE. Print i'th label. */
608 /*---------------------------------------------------------------------------*/
610 print_label (PrintInfo *pi,
614 glMergeRecord *record,
615 gboolean outline_flag,
616 gboolean reverse_flag)
618 const glTemplateLabelType *label_type;
619 gdouble width, height;
621 gl_debug (DEBUG_PRINT, "START");
623 label_type = gl_template_get_first_label_type (pi->template);
625 gl_label_get_size (label, &width, &height);
627 gnome_print_gsave (pi->pc);
629 /* Transform coordinate system to be relative to upper corner */
630 /* of the current label */
631 gnome_print_translate (pi->pc, x, y);
632 if (gl_label_get_rotate_flag (label)) {
633 gl_debug (DEBUG_PRINT, "Rotate flag set");
634 gnome_print_rotate (pi->pc, -90.0);
635 gnome_print_translate (pi->pc, -width, 0.0);
637 if ( reverse_flag ) {
638 gnome_print_translate (pi->pc, width, 0.0);
639 gnome_print_scale (pi->pc, -1.0, 1.0);
642 clip_to_outline (pi, label);
643 draw_label (pi, label, record);
645 draw_outline (pi, label);
647 clip_punchouts (pi, label);
649 gnome_print_grestore (pi->pc);
651 gl_debug (DEBUG_PRINT, "END");
654 /*---------------------------------------------------------------------------*/
655 /* PRIVATE. Draw label. */
656 /*---------------------------------------------------------------------------*/
658 draw_label (PrintInfo *pi,
660 glMergeRecord *record)
663 glLabelObject *object;
665 gl_debug (DEBUG_PRINT, "START");
667 for (p_obj = label->objects; p_obj != NULL; p_obj = p_obj->next) {
668 object = (glLabelObject *) p_obj->data;
670 draw_object (pi, object, record);
673 gl_debug (DEBUG_PRINT, "END");
676 /*---------------------------------------------------------------------------*/
677 /* PRIVATE. Draw object. */
678 /*---------------------------------------------------------------------------*/
680 draw_object (PrintInfo *pi,
681 glLabelObject *object,
682 glMergeRecord *record)
687 gl_debug (DEBUG_PRINT, "START");
689 gl_label_object_get_position (object, &x0, &y0);
690 gl_label_object_get_affine (object, affine);
692 gnome_print_gsave (pi->pc);
694 gnome_print_translate (pi->pc, x0, y0);
695 gnome_print_concat (pi->pc, affine);
697 if (GL_IS_LABEL_TEXT(object)) {
698 draw_text_object (pi, GL_LABEL_TEXT(object), record);
699 } else if (GL_IS_LABEL_BOX(object)) {
700 draw_box_object (pi, GL_LABEL_BOX(object));
701 } else if (GL_IS_LABEL_LINE(object)) {
702 draw_line_object (pi, GL_LABEL_LINE(object));
703 } else if (GL_IS_LABEL_ELLIPSE(object)) {
704 draw_ellipse_object (pi, GL_LABEL_ELLIPSE(object));
705 } else if (GL_IS_LABEL_IMAGE(object)) {
706 draw_image_object (pi, GL_LABEL_IMAGE(object), record);
707 } else if (GL_IS_LABEL_BARCODE(object)) {
708 draw_barcode_object (pi, GL_LABEL_BARCODE(object), record);
711 gnome_print_grestore (pi->pc);
713 gl_debug (DEBUG_PRINT, "END");
716 /*---------------------------------------------------------------------------*/
717 /* PRIVATE. Draw text object. */
718 /*---------------------------------------------------------------------------*/
720 draw_text_object (PrintInfo *pi,
722 glMergeRecord *record)
727 gdouble x_offset, y_offset, w, object_w, object_h;
732 GnomeFontWeight font_weight;
733 gboolean font_italic_flag;
735 GtkJustification just;
736 GnomeGlyphList *glyphlist;
739 gdouble text_line_spacing;
742 gl_debug (DEBUG_PRINT, "START");
744 gl_label_object_get_size (GL_LABEL_OBJECT(object), &object_w, &object_h);
745 lines = gl_label_text_get_lines (object);
746 font_family = gl_label_object_get_font_family (GL_LABEL_OBJECT(object));
747 font_size = gl_label_object_get_font_size (GL_LABEL_OBJECT(object));
748 font_weight = gl_label_object_get_font_weight (GL_LABEL_OBJECT(object));
749 font_italic_flag = gl_label_object_get_font_italic_flag (GL_LABEL_OBJECT(object));
750 color = gl_label_object_get_text_color (GL_LABEL_OBJECT(object));
751 just = gl_label_object_get_text_alignment (GL_LABEL_OBJECT(object));
752 text_line_spacing = gl_label_object_get_text_line_spacing (GL_LABEL_OBJECT(object));
754 font = gnome_font_find_closest_from_weight_slant (
759 gnome_print_setfont (pi->pc, font);
761 gnome_print_setrgbcolor (pi->pc,
762 GL_COLOR_F_RED (color),
763 GL_COLOR_F_GREEN (color),
764 GL_COLOR_F_BLUE (color));
765 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (color));
767 text = gl_text_node_lines_expand (lines, record);
768 line = g_strsplit (text, "\n", -1);
771 art_affine_identity (affine);
773 for (i = 0; line[i] != NULL; i++) {
775 glyphlist = gnome_glyphlist_from_text_dumb (font, color,
779 gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox);
783 case GTK_JUSTIFY_LEFT:
784 x_offset = GL_LABEL_TEXT_MARGIN;
786 case GTK_JUSTIFY_CENTER:
787 x_offset = (object_w - GL_LABEL_TEXT_MARGIN - w) / 2.0;
789 case GTK_JUSTIFY_RIGHT:
790 x_offset = object_w - GL_LABEL_TEXT_MARGIN - w;
794 break; /* shouldn't happen */
797 /* Work out the y position to the BOTTOM of the first line */
798 y_offset = GL_LABEL_TEXT_MARGIN +
799 + gnome_font_get_descender (font)
800 + (i + 1) * font_size * text_line_spacing;
802 /* Remove any text line spacing from the first row. */
803 y_offset -= font_size * (text_line_spacing - 1);
806 gnome_print_moveto (pi->pc, x_offset, y_offset);
808 gnome_print_gsave (pi->pc);
809 gnome_print_scale (pi->pc, 1.0, -1.0);
810 gnome_print_show (pi->pc, line[i]);
811 gnome_print_grestore (pi->pc);
816 gl_text_node_lines_free (&lines);
817 g_free (font_family);
819 gl_debug (DEBUG_PRINT, "END");
822 /*---------------------------------------------------------------------------*/
823 /* PRIVATE. Draw box object. */
824 /*---------------------------------------------------------------------------*/
826 draw_box_object (PrintInfo *pi,
831 guint line_color, fill_color;
833 gl_debug (DEBUG_PRINT, "START");
835 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
836 line_width = gl_label_object_get_line_width (GL_LABEL_OBJECT(object));
837 line_color = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
838 fill_color = gl_label_object_get_fill_color (GL_LABEL_OBJECT(object));
840 /* Paint fill color */
841 create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
842 gnome_print_setrgbcolor (pi->pc,
843 GL_COLOR_F_RED (fill_color),
844 GL_COLOR_F_GREEN (fill_color),
845 GL_COLOR_F_BLUE (fill_color));
846 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (fill_color));
847 gnome_print_fill (pi->pc);
850 create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
851 gnome_print_setrgbcolor (pi->pc,
852 GL_COLOR_F_RED (line_color),
853 GL_COLOR_F_GREEN (line_color),
854 GL_COLOR_F_BLUE (line_color));
855 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (line_color));
856 gnome_print_setlinewidth (pi->pc, line_width);
857 gnome_print_stroke (pi->pc);
859 gl_debug (DEBUG_PRINT, "END");
862 /*---------------------------------------------------------------------------*/
863 /* PRIVATE. Draw line object. */
864 /*---------------------------------------------------------------------------*/
866 draw_line_object (PrintInfo *pi,
873 gl_debug (DEBUG_PRINT, "START");
875 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
876 line_width = gl_label_object_get_line_width (GL_LABEL_OBJECT(object));
877 line_color = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
879 gnome_print_moveto (pi->pc, 0.0, 0.0);
880 gnome_print_lineto (pi->pc, w, h);
881 gnome_print_setrgbcolor (pi->pc,
882 GL_COLOR_F_RED (line_color),
883 GL_COLOR_F_GREEN (line_color),
884 GL_COLOR_F_BLUE (line_color));
885 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (line_color));
886 gnome_print_setlinewidth (pi->pc, line_width);
887 gnome_print_stroke (pi->pc);
889 gl_debug (DEBUG_PRINT, "END");
892 /*---------------------------------------------------------------------------*/
893 /* PRIVATE. Draw ellipse object. */
894 /*---------------------------------------------------------------------------*/
896 draw_ellipse_object (PrintInfo *pi,
897 glLabelEllipse *object)
899 gdouble x0, y0, rx, ry, w, h;
901 guint line_color, fill_color;
903 gl_debug (DEBUG_PRINT, "START");
905 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
906 line_width = gl_label_object_get_line_width (GL_LABEL_OBJECT(object));
907 line_color = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
908 fill_color = gl_label_object_get_fill_color (GL_LABEL_OBJECT(object));
915 /* Paint fill color */
916 create_ellipse_path (pi->pc, x0, y0, rx, ry);
917 gnome_print_setrgbcolor (pi->pc,
918 GL_COLOR_F_RED (fill_color),
919 GL_COLOR_F_GREEN (fill_color),
920 GL_COLOR_F_BLUE (fill_color));
921 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (fill_color));
922 gnome_print_fill (pi->pc);
925 create_ellipse_path (pi->pc, x0, y0, rx, ry);
926 gnome_print_setrgbcolor (pi->pc,
927 GL_COLOR_F_RED (line_color),
928 GL_COLOR_F_GREEN (line_color),
929 GL_COLOR_F_BLUE (line_color));
930 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (line_color));
931 gnome_print_setlinewidth (pi->pc, line_width);
932 gnome_print_stroke (pi->pc);
934 gl_debug (DEBUG_PRINT, "END");
937 /*---------------------------------------------------------------------------*/
938 /* PRIVATE. Draw image object. */
939 /*---------------------------------------------------------------------------*/
941 draw_image_object (PrintInfo *pi,
942 glLabelImage *object,
943 glMergeRecord *record)
946 const GdkPixbuf *pixbuf;
948 gint image_w, image_h, image_stride;
949 gboolean image_alpha_flag;
952 gl_debug (DEBUG_PRINT, "START");
954 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
956 pixbuf = gl_label_image_get_pixbuf (object, record);
957 image_data = gdk_pixbuf_get_pixels (pixbuf);
958 image_w = gdk_pixbuf_get_width (pixbuf);
959 image_h = gdk_pixbuf_get_height (pixbuf);
960 image_stride = gdk_pixbuf_get_rowstride(pixbuf);
961 image_alpha_flag = gdk_pixbuf_get_has_alpha(pixbuf);
963 gnome_print_gsave (pi->pc);
964 gnome_print_translate (pi->pc, 0.0, h);
965 gnome_print_scale (pi->pc, w, -h);
966 if (image_alpha_flag) {
967 #ifndef NO_ALPHA_HACK
970 image_data2 = get_pixels_as_rgb (pixbuf);
971 ret = gnome_print_rgbimage (pi->pc, image_data2,
972 image_w, image_h, image_stride);
973 g_free (image_data2);
975 ret = gnome_print_rgbaimage (pi->pc, image_data,
976 image_w, image_h, image_stride);
979 ret = gnome_print_rgbimage (pi->pc, image_data,
980 image_w, image_h, image_stride);
982 gnome_print_grestore (pi->pc);
984 gl_debug (DEBUG_PRINT, "END");
987 /*---------------------------------------------------------------------------*/
988 /* PRIVATE. Draw box object. */
989 /*---------------------------------------------------------------------------*/
991 draw_barcode_object (PrintInfo *pi,
992 glLabelBarcode *object,
993 glMergeRecord *record)
997 glBarcodeChar *bchar;
1001 gchar *text, *cstring;
1002 glTextNode *text_node;
1005 gboolean checksum_flag;
1009 gl_debug (DEBUG_PRINT, "START");
1011 text_node = gl_label_barcode_get_data (object);
1012 gl_label_barcode_get_props (object,
1013 &id, &text_flag, &checksum_flag);
1014 color = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
1015 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
1017 text = gl_text_node_expand (text_node, record);
1018 gbc = gl_barcode_new (id, text_flag, checksum_flag, w, h, text);
1020 gl_text_node_free (&text_node);
1025 font = gnome_font_find_closest_from_weight_slant (
1026 GL_BARCODE_FONT_FAMILY,
1027 GL_BARCODE_FONT_WEIGHT,
1029 gnome_print_setfont (pi->pc, font);
1031 gnome_print_setrgbcolor (pi->pc,
1032 GL_COLOR_F_RED (color),
1033 GL_COLOR_F_GREEN (color),
1034 GL_COLOR_F_BLUE (color));
1035 gnome_print_setopacity (pi->pc,
1036 GL_COLOR_F_ALPHA (color));
1038 y_offset = 12.0 - fabs (gnome_font_get_descender (font));
1039 gnome_print_moveto (pi->pc, 0.0, y_offset);
1041 gnome_print_gsave (pi->pc);
1042 gnome_print_scale (pi->pc, 1.0, -1.0);
1043 gnome_print_show (pi->pc, _("Invalid barcode data"));
1044 gnome_print_grestore (pi->pc);
1048 for (li = gbc->lines; li != NULL; li = li->next) {
1049 line = (glBarcodeLine *) li->data;
1051 gnome_print_moveto (pi->pc, line->x, line->y);
1052 gnome_print_lineto (pi->pc, line->x, line->y + line->length);
1053 gnome_print_setrgbcolor (pi->pc,
1054 GL_COLOR_F_RED (color),
1055 GL_COLOR_F_GREEN (color),
1056 GL_COLOR_F_BLUE (color));
1057 gnome_print_setopacity (pi->pc,
1058 GL_COLOR_F_ALPHA (color));
1059 gnome_print_setlinewidth (pi->pc, line->width);
1060 gnome_print_stroke (pi->pc);
1063 for (li = gbc->chars; li != NULL; li = li->next) {
1064 bchar = (glBarcodeChar *) li->data;
1066 font = gnome_font_find_closest_from_weight_slant (
1067 GL_BARCODE_FONT_FAMILY,
1068 GL_BARCODE_FONT_WEIGHT,
1069 FALSE, bchar->fsize);
1070 gnome_print_setfont (pi->pc, font);
1072 gnome_print_setrgbcolor (pi->pc,
1073 GL_COLOR_F_RED (color),
1074 GL_COLOR_F_GREEN (color),
1075 GL_COLOR_F_BLUE (color));
1076 gnome_print_setopacity (pi->pc,
1077 GL_COLOR_F_ALPHA (color));
1080 bchar->fsize - fabs (gnome_font_get_descender (font));
1082 gnome_print_moveto (pi->pc, bchar->x, bchar->y+y_offset);
1084 cstring = g_strdup_printf ("%c", bchar->c);
1085 gnome_print_gsave (pi->pc);
1086 gnome_print_scale (pi->pc, 1.0, -1.0);
1087 gnome_print_show (pi->pc, cstring);
1088 gnome_print_grestore (pi->pc);
1093 gl_barcode_free (&gbc);
1097 gl_debug (DEBUG_PRINT, "END");
1100 /*---------------------------------------------------------------------------*/
1101 /* PRIVATE. Draw outline. */
1102 /*---------------------------------------------------------------------------*/
1104 draw_outline (PrintInfo *pi,
1107 const glTemplateLabelType *label_type;
1111 gl_debug (DEBUG_PRINT, "START");
1113 label_type = gl_template_get_first_label_type (pi->template);
1115 gnome_print_setrgbcolor (pi->pc, 0.25, 0.25, 0.25);
1116 gnome_print_setopacity (pi->pc, 1.0);
1117 gnome_print_setlinewidth (pi->pc, 0.25);
1119 switch (label_type->shape) {
1121 case GL_TEMPLATE_SHAPE_RECT:
1122 gl_label_get_size (label, &w, &h);
1123 r = label_type->size.rect.r;
1125 /* simple rectangle */
1126 create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
1128 /* rectangle with rounded corners */
1129 create_rounded_rectangle_path (pi->pc, 0.0, 0.0,
1132 gnome_print_stroke (pi->pc);
1135 case GL_TEMPLATE_SHAPE_ROUND:
1137 r1 = label_type->size.round.r;
1138 create_ellipse_path (pi->pc, r1, r1, r1, r1);
1139 gnome_print_stroke (pi->pc);
1142 case GL_TEMPLATE_SHAPE_CD:
1143 if ((label_type->size.cd.h == 0) && (label_type->size.cd.w == 0)) {
1144 /* CD style, round label w/ concentric round hole */
1145 r1 = label_type->size.cd.r1;
1146 r2 = label_type->size.cd.r2;
1147 create_ellipse_path (pi->pc, r1, r1, r1, r1);
1148 gnome_print_stroke (pi->pc);
1149 create_ellipse_path (pi->pc, r1, r1, r2, r2);
1150 gnome_print_stroke (pi->pc);
1152 /* Business Card CD style, clipped round label w/ hole */
1153 gl_label_get_size (label, &w, &h);
1154 r1 = label_type->size.cd.r1;
1155 r2 = label_type->size.cd.r2;
1156 create_clipped_circle_path (pi->pc, w/2, h/2, w, h, r1);
1157 gnome_print_stroke (pi->pc);
1158 create_ellipse_path (pi->pc, w/2, h/2, r2, r2);
1159 gnome_print_stroke (pi->pc);
1164 g_warning ("Unknown template label style");
1168 gl_debug (DEBUG_PRINT, "END");
1171 /*---------------------------------------------------------------------------*/
1172 /* PRIVATE. Clip to outline. */
1173 /*---------------------------------------------------------------------------*/
1175 clip_to_outline (PrintInfo *pi,
1178 const glTemplateLabelType *label_type;
1183 gl_debug (DEBUG_PRINT, "START");
1185 label_type = gl_template_get_first_label_type (pi->template);
1187 switch (label_type->shape) {
1189 case GL_TEMPLATE_SHAPE_RECT:
1190 gl_label_get_size (label, &w, &h);
1191 r = label_type->size.rect.r;
1192 waste = label_type->waste;
1194 /* simple rectangle */
1195 create_rectangle_path (pi->pc,
1196 -waste, -waste, w+2*waste, h+2*waste);
1198 /* rectangle with rounded corners */
1199 create_rounded_rectangle_path (pi->pc, -waste, -waste,
1200 w+2*waste, h+2*waste, r);
1202 gnome_print_clip (pi->pc);
1205 case GL_TEMPLATE_SHAPE_ROUND:
1206 r1 = label_type->size.round.r;
1207 waste = label_type->waste;
1208 create_ellipse_path (pi->pc, r1, r1, r1+waste, r1+waste);
1209 gnome_print_clip (pi->pc);
1212 case GL_TEMPLATE_SHAPE_CD:
1213 waste = label_type->waste;
1214 if ((label_type->size.cd.h == 0) && (label_type->size.cd.w == 0)) {
1215 /* CD style, round label w/ concentric round hole */
1216 r1 = label_type->size.cd.r1;
1217 create_ellipse_path (pi->pc, r1, r1, r1+waste, r1+waste);
1219 /* Business Card CD style, clipped round label w/ hole */
1220 gl_label_get_size (label, &w, &h);
1221 r1 = label_type->size.cd.r1;
1222 create_clipped_circle_path (pi->pc,
1224 w+2*waste, h+2*waste,
1227 gnome_print_clip (pi->pc);
1231 g_warning ("Unknown template label style");
1235 gl_debug (DEBUG_PRINT, "END");
1238 /*---------------------------------------------------------------------------*/
1239 /* PRIVATE. Clip punchouts. (Save some ink by not printing in CD holes) */
1241 /* Ideally this would be done in clip_to_outline, but I am not sure how to */
1242 /* invert the region for gnome_print_clip, so instead, I will just draw */
1243 /* a white circle on top of everything else. */
1244 /*---------------------------------------------------------------------------*/
1246 clip_punchouts (PrintInfo *pi,
1249 const glTemplateLabelType *label_type;
1253 gl_debug (DEBUG_PRINT, "START");
1255 label_type = gl_template_get_first_label_type (pi->template);
1257 switch (label_type->shape) {
1259 case GL_TEMPLATE_SHAPE_RECT:
1260 case GL_TEMPLATE_SHAPE_ROUND:
1263 case GL_TEMPLATE_SHAPE_CD:
1264 gl_label_get_size (label, &w, &h);
1265 waste = label_type->waste;
1266 r2 = label_type->size.cd.r2;
1267 create_ellipse_path (pi->pc, w/2, h/2, r2-waste, r2-waste);
1268 gnome_print_setrgbcolor (pi->pc, 1.0, 1.0, 1.0);
1269 gnome_print_setopacity (pi->pc, 1.0);
1270 gnome_print_fill (pi->pc);
1274 g_warning ("Unknown template label style");
1278 gl_debug (DEBUG_PRINT, "END");
1281 /*---------------------------------------------------------------------------*/
1282 /* PRIVATE. Path creation utilities. */
1283 /*---------------------------------------------------------------------------*/
1285 create_rectangle_path (GnomePrintContext *pc,
1291 gl_debug (DEBUG_PRINT, "START");
1293 gnome_print_newpath (pc);
1294 gnome_print_moveto (pc, x0, y0);
1295 gnome_print_lineto (pc, x0 + w, y0);
1296 gnome_print_lineto (pc, x0 + w, y0 + h);
1297 gnome_print_lineto (pc, x0, y0 + h);
1298 gnome_print_lineto (pc, x0, y0);
1299 gnome_print_closepath (pc);
1301 gl_debug (DEBUG_PRINT, "END");
1305 create_ellipse_path (GnomePrintContext *pc,
1314 gl_debug (DEBUG_PRINT, "START");
1316 gnome_print_newpath (pc);
1317 gnome_print_moveto (pc, x0 + rx, y0);
1318 for (i_theta = ARC_FINE; i_theta <= 360; i_theta += ARC_FINE) {
1319 x = x0 + rx * cos (i_theta * G_PI / 180.0);
1320 y = y0 + ry * sin (i_theta * G_PI / 180.0);
1321 gnome_print_lineto (pc, x, y);
1323 gnome_print_closepath (pc);
1325 gl_debug (DEBUG_PRINT, "END");
1329 create_rounded_rectangle_path (GnomePrintContext *pc,
1339 gl_debug (DEBUG_PRINT, "START");
1341 gnome_print_newpath (pc);
1343 gnome_print_moveto (pc, x0 + r, y0);
1344 for (i_theta = ARC_COURSE; i_theta <= 90; i_theta += ARC_COURSE) {
1345 x = x0 + r - r * sin (i_theta * G_PI / 180.0);
1346 y = y0 + r - r * cos (i_theta * G_PI / 180.0);
1347 gnome_print_lineto (pc, x, y);
1349 for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1350 x = x0 + r - r * cos (i_theta * G_PI / 180.0);
1351 y = y0 + (h - r) + r * sin (i_theta * G_PI / 180.0);
1352 gnome_print_lineto (pc, x, y);
1354 for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1355 x = x0 + (w - r) + r * sin (i_theta * G_PI / 180.0);
1356 y = y0 + (h - r) + r * cos (i_theta * G_PI / 180.0);
1357 gnome_print_lineto (pc, x, y);
1359 for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1360 x = x0 + (w - r) + r * cos (i_theta * G_PI / 180.0);
1361 y = y0 + r - r * sin (i_theta * G_PI / 180.0);
1362 gnome_print_lineto (pc, x, y);
1364 gnome_print_lineto (pc, x0 + r, y0);
1366 gnome_print_closepath (pc);
1368 gl_debug (DEBUG_PRINT, "END");
1372 create_clipped_circle_path (GnomePrintContext *pc,
1380 gdouble theta1, theta2;
1383 gl_debug (DEBUG_PRINT, "START");
1385 theta1 = (180.0/G_PI) * acos (w / (2.0*r));
1386 theta2 = (180.0/G_PI) * asin (h / (2.0*r));
1388 gnome_print_newpath (pc);
1390 x = x0 + r * cos (theta1 * G_PI / 180.0);
1391 y = y0 + r * sin (theta1 * G_PI / 180.0);
1392 gnome_print_moveto (pc, x, y);
1394 for ( i_theta = theta1 + ARC_FINE; i_theta < theta2; i_theta +=ARC_FINE ) {
1395 x = x0 + r * cos (i_theta * G_PI / 180.0);
1396 y = y0 + r * sin (i_theta * G_PI / 180.0);
1397 gnome_print_lineto (pc, x, y);
1400 x = x0 + r * cos (theta2 * G_PI / 180.0);
1401 y = y0 + r * sin (theta2 * G_PI / 180.0);
1402 gnome_print_lineto (pc, x, y);
1404 if ( fabs (theta2 - 90.0) > GNOME_CANVAS_EPSILON ) {
1405 x = x0 + r * cos ((180-theta2) * G_PI / 180.0);
1406 y = y0 + r * sin ((180-theta2) * G_PI / 180.0);
1407 gnome_print_lineto (pc, x, y);
1410 for ( i_theta = 180-theta2+ARC_FINE; i_theta < (180-theta1); i_theta +=ARC_FINE ) {
1411 x = x0 + r * cos (i_theta * G_PI / 180.0);
1412 y = y0 + r * sin (i_theta * G_PI / 180.0);
1413 gnome_print_lineto (pc, x, y);
1416 x = x0 + r * cos ((180-theta1) * G_PI / 180.0);
1417 y = y0 + r * sin ((180-theta1) * G_PI / 180.0);
1418 gnome_print_lineto (pc, x, y);
1420 if ( fabs (theta1) > GNOME_CANVAS_EPSILON ) {
1421 x = x0 + r * cos ((180+theta1) * G_PI / 180.0);
1422 y = y0 + r * sin ((180+theta1) * G_PI / 180.0);
1423 gnome_print_lineto (pc, x, y);
1426 for ( i_theta = 180+theta1+ARC_FINE; i_theta < (180+theta2); i_theta +=ARC_FINE ) {
1427 x = x0 + r * cos (i_theta * G_PI / 180.0);
1428 y = y0 + r * sin (i_theta * G_PI / 180.0);
1429 gnome_print_lineto (pc, x, y);
1432 x = x0 + r * cos ((180+theta2) * G_PI / 180.0);
1433 y = y0 + r * sin ((180+theta2) * G_PI / 180.0);
1434 gnome_print_lineto (pc, x, y);
1436 if ( fabs (theta2 - 90.0) > GNOME_CANVAS_EPSILON ) {
1437 x = x0 + r * cos ((360-theta2) * G_PI / 180.0);
1438 y = y0 + r * sin ((360-theta2) * G_PI / 180.0);
1439 gnome_print_lineto (pc, x, y);
1442 for ( i_theta = 360-theta2+ARC_FINE; i_theta < (360-theta1); i_theta +=ARC_FINE ) {
1443 x = x0 + r * cos (i_theta * G_PI / 180.0);
1444 y = y0 + r * sin (i_theta * G_PI / 180.0);
1445 gnome_print_lineto (pc, x, y);
1448 if ( fabs (theta1) > GNOME_CANVAS_EPSILON ) {
1449 x = x0 + r * cos ((360-theta1) * G_PI / 180.0);
1450 y = y0 + r * sin ((360-theta1) * G_PI / 180.0);
1451 gnome_print_lineto (pc, x, y);
1454 x = x0 + r * cos (theta1 * G_PI / 180.0);
1455 y = y0 + r * sin (theta1 * G_PI / 180.0);
1456 gnome_print_lineto (pc, x, y);
1458 gnome_print_closepath (pc);
1460 gl_debug (DEBUG_PRINT, "END");
1463 #ifndef NO_ALPHA_HACK
1464 /*---------------------------------------------------------------------------*/
1465 /* PRIVATE. Extract a copy of rgba pixels, removing alpha by compositing */
1466 /* with a white background. */
1468 /* This is currently needed due to the lousy job gnome-print does in */
1469 /* rendering images with alpha channels to PS. This sacrafices the ability */
1470 /* to do compositing of images with other items in the background. */
1471 /*---------------------------------------------------------------------------*/
1473 get_pixels_as_rgb (const GdkPixbuf *pixbuf)
1475 gint bits_per_sample, channels;
1477 gint width, height, rowstride;
1479 guchar *buf_src, *buf_dest;
1480 guchar *p_src, *p_dest;
1483 gdouble alpha, beta;
1485 gl_debug (DEBUG_PRINT, "START");
1487 g_return_val_if_fail (pixbuf && GDK_IS_PIXBUF (pixbuf), NULL);
1489 /* extract pixels and parameters from pixbuf. */
1490 buf_src = gdk_pixbuf_get_pixels (pixbuf);
1491 bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);
1492 channels = gdk_pixbuf_get_n_channels (pixbuf);
1493 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
1494 width = gdk_pixbuf_get_width (pixbuf);
1495 height = gdk_pixbuf_get_height (pixbuf);
1496 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1498 /* validate assumptions about pixbuf. */
1499 g_return_val_if_fail (buf_src, NULL);
1500 g_return_val_if_fail (bits_per_sample == 8, NULL);
1501 g_return_val_if_fail (channels == 4, NULL);
1502 g_return_val_if_fail (has_alpha, NULL);
1503 g_return_val_if_fail (width > 0, NULL);
1504 g_return_val_if_fail (height > 0, NULL);
1505 g_return_val_if_fail (rowstride > 0, NULL);
1507 /* Allocate a destination buffer */
1508 bytes = height * rowstride;
1509 gl_debug (DEBUG_PRINT, "bytes = %d", bytes);
1510 buf_dest = g_try_malloc (bytes);
1514 gl_debug (DEBUG_PRINT, "buf_dest = %x", buf_dest);
1516 /* Copy pixels, transforming rgba to rgb by compositing with a white bg. */
1519 for ( iy=0; iy < height; iy++ ) {
1521 p_src = buf_src + iy*rowstride;
1522 p_dest = buf_dest + iy*rowstride;
1524 for ( ix=0; ix < width; ix++ ) {
1534 *p_dest++ = (guchar) (alpha*r + beta*255 + 0.5);
1535 *p_dest++ = (guchar) (alpha*g + beta*255 + 0.5);
1536 *p_dest++ = (guchar) (alpha*b + beta*255 + 0.5);
1542 gl_debug (DEBUG_PRINT, "START");