Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytm committed Jul 5, 2024
2 parents 832917c + 5a8fbdb commit d1b3426
Show file tree
Hide file tree
Showing 9 changed files with 468 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"]
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,36 @@ 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::<pic::Color>()
.min_dimensions(1, 1)
.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)
```
See [`test_annex_i_micro_qr_as_pic.pic`](src/test_annex_i_micro_qr_as_pic.pic) for a full example.
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
8 changes: 8 additions & 0 deletions examples/encode_pic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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);
}
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,25 @@ 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 @@ -5,6 +5,7 @@ use crate::types::Color;
use std::cmp::max;

pub mod image;
pub mod pic;
pub mod string;
pub mod svg;
pub mod unicode;
Expand Down
67 changes: 67 additions & 0 deletions src/render/pic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! PIC rendering support.
//!
//! # Example
//!
//! ```
//! use qrcode::QrCode;
//! use qrcode::render::pic;
//!
//! let code = QrCode::new(b"Hello").unwrap();
//! let pic = code.render::<pic::Color>().build();
//! println!("{}", pic);
#![cfg(feature = "pic")]

use std::fmt::Write;

use crate::render::{Canvas as RenderCanvas, Pixel};
use crate::types::Color as ModuleColor;

/// A PIC color.
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Color;

impl Pixel for Color {
type Canvas = Canvas;
type Image = String;

fn default_color(_color: ModuleColor) -> Self {
Color
}
}

#[doc(hidden)]
pub struct Canvas {
pic: String,
}

impl RenderCanvas for Canvas {
type Pixel = Color;
type Image = String;

fn new(width: u32, height: u32, _dark_pixel: Color, _light_pixel: Color) -> Self {
Canvas {
pic: format!(
concat!(
"maxpswid={w};maxpsht={h};movewid=0;moveht=1;boxwid=1;boxht=1\n",
"define p {{ box wid $3 ht $4 fill 1 with .nw at $1,-$2 }}\n",
"box wid maxpswid ht maxpsht with .nw at 0,0\n",
),
w = width,
h = height
),
}
}

fn draw_dark_pixel(&mut self, x: u32, y: u32) {
self.draw_dark_rect(x, y, 1, 1);
}

fn draw_dark_rect(&mut self, left: u32, top: u32, width: u32, height: u32) {
write!(self.pic, "p({left},{top},{width},{height})\n").unwrap();

Check warning on line 61 in src/render/pic.rs

View workflow job for this annotation

GitHub Actions / clippy

using `write!()` with a format string that ends in a single newline

warning: using `write!()` with a format string that ends in a single newline --> src/render/pic.rs:61:9 | 61 | write!(self.pic, "p({left},{top},{width},{height})\n").unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline = note: `#[warn(clippy::write_with_newline)]` on by default help: use `writeln!` instead | 61 - write!(self.pic, "p({left},{top},{width},{height})\n").unwrap(); 61 + writeln!(self.pic, "p({left},{top},{width},{height})").unwrap(); |
}

fn into_image(mut self) -> String {

Check warning on line 64 in src/render/pic.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable

Check warning on line 64 in src/render/pic.rs

View workflow job for this annotation

GitHub Actions / Check (1.67.1)

variable does not need to be mutable

Check warning on line 64 in src/render/pic.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

variable does not need to be mutable

Check warning on line 64 in src/render/pic.rs

View workflow job for this annotation

GitHub Actions / clippy

variable does not need to be mutable

warning: variable does not need to be mutable --> src/render/pic.rs:64:19 | 64 | fn into_image(mut self) -> String { | ----^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default
self.pic
}
}
96 changes: 96 additions & 0 deletions src/test_annex_i_micro_qr_as_pic.pic
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
maxpswid=17;maxpsht=17;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(2,2,1,1)
p(3,2,1,1)
p(4,2,1,1)
p(5,2,1,1)
p(6,2,1,1)
p(7,2,1,1)
p(8,2,1,1)
p(10,2,1,1)
p(12,2,1,1)
p(14,2,1,1)
p(2,3,1,1)
p(8,3,1,1)
p(10,3,1,1)
p(11,3,1,1)
p(12,3,1,1)
p(14,3,1,1)
p(2,4,1,1)
p(4,4,1,1)
p(5,4,1,1)
p(6,4,1,1)
p(8,4,1,1)
p(11,4,1,1)
p(12,4,1,1)
p(14,4,1,1)
p(2,5,1,1)
p(4,5,1,1)
p(5,5,1,1)
p(6,5,1,1)
p(8,5,1,1)
p(11,5,1,1)
p(12,5,1,1)
p(13,5,1,1)
p(14,5,1,1)
p(2,6,1,1)
p(4,6,1,1)
p(5,6,1,1)
p(6,6,1,1)
p(8,6,1,1)
p(10,6,1,1)
p(11,6,1,1)
p(12,6,1,1)
p(2,7,1,1)
p(8,7,1,1)
p(10,7,1,1)
p(14,7,1,1)
p(2,8,1,1)
p(3,8,1,1)
p(4,8,1,1)
p(5,8,1,1)
p(6,8,1,1)
p(7,8,1,1)
p(8,8,1,1)
p(11,8,1,1)
p(12,8,1,1)
p(13,8,1,1)
p(14,8,1,1)
p(11,9,1,1)
p(12,9,1,1)
p(2,10,1,1)
p(3,10,1,1)
p(5,10,1,1)
p(10,10,1,1)
p(14,10,1,1)
p(3,11,1,1)
p(4,11,1,1)
p(6,11,1,1)
p(8,11,1,1)
p(10,11,1,1)
p(12,11,1,1)
p(14,11,1,1)
p(2,12,1,1)
p(3,12,1,1)
p(4,12,1,1)
p(7,12,1,1)
p(8,12,1,1)
p(9,12,1,1)
p(10,12,1,1)
p(11,12,1,1)
p(12,12,1,1)
p(13,12,1,1)
p(5,13,1,1)
p(7,13,1,1)
p(12,13,1,1)
p(13,13,1,1)
p(2,14,1,1)
p(3,14,1,1)
p(4,14,1,1)
p(6,14,1,1)
p(9,14,1,1)
p(10,14,1,1)
p(12,14,1,1)
p(13,14,1,1)
p(14,14,1,1)
Loading

0 comments on commit d1b3426

Please sign in to comment.