utf-8: renamed canvas and pixel

This commit is contained in:
Vladimir Serov 2020-02-11 10:55:05 +03:00
parent 5b719f726e
commit b9d27ef174

View file

@ -5,22 +5,22 @@ use crate::render::{Canvas as RenderCanvas, Pixel, Color};
const CODEPAGE: [&str; 4] = [" ","\u{2584}","\u{2580}","\u{2588}"]; const CODEPAGE: [&str; 4] = [" ","\u{2584}","\u{2580}","\u{2588}"];
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum Unicode1x2 { pub enum Dense1x2 {
Dark, Light Dark, Light
} }
impl Pixel for Unicode1x2 { impl Pixel for Dense1x2 {
type Image = String; type Image = String;
type Canvas = Canvas; type Canvas = Canvas1x2;
fn default_color(color: Color) -> Unicode1x2 { color.select(Unicode1x2::Dark, Unicode1x2::Light) } fn default_color(color: Color) -> Dense1x2 { color.select(Dense1x2::Dark, Dense1x2::Light) }
fn default_unit_size() -> (u32, u32) { (1, 1) } fn default_unit_size() -> (u32, u32) { (1, 1) }
} }
impl Unicode1x2 { impl Dense1x2 {
fn value(&self) -> u8 { fn value(&self) -> u8 {
match self { match self {
Unicode1x2::Dark => {1} Dense1x2::Dark => {1}
Unicode1x2::Light => {0} Dense1x2::Light => {0}
} }
} }
fn parse_2_bits(sym: &u8) -> &'static str { fn parse_2_bits(sym: &u8) -> &'static str {
@ -28,21 +28,21 @@ impl Unicode1x2 {
} }
} }
pub struct Canvas { pub struct Canvas1x2 {
canvas: Vec<u8>, canvas: Vec<u8>,
width: u32, width: u32,
dark_pixel: u8 dark_pixel: u8
} }
impl RenderCanvas for Canvas { impl RenderCanvas for Canvas1x2 {
type Pixel = Unicode1x2; type Pixel = Dense1x2;
type Image = String; type Image = String;
fn new(width: u32, height: u32, dark_pixel: Unicode1x2, light_pixel: Unicode1x2) -> Self { fn new(width: u32, height: u32, dark_pixel: Dense1x2, light_pixel: Dense1x2) -> Self {
let a = vec![light_pixel.value(); (width * height) as usize]; let a = vec![light_pixel.value(); (width * height) as usize];
Canvas { Canvas1x2 {
width: width, width: width,
canvas: a, canvas: a,
dark_pixel: dark_pixel.value() dark_pixel: dark_pixel.value()
@ -71,7 +71,7 @@ impl RenderCanvas for Canvas {
} }
.iter() .iter()
// Mapping those 2-bit numbers to corresponding pixels. // Mapping those 2-bit numbers to corresponding pixels.
.map(Unicode1x2::parse_2_bits) .map(Dense1x2::parse_2_bits)
.collect::<Vec<&str>>() .collect::<Vec<&str>>()
.concat() .concat()
) )
@ -84,11 +84,11 @@ impl RenderCanvas for Canvas {
fn test_render_to_utf8_string() { fn test_render_to_utf8_string() {
use crate::render::Renderer; use crate::render::Renderer;
let colors = &[Color::Dark, Color::Light, Color::Light, Color::Dark]; let colors = &[Color::Dark, Color::Light, Color::Light, Color::Dark];
let image: String = Renderer::<Unicode1x2>::new(colors, 2, 1).build(); let image: String = Renderer::<Dense1x2>::new(colors, 2, 1).build();
assert_eq!(&image, "\n"); assert_eq!(&image, "\n");
let image2 = Renderer::<Unicode1x2>::new(colors, 2, 1).module_dimensions(2, 2).build(); let image2 = Renderer::<Dense1x2>::new(colors, 2, 1).module_dimensions(2, 2).build();
assert_eq!(&image2, " \n ██ \n ██ \n "); assert_eq!(&image2, " \n ██ \n ██ \n ");
} }
@ -96,12 +96,12 @@ fn test_render_to_utf8_string() {
#[test] #[test]
fn integration_render_utf8_1x2() { fn integration_render_utf8_1x2() {
use crate::{QrCode, Version, EcLevel}; use crate::{QrCode, Version, EcLevel};
use crate::render::utf8::Unicode1x2; use crate::render::unicode::Dense1x2;
let code = QrCode::with_version(b"12345678", Version::Micro(2), EcLevel::L).unwrap(); let code = QrCode::with_version(b"12345678", Version::Micro(2), EcLevel::L).unwrap();
let image = code.render::<Unicode1x2>() let image = code.render::<Dense1x2>()
.dark_color(Unicode1x2::Light) .dark_color(Dense1x2::Light)
.light_color(Unicode1x2::Dark) .light_color(Dense1x2::Dark)
.module_dimensions(1, 1) .module_dimensions(1, 1)
.build(); .build();
assert_eq!("█████████████████\n██ ▄▄▄▄▄ █▄▀▄█▄██\n██ █ █ █ █ ██\n██ █▄▄▄█ █▄▄██▀██\n██▄▄▄▄▄▄▄█▄▄▄▀ ██\n██▄ ▀ ▀ ▀▄▄ ████\n██▄▄▀▄█ ▀▀▀ ▀▄▄██\n██▄▄▄█▄▄█▄██▄█▄██\n▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀", image); assert_eq!("█████████████████\n██ ▄▄▄▄▄ █▄▀▄█▄██\n██ █ █ █ █ ██\n██ █▄▄▄█ █▄▄██▀██\n██▄▄▄▄▄▄▄█▄▄▄▀ ██\n██▄ ▀ ▀ ▀▄▄ ████\n██▄▄▀▄█ ▀▀▀ ▀▄▄██\n██▄▄▄█▄▄█▄██▄█▄██\n▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀", image);