Skip to content

Commit 7073daa

Browse files
adding qr node
1 parent fa45efa commit 7073daa

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ spin = "0.10"
239239
clap = "4.5"
240240
spirv-std = { git = "https://github.com/Firestar99/rust-gpu-new", rev = "c12f216121820580731440ee79ebc7403d6ea04f", features = ["bytemuck"] }
241241
cargo-gpu = { git = "https://github.com/Firestar99/cargo-gpu", rev = "3952a22d16edbd38689f3a876e417899f21e1fe7", default-features = false }
242+
qrcodegen = "1.8"
242243

243244
[workspace.lints.rust]
244245
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] }

node-graph/nodes/vector/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ kurbo = { workspace = true }
2323
rand = { workspace = true }
2424
rustc-hash = { workspace = true }
2525
log = { workspace = true }
26+
qrcodegen = { workspace = true }
2627

2728
# Optional workspace dependencies
2829
serde = { workspace = true, optional = true }

node-graph/nodes/vector/src/generator_nodes.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,33 @@ fn star<T: AsU64>(
186186
Table::new_from_element(Vector::from_subpath(subpath::Subpath::new_star_polygon(DVec2::splat(-diameter), points, diameter, inner_diameter)))
187187
}
188188

189+
/// Generates a QR code from the input text.
190+
#[node_macro::node(category("Vector: Shape"))]
191+
fn qr_code(_: impl Ctx, _primary: (), #[default("https://graphite.art")] text: String
192+
) -> Table<Vector> {
193+
let ecc = qrcodegen::QrCodeEcc::Medium;
194+
195+
let Ok(qr_code) = qrcodegen::QrCode::encode_text(&text, ecc) else {
196+
return Table::default();
197+
};
198+
199+
let size = qr_code.size();
200+
let mut vector = Vector::default();
201+
let offset = DVec2::splat(size as f64 / -2.);
202+
203+
for y in 0..size {
204+
for x in 0..size {
205+
if qr_code.get_module(x, y) {
206+
let corner1 = offset + DVec2::new(x as f64, y as f64);
207+
let corner2 = corner1 + DVec2::splat(1.);
208+
vector.append_subpath(subpath::Subpath::new_rect(corner1, corner2), false);
209+
}
210+
}
211+
}
212+
213+
Table::new_from_element(vector)
214+
}
215+
189216
/// Generates a line with endpoints at the two chosen coordinates.
190217
#[node_macro::node(category("Vector: Shape"))]
191218
fn line(
@@ -349,4 +376,11 @@ mod tests {
349376
assert!([90., 150., 40.].into_iter().any(|target| (target - angle).abs() < 1e-10), "unexpected angle of {angle}")
350377
}
351378
}
379+
380+
#[test]
381+
fn qr_code_test() {
382+
let qr = qr_code((), (), "https://graphite.art".to_string());
383+
assert!(qr.iter().next().unwrap().element.point_domain.ids().len() > 0);
384+
assert!(qr.iter().next().unwrap().element.segment_domain.ids().len() > 0);
385+
}
352386
}

0 commit comments

Comments
 (0)