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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Browser print-to-page scaling for normal-flow layout overflow.
//!
//! Chromium reduces an overflowing document's normal-flow width to the page's
//! printable width before emitting its PDF content stream. This is distinct
//! from CSS overflow: positioned visual overflow remains clipped by the page
//! and must not change the scale of unrelated flow content.
use super::engine::{LayoutElement, Page};
use crate::types::{Margin, PageSize, Point, Size};
/// A uniform scale applied to page content around the physical page's top-left
/// corner by the PDF renderer. It owns the print-fit decision so render paths
/// never infer a scale from individual paint operations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct PrintContentScale(f32);
impl PrintContentScale {
/// Keep normal-flow content within the physical page width without enlarging
/// content that already fits. Invalid or empty geometry leaves it alone.
pub(crate) fn from_flow_width(printable_width: f32, flow_right_edge: f32) -> Self {
if !printable_width.is_finite()
|| !flow_right_edge.is_finite()
|| printable_width <= 0.0
|| flow_right_edge <= printable_width
{
return Self::default();
}
Self(printable_width / flow_right_edge)
}
pub(crate) const fn factor(self) -> f32 {
self.0
}
pub(crate) const fn is_identity(self) -> bool {
self.0 == 1.0
}
/// Layout-space size whose fitted physical result has `physical` extent.
pub(crate) fn layout_size_for_physical(self, physical: Size) -> Size {
Size::new(
physical.width / self.factor(),
physical.height / self.factor(),
)
}
/// Layout-space offset whose fitted physical result has `physical` offset.
pub(crate) fn layout_point_for_physical(self, physical: Point) -> Point {
Point::new(physical.x / self.factor(), physical.y / self.factor())
}
}
impl Default for PrintContentScale {
fn default() -> Self {
Self(1.0)
}
}
/// Compute Chromium's document-wide print-fit scale after pagination.
///
/// Each physical page contributes its selected page-area width and normal-flow
/// scrollable overflow. The narrowest resulting factor applies to every page,
/// while rendering anchors it at each page area's own origin.
pub(crate) fn assign_page_print_scales(
pages: &mut [Page],
default_page_size: PageSize,
default_margin: Margin,
) {
let document_scale = pages
.iter()
.map(|page| {
let geometry = page
.geometry
.unwrap_or(super::page_context::PageGeometry::new(
default_page_size,
default_margin,
));
let flow_right_edge = page
.elements
.iter()
.filter_map(|(_, element)| element.print_fit_right_edge())
.fold(0.0_f32, f32::max);
PrintContentScale::from_flow_width(
geometry.page_area_size().width,
geometry.flow_right_in_page_area(flow_right_edge),
)
})
.map(PrintContentScale::factor)
.fold(1.0_f32, f32::min);
for page in pages {
page.print_content_scale = PrintContentScale(document_scale);
for (_, element) in &mut page.elements {
if let Some(background) = element.page_area_background_mut() {
background.apply_print_content_scale(page.print_content_scale);
}
}
}
}
#[cfg(test)]
mod tests;