141 lines
4.8 KiB
Rust
141 lines
4.8 KiB
Rust
use svg::{
|
|
node::{
|
|
element::{path::Data as PathData, Definitions, Path, Text, TextPath},
|
|
Text as TextNode,
|
|
},
|
|
Document,
|
|
};
|
|
use usvg::Tree;
|
|
|
|
pub const HOUR_NAMES: [&str; 24] = [
|
|
"Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud",
|
|
"Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow",
|
|
"Mushroom", "Thunder", "Frost", "Lantern",
|
|
];
|
|
pub const IMAGE_WIDTH: u32 = 700;
|
|
pub const HOUR_NAME_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.019109;
|
|
pub const OUTER_R: f32 = (IMAGE_WIDTH as f32) / 2.0 - 3.0 * HOUR_NAME_FONT_SIZE;
|
|
pub const RING_WIDTH: f32 = HOUR_NAME_FONT_SIZE * 3.0;
|
|
pub const UTC_HOUR_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.021462;
|
|
|
|
pub fn svg_to_usvg(document: Document) -> Tree {
|
|
let doc_str = document.to_string();
|
|
|
|
let mut opt = usvg::Options::default();
|
|
opt.fontdb.load_system_fonts();
|
|
opt.font_family = "Liberation Serif".to_string();
|
|
|
|
Tree::from_str(&doc_str, &opt.to_ref()).unwrap()
|
|
}
|
|
|
|
fn hour_name_path() -> Path {
|
|
let radius = OUTER_R - RING_WIDTH / 2.0;
|
|
let delta_x = radius * (15.0_f32.to_radians() / 2.0).sin();
|
|
let delta_y = radius * (1.0 - (15.0_f32.to_radians() / 2.0).cos());
|
|
let x1 = (IMAGE_WIDTH as f32) / 2.0 - delta_x;
|
|
let y1 = ((IMAGE_WIDTH as f32) / 2.0 - radius) + delta_y;
|
|
|
|
let path_data = PathData::new().move_to((x1, y1)).elliptical_arc_by((
|
|
radius,
|
|
radius,
|
|
15,
|
|
0,
|
|
1,
|
|
2.0 * delta_x,
|
|
0,
|
|
));
|
|
|
|
Path::new().set("id", "hour-name-path").set("d", path_data)
|
|
}
|
|
|
|
fn create_temp_document(hour_name: &str) -> Document {
|
|
let definitions = Definitions::new().add(hour_name_path());
|
|
|
|
let hour_name_text_path = TextPath::new()
|
|
.set("xlink:href", "#hour-name-path")
|
|
.set("startOffset", "50%")
|
|
.add(TextNode::new(hour_name));
|
|
let hour_name_text = Text::new()
|
|
.set("id", "hour-name")
|
|
.set("text-anchor", "middle")
|
|
.set("dominant-baseline", "mathematical")
|
|
.set("font-size", HOUR_NAME_FONT_SIZE)
|
|
.add(hour_name_text_path);
|
|
|
|
Document::new()
|
|
.set("viewBox", (0i32, 0i32, 700i32, 700i32))
|
|
.set("width", 700i32)
|
|
.set("height", 700i32)
|
|
.set("xmlns:xlink", "http://www.w3.org/1999/xlink")
|
|
.add(definitions)
|
|
.add(hour_name_text)
|
|
}
|
|
|
|
fn cache_hour_name_path(hour_name: &str) -> PathData {
|
|
let tree = svg_to_usvg(create_temp_document(hour_name));
|
|
let mut svg_path_data: PathData = PathData::new();
|
|
let text_node = tree.node_by_id("hour-name").unwrap();
|
|
match *text_node.borrow() {
|
|
usvg::NodeKind::Path(ref path) => {
|
|
let path_data = &path.data;
|
|
for segment in path_data.0.iter() {
|
|
match segment {
|
|
usvg::PathSegment::MoveTo { x, y } => {
|
|
svg_path_data = svg_path_data.move_to((*x, *y));
|
|
}
|
|
usvg::PathSegment::LineTo { x, y } => {
|
|
svg_path_data = svg_path_data.line_to((*x, *y));
|
|
}
|
|
usvg::PathSegment::CurveTo {
|
|
x1,
|
|
y1,
|
|
x2,
|
|
y2,
|
|
x,
|
|
y,
|
|
} => {
|
|
svg_path_data = svg_path_data.cubic_curve_to((*x1, *y1, *x2, *y2, *x, *y));
|
|
}
|
|
usvg::PathSegment::ClosePath => {
|
|
svg_path_data = svg_path_data.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_ => {
|
|
unreachable!()
|
|
}
|
|
}
|
|
|
|
svg_path_data
|
|
}
|
|
|
|
pub fn cache_hour_name_paths() -> [PathData; 24] {
|
|
[
|
|
cache_hour_name_path(HOUR_NAMES[0]),
|
|
cache_hour_name_path(HOUR_NAMES[1]),
|
|
cache_hour_name_path(HOUR_NAMES[2]),
|
|
cache_hour_name_path(HOUR_NAMES[3]),
|
|
cache_hour_name_path(HOUR_NAMES[4]),
|
|
cache_hour_name_path(HOUR_NAMES[5]),
|
|
cache_hour_name_path(HOUR_NAMES[6]),
|
|
cache_hour_name_path(HOUR_NAMES[7]),
|
|
cache_hour_name_path(HOUR_NAMES[8]),
|
|
cache_hour_name_path(HOUR_NAMES[9]),
|
|
cache_hour_name_path(HOUR_NAMES[10]),
|
|
cache_hour_name_path(HOUR_NAMES[11]),
|
|
cache_hour_name_path(HOUR_NAMES[12]),
|
|
cache_hour_name_path(HOUR_NAMES[13]),
|
|
cache_hour_name_path(HOUR_NAMES[14]),
|
|
cache_hour_name_path(HOUR_NAMES[15]),
|
|
cache_hour_name_path(HOUR_NAMES[16]),
|
|
cache_hour_name_path(HOUR_NAMES[17]),
|
|
cache_hour_name_path(HOUR_NAMES[18]),
|
|
cache_hour_name_path(HOUR_NAMES[19]),
|
|
cache_hour_name_path(HOUR_NAMES[20]),
|
|
cache_hour_name_path(HOUR_NAMES[21]),
|
|
cache_hour_name_path(HOUR_NAMES[22]),
|
|
cache_hour_name_path(HOUR_NAMES[23]),
|
|
]
|
|
}
|