|
| 1 | +//! EPS rendering support. |
| 2 | +//! |
| 3 | +//! # Example |
| 4 | +//! |
| 5 | +//! ``` |
| 6 | +//! use qrcode::QrCode; |
| 7 | +//! use qrcode::render::eps; |
| 8 | +//! |
| 9 | +//! let code = QrCode::new(b"Hello").unwrap(); |
| 10 | +//! let eps = code.render::<eps::Color>().build(); |
| 11 | +//! println!("{eps}"); |
| 12 | +
|
| 13 | +#![cfg(feature = "eps")] |
| 14 | + |
| 15 | +use alloc::format; |
| 16 | +use alloc::string::String; |
| 17 | +use core::fmt::Write; |
| 18 | + |
| 19 | +use crate::render::{Canvas as RenderCanvas, Pixel}; |
| 20 | +use crate::types::Color as ModuleColor; |
| 21 | + |
| 22 | +/// An EPS color (`[R, G, B]`). |
| 23 | +/// |
| 24 | +/// Each value must be in the range of 0.0 to 1.0. |
| 25 | +#[derive(Copy, Clone, Default, PartialEq, PartialOrd)] |
| 26 | +pub struct Color(pub [f64; 3]); |
| 27 | + |
| 28 | +impl Pixel for Color { |
| 29 | + type Canvas = Canvas; |
| 30 | + type Image = String; |
| 31 | + |
| 32 | + fn default_color(color: ModuleColor) -> Self { |
| 33 | + Color(color.select(Default::default(), [1.0; 3])) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[doc(hidden)] |
| 38 | +pub struct Canvas { |
| 39 | + eps: String, |
| 40 | + height: u32, |
| 41 | +} |
| 42 | + |
| 43 | +impl RenderCanvas for Canvas { |
| 44 | + type Pixel = Color; |
| 45 | + type Image = String; |
| 46 | + |
| 47 | + fn new(width: u32, height: u32, dark_pixel: Color, light_pixel: Color) -> Self { |
| 48 | + Canvas { |
| 49 | + eps: format!( |
| 50 | + concat!( |
| 51 | + "%!PS-Adobe-3.0 EPSF-3.0\n", |
| 52 | + "%%BoundingBox: 0 0 {w} {h}\n", |
| 53 | + "%%Pages: 1\n", |
| 54 | + "%%EndComments\n", |
| 55 | + "gsave\n", |
| 56 | + "{bgr} {bgg} {bgb} setrgbcolor\n", |
| 57 | + "0 0 {w} {h} rectfill\n", |
| 58 | + "grestore\n", |
| 59 | + "{fgr} {fgg} {fgb} setrgbcolor\n" |
| 60 | + ), |
| 61 | + w = width, |
| 62 | + h = height, |
| 63 | + fgr = dark_pixel.0[0], |
| 64 | + fgg = dark_pixel.0[1], |
| 65 | + fgb = dark_pixel.0[2], |
| 66 | + bgr = light_pixel.0[0], |
| 67 | + bgg = light_pixel.0[1], |
| 68 | + bgb = light_pixel.0[2], |
| 69 | + ), |
| 70 | + height, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn draw_dark_pixel(&mut self, x: u32, y: u32) { |
| 75 | + self.draw_dark_rect(x, y, 1, 1); |
| 76 | + } |
| 77 | + |
| 78 | + fn draw_dark_rect(&mut self, left: u32, top: u32, width: u32, height: u32) { |
| 79 | + let bottom = self.height - top; |
| 80 | + writeln!(self.eps, "{left} {bottom} {width} {height} rectfill").unwrap(); |
| 81 | + } |
| 82 | + |
| 83 | + fn into_image(mut self) -> String { |
| 84 | + self.eps.push_str("%%EOF"); |
| 85 | + self.eps |
| 86 | + } |
| 87 | +} |
0 commit comments