NetSurf
print.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Adam Blokus <adamblokus@gmail.com>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/** \file
20 * Implementation of paginated output.
21 */
22
23#include "utils/config.h"
24
25#include <assert.h>
26#include <string.h>
27#include <dom/dom.h>
28
29#include "utils/utils.h"
30#include "utils/nsoption.h"
31#include "utils/log.h"
32#include "utils/talloc.h"
33#include "netsurf/content.h"
34#include "netsurf/plotters.h"
35#include "content/hlcache.h"
36#include "css/utils.h"
37#include "html/box.h"
38
39#include "desktop/print.h"
40#include "desktop/printer.h"
41
42/* Default print settings */
43#define DEFAULT_PAGE_WIDTH 595
44#define DEFAULT_PAGE_HEIGHT 840
45#define DEFAULT_COPIES 1
46
49static float done_height;
50
54
55/**
56 * This function calls print setup, prints page after page until the whole
57 * content is printed calls cleaning up afterwise.
58 *
59 * \param content The content to be printed
60 * \param printer The printer interface for the printer to be used
61 * \param settings The settings for printing to use
62 * \return true if successful, false otherwise
63 */
65 const struct printer *printer,
67{
68 bool ret = true;
69
70 assert(content != NULL && printer != NULL && settings != NULL);
71
73 return false;
74
77 }
78
80
81 return ret;
82}
83
84
85/**
86 * The content passed to the function is duplicated with its boxes, font
87 * measuring functions are being set.
88 *
89 * \param content The content to be printed
90 * \param settings The settings for printing to use
91 * \return true if successful, false otherwise
92 */
93static struct hlcache_handle *
95{
97
99
100 return printed_content;
101}
102
103
104/**
105 * The content is resized to fit page width.
106 *
107 * \param content The content to be printed
108 * \param settings The settings for printing to use
109 * \return true if successful, false otherwise
110 */
111static bool
113{
114 if (settings == NULL)
115 return false;
116
117 /* Apply settings - adjust page size etc */
118
120 FIXTOFLT(FSUB(settings->margins[MARGINLEFT],
122
124 FIXTOFLT(FSUB(settings->margins[MARGINTOP],
126
128
129 NSLOG(netsurf, INFO,
130 "New layout applied.New height = %d ; New width = %d ",
133
134 return true;
135}
136
137
138/**
139 * This function prepares the content to be printed. The current browser content
140 * is duplicated and resized, printer initialization is called.
141 *
142 * \param content The content to be printed
143 * \param printer The printer interface for the printer to be used
144 * \param settings The settings for printing to use
145 * \param height updated to the height of the printed content
146 * \return true if successful, false otherwise
147 */
149 const struct printer *printer, struct print_settings *settings,
150 double *height)
151{
153
154 if (printed_content == NULL)
155 return false;
156
158
159 if (height)
161
163
164 done_height = 0;
165
166 return true;
167}
168
169/**
170 * This function draws one page, beginning with the height offset of done_height
171 *
172 * \param printer The printer interface for the printer to be used
173 * \param settings The settings for printing to use
174 * \return true if successful, false otherwise
175 */
177 struct print_settings *settings)
178{
179 struct rect clip;
180 struct content_redraw_data data;
181 struct redraw_context ctx = {
182 .interactive = false,
183 .background_images = !nsoption_bool(remove_backgrounds),
184 .plot = printer->plotter,
185 .priv = settings->priv
186 };
187
189
190 clip.x0 = 0;
191 clip.y0 = 0;
194
195 data.x = 0;
196 data.y = -done_height;
197 data.width = 0;
198 data.height = 0;
199 data.background_colour = 0xFFFFFF;
200 data.scale = settings->scale;
201 data.repeat_x = false;
202 data.repeat_y = false;
203
206
208 if (!content_redraw(printed_content, &data, &clip, &ctx))
209 return false;
210
215
216 return true;
217}
218
219
220/**
221 * Memory allocated during printing is being freed here.
222 *
223 * \param content The original content
224 * \param printer The printer interface for the printer to be used
225 * \param settings The print settings to use.
226 * \return true if successful, false otherwise
227 */
229 struct print_settings *settings)
230{
232
233 html_redraw_printing = false;
234
235 if (printed_content) {
237 }
238
239 free((void *)settings->output);
240 free(settings);
241
242 return true;
243}
244
245/**
246 * Generates one of the predefined print settings sets.
247 *
248 * \param configuration the requested configuration
249 * \param filename the filename or NULL
250 * \param font_func font handling functions
251 * \return print_settings in case if successful, NULL if unknown
252 * configuration or lack of memory.
253 */
255 const char *filename, const struct gui_layout_table *font_func)
256{
257 struct print_settings *settings;
258 css_fixed length = 0;
259 css_unit unit = CSS_UNIT_MM;
260 css_unit_ctx unit_len_ctx = {
261 .viewport_width = DEFAULT_PAGE_WIDTH,
262 .viewport_height = DEFAULT_PAGE_HEIGHT,
263 .root_style = NULL,
264 };
265
266 switch (configuration){
267 case PRINT_DEFAULT:
268 settings = (struct print_settings*)
269 malloc(sizeof(struct print_settings));
270 if (settings == NULL)
271 return NULL;
272
276
278
279 length = INTTOFIX(DEFAULT_MARGIN_LEFT_MM);
280 settings->margins[MARGINLEFT] = css_unit_len2device_px(
281 NULL, &unit_len_ctx, length, unit);
282 length = INTTOFIX(DEFAULT_MARGIN_RIGHT_MM);
283 settings->margins[MARGINRIGHT] = css_unit_len2device_px(
284 NULL, &unit_len_ctx, length, unit);
285 length = INTTOFIX(DEFAULT_MARGIN_TOP_MM);
286 settings->margins[MARGINTOP] = css_unit_len2device_px(
287 NULL, &unit_len_ctx, length, unit);
288 length = INTTOFIX(DEFAULT_MARGIN_BOTTOM_MM);
289 settings->margins[MARGINBOTTOM] = css_unit_len2device_px(
290 NULL, &unit_len_ctx, length, unit);
291 break;
292 /* use settings from the Export options tab */
293 case PRINT_OPTIONS:
294 settings = (struct print_settings*)
295 malloc(sizeof(struct print_settings));
296 if (settings == NULL)
297 return NULL;
298
302
303 settings->scale = (float)nsoption_int(export_scale) / 100;
304
305 length = INTTOFIX(nsoption_int(margin_left));
306 settings->margins[MARGINLEFT] = css_unit_len2device_px(
307 NULL, &unit_len_ctx, length, unit);
308 length = INTTOFIX(nsoption_int(margin_right));
309 settings->margins[MARGINRIGHT] = css_unit_len2device_px(
310 NULL, &unit_len_ctx, length, unit);
311 length = INTTOFIX(nsoption_int(margin_top));
312 settings->margins[MARGINTOP] = css_unit_len2device_px(
313 NULL, &unit_len_ctx, length, unit);
314 length = INTTOFIX(nsoption_int(margin_bottom));
315 settings->margins[MARGINBOTTOM] = css_unit_len2device_px(
316 NULL, &unit_len_ctx, length, unit);
317 break;
318 default:
319 return NULL;
320 }
321
322 /* Set font functions */
324
325 /* Output filename, or NULL if printing */
326 if (filename != NULL) {
327 settings->output = strdup(filename);
328 if (settings->output == NULL) {
329 free(settings);
330 return NULL;
331 }
332 } else {
333 settings->output = NULL;
334 }
335
336 return settings;
337}
Box interface.
void content_reformat(hlcache_handle *h, bool background, int width, int height)
Reformat to new size.
Definition: content.c:321
bool html_redraw_printing
Definition: print.c:51
#define DEFAULT_PAGE_HEIGHT
Definition: print.c:44
int html_redraw_printing_border
Definition: print.c:52
int html_redraw_printing_top_cropped
Definition: print.c:53
struct print_settings * print_make_settings(print_configuration configuration, const char *filename, const struct gui_layout_table *font_func)
Generates one of the predefined print settings sets.
Definition: print.c:254
bool print_draw_next_page(const struct printer *printer, struct print_settings *settings)
This function draws one page, beginning with the height offset of done_height.
Definition: print.c:176
static bool print_apply_settings(hlcache_handle *content, struct print_settings *settings)
The content is resized to fit page width.
Definition: print.c:112
static float page_content_width
Definition: print.c:47
static float page_content_height
Definition: print.c:47
#define DEFAULT_PAGE_WIDTH
Definition: print.c:43
static struct hlcache_handle * printed_content
Definition: print.c:48
bool print_cleanup(hlcache_handle *content, const struct printer *printer, struct print_settings *settings)
Memory allocated during printing is being freed here.
Definition: print.c:228
bool print_basic_run(hlcache_handle *content, const struct printer *printer, struct print_settings *settings)
This function calls print setup, prints page after page until the whole content is printed calls clea...
Definition: print.c:64
bool print_set_up(hlcache_handle *content, const struct printer *printer, struct print_settings *settings, double *height)
This function prepares the content to be printed.
Definition: print.c:148
static float done_height
Definition: print.c:49
static struct hlcache_handle * print_init(struct hlcache_handle *content, struct print_settings *settings)
The content passed to the function is duplicated with its boxes, font measuring functions are being s...
Definition: print.c:94
#define DEFAULT_COPIES
Definition: print.c:45
Conception: Generalized output-in-pages.
print_configuration
Predefined printing configuration names.
Definition: print.h:45
@ PRINT_DEFAULT
Definition: print.h:45
@ PRINT_OPTIONS
Definition: print.h:45
@ MARGINLEFT
Definition: print.h:42
@ MARGINTOP
Definition: print.h:42
@ MARGINRIGHT
Definition: print.h:42
@ MARGINBOTTOM
Definition: print.h:42
static struct print_settings * settings
Definition: print.c:50
nserror hlcache_handle_clone(hlcache_handle *handle, hlcache_handle **result)
Clone a high level cache handle.
Definition: hlcache.c:869
nserror hlcache_handle_release(hlcache_handle *handle)
Release a high-level cache handle.
Definition: hlcache.c:740
High-level resource cache interface.
Public content interface.
bool content_redraw(struct hlcache_handle *h, struct content_redraw_data *data, const struct rect *clip, const struct redraw_context *ctx)
Display content on screen with optional tiling.
Definition: content.c:558
int content_get_height(struct hlcache_handle *h)
Retrieve height of content.
Definition: content.c:1175
int content_get_width(struct hlcache_handle *h)
Retrieve width of content.
Definition: content.c:1158
Target independent plotting interface.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Printer interface.
int height
Definition: gui.c:161
Interface to utility string handling.
parameters to content redraw
Definition: content.h:40
int height
vertical dimension
Definition: content.h:48
bool repeat_y
whether content is tiled in y direction
Definition: content.h:59
bool repeat_x
whether content is tiled in x direction
Definition: content.h:58
int y
coordinate for top-left of redraw
Definition: content.h:42
int x
coordinate for top-left of redraw
Definition: content.h:41
colour background_colour
The background colour.
Definition: content.h:51
int width
dimensions to render content at (for scaling contents with intrinsic dimensions)
Definition: content.h:47
float scale
Scale for redraw (for scaling contents without intrinsic dimensions)
Definition: content.h:56
Content which corresponds to a single URL.
High-level cache handle.
Definition: hlcache.c:66
Settings for a print - filled in by print_make_settings or 'manually' by the caller.
Definition: print.h:50
float scale
Definition: print.h:55
const struct gui_layout_table * font_func
Definition: print.h:63
float page_width
Definition: print.h:52
float page_height
Definition: print.h:52
void * priv
Definition: print.h:66
unsigned int copies
Definition: print.h:57
css_fixed margins[4]
Definition: print.h:53
const char * output
Definition: print.h:60
Printer interface.
Definition: printer.h:34
const struct plotter_table * plotter
Definition: printer.h:35
void(* print_end)(void)
Definition: printer.h:41
bool(* print_begin)(struct print_settings *)
Definition: printer.h:37
bool(* print_next_page)(void)
Definition: printer.h:39
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59
Option reading and saving interface.
#define DEFAULT_MARGIN_BOTTOM_MM
Definition: nsoption.h:91
#define nsoption_int(OPTION)
Get the value of an integer option.
Definition: nsoption.h:313
#define DEFAULT_EXPORT_SCALE
Definition: nsoption.h:94
#define DEFAULT_MARGIN_RIGHT_MM
Definition: nsoption.h:93
#define DEFAULT_MARGIN_TOP_MM
Definition: nsoption.h:90
#define DEFAULT_MARGIN_LEFT_MM
Definition: nsoption.h:92
#define nsoption_bool(OPTION)
Get the value of a boolean option.
Definition: nsoption.h:304
Interface to a number of general purpose functionality.
static nserror clip(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
Definition: plot.c:357