[Refactor] Move svg => usvg conversion to a separate function

This commit is contained in:
Gergely Polonkai 2022-05-22 19:01:48 +02:00
parent b95c035133
commit 66e9c3d3f3
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 15 additions and 7 deletions

View File

@ -20,7 +20,7 @@ use svg::Document;
mod svg_clock; mod svg_clock;
use svg_clock::{HOUR_NAMES, HOUR_NAME_FONT_SIZE, IMAGE_WIDTH, OUTER_R, RING_WIDTH}; use svg_clock::{svg_to_usvg, HOUR_NAMES, HOUR_NAME_FONT_SIZE, IMAGE_WIDTH, OUTER_R, RING_WIDTH};
sctk::default_environment!(SeasonalClock, desktop); sctk::default_environment!(SeasonalClock, desktop);
@ -718,12 +718,7 @@ fn redraw(
config: &Option<Config>, config: &Option<Config>,
) -> Result<(), ::std::io::Error> { ) -> Result<(), ::std::io::Error> {
let document = gen_svg(config); let document = gen_svg(config);
let svg_tree = svg_to_usvg(document);
let bytes = document.to_string();
let mut opt = usvg::Options::default();
opt.fontdb.load_system_fonts();
opt.font_family = "Liberation Serif".to_string();
let svg_tree = usvg::Tree::from_str(&bytes, &opt.to_ref()).unwrap();
let (canvas, new_buffer) = pool.buffer( let (canvas, new_buffer) = pool.buffer(
buf_x as i32, buf_x as i32,

View File

@ -1,3 +1,6 @@
use svg::Document;
use usvg::Tree;
pub const HOUR_NAMES: [&str; 24] = [ pub const HOUR_NAMES: [&str; 24] = [
"Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud", "Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud",
"Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow", "Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow",
@ -7,3 +10,13 @@ pub const IMAGE_WIDTH: u32 = 700;
pub const HOUR_NAME_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.019109; 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 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 RING_WIDTH: f32 = HOUR_NAME_FONT_SIZE * 3.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()
}