From: Ian Jackson Date: Sun, 20 Sep 2020 22:16:55 +0000 (+0100) Subject: shapelib: wip, still does not compile X-Git-Tag: otter-0.2.0~922 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=21948bd565d74819fa3be6737f74736c77ddb695;p=otter.git shapelib: wip, still does not compile Signed-off-by: Ian Jackson --- diff --git a/src/pieces.rs b/src/pieces.rs index a074bc3b..842ddfdb 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -88,6 +88,17 @@ pub fn svg_rescale_path(input: &Html, scale: f64) -> Html { Html(out) } +#[throws(SE)] +pub fn svg_circle_path(diam: f64) -> Html { + let unit_path = Html::lit( + "M 0 1 a 1 1 0 1 0 0 -2 \ + a 1 1 0 1 0 0 2 z" + ); + let scale = diam * 0.5; + let path = svg_rescale_path(&unit_path, scale)?; + path +} + #[typetag::serde] impl Outline for SimpleShape { #[throws(IE)] @@ -145,12 +156,7 @@ impl SimpleShape { impl PieceSpec for piece_specs::Disc { #[throws(SpecError)] fn load(&self) -> Box { - let unit_path = Html::lit( - "M 0 1 a 1 1 0 1 0 0 -2 \ - a 1 1 0 1 0 0 2 z" - ); - let scale = (self.diam as f64) * 0.5; - let path = svg_rescale_path(&unit_path, scale)?; + let path = svg_circle_path(self.diam as f64)?; SimpleShape::new_from_path(Html::lit("circle"), path, self.diam, &self.faces)? } diff --git a/src/shapelib.rs b/src/shapelib.rs index bfc08939..1716c5b3 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -31,9 +31,9 @@ pub struct ItemData { #[derive(Debug,Deserialize)] pub struct GroupDetails { - size: Vec, + size: Vec, category: String, - #[serde(default)] centre: Option>, + #[serde(default)] centre: [f64; 2], #[serde(default)] flip: bool, #[serde(default="num_traits::identities::One::one")] scale: f64, #[serde(flatten)] outline: Box, @@ -50,7 +50,7 @@ struct GroupDefn { files: FileList, #[serde(default)] item_prefix: String, #[serde(default)] item_suffix: String, - #[serde(flatten)] info: Arc, + #[serde(flatten)] info: Arc, } #[derive(Deserialize,Debug)] @@ -112,11 +112,14 @@ define_index_type!{ pub struct DescId = u8; } struct ItemFace { svg: Html, desc: DescId, + centre: [f64; 2], + scale: f64, } #[derive(Debug,Serialize,Deserialize)] struct Item { faces: IndexVec, + desc_hidden: DescId, descs: IndexVec, outline: Box, } @@ -130,7 +133,30 @@ impl Outline for Item { delegate! { to self.outline { #[typetag::serde(name="Lib")] impl Piece for Item { - + #[throws(SpecError)] + fn resolve_spec_face(&self, face: Option) -> FaceId { + let face = face.unwrap_or_default(); + self.faces.get(face).ok_or(SpecError::FaceNotFound)?; + face + } + + #[throws(IE)] + fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) { + let face = &self.faces[pri.face]; + write!(&mut f.0, + r##"{}"##, + face.scale, face.centre[0], face.centre[1], + face.svg.0)?; + } + #[throws(IE)] + fn svg_x_defs(&self, _f: &mut Html, _pri : &PieceRenderInstructions) { + } + fn describe_html(&self, face : Option) -> Html { + Html(format!("a {}", self.descs[ match face { + Some(face) => self.faces[face].desc, + None => self.desc_hidden, + }])) + } } /* @@ -261,15 +287,27 @@ pub fn load(libname: String, dirname: String) { } #[derive(Serialize,Deserialize,Debug)] +struct Circle { diam: f64 } + +#[typetag::serde(name="Circle")] +impl Outline for Circle { +} + +#[derive(Deserialize,Debug)] struct CircleDefn { } #[typetag::deserialize(name="Circle")] impl OutlineDefn for CircleDefn { #[throws(LibraryLoadError)] fn check(&self, lgd: &GroupData) { Self::get_size(lgd)?; } + fn load(&self, lgd: &GroupData) -> Box { + Box::new(Circle { + diam: Self::get_size(lgd).unrap() + }) + } } impl CircleDefn { #[throws(LibraryLoadError)] - fn get_size(lgd: &GroupData) -> Coord { + fn get_size(lgd: &GroupData) -> f64 { match lgd.size.as_slice() { &[c] => c, size => throw!(LLE::WrongNumberOfSizeDimensions