spectrusty_utils/
printer.rs

1/*
2    Copyright (C) 2020-2022  Rafal Michalski
3
4    This file is part of SPECTRUSTY, a Rust library for building emulators.
5
6    For the full copyright notice, see the lib.rs file.
7*/
8//! Printer emulator related utilities.
9use std::io;
10
11mod epson_gfx;
12mod image_spooler;
13
14pub use epson_gfx::*;
15pub use image_spooler::*;
16
17/// A trait for dot matrix printer spoolers that can produce monochromatic images.
18///
19/// This trait defines an interface for the emulator program to be able to produce buffered images.
20pub trait DotMatrixGfx {
21    /// Returns `true` if the printer is currently capturing data. Returns `false` otherwise.
22    fn is_spooling(&self) -> bool;
23    /// Returns an approximate number of graphic lines already buffered.
24    fn lines_buffered(&self) -> usize;
25    /// Clears the buffered image data.
26    fn clear(&mut self);
27    /// Returns `true` if no image has been buffered. Otherwise returns `false`
28    fn is_empty(&self) -> bool {
29        self.lines_buffered() == 0
30    }
31    /// Renders already buffered image data as an SVG image written to the provided `target`.
32    /// Returns `Ok(true)` if an image has been rendered. If there was no image data spooled, returns `Ok(false)`.
33    fn write_svg_dot_gfx_lines(&self, description: &str, target: &mut dyn io::Write) -> io::Result<bool>;
34    /// Renders already buffered image data as a monochrome 8-bit greyscale image data.
35    /// Returns `Some(width, height)` if an image has been rendered. If there was no image data spooled, returns `None`.
36    ///
37    /// You may use `image` crate to render an actual image in some popular format:
38    ///
39    /// ```text
40    /// let mut buf: Vec<u8> = Vec::new();
41    /// if let Some((width, height)) = printer.write_gfx_data(&mut buf) {
42    ///     let img = image::ImageBuffer::<image::Luma<u8>, _>::from_vec(width, height, buf);
43    ///     img.save("printed.png").unwrap();
44    ///     // or alternatively
45    ///     image::save_buffer("printed.png", &buf, width, height, image::ColorType::L8).unwrap();
46    /// }
47    /// ```
48    fn write_gfx_data(&mut self, target: &mut Vec<u8>) -> Option<(u32, u32)>;
49}