| 1 | //! SVG rendering support. |
| 2 | //! |
| 3 | //! # Example |
| 4 | //! |
| 5 | //! ``` |
| 6 | //! use qrcode::QrCode; |
| 7 | //! use qrcode::render::svg; |
| 8 | //! |
| 9 | //! let code = QrCode::new(b"Hello").unwrap(); |
| 10 | //! let svg_xml = code.render::<svg::Color>().build(); |
| 11 | //! println!("{}", svg_xml); |
| 12 | |
| 13 | #![cfg(feature = "svg")] |
| 14 | |
| 15 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#; |
| 16 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 17 | |
| 18 | use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::{Canvasrust-analyzer cargo qrcode 0.14.1 render/Canvas# as RenderCanvasrust-analyzer cargo qrcode 0.14.1 render/Canvas#, Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel#}; |
| 19 | use craterust-analyzer cargo qrcode 0.14.1 crate/::typesrust-analyzer cargo qrcode 0.14.1 types/::Colorrust-analyzer cargo qrcode 0.14.1 types/Color# as ModuleColorrust-analyzer cargo qrcode 0.14.1 types/Color#; |
| 20 | |
| 21 | /// An SVG color. |
| 22 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 23 | pub struct Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#<'a>(pub &'a str); |
| 24 | |
| 25 | impl<'a> Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel# for Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#<'a> { |
| 26 | type Canvasrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Color<'a>`][Pixel]Canvas# = Canvasrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#<'a>; |
| 27 | type Imagerust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Color<'a>`][Pixel]Image# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 28 | |
| 29 | fn default_colorrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Color<'a>`][Pixel]default_color().(color: ModuleColorrust-analyzer cargo qrcode 0.14.1 types/Color#) -> Selfrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Color<'a>`][Pixel] { |
| 30 | Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#(color.selectrust-analyzer cargo qrcode 0.14.1 types/impl#[Color]select().("#000", "#fff")) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[doc(hidden)] |
| 35 | pub struct Canvasrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#<'a> { |
| 36 | svgrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#svg.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 37 | markerrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#<'a>>, |
| 38 | } |
| 39 | |
| 40 | impl<'a> RenderCanvasrust-analyzer cargo qrcode 0.14.1 render/Canvas# for Canvasrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#<'a> { |
| 41 | type Pixelrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]Pixel# = Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#<'a>; |
| 42 | type Imagerust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]Image# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 43 | |
| 44 | fn newrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]new().(width: u32, height: u32, dark_pixel: Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#<'a>, light_pixel: Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#<'a>) -> Selfrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas] { |
| 45 | Canvasrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas# { |
| 46 | svgrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#svg.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 47 | concatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/concat!!( |
| 48 | r#"<?xml version="1.0" standalone="yes"?>"#, |
| 49 | r#"<svg xmlns="http://www.w3.org/2000/svg""#, |
| 50 | r#" version="1.1" width="{w}" height="{h}""#, |
| 51 | r#" viewBox="0 0 {w} {h}" shape-rendering="crispEdges">"#, |
| 52 | r#"<rect x="0" y="0" width="{w}" height="{h}" fill="{bg}"/>"#, |
| 53 | r#"<path fill="{fg}" d=""#, |
| 54 | ), |
| 55 | w = width, |
| 56 | h = height, |
| 57 | fg = dark_pixel.0rust-analyzer cargo qrcode 0.14.1 render/svg/Color#0., |
| 58 | bg = light_pixel.0rust-analyzer cargo qrcode 0.14.1 render/svg/Color#0. |
| 59 | ), |
| 60 | markerrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn draw_dark_pixelrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]draw_dark_pixel().(&mut self, x: u32, y: u32) { |
| 65 | self.draw_dark_rectrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]draw_dark_rect().(x, y, 1, 1); |
| 66 | } |
| 67 | |
| 68 | fn draw_dark_rectrust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]draw_dark_rect().(&mut self, left: u32, top: u32, width: u32, height: u32) { |
| 69 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(self.svgrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#svg., "M{left} {top}h{width}v{height}H{left}V{top}").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 70 | } |
| 71 | |
| 72 | fn into_imagerust-analyzer cargo qrcode 0.14.1 render/svg/impl#[`Canvas<'a>`][Canvas]into_image().(mut self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 73 | self.svgrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#svg..push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(r#""/></svg>"#); |
| 74 | self.svgrust-analyzer cargo qrcode 0.14.1 render/svg/Canvas#svg. |
| 75 | } |
| 76 | } |