chiark / gitweb /
svg formatting rework wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jul 2020 14:37:10 +0000 (15:37 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jul 2020 14:37:10 +0000 (15:37 +0100)
src/gamestate.rs
src/imports.rs
src/pieces.rs

index a96d6e9b5d165bf015a14be1cd9fda19ff718d5a..de88436f0fa5769b84427ecabcfbda4d01f4c769 100644 (file)
@@ -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##"<g id="{}" data-dragraise="{}">{}</g>"##,
+    write!(defs, r##"<g id="{}" data-dragraise="{}">"##,
            pri.id_piece(),
-           dragraise,
-           pr.p.svg_piece(&pri)).unwrap();
+           dragraise).unwrap();
+    pr.p.svg_piece(&mut defs, &pri).unwrap();
+    write!(defs, r##"</g>"##).unwrap();
     write!(defs, r##"<path id="{}" stroke="black" fill="none" d="{}"/>"##,
            pri.id_select(),
            pr.p.surround_path(&pri)).unwrap();
index a1fd14abc28aa75ee0ca7d7ce7fb0e5e3effcded..77ac81088b47b790b5c21f127d8d84ace6dbe9da 100644 (file)
@@ -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;
index 47093ddc17ac456387227a75d747577d3354d837..c6170e80f3e7de374ee7909a8e471c7b908b70ee 100644 (file)
@@ -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##"<path fill="{}" d="{}"/>"##,
-            self.colours[pri.face], self.path)
+  #[throws(SE)]
+  fn svg_piece(&self, f: &mut String, pri: &PieceRenderInstructions) {
+    write!(f, r##"<path fill="{}" d="{}"/>"##,
+           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<Vec<(Pos, Box<dyn Piece>)>,SVGProcessError> {
+pub fn xxx_make_pieces() -> Result<Vec<(Pos, Box<dyn Piece>)>,SE> {
   Ok(vec![
     ([ 90, 80 ],
      Box::new(SimpleShape::new_circle(