Skip to content

Instantly share code, notes, and snippets.

@kenkoooo
Created May 24, 2022 09:58
Show Gist options
  • Save kenkoooo/90cd03d56cc9fa2a311ff8bd34977253 to your computer and use it in GitHub Desktop.
Save kenkoooo/90cd03d56cc9fa2a311ff8bd34977253 to your computer and use it in GitHub Desktop.
use svg::{
node::{
self,
element::{path::Data, Path, Text},
},
Document,
};
const WIDTH: f64 = 70.0;
const STROKE_WIDTH: f64 = 0.6;
const STROKE_COLOR: &str = "#cccccc";
const KANJI_X: f64 = WIDTH * 0.05;
const KANJI_Y: f64 = WIDTH * 0.8;
const KANJI_SIZE: f64 = WIDTH * 0.9;
const KANJI: [&str; 3] = [
"閑散全廃安逸巧拙併読",
"媒介寡少頑丈窮迫献身",
"自粛空疎壮健奪還逐次",
];
fn main() {
const UP: f64 = WIDTH / 2.;
const LEFT: f64 = WIDTH * 0.8;
for (page, kanji) in KANJI.iter().enumerate() {
let mut document = Document::new()
.set("viewbox", (0, 0, 210, 297))
.set("width", "210mm")
.set("height", "297mm");
let kanji = kanji.chars().collect::<Vec<_>>();
for row in 0..15 {
for col in 0..10 {
document = append_kanji(
document,
col as f64 * WIDTH + LEFT,
row as f64 * WIDTH + UP,
kanji[kanji.len() - 1 - col],
);
}
}
let path = format!("/home/kenkoooo/Desktop/kanji-{}.svg", page);
svg::save(path, &document).unwrap();
}
}
fn append_kanji(document: node::element::SVG, x: f64, y: f64, kanji: char) -> node::element::SVG {
let rect = Path::new()
.set("fill", "none")
.set("stroke", STROKE_COLOR)
.set("stroke-width", STROKE_WIDTH)
.set("stroke-dasharray", (4, 4))
.set("stroke-opacity", 1)
.set(
"d",
Data::new()
.move_to((x, y))
.line_by((0, WIDTH))
.line_by((WIDTH, 0))
.line_by((0, -WIDTH))
.close(),
);
let vertical = Path::new()
.set("fill", "none")
.set("stroke", STROKE_COLOR)
.set("stroke-width", STROKE_WIDTH)
.set("stroke-dasharray", (4, 4))
.set("stroke-opacity", 1)
.set(
"d",
Data::new().move_to((x + WIDTH / 2., y)).line_by((0, WIDTH)),
);
let horizontal = Path::new()
.set("fill", "none")
.set("stroke", STROKE_COLOR)
.set("stroke-width", STROKE_WIDTH)
.set("stroke-dasharray", (4, 4))
.set("stroke-opacity", 1)
.set(
"d",
Data::new().move_to((x, y + WIDTH / 2.)).line_by((WIDTH, 0)),
);
let text = Text::new()
.set("x", x + KANJI_X)
.set("y", y + KANJI_Y)
.set("fill", STROKE_COLOR)
.set("font-family", "KanjiStrokeOrders")
.set("font-size", KANJI_SIZE)
.add(node::Text::new(kanji));
document.add(rect).add(horizontal).add(vertical).add(text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment