source: trunk/third/gtkhtml3/src/htmlengine-print.c @ 21116

Revision 21116, 7.4 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21115, 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 <gtk/gtk.h>
24#include <libgnome/gnome-i18n.h>
25#include <libgnomeui/gnome-dialog-util.h>
26#include "gtkhtml.h"
27#include "gtkhtml-private.h"
28#include "gtkhtml-properties.h"
29#include "htmlprinter.h"
30#include "htmlengine-print.h"
31#include "htmlobject.h"
32
33
34
35/* #define CLIP_DEBUG */
36
37static void
38print_header_footer (HTMLPainter *painter, HTMLEngine *engine, gint width, gint y, gdouble height,
39                     GtkHTMLPrintCallback cb, gpointer user_data)
40{
41        HTMLPrinter *printer = HTML_PRINTER (painter);
42        GnomePrintContext *context = printer->context;
43        gdouble gx, gy;
44
45        gnome_print_gsave (context);
46        html_painter_set_clip_rectangle (painter, 0, y, width, SCALE_GNOME_PRINT_TO_ENGINE (height));
47#ifdef CLIP_DEBUG
48        html_painter_draw_rect (painter, 0, y, width, SCALE_GNOME_PRINT_TO_ENGINE (height));
49#endif
50        html_printer_coordinates_to_gnome_print (printer, 0, y, &gx, &gy);
51        (*cb) (GTK_HTML (engine->widget), context, gx, gy, SCALE_ENGINE_TO_GNOME_PRINT (width), height, user_data);
52        gnome_print_grestore (context);
53}
54
55static void
56print_page (HTMLPainter *painter,
57            HTMLEngine *engine,
58            gint start_y,
59            gint page_width, gint page_height, gint body_height,
60            gdouble header_height, gdouble footer_height,
61            GtkHTMLPrintCallback header_print, GtkHTMLPrintCallback footer_print, gpointer user_data)
62{
63        HTMLPrinter *printer = HTML_PRINTER (painter);
64        GnomePrintContext *context = printer->context;
65
66        html_painter_begin (painter, 0, 0, page_width, page_height);
67        if (header_print)
68                print_header_footer (painter, engine, page_width, 0, header_height, header_print, user_data);
69        gnome_print_gsave (context);
70        html_painter_set_clip_rectangle (painter, 0, header_height, page_width, body_height);
71#ifdef CLIP_DEBUG
72        html_painter_draw_rect (painter, 0, header_height, page_width, body_height);
73#endif
74        html_object_draw (engine->clue, painter,
75                          0, start_y,
76                          page_width, body_height,
77                          0, -start_y + header_height);
78        gnome_print_grestore (context);
79        if (footer_print)
80                print_header_footer (painter, engine, page_width, page_height - SCALE_GNOME_PRINT_TO_ENGINE (footer_height),
81                                     footer_height, footer_print, user_data);
82        html_painter_end (painter);
83}
84
85static gint
86print_all_pages (HTMLPainter *painter,
87                 HTMLEngine *engine,
88                 gdouble header_height, gdouble footer_height,
89                 GtkHTMLPrintCallback header_print, GtkHTMLPrintCallback footer_print, gpointer user_data, gboolean do_print)
90{
91        HTMLPrinter *printer = HTML_PRINTER (painter);
92        gint new_split_offset, split_offset;
93        gint page_width, page_height, body_height;
94        gint document_height;
95        gint pages = 0;
96
97        page_height = html_printer_get_page_height (printer);
98        page_width  = html_printer_get_page_width (printer);
99
100        if (header_height + footer_height >= page_height) {
101                header_print = footer_print = NULL;
102                g_warning ("Page header height + footer height >= page height, disabling header/footer printing");
103        }
104
105        body_height = page_height - SCALE_GNOME_PRINT_TO_ENGINE (header_height + footer_height);
106        split_offset = 0;
107
108        document_height = html_engine_get_doc_height (engine);
109
110        do {
111                pages ++;
112                new_split_offset = html_object_check_page_split (engine->clue, painter,
113                                                                 split_offset + body_height);
114
115                if (new_split_offset <= split_offset
116                    || new_split_offset - split_offset < engine->min_split_index * body_height)
117                        new_split_offset = split_offset + body_height;
118
119                if (do_print)
120                        print_page   (painter, engine, split_offset,
121                                      page_width, page_height,
122                                      new_split_offset - split_offset,
123                                      header_height, footer_height, header_print, footer_print, user_data);
124
125                split_offset = new_split_offset;
126        }  while (split_offset < document_height);
127
128        return pages;
129}
130
131static gboolean
132do_we_have_default_font (HTMLPainter *painter)
133{
134        HTMLFont *font;
135        gboolean rv = FALSE;
136
137        font = html_painter_get_font (painter, NULL, GTK_HTML_FONT_STYLE_DEFAULT);
138        if (font) {
139                rv = TRUE;
140        }
141
142        return rv;
143}
144
145static gint
146print_with_header_footer (HTMLEngine *engine,
147                                      GnomePrintContext *print_context,
148                                      gdouble header_height,
149                                      gdouble footer_height,
150                                      GtkHTMLPrintCallback header_print,
151                                      GtkHTMLPrintCallback footer_print,
152                                      gpointer user_data, gboolean do_print)
153{
154        HTMLPainter *printer;
155        HTMLPainter *old_painter;
156        gint pages = 0;
157
158        g_return_val_if_fail (engine->clue != NULL, 0);
159
160        printer = html_printer_new (GTK_WIDGET (engine->widget), print_context, GTK_HTML (engine->widget)->priv->print_master);
161        gtk_html_set_fonts (engine->widget, printer);
162
163        if (do_we_have_default_font (printer)) {
164                gint min_width, page_width;
165
166                old_painter = engine->painter;
167
168                g_object_ref (old_painter);
169                html_engine_set_painter (engine, printer);
170
171                min_width = html_engine_calc_min_width (engine);
172                page_width = html_painter_get_page_width (engine->painter, engine);
173                /* printf ("min_width %d page %d\n", min_width, page_width); */
174                if (min_width > page_width) {
175                        HTML_PRINTER (printer)->scale = MAX (0.5, ((gdouble) page_width) / min_width);
176                        html_font_manager_clear_font_cache (&printer->font_manager);
177                        html_object_change_set_down (engine->clue, HTML_CHANGE_ALL);
178                        html_engine_calc_size (engine, NULL);
179                        /* printf ("scale %lf\n", HTML_PRINTER (printer)->scale);
180                           gtk_html_debug_dump_tree (engine->clue, 0); */
181                }
182
183                pages = print_all_pages (HTML_PAINTER (printer), engine,
184                                         header_height, footer_height,
185                                         header_print, footer_print,
186                                         user_data, do_print);
187
188                html_engine_set_painter (engine, old_painter);
189                g_object_unref (G_OBJECT (old_painter));
190        } else {
191                /* TODO2 dialog instead of warning */
192                g_warning (_("Cannot allocate default font for printing\n"));
193        }
194
195        g_object_unref (G_OBJECT (printer));
196
197        return pages;
198}
199
200void
201html_engine_print (HTMLEngine *engine, GnomePrintContext *print_context)
202{
203        html_engine_print_with_header_footer (engine, print_context, .0, .0, NULL, NULL, NULL);
204}
205
206void
207html_engine_print_with_header_footer (HTMLEngine *engine,
208                                      GnomePrintContext *print_context,
209                                      gdouble header_height,
210                                      gdouble footer_height,
211                                      GtkHTMLPrintCallback header_print,
212                                      GtkHTMLPrintCallback footer_print,
213                                      gpointer user_data)
214{
215        print_with_header_footer (engine, print_context,
216                                  header_height, footer_height, header_print, footer_print, user_data, TRUE);
217}
218
219gint
220html_engine_print_get_pages_num (HTMLEngine *e, GnomePrintContext *print_context,
221                                 gdouble header_height, gdouble footer_height)
222{
223        return print_with_header_footer (e, print_context, header_height, footer_height, NULL, NULL, NULL, FALSE);
224}
225
226void
227html_engine_print_set_min_split_index (HTMLEngine *e, gdouble idx)
228{
229        e->min_split_index = idx;
230}
Note: See TracBrowser for help on using the repository browser.