259 lines
7.8 KiB
Rust
259 lines
7.8 KiB
Rust
use std::fmt;
|
|
|
|
use rctree::Node;
|
|
use svg::{
|
|
node::{
|
|
element::{path::Data as PathData, Definitions, Group, 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;
|
|
|
|
enum Season {
|
|
Spring,
|
|
Summer,
|
|
Autumn,
|
|
Winter,
|
|
}
|
|
|
|
impl fmt::Display for Season {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
Season::Spring => "spring",
|
|
Season::Summer => "summer",
|
|
Season::Autumn => "autumn",
|
|
Season::Winter => "winter",
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
pub fn seconds_to_degrees(seconds: i32) -> f32 {
|
|
seconds as f32 * 360.0 / 86400.0
|
|
}
|
|
|
|
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: usize) -> Document {
|
|
let definitions = Definitions::new().add(hour_name_path());
|
|
let utc_hour_y = IMAGE_WIDTH as f32 / 2.0 - OUTER_R + RING_WIDTH + UTC_HOUR_FONT_SIZE;
|
|
|
|
let hour_name_text_path = TextPath::new()
|
|
.set("xlink:href", "#hour-name-path")
|
|
.set("startOffset", "50%")
|
|
.add(TextNode::new(HOUR_NAMES[hour]));
|
|
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);
|
|
let utc_hour_text = Text::new()
|
|
.set("id", "utc-hour")
|
|
.set(
|
|
"transform",
|
|
format!("rotate(-7.5, {}, {})", IMAGE_WIDTH / 2, IMAGE_WIDTH / 2),
|
|
)
|
|
.set("x", IMAGE_WIDTH / 2)
|
|
.set("y", utc_hour_y)
|
|
.set("text-anchor", "middle")
|
|
.set("dominant-baseline", "mathematical")
|
|
.set("font-size", UTC_HOUR_FONT_SIZE)
|
|
.add(TextNode::new(format!("U {:02}", hour)));
|
|
|
|
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)
|
|
.add(utc_hour_text)
|
|
}
|
|
|
|
fn node_to_path(node: Node<usvg::NodeKind>) -> PathData {
|
|
let mut svg_path_data = PathData::new();
|
|
|
|
match *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
|
|
}
|
|
|
|
fn cache_hour_name_path(hour: usize) -> (PathData, PathData) {
|
|
let tree = svg_to_usvg(create_temp_document(hour));
|
|
let text_node = tree.node_by_id("hour-name").unwrap();
|
|
let utc_text_node = tree.node_by_id("utc-hour").unwrap();
|
|
(node_to_path(text_node), node_to_path(utc_text_node))
|
|
}
|
|
|
|
pub fn cache_hour_name_paths() -> [(PathData, PathData); 24] {
|
|
[
|
|
cache_hour_name_path(0),
|
|
cache_hour_name_path(1),
|
|
cache_hour_name_path(2),
|
|
cache_hour_name_path(3),
|
|
cache_hour_name_path(4),
|
|
cache_hour_name_path(5),
|
|
cache_hour_name_path(6),
|
|
cache_hour_name_path(7),
|
|
cache_hour_name_path(8),
|
|
cache_hour_name_path(9),
|
|
cache_hour_name_path(10),
|
|
cache_hour_name_path(11),
|
|
cache_hour_name_path(12),
|
|
cache_hour_name_path(13),
|
|
cache_hour_name_path(14),
|
|
cache_hour_name_path(15),
|
|
cache_hour_name_path(16),
|
|
cache_hour_name_path(17),
|
|
cache_hour_name_path(18),
|
|
cache_hour_name_path(19),
|
|
cache_hour_name_path(20),
|
|
cache_hour_name_path(21),
|
|
cache_hour_name_path(22),
|
|
cache_hour_name_path(23),
|
|
]
|
|
}
|
|
|
|
pub fn hour_marker(
|
|
hour: i32,
|
|
hour_name_path_data: &(PathData, PathData),
|
|
is_current_hour: bool,
|
|
) -> Group {
|
|
let season = match hour {
|
|
0..=5 => Season::Winter,
|
|
6..=11 => Season::Spring,
|
|
12..=17 => Season::Summer,
|
|
18..=23 => Season::Autumn,
|
|
_ => panic!("Hour out of range"),
|
|
};
|
|
let rotation = hour * 15;
|
|
|
|
let delta_x = OUTER_R * (15f32.to_radians() / 2.0).sin();
|
|
let delta_y = OUTER_R * (1.0 - (15f32.to_radians() / 2.0).cos());
|
|
|
|
let s_delta_x = 0.0 - RING_WIDTH * (15f32.to_radians() / 2.0).sin();
|
|
let s_delta_y = RING_WIDTH * (15f32.to_radians() / 2.0).cos();
|
|
|
|
let i_delta_x = -2.0 * (OUTER_R - RING_WIDTH) * (15f32.to_radians() / 2.0).sin();
|
|
|
|
let x1 = IMAGE_WIDTH as f32 / 2.0 - delta_x;
|
|
let y1 = (IMAGE_WIDTH as f32 / 2.0 - OUTER_R) + delta_y;
|
|
|
|
let path_data = PathData::new()
|
|
.move_to((x1, y1))
|
|
.elliptical_arc_by((OUTER_R, OUTER_R, 15, 0, 1, 2.0 * delta_x, 0))
|
|
.line_by((s_delta_x, s_delta_y))
|
|
.elliptical_arc_by((
|
|
OUTER_R - RING_WIDTH,
|
|
OUTER_R - RING_WIDTH,
|
|
15,
|
|
0,
|
|
0,
|
|
i_delta_x,
|
|
0,
|
|
))
|
|
.close();
|
|
let path = Path::new().set("class", "hour-outline").set("d", path_data);
|
|
let hour_name_path = Path::new()
|
|
.set("class", "hour-name")
|
|
.set("d", hour_name_path_data.0.clone());
|
|
let utc_hour_path = Path::new()
|
|
.set("class", "utc")
|
|
.set("d", hour_name_path_data.1.clone());
|
|
|
|
Group::new()
|
|
.set(
|
|
"class",
|
|
format!(
|
|
"hour {season}{}",
|
|
if is_current_hour { " active" } else { "" }
|
|
),
|
|
)
|
|
.set(
|
|
"transform",
|
|
format!(
|
|
"rotate({}, {}, {})",
|
|
rotation as f32 - 172.5,
|
|
IMAGE_WIDTH / 2,
|
|
IMAGE_WIDTH / 2
|
|
),
|
|
)
|
|
.add(path)
|
|
.add(hour_name_path)
|
|
.add(utc_hour_path)
|
|
}
|