Skip to content

Commit 61ee012

Browse files
committed
Support different page orientations
1 parent 63fa78c commit 61ee012

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

src/print.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ashpd::desktop::print::{PageSetup, PrintProxy};
1+
use ashpd::desktop::print::{Orientation, PageSetup, PrintProxy, Settings};
22
use cosmic_text::{Buffer, Scroll, fontdb};
33
use printpdf::*;
44
use std::{
@@ -69,21 +69,59 @@ fn generate_pdf<W: Write>(buffer: &mut Buffer, page_setup: &PageSetup, w: &mut W
6969
let mut doc = PdfDocument::new("");
7070
let mut pdf_fonts: HashMap<fontdb::ID, Option<FontId>> = HashMap::new();
7171

72-
//TODO: handle landscape mode
7372
let outer_width = Mm(page_setup.width.unwrap_or(216.0) as f32);
7473
let outer_height = Mm(page_setup.height.unwrap_or(279.0) as f32);
7574
let margin_top = Mm(page_setup.margin_top.unwrap_or(25.4) as f32);
7675
let margin_bottom = Mm(page_setup.margin_bottom.unwrap_or(25.4) as f32);
7776
let margin_left = Mm(page_setup.margin_left.unwrap_or(25.4) as f32);
7877
let margin_right = Mm(page_setup.margin_right.unwrap_or(25.4) as f32);
79-
let inner_width = outer_width - margin_left - margin_right;
80-
let inner_height = outer_height - margin_top - margin_bottom;
78+
let inner_width_mm = outer_width - margin_left - margin_right;
79+
let inner_height_mm = outer_height - margin_top - margin_bottom;
8180
let dpi = 96.0;
82-
let buffer_width = inner_width.into_pt().into_px(dpi).0 as f32;
83-
let buffer_height = inner_height.into_pt().into_px(dpi).0 as f32;
81+
let inner_width_px = inner_width_mm.into_pt().into_px(dpi);
82+
let inner_height_px = inner_height_mm.into_pt().into_px(dpi);
8483
let metrics = buffer.metrics();
8584
let font_size = Px(metrics.font_size as usize).into_pt(dpi);
8685
let line_height = Px(metrics.line_height as usize).into_pt(dpi);
86+
let (buffer_width, buffer_height, matrix) =
87+
match page_setup.orientation.unwrap_or(Orientation::Portrait) {
88+
Orientation::Portrait => (
89+
inner_width_px.0 as f32,
90+
inner_height_px.0 as f32,
91+
TextMatrix::TranslateRotate(
92+
Pt::from(margin_left),
93+
Pt::from(outer_height - margin_top) - line_height,
94+
0.0,
95+
),
96+
),
97+
Orientation::Landscape => (
98+
inner_height_px.0 as f32,
99+
inner_width_px.0 as f32,
100+
TextMatrix::TranslateRotate(
101+
Pt::from(margin_top) + line_height,
102+
Pt::from(margin_left),
103+
90.0,
104+
),
105+
),
106+
Orientation::ReversePortrait => (
107+
inner_width_px.0 as f32,
108+
inner_height_px.0 as f32,
109+
TextMatrix::TranslateRotate(
110+
Pt::from(outer_width - margin_left),
111+
Pt::from(margin_top) + line_height,
112+
180.0,
113+
),
114+
),
115+
Orientation::ReverseLandscape => (
116+
inner_height_px.0 as f32,
117+
inner_width_px.0 as f32,
118+
TextMatrix::TranslateRotate(
119+
Pt::from(outer_width - margin_left) - line_height,
120+
Pt::from(outer_height - margin_top),
121+
270.0,
122+
),
123+
),
124+
};
87125

88126
{
89127
let mut font_system = font_system().write().unwrap();
@@ -98,9 +136,7 @@ fn generate_pdf<W: Write>(buffer: &mut Buffer, page_setup: &PageSetup, w: &mut W
98136
let mut ops = vec![
99137
Op::SaveGraphicsState,
100138
Op::StartTextSection,
101-
Op::SetTextCursor {
102-
pos: Point::new(margin_left, outer_height - margin_top - line_height.into()),
103-
},
139+
Op::SetTextMatrix { matrix },
104140
Op::SetLineHeight { lh: line_height },
105141
];
106142

@@ -170,8 +206,8 @@ pub(crate) async fn print(mut buffer: Arc<Buffer>) -> Result<(), Box<dyn Error>>
170206
.prepare_print(
171207
window_id,
172208
&title,
173-
Default::default(),
174-
Default::default(),
209+
Settings::default(),
210+
PageSetup::default(),
175211
None,
176212
modal,
177213
)

0 commit comments

Comments
 (0)