feat: 顶层 API + PNG/SVG/ASCII 渲染器
This commit is contained in:
@@ -1 +1,27 @@
|
||||
// FIXME: ASCII 渲染器 — Task 10
|
||||
use crate::qr::QrCode;
|
||||
|
||||
/// 终端 ASCII 渲染:██ 表示暗模块," " 表示亮模块
|
||||
pub fn render_ascii(qr: &QrCode, invert: bool) -> String {
|
||||
let size = qr.size() as usize;
|
||||
let margin = qr.margin as usize;
|
||||
let total = size + 2 * margin;
|
||||
|
||||
let dark_char = if invert { " " } else { "██" };
|
||||
let light_char = if invert { "██" } else { " " };
|
||||
|
||||
let mut result = String::new();
|
||||
for y in 0..total {
|
||||
for x in 0..total {
|
||||
let mx = x.saturating_sub(margin);
|
||||
let my = y.saturating_sub(margin);
|
||||
let is_dark = if mx < size && my < size {
|
||||
qr.modules()[my][mx]
|
||||
} else {
|
||||
false
|
||||
};
|
||||
result.push_str(if is_dark { dark_char } else { light_char });
|
||||
}
|
||||
result.push('\n');
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user