From: Ian Jackson Date: Sun, 27 Sep 2020 17:34:04 +0000 (+0100) Subject: working lib layout algorithm X-Git-Tag: otter-0.2.0~842 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=d2d9f3bd1f1f23ee45c2f9cdb25d2b0e4e743bc5;p=otter.git working lib layout algorithm Signed-off-by: Ian Jackson --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 32dd3a21..699a15f5 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -765,20 +765,53 @@ mod library_add { impl Placement { /// If returns None, has already maybe tried to take some space - fn place(&mut self, bbox: &[Pos;2]) -> Option { + fn place(&mut self, bbox: &[Pos;2], pieces: &Vec) + -> Option { let PosC([w,h]) = bbox[1] - bbox[0]; + let mut did_newline = false; let (ncbot, tlhs) = 'search : loop { let ncbot = max(self.cbot, self.top + h); if ncbot > self.bot { None? } - let tlhs = self.clhs; + let mut any_clash_bot = None; + 'within_line: loop { + let tlhs = self.clhs; self.clhs += w; if self.clhs > self.rhs { break 'within_line } + + if let Some((nclhs, clash_bot)) = pieces.iter() + .filter_map(|p| { + let tl = p.pos + p.bbox[0]; + let br = p.pos + p.bbox[1]; + if + tl.0[0] >= self.clhs || + tl.0[1] >= ncbot || + br.0[0] <= tlhs || + br.0[1] <= self.top + { + None + } else { + eprintln!( + "at {:?} tlhs={} ncbot={} avoiding {} tl={:?} br={:?}", + &self, tlhs, ncbot, + &p.itemname, &tl, &br); + Some((br.0[0], br.0[1])) + } + }).next() { + self.clhs = nclhs; + any_clash_bot = Some(clash_bot); + continue 'within_line; + } + break 'search (ncbot, tlhs); } // line is full self.top = self.cbot; + if did_newline { + self.top = any_clash_bot?; // if not, will never fit + } + did_newline = true; self.clhs = self.lhs; // if we are simply too wide, we'll just loop until off the bottom }; @@ -796,11 +829,11 @@ mod library_add { let mut exitcode = 0; let mut insns = vec![]; for (ix, it) in items.iter().enumerate() { - dbg!(&it.itemname); + eprintln!("item {} {:?}", &it.itemname, &it.f0bbox); if let Some(already) = &already { if already.contains(&it.itemname) { continue } } - let pos = match placement.place(&items[0].f0bbox) { + let pos = match placement.place(&items[0].f0bbox, &pieces) { Some(pos) => pos, None => { let m = format!("out of space after {} at {}",