use super::{Canvas, alignment::ALIGNMENT_PATTERN_POSITIONS};
use crate::types::{Color, Version};
impl Canvas {
fn draw_line(
&mut self,
x1: i16,
y1: i16,
x2: i16,
y2: i16,
color_even: Color,
color_odd: Color,
) {
debug_assert!(x1 == x2 || y1 == y2);
if y1 == y2 {
for x in x1..=x2 {
self.put(x, y1, if x % 2 == 0 { color_even } else { color_odd });
}
} else {
for y in y1..=y2 {
self.put(x1, y, if y % 2 == 0 { color_even } else { color_odd });
}
}
}
fn draw_rmqr_line(&mut self) {
let (width, height) = (self.width, self.height);
self.draw_line(8, 0, width - 3, 0, Color::Dark, Color::Light);
if height == 7 {
self.draw_line(
8,
height - 1,
width - 6,
height - 1,
Color::Dark,
Color::Light,
);
} else {
self.draw_line(
3,
height - 1,
width - 6,
height - 1,
Color::Dark,
Color::Light,
);
}
if height >= 11 {
self.draw_line(0, 8, 0, height - 3, Color::Dark, Color::Light);
}
if height >= 9 {
self.draw_line(
width - 1,
2,
width - 1,
height - 6,
Color::Dark,
Color::Light,
);
}
let position_index = self.version.rect_micro_width_index().unwrap() + 34;
for x in ALIGNMENT_PATTERN_POSITIONS[position_index] {
self.draw_line(*x, 3, *x, height - 4, Color::Dark, Color::Light);
}
}
pub(super) fn draw_timing_patterns(&mut self) {
if let Version::RectMicro(..) = self.version {
self.draw_rmqr_line();
} else {
let width = self.width;
let (y, x1, x2) = match self.version {
Version::Micro(_) => (0, 8, width - 1),
Version::Normal(_) => (6, 8, width - 9),
Version::RectMicro(..) => unreachable!(),
};
self.draw_line(x1, y, x2, y, Color::Dark, Color::Light);
self.draw_line(y, x1, y, x2, Color::Dark, Color::Light);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::EcLevel;
#[test]
fn test_draw_timing_patterns_qr() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(
c.to_debug_str(),
concat!(
"\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"????????#.#.#????????\n",
"?????????????????????\n",
"??????#??????????????\n",
"??????.??????????????\n",
"??????#??????????????\n",
"??????.??????????????\n",
"??????#??????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????\n",
"?????????????????????"
)
);
}
#[test]
fn test_draw_timing_patterns_micro_qr() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(
c.to_debug_str(),
concat!(
"\n",
"????????#.#\n",
"???????????\n",
"???????????\n",
"???????????\n",
"???????????\n",
"???????????\n",
"???????????\n",
"???????????\n",
"#??????????\n",
".??????????\n",
"#??????????"
)
);
}
#[test]
fn test_draw_timing_patterns_rmqr_7x77() {
let mut c = Canvas::new(Version::RectMicro(7, 77), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(
c.to_debug_str(),
concat!(
"\n",
"????????#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#??\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"?????????????????????????.?????????????????????????.?????????????????????????\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"????????#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.?????"
)
);
}
#[test]
fn test_draw_timing_patterns_rmqr_9x77() {
let mut c = Canvas::new(Version::RectMicro(9, 77), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(
c.to_debug_str(),
concat!(
"\n",
"????????#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#??\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"????????????????????????????????????????????????????????????????????????????#\n",
"?????????????????????????.?????????????????????????.????????????????????????.\n",
"?????????????????????????#?????????????????????????#?????????????????????????\n",
"?????????????????????????.?????????????????????????.?????????????????????????\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"???.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.?????"
)
);
}
#[test]
fn test_draw_timing_patterns_rmqr_11x77() {
let mut c = Canvas::new(Version::RectMicro(11, 77), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(
c.to_debug_str(),
concat!(
"\n",
"????????#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#??\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"????????????????????????????????????????????????????????????????????????????#\n",
"?????????????????????????.?????????????????????????.????????????????????????.\n",
"?????????????????????????#?????????????????????????#????????????????????????#\n",
"?????????????????????????.?????????????????????????.????????????????????????.\n",
"?????????????????????????#?????????????????????????#?????????????????????????\n",
"?????????????????????????.?????????????????????????.?????????????????????????\n",
"#????????????????????????????????????????????????????????????????????????????\n",
"?????????????????????????????????????????????????????????????????????????????\n",
"???.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.?????"
)
);
}
}