Merge #5761: M3: a snapshot keeps its resolution when zoomed out (#5716)

# Conflicts:
#	ROADMAP.md
#	docs/roadmap.md
This commit is contained in:
bflatastic committed 2026-11-09 08:07:19 +07:00
commit a65141ab5f
5 files changed
+115 -5

No files matched your search

+1
View File
@@ -258,6 +258,7 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md
- **2026-11-02 (M10, #5743):** macOS print jobs name their title and owner (`com.apple.print.JobInfo.PMJobName`, `PMJobOwner`), as the system print dialog does, because a Kyocera filter crashes on a job without them. Checked by a test and through `doc_print` against a stand-in `lp`; not yet on a physical printer.
- **2026-11-02 (M3, #5708):** Pages shorter than the window sit in its middle, one page or row at a time and when scrolling continuously with every page in view.
- **2026-11-02 (M3, #5716):** A snapshot is drawn at 300 ppi, or at the zoom if that is finer, so one taken zoomed out stays readable; an area too large for one 4096-pixel tile comes out as fine as a tile allows (about 147 ppi for 2000 pt). A choice of resolution and a selection that stays active remain.
- **2026-11-02 (M14, #5737, #5460):** Preferences can turn off the update check at start. It stays on by default, and Help ▸ Check for updates still asks.
- **2026-11-01 (M7, #5657):** Header/footer, watermark and background additions and replacements stage the complete call before committing. Synthetic later-page failures preserve exact incremental bytes, object count, dirty state and allocation cursor; success controls retain other marks and shared/inherited resources. Estimates unchanged.
+48 -3
View File
@@ -120,13 +120,13 @@ impl PdfCraftApp {
Ok(())
}
/// Render `view_rect` (page view points) of `page` at the current zoom and copy it to the
/// clipboard (and keep it in `last_snapshot`).
/// Render `view_rect` (page view points) of `page` and copy it to the clipboard (and keep it
/// in `last_snapshot`), at the current zoom or [`SNAPSHOT_MIN_SCALE`] if that is finer.
pub fn snapshot(&mut self, index: usize, page: usize, view_rect: [f32; 4]) -> Result<(u32, u32), String> {
let view = &self.views[index];
let doc = self.session.get(view.id).ok_or("no document")?;
let ppp = self.ctx.as_ref().map_or(2.0, |c| c.pixels_per_point());
let scale = (view.zoom * ppp).clamp(0.5, 8.0);
let scale = snapshot_scale(view.render_scale(ppp), view_rect[2] - view_rect[0], view_rect[3] - view_rect[1]);
let tile = Tile {
x: (view_rect[0] * scale).floor().max(0.0) as u32,
y: (view_rect[1] * scale).floor().max(0.0) as u32,
@@ -153,6 +153,20 @@ impl PdfCraftApp {
}
}
/// The fewest pixels per point a snapshot is drawn at: 300 ppi, so what it copies stays readable
/// however far the page is zoomed out (#5716).
const SNAPSHOT_MIN_SCALE: f32 = 300.0 / 72.0;
/// The longest side a snapshot has, in pixels: the renderer draws at most 4096 a side in one tile
/// and cuts a larger area short, and this leaves room for rounding up.
const SNAPSHOT_MAX_SIDE: f32 = 4095.0;
/// The pixels per point to draw a `width` × `height` point area at, given the `on_screen` scale:
/// the finer of that and the minimum, or as fine as the renderer can draw the whole area.
fn snapshot_scale(on_screen: f32, width: f32, height: f32) -> f32 {
on_screen.max(SNAPSHOT_MIN_SCALE).min(SNAPSHOT_MAX_SIDE / width.max(height).max(1.0)).max(0.01)
}
/// The normalised bounds [u0, v0, u1, v1] of the pixels that aren't (near) white, if any.
pub(crate) fn ink_bounds(w: usize, h: usize, rgba: &[u8]) -> Option<[f32; 4]> {
if w == 0 || h == 0 || rgba.len() < w * h * 4 {
@@ -175,6 +189,37 @@ pub(crate) fn ink_bounds(w: usize, h: usize, rgba: &[u8]) -> Option<[f32; 4]> {
#[cfg(test)]
mod tests {
use super::{SNAPSHOT_MAX_SIDE, SNAPSHOT_MIN_SCALE, snapshot_scale};
#[test]
fn a_snapshot_is_drawn_at_the_finer_of_the_zoom_and_the_minimum() {
assert_eq!(snapshot_scale(0.5, 100.0, 100.0), SNAPSHOT_MIN_SCALE, "zoomed out");
assert_eq!(snapshot_scale(SNAPSHOT_MIN_SCALE, 100.0, 100.0), SNAPSHOT_MIN_SCALE);
assert_eq!(snapshot_scale(8.0, 100.0, 100.0), 8.0, "zoomed in further, it keeps the detail on screen");
}
#[test]
fn a_snapshot_stays_within_what_the_renderer_draws_at_once() {
// Past 983 pt a side, 300 ppi is more than one tile holds.
for (on_screen, w, h) in [(1.0, 2384.0, 3370.0), (1.0, 3370.0, 100.0), (8.0, 1000.0, 1000.0), (8.0, 14_400.0, 14_400.0)] {
let scale = snapshot_scale(on_screen, w, h);
let side = (w * scale).ceil().max((h * scale).ceil());
assert!(side <= SNAPSHOT_MAX_SIDE + 1.0, "{on_screen} on {w} × {h}: {side} pixels");
}
}
#[test]
fn a_snapshot_of_no_area_is_drawn_at_the_minimum() {
assert_eq!(snapshot_scale(1.0, 0.0, 0.0), SNAPSHOT_MIN_SCALE);
}
#[test]
fn a_snapshot_scale_survives_numbers_that_are_not() {
for scale in [snapshot_scale(f32::NAN, 5000.0, 5000.0), snapshot_scale(1.0, f32::NAN, 100.0), snapshot_scale(1.0, f32::INFINITY, 100.0)] {
assert!(scale.is_finite() && scale > 0.0, "{scale}");
}
}
#[test]
fn ink_bounds_finds_the_marked_area() {
let (w, h) = (10, 20);
+63
View File
@@ -1010,6 +1010,69 @@ fn marquee_zoom_and_snapshot() {
assert!(after > before * 2.0, "{before} → {after}");
}
/// #5716: what a snapshot holds doesn't depend on how far the page is zoomed out.
#[test]
fn a_snapshot_keeps_its_resolution_when_zoomed_out() {
let mut h = harness();
let i = h.state().active.unwrap();
h.state_mut().system_clipboard = false;
let mut sizes = Vec::new();
for zoom in ["25", "100"] {
h.state_mut().set_option("zoom", zoom).unwrap();
h.run_steps(2);
// The text line of the page, as a marquee would give it.
sizes.push(h.state_mut().snapshot(i, 0, [15.0, 125.0, 180.0, 160.0]).expect("a snapshot"));
}
assert_eq!(sizes[0], sizes[1], "the same pixels at either zoom");
assert_eq!(sizes[0], (688, 146), "165 × 35 pt at 300 ppi");
let (_, _, px) = h.state().last_snapshot.clone().expect("a snapshot");
assert!(px.as_chunks::<4>().0.iter().any(|p| p[0] < 100), "the text is in it");
// Zoomed in past the minimum, a snapshot has every pixel the screen shows, however far.
let ppp = h.ctx.pixels_per_point();
for zoom in [400.0_f32, 1600.0] {
h.state_mut().set_option("zoom", &zoom.to_string()).unwrap();
h.run_steps(2);
let (w, _) = h.state_mut().snapshot(i, 0, [15.0, 125.0, 180.0, 160.0]).expect("a snapshot");
let on_screen = (165.0 * zoom / 100.0 * pdfcraft_ui_egui::canvas::PT * ppp).ceil() as u32;
assert!(w.abs_diff(on_screen) <= 1, "{w} pixels at {zoom}%, {on_screen} on screen");
}
}
/// #5716: at 300 ppi this page is more than the renderer draws in one tile, which would cut off
/// the side the snapshot was taken for. A 2000 pt square with a black mark in its far corner.
#[test]
fn a_snapshot_of_a_large_page_reaches_its_far_corner() {
let stream = "0 0 0 rg 1900 0 100 100 re f";
let pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 2000 2000] /Contents 4 0 R >> endobj
4 0 obj << /Length {} >> stream
{stream}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
stream.len()
);
let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(move |_cc| {
let mut app = PdfCraftApp::new();
app.set_option("language", "en").unwrap();
app.open_bytes("large.pdf", None, pdf.into_bytes()).unwrap();
app.system_clipboard = false;
app
});
h.run_steps(4);
let i = h.state().active.unwrap();
let (w, hgt) = h.state_mut().snapshot(i, 0, [0.0, 0.0, 2000.0, 2000.0]).expect("a snapshot");
assert!(w <= 4096 && hgt <= 4096 && w == hgt, "{w} × {hgt}");
let (_, _, px) = h.state().last_snapshot.clone().expect("a snapshot");
let at = |x: u32, y: u32| px[((y * w + x) * 4) as usize];
assert!(at(w * 98 / 100, hgt * 98 / 100) < 100, "the mark in the far corner is in it");
assert!(at(w / 2, hgt / 2) > 200, "and the page around it is white");
}
#[test]
fn fit_visible_zooms_to_the_content_width() {
let mut h = harness();
+1
View File
@@ -325,6 +325,7 @@ Estimates unchanged.
| 2026-11-03 | patch | M5 #5850: Enter adds Fill & Sign line breaks; outside clicks commit, Escape cancels, and saved multiline appearances retain their text and height; estimates unchanged. |
| 2026-11-02 | patch | M10 #5743: macOS print jobs carry the job name and owner some vendors' filters need; not yet checked on a physical printer |
| 2026-11-02 | patch | M3 #5708: pages shorter than the window sit in its middle, on a whole physical pixel |
| 2026-11-02 | patch | M3 #5716: a snapshot keeps its resolution when zoomed out; a choice of resolution remains |
| 2026-11-02 | patch | M14 #5737, #5460: Preferences turns off the update check at start, on by default |
| 2026-11-01 | patch | M7 #5657: header/footer, watermark and background operations commit all selected pages together; later-page errors preserve marks, content, shared/inherited resources and pending edits; estimates unchanged. |
| 2026-10-31 | patch | #1577: reconcile optional page shadows with current viewer/organizer and settings modules; CI validation pending. |
+2 -2
View File
@@ -1586,8 +1586,8 @@ milestone = "M3"
status = "shipped"
commands = ["edit.snapshot"]
tools = ["page_render"]
evidence = ["crates/ui-egui/tests/it/viewer.rs::marquee_zoom_and_snapshot"]
notes = "copies the area at the current zoom to the clipboard as an image"
evidence = ["crates/ui-egui/tests/it/viewer.rs::marquee_zoom_and_snapshot", "crates/ui-egui/tests/it/viewer.rs::a_snapshot_keeps_its_resolution_when_zoomed_out", "crates/ui-egui/src/canvas/zoom_snap.rs::a_snapshot_is_drawn_at_the_finer_of_the_zoom_and_the_minimum", "crates/ui-egui/src/canvas/zoom_snap.rs::a_snapshot_stays_within_what_the_renderer_draws_at_once", "crates/ui-egui/tests/it/viewer.rs::a_snapshot_of_a_large_page_reaches_its_far_corner"]
notes = "copies the area to the clipboard as an image, at 300 ppi or the current zoom if that is finer (#5716), or as fine as the renderer draws in one tile (4096 pixels a side), drawn while the app waits. Missing: a choice of resolution, and a selection that stays active to copy later"
[[feature]]
id = "view.copy-image"