From 66e9c3d3f3f6d4421cae859c9e04977f3ee5bcfd Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 22 May 2022 19:01:48 +0200 Subject: [PATCH] [Refactor] Move svg => usvg conversion to a separate function --- src/main.rs | 9 ++------- src/svg_clock.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index b11e715..5f46a99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use svg::Document; 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); @@ -718,12 +718,7 @@ fn redraw( config: &Option, ) -> Result<(), ::std::io::Error> { let document = gen_svg(config); - - 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 svg_tree = svg_to_usvg(document); let (canvas, new_buffer) = pool.buffer( buf_x as i32, diff --git a/src/svg_clock.rs b/src/svg_clock.rs index 92def91..dea569e 100644 --- a/src/svg_clock.rs +++ b/src/svg_clock.rs @@ -1,3 +1,6 @@ +use svg::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", @@ -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 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 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() +}