From: Ian Jackson Date: Sun, 12 Jul 2020 11:56:56 +0000 (+0100) Subject: wip svg processing X-Git-Tag: otter-0.2.0~1365 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=33d41f4b822ce909785fe78cfc02991d011cc6e4;p=otter.git wip svg processing --- diff --git a/src/error.rs b/src/error.rs index 51ec90cc..454ad0f0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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} => { + } +} diff --git a/src/pieces.rs b/src/pieces.rs index 77299a99..8173a656 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -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 for SVGProcessError { + fn from(_: fmt::Error) -> Self { SVGProcessError::WriteFail } +} +impl Display for SVGProcessError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + ::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##""##,