1
- use ashpd:: desktop:: print:: { PageSetup , PrintProxy } ;
1
+ use ashpd:: desktop:: print:: { Orientation , PageSetup , PrintProxy , Settings } ;
2
2
use cosmic_text:: { Buffer , Scroll , fontdb} ;
3
3
use printpdf:: * ;
4
4
use std:: {
@@ -69,21 +69,59 @@ fn generate_pdf<W: Write>(buffer: &mut Buffer, page_setup: &PageSetup, w: &mut W
69
69
let mut doc = PdfDocument :: new ( "" ) ;
70
70
let mut pdf_fonts: HashMap < fontdb:: ID , Option < FontId > > = HashMap :: new ( ) ;
71
71
72
- //TODO: handle landscape mode
73
72
let outer_width = Mm ( page_setup. width . unwrap_or ( 216.0 ) as f32 ) ;
74
73
let outer_height = Mm ( page_setup. height . unwrap_or ( 279.0 ) as f32 ) ;
75
74
let margin_top = Mm ( page_setup. margin_top . unwrap_or ( 25.4 ) as f32 ) ;
76
75
let margin_bottom = Mm ( page_setup. margin_bottom . unwrap_or ( 25.4 ) as f32 ) ;
77
76
let margin_left = Mm ( page_setup. margin_left . unwrap_or ( 25.4 ) as f32 ) ;
78
77
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;
81
80
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) ;
84
83
let metrics = buffer. metrics ( ) ;
85
84
let font_size = Px ( metrics. font_size as usize ) . into_pt ( dpi) ;
86
85
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
+ } ;
87
125
88
126
{
89
127
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
98
136
let mut ops = vec ! [
99
137
Op :: SaveGraphicsState ,
100
138
Op :: StartTextSection ,
101
- Op :: SetTextCursor {
102
- pos: Point :: new( margin_left, outer_height - margin_top - line_height. into( ) ) ,
103
- } ,
139
+ Op :: SetTextMatrix { matrix } ,
104
140
Op :: SetLineHeight { lh: line_height } ,
105
141
] ;
106
142
@@ -170,8 +206,8 @@ pub(crate) async fn print(mut buffer: Arc<Buffer>) -> Result<(), Box<dyn Error>>
170
206
. prepare_print (
171
207
window_id,
172
208
& title,
173
- Default :: default ( ) ,
174
- Default :: default ( ) ,
209
+ Settings :: default ( ) ,
210
+ PageSetup :: default ( ) ,
175
211
None ,
176
212
modal,
177
213
)
0 commit comments