ltk_webkit/types/
print.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use std::fmt;

/// How [`crate::WebView::print_to_pdf`] lays the page out. Lengths are in PDF points, 1/72 of an inch. The browser adds no header or footer, so the PDF carries neither the address nor the date; its title is the document's.
#[ derive( Clone, Debug, PartialEq ) ]
pub struct PrintSettings
{
	/// Width and height, already turned for the orientation. Wins over the page's `@page` size.
	pub paper:       ( f32, f32 ),
	/// Top, right, bottom, left.
	pub margins:     [ f32; 4 ],
	/// 1.0 is 100 %, kept within 0.1 and 2.0.
	pub scale:       f32,
	/// Inclusive ranges from 1, printed in the document's order and once each; empty prints every page. Pages past the end are left out.
	pub pages:       Vec<( u32, u32 )>,
	pub backgrounds: bool,
}

impl Default for PrintSettings
{
	/// A4 with 1 cm margins, at 100 %, every page, without backgrounds.
	fn default() -> Self
	{
		Self { paper: ( 595.28, 841.89 ), margins: [ 28.35; 4 ], scale: 1.0, pages: Vec::new(), backgrounds: false }
	}
}

impl PrintSettings
{
	pub const MIN_SCALE: f32 = 0.1;
	pub const MAX_SCALE: f32 = 2.0;
}

/// One sheet of a [`Preview`], margins included, in RGBA.
#[ derive( Clone, Debug, PartialEq ) ]
pub struct PreviewPage
{
	/// In the document, from 0.
	pub index:  usize,
	pub rgba:   Vec<u8>,
	pub width:  u32,
	pub height: u32,
}

/// What [`crate::WebView::print_preview`] renders.
#[ derive( Clone, Debug, PartialEq ) ]
pub struct Preview
{
	/// The pages of the whole document laid out with those settings.
	pub total: usize,
	/// The pages the settings choose.
	pub pages: Vec<PreviewPage>,
}

#[ derive( Clone, Debug, PartialEq, Eq ) ]
pub enum PrintError
{
	/// The view is already printing or rendering a preview.
	Busy,
	/// Also no page left to print, or a range that ends before it starts.
	Failed( String ),
}

impl fmt::Display for PrintError
{
	fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result
	{
		match self
		{
			PrintError::Busy             => write!( f, "the view is already printing" ),
			PrintError::Failed( reason ) => write!( f, "printing failed: {reason}" ),
		}
	}
}

impl std::error::Error for PrintError {}