]> git.sur5r.net Git - glabels/blob - glabels2/src/print.c
2004-01-31 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / print.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  print.c:  Print module
5  *
6  *  Copyright (C) 2001  Jim Evins <evins@snaught.com>.
7  *
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.
12  *
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.
17  *
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
21  */
22 #include <config.h>
23
24 #include <math.h>
25 #include <time.h>
26 #include <ctype.h>
27 #include <gtk/gtk.h>
28
29 #include "print.h"
30 #include "label.h"
31 #include "label-text.h"
32 #include "label-box.h"
33 #include "label-line.h"
34 #include "label-ellipse.h"
35 #include "label-image.h"
36 #include "label-barcode.h"
37 #include "bc.h"
38 #include <libglabels/template.h>
39 #include "color.h"
40
41 #include "debug.h"
42
43 #define ARC_FINE   2  /* Resolution in degrees of large arcs */
44 #define ARC_COURSE 5  /* Resolution in degrees of small arcs */
45
46 #define TICK_OFFSET  2.25
47 #define TICK_LENGTH 18.0
48
49 /*=========================================================================*/
50 /* Private types.                                                          */
51 /*=========================================================================*/
52
53 typedef struct _PrintInfo {
54         /* gnome print context */
55         GnomePrintContext *pc;
56
57         /* gnome print configuration */
58         GnomePrintConfig *config;
59
60         /* gLabels Template */
61         glTemplate *template;
62         gboolean label_rotate_flag;
63
64         /* page size */
65         gdouble page_width;
66         gdouble page_height;
67
68         /* page counter */
69         gint sheet;
70 } PrintInfo;
71
72
73 /*=========================================================================*/
74 /* Private function prototypes.                                            */
75 /*=========================================================================*/
76 static PrintInfo *print_info_new              (GnomePrintJob    *job,
77                                                glLabel          *label);
78
79 static void       print_info_free             (PrintInfo       **pi);
80
81
82 static void       print_page_begin            (PrintInfo        *pi);
83
84 static void       print_page_end              (PrintInfo        *pi);
85
86 static void       print_crop_marks            (PrintInfo        *pi);
87
88 static void       print_label                 (PrintInfo        *pi,
89                                                glLabel          *label,
90                                                gdouble           x,
91                                                gdouble           y,
92                                                glMergeRecord    *record,
93                                                gboolean          outline_flag,
94                                                gboolean          reverse_flag);
95
96
97 static void       draw_label                  (PrintInfo        *pi,
98                                                glLabel          *label,
99                                                glMergeRecord    *record);
100
101
102 static void       draw_object                 (PrintInfo        *pi,
103                                                glLabelObject    *object,
104                                                glMergeRecord    *record);
105
106 static void       draw_text_object            (PrintInfo        *pi,
107                                                glLabelText      *object,
108                                                glMergeRecord    *record);
109
110 static void       draw_box_object             (PrintInfo        *pi,
111                                                glLabelBox       *object);
112
113 static void       draw_line_object            (PrintInfo        *pi,
114                                                glLabelLine      *object);
115
116 static void       draw_ellipse_object         (PrintInfo        *pi,
117                                                glLabelEllipse   *object);
118
119 static void       draw_image_object           (PrintInfo        *pi,
120                                                glLabelImage     *object,
121                                                glMergeRecord    *record);
122
123 static void       draw_barcode_object         (PrintInfo        *pi,
124                                                glLabelBarcode   *object,
125                                                glMergeRecord    *record);
126
127
128 static void       draw_outline                (PrintInfo        *pi,
129                                                glLabel          *label);
130
131 static void       clip_to_outline             (PrintInfo        *pi,
132                                                glLabel          *label);
133
134 static void       clip_punchouts              (PrintInfo        *pi,
135                                                glLabel          *label);
136
137
138 static void       create_rectangle_path         (GnomePrintContext *pc,
139                                                  gdouble            x0,
140                                                  gdouble            y0,
141                                                  gdouble            w,
142                                                  gdouble            h);
143
144 static void       create_ellipse_path           (GnomePrintContext *pc,
145                                                  gdouble            x0,
146                                                  gdouble            y0,
147                                                  gdouble            rx,
148                                                  gdouble            ry);
149
150 static void       create_rounded_rectangle_path (GnomePrintContext *pc,
151                                                  gdouble            x0,
152                                                  gdouble            y0,
153                                                  gdouble            w,
154                                                  gdouble            h,
155                                                  gdouble            r);
156
157 static void       create_clipped_circle_path    (GnomePrintContext *pc,
158                                                  gdouble            x0,
159                                                  gdouble            y0,
160                                                  gdouble            w,
161                                                  gdouble            h,
162                                                  gdouble            r);
163
164 #ifndef NO_ALPHA_HACK
165 static guchar *   get_pixels_as_rgb             (const GdkPixbuf   *pixbuf);
166 #endif
167
168 \f
169 /*****************************************************************************/
170 /* Simple (no merge data) print command.                                     */
171 /*****************************************************************************/
172 void
173 gl_print_simple (GnomePrintJob    *job,
174                  glLabel          *label,
175                  gint              n_sheets,
176                  gint              first,
177                  gint              last,
178                  glPrintFlags     *flags)
179 {
180         PrintInfo                 *pi;
181         const glTemplateLabelType *label_type;
182         gint                       i_sheet, i_label;
183         glTemplateOrigin          *origins;
184
185         gl_debug (DEBUG_PRINT, "START");
186
187         pi         = print_info_new (job, label);
188         label_type = gl_template_get_first_label_type (pi->template);
189
190         origins = gl_template_get_origins (label_type);
191
192         for (i_sheet = 0; i_sheet < n_sheets; i_sheet++) {
193
194                 print_page_begin (pi);
195                 if (flags->crop_marks) {
196                         print_crop_marks (pi);
197                 }
198
199                 for (i_label = first - 1; i_label < last; i_label++) {
200
201                         print_label (pi, label,
202                                      origins[i_label].x, origins[i_label].y,
203                                      NULL, flags->outline, flags->reverse);
204
205                 }
206
207                 print_page_end (pi);
208         }
209
210         g_free (origins);
211
212         print_info_free (&pi);
213
214         gl_debug (DEBUG_PRINT, "END");
215 }
216
217 /*****************************************************************************/
218 /* Merge print command (collated copies)                                     */
219 /*****************************************************************************/
220 void
221 gl_print_merge_collated (GnomePrintJob    *job,
222                          glLabel          *label,
223                          gint              n_copies,
224                          gint              first,
225                          glPrintFlags     *flags)
226 {
227         glMerge                   *merge;
228         const GList               *record_list;
229         PrintInfo                 *pi;
230         const glTemplateLabelType *label_type;
231         gint                       i_sheet, i_label, n_labels_per_page, i_copy;
232         glMergeRecord             *record;
233         GList                     *p;
234         glTemplateOrigin          *origins;
235
236         gl_debug (DEBUG_PRINT, "START");
237
238         merge = gl_label_get_merge (label);
239         record_list = gl_merge_get_record_list (merge);
240
241         pi = print_info_new (job, label);
242         label_type = gl_template_get_first_label_type (pi->template);
243
244         n_labels_per_page = gl_template_get_n_labels (label_type);
245         origins = gl_template_get_origins (label_type);
246
247         i_sheet = 0;
248         i_label = first - 1;
249
250         for ( p=(GList *)record_list; p!=NULL; p=p->next ) {
251                 record = (glMergeRecord *)p->data;
252                         
253                 if ( record->select_flag ) {
254                         for (i_copy = 0; i_copy < n_copies; i_copy++) {
255
256                                 if ((i_label == 0) || (i_sheet == 0)) {
257                                         i_sheet++;
258                                         print_page_begin (pi);
259                                         if (flags->crop_marks) {
260                                                 print_crop_marks (pi);
261                                         }
262                                 }
263
264                                 print_label (pi, label,
265                                              origins[i_label].x,
266                                              origins[i_label].y,
267                                              record,
268                                              flags->outline, flags->reverse);
269
270                                 i_label = (i_label + 1) % n_labels_per_page;
271                                 if (i_label == 0) {
272                                         print_page_end (pi);
273                                 }
274                         }
275                 }
276         }
277
278         if (i_label != 0) {
279                 print_page_end (pi);
280         }
281
282         g_free (origins);
283
284         print_info_free (&pi);
285
286         gl_debug (DEBUG_PRINT, "END");
287 }
288
289 /*****************************************************************************/
290 /* Merge print command (uncollated copies)                                   */
291 /*****************************************************************************/
292 void
293 gl_print_merge_uncollated (GnomePrintJob    *job,
294                            glLabel          *label,
295                            gint              n_copies,
296                            gint              first,
297                            glPrintFlags     *flags)
298 {
299         glMerge                   *merge;
300         const GList               *record_list;
301         PrintInfo                 *pi;
302         const glTemplateLabelType *label_type;
303         gint                       i_sheet, i_label, n_labels_per_page, i_copy;
304         glMergeRecord             *record;
305         GList                     *p;
306         glTemplateOrigin          *origins;
307
308         gl_debug (DEBUG_PRINT, "START");
309
310         merge = gl_label_get_merge (label);
311         record_list = gl_merge_get_record_list (merge);
312
313         pi = print_info_new (job, label);
314         label_type = gl_template_get_first_label_type (pi->template);
315
316         n_labels_per_page = gl_template_get_n_labels (label_type);
317         origins = gl_template_get_origins (label_type);
318
319         i_sheet = 0;
320         i_label = first - 1;
321
322         for (i_copy = 0; i_copy < n_copies; i_copy++) {
323
324                 for ( p=(GList *)record_list; p!=NULL; p=p->next ) {
325                         record = (glMergeRecord *)p->data;
326                         
327                         if ( record->select_flag ) {
328
329
330                                 if ((i_label == 0) || (i_sheet == 0)) {
331                                         i_sheet++;
332                                         print_page_begin (pi);
333                                         if (flags->crop_marks) {
334                                                 print_crop_marks (pi);
335                                         }
336                                 }
337
338                                 print_label (pi, label,
339                                              origins[i_label].x,
340                                              origins[i_label].y,
341                                              record,
342                                              flags->outline, flags->reverse);
343
344                                 i_label = (i_label + 1) % n_labels_per_page;
345                                 if (i_label == 0) {
346                                         print_page_end (pi);
347                                 }
348                         }
349                 }
350
351         }
352         if (i_label != 0) {
353                 print_page_end (pi);
354         }
355
356         g_free (origins);
357
358         print_info_free (&pi);
359
360         gl_debug (DEBUG_PRINT, "END");
361 }
362
363 /*****************************************************************************/
364 /* Batch print.  Call appropriate function above.                            */
365 /*****************************************************************************/
366 void
367 gl_print_batch (GnomePrintJob    *job,
368                 glLabel          *label,
369                 gint              n_sheets,
370                 gint              n_copies,
371                 glPrintFlags     *flags)
372 {
373         glMerge                   *merge;
374         glTemplate                *template;
375         const glTemplateLabelType *label_type;
376         gint                       n_per_page;
377         
378         gl_debug (DEBUG_PRINT, "START");
379
380         merge = gl_label_get_merge (label);
381         template = gl_label_get_template (label);
382         label_type = gl_template_get_first_label_type (template);
383
384         if ( merge == NULL ) {
385                 n_per_page = gl_template_get_n_labels(label_type);
386
387                 gl_print_simple (job, label, n_sheets, 1, n_per_page, flags);
388         } else {
389                 gl_print_merge_collated (job, label, n_copies, 1, flags);
390         }
391         gl_template_free (template);
392
393         gl_debug (DEBUG_PRINT, "END");
394 }
395
396 /*---------------------------------------------------------------------------*/
397 /* PRIVATE.  new print info structure                                        */
398 /*---------------------------------------------------------------------------*/
399 static PrintInfo *
400 print_info_new (GnomePrintJob    *job,
401                 glLabel          *label)
402 {
403         PrintInfo            *pi = g_new0 (PrintInfo, 1);
404         glTemplate           *template;
405
406         gl_debug (DEBUG_PRINT, "START");
407
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);
410
411         template = gl_label_get_template (label);
412
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);
417
418         pi->pc = gnome_print_job_get_context (job);
419         pi->config = gnome_print_job_get_config (job);
420
421         gl_debug (DEBUG_PRINT,
422                   "setting page size = \"%s\"", template->page_size);
423
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);
432
433         pi->page_width  = template->page_width;
434         pi->page_height = template->page_height;
435
436         pi->template = template;
437         pi->label_rotate_flag = gl_label_get_rotate_flag (label);
438
439         pi->sheet = 0;
440
441         gl_debug (DEBUG_PRINT, "END");
442
443         return pi;
444 }
445
446 /*---------------------------------------------------------------------------*/
447 /* PRIVATE.  free print info structure                                       */
448 /*---------------------------------------------------------------------------*/
449 static void
450 print_info_free (PrintInfo **pi)
451 {
452         gl_debug (DEBUG_PRINT, "START");
453
454         gl_template_free ((*pi)->template);
455         (*pi)->template = NULL;
456
457         gnome_print_context_close ((*pi)->pc);
458
459         g_free (*pi);
460         *pi = NULL;
461
462         gl_debug (DEBUG_PRINT, "END");
463 }
464
465 /*---------------------------------------------------------------------------*/
466 /* PRIVATE.  Begin a new page.                                               */
467 /*---------------------------------------------------------------------------*/
468 static void
469 print_page_begin (PrintInfo *pi)
470 {
471         gchar *str;
472
473         gl_debug (DEBUG_PRINT, "START");
474
475         pi->sheet++;
476
477         str = g_strdup_printf ("sheet%02d", pi->sheet);
478         gnome_print_beginpage (pi->pc, str);
479         g_free (str);
480
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);
484
485         gl_debug (DEBUG_PRINT, "END");
486 }
487
488 /*---------------------------------------------------------------------------*/
489 /* PRIVATE.  End a page.                                                     */
490 /*---------------------------------------------------------------------------*/
491 static void
492 print_page_end (PrintInfo *pi)
493 {
494         gl_debug (DEBUG_PRINT, "START");
495
496         gnome_print_showpage (pi->pc);
497
498         gl_debug (DEBUG_PRINT, "END");
499 }
500
501 /*---------------------------------------------------------------------------*/
502 /* PRIVATE.  Print crop tick marks.                                          */
503 /*---------------------------------------------------------------------------*/
504 static void
505 print_crop_marks (PrintInfo *pi)
506 {
507         const glTemplateLabelType *label_type;
508         gdouble                    w, h, page_w, page_h;
509         GList                     *p;
510         glTemplateLayout          *layout;
511         gdouble                    xmin, ymin, xmax, ymax, dx, dy;
512         gdouble                    x1, y1, x2, y2, x3, y3, x4, y4;
513         gint                       ix, iy, nx, ny;
514
515         gl_debug (DEBUG_PRINT, "START");
516
517         label_type = gl_template_get_first_label_type (pi->template);
518
519         gl_template_get_label_size (label_type, &w, &h);
520
521         page_w = pi->page_width;
522         page_h = pi->page_height;
523
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);
527
528         for (p=label_type->layouts; p != NULL; p=p->next) {
529
530                 layout = (glTemplateLayout *)p->data;
531
532                 xmin = layout->x0;
533                 ymin = layout->y0;
534                 xmax = layout->x0 + layout->dx*(layout->nx - 1) + w;
535                 ymax = layout->y0 + layout->dy*(layout->ny - 1) + h;
536
537                 dx = layout->dx;
538                 dy = layout->dy;
539
540                 nx = layout->nx;
541                 ny = layout->ny;
542
543                 for (ix=0; ix < nx; ix++) {
544
545                         x1 = xmin + ix*dx;
546                         x2 = x1 + w;
547
548                         y1 = MAX((ymin - TICK_OFFSET), 0.0);
549                         y2 = MAX((y1 - TICK_LENGTH), 0.0);
550
551                         y3 = MIN((ymax + TICK_OFFSET), page_h);
552                         y4 = MIN((y3 + TICK_LENGTH), page_h);
553
554                         gnome_print_moveto (pi->pc, x1, y1);
555                         gnome_print_lineto (pi->pc, x1, y2);
556                         gnome_print_stroke (pi->pc);
557
558                         gnome_print_moveto (pi->pc, x2, y1);
559                         gnome_print_lineto (pi->pc, x2, y2);
560                         gnome_print_stroke (pi->pc);
561
562                         gnome_print_moveto (pi->pc, x1, y3);
563                         gnome_print_lineto (pi->pc, x1, y4);
564                         gnome_print_stroke (pi->pc);
565
566                         gnome_print_moveto (pi->pc, x2, y3);
567                         gnome_print_lineto (pi->pc, x2, y4);
568                         gnome_print_stroke (pi->pc);
569
570                 }
571
572                 for (iy=0; iy < ny; iy++) {
573
574                         y1 = ymin + iy*dy;
575                         y2 = y1 + h;
576
577                         x1 = MAX((xmin - TICK_OFFSET), 0.0);
578                         x2 = MAX((x1 - TICK_LENGTH), 0.0);
579
580                         x3 = MIN((xmax + TICK_OFFSET), page_w);
581                         x4 = MIN((x3 + TICK_LENGTH), page_w);
582
583                         gnome_print_moveto (pi->pc, x1, y1);
584                         gnome_print_lineto (pi->pc, x2, y1);
585                         gnome_print_stroke (pi->pc);
586
587                         gnome_print_moveto (pi->pc, x1, y2);
588                         gnome_print_lineto (pi->pc, x2, y2);
589                         gnome_print_stroke (pi->pc);
590
591                         gnome_print_moveto (pi->pc, x3, y1);
592                         gnome_print_lineto (pi->pc, x4, y1);
593                         gnome_print_stroke (pi->pc);
594
595                         gnome_print_moveto (pi->pc, x3, y2);
596                         gnome_print_lineto (pi->pc, x4, y2);
597                         gnome_print_stroke (pi->pc);
598
599                 }
600
601         }
602
603         gl_debug (DEBUG_PRINT, "END");
604 }
605
606 /*---------------------------------------------------------------------------*/
607 /* PRIVATE.  Print i'th label.                                               */
608 /*---------------------------------------------------------------------------*/
609 static void
610 print_label (PrintInfo     *pi,
611              glLabel       *label,
612              gdouble        x,
613              gdouble        y,
614              glMergeRecord *record,
615              gboolean       outline_flag,
616              gboolean       reverse_flag)
617 {
618         const glTemplateLabelType *label_type;
619         gdouble                    width, height;
620
621         gl_debug (DEBUG_PRINT, "START");
622
623         label_type = gl_template_get_first_label_type (pi->template);
624
625         gl_label_get_size (label, &width, &height);
626
627         gnome_print_gsave (pi->pc);
628
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);
636         }
637         if ( reverse_flag ) {
638                 gnome_print_translate (pi->pc, width, 0.0);
639                 gnome_print_scale (pi->pc, -1.0, 1.0);
640         }
641
642         clip_to_outline (pi, label);
643         draw_label (pi, label, record);
644         if (outline_flag) {
645                 draw_outline (pi, label);
646         }
647         clip_punchouts (pi, label);
648
649         gnome_print_grestore (pi->pc);
650
651         gl_debug (DEBUG_PRINT, "END");
652 }
653
654 /*---------------------------------------------------------------------------*/
655 /* PRIVATE.  Draw label.                                                     */
656 /*---------------------------------------------------------------------------*/
657 static void
658 draw_label (PrintInfo     *pi,
659             glLabel       *label,
660             glMergeRecord *record)
661 {
662         GList *p_obj;
663         glLabelObject *object;
664
665         gl_debug (DEBUG_PRINT, "START");
666
667         for (p_obj = label->objects; p_obj != NULL; p_obj = p_obj->next) {
668                 object = (glLabelObject *) p_obj->data;
669
670                 draw_object (pi, object, record);
671         }
672
673         gl_debug (DEBUG_PRINT, "END");
674 }
675
676 /*---------------------------------------------------------------------------*/
677 /* PRIVATE.  Draw object.                                                    */
678 /*---------------------------------------------------------------------------*/
679 static void
680 draw_object (PrintInfo     *pi,
681              glLabelObject *object,
682              glMergeRecord *record)
683 {
684         gdouble x0, y0;
685         gdouble affine[6];
686
687         gl_debug (DEBUG_PRINT, "START");
688
689         gl_label_object_get_position (object, &x0, &y0);
690         gl_label_object_get_affine (object, affine);
691
692         gnome_print_gsave (pi->pc);
693
694         gnome_print_translate (pi->pc, x0, y0);
695         gnome_print_concat (pi->pc, affine);
696
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);
709         }
710
711         gnome_print_grestore (pi->pc);
712
713         gl_debug (DEBUG_PRINT, "END");
714 }
715
716 /*---------------------------------------------------------------------------*/
717 /* PRIVATE.  Draw text object.                                               */
718 /*---------------------------------------------------------------------------*/
719 static void
720 draw_text_object (PrintInfo     *pi,
721                   glLabelText   *object,
722                   glMergeRecord *record)
723 {
724         GnomeFont *font;
725         gchar **line;
726         gint i;
727         gdouble x_offset, y_offset, w, object_w, object_h;
728         gchar *text;
729         GList *lines;
730         gchar *font_family;
731         gdouble font_size;
732         GnomeFontWeight font_weight;
733         gboolean font_italic_flag;
734         guint color;
735         GtkJustification just;
736         GnomeGlyphList *glyphlist;
737         ArtDRect bbox;
738         gdouble affine[6];
739         gdouble text_line_spacing;
740
741
742         gl_debug (DEBUG_PRINT, "START");
743
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));
753
754         font = gnome_font_find_closest_from_weight_slant (
755                                        font_family,
756                                        font_weight,
757                                        font_italic_flag,
758                                        font_size);
759         gnome_print_setfont (pi->pc, font);
760
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));
766
767         text = gl_text_node_lines_expand (lines, record);
768         line = g_strsplit (text, "\n", -1);
769         g_free (text);
770
771         art_affine_identity (affine);
772
773         for (i = 0; line[i] != NULL; i++) {
774
775                 glyphlist = gnome_glyphlist_from_text_dumb (font, color,
776                                                             0.0, 0.0,
777                                                             line[i]);
778
779                 gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox);
780                 w = bbox.x1;
781
782                 switch (just) {
783                 case GTK_JUSTIFY_LEFT:
784                         x_offset = GL_LABEL_TEXT_MARGIN;
785                         break;
786                 case GTK_JUSTIFY_CENTER:
787                         x_offset = (object_w - GL_LABEL_TEXT_MARGIN - w) / 2.0;
788                         break;
789                 case GTK_JUSTIFY_RIGHT:
790                         x_offset = object_w - GL_LABEL_TEXT_MARGIN - w;
791                         break;
792                 default:
793                         x_offset = 0.0;
794                         break;  /* shouldn't happen */
795                 }
796
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;
801
802                 /* Remove any text line spacing from the first row. */
803                 y_offset -= font_size * (text_line_spacing - 1);
804
805
806                 gnome_print_moveto (pi->pc, x_offset, y_offset);
807
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);
812         }
813
814         g_strfreev (line);
815
816         gl_text_node_lines_free (&lines);
817         g_free (font_family);
818
819         gl_debug (DEBUG_PRINT, "END");
820 }
821
822 /*---------------------------------------------------------------------------*/
823 /* PRIVATE.  Draw box object.                                                */
824 /*---------------------------------------------------------------------------*/
825 static void
826 draw_box_object (PrintInfo  *pi,
827                  glLabelBox *object)
828 {
829         gdouble w, h;
830         gdouble line_width;
831         guint line_color, fill_color;
832
833         gl_debug (DEBUG_PRINT, "START");
834
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));
839
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);
848
849         /* Draw outline */
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);
858
859         gl_debug (DEBUG_PRINT, "END");
860 }
861
862 /*---------------------------------------------------------------------------*/
863 /* PRIVATE.  Draw line object.                                               */
864 /*---------------------------------------------------------------------------*/
865 static void
866 draw_line_object (PrintInfo   *pi,
867                   glLabelLine *object)
868 {
869         gdouble w, h;
870         gdouble line_width;
871         guint line_color;
872
873         gl_debug (DEBUG_PRINT, "START");
874
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));
878
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);
888
889         gl_debug (DEBUG_PRINT, "END");
890 }
891
892 /*---------------------------------------------------------------------------*/
893 /* PRIVATE.  Draw ellipse object.                                            */
894 /*---------------------------------------------------------------------------*/
895 static void
896 draw_ellipse_object (PrintInfo      *pi,
897                      glLabelEllipse *object)
898 {
899         gdouble x0, y0, rx, ry, w, h;
900         gdouble line_width;
901         guint line_color, fill_color;
902
903         gl_debug (DEBUG_PRINT, "START");
904
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));
909
910         rx = w / 2.0;
911         ry = h / 2.0;
912         x0 = rx;
913         y0 = ry;
914
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);
923
924         /* Draw outline */
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);
933
934         gl_debug (DEBUG_PRINT, "END");
935 }
936
937 /*---------------------------------------------------------------------------*/
938 /* PRIVATE.  Draw image object.                                              */
939 /*---------------------------------------------------------------------------*/
940 static void
941 draw_image_object (PrintInfo     *pi,
942                    glLabelImage  *object,
943                    glMergeRecord *record)
944 {
945         gdouble w, h;
946         const GdkPixbuf *pixbuf;
947         guchar *image_data;
948         gint image_w, image_h, image_stride;
949         gboolean image_alpha_flag;
950         gint ret;
951
952         gl_debug (DEBUG_PRINT, "START");
953
954         gl_label_object_get_size     (GL_LABEL_OBJECT(object), &w, &h);
955
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);
962
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
968                 guchar *image_data2;
969
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);
974 #else
975                 ret = gnome_print_rgbaimage (pi->pc, image_data,
976                                             image_w, image_h, image_stride);
977 #endif
978         } else {
979                 ret = gnome_print_rgbimage (pi->pc, image_data,
980                                             image_w, image_h, image_stride);
981         }
982         gnome_print_grestore (pi->pc);
983
984         gl_debug (DEBUG_PRINT, "END");
985 }
986
987 /*---------------------------------------------------------------------------*/
988 /* PRIVATE.  Draw box object.                                                */
989 /*---------------------------------------------------------------------------*/
990 static void
991 draw_barcode_object (PrintInfo      *pi,
992                      glLabelBarcode *object,
993                      glMergeRecord  *record)
994 {
995         glBarcode *gbc;
996         glBarcodeLine *line;
997         glBarcodeChar *bchar;
998         GList *li;
999         gdouble y_offset;
1000         GnomeFont *font;
1001         gchar *text, *cstring;
1002         glTextNode          *text_node;
1003         glBarcodeStyle      style;
1004         gboolean            text_flag;
1005         gboolean            checksum_flag;
1006         guint               color;
1007         gdouble             w, h;
1008
1009         gl_debug (DEBUG_PRINT, "START");
1010
1011         text_node = gl_label_barcode_get_data (object);
1012         gl_label_barcode_get_props (object,
1013                                     &style, &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);
1016
1017         text = gl_text_node_expand (text_node, record);
1018         gbc = gl_barcode_new (style, text_flag, checksum_flag, w, h, text);
1019         g_free (text);
1020         gl_text_node_free (&text_node);
1021
1022         if (gbc == NULL) {
1023
1024                 font = gnome_font_find_closest_from_weight_slant (
1025                                                GL_BARCODE_FONT_FAMILY,
1026                                                GL_BARCODE_FONT_WEIGHT,
1027                                                FALSE, 12.0);
1028                 gnome_print_setfont (pi->pc, font);
1029
1030                 gnome_print_setrgbcolor (pi->pc,
1031                                          GL_COLOR_F_RED (color),
1032                                          GL_COLOR_F_GREEN (color),
1033                                          GL_COLOR_F_BLUE (color));
1034                 gnome_print_setopacity (pi->pc,
1035                                         GL_COLOR_F_ALPHA (color));
1036
1037                 y_offset = 12.0 - fabs (gnome_font_get_descender (font));
1038                 gnome_print_moveto (pi->pc, 0.0, y_offset);
1039
1040                 gnome_print_gsave (pi->pc);
1041                 gnome_print_scale (pi->pc, 1.0, -1.0);
1042                 gnome_print_show (pi->pc, _("Invalid barcode data"));
1043                 gnome_print_grestore (pi->pc);
1044
1045         } else {
1046
1047                 for (li = gbc->lines; li != NULL; li = li->next) {
1048                         line = (glBarcodeLine *) li->data;
1049
1050                         gnome_print_moveto (pi->pc, line->x, line->y);
1051                         gnome_print_lineto (pi->pc, line->x, line->y + line->length);
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));
1058                         gnome_print_setlinewidth (pi->pc, line->width);
1059                         gnome_print_stroke (pi->pc);
1060                 }
1061
1062                 for (li = gbc->chars; li != NULL; li = li->next) {
1063                         bchar = (glBarcodeChar *) li->data;
1064
1065                         font = gnome_font_find_closest_from_weight_slant (
1066                                                        GL_BARCODE_FONT_FAMILY,
1067                                                        GL_BARCODE_FONT_WEIGHT,
1068                                                        FALSE, bchar->fsize);
1069                         gnome_print_setfont (pi->pc, font);
1070
1071                         gnome_print_setrgbcolor (pi->pc,
1072                                                  GL_COLOR_F_RED (color),
1073                                                  GL_COLOR_F_GREEN (color),
1074                                                  GL_COLOR_F_BLUE (color));
1075                         gnome_print_setopacity (pi->pc,
1076                                                 GL_COLOR_F_ALPHA (color));
1077
1078                         y_offset =
1079                             bchar->fsize - fabs (gnome_font_get_descender (font));
1080
1081                         gnome_print_moveto (pi->pc, bchar->x, bchar->y+y_offset);
1082
1083                         cstring = g_strdup_printf ("%c", bchar->c);
1084                         gnome_print_gsave (pi->pc);
1085                         gnome_print_scale (pi->pc, 1.0, -1.0);
1086                         gnome_print_show (pi->pc, cstring);
1087                         gnome_print_grestore (pi->pc);
1088                         g_free (cstring);
1089
1090                 }
1091
1092                 gl_barcode_free (&gbc);
1093
1094         }
1095
1096         gl_debug (DEBUG_PRINT, "END");
1097 }
1098
1099 /*---------------------------------------------------------------------------*/
1100 /* PRIVATE.  Draw outline.                                                   */
1101 /*---------------------------------------------------------------------------*/
1102 static void
1103 draw_outline (PrintInfo *pi,
1104               glLabel   *label)
1105 {
1106         const glTemplateLabelType *label_type;
1107         gdouble                    w, h, r;
1108         gdouble                    r1, r2;
1109
1110         gl_debug (DEBUG_PRINT, "START");
1111
1112         label_type = gl_template_get_first_label_type (pi->template);
1113
1114         gnome_print_setrgbcolor (pi->pc, 0.25, 0.25, 0.25);
1115         gnome_print_setopacity (pi->pc, 1.0);
1116         gnome_print_setlinewidth (pi->pc, 0.25);
1117
1118         switch (label_type->shape) {
1119
1120         case GL_TEMPLATE_SHAPE_RECT:
1121                 gl_label_get_size (label, &w, &h);
1122                 r = label_type->size.rect.r;
1123                 if (r == 0.0) {
1124                         /* simple rectangle */
1125                         create_rectangle_path (pi->pc, 0.0, 0.0, w, h);
1126                 } else {
1127                         /* rectangle with rounded corners */
1128                         create_rounded_rectangle_path (pi->pc, 0.0, 0.0,
1129                                                        w, h, r);
1130                 }
1131                 gnome_print_stroke (pi->pc);
1132                 break;
1133
1134         case GL_TEMPLATE_SHAPE_ROUND:
1135                 /* Round style */
1136                 r1 = label_type->size.round.r;
1137                 create_ellipse_path (pi->pc, r1, r1, r1, r1);
1138                 gnome_print_stroke (pi->pc);
1139                 break;
1140
1141         case GL_TEMPLATE_SHAPE_CD:
1142                 if ((label_type->size.cd.h == 0) && (label_type->size.cd.w == 0)) {
1143                         /* CD style, round label w/ concentric round hole */
1144                         r1 = label_type->size.cd.r1;
1145                         r2 = label_type->size.cd.r2;
1146                         create_ellipse_path (pi->pc, r1, r1, r1, r1);
1147                         gnome_print_stroke (pi->pc);
1148                         create_ellipse_path (pi->pc, r1, r1, r2, r2);
1149                         gnome_print_stroke (pi->pc);
1150                 } else {
1151                         /* Business Card CD style, clipped round label w/ hole */
1152                         gl_label_get_size (label, &w, &h);
1153                         r1 = label_type->size.cd.r1;
1154                         r2 = label_type->size.cd.r2;
1155                         create_clipped_circle_path (pi->pc, w/2, h/2, w, h, r1);
1156                         gnome_print_stroke (pi->pc);
1157                         create_ellipse_path (pi->pc, w/2, h/2, r2, r2);
1158                         gnome_print_stroke (pi->pc);
1159                 }
1160                 break;
1161
1162         default:
1163                 g_warning ("Unknown template label style");
1164                 break;
1165         }
1166
1167         gl_debug (DEBUG_PRINT, "END");
1168 }
1169
1170 /*---------------------------------------------------------------------------*/
1171 /* PRIVATE.  Clip to outline.                                                */
1172 /*---------------------------------------------------------------------------*/
1173 static void
1174 clip_to_outline (PrintInfo *pi,
1175                  glLabel   *label)
1176 {
1177         const glTemplateLabelType *label_type;
1178         gdouble                    w, h, r;
1179         gdouble                    r1;
1180         gdouble                    waste;
1181
1182         gl_debug (DEBUG_PRINT, "START");
1183
1184         label_type = gl_template_get_first_label_type (pi->template);
1185
1186         switch (label_type->shape) {
1187
1188         case GL_TEMPLATE_SHAPE_RECT:
1189                 gl_label_get_size (label, &w, &h);
1190                 r = label_type->size.rect.r;
1191                 waste = label_type->waste;
1192                 if (r == 0.0) {
1193                         /* simple rectangle */
1194                         create_rectangle_path (pi->pc,
1195                                                -waste, -waste, w+2*waste, h+2*waste);
1196                 } else {
1197                         /* rectangle with rounded corners */
1198                         create_rounded_rectangle_path (pi->pc, -waste, -waste,
1199                                                        w+2*waste, h+2*waste, r);
1200                 }
1201                 gnome_print_clip (pi->pc);
1202                 break;
1203
1204         case GL_TEMPLATE_SHAPE_ROUND:
1205                 r1 = label_type->size.round.r;
1206                 waste = label_type->waste;
1207                 create_ellipse_path (pi->pc, r1, r1, r1+waste, r1+waste);
1208                 gnome_print_clip (pi->pc);
1209                 break;
1210
1211         case GL_TEMPLATE_SHAPE_CD:
1212                 waste = label_type->waste;
1213                 if ((label_type->size.cd.h == 0) && (label_type->size.cd.w == 0)) {
1214                         /* CD style, round label w/ concentric round hole */
1215                         r1 = label_type->size.cd.r1;
1216                         create_ellipse_path (pi->pc, r1, r1, r1+waste, r1+waste);
1217                 } else {
1218                         /* Business Card CD style, clipped round label w/ hole */
1219                         gl_label_get_size (label, &w, &h);
1220                         r1 = label_type->size.cd.r1;
1221                         create_clipped_circle_path (pi->pc,
1222                                                     w/2, h/2,
1223                                                     w+2*waste, h+2*waste,
1224                                                     r1+waste);
1225                 }
1226                 gnome_print_clip (pi->pc);
1227                 break;
1228
1229         default:
1230                 g_warning ("Unknown template label style");
1231                 break;
1232         }
1233
1234         gl_debug (DEBUG_PRINT, "END");
1235 }
1236
1237 /*---------------------------------------------------------------------------*/
1238 /* PRIVATE.  Clip punchouts.  (Save some ink by not printing in CD holes)    */
1239 /*                                                                           */
1240 /* Ideally this would be done in clip_to_outline, but I am not sure how to   */
1241 /* invert the region for gnome_print_clip, so instead, I will just draw      */
1242 /* a white circle on top of everything else.                                 */
1243 /*---------------------------------------------------------------------------*/
1244 static void
1245 clip_punchouts (PrintInfo *pi,
1246                 glLabel   *label)
1247 {
1248         const glTemplateLabelType *label_type;
1249         gdouble                    w, h, r2;
1250         gdouble                    waste;
1251
1252         gl_debug (DEBUG_PRINT, "START");
1253
1254         label_type = gl_template_get_first_label_type (pi->template);
1255
1256         switch (label_type->shape) {
1257
1258         case GL_TEMPLATE_SHAPE_RECT:
1259         case GL_TEMPLATE_SHAPE_ROUND:
1260                 break;
1261
1262         case GL_TEMPLATE_SHAPE_CD:
1263                 gl_label_get_size (label, &w, &h);
1264                 waste = label_type->waste;
1265                 r2    = label_type->size.cd.r2;
1266                 create_ellipse_path (pi->pc, w/2, h/2, r2-waste, r2-waste);
1267                 gnome_print_setrgbcolor (pi->pc, 1.0, 1.0, 1.0);
1268                 gnome_print_setopacity (pi->pc, 1.0);
1269                 gnome_print_fill (pi->pc);
1270                 break;
1271
1272         default:
1273                 g_warning ("Unknown template label style");
1274                 break;
1275         }
1276
1277         gl_debug (DEBUG_PRINT, "END");
1278 }
1279
1280 /*---------------------------------------------------------------------------*/
1281 /* PRIVATE.  Path creation utilities.                                        */
1282 /*---------------------------------------------------------------------------*/
1283 static void
1284 create_rectangle_path (GnomePrintContext *pc,
1285                        gdouble            x0,
1286                        gdouble            y0,
1287                        gdouble            w,
1288                        gdouble            h)
1289 {
1290         gl_debug (DEBUG_PRINT, "START");
1291
1292         gnome_print_newpath (pc);
1293         gnome_print_moveto (pc, x0, y0);
1294         gnome_print_lineto (pc, x0 + w, y0);
1295         gnome_print_lineto (pc, x0 + w, y0 + h);
1296         gnome_print_lineto (pc, x0, y0 + h);
1297         gnome_print_lineto (pc, x0, y0);
1298         gnome_print_closepath (pc);
1299
1300         gl_debug (DEBUG_PRINT, "END");
1301 }
1302
1303 static void
1304 create_ellipse_path (GnomePrintContext *pc,
1305                      gdouble            x0,
1306                      gdouble            y0,
1307                      gdouble            rx,
1308                      gdouble            ry)
1309 {
1310         gdouble x, y;
1311         gint i_theta;
1312
1313         gl_debug (DEBUG_PRINT, "START");
1314
1315         gnome_print_newpath (pc);
1316         gnome_print_moveto (pc, x0 + rx, y0);
1317         for (i_theta = ARC_FINE; i_theta <= 360; i_theta += ARC_FINE) {
1318                 x = x0 + rx * cos (i_theta * G_PI / 180.0);
1319                 y = y0 + ry * sin (i_theta * G_PI / 180.0);
1320                 gnome_print_lineto (pc, x, y);
1321         }
1322         gnome_print_closepath (pc);
1323
1324         gl_debug (DEBUG_PRINT, "END");
1325 }
1326
1327 static void
1328 create_rounded_rectangle_path (GnomePrintContext *pc,
1329                                gdouble            x0,
1330                                gdouble            y0,
1331                                gdouble            w,
1332                                gdouble            h,
1333                                gdouble            r)
1334 {
1335         gdouble x, y;
1336         gint i_theta;
1337
1338         gl_debug (DEBUG_PRINT, "START");
1339
1340         gnome_print_newpath (pc);
1341
1342         gnome_print_moveto (pc, x0 + r, y0);
1343         for (i_theta = ARC_COURSE; i_theta <= 90; i_theta += ARC_COURSE) {
1344                 x = x0 + r - r * sin (i_theta * G_PI / 180.0);
1345                 y = y0 + r - r * cos (i_theta * G_PI / 180.0);
1346                 gnome_print_lineto (pc, x, y);
1347         }
1348         for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1349                 x = x0 + r - r * cos (i_theta * G_PI / 180.0);
1350                 y = y0 + (h - r) + r * sin (i_theta * G_PI / 180.0);
1351                 gnome_print_lineto (pc, x, y);
1352         }
1353         for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1354                 x = x0 + (w - r) + r * sin (i_theta * G_PI / 180.0);
1355                 y = y0 + (h - r) + r * cos (i_theta * G_PI / 180.0);
1356                 gnome_print_lineto (pc, x, y);
1357         }
1358         for (i_theta = 0; i_theta <= 90; i_theta += ARC_COURSE) {
1359                 x = x0 + (w - r) + r * cos (i_theta * G_PI / 180.0);
1360                 y = y0 + r - r * sin (i_theta * G_PI / 180.0);
1361                 gnome_print_lineto (pc, x, y);
1362         }
1363         gnome_print_lineto (pc, x0 + r, y0);
1364
1365         gnome_print_closepath (pc);
1366
1367         gl_debug (DEBUG_PRINT, "END");
1368 }
1369
1370 static void
1371 create_clipped_circle_path (GnomePrintContext *pc,
1372                             gdouble            x0,
1373                             gdouble            y0,
1374                             gdouble            w,
1375                             gdouble            h,
1376                             gdouble            r)
1377 {
1378         gdouble x, y;
1379         gdouble theta1, theta2;
1380         gint    i_theta;
1381
1382         gl_debug (DEBUG_PRINT, "START");
1383
1384         theta1 = (180.0/G_PI) * acos (w / (2.0*r));
1385         theta2 = (180.0/G_PI) * asin (h / (2.0*r));
1386
1387         gnome_print_newpath (pc);
1388
1389         x = x0 + r * cos (theta1 * G_PI / 180.0);
1390         y = y0 + r * sin (theta1 * G_PI / 180.0);
1391         gnome_print_moveto (pc, x, y);
1392
1393         for ( i_theta = theta1 + ARC_FINE; i_theta < theta2; i_theta +=ARC_FINE ) {
1394                 x = x0 + r * cos (i_theta * G_PI / 180.0);
1395                 y = y0 + r * sin (i_theta * G_PI / 180.0);
1396                 gnome_print_lineto (pc, x, y);
1397         }
1398
1399         x = x0 + r * cos (theta2 * G_PI / 180.0);
1400         y = y0 + r * sin (theta2 * G_PI / 180.0);
1401         gnome_print_lineto (pc, x, y);
1402
1403         if ( fabs (theta2 - 90.0) > GNOME_CANVAS_EPSILON ) {
1404                 x = x0 + r * cos ((180-theta2) * G_PI / 180.0);
1405                 y = y0 + r * sin ((180-theta2) * G_PI / 180.0);
1406                 gnome_print_lineto (pc, x, y);
1407         }
1408
1409         for ( i_theta = 180-theta2+ARC_FINE; i_theta < (180-theta1); i_theta +=ARC_FINE ) {
1410                 x = x0 + r * cos (i_theta * G_PI / 180.0);
1411                 y = y0 + r * sin (i_theta * G_PI / 180.0);
1412                 gnome_print_lineto (pc, x, y);
1413         }
1414
1415         x = x0 + r * cos ((180-theta1) * G_PI / 180.0);
1416         y = y0 + r * sin ((180-theta1) * G_PI / 180.0);
1417         gnome_print_lineto (pc, x, y);
1418
1419         if ( fabs (theta1) > GNOME_CANVAS_EPSILON ) {
1420                 x = x0 + r * cos ((180+theta1) * G_PI / 180.0);
1421                 y = y0 + r * sin ((180+theta1) * G_PI / 180.0);
1422                 gnome_print_lineto (pc, x, y);
1423         }
1424
1425         for ( i_theta = 180+theta1+ARC_FINE; i_theta < (180+theta2); i_theta +=ARC_FINE ) {
1426                 x = x0 + r * cos (i_theta * G_PI / 180.0);
1427                 y = y0 + r * sin (i_theta * G_PI / 180.0);
1428                 gnome_print_lineto (pc, x, y);
1429         }
1430
1431         x = x0 + r * cos ((180+theta2) * G_PI / 180.0);
1432         y = y0 + r * sin ((180+theta2) * G_PI / 180.0);
1433         gnome_print_lineto (pc, x, y);
1434
1435         if ( fabs (theta2 - 90.0) > GNOME_CANVAS_EPSILON ) {
1436                 x = x0 + r * cos ((360-theta2) * G_PI / 180.0);
1437                 y = y0 + r * sin ((360-theta2) * G_PI / 180.0);
1438                 gnome_print_lineto (pc, x, y);
1439         }
1440
1441         for ( i_theta = 360-theta2+ARC_FINE; i_theta < (360-theta1); i_theta +=ARC_FINE ) {
1442                 x = x0 + r * cos (i_theta * G_PI / 180.0);
1443                 y = y0 + r * sin (i_theta * G_PI / 180.0);
1444                 gnome_print_lineto (pc, x, y);
1445         }
1446
1447         if ( fabs (theta1) > GNOME_CANVAS_EPSILON ) {
1448                 x = x0 + r * cos ((360-theta1) * G_PI / 180.0);
1449                 y = y0 + r * sin ((360-theta1) * G_PI / 180.0);
1450                 gnome_print_lineto (pc, x, y);
1451         }
1452
1453         x = x0 + r * cos (theta1 * G_PI / 180.0);
1454         y = y0 + r * sin (theta1 * G_PI / 180.0);
1455         gnome_print_lineto (pc, x, y);
1456
1457         gnome_print_closepath (pc);
1458
1459         gl_debug (DEBUG_PRINT, "END");
1460 }
1461
1462 #ifndef NO_ALPHA_HACK
1463 /*---------------------------------------------------------------------------*/
1464 /* PRIVATE.  Extract a copy of rgba pixels, removing alpha by compositing    */
1465 /* with a white background.                                                  */
1466 /*                                                                           */
1467 /* This is currently needed due to the lousy job gnome-print does in         */
1468 /* rendering images with alpha channels to PS.  This sacrafices the ability  */
1469 /* to do compositing of images with other items in the background.           */
1470 /*---------------------------------------------------------------------------*/
1471 static guchar *
1472 get_pixels_as_rgb (const GdkPixbuf *pixbuf)
1473 {
1474         gint             bits_per_sample, channels;
1475         gboolean         has_alpha;
1476         gint             width, height, rowstride;
1477         gulong           bytes;
1478         guchar          *buf_src, *buf_dest;
1479         guchar          *p_src, *p_dest;
1480         gint             ix, iy;
1481         guchar           r, g, b, a;
1482         gdouble          alpha, beta;
1483
1484         gl_debug (DEBUG_PRINT, "START");
1485
1486         g_return_val_if_fail (pixbuf && GDK_IS_PIXBUF (pixbuf), NULL);
1487
1488         /* extract pixels and parameters from pixbuf. */
1489         buf_src         = gdk_pixbuf_get_pixels (pixbuf);
1490         bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);
1491         channels        = gdk_pixbuf_get_n_channels (pixbuf);
1492         has_alpha       = gdk_pixbuf_get_has_alpha (pixbuf);
1493         width           = gdk_pixbuf_get_width (pixbuf);
1494         height          = gdk_pixbuf_get_height (pixbuf);
1495         rowstride       = gdk_pixbuf_get_rowstride (pixbuf);
1496
1497         /* validate assumptions about pixbuf. */
1498         g_return_val_if_fail (buf_src, NULL);
1499         g_return_val_if_fail (bits_per_sample == 8, NULL);
1500         g_return_val_if_fail (channels == 4, NULL);
1501         g_return_val_if_fail (has_alpha, NULL);
1502         g_return_val_if_fail (width > 0, NULL);
1503         g_return_val_if_fail (height > 0, NULL);
1504         g_return_val_if_fail (rowstride > 0, NULL);
1505
1506         /* Allocate a destination buffer */
1507         bytes = height * rowstride;
1508         gl_debug (DEBUG_PRINT, "bytes = %d", bytes);
1509         buf_dest = g_try_malloc (bytes);
1510         if (!buf_dest) {
1511                 return NULL;
1512         }
1513         gl_debug (DEBUG_PRINT, "buf_dest = %x", buf_dest);
1514
1515         /* Copy pixels, transforming rgba to rgb by compositing with a white bg. */
1516         p_src  = buf_src;
1517         p_dest = buf_dest;
1518         for ( iy=0; iy < height; iy++ ) {
1519         
1520                 p_src  = buf_src + iy*rowstride;
1521                 p_dest = buf_dest + iy*rowstride;
1522
1523                 for ( ix=0; ix < width; ix++ ) {
1524
1525                         r = *p_src++;
1526                         g = *p_src++;
1527                         b = *p_src++;
1528                         a = *p_src++;
1529
1530                         alpha = a / 255.0;
1531                         beta  = 1.0 - alpha;
1532
1533                         *p_dest++ = (guchar) (alpha*r + beta*255 + 0.5);
1534                         *p_dest++ = (guchar) (alpha*g + beta*255 + 0.5);
1535                         *p_dest++ = (guchar) (alpha*b + beta*255 + 0.5);
1536
1537                 }
1538
1539         }
1540
1541         gl_debug (DEBUG_PRINT, "START");
1542
1543         return buf_dest;
1544 }
1545 #endif