chiark / gitweb /
wip svg processing
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jul 2020 11:56:56 +0000 (12:56 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jul 2020 11:56:56 +0000 (12:56 +0100)
src/error.rs
src/pieces.rs

index 51ec90cce8fd1b6805b41b3d469558755e32bcd4..454ad0f090d50c0b98a417d595c22aeed25f6a71 100644 (file)
@@ -76,3 +76,9 @@ impl IdForById for PieceId {
   type Error = GameError;
   const ERROR : GameError = GameError::PieceGone;
 }
+
+#[macro_export]
+macro_rules! display_as_debug {
+  {$x:ty} => {
+  }
+}
index 77299a99993314e3af051418bfdb31e6499bcb5b..8173a6569e4aa77d96e3737b938c1a3d65547ff6 100644 (file)
@@ -15,6 +15,69 @@ struct SimpleShape {
 
 const SELECT_SCALE : f64 = 1.1;
 
+
+#[derive(Copy,Clone,Debug,Error)]
+pub enum SVGProcessError {
+  UnknownOperator,
+  BadNumber,
+  WriteFail,
+}
+impl From<fmt::Error> for SVGProcessError {
+  fn from(_: fmt::Error) -> Self { SVGProcessError::WriteFail }
+}
+impl Display for SVGProcessError {
+  fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+    <Self as Debug>::fmt(self, f)
+  }
+}
+
+#[throws(SVGProcessError)]
+pub fn svg_rescale_path(input: &str, scale: f64) -> String {
+  use SVGProcessError::*;
+
+  type BM = u64;
+  type BI = u32;
+  #[derive(Debug,Copy,Clone)]
+  struct RotatingBitmap {
+    bits: BM,
+    len: BI,
+    index: BI,
+  };
+  impl RotatingBitmap {
+    const fn new(bits: BM, len: BI) -> Self { Self{ bits, len, index:0 } }
+    fn reset(&mut self) { self.index= 0; }
+    fn next(&mut self) -> bool {
+      return false;
+    }
+  }
+  const ALWAYS_MAP : RotatingBitmap = RotatingBitmap::new(0x01, 1);
+  
+  let mut out = String::new();
+  let mut map = ALWAYS_MAP;
+  let mut first = iter::once(());
+
+  for w in input.split_ascii_whitespace() {
+    if !first.next().is_some() { write!(&mut out, " ")?; }
+    match w {
+      "L" | "l" | "M" | "m" |
+      "V" | "v" | "H" | "h" => map = ALWAYS_MAP,
+      "A" | "a"             => map = RotatingBitmap::new(0x63, 7),
+      "z"                   => map.reset(),
+      v if v.starts_with(|s:char| s=='-' || s=='.' || s.is_ascii_digit()) => {
+        if map.next() {
+          let v : f64 = v.parse().map_err(|_| BadNumber)?;
+          write!(&mut out, "{} ", v * scale)?;
+          continue;
+        }
+      }
+      _ => Err(UnknownOperator)?,
+    };
+    write!(&mut out, "{}", w)?;
+  }
+
+  out
+}
+
 impl Piece for SimpleShape {
   fn svg_piece(&self, pri : &PieceRenderInstructions) -> String {
     format!(r##"<use fill="{}" href="#{}"/>"##,