Debian Codegraph @

src/wasm.rs

raw symbols SCIP
1 use crate::QRCode;
2 #[cfg(feature = "svg")]
3 use crate::{convert, Version, ECL};
4 #[cfg(feature = "wasm-bindgen")]
5 use wasm_bindgen::prelude::*;
6
7 fn bool_to_u8(qr: QRCode) -> Vec<u8> {
8 let dim = qr.size;
9 qr.data[..dim * dim]
10 .iter()
11 .map(|x| u8::from(x.value()))
12 .collect()
13 }
14
15 /// Generate a QR code from a string. All parameters are automatically set.
16 #[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
17 #[must_use]
18 pub fn qr(content: &str) -> Vec<u8> {
19 let qrcode = QRCode::new(content.as_bytes(), None, None, None, None);
20 qrcode.map(bool_to_u8).unwrap_or(Vec::new())
21 }
22
23 /// Configuration for the SVG output.
24 #[cfg(feature = "svg")]
25 #[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
26 #[derive(Debug, Clone)]
27 pub struct SvgOptions {
28 shape: convert::Shape,
29 module_color: Vec<u8>,
30 margin: usize,
31
32 ecl: Option<ECL>,
33 version: Option<Version>,
34
35 background_color: Vec<u8>,
36
37 image: String,
38 image_background_color: Vec<u8>,
39 image_background_shape: convert::ImageBackgroundShape,
40 image_size: Option<f64>,
41 image_gap: Option<f64>,
42 image_position: Vec<f64>,
43 }
44
45 #[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
46 #[cfg(feature = "svg")]
47 impl SvgOptions {
48 fn color_to_code(color: String) -> Vec<u8> {
49 let mut color = color;
50 if color.starts_with('#') {
51 color.remove(0);
52 }
53 let color = color.as_bytes();
54 let color = color.chunks_exact(2);
55 let color = color.map(|x| u8::from_str_radix(std::str::from_utf8(x).unwrap(), 16).unwrap());
56
57 let mut color = color.collect::<Vec<u8>>();
58 if color.len() == 3 {
59 color.push(255);
60 }
61
62 color
63 }
64
65 /// Updates the shape of the QRCode modules.
66 pub fn shape(self, shape: convert::Shape) -> Self {
67 Self { shape, ..self }
68 }
69
70 /// Updates the module color of the QRCode. Tales a string in the format `#RRGGBB[AA]`.
71 pub fn module_color(self, module_color: String) -> Self {
72 let code = Self::color_to_code(module_color);
73 if code.len() != 4 {
74 return self;
75 }
76 Self {
77 module_color: code,
78 ..self
79 }
80 }
81
82 /// Updates the margin of the QRCode.
83 pub fn margin(self, margin: usize) -> Self {
84 Self { margin, ..self }
85 }
86
87 /// Updates the background color of the QRCode. Tales a string in the format `#RRGGBB[AA]`.
88 pub fn background_color(self, background_color: String) -> Self {
89 let code = Self::color_to_code(background_color);
90 if code.len() != 4 {
91 return self;
92 }
93 Self {
94 background_color: code,
95 ..self
96 }
97 }
98
99 /// Updates the image of the QRCode. Takes base64 or a url.
100 pub fn image(self, image: String) -> Self {
101 Self { image, ..self }
102 }
103
104 /// Updates the background color of the image. Takes a string in the format `#RRGGBB[AA]`.
105 pub fn image_background_color(self, image_background_color: String) -> Self {
106 let code = Self::color_to_code(image_background_color);
107 if code.len() != 4 {
108 return self;
109 }
110
111 Self {
112 image_background_color: code,
113 ..self
114 }
115 }
116
117 /// Updates the shape of the image background. Takes an convert::ImageBackgroundShape.
118 pub fn image_background_shape(
119 self,
120 image_background_shape: convert::ImageBackgroundShape,
121 ) -> Self {
122 Self {
123 image_background_shape,
124 ..self
125 }
126 }
127
128 /// Updates the size of the image. (unit being module size).
129 pub fn image_size(self, size: f64) -> Self {
130 Self {
131 image_size: Some(size),
132 ..self
133 }
134 }
135
136 /// Updates the gap between background color and the image. (unit being module size).
137 pub fn image_gap(self, gap: f64) -> Self {
138 Self {
139 image_gap: Some(gap),
140 ..self
141 }
142 }
143
144 /// Updates the position of the image. Takes an array [x, y] (unit being module size).
145 pub fn image_position(self, image_position: Vec<f64>) -> Self {
146 if image_position.len() != 2 {
147 return self;
148 }
149
150 Self {
151 image_position,
152 ..self
153 }
154 }
155
156 /// Updates the error correction level of the QRCode (can increase the size of the QRCode)
157 pub fn ecl(self, ecl: ECL) -> Self {
158 Self {
159 ecl: Some(ecl),
160 ..self
161 }
162 }
163
164 /// Forces the version of the QRCode
165 pub fn version(self, version: Version) -> Self {
166 Self {
167 version: Some(version),
168 ..self
169 }
170 }
171 }
172
173 #[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
174 #[cfg(feature = "svg")]
175 impl SvgOptions {
176 /// Creates a new SvgOptions object.
177 #[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
178 pub fn new() -> Self {
179 Self {
180 shape: convert::Shape::Square,
181 module_color: vec![0, 0, 0, 255],
182 margin: 4,
183
184 ecl: None,
185 version: None,
186
187 background_color: vec![255, 255, 255, 255],
188
189 image: String::new(),
190 image_background_color: vec![255, 255, 255, 255],
191 image_background_shape: convert::ImageBackgroundShape::Square,
192 image_size: None,
193 image_gap: None,
194 image_position: vec![],
195 }
196 }
197 }
198
199 /// Generate a QR code from a string. All parameters are automatically set.
200 #[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
201 #[cfg(feature = "svg")]
202 pub fn qr_svg(content: &str, options: SvgOptions) -> String {
203 use crate::convert::svg::SvgBuilder;
204 use crate::convert::Builder;
205 let qrcode = QRCode::new(content.as_bytes(), options.ecl, options.version, None, None);
206
207 let mut builder = SvgBuilder::default();
208 builder.shape(options.shape);
209 builder.margin(options.margin);
210 builder.background_color(options.background_color);
211 builder.module_color(options.module_color);
212 if !options.image.is_empty() {
213 builder.image(options.image);
214 }
215
216 builder.image_background_color(options.image_background_color);
217 builder.image_background_shape(options.image_background_shape);
218
219 if let Some(image_size) = options.image_size {
220 builder.image_size(image_size);
221 }
222
223 if let Some(image_gap) = options.image_gap {
224 builder.image_gap(image_gap);
225 }
226
227 if options.image_position.len() == 2 {
228 let x = options.image_position[0];
229 let y = options.image_position[1];
230 builder.image_position(x, y);
231 }
232
233 qrcode
234 .map(|qrcode| builder.to_str(&qrcode))
235 .unwrap_or(String::new())
236 }