summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Wiese <tobias@tobiaswiese.com>2026-01-27 00:31:06 +0100
committerTobias Wiese <tobias@tobiaswiese.com>2026-01-27 01:11:06 +0100
commit31f39660aa62d8e451f43bc32c994eba6e61e972 (patch)
tree79355039a5c10d8d243e668b565583ef6233980b
parent45d7335d8e18410728a0384fd6456c609aec3be8 (diff)
svg: convert data matrix to svgHEADv0.2.0master
-rw-r--r--Cargo.lock9
-rw-r--r--Cargo.toml10
-rw-r--r--examples/dmtx_svg.rs14
-rw-r--r--src/lib.rs24
4 files changed, 55 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0e54f58..f944d28 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,9 +4,10 @@ version = 4
[[package]]
name = "iec16022"
-version = "0.1.0"
+version = "0.2.0"
dependencies = [
"libc",
+ "svg",
]
[[package]]
@@ -14,3 +15,9 @@ name = "libc"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "Could not get crate checksum"
+
+[[package]]
+name = "svg"
+version = "0.16.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "583e1c5c326fd6fede8797006de3b95ad6bcd60a592952952c5ba7ddd7e84c83"
diff --git a/Cargo.toml b/Cargo.toml
index c2fd8ba..618f995 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,8 +1,16 @@
[package]
name = "iec16022"
-version = "0.1.0"
+version = "0.2.0"
edition = "2024"
license = "GPL-3.0-or-later"
+[[example]]
+name = "dmtx_svg"
+required-features = ["svg"]
+
[dependencies]
libc = "0.2"
+svg = { version = "0.16", optional = true }
+
+[features]
+svg = ["dep:svg"]
diff --git a/examples/dmtx_svg.rs b/examples/dmtx_svg.rs
new file mode 100644
index 0000000..5a3915a
--- /dev/null
+++ b/examples/dmtx_svg.rs
@@ -0,0 +1,14 @@
+use iec16022::DataMatrix;
+use std::env::args_os;
+use std::os::unix::ffi::OsStringExt;
+
+fn main() {
+ let data = args_os()
+ .skip(1) // program name
+ .map(|arg| arg.into_vec())
+ .collect::<Vec<_>>()
+ .join(&b' ');
+ let dmtx = DataMatrix::encode(&data).unwrap();
+
+ print!("{}", dmtx.svg());
+}
diff --git a/src/lib.rs b/src/lib.rs
index ddb4405..e53ea75 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -108,6 +108,30 @@ impl DataMatrix {
let idx = row * self.width + col;
self.grid()[idx]
}
+
+ #[cfg(feature = "svg")]
+ pub fn svg(&self) -> svg::Document {
+ use svg::node::element::Rectangle;
+
+ let mut svg = svg::Document::new()
+ // -1, -1 for quiet zone
+ // +2 for size of quiet zone
+ .set("viewBox", format!("-1 -1 {} {}", self.width() + 2, self.height() + 2));
+ for row in 0..self.height() {
+ for col in 0..self.width() {
+ if self.get(row, col) {
+ svg = svg.add(
+ Rectangle::new()
+ .set("width", "1")
+ .set("height", "1")
+ .set("x", col)
+ .set("y", self.height() - row - 1),
+ );
+ }
+ }
+ }
+ svg
+ }
}
impl Drop for DataMatrix {