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"
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 gint i_sheet, i_label;
182 glTemplateOrigin *origins;
184 gl_debug (DEBUG_PRINT, "START");
186 pi = print_info_new (job, label);
188 origins = gl_template_get_origins (pi->template);
190 for (i_sheet = 0; i_sheet < n_sheets; i_sheet++) {
192 print_page_begin (pi);
193 if (flags->crop_marks) {
194 print_crop_marks (pi);
197 for (i_label = first - 1; i_label < last; i_label++) {
199 print_label (pi, label,
200 origins[i_label].x, origins[i_label].y,
201 NULL, flags->outline, flags->reverse);
210 print_info_free (&pi);
212 gl_debug (DEBUG_PRINT, "END");
215 /*****************************************************************************/
216 /* Merge print command (collated copies) */
217 /*****************************************************************************/
219 gl_print_merge_collated (GnomePrintJob *job,
226 const GList *record_list;
228 gint i_sheet, i_label, n_labels_per_page, i_copy;
229 glMergeRecord *record;
231 glTemplateOrigin *origins;
233 gl_debug (DEBUG_PRINT, "START");
235 merge = gl_label_get_merge (label);
236 record_list = gl_merge_get_record_list (merge);
238 pi = print_info_new (job, label);
240 n_labels_per_page = gl_template_get_n_labels (pi->template);
241 origins = gl_template_get_origins (pi->template);
246 for ( p=(GList *)record_list; p!=NULL; p=p->next ) {
247 record = (glMergeRecord *)p->data;
249 if ( record->select_flag ) {
250 for (i_copy = 0; i_copy < n_copies; i_copy++) {
252 if ((i_label == 0) || (i_sheet == 0)) {
254 print_page_begin (pi);
255 if (flags->crop_marks) {
256 print_crop_marks (pi);
260 print_label (pi, label,
264 flags->outline, flags->reverse);
266 i_label = (i_label + 1) % n_labels_per_page;
280 print_info_free (&pi);
282 gl_debug (DEBUG_PRINT, "END");
285 /*****************************************************************************/
286 /* Merge print command (uncollated copies) */
287 /*****************************************************************************/
289 gl_print_merge_uncollated (GnomePrintJob *job,
296 const GList *record_list;
298 gint i_sheet, i_label, n_labels_per_page, i_copy;
299 glMergeRecord *record;
301 glTemplateOrigin *origins;
303 gl_debug (DEBUG_PRINT, "START");
305 merge = gl_label_get_merge (label);
306 record_list = gl_merge_get_record_list (merge);
308 pi = print_info_new (job, label);
310 n_labels_per_page = gl_template_get_n_labels (pi->template);
311 origins = gl_template_get_origins (pi->template);
316 for (i_copy = 0; i_copy < n_copies; i_copy++) {
318 for ( p=(GList *)record_list; p!=NULL; p=p->next ) {
319 record = (glMergeRecord *)p->data;
321 if ( record->select_flag ) {
324 if ((i_label == 0) || (i_sheet == 0)) {
326 print_page_begin (pi);
327 if (flags->crop_marks) {
328 print_crop_marks (pi);
332 print_label (pi, label,
336 flags->outline, flags->reverse);
338 i_label = (i_label + 1) % n_labels_per_page;
352 print_info_free (&pi);
354 gl_debug (DEBUG_PRINT, "END");
357 /*****************************************************************************/
358 /* Batch print. Call appropriate function above. */
359 /*****************************************************************************/
361 gl_print_batch (GnomePrintJob *job,
369 glTemplate *template;
371 gl_debug (DEBUG_PRINT, "START");
373 merge = gl_label_get_merge (label);
374 template = gl_label_get_template (label);
376 if ( merge == NULL ) {
377 n_per_page = gl_template_get_n_labels(template);
379 gl_print_simple (job, label, n_sheets, 1, n_per_page, flags);
381 gl_print_merge_collated (job, label, n_copies, 1, flags);
383 gl_template_free (&template);
385 g_object_unref (G_OBJECT(merge));
387 gl_debug (DEBUG_PRINT, "END");
390 /*---------------------------------------------------------------------------*/
391 /* PRIVATE. new print info structure */
392 /*---------------------------------------------------------------------------*/
394 print_info_new (GnomePrintJob *job,
397 PrintInfo *pi = g_new0 (PrintInfo, 1);
398 glTemplate *template;
400 gl_debug (DEBUG_PRINT, "START");
402 g_return_val_if_fail (job && GNOME_IS_PRINT_JOB (job), NULL);
403 g_return_val_if_fail (label && GL_IS_LABEL (label), NULL);
405 template = gl_label_get_template (label);
407 g_return_val_if_fail (template, NULL);
408 g_return_val_if_fail (template->page_size, NULL);
409 g_return_val_if_fail (template->page_width > 0, NULL);
410 g_return_val_if_fail (template->page_height > 0, NULL);
412 pi->pc = gnome_print_job_get_context (job);
413 pi->config = gnome_print_job_get_config (job);
415 gl_debug (DEBUG_PRINT,
416 "setting page size = \"%s\"", template->page_size);
418 gnome_print_config_set_length (pi->config,
419 GNOME_PRINT_KEY_PAPER_WIDTH,
420 template->page_width,
421 GNOME_PRINT_PS_UNIT);
422 gnome_print_config_set_length (pi->config,
423 GNOME_PRINT_KEY_PAPER_HEIGHT,
424 template->page_height,
425 GNOME_PRINT_PS_UNIT);
427 pi->page_width = template->page_width;
428 pi->page_height = template->page_height;
430 pi->template = template;
431 pi->label_rotate_flag = gl_label_get_rotate_flag (label);
435 gl_debug (DEBUG_PRINT, "END");
440 /*---------------------------------------------------------------------------*/
441 /* PRIVATE. free print info structure */
442 /*---------------------------------------------------------------------------*/
444 print_info_free (PrintInfo **pi)
446 gl_debug (DEBUG_PRINT, "START");
448 gl_template_free (&(*pi)->template);
450 gnome_print_context_close ((*pi)->pc);
455 gl_debug (DEBUG_PRINT, "END");
458 /*---------------------------------------------------------------------------*/
459 /* PRIVATE. Begin a new page. */
460 /*---------------------------------------------------------------------------*/
462 print_page_begin (PrintInfo *pi)
466 gl_debug (DEBUG_PRINT, "START");
470 str = g_strdup_printf ("sheet%02d", pi->sheet);
471 gnome_print_beginpage (pi->pc, str);
474 /* Translate and scale, so that our origin is at the upper left. */
475 gnome_print_translate (pi->pc, 0.0, pi->page_height);
476 gnome_print_scale (pi->pc, 1.0, -1.0);
478 gl_debug (DEBUG_PRINT, "END");
481 /*---------------------------------------------------------------------------*/
482 /* PRIVATE. End a page. */
483 /*---------------------------------------------------------------------------*/
485 print_page_end (PrintInfo *pi)
487 gl_debug (DEBUG_PRINT, "START");
489 gnome_print_showpage (pi->pc);
491 gl_debug (DEBUG_PRINT, "END");
494 /*---------------------------------------------------------------------------*/
495 /* PRIVATE. Print crop tick marks. */
496 /*---------------------------------------------------------------------------*/
498 print_crop_marks (PrintInfo *pi)
500 gdouble w, h, page_w, page_h;
502 glTemplateLayout *layout;
503 gdouble xmin, ymin, xmax, ymax, dx, dy;
504 gdouble x1, y1, x2, y2, x3, y3, x4, y4;
507 gl_debug (DEBUG_PRINT, "START");
509 gl_template_get_label_size (pi->template, &w, &h);
511 page_w = pi->page_width;
512 page_h = pi->page_height;
514 gnome_print_setrgbcolor (pi->pc, 0.25, 0.25, 0.25);
515 gnome_print_setopacity (pi->pc, 1.0);
516 gnome_print_setlinewidth (pi->pc, 0.25);
518 for (p=pi->template->label.any.layouts; p != NULL; p=p->next) {
520 layout = (glTemplateLayout *)p->data;
524 xmax = layout->x0 + layout->dx*(layout->nx - 1) + w;
525 ymax = layout->y0 + layout->dy*(layout->ny - 1) + h;
533 for (ix=0; ix < nx; ix++) {
538 y1 = MAX((ymin - TICK_OFFSET), 0.0);
539 y2 = MAX((y1 - TICK_LENGTH), 0.0);
541 y3 = MIN((ymax + TICK_OFFSET), page_h);
542 y4 = MIN((y3 + TICK_LENGTH), page_h);
544 gnome_print_moveto (pi->pc, x1, y1);
545 gnome_print_lineto (pi->pc, x1, y2);
546 gnome_print_stroke (pi->pc);
548 gnome_print_moveto (pi->pc, x2, y1);
549 gnome_print_lineto (pi->pc, x2, y2);
550 gnome_print_stroke (pi->pc);
552 gnome_print_moveto (pi->pc, x1, y3);
553 gnome_print_lineto (pi->pc, x1, y4);
554 gnome_print_stroke (pi->pc);
556 gnome_print_moveto (pi->pc, x2, y3);
557 gnome_print_lineto (pi->pc, x2, y4);
558 gnome_print_stroke (pi->pc);
562 for (iy=0; iy < ny; iy++) {
567 x1 = MAX((xmin - TICK_OFFSET), 0.0);
568 x2 = MAX((x1 - TICK_LENGTH), 0.0);
570 x3 = MIN((xmax + TICK_OFFSET), page_w);
571 x4 = MIN((x3 + TICK_LENGTH), page_w);
573 gnome_print_moveto (pi->pc, x1, y1);
574 gnome_print_lineto (pi->pc, x2, y1);
575 gnome_print_stroke (pi->pc);
577 gnome_print_moveto (pi->pc, x1, y2);
578 gnome_print_lineto (pi->pc, x2, y2);
579 gnome_print_stroke (pi->pc);
581 gnome_print_moveto (pi->pc, x3, y1);
582 gnome_print_lineto (pi->pc, x4, y1);
583 gnome_print_stroke (pi->pc);
585 gnome_print_moveto (pi->pc, x3, y2);
586 gnome_print_lineto (pi->pc, x4, y2);
587 gnome_print_stroke (pi->pc);
593 gl_debug (DEBUG_PRINT, "END");
596 /*---------------------------------------------------------------------------*/
597 /* PRIVATE. Print i'th label. */
598 /*---------------------------------------------------------------------------*/
600 print_label (PrintInfo *pi,
604 glMergeRecord *record,
605 gboolean outline_flag,
606 gboolean reverse_flag)
608 gdouble width, height;
609 glTemplate *template;
611 gl_debug (DEBUG_PRINT, "START");
613 template = gl_label_get_template (label);
615 gl_label_get_size (label, &width, &height);
617 gnome_print_gsave (pi->pc);
619 /* Transform coordinate system to be relative to upper corner */
620 /* of the current label */
621 gnome_print_translate (pi->pc, x, y);
622 if (gl_label_get_rotate_flag (label)) {
623 gl_debug (DEBUG_PRINT, "Rotate flag set");
624 gnome_print_rotate (pi->pc, -90.0);
625 gnome_print_translate (pi->pc, -width, 0.0);
627 if ( reverse_flag ) {
628 gnome_print_translate (pi->pc, width, 0.0);
629 gnome_print_scale (pi->pc, -1.0, 1.0);
632 clip_to_outline (pi, label);
633 draw_label (pi, label, record);
635 draw_outline (pi, label);
637 clip_punchouts (pi, label);
639 gnome_print_grestore (pi->pc);
641 gl_template_free (&template);
643 gl_debug (DEBUG_PRINT, "END");
646 /*---------------------------------------------------------------------------*/
647 /* PRIVATE. Draw label. */
648 /*---------------------------------------------------------------------------*/
650 draw_label (PrintInfo *pi,
652 glMergeRecord *record)
655 glLabelObject *object;
657 gl_debug (DEBUG_PRINT, "START");
659 for (p_obj = label->objects; p_obj != NULL; p_obj = p_obj->next) {
660 object = (glLabelObject *) p_obj->data;
662 draw_object (pi, object, record);
665 gl_debug (DEBUG_PRINT, "END");
668 /*---------------------------------------------------------------------------*/
669 /* PRIVATE. Draw object. */
670 /*---------------------------------------------------------------------------*/
672 draw_object (PrintInfo *pi,
673 glLabelObject *object,
674 glMergeRecord *record)
679 gl_debug (DEBUG_PRINT, "START");
681 gl_label_object_get_position (object, &x0, &y0);
682 gl_label_object_get_affine (object, affine);
684 gnome_print_gsave (pi->pc);
686 gnome_print_translate (pi->pc, x0, y0);
687 gnome_print_concat (pi->pc, affine);
689 if (GL_IS_LABEL_TEXT(object)) {
690 draw_text_object (pi, GL_LABEL_TEXT(object), record);
691 } else if (GL_IS_LABEL_BOX(object)) {
692 draw_box_object (pi, GL_LABEL_BOX(object));
693 } else if (GL_IS_LABEL_LINE(object)) {
694 draw_line_object (pi, GL_LABEL_LINE(object));
695 } else if (GL_IS_LABEL_ELLIPSE(object)) {
696 draw_ellipse_object (pi, GL_LABEL_ELLIPSE(object));
697 } else if (GL_IS_LABEL_IMAGE(object)) {
698 draw_image_object (pi, GL_LABEL_IMAGE(object), record);
699 } else if (GL_IS_LABEL_BARCODE(object)) {
700 draw_barcode_object (pi, GL_LABEL_BARCODE(object), record);
703 gnome_print_grestore (pi->pc);
705 gl_debug (DEBUG_PRINT, "END");
708 /*---------------------------------------------------------------------------*/
709 /* PRIVATE. Draw text object. */
710 /*---------------------------------------------------------------------------*/
712 draw_text_object (PrintInfo *pi,
714 glMergeRecord *record)
719 gdouble x_offset, y_offset, w, object_w, object_h;
724 GnomeFontWeight font_weight;
725 gboolean font_italic_flag;
727 GtkJustification just;
728 GnomeGlyphList *glyphlist;
733 gl_debug (DEBUG_PRINT, "START");
735 gl_label_object_get_size (GL_LABEL_OBJECT(object), &object_w, &object_h);
736 lines = gl_label_text_get_lines (object);
737 gl_label_text_get_props (object,
738 &font_family, &font_size, &font_weight,
742 font = gnome_font_find_closest_from_weight_slant (
747 gnome_print_setfont (pi->pc, font);
749 gnome_print_setrgbcolor (pi->pc,
750 GL_COLOR_F_RED (color),
751 GL_COLOR_F_GREEN (color),
752 GL_COLOR_F_BLUE (color));
753 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (color));
755 text = gl_text_node_lines_expand (lines, record);
756 line = g_strsplit (text, "\n", -1);
759 art_affine_identity (affine);
761 for (i = 0; line[i] != NULL; i++) {
763 glyphlist = gnome_glyphlist_from_text_dumb (font, color,
767 gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox);
771 case GTK_JUSTIFY_LEFT:
772 x_offset = GL_LABEL_TEXT_MARGIN;
774 case GTK_JUSTIFY_CENTER:
775 x_offset = (object_w - GL_LABEL_TEXT_MARGIN - w) / 2.0;
777 case GTK_JUSTIFY_RIGHT:
778 x_offset = object_w - GL_LABEL_TEXT_MARGIN - w;
782 break; /* shouldn't happen */
785 y_offset = GL_LABEL_TEXT_MARGIN + (i + 1) * font_size
786 + gnome_font_get_descender (font);
788 gnome_print_moveto (pi->pc, x_offset, y_offset);
790 gnome_print_gsave (pi->pc);
791 gnome_print_scale (pi->pc, 1.0, -1.0);
792 gnome_print_show (pi->pc, line[i]);
793 gnome_print_grestore (pi->pc);
798 gl_text_node_lines_free (&lines);
799 g_free (font_family);
801 gl_debug (DEBUG_PRINT, "END");
804 /*---------------------------------------------------------------------------*/
805 /* PRIVATE. Draw box object. */
806 /*---------------------------------------------------------------------------*/
808 draw_box_object (PrintInfo *pi,
813 guint line_color, fill_color;
815 gl_debug (DEBUG_PRINT, "START");
817 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
818 line_width = gl_label_box_get_line_width (object);
819 line_color = gl_label_box_get_line_color (object);
820 fill_color = gl_label_box_get_fill_color (object);
822 /* Paint fill color */
823 create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
824 gnome_print_setrgbcolor (pi->pc,
825 GL_COLOR_F_RED (fill_color),
826 GL_COLOR_F_GREEN (fill_color),
827 GL_COLOR_F_BLUE (fill_color));
828 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (fill_color));
829 gnome_print_fill (pi->pc);
832 create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
833 gnome_print_setrgbcolor (pi->pc,
834 GL_COLOR_F_RED (line_color),
835 GL_COLOR_F_GREEN (line_color),
836 GL_COLOR_F_BLUE (line_color));
837 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (line_color));
838 gnome_print_setlinewidth (pi->pc, line_width);
839 gnome_print_stroke (pi->pc);
841 gl_debug (DEBUG_PRINT, "END");
844 /*---------------------------------------------------------------------------*/
845 /* PRIVATE. Draw line object. */
846 /*---------------------------------------------------------------------------*/
848 draw_line_object (PrintInfo *pi,
855 gl_debug (DEBUG_PRINT, "START");
857 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
858 line_width = gl_label_line_get_line_width (object);
859 line_color = gl_label_line_get_line_color (object);
861 gnome_print_moveto (pi->pc, 0.0, 0.0);
862 gnome_print_lineto (pi->pc, w, h);
863 gnome_print_setrgbcolor (pi->pc,
864 GL_COLOR_F_RED (line_color),
865 GL_COLOR_F_GREEN (line_color),
866 GL_COLOR_F_BLUE (line_color));
867 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (line_color));
868 gnome_print_setlinewidth (pi->pc, line_width);
869 gnome_print_stroke (pi->pc);
871 gl_debug (DEBUG_PRINT, "END");
874 /*---------------------------------------------------------------------------*/
875 /* PRIVATE. Draw ellipse object. */
876 /*---------------------------------------------------------------------------*/
878 draw_ellipse_object (PrintInfo *pi,
879 glLabelEllipse *object)
881 gdouble x0, y0, rx, ry, w, h;
883 guint line_color, fill_color;
885 gl_debug (DEBUG_PRINT, "START");
887 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
888 line_width = gl_label_ellipse_get_line_width (object);
889 line_color = gl_label_ellipse_get_line_color (object);
890 fill_color = gl_label_ellipse_get_fill_color (object);
897 /* Paint fill color */
898 create_ellipse_path (pi->pc, x0, y0, rx, ry);
899 gnome_print_setrgbcolor (pi->pc,
900 GL_COLOR_F_RED (fill_color),
901 GL_COLOR_F_GREEN (fill_color),
902 GL_COLOR_F_BLUE (fill_color));
903 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (fill_color));
904 gnome_print_fill (pi->pc);
907 create_ellipse_path (pi->pc, x0, y0, rx, ry);
908 gnome_print_setrgbcolor (pi->pc,
909 GL_COLOR_F_RED (line_color),
910 GL_COLOR_F_GREEN (line_color),
911 GL_COLOR_F_BLUE (line_color));
912 gnome_print_setopacity (pi->pc, GL_COLOR_F_ALPHA (line_color));
913 gnome_print_setlinewidth (pi->pc, line_width);
914 gnome_print_stroke (pi->pc);
916 gl_debug (DEBUG_PRINT, "END");
919 /*---------------------------------------------------------------------------*/
920 /* PRIVATE. Draw image object. */
921 /*---------------------------------------------------------------------------*/
923 draw_image_object (PrintInfo *pi,
924 glLabelImage *object,
925 glMergeRecord *record)
928 const GdkPixbuf *pixbuf;
930 gint image_w, image_h, image_stride;
931 gboolean image_alpha_flag;
934 gl_debug (DEBUG_PRINT, "START");
936 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
938 pixbuf = gl_label_image_get_pixbuf (object, record);
939 image_data = gdk_pixbuf_get_pixels (pixbuf);
940 image_w = gdk_pixbuf_get_width (pixbuf);
941 image_h = gdk_pixbuf_get_height (pixbuf);
942 image_stride = gdk_pixbuf_get_rowstride(pixbuf);
943 image_alpha_flag = gdk_pixbuf_get_has_alpha(pixbuf);
945 gnome_print_gsave (pi->pc);
946 gnome_print_translate (pi->pc, 0.0, h);
947 gnome_print_scale (pi->pc, w, -h);
948 if (image_alpha_flag) {
949 #ifndef NO_ALPHA_HACK
952 image_data2 = get_pixels_as_rgb (pixbuf);
953 ret = gnome_print_rgbimage (pi->pc, image_data2,
954 image_w, image_h, image_stride);
955 g_free (image_data2);
957 ret = gnome_print_rgbaimage (pi->pc, image_data,
958 image_w, image_h, image_stride);
961 ret = gnome_print_rgbimage (pi->pc, image_data,
962 image_w, image_h, image_stride);
964 gnome_print_grestore (pi->pc);
966 gl_debug (DEBUG_PRINT, "END");
969 /*---------------------------------------------------------------------------*/
970 /* PRIVATE. Draw box object. */
971 /*---------------------------------------------------------------------------*/
973 draw_barcode_object (PrintInfo *pi,
974 glLabelBarcode *object,
975 glMergeRecord *record)
979 glBarcodeChar *bchar;
983 gchar *text, *cstring;
984 glTextNode *text_node;
985 glBarcodeStyle style;
987 gboolean checksum_flag;
991 gl_debug (DEBUG_PRINT, "START");
993 text_node = gl_label_barcode_get_data (object);
994 gl_label_barcode_get_props (object,
995 &style, &text_flag, &checksum_flag, &color);
996 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
998 text = gl_text_node_expand (text_node, record);
999 gbc = gl_barcode_new (style, text_flag, checksum_flag, w, h, text);
1001 gl_text_node_free (&text_node);
1005 font = gnome_font_find_closest_from_weight_slant (
1006 GL_BARCODE_FONT_FAMILY,
1007 GL_BARCODE_FONT_WEIGHT,
1009 gnome_print_setfont (pi->pc, font);
1011 gnome_print_setrgbcolor (pi->pc,
1012 GL_COLOR_F_RED (color),
1013 GL_COLOR_F_GREEN (color),
1014 GL_COLOR_F_BLUE (color));
1015 gnome_print_setopacity (pi->pc,
1016 GL_COLOR_F_ALPHA (color));
1018 y_offset = 12.0 - gnome_font_get_descender (font);
1019 gnome_print_moveto (pi->pc, 0.0, y_offset);
1021 gnome_print_gsave (pi->pc);
1022 gnome_print_scale (pi->pc, 1.0, -1.0);
1023 gnome_print_show (pi->pc, _("Invalid barcode"));
1024 gnome_print_grestore (pi->pc);
1028 for (li = gbc->lines; li != NULL; li = li->next) {
1029 line = (glBarcodeLine *) li->data;
1031 gnome_print_moveto (pi->pc, line->x, line->y);
1032 gnome_print_lineto (pi->pc, line->x, line->y + line->length);
1033 gnome_print_setrgbcolor (pi->pc,
1034 GL_COLOR_F_RED (color),
1035 GL_COLOR_F_GREEN (color),
1036 GL_COLOR_F_BLUE (color));
1037 gnome_print_setopacity (pi->pc,
1038 GL_COLOR_F_ALPHA (color));
1039 gnome_print_setlinewidth (pi->pc, line->width);
1040 gnome_print_stroke (pi->pc);
1043 for (li = gbc->chars; li != NULL; li = li->next) {
1044 bchar = (glBarcodeChar *) li->data;
1046 font = gnome_font_find_closest_from_weight_slant (
1047 GL_BARCODE_FONT_FAMILY,
1048 GL_BARCODE_FONT_WEIGHT,
1049 FALSE, bchar->fsize);
1050 gnome_print_setfont (pi->pc, font);
1052 gnome_print_setrgbcolor (pi->pc,
1053 GL_COLOR_F_RED (color),
1054 GL_COLOR_F_GREEN (color),
1055 GL_COLOR_F_BLUE (color));
1056 gnome_print_setopacity (pi->pc,
1057 GL_COLOR_F_ALPHA (color));
1060 bchar->y + bchar->fsize -
1061 gnome_font_get_descender (font);
1062 gnome_print_moveto (pi->pc, bchar->x, y_offset);
1064 cstring = g_strdup_printf ("%c", bchar->c);
1065 gnome_print_gsave (pi->pc);
1066 gnome_print_scale (pi->pc, 1.0, -1.0);
1067 gnome_print_show (pi->pc, cstring);
1068 gnome_print_grestore (pi->pc);
1073 gl_barcode_free (&gbc);
1077 gl_debug (DEBUG_PRINT, "END");
1080 /*---------------------------------------------------------------------------*/
1081 /* PRIVATE. Draw outline. */
1082 /*---------------------------------------------------------------------------*/
1084 draw_outline (PrintInfo *pi,
1089 glTemplate *template;
1091 gl_debug (DEBUG_PRINT, "START");
1093 template = gl_label_get_template (label);
1095 gnome_print_setrgbcolor (pi->pc, 0.25, 0.25, 0.25);
1096 gnome_print_setopacity (pi->pc, 1.0);
1097 gnome_print_setlinewidth (pi->pc, 0.25);
1099 switch (template->label.style) {
1101 case GL_TEMPLATE_STYLE_RECT:
1102 gl_label_get_size (label, &w, &h);
1103 r = template->label.rect.r;
1105 /* simple rectangle */
1106 create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
1108 /* rectangle with rounded corners */
1109 create_rounded_rectangle_path (pi->pc, 0.0, 0.0,
1112 gnome_print_stroke (pi->pc);
1115 case GL_TEMPLATE_STYLE_ROUND:
1117 r1 = template->label.round.r;
1118 create_ellipse_path (pi->pc, r1, r1, r1, r1);
1119 gnome_print_stroke (pi->pc);
1122 case GL_TEMPLATE_STYLE_CD:
1123 if ((template->label.cd.h == 0) && (template->label.cd.w == 0)) {
1124 /* CD style, round label w/ concentric round hole */
1125 r1 = template->label.cd.r1;
1126 r2 = template->label.cd.r2;
1127 create_ellipse_path (pi->pc, r1, r1, r1, r1);
1128 gnome_print_stroke (pi->pc);
1129 create_ellipse_path (pi->pc, r1, r1, r2, r2);
1130 gnome_print_stroke (pi->pc);
1132 /* Business Card CD style, clipped round label w/ hole */
1133 gl_label_get_size (label, &w, &h);
1134 r1 = template->label.cd.r1;
1135 r2 = template->label.cd.r2;
1136 create_clipped_circle_path (pi->pc, w/2, h/2, w, h, r1);
1137 gnome_print_stroke (pi->pc);
1138 create_ellipse_path (pi->pc, w/2, h/2, r2, r2);
1139 gnome_print_stroke (pi->pc);
1144 g_warning ("Unknown template label style");
1148 gl_template_free (&template);
1150 gl_debug (DEBUG_PRINT, "END");
1153 /*---------------------------------------------------------------------------*/
1154 /* PRIVATE. Clip to outline. */
1155 /*---------------------------------------------------------------------------*/
1157 clip_to_outline (PrintInfo *pi,
1163 glTemplate *template;
1165 gl_debug (DEBUG_PRINT, "START");
1167 template = gl_label_get_template (label);
1169 switch (template->label.style) {
1171 case GL_TEMPLATE_STYLE_RECT:
1172 gl_label_get_size (label, &w, &h);
1173 r = template->label.rect.r;
1174 waste = template->label.rect.waste;
1176 /* simple rectangle */
1177 create_rectangle_path (pi->pc, -waste, -waste, w+waste, h+waste);
1179 /* rectangle with rounded corners */
1180 create_rounded_rectangle_path (pi->pc, -waste, -waste,
1181 w+waste, h+waste, r);
1183 gnome_print_clip (pi->pc);
1186 case GL_TEMPLATE_STYLE_ROUND:
1187 r1 = template->label.round.r;
1188 waste = template->label.round.waste;
1189 create_ellipse_path (pi->pc, r1, r1, r1+waste, r1+waste);
1190 gnome_print_clip (pi->pc);
1193 case GL_TEMPLATE_STYLE_CD:
1194 waste = template->label.cd.waste;
1195 if ((template->label.cd.h == 0) && (template->label.cd.w == 0)) {
1196 /* CD style, round label w/ concentric round hole */
1197 r1 = template->label.cd.r1;
1198 create_ellipse_path (pi->pc, r1, r1, r1+waste, r1+waste);
1200 /* Business Card CD style, clipped round label w/ hole */
1201 gl_label_get_size (label, &w, &h);
1202 r1 = template->label.cd.r1;
1203 create_clipped_circle_path (pi->pc,
1205 w+2*waste, h+2*waste,
1208 gnome_print_clip (pi->pc);
1212 g_warning ("Unknown template label style");
1216 gl_template_free (&template);
1218 gl_debug (DEBUG_PRINT, "END");
1221 /*---------------------------------------------------------------------------*/
1222 /* PRIVATE. Clip punchouts. (Save some ink by not printing in CD holes) */
1224 /* Ideally this would be done in clip_to_outline, but I am not sure how to */
1225 /* invert the region for gnome_print_clip, so instead, I will just draw */
1226 /* a white circle on top of everything else. */
1227 /*---------------------------------------------------------------------------*/
1229 clip_punchouts (PrintInfo *pi,
1234 glTemplate *template;
1236 gl_debug (DEBUG_PRINT, "START");
1238 template = gl_label_get_template (label);
1240 switch (template->label.style) {
1242 case GL_TEMPLATE_STYLE_RECT:
1243 case GL_TEMPLATE_STYLE_ROUND:
1246 case GL_TEMPLATE_STYLE_CD:
1247 gl_label_get_size (label, &w, &h);
1248 waste = template->label.cd.waste;
1249 r2 = template->label.cd.r2;
1250 create_ellipse_path (pi->pc, w/2, h/2, r2-waste, r2-waste);
1251 gnome_print_setrgbcolor (pi->pc, 1.0, 1.0, 1.0);
1252 gnome_print_setopacity (pi->pc, 1.0);
1253 gnome_print_fill (pi->pc);
1257 g_warning ("Unknown template label style");
1261 gl_template_free (&template);
1263 gl_debug (DEBUG_PRINT, "END");
1266 /*---------------------------------------------------------------------------*/
1267 /* PRIVATE. Path creation utilities. */
1268 /*---------------------------------------------------------------------------*/
1270 create_rectangle_path (GnomePrintContext *pc,
1276 gl_debug (DEBUG_PRINT, "START");
1278 gnome_print_newpath (pc);
1279 gnome_print_moveto (pc, x0, y0);
1280 gnome_print_lineto (pc, x0 + w, y0);
1281 gnome_print_lineto (pc, x0 + w, y0 + h);
1282 gnome_print_lineto (pc, x0, y0 + h);
1283 gnome_print_lineto (pc, x0, y0);
1284 gnome_print_closepath (pc);
1286 gl_debug (DEBUG_PRINT, "END");
1290 create_ellipse_path (GnomePrintContext *pc,
1299 gl_debug (DEBUG_PRINT, "START");
1301 gnome_print_newpath (pc);
1302 gnome_print_moveto (pc, x0 + rx, y0);
1303 for (i_theta = ARC_FINE; i_theta <= 360; i_theta += ARC_FINE) {
1304 x = x0 + rx * cos (i_theta * G_PI / 180.0);
1305 y = y0 + ry * sin (i_theta * G_PI / 180.0);
1306 gnome_print_lineto (pc, x, y);
1308 gnome_print_closepath (pc);
1310 gl_debug (DEBUG_PRINT, "END");
1314 create_rounded_rectangle_path (GnomePrintContext *pc,
1324 gl_debug (DEBUG_PRINT, "START");
1326 gnome_print_newpath (pc);
1328 gnome_print_moveto (pc, x0 + r, y0);
1329 for (i_theta = ARC_COURSE; i_theta <= 90; i_theta += ARC_COURSE) {
1330 x = x0 + r - r * sin (i_theta * G_PI / 180.0);
1331 y = y0 + r - r * cos (i_theta * G_PI / 180.0);
1332 gnome_print_lineto (pc, x, y);
1334 for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1335 x = x0 + r - r * cos (i_theta * G_PI / 180.0);
1336 y = y0 + (h - r) + r * sin (i_theta * G_PI / 180.0);
1337 gnome_print_lineto (pc, x, y);
1339 for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1340 x = x0 + (w - r) + r * sin (i_theta * G_PI / 180.0);
1341 y = y0 + (h - r) + r * cos (i_theta * G_PI / 180.0);
1342 gnome_print_lineto (pc, x, y);
1344 for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1345 x = x0 + (w - r) + r * cos (i_theta * G_PI / 180.0);
1346 y = y0 + r - r * sin (i_theta * G_PI / 180.0);
1347 gnome_print_lineto (pc, x, y);
1349 gnome_print_lineto (pc, x0 + r, y0);
1351 gnome_print_closepath (pc);
1353 gl_debug (DEBUG_PRINT, "END");
1357 create_clipped_circle_path (GnomePrintContext *pc,
1365 gdouble theta1, theta2;
1368 gl_debug (DEBUG_PRINT, "START");
1370 theta1 = (180.0/G_PI) * acos (w / (2.0*r));
1371 theta2 = (180.0/G_PI) * asin (h / (2.0*r));
1373 gnome_print_newpath (pc);
1375 x = x0 + r * cos (theta1 * G_PI / 180.0);
1376 y = y0 + r * sin (theta1 * G_PI / 180.0);
1377 gnome_print_moveto (pc, x, y);
1379 for ( i_theta = theta1 + ARC_FINE; i_theta < theta2; i_theta +=ARC_FINE ) {
1380 x = x0 + r * cos (i_theta * G_PI / 180.0);
1381 y = y0 + r * sin (i_theta * G_PI / 180.0);
1382 gnome_print_lineto (pc, x, y);
1385 x = x0 + r * cos (theta2 * G_PI / 180.0);
1386 y = y0 + r * sin (theta2 * G_PI / 180.0);
1387 gnome_print_lineto (pc, x, y);
1389 if ( fabs (theta2 - 90.0) > GNOME_CANVAS_EPSILON ) {
1390 x = x0 + r * cos ((180-theta2) * G_PI / 180.0);
1391 y = y0 + r * sin ((180-theta2) * G_PI / 180.0);
1392 gnome_print_lineto (pc, x, y);
1395 for ( i_theta = 180-theta2+ARC_FINE; i_theta < (180-theta1); i_theta +=ARC_FINE ) {
1396 x = x0 + r * cos (i_theta * G_PI / 180.0);
1397 y = y0 + r * sin (i_theta * G_PI / 180.0);
1398 gnome_print_lineto (pc, x, y);
1401 x = x0 + r * cos ((180-theta1) * G_PI / 180.0);
1402 y = y0 + r * sin ((180-theta1) * G_PI / 180.0);
1403 gnome_print_lineto (pc, x, y);
1405 if ( fabs (theta1) > GNOME_CANVAS_EPSILON ) {
1406 x = x0 + r * cos ((180+theta1) * G_PI / 180.0);
1407 y = y0 + r * sin ((180+theta1) * G_PI / 180.0);
1408 gnome_print_lineto (pc, x, y);
1411 for ( i_theta = 180+theta1+ARC_FINE; i_theta < (180+theta2); i_theta +=ARC_FINE ) {
1412 x = x0 + r * cos (i_theta * G_PI / 180.0);
1413 y = y0 + r * sin (i_theta * G_PI / 180.0);
1414 gnome_print_lineto (pc, x, y);
1417 x = x0 + r * cos ((180+theta2) * G_PI / 180.0);
1418 y = y0 + r * sin ((180+theta2) * G_PI / 180.0);
1419 gnome_print_lineto (pc, x, y);
1421 if ( fabs (theta2 - 90.0) > GNOME_CANVAS_EPSILON ) {
1422 x = x0 + r * cos ((360-theta2) * G_PI / 180.0);
1423 y = y0 + r * sin ((360-theta2) * G_PI / 180.0);
1424 gnome_print_lineto (pc, x, y);
1427 for ( i_theta = 360-theta2+ARC_FINE; i_theta < (360-theta1); i_theta +=ARC_FINE ) {
1428 x = x0 + r * cos (i_theta * G_PI / 180.0);
1429 y = y0 + r * sin (i_theta * G_PI / 180.0);
1430 gnome_print_lineto (pc, x, y);
1433 if ( fabs (theta1) > GNOME_CANVAS_EPSILON ) {
1434 x = x0 + r * cos ((360-theta1) * G_PI / 180.0);
1435 y = y0 + r * sin ((360-theta1) * G_PI / 180.0);
1436 gnome_print_lineto (pc, x, y);
1439 x = x0 + r * cos (theta1 * G_PI / 180.0);
1440 y = y0 + r * sin (theta1 * G_PI / 180.0);
1441 gnome_print_lineto (pc, x, y);
1443 gnome_print_closepath (pc);
1445 gl_debug (DEBUG_PRINT, "END");
1448 #ifndef NO_ALPHA_HACK
1449 /*---------------------------------------------------------------------------*/
1450 /* PRIVATE. Extract a copy of rgba pixels, removing alpha by compositing */
1451 /* with a white background. */
1453 /* This is currently needed due to the lousy job gnome-print does in */
1454 /* rendering images with alpha channels to PS. This sacrafices the ability */
1455 /* to do compositing of images with other items in the background. */
1456 /*---------------------------------------------------------------------------*/
1458 get_pixels_as_rgb (const GdkPixbuf *pixbuf)
1460 gint bits_per_sample, channels;
1462 gint width, height, rowstride;
1464 guchar *buf_src, *buf_dest;
1465 guchar *p_src, *p_dest;
1468 gdouble alpha, beta;
1470 gl_debug (DEBUG_PRINT, "START");
1472 g_return_val_if_fail (pixbuf && GDK_IS_PIXBUF (pixbuf), NULL);
1474 /* extract pixels and parameters from pixbuf. */
1475 buf_src = gdk_pixbuf_get_pixels (pixbuf);
1476 bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);
1477 channels = gdk_pixbuf_get_n_channels (pixbuf);
1478 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
1479 width = gdk_pixbuf_get_width (pixbuf);
1480 height = gdk_pixbuf_get_height (pixbuf);
1481 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
1483 /* validate assumptions about pixbuf. */
1484 g_return_val_if_fail (buf_src, NULL);
1485 g_return_val_if_fail (bits_per_sample == 8, NULL);
1486 g_return_val_if_fail (channels == 4, NULL);
1487 g_return_val_if_fail (has_alpha, NULL);
1488 g_return_val_if_fail (width > 0, NULL);
1489 g_return_val_if_fail (height > 0, NULL);
1490 g_return_val_if_fail (rowstride > 0, NULL);
1492 /* Allocate a destination buffer */
1493 bytes = height * rowstride;
1494 gl_debug (DEBUG_PRINT, "bytes = %d", bytes);
1495 buf_dest = g_try_malloc (bytes);
1499 gl_debug (DEBUG_PRINT, "buf_dest = %x", buf_dest);
1501 /* Copy pixels, transforming rgba to rgb by compositing with a white bg. */
1504 for ( iy=0; iy < height; iy++ ) {
1506 p_src = buf_src + iy*rowstride;
1507 p_dest = buf_dest + iy*rowstride;
1509 for ( ix=0; ix < width; ix++ ) {
1519 *p_dest++ = (guchar) (alpha*r + beta*255 + 0.5);
1520 *p_dest++ = (guchar) (alpha*g + beta*255 + 0.5);
1521 *p_dest++ = (guchar) (alpha*b + beta*255 + 0.5);
1527 gl_debug (DEBUG_PRINT, "START");