Skip to content

Commit

Permalink
pic: initial support
Browse files Browse the repository at this point in the history
  • Loading branch information
afh committed Jul 2, 2024
1 parent 7aaa476 commit cb26cf2
Show file tree
Hide file tree
Showing 9 changed files with 690 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ image = { version = "0.25", default-features = false, optional = true }
image = "0.25"

[features]
default = ["image", "svg"]
default = ["image", "svg", "pic"]
bench = []
svg = []
pic = []

[[bin]]
name = "qrencode"
Expand All @@ -41,3 +42,7 @@ name = "encode_string"
[[example]]
name = "encode_svg"
required-features = ["svg"]

[[example]]
name = "encode_pic"
required-features = ["pic"]
244 changes: 244 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,247 @@ Generates this output:
█████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
```

## PIC generation

```rust
use qrcode::render::pic;
use qrcode::QrCode;

fn main() {
let code = QrCode::new(b"01234567").unwrap();
let image = code
.render()
.min_dimensions(1, 1)
.dark_color(pic::Color("#800000"))
.light_color(pic::Color("#ffff80"))
.build();
println!("{}", image);
}
```

Generates [PIC](https://en.wikipedia.org/wiki/PIC_(markup_language))
output that renders as follows:

```pic
maxpswid=29;maxpsht=29;movewid=0;moveht=1;boxwid=1;boxht=1
define p { box wid $3 ht $4 fill 1 with .nw at $1,-$2 }
box wid maxpswid ht maxpsht with .nw at 0,0
p(4,4,1,1)
p(5,4,1,1)
p(6,4,1,1)
p(7,4,1,1)
p(8,4,1,1)
p(9,4,1,1)
p(10,4,1,1)
p(13,4,1,1)
p(15,4,1,1)
p(16,4,1,1)
p(18,4,1,1)
p(19,4,1,1)
p(20,4,1,1)
p(21,4,1,1)
p(22,4,1,1)
p(23,4,1,1)
p(24,4,1,1)
p(4,5,1,1)
p(10,5,1,1)
p(13,5,1,1)
p(14,5,1,1)
p(15,5,1,1)
p(16,5,1,1)
p(18,5,1,1)
p(24,5,1,1)
p(4,6,1,1)
p(6,6,1,1)
p(7,6,1,1)
p(8,6,1,1)
p(10,6,1,1)
p(12,6,1,1)
p(18,6,1,1)
p(20,6,1,1)
p(21,6,1,1)
p(22,6,1,1)
p(24,6,1,1)
p(4,7,1,1)
p(6,7,1,1)
p(7,7,1,1)
p(8,7,1,1)
p(10,7,1,1)
p(12,7,1,1)
p(13,7,1,1)
p(18,7,1,1)
p(20,7,1,1)
p(21,7,1,1)
p(22,7,1,1)
p(24,7,1,1)
p(4,8,1,1)
p(6,8,1,1)
p(7,8,1,1)
p(8,8,1,1)
p(10,8,1,1)
p(12,8,1,1)
p(14,8,1,1)
p(15,8,1,1)
p(16,8,1,1)
p(18,8,1,1)
p(20,8,1,1)
p(21,8,1,1)
p(22,8,1,1)
p(24,8,1,1)
p(4,9,1,1)
p(10,9,1,1)
p(12,9,1,1)
p(16,9,1,1)
p(18,9,1,1)
p(24,9,1,1)
p(4,10,1,1)
p(5,10,1,1)
p(6,10,1,1)
p(7,10,1,1)
p(8,10,1,1)
p(9,10,1,1)
p(10,10,1,1)
p(12,10,1,1)
p(14,10,1,1)
p(16,10,1,1)
p(18,10,1,1)
p(19,10,1,1)
p(20,10,1,1)
p(21,10,1,1)
p(22,10,1,1)
p(23,10,1,1)
p(24,10,1,1)
p(12,11,1,1)
p(15,11,1,1)
p(16,11,1,1)
p(4,12,1,1)
p(6,12,1,1)
p(7,12,1,1)
p(8,12,1,1)
p(9,12,1,1)
p(10,12,1,1)
p(13,12,1,1)
p(16,12,1,1)
p(18,12,1,1)
p(19,12,1,1)
p(20,12,1,1)
p(21,12,1,1)
p(22,12,1,1)
p(7,13,1,1)
p(9,13,1,1)
p(11,13,1,1)
p(12,13,1,1)
p(14,13,1,1)
p(16,13,1,1)
p(19,13,1,1)
p(21,13,1,1)
p(22,13,1,1)
p(6,14,1,1)
p(10,14,1,1)
p(11,14,1,1)
p(13,14,1,1)
p(15,14,1,1)
p(17,14,1,1)
p(20,14,1,1)
p(21,14,1,1)
p(22,14,1,1)
p(23,14,1,1)
p(24,14,1,1)
p(8,15,1,1)
p(13,15,1,1)
p(19,15,1,1)
p(20,15,1,1)
p(21,15,1,1)
p(22,15,1,1)
p(7,16,1,1)
p(8,16,1,1)
p(9,16,1,1)
p(10,16,1,1)
p(11,16,1,1)
p(12,16,1,1)
p(15,16,1,1)
p(17,16,1,1)
p(20,16,1,1)
p(12,17,1,1)
p(14,17,1,1)
p(15,17,1,1)
p(16,17,1,1)
p(17,17,1,1)
p(18,17,1,1)
p(21,17,1,1)
p(22,17,1,1)
p(4,18,1,1)
p(5,18,1,1)
p(6,18,1,1)
p(7,18,1,1)
p(8,18,1,1)
p(9,18,1,1)
p(10,18,1,1)
p(13,18,1,1)
p(14,18,1,1)
p(16,18,1,1)
p(18,18,1,1)
p(19,18,1,1)
p(4,19,1,1)
p(10,19,1,1)
p(12,19,1,1)
p(14,19,1,1)
p(15,19,1,1)
p(16,19,1,1)
p(17,19,1,1)
p(18,19,1,1)
p(22,19,1,1)
p(24,19,1,1)
p(4,20,1,1)
p(6,20,1,1)
p(7,20,1,1)
p(8,20,1,1)
p(10,20,1,1)
p(12,20,1,1)
p(16,20,1,1)
p(19,20,1,1)
p(21,20,1,1)
p(22,20,1,1)
p(4,21,1,1)
p(6,21,1,1)
p(7,21,1,1)
p(8,21,1,1)
p(10,21,1,1)
p(12,21,1,1)
p(13,21,1,1)
p(16,21,1,1)
p(19,21,1,1)
p(4,22,1,1)
p(6,22,1,1)
p(7,22,1,1)
p(8,22,1,1)
p(10,22,1,1)
p(12,22,1,1)
p(14,22,1,1)
p(15,22,1,1)
p(17,22,1,1)
p(20,22,1,1)
p(22,22,1,1)
p(4,23,1,1)
p(10,23,1,1)
p(19,23,1,1)
p(20,23,1,1)
p(22,23,1,1)
p(23,23,1,1)
p(4,24,1,1)
p(5,24,1,1)
p(6,24,1,1)
p(7,24,1,1)
p(8,24,1,1)
p(9,24,1,1)
p(10,24,1,1)
p(12,24,1,1)
p(13,24,1,1)
p(14,24,1,1)
p(15,24,1,1)
p(17,24,1,1)
p(20,24,1,1)
p(22,24,1,1)
```
16 changes: 16 additions & 0 deletions examples/encode_pic.roff
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.\" To convert this file to pdf use:
.\" groff -Tpdf -P-p14.5c,14.5c -p encode_pic.roff > encode_pic.pdf
.
.po -1i
.vs 0
.
.\" For most flexibility include the QR-Code using copy
.\" and define your preferred picture scale, e.g.:
.PS
# To make the QR-Code smaller make scale a larger number
# To make the QR-Code bigger make scale a smaller number
scale=2.54*2
# To generate encode_pic.pic run: cargo run --example encode_pic > encode_pic.pic
copy "encode_pic.pic"
.PE
.ex
11 changes: 11 additions & 0 deletions examples/encode_pic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use qrcode::render::pic;
use qrcode::QrCode;

fn main() {
let code = QrCode::new(b"01234567").unwrap();
let image = code
.render::<pic::Color>()
.min_dimensions(1, 1)
.build();
println!("{}", image);
}
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,28 @@ mod svg_tests {
assert_eq!(&image, expected);
}
}

#[cfg(all(test, feature = "pic"))]
mod pic_tests {
use crate::render::pic::Color as PicColor;
use crate::{EcLevel, QrCode, Version};

#[test]
fn test_annex_i_qr_as_pic() {
let code = QrCode::new(b"01234567").unwrap();
let image = code.render::<PicColor>().build();
let expected = include_str!("test_annex_i_qr_as_pic.pic");
assert_eq!(&image, expected);
}

#[test]
fn test_annex_i_micro_qr_as_pic() {
let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
let image = code
.render::<PicColor>()
.min_dimensions(1, 1)
.build();
let expected = include_str!("test_annex_i_micro_qr_as_pic.pic");
assert_eq!(&image, expected);
}
}
1 change: 1 addition & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::cmp::max;
pub mod image;
pub mod string;
pub mod svg;
pub mod pic;
pub mod unicode;

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit cb26cf2

Please sign in to comment.