source: trunk/third/gtkhtml/src/htmlprinter.c @ 18136

Revision 18136, 23.3 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18135, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2/* This file is part of the GtkHTML library.
3
4   Copyright (C) 2000 Helix Code, Inc.
5   
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Library General Public
8   License as published by the Free Software Foundation; either
9   version 2 of the License, or (at your option) any later version.
10   
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Library General Public License for more details.
15   
16   You should have received a copy of the GNU Library General Public License
17   along with this library; see the file COPYING.LIB.  If not, write to
18   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.
20*/
21
22#include <config.h>
23#include "gtkhtml-compat.h"
24
25#include <gnome.h>
26#include <ctype.h>
27#include <gal/unicode/gunicode.h>
28
29#include "htmlembedded.h"
30#include "gtkhtml-embedded.h"
31#include "htmlfontmanager.h"
32#include "htmlprinter.h"
33
34/* #define PRINTER_DEBUG */
35
36
37static HTMLPainterClass *parent_class = NULL;
38
39
40/* The size of a pixel in the printed output, in points.  */
41#define PIXEL_SIZE .5
42
43/* Hm, this might need fixing.  */
44#define SPACING_FACTOR 1.2
45
46
47/* This is just a quick hack until GnomePrintContext is fixed to hold the paper
48   size.  */
49
50static const GnomePaper *paper = NULL;
51
52static void
53insure_paper (HTMLPrinter *printer)
54{
55        if (paper != NULL)
56                return;
57
58        if (printer->print_master) {
59                paper = gnome_print_master_get_paper (printer->print_master);
60        }
61        if (!paper)
62                paper = gnome_paper_with_name (_("US-Letter"));
63        if (!paper)
64                paper = gnome_paper_with_name (gnome_paper_name_default ());
65        g_assert (paper != NULL);
66}
67
68static gdouble
69printer_get_page_height (HTMLPrinter *printer)
70{
71        insure_paper (printer);
72        return gnome_paper_psheight (paper) / printer->scale;
73}
74
75static gdouble
76printer_get_page_width (HTMLPrinter *printer)
77{
78        insure_paper (printer);
79        return gnome_paper_pswidth (paper) / printer->scale;
80}
81
82static gdouble
83get_lmargin (HTMLPrinter *printer)
84{
85        insure_paper (printer);
86        return gnome_paper_lmargin (paper) / 2;
87}
88
89static gdouble
90get_rmargin (HTMLPrinter *printer)
91{
92        insure_paper (printer);
93        return gnome_paper_rmargin (paper) / 2;
94}
95
96static gdouble
97get_tmargin (HTMLPrinter *printer)
98{
99        insure_paper (printer);
100        return gnome_paper_tmargin (paper) / 2;
101}
102
103static gdouble
104get_bmargin (HTMLPrinter *printer)
105{
106        insure_paper (printer);
107        return gnome_paper_bmargin (paper) / 2;
108}
109
110gdouble
111html_printer_scale_to_gnome_print (HTMLPrinter *printer, gint x)
112{
113        return SCALE_ENGINE_TO_GNOME_PRINT (x);
114}
115
116void
117html_printer_coordinates_to_gnome_print (HTMLPrinter *printer,
118                                         gint engine_x, gint engine_y,
119                                         gdouble *print_x_return, gdouble *print_y_return)
120{
121        gdouble print_x, print_y;
122
123        print_x = SCALE_ENGINE_TO_GNOME_PRINT (engine_x);
124        print_y = SCALE_ENGINE_TO_GNOME_PRINT (engine_y);
125
126        print_x = print_x + get_lmargin (printer)/printer->scale;
127        print_y = (printer_get_page_height (printer) - print_y) - get_tmargin (printer)/printer->scale;
128
129        *print_x_return = print_x;
130        *print_y_return = print_y;
131}
132
133#if 0
134static void
135gnome_print_coordinates_to_engine (HTMLPrinter *printer,
136                                   gdouble print_x, gdouble print_y,
137                                   gint *engine_x_return, gint *engine_y_return)
138{
139        print_x -= get_lmargin (printer);
140        print_y -= get_bmargin (printer);
141
142        *engine_x_return = SCALE_ENGINE_TO_GNOME_PRINT (print_x);
143        *engine_y_return = SCALE_GNOME_PRINT_TO_ENGINE (get_page_height (printer) - print_y);
144}
145#endif
146
147
148/* GtkObject methods.  */
149
150static void
151finalize (GtkObject *object)
152{
153        HTMLPrinter *printer;
154
155        printer = HTML_PRINTER (object);
156
157        if (printer->print_context != NULL) {
158                gnome_print_context_close (printer->print_context);
159                gtk_object_unref (GTK_OBJECT (printer->print_context));
160        }
161
162        (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
163}
164
165
166static void
167begin (HTMLPainter *painter,
168       int x1, int y1,
169       int x2, int y2)
170{
171        HTMLPrinter *printer;
172        GnomePrintContext *pc;
173        gdouble printer_x1, printer_y1;
174        gdouble printer_x2, printer_y2;
175#ifdef PRINTER_DEBUG
176        gdouble dash [2];
177#endif
178        printer = HTML_PRINTER (painter);
179        g_return_if_fail (printer);
180        pc      = printer->print_context;
181        g_return_if_fail (pc);
182
183        gnome_print_beginpage (pc, "page");
184        gnome_print_gsave (pc);
185        gnome_print_scale (pc, printer->scale, printer->scale);
186
187        html_printer_coordinates_to_gnome_print (printer, x1, y1, &printer_x1, &printer_y1);
188        printer_x2 = printer_x1 + SCALE_ENGINE_TO_GNOME_PRINT (x2);
189        printer_y2 = printer_y1 - SCALE_ENGINE_TO_GNOME_PRINT (y2);
190
191        gnome_print_newpath (pc);
192
193        gnome_print_moveto (pc, printer_x1, printer_y1);
194        gnome_print_lineto (pc, printer_x1, printer_y2);
195        gnome_print_lineto (pc, printer_x2, printer_y2);
196        gnome_print_lineto (pc, printer_x2, printer_y1);
197        gnome_print_lineto (pc, printer_x1, printer_y1);
198        gnome_print_closepath (pc);
199
200#ifdef PRINTER_DEBUG
201        gnome_print_gsave (pc);
202        dash [0] = 10.0;
203        dash [1] = 10.0;
204        gnome_print_setrgbcolor (pc, .5, .5, .5);
205        gnome_print_setlinewidth (pc, .3);
206        gnome_print_setdash (pc, 2, dash, .0);
207        gnome_print_stroke (pc);
208        gnome_print_grestore (pc);
209#endif
210        gnome_print_clip (pc);
211}
212
213static void
214end (HTMLPainter *painter)
215{
216        HTMLPrinter *printer;
217
218        printer = HTML_PRINTER (painter);
219        g_return_if_fail (printer->print_context != NULL);
220
221        gnome_print_grestore (printer->print_context);
222        gnome_print_showpage (printer->print_context);
223}
224
225static void
226clear (HTMLPainter *painter)
227{
228}
229
230
231static void
232alloc_color (HTMLPainter *painter, GdkColor *color)
233{
234}
235
236static void
237free_color (HTMLPainter *painter, GdkColor *color)
238{
239}
240
241
242static void
243set_pen (HTMLPainter *painter,
244         const GdkColor *color)
245{
246        HTMLPrinter *printer;
247
248        printer = HTML_PRINTER (painter);
249        g_return_if_fail (printer->print_context != NULL);
250
251        gnome_print_setrgbcolor (printer->print_context,
252                                 color->red / 65535.0, color->green / 65535.0, color->blue / 65535.0);
253}
254
255static const GdkColor *
256get_black (const HTMLPainter *painter)
257{
258        static GdkColor black = { 0, 0, 0, 0 };
259
260        return &black;
261}
262
263static void
264prepare_rectangle (HTMLPainter *painter, gint _x, gint _y, gint w, gint h)
265{
266        HTMLPrinter *printer = HTML_PRINTER (painter);
267        GnomePrintContext *context = printer->print_context;
268        gdouble x;
269        gdouble y;
270        gdouble width;
271        gdouble height;
272
273        width = SCALE_ENGINE_TO_GNOME_PRINT (w);
274        height = SCALE_ENGINE_TO_GNOME_PRINT (h);
275
276        html_printer_coordinates_to_gnome_print (HTML_PRINTER (painter), _x, _y, &x, &y);
277
278        gnome_print_newpath (context);
279        gnome_print_moveto  (context, x, y);
280        gnome_print_lineto  (context, x + width, y);
281        gnome_print_lineto  (context, x + width, y - height);
282        gnome_print_lineto  (context, x, y - height);
283        gnome_print_lineto  (context, x, y);
284        gnome_print_closepath (context);
285}
286
287static void
288do_rectangle (HTMLPainter *painter, gint x, gint y, gint w, gint h, gint lw)
289{
290        HTMLPrinter *printer = HTML_PRINTER (painter);
291        GnomePrintContext *context = printer->print_context;
292
293        gnome_print_setlinewidth (context, SCALE_ENGINE_TO_GNOME_PRINT (lw) * PIXEL_SIZE);
294        prepare_rectangle (painter, x, y, w, h);
295        gnome_print_stroke (context);
296}
297
298static void
299set_clip_rectangle (HTMLPainter *painter,
300                    gint x, gint y,
301                    gint width, gint height)
302{
303        prepare_rectangle (painter, x, y, width, height);
304        gnome_print_clip (HTML_PRINTER (painter)->print_context);
305}
306
307/* HTMLPainter drawing functions.  */
308
309static void
310draw_line (HTMLPainter *painter,
311           gint x1, gint y1,
312           gint x2, gint y2)
313{
314        HTMLPrinter *printer;
315        double printer_x1, printer_y1;
316        double printer_x2, printer_y2;
317
318        printer = HTML_PRINTER (painter);
319        g_return_if_fail (printer->print_context != NULL);
320
321        html_printer_coordinates_to_gnome_print (printer, x1, y1, &printer_x1, &printer_y1);
322        html_printer_coordinates_to_gnome_print (printer, x2, y2, &printer_x2, &printer_y2);
323
324        gnome_print_setlinewidth (printer->print_context, PIXEL_SIZE);
325
326        gnome_print_newpath (printer->print_context);
327        gnome_print_moveto (printer->print_context, printer_x1, printer_y1);
328        gnome_print_lineto (printer->print_context, printer_x2, printer_y2);
329
330        gnome_print_stroke (printer->print_context);
331}
332
333static void
334draw_rect (HTMLPainter *painter,
335           gint x, gint y,
336           gint width, gint height)
337{
338        do_rectangle (painter, x, y, width, height, 1);
339}
340
341static void
342draw_panel (HTMLPainter *painter,
343            GdkColor *bg,
344            gint _x, gint _y,
345            gint w, gint h,
346            GtkHTMLEtchStyle inset,
347            gint bordersize)
348{
349        HTMLPrinter *printer = HTML_PRINTER (painter);
350        GnomePrintContext *pc = printer->print_context;
351        GdkColor *col1 = NULL, *col2 = NULL;
352        GdkColor dark, light;
353        gdouble x;
354        gdouble y;
355        gdouble width, bs;
356        gdouble height;
357
358        #define INC 0x8000
359        #define DARK(c)  dark.c = MAX (((gint) bg->c) - INC, 0)
360        #define LIGHT(c) light.c = MIN (((gint) bg->c) + INC, 0xffff)
361
362        DARK(red);
363        DARK(green);
364        DARK(blue);
365        LIGHT(red);
366        LIGHT(green);
367        LIGHT(blue);
368
369        switch (inset) {
370        case GTK_HTML_ETCH_NONE:
371                /* use the current pen color */
372                col1 = NULL;
373                col2 = NULL;
374                break;
375        case GTK_HTML_ETCH_OUT:
376                col1 = &light;
377                col2 = &dark;
378                break;
379        default:
380        case GTK_HTML_ETCH_IN:
381                col1 = &dark;
382                col2 = &light;
383                break;
384        }
385
386        width  = SCALE_ENGINE_TO_GNOME_PRINT (w);
387        height = SCALE_ENGINE_TO_GNOME_PRINT (h);
388        bs     = SCALE_ENGINE_TO_GNOME_PRINT (bordersize);
389
390        html_printer_coordinates_to_gnome_print (HTML_PRINTER (painter), _x, _y, &x, &y);
391
392        if (col2)
393                gnome_print_setrgbcolor (pc, col1->red / 65535.0, col1->green / 65535.0, col1->blue / 65535.0);
394
395        gnome_print_newpath (pc);
396        gnome_print_moveto  (pc, x, y);
397        gnome_print_lineto  (pc, x + width, y);
398        gnome_print_lineto  (pc, x + width - bs, y - bs);
399        gnome_print_lineto  (pc, x + bs, y - bs );
400        gnome_print_lineto  (pc, x + bs, y - height + bs);
401        gnome_print_lineto  (pc, x, y - height);
402        gnome_print_closepath (pc);
403        gnome_print_fill    (pc);
404
405        if (col1)
406                gnome_print_setrgbcolor (pc, col2->red / 65535.0, col2->green / 65535.0, col2->blue / 65535.0);
407
408        gnome_print_newpath (pc);
409        gnome_print_moveto  (pc, x, y - height);
410        gnome_print_lineto  (pc, x + width, y - height);
411        gnome_print_lineto  (pc, x + width, y);
412        gnome_print_lineto  (pc, x + width - bs, y - bs);
413        gnome_print_lineto  (pc, x + width - bs, y - height + bs);
414        gnome_print_lineto  (pc, x + bs, y - height + bs);
415        gnome_print_closepath (pc);
416        gnome_print_fill    (pc);
417}
418
419static void
420draw_background (HTMLPainter *painter,
421                 GdkColor *color,
422                 GdkPixbuf *pixbuf,
423                 gint ix, gint iy,
424                 gint pix_width, gint pix_height,
425                 gint tile_x, gint tile_y)
426{
427        GnomePrintContext *pc;
428        HTMLPrinter *printer;
429        gdouble x, y, width, height;
430
431        printer = HTML_PRINTER (painter);
432        g_return_if_fail (printer);
433        pc = printer->print_context;
434        g_return_if_fail (printer->print_context);
435
436        width = SCALE_ENGINE_TO_GNOME_PRINT  (pix_width);
437        height = SCALE_ENGINE_TO_GNOME_PRINT (pix_height);
438        html_printer_coordinates_to_gnome_print (printer, ix, iy, &x, &y);
439
440        if (color) {
441                gnome_print_setrgbcolor (pc, color->red / 65535.0, color->green / 65535.0, color->blue / 65535.0);
442
443                gnome_print_newpath (pc);
444                gnome_print_moveto (pc, x, y);
445                gnome_print_lineto (pc, x + width, y);
446                gnome_print_lineto (pc, x + width, y - height);
447                gnome_print_lineto (pc, x, y - height);
448                gnome_print_lineto (pc, x, y);
449                gnome_print_closepath (pc);
450
451                gnome_print_fill (pc);
452        }
453}
454
455static void
456draw_pixmap (HTMLPainter *painter,
457             GdkPixbuf *pixbuf,
458             gint x, gint y,
459             gint scale_width, gint scale_height,
460             const GdkColor *color)
461{
462        HTMLPrinter *printer;
463        gint width, height;
464        double print_x, print_y;
465        double print_scale_width, print_scale_height;
466
467        printer = HTML_PRINTER (painter);
468        g_return_if_fail (printer->print_context != NULL);
469
470        width = gdk_pixbuf_get_width (pixbuf);
471        height = gdk_pixbuf_get_height (pixbuf);
472
473        html_printer_coordinates_to_gnome_print (printer, x, y, &print_x, &print_y);
474
475        print_scale_width  = SCALE_ENGINE_TO_GNOME_PRINT (scale_width);
476        print_scale_height = SCALE_ENGINE_TO_GNOME_PRINT (scale_height);
477
478        gnome_print_gsave (printer->print_context);
479        gnome_print_translate (printer->print_context, print_x, print_y - print_scale_height);
480        gnome_print_scale (printer->print_context, print_scale_width, print_scale_height);
481        gnome_print_pixbuf (printer->print_context, pixbuf);
482        gnome_print_grestore (printer->print_context);
483}
484
485static void
486fill_rect (HTMLPainter *painter,
487           gint x, gint y,
488           gint width, gint height)
489{
490        HTMLPrinter *printer;
491        double printer_x, printer_y;
492        double printer_width, printer_height;
493
494        printer = HTML_PRINTER (painter);
495        g_return_if_fail (printer->print_context != NULL);
496
497        printer_width = SCALE_ENGINE_TO_GNOME_PRINT (width);
498        printer_height = SCALE_ENGINE_TO_GNOME_PRINT (height);
499
500        html_printer_coordinates_to_gnome_print (printer, x, y, &printer_x, &printer_y);
501
502        gnome_print_newpath (printer->print_context);
503        gnome_print_moveto (printer->print_context, printer_x, printer_y);
504        gnome_print_lineto (printer->print_context, printer_x + printer_width, printer_y);
505        gnome_print_lineto (printer->print_context, printer_x + printer_width, printer_y - printer_height);
506        gnome_print_lineto (printer->print_context, printer_x, printer_y - printer_height);
507        gnome_print_lineto (printer->print_context, printer_x, printer_y);
508        gnome_print_closepath (printer->print_context);
509
510        gnome_print_fill (printer->print_context);
511}
512
513static void
514draw_text (HTMLPainter *painter,
515           gint x, gint y,
516           const gchar *text,
517           gint len)
518{
519        GnomeFont *font;
520        HTMLPrinter *printer;
521        gdouble print_x, print_y;
522
523        printer = HTML_PRINTER (painter);
524        g_return_if_fail (printer->print_context != NULL);
525
526        html_printer_coordinates_to_gnome_print (printer, x, y, &print_x, &print_y);
527
528        gnome_print_newpath (printer->print_context);
529        gnome_print_moveto (printer->print_context, print_x, print_y);
530
531        font = html_painter_get_font (painter, painter->font_face, painter->font_style);
532        gnome_print_setfont (printer->print_context, font);
533        gnome_print_show_sized (printer->print_context, text, g_utf8_offset_to_pointer (text, len) - text);
534
535        if (painter->font_style & (GTK_HTML_FONT_STYLE_UNDERLINE | GTK_HTML_FONT_STYLE_STRIKEOUT)) {
536                double text_width;
537                double ascender, descender;
538                double y;
539
540                gnome_print_gsave (printer->print_context);
541
542                /* FIXME: We need something in GnomeFont to do this right.  */
543                gnome_print_setlinewidth (printer->print_context, 1.0);
544                gnome_print_setlinecap (printer->print_context, GDK_CAP_BUTT);
545
546                text_width = gnome_font_get_width_utf8_sized (font, text, len);
547                if (painter->font_style & GTK_HTML_FONT_STYLE_UNDERLINE) {
548                        descender = gnome_font_get_descender (font);
549                        y = print_y + gnome_font_get_underline_position (font);
550
551                        gnome_print_newpath (printer->print_context);
552                        gnome_print_moveto (printer->print_context, print_x, y);
553                        gnome_print_lineto (printer->print_context, print_x + text_width, y);
554                        gnome_print_setlinewidth (printer->print_context,
555                                                  gnome_font_get_underline_thickness (font));
556                        gnome_print_stroke (printer->print_context);
557                }
558
559                if (painter->font_style & GTK_HTML_FONT_STYLE_STRIKEOUT) {
560                        ascender = gnome_font_get_ascender (font);
561                        y = print_y + ascender / 2.0;
562                        gnome_print_newpath (printer->print_context);
563                        gnome_print_moveto (printer->print_context, print_x, y);
564                        gnome_print_lineto (printer->print_context, print_x + text_width, y);
565                        gnome_print_setlinewidth (printer->print_context,
566                                                  gnome_font_get_underline_thickness (font));
567                        gnome_print_stroke (printer->print_context);
568                }
569
570                gnome_print_grestore (printer->print_context);
571        }
572}
573
574static void
575draw_embedded (HTMLPainter *p, HTMLEmbedded *o, gint x, gint y)
576{
577        gdouble print_x, print_y;       
578        HTMLPrinter *printer = HTML_PRINTER(p);
579        GtkWidget *embedded_widget;
580
581        html_printer_coordinates_to_gnome_print (printer, x, y, &print_x, &print_y);
582        gnome_print_gsave(printer->print_context);
583
584        gnome_print_translate(printer->print_context,
585                              print_x, print_y - o->height * PIXEL_SIZE);
586 
587        embedded_widget = html_embedded_get_widget(o);
588        if (embedded_widget && GTK_IS_HTML_EMBEDDED (embedded_widget)) {
589                gtk_signal_emit_by_name(GTK_OBJECT (embedded_widget),
590                                        "draw_print",
591                                        printer->print_context);
592        }
593
594        gnome_print_grestore(printer->print_context);
595}
596
597static void
598draw_shade_line (HTMLPainter *painter,
599                 gint x, gint y,
600                 gint width)
601{
602        HTMLPrinter *printer;
603
604        printer = HTML_PRINTER (painter);
605        g_return_if_fail (printer->print_context != NULL);
606
607        /* FIXME */
608}
609
610static guint
611calc_ascent (HTMLPainter *painter,
612             GtkHTMLFontStyle style,
613             HTMLFontFace *face)
614{
615        HTMLPrinter *printer;
616        GnomeFont *font;
617        double ascender;
618
619        printer = HTML_PRINTER (painter);
620        g_return_val_if_fail (printer->print_context != NULL, 0);
621
622        font = html_painter_get_font (painter, face, style);
623        g_return_val_if_fail (font != NULL, 0);
624
625        ascender = gnome_font_get_ascender (font) * SPACING_FACTOR;
626        return SCALE_GNOME_PRINT_TO_ENGINE (ascender);
627}
628
629static guint
630calc_descent (HTMLPainter *painter,
631              GtkHTMLFontStyle style,
632              HTMLFontFace *face)
633{
634        HTMLPrinter *printer;
635        GnomeFont *font;
636        double descender;
637
638        printer = HTML_PRINTER (painter);
639        g_return_val_if_fail (printer->print_context != NULL, 0);
640
641        font = html_painter_get_font (painter, face, style);
642        g_return_val_if_fail (font != NULL, 0);
643
644        descender = gnome_font_get_descender (font) * SPACING_FACTOR;
645        return SCALE_GNOME_PRINT_TO_ENGINE (descender);
646}
647
648static guint
649calc_text_width (HTMLPainter *painter,
650                 const gchar *text,
651                 guint len,
652                 GtkHTMLFontStyle style,
653                 HTMLFontFace *face)
654{
655        HTMLPrinter *printer;
656        GnomeFont *font;
657        double width;
658
659        printer = HTML_PRINTER (painter);
660        g_return_val_if_fail (printer->print_context != NULL, 0);
661
662        font = html_painter_get_font (painter, face, style);
663        g_return_val_if_fail (font != NULL, 0);
664
665        width = gnome_font_get_width_utf8_sized (font, text, g_utf8_offset_to_pointer (text, len) - text);
666
667        return SCALE_GNOME_PRINT_TO_ENGINE (width);
668}
669
670static guint
671calc_text_width_bytes (HTMLPainter *painter,
672                       const gchar *text,
673                       guint len,
674                       HTMLFont *font,
675                       GtkHTMLFontStyle style)
676{
677        HTMLPrinter *printer;
678        double width;
679
680        printer = HTML_PRINTER (painter);
681        g_return_val_if_fail (printer->print_context != NULL, 0);
682        g_return_val_if_fail (font != NULL, 0);
683
684        width = gnome_font_get_width_utf8_sized (font->data, text, len);
685
686        return SCALE_GNOME_PRINT_TO_ENGINE (width);
687}
688
689static guint
690get_pixel_size (HTMLPainter *painter)
691{
692        HTMLPrinter *printer = HTML_PRINTER (painter);
693
694        return SCALE_GNOME_PRINT_TO_ENGINE (PIXEL_SIZE);
695}
696
697static inline gdouble
698get_font_size (gboolean points, gdouble size)
699{
700        return points ? size / 10 : size;
701}
702
703static HTMLFont *
704alloc_font (gchar *face, gdouble size, gboolean points, GtkHTMLFontStyle style)
705{
706        GnomeFontWeight weight;
707        GnomeFont *font;
708        gboolean italic;
709        gchar *family = NULL;
710
711        weight = (style & GTK_HTML_FONT_STYLE_BOLD) ? GNOME_FONT_BOLD : GNOME_FONT_BOOK;
712        italic = (style & GTK_HTML_FONT_STYLE_ITALIC);
713
714        /* gnome-print is case sensitive - need to be fixed */
715        if (face && *face) {
716                gchar *s;
717
718                s = family = html_font_manager_get_attr (face, 2);
719
720                /* capitalize */
721                *s = toupper (*s);
722                s++;
723                while (*s) {
724                        *s = tolower (*s);
725                        s++;
726                }
727        }
728
729        font = gnome_font_new_closest (family ? family : (style & GTK_HTML_FONT_STYLE_FIXED ? "Courier" : "Helvetica"),
730                                       weight, italic, get_font_size (points, size));
731        g_free (family);
732
733        if (font == NULL) {
734                GList *family_list;
735
736                family_list = gnome_font_family_list ();
737                if (family_list && family_list->data) {
738                        font = gnome_font_new_closest (family_list->data,
739                                                       weight, italic, get_font_size (points, size));
740                        gnome_font_family_list_free (family_list);
741                }
742        }
743
744        return font ? html_font_new (font,
745                                     SCALE_GNOME_PRINT_FONT_TO_ENGINE (gnome_font_get_width_utf8_sized (font, " ", 1)),
746                                     SCALE_GNOME_PRINT_FONT_TO_ENGINE (gnome_font_get_width_utf8_sized (font, "\xc2\xa0", 2)),
747                                     SCALE_GNOME_PRINT_FONT_TO_ENGINE (gnome_font_get_width_utf8_sized (font, "\t", 1)))
748                : NULL;
749}
750
751static void
752ref_font (HTMLFont *font)
753{
754        gtk_object_ref (GTK_OBJECT (font->data));
755}
756
757static void
758unref_font (HTMLFont *font)
759{
760        gtk_object_unref (GTK_OBJECT (font->data));
761}
762
763static guint
764get_page_width (HTMLPainter *painter, HTMLEngine *e)
765{
766        return html_printer_get_page_width (HTML_PRINTER (painter));
767}
768
769static guint
770get_page_height (HTMLPainter *painter, HTMLEngine *e)
771{
772        return html_printer_get_page_height (HTML_PRINTER (painter));
773}
774
775static HTMLFontManagerId
776get_font_manager_id ()
777{
778        return HTML_FONT_MANAGER_ID_PRINTER;
779}
780
781static void
782init (GtkObject *object)
783{
784        HTMLPrinter *printer;
785
786        printer                = HTML_PRINTER (object);
787        printer->print_context = NULL;
788        printer->scale         = 1.0;
789}
790
791static void
792class_init (GtkObjectClass *object_class)
793{
794        HTMLPainterClass *painter_class;
795
796        painter_class = HTML_PAINTER_CLASS (object_class);
797
798        object_class->finalize = finalize;
799
800        painter_class->begin = begin;
801        painter_class->end = end;
802        painter_class->alloc_font = alloc_font;
803        painter_class->ref_font   = ref_font;
804        painter_class->unref_font = unref_font;
805        painter_class->alloc_color = alloc_color;
806        painter_class->free_color = free_color;
807        painter_class->calc_ascent = calc_ascent;
808        painter_class->calc_descent = calc_descent;
809        painter_class->calc_text_width = calc_text_width;
810        painter_class->calc_text_width_bytes = calc_text_width_bytes;
811        painter_class->set_pen = set_pen;
812        painter_class->get_black = get_black;
813        painter_class->draw_line = draw_line;
814        painter_class->draw_rect = draw_rect;
815        painter_class->draw_panel = draw_panel;
816        painter_class->draw_text = draw_text;
817        painter_class->fill_rect = fill_rect;
818        painter_class->draw_pixmap = draw_pixmap;
819        painter_class->clear = clear;
820        painter_class->draw_shade_line = draw_shade_line;
821        painter_class->draw_background = draw_background;
822        painter_class->get_pixel_size = get_pixel_size;
823        painter_class->set_clip_rectangle = set_clip_rectangle;
824        painter_class->draw_embedded = draw_embedded;
825        painter_class->get_page_width = get_page_width;
826        painter_class->get_page_height = get_page_height;
827        painter_class->get_font_manager_id = get_font_manager_id;
828
829        parent_class = gtk_type_class (html_painter_get_type ());
830}
831
832GtkType
833html_printer_get_type (void)
834{
835        static GtkType type = 0;
836
837        if (type == 0) {
838                static const GtkTypeInfo info = {
839                        "HTMLPrinter",
840                        sizeof (HTMLPrinter),
841                        sizeof (HTMLPrinterClass),
842                        (GtkClassInitFunc) class_init,
843                        (GtkObjectInitFunc) init,
844                        /* reserved_1 */ NULL,
845                        /* reserved_2 */ NULL,
846                        (GtkClassInitFunc) NULL,
847                };
848
849                type = gtk_type_unique (HTML_TYPE_PAINTER, &info);
850        }
851
852        return type;
853}
854
855
856HTMLPainter *
857html_printer_new (GnomePrintContext *print_context, GnomePrintMaster *print_master)
858{
859        HTMLPrinter *new;
860
861        new = gtk_type_new (html_printer_get_type ());
862
863        gtk_object_ref (GTK_OBJECT (print_context));
864        new->print_context = print_context;
865        new->print_master = print_master;
866
867        return HTML_PAINTER (new);
868}
869
870
871guint
872html_printer_get_page_width (HTMLPrinter *printer)
873{
874        double printer_width;
875        guint engine_width;
876
877        g_return_val_if_fail (printer != NULL, 0);
878        g_return_val_if_fail (HTML_IS_PRINTER (printer), 0);
879
880        printer_width = printer_get_page_width (printer) - get_lmargin (printer) - get_rmargin (printer);
881        engine_width = SCALE_GNOME_PRINT_TO_ENGINE (printer_width);
882
883        return engine_width;
884}
885
886guint
887html_printer_get_page_height (HTMLPrinter *printer)
888{
889        double printer_height;
890        guint engine_height;
891
892        g_return_val_if_fail (printer != NULL, 0);
893        g_return_val_if_fail (HTML_IS_PRINTER (printer), 0);
894
895        printer_height = printer_get_page_height (printer) - get_tmargin (printer) - get_bmargin (printer);
896        engine_height = SCALE_GNOME_PRINT_TO_ENGINE (printer_height);
897
898        return engine_height;
899}
Note: See TracBrowser for help on using the repository browser.