From: Ian Jackson Date: Sun, 12 Jul 2020 14:37:10 +0000 (+0100) Subject: svg formatting rework wip X-Git-Tag: otter-0.2.0~1357 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=a7b82967bc10acf1a6e1e89f206ee112cc3b3461;p=otter.git svg formatting rework wip --- diff --git a/src/gamestate.rs b/src/gamestate.rs index a96d6e9b..de88436f 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -64,8 +64,11 @@ pub struct LogEntry { // ---------- piece trait, and rendering ---------- +type SE = SVGProcessingError; +type SR = Result<(),SE>; + pub trait Piece : Send + Debug { - fn svg_piece(&self, pri : &PieceRenderInstructions) -> String; + fn svg_piece(&self, f: &mut String, pri: &PieceRenderInstructions) -> SR; fn outline_path(&self, pri : &PieceRenderInstructions) -> String; fn surround_path(&self, pri : &PieceRenderInstructions) -> String; fn svg_x_ids(&self) -> VisiblePieceIdSvgIds; @@ -137,10 +140,11 @@ impl PieceState { Some(n) => n, None => -1, }; - write!(defs, r##"{}"##, + write!(defs, r##""##, pri.id_piece(), - dragraise, - pr.p.svg_piece(&pri)).unwrap(); + dragraise).unwrap(); + pr.p.svg_piece(&mut defs, &pri).unwrap(); + write!(defs, r##""##).unwrap(); write!(defs, r##""##, pri.id_select(), pr.p.surround_path(&pri)).unwrap(); diff --git a/src/imports.rs b/src/imports.rs index a1fd14ab..77ac8108 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -2,6 +2,7 @@ pub use std::io; pub use std::io::{BufReader,Read,Write}; pub use std::fmt::Write as _; +pub use std::fmt::Formatter; pub use std::fmt::{self,Display,Debug}; pub use std::thread; pub use std::time::Duration; diff --git a/src/pieces.rs b/src/pieces.rs index 47093ddc..c6170e80 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -20,19 +20,20 @@ const SELECT_SCALE : f64 = 1.1; #[derive(Copy,Clone,Debug,Error)] -pub enum SVGProcessError { +pub enum SVGProcessingError { UnknownOperator, BadNumber, WriteFail, } -display_as_debug!{SVGProcessError} -error_from_losedetails!{SVGProcessError, WriteFail, fmt::Error} -error_from_losedetails!{SVGProcessError, BadNumber, std::num::ParseFloatError} -#[throws(SVGProcessError)] -pub fn svg_rescale_path(input: &str, scale: f64) -> String { - use SVGProcessError::*; +display_as_debug!{SVGProcessingError} +error_from_losedetails!{SVGProcessingError,WriteFail,fmt::Error} +error_from_losedetails!{SVGProcessingError,BadNumber,std::num::ParseFloatError} + +type SE = SVGProcessingError; +#[throws(SE)] +pub fn svg_rescale_path(input: &str, scale: f64) -> String { type BM = u64; type BI = u32; #[derive(Debug,Copy,Clone)] @@ -71,7 +72,7 @@ pub fn svg_rescale_path(input: &str, scale: f64) -> String { continue; } } - _ => Err(UnknownOperator)?, + _ => Err(SE::UnknownOperator)?, }; write!(&mut out, "{}", w)?; } @@ -81,9 +82,10 @@ eprintln!("rescaled by {}: {} as {}",scale,&input,&out); } impl Piece for SimpleShape { - fn svg_piece(&self, pri : &PieceRenderInstructions) -> String { - format!(r##""##, - self.colours[pri.face], self.path) + #[throws(SE)] + fn svg_piece(&self, f: &mut String, pri: &PieceRenderInstructions) { + write!(f, r##""##, + self.colours[pri.face], self.path)?; } fn outline_path(&self, _pri : &PieceRenderInstructions) -> String { self.path.clone() @@ -116,7 +118,7 @@ impl Piece for SimpleShape { } impl SimpleShape { - #[throws(SVGProcessError)] + #[throws(SE)] fn new_from_path(desc: String, path: String, approx_dia: Coord, colours: ColourMap) -> Self { SimpleShape { @@ -124,7 +126,7 @@ impl SimpleShape { desc, approx_dia, path, colours, } } - #[throws(SVGProcessError)] + #[throws(SE)] fn new_circle(dia: Coord, colours: ColourMap) -> Self { let unit_path = "M 0 1 a 1 1 0 1 0 0 -2 \ @@ -133,7 +135,7 @@ impl SimpleShape { let path = svg_rescale_path(&unit_path, scale)?; Self::new_from_path("circle".to_owned(), path, dia, colours)? } - #[throws(SVGProcessError)] + #[throws(SE)] fn new_square(edgelen: Coord, colours: ColourMap) -> Self { let unit_path = "M -1 -1 h 2 v 2 h -2 z"; @@ -143,7 +145,7 @@ impl SimpleShape { } } -pub fn xxx_make_pieces() -> Result)>,SVGProcessError> { +pub fn xxx_make_pieces() -> Result)>,SE> { Ok(vec![ ([ 90, 80 ], Box::new(SimpleShape::new_circle(