| 1 | //! Converts [`QRCode`] to an image |
| 2 | //! |
| 3 | //! ```rust |
| 4 | //! use fast_qr::convert::ConvertError; |
| 5 | //! use fast_qr::convert::{image::ImageBuilder, Builder, Shape}; |
| 6 | //! use fast_qr::qr::QRBuilder; |
| 7 | //! |
| 8 | //! # fn main() -> Result<(), ConvertError> { |
| 9 | //! // QRBuilde::new can fail if content is too big for version, |
| 10 | //! // please check before unwrapping. |
| 11 | //! let qrcode = QRBuilder::new("https://example.com/") |
| 12 | //! .build() |
| 13 | //! .unwrap(); |
| 14 | //! |
| 15 | //! let _img = ImageBuilder::default() |
| 16 | //! .shape(Shape::RoundedSquare) |
| 17 | //! .fit_width(600) |
| 18 | //! .to_file(&qrcode, "out.png"); |
| 19 | //! |
| 20 | //! # std::fs::remove_file("out.png"); |
| 21 | //! # Ok(()) |
| 22 | //! # } |
| 23 | //! ``` |
| 24 | |
| 25 | 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/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#; |
| 26 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 27 | |
| 28 | use craterust-analyzer cargo fast_qr 0.13.1 crate/::QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#; |
| 29 | |
| 30 | use superrust-analyzer cargo fast_qr 0.13.1 convert/::Colorrust-analyzer cargo fast_qr 0.13.1 convert/Color#; |
| 31 | use superrust-analyzer cargo fast_qr 0.13.1 convert/::{svgrust-analyzer cargo fast_qr 0.13.1 convert/svg/::SvgBuilderrust-analyzer cargo fast_qr 0.13.1 convert/svg/SvgBuilder#, Builderrust-analyzer cargo fast_qr 0.13.1 convert/Builder#, Shaperust-analyzer cargo fast_qr 0.13.1 convert/Shape#}; |
| 32 | |
| 33 | use resvgrust-analyzer cargo resvg 0.47.0 crate/::tiny_skiarust-analyzer cargo tiny-skia 0.12.0 crate/::{selfrust-analyzer cargo tiny-skia 0.12.0 crate/, Pixmaprust-analyzer cargo tiny-skia 0.12.0 pixmap/Pixmap#}; |
| 34 | use resvgrust-analyzer cargo resvg 0.47.0 crate/::usvgrust-analyzer cargo usvg 0.47.0 crate/::{selfrust-analyzer cargo usvg 0.47.0 crate/, Sizerust-analyzer cargo tiny-skia-path 0.12.0 size/Size#, Transformrust-analyzer cargo tiny-skia-path 0.12.0 transform/Transform#}; |
| 35 | |
| 36 | /// [`ImageBuilder`] contains an [`SvgBuilder`] and adds some options \ |
| 37 | /// - fit_height adds a max-height boundary |
| 38 | /// - fit_width adds a max-width boundary |
| 39 | pub struct ImageBuilderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder# { |
| 40 | fit_heightrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_height.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>, |
| 41 | fit_widthrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_width.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>, |
| 42 | svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder.: SvgBuilderrust-analyzer cargo fast_qr 0.13.1 convert/svg/SvgBuilder#, |
| 43 | } |
| 44 | |
| 45 | /// Error when converting to image |
| 46 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 47 | pub enum ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError# { |
| 48 | /// Error while writing to file |
| 49 | IoErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#IoError#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#), |
| 50 | /// Error while creating image |
| 51 | ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#ImageError#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 52 | /// Error while convert to bytes |
| 53 | EncodingErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#EncodingError#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 54 | } |
| 55 | |
| 56 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError# {} |
| 57 | |
| 58 | impl 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/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError# { |
| 59 | fn fmtrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageError][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> 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/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 60 | match self { |
| 61 | ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#::IoErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#IoError#(io_err) => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(io_err.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()), |
| 62 | ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#::ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#ImageError#(error) => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(error.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()), |
| 63 | ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#::EncodingErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#EncodingError#(error) => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(error.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()), |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Creates an ImageBuilder instance, which contains an [`SvgBuilder`] |
| 69 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for ImageBuilderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder# { |
| 70 | fn defaultrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Default]default().() -> Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Default] { |
| 71 | ImageBuilderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder# { |
| 72 | fit_heightrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_height.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 73 | fit_widthrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_width.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 74 | svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Default]default().(), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Builderrust-analyzer cargo fast_qr 0.13.1 convert/Builder# for ImageBuilderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder# { |
| 80 | fn marginrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]margin().(&mut self, margin: usize) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 81 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..marginrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]margin().(margin); |
| 82 | self |
| 83 | } |
| 84 | |
| 85 | fn module_colorrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]module_color().<C: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Colorrust-analyzer cargo fast_qr 0.13.1 convert/Color#>>(&mut self, module_color: C) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 86 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..module_colorrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]module_color().(module_color); |
| 87 | self |
| 88 | } |
| 89 | |
| 90 | fn background_colorrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]background_color().<C: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Colorrust-analyzer cargo fast_qr 0.13.1 convert/Color#>>(&mut self, background_color: C) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 91 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..background_colorrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]background_color().(background_color); |
| 92 | self |
| 93 | } |
| 94 | |
| 95 | fn shaperust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]shape().(&mut self, shape: Shaperust-analyzer cargo fast_qr 0.13.1 convert/Shape#) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 96 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..shaperust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]shape().(shape); |
| 97 | self |
| 98 | } |
| 99 | |
| 100 | fn imagerust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]image().(&mut self, image: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 101 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..imagerust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]image().(image); |
| 102 | self |
| 103 | } |
| 104 | |
| 105 | fn image_background_colorrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]image_background_color().<C: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Colorrust-analyzer cargo fast_qr 0.13.1 convert/Color#>>(&mut self, image_background_color: C) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 106 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder. |
| 107 | .image_background_colorrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]image_background_color().(image_background_color); |
| 108 | self |
| 109 | } |
| 110 | |
| 111 | fn image_background_shaperust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]image_background_shape().( |
| 112 | &mut self, |
| 113 | image_background_shape: superrust-analyzer cargo fast_qr 0.13.1 convert/::ImageBackgroundShaperust-analyzer cargo fast_qr 0.13.1 convert/ImageBackgroundShape#, |
| 114 | ) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 115 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder. |
| 116 | .image_background_shaperust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]image_background_shape().(image_background_shape); |
| 117 | self |
| 118 | } |
| 119 | |
| 120 | fn image_sizerust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]image_size().(&mut self, image_size: f64) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 121 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..image_sizerust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]image_size().(image_size); |
| 122 | self |
| 123 | } |
| 124 | |
| 125 | fn image_gaprust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]image_gap().(&mut self, gap: f64) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 126 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..image_gaprust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]image_gap().(gap); |
| 127 | self |
| 128 | } |
| 129 | |
| 130 | fn image_positionrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]image_position().(&mut self, x: f64, y: f64) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 131 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..image_positionrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]image_position().(x, y); |
| 132 | self |
| 133 | } |
| 134 | |
| 135 | fn shape_colorrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder]shape_color().<C: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Colorrust-analyzer cargo fast_qr 0.13.1 convert/Color#>>(&mut self, shape: Shaperust-analyzer cargo fast_qr 0.13.1 convert/Shape#, color: C) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder][Builder] { |
| 136 | self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..shape_colorrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder][Builder]shape_color().(shape, color); |
| 137 | self |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl ImageBuilderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder# { |
| 142 | /// Add a max-height boundary |
| 143 | pub fn fit_heightrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]fit_height().(&mut self, height: u32) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder] { |
| 144 | self.fit_heightrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_height. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(height); |
| 145 | self |
| 146 | } |
| 147 | |
| 148 | /// Add a max-width boundary |
| 149 | pub fn fit_widthrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]fit_width().(&mut self, width: u32) -> &mut Selfrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder] { |
| 150 | self.fit_widthrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_width. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(width); |
| 151 | self |
| 152 | } |
| 153 | |
| 154 | // From https://github.com/RazrFalcon/resvg/blob/374a25f/crates/resvg/tests/integration/main.rs |
| 155 | /// Return a pixmap containing the svg for a QRCode |
| 156 | pub fn to_pixmaprust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]to_pixmap().(&self, qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> Pixmaprust-analyzer cargo tiny-skia 0.12.0 pixmap/Pixmap# { |
| 157 | let opt = usvgrust-analyzer cargo usvg 0.47.0 crate/::Optionsrust-analyzer cargo usvg 0.47.0 parser/options/Options#::defaultrust-analyzer cargo usvg 0.47.0 parser/options/impl#[`Options<'_>`][Default]default().(); |
| 158 | |
| 159 | let tree = { |
| 160 | let svg_data = self.svg_builderrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#svg_builder..to_strrust-analyzer cargo fast_qr 0.13.1 convert/svg/impl#[SvgBuilder]to_str().(qr); |
| 161 | let tree = usvgrust-analyzer cargo usvg 0.47.0 crate/::Treerust-analyzer cargo usvg 0.47.0 tree/Tree#::from_strrust-analyzer cargo usvg 0.47.0 parser/impl#[`crate::Tree`]from_str().(&svg_data, &opt); |
| 162 | tree.expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to parse SVG") |
| 163 | }; |
| 164 | |
| 165 | let (width, height, transform) = self.fitrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]fit().(tree.sizerust-analyzer cargo usvg 0.47.0 tree/impl#[Tree]size().()); |
| 166 | let mut pixmap = tiny_skiarust-analyzer cargo tiny-skia 0.12.0 crate/::Pixmaprust-analyzer cargo tiny-skia 0.12.0 pixmap/Pixmap#::newrust-analyzer cargo tiny-skia 0.12.0 pixmap/impl#[Pixmap]new().(width, height).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Failed to create pixmap"); |
| 167 | |
| 168 | resvgrust-analyzer cargo resvg 0.47.0 crate/::renderrust-analyzer cargo resvg 0.47.0 render().(&tree, transform, &mut pixmap.as_mutrust-analyzer cargo tiny-skia 0.12.0 pixmap/impl#[Pixmap]as_mut().()); |
| 169 | |
| 170 | pixmap |
| 171 | } |
| 172 | |
| 173 | /// Saves the image for a QRCode to a file |
| 174 | pub fn to_filerust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]to_file().(&self, qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#, file: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#> { |
| 175 | use iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#}; |
| 176 | |
| 177 | self.to_pixmaprust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]to_pixmap().(qr) |
| 178 | .save_pngrust-analyzer cargo tiny-skia 0.12.0 pixmap/impl#[Pixmap]save_png().(file) |
| 179 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#::IoErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#IoError#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, err.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))) |
| 180 | } |
| 181 | |
| 182 | /// Saves the image for a QRCode in a byte buffer |
| 183 | pub fn to_bytesrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]to_bytes().(&self, qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#> { |
| 184 | let out = self.to_pixmaprust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]to_pixmap().(qr); |
| 185 | out.encode_pngrust-analyzer cargo tiny-skia 0.12.0 pixmap/impl#[Pixmap]encode_png().() |
| 186 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| ImageErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#::EncodingErrorrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageError#EncodingError#(err.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())) |
| 187 | } |
| 188 | |
| 189 | fn fitrust-analyzer cargo fast_qr 0.13.1 convert/image/impl#[ImageBuilder]fit().(&self, tree_size: Sizerust-analyzer cargo tiny-skia-path 0.12.0 size/Size#) -> (u32, u32, Transformrust-analyzer cargo tiny-skia-path 0.12.0 transform/Transform#) { |
| 190 | let factor = match (self.fit_widthrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_width., self.fit_heightrust-analyzer cargo fast_qr 0.13.1 convert/image/ImageBuilder#fit_height.) { |
| 191 | // Get the scaling factor by checking which dimensions can be scaled up the least |
| 192 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(width), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(height)) => { |
| 193 | (width as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().tree_size.widthrust-analyzer cargo tiny-skia-path 0.12.0 size/impl#[Size]width().()).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]min().(height as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().tree_size.heightrust-analyzer cargo tiny-skia-path 0.12.0 size/impl#[Size]height().()) |
| 194 | } |
| 195 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(width), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => width as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().tree_size.widthrust-analyzer cargo tiny-skia-path 0.12.0 size/impl#[Size]width().(), |
| 196 | (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(height)) => height as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().tree_size.heightrust-analyzer cargo tiny-skia-path 0.12.0 size/impl#[Size]height().(), |
| 197 | (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => 1f32, |
| 198 | }; |
| 199 | |
| 200 | ( |
| 201 | (tree_size.widthrust-analyzer cargo tiny-skia-path 0.12.0 size/impl#[Size]width().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().factor) as u32, |
| 202 | (tree_size.heightrust-analyzer cargo tiny-skia-path 0.12.0 size/impl#[Size]height().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().factor) as u32, |
| 203 | tiny_skiarust-analyzer cargo tiny-skia 0.12.0 crate/::Transformrust-analyzer cargo tiny-skia-path 0.12.0 transform/Transform#::from_scalerust-analyzer cargo tiny-skia-path 0.12.0 transform/impl#[Transform]from_scale().(factor, factor), |
| 204 | ) |
| 205 | } |
| 206 | } |