freight_car_forwarder 0.1.11

Port of the C++ port of Timothy O'Connor's Freight Car Forwarding system.
Documentation
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// -!- rust -!- //////////////////////////////////////////////////////////////
//
//  System        : 
//  Module        : 
//  Object Name   : $RCSfile$
//  Revision      : $Revision$
//  Date          : $Date$
//  Author        : $Author$
//  Created By    : Robert Heller
//  Created       : 2025-09-02 15:13:52
//  Last Modified : <250919.1600>
//
//  Description	
//
//  Notes
//
//  History
//	
/////////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2025  Robert Heller D/B/A Deepwoods Software
//			51 Locke Hill Road
//			Wendell, MA 01379-9728
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// 
//
//////////////////////////////////////////////////////////////////////////////
extern crate cairo;
extern crate pango;
extern crate pango_sys;

/// Page size enum
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PageSize {
    /// US Letter
    Letter,
    /// A4
    A4,
}
/// Horizontal letter spacing
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TypeSpacing {
    /// Normal spacing
    One,
    /// Narrow (Condensed 60%)
    Half,
    /// Wide letter (double width)
    Double,
}

/// Type weight
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TypeWeight {
    /// Normal weight
    Normal,
    /// Heavy (Bold) weight
    Bold,
}

/// Text slant
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TypeSlant {
    /// Upright
    Roman,
    /// Slanted
    Italic,
}

/// Printer device.  This connects to a Cairo Pango PDF output file
pub struct Printer {
    pageSize: PageSize,
    pdf_surface: cairo::PdfSurface,
    pdf_context: cairo::Context,
    layout: pango::Layout,
    Courier: pango::FontDescription,
    CourierBold: pango::FontDescription,
    CourierOblique: pango::FontDescription,
    CourierBoldOblique: pango::FontDescription,
    current_slant: TypeSlant,
    current_weight: TypeWeight,
    current_spacing: TypeSpacing,
    swidth: f64,
    sheight: f64,
    title: String,
    lines: u32,
    maxLines: u32,
    partline: bool,
    needPage: bool,
    currentColumn: u32,
    currentColumnFraction: f64,
}

        

impl Printer {
    /// Open a PDF output file
    /// ## Parameters:
    /// - filename the output file name
    /// - title the title at the top of each page
    /// - pageSize the page size
    ///
    /// __Returns__ a freshly opened PDF output device.
    pub fn new(filename: &str, title: &str, pageSize: PageSize) -> Self {
        let swidth: f64;
        let sheight: f64;
        match pageSize {
            PageSize::Letter => {swidth = 8.5*72.0;
                                 sheight = 11.0*72.0;},
            PageSize::A4 => {swidth = 595.0; sheight = 842.0;},
        };
        let pdf_surface = cairo::PdfSurface::new(swidth,sheight,filename)
                        .expect("Failed to open PDF output file");
        let context = cairo::Context::new(&pdf_surface).
                            expect("Failed to create a context");
        let layout = pangocairo::functions::create_layout(&context);
        let mut this = Self {
                             pageSize: pageSize,
                             pdf_surface: pdf_surface,
                             pdf_context: context,
                             layout: layout,
                             Courier: pango::FontDescription::from_string("Courier 10px"),
                             CourierBold: pango::FontDescription::from_string("Courier Bold 10px"),
                             CourierOblique: pango::FontDescription::from_string("Courier Oblique 10px"),
                             CourierBoldOblique: pango::FontDescription::from_string("Courier BoldOblique 10px"),
                             swidth: swidth, sheight: sheight, 
                             maxLines: 0,
                             partline: false,
                             needPage: false,
                             currentColumn: 0,
                             currentColumnFraction: 0.0,
                             current_slant: TypeSlant::Roman,
                             current_weight: TypeWeight::Normal,
                             current_spacing: TypeSpacing::One,
                             title: String::from(title),
                             lines: 0,
                             };
        this.maxLines = ((this.sheight-72.0) / 12.0) as u32;
        this.SetTypeSpacing(TypeSpacing::Double);
        this.PutLine(&this.title.clone());
        this.SetTypeSpacing(TypeSpacing::One);
        this
    }
    //fn ClosePrinter(&mut self) -> bool {
    //    true
    //}
    /// Is the printer opened?  With this implementation it is always open
    /// until it goes out of scope
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ true
    pub fn IsOpenP(&self) -> bool {true}
    /// Page size
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ The page size.
    pub fn PrinterPageSize(&self) -> PageSize {self.pageSize}
    /// Set the type spacing
    /// ## Parameters:
    /// - spacing the new spacing
    ///
    /// __Returns__ true
    pub fn SetTypeSpacing(&mut self,spacing: TypeSpacing) -> bool {
        self.current_spacing = spacing;
        true
    }
    /// Set the type slant
    /// ## Parameters:
    /// - slant the new slant
    ///
    /// __Returns__ true
    pub fn SetTypeSlant(&mut self,slant: TypeSlant) -> bool {
        self.current_slant = slant;
        true
    }
    /// Set the type weight
    /// ## Parameters:
    /// - weight the new weight
    ///
    /// __Returns__ true
    pub fn SetTypeWeight(&mut self,weight: TypeWeight) -> bool {
        self.current_weight = weight;
        true
    }
    /// Force a new page
    /// ## Parameters:
    /// - heading the page heading
    ///
    /// __Returns__ true
    pub fn NewPage(&mut self,heading: &str) -> bool {
        self.needPage = true;
        self.lines = 0;
        if heading.len() > 0 {
            let savedTS = self.current_spacing;
            self.SetTypeSpacing(TypeSpacing::Double);
            self.PutLine(heading);
            self.SetTypeSpacing(savedTS);
            true
        } else {
            true
        }
    }
    /// Print a string and advance to the beginning of a new line
    /// ## Parameters:
    /// - line text to print
    ///
    /// __Returns__ true
    pub fn PutLine(&mut self,line: &str) -> bool {
        if line.len() > 0 {self.Put(line);}
        self.partline = false;
        self.lines += 1;
        if self.lines >= self.maxLines {self.NewPage(&self.title.clone());}
        self.currentColumn = 0;
        self.currentColumnFraction = 0.0;
        true
    }
    /// Print a string
    /// ## Parameters:
    /// - text text to print
    ///
    /// __Returns__ the fractional collum position
    fn putstring(&mut self,text: &str) -> f64 {
        let desc: &pango::FontDescription = 
            match self.current_slant {
                TypeSlant::Roman => 
                    match self.current_weight {
                        TypeWeight::Normal => &self.Courier,
                        TypeWeight::Bold => &self.CourierBold,
                    },
                 TypeSlant::Italic => 
                    match self.current_weight {
                        TypeWeight::Normal => &self.CourierOblique,
                        TypeWeight::Bold => &self.CourierBoldOblique,
                    },
            };
        self.layout.set_font_description(Some(desc));
        self.layout.set_text(" ");
        let wh = self.layout.size();
        let w = wh.0;
        let h = wh.1;
        self.layout.set_font_description(Some(desc));
        self.layout.set_text(text);
        self.pdf_context.save().expect("Failed to save context");
        match self.current_spacing {
            TypeSpacing::One => self.pdf_context.scale(1.0,1.0),
            TypeSpacing::Half => self.pdf_context.scale(0.6,1.0),
            TypeSpacing::Double => self.pdf_context.scale(2.0,1.0),
        };
        pangocairo::functions::update_layout(&self.pdf_context,&self.layout);
        let y = (self.lines as f64 * pango::units_to_double(h as i32))+36.0;
        let x = (self.currentColumn as f64 * pango::units_to_double(w as i32))+36.0;
        self.pdf_context.move_to(x,y);
        pangocairo::functions::show_layout(&self.pdf_context, &self.layout);
        self.pdf_context.restore().expect("Failed to restore context");
        let pp = (w as u32 + 512) >> 10;
        (pango::units_to_double(w as i32) * text.len() as f64) / pp as f64
    }
    /// Advance to the specified column
    /// - column the column to advance to
    ///
    /// __Return__ true
    pub fn Tab(&mut self, column: u8) -> bool {
        while self.currentColumn < column.into() {self.Put(" ");}
        true
    }
}

/// Generic output function
pub trait __Put<T> {
    fn Put(&mut self,object: T) -> bool;
}

impl __Put<char> for Printer {
    /// Print one character
    /// - object the character to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: char) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<u8> for Printer {
    /// Print one u8 number
    /// - object the u8 number to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: u8) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<usize> for Printer {
    /// Print one usize number
    /// - object the usize number to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: usize) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<isize> for Printer {
    /// Print one isize number
    /// - object the isize number to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: isize) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<u32> for Printer {
    /// Print one u32 number
    /// - object the u32 number to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: u32) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<i32> for Printer {
    /// Print one i32 number
    /// - object the i32 number to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: i32) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<f64> for Printer {
    /// Print one f64 number
    /// - object the f64 number to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: f64) -> bool {
        self.Put(format!("{}",object));
        true
    }
}

impl __Put<String> for Printer {
    /// Print one String
    /// - object the String to print
    ///
    /// __Returns__ true
    fn Put(&mut self,object: String) -> bool {
        self.Put(object.as_str());
        true
    }
}

impl __Put<&str> for Printer {
    /// Print one &str 
    /// (This is the real print function where the printing really happens)
    /// - object the &str to print
    ///
    /// __Returns__ true
    fn Put(&mut self,text: &str) -> bool {
        let mut lineIter = text.lines();
        let mut line = lineIter.next();
        loop {
            if line.is_none() {break;}
            if self.needPage {
                self.pdf_context.show_page().expect("Error showing page");
                self.needPage = false;
            }
            self.currentColumnFraction += self.putstring(line.unwrap());
            self.currentColumn = self.currentColumnFraction as u32;
            self.partline = true;
            line = lineIter.next();
            if line.is_some() {
                self.currentColumn = 0;
                self.currentColumnFraction = 0.0;
                self.partline = false;
                self.lines += 1;
                if self.lines >= self.maxLines {self.NewPage(&self.title.clone());}
            }
        }
        true
    }
}

//impl Drop for Printer {
//    fn drop(&mut self) {
//        self.ClosePrinter();
//    }
//}