Repository navigation
Fix #700, #709, #715, #726, #710: i18n audit, import picture sizes, ink Delete, pan edge, touchpad scroll #731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
057d325
7d2de5d
d23e041
f3852cc
de28e0e
aa9d9ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,11 +83,30 @@ const BOX_WIDTH: f32 = 13.0 * PER_HALF_INCH; | |
| const BOX_LEFT: f32 = PER_HALF_INCH; | ||
| const LINE_HEIGHT: f32 = 24.0; | ||
|
|
||
| /// How tall a block stands in a box of this width: a picture at its size, fitted to the box | ||
| /// when it is wider, and anything else as one line. | ||
| fn block_height(block: &Block, box_width: f32) -> f32 { | ||
| match block { | ||
| Block::Image { width, height: Some(h), .. } => { | ||
| let fit = width.filter(|w| *w > box_width && *w > 0.0).map_or(1.0, |w| box_width / w); | ||
| h * fit | ||
| } | ||
| _ => LINE_HEIGHT, | ||
| } | ||
| } | ||
|
|
||
| impl Ctx<'_, '_> { | ||
| /// Converts a page box by box, so each lands where it was and the ink stays on it. | ||
| /// | ||
| /// A box keeps OneNote's place unless a box above it, overlapping it across the page, has | ||
| /// grown past that place: NoteCraft's boxes carry a header and padding OneNote's do not, | ||
| /// so boxes OneNote stacked edge to edge would otherwise overlap. Such a box moves down | ||
| /// just below the one above. | ||
| fn placed(&mut self, page: &Page, first_row_header: bool) { | ||
| let mut contents = page.blocks.iter(); | ||
| let mut boxes = Vec::new(); | ||
| let mut boxes: Vec<Container> = Vec::new(); | ||
| // Each placed box's left, right and estimated bottom, to push later boxes down. | ||
| let mut extents: Vec<(f32, f32, f32, f32)> = Vec::new(); | ||
| let mut below = PER_HALF_INCH; | ||
| for place in &page.places { | ||
| let before = self.out.blocks.len(); | ||
|
|
@@ -103,9 +122,17 @@ impl Ctx<'_, '_> { | |
| (*width, *height) = (Some(w), Some(h)); | ||
| } | ||
| let x = place.x.map_or(BOX_LEFT, |x| x * PER_HALF_INCH); | ||
| let y = place.y.map_or(below, |y| y * PER_HALF_INCH); | ||
| let width = size.map(|s| s.0).or_else(|| place.width.map(|w| w * PER_HALF_INCH)).unwrap_or(BOX_WIDTH); | ||
| below = y + size.map_or(made as f32 * LINE_HEIGHT, |s| s.1) + LINE_HEIGHT; | ||
| let mut y = place.y.map_or(below, |y| y * PER_HALF_INCH); | ||
| for &(left, right, top, bottom) in &extents { | ||
| if top <= y && y < bottom && left < x + width && x < right { | ||
| y = bottom; | ||
|
Comment on lines
+127
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recheck earlier extents after moving a box. For three horizontally overlapping boxes, let A occupy y=192..240 and B occupy y=48..192, in that insertion order. If C starts at y=96, the scan skips A and then moves C below B to y=192. C now starts at the same position as A. Before this change, C did not overlap A. Restart the collision scan after each movement, or process extents in vertical order. Keep the horizontal-overlap check so side-by-side boxes retain their positions.
|
||
| } | ||
| } | ||
| let content: f32 = self.out.blocks.get(before..).unwrap_or(&[]).iter().map(|b| block_height(b, width)).sum(); | ||
| let bottom = y + content + LINE_HEIGHT; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the container chrome in the bottom estimate. For a 216-point picture at y=48, this records bottom=288. The renderer starts that picture after a 24-point header and a 14-point handle, so the picture reaches at least y=302. A following box moved to y=288 can cover the picture with its header. Include the header, handle, trailing space and padding used by
|
||
| below = bottom; | ||
| extents.push((x, x + width, y, bottom)); | ||
| boxes.push(Container::new(x, y, width, made)); | ||
| } | ||
| self.out.boxes = Some(boxes); | ||
|
|
@@ -135,7 +162,13 @@ impl Ctx<'_, '_> { | |
| self.numbers.clear(); | ||
| if let Some(span) = self.attachment(content) { | ||
| let block = match span.image.clone() { | ||
| Some(src) => Block::Image { alt: span.text, src, width: None, height: None, emoji: Vec::new() }, | ||
| Some(src) => { | ||
| let size = match content { | ||
| Content::Picture { size, .. } => size.map(|(w, h)| (w * PER_HALF_INCH, h * PER_HALF_INCH)), | ||
| _ => None, | ||
| }; | ||
| Block::Image { alt: span.text, src, width: size.map(|s| s.0), height: size.map(|s| s.1), emoji: Vec::new() } | ||
| } | ||
| None => Block::Paragraph(vec![span], Vec::new()), | ||
| }; | ||
| self.out.blocks.push(block); | ||
|
|
@@ -202,7 +235,7 @@ impl Ctx<'_, '_> { | |
| /// A picture as an image span, or a file as a link span. `None` when its bytes are gone. | ||
| fn attachment(&mut self, content: &Content) -> Option<Span> { | ||
| match content { | ||
| Content::Picture { name, alt, file, ext } => { | ||
| Content::Picture { name, alt, file, ext, .. } => { | ||
| let Some(bytes) = file.as_deref().and_then(|r| self.file_data.read(r)) else { | ||
| self.out.loss.missing_data += 1; | ||
| return None; | ||
|
|
@@ -489,6 +522,7 @@ mod tests { | |
| alt: String::new(), | ||
| file: Some("<ifndf>{00000000-0000-0000-0000-000000000000}".into()), | ||
| ext: String::new(), | ||
| size: None, | ||
| }], | ||
| ..Page::default() | ||
| }; | ||
|
|
@@ -522,4 +556,58 @@ mod tests { | |
| assert_eq!(boxes.first().map(|b| (b.x, b.y, b.width, b.blocks)), Some((96.0, 240.0, 480.0, 1))); | ||
| assert_eq!(boxes.get(1).map(|b| (b.x, b.blocks)), Some((48.0, 2))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_scaled_picture_inside_an_outline_keeps_its_size_and_pushes_the_box_below_down() { | ||
| use notecraft_onenote::testing::OneNote; | ||
| use notecraft_onenote::{Ink, InkPoint, InkStroke, Place}; | ||
| let png = include_bytes!("../tests/fixtures/synthetic-pixel.png"); | ||
| let mut one = OneNote::new(); | ||
| let shots = [OneNote::picture("a.png", "", "png", png).sized(8.0, 4.5), OneNote::picture("b.png", "", "png", png).sized(8.0, 4.5)]; | ||
| one.page_with_attachments("Shots", 1, &[], &shots); | ||
| let data = one.finish(); | ||
| let store = notecraft_onenote::read(&data).expect("a synthetic section"); | ||
| let mut page = notecraft_onenote::section(&store).pages.into_iter().next().expect("a page"); | ||
| assert_eq!(page.blocks.len(), 2, "{:?}", page.blocks); | ||
| // Two boxes OneNote stacked edge to edge: the second starts one half-inch below the | ||
| // first, but the first holds a picture four and a half half-inches tall. | ||
| page.places = vec![ | ||
| Place { x: Some(1.0), y: Some(1.0), width: Some(10.0), picture: None, blocks: 1 }, | ||
| Place { x: Some(1.0), y: Some(2.0), width: Some(10.0), picture: None, blocks: 1 }, | ||
| ]; | ||
| page.ink = Ink { | ||
| strokes: vec![InkStroke { points: vec![InkPoint { x: 1.0, y: 1.0, pressure: None }], colour: None, width: 0.1, highlighter: false }], | ||
| ..Ink::default() | ||
| }; | ||
| let out = convert_page(&store, &data, &page, false, &mut |name: &str, _: &[u8]| Some(format!("assets/{name}"))); | ||
| let sizes: Vec<_> = | ||
| out.blocks.iter().filter_map(|b| if let Block::Image { width, height, .. } = b { Some((*width, *height)) } else { None }).collect(); | ||
| assert_eq!(sizes, [(Some(8.0 * PER_HALF_INCH), Some(4.5 * PER_HALF_INCH)); 2], "the picture keeps OneNote's displayed size"); | ||
| let boxes = out.boxes.expect("boxes"); | ||
| let (first, second) = (&boxes[0], &boxes[1]); | ||
| assert!(second.y >= first.y + 4.5 * PER_HALF_INCH, "the box below moves down past the picture: {} then {}", first.y, second.y); | ||
| } | ||
|
|
||
| #[test] | ||
| fn boxes_side_by_side_keep_their_own_places() { | ||
| use notecraft_onenote::{Ink, InkPoint, InkStroke, Place}; | ||
| let line = |t: &str| Content::Text { runs: vec![Run { text: t.into(), ..Run::default() }], list: None, depth: 0 }; | ||
| let lines: Vec<Content> = (0..6).map(|n| line(&format!("line {n}"))).collect(); | ||
| let page = Page { | ||
| title: "t".into(), | ||
| level: 1, | ||
| blocks: lines, | ||
| places: vec![ | ||
| Place { x: Some(1.0), y: Some(1.0), width: Some(5.0), picture: None, blocks: 5 }, | ||
| Place { x: Some(8.0), y: Some(1.0), width: Some(5.0), picture: None, blocks: 1 }, | ||
| ], | ||
| ink: Ink { | ||
| strokes: vec![InkStroke { points: vec![InkPoint { x: 1.0, y: 1.0, pressure: None }], colour: None, width: 0.1, highlighter: false }], | ||
| ..Ink::default() | ||
| }, | ||
| }; | ||
| let out = convert_page(&empty_store(), &[], &page, false, &mut |_, _| None); | ||
| let boxes = out.boxes.expect("boxes"); | ||
| assert_eq!(boxes.iter().map(|b| b.y).collect::<Vec<_>>(), [PER_HALF_INCH, PER_HALF_INCH], "a box beside another is not pushed down"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14539,6 +14539,24 @@ impl NoteCraftApp { | |
| if top < 0.0 { (-top + INK_ABOVE_ROOM) * zoom } else { 0.0 } | ||
| } | ||
|
|
||
| /// How far, in screen points, the sideways pan may take the view past the edge the open page | ||
| /// lines up on (issue #726): the left edge of a left-to-right page, the right edge of a | ||
| /// right-to-left one. Nothing, unless ink or a box already reaches past that edge, in which | ||
| /// case as far as it with a little room beyond. Gives whether the page is right to left too. | ||
| fn room_past_page_edge(&self) -> (bool, f32) { | ||
| #[allow(clippy::cast_precision_loss)] | ||
| let zoom = self.zoom_percent as f32 / 100.0; | ||
| let rtl = self.editor.as_ref().is_some_and(|ed| ed.page_dir == Some(notecraft_model::Dir::Rtl)); | ||
| if rtl { | ||
| let (Some(ed), Some((_, right)), Some(view)) = (self.editor.as_ref(), self.page_hspan, self.page_view) else { return (true, 0.0) }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Measure overflow in the read-only preview. On a locked RTL page, Measure the preview’s content extent and allow enough pan to reveal its overflow. Keep the alignment-edge limit when there is no overflow.
|
||
| let past = right - (ed.origin.x + view.width()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the page margin in the RTL pan limit. With a 1200-point viewport at 100% zoom, centring places the page origin 210 points inside the viewport. Existing ink with a right bound of x=1521 gives room=345 here. At pan=-345, that bound remains 1386 points from the viewport's left edge, so the ink stays off-screen. Subtracting Keep the RTL limit tied to one geometry snapshot. When the same RTL page reopens, For example, with width Preserve matching geometry together, or wait for the replacement editor's layout before applying this limit.
|
||
| (true, if past > 0.5 { past + INK_ABOVE_ROOM * zoom } else { 0.0 }) | ||
| } else { | ||
| let left = self.pad.as_ref().and_then(crate::inkpad::Pad::bounds).map_or(0.0, |[x0, _, _, _]| x0); | ||
| (false, if left < 0.0 { (-left + INK_ABOVE_ROOM) * zoom } else { 0.0 }) | ||
| } | ||
| } | ||
|
|
||
| fn wheel_canvas_pan_delta(delta: egui::Vec2, modifiers: Modifiers, canvas_pan_enabled: bool) -> egui::Vec2 { | ||
| if modifiers.ctrl { | ||
| return egui::Vec2::ZERO; | ||
|
|
@@ -14679,6 +14697,12 @@ impl NoteCraftApp { | |
| if self.canvas_pan.y > top { | ||
| self.canvas_pan.y = top; | ||
| } | ||
| // Sideways the page lines up on one edge, where typing starts: the left for a | ||
| // left-to-right page, the right for a right-to-left one. Panned past it, a finger or a | ||
| // pen reached space where only ink could go (issue #726). The other way the canvas is | ||
| // endless, as down the page. | ||
| let (rtl, room) = self.room_past_page_edge(); | ||
| self.canvas_pan.x = if rtl { self.canvas_pan.x.max(-room) } else { self.canvas_pan.x.min(room) }; | ||
| } else { | ||
| self.canvas_drag = None; | ||
| } | ||
|
|
@@ -21808,6 +21832,68 @@ mod canvas_pan_tests { | |
| settle(&mut h); | ||
| assert_eq!(h.state().canvas_pan, pan, "a page that did not open did not move this one"); | ||
| } | ||
|
|
||
| /// Two fingers on the touchpad, moved `dx` points sideways (positive: the page moves right). | ||
| fn swipe_sideways(h: &mut H, dx: f32) { | ||
| let over = h.state().page_view.expect("the page is shown").center(); | ||
| h.event(egui::Event::PointerMoved(over)); | ||
| h.step(); | ||
| for _ in 0..20 { | ||
| h.event(egui::Event::MouseWheel { | ||
| unit: egui::MouseWheelUnit::Point, | ||
| delta: vec2(dx / 20.0, 0.0), | ||
| phase: egui::TouchPhase::Move, | ||
| modifiers: egui::Modifiers::NONE, | ||
| source: egui::MouseWheelSource::Trackpad, | ||
| }); | ||
| h.step(); | ||
| } | ||
| settle(h); | ||
| } | ||
|
|
||
| /// Issue #726: a left-to-right page lines up on its left edge, where typing starts, so a | ||
| /// finger or a pen cannot pan the page right past that edge into space where only ink could | ||
| /// go. To the right, where there is room to draw, the canvas is still endless. | ||
| #[test] | ||
| fn a_left_to_right_page_cannot_be_panned_past_its_left_edge() { | ||
| let (_t, _sample, mut h) = drawing("pan-left-edge"); | ||
| swipe_sideways(&mut h, 3000.0); | ||
| assert!(h.state().canvas_pan.x <= 0.5, "no pan left of the page's edge: {:?}", h.state().canvas_pan); | ||
| swipe_sideways(&mut h, -3000.0); | ||
| assert!(h.state().canvas_pan.x < -2000.0, "the canvas still pans to the right: {:?}", h.state().canvas_pan); | ||
| swipe_sideways(&mut h, 8000.0); | ||
| assert!(h.state().canvas_pan.x.abs() < 0.5, "back to the edge and no further: {:?}", h.state().canvas_pan); | ||
| } | ||
|
|
||
| /// Issue #726, the other way round: a right-to-left page lines up on its right edge, so it | ||
| /// cannot be panned left past that edge, and the room to draw is to its left. | ||
| #[test] | ||
| fn a_right_to_left_page_cannot_be_panned_past_its_right_edge() { | ||
| let (_t, _sample, mut h) = drawing("pan-right-edge"); | ||
| h.state_mut().editor.as_mut().expect("editor").page_dir = Some(notecraft_model::Dir::Rtl); | ||
| settle(&mut h); | ||
| swipe_sideways(&mut h, -3000.0); | ||
| assert!(h.state().canvas_pan.x >= -0.5, "no pan right of the page's edge: {:?}", h.state().canvas_pan); | ||
| swipe_sideways(&mut h, 3000.0); | ||
| assert!(h.state().canvas_pan.x > 2000.0, "the canvas still pans to the left: {:?}", h.state().canvas_pan); | ||
| } | ||
|
|
||
| /// Ink already drawn left of a left-to-right page can still be reached. | ||
| #[test] | ||
| fn ink_left_of_the_page_can_still_be_panned_to() { | ||
| let (_t, _sample, mut h) = drawing("pan-to-ink-left"); | ||
| { | ||
| let pad = h.state_mut().pad.as_mut().expect("a pad for the page"); | ||
| let mut stroke = notecraft_ink::Stroke::new(notecraft_ink::Tool::Pen, notecraft_ink::Rgb(0, 0, 0), 2.0); | ||
| stroke.add(notecraft_ink::Dab::new(-400.0, 100.0, 1.0)); | ||
| stroke.add(notecraft_ink::Dab::new(-380.0, 140.0, 1.0)); | ||
| pad.drawing.add(stroke); | ||
| } | ||
| settle(&mut h); | ||
| swipe_sideways(&mut h, 8000.0); | ||
| let pan = h.state().canvas_pan.x; | ||
| assert!(pan > 350.0 && pan < 500.0, "left as far as the ink beside the page, and no further: {pan}"); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overlap estimate reserves less height than the picture draws.
block_heightshrinks a picture to the box width, but the page renderer does not. When a picture has a stored width and height, the renderer draws it at its declared size. For the example in this comment, a 768 x 384 picture in a 384-wide box reserves 192 units, while the renderer draws 384 units tall. The next box in that column starts at the reserved bottom and overlaps the lower half of the picture.The reserved height must equal the height the renderer draws. Choose one of these:
block_heightdoes not applyfitto the height.