Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utf-8: initial support #42

Merged
merged 17 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,40 @@ fn main() {

Generates this SVG:

[![Output](src/test_annex_i_micro_qr_as_svg.svg)](src/test_annex_i_micro_qr_as_svg.svg)
[![Output](src/test_annex_i_micro_qr_as_svg.svg)](src/test_annex_i_micro_qr_as_svg.svg)

## Unicode string generation

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

fn main() {
let code = QrCode::new("mow mow").unwrap();
let image = code.render::<utf8::Unicode1x2>()
.dark_color(utf8::Unicode1x2::Light)
.light_color(utf8::Unicode1x2::Dark)
.build();
println!("{}", image);
}
```

Generates this output:

```
cab404 marked this conversation as resolved.
Show resolved Hide resolved
█████████████████████████████
█████████████████████████████
████ ▄▄▄▄▄ █ ▀▀▀▄█ ▄▄▄▄▄ ████
████ █ █ █▀ ▀ ▀█ █ █ ████
████ █▄▄▄█ ██▄ ▀█ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ ▀▄▀ █▄▄▄▄▄▄▄████
████▄▀ ▄▀ ▄ █▄█ ▀ ▀█ █▄ ████
████▄██▄▄▀▄▄▀█▄ ██▀▀█▀▄▄▄████
█████▄▄▄█▄▄█ ▀▀▄█▀▀▀▄█▄▄████
████ ▄▄▄▄▄ █ ▄▄██▄ ▄ ▀▀████
████ █ █ █▀▄▄▀▄▄ ▄▄▄▄ ▄████
████ █▄▄▄█ █▄ █▄▀▄▀██▄█▀████
████▄▄▄▄▄▄▄█▄████▄█▄██▄██████
█████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
```
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 utf8;
cab404 marked this conversation as resolved.
Show resolved Hide resolved

//------------------------------------------------------------------------------
//{{{ Pixel trait
Expand Down
110 changes: 110 additions & 0 deletions src/render/utf8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! UTF-8 rendering, with 2 pixels per symbol.

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

const CODEPAGE: [&str; 4] = [" ","\u{2584}","\u{2580}","\u{2588}"];

#[derive(Copy, Clone, PartialEq)]
pub enum Unicode1x2 {
Dark, Light
}

impl Pixel for Unicode1x2 {
type Image = String;
type Canvas = Canvas;
fn default_color(color: Color) -> Unicode1x2 { color.select(Unicode1x2::Dark, Unicode1x2::Light) }
fn default_unit_size() -> (u32, u32) { (1, 1) }
}

impl Unicode1x2 {
fn value(&self) -> u8 {
match self {
Unicode1x2::Dark => {1}
Unicode1x2::Light => {0}
}
}
#[doc(hidden)]
fn parse_2_bits(sym: &u8) -> &'static str {
cab404 marked this conversation as resolved.
Show resolved Hide resolved
CODEPAGE[*sym as usize]
}
}

pub struct Canvas {
cab404 marked this conversation as resolved.
Show resolved Hide resolved
canvas: Vec<u8>,
width: u32,
dark_pixel: u8
}

impl RenderCanvas for Canvas {

type Pixel = Unicode1x2;
type Image = String;


fn new(width: u32, height: u32, dark_pixel: Unicode1x2, light_pixel: Unicode1x2) -> Self {
let a = vec![light_pixel.value(); (width * height) as usize];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an assert! that (width+1)/2 == height

Copy link
Contributor Author

@cab404 cab404 Feb 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why? It works on non-rectangle canvas just fine :| I can make test cases for that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other non-text canvas, the width and height can be independently scaled and result in rectangular modules. This is not the case for 1x2 since the width:height ration is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and also my implementation doesn't store it on per-symbol basis :|

Canvas {
width: width,
canvas: a,
dark_pixel: dark_pixel.value()
}
}

fn draw_dark_pixel(&mut self, x: u32, y: u32) {
self.canvas[(x + y * self.width) as usize] = self.dark_pixel;
}

fn into_image(self) -> String {
self.canvas
// Chopping array into 1-line sized fragments
.chunks_exact(self.width as usize)
.collect::<Vec<&[u8]>>()
// And then glueing every 2 lines.
.chunks(2)
.map(|rows|
{
// Then zipping those 2 lines together into a single 2-bit number list.
if rows.len() == 2 {
rows[0].iter().zip(rows[1]).map(|(top,bot)| (top * 2 + bot)).collect::<Vec<u8>>()
} else {
rows[0].iter().map(|top| (top * 2)).collect::<Vec<u8>>()
}
}
.iter()
// Mapping those 2-bit numbers to corresponding pixels.
.map(Unicode1x2::parse_2_bits)
.collect::<Vec<&str>>()
.concat()
)
.collect::<Vec<String>>()
.join("\n")
}
}

#[test]
fn test_render_to_utf8_string() {
use crate::render::Renderer;
let colors = &[Color::Dark, Color::Light, Color::Light, Color::Dark];
let image: String = Renderer::<Unicode1x2>::new(colors, 2, 1).build();

assert_eq!(&image, " ▄ \n ▀ ");

let image2 = Renderer::<Unicode1x2>::new(colors, 2, 1).module_dimensions(2, 2).build();

assert_eq!(&image2, " \n ██ \n ██ \n ");
}

#[test]
fn integration_render_utf8_1x2() {
use crate::{QrCode, Version, EcLevel};
use crate::render::utf8::Unicode1x2;

let code = QrCode::with_version(b"12345678", Version::Micro(2), EcLevel::L).unwrap();
let image = code.render::<Unicode1x2>()
.dark_color(Unicode1x2::Light)
.light_color(Unicode1x2::Dark)
.module_dimensions(1, 1)
.build();
assert_eq!("█████████████████\n██ ▄▄▄▄▄ █▄▀▄█▄██\n██ █ █ █ █ ██\n██ █▄▄▄█ █▄▄██▀██\n██▄▄▄▄▄▄▄█▄▄▄▀ ██\n██▄ ▀ ▀ ▀▄▄ ████\n██▄▄▀▄█ ▀▀▀ ▀▄▄██\n██▄▄▄█▄▄█▄██▄█▄██\n▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀", image);
cab404 marked this conversation as resolved.
Show resolved Hide resolved

}