Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge upstream/main into main preserving custom features

Combiler0179eed72c0857
diff --cc Cargo.lock
index cd2109466,fd186d995..3e4dcfb7b
--- a/Cargo.lock
+++ b/Cargo.lock
@@@ -4216,8 -4374,7 +4374,9 @@@ dependencies =
"pdfcraft-cos",
"pdfcraft-fonts",
"pdfcraft-model",
+ "pdfcraft-render",
+ "proptest",
+ "sha2 0.11.1",
"thiserror 2.0.21",
"unicode-bidi",
]
diff --cc crates/edit/src/images.rs
index 63372a39d,f66e7f24e..8d19a2320
--- a/crates/edit/src/images.rs
+++ b/crates/edit/src/images.rs
@@@ -547,999 -473,6 +547,1032 @@@ fn clip_matrix(draw: Matrix, cm: Matrix
Some(outer.then(&t).then(&outer.invert()?))
}
+pub(crate) fn is_entire_page_wrapped(ops: &[Op]) -> bool {
+ if ops.len() < 2 || !ops.first().is_some_and(|o| o.is("q")) || !ops.last().is_some_and(|o| o.is("Q")) {
+ return false;
+ }
+ let mut depth: usize = 0;
+ for (i, op) in ops.iter().enumerate() {
+ if op.is("q") {
+ depth = depth.saturating_add(1);
+ } else if op.is("Q") {
+ if depth == 1 && i < ops.len().saturating_sub(1) {
+ return false;
+ }
+ depth = depth.saturating_sub(1);
+ }
+ }
+ depth == 0
+}
+
+pub(crate) fn content_units(ops: &[Op], scan_start: usize, scan_end: usize) -> Vec<std::ops::Range<usize>> {
+ let mut units = Vec::new();
+ let mut i = scan_start;
+ while i < scan_end {
+ let op = match ops.get(i) {
+ Some(o) => o,
+ None => break,
+ };
+ if op.is("BDC") || op.is("BMC") {
+ // A marked-content sequence is one unit up to its matching EMC (#3145), so Arrange moves
+ // it whole and never inserts into another one: its content belongs to its tag (ISO 32000-2
+ // § 14.6). An unbalanced sequence runs to the end of the scan.
+ let mut depth: usize = 1;
+ let mut j = i.saturating_add(1);
+ while j < scan_end {
+ if let Some(next) = ops.get(j) {
+ if next.is("BDC") || next.is("BMC") {
+ depth = depth.saturating_add(1);
+ } else if next.is("EMC") {
+ depth = depth.saturating_sub(1);
+ if depth == 0 {
+ j = j.saturating_add(1);
+ break;
+ }
+ }
+ }
+ j = j.saturating_add(1);
+ }
+ units.push(i..j);
+ i = j;
+ } else if op.is("q") {
+ let mut depth: usize = 1;
+ let mut j = i.saturating_add(1);
+ while j < scan_end {
+ if let Some(next) = ops.get(j) {
+ if next.is("q") {
+ depth = depth.saturating_add(1);
+ } else if next.is("Q") {
+ depth = depth.saturating_sub(1);
+ if depth == 0 {
+ j = j.saturating_add(1);
+ break;
+ }
+ }
+ }
+ j = j.saturating_add(1);
+ }
+ units.push(i..j);
+ i = j;
+ } else if op.is("BT") {
+ let mut j = i.saturating_add(1);
+ while j < scan_end {
+ if ops.get(j).is_some_and(|o| o.is("ET")) {
+ j = j.saturating_add(1);
+ break;
+ }
+ j = j.saturating_add(1);
+ }
+ units.push(i..j);
+ i = j;
+ } else if op.is("BI") {
+ let mut j = i.saturating_add(1);
+ while j < scan_end {
+ if ops.get(j).is_some_and(|o| o.is("EI")) {
+ j = j.saturating_add(1);
+ break;
+ }
+ j = j.saturating_add(1);
+ }
+ units.push(i..j);
+ i = j;
+ } else if op.is("Do") || op.is("sh") {
+ units.push(i..i.saturating_add(1));
+ i = i.saturating_add(1);
+ } else {
+ let mut j = i;
+ let mut found_end = false;
+ while j < scan_end {
+ let next = match ops.get(j) {
+ Some(o) => o,
+ None => break,
+ };
+ if matches!(next.op.as_slice(), b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*" | b"n") {
+ j = j.saturating_add(1);
+ found_end = true;
+ break;
+ } else if j > i
+ && matches!(
+ next.op.as_slice(),
+ b"q" | b"Q" | b"BT" | b"BI" | b"Do" | b"sh" | b"BDC" | b"BMC" | b"EMC" | b"Tj" | b"TJ" | b"'" | b"\""
+ )
+ {
+ found_end = true;
+ break;
+ }
+ j = j.saturating_add(1);
+ }
+ let u_end = if found_end { j } else { scan_end.max(i.saturating_add(1)) };
+ units.push(i..u_end);
+ i = u_end;
+ }
+ }
+ units.retain(|u| {
+ ops.get(u.clone()).is_some_and(|slice| {
+ slice.iter().any(|op| {
+ matches!(
+ op.op.as_slice(),
+ b"Do" | b"sh" | b"BI" | b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*" | b"Tj" | b"TJ" | b"'" | b"\""
+ )
+ })
+ })
+ });
+ units
+}
+
+#[derive(Clone, Debug, PartialEq)]
+pub(crate) struct ExtractedScopeState {
+ pub clip_ops: Vec<Op>,
+ pub state_ops: Vec<Op>,
+ pub effective_cm: Matrix,
+ pub do_op: Op,
+}
+
+#[derive(Clone, Debug, Default, PartialEq)]
+pub(crate) struct EffectiveGState {
+ pub(crate) ca: Option<f64>,
+ pub(crate) ca_stroke: Option<f64>,
+ pub(crate) bm: Option<Vec<u8>>,
+ pub(crate) smask: Option<MaskIdentity>,
+}
+
+impl EffectiveGState {
+ pub(crate) fn effective_ca(&self) -> f64 {
+ self.ca.unwrap_or(1.0)
+ }
+
+ pub(crate) fn effective_ca_stroke(&self) -> f64 {
+ self.ca_stroke.unwrap_or(1.0)
+ }
+
+ pub(crate) fn effective_bm(&self) -> &[u8] {
+ self.bm.as_deref().unwrap_or(b"Normal")
+ }
+
+ pub(crate) fn effective_smask(&self) -> Option<&MaskIdentity> {
+ self.smask.as_ref()
+ }
+
+ pub(crate) fn visually_differs(&self, other: &EffectiveGState) -> bool {
+ (self.effective_ca() - other.effective_ca()).abs() > 1e-4
+ || (self.effective_ca_stroke() - other.effective_ca_stroke()).abs() > 1e-4
+ || self.effective_bm() != other.effective_bm()
+ || self.effective_smask() != other.effective_smask()
+ }
+
+ pub(crate) fn merge_dict(&mut self, dict: &Dict, doc: &Document, gs_name: Option<&[u8]>) {
+ if let Some(val) = dict.get(b"ca").map(|v| doc.resolve(v)).and_then(|v| v.as_f64()) {
+ self.ca = Some(val);
+ }
+ if let Some(val) = dict.get(b"CA").map(|v| doc.resolve(v)).and_then(|v| v.as_f64()) {
+ self.ca_stroke = Some(val);
+ }
+ if let Some(bm_obj) = dict.get(b"BM").map(|v| doc.resolve(v)) {
+ if let Some(name) = bm_obj.as_name() {
+ self.bm = Some(name.to_vec());
+ } else if let Some(arr) = bm_obj.as_array()
+ && let Some(first_name) = arr.first().map(|v| doc.resolve(v)).and_then(|v| v.as_name().map(|n| n.to_vec()))
+ {
+ self.bm = Some(first_name);
+ }
+ }
+ if let Some(raw_smask) = dict.get(b"SMask") {
+ let resolved = doc.resolve(raw_smask);
+ if let Some(name) = resolved.as_name() {
+ if name == b"None" {
+ self.smask = None;
+ } else {
+ self.smask = Some(MaskIdentity::Named(name.to_vec()));
+ }
+ } else if let Some(mask_dict) = resolved.as_dict() {
+ if let Some(gr) = mask_dict.get(b"G").and_then(|g| g.as_ref()) {
+ self.smask = Some(MaskIdentity::Group(gr.num, gr.generation));
+ } else if let Some(r) = raw_smask.as_ref() {
+ self.smask = Some(MaskIdentity::Ref(r.num, r.generation));
+ } else if let Some(name) = mask_dict.get(b"Name").map(|n| doc.resolve(n)).and_then(|n| n.as_name().map(|s| s.to_vec())) {
+ self.smask = Some(MaskIdentity::Named(name));
+ } else if let Some(name) = gs_name {
+ self.smask = Some(MaskIdentity::Named(name.to_vec()));
+ } else {
+ use std::collections::hash_map::DefaultHasher;
+ use std::hash::{Hash, Hasher};
+ let mut hasher = DefaultHasher::new();
+ for (k, v) in mask_dict.iter() {
+ k.as_slice().hash(&mut hasher);
+ format!("{v:?}").hash(&mut hasher);
+ }
+ let id = format!("InlineMask_{:016x}", hasher.finish());
+ self.smask = Some(MaskIdentity::Named(id.into_bytes()));
+ }
+ } else if let Some(r) = raw_smask.as_ref() {
+ self.smask = Some(MaskIdentity::Ref(r.num, r.generation));
+ }
+ }
+ }
+}
+
+const MAX_ACTIVE_GS_OPS: usize = 128;
+
+type ActiveGsOp = (Vec<u8>, Op, Matrix);
+type ActiveGsOps = std::rc::Rc<Vec<ActiveGsOp>>;
+
+#[derive(Clone, Default)]
+struct ExtendedGStateTracker {
+ stack: Vec<(EffectiveGState, ActiveGsOps)>,
+ current_state: EffectiveGState,
+ active_gs_ops: ActiveGsOps,
+ overflow_depth: usize,
+}
+
+impl ExtendedGStateTracker {
+ fn step(&mut self, op: &Op, ext_gstates: &Dict, doc: &Document, ctm: Matrix) -> Result<(), EditError> {
+ match op.op.as_slice() {
+ b"q" => {
+ if self.stack.len() >= 4096 || self.overflow_depth > 0 {
+ self.overflow_depth = self.overflow_depth.saturating_add(1);
+ return Err(EditError::Invalid("the image graphics state is too deeply nested".into()));
+ }
+ self.stack.push((self.current_state.clone(), std::rc::Rc::clone(&self.active_gs_ops)));
+ }
+ b"Q" => {
+ if self.overflow_depth > 0 {
+ self.overflow_depth = self.overflow_depth.saturating_sub(1);
+ } else if let Some((prev_state, prev_ops)) = self.stack.pop() {
+ self.current_state = prev_state;
+ self.active_gs_ops = prev_ops;
+ }
+ }
+ b"gs" => {
+ if self.overflow_depth > 0 {
+ return Ok(());
+ }
+ let valid_gs = op
+ .name(0)
+ .and_then(|name| ext_gstates.get(name).map(|v| doc.resolve(v)).and_then(|v| v.as_dict().cloned()).map(|dict| (name, dict)));
+ if let Some((name, dict)) = valid_gs {
+ self.current_state.merge_dict(&dict, doc, Some(name));
+ let mut next = (*self.active_gs_ops).clone();
+ if let Some(pos) = next.iter().position(|(k, _, _)| k.as_slice() == name) {
+ if let Some(entry) = next.get_mut(pos) {
+ *entry = (name.to_vec(), op.clone(), ctm);
+ }
+ } else if next.len() < MAX_ACTIVE_GS_OPS {
+ next.push((name.to_vec(), op.clone(), ctm));
+ }
+ self.active_gs_ops = std::rc::Rc::new(next);
+ }
+ }
+ _ => {}
+ }
+ Ok(())
+ }
+}
+
+fn clip_path_to_ops(clip: &ClipPath) -> Vec<Op> {
+ let mut ops = Vec::new();
+ let mut current_pt = Point::ZERO;
+ for el in clip.path.elements() {
+ match el {
+ kurbo::PathEl::MoveTo(p) => {
+ if !p.x.is_finite() || !p.y.is_finite() {
+ return Vec::new();
+ }
+ current_pt = *p;
+ ops.push(Op::new("m", vec![pdfcraft_content::num(p.x), pdfcraft_content::num(p.y)]));
+ }
+ kurbo::PathEl::LineTo(p) => {
+ if !p.x.is_finite() || !p.y.is_finite() {
+ return Vec::new();
+ }
+ current_pt = *p;
+ ops.push(Op::new("l", vec![pdfcraft_content::num(p.x), pdfcraft_content::num(p.y)]));
+ }
+ kurbo::PathEl::QuadTo(p1, p2) => {
+ if !p1.x.is_finite() || !p1.y.is_finite() || !p2.x.is_finite() || !p2.y.is_finite() {
+ return Vec::new();
+ }
+ let c1_x = current_pt.x + (p1.x - current_pt.x) * (2.0 / 3.0);
+ let c1_y = current_pt.y + (p1.y - current_pt.y) * (2.0 / 3.0);
+ let c2_x = p2.x + (p1.x - p2.x) * (2.0 / 3.0);
+ let c2_y = p2.y + (p1.y - p2.y) * (2.0 / 3.0);
+ if !c1_x.is_finite() || !c1_y.is_finite() || !c2_x.is_finite() || !c2_y.is_finite() {
+ return Vec::new();
+ }
+ current_pt = *p2;
+ ops.push(Op::new(
+ "c",
+ vec![
+ pdfcraft_content::num(c1_x),
+ pdfcraft_content::num(c1_y),
+ pdfcraft_content::num(c2_x),
+ pdfcraft_content::num(c2_y),
+ pdfcraft_content::num(p2.x),
+ pdfcraft_content::num(p2.y),
+ ],
+ ));
+ }
+ kurbo::PathEl::CurveTo(p1, p2, p3) => {
+ if !p1.x.is_finite() || !p1.y.is_finite() || !p2.x.is_finite() || !p2.y.is_finite() || !p3.x.is_finite() || !p3.y.is_finite() {
+ return Vec::new();
+ }
+ current_pt = *p3;
+ ops.push(Op::new(
+ "c",
+ vec![
+ pdfcraft_content::num(p1.x),
+ pdfcraft_content::num(p1.y),
+ pdfcraft_content::num(p2.x),
+ pdfcraft_content::num(p2.y),
+ pdfcraft_content::num(p3.x),
+ pdfcraft_content::num(p3.y),
+ ],
+ ));
+ }
+ kurbo::PathEl::ClosePath => {
+ ops.push(Op::new("h", vec![]));
+ }
+ }
+ }
+ if !ops.is_empty() {
+ ops.push(Op::new(if clip.even_odd { "W*" } else { "W" }, vec![]));
+ ops.push(Op::new("n", vec![]));
+ }
+ ops
+}
+
+pub(crate) fn extract_scope_state(ops: &[Op], start: usize, target: usize, initial_ctm: Matrix) -> Result<Option<ExtractedScopeState>, EditError> {
+ if target >= ops.len() || start > target {
+ return Ok(None);
+ }
+ let target_op = ops.get(target).cloned().ok_or_else(|| EditError::Invalid("the image operation is missing".into()))?;
+ if start == target {
+ return Ok(Some(ExtractedScopeState { clip_ops: Vec::new(), state_ops: Vec::new(), effective_cm: initial_ctm, do_op: target_op }));
+ }
+ let mut clip_ops = Vec::new();
+ let mut state_ops = Vec::new();
+ let mut pending_path: Vec<Op> = Vec::new();
+ let mut pending_clip = false;
+ let mut active_path = false;
+ let mut depth: usize = 0;
+ let mut matrix = initial_ctm;
+ let mut matrix_at_clip = None;
+
+ let scan_start = if ops.get(start).is_some_and(|o| o.is("q")) { start.saturating_add(1) } else { start };
+
+ for i in scan_start..target {
+ let Some(op) = ops.get(i) else {
+ return Ok(None);
+ };
+ match op.op.as_slice() {
+ b"q" => {
+ if depth >= 4096 {
+ return Err(EditError::Invalid("the image graphics state is too deeply nested".into()));
+ }
+ depth = depth.saturating_add(1);
+ }
+ b"Q" => {
+ if depth == 0 {
+ return Ok(None);
+ }
+ depth = depth.saturating_sub(1);
+ }
+ _ if depth > 0 => {}
+ b"cm" => {
+ if active_path {
+ return Ok(None);
+ }
+ if let Some(m) = op.nums::<6>() {
+ matrix = Matrix(m).then(&matrix);
+ }
+ }
+ b"m" | b"l" | b"c" | b"v" | b"y" | b"re" | b"h" => {
+ active_path = true;
+ pending_path.push(op.clone());
+ }
+ b"W" | b"W*" => {
+ if !active_path {
+ return Ok(None);
+ }
+ pending_clip = true;
+ pending_path.push(op.clone());
+ }
+ b"n" => {
+ if pending_clip {
+ if matrix_at_clip.is_none() {
+ matrix_at_clip = Some(matrix);
+ } else if matrix_at_clip != Some(matrix) {
+ return Ok(None);
+ }
+ clip_ops.extend(std::mem::take(&mut pending_path));
+ clip_ops.push(op.clone());
+ pending_clip = false;
+ active_path = false;
+ } else {
+ pending_path.clear();
+ active_path = false;
+ }
+ }
+ b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*" => {
+ if pending_clip {
+ return Ok(None);
+ }
+ pending_path.clear();
+ active_path = false;
+ }
+ b"gs" | b"CS" | b"cs" | b"SC" | b"sc" | b"SCN" | b"scn" | b"G" | b"g" | b"RG" | b"rg" | b"K" | b"k" | b"ri" | b"i" => {
+ state_ops.push(op.clone());
+ }
+ b"BT" => {
+ return Ok(None);
+ }
+ _ => {}
+ }
+ }
+
+ if depth != 0 || active_path || pending_clip {
+ return Ok(None);
+ }
+
+ if !clip_ops.is_empty() && matrix_at_clip != Some(Matrix::IDENTITY) {
+ return Ok(None);
+ }
+
+ Ok(Some(ExtractedScopeState { clip_ops, state_ops, effective_cm: matrix, do_op: target_op }))
+}
+
+/// Change the stacking order of image `index` on `page`.
- pub fn arrange_image(doc: &mut Document, page: usize, index: usize, order: ArrangeOrder) -> Result<(), EditError> {
- let images = page_images(doc, page)?;
- let img = images.get(index).cloned().ok_or_else(|| EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1)))?;
- let p = page_of(doc, page)?;
- let all = content_streams(doc, &p.dict)?;
- let pieces = pdfcraft_content::Pieces::join(&all.iter().map(|(_, data)| data.as_slice()).collect::<Vec<_>>());
- let ops = pieces.parse().ops;
- let target = img.op;
- if !ops.get(target).is_some_and(|op| op.is("Do")) {
- return Err(EditError::Invalid("the image operation could not be located".into()));
- }
-
- let is_wrapped = is_entire_page_wrapped(&ops);
- let (scan_start, scan_end) = if is_wrapped { (1, ops.len().saturating_sub(1)) } else { (0, ops.len()) };
- let units = content_units(&ops, scan_start, scan_end);
- let Some(target_idx) = units.iter().position(|u| u.start <= target && target < u.end) else {
- return Err(EditError::Invalid("the image content unit could not be located".into()));
- };
- let Some(target_unit) = units.get(target_idx) else { return Ok(()) };
- let has_other_paint = ops.iter().enumerate().any(|(i, op)| {
- i >= target_unit.start
- && i < target_unit.end
- && i != target
- && matches!(
- op.op.as_slice(),
- b"Do" | b"Tj" | b"TJ" | b"'" | b"\"" | b"sh" | b"BI" | b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*"
- )
- });
- if units.len() <= 1 && !has_other_paint {
- return Err(EditError::Invalid("the page has only one content unit to arrange".into()));
- }
-
- let (dest_unit, insert_after) = match order {
++fn target_destination_unit<'a>(
++ units: &'a [std::ops::Range<usize>],
++ target_idx: usize,
++ target_unit: &'a std::ops::Range<usize>,
++ order: ArrangeOrder,
++ has_other_paint: bool,
++) -> Result<(&'a std::ops::Range<usize>, bool), EditError> {
++ match order {
+ ArrangeOrder::BringToFront => {
+ if target_idx == units.len().saturating_sub(1) {
+ if has_other_paint {
- (target_unit, true)
++ Ok((target_unit, true))
+ } else {
- return Err(EditError::Invalid("the image is already in front".into()));
++ Err(EditError::Invalid("the image is already in front".into()))
+ }
+ } else {
- let Some(u) = units.last() else {
- return Err(EditError::Invalid("the image is already in front".into()));
- };
- (u, true)
++ let u = units.last().ok_or_else(|| EditError::Invalid("the image is already in front".into()))?;
++ Ok((u, true))
+ }
+ }
+ ArrangeOrder::SendToBack => {
+ if target_idx == 0 {
+ if has_other_paint {
- (target_unit, false)
++ Ok((target_unit, false))
+ } else {
- return Err(EditError::Invalid("the image is already at the back".into()));
++ Err(EditError::Invalid("the image is already at the back".into()))
+ }
+ } else {
- let Some(u) = units.first() else {
- return Err(EditError::Invalid("the image is already at the back".into()));
- };
- (u, false)
++ let u = units.first().ok_or_else(|| EditError::Invalid("the image is already at the back".into()))?;
++ Ok((u, false))
+ }
+ }
+ ArrangeOrder::BringForward => {
+ if target_idx == units.len().saturating_sub(1) {
+ if has_other_paint {
- (target_unit, true)
++ Ok((target_unit, true))
+ } else {
- return Err(EditError::Invalid("the image is already in front".into()));
++ Err(EditError::Invalid("the image is already in front".into()))
+ }
+ } else {
- let Some(u) = units.get(target_idx.saturating_add(1)) else {
- return Err(EditError::Invalid("the image is already in front".into()));
- };
- (u, true)
++ let u = units.get(target_idx.saturating_add(1)).ok_or_else(|| EditError::Invalid("the image is already in front".into()))?;
++ Ok((u, true))
+ }
+ }
+ ArrangeOrder::SendBackward => {
+ if target_idx == 0 {
+ if has_other_paint {
- (target_unit, false)
++ Ok((target_unit, false))
+ } else {
- return Err(EditError::Invalid("the image is already at the back".into()));
++ Err(EditError::Invalid("the image is already at the back".into()))
+ }
+ } else {
- let Some(u) = target_idx.checked_sub(1).and_then(|i| units.get(i)) else {
- return Err(EditError::Invalid("the image is already at the back".into()));
- };
- (u, false)
++ let u = target_idx.checked_sub(1).and_then(|i| units.get(i)).ok_or_else(|| EditError::Invalid("the image is already at the back".into()))?;
++ Ok((u, false))
+ }
+ }
- };
-
- // A marked-content sequence that holds other content cannot be split without generating
- // a new structure element and MCID, so we refuse to rearrange it to avoid leaving the image untagged.
- // If the sequence holds only this image, the whole marked unit is moved intact.
- let marked_unit = ops.get(target_unit.start).is_some_and(|o| o.is("BDC") || o.is("BMC"));
- if marked_unit && has_other_paint {
- return Err(EditError::Invalid("cannot rearrange an image that shares a marked-content sequence with other content".into()));
+ }
++}
+
- let target_op = ops.get(target).ok_or_else(|| EditError::Invalid("the image operation could not be located".into()))?;
- let (mut target_span, scope_start, rem_start, rem_end) = if has_other_paint {
- if let Ok(Some((sub_start, sub_end, _))) = image_exclusive_scope(&ops, target)
++fn determine_arrange_spans(
++ ops: &[Op],
++ target: usize,
++ target_unit: &std::ops::Range<usize>,
++ target_op: &Op,
++ has_other_paint: bool,
++ marked_unit: bool,
++) -> Option<(std::ops::Range<usize>, usize, usize, usize)> {
++ if has_other_paint {
++ if let Ok(Some((sub_start, sub_end, _))) = image_exclusive_scope(ops, target)
+ && sub_start >= target_unit.start
+ && sub_end < target_unit.end
+ {
+ let start_op = ops.get(sub_start).unwrap_or(target_op);
+ let end_op = ops.get(sub_end).unwrap_or(target_op);
- (start_op.span.start..end_op.span.end, sub_start, sub_start, sub_end.saturating_add(1))
++ Some((start_op.span.start..end_op.span.end, sub_start, sub_start, sub_end.saturating_add(1)))
+ } else {
- (target_op.span.start..target_op.span.end, target, target, target.saturating_add(1))
++ Some((target_op.span.start..target_op.span.end, target, target, target.saturating_add(1)))
+ }
- } else if let Ok(Some((sub_start, sub_end, _))) = image_exclusive_scope(&ops, target)
++ } else if let Ok(Some((sub_start, sub_end, _))) = image_exclusive_scope(ops, target)
+ && sub_start >= target_unit.start
+ && sub_end < target_unit.end
+ && !marked_unit
+ {
+ let start_op = ops.get(sub_start).unwrap_or(target_op);
+ let end_op = ops.get(sub_end).unwrap_or(target_op);
- (start_op.span.start..end_op.span.end, sub_start, sub_start, sub_end.saturating_add(1))
++ Some((start_op.span.start..end_op.span.end, sub_start, sub_start, sub_end.saturating_add(1)))
+ } else if marked_unit {
+ // A marked-content unit holding only this image moves whole, with its BDC…EMC.
+ // The whole sequence is removed; the image's state is rebuilt from the ops inside it.
- let Some(first_op) = ops.get(target_unit.start) else { return Ok(()) };
- let Some(last_op) = target_unit.end.checked_sub(1).and_then(|i| ops.get(i)) else { return Ok(()) };
- (first_op.span.start..last_op.span.end, target_unit.start.saturating_add(1).min(target), target_unit.start, target_unit.end)
++ let first_op = ops.get(target_unit.start)?;
++ let last_op = target_unit.end.checked_sub(1).and_then(|i| ops.get(i))?;
++ Some((first_op.span.start..last_op.span.end, target_unit.start.saturating_add(1).min(target), target_unit.start, target_unit.end))
+ } else {
- let Some(first_op) = ops.get(target_unit.start) else { return Ok(()) };
- let Some(last_op) = target_unit.end.checked_sub(1).and_then(|i| ops.get(i)) else { return Ok(()) };
- (first_op.span.start..last_op.span.end, target_unit.start, target_unit.start, target_unit.end)
- };
-
- let (dest_pos, scan_target) = if insert_after {
- let Some(op) = dest_unit.end.checked_sub(1).and_then(|i| ops.get(i)) else { return Ok(()) };
- (op.span.end, dest_unit.end)
- } else {
- let Some(op) = ops.get(dest_unit.start) else { return Ok(()) };
- (op.span.start, dest_unit.start)
- };
++ let first_op = ops.get(target_unit.start)?;
++ let last_op = target_unit.end.checked_sub(1).and_then(|i| ops.get(i))?;
++ Some((first_op.span.start..last_op.span.end, target_unit.start, target_unit.start, target_unit.end))
++ }
++}
+
++fn scan_leaking_ops(ops: &[Op], rem_start: usize, rem_end: usize) -> (Vec<Op>, bool) {
+ let mut q_depth = 0usize;
+ let mut leaking_ops = Vec::new();
+ let mut pending_clip_path: Vec<Op> = Vec::new();
+ let mut pending_clip = false;
+ let mut has_leaking_clip = false;
+ for op in ops.get(rem_start..rem_end).unwrap_or_default() {
+ match op.op.as_slice() {
+ b"q" => q_depth = q_depth.saturating_add(1),
+ b"Q" => q_depth = q_depth.saturating_sub(1),
+ b"m" | b"l" | b"c" | b"v" | b"y" | b"re" | b"h" if q_depth == 0 => {
+ pending_clip_path.push(op.clone());
+ }
+ b"W" | b"W*" if q_depth == 0 => {
+ if !pending_clip_path.is_empty() {
+ pending_clip = true;
+ pending_clip_path.push(op.clone());
+ }
+ }
+ b"n" if q_depth == 0 => {
+ if pending_clip {
+ pending_clip_path.push(op.clone());
+ leaking_ops.extend(std::mem::take(&mut pending_clip_path));
+ has_leaking_clip = true;
+ pending_clip = false;
+ } else {
+ pending_clip_path.clear();
+ }
+ }
+ b"rg" | b"RG" | b"g" | b"G" | b"k" | b"K" | b"cs" | b"CS" | b"sc" | b"scn" | b"SC" | b"SCN" | b"w" | b"J" | b"j" | b"M" | b"d"
+ | b"ri" | b"i" | b"gs" | b"Tf" | b"Tc" | b"Tw" | b"Tz" | b"TL" | b"Tr" | b"Ts"
+ if q_depth == 0 =>
+ {
+ leaking_ops.push(op.clone());
+ }
+ b"cm" if q_depth == 0 => {
+ leaking_ops.push(op.clone());
+ }
+ _ => {
+ if q_depth == 0 && !op.is("BDC") && !op.is("BMC") && !op.is("EMC") && !pending_clip {
+ pending_clip_path.clear();
+ }
+ }
+ }
+ }
++ (leaking_ops, has_leaking_clip)
++}
++
++fn build_relocated_unit_ops(
++ marked_open: Option<&Op>,
++ source_gs_ops: &[(Op, Matrix)],
++ extracted: ExtractedScopeState,
++ dest_ctm: Matrix,
++ draw_matrix: [f64; 6],
++ has_closing_emc: bool,
++) -> Result<Vec<Op>, EditError> {
++ let mut unit_ops = Vec::new();
++ if let Some(open) = marked_open {
++ unit_ops.push(open.clone());
++ }
++ unit_ops.push(Op::new("q", vec![]));
++ let mut current_ctm = dest_ctm;
++
++ for (gs_op, target_ctm) in source_gs_ops {
++ if *target_ctm != current_ctm {
++ let current_inv = current_ctm.invert().ok_or_else(|| EditError::Invalid("unsupported graphics state: non-invertible CTM".into()))?;
++ let delta = target_ctm.then(&current_inv);
++ if !delta.0.iter().all(|v| v.is_finite()) {
++ return Err(EditError::Invalid("unsupported graphics state: invalid transform coordinates".into()));
++ }
++ if delta != Matrix::IDENTITY {
++ unit_ops.push(Op::new("cm", delta.0.map(pdfcraft_content::num).to_vec()));
++ current_ctm = *target_ctm;
++ }
++ }
++ unit_ops.push(gs_op.clone());
++ }
++
++ let non_gs_ops = extracted.state_ops.into_iter().filter(|o| !o.is("gs"));
++ unit_ops.extend(non_gs_ops);
++
++ if !extracted.clip_ops.is_empty() {
++ if current_ctm != Matrix::IDENTITY {
++ let current_inv = current_ctm.invert().ok_or_else(|| EditError::Invalid("unsupported graphics state: non-invertible CTM".into()))?;
++ unit_ops.push(Op::new("cm", current_inv.0.map(pdfcraft_content::num).to_vec()));
++ }
++ unit_ops.extend(extracted.clip_ops);
++ if Matrix(draw_matrix) != Matrix::IDENTITY {
++ unit_ops.push(Op::new("cm", draw_matrix.map(pdfcraft_content::num).to_vec()));
++ }
++ } else if Matrix(draw_matrix) != current_ctm {
++ let current_inv = current_ctm.invert().ok_or_else(|| EditError::Invalid("unsupported graphics state: non-invertible CTM".into()))?;
++ let delta = Matrix(draw_matrix).then(&current_inv);
++ if !delta.0.iter().all(|v| v.is_finite()) {
++ return Err(EditError::Invalid("unsupported graphics state: invalid transform coordinates".into()));
++ }
++ if delta != Matrix::IDENTITY {
++ unit_ops.push(Op::new("cm", delta.0.map(pdfcraft_content::num).to_vec()));
++ }
++ }
++ unit_ops.push(extracted.do_op);
++ unit_ops.push(Op::new("Q", vec![]));
++ if has_closing_emc {
++ unit_ops.push(Op::new("EMC", vec![]));
++ }
++ Ok(unit_ops)
++}
++
++/// Change the stacking order of image `index` on `page`.
++pub fn arrange_image(doc: &mut Document, page: usize, index: usize, order: ArrangeOrder) -> Result<(), EditError> {
++ let images = page_images(doc, page)?;
++ let img = images.get(index).cloned().ok_or_else(|| EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1)))?;
++ let p = page_of(doc, page)?;
++ let all = content_streams(doc, &p.dict)?;
++ let pieces = pdfcraft_content::Pieces::join(&all.iter().map(|(_, data)| data.as_slice()).collect::<Vec<_>>());
++ let ops = pieces.parse().ops;
++ let target = img.op;
++ if !ops.get(target).is_some_and(|op| op.is("Do")) {
++ return Err(EditError::Invalid("the image operation could not be located".into()));
++ }
++
++ let is_wrapped = is_entire_page_wrapped(&ops);
++ let (scan_start, scan_end) = if is_wrapped { (1, ops.len().saturating_sub(1)) } else { (0, ops.len()) };
++ let units = content_units(&ops, scan_start, scan_end);
++ let Some(target_idx) = units.iter().position(|u| u.start <= target && target < u.end) else {
++ return Err(EditError::Invalid("the image content unit could not be located".into()));
++ };
++ let Some(target_unit) = units.get(target_idx) else { return Ok(()) };
++ let has_other_paint = ops.iter().enumerate().any(|(i, op)| {
++ i >= target_unit.start
++ && i < target_unit.end
++ && i != target
++ && matches!(
++ op.op.as_slice(),
++ b"Do" | b"Tj" | b"TJ" | b"'" | b"\"" | b"sh" | b"BI" | b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*"
++ )
++ });
++ if units.len() <= 1 && !has_other_paint {
++ return Err(EditError::Invalid("the page has only one content unit to arrange".into()));
++ }
++
++ let (dest_unit, insert_after) = target_destination_unit(&units, target_idx, target_unit, order, has_other_paint)?;
++
++ // A marked-content sequence that holds other content cannot be split without generating
++ // a new structure element and MCID, so we refuse to rearrange it to avoid leaving the image untagged.
++ // If the sequence holds only this image, the whole marked unit is moved intact.
++ let marked_unit = ops.get(target_unit.start).is_some_and(|o| o.is("BDC") || o.is("BMC"));
++ if marked_unit && has_other_paint {
++ return Err(EditError::Invalid("cannot rearrange an image that shares a marked-content sequence with other content".into()));
++ }
++
++ let target_op = ops.get(target).ok_or_else(|| EditError::Invalid("the image operation could not be located".into()))?;
++ let Some((mut target_span, scope_start, rem_start, rem_end)) =
++ determine_arrange_spans(&ops, target, target_unit, target_op, has_other_paint, marked_unit)
++ else {
++ return Ok(());
++ };
++
++ let (dest_pos, scan_target) = if insert_after {
++ let Some(op) = dest_unit.end.checked_sub(1).and_then(|i| ops.get(i)) else { return Ok(()) };
++ (op.span.end, dest_unit.end)
++ } else {
++ let Some(op) = ops.get(dest_unit.start) else { return Ok(()) };
++ (op.span.start, dest_unit.start)
++ };
++
++ let (leaking_ops, has_leaking_clip) = scan_leaking_ops(&ops, rem_start, rem_end);
+
+ let res = p.dict.get(b"Resources").map(|r| doc.resolve(r)).and_then(|r| r.as_dict().cloned()).unwrap_or_default();
+ let ext_gstates = res.get(b"ExtGState").map(|g| doc.resolve(g)).and_then(|g| g.as_dict().cloned()).unwrap_or_default();
+
+ let mut state = ImageState::new(Matrix::IDENTITY, p.crop(doc), Vec::new());
+ let mut tracker = ExtendedGStateTracker::default();
+ let mut source_effective_state = EffectiveGState::default();
+ let mut source_gs_ops = Vec::new();
+ let mut dest_effective_state = EffectiveGState::default();
+ let mut dest_ctm = Matrix::IDENTITY;
+ let mut scope_start_ctm = Matrix::IDENTITY;
+ let mut outer_clips = Vec::new();
+ let mut dest_clips = Vec::new();
+ let mut ctm_before_removal = Matrix::IDENTITY;
+ let mut ctm_after_removal = Matrix::IDENTITY;
+
+ let max_scan = scan_target.max(target.saturating_add(1)).max(scope_start.saturating_add(1)).max(rem_end);
+ for (i, op) in ops.iter().enumerate() {
+ if i == rem_start {
+ ctm_before_removal = state.placement.matrix;
+ }
+ if i == scope_start {
+ scope_start_ctm = state.placement.matrix;
+ outer_clips = state.placement.clips.clone();
+ }
+ if i == target {
+ source_effective_state = tracker.current_state.clone();
+ source_gs_ops = tracker.active_gs_ops.iter().map(|(_, op, ctm)| (op.clone(), *ctm)).collect();
+ }
+ if i == scan_target {
+ dest_effective_state = tracker.current_state.clone();
+ dest_ctm = state.placement.matrix;
+ dest_clips = state.placement.clips.clone();
+ }
+ if i >= max_scan {
+ break;
+ }
+ state.apply(op)?;
+ tracker.step(op, &ext_gstates, doc, state.placement.matrix)?;
+ if i == rem_end.saturating_sub(1) {
+ ctm_after_removal = state.placement.matrix;
+ }
+ }
+ if scan_target >= ops.len() {
+ dest_effective_state = tracker.current_state.clone();
+ dest_ctm = state.placement.matrix;
+ dest_clips = state.placement.clips.clone();
+ }
+ if dest_ctm.invert().is_none() {
+ return Err(EditError::Invalid("unsupported graphics state: non-invertible CTM".into()));
+ }
+
+ let mut extracted = match extract_scope_state(&ops, scope_start, target, scope_start_ctm)? {
+ Some(ext) => ext,
+ None if scope_start != target => {
+ let target_op = ops.get(target).cloned().ok_or_else(|| EditError::Invalid("the image operation is missing".into()))?;
+ if !marked_unit || has_other_paint {
+ target_span = target_op.span.start..target_op.span.end;
+ }
+ extract_scope_state(&ops, target, target, Matrix(img.draw_matrix))?
+ .ok_or_else(|| EditError::Invalid("unsupported graphics state: unable to cleanly extract image scope state".into()))?
+ }
+ None => {
+ return Err(EditError::Invalid("unsupported graphics state: unable to cleanly extract image scope state".into()));
+ }
+ };
+
+ // The clips the moved unit draws itself: all of the image's when they are rebuilt from
+ // `img.clips`, else those its own scope set after the clips around it.
+ let all_clips = extracted.clip_ops.is_empty();
+ let reemitted = if all_clips {
+ img.clips.as_slice()
+ } else if img.clips.starts_with(&outer_clips) {
+ img.clips.get(outer_clips.len()..).unwrap_or_default()
+ } else {
+ return Err(EditError::Invalid("unsupported graphics state: the image clipping path cannot be cleanly preserved".into()));
+ };
+ // At the destination it is clipped by the clips there and the ones it draws. Moving past a
+ // clip the image isn't under (or out from under one it is) would change what part shows.
+ if !dest_clips.iter().all(|c| img.clips.contains(c)) || !img.clips.iter().all(|c| dest_clips.contains(c) || reemitted.contains(c)) {
+ return Err(EditError::Invalid(
+ "unsupported graphics state: moving the image past a clipping path would change what part of it shows".into(),
+ ));
+ }
+
+ if all_clips && !img.clips.is_empty() {
+ for clip in &img.clips {
+ let ops = clip_path_to_ops(clip);
+ if !ops.is_empty() {
+ extracted.clip_ops.extend(ops);
+ }
+ }
+ }
+
+ let has_inherited_clip = !img.clips.is_empty() && extracted.clip_ops.is_empty();
+ if has_inherited_clip {
+ return Err(EditError::Invalid("unsupported graphics state: inherited clipping path cannot be cleanly preserved".into()));
+ }
+
- let non_gs_ops: Vec<Op> = extracted.state_ops.into_iter().filter(|o| !o.is("gs")).collect();
-
+ let mut reconstructed_state = dest_effective_state.clone();
+ for (op, _) in &source_gs_ops {
+ if let Some(name) = op.name(0)
+ && let Some(dict) = ext_gstates.get(name).map(|v| doc.resolve(v)).and_then(|v| v.as_dict().cloned())
+ {
+ reconstructed_state.merge_dict(&dict, doc, Some(name));
+ }
+ }
+
+ if reconstructed_state.visually_differs(&source_effective_state) {
+ return Err(EditError::Invalid(
+ "unsupported graphics state: moving the image would alter effective opacity, blend mode, or soft mask".into(),
+ ));
+ }
+
- let mut unit_ops = Vec::new();
- let marked_open = if marked_unit && !has_other_paint { ops.get(target_unit.start).cloned() } else { None };
- if let Some(open) = &marked_open {
- unit_ops.push(open.clone());
- }
- unit_ops.push(Op::new("q", vec![]));
- let mut current_ctm = dest_ctm;
-
- for (gs_op, target_ctm) in &source_gs_ops {
- if *target_ctm != current_ctm {
- let Some(current_inv) = current_ctm.invert() else {
- return Err(EditError::Invalid("unsupported graphics state: non-invertible CTM".into()));
- };
- let delta = target_ctm.then(&current_inv);
- if !delta.0.iter().all(|v| v.is_finite()) {
- return Err(EditError::Invalid("unsupported graphics state: invalid transform coordinates".into()));
- }
- if delta != Matrix::IDENTITY {
- unit_ops.push(Op::new("cm", delta.0.map(pdfcraft_content::num).to_vec()));
- current_ctm = *target_ctm;
- }
- }
- unit_ops.push(gs_op.clone());
- }
-
- unit_ops.extend(non_gs_ops);
-
- if !extracted.clip_ops.is_empty() {
- if current_ctm != Matrix::IDENTITY {
- let Some(current_inv) = current_ctm.invert() else {
- return Err(EditError::Invalid("unsupported graphics state: non-invertible CTM".into()));
- };
- unit_ops.push(Op::new("cm", current_inv.0.map(pdfcraft_content::num).to_vec()));
- }
- unit_ops.extend(extracted.clip_ops);
- if Matrix(img.draw_matrix) != Matrix::IDENTITY {
- unit_ops.push(Op::new("cm", img.draw_matrix.map(pdfcraft_content::num).to_vec()));
- }
- } else {
- if Matrix(img.draw_matrix) != current_ctm {
- let Some(current_inv) = current_ctm.invert() else {
- return Err(EditError::Invalid("unsupported graphics state: non-invertible CTM".into()));
- };
- let delta = Matrix(img.draw_matrix).then(&current_inv);
- if !delta.0.iter().all(|v| v.is_finite()) {
- return Err(EditError::Invalid("unsupported graphics state: invalid transform coordinates".into()));
- }
- if delta != Matrix::IDENTITY {
- unit_ops.push(Op::new("cm", delta.0.map(pdfcraft_content::num).to_vec()));
- }
- }
- }
- unit_ops.push(extracted.do_op);
- unit_ops.push(Op::new("Q", vec![]));
- // An unbalanced sequence had no EMC; it doesn't gain one.
- if marked_open.is_some() && target_unit.end.checked_sub(1).and_then(|i| ops.get(i)).is_some_and(|o| o.is("EMC")) {
- unit_ops.push(Op::new("EMC", vec![]));
- }
++ let marked_open = if marked_unit && !has_other_paint { ops.get(target_unit.start) } else { None };
++ let has_closing_emc = marked_open.is_some() && target_unit.end.checked_sub(1).and_then(|i| ops.get(i)).is_some_and(|o| o.is("EMC"));
++ let unit_ops = build_relocated_unit_ops(
++ marked_open,
++ &source_gs_ops,
++ extracted,
++ dest_ctm,
++ img.draw_matrix,
++ has_closing_emc,
++ )?;
+
+ let mut serialized = Vec::new();
+ serialized.push(b'\n');
+ serialized.extend_from_slice(&serialize_ops(&unit_ops));
+ serialized.push(b'\n');
+
+ let leaking = if let Some(inv) = ctm_before_removal.invert() { ctm_after_removal.then(&inv) } else { Matrix::IDENTITY };
+ if leaking != Matrix::IDENTITY && has_leaking_clip {
+ return Err(EditError::Invalid("unsupported graphics state: moving the image would alter clipping path transform".into()));
+ }
+ let mut leak_ops = leaking_ops;
+ if leaking != Matrix::IDENTITY && leak_ops.iter().all(|o| !o.is("cm")) {
+ leak_ops.insert(0, Op::new("cm", leaking.0.map(pdfcraft_content::num).to_vec()));
+ }
+ let removal_replacement = if !leak_ops.is_empty() {
+ let mut s = Vec::new();
+ s.push(b'\n');
+ s.extend_from_slice(&serialize_ops(&leak_ops));
+ s.push(b'\n');
+ s
+ } else {
+ Vec::new()
+ };
+
+ let mut edits = Vec::new();
+ if dest_pos >= target_span.end {
+ edits.push((target_span, removal_replacement));
+ edits.push((dest_pos..dest_pos, serialized));
+ } else if dest_pos <= target_span.start {
+ edits.push((dest_pos..dest_pos, serialized));
+ edits.push((target_span, removal_replacement));
+ } else {
+ return Err(EditError::Invalid("cannot arrange image into overlapping content range".into()));
+ }
+
+ let changed = pieces.splice(edits);
+ let contents = crate::text::rewritten_contents(doc, &all, changed);
+ doc.update_dict(p.obj, |d| d.set(b"Contents".to_vec(), contents))?;
+ Ok(())
+}
+
+/// Locate the innermost graphics state scope enclosing only `target` (with no other painting operations).
+/// Returns `Some((start, end, scope_matrix))` where `start` is the index of `q`, `end` is the index of `Q`,
+/// and `scope_matrix` is the CTM in effect immediately before entering that `q`.
+fn image_exclusive_scope(ops: &[Op], target: usize) -> Result<Option<(usize, usize, Matrix)>, EditError> {
+ struct Scope {
+ start: usize,
+ matrix: Matrix,
+ inherited_clip: bool,
+ inherited_path: bool,
+ selected: bool,
+ other_paint: bool,
+ }
+ let mut stack: Vec<Scope> = Vec::new();
+ let mut matrix = Matrix::IDENTITY;
+ let mut clipped = false;
+ let mut path = false;
+ let mut pending_clip = false;
+ let mut text = false;
+ let mut at_image = None;
+ let mut isolated = None;
+ for (index, op) in ops.iter().enumerate() {
+ match op.op.as_slice() {
+ b"q" => {
+ if stack.len() >= 256 {
+ return Err(EditError::Invalid("the image graphics state is too deeply nested".into()));
+ }
+ stack.push(Scope { start: index, matrix, inherited_clip: clipped, inherited_path: path, selected: false, other_paint: false });
+ }
+ b"Q" => {
+ if let Some(scope) = stack.pop() {
+ if scope.selected && !scope.other_paint && !scope.inherited_clip && !scope.inherited_path {
+ isolated.get_or_insert((scope.start, index, scope.matrix));
+ }
+ matrix = scope.matrix;
+ clipped = scope.inherited_clip;
+ path = scope.inherited_path;
+ pending_clip = false;
+ }
+ }
+ b"cm" => {
+ if let Some(m) = op.nums::<6>() {
+ matrix = Matrix(m).then(&matrix);
+ }
+ }
+ b"BT" => text = true,
+ b"ET" => text = false,
+ b"m" | b"l" | b"c" | b"v" | b"y" | b"re" | b"h" => path = true,
+ b"W" | b"W*" => pending_clip = true,
+ _ => {}
+ }
+ if index == target {
+ if text {
+ return Err(EditError::Invalid("an image inside a text object cannot be cropped".into()));
+ }
+ at_image = Some(matrix);
+ for scope in &mut stack {
+ scope.selected = true;
+ }
+ } else if matches!(
+ op.op.as_slice(),
+ b"Do" | b"Tj" | b"TJ" | b"'" | b"\"" | b"sh" | b"BI" | b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*"
+ ) {
+ for scope in &mut stack {
+ scope.other_paint = true;
+ }
+ }
+ if matches!(op.op.as_slice(), b"n" | b"S" | b"s" | b"f" | b"F" | b"f*" | b"B" | b"B*" | b"b" | b"b*") {
+ clipped |= pending_clip;
+ pending_clip = false;
+ path = false;
+ }
+ }
+ if at_image.is_none() {
+ return Err(EditError::Invalid("the image operation is missing".into()));
+ }
+ Ok(isolated)
+}
+
+/// The scope `start..=end` without its private clipping paths and clipping `W n` sequences.
+fn extract_image_drawing_ops(ops: &[Op], start: usize, end: usize) -> Result<(Vec<Op>, bool), EditError> {
+ let is_path = |o: &Op| matches!(o.op.as_slice(), b"m" | b"l" | b"c" | b"v" | b"y" | b"h" | b"re");
+ let mut drawing = Vec::new();
+ let mut removed_clip = false;
+ let mut i = start.saturating_add(1);
+ while i < end {
+ let Some(op) = ops.get(i) else { break };
+ if is_path(op) {
+ let mut k = i;
+ while ops.get(k).is_some_and(is_path) {
+ k = k.saturating_add(1);
+ }
+ if ops.get(k).is_some_and(|o| o.is("W") || o.is("W*")) && ops.get(k.saturating_add(1)).is_some_and(|o| o.is("n")) {
+ removed_clip = true;
+ i = k.saturating_add(2);
+ continue;
+ }
+ }
+ drawing.push(op.clone());
+ i = i.saturating_add(1);
+ }
+ Ok((drawing, removed_clip))
+}
+
+/// Remove any private clipping path / crop on image `index` on `page`.
+pub fn remove_image_crop(doc: &mut Document, page: usize, index: usize) -> Result<(), EditError> {
+ let images = page_images(doc, page)?;
+ let img = images.get(index).cloned().ok_or_else(|| EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1)))?;
+ let p = page_of(doc, page)?;
+ let all = content_streams(doc, &p.dict)?;
+ let pieces = pdfcraft_content::Pieces::join(&all.iter().map(|(_, data)| data.as_slice()).collect::<Vec<_>>());
+ let ops = pieces.parse().ops;
+ let target = img.op;
+ if !ops.get(target).is_some_and(|op| op.is("Do")) {
+ return Err(EditError::Invalid("the image operation could not be located".into()));
+ }
+ let Some((start, end, _)) = image_exclusive_scope(&ops, target)? else {
+ return Err(EditError::Invalid("the image has no crop to remove".into()));
+ };
+ let (drawing_ops, removed_clip) = extract_image_drawing_ops(&ops, start, end)?;
+ if !removed_clip {
+ return Err(EditError::Invalid("the image has no crop to remove".into()));
+ }
+ let start_op = ops.get(start).ok_or_else(|| EditError::Invalid("the image operation is missing".into()))?;
+ let end_op = ops.get(end).ok_or_else(|| EditError::Invalid("the image operation is missing".into()))?;
+ let range = start_op.span.start..end_op.span.end;
+ let mut unit_ops = Vec::new();
+ unit_ops.push(Op::new("q", vec![]));
+ unit_ops.extend(drawing_ops);
+ unit_ops.push(Op::new("Q", vec![]));
+ let serialized = serialize_ops(&unit_ops);
+ let changed = pieces.splice([(range, serialized)]);
+ let contents = crate::text::rewritten_contents(doc, &all, changed);
+ doc.update_dict(p.obj, |d| d.set(b"Contents".to_vec(), contents))?;
+ Ok(())
+}
+
+/// Crop image `index` on `page` to `crop_rect` in user space `[x0, y0, x1, y1]`.
+pub fn crop_image(doc: &mut Document, page: usize, index: usize, crop_rect: [f64; 4]) -> Result<(), EditError> {
+ change_image(doc, page, index, &ImageChange::Crop(crop_rect))
+}
+
/// Change image `index` (from [`page_images`]) on `page`.
pub fn change_image(doc: &mut Document, page: usize, index: usize, change: &ImageChange) -> Result<(), EditError> {
let images = page_images(doc, page)?;
diff --cc crates/edit/src/tests.rs
index aaf5a78e5,d45e983a2..3e0980028
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@@ -8018,50 -7758,331 +8734,375 @@@ fn a_flatten_refused_on_a_later_page_ch
assert_eq!((*doc.get(first)).clone(), before, "the first page keeps its comment and content");
}
+#[test]
+fn arrange_preserves_clip_leaked_out_of_marked_unit() {
+ let content = "/Figure <</MCID 0>> BDC 0 0 300 300 re W n q 10 0 0 10 50 50 cm /Im0 Do Q EMC q 10 0 0 10 400 400 cm /Im0 Do Q";
+ let mut doc = image_page_with(content);
+ let initial = images::page_images(&doc, 0).unwrap();
+ assert_eq!(initial.len(), 2);
+ assert!(initial[0].visible_rect.is_some());
+ assert!(initial[1].visible_rect.is_none(), "second image is fully clipped initially");
+
+ images::arrange_image(&mut doc, 0, 0, images::ArrangeOrder::BringToFront).unwrap();
+ let doc = reopen(&doc);
+ let after = images::page_images(&doc, 0).unwrap();
+ assert_eq!(after.len(), 2);
+ let img1 = after.iter().find(|img| (img.rect[0] - 400.0).abs() < 1e-3).expect("second image exists");
+ assert!(img1.visible_rect.is_none(), "second image must stay clipped after BringToFront");
+
+ // Conflicting leaking transform with leaking clip must be refused
+ let content_cm_clip =
+ "/Figure <</MCID 0>> BDC 2 0 0 2 0 0 cm 0 0 300 300 re W n q 10 0 0 10 50 50 cm /Im0 Do Q EMC q 10 0 0 10 400 400 cm /Im0 Do Q";
+ let mut doc3 = image_page_with(content_cm_clip);
+ let res3 = images::arrange_image(&mut doc3, 0, 0, images::ArrangeOrder::BringToFront);
+ assert!(res3.is_err(), "moving marked unit with both leaking cm and leaking clip must be refused");
+}
+
+#[test]
+fn arrange_preserves_leaked_color_and_text_state() {
+ let content = "/Figure <</MCID 0>> BDC /DeviceRGB cs 1 0 0 sc /F1 14 Tf 2 Tc q 10 0 0 10 50 50 cm /Im0 Do Q EMC 0 0 10 10 re f q 10 0 0 10 200 200 cm /Im0 Do Q";
+ let mut doc = image_page_with(content);
+ images::arrange_image(&mut doc, 0, 0, images::ArrangeOrder::BringToFront).unwrap();
+ let doc = reopen(&doc);
+ let bytes = page_content_bytes(&doc, 0);
+ let s = String::from_utf8_lossy(&bytes);
+ assert!(s.contains("1 0 0 sc"), "sc must be preserved in content stream: {s}");
+ assert!(s.contains("/F1 14 Tf"), "Tf must be preserved in content stream: {s}");
+ assert!(s.contains("2 Tc"), "Tc must be preserved in content stream: {s}");
+
+ // Pixel test: 0 0 10 10 re f still fills red [255, 0, 0] after arrange
+ let pdf_bytes = write_incremental(&doc, &SaveOptions::default()).unwrap();
+ let mut renderer = pdfcraft_render::PageRenderer::new(Arc::new(pdf_bytes), pdfcraft_render::RenderConfig::default());
+ let page =
+ renderer.render(pdfcraft_render::RenderRequest { page: 0, kind: pdfcraft_render::RequestKind::Pixels, scale: 1.0, ..Default::default() });
+ let idx = (((page.height - 5) * page.width + 5) * 4) as usize;
+ assert_eq!(page.rgba[idx..idx + 3], [255, 0, 0], "point (5, 5 from bottom) inside 10x10 rect fills red");
+}
+ /// Existing-row alignment is explicitly refused without mutating the paragraph.
+ #[test]
+ fn one_row_of_a_paragraph_takes_its_own_alignment_is_refused_without_changing_the_document() {
+ for align in [added::Align::Left, added::Align::Center, added::Align::Right, added::Align::Justify] {
+ let mut doc = text_page("BT /F1 10 Tf 12 TL 72 700 Td (First row) Tj T* (Second row) Tj ET");
+ let before = page_content_bytes(&doc, 0);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let style = text::BlockStyle { aligns: vec![None, Some(align)], ..Default::default() };
+ let error = text::rewrite_block(&mut doc, 0, 0, Some(&blocks[0].editor_text), &style).unwrap_err();
+ assert!(error.to_string().contains("selected-row alignment"));
+ assert_eq!(page_content_bytes(&doc, 0), before);
+ assert_eq!(page_content_bytes(&reopen(&doc), 0), before);
+ }
+ }
+
+ /// Refusal preserves all existing rows and their saved content stream.
+ #[test]
+ fn rows_without_their_own_alignment_keep_their_operators_is_refused_without_changing_the_document() {
+ for align in [added::Align::Left, added::Align::Center, added::Align::Right, added::Align::Justify] {
+ let mut doc = text_page("BT /F1 10 Tf 12 TL 72 700 Td (First row) Tj T* (Second row) Tj ET");
+ let before = page_content_bytes(&doc, 0);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let style = text::BlockStyle { aligns: vec![None, Some(align)], ..Default::default() };
+ let error = text::rewrite_block(&mut doc, 0, 0, Some(&blocks[0].editor_text), &style).unwrap_err();
+ assert!(error.to_string().contains("selected-row alignment"));
+ assert_eq!(page_content_bytes(&doc, 0), before);
+ assert_eq!(page_content_bytes(&reopen(&doc), 0), before);
+ }
+ }
+
+ /// Mixed-style rows also refuse unsupported per-row intent before writing.
+ #[test]
+ fn one_row_of_a_styled_region_takes_its_own_alignment_is_refused_without_changing_the_document() {
+ for align in [added::Align::Left, added::Align::Center, added::Align::Right, added::Align::Justify] {
+ let mut doc = mixed_region_page();
+ let before = page_content_bytes(&doc, 0);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let style = text::BlockStyle { aligns: vec![None, Some(align)], ..Default::default() };
+ let error = text::rewrite_block(&mut doc, 0, 1, Some(&blocks[1].editor_text), &style).unwrap_err();
+ assert!(error.to_string().contains("selected-row alignment"));
+ assert_eq!(page_content_bytes(&doc, 0), before);
+ assert_eq!(page_content_bytes(&reopen(&doc), 0), before);
+ }
+ }
+
+ /// Added text: each paragraph is laid out with its own alignment, a justified paragraph's last
+ /// line isn't stretched, and the alignments survive saving and reopening.
+ #[test]
+ fn added_text_paragraphs_keep_their_own_alignments() {
+ let mut doc = fixture();
+ let text = AddedText {
+ rect: [72.0, 600.0, 372.0, 700.0],
+ text: "Left line\nRight line\nCentred line".into(),
+ size: 12.0,
+ aligns: vec![None, Some(Align::Right), Some(Align::Center)],
+ ..AddedText::default()
+ };
+ assert_eq!(text.paragraph_align(1), Align::Right);
+ add_content(&mut doc, 0, &Content::Text(text.clone())).unwrap();
+ let doc = reopen(&doc);
+ let Content::Text(t) = &list_added(&doc)[0].content else { panic!() };
+ assert_eq!(t.aligns, text.aligns, "saved with the item");
+ let page = streams(&doc, 0).join("\n");
+ let x_of = |s: &str| -> f64 {
+ let at = page.find(&format!("({s}) Tj")).unwrap();
+ let tm = page[..at].rfind(" Tm").unwrap();
+ let nums: Vec<f64> = page[..tm].rsplit("1 0 0 1 ").next().unwrap().split_whitespace().filter_map(|v| v.parse().ok()).collect();
+ nums[0]
+ };
+ let w = |s: &str| Family::Helvetica.width(s, 12.0, false, false);
+ assert!((x_of("Left line") - 72.0).abs() < 0.01);
+ assert!((x_of("Right line") - (372.0 - w("Right line"))).abs() < 0.01);
+ assert!((x_of("Centred line") - (72.0 + (300.0 - w("Centred line")) / 2.0)).abs() < 0.01);
+ // A paragraph's own alignment the same as the box's is no entry at all.
+ let tidy = paragraph_align::tidy(vec![Some(Align::Left), Some(Align::Right)], Align::Left);
+ assert_eq!(tidy, [None, Some(Align::Right)]);
+ }
+
+ /// With character spacing, a paragraph aligned on its own is placed by its spaced width: a
+ /// right-aligned line still ends at the box's right edge, and a centred one sits in the middle.
+ #[test]
+ fn added_text_paragraphs_align_by_their_spaced_width() {
+ let mut doc = fixture();
+ let text = AddedText {
+ rect: [72.0, 600.0, 372.0, 700.0],
+ text: "Left line\nRight line\nCentred line".into(),
+ size: 12.0,
+ aligns: vec![None, Some(Align::Right), Some(Align::Center)],
+ char_spacing: 2.0,
+ ..AddedText::default()
+ };
+ add_content(&mut doc, 0, &Content::Text(text)).unwrap();
+ let page = streams(&reopen(&doc), 0).join("\n");
+ assert!(page.contains("2 Tc"), "{page}");
+ let x_of = |s: &str| -> f64 {
+ let at = page.find(&format!("({s}) Tj")).unwrap();
+ let tm = page[..at].rfind(" Tm").unwrap();
+ let nums: Vec<f64> = page[..tm].rsplit("1 0 0 1 ").next().unwrap().split_whitespace().filter_map(|v| v.parse().ok()).collect();
+ nums[0]
+ };
+ let w = |s: &str| Family::Helvetica.width(s, 12.0, false, false) + 2.0 * s.chars().count() as f64;
+ assert!((x_of("Left line") - 72.0).abs() < 0.01);
+ assert!((x_of("Right line") - (372.0 - w("Right line"))).abs() < 0.01);
+ assert!((x_of("Centred line") - (72.0 + (300.0 - w("Centred line")) / 2.0)).abs() < 0.01);
+ }
+
+ /// A one-row title over a body column: an alignment picked for it is measured across the column.
+ /// Left puts it on the column's left edge, and centring it again leaves it where it is, rather
+ /// than aligning it in its own box (a no-op) or toward the page's margin (moving it right each
+ /// time). It is aligned as the editor aligns a selected row, in whichever paragraph holds it.
+ #[test]
+ fn a_lone_row_aligns_across_the_column_it_sits_in() {
+ let mut doc = text_page(
+ "BT /F1 24 Tf 1 0 0 1 250 700 Tm (Title) Tj ET BT /F1 10 Tf 12 TL 72 650 Td (The body of the page runs across the whole column here.) Tj T* (It has a second row that is a little shorter.) Tj ET",
+ );
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ assert_eq!(blocks[0].text, "Title");
+ let column = [blocks[1].rect[0], blocks[1].rect[2]];
+ let title = |doc: &Document| text::text_lines(doc, 0).unwrap().into_iter().find(|l| l.text == "Title").unwrap().rect;
+ let align = |doc: &mut Document, a: added::Align| {
+ let lines = text::text_lines(doc, 0).unwrap();
+ let line = lines.iter().position(|l| l.text == "Title").unwrap();
+ let blocks = text::text_blocks(doc, 0).unwrap();
+ let (index, block) = blocks.iter().enumerate().find(|(_, b)| b.lines.contains(&line)).unwrap();
+ // The title is the paragraph's first row.
+ let style = text::BlockStyle { align: Some(a), ..Default::default() };
+ text::rewrite_block(doc, 0, index, Some(&block.editor_text), &style).unwrap();
+ };
+ align(&mut doc, added::Align::Center);
+ let centred = title(&doc);
+ assert!(((centred[0] + centred[2]) / 2.0 - (column[0] + column[1]) / 2.0).abs() < 1.0, "centred in the column: {centred:?} vs {column:?}");
+ align(&mut doc, added::Align::Center);
+ assert!((title(&doc)[0] - centred[0]).abs() < 0.5, "centring again leaves it where it is: {:?} vs {centred:?}", title(&doc));
+ align(&mut doc, added::Align::Left);
+ assert!((title(&doc)[0] - column[0]).abs() < 0.5, "left: back on the column's edge, {:?} vs {column:?}", title(&doc));
+ align(&mut doc, added::Align::Right);
+ assert!((title(&doc)[2] - column[1]).abs() < 1.0, "right: on the column's right edge, {:?} vs {column:?}", title(&doc));
+ }
+
+ /// An edited paragraph's text has a line break between its source rows (`editor_text`), as the
+ /// paragraph editor sends it. Those are the rows of one paragraph, not paragraph ends: justified
+ /// in a font of its own (the single-style rewrite), every row but the last still fills the width.
+ #[test]
+ fn a_justified_paragraph_from_the_editor_fills_its_rows() {
+ let mut doc =
+ text_page("BT /F1 10 Tf 12 TL 100 700 Td (One two three four five six seven) Tj T* (eight nine ten eleven twelve) Tj T* (end) Tj ET");
+ let block = text::text_blocks(&doc, 0).unwrap()[0].clone();
+ assert_eq!(block.editor_text.matches('\n').count(), 2);
+ let style = text::BlockStyle { align: Some(added::Align::Justify), family: Some((Family::Helvetica, true, false)), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, 0, Some(&block.editor_text), &style).unwrap();
+ let lines = text::text_lines(&doc, 0).unwrap();
+ let row = |t: &str| lines.iter().find(|l| l.text.starts_with(t)).unwrap().rect;
+ let right = row("One")[2];
+ assert!((row("eight")[2] - right).abs() < 1.0, "the second row is stretched: {:?} vs {right}", row("eight"));
+ assert!(row("end")[2] < right - 20.0, "the last row isn't");
+ }
+
+ /// An alignment for a row the draft doesn't have names no row: a one-row title given one for a
+ /// second row only stays where it is, not moved to its column's edge. Nor does a row of the draft
+ /// with no alignment of its own move because another row has one (CodeRabbit on 648dd428).
+ #[test]
+ fn a_title_moves_to_its_column_only_for_an_alignment_of_its_own() {
+ let page = "BT /F1 24 Tf 1 0 0 1 250 700 Tm (Title) Tj ET BT /F1 10 Tf 12 TL 72 650 Td (The body of the page runs across the whole column here.) Tj T* (It has a second row that is a little shorter.) Tj ET";
+ let title = |doc: &Document| text::text_lines(doc, 0).unwrap().into_iter().find(|l| l.text == "Title").unwrap().rect;
+ let before = title(&text_page(page));
+ for draft in ["Title", "Title\nSub"] {
+ let mut doc = text_page(page);
+ let style = text::BlockStyle { aligns: vec![None, Some(added::Align::Right)], ..Default::default() };
+ assert!(text::rewrite_block(&mut doc, 0, 0, Some(draft), &style).is_err());
+ assert!((title(&doc)[0] - before[0]).abs() < 0.5, "{draft:?}: the title stays put, {:?} vs {before:?}", title(&doc));
+ }
+ }
+
+ /// A column whose rows are several runs (a second style on the same baseline) reaches the right
+ /// edge of its whole rows, not of the run at the left edge: a title wider than that first run is
+ /// still held by the column and aligns to it (CodeRabbit on b9940d7d).
+ #[test]
+ fn a_column_is_measured_by_its_whole_rows_not_its_first_runs() {
+ let page = "BT /F1 18 Tf 1 0 0 1 100 672 Tm (A longer title) Tj ET BT /F1 10 Tf 12 TL 50 610 Td (The first column runs to here.) Tj ET BT /F1 11 Tf 1 0 0 rg 1 0 0 1 185 610 Tm (and keeps going on.) Tj ET BT /F1 10 Tf 12 TL 50 598 Td (A second row of it.) Tj ET";
+ let blocks = text::text_blocks(&text_page(page), 0).unwrap();
+ let body = blocks.iter().find(|b| b.text.starts_with("The first")).expect("the body is a block");
+ assert!(body.rows.first().is_some_and(|row| row.len() >= 2), "the body's first row is several runs: {:?}", body.rows);
+ let lines = text::text_lines(&text_page(page), 0).unwrap();
+ let column_right =
+ body.rows.iter().filter_map(|row| row.iter().filter_map(|i| lines.get(*i)).map(|l| l.rect[2]).reduce(f64::max)).fold(f64::MIN, f64::max);
+ let mut doc = text_page(page);
+ let index = blocks.iter().position(|b| b.text == "A longer title").expect("the title is a block of its own");
+ let style = text::BlockStyle { align: Some(added::Align::Right), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, index, Some("A longer title"), &style).unwrap();
+ let title = text::text_lines(&doc, 0).unwrap().into_iter().find(|l| l.text == "A longer title").unwrap().rect;
+ assert!((title[2] - column_right).abs() < 1.0, "right: on the whole row's right edge, {title:?} vs {column_right}");
+ }
+
+ /// A row with no column of its own is aligned in its own box, but that box is not the width its new
+ /// text wraps at: changing the text together with an alignment keeps a single row on one row, as
+ /// it does without the alignment (CodeRabbit on b9940d7d).
+ #[test]
+ fn an_isolated_row_edited_with_an_alignment_does_not_wrap_at_its_old_width() {
+ for align in [added::Align::Center, added::Align::Right] {
+ let mut doc = text_page("BT /F1 18 Tf 1 0 0 1 100 700 Tm (Title) Tj ET");
+ let longer = "A much longer title for the page";
+ let style = text::BlockStyle { align: Some(align), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, 0, Some(longer), &style).unwrap();
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert_eq!(lines.iter().map(|l| l.text.as_str()).collect::<Vec<_>>(), [longer], "{align:?}: the title stays on one row");
+ }
+ }
+
+ /// A heading across the whole page sits closer to the title than the title's own column does: the
+ /// title is still aligned in its own column, not across the heading's page-wide frame (FredApps's
+ /// review of 43f848d5). Centre and right both go by the column.
+ #[test]
+ fn a_lone_row_aligns_in_its_own_column_not_a_closer_full_width_heading() {
+ let page = "BT /F1 12 Tf 1 0 0 1 50 700 Tm (A heading that runs across both columns of the page, well past the first column, to the far margin.) Tj ET BT /F1 18 Tf 1 0 0 1 120 672 Tm (Title) Tj ET BT /F1 10 Tf 12 TL 50 610 Td (The first column runs to here.) Tj T* (A second row of it.) Tj ET BT /F1 10 Tf 12 TL 320 610 Td (The second column has more rows.) Tj T* (One.) Tj T* (Two.) Tj T* (Three.) Tj ET";
+ let blocks = text::text_blocks(&text_page(page), 0).unwrap();
+ let column = blocks.iter().find(|b| b.text.starts_with("The first")).unwrap().rect;
+ let heading = blocks.iter().find(|b| b.text.starts_with("A heading")).unwrap().rect;
+ let title_block = blocks.iter().find(|b| b.text == "Title").expect("the title is a block of its own").rect;
+ let gap = |other: [f64; 4]| (other[1] - title_block[3]).max(title_block[1] - other[3]).max(0.0);
+ assert!(gap(heading) < gap(column), "the heading is the nearer block: {} vs {}", gap(heading), gap(column));
+ assert!(heading[2] - heading[0] > 2.0 * (column[2] - column[0]), "the heading is page-wide: {heading:?} vs {column:?}");
+ let title_after = |align: added::Align| {
+ let mut doc = text_page(page);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let index = blocks.iter().position(|b| b.text == "Title").unwrap();
+ let style = text::BlockStyle { align: Some(align), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, index, Some("Title"), &style).unwrap();
+ text::text_lines(&doc, 0).unwrap().into_iter().find(|l| l.text == "Title").unwrap().rect
+ };
+ let centred = title_after(added::Align::Center);
+ assert!(((centred[0] + centred[2]) / 2.0 - (column[0] + column[2]) / 2.0).abs() < 1.0, "centred in its own column: {centred:?} vs {column:?}");
+ let right = title_after(added::Align::Right);
+ assert!((right[2] - column[2]).abs() < 1.0, "right: on its own column's right edge, {right:?} vs {column:?}");
+ }
+
+ /// The same on a page of two columns, where the other column has more rows: a title over the
+ /// first column is aligned across the first column, not its own box.
+ #[test]
+ fn a_lone_row_aligns_across_its_own_column_of_two() {
+ let mut doc = text_page(
+ "BT /F1 18 Tf 1 0 0 1 120 700 Tm (Title) Tj ET \
+ BT /F1 10 Tf 12 TL 50 660 Td (The first column runs to here.) Tj T* (A second row of it.) Tj ET \
+ BT /F1 10 Tf 12 TL 320 660 Td (The second column has more rows.) Tj T* (One.) Tj T* (Two.) Tj T* (Three.) Tj T* (Four.) Tj ET",
+ );
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let column = blocks.iter().find(|b| b.text.starts_with("The first")).unwrap().rect;
+ let index = blocks.iter().position(|b| b.text == "Title").unwrap();
+ let style = text::BlockStyle { align: Some(added::Align::Left), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, index, Some("Title"), &style).unwrap();
+ let title = text::text_lines(&doc, 0).unwrap().into_iter().find(|l| l.text == "Title").unwrap().rect;
+ assert!((title[0] - column[0]).abs() < 0.5, "left: on the first column's edge, {title:?} vs {column:?}");
+ }
+
+ /// A one-row title in two colours is rewritten by the styled-region path, which is given the
+ /// same column frame: centred twice, it stays centred in the column instead of moving right.
+ #[test]
+ fn a_lone_row_of_two_styles_aligns_across_its_column() {
+ let mut doc = text_page(
+ "BT /F1 18 Tf 1 0 0 1 250 700 Tm (Big ) Tj 1 0 0 rg (Title) Tj ET \
+ BT 0 g /F1 10 Tf 12 TL 72 650 Td (The body of the page runs across the whole column here.) Tj T* (It has a second row that is a little shorter.) Tj ET",
+ );
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ assert_eq!(blocks[0].text, "Big Title");
+ let column = [blocks[1].rect[0], blocks[1].rect[2]];
+ let title = |doc: &Document| {
+ let lines = text::text_lines(doc, 0).unwrap();
+ let block = text::text_blocks(doc, 0).unwrap().into_iter().find(|b| b.text == "Big Title").unwrap();
+ block.lines.iter().map(|i| lines[*i].rect).reduce(|a, b| [a[0].min(b[0]), a[1].min(b[1]), a[2].max(b[2]), a[3].max(b[3])]).unwrap()
+ };
+ let centre = |doc: &mut Document| {
+ let blocks = text::text_blocks(doc, 0).unwrap();
+ let (index, block) = blocks.iter().enumerate().find(|(_, b)| b.text == "Big Title").unwrap();
+ let style = text::BlockStyle { align: Some(added::Align::Center), ..Default::default() };
+ text::rewrite_block(doc, 0, index, Some(&block.editor_text), &style).unwrap();
+ };
+ centre(&mut doc);
+ let centred = title(&doc);
+ assert!(((centred[0] + centred[2]) / 2.0 - (column[0] + column[1]) / 2.0).abs() < 1.0, "centred in the column: {centred:?} vs {column:?}");
+ centre(&mut doc);
+ assert!((title(&doc)[0] - centred[0]).abs() < 0.5, "centring again leaves it where it is: {:?} vs {centred:?}", title(&doc));
+ }
+
+ #[test]
+ fn a_titles_column_ignores_distant_full_width_headers_and_footers() {
+ for (title_x, column_prefix) in [(95, "The first"), (365, "The second")] {
+ for mixed in [false, true] {
+ for title_y in [710, 580] {
+ let title = if mixed { "(Big ) Tj 1 0 0 rg (Title) Tj" } else { "(Title) Tj" };
+ let name = if mixed { "Big Title" } else { "Title" };
+ let mut doc = text_page(&format!(
+ "BT /F1 10 Tf 12 TL 50 780 Td (A page-wide header extending well beyond either body column on this page and continuing all the way to the right margin.) Tj T* (A repeated page-wide heading across both columns and continuing to the right margin of the page.) Tj ET \
+ BT /F1 18 Tf 1 0 0 1 {title_x} {title_y} Tm {title} ET \
+ BT 0 g /F1 10 Tf 12 TL 50 660 Td (The first column runs to here.) Tj T* (A second row of it.) Tj ET \
+ BT /F1 10 Tf 12 TL 320 660 Td (The second column runs to here.) Tj T* (Another row.) Tj ET \
+ BT /F1 10 Tf 50 40 Td (A page-wide footer extending well beyond either body column on this page and continuing all the way to the right margin.) Tj ET"
+ ));
+ let rect = |doc: &Document, prefix: &str| {
+ let blocks = text::text_blocks(doc, 0).unwrap();
+ blocks.iter().find(|b| b.text.starts_with(prefix)).unwrap_or_else(|| panic!("missing {prefix:?} in {blocks:?}")).rect
+ };
+ let column = rect(&doc, column_prefix);
+ let header = rect(&doc, "A page-wide header");
+ let footer = rect(&doc, "A page-wide footer");
+ assert!(header[2] > column[2] + 20.0 && footer[2] > column[2] + 20.0);
+ for align in [added::Align::Center, added::Align::Center, added::Align::Right, added::Align::Left] {
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let (index, block) = blocks.iter().enumerate().find(|(_, b)| b.text == name).unwrap();
+ let style = text::BlockStyle { align: Some(align), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, index, Some(&block.editor_text), &style).unwrap();
+ doc = reopen(&doc);
+ let title = rect(&doc, name);
+ let error = match align {
+ added::Align::Center => ((title[0] + title[2]) - (column[0] + column[2])).abs() / 2.0,
+ added::Align::Right => (title[2] - column[2]).abs(),
+ _ => (title[0] - column[0]).abs(),
+ };
+ assert!(error < 1.0, "{align:?}, mixed={mixed}, title_y={title_y}: {title:?} must use {column:?}");
+ assert_eq!(rect(&doc, "A page-wide header"), header);
+ assert_eq!(rect(&doc, "A page-wide footer"), footer);
+ assert_eq!(rect(&doc, column_prefix), column);
+ }
+ }
+ }
+ }
+ }
/// How each glyph-showing operator on page 0 is painted, read the way a renderer reads the page:
/// its text, `Tr`, stroke colour operators, line width (from `w` or an ExtGState's `/LW`) and
@@@ -8479,127 -8500,79 +9520,204 @@@ fn a_run_of_spaces_in_a_subset_without_
assert_eq!(content.matches("[-330] TJ").count(), 5, "{content}");
}
+#[test]
+fn image_exclusive_scope_resets_path_on_scope_exit() {
+ // An earlier scope has a path operation closed by Q (with 'h' closepath).
+ // The subsequent image in its own scope must cleanly extract and allow crop removal.
+ let content = "q 0 0 m 10 10 l h Q q 100 100 200 100 re W n 200 0 0 100 100 100 cm /Im0 Do Q";
+ let mut doc = image_page_with(content);
+ let before = images::page_images(&doc, 0).unwrap();
+ assert_eq!(before.len(), 1);
+ images::remove_image_crop(&mut doc, 0, 0).unwrap();
+}
+
+#[test]
+fn content_units_does_not_swallow_subsequent_units_on_standalone_gs() {
+ // /GS0 gs followed by Q and subsequent images must isolate each Do unit properly
+ let content = "q /GS0 gs Q q 200 0 0 100 100 100 cm /Im0 Do Q q 10 0 0 10 300 300 cm /Im0 Do Q";
+ let pieces = pdfcraft_content::Pieces::join(&[content.as_bytes()]);
+ let ops = pieces.parse().ops;
+ let units = images::content_units(&ops, 0, ops.len());
+ assert_eq!(units.len(), 2);
+}
+
+#[test]
+fn different_inline_soft_masks_have_distinct_identities() {
+ let doc = Document::new_empty();
+ let mut dict1 = Dict::default();
+ let mut mask1 = Dict::default();
+ mask1.set(b"Type".to_vec(), Object::Name(b"Mask1".to_vec()));
+ dict1.set(b"SMask".to_vec(), Object::Dict(mask1));
+
+ let mut dict2 = Dict::default();
+ let mut mask2 = Dict::default();
+ mask2.set(b"Type".to_vec(), Object::Name(b"Mask2".to_vec()));
+ dict2.set(b"SMask".to_vec(), Object::Dict(mask2));
+
+ let mut state1 = images::EffectiveGState::default();
+ state1.merge_dict(&dict1, &doc, None);
+
+ let mut state2 = images::EffectiveGState::default();
+ state2.merge_dict(&dict2, &doc, None);
+
+ assert!(state1.visually_differs(&state2), "two different inline soft masks must visually differ");
+}
+
+#[test]
+fn cid_to_gid_map_odd_length_is_trimmed_to_even() {
+ let mut m_bytes = vec![0u8, 1u8, 0u8, 2u8, 0u8];
+ let needed_len = 5;
+ if m_bytes.len() > needed_len {
+ m_bytes.truncate(needed_len);
+ }
+ if m_bytes.len() % 2 != 0 {
+ m_bytes.pop();
+ }
+ assert_eq!(m_bytes.len(), 4);
+ assert_eq!(m_bytes.len() % 2, 0);
+}
+
+#[test]
+fn is_entire_page_wrapped_with_nested_scopes() {
+ let wrapped = "q 1 0 0 1 0 0 cm q 0 0 10 10 re f Q 200 0 0 100 100 100 cm /Im0 Do Q";
+ let pieces = pdfcraft_content::Pieces::join(&[wrapped.as_bytes()]);
+ let ops = pieces.parse().ops;
+ assert!(images::is_entire_page_wrapped(&ops), "page wrapped in outer q...Q with inner nested q...Q is wrapped");
+
+ let unwrapped = "q 1 0 0 1 0 0 cm Q q 200 0 0 100 100 100 cm /Im0 Do Q";
+ let pieces2 = pdfcraft_content::Pieces::join(&[unwrapped.as_bytes()]);
+ let ops2 = pieces2.parse().ops;
+ assert!(!images::is_entire_page_wrapped(&ops2), "page with two separate top-level q...Q scopes is not wrapped");
+}
+
+#[test]
+fn hostile_page_with_many_gs_and_nested_q_does_not_hang_or_oom() {
+ use std::fmt::Write as _;
+ let mut stream = String::new();
+ for i in 0..10_000 {
+ let _ = write!(stream, "/G{i} gs ");
+ }
+ for _ in 0..500 {
+ stream.push_str("q ");
+ }
+ stream.push_str("100 0 0 100 0 0 cm /Im0 Do ");
+ for _ in 0..500 {
+ stream.push_str("Q ");
+ }
+ let doc = image_page_with(&stream);
+ let start = std::time::Instant::now();
+ let imgs = images::page_images(&doc, 0).unwrap();
+ assert_eq!(imgs.len(), 1);
+ assert!(start.elapsed().as_secs() < 5, "page_images on hostile 10k gs must finish in under 5s, took {:?}", start.elapsed());
+}
+
+#[test]
+fn crop_image_with_leading_cm_in_scope_places_clip_before_internal_cm() {
+ let content = "q 10 0 0 10 50 50 cm /Im0 Do Q";
+ let mut doc = image_page_with(content);
+ let before = images::page_images(&doc, 0).unwrap();
+ assert_eq!(before.len(), 1);
+ images::crop_image(&mut doc, 0, 0, [50.0, 50.0, 55.0, 55.0]).unwrap();
+ let doc = reopen(&doc);
+ let after = images::page_images(&doc, 0).unwrap();
+ assert_eq!(after.len(), 1);
+ assert!(after[0].visible_rect.is_some());
+ let (v0, v1, v2, v3) =
+ (after[0].visible_rect.unwrap()[0], after[0].visible_rect.unwrap()[1], after[0].visible_rect.unwrap()[2], after[0].visible_rect.unwrap()[3]);
+ assert!((v0 - 50.0).abs() < 1e-3 && (v2 - 55.0).abs() < 1e-3, "visible rect must match crop coordinates: {:?}", after[0].visible_rect);
+ assert!((v1 - 50.0).abs() < 1e-3 && (v3 - 55.0).abs() < 1e-3, "visible rect must match crop coordinates: {:?}", after[0].visible_rect);
+}
+
+#[test]
+fn remove_crop_preserves_cm_before_extgstate() {
+ let content = "q 2 0 0 2 0 0 cm /GS0 gs 0 0 100 100 re W n 10 0 0 10 50 50 cm /Im0 Do Q";
+ let mut doc = image_page_with(content);
+ let before = images::page_images(&doc, 0).unwrap();
+ assert_eq!(before.len(), 1);
+ images::remove_image_crop(&mut doc, 0, 0).unwrap();
+ let doc = reopen(&doc);
+ let bytes = page_content_bytes(&doc, 0);
+ let s = String::from_utf8_lossy(&bytes);
+ assert!(!s.contains("W n"), "clipping path must be removed: {s}");
+ // Verify that the initial cm appears before /GS0 gs
+ let cm_pos = s.find("2 0 0 2 0 0 cm").expect("cm must be preserved");
+ let gs_pos = s.find("/GS0 gs").expect("gs must be preserved");
+ assert!(cm_pos < gs_pos, "cm must precede /GS0 gs so soft mask space is preserved: {s}");
+}
++
+ /// Inside a text object `q`/`Q` save and restore the text and text line matrices (ISO 32000-2
+ /// §9.4.1, as corrected by the PDF Association's errata, issue 368), as the renderer does: the
+ /// text after `Q` is found, and stays, where it is drawn.
+ #[test]
+ fn q_inside_a_text_object_restores_the_text_matrices() {
+ for (content, text, baseline) in [
+ ("BT /F1 12 Tf 72 700 Td q 0 -100 Td Q (After) Tj ET", "After", [72.0, 700.0]),
+ ("BT /F1 12 Tf 72 700 Td q 1 0 0 1 300 300 Tm Q (After) Tj ET", "After", [72.0, 700.0]),
+ // `T*` moves from the restored line, not the one set inside q/Q.
+ ("BT /F1 12 Tf 14 TL 72 700 Td q 0 -100 Td Q T* (Next) Tj ET", "Next", [72.0, 686.0]),
+ ] {
+ let mut doc = text_page(content);
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert_eq!((lines[0].text.as_str(), lines[0].baseline()), (text, baseline), "{content}");
+ text::replace_line(&mut doc, 0, 0, "Edited").unwrap();
+ let doc = reopen(&doc);
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert_eq!((lines[0].text.as_str(), lines[0].baseline()), ("Edited", baseline), "{content} after editing");
+ }
+ }
+
+ /// Whole-paragraph alignment retains both soft wraps and typed hard breaks after save/reopen.
+ /// Selected-row alignment for existing PDF content is withheld until it can persist safely.
+ #[test]
+ fn wrapped_rows_keep_whole_paragraph_alignment_and_hard_break_markers() {
+ let mut doc = text_page("BT /F1 10 Tf 12 TL 72 700 Td (Old) Tj ET");
+ let style = text::BlockStyle { width: Some(60.0), align: Some(added::Align::Left), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, 0, Some("One two three four five six\nTail"), &style).unwrap();
+ let doc = reopen(&doc);
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert!(lines.len() > 2, "the first logical row must wrap");
+ let tail = lines.iter().find(|line| line.text == "Tail").unwrap();
+ assert!((tail.rect[0] - 72.0).abs() < 1.0, "the second logical row keeps the paragraph alignment: {:?}", tail.rect);
+ for line in lines.iter().filter(|line| line.text != "Tail") {
+ assert!((line.rect[0] - 72.0).abs() < 1.0, "soft-wrapped rows stay left-aligned: {:?}", line.rect);
+ }
+ let content = String::from_utf8_lossy(&page_content_bytes(&doc, 0)).into_owned();
+ assert!(content.contains("/PCSoft") && content.contains("/PCHard"), "both break kinds survive: {content}");
+ }
+
+ #[test]
+ fn hostile_soft_wrap_counts_fail_before_reflow_allocates() {
+ for marker in [
+ "/PCSoft << /N 9223372036854775807 >> BDC EMC",
+ "/PCSoft << /N 16777217 >> BDC EMC",
+ "/PCSoft << /N 8388608 >> BDC EMC /PCSoft << /N 8388609 >> BDC EMC",
+ ] {
+ let doc = text_page(&format!("BT /F1 12 Tf 20 700 Td (First) Tj {marker} 0 -14 Td (Second) Tj ET"));
+ let error = text::text_lines(&doc, 0).unwrap_err();
+ assert!(error.to_string().contains("too large"), "{marker}: {error}");
+ }
+ }
+
+ #[test]
+ fn ordinary_soft_wrap_counts_keep_their_spaces() {
+ for gap in [0, 1, 2, 8] {
+ let doc = text_page(&format!("BT /F1 12 Tf 20 700 Td (First) Tj /PCSoft << /N {gap} >> BDC EMC 0 -14 Td (Second) Tj ET"));
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert_eq!(lines.len(), 2);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ assert_eq!(blocks.len(), 1);
+ assert_eq!(blocks[0].reflow_text, format!("First{}Second", " ".repeat(gap)));
+ }
+ }
+
+ #[test]
+ fn styled_existing_justification_is_refused_instead_of_silently_left_aligning() {
+ let mut doc = mixed_region_page();
+ let before = page_content_bytes(&doc, 0);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ let style = text::BlockStyle { align: Some(added::Align::Justify), ..Default::default() };
+ let error = text::rewrite_block(&mut doc, 0, 1, Some(&blocks[1].editor_text), &style).unwrap_err();
+ assert!(error.to_string().contains("justification"));
+ assert_eq!(page_content_bytes(&doc, 0), before);
+ assert_eq!(page_content_bytes(&reopen(&doc), 0), before);
+ }
diff --cc crates/engine/src/doc_state.rs
index db7e3b950,d28429de6..91b33ad01
--- a/crates/engine/src/doc_state.rs
+++ b/crates/engine/src/doc_state.rs
@@@ -1,4 -1,4 +1,4 @@@
- use super::*;
-use super::{Arc, EditError, Hidden, SignatureInfo, TrustStore, optimize};
++use super::{Arc, ArrangeOrder, EditError, Hidden, SignatureInfo, TrustStore, optimize};
/// A change to an existing page image.
#[derive(Clone, Debug, PartialEq)]
diff --cc crates/engine/src/edit/run.rs
index 300a93845,c6b1a7438..ab38aa089
--- a/crates/engine/src/edit/run.rs
+++ b/crates/engine/src/edit/run.rs
@@@ -234,6 -250,6 +250,194 @@@ impl EditCtx
}
}
++fn run_edit_page_image(
++ doc: &mut pdfcraft_cos::Document,
++ page: usize,
++ index: usize,
++ change: &ImageEdit,
++) -> Result<(), EditError> {
++ match change {
++ ImageEdit::Move(to) if !to.iter().all(|v| v.is_finite()) => {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid position".into())));
++ }
++ ImageEdit::Transform(t) if !t.iter().all(|v| v.is_finite()) => {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("the image transform must be finite".into())));
++ }
++ ImageEdit::Crop(keep) if !keep.iter().all(|v| v.is_finite()) => {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid crop".into())));
++ }
++ _ => {}
++ }
++ let img = pdfcraft_edit::page_images(doc, page)?
++ .into_iter()
++ .nth(index)
++ .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1))))?;
++ let c = match change {
++ ImageEdit::Move(to) => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::rect_to_rect(img.rect, *to)),
++ ImageEdit::Transform(t) => pdfcraft_edit::ImageChange::Transform(*t),
++ ImageEdit::Rotate(q) => {
++ pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(img.visible_rect.unwrap_or(img.rect), *q, false, false))
++ }
++ ImageEdit::RotateBy(degrees) => {
++ let t = pdfcraft_edit::turn_by_degrees(img.visible_rect.unwrap_or(img.rect), *degrees)
++ .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid("the rotation angle must be a finite number".into())))?;
++ pdfcraft_edit::ImageChange::Transform(t)
++ }
++ ImageEdit::Flip { horizontal } => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(
++ img.visible_rect.unwrap_or(img.rect),
++ 0,
++ *horizontal,
++ !*horizontal,
++ )),
++ ImageEdit::Replace { name, bytes } => {
++ if img.is_form {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid(
++ "grouped Form artwork cannot be replaced with a bitmap".into(),
++ )));
++ }
++ pdfcraft_edit::ImageChange::Replace(embed_image(doc, name, bytes)?.0)
++ }
++ ImageEdit::Crop(keep) => pdfcraft_edit::ImageChange::Crop(*keep),
++ ImageEdit::Delete => pdfcraft_edit::ImageChange::Delete,
++ ImageEdit::Arrange(order) => pdfcraft_edit::ImageChange::Arrange(*order),
++ ImageEdit::RemoveCrop => pdfcraft_edit::ImageChange::RemoveCrop,
++ };
++ let page_model = pdfcraft_model::pages(doc)
++ .into_iter()
++ .nth(page)
++ .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} not found", page + 1))))?;
++ let target_stream_obj = match page_model.dict.get(b"Contents") {
++ None => None,
++ Some(c) => match &*doc.resolve(c) {
++ pdfcraft_cos::Object::Array(a) => a.get(img.stream).and_then(|o| o.as_ref()),
++ _ if img.stream == 0 => c.as_ref(),
++ _ => None,
++ },
++ };
++
++ let added_items: Vec<(usize, pdfcraft_edit::Added)> =
++ pdfcraft_edit::list_added(doc).into_iter().filter(|a| a.page == page).enumerate().collect();
++ let matched_added = if let Some(st_ref) = target_stream_obj {
++ added_items.iter().find(|(_, a)| a.obj == st_ref)
++ } else if let Some(target_ref) = img.object {
++ added_items.iter().find(|(_, a)| matches!(&a.content, AddedContent::Image(ai) if ai.image == target_ref))
++ } else {
++ None
++ };
++
++ let mut ai_to_update = None;
++ if let Some((added_idx, added_item)) = matched_added
++ && let AddedContent::Image(ai) = added_item.content.clone()
++ {
++ let has_active_crop = ai.crop.iter().any(|v| v.abs() > 1e-6)
++ || img.visible_rect.is_some_and(|v| {
++ (v[0] - img.rect[0]).abs() > 1e-4
++ || (v[1] - img.rect[1]).abs() > 1e-4
++ || (v[2] - img.rect[2]).abs() > 1e-4
++ || (v[3] - img.rect[3]).abs() > 1e-4
++ });
++ let rotated_transform = matches!(change, ImageEdit::Transform(t) if t[1].abs() >= 1e-6 || t[2].abs() >= 1e-6);
++ let should_promote = has_active_crop
++ || rotated_transform
++ || matches!(
++ change,
++ ImageEdit::Crop(_) | ImageEdit::RemoveCrop | ImageEdit::RotateBy(_) | ImageEdit::Arrange(_) | ImageEdit::Delete
++ );
++
++ if should_promote {
++ doc.update_dict(added_item.obj, |d| {
++ d.remove(b"PCAdded");
++ d.remove(b"PCMark");
++ })
++ .map_err(|e| EditError::Edit(pdfcraft_edit::EditError::Invalid(e.to_string())))?;
++ } else {
++ ai_to_update = Some((*added_idx, ai));
++ }
++ }
++
++ if let Some((added_idx, mut ai)) = ai_to_update {
++ let view = page_model.view_matrix(doc);
++ let det = view[0] * view[3] - view[1] * view[2];
++ if det.abs() < 1e-12 || !det.is_finite() {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid page transform".into())));
++ }
++ let (ia, ib, ic, id) = (view[3] / det, -view[1] / det, -view[2] / det, view[0] / det);
++ let to_display = [ia, ib, ic, id, -(view[4] * ia + view[5] * ic), -(view[4] * ib + view[5] * id)];
++ let apply = |m: [f64; 6], x: f64, y: f64| (m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]);
++ let bbox = |m: [f64; 6], r: [f64; 4]| {
++ let pts = [apply(m, r[0], r[1]), apply(m, r[2], r[1]), apply(m, r[0], r[3]), apply(m, r[2], r[3])];
++ let mut b = [f64::MAX, f64::MAX, f64::MIN, f64::MIN];
++ for (x, y) in pts {
++ b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)];
++ }
++ b
++ };
++
++ match change {
++ ImageEdit::Move(to) => {
++ if !to.iter().all(|v| v.is_finite()) {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid position".into())));
++ }
++ ai.rect = bbox(to_display, *to);
++ pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
++ }
++ ImageEdit::Transform(t) => {
++ let mul = |m1: [f64; 6], m2: [f64; 6]| {
++ [
++ m1[0] * m2[0] + m1[1] * m2[2],
++ m1[0] * m2[1] + m1[1] * m2[3],
++ m1[2] * m2[0] + m1[3] * m2[2],
++ m1[2] * m2[1] + m1[3] * m2[3],
++ m1[4] * m2[0] + m1[5] * m2[2] + m2[4],
++ m1[4] * m2[1] + m1[5] * m2[3] + m2[5],
++ ]
++ };
++ let t_display = mul(mul(view, *t), to_display);
++ if t_display[0] < 0.0 {
++ ai.flip_h = !ai.flip_h;
++ }
++ if t_display[3] < 0.0 {
++ ai.flip_v = !ai.flip_v;
++ }
++ ai.rect = bbox(t_display, ai.rect);
++ pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
++ }
++ ImageEdit::Rotate(q) => {
++ let k = ((4 - q.rem_euclid(4)) % 4) as u8;
++ ai.rotation = (ai.rotation + k) % 4;
++ let r = ai.rect;
++ let (cx, cy) = ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0);
++ if k % 2 == 1 {
++ let (w, h) = (r[2] - r[0], r[3] - r[1]);
++ ai.rect = [cx - h / 2.0, cy - w / 2.0, cx + h / 2.0, cy + w / 2.0];
++ }
++ pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
++ }
++ ImageEdit::Flip { horizontal: true } => {
++ ai.flip_h = !ai.flip_h;
++ pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
++ }
++ ImageEdit::Flip { horizontal: false } => {
++ ai.flip_v = !ai.flip_v;
++ pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
++ }
++ ImageEdit::Replace { .. } => {
++ let pdfcraft_edit::ImageChange::Replace(new_image) = c else {
++ return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("expected replace".into())));
++ };
++ ai.image = new_image;
++ pdfcraft_edit::update_content(doc, page, added_idx, &AddedContent::Image(ai))?;
++ }
++ ImageEdit::Crop(_) | ImageEdit::RemoveCrop | ImageEdit::RotateBy(_) | ImageEdit::Arrange(_) | ImageEdit::Delete => {
++ pdfcraft_edit::change_image(doc, page, index, &c)?;
++ }
++ }
++ } else {
++ pdfcraft_edit::change_image(doc, page, index, &c)?;
++ }
++ Ok(())
++}
++
/// Perform an edit on a working copy (the caller discards it on error).
pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut EditCtx) -> Result<(), EditError> {
match edit {
@@@ -470,187 -525,40 +713,7 @@@
Edit::AddOcrText { page, words } => {
pdfcraft_edit::stamp(doc, *page, "OCR", pdfcraft_ocr::text_layer(words))?;
}
-- Edit::EditPageImage { page, index, change } => {
- match change {
- ImageEdit::Move(to) if !to.iter().all(|v| v.is_finite()) => {
- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid position".into())));
- }
- ImageEdit::Transform(t) if !t.iter().all(|v| v.is_finite()) => {
- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("the image transform must be finite".into())));
- }
- ImageEdit::Crop(keep) if !keep.iter().all(|v| v.is_finite()) => {
- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid crop".into())));
- }
- _ => {}
- }
-- let img = pdfcraft_edit::page_images(doc, *page)?
-- .into_iter()
-- .nth(*index)
-- .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} has no image {}", page + 1, index + 1))))?;
-- let c = match change {
-- ImageEdit::Move(to) => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::rect_to_rect(img.rect, *to)),
- ImageEdit::Transform(t) => pdfcraft_edit::ImageChange::Transform(*t),
-- ImageEdit::Rotate(q) => {
-- pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(img.visible_rect.unwrap_or(img.rect), *q, false, false))
-- }
-- ImageEdit::RotateBy(degrees) => {
-- let t = pdfcraft_edit::turn_by_degrees(img.visible_rect.unwrap_or(img.rect), *degrees)
-- .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid("the rotation angle must be a finite number".into())))?;
-- pdfcraft_edit::ImageChange::Transform(t)
-- }
-- ImageEdit::Flip { horizontal } => pdfcraft_edit::ImageChange::Transform(pdfcraft_edit::turn_about_centre(
-- img.visible_rect.unwrap_or(img.rect),
-- 0,
-- *horizontal,
-- !*horizontal,
-- )),
-- ImageEdit::Replace { name, bytes } => {
-- if img.is_form {
-- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid(
-- "grouped Form artwork cannot be replaced with a bitmap".into(),
-- )));
-- }
-- pdfcraft_edit::ImageChange::Replace(embed_image(doc, name, bytes)?.0)
-- }
-- ImageEdit::Crop(keep) => pdfcraft_edit::ImageChange::Crop(*keep),
-- ImageEdit::Delete => pdfcraft_edit::ImageChange::Delete,
- ImageEdit::Arrange(order) => pdfcraft_edit::ImageChange::Arrange(*order),
- ImageEdit::RemoveCrop => pdfcraft_edit::ImageChange::RemoveCrop,
- };
- let page_model = pdfcraft_model::pages(doc)
- .into_iter()
- .nth(*page)
- .ok_or_else(|| EditError::Edit(pdfcraft_edit::EditError::Invalid(format!("page {} not found", page + 1))))?;
- let target_stream_obj = match page_model.dict.get(b"Contents") {
- None => None,
- Some(c) => match &*doc.resolve(c) {
- pdfcraft_cos::Object::Array(a) => a.get(img.stream).and_then(|o| o.as_ref()),
- _ if img.stream == 0 => c.as_ref(),
- _ => None,
- },
- };
-
- let added_items: Vec<(usize, pdfcraft_edit::Added)> =
- pdfcraft_edit::list_added(doc).into_iter().filter(|a| a.page == *page).enumerate().collect();
- let matched_added = if let Some(st_ref) = target_stream_obj {
- added_items.iter().find(|(_, a)| a.obj == st_ref)
- } else if let Some(target_ref) = img.object {
- added_items.iter().find(|(_, a)| matches!(&a.content, AddedContent::Image(ai) if ai.image == target_ref))
- } else {
- None
-- };
-
- let mut ai_to_update = None;
- if let Some((added_idx, added_item)) = matched_added
- && let AddedContent::Image(ai) = added_item.content.clone()
- {
- let has_active_crop = ai.crop.iter().any(|v| v.abs() > 1e-6)
- || img.visible_rect.is_some_and(|v| {
- (v[0] - img.rect[0]).abs() > 1e-4
- || (v[1] - img.rect[1]).abs() > 1e-4
- || (v[2] - img.rect[2]).abs() > 1e-4
- || (v[3] - img.rect[3]).abs() > 1e-4
- });
- let rotated_transform = matches!(change, ImageEdit::Transform(t) if t[1].abs() >= 1e-6 || t[2].abs() >= 1e-6);
- let should_promote = has_active_crop
- || rotated_transform
- || matches!(
- change,
- ImageEdit::Crop(_) | ImageEdit::RemoveCrop | ImageEdit::RotateBy(_) | ImageEdit::Arrange(_) | ImageEdit::Delete
- );
-
- if should_promote {
- doc.update_dict(added_item.obj, |d| {
- d.remove(b"PCAdded");
- d.remove(b"PCMark");
- })
- .map_err(|e| EditError::Edit(pdfcraft_edit::EditError::Invalid(e.to_string())))?;
- } else {
- ai_to_update = Some((*added_idx, ai));
- }
- }
-
- if let Some((added_idx, mut ai)) = ai_to_update {
- let view = page_model.view_matrix(doc);
- let det = view[0] * view[3] - view[1] * view[2];
- if det.abs() < 1e-12 || !det.is_finite() {
- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid page transform".into())));
- }
- let (ia, ib, ic, id) = (view[3] / det, -view[1] / det, -view[2] / det, view[0] / det);
- let to_display = [ia, ib, ic, id, -(view[4] * ia + view[5] * ic), -(view[4] * ib + view[5] * id)];
- let apply = |m: [f64; 6], x: f64, y: f64| (m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]);
- let bbox = |m: [f64; 6], r: [f64; 4]| {
- let pts = [apply(m, r[0], r[1]), apply(m, r[2], r[1]), apply(m, r[0], r[3]), apply(m, r[2], r[3])];
- let mut b = [f64::MAX, f64::MAX, f64::MIN, f64::MIN];
- for (x, y) in pts {
- b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)];
- }
- b
- };
-
- match change {
- ImageEdit::Move(to) => {
- if !to.iter().all(|v| v.is_finite()) {
- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("invalid position".into())));
- }
- ai.rect = bbox(to_display, *to);
- pdfcraft_edit::update_content(doc, *page, added_idx, &AddedContent::Image(ai))?;
- }
- ImageEdit::Transform(t) => {
- let mul = |m1: [f64; 6], m2: [f64; 6]| {
- [
- m1[0] * m2[0] + m1[1] * m2[2],
- m1[0] * m2[1] + m1[1] * m2[3],
- m1[2] * m2[0] + m1[3] * m2[2],
- m1[2] * m2[1] + m1[3] * m2[3],
- m1[4] * m2[0] + m1[5] * m2[2] + m2[4],
- m1[4] * m2[1] + m1[5] * m2[3] + m2[5],
- ]
- };
- let t_display = mul(mul(view, *t), to_display);
- if t_display[0] < 0.0 {
- ai.flip_h = !ai.flip_h;
- }
- if t_display[3] < 0.0 {
- ai.flip_v = !ai.flip_v;
- }
- ai.rect = bbox(t_display, ai.rect);
- pdfcraft_edit::update_content(doc, *page, added_idx, &AddedContent::Image(ai))?;
- }
- ImageEdit::Rotate(q) => {
- let k = ((4 - q.rem_euclid(4)) % 4) as u8;
- ai.rotation = (ai.rotation + k) % 4;
- let r = ai.rect;
- let (cx, cy) = ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0);
- if k % 2 == 1 {
- let (w, h) = (r[2] - r[0], r[3] - r[1]);
- ai.rect = [cx - h / 2.0, cy - w / 2.0, cx + h / 2.0, cy + w / 2.0];
- }
- pdfcraft_edit::update_content(doc, *page, added_idx, &AddedContent::Image(ai))?;
- }
- ImageEdit::Flip { horizontal: true } => {
- ai.flip_h = !ai.flip_h;
- pdfcraft_edit::update_content(doc, *page, added_idx, &AddedContent::Image(ai))?;
- }
- ImageEdit::Flip { horizontal: false } => {
- ai.flip_v = !ai.flip_v;
- pdfcraft_edit::update_content(doc, *page, added_idx, &AddedContent::Image(ai))?;
- }
- ImageEdit::Replace { .. } => {
- let pdfcraft_edit::ImageChange::Replace(new_image) = c else {
- return Err(EditError::Edit(pdfcraft_edit::EditError::Invalid("expected replace".into())));
- };
- ai.image = new_image;
- pdfcraft_edit::update_content(doc, *page, added_idx, &AddedContent::Image(ai))?;
- }
- ImageEdit::Crop(_) | ImageEdit::RemoveCrop | ImageEdit::RotateBy(_) | ImageEdit::Arrange(_) | ImageEdit::Delete => {
- pdfcraft_edit::change_image(doc, *page, *index, &c)?;
- }
- }
- } else {
- pdfcraft_edit::change_image(doc, *page, *index, &c)?;
- }
- pdfcraft_edit::change_image(doc, *page, *index, &c)?;
-- }
++ Edit::EditPageImage { page, index, change } => run_edit_page_image(doc, *page, *index, change)?,
Edit::MoveObjects { page, objects, offset } => pdfcraft_edit::move_objects(doc, *page, objects, *offset)?,
Edit::EditTextBlock { page, block, text, style } => {
pdfcraft_edit::rewrite_block(doc, *page, *block, Some(text), style)?;
diff --cc crates/ui-egui/src/app.rs
index 07327d1bc,425c2e26c..25937f33e
--- a/crates/ui-egui/src/app.rs
+++ b/crates/ui-egui/src/app.rs
@@@ -104,9 -136,11 +136,13 @@@ impl PdfCraftApp
#[cfg(all(test, not(target_arch = "wasm32")))]
open_prepare_gate: None,
pinned: Default::default(),
+ pinned_tools: Vec::new(),
+ pin_tool_search: String::new(),
toast: None,
+ toast_script: None,
+ script_messages: std::collections::VecDeque::new(),
+ script_messages_dropped: 0,
+ toast_dropped: 0,
integrated_titlebar: false,
window_controls: None,
system_title_bar: false,
@@@ -191,9 -224,9 +227,10 @@@
system_clipboard: true,
clipboard_override: None,
v_pressed: false,
+ last_command_time: None,
attach_override: None,
duplicate_draft: None,
+ page_props_draft: None,
field_table_draft: None,
form_preview: false,
window_title: String::new(),
diff --cc crates/ui-egui/src/app/prefs.rs
index eb9499989,e5eaae586..191e16b87
--- a/crates/ui-egui/src/app/prefs.rs
+++ b/crates/ui-egui/src/app/prefs.rs
@@@ -139,8 -139,8 +139,9 @@@ impl PdfCraftApp
});
// Set apart: one more key inside the macro above passes serde_json's recursion limit.
v["default_page_gaps"] = serde_json::Value::Bool(self.view_defaults.show_page_gaps);
+ v["fill_text_color"] = serde_json::json!(self.fill_text_color);
v["redact_prefs"] = self.redact_defaults.to_json();
+ v["pinned_tools"] = serde_json::json!(self.pinned_tools);
// Set outside json! above, which is at serde_json's macro recursion limit.
v["hide_empty_highlight_hover"] = serde_json::Value::Bool(self.comment_prefs.hide_empty_highlight_hover);
v["comment_tool_pinned"] = serde_json::Value::Bool(self.comment_prefs.pinned);
diff --cc crates/ui-egui/src/canvas.rs
index 0839a866a,8b280ba75..3666abbf5
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@@ -1,3419 -1,3496 +1,4256 @@@
--//! The document area: page layout, zoom, render scheduling, overlays (links, annotation hovers,
--//! form-field highlights), and the organize-pages grid.
--//!
--//! Page images come from the engine's render pool as whole-page rasters at the current zoom;
--//! stale rasters are shown stretched until the sharp one arrives (tiling comes in M3.3).
--
--use std::collections::{BTreeSet, HashMap, HashSet};
--use std::ops::Range;
--use std::sync::Arc;
--
--use egui::{Align2, Color32, CornerRadius, Pos2, Rect, Sense, Stroke, TextureHandle, TextureOptions, Vec2, pos2, vec2};
--use pdfcraft_engine::{DocId, Edit};
--use pdfcraft_render::{DestView, DocInfo, LayerOp, LinkTarget, PRINT_TAG, PageText, RenderPool, RenderRequest, RequestKind, Tile, device_pixels};
--
-use crate::page_cursor::{Layer, Subject};
--use crate::page_pointer::PagePointer;
--use crate::theme::{self, Tokens};
--use crate::{PdfCraftApp, QuickTool, RightPanel, comments, icons, widgets};
--
--mod area;
--pub(super) mod autoscroll;
--mod bars;
--mod cache;
--pub(super) mod crop;
--mod find;
--pub(super) mod fling;
--pub(super) mod image_pick;
--mod navigation;
--mod organize;
-pub(super) mod page_cursor;
--pub(super) mod page_pointer;
-pub(super) mod page_router;
--pub mod scroll_speed;
--mod selection;
--pub(super) mod signature_drag;
--pub(super) mod wheel_boost;
--pub(super) mod wheel_pager;
--mod xform;
--mod zoom;
--pub(super) mod zoom_snap;
--
--pub(crate) use area::signature_badges;
--use area::{
- SUPPRESS_SECS, SUPPRESS_TAIL_SECS, annotation_hover_text, drawing_edge_pointer, glide_interrupted, page_scroll, presentation_click, run_button,
- set_layers, zoom_anchor,
- SUPPRESS_SECS, SUPPRESS_TAIL_SECS, drawing_edge_pointer, glide_interrupted, page_scroll, presentation_click, run_button, set_layers, zoom_anchor,
--};
--#[cfg(test)]
--use bars::in_size_hint_zone;
--use bars::{Notice, notices, page_size_hint, quick_bar};
--pub use cache::RasterMemory;
--pub(crate) use cache::{BASE_SIDE, TILE_THRESHOLD, texture_image, thumbnail_rows};
--#[cfg(test)]
--use cache::{
-- GRAYSCALE_TAG, GRID_TAG, MAX_CACHED_TEXT_PAGES, MAX_THUMB_DEMAND, PAGE_BYTES, THUMB_BYTES, THUMB_H, THUMB_TAG, THUMB_W, TILE, grayscale,
-- rgba_bytes,
--};
--use cache::{OLD_TILES, PageTex, STALE_TAG, TEXT_TAG, TILE_BYTES, scale_tag, tag_scale, texture_bytes, tile_side};
--pub use find::Find;
--use find::find_bar;
--pub use navigation::shortcuts;
--pub(crate) use organize::drop_gap;
--#[cfg(test)]
--pub(crate) use organize::grid_page_rect;
--use organize::organize_grid;
--#[cfg(test)]
--use organize::sharp_enough;
--use selection::{Selection, Span, click_unit, selection_actions};
--pub use xform::PageXform;
--use xform::snap_to_pixels;
--use zoom::anchored_scroll;
--pub use zoom::{GRID_ZOOM_RANGE, THUMBNAIL_ZOOM_RANGE};
--
--/// Logical pixels per PDF point at 100% (96 dpi, like browsers).
--pub const PT: f32 = 96.0 / 72.0;
--const GAP: f32 = 14.0;
--const MARGIN: f32 = 28.0;
--/// Horizontal gutter that keeps pages clear of the floating quick-action bar.
--const SIDE: f32 = 70.0;
- use crate::edit_text_ui::ImageHandle;
-
- /// Extension trait on `egui::Painter` to draw polygons.
- pub trait PainterExt {
- fn polygon(&self, points: Vec<Pos2>, fill: Color32, stroke: Stroke) -> egui::layers::ShapeIdx;
- }
-
- impl PainterExt for egui::Painter {
- fn polygon(&self, points: Vec<Pos2>, fill: Color32, stroke: Stroke) -> egui::layers::ShapeIdx {
- self.add(egui::Shape::convex_polygon(points, fill, stroke))
- }
- }
-
- /// User-space selectable bounding box for an image object.
- /// If the image object has an active clipping path applied to it (the crop box),
- /// this is the mathematical intersection of the full CTM-derived rect and the clipping path's rect.
- pub fn image_selectable_bounds(im: &pdfcraft_engine::PageImage) -> [f64; 4] {
- if let Some(clip) = im.clip_rect {
- let r = [im.rect[0].max(clip[0]), im.rect[1].max(clip[1]), im.rect[2].min(clip[2]), im.rect[3].min(clip[3])];
- if r.iter().all(|v| v.is_finite()) && r[2] > r[0] && r[3] > r[1] {
- return r;
- }
- }
- im.visible_rect.unwrap_or(im.rect)
- }
-
- /// Screen-space selectable bounding box for an image object.
- /// If the image object has an active clipping path applied to it (the crop box),
- /// the final selectable `egui::Rect` is the mathematical intersection of the image's
- /// full CTM-derived rect and the clipping path's rect.
- pub fn image_screen_rect(im: &pdfcraft_engine::PageImage, xf: &PageXform, info: &DocInfo, page: usize) -> Rect {
- let full = xf.user_rect_f64(info, page, im.rect);
- if let Some(clip) = im.clip_rect {
- let clip_r = xf.user_rect_f64(info, page, clip);
- let inter = full.intersect(clip_r);
- if inter.is_positive() {
- return inter;
- }
- }
- if let Some(vr) = im.visible_rect {
- let r = xf.user_rect_f64(info, page, vr);
- if r.is_positive() {
- return r;
- }
- }
- full
- }
-
- /// An Oriented Bounding Box (OBB) representing a rotated rectangle in screen space,
- /// with 8 resize handles and rotated local axes.
- #[derive(Clone, Copy, Debug, PartialEq)]
- pub struct ImageObb {
- pub s00: Pos2,
- pub s10: Pos2,
- pub s11: Pos2,
- pub s01: Pos2,
- pub s_bc: Pos2,
- pub s_rc: Pos2,
- pub s_tc: Pos2,
- pub s_lc: Pos2,
- pub s_rot: Pos2,
- pub screen_center: Pos2,
- pub width: f32,
- pub height: f32,
- pub theta: f32,
- pub u_axis: (f64, f64),
- pub v_axis: (f64, f64),
- pub is_rotated: bool,
- }
-
- impl ImageObb {
- /// Calculates the 4 rotated corners manually using center, width, height, and rotation angle (θ).
- /// Local axes: `u_axis = (cos θ, sin θ)` and `v_axis = (-sin θ, cos θ)`.
- /// Places the 8 resize handles exactly at these rotated vertices and midpoints.
- pub fn from_center_size_angle(center: Pos2, width: f32, height: f32, theta: f32) -> Self {
- let cos_t = theta.cos();
- let sin_t = theta.sin();
- let u_axis = (cos_t as f64, sin_t as f64);
- let v_axis = (-sin_t as f64, cos_t as f64);
- let u_vec = vec2(cos_t, sin_t);
- let v_vec = vec2(-sin_t, cos_t);
- let hw = width * 0.5;
- let hh = height * 0.5;
-
- let s00 = center - u_vec * hw - v_vec * hh;
- let s10 = center + u_vec * hw - v_vec * hh;
- let s11 = center + u_vec * hw + v_vec * hh;
- let s01 = center - u_vec * hw + v_vec * hh;
-
- let s_bc = center - v_vec * hh;
- let s_rc = center + u_vec * hw;
- let s_tc = center + v_vec * hh;
- let s_lc = center - u_vec * hw;
-
- let s_rot = s_tc + v_vec * 24.0;
- let is_rotated = theta.abs() > 1e-4;
-
- Self { s00, s10, s11, s01, s_bc, s_rc, s_tc, s_lc, s_rot, screen_center: center, width, height, theta, u_axis, v_axis, is_rotated }
- }
-
- pub fn new(matrix: [f64; 6], xf: &PageXform, info: &DocInfo, page: usize) -> Self {
- Self::from_matrix(matrix, xf, info, page)
- }
-
- pub fn from_matrix(matrix: [f64; 6], xf: &PageXform, info: &DocInfo, page: usize) -> Self {
- let m = crate::edit_text_ui::Matrix(matrix);
- let p00 = m.apply(0.0, 0.0);
- let p10 = m.apply(1.0, 0.0);
- let p11 = m.apply(1.0, 1.0);
- let p01 = m.apply(0.0, 1.0);
-
- let s00 = crate::edit_text_ui::user_to_screen(xf, info, page, p00.0, p00.1);
- let s10 = crate::edit_text_ui::user_to_screen(xf, info, page, p10.0, p10.1);
- let s11 = crate::edit_text_ui::user_to_screen(xf, info, page, p11.0, p11.1);
- let s01 = crate::edit_text_ui::user_to_screen(xf, info, page, p01.0, p01.1);
-
- let s_bc = Pos2::new((s00.x + s10.x) * 0.5, (s00.y + s10.y) * 0.5);
- let s_rc = Pos2::new((s10.x + s11.x) * 0.5, (s10.y + s11.y) * 0.5);
- let s_tc = Pos2::new((s01.x + s11.x) * 0.5, (s01.y + s11.y) * 0.5);
- let s_lc = Pos2::new((s00.x + s01.x) * 0.5, (s00.y + s01.y) * 0.5);
-
- let screen_center = Pos2::new((s00.x + s10.x + s11.x + s01.x) * 0.25, (s00.y + s10.y + s11.y + s01.y) * 0.25);
- let out_dir = s_tc - screen_center;
- let out_len = out_dir.length().max(1e-4);
- let out_norm = out_dir / out_len;
- let s_rot = s_tc + out_norm * 24.0;
-
- let [a, b, c, d, ..] = matrix;
- let w_u = (a * a + b * b).sqrt().max(1e-6);
- let h_v = (c * c + d * d).sqrt().max(1e-6);
- let u_axis = (a / w_u, b / w_u);
- let v_axis = (c / h_v, d / h_v);
- let theta = (b).atan2(a) as f32;
- let is_rotated = b.abs() > 1e-4 || c.abs() > 1e-4;
-
- Self {
- s00,
- s10,
- s11,
- s01,
- s_bc,
- s_rc,
- s_tc,
- s_lc,
- s_rot,
- screen_center,
- width: (s10 - s00).length(),
- height: (s01 - s00).length(),
- theta,
- u_axis,
- v_axis,
- is_rotated,
- }
- }
-
- pub fn from_image(im: &pdfcraft_engine::PageImage, xf: &PageXform, info: &DocInfo, page: usize) -> Self {
- let [_, b, c, ..] = im.matrix;
- let is_rot = b.abs() > 1e-4 || c.abs() > 1e-4;
- let bounds = image_selectable_bounds(im);
- let matrix = if !is_rot && bounds[2] > bounds[0] && bounds[3] > bounds[1] {
- [bounds[2] - bounds[0], 0.0, 0.0, bounds[3] - bounds[1], bounds[0], bounds[1]]
- } else {
- im.matrix
- };
- Self::from_matrix(matrix, xf, info, page)
- }
-
- pub fn handles(&self) -> [(ImageHandle, Pos2); 8] {
- [
- (ImageHandle::TopLeft, self.s01),
- (ImageHandle::TopCenter, self.s_tc),
- (ImageHandle::TopRight, self.s11),
- (ImageHandle::RightCenter, self.s_rc),
- (ImageHandle::BottomRight, self.s10),
- (ImageHandle::BottomCenter, self.s_bc),
- (ImageHandle::BottomLeft, self.s00),
- (ImageHandle::LeftCenter, self.s_lc),
- ]
- }
-
- pub fn handle_pos(&self, handle: ImageHandle) -> Pos2 {
- match handle {
- ImageHandle::TopLeft => self.s01,
- ImageHandle::TopCenter => self.s_tc,
- ImageHandle::TopRight => self.s11,
- ImageHandle::RightCenter => self.s_rc,
- ImageHandle::BottomRight => self.s10,
- ImageHandle::BottomCenter => self.s_bc,
- ImageHandle::BottomLeft => self.s00,
- ImageHandle::LeftCenter => self.s_lc,
- }
- }
-
- pub fn handle_id(&self, base_id: egui::Id, handle: ImageHandle) -> egui::Id {
- base_id.with(match handle {
- ImageHandle::TopLeft => "handle_tl",
- ImageHandle::TopCenter => "handle_tc",
- ImageHandle::TopRight => "handle_tr",
- ImageHandle::RightCenter => "handle_rc",
- ImageHandle::BottomRight => "handle_br",
- ImageHandle::BottomCenter => "handle_bc",
- ImageHandle::BottomLeft => "handle_bl",
- ImageHandle::LeftCenter => "handle_lc",
- })
- }
-
- pub fn handles_with_ids(&self, base_id: egui::Id) -> [(ImageHandle, Pos2, egui::Id); 8] {
- [
- (ImageHandle::TopLeft, self.s01, self.handle_id(base_id, ImageHandle::TopLeft)),
- (ImageHandle::TopCenter, self.s_tc, self.handle_id(base_id, ImageHandle::TopCenter)),
- (ImageHandle::TopRight, self.s11, self.handle_id(base_id, ImageHandle::TopRight)),
- (ImageHandle::RightCenter, self.s_rc, self.handle_id(base_id, ImageHandle::RightCenter)),
- (ImageHandle::BottomRight, self.s10, self.handle_id(base_id, ImageHandle::BottomRight)),
- (ImageHandle::BottomCenter, self.s_bc, self.handle_id(base_id, ImageHandle::BottomCenter)),
- (ImageHandle::BottomLeft, self.s00, self.handle_id(base_id, ImageHandle::BottomLeft)),
- (ImageHandle::LeftCenter, self.s_lc, self.handle_id(base_id, ImageHandle::LeftCenter)),
- ]
- }
-
- pub fn corners(&self) -> [Pos2; 4] {
- [self.s00, self.s10, self.s11, self.s01]
- }
-
- /// Resizes the OBB proportionally along its local axes using projected mouse delta,
- /// locking the diagonally opposite corner as the fixed anchor point.
- pub fn scale_proportional(&self, handle: ImageHandle, delta: Vec2, min_size: f32) -> Self {
- let u_vec = vec2(self.u_axis.0 as f32, self.u_axis.1 as f32);
- let v_vec = vec2(self.v_axis.0 as f32, self.v_axis.1 as f32);
- let delta_u = delta.dot(u_vec);
- let delta_v = delta.dot(v_vec);
- let min_s = min_size.max(5.0);
-
- match handle {
- ImageHandle::TopRight => {
- let anchor = self.s00;
- let s_u = (self.width + delta_u) / self.width.max(1e-4);
- let s_v = (self.height + delta_v) / self.height.max(1e-4);
- let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
- let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
- let nw = self.width * s;
- let nh = self.height * s;
- let new_center = anchor + u_vec * (nw * 0.5) + v_vec * (nh * 0.5);
- Self::from_center_size_angle(new_center, nw, nh, self.theta)
- }
- ImageHandle::TopLeft => {
- let anchor = self.s10;
- let s_u = (self.width - delta_u) / self.width.max(1e-4);
- let s_v = (self.height + delta_v) / self.height.max(1e-4);
- let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
- let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
- let nw = self.width * s;
- let nh = self.height * s;
- let new_center = anchor - u_vec * (nw * 0.5) + v_vec * (nh * 0.5);
- Self::from_center_size_angle(new_center, nw, nh, self.theta)
- }
- ImageHandle::BottomLeft => {
- let anchor = self.s11;
- let s_u = (self.width - delta_u) / self.width.max(1e-4);
- let s_v = (self.height - delta_v) / self.height.max(1e-4);
- let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
- let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
- let nw = self.width * s;
- let nh = self.height * s;
- let new_center = anchor - u_vec * (nw * 0.5) - v_vec * (nh * 0.5);
- Self::from_center_size_angle(new_center, nw, nh, self.theta)
- }
- ImageHandle::BottomRight => {
- let anchor = self.s01;
- let s_u = (self.width + delta_u) / self.width.max(1e-4);
- let s_v = (self.height - delta_v) / self.height.max(1e-4);
- let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
- let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
- let nw = self.width * s;
- let nh = self.height * s;
- let new_center = anchor + u_vec * (nw * 0.5) - v_vec * (nh * 0.5);
- Self::from_center_size_angle(new_center, nw, nh, self.theta)
- }
- ImageHandle::RightCenter => {
- let anchor = self.s_lc;
- let nw = (self.width + delta_u).max(min_s);
- let new_center = anchor + u_vec * (nw * 0.5);
- Self::from_center_size_angle(new_center, nw, self.height, self.theta)
- }
- ImageHandle::LeftCenter => {
- let anchor = self.s_rc;
- let nw = (self.width - delta_u).max(min_s);
- let new_center = anchor - u_vec * (nw * 0.5);
- Self::from_center_size_angle(new_center, nw, self.height, self.theta)
- }
- ImageHandle::TopCenter => {
- let anchor = self.s_bc;
- let nh = (self.height + delta_v).max(min_s);
- let new_center = anchor + v_vec * (nh * 0.5);
- Self::from_center_size_angle(new_center, self.width, nh, self.theta)
- }
- ImageHandle::BottomCenter => {
- let anchor = self.s_tc;
- let nh = (self.height - delta_v).max(min_s);
- let new_center = anchor - v_vec * (nh * 0.5);
- Self::from_center_size_angle(new_center, self.width, nh, self.theta)
- }
- }
- }
- }
-
- /// Projects mouse delta onto local unit axes `u_axis` and `v_axis` using the dot product.
- pub fn project_delta(delta: Vec2, u_axis: (f64, f64), v_axis: (f64, f64)) -> (f64, f64) {
- let dx = delta.x as f64;
- let dy = delta.y as f64;
- (dx * u_axis.0 + dy * u_axis.1, dx * v_axis.0 + dy * v_axis.1)
- }
-
- /// Visible grid rows, plus one adjacent row in either direction. Work is independent of
- /// document length, including after a large scroll jump.
--
--#[derive(Clone, Copy, Debug, PartialEq, Eq)]
--pub enum Fit {
-- Width,
-- Page,
-- Height,
-- None,
--}
--
--/// The page display as it was before full screen, put back on leaving it
--/// ([`DocView::enter_presentation`]).
--#[derive(Clone, Copy, Debug, PartialEq)]
--pub struct SavedDisplay {
-- pub layout: PageLayout,
-- pub continuous: bool,
-- pub cover: bool,
-- pub fit: Fit,
-- pub zoom: f32,
--}
--
--/// The base page display: one page across, or a two-page spread. Whether it scrolls continuously
--/// is a separate flag (`continuous`), as in Acrobat, so every combination is reachable (#752).
--#[derive(Clone, Copy, Debug, PartialEq, Eq)]
--pub enum PageLayout {
-- Single,
-- TwoUp,
--}
--
--impl PageLayout {
-- /// The base page displays, in the order the View menu, the rail's Page display button and
-- /// Preferences list them.
-- pub const ORDER: [Self; 2] = [Self::Single, Self::TwoUp];
--
-- /// The name settings and view options use for the base layout: `single` or `two-up`.
-- pub fn as_str(self) -> &'static str {
-- match self {
-- Self::Single => "single",
-- Self::TwoUp => "two-up",
-- }
-- }
--
-- /// The base layout an [`as_str`](Self::as_str) name stands for, in any case. `None` for
-- /// anything else, so callers can reject a typo rather than switch layouts silently.
-- pub fn try_parse(value: &str) -> Option<Self> {
-- Self::ORDER.into_iter().find(|l| l.as_str().eq_ignore_ascii_case(value.trim()))
-- }
--
-- /// Rail icon for this layout.
-- pub fn icon(self) -> &'static str {
-- match self {
-- Self::Single => "file-text",
-- Self::TwoUp => "columns-2",
-- }
-- }
--
-- /// Registry command that switches to this base layout (`PdfCraftApp::execute`).
-- pub fn command(self) -> &'static str {
-- match self {
-- Self::Single => "view.layout.single",
-- Self::TwoUp => "view.layout.two_up",
-- }
-- }
--
-- /// The base layout a [`command`](Self::command) id switches to.
-- pub fn from_command(id: &str) -> Option<Self> {
-- Self::ORDER.into_iter().find(|l| l.command() == id)
-- }
--
-- /// Menu label: the label of [`command`](Self::command), which the catalogs translate.
-- pub fn label(self) -> &'static str {
-- match self {
-- Self::Single => "Single page",
-- Self::TwoUp => "Two-page view",
-- }
-- }
--}
--
--/// The single page-display token settings and the control channel use, combining the base layout
--/// with continuous scrolling. `single` and `two-page` show one page or one spread at a time;
--/// `continuous` (Single, scrolling) and `two-up` (Two-page, scrolling) keep their earlier names
--/// for back-compat.
--pub fn display_token(layout: PageLayout, continuous: bool) -> &'static str {
-- match (layout, continuous) {
-- (PageLayout::Single, false) => "single",
-- (PageLayout::Single, true) => "continuous",
-- (PageLayout::TwoUp, true) => "two-up",
-- (PageLayout::TwoUp, false) => "two-page",
-- }
--}
--
--/// The base layout and continuous flag a [`display_token`] names, in any case. `None` for a typo.
--pub fn parse_display_token(value: &str) -> Option<(PageLayout, bool)> {
-- match value.trim().to_ascii_lowercase().as_str() {
-- "single" => Some((PageLayout::Single, false)),
-- "continuous" => Some((PageLayout::Single, true)),
-- "two-up" => Some((PageLayout::TwoUp, true)),
-- "two-page" | "two-up-single" => Some((PageLayout::TwoUp, false)),
-- _ => None,
-- }
--}
--
--/// How a newly opened document is shown (Preferences ▸ Documents and view). A PDF that asks
--/// for its own layout or zoom gets that instead.
--#[derive(Clone, Copy, Debug, PartialEq)]
--pub struct ViewDefaults {
-- pub layout: PageLayout,
-- /// Whether the base layout scrolls continuously.
-- pub continuous: bool,
-- /// Separate pages with a gutter and shadow; off joins adjacent page edges.
-- pub show_page_gaps: bool,
-- /// [`Fit::Width`] or [`Fit::Page`], or [`Fit::None`] for a fixed `zoom`.
-- pub fit: Fit,
-- /// The zoom with [`Fit::None`] (1.0 = 100%).
-- pub zoom: f32,
-- /// Highlight existing fields: a preference in Acrobat, so it carries over to the next
-- /// document and the next session. On for new settings; a stored choice wins.
-- pub highlight_fields: bool,
--}
--
--impl Default for ViewDefaults {
-- fn default() -> Self {
-- // Single page, scrolling continuously at fit width: the earlier "continuous" default.
-- Self { layout: PageLayout::Single, continuous: true, show_page_gaps: true, fit: Fit::Width, zoom: 1.0, highlight_fields: true }
-- }
--}
--
--impl ViewDefaults {
-- /// The zoom as settings and view options name it: `fit-width`, `fit-page` or a percentage.
-- pub fn zoom_name(&self) -> String {
-- match self.fit {
-- Fit::Page => "fit-page".into(),
-- Fit::None => format!("{}%", (self.zoom * 100.0).round()),
-- Fit::Width | Fit::Height => "fit-width".into(),
-- }
-- }
--
-- /// These defaults with the zoom a [`zoom_name`](Self::zoom_name) names, in any case. `None`
-- /// for anything else, including a percentage outside the 8–6400% the view can show.
-- pub fn with_zoom(self, name: &str) -> Option<Self> {
-- let name = name.trim();
-- let (fit, zoom) = if name.eq_ignore_ascii_case("fit-width") {
-- (Fit::Width, self.zoom)
-- } else if name.eq_ignore_ascii_case("fit-page") {
-- (Fit::Page, self.zoom)
-- } else {
-- let percent = name.strip_suffix('%').unwrap_or(name).trim().parse::<f32>().ok().filter(|p| (8.0..=6400.0).contains(p))?;
-- (Fit::None, percent / 100.0)
-- };
-- Some(Self { fit, zoom, ..self })
-- }
--}
--
--/// A historical snapshot opened from another document: View signed version opens the bytes
--/// a signature covers, View revision an earlier incremental revision. Repeats reuse the tab,
--/// and the tab and notice bar name what it is, instead of piling up identical tabs.
--/// What a historical snapshot shows: the bytes a signature covers, or an earlier
--/// incremental revision.
--#[derive(Clone, Copy, Debug, PartialEq, Eq)]
--pub enum SnapshotKind {
-- SignedVersion,
-- Revision,
--}
--
--#[derive(Clone, Debug)]
--pub struct Snapshot {
-- pub kind: SnapshotKind,
-- /// The 1-based revision shown.
-- pub revision: usize,
-- /// The name of the document it was opened from.
-- pub from: String,
--}
--
--/// The name shown for a document tab, the window title and the Combine picker.
--/// A snapshot reads like its source document, plus the snapshot suffix that tells
--/// the tabs apart. Without a title the file name (which already carries the suffix)
--/// is enough.
--pub(crate) fn tab_label(app: &PdfCraftApp, index: usize) -> Option<String> {
-- let view = app.views.get(index)?;
-- let doc = app.session.get(view.id)?;
-- let Some(snap) = view.snapshot.as_ref() else { return Some(doc.display_name()) };
-- Some(snapshot_tab_name(&doc.display_name(), &doc.name, snap))
--}
--
--/// A snapshot's tab name from its source title, file name and marker. Pure so the
--/// tab strip, window title and picker cannot disagree with each other.
--pub(crate) fn snapshot_tab_name(title: &str, file_name: &str, snapshot: &Snapshot) -> String {
-- if title == file_name {
-- return file_name.into();
-- }
-- if snapshot.kind == SnapshotKind::SignedVersion {
-- format!("{title} (signed version)")
-- } else {
-- format!("{title} (revision {})", snapshot.revision)
-- }
--}
--
--pub struct DocView {
-- pub id: DocId,
-- pub zoom: f32,
-- pub fit: Fit,
-- pub layout: PageLayout,
-- /// Whether the base layout scrolls continuously (independent of `layout`, as in Acrobat).
-- pub continuous: bool,
-- pub show_page_gaps: bool,
-- /// View rotation in degrees clockwise (0, 90, 180, 270); display only, never saved.
-- pub rotation: u16,
-- pub current: usize,
-- pub organize: bool,
-- pub highlight_fields: bool,
-- pub page_input: String,
-- /// Select the replacement page label once if its editor remains focused.
-- pub(crate) page_input_reselect: bool,
-- pub notice_dismissed: bool,
-- /// The user has been told that the form's scripts were turned off (it is said once).
-- pub scripts_off_noted: bool,
-- /// The file changed on disk while this document had unsaved changes: the notice bar asks
-- /// before replacing it (#431).
-- pub disk_changed: bool,
-- /// The snapshot notice bar's Close was clicked: the tab closes at the top of the next
-- /// frame, where no document borrow is held (the bar itself can't close it).
-- pub close_requested: bool,
-- /// A historical snapshot of another document (see [`Snapshot`]): `None` for real documents.
-- pub snapshot: Option<Snapshot>,
-- /// Two-page view: show the first page alone, as a cover (View ▸ Page display).
-- pub cover: bool,
-- /// Previous view / Next view: pages visited before (and after, once going back).
-- pub back: Vec<usize>,
-- pub forward: Vec<usize>,
-- /// Pending navigation: page and fraction down the page to align with the viewport top.
-- pub goto: Option<(usize, f32)>,
-- /// Pending position within that page from a destination ([`Self::go_to_dest`]): the point
-- /// (fractions of the displayed page; `None` leaves that axis to `goto`) to put `rel` points
-- /// from the viewport's top-left corner.
-- goto_point: Option<(usize, Option<f32>, Option<f32>, Vec2)>,
-- /// Pending keyboard scrolling, in points down (negative: up): ↓ / ↑, and Page Down /
-- /// Page Up where the pages scroll.
-- pub key_scroll: f32,
-- /// Single-page view, as last drawn: the page shown and whether the view was at its top and
-- /// at its bottom, so ↑ / ↓ turn the page there instead of doing nothing (#273).
-- single_edges: Option<(usize, bool, bool)>,
-- /// Briefly outline an annotation after navigating to it from a panel.
-- pub flash: Option<(usize, [f32; 4], f64)>,
-- /// Compare files: differences shaded on this document's pages (page, user-space box, colour).
-- pub compare_marks: Vec<(usize, [f32; 4], Color32)>,
-- /// Full screen (Acrobat's Full Screen Mode): the display saved on entering it, while the view
-- /// shows one page or spread at a time, fitted to the screen. `None` outside full screen.
-- presentation: Option<SavedDisplay>,
-- pages: HashMap<usize, PageTex>,
-- /// Pages the renderer could not draw, with the reason (never re-requested).
-- errors: HashMap<usize, String>,
-- /// When each still-missing page was first shown, to flag unusually slow renders.
-- waiting_since: HashMap<usize, f64>,
-- thumbs: HashMap<usize, PageTex>,
-- /// This frame's consumers, with visible cells ahead of prefetch.
-- thumb_demand: HashMap<usize, bool>,
-- frame_queue: Vec<RenderRequest>,
-- /// A page-list edit after layout invalidates this frame's scales and page indexes.
-- frame_geometry_changed: bool,
-- /// Pages on screen this frame. Their rasters and tiles are always admitted: the viewport
-- /// bounds them, and the byte and count limits apply to prefetch only.
-- frame_visible: HashSet<usize>,
-- /// Thumbnails that are out of date (still shown until their replacement arrives).
-- stale_thumbs: HashSet<usize>,
-- /// Tiles that are out of date (still shown until their replacement arrives).
-- stale_tiles: HashSet<(usize, u64, u32, u32)>,
-- /// Scale tag of the current zoom level, as last drawn.
-- current_scale_tag: u64,
-- /// Print dialog: the current sheet's pages at the preview pane's device resolution.
-- print_pages: HashMap<usize, (u64, TextureHandle)>,
-- /// Print dialog: preview rasters become luminance when Print ▸ Print in grayscale is on.
-- grayscale: bool,
-- /// Sharp tiles of large pages: (page, scale tag, tile x, tile y) → texture. Tiles of an
-- /// earlier zoom stay, drawn stretched, until those of the current one cover the page.
-- tiles: HashMap<(usize, u64, u32, u32), TextureHandle>,
-- /// Text layers, extracted in the background on demand (selection, find, copy).
-- pub(crate) texts: HashMap<usize, Arc<PageText>>,
-- pub(crate) text_failed: HashSet<usize>,
-- pub find: Option<Find>,
-- selection: Option<Selection>,
-- /// Select All while scrolling continuously (#2639): the text of every page of the document,
-- /// highlighted on each page as its text arrives and copied whole ([`DocView::whole_text`]).
-- pub(crate) all_text: bool,
-- /// The selection whose action bar was closed by its Copy button: the bar stays hidden
-- /// until the selection changes, as it does once a markup button consumes the selection.
-- actions_dismissed: Option<Selection>,
-- /// The drag in progress started a text selection: the wheel scrolls the page during it
-- /// (#721), which egui's scroll area refuses while anything is dragged.
-- selection_drag: bool,
-- /// Clicks in the current run on the text layer: two select a word, three the line, four the page.
-- clicks: u32,
-- last_press_time: f64,
-- last_press_pos: Option<Pos2>,
-- /// Full screen: a click's page turn, when it falls due (input time) and which way. It waits
-- /// out the double-click time, as the click may be the first of a double-click selecting a word.
-- present_turn: Option<(f64, bool)>,
-- last_queue: Vec<RenderRequest>,
-- /// Finished renders wait in the pool instead of reaching the view (tests): what the view
-- /// shows before a raster arrives can be looked at however fast the renderer is.
-- pub hold_rasters: bool,
-- viewport_w: f32,
-- viewport_h: f32,
-- page_count: usize,
-- /// Page heights in points (view space), for mapping text positions to scroll offsets.
-- page_heights: Vec<f32>,
-- /// Screen rects of the pages drawn last frame (hit-testing, tests, automation).
-- screen_rects: Vec<(usize, Rect)>,
-- screen_xforms: Vec<(usize, PageXform)>,
-- /// The scroll viewport on screen last frame.
-- viewport_screen: Rect,
-- /// Pending zoom anchor: page, fractional position (also in gutters), and viewport offset.
-- zoom_anchor: Option<(usize, f32, f32, Vec2)>,
-- /// The page view has been shown, so its scroll offset in egui's memory is this document's.
-- shown: bool,
-- /// Pages that Edit already tried to turn into editable text (once each, so Undo sticks).
-- pub(crate) auto_ocr_tried: std::collections::HashSet<usize>,
-- /// Pages Edit looked at and found not to be a scan, with the document's edit generation then:
-- /// they are looked at again only after the document changes (an image added, say).
-- pub(crate) auto_ocr_skipped: std::collections::HashMap<usize, u64>,
-- /// Turns wheel input into page turns in single-page view.
-- wheel: crate::wheel_pager::WheelPager,
-- /// Until this time, apply precise (touchpad) wheel input 1:1 and suppress egui's eased copy.
-- /// A precision touchpad sends pixel-precise `MouseWheel` events; on Windows they arrive with
-- /// no `TouchPhase::Start`, so egui eases deltas of 8 px or more and the page lags the finger
-- /// (#759). The window outlasts a single frame so egui's internal easing tail is suppressed as
-- /// it decays, instead of adding on top of the 1:1 motion.
-- precise_scroll_until: f64,
-- /// Per-axis line-wheel state; horizontal input cannot extend a vertical burst.
-- wheel_boost_x: crate::wheel_boost::WheelBoost,
-- wheel_boost_y: crate::wheel_boost::WheelBoost,
-- /// Touchpad momentum after the fingers lift (#531).
-- fling: crate::fling::Fling,
-- pub(crate) auto_scroll: crate::autoscroll::AutoScroll,
-- /// Pages selected in the organize grid or the Pages panel (0-based). Empty means "the current page".
-- pub selected: BTreeSet<usize>,
-- pub(crate) organize_range: crate::page_selection_ui::PageSelectionInput,
-- /// Anchor for ⇧-click range selection in the organize grid.
-- select_anchor: Option<usize>,
-- /// An edit requested by the view (organize toolbar, keys), applied by the app this frame.
-- pub pending_edit: Option<Edit>,
-- /// Edit text: the lines per page (with the document generation they were read at), and the
-- /// line being edited.
-- pub(crate) edit_lines: HashMap<usize, (u64, Vec<pdfcraft_engine::TextBlock>)>,
-- pub line_editor: Option<crate::edit_text_ui::LineEditor>,
-- /// The paragraph editor whose edit was just queued. It opens again with its typing if the
-- /// edit fails, as a refused form field does.
-- pub(crate) applied_editor: Option<crate::edit_text_ui::LineEditor>,
-- /// The box a click switched to while another paragraph was being edited: it opens once the
-- /// queued edit has applied, so its index and geometry come from the document it now is (#2022).
-- /// It is held as the paragraph's text and box, not an index: the edit can regroup the page.
-- pub(crate) pending_text_open: Option<(usize, pdfcraft_engine::BlockKey)>,
-- pub(crate) text_background: crate::edit_text_ui::TextBackground,
-- pub(crate) move_background: crate::content_ui::MoveBackground,
-- /// Edit text & images: the images per page (by document generation), and the selected one.
-- pub(crate) edit_images: HashMap<usize, (u64, Vec<pdfcraft_engine::PageImage>)>,
-- pub(crate) objects: crate::object_ui::ObjectSelection,
-- pub image_selection: Option<crate::edit_text_ui::ImageSelection>,
-- /// Select tool: the picture clicked, to copy in its original format.
-- pub picked_image: Option<crate::image_pick::PickedImage>,
-- /// A paragraph box being dragged (moved, or resized from its right edge) in Edit text.
-- pub block_drag: Option<crate::edit_text_ui::BlockDrag>,
-- /// Commenting state: selected comment, gestures, composer.
-- pub comments: crate::comments::CommentView,
-- pub measure: crate::measure_ui::MeasureView,
-- /// Form filling state: the focused field.
-- pub forms: crate::forms_ui::FormView,
-- /// Prepare a form: the selected field and the gesture in progress.
-- pub prepare: crate::prepare::PrepareView,
-- /// Prepare a form ▸ Calculation order.
-- pub(crate) calc_order: crate::calc_order_ui::CalcOrderView,
-- /// Edit a PDF: the selected added item, the text being typed.
-- pub content: crate::content_ui::ContentView,
-- /// Edit a PDF ▸ Link tool state.
-- pub links: crate::link_ui::LinkView,
-- /// Redact tool: the box being drawn, and a mark to add (page, quads) for the app to style.
-- pub redact_drag: crate::redact_ui::AreaDrag,
-- pub pending_redaction: Option<(usize, Vec<[f64; 8]>)>,
-- /// A crop rectangle being dragged (Crop tool).
-- pub crop_drag: crate::crop::CropDrag,
-- /// Enhance ▸ Camera image: the quadrilateral being placed.
-- pub camera: Option<crate::camera_ui::CameraQuad>,
-- /// Enhance ▸ Camera image: the page just enhanced, while the bar offers rotate, crop and
-- /// organize.
-- pub camera_done: Option<crate::camera_ui::CameraDone>,
-- /// Use a certificate: a signature rectangle being drawn, or an empty signature field clicked.
-- pub sign: crate::sign_ui::SignView,
-- /// Organize: the pages being dragged to a new place.
-- pub org_drag: Option<Vec<usize>>,
-- /// Pages panel: the pages being dragged to a new place.
-- pub panel_drag: Option<Vec<usize>>,
- /// Transient keyboard owner for this document's Pages sidebar.
- pub(crate) pages_focus_id: Option<egui::Id>,
-- /// Marquee Zoom / Snapshot: the rectangle being dragged (page, start), and a finished one.
-- pub marquee: Option<(usize, Pos2)>,
-- pub marquee_done: Option<crate::zoom_snap::Marquee>,
-- /// Fill & Sign text being typed.
-- pub fill_text: Option<crate::fill_sign::TypeBox>,
-- /// A queued Fill & Sign signature: select it after its edit succeeds, then leave placement.
-- pub(crate) fill_signature_page: Option<usize>,
-- pub(crate) signature_drag: crate::signature_drag::SignatureDrag,
- /// What the page modules claim for the cursor this frame; `document_area` sets it once.
- pub(crate) cursor: crate::page_cursor::PageCursor,
- /// Who the press the pages hold belongs to.
- pub(crate) router: crate::page_router::PageRouter,
- /// Escape cancelled the press the pages hold: the page ignores it until the button is up, so
- /// its release neither clicks nor ends a drag.
- pub(crate) press_cancelled: bool,
-- /// A non-edit action requested by the organize toolbar, handled by the app.
-- pub pending_action: Option<ViewAction>,
-- /// The grid gap the next inserted files go to (set by a "+" between pages); otherwise they
-- /// go after the selection.
-- pub insert_at: Option<usize>,
-- /// The page-grid gap under the pointer this frame (where dropped files go), when it can
-- /// take pages.
-- pub grid_gap: Option<usize>,
-- /// The visible part of the Pages panel as last drawn, when it can take dropped files
-- /// (cleared each frame before the panels draw).
-- pub pages_panel: Option<Rect>,
-- /// The Pages-panel gap under the pointer (where dropped files go), when it can take pages.
-- pub panel_gap: Option<usize>,
-- /// The current page the Pages panel last followed: it scrolls to the current page's
-- /// thumbnail only when the page changes.
-- pub(crate) panel_followed: Option<usize>,
-- /// Scale of the thumbnails in the Pages inspector, independent of document and grid zoom.
-- thumbnail_zoom: f32,
-- /// How large the page grid draws its pages (1.0 = the usual thumbnails).
-- grid_zoom: f32,
-- /// A zoom asked for this frame (toolbar, pinch, keys); the grid applies it once it has laid
-- /// out, so it can keep the same pages in view.
-- grid_zoom_request: Option<f32>,
-- /// The page to keep in place after a grid zoom, and how far below the top of the grid its
-- /// cell was.
-- grid_anchor: Option<(usize, f32)>,
-- /// The page the grid scrolls into view on its next frame (Organize just opened on it).
-- grid_reveal: Option<usize>,
-- /// Sharp renders of the pages the grid shows larger than a thumbnail: only those in view,
-- /// at the size drawn (device pixels wide).
-- grid_pages: HashMap<usize, (u32, TextureHandle)>,
-- /// Zoom-settle debounce for heavy pages: the last requested device scale, and the egui
-- /// time until which sharp whole-page/tile re-renders are held back. While the zoom is
-- /// still moving, every frame would otherwise cancel the in-flight full-page interpret
-- /// (seconds on image-heavy CAD plots) and start it over, so a sharp raster never
-- /// arrives. The stale raster stays on screen stretched meanwhile.
-- last_scale: f32,
-- sharp_hold_until: f64,
-- /// Pan-settle debounce for tiled pages: the scroll offset last frame, and the egui time
-- /// until which sharp tile re-renders are held back. Each tile costs a full page
-- /// interpret, so a drag would otherwise queue a storm of them (one per frame) while the
-- /// in-flight ones cannot be cancelled mid-operator. Older tiles and the backdrop stay
-- /// stretched meanwhile; fresh tiles go out once the pan settles.
-- pub(crate) last_scroll: Vec2,
-- /// Side-by-side compare: scroll to here this frame, following the other half.
-- pub(crate) sync_scroll: Option<Vec2>,
-- /// The page widget's id, by which the Hand tool finds its drag before the pages are laid out.
-- page_widget: Option<egui::Id>,
-- pan_hold_until: f64,
-- /// Pages whose raster proved slow (see `SLOW_MILLIS`): only these get the zoom/pan
-- /// settle holds. Fast pages render straight through, with no added latency.
-- slow_pages: HashSet<usize>,
--}
--
--/// Organize-toolbar actions that need the app (file pickers, new tabs, dialogs).
--#[derive(Clone, Copy, Debug, PartialEq, Eq)]
--pub enum ViewAction {
-- InsertFromFile,
-- /// Insert files at a gap of the page grid (0 = before the first page).
-- InsertFromFileAt(usize),
-- /// Replace the selected pages with pages from another file.
-- ReplacePages,
-- /// Save the document as the grid shows it.
-- Save,
-- Extract,
-- Split,
-- /// Split before each selected page: opens Split on that mode.
-- SplitBeforeSelected,
-- /// Copy the selected pages (Cut also deletes them).
-- CopyPages {
-- cut: bool,
-- },
-- /// Paste copied pages after the selection.
-- PastePages,
-- /// Enhance ▸ Camera image: straighten what the corners enclose.
-- EnhanceCamera,
-- /// Leave Enhance ▸ Camera image (Cancel before Enhance page, Done after it).
-- CancelCamera,
-- /// After Enhance page: turn the enhanced page a quarter.
-- CameraRotate {
-- clockwise: bool,
-- },
-- /// After Enhance page: enhance again at the slider's clean-up level.
-- CameraLevel,
-- /// After Enhance page: leave the tool for this command (Crop pages, Organize pages).
-- CameraNext(&'static str),
- }
-
- /// Default page right-click menu actions.
- #[derive(Clone, Debug, PartialEq)]
- pub(crate) enum PageMenuAction {
- RotateCCW(usize),
- RotateCW(usize),
- Print,
- SelectTool,
- HandTool,
- MarqueeZoom,
- Copy,
- Paste(usize, [f64; 2]),
- AddComment(usize, [f64; 2]),
- AddBookmark(usize),
- TakeSnapshot,
- EditPdf,
- AddText,
- AddImage,
- DeletePage(usize),
- ExportPdf,
- Find,
- DocProperties,
- FlattenComments,
- }
-
- pub(crate) struct PageMenuPerms {
- pub can_modify: bool,
- pub can_assemble: bool,
- pub can_annotate: bool,
- pub can_print: bool,
- pub can_copy: bool,
- pub can_paste: bool,
- pub has_annotations: bool,
- }
-
- pub(crate) fn default_page_context_menu(ui: &mut egui::Ui, page: usize, at: [f64; 2], perms: PageMenuPerms, action: &mut Option<PageMenuAction>) {
- // Top horizontal icon bar: Rotate CCW, Print, Select tool, Hand tool, Marquee Zoom
- ui.horizontal(|ui| {
- ui.spacing_mut().item_spacing.x = 4.0;
- if crate::icons::button(ui, "rotate-ccw", 24.0, false, tl!("Rotate view counterclockwise")).clicked() {
- *action = Some(PageMenuAction::RotateCCW(page));
- ui.close();
- }
- ui.add_enabled_ui(perms.can_print, |ui| {
- if crate::icons::button(ui, "printer", 24.0, false, tl!("Print")).clicked() {
- *action = Some(PageMenuAction::Print);
- ui.close();
- }
- });
- if crate::icons::button(ui, "mouse-pointer-2", 24.0, false, tl!("Select Tool")).clicked() {
- *action = Some(PageMenuAction::SelectTool);
- ui.close();
- }
- if crate::icons::button(ui, "hand", 24.0, false, tl!("Hand Tool")).clicked() {
- *action = Some(PageMenuAction::HandTool);
- ui.close();
- }
- if crate::icons::button(ui, "zoom-in", 24.0, false, tl!("Marquee Zoom")).clicked() {
- *action = Some(PageMenuAction::MarqueeZoom);
- ui.close();
- }
- });
-
- ui.separator();
-
- if ui.add_enabled(perms.can_copy, egui::Button::new(tl!("Copy")).shortcut_text("Ctrl+C")).clicked() {
- *action = Some(PageMenuAction::Copy);
- ui.close();
- }
- if ui.add_enabled(perms.can_paste, egui::Button::new(tl!("Paste")).shortcut_text("Ctrl+V")).clicked() {
- *action = Some(PageMenuAction::Paste(page, at));
- ui.close();
- }
-
- ui.separator();
-
- if ui.add_enabled(perms.can_annotate, egui::Button::new(tl!("Add comment"))).clicked() {
- *action = Some(PageMenuAction::AddComment(page, at));
- ui.close();
- }
- if perms.has_annotations && ui.add_enabled(perms.can_modify, egui::Button::new(tl!("Flatten comments"))).clicked() {
- *action = Some(PageMenuAction::FlattenComments);
- ui.close();
- }
- if ui.add_enabled(perms.can_assemble, egui::Button::new(tl!("Add bookmark"))).clicked() {
- *action = Some(PageMenuAction::AddBookmark(page));
- ui.close();
- }
- if ui.add_enabled(perms.can_copy, egui::Button::new(tl!("Take a snapshot"))).clicked() {
- *action = Some(PageMenuAction::TakeSnapshot);
- ui.close();
- }
-
- ui.separator();
-
- if ui.add_enabled(perms.can_modify, egui::Button::new(egui::RichText::new(tl!("Edit this PDF")).strong())).clicked() {
- *action = Some(PageMenuAction::EditPdf);
- ui.close();
- }
- if ui.add_enabled(perms.can_modify, egui::Button::new(tl!("Add text"))).clicked() {
- *action = Some(PageMenuAction::AddText);
- ui.close();
- }
- if ui.add_enabled(perms.can_modify, egui::Button::new(tl!("Add image"))).clicked() {
- *action = Some(PageMenuAction::AddImage);
- ui.close();
- }
- if ui.add_enabled(perms.can_assemble, egui::Button::new(tl!("Delete page"))).clicked() {
- *action = Some(PageMenuAction::DeletePage(page));
- ui.close();
- }
- if ui.add_enabled(perms.can_copy, egui::Button::new(tl!("Export this PDF"))).clicked() {
- *action = Some(PageMenuAction::ExportPdf);
- ui.close();
- }
-
- ui.separator();
-
- if ui.button(tl!("Find text or tools")).clicked() {
- *action = Some(PageMenuAction::Find);
- ui.close();
- }
- if ui.button(tl!("Rotate view clockwise")).clicked() {
- *action = Some(PageMenuAction::RotateCW(page));
- ui.close();
- }
- if ui.button(tl!("Document properties…")).clicked() {
- *action = Some(PageMenuAction::DocProperties);
- ui.close();
- }
--}
--
--impl DocView {
-- /// Whether a middle-button scrolling gesture is active (tests and automation).
-- pub fn auto_scrolling(&self) -> bool {
-- self.auto_scroll.active()
-- }
--
-- /// Whether a held middle-button pan is active, outside Linux (tests and automation).
-- pub fn middle_panning(&self) -> bool {
-- self.auto_scroll.panning()
-- }
--
-- /// Abandon only pointer previews: clearing egui's held button must not commit an app-owned
-- /// grab through a tool's `!primary_down` fallback. Keep selections, text drafts and edits
-- /// that were already completed before cancellation.
-- pub(crate) fn cancel_pointer_gestures(&mut self) {
-- self.marquee = None;
-- self.marquee_done = None;
-- self.comments.gesture = None;
-- self.links.drag = None;
-- self.sign.drag = None;
-- self.redact_drag = None;
-- self.crop_drag = None;
-- self.block_drag = None;
-- self.org_drag = None;
-- self.panel_drag = None;
-- self.selection_drag = false;
- self.router.cancel();
-- self.auto_scroll.cancel();
-- self.prepare.cancel_pointer_gesture();
-- self.content.cancel_pointer_gesture();
-- self.objects.cancel_pointer_gesture();
-- if let Some(image) = &mut self.image_selection {
-- image.cancel_pointer_gesture();
-- }
-- }
--
-- /// A view of a newly opened document, shown as `defaults` say. Opening a file then applies
-- /// any layout or zoom the PDF itself asks for.
-- pub fn new(id: DocId, info: &DocInfo, defaults: ViewDefaults) -> Self {
-- Self {
-- id,
-- zoom: defaults.zoom,
-- fit: defaults.fit,
-- layout: defaults.layout,
-- continuous: defaults.continuous,
-- show_page_gaps: defaults.show_page_gaps,
-- rotation: 0,
-- current: 0,
-- organize: false,
-- highlight_fields: defaults.highlight_fields,
-- page_input: "1".into(),
-- page_input_reselect: false,
-- notice_dismissed: false,
-- scripts_off_noted: false,
-- disk_changed: false,
-- close_requested: false,
-- snapshot: None,
-- cover: false,
-- back: Vec::new(),
-- forward: Vec::new(),
-- goto: None,
-- goto_point: None,
-- key_scroll: 0.0,
-- single_edges: None,
-- flash: None,
-- compare_marks: Vec::new(),
-- presentation: None,
-- pages: HashMap::new(),
-- errors: HashMap::new(),
-- waiting_since: HashMap::new(),
-- thumbs: HashMap::new(),
-- thumb_demand: HashMap::new(),
-- frame_queue: Vec::new(),
-- frame_geometry_changed: false,
-- frame_visible: HashSet::new(),
-- stale_thumbs: HashSet::new(),
-- stale_tiles: HashSet::new(),
-- current_scale_tag: 0,
-- print_pages: HashMap::new(),
-- grayscale: false,
-- tiles: HashMap::new(),
-- texts: HashMap::new(),
-- text_failed: HashSet::new(),
-- find: None,
-- selection: None,
-- all_text: false,
-- actions_dismissed: None,
-- selection_drag: false,
-- clicks: 0,
-- last_press_time: 0.0,
-- last_press_pos: None,
-- present_turn: None,
-- last_queue: Vec::new(),
-- hold_rasters: false,
-- viewport_w: 800.0,
-- viewport_h: 600.0,
-- page_count: info.pages.len(),
-- page_heights: info.pages.iter().map(|p| p.height).collect(),
-- screen_rects: Vec::new(),
-- screen_xforms: Vec::new(),
-- viewport_screen: Rect::NOTHING,
-- zoom_anchor: None,
-- shown: false,
-- auto_ocr_tried: Default::default(),
-- auto_ocr_skipped: Default::default(),
-- wheel: Default::default(),
-- precise_scroll_until: 0.0,
-- wheel_boost_x: Default::default(),
-- wheel_boost_y: Default::default(),
-- fling: Default::default(),
-- auto_scroll: Default::default(),
-- selected: BTreeSet::new(),
-- select_anchor: None,
-- organize_range: Default::default(),
-- pending_edit: None,
-- edit_lines: HashMap::new(),
-- line_editor: None,
-- applied_editor: None,
-- pending_text_open: None,
-- text_background: Default::default(),
-- move_background: Default::default(),
-- edit_images: HashMap::new(),
-- objects: Default::default(),
-- image_selection: None,
-- picked_image: None,
-- block_drag: None,
-- pending_action: None,
-- insert_at: None,
-- grid_gap: None,
-- pages_panel: None,
-- panel_gap: None,
-- panel_followed: None,
-- thumbnail_zoom: 1.0,
-- grid_zoom: 1.0,
-- grid_zoom_request: None,
-- grid_anchor: None,
-- grid_reveal: None,
-- grid_pages: HashMap::new(),
-- last_scale: 0.0,
-- sharp_hold_until: 0.0,
-- last_scroll: Vec2::ZERO,
-- sync_scroll: None,
-- page_widget: None,
-- pan_hold_until: 0.0,
-- slow_pages: HashSet::new(),
-- comments: Default::default(),
-- measure: Default::default(),
-- forms: Default::default(),
-- prepare: Default::default(),
-- calc_order: Default::default(),
-- content: Default::default(),
-- links: Default::default(),
-- redact_drag: None,
-- pending_redaction: None,
-- crop_drag: None,
-- camera: None,
-- camera_done: None,
-- sign: Default::default(),
-- org_drag: None,
-- panel_drag: None,
- pages_focus_id: None,
-- marquee: None,
-- marquee_done: None,
-- fill_text: None,
-- fill_signature_page: None,
-- signature_drag: Default::default(),
- cursor: Default::default(),
- router: Default::default(),
- press_cancelled: false,
-- }
-- }
--
-- /// The document's content or page list changed (an edit, undo or redo): drop caches and
-- /// adopt the new page geometry, keeping the reader's place where possible.
-- pub fn document_changed(&mut self, info: &DocInfo) {
-- self.invalidate_content();
-- self.frame_geometry_changed = true;
-- self.page_count = info.pages.len();
-- self.page_heights = info.pages.iter().map(|p| p.height).collect();
-- let last = self.page_count.saturating_sub(1);
-- self.current = self.current.min(last);
-- self.selected.retain(|p| *p <= last);
-- // Text being typed on a page that no longer exists goes with the page.
-- if self.content.draft.as_ref().is_some_and(|d| d.page > last || self.page_count == 0) {
-- self.content.draft = None;
-- }
-- if self.select_anchor.is_some_and(|a| a > last) {
-- self.select_anchor = None;
-- }
-- self.goto = None;
-- self.goto_point = None;
-- self.zoom_anchor = None;
-- self.flash = None;
-- // Thumbnail drags hold page indexes from before the change.
-- self.panel_drag = None;
-- self.org_drag = None;
-- self.reset_coarse_state();
-- }
--
-- /// The document was replaced by a newer version of its file (#431). The reader's place
-- /// stays: zoom, layout, rotation, page, scroll, full screen, view history and the find bar,
-- /// the snapshot marker and its pending close, and the page images on screen until their
-- /// replacements are drawn, so nothing flashes. Everything that pointed into the old version (a selected
-- /// comment, a field being typed in, text being edited, a drag, page selection) starts
-- /// afresh, so no stale index reaches the new one.
-- pub fn document_replaced(&mut self, info: &DocInfo) {
-- let mut fresh = Self::new(self.id, info, ViewDefaults::default());
-- fresh.zoom = self.zoom;
-- fresh.fit = self.fit;
-- fresh.layout = self.layout;
-- fresh.continuous = self.continuous;
-- fresh.show_page_gaps = self.show_page_gaps;
-- fresh.rotation = self.rotation;
-- fresh.current = self.current;
-- fresh.organize = self.organize;
-- fresh.grid_zoom = self.grid_zoom;
-- fresh.thumbnail_zoom = self.thumbnail_zoom;
-- // Same document id, so the scroll offset egui keeps for it is still this document's.
-- fresh.shown = self.shown;
-- fresh.highlight_fields = self.highlight_fields;
-- fresh.notice_dismissed = self.notice_dismissed;
-- fresh.snapshot = self.snapshot.clone();
-- fresh.close_requested = self.close_requested;
-- fresh.cover = self.cover;
-- // Still presenting, with the display to go back to on leaving.
-- fresh.presentation = self.presentation.take();
-- fresh.back = std::mem::take(&mut self.back);
-- fresh.forward = std::mem::take(&mut self.forward);
-- fresh.find = self.find.take();
-- fresh.pages = std::mem::take(&mut self.pages);
-- fresh.thumbs = std::mem::take(&mut self.thumbs);
-- fresh.viewport_w = self.viewport_w;
-- fresh.viewport_h = self.viewport_h;
-- fresh.viewport_screen = self.viewport_screen;
-- *self = fresh;
-- // Marks the kept images out of date and clamps the page to the new page count.
-- self.document_changed(info);
-- self.page_input = self.current_page_label(info.pages.get(self.current).map(|page| page.label.as_str()));
-- self.page_input_reselect = true;
-- }
--
-- /// Select pages in the organize grid (tests, automation). Empty clears the selection.
-- pub(crate) fn page_count(&self) -> usize {
-- self.page_count
-- }
--
-- /// Drops wheel state after a discontinuous jump so the destination cannot
-- /// inherit momentum from the previous viewport.
-- fn reset_coarse_state(&mut self) {
-- self.wheel.clear_motion();
-- self.wheel_boost_x.reset();
-- self.wheel_boost_y.reset();
-- }
--}
--
- /// Draws the document tab at `index`: its notice bars, then its pages (or the Organize grid) in a
- /// scroll area with this frame's zoom, scrolling, tool input, overlays and render requests.
- #[allow(clippy::too_many_arguments)]
- fn paint_image_suppressed(
- xf: &PageXform,
- painter: &egui::Painter,
- tex: egui::TextureId,
- u0: f32,
- v0: f32,
- u1: f32,
- v1: f32,
- suppress: Option<[f32; 4]>,
- is_full_page_tex: bool,
- ) {
- let Some([su0, sv0, su1, sv1]) = suppress else {
- if is_full_page_tex {
- xf.paint_page_texture_rect(painter, tex, u0, v0, u1, v1);
-/// The renders a frame wants (page, scale, tag, tile), and the pages visible.
-type PageRenders = (Vec<(usize, f32, u64, Option<Tile>)>, Vec<usize>);
-
-/// What the page-scroll closure reads: this frame's zoom, scrolling, tool and document state.
-#[derive(Clone, Copy)]
-struct PageEnv<'a> {
- added: &'a Vec<pdfcraft_engine::Added>,
- allowed: bool,
- author: &'a String,
- auto_delta: Vec2,
- avail: Rect,
- boost_extra: Vec2,
- by_line: &'a String,
- calc_order: bool,
- can_copy: bool,
- can_crop: bool,
- can_fill: bool,
- can_modify: bool,
- comments_hidden: bool,
- content_h: f32,
- content_w: f32,
- custom_stamp: &'a Option<crate::stamps_ui::CustomStamp>,
- date_text: &'a Result<String, String>,
- doc: &'a pdfcraft_engine::Document,
- doc_links: &'a Vec<pdfcraft_engine::LinkItem>,
- edge_delta: Vec2,
- editing_content: bool,
- form: &'a Arc<Vec<pdfcraft_engine::FormField>>,
- generation: u64,
- glide: Vec2,
- hand: bool,
- info: &'a DocInfo,
- initials: &'a Option<crate::fill_sign::SavedSig>,
- key_scroll: f32,
- middle_gesture: bool,
- modal: bool,
- navigation_enabled: bool,
- paper: Color32,
- ppp: f32,
- precise_scroll: Vec2,
- prefs: &'a comments::CommentPrefs,
- preparing: bool,
- rects: &'a Vec<Rect>,
- redact_mark: ([f64; 3], f64),
- scale: f32,
- selection_wheel: bool,
- selects_text: bool,
- signature: &'a Option<crate::fill_sign::SavedSig>,
- single_paging: bool,
- t: &'a Tokens,
- table_preview: &'a Option<crate::field_table_ui::Preview>,
- tag: u64,
- text_style: &'a pdfcraft_engine::AddedText,
- tool: QuickTool,
- units: crate::units::Unit,
- unobstructed: bool,
- visible_pages: &'a Vec<usize>,
- y_shift: f32,
- fill_text_size: f64,
- fill_text_color: [f64; 3],
- today: (i64, u32, u32),
-}
-
-/// What the page-scroll closure produces for the rest of the frame.
-#[derive(Default)]
-struct PageOut {
- canvas_action: Option<comments::CanvasAction>,
- clicked_link: Option<LinkTarget>,
- copy_selected: bool,
- field_menu: Option<FieldMenu>,
- field_placed: bool,
- field_props: bool,
- hover_text: Option<(Pos2, String)>,
- image_action: Option<crate::edit_text_ui::ImageAction>,
- open_boxes: bool,
- open_initials: bool,
- open_signature: bool,
- open_table: Option<(usize, [f64; 4])>,
- primary_down: bool,
- refused: Option<String>,
- stamp_placed: bool,
-}
-
-/// The page-scroll closure's body: lays out and draws the visible pages, their tools and overlays, and
-/// returns the renders wanted and the pages visible.
-/// The state of one pass over the visible pages: what is read, and what the pages add up to.
-struct PageFrame<'a> {
- env: PageEnv<'a>,
- resp: &'a egui::Response,
- ptr: &'a PagePointer,
- origin: Pos2,
- visible: Rect,
- pressed: bool,
- pointer: Option<Pos2>,
- signature_preview: &'a mut Option<(crate::fill_sign::SavedSig, egui::TextureHandle)>,
- out: PageOut,
- wanted: Vec<(usize, f32, u64, Option<Tile>)>,
- visible_now: Vec<usize>,
- current: usize,
- best_overlap: f32,
- current_overlap: f32,
- current_height: f32,
- pressed_page: Option<usize>,
- calc_hover: Option<crate::calc_order_ui::Focus>,
-}
-
-/// Visible heights (points) closer than this are equal: one page size can meet the viewport a
-/// fraction of a point differently once the scroll offset is applied.
-const PAGE_TIE: f32 = 0.5;
-
-/// Draws page `i` of the visible pages: its raster, the tools' input and overlays.
-/// One visible page: its index, its rectangle on screen and the transform to and from the page.
-#[derive(Clone, Copy)]
-struct PageSlot {
- i: usize,
- r: Rect,
- xf: PageXform,
-}
-
-/// Paints the page's paper, shadow and raster (or why it can't be shown), and asks for the renders
-/// it wants.
-fn paint_page_surface(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot) {
- let PageEnv { paper, ppp, scale, t, tag, .. } = frame.env;
- let PageSlot { i, r, xf } = *slot;
- let visible = frame.visible;
- let info = frame.env.info;
- let painter = ui.painter();
- // Shadows would darken the preceding page when pages touch.
- if view.show_page_gaps {
- painter.add(egui::epaint::Shadow { offset: [0, 3], blur: 14, spread: 0, color: t.page_shadow }.as_shape(r, CornerRadius::ZERO));
- }
- painter.rect_filled(r, CornerRadius::ZERO, paper);
- if let Some(err) = view.errors.get(&i) {
- painter.rect_filled(r, CornerRadius::ZERO, Color32::from_rgb(0xFB, 0xF4, 0xF4));
- icons::paint(
- ui,
- Rect::from_center_size(r.center() - vec2(0.0, 26.0), vec2(28.0, 28.0)),
- "triangle-alert",
- 26.0,
- Color32::from_rgb(0xC8, 0x3A, 0x3A),
- );
- let message = crate::i18n::fmt(tl!("This page couldn't be displayed.\n{e}"), &[("e", err)]);
- let msg = ui.fonts_mut(|f| f.layout(message, theme::regular(12.5), Color32::from_rgb(0x6A, 0x2A, 0x2A), (r.width() - 40.0).max(80.0)));
- painter.galley(pos2(r.center().x - msg.size().x / 2.0, r.center().y), msg, Color32::BLACK);
- } else {
- let (pw_pt, ph_pt) = (info.pages[i].width.max(1.0), info.pages[i].height.max(1.0));
- // A GPU may take smaller textures than these (OpenGL drivers report as little as
- // 2048 pixels, and uploading a bigger one aborts), so both stay within its limit.
- let max_side = ui.ctx().input(|inp| inp.max_texture_side) as f32;
- let tiled = pw_pt.max(ph_pt) * scale > TILE_THRESHOLD.min(max_side);
- // Whole-page raster: sharp when small, a low-res backdrop when tiled.
- let (want_scale, want_tag) = if tiled {
- let bs = BASE_SIDE.min(max_side) / pw_pt.max(ph_pt);
- (bs, scale_tag(bs))
-- } else {
- xf.paint_image(painter, tex, u0, v0, u1, v1);
- (scale, tag)
- };
- // Under tiles, a whole-page raster sharper than the backdrop (from before the page
- // grew past the tiling size) is a better backdrop: keep demanding it as it is.
- match view.pages.get(&i).filter(|p| tiled && p.tag != STALE_TAG && tag_scale(p.tag) >= want_scale) {
- Some(p) => frame.wanted.push((i, tag_scale(p.tag), p.tag, None)),
- None => frame.wanted.push((i, want_scale, want_tag, None)),
-- }
- return;
- match view.pages.get(&i) {
- Some(p) => {
- if !tiled && p.tag == want_tag {
- xf.texel_aligned(p.tex.size(), ppp).paint_image(painter, p.tex.id(), 0.0, 0.0, 1.0, 1.0);
- } else {
- // A backdrop, or a raster at an older scale until the new one arrives.
- xf.paint_image(painter, p.tex.id(), 0.0, 0.0, 1.0, 1.0);
- }
- }
- None => {
- let now = ui.input(|inp| inp.time);
- let since = *view.waiting_since.entry(i).or_insert(now);
- let msg = if now - since > 6.0 { "Still rendering — this page is unusually complex…" } else { "Rendering…" };
- painter.text(r.center(), Align2::CENTER_CENTER, tl!(msg), theme::regular(12.0), t.text_faint);
- }
- }
- if tiled && r.intersects(visible) {
- paint_page_tiles(ui, view, frame, slot, (pw_pt, ph_pt, max_side));
- }
- }
-}
-
-/// Paints the tiles of a page too big to draw whole: the tiles of earlier zooms first, stretched,
-/// then the visible tiles of this one, nearest the viewport centre first, asking for those missing.
-fn paint_page_tiles(ui: &egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot, (pw_pt, ph_pt, max_side): (f32, f32, f32)) {
- let PageEnv { ppp, scale, tag, .. } = frame.env;
- let PageSlot { i, r, xf } = *slot;
- let visible = frame.visible;
- let painter = ui.painter();
- // Device-pixel geometry of the scaled page, and the visible part of it
- // (found by mapping the visible screen corners back into the page). Sides round
- // up as whole-page rasters do, so the last partial row and column get tiles.
- let (dw, dh) = (device_pixels(pw_pt, scale), device_pixels(ph_pt, scale));
- // Adaptive tile side for this zoom and page, within the GPU texture limit:
- // a zoomed viewport or a giant sheet is covered by a few large tiles
- // instead of many small ones, and each tile costs one full page interpret.
- // Never zero (u32 division below).
- let ts = tile_side(scale, dw.max(dh)).min(max_side as u32).max(64);
- let vis = r.intersect(visible);
- let corners = [vis.left_top(), vis.right_top(), vis.right_bottom(), vis.left_bottom()].map(|c| xf.screen_to_norm(c));
- let (u0, u1) = corners.iter().fold((1.0f32, 0.0f32), |(a, b), c| (a.min(c.0), b.max(c.0)));
- let (v0, v1) = corners.iter().fold((1.0f32, 0.0f32), |(a, b), c| (a.min(c.1), b.max(c.1)));
- let (vx0, vy0) = ((u0.max(0.0) * dw as f32) as u32, (v0.max(0.0) * dh as f32) as u32);
- let (vx1, vy1) = (((u1.min(1.0) * dw as f32).ceil() as u32).min(dw), ((v1.min(1.0) * dh as f32).ceil() as u32).min(dh));
- // Tiles of earlier zooms first, stretched, the nearest scale last (on top):
- // until the current tiles arrive, a zoom shows them rather than the backdrop.
- let mut older: Vec<(f32, u32, u32, &TextureHandle)> =
- view.tiles.iter().filter(|((p, t, _, _), _)| *p == i && *t != tag).map(|((_, t, ox, oy), tex)| (tag_scale(*t), *ox, *oy, tex)).collect();
- older.sort_by(|a, b| (b.0 / scale).ln().abs().total_cmp(&(a.0 / scale).ln().abs()));
- for (s, ox, oy, tex) in older {
- let (ow, oh) = (device_pixels(pw_pt, s) as f32, device_pixels(ph_pt, s) as f32);
- let [w, h] = tex.size();
- let (x, y) = (ox as f32, oy as f32);
- xf.paint_image(painter, tex.id(), x / ow, y / oh, (x + w as f32) / ow, (y + h as f32) / oh);
- }
- // Visible tiles, nearest the viewport centre first: each tile costs a full
- // page interpret, so the workers sharpen what is looked at before the edges.
- let (vcx, vcy) = ((vx0 + vx1) as f32 / 2.0, (vy0 + vy1) as f32 / 2.0);
- let mut coords: Vec<(u32, u32)> = Vec::new();
- for ty in vy0 / ts..=(vy1.saturating_sub(1)) / ts {
- for tx in vx0 / ts..=(vx1.saturating_sub(1)) / ts {
- coords.push((tx, ty));
- }
- }
- coords.sort_by(|a, b| {
- let da = ((a.0 * ts + ts / 2) as f32 - vcx).abs() + ((a.1 * ts + ts / 2) as f32 - vcy).abs();
- let db = ((b.0 * ts + ts / 2) as f32 - vcx).abs() + ((b.1 * ts + ts / 2) as f32 - vcy).abs();
- da.total_cmp(&db)
- });
- for (tx, ty) in coords {
- let (x, y) = (tx * ts, ty * ts);
- let (w, h) = (ts.min(dw.saturating_sub(x)), ts.min(dh.saturating_sub(y)));
- if w == 0 || h == 0 {
- continue;
- }
- frame.wanted.push((i, scale, tag, Some(Tile { x, y, w, h })));
- if let Some(tex) = view.tiles.get(&(i, tag, x, y)) {
- let (fw, fh) = (dw as f32, dh as f32);
- xf.texel_aligned([dw as usize, dh as usize], ppp).paint_image(
- painter,
- tex.id(),
- x as f32 / fw,
- y as f32 / fh,
- (x + w) as f32 / fw,
- (y + h) as f32 / fh,
- );
- }
- }
-}
-
-/// What the page's tools did with the pointer this frame.
-struct PageInput {
- /// A tool, a field or a comment took the pointer: text selection must leave it alone.
- consumed: bool,
- /// Redact or Highlight draws boxes off text.
- area_tool: bool,
-}
-
-/// Common geometry and pointer state for a tool acting on one page.
-#[derive(Clone, Copy)]
-pub(crate) struct PageToolContext<'a> {
- pub ui: &'a egui::Ui,
- pub resp: &'a egui::Response,
- pub xf: &'a PageXform,
- pub ptr: &'a PagePointer,
- pub page: usize,
- pub doc: &'a pdfcraft_engine::Document,
- pub info: &'a DocInfo,
-}
-
-/// Comments: tools, selection, moving and resizing come before text selection. Form fields take
-/// clicks first with the Select tool (as Acrobat fills fields in every viewing mode); then
-/// comments; then text selection.
-fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot) -> PageInput {
- let PageEnv {
- added,
- allowed,
- author,
- by_line,
- can_crop,
- can_fill,
- can_modify,
- comments_hidden,
- custom_stamp,
- date_text,
- doc,
- doc_links,
- editing_content,
- form,
- info,
- initials,
- prefs,
- preparing,
- redact_mark,
- signature,
- table_preview,
- text_style,
- tool,
- units,
- fill_text_size,
- fill_text_color,
- ..
- } = frame.env;
- let PageSlot { i, xf, .. } = *slot;
- let PageFrame { resp, ptr, pointer, .. } = *frame;
- let cx = PageToolContext { ui, resp, xf: &xf, ptr, page: i, doc, info };
- let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
- let (lines, images) =
- if matches!(tool, QuickTool::AddText | QuickTool::EditText) && can_modify { edit_inventory(view, doc, i) } else { Default::default() };
- let field_tool = match tool {
- QuickTool::Field(f) => Some(f),
- _ => None,
-- };
- let iu0 = u0.max(su0);
- let iu1 = u1.min(su1);
- let iv0 = v0.max(sv0);
- let iv1 = v1.min(sv1);
- if iu0 >= iu1 || iv0 >= iv1 {
- if is_full_page_tex {
- xf.paint_page_texture_rect(painter, tex, u0, v0, u1, v1);
- } else {
- xf.paint_image(painter, tex, u0, v0, u1, v1);
- // While a table is being set up, nothing else is drawn or moved.
- let prepare_allowed = can_modify && table_preview.is_none() && frame.out.open_table.is_none();
- let hits = PageHits {
- page: i,
- xf: &xf,
- info,
- tool,
- pcx: &pcx,
- form,
- can_fill,
- can_modify,
- editing_content,
- preparing,
- field_tool,
- prepare_allowed,
- added,
- lines: &lines,
- images: &images,
- clip: ui.clip_rect(),
- };
- if let Some(o) = ptr.pressed().filter(|o| xf.rect.contains(*o)) {
- let at = hits.at(view, o);
- view.router.press(i, at, tool);
- }
- if let QuickTool::Measure(measure_tool) = tool {
- crate::measure_ui::page_input(ui, resp, doc, view, &pcx, measure_tool, units);
- }
-
- // Form fields take clicks first with the Select tool (as Acrobat fills fields in
- // every viewing mode); then comments; then text selection.
- if let QuickTool::Stamp(kind) = tool
- && allowed
- && let Some(p) = ptr.within(xf.rect)
- {
- view.cursor.claim(Layer::ToolArea, egui::CursorIcon::Crosshair);
- // The tool's own click: nothing drawn over the page here holds the press.
- if resp.clicked() && view.router.owner(i).is_none() {
- // Centred on the click, upright as the page is shown.
- let (vx, vy) = xf.screen_to_view(p);
- let (w, h) = kind.size();
- let corners = [(vx as f64 - w / 2.0, vy as f64 - h / 2.0), (vx as f64 + w / 2.0, vy as f64 + h / 2.0)];
- let u: Vec<[f32; 2]> = corners.iter().map(|(x, y)| info.pages[i].view_to_user(*x as f32, *y as f32)).collect();
- let rect = [u[0][0].min(u[1][0]) as f64, u[0][1].min(u[1][1]) as f64, u[0][0].max(u[1][0]) as f64, u[0][1].max(u[1][1]) as f64];
- let by = (kind.group() == pdfcraft_engine::StampGroup::Dynamic).then(|| by_line.clone());
- let shape = pdfcraft_engine::Shape::Stamp { rect, stamp: kind, by };
- view.pending_edit = Some(pdfcraft_engine::Edit::AddAnnotation(pdfcraft_engine::NewAnnotation {
- page: i,
- style: pdfcraft_engine::Style::default_for(&shape),
- shape,
- contents: String::new(),
- author: author.clone(),
- }));
- frame.out.stamp_placed = true;
-- }
- return;
-- }
- let map_uv = |pu: f32, pv: f32| -> (f32, f32) {
- if is_full_page_tex {
- (pu, pv)
- } else {
- let du = (u1 - u0).max(1e-6);
- let dv = (v1 - v0).max(1e-6);
- ((pu - u0) / du, (pv - v0) / dv)
- if let Some(cs) = custom_stamp.as_ref()
- && allowed
- && let Some(p) = ptr.within(xf.rect)
- {
- view.cursor.claim(Layer::ToolArea, egui::CursorIcon::Crosshair);
- // The tool's own click: nothing drawn over the page here holds the press.
- if resp.clicked() && view.router.owner(i).is_none() {
- // Centred on the click at its natural size (the engine sizes it).
- let (vx, vy) = xf.screen_to_view(p);
- let u = info.pages[i].view_to_user(vx, vy);
- let (x, y) = (u[0] as f64, u[1] as f64);
- view.pending_edit = Some(pdfcraft_engine::Edit::AddCustomStamp {
- page: i,
- rect: [x, y, x, y],
- name: cs.name.clone(),
- file: pdfcraft_engine::MarkFile { name: cs.file.clone(), bytes: cs.data.clone(), page: cs.page },
- author: author.clone(),
- });
- frame.out.stamp_placed = true;
- }
- }
- // Over a mark already placed, the Fill & Sign tool picks it up (comments, below).
- if let QuickTool::Fill(ft) = tool
- && allowed
- && !comments::fill_grabs(&pcx, view)
- {
- match crate::fill_sign::page_input(
- cx,
- crate::fill_sign::PageInput {
- tool: ft,
- text_size: fill_text_size,
- text_color: fill_text_color,
- signature: signature.as_ref(),
- initials: initials.as_ref(),
- preview: frame.signature_preview,
- author,
- date_text,
- },
- view,
- ) {
- Some(crate::fill_sign::FillAction::Edit(e)) => view.pending_edit = Some(*e),
- Some(crate::fill_sign::FillAction::Signature(e)) => {
- view.pending_edit = Some(*e);
- view.fill_signature_page = Some(i);
- }
- Some(crate::fill_sign::FillAction::CreateSignature) => frame.out.open_signature = true,
- Some(crate::fill_sign::FillAction::CreateInitials) => frame.out.open_initials = true,
- Some(crate::fill_sign::FillAction::Refused(why)) => frame.out.refused = Some(why),
- None => {}
-- }
- }
- if matches!(tool, QuickTool::SignArea { .. }) {
- crate::sign_ui::page_input(ui, resp, &xf, ptr, i, info, view);
- }
- if matches!(tool, QuickTool::MarqueeZoom | QuickTool::Snapshot) {
- crate::zoom_snap::page_input(ui, resp, &xf, ptr, i, view);
- }
- if tool == QuickTool::EnhanceCamera {
- crate::camera_ui::page_input(resp, &xf, ptr, i, view, can_modify);
- }
- if tool == QuickTool::Crop
- && crate::crop::page_input(ui, crate::crop::CropPageInput { resp, xf: &xf, ptr, page: i, info, view, allowed: can_crop })
- {
- view.current = i;
- frame.out.open_boxes = true;
- }
- let on_field = if preparing {
- let o = crate::prepare::page_input(cx, form, field_tool, prepare_allowed, view);
- frame.out.field_props |= o.properties;
- frame.out.field_placed |= o.placed;
- frame.out.open_table = frame.out.open_table.or(o.table);
- o.consumed || field_tool.is_some()
- } else {
- tool == QuickTool::Select && crate::forms_ui::page_input(cx, form, can_fill, view)
-- };
- let draw_strip = |pu0: f32, pv0: f32, pu1: f32, pv1: f32| {
- if pu1 <= pu0 || pv1 <= pv0 {
- return;
- // Redact draws boxes off text; so does Highlight (an area highlight, as in Acrobat).
- let area_tool = tool == QuickTool::Redact && can_modify || tool == QuickTool::Comment(comments::CommentTool::Highlight) && allowed;
- let boxing = area_tool && {
- let text = view.page_text(i);
- let over_text = |p: Pos2| {
- let (vx, vy) = xf.screen_to_view(p);
- text.as_ref().is_some_and(|t| t.glyphs.iter().any(|g| vx >= g.rect[0] && vx <= g.rect[2] && vy >= g.rect[1] && vy <= g.rect[3]))
- };
- crate::redact_ui::page_input(cx, over_text, view)
- };
- let on_objects = tool == QuickTool::EditText && can_modify && crate::object_ui::page_input(cx, view);
- // Add text acts on the page's own paragraphs over them, and on the one its press was on until
- // the button is up: a drag that leaves the box still ends, and no new box takes it over.
- let over_source = tool == QuickTool::AddText
- && (matches!(view.router.owner(i), Some(crate::page_router::Owner::Paragraph { .. }))
- || pointer.is_some_and(|p| {
- !crate::content_ui::hits_added(&xf, info, i, added, p) && crate::edit_text_ui::over_paragraph(&xf, info, i, &lines, p)
- }));
- let source_blocked = over_source && view.content.hold_blocked();
- if over_source && resp.clicked() && !source_blocked {
- if let Some(edit) = view.content.finish_draft(added) {
- view.pending_edit = Some(edit);
-- }
- let (tu0, tv0) = map_uv(pu0, pv0);
- let (tu1, tv1) = map_uv(pu1, pv1);
- xf.paint_image_region(painter, tex, pu0, pv0, pu1, pv1, tu0, tv0, tu1, tv1);
- view.content.selected = None;
- }
- let on_content = !on_objects && editing_content && can_modify && {
- source_blocked
- || crate::content_ui::page_input(
- cx,
- added,
- tool == QuickTool::AddText && !over_source,
- matches!(tool, QuickTool::AddText | QuickTool::EditText),
- text_style,
- view,
- )
- || tool == QuickTool::AddText && !over_source
- };
- // Added content owns its gesture; its source text must not queue a second edit.
- // Edit text & images boxes every paragraph and image whenever the tool is on. Only the
- // input waits while selected objects or added content own the pointer, or while page
- // input is blocked (another tab, a dialog, the window unfocused): the boxes stay drawn
- // instead of vanishing until the pointer comes back.
- let others_own_pointer = on_objects || on_content;
- let on_edit_text = (tool == QuickTool::EditText || over_source) && can_modify && {
- // Editable text takes precedence over background/overlapping artwork. Both
- // inventories are painted every frame, even when text owns the pointer.
- let text_used = crate::edit_text_ui::page_input(cx, &lines, view, !others_own_pointer);
- let image_cx = crate::edit_text_ui::ImageCx {
- ui,
- resp,
- xf: &xf,
- ptr,
- page: i,
- info,
- allow_interaction: !others_own_pointer && (matches!(view.router.owner(i), Some(crate::page_router::Owner::Picture { .. })) || !text_used),
- };
- let image_used = crate::edit_text_ui::image_input(&image_cx, &images, view, &mut frame.out.image_action);
- !others_own_pointer && (text_used || image_used)
-- };
- let on_link = tool == QuickTool::Link && can_modify && crate::link_ui::page_input(cx, doc_links, view);
- if tool == QuickTool::Select
- && resp.secondary_clicked()
- && can_modify
- && let Some(p) = pointer.filter(|p| xf.rect.contains(*p))
- && let Some(l) = doc.links().iter().find(|l| l.page == i && crate::link_ui::screen_rect(&xf, info, i, l.rect).expand(2.0).contains(p))
- {
- view.links.context = Some((i, l.index));
- }
- let consumed = on_objects || on_edit_text || on_link || on_content || boxing || on_field || comments::page_input(ui, resp, &pcx, view);
- // The cursor and the tip from what lies under the pointer, after the tools claimed theirs (the
- // last claim on a layer stands).
- if frame.env.unobstructed
- && let Some(p) = pointer
- {
- for h in hits.at(view, p) {
- view.cursor.claim_with_tip(h.layer, h.icon, h.tip);
- }
- }
- PageInput { consumed, area_tool }
-}
--
- if iv0 > v0 {
- draw_strip(u0, v0, u1, iv0);
-/// Edit text's paragraphs and pictures on page `i`, kept while the document is unchanged.
-fn edit_inventory(
- view: &mut DocView,
- doc: &pdfcraft_engine::Document,
- i: usize,
-) -> (Vec<pdfcraft_engine::TextBlock>, Vec<pdfcraft_engine::PageImage>) {
- let generation = doc.edit_generation();
- let lines = match view.edit_lines.get(&i) {
- Some((g, l)) if *g == generation => l.clone(),
- _ => {
- let l = doc.text_blocks(i);
- view.edit_lines.insert(i, (generation, l.clone()));
- l
- }
- };
- let images = match view.edit_images.get(&i) {
- Some((g, l)) if *g == generation => l.clone(),
- _ => {
- let l = doc.page_images(i);
- view.edit_images.insert(i, (generation, l.clone()));
- l
- }
- };
- (lines, images)
-}
-
-/// What the page modules have under a point on one page, for the page router.
-struct PageHits<'a> {
- page: usize,
- xf: &'a PageXform,
- info: &'a DocInfo,
- tool: QuickTool,
- pcx: &'a comments::PageCx<'a>,
- form: &'a [pdfcraft_engine::FormField],
- can_fill: bool,
- can_modify: bool,
- /// Edit a PDF is open: added text and pictures can be picked up.
- editing_content: bool,
- preparing: bool,
- field_tool: Option<crate::prepare::FieldTool>,
- /// Prepare may draw and move fields (not while a table is being set up).
- prepare_allowed: bool,
- added: &'a [pdfcraft_engine::Added],
- lines: &'a [pdfcraft_engine::TextBlock],
- images: &'a [pdfcraft_engine::PageImage],
- /// The pages' visible area (the picture's rotate knob stays inside it).
- clip: Rect,
-}
-
-impl PageHits<'_> {
- /// The hits at `p`, bottom up: links, comments; Edit text's pictures under its paragraphs (text
- /// on top of artwork takes the press), added content, the objects selected together and the
- /// open paragraph's frame, the selected picture's grips over them all; then form fields
- /// (Prepare's, being moved, over the comments; filled ones under added content and the
- /// comments that move).
- fn at(&self, view: &DocView, p: Pos2) -> Vec<crate::page_router::Hit> {
- let PageHits {
- page,
- xf,
- info,
- tool,
- pcx,
- form,
- can_fill,
- can_modify,
- editing_content,
- preparing,
- field_tool,
- prepare_allowed,
- added,
- lines,
- images,
- clip,
- } = *self;
- let fields = if preparing {
- crate::prepare::hit(xf, info, page, form, field_tool, prepare_allowed, &view.prepare, p)
- } else if tool == QuickTool::Select {
- crate::forms_ui::hit(xf, info, page, form, can_fill, p)
- } else {
- None
- };
- // With Add text, only the paragraphs and pictures not under added content.
- let editing =
- can_modify && (tool == QuickTool::EditText || tool == QuickTool::AddText && !crate::content_ui::hits_added(xf, info, page, added, p));
- let (pictures, paragraphs, objects, grips) = if editing {
- let open = view.line_editor.as_ref().filter(|e| e.page == page);
- let frame = open
- .filter(|e| !e.has_changes())
- .and_then(|e| lines.get(e.block))
- .map(|l| xf.user_rect(info, page, l.rect.map(|v| v as f32)).expand(2.0));
- // Add text acts on the page's paragraphs only in their boxes, edges included (it
- // finishes the box being typed there, `over_source`): not on pictures, nor beside a
- // paragraph, where a click starts a new box.
- let edit_text = tool == QuickTool::EditText;
- let on_paragraphs = edit_text || crate::edit_text_ui::over_paragraph(xf, info, page, lines, p);
- (
- edit_text.then(|| crate::edit_text_ui::picture_hit(xf, page, info, images, view, p)).flatten(),
- on_paragraphs.then(|| crate::edit_text_ui::hit(xf, info, page, lines, open.map(|e| e.block), p)).flatten(),
- edit_text.then(|| crate::object_ui::hit(view, xf, info, page, frame, p)).flatten(),
- edit_text.then(|| crate::edit_text_ui::picture_handle_hit(clip, xf, page, info, images, view, p)).flatten(),
- )
- } else {
- (None, None, None, None)
- };
- // Added text and pictures, over the page's own paragraphs and under the objects selected
- // together.
- let picks_up = matches!(tool, QuickTool::Select | QuickTool::AddText | QuickTool::EditText);
- let added = (editing_content && can_modify && picks_up).then(|| crate::content_ui::hit(xf, info, page, added, view, p)).flatten();
- [link_hit(info, xf, page, p), comments::hit(pcx, view, p), pictures, paragraphs, added, objects, grips, fields]
- .into_iter()
- .flatten()
- .collect()
-- }
- if v1 > iv1 {
- draw_strip(u0, iv1, u1, v1);
-}
-
-/// Text layer: find matches, selection, I-beam and drag-to-select.
-fn text_layer(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot, input: &PageInput) {
- let PageEnv { info, selects_text, tool, .. } = frame.env;
- let PageSlot { i, r, xf } = *slot;
- let PageFrame { resp, ptr, pointer, .. } = *frame;
- if let Some(text) = view.texts.get(&i).cloned() {
- paint_text_marks(ui, view, &text, i, xf, resp.dragged());
- if selects_text
- && !input.consumed
- && let Some(p) = pointer.filter(|p| r.contains(*p))
- {
- text_pointer_input(ui, view, &text, TextPointer { page: i, xf, rect: r, info, tool, resp, ptr: ptr.clone(), pointer: p });
- }
-- }
- if iu0 > u0 {
- draw_strip(u0, iv0, iu0, iv1);
-}
-
-fn paint_text_marks(ui: &egui::Ui, view: &DocView, text: &PageText, page: usize, xf: PageXform, dragging: bool) {
- let painter = ui.painter();
- let to_screen = |g: [f32; 4]| xf.view_rect(g);
- if let Some(find) = &view.find {
- for (index, (match_page, range)) in find.matches.iter().enumerate() {
- if *match_page != page {
- continue;
- }
- let fill = if find.current == Some(index) {
- Color32::from_rgba_unmultiplied(255, 140, 0, 110)
- } else {
- Color32::from_rgba_unmultiplied(255, 214, 0, 90)
- };
- for line_rect in text.line_rects(range.clone()) {
- painter.rect_filled(to_screen(line_rect).expand(1.0), CornerRadius::same(2), fill);
- }
- }
-- }
- if u1 > iu1 {
- draw_strip(iu1, iv0, u1, iv1);
- if view.all_text {
- for line_rect in text.line_rects(0..text.glyphs.len()) {
- painter.rect_filled(to_screen(line_rect), CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x3A, 0x7B, 0xF0, 70));
- }
- }
- if let Some(selection) = view.selection.filter(|selection| selection.page == page) {
- for line_rect in selection.rects(text) {
- painter.rect_filled(to_screen(line_rect), CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x3A, 0x7B, 0xF0, 70));
- }
- if let Span::Column { rect } = selection.span
- && dragging
- {
- let outline = Stroke::new(1.0, Color32::from_rgb(0x3A, 0x7B, 0xF0));
- painter.rect_stroke(to_screen(rect), CornerRadius::ZERO, outline, egui::StrokeKind::Middle);
- }
-- }
--}
--
- pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
- let t = Tokens::get(ui.ctx());
- // Keyboard scrolling applies to this frame's page view only, never later.
- let key_scroll = std::mem::take(&mut app.views[index].key_scroll);
- if key_scroll != 0.0 {
- app.views[index].end_find_following();
-struct TextPointer<'a> {
- page: usize,
- xf: PageXform,
- rect: Rect,
- info: &'a DocInfo,
- tool: QuickTool,
- resp: &'a egui::Response,
- ptr: PagePointer,
- pointer: Pos2,
-}
-
-struct TextGesture<'a> {
- page: usize,
- xf: PageXform,
- rect: Rect,
- tool: QuickTool,
- ptr: PagePointer,
- resp: &'a egui::Response,
- pointer: Pos2,
- vx: f32,
- vy: f32,
- over_text: bool,
- over_link: bool,
- column: bool,
-}
-
-fn text_pointer_input(ui: &mut egui::Ui, view: &mut DocView, text: &PageText, pointer: TextPointer<'_>) {
- let TextPointer { page, xf, rect, info, tool, resp, ptr, pointer: screen_pointer } = pointer;
- let (vx, vy) = xf.screen_to_view(screen_pointer);
- let over_text = text.glyphs.iter().any(|glyph| vx >= glyph.rect[0] && vx <= glyph.rect[2] && vy >= glyph.rect[1] && vy <= glyph.rect[3]);
- let over_link = info.links.iter().any(|link| link.page == page && xf.user_rect(info, page, link.rect).contains(screen_pointer));
- let column = tool == QuickTool::ColumnSelect || ui.input(|input| input.modifiers.alt);
- let gesture = TextGesture { page, xf, rect, tool, ptr, resp, pointer: screen_pointer, vx, vy, over_text, over_link, column };
- update_text_cursor(view, &gesture);
- update_selection_on_press(ui, view, text, &gesture);
- update_dragged_selection(view, text, &gesture);
- finish_text_pointer_gesture(ui, view, text, &gesture);
-}
-
-fn update_text_cursor(view: &mut DocView, gesture: &TextGesture<'_>) {
- // A press on a link selects nothing (it is the link's), so no cursor here says it would.
- if gesture.column && !gesture.over_link {
- view.cursor.claim(Layer::Text, egui::CursorIcon::Crosshair);
- } else if gesture.over_text && !gesture.over_link {
- view.cursor.claim(Layer::Text, egui::CursorIcon::Text);
-- }
- // The Search panel closed: its search moves to the find bar.
- let search_open = app.right == Some(crate::RightPanel::Search);
- if let Some(f) = app.views[index].find.as_mut()
- && f.in_panel
- && !search_open
-}
-
-fn update_selection_on_press(ui: &egui::Ui, view: &mut DocView, text: &PageText, gesture: &TextGesture<'_>) {
- // A press held by what keeps its drag (a link, a comment that moves) selects no text; one on
- // text markup lets its drag select the text underneath.
- let press_here = gesture.ptr.origin.is_some_and(|origin| gesture.rect.contains(origin));
- if gesture.ptr.pressed().is_some() && press_here && !view.router.keeps_drag() {
- let now = ui.input(|input| input.time);
- let (max_delay, max_dist) = ui.ctx().options(|options| (options.input_options.max_double_click_delay, options.input_options.max_click_dist));
- let consecutive = now - view.last_press_time <= max_delay && view.last_press_pos.is_some_and(|pos| pos.distance(gesture.pointer) <= max_dist);
- view.clicks = if consecutive { view.clicks.saturating_add(1) } else { 1 };
- view.last_press_time = now;
- view.last_press_pos = Some(gesture.pointer);
- let clicks = selection_click_count(view.clicks, gesture.tool);
- if clicks >= 2
- && gesture.over_text
- && let Some((first, last)) = text.nearest(gesture.vx, gesture.vy).and_then(|anchor| click_unit(text, clicks, anchor))
- {
- view.selection = Some(Selection::run(gesture.page, first, last));
- }
- }
- if gesture.resp.drag_started() && press_here && !view.router.keeps_drag() {
- view.actions_dismissed = None;
- view.selection_drag = true;
- let origin = gesture.ptr.origin.unwrap_or(gesture.pointer);
- let (ox, oy) = gesture.xf.screen_to_view(origin);
- if view.clicks < 2 {
- view.selection = if gesture.column {
- Some(Selection { page: gesture.page, span: Span::Column { rect: [ox, oy, gesture.vx, gesture.vy] } })
- } else {
- text.nearest(ox, oy).map(|anchor| Selection::run(gesture.page, anchor, anchor))
- };
- }
- }
-}
-
-fn selection_click_count(clicks: u32, tool: QuickTool) -> u32 {
- if clicks > 2 && tool != QuickTool::Select { 1 } else { clicks }
-}
-
-fn update_dragged_selection(view: &mut DocView, text: &PageText, gesture: &TextGesture<'_>) {
- let clicks = selection_click_count(view.clicks, gesture.tool);
- if gesture.resp.dragged()
- && let Some(selection) = view.selection.as_mut().filter(|selection| selection.page == gesture.page)
-- {
- f.in_panel = false;
- match &mut selection.span {
- Span::Run { anchor, head } if clicks >= 2 => {
- if let Some(hit) = text.nearest(gesture.vx, gesture.vy)
- && let Some((first, last)) = click_unit(text, clicks, *anchor)
- {
- let (to_first, to_last) = click_unit(text, clicks, hit).unwrap_or((hit, hit));
- (*anchor, *head) = if to_first < first { (last, to_first) } else { (first, to_last.max(last)) };
- }
- }
- Span::Run { head, .. } => {
- if let Some(hit) = text.nearest(gesture.vx, gesture.vy) {
- *head = hit;
- }
- }
- Span::Column { rect } => {
- rect[2] = gesture.vx;
- rect[3] = gesture.vy;
- }
- }
-- }
- // The snapshot notice bar's Close was clicked: close here, where no document borrow is
- // held. The tab is looked up by id: indices may have shifted since the click, and the
- // flag rides its own view, so a stale flag can never close the wrong tab.
- let pending = app.views.get(index).filter(|v| v.close_requested).map(|v| v.id).and_then(|id| app.views.iter().position(|v| v.id == id));
- if let Some(i) = pending {
- app.views[i].close_requested = false;
- if app.has_unsaved_work(i) {
- app.close_request = Some(crate::CloseRequest::Tab(app.views[i].id));
- if gesture.resp.drag_stopped()
- && view.selection.is_some_and(|selection| {
- selection.page == gesture.page && matches!(selection.span, Span::Column { .. }) && selection.text(text).is_empty()
- })
- {
- view.selection = None;
- }
-}
-
-fn finish_text_pointer_gesture(ui: &egui::Ui, view: &mut DocView, text: &PageText, gesture: &TextGesture<'_>) {
- let release_clicks = if gesture.resp.triple_clicked() {
- view.clicks.max(2).saturating_add(1)
- } else if gesture.resp.double_clicked() {
- view.clicks.max(2)
- } else {
- 0
- };
- if release_clicks > 0 {
- view.clicks = release_clicks;
- }
- let clicks = selection_click_count(view.clicks, gesture.tool);
- if release_clicks > 0
- && clicks >= 2
- && gesture.over_text
- && view.selection.is_none()
- && let Some((first, last)) = text.nearest(gesture.vx, gesture.vy).and_then(|anchor| click_unit(text, clicks, anchor))
- {
- view.selection = Some(Selection::run(gesture.page, first, last));
- } else if gesture.resp.clicked() && view.clicks < 2 && !view.router.keeps_drag() {
- let shift = ui.input(|input| input.modifiers.shift);
- match (
- view.selection.as_mut().filter(|selection| shift && selection.page == gesture.page).map(|selection| &mut selection.span),
- text.nearest(gesture.vx, gesture.vy),
- ) {
- (Some(Span::Run { head, .. }), Some(hit)) => *head = hit,
- _ => view.selection = None,
- }
- }
-}
-
-/// Everything drawn over the page after its text: pictures, comments, content, links, fields, marks
-/// and badges.
-fn page_overlays(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot, input: &PageInput) {
- let PageEnv {
- added,
- allowed,
- calc_order,
- can_modify,
- comments_hidden,
- doc,
- doc_links,
- editing_content,
- form,
- generation,
- info,
- prefs,
- preparing,
- redact_mark,
- table_preview,
- tool,
- unobstructed,
- ..
- } = frame.env;
- let PageSlot { i, xf, .. } = *slot;
- let PageFrame { resp, ptr, pointer, .. } = *frame;
- let painter = ui.painter();
- let tool_cx = PageToolContext { ui, resp, xf: &xf, ptr, page: i, doc, info };
- let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
- if tool == QuickTool::Select && !input.consumed {
- crate::image_pick::page_input(resp, &xf, i, info, doc, pointer, view);
- }
- crate::image_pick::drop_stale(doc, view);
- crate::image_pick::paint(painter, &xf, i, info, view);
- // A cancelled press ends no selection: no markup or redaction is made from it.
- if !view.press_cancelled {
- comments::page_after_text(resp, &pcx, view);
- if tool == QuickTool::Redact && can_modify {
- crate::redact_ui::after_text(resp, i, info, view);
- }
- }
- if input.area_tool {
- crate::redact_ui::paint(painter, i, view, pointer);
- }
- comments::paint_page(ui, painter, &pcx, view);
- if editing_content {
- crate::content_ui::paint_page(painter, &xf, i, info, added, view, pointer);
- }
- if tool == QuickTool::EditText {
- crate::object_ui::paint(painter, &xf, i, info, &view.objects);
- }
- if tool == QuickTool::EnhanceCamera {
- crate::camera_ui::paint(painter, &xf, i, view);
- }
- if tool == QuickTool::Link {
- crate::link_ui::paint(painter, &xf, i, info, doc_links, view, pointer);
- }
- if preparing {
- crate::prepare::paint_page(tool_cx, form, view);
- if let Some(preview) = &table_preview {
- crate::field_table_ui::paint_preview(painter, &xf, i, preview);
- }
- if calc_order {
- let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
- if let Some(field) = crate::calc_order_ui::paint_page(tool_cx, form, &view.calc_order, generation, selected) {
- frame.calc_hover = Some(field);
- }
- }
- } else {
- crate::forms_ui::paint_page(painter, &xf, i, info, form, view, pointer);
- }
-
- // Form-field highlight (Acrobat's "Highlight existing fields"); required fields get a
- // red border. Radio buttons and round XFA check boxes are round, and so is theirs (as
- // in Acrobat). Only what can be filled is marked: a read-only widget (its field's, or a
- // radio button's own) is left as it is drawn.
- paint_field_highlights(painter, &xf, info, form.as_slice(), i, view.highlight_fields);
- // Links: the hand and the link's target, which count only for the tools that follow or
- // edit links (`Layer::reached_by`). Tips draw above the palette, so none shows while it is
- // open, even beside it. A click follows the link its press was on, released over it: one that
- // slips off the link first follows nothing, as a button's does.
- if unobstructed
- && resp.clicked()
- && tool == QuickTool::Select
- && !input.consumed
- && let Some(owner @ crate::page_router::Owner::Link { index }) = view.router.owner(i)
- && pointer.and_then(|p| link_hit(info, &xf, i, p)).is_some_and(|h| h.owner == owner)
- && let Some(l) = info.links.get(index)
- {
- frame.out.clicked_link = Some(l.target.clone());
- }
- // Comments say what they are with any tool, beside its cursor, as Acrobat's do.
- comments::describe(&pcx, view);
- for (mp, mr, mc) in &view.compare_marks {
- if *mp == i {
- let sr = xf.user_rect(info, i, *mr).expand(1.5);
- painter.rect_filled(sr, CornerRadius::same(2), mc.gamma_multiply(0.28));
- }
- }
- if let Some((fp, fr, t0)) = view.flash
- && fp == i
- {
- let now = ui.input(|inp| inp.time);
- let t0 = if t0 == 0.0 { now } else { t0 };
- view.flash = Some((fp, fr, t0));
- let age = (now - t0) as f32;
- if age < 1.6 {
- let a = ((1.6 - age) / 1.6 * 255.0) as u8;
- let sr = xf.user_rect(info, i, fr).expand(4.0);
- painter.rect_stroke(
- sr,
- CornerRadius::same(3),
- Stroke::new(2.5, Color32::from_rgba_unmultiplied(0x1B, 0x63, 0xE0, a)),
- egui::StrokeKind::Outside,
- );
- ui.ctx().request_repaint();
-- } else {
- app.close_tab(i);
- view.flash = None;
- }
- }
- // Validation is viewer chrome over the signer's original artwork, which
- // may still say "Signature Not Verified". These badges never enter document
- // streams, image/text exports, vector printing or Print as Image.
- for badge in signature_badges(&doc.signatures, i) {
- let [x0, y0, x1, y1] = badge.rect;
- let sr = xf.user_rect(info, i, [x0 as f32, y0 as f32, x1 as f32, y1 as f32]);
- let (icon, color) = crate::sign_ui::signature_status_style(badge.status);
- painter.rect_stroke(sr, CornerRadius::same(2), Stroke::new(1.5, color), egui::StrokeKind::Outside);
- // A widget too small for the mark keeps its outline: the verdict still shows.
- if sr.width() < 4.0 || sr.height() < 4.0 {
- continue;
- }
- // Big enough to read at a glance, small enough to leave the signer's own
- // text visible around it.
- let side = (sr.width().min(sr.height()) * 0.8).clamp(24.0, 192.0);
- let mark = Rect::from_center_size(sr.center(), vec2(side, side));
- icons::paint(ui, mark, icon, side * 0.64, color);
- // Overlapping widgets paint in order, so the topmost badge's verdict is the tip, with
- // any tool; a link's or a comment's tip wins over any badge.
- if pointer.is_some_and(|p| mark.expand(2.0).contains(p)) {
- view.cursor.describe(Subject::SignatureBadge, badge.summary.to_string());
-- }
- }
-}
-
-fn paint_field_highlights(painter: &egui::Painter, xf: &PageXform, info: &DocInfo, form: &[pdfcraft_engine::FormField], page: usize, enabled: bool) {
- if !enabled {
-- return;
-- }
- let Some(doc) = app.session.get(app.views[index].id) else { return };
- let info = &doc.info;
- let paper = app.session.reading_colors(app.views[index].id).and_then(|c| c.params).map_or(Color32::WHITE, |p| {
- let [r, g, b] = p.background;
- Color32::from_rgb(r, g, b)
- });
- if info.pages.is_empty() {
- app.views[index].auto_scroll.cancel();
- ui.centered_and_justified(|ui| ui.label(tl!("This document has no pages.")));
- for field in form.iter() {
- let required = field.has(pdfcraft_engine::field_flags::REQUIRED);
- for widget in field.widgets.iter().filter(|widget| widget.page == Some(page) && !widget.hidden && !widget.read_only) {
- let rect = widget.rect;
- let screen_rect = xf.user_rect(info, page, [rect[0] as f32, rect[1] as f32, rect[2] as f32, rect[3] as f32]);
- let tint = Color32::from_rgba_unmultiplied(0x6E, 0x8E, 0xF5, 48);
- let (width, color) = if required { (2.0, Color32::from_rgb(0xE3, 0x22, 0x22)) } else { (1.0, Color32::from_rgb(0x6E, 0x8E, 0xF5)) };
- if widget.round {
- let radius = screen_rect.width().min(screen_rect.height()) / 2.0;
- painter.circle_filled(screen_rect.center(), radius, tint);
- painter.circle_stroke(screen_rect.center(), radius - width / 2.0, Stroke::new(width, color));
- } else {
- painter.rect_filled(screen_rect, CornerRadius::same(1), tint);
- painter.rect_stroke(screen_rect, CornerRadius::same(1), Stroke::new(width, color), egui::StrokeKind::Inside);
- }
- }
- }
-}
-
-fn draw_page(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, i: usize) {
- let PageEnv { allowed, comments_hidden, doc, editing_content, info, ppp, prefs, rects, redact_mark, scale, t, tool, .. } = frame.env;
- let PageFrame { ptr, origin, visible, pressed, pointer, .. } = *frame;
- let r = snap_to_pixels(rects[i].translate(origin.to_vec2()), ppp);
- if !r.intersects(visible.expand(400.0)) {
-- return;
-- }
- // The print dialog queues its own rasters later in the frame. Closing it drops them.
- if app.dialog != Some(crate::Dialog::Print) {
- app.views[index].print_pages.clear();
- if pressed && frame.pressed_page.is_none() && pointer.is_some_and(|p| r.contains(p)) {
- frame.pressed_page = Some(i);
-- }
- // The Prepare a form panel is open (or a field tool is picked): fields are edited, not filled.
- let preparing = app.is_preparing();
- // Prepare a form ▸ Calculation order: the order's steps and links on the page.
- let calc_order = preparing && app.left_open && app.left == crate::LeftPanel::CalculationOrder;
- // Edit a PDF: added text and images can be selected, moved and edited.
- let editing_content =
- (app.left_open && app.left == crate::LeftPanel::Tool("edit")) || matches!(app.quick_tool, QuickTool::AddText | QuickTool::EditText);
- let text_style = app.text_style.clone();
- let show_secured_notice = app.show_secured_notice;
- // Asking means opening the system clipboard (and decoding any image on it), so it's asked
- // once when a right-click opens a menu, not on every frame.
- let can_paste_key = egui::Id::unique("can-paste-image");
- let can_paste_image = if ui.input(|i| i.pointer.secondary_pressed()) {
- let can = app.can_paste_image();
- ui.data_mut(|d| d.insert_temp(can_paste_key, can));
- can
- } else {
- ui.data(|d| d.get_temp(can_paste_key)).unwrap_or(false)
- if r.intersects(visible) {
- frame.visible_now.push(i);
- view.screen_rects.push((i, r));
- view.screen_xforms.push((i, PageXform { rect: r, rot: view.rotation, pw: info.pages[i].width.max(1.0), ph: info.pages[i].height.max(1.0) }));
- }
- let overlap = r.intersect(visible).height();
- if i == view.current {
- frame.current_overlap = overlap;
- frame.current_height = r.height();
- }
- // Pages shown equally (two rows wholly on screen) differ only by rounding: the
- // topmost one counts.
- if overlap > frame.best_overlap + PAGE_TIE {
- frame.best_overlap = overlap;
- frame.current = i;
- }
- let xf = PageXform { rect: r, rot: view.rotation, pw: info.pages[i].width.max(1.0), ph: info.pages[i].height.max(1.0) };
- paint_page_surface(ui, view, frame, &PageSlot { i, r, xf });
- let painter = ui.painter();
- view.text_background.paint(painter, &xf, i, view.line_editor.as_ref(), view.content.draft.as_ref());
- if editing_content {
- view.move_background.paint(painter, &xf, i, info, &view.content, pointer);
- }
- if view.show_page_gaps {
- painter.rect_stroke(r, CornerRadius::ZERO, Stroke::new(0.5, t.border.gamma_multiply(0.8)), egui::StrokeKind::Outside);
- }
-
- let slot = PageSlot { i, r, xf };
- // A cancelled press takes nothing until the button is up.
- let input = if view.press_cancelled { PageInput { consumed: true, area_tool: false } } else { page_tool_input(ui, view, frame, &slot) };
- let painter = ui.painter();
- let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
- let preview_target = (matches!(tool, QuickTool::Select | QuickTool::Fill(_)) && !comments_hidden).then_some(view.comments.selected).flatten();
- view.signature_drag.prepare(ui.ctx(), doc, preview_target, scale);
- view.signature_drag.paint(painter, &pcx, &view.comments, view.pending_edit.as_ref());
- // Any other comment being moved: drawn where it goes, over the page that still shows it
- // where it was.
- let stale = view.signature_drag.moving_page().is_some_and(|p| view.page_out_of_date(p));
- view.signature_drag.prepare_move(ui.ctx(), doc, &view.comments, view.pending_edit.as_ref(), stale, scale);
- view.signature_drag.paint_move(painter, &pcx, &view.comments, view.pending_edit.as_ref());
-
- // Any press on the page ends Select All of the document: text read or not, any tool,
- // on a link too.
- // A press that lands on something drawn over the page (a menu, a dialog) is not one.
- if view.all_text && frame.pressed_page == Some(i) {
- view.all_text = false;
- }
- text_layer(ui, view, frame, &slot, &input);
- page_overlays(ui, view, frame, &slot, &input);
-}
-
-/// The link under `p` on `page`: the hand, and where a click goes. Only the Select tool follows
-/// links and the Link tool edits them (`Layer::reached_by`).
-fn link_hit(info: &DocInfo, xf: &PageXform, page: usize, p: Pos2) -> Option<crate::page_router::Hit> {
- let (index, l) = info.links.iter().enumerate().rfind(|(_, l)| l.page == page && xf.user_rect(info, page, l.rect).contains(p))?;
- let label = match &l.target {
- LinkTarget::Page(n, _) => crate::i18n::fmt(tl!("Go to page {p}"), &[("p", info.pages.get(*n).map(|p| p.label.as_str()).unwrap_or("?"))]),
- LinkTarget::Uri(u) => u.clone(),
- LinkTarget::SetLayers { .. } => tl!("Set layer visibility").to_string(),
- LinkTarget::RemoteGoTo { file, .. } => file.clone(),
- LinkTarget::Other(s) => crate::i18n::fmt(tl!("{s} action"), &[("s", s)]),
-- };
- let view = &mut app.views[index];
- // Opened without the owner password and something is restricted. The notice says so unless
- // the user turned it off (#4035): the restrictions themselves, and Document Properties ▸
- // Security, stay as they are.
- let secured = show_secured_notice && doc.security_summary().is_some_and(|s| !(s.owner || (s.permissions.modify() && s.permissions.assemble())));
- let repaired = !doc.repair_log().is_empty();
- match notices(view, doc, secured, repaired, crate::sign_ui::banner(&doc.signatures), ui, &t) {
- #[cfg(not(target_arch = "wasm32"))]
- Some(Notice::Reload) => app.watch.reload_request = Some(view.id),
- #[cfg(target_arch = "wasm32")]
- Some(Notice::Reload) => {}
- Some(Notice::Repairs) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Advanced)),
- Some(Notice::Security) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Security)),
- Some(Notice::Unlock) => app.unlock_prompt = Some(crate::UnlockPrompt::new(doc, None)),
- // Flags the view: the tab closes at the top of the next frame, where no document
- // borrow is held.
- Some(Notice::Close) => {
- if let Some(view) = app.views.get_mut(index) {
- view.close_requested = true;
- }
- let owner = crate::page_router::Owner::Link { index };
- Some(crate::page_router::Hit { tip: Some(label), ..crate::page_router::Hit::new(owner, Layer::Link, egui::CursorIcon::PointingHand) })
-}
-
-/// Escape while the pages hold a press abandons the gesture it drives, whichever tool's: nothing is
-/// placed, drawn, moved, resized or opened, and the press is ignored until the button is up. The
-/// frame of the release still belongs to the cancelled press.
-fn cancel_on_escape(ctx: &egui::Context, view: &mut DocView, ptr: &PagePointer) {
- // Over once the button is up and its release seen, or when a new press comes (its release and
- // the next press can share a frame).
- let new_press = matches!(ptr.edges.last(), Some(crate::page_pointer::Edge::Press(_)));
- if view.press_cancelled && (new_press || !ptr.any_down && ptr.edges.is_empty()) {
- view.press_cancelled = false;
- }
- if !view.press_cancelled && ptr.origin.is_some() && ptr.primary_down && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))
- {
- // A text selection being dragged goes with its drag: the markup, redaction or Replace
- // Text that its end would make is not made.
- if view.selection_drag {
- view.clear_selection();
-- }
- Some(Notice::Signatures) => app.right = Some(RightPanel::Signatures),
- Some(Notice::FieldHighlights(on)) => app.view_defaults.highlight_fields = on,
- Some(Notice::Console) => app.dialog = Some(crate::Dialog::JsConsole),
- // Back on in Preferences ▸ General.
- Some(Notice::HideSecured) => app.show_secured_notice = false,
- None => {}
- view.cancel_pointer_gestures();
- view.press_cancelled = true;
-- }
- // No dialog, close prompt or palette over the page: only then does page input count.
- let modal = app.modal_open();
- let unobstructed = !modal && !app.palette_open;
- let organize_zoom = app.organize_zoom;
- let view = &mut app.views[index];
- if view.organize {
- // One remembered grid size for every document, kept in the settings.
- view.set_grid_zoom(organize_zoom);
- organize_grid(view, info, doc.allows_assembly(), doc.dirty, unobstructed, ui, &t);
- app.organize_zoom = view.grid_zoom();
- return;
-}
-
-fn draw_pages(
- ui: &mut egui::Ui,
- viewport: Rect,
- view: &mut DocView,
- env: &PageEnv<'_>,
- out: &mut PageOut,
- signature_preview: &mut Option<(crate::fill_sign::SavedSig, egui::TextureHandle)>,
-) -> PageRenders {
- let PageEnv {
- allowed,
- auto_delta,
- avail,
- boost_extra,
- calc_order,
- can_copy,
- can_modify,
- content_h,
- content_w,
- doc,
- edge_delta,
- form,
- generation,
- glide,
- hand,
- info,
- key_scroll,
- middle_gesture,
- modal,
- navigation_enabled,
- precise_scroll,
- prefs,
- preparing,
- selection_wheel,
- single_paging,
- tool,
- unobstructed,
- visible_pages,
- y_shift,
- ..
- } = *env;
- // egui's drag responses accept every pointer button. Keep a wheel gesture from also
- // selecting text, drawing a mark, or panning with the Hand tool.
- if middle_gesture {
- let opacity = ui.opacity();
- ui.disable();
- // Only interactions pause; the document must keep its original colours.
- ui.set_opacity(opacity);
- }
- // Middle-button auto-scroll, the keyboard, precise touchpad input (#759) and its glide
- // (#531), accelerated wheel surplus (#1072), and a drawing's edge scroll; positive y
- // moves the content down. All scroll without easing, unlike egui's wheel default.
- // ScrollArea ignores wheel input during a widget drag. Defer the wheel contribution
- // until after the text widgets when a selection owns that drag.
- let wheel_delta = if selection_wheel { Vec2::ZERO } else { precise_scroll + boost_extra };
- let delta = auto_delta + edge_delta - vec2(0.0, key_scroll) + wheel_delta + glide;
- if delta != Vec2::ZERO && delta.is_finite() {
- ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
- }
- let (resp_rect, resp) = ui.allocate_exact_size(vec2(content_w, content_h), Sense::click_and_drag());
- view.page_widget = Some(resp.id);
- // A drag lasts only while egui reports it, so the mark can't outlive one.
- if !resp.dragged_by(egui::PointerButton::Primary) {
- view.selection_drag = false;
- }
- // The Hand tool pans by this widget's drag, before the pages are laid out (above).
- if hand {
- if resp.dragged() {
- view.cursor.claim(Layer::Drag, egui::CursorIcon::Grabbing);
- } else if resp.hovered() {
- view.cursor.claim(Layer::ToolArea, egui::CursorIcon::Grab);
- }
- }
- let origin = resp_rect.min - vec2(0.0, y_shift);
- let visible = viewport.translate(resp_rect.min.to_vec2());
- view.screen_rects.clear();
- view.screen_xforms.clear();
- // The pointer as the pages see it: nothing over them (a dialog from the frame it opens),
- // or a press they hold. The page modules read it rather than egui's raw pointer state.
- let ptr = PagePointer::read(ui, &resp, modal);
- cancel_on_escape(ui.ctx(), view, &ptr);
- view.router.settle(ptr.any_down, !ptr.edges.is_empty());
- // A new press is no longer the last one's, wherever it lands (the page it is on gives it
- // its owner; in the gap between pages it has none).
- if ptr.pressed().is_some() {
- view.router.cancel();
- // The page widget is not focusable. Explicitly return keyboard ownership from Pages
- // when the user presses the document, without stealing focus through a modal.
- if let Some(id) = view.pages_focus_id {
- ui.memory_mut(|m| m.surrender_focus(id));
- }
- }
- let pointer = ptr.pos;
- // A press on a page makes it the current one (as in Acrobat), so a page command with
- // no selection (Rotate, Delete) acts on the page the reader pointed at, not on a page
- // gone to earlier that still shows whole beside it.
- let pressed = unobstructed && ptr.pressed().is_some();
- if resp.secondary_clicked() {
- // Set again below when the right-click lands on a link.
- view.links.context = None;
- }
- let mut frame = PageFrame {
- env: *env,
- resp: &resp,
- ptr: &ptr,
- origin,
- visible,
- pressed,
- pointer,
- signature_preview,
- out: std::mem::take(out),
- wanted: Vec::new(),
- visible_now: Vec::new(),
- current: view.current,
- best_overlap: -1.0,
- current_overlap: -1.0,
- current_height: f32::INFINITY,
- pressed_page: None,
- calc_hover: None,
- };
- for &i in visible_pages {
- draw_page(ui, view, &mut frame, i);
- }
- let PageFrame {
- wanted, visible_now, mut current, best_overlap, current_overlap, current_height, pressed_page, calc_hover, out: page_out, ..
- } = frame;
- let PageOut {
- mut canvas_action,
- clicked_link,
- mut copy_selected,
- mut field_menu,
- field_placed,
- field_props,
- hover_text: _,
- image_action,
- open_boxes,
- open_initials,
- open_signature,
- open_table,
- primary_down: _,
- refused,
- stamp_placed,
- } = page_out;
- let painter = ui.painter();
- if calc_order {
- view.calc_order.set_hover_page(calc_hover);
- let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
- crate::calc_order_ui::paint_trace(ui, painter, visible, &view.calc_order, generation, selected);
- }
- // The pages' one cursor, from the claim on the highest layer the tool reaches, and the tip.
- let hover_text = pointer.filter(|_| unobstructed).zip(view.cursor.resolve(ui.ctx(), tool));
- // ScrollArea ignores the wheel while any widget is dragged, so a text-selection drag
- // scrolls the page itself (#721). After the page loop, so it keeps scrolling once the
- // pointer leaves the selection's page for the gap or the next page; over the whole
- // document area, like #759. Combine direct and smoothed motion in one request so mixed
- // Point and Line events retain their full contribution.
- if selection_wheel {
- let smooth = if view.precise_scroll_until == 0.0 { ui.input_mut(|input| std::mem::take(&mut input.smooth_scroll_delta)) } else { Vec2::ZERO };
- let delta = precise_scroll + boost_extra + smooth;
- if delta != Vec2::ZERO && delta.is_finite() {
- ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
- }
- }
- // Held at the top or bottom edge, or dragged past it, a text selection scrolls the view
- // so it can reach text off screen (#1670); the selection follows the pointer on its page.
- if view.selection_drag
- && navigation_enabled
- && unobstructed
- && !single_paging
- && let Some(p) = ptr.pos
- {
- let dy = crate::autoscroll::edge_delta(p.y, avail, ui.input(|i| i.stable_dt));
- if dy != 0.0 {
- view.end_find_following();
- ui.scroll_with_delta_animation(vec2(0.0, dy), egui::style::ScrollAnimation::none());
- // Keep scrolling while the pointer rests there and sends no events.
- ui.ctx().request_repaint();
- }
- }
- // The page navigated to stays current while no page shows more of itself, so the next
- // page step starts from it rather than from a page further down the screen (#188).
- // It also stays current while it is wholly on screen: a short page (a cheque) gone to
- // from the Pages panel shows less of itself than the tall page below it, and must not
- // hand the highlight to that page.
- if current_overlap >= best_overlap - PAGE_TIE || current_overlap >= current_height - PAGE_TIE {
- current = view.current;
- }
- if let Some(p) = pressed_page {
- current = p;
- }
- if view.continuous {
- view.current = current;
-- }
- resp.context_menu(|ui| {
- // Preparing a form: the selected field's menu.
- if preparing && let Some((name, wi)) = view.prepare.selected.clone() {
- // Several fields: Align, Center, Distribute, Set Fields to Same Size.
- if !view.prepare.also.is_empty() {
- use crate::prepare::Arrange as A;
- let others = view.prepare.also.clone();
- let anchor = (name.clone(), wi);
- let mut pick = |ui: &mut egui::Ui, op: A, label: &str| {
- if ui.add_enabled(can_modify, egui::Button::new(tl!(label))).clicked() {
- view.pending_edit = crate::prepare::arrange(form, &anchor, &others, op);
- ui.close();
- }
- };
- ui.menu_button(tl!("Align"), |ui| {
- pick(ui, A::AlignLeft, "Left");
- pick(ui, A::AlignRight, "Right");
- pick(ui, A::AlignTop, "Top");
- pick(ui, A::AlignBottom, "Bottom");
- pick(ui, A::AlignCenterV, "Center vertically");
- pick(ui, A::AlignCenterH, "Center horizontally");
- });
- ui.menu_button(tl!("Distribute"), |ui| {
- pick(ui, A::DistributeH, "Horizontally");
- pick(ui, A::DistributeV, "Vertically");
- });
- ui.menu_button(tl!("Set Fields to Same Size"), |ui| {
- pick(ui, A::SameHeight, "Height");
- pick(ui, A::SameWidth, "Width");
- pick(ui, A::SameSize, "Both");
- });
- ui.separator();
- }
- if ui.button(tl!("Properties…")).clicked() {
- field_menu = Some(FieldMenu::Properties);
- ui.close();
- }
- if ui.add_enabled(can_modify, egui::Button::new(tl!("Duplicate…"))).clicked() {
- field_menu = Some(FieldMenu::Duplicate(name.clone()));
- ui.close();
- }
- ui.separator();
- let delete = if view.prepare.also.is_empty() { "Delete" } else { "Delete selected fields" };
- if ui.add_enabled(can_modify, egui::Button::new(tl!(delete))).clicked() {
- view.pending_edit = crate::prepare::delete_selected(view);
- ui.close();
- }
- return;
- }
- if let Some((page, index)) = view.links.context {
- if ui.button(tl!("Properties…")).clicked() {
- view.links.open_existing = Some((page, index));
- ui.close();
- }
- if ui.add_enabled(can_modify, egui::Button::new(tl!("Delete"))).clicked() {
- view.pending_edit = Some(Edit::DeleteLink { page, index });
- view.links.selected = None;
- view.links.context = None;
- ui.close();
- }
- ui.separator();
- if ui.add_enabled(can_modify, egui::Button::new(tl!("Remove all links"))).clicked() {
- view.pending_edit = Some(Edit::RemoveLinks { pages: None });
- view.links.selected = None;
- view.links.context = None;
- ui.close();
- }
- return;
- }
- if view.comments.selected.is_none() {
- let selected = view.selected_text().is_some_and(|t| !t.is_empty());
- let key = crate::commands::shortcut_label(ui.ctx(), crate::commands::COPY);
- if ui.add_enabled(can_copy && selected, egui::Button::new(tl!("Copy")).shortcut_text(key)).clicked() {
- copy_selected = true;
- ui.close();
- }
- ui.separator();
- }
- // Over a picture (and no comment): Copy Image, as in Acrobat's page menu.
- let mut copy_image = None;
- if view.comments.selected.is_none()
- && let Some((page, at)) = view.comments.context_at
- {
- // The page's content is parsed once per right-click, not on every frame the menu is open.
- let key = egui::Id::unique("copy-image-under-menu");
- let cached = ui.data(|d| d.get_temp::<(usize, [f64; 2], Option<usize>)>(key)).filter(|c| c.0 == page && c.1 == at);
- let hit = match cached {
- Some(c) => c.2,
- None => {
- let hit = doc.page_image_at(page, at);
- ui.data_mut(|d| d.insert_temp(key, (page, at, hit)));
- hit
- }
- };
- if let Some(image) = hit {
- if ui.button(tl!("Copy Image")).clicked() {
- copy_image = Some(comments::CanvasAction::CopyImage(page, image));
- ui.close();
- }
- if ui.button(tl!("Copy Image in Original Format")).clicked() {
- copy_image = Some(comments::CanvasAction::CopyImageFile(page, image));
- ui.close();
- }
- ui.separator();
- }
- }
- canvas_action =
- comments::context_menu(ui, view, info, &|page, index| doc.annotation_deletable(page, index), prefs, allowed, can_modify).or(copy_image);
- });
- *out = PageOut {
- canvas_action,
- clicked_link,
- copy_selected,
- field_menu,
- field_placed,
- field_props,
- hover_text,
- image_action,
- open_boxes,
- open_initials,
- open_signature,
- open_table,
- primary_down: ptr.primary_down,
- refused,
- stamp_placed,
- };
- (wanted, visible_now)
-}
--
- let avail = ui.available_rect_before_wrap();
- view.viewport_w = avail.width();
- view.viewport_h = avail.height();
- let old_zoom = view.zoom;
- view.fit_zoom(info);
-/// A zoom the fit mode changed keeps the page at the top of the view in place.
-fn anchor_zoom_change(view: &mut DocView, avail_width: f32, old_zoom: f32) {
-- if view.shown && view.zoom_anchor.is_none() && view.goto.is_none() && view.goto_point.is_none() && (view.zoom - old_zoom).abs() > 1e-4 {
-- let prev_viewport = view.viewport_screen;
-- let top_y = prev_viewport.min.y;
-- let anchor = view
-- .screen_rects
-- .iter()
-- .find(|(_, r)| r.min.y <= top_y && r.max.y > top_y)
-- .map(|(p, r)| {
-- let fy = if r.height() > 0.0 { ((top_y - r.min.y) / r.height()).clamp(0.0, 1.0) } else { 0.0 };
-- (*p, fy, 0.0f32)
-- })
-- .or_else(|| view.screen_rects.iter().find(|(_, r)| r.min.y >= top_y).map(|(p, r)| (*p, 0.0f32, (r.min.y - top_y).max(0.0))))
-- .or_else(|| view.screen_rects.last().map(|(p, r)| (*p, 1.0f32, (r.max.y - top_y).max(0.0))));
-- if let Some((page, fy, rel_y)) = anchor {
- view.zoom_anchor = Some((page, 0.5, fy, vec2(avail.width() / 2.0, rel_y)));
- view.zoom_anchor = Some((page, 0.5, fy, vec2(avail_width / 2.0, rel_y)));
-- } else {
- view.zoom_anchor = Some((view.current, 0.5, 0.0, vec2(avail.width() / 2.0, MARGIN)));
- }
- }
-
- // Pinch / ctrl+scroll zoom, anchored on the current page.
- let zoom_delta = crate::motion::zoom_delta(ui.ctx());
- // Exclude the previous frame's scrollbar tracks from gesture activation.
- let navigation_rect = if view.viewport_screen.is_positive() { avail.intersect(view.viewport_screen) } else { avail };
- let navigation_enabled = app.dialog.is_none() && app.close_request.is_none() && !app.palette_open && ui.is_enabled() && ui.input(|i| i.focused);
- // Full screen: a click on the document goes forward a page and a shift-click back, as in
- // Acrobat, unless the click did something else (a link, a word selected). Decided below,
- // once the pages have had the click.
- let present_click =
- view.presentation.is_some() && navigation_enabled && presentation_click(ui, navigation_rect) && app.quick_tool == QuickTool::Select;
- // Shift as held now, or as the click's own event carried it (a synthetic click).
- let present_back = present_click
- && ui.input(|i| {
- i.modifiers.shift
- || i.events.iter().any(|e| {
- matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: false, modifiers, .. } if modifiers.shift)
- })
- });
- if zoom_delta.is_finite() && zoom_delta > 0.0 && zoom_delta != 1.0 && navigation_enabled && ui.rect_contains_pointer(navigation_rect) {
- let z = view.zoom * zoom_delta;
- match zoom_anchor(ui) {
- Some(p) => view.zoom_at(z, p),
- None => view.set_zoom(z),
- view.zoom_anchor = Some((view.current, 0.5, 0.0, vec2(avail_width / 2.0, MARGIN)));
-- }
-- }
- view.viewport_screen = avail;
- let auto_delta = if unobstructed {
- view.auto_scroll.update(ui, avail, false)
- } else {
- view.auto_scroll.cancel();
- Vec2::ZERO
- };
-}
--
- // Drawing a comment: the pointer in a band along any edge (or just past it) scrolls the view
- // that way, so a point beyond what is shown can be reached without letting go of the tool.
- let edge_delta = if unobstructed && matches!(view.comments.gesture, Some(comments::Gesture::Draw { .. })) {
- let (pointer, dt) = drawing_edge_pointer(ui);
- crate::autoscroll::edge_scroll(pointer, avail, dt)
- } else {
- Vec2::ZERO
- };
- if edge_delta != Vec2::ZERO {
- ui.ctx().request_repaint();
- }
-/// What the pager needs to know about the page row on screen.
-#[derive(Clone, Copy)]
-struct PagingCx<'a> {
- row: &'a [usize],
- rects: &'a [Rect],
- avail: Rect,
- middle_gesture: bool,
- unobstructed: bool,
-}
--
- let content_w = view.content_width(info, avail.width());
- let rects = view.layout(info, content_w, ui.ctx().pixels_per_point());
- // When not scrolling continuously, only one row is shown at a time: the current page, or the
- // current two-page spread (with the cover rule). These indices drive paging, which pages are
- // drawn, and the content height (#752). Empty for a document with no pages.
- let shown_row = |view: &DocView| -> Vec<usize> {
- if rects.is_empty() {
- return Vec::new();
- }
- let last = rects.len() - 1;
- let cur = view.current.min(last);
- match view.layout {
- PageLayout::Single => vec![cur],
- PageLayout::TwoUp if view.cover && cur == 0 => vec![0],
- PageLayout::TwoUp => {
- let first = if view.cover { cur - (cur + 1) % 2 } else { cur - cur % 2 };
- if first < last { vec![first, first + 1] } else { vec![first] }
- }
- }
- };
- let row = shown_row(view);
- let middle_gesture = view.auto_scroll.blocks_input();
- // Not scrolling: when the whole page or spread fits, the wheel would do nothing, so it turns
- // pages instead; zoomed in far enough to pan, it pans, and at the edge it turns the page on
- // (#1924, `single_page_wheel_edge`). Touch drags are untouched: with
- // nothing to pan, touch users turn pages with the rail buttons, the page box and the arrow
- // keys. Runs before `visible_pages` so the frame that turns the page draws it.
- let mut single_paging = false;
- let mut edge_turned = false;
-/// Not scrolling: when the whole page or spread fits, the wheel turns pages instead. Returns whether
-/// paging owns the wheel, and whether it turned the page at an edge.
-fn single_page_paging(ui: &egui::Ui, view: &mut DocView, cx: &PagingCx<'_>) -> (bool, bool) {
- let (mut single_paging, mut edge_turned) = (false, false);
- let PagingCx { row, rects, avail, middle_gesture, unobstructed } = *cx;
-- if !view.continuous {
-- // The row's bounding box on screen (one page, or a two-page spread).
-- let row_rect = row.iter().filter_map(|&i| rects.get(i)).copied().reduce(Rect::union);
-- // Within a point, so layout rounding can't stop a fitting page from turning.
-- let fits = row_rect.is_some_and(|r| r.height() + 2.0 * MARGIN <= avail.height() + 1.0);
-- let can_turn = !middle_gesture && unobstructed && ui.rect_contains_pointer(avail);
-- single_paging = can_turn && fits;
-- // Every wheel event goes to the pager, so it follows each trackpad touch to its end
-- // even while the page can't turn.
-- ui.input(|i| {
-- for e in &i.events {
-- if let egui::Event::MouseWheel { unit, delta, phase, modifiers, .. } = e {
-- // Zooming (⌘ or Ctrl) and sideways scrolling (Shift) never turn pages.
-- let dy = if modifiers.command || modifiers.ctrl || modifiers.shift { 0.0 } else { delta.y };
-- if fits {
-- view.single_page_wheel(*unit, dy, *phase, i.time, can_turn);
-- } else {
-- edge_turned |= view.single_page_wheel_edge(*unit, dy, *phase, i.time, can_turn);
-- }
-- }
-- }
-- });
-- if single_paging {
-- // Paging owns vertical wheel motion here. A diagonal gesture keeps its sideways
-- // part, as `ScrollArea` itself handles each axis.
-- ui.input_mut(|i| i.smooth_scroll_delta.y = 0.0);
-- }
-- }
- // Point input bypasses egui smoothing for 1:1 motion. Line input keeps egui's configured,
- // smoothed base and adds only the accelerated surplus. While point suppression is active,
- // reconstruct line and page input here so current coarse events survive without exposing an
- // older eased tail.
- (single_paging, edge_turned)
-}
-
-/// This frame's wheel motion over the page view, after paging and point/line suppression.
-struct Wheel {
- precise_scroll: Vec2,
- line_base: Vec2,
- boost_extra: Vec2,
- has_page: bool,
- /// The pager owns the vertical axis (single-page view, or the rest of a gesture that turned the page).
- paging_owns_vertical: bool,
-}
-
-/// Point input bypasses egui smoothing for 1:1 motion. Line input keeps egui's configured, smoothed
-/// base and adds only the accelerated surplus. While point suppression is active, line and page
-/// input are reconstructed so current coarse events survive without exposing an older eased tail.
-fn read_wheel(ui: &mut egui::Ui, view: &mut DocView, avail: Rect, single_paging: bool, edge_turned: bool) -> Wheel {
-- let now = ui.input(|i| i.time);
-- let input = if ui.rect_contains_pointer(avail) {
-- // Match the line distance used by egui's other scroll areas.
-- let live = ui.ctx().options(|o| o.input_options.line_scroll_speed);
-- let line_points = if live.is_finite() && live > 0.0 { live } else { crate::scroll_speed::EGUI_LINE_POINTS };
-- ui.input(|i| crate::wheel_boost::accumulate(&i.events, line_points, single_paging))
-- } else {
-- crate::wheel_boost::FrameInput::default()
-- };
-- let mut precise_scroll = input.precise;
-- let mut line_base = input.line;
-- let mut boost_extra = crate::wheel_boost::boost_extra(input, &mut view.wheel_boost_x, &mut view.wheel_boost_y, now);
-- let has_page = input.page != Vec2::ZERO;
-- if now.is_finite() && precise_scroll != Vec2::ZERO && !has_page {
-- view.precise_scroll_until = now + SUPPRESS_SECS;
-- }
-- if edge_turned {
-- // The turn leaves egui's eased copy of the notch to decay for a few frames; it must not
-- // scroll the page just shown away from its top (or bottom).
-- view.precise_scroll_until = now + 0.25;
- view.fling.stop();
-- }
- // Paging owns the vertical axis in single-page view; leave it to the pager.
- // The rest of a gesture that turned the page is swallowed too, or it would pan the new page
- // away from the edge it was shown at.
-- let paging_owns_vertical = single_paging || (!view.continuous && view.wheel.gesture_turned());
-- if paging_owns_vertical {
-- precise_scroll.y = 0.0;
-- line_base.y = 0.0;
-- boost_extra.y = 0.0;
-- }
-- // egui smooths a Point event that moved at least 8 points, keeping part of it for later
-- // passes. Suppression must outlive that whole action: once it expired after a fixed 0.25 s,
-- // the last few eased points leaked through the ScrollArea after a precise 1:1 scroll (held
-- // point input measured 33 points for a 30-point event). Keep hiding the eased copy until
-- // egui says the action is over, then forget the suppression.
-- let scrolling = ui.input(|i| i.is_scrolling());
-- if now < view.precise_scroll_until || (view.precise_scroll_until != 0.0 && scrolling) {
-- if scrolling {
-- // Keep hiding egui's unprocessed copy until it has drained. The short rolling grace
-- // avoids a delayed duplicate without prolonging suppression after the tail is gone.
-- view.precise_scroll_until = view.precise_scroll_until.max(now + SUPPRESS_TAIL_SECS);
-- }
-- ui.input_mut(|i| i.smooth_scroll_delta = Vec2::ZERO);
-- // The point path suppresses every eased wheel delta. Restore the line base through the
-- // same un-eased path as its surplus; paging-owned axes were cleared above.
-- boost_extra = crate::wheel_boost::cap_frame(boost_extra + line_base);
-- if has_page {
-- // Page units normally stay on egui's viewport-sized path. During suppression only,
-- // reproduce the current event directly so an older hidden tail stays hidden.
-- let page_scale = ui.input(|i| i.viewport_rect().height());
-- let mut page_delta =
-- if page_scale.is_finite() && page_scale > 0.0 { crate::wheel_boost::cap_frame(input.page * page_scale) } else { Vec2::ZERO };
-- if paging_owns_vertical {
-- page_delta.y = 0.0;
-- }
-- boost_extra = crate::wheel_boost::cap_frame(boost_extra + page_delta);
-- }
-- } else {
-- precise_scroll = Vec2::ZERO;
-- // Selection handling below uses zero to distinguish an inactive window. Clearing a past
-- // deadline also keeps a later line/page action from being mistaken for the old tail.
-- view.precise_scroll_until = 0.0;
-- }
-- if has_page && paging_owns_vertical {
-- // Page input belongs to egui on the free axis; the pager still owns vertical motion.
-- ui.input_mut(|i| i.smooth_scroll_delta.y = 0.0);
-- }
- // Touchpad momentum (#531): a swipe over the page lifted at speed glides on and slows to a
- // stop, through the same un-eased path. Never while the pager owns the wheel, a gesture or
- // dialog has the input, or animations are off; any click, key, zoom or other scrolling ends it.
- // Asked of the preference, not of egui's animation time, which test harnesses zero.
- let animates = app.animation_effects.animates(app.motion.reduced);
- let may_glide = !paging_owns_vertical && !middle_gesture && navigation_enabled && unobstructed && animates;
- let over_page = ui.rect_contains_pointer(avail);
- ui.input(|i| view.fling.read(&i.events, now, may_glide && over_page));
- let interrupted = glide_interrupted(ui);
- if !may_glide || interrupted || key_scroll != 0.0 || auto_delta != Vec2::ZERO || zoom_delta != 1.0 {
- view.fling.stop();
- Wheel { precise_scroll, line_base, boost_extra, has_page, paging_owns_vertical }
-}
-
-/// What decides which renders a frame asks for.
-#[derive(Clone, Copy)]
-struct RenderPlan<'a> {
- visible_pages: &'a [usize],
- tag: u64,
- scale: f32,
- sharp_held: bool,
- pan_held: bool,
- hand: bool,
-}
-
-/// Queues the renders for the pages and tiles `renders` wants (nearest the view first), prefetches the
-/// rows either side, frees tiles of far pages and old zooms, and asks for the text layers.
-fn plan_renders(ui: &egui::Ui, view: &mut DocView, info: &DocInfo, renders: PageRenders, plan: &RenderPlan<'_>) {
- let (mut wanted, visible_now) = renders;
- let RenderPlan { visible_pages, tag, scale, sharp_held, pan_held, hand } = *plan;
- // Single-page and two-page views draw only the current row, so the next one was first asked
- // for on the turn and showed blank paper until it rendered. Prefetch the rows either side,
- // as continuous scrolling does for the pages just off screen.
- if !view.continuous
- && let (Some(&first), Some(&last)) = (visible_pages.first(), visible_pages.last())
- {
- let n = if view.layout == PageLayout::Single { 1 } else { 2 };
- let max_side = ui.ctx().input(|inp| inp.max_texture_side) as f32;
- for i in (last.saturating_add(1)..=last.saturating_add(n)).chain(first.saturating_sub(n)..first) {
- let Some(p) = info.pages.get(i).filter(|_| !view.errors.contains_key(&i)) else { continue };
- // As drawn: the whole page at this scale, or the backdrop of a tiled page.
- let side = p.width.max(p.height).max(1.0);
- wanted.push(if side * scale > TILE_THRESHOLD.min(max_side) {
- let bs = BASE_SIDE.min(max_side) / side;
- (i, bs, scale_tag(bs), None)
- } else {
- (i, scale, tag, None)
- });
- }
-- }
- let glide = view.fling.step(now);
- if view.fling.gliding() {
- ui.ctx().request_repaint();
- let cur = view.current;
- wanted.sort_by_key(|w| (!visible_now.contains(&w.0), w.0.abs_diff(cur), w.3.is_some()));
- // Tiles of far-away pages are useless: free them. Tiles of an earlier zoom stay for a page
- // that is on screen, whether or not the page is tiled at this zoom (#2196): a zoom out below
- // the tiling threshold used to drop the tiles of the zoom just left, so coming back to it
- // was a cold miss and the page re-blurred through the backdrop while every tile was
- // interpreted again. `OLD_TILES` still bounds how many older tiles are kept, nearest scale
- // first, and admission keeps them inside `TILE_BYTES`.
- let missing: HashSet<usize> = wanted.iter().filter(|w| w.3.is_some()).map(|w| w.0).collect();
- let on_screen: HashSet<usize> = visible_now.iter().copied().collect();
- view.tiles.retain(|(p, t, _, _), _| p.abs_diff(cur) <= 2 && (*t == tag || missing.contains(p) || on_screen.contains(p)));
- let mut older: Vec<(f32, (usize, u64, u32, u32))> =
- view.tiles.keys().filter(|k| k.1 != tag).map(|k| ((tag_scale(k.1) / scale).ln().abs(), *k)).collect();
- // Tiles of earlier zooms are kept nearest scale first, and bounded by count *and* bytes: a
- // large scan renders a tile up to 4096² (about 67 MiB), so a count alone let a few tiles of
- // a few zooms run far past the tile allowance. #2196 keeps a zoom already visited, which
- // makes that reachable in ordinary zooming, so the byte bound has to be here too.
- older.sort_by(|a, b| a.0.total_cmp(&b.0));
- let mut kept = 0usize;
- let mut kept_bytes = 0usize;
- let mut drop: Vec<(usize, u64, u32, u32)> = Vec::new();
- for (_dist, key) in older {
- let bytes = view.tiles.get(&key).map_or(0, texture_bytes);
- if kept >= OLD_TILES || kept_bytes.saturating_add(bytes) > TILE_BYTES {
- drop.push(key);
- } else {
- kept = kept.saturating_add(1);
- kept_bytes = kept_bytes.saturating_add(bytes);
- }
-- }
- // Any reader-owned motion ends automatic search following, even when it stays on the same
- // page: the wheel, keyboard, middle-button auto-scroll, a drawing's edge scroll, or the
- // momentum left by a touchpad swipe. The automatic flash uses `goto`, not these deltas, so
- // its programmatic scroll is never mistaken for a reader action.
- let wheel_scrolled = precise_scroll != Vec2::ZERO || line_base != Vec2::ZERO || boost_extra != Vec2::ZERO || has_page;
- if wheel_scrolled || auto_delta != Vec2::ZERO || edge_delta != Vec2::ZERO || glide != Vec2::ZERO {
- view.end_find_following();
- for key in drop {
- view.tiles.remove(&key);
-- }
- // The wheel may have turned the page above: draw the row it turned to. Measured from the old
- // row, the jump to the new page scrolled both out of view, a blank frame on every turn forward.
- let row = shown_row(view);
- let visible_pages: Vec<usize> = if view.continuous { (0..rects.len()).collect() } else { row.clone() };
- let (y_shift, content_h) = if view.continuous {
- (0.0, rects.iter().map(|r| r.bottom()).reduce(f32::max).map(|bottom| bottom + MARGIN).unwrap_or(0.0))
- } else {
- // Only the current row scrolls into view; the content is its bounding box plus margins.
- match row.iter().filter_map(|&i| rects.get(i)).copied().reduce(Rect::union) {
- Some(r) => (r.top() - MARGIN, r.height() + 2.0 * MARGIN),
- None => (0.0, 0.0),
- let mut queue: Vec<RenderRequest> = wanted
- .iter()
- .map(|&(page, wanted_scale, wanted_tag, tile)| RenderRequest {
- page,
- kind: RequestKind::Pixels,
- tile,
- scale: wanted_scale,
- tag: wanted_tag,
- // The low-res backdrop under tiles is a preview: tiny images are skipped for it.
- // It is the whole-page request whose scale is not the current one; sharp tiles
- // and whole-page finals always draw everything.
- preview: tile.is_none() && (wanted_scale != scale || wanted_tag != tag),
- })
- .collect();
- // While the zoom or pan is settling, hold back sharp re-renders of pages that already
- // show something (a stale whole-page raster or older tiles, drawn stretched): each new
- // scale would cancel the running full-page interpret and restart it from scratch, and
- // each pan frame would queue a storm of full-page tile interprets.
- // Pages with nothing on screen yet still queue, so panning to a fresh page draws it.
- if sharp_held || pan_held {
- let main_tag = tag;
- queue.retain(|req| {
- // Fast pages render straight through: the hold only protects heavy pages
- // whose full-page interpret costs seconds.
- if !view.slow_pages.contains(&req.page) {
- return true;
- }
- if req.tile.is_some() {
- let has_current_tile = view.tiles.keys().any(|(p, t, _, _)| *p == req.page && *t == req.tag);
- if has_current_tile {
- return false;
- }
- let has_any_surface = view.pages.contains_key(&req.page) || view.tiles.keys().any(|(p, _, _, _)| *p == req.page);
- !has_any_surface
- } else if req.tag != main_tag {
- // The low-res backdrop under tiles is zoom-independent: let it through,
- // it is the stretched surface the hold relies on.
- true
- } else {
- // The stale raster stays stretched until the zoom or pan settles; a page with
- // nothing on screen yet still renders.
- !view.pages.contains_key(&req.page)
- }
- });
- }
- // Text layers: visible pages for selection, every page while a search is active. (Select All
- // of the document needs no more: shown pages highlight as their text comes, and Copy reads
- // the rest, as text far away is let go to bound memory.)
- let need_text = |p: &usize| !view.texts.contains_key(p) && !view.text_failed.contains(p);
- let mut text_pages: Vec<usize> = if hand { Vec::new() } else { visible_now.iter().copied().filter(need_text).collect() };
- if view.find.as_ref().is_some_and(|f| !f.case_query.trim().is_empty()) {
- let n = info.pages.len();
- let rest: Vec<usize> = (0..n).map(|k| (cur + k) % n).filter(need_text).filter(|p| !text_pages.contains(p)).collect();
- text_pages.extend(rest);
- }
- queue.extend(text_pages.into_iter().map(|page| RenderRequest {
- page,
- kind: RequestKind::Text,
- tile: None,
- scale: 1.0,
- tag: TEXT_TAG,
- preview: false,
- }));
- view.frame_queue = queue;
- view.frame_visible = visible_now.iter().copied().collect();
-}
-
-/// Acts on the notice bar button that was clicked.
-fn apply_notice(app: &mut PdfCraftApp, index: usize, notice: Option<Notice>) {
- match notice {
- #[cfg(not(target_arch = "wasm32"))]
- Some(Notice::Reload) => app.watch.reload_request = app.views.get(index).map(|view| view.id),
- #[cfg(target_arch = "wasm32")]
- Some(Notice::Reload) => {}
- Some(Notice::Repairs) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Advanced)),
- Some(Notice::Security) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Security)),
- Some(Notice::Unlock) => {
- if let Some(doc) = app.views.get(index).and_then(|view| app.session.get(view.id)) {
- app.unlock_prompt = Some(crate::UnlockPrompt::new(doc, None));
- }
-- }
- };
- // Flags the view: the tab closes at the top of the next frame, where no document
- // borrow is held.
- Some(Notice::Close) => {
- if let Some(view) = app.views.get_mut(index) {
- view.close_requested = true;
- }
- }
- Some(Notice::Signatures) => app.right = Some(RightPanel::Signatures),
- Some(Notice::FieldHighlights(on)) => app.view_defaults.highlight_fields = on,
- Some(Notice::Console) => app.dialog = Some(crate::Dialog::JsConsole),
- // Back on in Preferences ▸ General.
- Some(Notice::HideSecured) => app.show_secured_notice = false,
- None => {}
- }
-}
-
-/// The snapshot notice bar's Close was clicked: close here, where no document borrow is held. The
-/// tab is looked up by id: indices may have shifted since the click, and the flag rides its own
-/// view, so a stale flag can never close the wrong tab. Whether a tab was closed (or asked about).
-fn close_requested_tab(app: &mut PdfCraftApp, index: usize) -> bool {
- let pending = app.views.get(index).filter(|v| v.close_requested).map(|v| v.id).and_then(|id| app.views.iter().position(|v| v.id == id));
- let Some(i) = pending else { return false };
- app.views[i].close_requested = false;
- if app.has_unsaved_work(i) {
- app.close_request = Some(crate::CloseRequest::Tab(app.views[i].id));
- } else {
- app.close_tab(i);
- }
- true
-}
-
-/// Where the pages are laid out, for placing the scroll area.
-#[derive(Clone, Copy)]
-struct ScrollGeometry<'a> {
- rects: &'a [Rect],
- y_shift: f32,
- content_w: f32,
- content_h: f32,
- avail: Rect,
- middle_gesture: bool,
- hand: bool,
-}
--
-/// The page view's scroll area, with the offset this frame's jumps, drags and zoom anchor ask for.
-fn page_scroll_area(ui: &egui::Ui, view: &mut DocView, geometry: &ScrollGeometry<'_>) -> egui::ScrollArea {
- let ScrollGeometry { rects, y_shift, content_w, content_h, avail, middle_gesture, hand } = *geometry;
-- let mut scroll = egui::ScrollArea::both().auto_shrink([false, false]).scroll_source(egui::scroll_area::ScrollSource {
-- drag: if middle_gesture {
-- egui::scroll_area::DragScroll::Never
- } else if app.quick_tool == QuickTool::Hand {
- } else if hand {
-- egui::scroll_area::DragScroll::Always
-- } else {
-- egui::scroll_area::DragScroll::OnTouch
-- },
-- ..Default::default()
-- });
-- // Each document keeps its own scroll position when switching tabs (#189).
-- scroll = scroll.id_salt(("page-view", view.id));
- // When a page jump or programmatic reposition occurs, or in non-continuous page display
- // where single-page wheel turns jump to the page edge, disable animation so egui's
- // queued target does not overwrite or ease the jump. For continuous page scrolling,
- // animate if motion effects are enabled.
- let has_jump = !view.shown
- || edge_turned
- || view.goto.is_some()
- || view.goto_point.is_some()
- || view.zoom_anchor.is_some()
- || view.sync_scroll.is_some()
- || (app.quick_tool == QuickTool::Hand && !middle_gesture && view.page_widget.is_some_and(|id| ui.ctx().is_being_dragged(id)));
- scroll = scroll.animated(view.continuous && animates && !has_jump);
- // Every scroll here asks for no animation. An animated area would still queue a zero-length
- // offset target for the next frame, which overwrites the jump a page turn just set.
- scroll = scroll.animated(false);
-- // A newly opened document starts at the top. eframe saves egui's memory with the settings
-- // and document ids start again at 1 every launch, so the offset stored under this id can be
-- // where an earlier session left another document.
-- if !std::mem::replace(&mut view.shown, true) {
-- scroll = scroll.scroll_offset(Vec2::ZERO);
-- }
-- // The Hand tool pans here, before the pages are laid out, so the page moves with the pointer
-- // in the frame the pointer moves (#924). Scrolling by the drag from inside the area moved the
-- // page only after that frame was drawn, a frame behind. The frame egui tells the drag from a
-- // click, the page catches up with all the pointer has moved since the press. A jump asked for
-- // below still wins.
- if app.quick_tool == QuickTool::Hand
- if hand
-- && !middle_gesture
-- && let Some(id) = view.page_widget.filter(|id| ui.ctx().is_being_dragged(*id))
-- {
-- let moved = if ui.ctx().drag_started_id() == Some(id) {
-- ui.input(|i| i.pointer.total_drag_delta()).unwrap_or(Vec2::ZERO)
-- } else {
-- ui.input(|i| i.pointer.delta())
-- };
-- if moved != Vec2::ZERO {
-- view.end_find_following();
-- }
-- // Within the content, as egui keeps it: past an end the page would show there for a
-- // frame and spring back. `max` is never NaN or below zero, so the clamp can't panic.
-- let max = vec2((content_w - avail.width()).max(0.0), (content_h - avail.height()).max(0.0));
-- scroll = scroll.scroll_offset((view.last_scroll - moved).clamp(Vec2::ZERO, max));
-- }
-- if let Some(v) = view.sync_scroll.take() {
-- scroll = scroll.scroll_offset(v);
-- }
-- if let Some((page, fx, fy, rel)) = view.zoom_anchor.take() {
-- let r = rects[page.min(rects.len() - 1)];
-- let point = pos2(r.left() + fx * r.width(), r.top() - y_shift + fy * r.height());
-- scroll = scroll.scroll_offset(anchored_scroll(point, rel, vec2(content_w, content_h), avail.size()));
-- } else if let Some((page, frac)) = view.goto.take() {
-- let page = page.min(rects.len() - 1);
-- let r = rects[page];
-- // Clamped here: egui clamps only after drawing, so a page that fits was drawn MARGIN - page_gap()
-- // too high for a frame and then hopped into place. (`avail` is the viewport, as the
-- // scroll bars float.)
-- let max_y = (content_h - avail.height()).max(0.0);
-- scroll = scroll.vertical_scroll_offset((r.top() - y_shift - view.page_gap() + frac * r.height()).max(0.0).min(max_y));
-- // Horizontally too, as Acrobat does (#2665): beside a much wider page the others sit off
-- // to one side. A page already shown whole, or one filling the window's width, keeps the
-- // scroll, so paging through a zoomed-in document stays on the same column. Otherwise a
-- // page that fits is centred, and a wider one scrolled just far enough to fill the window.
-- // The page counts with its side gutters, which keep its edge clear of the floating
-- // quick-action bar as the layout does.
-- let (x, w) = (view.last_scroll.x, avail.width());
-- let (left, right) = (r.left() - SIDE, r.right() + SIDE);
-- let target = if right - left <= w { if left >= x && right <= x + w { x } else { r.center().x - w / 2.0 } } else { x.clamp(left, right - w) };
-- if (target - x).abs() > 0.5 {
-- scroll = scroll.horizontal_scroll_offset(target.clamp(0.0, (content_w - w).max(0.0)));
-- }
-- }
- let ppp = ui.ctx().pixels_per_point();
- let scale = view.render_scale(ppp);
- let tag = scale_tag(scale);
- // Hold sharp re-renders while the zoom is still moving: a new scale every frame would
- // cancel the in-flight full-page interpret and restart it, so on a heavy page (a CAD
- // plot with ~1000 images renders for seconds) the sharp raster never arrives and the
- // view looks stuck on a soft image. The stale raster stays stretched meanwhile; the
- // sharp request goes out once the zoom settles.
- let now_scale = ui.input(|i| i.time);
- if (scale - view.last_scale).abs() > f32::EPSILON {
- view.last_scale = scale;
- view.sharp_hold_until = now_scale + 0.35;
- ui.ctx().request_repaint_after(std::time::Duration::from_millis(380));
- scroll
-}
-
-/// Notes how far the page view scrolled this frame: reader-owned scrolling ends search following,
-/// a pan holds back fresh tile renders until it settles, and a glide keeps going or stops. Whether
-/// a pan is still settling.
-fn track_scroll<T>(ui: &egui::Ui, view: &mut DocView, out: &egui::containers::scroll_area::ScrollAreaOutput<T>, glide: Vec2) -> bool {
- let scroll_offset = out.state.clamped_offset();
- // The document ScrollArea's own scroll surfaces move the offset without the wheel deltas
- // above: its two scroll-bar tracks/handles and, for touch or the Hand tool, its drag
- // background. The output gives us the area id, and egui's pinned ScrollArea registers those
- // widgets under `id.with(0)` (horizontal), `id.with(1)` (vertical) and `id.with("area")`.
- // Ask for their exact responses instead of treating any held drag as scrolling: a text
- // selection, a panel splitter or another widget's drag must not excuse the programmatic jump
- // a search flash makes, while a track click (which moves the offset with no pointer motion)
- // must still count as the reader scrolling.
- let scroll_surface = |id: egui::Id| ui.ctx().read_response(id).is_some_and(|r| r.is_pointer_button_down_on() || r.dragged() || r.clicked());
- let pointer_scrolling = scroll_surface(out.id.with(0usize)) || scroll_surface(out.id.with(1usize)) || scroll_surface(out.id.with("area"));
- if pointer_scrolling && (scroll_offset - view.last_scroll).length() > 2.0 {
- view.end_find_following();
-- }
- let sharp_held = now_scale < view.sharp_hold_until;
- view.current_scale_tag = tag;
- let hand = app.quick_tool == QuickTool::Hand;
- let tool = app.quick_tool;
- // Text selection runs for the Select tool and for the markup tools (highlight…).
- let selects_text = match tool {
- let now_settle = ui.input(|i| i.time);
- if (scroll_offset - view.last_scroll).length() > 2.0 {
- view.pan_hold_until = now_settle + 0.25;
- ui.ctx().request_repaint_after(std::time::Duration::from_millis(280));
- }
- if glide != Vec2::ZERO {
- view.fling.moved((scroll_offset - view.last_scroll).length() > 0.01);
- }
- view.last_scroll = scroll_offset;
- now_settle < view.pan_hold_until
-}
-
-/// The hover tip, find bar, size hint and the in-place editors drawn over the pages. Whether the
-/// text editor was finished.
-fn frame_overlays(ui: &mut egui::Ui, view: &mut DocView, env: &PageEnv<'_>, primary_down: bool, hover_text: Option<(Pos2, String)>) -> bool {
- let PageEnv { added, author, avail, can_modify, doc, form, info, prefs, t, today, tool, units, unobstructed, .. } = *env;
- if let Some((pos, text)) = hover_text {
- // Click-through, as a tooltip is: the pages lose the pointer under anything that isn't.
- let tip = egui::Area::new(egui::Id::unique("canvas-hover")).order(egui::Order::Tooltip).interactable(false);
- tip.fixed_pos(pos + vec2(14.0, 16.0)).show(ui.ctx(), |ui| {
- egui::Frame::popup(ui.style()).show(ui, |ui| {
- ui.set_max_width(320.0);
- ui.label(text);
- });
- });
- }
- find_bar(view, info.pages.len(), avail, ui, t);
- page_size_hint(view, info, units, avail, unobstructed, ui, t);
- if tool == QuickTool::EnhanceCamera {
- crate::camera_ui::bar(ui.ctx(), view, avail, t, can_modify);
- }
- if let Some(e) = comments::composer(ui.ctx(), view, info, prefs) {
- view.pending_edit = Some(e);
- }
- if let Some(e) = crate::edit_text_ui::overlay(ui.ctx(), view, info) {
- view.pending_edit = Some(e);
- }
- let text_box = crate::forms_ui::text_box(doc, view, form);
- if let Some(e) = crate::forms_ui::overlay(ui.ctx(), view, info, form, primary_down, text_box.as_ref(), today) {
- view.pending_edit = Some(e);
- }
- if let Some(e) = crate::fill_sign::type_box(ui.ctx(), view, info, author) {
- view.pending_edit = Some(e);
- }
- let (typed, text_done) = crate::content_ui::editor(ui.ctx(), view, info, added);
- if typed.is_some() {
- view.pending_edit = typed;
- }
- text_done
-}
-
-/// Full screen: the click turns the page unless it did something else first. It waits out the
-/// double-click time: turning on the first click's release sent a double-click's second click,
-/// meant to select a word, to the next page (review on #2070). A selection cancels it, and so do a
-/// link followed meanwhile and leaving full screen; a double-click elsewhere still turns once.
-/// `click` is `Some(back)` on the frame of a click, `back` when it was a shift-click.
-fn schedule_presentation_turn(ui: &egui::Ui, view: &mut DocView, click: Option<bool>, link_followed: bool) {
- let now = ui.input(|i| i.time);
- if let Some(back) = click
- && !link_followed
- && view.selection.is_none()
- && view.clicks < 2
- {
- let delay = ui.ctx().options(|o| o.input_options.max_double_click_delay);
- view.present_turn = Some((now + delay, !back));
- }
- if let Some((due, forward)) = view.present_turn {
- if link_followed || view.selection.is_some() || view.presentation.is_none() {
- view.present_turn = None;
- } else if now >= due {
- view.present_turn = None;
- view.step_page(forward);
- } else {
- ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64((due - now).max(0.0)));
- }
- }
-}
-
-/// Follows the link that was clicked. A link to another file comes back, to be opened once the
-/// document is no longer borrowed.
-fn follow_link(
- app: &mut PdfCraftApp,
- index: usize,
- target: Option<LinkTarget>,
-) -> Option<(pdfcraft_engine::DocId, String, pdfcraft_render::RemoteDestination)> {
- let view_id = app.views[index].id;
- match target? {
- LinkTarget::Page(p, dest) => {
- let view = &mut app.views[index];
- if let Some(doc) = app.session.get(view.id) {
- view.go_to_dest(p, dest, &doc.info);
- }
- }
- LinkTarget::Uri(u) => app.request_document_url(&u, crate::LinkOrigin::Link),
- LinkTarget::SetLayers { changes, preserve_rb } => set_layers(app, index, &changes, preserve_rb),
- LinkTarget::RemoteGoTo { file, destination, .. } => return Some((view_id, file, destination)),
- LinkTarget::Other(s) => app.notify_fmt("This PDF uses the unsupported {s} action.", &[("s", &s)]),
- }
- None
-}
-
-/// A finished marquee zoom, a signature rectangle drawn, or an empty signature field clicked.
-/// Returns the tool to go on with.
-fn finish_sign_requests(app: &mut PdfCraftApp, index: usize, mut tool: QuickTool) -> QuickTool {
- if let Some(done) = app.views[index].marquee_done.take() {
- app.finish_marquee(index, done);
- }
- // A signature rectangle was drawn, or an empty signature field clicked.
- let drawn = app.views[index].sign.drawn.take();
- if let (Some((page, rect)), QuickTool::SignArea { certify }) = (drawn, tool) {
- tool = QuickTool::Select;
- app.quick_tool = tool;
- app.start_signing(page, Some(rect), None, certify.then_some(2));
- }
- if let Some(field) = app.views[index].sign.field.take() {
- let signed = app.session.get(app.views[index].id).is_some_and(|d| d.signatures.iter().any(|s| s.field == field && s.signed));
- if signed {
- app.right = Some(RightPanel::Signatures);
- if !app.sig_expanded.contains(&field) {
- app.sig_expanded.push(field);
- }
- } else {
- let page = app.views[index].current;
- app.start_signing(page, None, Some(field), None);
- }
- }
- tool
-}
-
-/// When not scrolling continuously, only one row is shown at a time: the current page, or the
-/// current two-page spread (with the cover rule). These indices drive paging, which pages are
-/// drawn, and the content height (#752). Empty for a document with no pages.
-fn shown_row(view: &DocView, rects: &[Rect]) -> Vec<usize> {
- if rects.is_empty() {
- return Vec::new();
- }
- let last = rects.len() - 1;
- let cur = view.current.min(last);
- match view.layout {
- PageLayout::Single => vec![cur],
- PageLayout::TwoUp if view.cover && cur == 0 => vec![0],
- PageLayout::TwoUp => {
- let first = if view.cover { cur - (cur + 1) % 2 } else { cur - cur % 2 };
- if first < last { vec![first, first + 1] } else { vec![first] }
- }
- }
-}
-
-/// Full screen: a click on the document goes forward a page and a shift-click back, as in
-/// Acrobat, unless the click did something else (a link, a word selected). Decided once the pages
-/// have had the click. `Some(back)` on the frame of a click, `back` when it was a shift-click.
-fn presentation_click_state(ui: &egui::Ui, view: &DocView, navigation_rect: Rect, enabled: bool) -> Option<bool> {
- let clicked = view.presentation.is_some() && enabled && presentation_click(ui, navigation_rect);
- // Shift as held now, or as the click's own event carried it (a synthetic click).
- clicked.then(|| {
- ui.input(|i| {
- i.modifiers.shift
- || i.events.iter().any(|e| {
- matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: false, modifiers, .. } if modifiers.shift)
- })
- })
- })
-}
-
-/// Pinch / ctrl+scroll zoom, anchored on the pointer or the current page.
-fn pinch_zoom(ui: &egui::Ui, view: &mut DocView, zoom_delta: f32, navigation_rect: Rect, enabled: bool) {
- if zoom_delta.is_finite() && zoom_delta > 0.0 && zoom_delta != 1.0 && enabled && ui.rect_contains_pointer(navigation_rect) {
- let z = view.zoom * zoom_delta;
- match zoom_anchor(ui) {
- Some(p) => view.zoom_at(z, p),
- None => view.set_zoom(z),
- }
- }
-}
-
-/// The middle-button auto-scroll's delta, and the edge scroll a drawing in progress asks for: the
-/// pointer in a band along any edge (or just past it) scrolls the view that way, so a point
-/// beyond what is shown can be reached without letting go of the tool.
-fn auto_scroll_deltas(ui: &egui::Ui, view: &mut DocView, avail: Rect, unobstructed: bool) -> (Vec2, Vec2) {
- let auto_delta = if unobstructed {
- view.auto_scroll.update(ui, avail, false)
- } else {
- view.auto_scroll.cancel();
- Vec2::ZERO
- };
-
- // Drawing a comment: the pointer in a band along any edge (or just past it) scrolls the view
- // that way, so a point beyond what is shown can be reached without letting go of the tool.
- let edge_delta = if unobstructed && matches!(view.comments.gesture, Some(comments::Gesture::Draw { .. })) {
- let (pointer, dt) = drawing_edge_pointer(ui);
- crate::autoscroll::edge_scroll(pointer, avail, dt)
- } else {
- Vec2::ZERO
- };
- if edge_delta != Vec2::ZERO {
- ui.ctx().request_repaint();
- }
- (auto_delta, edge_delta)
-}
-
-/// What decides whether touchpad momentum goes on.
-#[derive(Clone, Copy)]
-struct GlideCx {
- now: f64,
- may_glide: bool,
- key_scroll: f32,
- auto_delta: Vec2,
- zoom_delta: f32,
- edge_delta: Vec2,
- wheel_scrolled: bool,
-}
-
-/// Touchpad momentum (#531): a swipe over the page lifted at speed glides on and slows to a stop,
-/// through the same un-eased path. Never while the pager owns the wheel, a gesture or dialog has
-/// the input, or animations are off; any click, key, zoom or other scrolling ends it. Any
-/// reader-owned motion ends automatic search following, even when it stays on the same page; the
-/// automatic flash uses `goto`, not these deltas, so its programmatic scroll is never mistaken for
-/// a reader action. Returns this frame's glide.
-fn step_glide(ui: &egui::Ui, view: &mut DocView, avail: Rect, cx: &GlideCx) -> Vec2 {
- let GlideCx { now, may_glide, key_scroll, auto_delta, zoom_delta, edge_delta, wheel_scrolled } = *cx;
- let over_page = ui.rect_contains_pointer(avail);
- ui.input(|i| view.fling.read(&i.events, now, may_glide && over_page));
- let interrupted = glide_interrupted(ui);
- if !may_glide || interrupted || key_scroll != 0.0 || auto_delta != Vec2::ZERO || zoom_delta != 1.0 {
- view.fling.stop();
- }
- let glide = view.fling.step(now);
- if view.fling.gliding() {
- ui.ctx().request_repaint();
- }
- if wheel_scrolled || auto_delta != Vec2::ZERO || edge_delta != Vec2::ZERO || glide != Vec2::ZERO {
- view.end_find_following();
- }
- glide
-}
-
-/// Text selection runs for the Select tool and for the markup tools (highlight…).
-fn tool_selects_text(tool: QuickTool, preparing: bool) -> bool {
- match tool {
-- QuickTool::Comment(t) => t.markup().is_some() || t == comments::CommentTool::ReplaceText,
-- QuickTool::Select => !preparing,
-- QuickTool::Redact | QuickTool::ColumnSelect => true,
-- QuickTool::Hand
-- | QuickTool::Measure(_)
-- | QuickTool::Crop
-- | QuickTool::EnhanceCamera
-- | QuickTool::Fill(_)
-- | QuickTool::Field(_)
-- | QuickTool::AddText
-- | QuickTool::EditText
-- | QuickTool::Stamp(_)
-- | QuickTool::CustomStamp(_)
-- | QuickTool::Link
-- | QuickTool::SignArea { .. }
-- | QuickTool::MarqueeZoom
-- | QuickTool::Snapshot => false,
- }
-}
-
-/// Holds sharp re-renders while the zoom is still moving: a new scale every frame would cancel the
-/// in-flight full-page interpret and restart it, so on a heavy page (a CAD plot with ~1000 images
-/// renders for seconds) the sharp raster never arrives and the view looks stuck on a soft image.
-/// The stale raster stays stretched meanwhile; the sharp request goes out once the zoom settles.
-fn hold_sharp_renders(ui: &egui::Ui, view: &mut DocView, scale: f32) -> bool {
- let now_scale = ui.input(|i| i.time);
- if (scale - view.last_scale).abs() > f32::EPSILON {
- view.last_scale = scale;
- view.sharp_hold_until = now_scale + 0.35;
- ui.ctx().request_repaint_after(std::time::Duration::from_millis(380));
- }
- now_scale < view.sharp_hold_until
-}
-
-/// A destination's position (bookmarks, links) refines `goto`: its point at the window's top-left,
-/// or the centre of a /FitR rectangle at the window's centre.
-fn refine_goto_point(mut scroll: egui::ScrollArea, view: &mut DocView, geometry: &ScrollGeometry<'_>) -> egui::ScrollArea {
- let ScrollGeometry { rects, y_shift, content_w, content_h, avail, .. } = *geometry;
- if let Some((page, fx, fy, rel)) = view.goto_point.take() {
- let r = rects[page.min(rects.len() - 1)];
- // Within the content, so an unreachable point never shows a frame scrolled past the end.
- let (max_x, max_y) = ((content_w - avail.width()).max(0.0), (content_h - avail.height()).max(0.0));
- if let Some(fx) = fx {
- scroll = scroll.horizontal_scroll_offset((r.left() + fx * r.width() - rel.x).clamp(0.0, max_x));
- }
- if let Some(fy) = fy {
- scroll = scroll.vertical_scroll_offset((r.top() - y_shift + fy * r.height() - rel.y).clamp(0.0, max_y));
- }
- }
- scroll
-}
-
-/// Draws the document tab at `index`: its notice bars, then its pages (or the Organize grid) in a
-/// scroll area with this frame's zoom, scrolling, tool input, overlays and render requests.
-pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
- let t = Tokens::get(ui.ctx());
- // Keyboard scrolling applies to this frame's page view only, never later.
- let key_scroll = std::mem::take(&mut app.views[index].key_scroll);
- if key_scroll != 0.0 {
- app.views[index].end_find_following();
- }
- // The Search panel closed: its search moves to the find bar.
- let search_open = app.right == Some(crate::RightPanel::Search);
- if let Some(f) = app.views[index].find.as_mut()
- && f.in_panel
- && !search_open
- {
- f.in_panel = false;
- }
- if close_requested_tab(app, index) {
- return;
- }
- let Some(doc) = app.session.get(app.views[index].id) else { return };
- let info = &doc.info;
- let paper = app.session.reading_colors(app.views[index].id).and_then(|c| c.params).map_or(Color32::WHITE, |p| {
- let [r, g, b] = p.background;
- Color32::from_rgb(r, g, b)
- });
- if info.pages.is_empty() {
- app.views[index].auto_scroll.cancel();
- ui.centered_and_justified(|ui| ui.label(tl!("This document has no pages.")));
- return;
- }
- // The print dialog queues its own rasters later in the frame. Closing it drops them.
- if app.dialog != Some(crate::Dialog::Print) {
- app.views[index].print_pages.clear();
- }
- // The Prepare a form panel is open (or a field tool is picked): fields are edited, not filled.
- let preparing = app.is_preparing();
- // Prepare a form ▸ Calculation order: the order's steps and links on the page.
- let calc_order = preparing && app.left_open && app.left == crate::LeftPanel::CalculationOrder;
- // Edit a PDF: added text and images can be selected, moved and edited.
- let editing_content =
- (app.left_open && app.left == crate::LeftPanel::Tool("edit")) || matches!(app.quick_tool, QuickTool::AddText | QuickTool::EditText);
- let text_style = app.text_style.clone();
- let show_secured_notice = app.show_secured_notice;
- let view = &mut app.views[index];
- // Opened without the owner password and something is restricted. The notice says so unless
- // the user turned it off (#4035): the restrictions themselves, and Document Properties ▸
- // Security, stay as they are.
- let secured = show_secured_notice && doc.security_summary().is_some_and(|s| !(s.owner || (s.permissions.modify() && s.permissions.assemble())));
- let repaired = !doc.repair_log().is_empty();
- let notice = notices(view, doc, secured, repaired, crate::sign_ui::banner(&doc.signatures), ui, &t);
- apply_notice(app, index, notice);
- let Some(doc) = app.session.get(app.views[index].id) else { return };
- let info = &doc.info;
- // No dialog, close prompt or palette over the page: only then does page input count.
- let modal = app.modal_open();
- let unobstructed = !modal && !app.palette_open;
- let organize_zoom = app.organize_zoom;
- let view = &mut app.views[index];
- if view.organize {
- // One remembered grid size for every document, kept in the settings.
- view.set_grid_zoom(organize_zoom);
- organize_grid(view, info, doc.allows_assembly(), doc.dirty, unobstructed, ui, &t);
- app.organize_zoom = view.grid_zoom();
- return;
- }
-
- let avail = ui.available_rect_before_wrap();
- view.viewport_w = avail.width();
- view.viewport_h = avail.height();
- let old_zoom = view.zoom;
- view.fit_zoom(info);
- anchor_zoom_change(view, avail.width(), old_zoom);
-
- // Pinch / ctrl+scroll zoom, anchored on the current page.
- let zoom_delta = crate::motion::zoom_delta(ui.ctx());
- // Exclude the previous frame's scrollbar tracks from gesture activation.
- let navigation_rect = if view.viewport_screen.is_positive() { avail.intersect(view.viewport_screen) } else { avail };
- let navigation_enabled = app.dialog.is_none() && app.close_request.is_none() && !app.palette_open && ui.is_enabled() && ui.input(|i| i.focused);
- let present = presentation_click_state(ui, view, navigation_rect, navigation_enabled && app.quick_tool == QuickTool::Select);
- pinch_zoom(ui, view, zoom_delta, navigation_rect, navigation_enabled);
- view.viewport_screen = avail;
- let (auto_delta, edge_delta) = auto_scroll_deltas(ui, view, avail, unobstructed);
-
- let content_w = view.content_width(info, avail.width());
- let rects = view.layout(info, content_w, ui.ctx().pixels_per_point());
- // When not scrolling continuously, only one row is shown at a time: the current page, or the
- // current two-page spread (with the cover rule). These indices drive paging, which pages are
- // drawn, and the content height (#752). Empty for a document with no pages.
- let row = shown_row(view, &rects);
- let middle_gesture = view.auto_scroll.blocks_input();
- // Not scrolling: when the whole page or spread fits, the wheel would do nothing, so it turns
- // pages instead; zoomed in far enough to pan, it pans, and at the edge it turns the page on
- // (#1924, `single_page_wheel_edge`). Touch drags are untouched: with
- // nothing to pan, touch users turn pages with the rail buttons, the page box and the arrow
- // keys. Runs before `visible_pages` so the frame that turns the page draws it.
- let (single_paging, edge_turned) = single_page_paging(ui, view, &PagingCx { row: &row, rects: &rects, avail, middle_gesture, unobstructed });
- // Point input bypasses egui smoothing for 1:1 motion. Line input keeps egui's configured,
- // smoothed base and adds only the accelerated surplus. While point suppression is active,
- // reconstruct line and page input here so current coarse events survive without exposing an
- // older eased tail.
- let now = ui.input(|i| i.time);
- let Wheel { precise_scroll, line_base, boost_extra, has_page, paging_owns_vertical } = read_wheel(ui, view, avail, single_paging, edge_turned);
- let animates = app.animation_effects.animates(app.motion.reduced);
- let may_glide = !paging_owns_vertical && !middle_gesture && navigation_enabled && unobstructed && animates;
- let wheel_scrolled = precise_scroll != Vec2::ZERO || line_base != Vec2::ZERO || boost_extra != Vec2::ZERO || has_page;
- let motion = GlideCx { now, may_glide, key_scroll, auto_delta, zoom_delta, edge_delta, wheel_scrolled };
- let glide = step_glide(ui, view, avail, &motion);
- // The wheel may have turned the page above: draw the row it turned to. Measured from the old
- // row, the jump to the new page scrolled both out of view, a blank frame on every turn forward.
- let row = shown_row(view, &rects);
- let visible_pages: Vec<usize> = if view.continuous { (0..rects.len()).collect() } else { row.clone() };
- let (y_shift, content_h) = if view.continuous {
- (0.0, rects.iter().map(|r| r.bottom()).reduce(f32::max).map(|bottom| bottom + MARGIN).unwrap_or(0.0))
- } else {
- // Only the current row scrolls into view; the content is its bounding box plus margins.
- match row.iter().filter_map(|&i| rects.get(i)).copied().reduce(Rect::union) {
- Some(r) => (r.top() - MARGIN, r.height() + 2.0 * MARGIN),
- None => (0.0, 0.0),
- }
-- };
-
- let mut scroll = page_scroll_area(
- ui,
- view,
- &ScrollGeometry { rects: &rects, y_shift, content_w, content_h, avail, middle_gesture, hand: app.quick_tool == QuickTool::Hand },
- );
- let ppp = ui.ctx().pixels_per_point();
- let scale = view.render_scale(ppp);
- let tag = scale_tag(scale);
- // Hold sharp re-renders while the zoom is still moving: a new scale every frame would
- // cancel the in-flight full-page interpret and restart it, so on a heavy page (a CAD
- // plot with ~1000 images renders for seconds) the sharp raster never arrives and the
- // view looks stuck on a soft image. The stale raster stays stretched meanwhile; the
- // sharp request goes out once the zoom settles.
- let sharp_held = hold_sharp_renders(ui, view, scale);
- view.current_scale_tag = tag;
- let hand = app.quick_tool == QuickTool::Hand;
- let tool = app.quick_tool;
- // Text selection runs for the Select tool and for the markup tools (highlight…).
- let selects_text = tool_selects_text(tool, preparing);
-- let prefs = &app.comment_prefs;
-- let redact_mark = (app.redact_prefs.hover_fill, app.redact_prefs.opacity);
-- let units = app.units;
-- let allowed = doc.allows_annotation();
-- let can_copy = doc.allows_copying();
- let mut copy_selected = false;
-- let comments_hidden = doc.comments_hidden();
-- let form = doc.form.clone();
-- let generation = doc.edit_generation();
-- let can_fill = doc.allows_form_filling();
-- let can_crop = doc.allows_assembly();
- let mut open_boxes = false;
-- let signature = app.signature.clone();
-- let initials = app.initials.clone();
- let mut open_initials = false;
-- let author = app.comment_prefs.author.clone();
-- let today = app.session.today();
-- // In Preferences ▸ Date format, or why it can't be stamped into the PDF.
-- let date_text = crate::date_text_for_pdf(&app.session);
- let mut refused: Option<String> = None;
-- let by_line = app.session.stamp_by_line(&author);
- let mut stamp_placed = false;
- let mut image_action: Option<crate::edit_text_ui::ImageAction> = None;
-- let custom_stamp = match app.quick_tool {
-- QuickTool::CustomStamp(i) => app.custom_stamps.get(i).cloned(),
-- _ => None,
-- };
- let mut open_signature = false;
- let mut hover_text: Option<(Pos2, String)> = None;
- let mut badge_hover: Option<(Pos2, String)> = None;
- let mut clicked_link: Option<LinkTarget> = None;
- let mut remote_open: Option<(pdfcraft_engine::DocId, String, pdfcraft_render::RemoteDestination)> = None;
- let mut canvas_action: Option<comments::CanvasAction> = None;
- let mut page_action: Option<PageMenuAction> = None;
- let mut field_menu: Option<FieldMenu> = None;
-- let mut open_props: Option<(usize, usize)> = None;
- let mut field_props = false;
- let mut field_placed = false;
- let mut open_table: Option<(usize, [f64; 4])> = None;
-- // The table being set up in its window, shown on its own document's page.
-- let table_preview = app.field_table_draft.as_mut().filter(|d| d.doc == view.id).and_then(|d| Some(d.preview(info.pages.get(d.page)?, &form)));
-- let can_modify = doc.allows_modification();
-- if preparing {
-- crate::prepare::after_refresh(view, &form);
-- } else {
-- view.prepare.selected = None;
-- }
-- view.objects.prepare(doc, tool == QuickTool::EditText && can_modify, ui.ctx());
-- view.text_background.prepare(ui.ctx(), doc, view.line_editor.as_ref(), view.content.draft.as_ref(), scale);
-- let added = if editing_content { doc.added().to_vec() } else { Vec::new() };
-- let doc_links = if tool == QuickTool::Link { doc.links().to_vec() } else { Vec::new() };
-- if editing_content {
-- crate::content_ui::after_refresh(view, &added);
-- } else {
-- view.content.selected = None;
-- }
-- view.move_background.prepare(ui.ctx(), doc, &view.content, scale);
--
- // A destination's position (bookmarks, links) refines `goto`: its point at the window's
- // top-left, or the centre of a /FitR rectangle at the window's centre.
- if let Some((page, fx, fy, rel)) = view.goto_point.take() {
- let r = rects[page.min(rects.len() - 1)];
- // Within the content, so an unreachable point never shows a frame scrolled past the end.
- let (max_x, max_y) = ((content_w - avail.width()).max(0.0), (content_h - avail.height()).max(0.0));
- if let Some(fx) = fx {
- scroll = scroll.horizontal_scroll_offset((r.left() + fx * r.width() - rel.x).clamp(0.0, max_x));
- }
- if let Some(fy) = fy {
- scroll = scroll.vertical_scroll_offset((r.top() - y_shift + fy * r.height() - rel.y).clamp(0.0, max_y));
- }
- }
- scroll = refine_goto_point(scroll, view, &ScrollGeometry { rects: &rects, y_shift, content_w, content_h, avail, middle_gesture, hand });
-- let selection_wheel = view.selection_drag && navigation_enabled && unobstructed && !single_paging && ui.rect_contains_pointer(avail);
- let env = PageEnv {
- added: &added,
- allowed,
- author: &author,
- auto_delta,
- avail,
- boost_extra,
- by_line: &by_line,
- calc_order,
- can_copy,
- can_crop,
- can_fill,
- can_modify,
- comments_hidden,
- content_h,
- content_w,
- custom_stamp: &custom_stamp,
- date_text: &date_text,
- doc,
- doc_links: &doc_links,
- edge_delta,
- editing_content,
- form: &form,
- generation,
- glide,
- hand,
- info,
- initials: &initials,
- key_scroll,
- middle_gesture,
- modal,
- navigation_enabled,
- paper,
- ppp,
- precise_scroll,
- prefs,
- preparing,
- rects: &rects,
- redact_mark,
- scale,
- selection_wheel,
- selects_text,
- signature: &signature,
- single_paging,
- t: &t,
- table_preview: &table_preview,
- tag,
- text_style: &text_style,
- tool,
- units,
- unobstructed,
- visible_pages: &visible_pages,
- y_shift,
- fill_text_size: app.fill_text_size,
- fill_text_color: app.fill_text_color,
- today,
- };
- let mut page_out = PageOut::default();
-- let out = page_scroll(ui, scroll, egui::Id::unique(("page-view-scope", view.id)), |ui, viewport| {
- // egui's drag responses accept every pointer button. Keep a wheel gesture from also
- // selecting text, drawing a mark, or panning with the Hand tool.
- if middle_gesture {
- let opacity = ui.opacity();
- ui.disable();
- // Only interactions pause; the document must keep its original colours.
- ui.set_opacity(opacity);
- }
- // Middle-button auto-scroll, the keyboard, precise touchpad input (#759) and its glide
- // (#531), accelerated wheel surplus (#1072), and a drawing's edge scroll; positive y
- // moves the content down. All scroll without easing, unlike egui's wheel default.
- // ScrollArea ignores wheel input during a widget drag. Defer the wheel contribution
- // until after the text widgets when a selection owns that drag.
- let wheel_delta = if selection_wheel { Vec2::ZERO } else { precise_scroll + boost_extra };
- let delta = auto_delta + edge_delta - vec2(0.0, key_scroll) + wheel_delta + glide;
- if delta != Vec2::ZERO && delta.is_finite() {
- let anim = if animates && key_scroll != 0.0 { ui.style().scroll_animation } else { egui::style::ScrollAnimation::none() };
- ui.scroll_with_delta_animation(delta, anim);
- }
- let (resp_rect, resp) = ui.allocate_exact_size(vec2(content_w, content_h), Sense::click_and_drag());
- view.page_widget = Some(resp.id);
- // A drag lasts only while egui reports it, so the mark can't outlive one.
- if !resp.dragged_by(egui::PointerButton::Primary) {
- view.selection_drag = false;
- }
- // The Hand tool pans by this widget's drag, before the pages are laid out (above).
- if hand {
- if resp.dragged() {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
- } else if resp.hovered() {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);
- }
- }
- let origin = resp_rect.min - vec2(0.0, y_shift);
- let painter = ui.painter();
- let visible = viewport.translate(resp_rect.min.to_vec2());
- let mut wanted = Vec::new();
- let mut visible_now = Vec::new();
- view.screen_rects.clear();
- view.screen_xforms.clear();
- for &i in &visible_pages {
- let r = snap_to_pixels(rects[i].translate(origin.to_vec2()), ppp);
- if r.intersects(visible) {
- view.screen_rects.push((i, r));
- view.screen_xforms
- .push((i, PageXform { rect: r, rot: view.rotation, pw: info.pages[i].width.max(1.0), ph: info.pages[i].height.max(1.0) }));
- }
- }
- // Visible heights (points) closer than this are equal: one page size can meet the
- // viewport a fraction of a point differently once the scroll offset is applied.
- const TIE: f32 = 0.5;
- let mut current = view.current;
- let mut best_overlap = -1.0f32;
- let mut current_overlap = -1.0f32;
- let mut current_height = f32::INFINITY;
- // The pointer as the pages see it: nothing over them (a dialog from the frame it opens),
- // or a press they hold. The page modules read it rather than egui's raw pointer state.
- let ptr = PagePointer::read(ui, &resp, modal);
- let pointer = ptr.pos;
- // A press on a page makes it the current one (as in Acrobat), so a page command with
- // no selection (Rotate, Delete) acts on the page the reader pointed at, not on a page
- // gone to earlier that still shows whole beside it.
- let pressed = unobstructed && ptr.pressed().is_some();
- let mut pressed_page = None;
- if resp.secondary_clicked() {
- // Set again below when the right-click lands on a link.
- view.links.context = None;
- let mut over_image = false;
- if tool == QuickTool::EditText {
- let pointer_pos = ptr.pos;
- if let Some(p) = pointer_pos {
- for (page_idx, xf) in &view.screen_xforms {
- let images = doc.page_images(*page_idx);
- for (idx, img) in images.iter().enumerate().rev() {
- let obb = ImageObb::from_image(img, xf, info, *page_idx);
- let b = image_screen_rect(img, xf, info, *page_idx);
- if (b.is_positive() && b.contains(p)) || crate::edit_text_ui::quad_contains(obb.corners(), p) {
- over_image = true;
- if view.image_selection.as_ref().is_none_or(|s| s.page != *page_idx || s.index != idx) {
- view.image_selection = Some(crate::edit_text_ui::ImageSelection {
- page: *page_idx,
- index: idx,
- drag: None,
- crop: None,
- crop_mode: false,
- crop_rect: None,
- turning: false,
- settled: None,
- });
- }
- break;
- }
- }
- if over_image {
- break;
- }
- }
- }
- }
- ui.ctx().data_mut(|d| d.insert_persisted(crate::edit_text_ui::image_menu_id(), over_image));
- }
- let mut calc_hover = None;
- for &i in &visible_pages {
- badge_hover = None;
- let r = snap_to_pixels(rects[i].translate(origin.to_vec2()), ppp);
- if !r.intersects(visible.expand(400.0)) {
- continue;
- }
- if pressed && pressed_page.is_none() && pointer.is_some_and(|p| r.contains(p)) {
- pressed_page = Some(i);
- }
- if r.intersects(visible) {
- visible_now.push(i);
- }
- let overlap = r.intersect(visible).height();
- if i == view.current {
- current_overlap = overlap;
- current_height = r.height();
- }
- // Pages shown equally (two rows wholly on screen) differ only by rounding: the
- // topmost one counts.
- if overlap > best_overlap + TIE {
- best_overlap = overlap;
- current = i;
- }
- let xf = PageXform { rect: r, rot: view.rotation, pw: info.pages[i].width.max(1.0), ph: info.pages[i].height.max(1.0) };
- // Shadows would darken the preceding page when pages touch.
- if view.show_page_gaps {
- painter.add(egui::epaint::Shadow { offset: [0, 3], blur: 14, spread: 0, color: t.page_shadow }.as_shape(r, CornerRadius::ZERO));
- }
- painter.rect_filled(r, CornerRadius::ZERO, paper);
- if let Some(err) = view.errors.get(&i) {
- painter.rect_filled(r, CornerRadius::ZERO, Color32::from_rgb(0xFB, 0xF4, 0xF4));
- icons::paint(
- ui,
- Rect::from_center_size(r.center() - vec2(0.0, 26.0), vec2(28.0, 28.0)),
- "triangle-alert",
- 26.0,
- Color32::from_rgb(0xC8, 0x3A, 0x3A),
- );
- let message = crate::i18n::fmt(tl!("This page couldn't be displayed.\n{e}"), &[("e", err)]);
- let msg =
- ui.fonts_mut(|f| f.layout(message, theme::regular(12.5), Color32::from_rgb(0x6A, 0x2A, 0x2A), (r.width() - 40.0).max(80.0)));
- painter.galley(pos2(r.center().x - msg.size().x / 2.0, r.center().y), msg, Color32::BLACK);
- } else {
- let (pw_pt, ph_pt) = (info.pages[i].width.max(1.0), info.pages[i].height.max(1.0));
- // A GPU may take smaller textures than these (OpenGL drivers report as little as
- // 2048 pixels, and uploading a bigger one aborts), so both stay within its limit.
- let max_side = ui.ctx().input(|inp| inp.max_texture_side) as f32;
- let tiled = pw_pt.max(ph_pt) * scale > TILE_THRESHOLD.min(max_side);
- // Whole-page raster: sharp when small, a low-res backdrop when tiled.
- let (want_scale, want_tag) = if tiled {
- let bs = BASE_SIDE.min(max_side) / pw_pt.max(ph_pt);
- (bs, scale_tag(bs))
- } else {
- (scale, tag)
- };
- // Under tiles, a whole-page raster sharper than the backdrop (from before the page
- // grew past the tiling size) is a better backdrop: keep demanding it as it is.
- match view.pages.get(&i).filter(|p| tiled && p.tag != STALE_TAG && tag_scale(p.tag) >= want_scale) {
- Some(p) => wanted.push((i, tag_scale(p.tag), p.tag, None)),
- None => wanted.push((i, want_scale, want_tag, None)),
- }
- let active_drag_suppress = if let Some(sel) = view.image_selection.as_ref()
- && sel.page == i
- && sel.drag.is_some()
- {
- let images = doc.page_images(i);
- if let Some(img) = images.get(sel.index) {
- let obb = ImageObb::from_image(img, &xf, info, i);
- let uvs = obb.corners().map(|c| xf.screen_to_norm(c));
- let u0 = uvs.iter().map(|(u, _)| *u).fold(1.0f32, f32::min).clamp(0.0, 1.0);
- let u1 = uvs.iter().map(|(u, _)| *u).fold(0.0f32, f32::max).clamp(0.0, 1.0);
- let v0 = uvs.iter().map(|(_, v)| *v).fold(1.0f32, f32::min).clamp(0.0, 1.0);
- let v1 = uvs.iter().map(|(_, v)| *v).fold(0.0f32, f32::max).clamp(0.0, 1.0);
- if u1 > u0 && v1 > v0 { Some([u0, v0, u1, v1]) } else { None }
- } else {
- None
- }
- } else {
- None
- };
- match view.pages.get(&i) {
- Some(p) => {
- if !tiled && p.tag == want_tag {
- paint_image_suppressed(
- &xf.texel_aligned(p.tex.size(), ppp),
- painter,
- p.tex.id(),
- 0.0,
- 0.0,
- 1.0,
- 1.0,
- active_drag_suppress,
- true,
- );
- } else {
- // A backdrop, or a raster at an older scale until the new one arrives.
- paint_image_suppressed(&xf, painter, p.tex.id(), 0.0, 0.0, 1.0, 1.0, active_drag_suppress, true);
- }
- }
- None => {
- let now = ui.input(|inp| inp.time);
- let since = *view.waiting_since.entry(i).or_insert(now);
- let msg = if now - since > 6.0 { "Still rendering — this page is unusually complex…" } else { "Rendering…" };
- painter.text(r.center(), Align2::CENTER_CENTER, tl!(msg), theme::regular(12.0), t.text_faint);
- }
- }
- if tiled && r.intersects(visible) {
- // Device-pixel geometry of the scaled page, and the visible part of it
- // (found by mapping the visible screen corners back into the page). Sides round
- // up as whole-page rasters do, so the last partial row and column get tiles.
- let (dw, dh) = (device_pixels(pw_pt, scale), device_pixels(ph_pt, scale));
- // Adaptive tile side for this zoom and page, within the GPU texture limit:
- // a zoomed viewport or a giant sheet is covered by a few large tiles
- // instead of many small ones, and each tile costs one full page interpret.
- // Never zero (u32 division below).
- let ts = tile_side(scale, dw.max(dh)).min(max_side as u32).max(64);
- let vis = r.intersect(visible);
- let corners = [vis.left_top(), vis.right_top(), vis.right_bottom(), vis.left_bottom()].map(|c| xf.screen_to_norm(c));
- let (u0, u1) = corners.iter().fold((1.0f32, 0.0f32), |(a, b), c| (a.min(c.0), b.max(c.0)));
- let (v0, v1) = corners.iter().fold((1.0f32, 0.0f32), |(a, b), c| (a.min(c.1), b.max(c.1)));
- let (vx0, vy0) = ((u0.max(0.0) * dw as f32) as u32, (v0.max(0.0) * dh as f32) as u32);
- let (vx1, vy1) = (((u1.min(1.0) * dw as f32).ceil() as u32).min(dw), ((v1.min(1.0) * dh as f32).ceil() as u32).min(dh));
- // Tiles of earlier zooms first, stretched, the nearest scale last (on top):
- // until the current tiles arrive, a zoom shows them rather than the backdrop.
- let mut older: Vec<(f32, u32, u32, &TextureHandle)> = view
- .tiles
- .iter()
- .filter(|((p, t, _, _), _)| *p == i && *t != tag)
- .map(|((_, t, ox, oy), tex)| (tag_scale(*t), *ox, *oy, tex))
- .collect();
- older.sort_by(|a, b| (b.0 / scale).ln().abs().total_cmp(&(a.0 / scale).ln().abs()));
- for (s, ox, oy, tex) in older {
- let (ow, oh) = (device_pixels(pw_pt, s) as f32, device_pixels(ph_pt, s) as f32);
- let [w, h] = tex.size();
- let (x, y) = (ox as f32, oy as f32);
- paint_image_suppressed(
- &xf,
- painter,
- tex.id(),
- x / ow,
- y / oh,
- (x + w as f32) / ow,
- (y + h as f32) / oh,
- active_drag_suppress,
- false,
- );
- }
- // Visible tiles, nearest the viewport centre first: each tile costs a full
- // page interpret, so the workers sharpen what is looked at before the edges.
- let (vcx, vcy) = ((vx0 + vx1) as f32 / 2.0, (vy0 + vy1) as f32 / 2.0);
- let mut coords: Vec<(u32, u32)> = Vec::new();
- for ty in vy0 / ts..=(vy1.saturating_sub(1)) / ts {
- for tx in vx0 / ts..=(vx1.saturating_sub(1)) / ts {
- coords.push((tx, ty));
- }
- }
- coords.sort_by(|a, b| {
- let da = ((a.0 * ts + ts / 2) as f32 - vcx).abs() + ((a.1 * ts + ts / 2) as f32 - vcy).abs();
- let db = ((b.0 * ts + ts / 2) as f32 - vcx).abs() + ((b.1 * ts + ts / 2) as f32 - vcy).abs();
- da.total_cmp(&db)
- });
- for (tx, ty) in coords {
- let (x, y) = (tx * ts, ty * ts);
- let (w, h) = (ts.min(dw.saturating_sub(x)), ts.min(dh.saturating_sub(y)));
- if w == 0 || h == 0 {
- continue;
- }
- wanted.push((i, scale, tag, Some(Tile { x, y, w, h })));
- if let Some(tex) = view.tiles.get(&(i, tag, x, y)) {
- let (fw, fh) = (dw as f32, dh as f32);
- paint_image_suppressed(
- &xf.texel_aligned([dw as usize, dh as usize], ppp),
- painter,
- tex.id(),
- x as f32 / fw,
- y as f32 / fh,
- (x + w) as f32 / fw,
- (y + h) as f32 / fh,
- active_drag_suppress,
- false,
- );
- }
- }
- }
- }
- view.text_background.paint(painter, &xf, i, view.line_editor.as_ref(), view.content.draft.as_ref());
- if editing_content {
- view.move_background.paint(painter, &xf, i, info, &view.content, pointer);
- }
- if view.show_page_gaps {
- painter.rect_stroke(r, CornerRadius::ZERO, Stroke::new(0.5, t.border.gamma_multiply(0.8)), egui::StrokeKind::Outside);
- }
-
- // Comments: tools, selection, moving and resizing come before text selection.
- let pcx = comments::PageCx { page: i, xf: &xf, ptr: &ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
- if let QuickTool::Measure(measure_tool) = tool {
- crate::measure_ui::page_input(ui, &resp, doc, view, &pcx, measure_tool, units);
- }
-
- // Form fields take clicks first with the Select tool (as Acrobat fills fields in
- // every viewing mode); then comments; then text selection.
- if let QuickTool::Stamp(kind) = tool
- && allowed
- && let Some(p) = ptr.within(xf.rect)
- {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
- if resp.clicked() {
- // Centred on the click, upright as the page is shown.
- let (vx, vy) = xf.screen_to_view(p);
- let (w, h) = kind.size();
- let corners = [(vx as f64 - w / 2.0, vy as f64 - h / 2.0), (vx as f64 + w / 2.0, vy as f64 + h / 2.0)];
- let u: Vec<[f32; 2]> = corners.iter().map(|(x, y)| info.pages[i].view_to_user(*x as f32, *y as f32)).collect();
- let rect = [u[0][0].min(u[1][0]) as f64, u[0][1].min(u[1][1]) as f64, u[0][0].max(u[1][0]) as f64, u[0][1].max(u[1][1]) as f64];
- let by = (kind.group() == pdfcraft_engine::StampGroup::Dynamic).then(|| by_line.clone());
- let shape = pdfcraft_engine::Shape::Stamp { rect, stamp: kind, by };
- view.pending_edit = Some(pdfcraft_engine::Edit::AddAnnotation(pdfcraft_engine::NewAnnotation {
- page: i,
- style: pdfcraft_engine::Style::default_for(&shape),
- shape,
- contents: String::new(),
- author: author.clone(),
- }));
- stamp_placed = true;
- }
- }
- if let Some(cs) = custom_stamp.as_ref()
- && allowed
- && let Some(p) = ptr.within(xf.rect)
- {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
- if resp.clicked() {
- // Centred on the click at its natural size (the engine sizes it).
- let (vx, vy) = xf.screen_to_view(p);
- let u = info.pages[i].view_to_user(vx, vy);
- let (x, y) = (u[0] as f64, u[1] as f64);
- view.pending_edit = Some(pdfcraft_engine::Edit::AddCustomStamp {
- page: i,
- rect: [x, y, x, y],
- name: cs.name.clone(),
- file: pdfcraft_engine::MarkFile { name: cs.file.clone(), bytes: cs.data.clone(), page: cs.page },
- author: author.clone(),
- });
- stamp_placed = true;
- }
- }
- // Over a mark already placed, the Fill & Sign tool picks it up (comments, below).
- if let QuickTool::Fill(ft) = tool
- && allowed
- && !comments::fill_grabs(&pcx, view)
- {
- match crate::fill_sign::page_input(
- ui,
- &resp,
- &xf,
- &ptr,
- i,
- info,
- ft,
- app.fill_text_size,
- view,
- signature.as_ref(),
- initials.as_ref(),
- &mut app.signature_preview,
- &author,
- &date_text,
- ) {
- Some(crate::fill_sign::FillAction::Edit(e)) => view.pending_edit = Some(*e),
- Some(crate::fill_sign::FillAction::Signature(e)) => {
- view.pending_edit = Some(*e);
- view.fill_signature_page = Some(i);
- }
- Some(crate::fill_sign::FillAction::CreateSignature) => open_signature = true,
- Some(crate::fill_sign::FillAction::CreateInitials) => open_initials = true,
- Some(crate::fill_sign::FillAction::Refused(why)) => refused = Some(why),
- None => {}
- }
- }
- if matches!(tool, QuickTool::SignArea { .. }) {
- crate::sign_ui::page_input(ui, &resp, &xf, &ptr, i, info, view);
- }
- if matches!(tool, QuickTool::MarqueeZoom | QuickTool::Snapshot) {
- crate::zoom_snap::page_input(ui, &resp, &xf, &ptr, i, view);
- }
- if tool == QuickTool::EnhanceCamera {
- crate::camera_ui::page_input(ui, &resp, &xf, &ptr, i, view, can_modify);
- }
- if tool == QuickTool::Crop && crate::crop::page_input(ui, &resp, &xf, &ptr, i, info, view, can_crop) {
- view.current = i;
- open_boxes = true;
- }
- // `over_field`: a form field is under the pointer and has set its cursor.
- let (on_field, over_field) = if preparing {
- let field_tool = match tool {
- QuickTool::Field(f) => Some(f),
- _ => None,
- };
- // While a table is being set up, nothing else is drawn or moved.
- let allowed = can_modify && table_preview.is_none() && open_table.is_none();
- let o = crate::prepare::page_input(ui, &resp, &xf, &ptr, i, info, &form, field_tool, allowed, view);
- field_props |= o.properties;
- field_placed |= o.placed;
- open_table = open_table.or(o.table);
- (o.consumed || field_tool.is_some(), false)
- } else {
- use crate::forms_ui::FieldPointer;
- let field = if tool == QuickTool::Select
- && !(editing_content && can_modify && crate::content_ui::select_grabs(&xf, &ptr, i, info, &added, view))
- && !comments::select_grabs(&pcx, view)
- {
- crate::forms_ui::page_input(ui, &resp, &xf, &ptr, i, info, &form, can_fill, view)
- } else {
- FieldPointer::Away
- };
- (field == FieldPointer::Owned, field == FieldPointer::Over)
- };
- // Redact draws boxes off text; so does Highlight (an area highlight, as in Acrobat).
- let area_tool = tool == QuickTool::Redact && can_modify || tool == QuickTool::Comment(comments::CommentTool::Highlight) && allowed;
- let boxing = area_tool && {
- let text = view.page_text(i);
- let over_text = |p: Pos2| {
- let (vx, vy) = xf.screen_to_view(p);
- text.as_ref().is_some_and(|t| t.glyphs.iter().any(|g| vx >= g.rect[0] && vx <= g.rect[2] && vy >= g.rect[1] && vy <= g.rect[3]))
- };
- crate::redact_ui::page_input(ui, &resp, &xf, &ptr, i, info, over_text, view)
- };
- let image_owns_pointer = tool == QuickTool::EditText && crate::edit_text_ui::image_owns_pointer(ui, &xf, &ptr, i, info, view);
- let on_objects =
- !image_owns_pointer && tool == QuickTool::EditText && can_modify && crate::object_ui::page_input(ui, &resp, &xf, &ptr, i, doc, view);
- let lines = if matches!(tool, QuickTool::AddText | QuickTool::EditText) && can_modify {
- let generation = doc.edit_generation();
- match view.edit_lines.get(&i) {
- Some((g, l)) if *g == generation => l.clone(),
- _ => {
- let l = doc.text_blocks(i);
- view.edit_lines.insert(i, (generation, l.clone()));
- l
- }
- }
- } else {
- Vec::new()
- };
- let over_source = tool == QuickTool::AddText
- && pointer.is_some_and(|p| {
- !crate::content_ui::hits_added(&xf, info, i, &added, p)
- && lines.iter().any(|line| xf.user_rect(info, i, line.rect.map(|v| v as f32)).expand(2.0).contains(p))
- });
- let source_blocked = over_source && view.content.hold_blocked();
- if over_source && resp.clicked() && !source_blocked {
- if let Some(edit) = view.content.finish_draft(&added) {
- view.pending_edit = Some(edit);
- }
- view.content.selected = None;
- }
- let on_content = !on_objects && editing_content && can_modify && {
- source_blocked
- || crate::content_ui::page_input(
- ui,
- &resp,
- &xf,
- &ptr,
- i,
- info,
- &added,
- tool == QuickTool::AddText && !over_source,
- matches!(tool, QuickTool::AddText | QuickTool::EditText),
- &text_style,
- view,
- )
- || tool == QuickTool::AddText && !over_source
- };
- // Added content owns its gesture; its source text must not queue a second edit.
- // Edit text & images boxes every paragraph and image whenever the tool is on. Only the
- // input waits while selected objects or added content own the pointer, or while page
- // input is blocked (another tab, a dialog, the window unfocused): the boxes stay drawn
- // instead of vanishing until the pointer comes back.
- let others_own_pointer = on_objects || on_content;
- let on_edit_text = (tool == QuickTool::EditText || over_source) && can_modify && {
- let generation = doc.edit_generation();
- let images = match view.edit_images.get(&i) {
- Some((g, l)) if *g == generation => l.clone(),
- _ => {
- let l = doc.page_images(i);
- view.edit_images.insert(i, (generation, l.clone()));
- l
- }
- };
- // Editable text takes precedence over background/overlapping artwork. Both
- // inventories are painted every frame, even when text owns the pointer.
- let text_used =
- crate::edit_text_ui::page_input(ui, &resp, &xf, &ptr, i, doc, &lines, view, !others_own_pointer && !image_owns_pointer);
- let image_used = crate::edit_text_ui::image_input(
- ui,
- &resp,
- &xf,
- &ptr,
- i,
- info,
- &images,
- view,
- &mut image_action,
- !others_own_pointer && (image_owns_pointer || !text_used),
- );
- !others_own_pointer && (text_used || image_used)
- };
- let on_link = tool == QuickTool::Link && can_modify && crate::link_ui::page_input(ui, &resp, &xf, &ptr, i, info, &doc_links, view);
- if tool == QuickTool::Select
- && resp.secondary_clicked()
- && can_modify
- && let Some(p) = pointer.filter(|p| xf.rect.contains(*p))
- && let Some(l) = doc.links().iter().find(|l| l.page == i && crate::link_ui::screen_rect(&xf, info, i, l.rect).expand(2.0).contains(p))
- {
- view.links.context = Some((i, l.index));
- }
- let consumed = on_objects || on_edit_text || on_link || on_content || boxing || on_field || comments::page_input(ui, &resp, &pcx, view);
-
- let preview_target =
- (matches!(tool, QuickTool::Select | QuickTool::Fill(_)) && !comments_hidden).then_some(view.comments.selected).flatten();
- view.signature_drag.prepare(ui.ctx(), doc, preview_target, scale);
- view.signature_drag.paint(painter, &pcx, &view.comments, view.pending_edit.as_ref());
-
- // Any press on the page ends Select All of the document: text read or not, any tool,
- // on a link too.
- // A press that lands on something drawn over the page (a menu, a dialog) is not one.
- if view.all_text && pressed_page == Some(i) {
- view.all_text = false;
- }
- // Text layer: find matches, selection, I-beam and drag-to-select.
- let to_screen = |g: [f32; 4]| xf.view_rect(g);
- if let Some(text) = view.texts.get(&i).cloned() {
- if let Some(f) = &view.find {
- for (k, (mp, range)) in f.matches.iter().enumerate() {
- if *mp != i {
- continue;
- }
- let current = f.current == Some(k);
- for lr in text.line_rects(range.clone()) {
- let fill = if current {
- Color32::from_rgba_unmultiplied(255, 140, 0, 110)
- } else {
- Color32::from_rgba_unmultiplied(255, 214, 0, 90)
- };
- painter.rect_filled(to_screen(lr).expand(1.0), CornerRadius::same(2), fill);
- }
- }
- }
- if view.all_text {
- for lr in text.line_rects(0..text.glyphs.len()) {
- painter.rect_filled(to_screen(lr), CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x3A, 0x7B, 0xF0, 70));
- }
- }
- if let Some(sel) = view.selection.filter(|s| s.page == i) {
- for lr in sel.rects(&text) {
- painter.rect_filled(to_screen(lr), CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x3A, 0x7B, 0xF0, 70));
- }
- // The rectangle being drawn, as Acrobat shows it while column-selecting.
- if let Span::Column { rect } = sel.span
- && resp.dragged()
- {
- let outline = Stroke::new(1.0, Color32::from_rgb(0x3A, 0x7B, 0xF0));
- painter.rect_stroke(to_screen(rect), CornerRadius::ZERO, outline, egui::StrokeKind::Middle);
- }
- }
- if selects_text
- && !consumed
- && let Some(p) = pointer.filter(|p| r.contains(*p))
- {
- let (vx, vy) = xf.screen_to_view(p);
- let over_text = text.glyphs.iter().any(|g| vx >= g.rect[0] && vx <= g.rect[2] && vy >= g.rect[1] && vy <= g.rect[3]);
- let over_link = info.links.iter().any(|l| l.page == i && xf.user_rect(info, i, l.rect).contains(p));
- // Column select (#740): the Column select tool, or Alt (Option on macOS) held
- // while a drag starts, as in Acrobat.
- let column = tool == QuickTool::ColumnSelect || ui.input(|inp| inp.modifiers.alt);
- // A link or a form field under the pointer keeps its own cursor: a check box
- // drawn with a glyph, or set on printed text, shows the hand, not the I-beam.
- // So do the mode bar and side panels over a page scrolled under them (#2755),
- // unless a selection is being dragged there.
- let cursor_free = !over_link && !over_field && (resp.contains_pointer() || resp.dragged());
- if column && cursor_free {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
- } else if over_text && cursor_free {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Text);
- }
- // The pages' press, kept when its release came in the same frame (#2100): it
- // still starts multi-click selection.
- let press_here = ptr.origin.is_some_and(|o| r.contains(o));
- let pressed = ptr.pressed().is_some();
- if pressed && press_here && !over_link {
- let now = ui.input(|inp| inp.time);
- // The same limits egui uses to count clicks on release.
- let (max_delay, max_dist) = ui.ctx().options(|o| (o.input_options.max_double_click_delay, o.input_options.max_click_dist));
- let is_consecutive =
- now - view.last_press_time <= max_delay && view.last_press_pos.is_some_and(|pos| pos.distance(p) <= max_dist);
- view.clicks = if is_consecutive { view.clicks.saturating_add(1) } else { 1 };
- view.last_press_time = now;
- view.last_press_pos = Some(p);
-
- // Multi-click highlights immediately on pointer press instead of release (#1024).
- let clicks = if view.clicks > 2 && tool != QuickTool::Select { 1 } else { view.clicks };
- if clicks >= 2 && over_text {
- let span = text.nearest(vx, vy).and_then(|a| click_unit(&text, clicks, a));
- if let Some((first, last)) = span {
- view.selection = Some(Selection::run(i, first, last));
- }
- }
- }
- if resp.drag_started() && press_here && !over_link {
- // A drag replaces the selection without passing through none, so a
- // re-drag over text whose bar Copy closed would keep the bar hidden.
- view.actions_dismissed = None;
- view.selection_drag = true;
- let origin = ptr.origin.unwrap_or(p);
- let (ox, oy) = xf.screen_to_view(origin);
- if view.clicks < 2 {
- view.selection = if column {
- Some(Selection { page: i, span: Span::Column { rect: [ox, oy, vx, vy] } })
- } else {
- text.nearest(ox, oy).map(|a| Selection::run(i, a, a))
- };
- }
- }
- let clicks = if view.clicks > 2 && tool != QuickTool::Select { 1 } else { view.clicks };
- if resp.dragged()
- && let Some(sel) = view.selection.as_mut().filter(|s| s.page == i)
- {
- match &mut sel.span {
- // After a multi-click the drag extends by its unit (#1685), as in
- // Acrobat and browsers: whole words or lines, always keeping the one
- // clicked, which holds the anchor whichever way the drag went.
- Span::Run { anchor, head } if clicks >= 2 => {
- if let Some(h) = text.nearest(vx, vy)
- && let Some((first, last)) = click_unit(&text, clicks, *anchor)
- {
- let (to_first, to_last) = click_unit(&text, clicks, h).unwrap_or((h, h));
- (*anchor, *head) = if to_first < first { (last, to_first) } else { (first, to_last.max(last)) };
- }
- }
- Span::Run { head, .. } => {
- if let Some(h) = text.nearest(vx, vy) {
- *head = h;
- }
- }
- Span::Column { rect } => {
- rect[2] = vx;
- rect[3] = vy;
- }
- }
- }
- // A rectangle around no text selects nothing.
- if resp.drag_stopped()
- && view.selection.is_some_and(|s| s.page == i && matches!(s.span, Span::Column { .. }) && s.text(&text).is_empty())
- {
- view.selection = None;
- }
- // Quick clicks widen the selection: two a word, three the line, four the page.
- // Release-based fallback for platforms or synthetic events that report double_clicked.
- let release_clicks = if resp.triple_clicked() {
- view.clicks.max(2).saturating_add(1)
- } else if resp.double_clicked() {
- view.clicks.max(2)
- } else {
- 0
- };
- if release_clicks > 0 {
- view.clicks = release_clicks;
- }
- let clicks = if view.clicks > 2 && tool != QuickTool::Select { 1 } else { view.clicks };
- // Only on the frame egui reports the multi-click: `view.clicks` outlives it, and
- // acting on every frame re-selected the word as soon as a selection was cleared
- // (a highlight made by the double-click left its word selected).
- if release_clicks > 0 && clicks >= 2 && over_text {
- if view.selection.is_none() {
- let span = text.nearest(vx, vy).and_then(|a| click_unit(&text, clicks, a));
- if let Some((first, last)) = span {
- view.selection = Some(Selection::run(i, first, last));
- }
- }
- } else if resp.clicked() && view.clicks < 2 && !over_link {
- // ⇧-click extends a selection on this page to the click, keeping its
- // anchor (#527). Otherwise (no shift, no selection on this page, or a
- // column selection, which has no anchor glyph; a selection cannot yet
- // span pages) a click clears the selection.
- let shift = ui.input(|inp| inp.modifiers.shift);
- match (view.selection.as_mut().filter(|s| shift && s.page == i).map(|s| &mut s.span), text.nearest(vx, vy)) {
- (Some(Span::Run { head, .. }), Some(h)) => *head = h,
- _ => view.selection = None,
- }
- }
- }
- }
-
- if tool == QuickTool::Select && !consumed {
- crate::image_pick::page_input(&resp, &xf, i, info, doc, pointer, view);
- }
- crate::image_pick::drop_stale(doc, view);
- crate::image_pick::paint(painter, &xf, i, info, view);
- comments::page_after_text(&resp, &pcx, view);
- if tool == QuickTool::Redact && can_modify {
- crate::redact_ui::after_text(&resp, i, info, view);
- }
- if area_tool {
- crate::redact_ui::paint(painter, i, view, pointer);
- }
- comments::paint_page(ui, painter, &pcx, view);
- if editing_content {
- crate::content_ui::paint_page(painter, &xf, i, info, &added, view, pointer);
- }
- if tool == QuickTool::EditText {
- crate::object_ui::paint(painter, &xf, i, info, &view.objects);
- }
- if tool == QuickTool::EnhanceCamera {
- crate::camera_ui::paint(painter, &xf, i, view);
- }
- if tool == QuickTool::Link {
- crate::link_ui::paint(painter, &xf, i, info, &doc_links, view, pointer);
- }
- if preparing {
- crate::prepare::paint_page(ui, painter, &xf, i, info, &form, view, pointer);
- if let Some(preview) = &table_preview {
- crate::field_table_ui::paint_preview(painter, &xf, i, preview);
- }
- if calc_order {
- let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
- if let Some(name) =
- crate::calc_order_ui::paint_page(painter, &xf, i, info, &form, &view.calc_order, generation, selected, pointer)
- {
- calc_hover = Some(name);
- }
- }
- } else {
- crate::forms_ui::paint_page(painter, &xf, i, info, &form, view, pointer);
- }
-
- // Form-field highlight (Acrobat's "Highlight existing fields"); required fields get a
- // red border. Radio buttons and round XFA check boxes are round, and so is theirs (as
- // in Acrobat). Only what can be filled is marked: a read-only widget (its field's, or a
- // radio button's own) is left as it is drawn.
- if view.highlight_fields {
- for f in form.iter() {
- let required = f.has(pdfcraft_engine::field_flags::REQUIRED);
- for w in f.widgets.iter().filter(|w| w.page == Some(i) && !w.hidden && !w.read_only) {
- let r = w.rect;
- let sr = xf.user_rect(info, i, [r[0] as f32, r[1] as f32, r[2] as f32, r[3] as f32]);
- let tint = Color32::from_rgba_unmultiplied(0x6E, 0x8E, 0xF5, 48);
- let (width, color) =
- if required { (2.0, Color32::from_rgb(0xE3, 0x22, 0x22)) } else { (1.0, Color32::from_rgb(0x6E, 0x8E, 0xF5)) };
- if w.round {
- let radius = sr.width().min(sr.height()) / 2.0;
- painter.circle_filled(sr.center(), radius, tint);
- painter.circle_stroke(sr.center(), radius - width / 2.0, Stroke::new(width, color));
- } else {
- painter.rect_filled(sr, CornerRadius::same(1), tint);
- painter.rect_stroke(sr, CornerRadius::same(1), Stroke::new(width, color), egui::StrokeKind::Inside);
- }
- }
- }
- }
- // Fill & Sign keeps its placement cursor clear of link/comment hover feedback. Tips
- // draw above the palette, so none shows while it is open, even beside it.
- if !matches!(tool, QuickTool::Fill(_))
- && unobstructed
- && let Some(p) = pointer
- {
- for l in info.links.iter().filter(|l| l.page == i) {
- let sr = xf.user_rect(info, i, l.rect);
- if sr.contains(p) {
- ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
- let label = match &l.target {
- LinkTarget::Page(n, _) => {
- crate::i18n::fmt(tl!("Go to page {p}"), &[("p", info.pages.get(*n).map(|p| p.label.as_str()).unwrap_or("?"))])
- }
- LinkTarget::Uri(u) => u.clone(),
- LinkTarget::SetLayers { .. } => tl!("Set layer visibility").to_string(),
- LinkTarget::RemoteGoTo { file, .. } => file.clone(),
- LinkTarget::Other(s) => crate::i18n::fmt(tl!("{s} action"), &[("s", s)]),
- };
- hover_text = Some((p, label));
- if resp.clicked() && tool == QuickTool::Select && !consumed {
- clicked_link = Some(l.target.clone());
- }
- }
- }
- // Annotation hover shows the comment, as Acrobat's popups do; not while drawing
- // freehand, where it would cover the next stroke (#429).
- let freehand = matches!(tool, QuickTool::Comment(comments::CommentTool::Ink | comments::CommentTool::Eraser));
- let gesturing = view.comments.gesture.is_some() || freehand;
- // Inspection sorts annotations spatially, but the renderer paints in /Annots
- // order. Keep the shared list intact and choose the last eligible raw index.
- if hover_text.is_none()
- && !gesturing
- && let Some((_, text)) = info
- .annotations
- .iter()
- .filter(|a| a.page == i)
- .filter(|a| {
- !(prefs.hide_empty_highlight_hover && a.subtype == "Highlight" && a.contents.as_deref().is_none_or(str::is_empty))
- })
- .filter(|a| pcx.screen_rects(a).any(|r| r.contains(p)))
- .filter_map(|a| annotation_hover_text(a).map(|text| (a.index, text)))
- .max_by_key(|(index, _)| *index)
- {
- hover_text = Some((p, text));
- }
- }
- for (mp, mr, mc) in &view.compare_marks {
- if *mp == i {
- let sr = xf.user_rect(info, i, *mr).expand(1.5);
- painter.rect_filled(sr, CornerRadius::same(2), mc.gamma_multiply(0.28));
- }
- }
- if let Some((fp, fr, t0)) = view.flash
- && fp == i
- {
- let now = ui.input(|inp| inp.time);
- let t0 = if t0 == 0.0 { now } else { t0 };
- view.flash = Some((fp, fr, t0));
- let age = (now - t0) as f32;
- if age < 1.6 {
- let a = ((1.6 - age) / 1.6 * 255.0) as u8;
- let sr = xf.user_rect(info, i, fr).expand(4.0);
- painter.rect_stroke(
- sr,
- CornerRadius::same(3),
- Stroke::new(2.5, Color32::from_rgba_unmultiplied(0x1B, 0x63, 0xE0, a)),
- egui::StrokeKind::Outside,
- );
- ui.ctx().request_repaint();
- } else {
- view.flash = None;
- }
- }
- // Validation is viewer chrome over the signer's original artwork, which
- // may still say "Signature Not Verified". These badges never enter document
- // streams, image/text exports, vector printing or Print as Image.
- for badge in signature_badges(&doc.signatures, i) {
- let [x0, y0, x1, y1] = badge.rect;
- let sr = xf.user_rect(info, i, [x0 as f32, y0 as f32, x1 as f32, y1 as f32]);
- let (icon, color) = crate::sign_ui::signature_status_style(badge.status);
- painter.rect_stroke(sr, CornerRadius::same(2), Stroke::new(1.5, color), egui::StrokeKind::Outside);
- // A widget too small for the mark keeps its outline: the verdict still shows.
- if sr.width() < 4.0 || sr.height() < 4.0 {
- continue;
- }
- // Big enough to read at a glance, small enough to leave the signer's own
- // text visible around it.
- let side = (sr.width().min(sr.height()) * 0.8).clamp(24.0, 192.0);
- let mark = Rect::from_center_size(sr.center(), vec2(side, side));
- icons::paint(ui, mark, icon, side * 0.64, color);
- if let Some(p) = pointer.filter(|p| mark.expand(2.0).contains(*p)) {
- badge_hover = Some((p, badge.summary.to_string()));
- }
- }
- // Overlapping widgets paint in order, so the topmost badge owns the hover; an
- // earlier link or annotation hover still wins over any badge.
- if hover_text.is_none() {
- hover_text = badge_hover.take();
- }
- }
- if calc_order {
- view.calc_order.set_hover_page(calc_hover);
- let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
- crate::calc_order_ui::paint_trace(ui, painter, visible, &view.calc_order, generation, selected);
- }
- // ScrollArea ignores the wheel while any widget is dragged, so a text-selection drag
- // scrolls the page itself (#721). After the page loop, so it keeps scrolling once the
- // pointer leaves the selection's page for the gap or the next page; over the whole
- // document area, like #759. Combine direct and smoothed motion in one request so mixed
- // Point and Line events retain their full contribution.
- if selection_wheel {
- let smooth =
- if view.precise_scroll_until == 0.0 { ui.input_mut(|input| std::mem::take(&mut input.smooth_scroll_delta)) } else { Vec2::ZERO };
- let delta = precise_scroll + boost_extra + smooth;
- if delta != Vec2::ZERO && delta.is_finite() {
- ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
- }
- }
- // Held at the top or bottom edge, or dragged past it, a text selection scrolls the view
- // so it can reach text off screen (#1670); the selection follows the pointer on its page.
- if view.selection_drag
- && navigation_enabled
- && unobstructed
- && !single_paging
- && let Some(p) = ptr.pos
- {
- let dy = crate::autoscroll::edge_delta(p.y, avail, ui.input(|i| i.stable_dt));
- if dy != 0.0 {
- view.end_find_following();
- ui.scroll_with_delta_animation(vec2(0.0, dy), egui::style::ScrollAnimation::none());
- // Keep scrolling while the pointer rests there and sends no events.
- ui.ctx().request_repaint();
- }
- }
- // The page navigated to stays current while no page shows more of itself, so the next
- // page step starts from it rather than from a page further down the screen (#188).
- // It also stays current while it is wholly on screen: a short page (a cheque) gone to
- // from the Pages panel shows less of itself than the tall page below it, and must not
- // hand the highlight to that page.
- if current_overlap >= best_overlap - TIE || current_overlap >= current_height - TIE {
- current = view.current;
- }
- if let Some(p) = pressed_page {
- current = p;
- }
- if view.continuous {
- view.current = current;
- }
- resp.context_menu(|ui| {
- // Preparing a form: the selected field's menu.
- if preparing && let Some((name, wi)) = view.prepare.selected.clone() {
- // Several fields: Align, Center, Distribute, Set Fields to Same Size.
- if !view.prepare.also.is_empty() {
- use crate::prepare::Arrange as A;
- let others = view.prepare.also.clone();
- let anchor = (name.clone(), wi);
- let mut pick = |ui: &mut egui::Ui, op: A, label: &str| {
- if ui.add_enabled(can_modify, egui::Button::new(tl!(label))).clicked() {
- view.pending_edit = crate::prepare::arrange(&form, &anchor, &others, op);
- ui.close();
- }
- };
- ui.menu_button(tl!("Align"), |ui| {
- pick(ui, A::AlignLeft, "Left");
- pick(ui, A::AlignRight, "Right");
- pick(ui, A::AlignTop, "Top");
- pick(ui, A::AlignBottom, "Bottom");
- pick(ui, A::AlignCenterV, "Center vertically");
- pick(ui, A::AlignCenterH, "Center horizontally");
- });
- ui.menu_button(tl!("Distribute"), |ui| {
- pick(ui, A::DistributeH, "Horizontally");
- pick(ui, A::DistributeV, "Vertically");
- });
- ui.menu_button(tl!("Set Fields to Same Size"), |ui| {
- pick(ui, A::SameHeight, "Height");
- pick(ui, A::SameWidth, "Width");
- pick(ui, A::SameSize, "Both");
- });
- ui.separator();
- }
- if ui.button(tl!("Properties…")).clicked() {
- field_menu = Some(FieldMenu::Properties);
- ui.close();
- }
- if ui.add_enabled(can_modify, egui::Button::new(tl!("Duplicate…"))).clicked() {
- field_menu = Some(FieldMenu::Duplicate(name.clone()));
- ui.close();
- }
- ui.separator();
- let delete = if view.prepare.also.is_empty() { "Delete" } else { "Delete selected fields" };
- if ui.add_enabled(can_modify, egui::Button::new(tl!(delete))).clicked() {
- view.pending_edit = crate::prepare::delete_selected(view);
- ui.close();
- }
- return;
- }
- if let Some((page, index)) = view.links.context {
- if ui.button(tl!("Properties…")).clicked() {
- view.links.open_existing = Some((page, index));
- ui.close();
- }
- if ui.add_enabled(can_modify, egui::Button::new(tl!("Delete"))).clicked() {
- view.pending_edit = Some(Edit::DeleteLink { page, index });
- view.links.selected = None;
- view.links.context = None;
- ui.close();
- }
- ui.separator();
- if ui.add_enabled(can_modify, egui::Button::new(tl!("Remove all links"))).clicked() {
- view.pending_edit = Some(Edit::RemoveLinks { pages: None });
- view.links.selected = None;
- view.links.context = None;
- ui.close();
- }
- return;
- }
- let is_image_menu = tool == QuickTool::EditText
- && (ui.ctx().data_mut(|d| d.get_persisted::<bool>(crate::edit_text_ui::image_menu_id())).unwrap_or(false)
- || view.image_selection.as_ref().is_some_and(|sel| {
- let pointer_pos = ptr.pos;
- let Some(p) = pointer_pos else { return false };
- let Some((_, xf)) = view.screen_xforms.iter().find(|(page_idx, _)| *page_idx == sel.page) else { return false };
- let images = doc.page_images(sel.page);
- let Some(img) = images.get(sel.index) else { return false };
- let obb = ImageObb::from_image(img, xf, info, sel.page);
- let b = image_screen_rect(img, xf, info, sel.page);
- (b.is_positive() && b.contains(p)) || crate::edit_text_ui::quad_contains(obb.corners(), p)
- }));
- if is_image_menu && let Some(sel) = view.image_selection.clone() {
- let images = doc.page_images(sel.page);
- let at = view.comments.context_at.map(|(_, at)| at);
- crate::edit_text_ui::image_context_menu(ui, view, &images, sel.page, sel.index, can_paste_image, &mut image_action, at);
- return;
- }
- if view.comments.selected.is_some() {
- canvas_action = comments::context_menu(ui, view, info, prefs, allowed, can_modify);
- return;
- }
- // Over a picture (and no comment): Copy Image, as in Acrobat's page menu.
- let mut copy_image = None;
- if let Some((page, at)) = view.comments.context_at {
- // The page's content is parsed once per right-click, not on every frame the menu is open.
- let key = egui::Id::unique("copy-image-under-menu");
- let cached = ui.data(|d| d.get_temp::<(usize, [f64; 2], Option<usize>)>(key)).filter(|c| c.0 == page && c.1 == at);
- let hit = match cached {
- Some(c) => c.2,
- None => {
- let hit = doc.page_image_at(page, at);
- ui.data_mut(|d| d.insert_temp(key, (page, at, hit)));
- hit
- }
- };
- if let Some(image) = hit {
- if ui.button(tl!("Copy Image")).clicked() {
- copy_image = Some(comments::CanvasAction::CopyImage(page, image));
- ui.close();
- }
- if ui.button(tl!("Copy Image in Original Format")).clicked() {
- copy_image = Some(comments::CanvasAction::CopyImageFile(page, image));
- ui.close();
- }
- ui.separator();
- }
- }
- let has_copy_image = copy_image.is_some();
- if copy_image.is_some() {
- canvas_action = copy_image;
- }
- let (page, at) = view.comments.context_at.unwrap_or((view.current, [0.0, 0.0]));
- let can_copy_items = can_copy && (view.image_selection.is_some() || view.selected_text().is_some() || has_copy_image);
- let can_paste = can_paste_image && can_modify;
- let perms = PageMenuPerms {
- can_modify,
- can_assemble: doc.allows_assembly(),
- can_annotate: allowed,
- can_print: doc.allows_printing(),
- can_copy: can_copy_items,
- can_paste,
- has_annotations: !info.annotations.is_empty(),
- };
- default_page_context_menu(ui, page, at, perms, &mut page_action);
- });
- if !resp.context_menu_opened() {
- ui.ctx().data_mut(|d| d.insert_persisted(crate::edit_text_ui::image_menu_id(), false));
- }
- (wanted, visible_now)
- draw_pages(ui, viewport, view, &env, &mut page_out, &mut app.signature_preview)
-- });
- let PageOut {
- canvas_action,
- clicked_link,
- mut copy_selected,
- field_menu,
- field_placed,
- mut field_props,
- hover_text,
- mut image_action,
- open_boxes,
- open_initials,
- open_signature,
- open_table,
- primary_down,
- mut refused,
- stamp_placed,
- } = page_out;
-- view.single_edges = visible_pages.first().copied().filter(|_| !view.continuous).map(|page| {
-- // At the top once the page's top edge shows where going to a page puts it (a gap
-- // below the window's top). Within a point, as for wheel paging, so layout rounding
-- // can't hide an edge.
-- let max_y = (out.content_size.y - out.inner_rect.height()).max(0.0);
-- (page, out.state.clamped_offset().y <= MARGIN - view.page_gap() + 1.0, out.state.clamped_offset().y >= max_y - 1.0)
-- });
--
-- view.viewport_screen = out.inner_rect;
-- view.auto_scroll.paint(ui, avail);
-- if unobstructed && matches!(tool, QuickTool::Select | QuickTool::ColumnSelect) {
-- copy_selected |= selection_actions(ui, avail, view, info, prefs, allowed && !comments_hidden, can_copy);
-- }
--
-- // Collect all main-page demand, including cached surfaces, for byte admission later.
-- // Pan-settle debounce for tiled pages: each tile costs a full page interpret, so holding
-- // fresh tiles back while the scroll offset is still moving keeps a drag on the stretched
-- // backdrop/older tiles instead of queueing a storm of interprets (one per frame) that
-- // the workers cannot cancel mid-operator. Fresh tiles go out once the pan settles;
-- // pages with nothing on screen yet still queue.
- let scroll_offset = out.state.clamped_offset();
- // The document ScrollArea's own scroll surfaces move the offset without the wheel deltas
- // above: its two scroll-bar tracks/handles and, for touch or the Hand tool, its drag
- // background. The output gives us the area id, and egui's pinned ScrollArea registers those
- // widgets under `id.with(0)` (horizontal), `id.with(1)` (vertical) and `id.with("area")`.
- // Ask for their exact responses instead of treating any held drag as scrolling: a text
- // selection, a panel splitter or another widget's drag must not excuse the programmatic jump
- // a search flash makes, while a track click (which moves the offset with no pointer motion)
- // must still count as the reader scrolling.
- let scroll_surface = |id: egui::Id| ui.ctx().read_response(id).is_some_and(|r| r.is_pointer_button_down_on() || r.dragged() || r.clicked());
- let pointer_scrolling = scroll_surface(out.id.with(0usize)) || scroll_surface(out.id.with(1usize)) || scroll_surface(out.id.with("area"));
- if pointer_scrolling && (scroll_offset - view.last_scroll).length() > 2.0 {
- view.end_find_following();
- }
- let now_settle = ui.input(|i| i.time);
- if (scroll_offset - view.last_scroll).length() > 2.0 {
- view.pan_hold_until = now_settle + 0.25;
- ui.ctx().request_repaint_after(std::time::Duration::from_millis(280));
- }
- if glide != Vec2::ZERO {
- view.fling.moved((scroll_offset - view.last_scroll).length() > 0.01);
- }
- view.last_scroll = scroll_offset;
- let pan_held = now_settle < view.pan_hold_until;
- let (mut wanted, visible_now) = out.inner;
- // Single-page and two-page views draw only the current row, so the next one was first asked
- // for on the turn and showed blank paper until it rendered. Prefetch the rows either side,
- // as continuous scrolling does for the pages just off screen.
- if !view.continuous
- && let (Some(&first), Some(&last)) = (visible_pages.first(), visible_pages.last())
- {
- let n = if view.layout == PageLayout::Single { 1 } else { 2 };
- let max_side = ui.ctx().input(|inp| inp.max_texture_side) as f32;
- for i in (last.saturating_add(1)..=last.saturating_add(n)).chain(first.saturating_sub(n)..first) {
- let Some(p) = info.pages.get(i).filter(|_| !view.errors.contains_key(&i)) else { continue };
- // As drawn: the whole page at this scale, or the backdrop of a tiled page.
- let side = p.width.max(p.height).max(1.0);
- wanted.push(if side * scale > TILE_THRESHOLD.min(max_side) {
- let bs = BASE_SIDE.min(max_side) / side;
- (i, bs, scale_tag(bs), None)
- } else {
- (i, scale, tag, None)
- });
- }
- }
- let cur = view.current;
- wanted.sort_by_key(|w| (!visible_now.contains(&w.0), w.0.abs_diff(cur), w.3.is_some()));
- // Tiles of far-away pages are useless: free them. Tiles of an earlier zoom stay for a page
- // that is on screen, whether or not the page is tiled at this zoom (#2196): a zoom out below
- // the tiling threshold used to drop the tiles of the zoom just left, so coming back to it
- // was a cold miss and the page re-blurred through the backdrop while every tile was
- // interpreted again. `OLD_TILES` still bounds how many older tiles are kept, nearest scale
- // first, and admission keeps them inside `TILE_BYTES`.
- let missing: HashSet<usize> = wanted.iter().filter(|w| w.3.is_some()).map(|w| w.0).collect();
- let on_screen: HashSet<usize> = visible_now.iter().copied().collect();
- view.tiles.retain(|(p, t, _, _), _| p.abs_diff(cur) <= 2 && (*t == tag || missing.contains(p) || on_screen.contains(p)));
- let mut older: Vec<(f32, (usize, u64, u32, u32))> =
- view.tiles.keys().filter(|k| k.1 != tag).map(|k| ((tag_scale(k.1) / scale).ln().abs(), *k)).collect();
- // Tiles of earlier zooms are kept nearest scale first, and bounded by count *and* bytes: a
- // large scan renders a tile up to 4096² (about 67 MiB), so a count alone let a few tiles of
- // a few zooms run far past the tile allowance. #2196 keeps a zoom already visited, which
- // makes that reachable in ordinary zooming, so the byte bound has to be here too.
- older.sort_by(|a, b| a.0.total_cmp(&b.0));
- let mut kept = 0usize;
- let mut kept_bytes = 0usize;
- let mut drop: Vec<(usize, u64, u32, u32)> = Vec::new();
- for (_dist, key) in older {
- let bytes = view.tiles.get(&key).map_or(0, texture_bytes);
- if kept >= OLD_TILES || kept_bytes.saturating_add(bytes) > TILE_BYTES {
- drop.push(key);
- } else {
- kept = kept.saturating_add(1);
- kept_bytes = kept_bytes.saturating_add(bytes);
- }
- }
- for key in drop {
- view.tiles.remove(&key);
- }
- let mut queue: Vec<RenderRequest> = wanted
- .iter()
- .map(|&(page, wanted_scale, wanted_tag, tile)| RenderRequest {
- page,
- kind: RequestKind::Pixels,
- tile,
- scale: wanted_scale,
- tag: wanted_tag,
- // The low-res backdrop under tiles is a preview: tiny images are skipped for it.
- // It is the whole-page request whose scale is not the current one; sharp tiles
- // and whole-page finals always draw everything.
- preview: tile.is_none() && (wanted_scale != scale || wanted_tag != tag),
- })
- .collect();
- // While the zoom or pan is settling, hold back sharp re-renders of pages that already
- // show something (a stale whole-page raster or older tiles, drawn stretched): each new
- // scale would cancel the running full-page interpret and restart it from scratch, and
- // each pan frame would queue a storm of full-page tile interprets.
- // Pages with nothing on screen yet still queue, so panning to a fresh page draws it.
- if sharp_held || pan_held {
- let main_tag = tag;
- queue.retain(|req| {
- // Fast pages render straight through: the hold only protects heavy pages
- // whose full-page interpret costs seconds.
- if !view.slow_pages.contains(&req.page) {
- return true;
- }
- if req.tile.is_some() {
- let has_current_tile = view.tiles.keys().any(|(p, t, _, _)| *p == req.page && *t == req.tag);
- if has_current_tile {
- return false;
- }
- let has_any_surface = view.pages.contains_key(&req.page) || view.tiles.keys().any(|(p, _, _, _)| *p == req.page);
- !has_any_surface
- } else if req.tag != main_tag {
- // The low-res backdrop under tiles is zoom-independent: let it through,
- // it is the stretched surface the hold relies on.
- true
- } else {
- // The stale raster stays stretched until the zoom or pan settles; a page with
- // nothing on screen yet still renders.
- !view.pages.contains_key(&req.page)
- }
- });
- }
- // Text layers: visible pages for selection, every page while a search is active. (Select All
- // of the document needs no more: shown pages highlight as their text comes, and Copy reads
- // the rest, as text far away is let go to bound memory.)
- let need_text = |p: &usize| !view.texts.contains_key(p) && !view.text_failed.contains(p);
- let mut text_pages: Vec<usize> = if hand { Vec::new() } else { visible_now.iter().copied().filter(need_text).collect() };
- if view.find.as_ref().is_some_and(|f| !f.case_query.trim().is_empty()) {
- let n = info.pages.len();
- let rest: Vec<usize> = (0..n).map(|k| (cur + k) % n).filter(need_text).filter(|p| !text_pages.contains(p)).collect();
- text_pages.extend(rest);
- }
- queue.extend(text_pages.into_iter().map(|page| RenderRequest {
- page,
- kind: RequestKind::Text,
- tile: None,
- scale: 1.0,
- tag: TEXT_TAG,
- preview: false,
- }));
- view.frame_queue = queue;
- view.frame_visible = visible_now.iter().copied().collect();
- let pan_held = track_scroll(ui, view, &out, glide);
- plan_renders(ui, view, info, out.inner, &RenderPlan { visible_pages: &visible_pages, tag, scale, sharp_held, pan_held, hand });
--
- if let Some((pos, text)) = hover_text {
- // Click-through, as a tooltip is: the pages lose the pointer under anything that isn't.
- let tip = egui::Area::new(egui::Id::unique("canvas-hover")).order(egui::Order::Tooltip).interactable(false);
- tip.fixed_pos(pos + vec2(14.0, 16.0)).show(ui.ctx(), |ui| {
- egui::Frame::popup(ui.style()).show(ui, |ui| {
- ui.set_max_width(320.0);
- ui.label(text);
- });
- });
- }
- find_bar(view, info.pages.len(), avail, ui, &t);
- page_size_hint(view, info, units, avail, unobstructed, ui, &t);
- if tool == QuickTool::EnhanceCamera {
- crate::camera_ui::bar(ui.ctx(), view, avail, &t, can_modify);
- }
- if let Some(e) = comments::composer(ui.ctx(), view, info, prefs) {
- view.pending_edit = Some(e);
- }
- if let Some(e) = crate::edit_text_ui::overlay(ui.ctx(), view, info) {
- view.pending_edit = Some(e);
- }
- let text_box = crate::forms_ui::text_box(doc, view, &form);
- if let Some(e) = crate::forms_ui::overlay(ui.ctx(), view, info, &form, text_box.as_ref(), today) {
- view.pending_edit = Some(e);
- }
- if let Some(e) = crate::fill_sign::type_box(ui.ctx(), view, info, &author) {
- view.pending_edit = Some(e);
- }
- let (typed, text_done) = crate::content_ui::editor(ui.ctx(), view, info, &added);
- if typed.is_some() {
- view.pending_edit = typed;
- }
- let text_done = frame_overlays(ui, view, &env, primary_down, hover_text);
-- if let Some(why) = view.objects.notice.take() {
-- refused = Some(why);
-- }
-- let form_notice = view.forms.notice.take();
-- // One crop, then back to selecting (as Acrobat does).
-- let cropped = view.pending_edit.as_ref().is_some_and(|e| matches!(e, pdfcraft_engine::Edit::SetPageBox { .. }));
-- let mut tool = app.quick_tool;
-- comments::keys(ui.ctx(), view, &mut tool, allowed);
-- if preparing {
-- crate::prepare::keys(ui.ctx(), view);
-- }
-- if editing_content {
-- crate::content_ui::keys(ui.ctx(), view);
-- }
-- if tool == QuickTool::Link {
-- crate::link_ui::keys(ui.ctx(), view);
-- } else {
-- view.links.selected = None;
- }
- if tool != QuickTool::EditText {
- view.image_selection = None;
-- }
-- if stamp_placed {
-- tool = QuickTool::Select;
-- }
-- // Text: each click on the page starts a box (#74); Done in the editor goes back to selecting.
-- if text_done && tool == QuickTool::AddText {
-- tool = QuickTool::Select;
-- }
-- // One field, then back to selecting (Acrobat's default without "Keep tools pinned").
-- if field_placed {
-- tool = QuickTool::Select;
-- }
-- if matches!(field_menu, Some(FieldMenu::Properties)) {
-- field_props = true;
-- }
-- let field_props = field_props.then(|| view.prepare.selected.clone()).flatten();
-- match canvas_action {
-- Some(comments::CanvasAction::Edit(e)) => view.pending_edit = Some(*e),
-- // `choose_right_panel` would borrow all of `app` while `view` is held; same effect.
-- Some(comments::CanvasAction::OpenComments) => {
-- app.right = Some(RightPanel::Comments);
-- app.comments_panel_closed = false;
-- }
-- Some(comments::CanvasAction::Properties(p, i)) => open_props = Some((p, i)),
-- Some(comments::CanvasAction::CopyImage(p, i)) => image_action = Some(crate::edit_text_ui::ImageAction::Copy(p, i)),
-- Some(comments::CanvasAction::CopyImageFile(p, i)) => image_action = Some(crate::edit_text_ui::ImageAction::CopyFile(p, i)),
- None => {}
- }
- // Full screen: the click turns the page unless it did something else first. It waits out the
- // double-click time: turning on the first click's release sent a double-click's second click,
- // meant to select a word, to the next page (review on #2070). A selection cancels it, and so
- // do a link followed meanwhile and leaving full screen; a double-click elsewhere still turns
- // once.
- let now = ui.input(|i| i.time);
- if present_click && clicked_link.is_none() && view.selection.is_none() && view.clicks < 2 {
- let delay = ui.ctx().options(|o| o.input_options.max_double_click_delay);
- view.present_turn = Some((now + delay, !present_back));
- }
- if let Some((due, forward)) = view.present_turn {
- if clicked_link.is_some() || view.selection.is_some() || view.presentation.is_none() {
- view.present_turn = None;
- } else if now >= due {
- view.present_turn = None;
- view.step_page(forward);
- } else {
- ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64((due - now).max(0.0)));
- }
- }
- match clicked_link {
- Some(LinkTarget::Page(p, dest)) => view.go_to_dest(p, dest, info),
- Some(LinkTarget::Uri(u)) => app.request_document_url(&u, crate::LinkOrigin::Link),
- Some(LinkTarget::SetLayers { changes, preserve_rb }) => set_layers(app, index, &changes, preserve_rb),
- Some(LinkTarget::RemoteGoTo { file, destination, .. }) => remote_open = Some((view.id, file, destination)),
- Some(LinkTarget::Other(s)) => app.notify_fmt("This PDF uses the unsupported {s} action.", &[("s", &s)]),
-- None => {}
-- }
- schedule_presentation_turn(ui, view, present, clicked_link.is_some());
- let remote_open = follow_link(app, index, clicked_link);
-- if tool == QuickTool::Crop && cropped {
- tool = QuickTool::Select;
- }
- if let Some(done) = app.views[index].marquee_done.take() {
- app.finish_marquee(index, done);
- }
- // A signature rectangle was drawn, or an empty signature field clicked.
- let drawn = app.views[index].sign.drawn.take();
- if let (Some((page, rect)), QuickTool::SignArea { certify }) = (drawn, tool) {
-- tool = QuickTool::Select;
- app.quick_tool = tool;
- app.start_signing(page, Some(rect), None, certify.then_some(2));
- }
- if let Some(field) = app.views[index].sign.field.take() {
- let signed = app.session.get(app.views[index].id).is_some_and(|d| d.signatures.iter().any(|s| s.field == field && s.signed));
- if signed {
- app.right = Some(RightPanel::Signatures);
- if !app.sig_expanded.contains(&field) {
- app.sig_expanded.push(field);
- }
- } else {
- let page = app.views[index].current;
- app.start_signing(page, None, Some(field), None);
- }
-- }
- let tool = finish_sign_requests(app, index, tool);
-- app.quick_tool = tool;
- let requests = FrameRequests {
- open_props,
- field_props,
- field_menu,
- image_action,
- refused,
- open_signature,
- open_initials,
- open_boxes,
- open_table,
- form_notice,
- copy_selected,
- remote_open,
- form,
- generation,
- avail,
- };
- apply_frame_requests(app, index, ui, requests);
-}
-
-/// What the frame's input asked the app to do, once the document is no longer borrowed.
-struct FrameRequests {
- open_props: Option<(usize, usize)>,
- field_props: Option<(String, usize)>,
- field_menu: Option<FieldMenu>,
- image_action: Option<crate::edit_text_ui::ImageAction>,
- refused: Option<String>,
- open_signature: bool,
- open_initials: bool,
- open_boxes: bool,
- open_table: Option<(usize, [f64; 4])>,
- form_notice: Option<crate::forms_ui::FormNotice>,
- copy_selected: bool,
- remote_open: Option<(pdfcraft_engine::DocId, String, pdfcraft_render::RemoteDestination)>,
- form: Arc<Vec<pdfcraft_engine::FormField>>,
- generation: u64,
- avail: Rect,
-}
-
-/// Opens the dialogs and runs the app commands the frame asked for.
-fn apply_frame_requests(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui, requests: FrameRequests) {
- let FrameRequests {
- open_props,
- field_props,
- field_menu,
- image_action,
- refused,
- open_signature,
- open_initials,
- open_boxes,
- open_table,
- form_notice,
- copy_selected,
- remote_open,
- form,
- generation,
- avail,
- } = requests;
-- if let Some((page, at)) = app.views[index].comments.attach_at.take() {
-- app.attach_file_comment(page, at);
-- }
-- if let Some((p, i)) = open_props {
-- app.open_comment_props(p, i);
-- }
-- if let Some((p, i)) = app.views[index].comments.default_request.take() {
-- app.make_comment_default(p, i);
-- }
-- if let Some((p, i, open)) = app.views[index].comments.attachment_request.take() {
-- let doc = app.views[index].id;
-- app.comment_attachment_action(doc, p, i, open);
-- }
-- if let Some((name, w)) = field_props {
-- app.open_field_props(&name, w);
-- }
-- if let Some(FieldMenu::Duplicate(name)) = field_menu {
-- let pages = app.session.get(app.views[index].id).map_or(1, |d| d.info.pages.len());
-- app.duplicate_draft = Some(crate::DuplicateDraft { name, all: true, from: 1, to: pages });
-- app.dialog = Some(crate::Dialog::DuplicateField);
-- }
-- if let Some((page, rect)) = app.views[index].links.open_new.take() {
-- app.open_link_props(page, Some(rect), None);
-- }
-- if let Some((page, i)) = app.views[index].links.open_existing.take() {
-- app.open_link_props(page, None, Some(i));
-- }
-- if let Some((page, quads)) = app.views[index].pending_redaction.take() {
-- let author = app.comment_prefs.author.clone();
-- app.views[index].pending_edit = Some(if app.quick_tool == QuickTool::Comment(comments::CommentTool::Highlight) {
-- // An area highlight: a highlight over the box.
-- let style = app.comment_prefs.style(comments::CommentTool::Highlight);
-- pdfcraft_engine::Edit::AddAnnotation(pdfcraft_engine::NewAnnotation {
-- page,
-- shape: pdfcraft_engine::Shape::TextMarkup { kind: pdfcraft_engine::Markup::Highlight, quads },
-- style,
-- contents: String::new(),
-- author,
-- })
-- } else {
-- app.redact_prefs.mark(page, quads, &author)
-- });
-- }
-- match image_action {
-- Some(crate::edit_text_ui::ImageAction::Replace(page, index)) => app.replace_page_image_dialog(page, index),
-- Some(crate::edit_text_ui::ImageAction::Save(page, index)) => app.save_page_image(page, index),
-- Some(crate::edit_text_ui::ImageAction::Copy(page, index)) => {
-- app.copy_page_image(page, index);
-- }
-- Some(crate::edit_text_ui::ImageAction::CopyFile(page, index)) => app.copy_page_image_file(page, index),
-- Some(crate::edit_text_ui::ImageAction::Cut(page, image)) => {
-- app.views[index].image_selection = None;
-- app.cut_page_image(page, image);
- }
- Some(crate::edit_text_ui::ImageAction::Paste(page, at)) => {
- if let Some(pt) = at {
- app.paste_image_at(page, pt);
- } else {
- app.paste_image(page, None);
- }
- }
- Some(crate::edit_text_ui::ImageAction::DrawFreehand) => {
- app.quick_tool = crate::QuickTool::Comment(crate::comments::CommentTool::Ink);
-- }
-- Some(crate::edit_text_ui::ImageAction::Redact(page, index)) => app.redact_page_image(page, index),
-- Some(crate::edit_text_ui::ImageAction::Link(page, index)) => app.link_page_image(page, index),
-- Some(crate::edit_text_ui::ImageAction::Recognize(page)) => app.recognize_text_on_page(page),
- Some(crate::edit_text_ui::ImageAction::AddComment(page, i)) => {
- if let Some(d) = app.session.get(app.views[index].id) {
- let images = d.page_images(page);
- if let Some(img) = images.get(i) {
- let r = img.visible_rect.unwrap_or(img.rect);
- let center = [(r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0];
- app.views[index].comments.context_at = Some((page, center));
- app.views[index].comments.composer = Some(crate::comments::Composer {
- page,
- at: center,
- kind: crate::comments::ComposerKind::Note,
- text: String::new(),
- focus: true,
- });
- }
- }
- }
- Some(crate::edit_text_ui::ImageAction::Crop) => {
- let doc_id = app.views[index].id;
- if let Some(s) = app.views[index].image_selection.as_mut()
- && let Some(d) = app.session.get(doc_id)
- {
- let images = d.page_images(s.page);
- if let Some(im) = images.get(s.index) {
- s.crop = Some(crate::edit_text_ui::CropMode::new(im.visible_rect.unwrap_or(im.rect)));
- s.crop_mode = true;
- s.crop_rect = None;
- }
- }
- }
- None => {}
- }
- match page_action {
- Some(PageMenuAction::Copy) => {
- if let Some(sel) = app.views[index].image_selection.clone() {
- app.copy_page_image(sel.page, sel.index);
- } else if app.views[index].selected_text().is_some() {
- app.execute("edit.copy");
- } else if let Some((page, at)) = app.views[index].comments.context_at
- && let Some(doc) = app.session.get(app.views[index].id)
- && let Some(image) = doc.page_image_at(page, at)
- {
- app.copy_page_image(page, image);
- }
- }
- Some(PageMenuAction::Paste(page, at)) => {
- app.paste_image_at(page, at);
- }
- Some(PageMenuAction::RotateCCW(page)) => {
- let _ = page;
- app.views[index].rotate_view(false);
- }
- Some(PageMenuAction::RotateCW(page)) => {
- let _ = page;
- app.views[index].rotate_view(true);
- }
- Some(PageMenuAction::Print) => {
- app.open_print();
- }
- Some(PageMenuAction::SelectTool) => {
- app.quick_tool = QuickTool::Select;
- }
- Some(PageMenuAction::HandTool) => {
- app.quick_tool = QuickTool::Hand;
- }
- Some(PageMenuAction::MarqueeZoom) => {
- app.quick_tool = QuickTool::MarqueeZoom;
- }
- Some(PageMenuAction::AddComment(page, at)) => {
- app.views[index].comments.composer =
- Some(crate::comments::Composer { page, at, kind: crate::comments::ComposerKind::Note, text: String::new(), focus: true });
- }
- Some(PageMenuAction::FlattenComments) => {
- app.views[index].comments.selected = None;
- app.views[index].pending_edit = Some(pdfcraft_engine::Edit::Flatten { comments: true, fields: false });
- }
- Some(PageMenuAction::AddBookmark(_page)) => {
- app.bookmark_action(crate::panels::BmAction::New);
- }
- Some(PageMenuAction::TakeSnapshot) => {
- app.quick_tool = QuickTool::Snapshot;
- }
- Some(PageMenuAction::EditPdf) => {
- app.quick_tool = QuickTool::EditText;
- }
- Some(PageMenuAction::AddText) => {
- app.quick_tool = QuickTool::AddText;
- }
- Some(PageMenuAction::AddImage) => {
- app.add_image_dialog();
- }
- Some(PageMenuAction::DeletePage(page)) => {
- app.views[index].pending_edit = Some(pdfcraft_engine::Edit::DeletePages { pages: vec![page] });
- }
- Some(PageMenuAction::ExportPdf) => {
- app.select_tool("export");
- }
- Some(PageMenuAction::Find) => {
- app.views[index].open_find();
- }
- Some(PageMenuAction::DocProperties) => {
- app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Description));
- }
-- None => {}
-- }
-- if let Some(why) = refused {
-- app.notify_error(why);
-- }
-- if open_signature || open_initials {
-- app.signature_draft = crate::fill_sign::SigDraft::new(open_initials, &app.comment_prefs.author);
-- app.dialog = Some(crate::Dialog::Signature);
-- }
-- if open_boxes {
-- app.boxes_draft.range = crate::pageboxes::Range::Current;
-- app.boxes_draft.seeded = None;
-- app.dialog = Some(crate::Dialog::PageBoxes);
-- }
-- if let Some((page, rect)) = open_table {
-- app.field_table_draft = Some(crate::field_table_ui::FieldTableDraft::new(app.views[index].id, page, rect, &form, generation));
-- app.dialog = Some(crate::Dialog::FieldTable);
-- // The window lasts while the form is prepared. Drawn with the tool picked but its panel
-- // closed, the tool goes back to Select above, so the panel opens to keep it prepared.
-- app.left = crate::LeftPanel::Tool("form");
-- app.reveal_tools();
-- }
-- if let Some(n) = form_notice {
-- match n {
-- crate::forms_ui::FormNotice::Security => app.notify_tr("The document's security settings don't allow filling in form fields"),
-- crate::forms_ui::FormNotice::ReadOnly(name) => app.notify_fmt("{name} is read-only", &[("name", &name)]),
-- crate::forms_ui::FormNotice::NoAction(name) => app.notify_fmt("{name} has no action", &[("name", &name)]),
-- }
-- }
-- if let Some((name, action)) = app.views[index].forms.button.take() {
-- run_button(app, index, &name, action);
-- }
-- if copy_selected {
-- app.execute("edit.copy");
-- }
-- // Read mode hides the tools (#2646), and full screen is the page and nothing else (#1459);
-- // the quick tools come back with the window.
-- if app.mode != crate::Mode::Read && !app.full_screen {
-- quick_bar(app, avail, ui);
-- }
-- if let Some((source, file, destination)) = remote_open {
-- app.open_remote_link(source, file, destination);
-- }
--}
--
--/// The prepare-mode field menu's choices.
--enum FieldMenu {
-- Properties,
-- Duplicate(String),
--}
--
- pub use crate::image_engine::{HandleId, MIN_RECT_SIZE, apply_crop, apply_resize, apply_resize_proportional, reset_crop};
--#[cfg(test)]
--mod tests;
++//! The document area: page layout, zoom, render scheduling, overlays (links, annotation hovers,
++//! form-field highlights), and the organize-pages grid.
++//!
++//! Page images come from the engine's render pool as whole-page rasters at the current zoom;
++//! stale rasters are shown stretched until the sharp one arrives (tiling comes in M3.3).
++
++use std::collections::{BTreeSet, HashMap, HashSet};
++use std::ops::Range;
++use std::sync::Arc;
++
++use egui::{Align2, Color32, CornerRadius, Pos2, Rect, Sense, Stroke, TextureHandle, TextureOptions, Vec2, pos2, vec2};
++use pdfcraft_engine::{DocId, Edit};
++use pdfcraft_render::{DestView, DocInfo, LayerOp, LinkTarget, PRINT_TAG, PageText, RenderPool, RenderRequest, RequestKind, Tile, device_pixels};
++
++use crate::page_cursor::{Layer, Subject};
++use crate::page_pointer::PagePointer;
++use crate::theme::{self, Tokens};
++use crate::{PdfCraftApp, QuickTool, RightPanel, comments, icons, widgets};
++
++mod area;
++pub(super) mod autoscroll;
++mod bars;
++mod cache;
++pub(super) mod crop;
++mod find;
++pub(super) mod fling;
++pub(super) mod image_pick;
++mod navigation;
++mod organize;
++pub(super) mod page_cursor;
++pub(super) mod page_pointer;
++pub(super) mod page_router;
++pub mod scroll_speed;
++mod selection;
++pub(super) mod signature_drag;
++pub(super) mod wheel_boost;
++pub(super) mod wheel_pager;
++mod xform;
++mod zoom;
++pub(super) mod zoom_snap;
++
++pub(crate) use area::signature_badges;
++use area::{
++ SUPPRESS_SECS, SUPPRESS_TAIL_SECS, drawing_edge_pointer, glide_interrupted, page_scroll, presentation_click, run_button, set_layers, zoom_anchor,
++};
++#[cfg(test)]
++use bars::in_size_hint_zone;
++use bars::{Notice, notices, page_size_hint, quick_bar};
++pub use cache::RasterMemory;
++pub(crate) use cache::{BASE_SIDE, TILE_THRESHOLD, texture_image, thumbnail_rows};
++#[cfg(test)]
++use cache::{
++ GRAYSCALE_TAG, GRID_TAG, MAX_CACHED_TEXT_PAGES, MAX_THUMB_DEMAND, PAGE_BYTES, THUMB_BYTES, THUMB_H, THUMB_TAG, THUMB_W, TILE, grayscale,
++ rgba_bytes,
++};
++use cache::{OLD_TILES, PageTex, STALE_TAG, TEXT_TAG, TILE_BYTES, scale_tag, tag_scale, texture_bytes, tile_side};
++pub use find::Find;
++use find::find_bar;
++pub use navigation::shortcuts;
++pub(crate) use organize::drop_gap;
++#[cfg(test)]
++pub(crate) use organize::grid_page_rect;
++use organize::organize_grid;
++#[cfg(test)]
++use organize::sharp_enough;
++use selection::{Selection, Span, click_unit, selection_actions};
++pub use xform::PageXform;
++use xform::snap_to_pixels;
++use zoom::anchored_scroll;
++pub use zoom::{GRID_ZOOM_RANGE, THUMBNAIL_ZOOM_RANGE};
++
++/// Logical pixels per PDF point at 100% (96 dpi, like browsers).
++pub const PT: f32 = 96.0 / 72.0;
++const GAP: f32 = 14.0;
++const MARGIN: f32 = 28.0;
++/// Horizontal gutter that keeps pages clear of the floating quick-action bar.
++const SIDE: f32 = 70.0;
++
++#[derive(Clone, Copy, Debug, PartialEq, Eq)]
++pub enum Fit {
++ Width,
++ Page,
++ Height,
++ None,
++}
++
++use crate::edit_text_ui::ImageHandle;
++
++/// Extension trait on `egui::Painter` to draw polygons.
++pub trait PainterExt {
++ fn polygon(&self, points: Vec<Pos2>, fill: Color32, stroke: Stroke) -> egui::layers::ShapeIdx;
++}
++
++impl PainterExt for egui::Painter {
++ fn polygon(&self, points: Vec<Pos2>, fill: Color32, stroke: Stroke) -> egui::layers::ShapeIdx {
++ self.add(egui::Shape::convex_polygon(points, fill, stroke))
++ }
++}
++
++/// User-space selectable bounding box for an image object.
++/// If the image object has an active clipping path applied to it (the crop box),
++/// this is the mathematical intersection of the full CTM-derived rect and the clipping path's rect.
++pub fn image_selectable_bounds(im: &pdfcraft_engine::PageImage) -> [f64; 4] {
++ if let Some(clip) = im.clip_rect {
++ let r = [im.rect[0].max(clip[0]), im.rect[1].max(clip[1]), im.rect[2].min(clip[2]), im.rect[3].min(clip[3])];
++ if r.iter().all(|v| v.is_finite()) && r[2] > r[0] && r[3] > r[1] {
++ return r;
++ }
++ }
++ im.visible_rect.unwrap_or(im.rect)
++}
++
++/// Screen-space selectable bounding box for an image object.
++/// If the image object has an active clipping path applied to it (the crop box),
++/// the final selectable `egui::Rect` is the mathematical intersection of the image's
++/// full CTM-derived rect and the clipping path's rect.
++pub fn image_screen_rect(im: &pdfcraft_engine::PageImage, xf: &PageXform, info: &DocInfo, page: usize) -> Rect {
++ let full = xf.user_rect_f64(info, page, im.rect);
++ if let Some(clip) = im.clip_rect {
++ let clip_r = xf.user_rect_f64(info, page, clip);
++ let inter = full.intersect(clip_r);
++ if inter.is_positive() {
++ return inter;
++ }
++ }
++ if let Some(vr) = im.visible_rect {
++ let r = xf.user_rect_f64(info, page, vr);
++ if r.is_positive() {
++ return r;
++ }
++ }
++ full
++}
++
++/// An Oriented Bounding Box (OBB) representing a rotated rectangle in screen space,
++/// with 8 resize handles and rotated local axes.
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub struct ImageObb {
++ pub s00: Pos2,
++ pub s10: Pos2,
++ pub s11: Pos2,
++ pub s01: Pos2,
++ pub s_bc: Pos2,
++ pub s_rc: Pos2,
++ pub s_tc: Pos2,
++ pub s_lc: Pos2,
++ pub s_rot: Pos2,
++ pub screen_center: Pos2,
++ pub width: f32,
++ pub height: f32,
++ pub theta: f32,
++ pub u_axis: (f64, f64),
++ pub v_axis: (f64, f64),
++ pub is_rotated: bool,
++}
++
++impl ImageObb {
++ /// Calculates the 4 rotated corners manually using center, width, height, and rotation angle (θ).
++ /// Local axes: `u_axis = (cos θ, sin θ)` and `v_axis = (-sin θ, cos θ)`.
++ /// Places the 8 resize handles exactly at these rotated vertices and midpoints.
++ pub fn from_center_size_angle(center: Pos2, width: f32, height: f32, theta: f32) -> Self {
++ let cos_t = theta.cos();
++ let sin_t = theta.sin();
++ let u_axis = (cos_t as f64, sin_t as f64);
++ let v_axis = (-sin_t as f64, cos_t as f64);
++ let u_vec = vec2(cos_t, sin_t);
++ let v_vec = vec2(-sin_t, cos_t);
++ let hw = width * 0.5;
++ let hh = height * 0.5;
++
++ let s00 = center - u_vec * hw - v_vec * hh;
++ let s10 = center + u_vec * hw - v_vec * hh;
++ let s11 = center + u_vec * hw + v_vec * hh;
++ let s01 = center - u_vec * hw + v_vec * hh;
++
++ let s_bc = center - v_vec * hh;
++ let s_rc = center + u_vec * hw;
++ let s_tc = center + v_vec * hh;
++ let s_lc = center - u_vec * hw;
++
++ let s_rot = s_tc + v_vec * 24.0;
++ let is_rotated = theta.abs() > 1e-4;
++
++ Self { s00, s10, s11, s01, s_bc, s_rc, s_tc, s_lc, s_rot, screen_center: center, width, height, theta, u_axis, v_axis, is_rotated }
++ }
++
++ pub fn new(matrix: [f64; 6], xf: &PageXform, info: &DocInfo, page: usize) -> Self {
++ Self::from_matrix(matrix, xf, info, page)
++ }
++
++ pub fn from_matrix(matrix: [f64; 6], xf: &PageXform, info: &DocInfo, page: usize) -> Self {
++ let m = crate::edit_text_ui::Matrix(matrix);
++ let p00 = m.apply(0.0, 0.0);
++ let p10 = m.apply(1.0, 0.0);
++ let p11 = m.apply(1.0, 1.0);
++ let p01 = m.apply(0.0, 1.0);
++
++ let s00 = crate::edit_text_ui::user_to_screen(xf, info, page, p00.0, p00.1);
++ let s10 = crate::edit_text_ui::user_to_screen(xf, info, page, p10.0, p10.1);
++ let s11 = crate::edit_text_ui::user_to_screen(xf, info, page, p11.0, p11.1);
++ let s01 = crate::edit_text_ui::user_to_screen(xf, info, page, p01.0, p01.1);
++
++ let s_bc = Pos2::new((s00.x + s10.x) * 0.5, (s00.y + s10.y) * 0.5);
++ let s_rc = Pos2::new((s10.x + s11.x) * 0.5, (s10.y + s11.y) * 0.5);
++ let s_tc = Pos2::new((s01.x + s11.x) * 0.5, (s01.y + s11.y) * 0.5);
++ let s_lc = Pos2::new((s00.x + s01.x) * 0.5, (s00.y + s01.y) * 0.5);
++
++ let screen_center = Pos2::new((s00.x + s10.x + s11.x + s01.x) * 0.25, (s00.y + s10.y + s11.y + s01.y) * 0.25);
++ let out_dir = s_tc - screen_center;
++ let out_len = out_dir.length().max(1e-4);
++ let out_norm = out_dir / out_len;
++ let s_rot = s_tc + out_norm * 24.0;
++
++ let [a, b, c, d, ..] = matrix;
++ let w_u = (a * a + b * b).sqrt().max(1e-6);
++ let h_v = (c * c + d * d).sqrt().max(1e-6);
++ let u_axis = (a / w_u, b / w_u);
++ let v_axis = (c / h_v, d / h_v);
++ let theta = (b).atan2(a) as f32;
++ let is_rotated = b.abs() > 1e-4 || c.abs() > 1e-4;
++
++ Self {
++ s00,
++ s10,
++ s11,
++ s01,
++ s_bc,
++ s_rc,
++ s_tc,
++ s_lc,
++ s_rot,
++ screen_center,
++ width: (s10 - s00).length(),
++ height: (s01 - s00).length(),
++ theta,
++ u_axis,
++ v_axis,
++ is_rotated,
++ }
++ }
++
++ pub fn from_image(im: &pdfcraft_engine::PageImage, xf: &PageXform, info: &DocInfo, page: usize) -> Self {
++ let [_, b, c, ..] = im.matrix;
++ let is_rot = b.abs() > 1e-4 || c.abs() > 1e-4;
++ let bounds = image_selectable_bounds(im);
++ let matrix = if !is_rot && bounds[2] > bounds[0] && bounds[3] > bounds[1] {
++ [bounds[2] - bounds[0], 0.0, 0.0, bounds[3] - bounds[1], bounds[0], bounds[1]]
++ } else {
++ im.matrix
++ };
++ Self::from_matrix(matrix, xf, info, page)
++ }
++
++ pub fn handles(&self) -> [(ImageHandle, Pos2); 8] {
++ [
++ (ImageHandle::TopLeft, self.s01),
++ (ImageHandle::TopCenter, self.s_tc),
++ (ImageHandle::TopRight, self.s11),
++ (ImageHandle::RightCenter, self.s_rc),
++ (ImageHandle::BottomRight, self.s10),
++ (ImageHandle::BottomCenter, self.s_bc),
++ (ImageHandle::BottomLeft, self.s00),
++ (ImageHandle::LeftCenter, self.s_lc),
++ ]
++ }
++
++ pub fn handle_pos(&self, handle: ImageHandle) -> Pos2 {
++ match handle {
++ ImageHandle::TopLeft => self.s01,
++ ImageHandle::TopCenter => self.s_tc,
++ ImageHandle::TopRight => self.s11,
++ ImageHandle::RightCenter => self.s_rc,
++ ImageHandle::BottomRight => self.s10,
++ ImageHandle::BottomCenter => self.s_bc,
++ ImageHandle::BottomLeft => self.s00,
++ ImageHandle::LeftCenter => self.s_lc,
++ }
++ }
++
++ pub fn handle_id(&self, base_id: egui::Id, handle: ImageHandle) -> egui::Id {
++ base_id.with(match handle {
++ ImageHandle::TopLeft => "handle_tl",
++ ImageHandle::TopCenter => "handle_tc",
++ ImageHandle::TopRight => "handle_tr",
++ ImageHandle::RightCenter => "handle_rc",
++ ImageHandle::BottomRight => "handle_br",
++ ImageHandle::BottomCenter => "handle_bc",
++ ImageHandle::BottomLeft => "handle_bl",
++ ImageHandle::LeftCenter => "handle_lc",
++ })
++ }
++
++ pub fn handles_with_ids(&self, base_id: egui::Id) -> [(ImageHandle, Pos2, egui::Id); 8] {
++ [
++ (ImageHandle::TopLeft, self.s01, self.handle_id(base_id, ImageHandle::TopLeft)),
++ (ImageHandle::TopCenter, self.s_tc, self.handle_id(base_id, ImageHandle::TopCenter)),
++ (ImageHandle::TopRight, self.s11, self.handle_id(base_id, ImageHandle::TopRight)),
++ (ImageHandle::RightCenter, self.s_rc, self.handle_id(base_id, ImageHandle::RightCenter)),
++ (ImageHandle::BottomRight, self.s10, self.handle_id(base_id, ImageHandle::BottomRight)),
++ (ImageHandle::BottomCenter, self.s_bc, self.handle_id(base_id, ImageHandle::BottomCenter)),
++ (ImageHandle::BottomLeft, self.s00, self.handle_id(base_id, ImageHandle::BottomLeft)),
++ (ImageHandle::LeftCenter, self.s_lc, self.handle_id(base_id, ImageHandle::LeftCenter)),
++ ]
++ }
++
++ pub fn corners(&self) -> [Pos2; 4] {
++ [self.s00, self.s10, self.s11, self.s01]
++ }
++
++ /// Resizes the OBB proportionally along its local axes using projected mouse delta,
++ /// locking the diagonally opposite corner as the fixed anchor point.
++ pub fn scale_proportional(&self, handle: ImageHandle, delta: Vec2, min_size: f32) -> Self {
++ let u_vec = vec2(self.u_axis.0 as f32, self.u_axis.1 as f32);
++ let v_vec = vec2(self.v_axis.0 as f32, self.v_axis.1 as f32);
++ let delta_u = delta.dot(u_vec);
++ let delta_v = delta.dot(v_vec);
++ let min_s = min_size.max(5.0);
++
++ match handle {
++ ImageHandle::TopRight => {
++ let anchor = self.s00;
++ let s_u = (self.width + delta_u) / self.width.max(1e-4);
++ let s_v = (self.height + delta_v) / self.height.max(1e-4);
++ let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
++ let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
++ let nw = self.width * s;
++ let nh = self.height * s;
++ let new_center = anchor + u_vec * (nw * 0.5) + v_vec * (nh * 0.5);
++ Self::from_center_size_angle(new_center, nw, nh, self.theta)
++ }
++ ImageHandle::TopLeft => {
++ let anchor = self.s10;
++ let s_u = (self.width - delta_u) / self.width.max(1e-4);
++ let s_v = (self.height + delta_v) / self.height.max(1e-4);
++ let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
++ let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
++ let nw = self.width * s;
++ let nh = self.height * s;
++ let new_center = anchor - u_vec * (nw * 0.5) + v_vec * (nh * 0.5);
++ Self::from_center_size_angle(new_center, nw, nh, self.theta)
++ }
++ ImageHandle::BottomLeft => {
++ let anchor = self.s11;
++ let s_u = (self.width - delta_u) / self.width.max(1e-4);
++ let s_v = (self.height - delta_v) / self.height.max(1e-4);
++ let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
++ let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
++ let nw = self.width * s;
++ let nh = self.height * s;
++ let new_center = anchor - u_vec * (nw * 0.5) - v_vec * (nh * 0.5);
++ Self::from_center_size_angle(new_center, nw, nh, self.theta)
++ }
++ ImageHandle::BottomRight => {
++ let anchor = self.s01;
++ let s_u = (self.width + delta_u) / self.width.max(1e-4);
++ let s_v = (self.height - delta_v) / self.height.max(1e-4);
++ let s = if (s_u - 1.0).abs() >= (s_v - 1.0).abs() { s_u } else { s_v };
++ let s = s.max(min_s / self.width.max(1e-4)).max(min_s / self.height.max(1e-4));
++ let nw = self.width * s;
++ let nh = self.height * s;
++ let new_center = anchor + u_vec * (nw * 0.5) - v_vec * (nh * 0.5);
++ Self::from_center_size_angle(new_center, nw, nh, self.theta)
++ }
++ ImageHandle::RightCenter => {
++ let anchor = self.s_lc;
++ let nw = (self.width + delta_u).max(min_s);
++ let new_center = anchor + u_vec * (nw * 0.5);
++ Self::from_center_size_angle(new_center, nw, self.height, self.theta)
++ }
++ ImageHandle::LeftCenter => {
++ let anchor = self.s_rc;
++ let nw = (self.width - delta_u).max(min_s);
++ let new_center = anchor - u_vec * (nw * 0.5);
++ Self::from_center_size_angle(new_center, nw, self.height, self.theta)
++ }
++ ImageHandle::TopCenter => {
++ let anchor = self.s_bc;
++ let nh = (self.height + delta_v).max(min_s);
++ let new_center = anchor + v_vec * (nh * 0.5);
++ Self::from_center_size_angle(new_center, self.width, nh, self.theta)
++ }
++ ImageHandle::BottomCenter => {
++ let anchor = self.s_tc;
++ let nh = (self.height - delta_v).max(min_s);
++ let new_center = anchor - v_vec * (nh * 0.5);
++ Self::from_center_size_angle(new_center, self.width, nh, self.theta)
++ }
++ }
++ }
++}
++
++/// Projects mouse delta onto local unit axes `u_axis` and `v_axis` using the dot product.
++pub fn project_delta(delta: Vec2, u_axis: (f64, f64), v_axis: (f64, f64)) -> (f64, f64) {
++ let dx = delta.x as f64;
++ let dy = delta.y as f64;
++ (dx * u_axis.0 + dy * u_axis.1, dx * v_axis.0 + dy * v_axis.1)
++}
++
++/// The page display as it was before full screen, put back on leaving it
++/// ([`DocView::enter_presentation`]).
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub struct SavedDisplay {
++ pub layout: PageLayout,
++ pub continuous: bool,
++ pub cover: bool,
++ pub fit: Fit,
++ pub zoom: f32,
++}
++
++/// The base page display: one page across, or a two-page spread. Whether it scrolls continuously
++/// is a separate flag (`continuous`), as in Acrobat, so every combination is reachable (#752).
++#[derive(Clone, Copy, Debug, PartialEq, Eq)]
++pub enum PageLayout {
++ Single,
++ TwoUp,
++}
++
++impl PageLayout {
++ /// The base page displays, in the order the View menu, the rail's Page display button and
++ /// Preferences list them.
++ pub const ORDER: [Self; 2] = [Self::Single, Self::TwoUp];
++
++ /// The name settings and view options use for the base layout: `single` or `two-up`.
++ pub fn as_str(self) -> &'static str {
++ match self {
++ Self::Single => "single",
++ Self::TwoUp => "two-up",
++ }
++ }
++
++ /// The base layout an [`as_str`](Self::as_str) name stands for, in any case. `None` for
++ /// anything else, so callers can reject a typo rather than switch layouts silently.
++ pub fn try_parse(value: &str) -> Option<Self> {
++ Self::ORDER.into_iter().find(|l| l.as_str().eq_ignore_ascii_case(value.trim()))
++ }
++
++ /// Rail icon for this layout.
++ pub fn icon(self) -> &'static str {
++ match self {
++ Self::Single => "file-text",
++ Self::TwoUp => "columns-2",
++ }
++ }
++
++ /// Registry command that switches to this base layout (`PdfCraftApp::execute`).
++ pub fn command(self) -> &'static str {
++ match self {
++ Self::Single => "view.layout.single",
++ Self::TwoUp => "view.layout.two_up",
++ }
++ }
++
++ /// The base layout a [`command`](Self::command) id switches to.
++ pub fn from_command(id: &str) -> Option<Self> {
++ Self::ORDER.into_iter().find(|l| l.command() == id)
++ }
++
++ /// Menu label: the label of [`command`](Self::command), which the catalogs translate.
++ pub fn label(self) -> &'static str {
++ match self {
++ Self::Single => "Single page",
++ Self::TwoUp => "Two-page view",
++ }
++ }
++}
++
++/// The single page-display token settings and the control channel use, combining the base layout
++/// with continuous scrolling. `single` and `two-page` show one page or one spread at a time;
++/// `continuous` (Single, scrolling) and `two-up` (Two-page, scrolling) keep their earlier names
++/// for back-compat.
++pub fn display_token(layout: PageLayout, continuous: bool) -> &'static str {
++ match (layout, continuous) {
++ (PageLayout::Single, false) => "single",
++ (PageLayout::Single, true) => "continuous",
++ (PageLayout::TwoUp, true) => "two-up",
++ (PageLayout::TwoUp, false) => "two-page",
++ }
++}
++
++/// The base layout and continuous flag a [`display_token`] names, in any case. `None` for a typo.
++pub fn parse_display_token(value: &str) -> Option<(PageLayout, bool)> {
++ match value.trim().to_ascii_lowercase().as_str() {
++ "single" => Some((PageLayout::Single, false)),
++ "continuous" => Some((PageLayout::Single, true)),
++ "two-up" => Some((PageLayout::TwoUp, true)),
++ "two-page" | "two-up-single" => Some((PageLayout::TwoUp, false)),
++ _ => None,
++ }
++}
++
++/// How a newly opened document is shown (Preferences ▸ Documents and view). A PDF that asks
++/// for its own layout or zoom gets that instead.
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub struct ViewDefaults {
++ pub layout: PageLayout,
++ /// Whether the base layout scrolls continuously.
++ pub continuous: bool,
++ /// Separate pages with a gutter and shadow; off joins adjacent page edges.
++ pub show_page_gaps: bool,
++ /// [`Fit::Width`] or [`Fit::Page`], or [`Fit::None`] for a fixed `zoom`.
++ pub fit: Fit,
++ /// The zoom with [`Fit::None`] (1.0 = 100%).
++ pub zoom: f32,
++ /// Highlight existing fields: a preference in Acrobat, so it carries over to the next
++ /// document and the next session. On for new settings; a stored choice wins.
++ pub highlight_fields: bool,
++}
++
++impl Default for ViewDefaults {
++ fn default() -> Self {
++ // Single page, scrolling continuously at fit width: the earlier "continuous" default.
++ Self { layout: PageLayout::Single, continuous: true, show_page_gaps: true, fit: Fit::Width, zoom: 1.0, highlight_fields: true }
++ }
++}
++
++impl ViewDefaults {
++ /// The zoom as settings and view options name it: `fit-width`, `fit-page` or a percentage.
++ pub fn zoom_name(&self) -> String {
++ match self.fit {
++ Fit::Page => "fit-page".into(),
++ Fit::None => format!("{}%", (self.zoom * 100.0).round()),
++ Fit::Width | Fit::Height => "fit-width".into(),
++ }
++ }
++
++ /// These defaults with the zoom a [`zoom_name`](Self::zoom_name) names, in any case. `None`
++ /// for anything else, including a percentage outside the 8–6400% the view can show.
++ pub fn with_zoom(self, name: &str) -> Option<Self> {
++ let name = name.trim();
++ let (fit, zoom) = if name.eq_ignore_ascii_case("fit-width") {
++ (Fit::Width, self.zoom)
++ } else if name.eq_ignore_ascii_case("fit-page") {
++ (Fit::Page, self.zoom)
++ } else {
++ let percent = name.strip_suffix('%').unwrap_or(name).trim().parse::<f32>().ok().filter(|p| (8.0..=6400.0).contains(p))?;
++ (Fit::None, percent / 100.0)
++ };
++ Some(Self { fit, zoom, ..self })
++ }
++}
++
++/// A historical snapshot opened from another document: View signed version opens the bytes
++/// a signature covers, View revision an earlier incremental revision. Repeats reuse the tab,
++/// and the tab and notice bar name what it is, instead of piling up identical tabs.
++/// What a historical snapshot shows: the bytes a signature covers, or an earlier
++/// incremental revision.
++#[derive(Clone, Copy, Debug, PartialEq, Eq)]
++pub enum SnapshotKind {
++ SignedVersion,
++ Revision,
++}
++
++#[derive(Clone, Debug)]
++pub struct Snapshot {
++ pub kind: SnapshotKind,
++ /// The 1-based revision shown.
++ pub revision: usize,
++ /// The name of the document it was opened from.
++ pub from: String,
++}
++
++/// The name shown for a document tab, the window title and the Combine picker.
++/// A snapshot reads like its source document, plus the snapshot suffix that tells
++/// the tabs apart. Without a title the file name (which already carries the suffix)
++/// is enough.
++pub(crate) fn tab_label(app: &PdfCraftApp, index: usize) -> Option<String> {
++ let view = app.views.get(index)?;
++ let doc = app.session.get(view.id)?;
++ let Some(snap) = view.snapshot.as_ref() else { return Some(doc.display_name()) };
++ Some(snapshot_tab_name(&doc.display_name(), &doc.name, snap))
++}
++
++/// A snapshot's tab name from its source title, file name and marker. Pure so the
++/// tab strip, window title and picker cannot disagree with each other.
++pub(crate) fn snapshot_tab_name(title: &str, file_name: &str, snapshot: &Snapshot) -> String {
++ if title == file_name {
++ return file_name.into();
++ }
++ if snapshot.kind == SnapshotKind::SignedVersion {
++ format!("{title} (signed version)")
++ } else {
++ format!("{title} (revision {})", snapshot.revision)
++ }
++}
++
++pub struct DocView {
++ pub id: DocId,
++ pub zoom: f32,
++ pub fit: Fit,
++ pub layout: PageLayout,
++ /// Whether the base layout scrolls continuously (independent of `layout`, as in Acrobat).
++ pub continuous: bool,
++ pub show_page_gaps: bool,
++ /// View rotation in degrees clockwise (0, 90, 180, 270); display only, never saved.
++ pub rotation: u16,
++ pub current: usize,
++ pub organize: bool,
++ pub highlight_fields: bool,
++ pub page_input: String,
++ /// Select the replacement page label once if its editor remains focused.
++ pub(crate) page_input_reselect: bool,
++ pub notice_dismissed: bool,
++ /// The user has been told that the form's scripts were turned off (it is said once).
++ pub scripts_off_noted: bool,
++ /// The file changed on disk while this document had unsaved changes: the notice bar asks
++ /// before replacing it (#431).
++ pub disk_changed: bool,
++ /// The snapshot notice bar's Close was clicked: the tab closes at the top of the next
++ /// frame, where no document borrow is held (the bar itself can't close it).
++ pub close_requested: bool,
++ /// A historical snapshot of another document (see [`Snapshot`]): `None` for real documents.
++ pub snapshot: Option<Snapshot>,
++ /// Two-page view: show the first page alone, as a cover (View ▸ Page display).
++ pub cover: bool,
++ /// Previous view / Next view: pages visited before (and after, once going back).
++ pub back: Vec<usize>,
++ pub forward: Vec<usize>,
++ /// Pending navigation: page and fraction down the page to align with the viewport top.
++ pub goto: Option<(usize, f32)>,
++ /// Pending position within that page from a destination ([`Self::go_to_dest`]): the point
++ /// (fractions of the displayed page; `None` leaves that axis to `goto`) to put `rel` points
++ /// from the viewport's top-left corner.
++ goto_point: Option<(usize, Option<f32>, Option<f32>, Vec2)>,
++ /// Pending keyboard scrolling, in points down (negative: up): ↓ / ↑, and Page Down /
++ /// Page Up where the pages scroll.
++ pub key_scroll: f32,
++ /// Single-page view, as last drawn: the page shown and whether the view was at its top and
++ /// at its bottom, so ↑ / ↓ turn the page there instead of doing nothing (#273).
++ single_edges: Option<(usize, bool, bool)>,
++ /// Briefly outline an annotation after navigating to it from a panel.
++ pub flash: Option<(usize, [f32; 4], f64)>,
++ /// Compare files: differences shaded on this document's pages (page, user-space box, colour).
++ pub compare_marks: Vec<(usize, [f32; 4], Color32)>,
++ /// Full screen (Acrobat's Full Screen Mode): the display saved on entering it, while the view
++ /// shows one page or spread at a time, fitted to the screen. `None` outside full screen.
++ presentation: Option<SavedDisplay>,
++ pages: HashMap<usize, PageTex>,
++ /// Pages the renderer could not draw, with the reason (never re-requested).
++ errors: HashMap<usize, String>,
++ /// When each still-missing page was first shown, to flag unusually slow renders.
++ waiting_since: HashMap<usize, f64>,
++ thumbs: HashMap<usize, PageTex>,
++ /// This frame's consumers, with visible cells ahead of prefetch.
++ thumb_demand: HashMap<usize, bool>,
++ frame_queue: Vec<RenderRequest>,
++ /// A page-list edit after layout invalidates this frame's scales and page indexes.
++ frame_geometry_changed: bool,
++ /// Pages on screen this frame. Their rasters and tiles are always admitted: the viewport
++ /// bounds them, and the byte and count limits apply to prefetch only.
++ frame_visible: HashSet<usize>,
++ /// Thumbnails that are out of date (still shown until their replacement arrives).
++ stale_thumbs: HashSet<usize>,
++ /// Tiles that are out of date (still shown until their replacement arrives).
++ stale_tiles: HashSet<(usize, u64, u32, u32)>,
++ /// Scale tag of the current zoom level, as last drawn.
++ current_scale_tag: u64,
++ /// Print dialog: the current sheet's pages at the preview pane's device resolution.
++ print_pages: HashMap<usize, (u64, TextureHandle)>,
++ /// Print dialog: preview rasters become luminance when Print ▸ Print in grayscale is on.
++ grayscale: bool,
++ /// Sharp tiles of large pages: (page, scale tag, tile x, tile y) → texture. Tiles of an
++ /// earlier zoom stay, drawn stretched, until those of the current one cover the page.
++ tiles: HashMap<(usize, u64, u32, u32), TextureHandle>,
++ /// Text layers, extracted in the background on demand (selection, find, copy).
++ pub(crate) texts: HashMap<usize, Arc<PageText>>,
++ pub(crate) text_failed: HashSet<usize>,
++ pub find: Option<Find>,
++ selection: Option<Selection>,
++ /// Select All while scrolling continuously (#2639): the text of every page of the document,
++ /// highlighted on each page as its text arrives and copied whole ([`DocView::whole_text`]).
++ pub(crate) all_text: bool,
++ /// The selection whose action bar was closed by its Copy button: the bar stays hidden
++ /// until the selection changes, as it does once a markup button consumes the selection.
++ actions_dismissed: Option<Selection>,
++ /// The drag in progress started a text selection: the wheel scrolls the page during it
++ /// (#721), which egui's scroll area refuses while anything is dragged.
++ selection_drag: bool,
++ /// Clicks in the current run on the text layer: two select a word, three the line, four the page.
++ clicks: u32,
++ last_press_time: f64,
++ last_press_pos: Option<Pos2>,
++ /// Full screen: a click's page turn, when it falls due (input time) and which way. It waits
++ /// out the double-click time, as the click may be the first of a double-click selecting a word.
++ present_turn: Option<(f64, bool)>,
++ last_queue: Vec<RenderRequest>,
++ /// Finished renders wait in the pool instead of reaching the view (tests): what the view
++ /// shows before a raster arrives can be looked at however fast the renderer is.
++ pub hold_rasters: bool,
++ viewport_w: f32,
++ viewport_h: f32,
++ page_count: usize,
++ /// Page heights in points (view space), for mapping text positions to scroll offsets.
++ page_heights: Vec<f32>,
++ /// Screen rects of the pages drawn last frame (hit-testing, tests, automation).
++ screen_rects: Vec<(usize, Rect)>,
++ screen_xforms: Vec<(usize, PageXform)>,
++ /// The scroll viewport on screen last frame.
++ viewport_screen: Rect,
++ /// Pending zoom anchor: page, fractional position (also in gutters), and viewport offset.
++ zoom_anchor: Option<(usize, f32, f32, Vec2)>,
++ /// The page view has been shown, so its scroll offset in egui's memory is this document's.
++ shown: bool,
++ /// Pages that Edit already tried to turn into editable text (once each, so Undo sticks).
++ pub(crate) auto_ocr_tried: std::collections::HashSet<usize>,
++ /// Pages Edit looked at and found not to be a scan, with the document's edit generation then:
++ /// they are looked at again only after the document changes (an image added, say).
++ pub(crate) auto_ocr_skipped: std::collections::HashMap<usize, u64>,
++ /// Turns wheel input into page turns in single-page view.
++ wheel: crate::wheel_pager::WheelPager,
++ /// Until this time, apply precise (touchpad) wheel input 1:1 and suppress egui's eased copy.
++ /// A precision touchpad sends pixel-precise `MouseWheel` events; on Windows they arrive with
++ /// no `TouchPhase::Start`, so egui eases deltas of 8 px or more and the page lags the finger
++ /// (#759). The window outlasts a single frame so egui's internal easing tail is suppressed as
++ /// it decays, instead of adding on top of the 1:1 motion.
++ precise_scroll_until: f64,
++ /// Per-axis line-wheel state; horizontal input cannot extend a vertical burst.
++ wheel_boost_x: crate::wheel_boost::WheelBoost,
++ wheel_boost_y: crate::wheel_boost::WheelBoost,
++ /// Touchpad momentum after the fingers lift (#531).
++ fling: crate::fling::Fling,
++ pub(crate) auto_scroll: crate::autoscroll::AutoScroll,
++ /// Pages selected in the organize grid or the Pages panel (0-based). Empty means "the current page".
++ pub selected: BTreeSet<usize>,
++ pub(crate) organize_range: crate::page_selection_ui::PageSelectionInput,
++ /// Anchor for ⇧-click range selection in the organize grid.
++ select_anchor: Option<usize>,
++ /// An edit requested by the view (organize toolbar, keys), applied by the app this frame.
++ pub pending_edit: Option<Edit>,
++ /// Edit text: the lines per page (with the document generation they were read at), and the
++ /// line being edited.
++ pub(crate) edit_lines: HashMap<usize, (u64, Vec<pdfcraft_engine::TextBlock>)>,
++ pub line_editor: Option<crate::edit_text_ui::LineEditor>,
++ /// The paragraph editor whose edit was just queued. It opens again with its typing if the
++ /// edit fails, as a refused form field does.
++ pub(crate) applied_editor: Option<crate::edit_text_ui::LineEditor>,
++ /// The box a click switched to while another paragraph was being edited: it opens once the
++ /// queued edit has applied, so its index and geometry come from the document it now is (#2022).
++ /// It is held as the paragraph's text and box, not an index: the edit can regroup the page.
++ pub(crate) pending_text_open: Option<(usize, pdfcraft_engine::BlockKey)>,
++ pub(crate) text_background: crate::edit_text_ui::TextBackground,
++ pub(crate) move_background: crate::content_ui::MoveBackground,
++ /// Edit text & images: the images per page (by document generation), and the selected one.
++ pub(crate) edit_images: HashMap<usize, (u64, Vec<pdfcraft_engine::PageImage>)>,
++ pub(crate) objects: crate::object_ui::ObjectSelection,
++ pub image_selection: Option<crate::edit_text_ui::ImageSelection>,
++ /// Select tool: the picture clicked, to copy in its original format.
++ pub picked_image: Option<crate::image_pick::PickedImage>,
++ /// A paragraph box being dragged (moved, or resized from its right edge) in Edit text.
++ pub block_drag: Option<crate::edit_text_ui::BlockDrag>,
++ /// Commenting state: selected comment, gestures, composer.
++ pub comments: crate::comments::CommentView,
++ pub measure: crate::measure_ui::MeasureView,
++ /// Form filling state: the focused field.
++ pub forms: crate::forms_ui::FormView,
++ /// Prepare a form: the selected field and the gesture in progress.
++ pub prepare: crate::prepare::PrepareView,
++ /// Prepare a form ▸ Calculation order.
++ pub(crate) calc_order: crate::calc_order_ui::CalcOrderView,
++ /// Edit a PDF: the selected added item, the text being typed.
++ pub content: crate::content_ui::ContentView,
++ /// Edit a PDF ▸ Link tool state.
++ pub links: crate::link_ui::LinkView,
++ /// Redact tool: the box being drawn, and a mark to add (page, quads) for the app to style.
++ pub redact_drag: crate::redact_ui::AreaDrag,
++ pub pending_redaction: Option<(usize, Vec<[f64; 8]>)>,
++ /// A crop rectangle being dragged (Crop tool).
++ pub crop_drag: crate::crop::CropDrag,
++ /// Enhance ▸ Camera image: the quadrilateral being placed.
++ pub camera: Option<crate::camera_ui::CameraQuad>,
++ /// Enhance ▸ Camera image: the page just enhanced, while the bar offers rotate, crop and
++ /// organize.
++ pub camera_done: Option<crate::camera_ui::CameraDone>,
++ /// Use a certificate: a signature rectangle being drawn, or an empty signature field clicked.
++ pub sign: crate::sign_ui::SignView,
++ /// Organize: the pages being dragged to a new place.
++ pub org_drag: Option<Vec<usize>>,
++ /// Pages panel: the pages being dragged to a new place.
++ pub panel_drag: Option<Vec<usize>>,
++ /// Transient keyboard owner for this document's Pages sidebar.
++ pub(crate) pages_focus_id: Option<egui::Id>,
++ /// Marquee Zoom / Snapshot: the rectangle being dragged (page, start), and a finished one.
++ pub marquee: Option<(usize, Pos2)>,
++ pub marquee_done: Option<crate::zoom_snap::Marquee>,
++ /// Fill & Sign text being typed.
++ pub fill_text: Option<crate::fill_sign::TypeBox>,
++ /// A queued Fill & Sign signature: select it after its edit succeeds, then leave placement.
++ pub(crate) fill_signature_page: Option<usize>,
++ pub(crate) signature_drag: crate::signature_drag::SignatureDrag,
++ /// What the page modules claim for the cursor this frame; `document_area` sets it once.
++ pub(crate) cursor: crate::page_cursor::PageCursor,
++ /// Who the press the pages hold belongs to.
++ pub(crate) router: crate::page_router::PageRouter,
++ /// Escape cancelled the press the pages hold: the page ignores it until the button is up, so
++ /// its release neither clicks nor ends a drag.
++ pub(crate) press_cancelled: bool,
++ /// A non-edit action requested by the organize toolbar, handled by the app.
++ pub pending_action: Option<ViewAction>,
++ /// The grid gap the next inserted files go to (set by a "+" between pages); otherwise they
++ /// go after the selection.
++ pub insert_at: Option<usize>,
++ /// The page-grid gap under the pointer this frame (where dropped files go), when it can
++ /// take pages.
++ pub grid_gap: Option<usize>,
++ /// The visible part of the Pages panel as last drawn, when it can take dropped files
++ /// (cleared each frame before the panels draw).
++ pub pages_panel: Option<Rect>,
++ /// The Pages-panel gap under the pointer (where dropped files go), when it can take pages.
++ pub panel_gap: Option<usize>,
++ /// The current page the Pages panel last followed: it scrolls to the current page's
++ /// thumbnail only when the page changes.
++ pub(crate) panel_followed: Option<usize>,
++ /// Scale of the thumbnails in the Pages inspector, independent of document and grid zoom.
++ thumbnail_zoom: f32,
++ /// How large the page grid draws its pages (1.0 = the usual thumbnails).
++ grid_zoom: f32,
++ /// A zoom asked for this frame (toolbar, pinch, keys); the grid applies it once it has laid
++ /// out, so it can keep the same pages in view.
++ grid_zoom_request: Option<f32>,
++ /// The page to keep in place after a grid zoom, and how far below the top of the grid its
++ /// cell was.
++ grid_anchor: Option<(usize, f32)>,
++ /// The page the grid scrolls into view on its next frame (Organize just opened on it).
++ grid_reveal: Option<usize>,
++ /// Sharp renders of the pages the grid shows larger than a thumbnail: only those in view,
++ /// at the size drawn (device pixels wide).
++ grid_pages: HashMap<usize, (u32, TextureHandle)>,
++ /// Zoom-settle debounce for heavy pages: the last requested device scale, and the egui
++ /// time until which sharp whole-page/tile re-renders are held back. While the zoom is
++ /// still moving, every frame would otherwise cancel the in-flight full-page interpret
++ /// (seconds on image-heavy CAD plots) and start it over, so a sharp raster never
++ /// arrives. The stale raster stays on screen stretched meanwhile.
++ last_scale: f32,
++ sharp_hold_until: f64,
++ /// Pan-settle debounce for tiled pages: the scroll offset last frame, and the egui time
++ /// until which sharp tile re-renders are held back. Each tile costs a full page
++ /// interpret, so a drag would otherwise queue a storm of them (one per frame) while the
++ /// in-flight ones cannot be cancelled mid-operator. Older tiles and the backdrop stay
++ /// stretched meanwhile; fresh tiles go out once the pan settles.
++ pub(crate) last_scroll: Vec2,
++ /// Side-by-side compare: scroll to here this frame, following the other half.
++ pub(crate) sync_scroll: Option<Vec2>,
++ /// The page widget's id, by which the Hand tool finds its drag before the pages are laid out.
++ page_widget: Option<egui::Id>,
++ pan_hold_until: f64,
++ /// Pages whose raster proved slow (see `SLOW_MILLIS`): only these get the zoom/pan
++ /// settle holds. Fast pages render straight through, with no added latency.
++ slow_pages: HashSet<usize>,
++}
++
++/// Organize-toolbar actions that need the app (file pickers, new tabs, dialogs).
++#[derive(Clone, Copy, Debug, PartialEq, Eq)]
++pub enum ViewAction {
++ InsertFromFile,
++ /// Insert files at a gap of the page grid (0 = before the first page).
++ InsertFromFileAt(usize),
++ /// Replace the selected pages with pages from another file.
++ ReplacePages,
++ /// Save the document as the grid shows it.
++ Save,
++ Extract,
++ Split,
++ /// Split before each selected page: opens Split on that mode.
++ SplitBeforeSelected,
++ /// Copy the selected pages (Cut also deletes them).
++ CopyPages {
++ cut: bool,
++ },
++ /// Paste copied pages after the selection.
++ PastePages,
++ /// Enhance ▸ Camera image: straighten what the corners enclose.
++ EnhanceCamera,
++ /// Leave Enhance ▸ Camera image (Cancel before Enhance page, Done after it).
++ CancelCamera,
++ /// After Enhance page: turn the enhanced page a quarter.
++ CameraRotate {
++ clockwise: bool,
++ },
++ /// After Enhance page: enhance again at the slider's clean-up level.
++ CameraLevel,
++ /// After Enhance page: leave the tool for this command (Crop pages, Organize pages).
++ CameraNext(&'static str),
++}
++
++/// Default page right-click menu actions.
++#[derive(Clone, Debug, PartialEq)]
++pub(crate) enum PageMenuAction {
++ RotateCCW(usize),
++ RotateCW(usize),
++ Print,
++ SelectTool,
++ HandTool,
++ MarqueeZoom,
++ Copy,
++ Paste(usize, [f64; 2]),
++ AddComment(usize, [f64; 2]),
++ AddBookmark(usize),
++ TakeSnapshot,
++ EditPdf,
++ AddText,
++ AddImage,
++ DeletePage(usize),
++ ExportPdf,
++ Find,
++ DocProperties,
++ FlattenComments,
++}
++
++pub(crate) struct PageMenuPerms {
++ pub can_modify: bool,
++ pub can_assemble: bool,
++ pub can_annotate: bool,
++ pub can_print: bool,
++ pub can_copy: bool,
++ pub can_paste: bool,
++ pub has_annotations: bool,
++}
++
++pub(crate) fn default_page_context_menu(ui: &mut egui::Ui, page: usize, at: [f64; 2], perms: PageMenuPerms, action: &mut Option<PageMenuAction>) {
++ // Top horizontal icon bar: Rotate CCW, Print, Select tool, Hand tool, Marquee Zoom
++ ui.horizontal(|ui| {
++ ui.spacing_mut().item_spacing.x = 4.0;
++ if crate::icons::button(ui, "rotate-ccw", 24.0, false, tl!("Rotate view counterclockwise")).clicked() {
++ *action = Some(PageMenuAction::RotateCCW(page));
++ ui.close();
++ }
++ ui.add_enabled_ui(perms.can_print, |ui| {
++ if crate::icons::button(ui, "printer", 24.0, false, tl!("Print")).clicked() {
++ *action = Some(PageMenuAction::Print);
++ ui.close();
++ }
++ });
++ if crate::icons::button(ui, "mouse-pointer-2", 24.0, false, tl!("Select Tool")).clicked() {
++ *action = Some(PageMenuAction::SelectTool);
++ ui.close();
++ }
++ if crate::icons::button(ui, "hand", 24.0, false, tl!("Hand Tool")).clicked() {
++ *action = Some(PageMenuAction::HandTool);
++ ui.close();
++ }
++ if crate::icons::button(ui, "zoom-in", 24.0, false, tl!("Marquee Zoom")).clicked() {
++ *action = Some(PageMenuAction::MarqueeZoom);
++ ui.close();
++ }
++ });
++
++ ui.separator();
++
++ if ui.add_enabled(perms.can_copy, egui::Button::new(tl!("Copy")).shortcut_text("Ctrl+C")).clicked() {
++ *action = Some(PageMenuAction::Copy);
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_paste, egui::Button::new(tl!("Paste")).shortcut_text("Ctrl+V")).clicked() {
++ *action = Some(PageMenuAction::Paste(page, at));
++ ui.close();
++ }
++
++ ui.separator();
++
++ if ui.add_enabled(perms.can_annotate, egui::Button::new(tl!("Add comment"))).clicked() {
++ *action = Some(PageMenuAction::AddComment(page, at));
++ ui.close();
++ }
++ if perms.has_annotations && ui.add_enabled(perms.can_modify, egui::Button::new(tl!("Flatten comments"))).clicked() {
++ *action = Some(PageMenuAction::FlattenComments);
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_assemble, egui::Button::new(tl!("Add bookmark"))).clicked() {
++ *action = Some(PageMenuAction::AddBookmark(page));
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_copy, egui::Button::new(tl!("Take a snapshot"))).clicked() {
++ *action = Some(PageMenuAction::TakeSnapshot);
++ ui.close();
++ }
++
++ ui.separator();
++
++ if ui.add_enabled(perms.can_modify, egui::Button::new(egui::RichText::new(tl!("Edit this PDF")).strong())).clicked() {
++ *action = Some(PageMenuAction::EditPdf);
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_modify, egui::Button::new(tl!("Add text"))).clicked() {
++ *action = Some(PageMenuAction::AddText);
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_modify, egui::Button::new(tl!("Add image"))).clicked() {
++ *action = Some(PageMenuAction::AddImage);
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_assemble, egui::Button::new(tl!("Delete page"))).clicked() {
++ *action = Some(PageMenuAction::DeletePage(page));
++ ui.close();
++ }
++ if ui.add_enabled(perms.can_copy, egui::Button::new(tl!("Export this PDF"))).clicked() {
++ *action = Some(PageMenuAction::ExportPdf);
++ ui.close();
++ }
++
++ ui.separator();
++
++ if ui.button(tl!("Find text or tools")).clicked() {
++ *action = Some(PageMenuAction::Find);
++ ui.close();
++ }
++ if ui.button(tl!("Rotate view clockwise")).clicked() {
++ *action = Some(PageMenuAction::RotateCW(page));
++ ui.close();
++ }
++ if ui.button(tl!("Document properties…")).clicked() {
++ *action = Some(PageMenuAction::DocProperties);
++ ui.close();
++ }
++}
++
++impl DocView {
++ /// Whether a middle-button scrolling gesture is active (tests and automation).
++ pub fn auto_scrolling(&self) -> bool {
++ self.auto_scroll.active()
++ }
++
++ /// Whether a held middle-button pan is active, outside Linux (tests and automation).
++ pub fn middle_panning(&self) -> bool {
++ self.auto_scroll.panning()
++ }
++
++ /// Abandon only pointer previews: clearing egui's held button must not commit an app-owned
++ /// grab through a tool's `!primary_down` fallback. Keep selections, text drafts and edits
++ /// that were already completed before cancellation.
++ pub(crate) fn cancel_pointer_gestures(&mut self) {
++ self.marquee = None;
++ self.marquee_done = None;
++ self.comments.gesture = None;
++ self.links.drag = None;
++ self.sign.drag = None;
++ self.redact_drag = None;
++ self.crop_drag = None;
++ self.block_drag = None;
++ self.org_drag = None;
++ self.panel_drag = None;
++ self.selection_drag = false;
++ self.router.cancel();
++ self.auto_scroll.cancel();
++ self.prepare.cancel_pointer_gesture();
++ self.content.cancel_pointer_gesture();
++ self.objects.cancel_pointer_gesture();
++ if let Some(image) = &mut self.image_selection {
++ image.cancel_pointer_gesture();
++ }
++ }
++
++ /// A view of a newly opened document, shown as `defaults` say. Opening a file then applies
++ /// any layout or zoom the PDF itself asks for.
++ pub fn new(id: DocId, info: &DocInfo, defaults: ViewDefaults) -> Self {
++ Self {
++ id,
++ zoom: defaults.zoom,
++ fit: defaults.fit,
++ layout: defaults.layout,
++ continuous: defaults.continuous,
++ show_page_gaps: defaults.show_page_gaps,
++ rotation: 0,
++ current: 0,
++ organize: false,
++ highlight_fields: defaults.highlight_fields,
++ page_input: "1".into(),
++ page_input_reselect: false,
++ notice_dismissed: false,
++ scripts_off_noted: false,
++ disk_changed: false,
++ close_requested: false,
++ snapshot: None,
++ cover: false,
++ back: Vec::new(),
++ forward: Vec::new(),
++ goto: None,
++ goto_point: None,
++ key_scroll: 0.0,
++ single_edges: None,
++ flash: None,
++ compare_marks: Vec::new(),
++ presentation: None,
++ pages: HashMap::new(),
++ errors: HashMap::new(),
++ waiting_since: HashMap::new(),
++ thumbs: HashMap::new(),
++ thumb_demand: HashMap::new(),
++ frame_queue: Vec::new(),
++ frame_geometry_changed: false,
++ frame_visible: HashSet::new(),
++ stale_thumbs: HashSet::new(),
++ stale_tiles: HashSet::new(),
++ current_scale_tag: 0,
++ print_pages: HashMap::new(),
++ grayscale: false,
++ tiles: HashMap::new(),
++ texts: HashMap::new(),
++ text_failed: HashSet::new(),
++ find: None,
++ selection: None,
++ all_text: false,
++ actions_dismissed: None,
++ selection_drag: false,
++ clicks: 0,
++ last_press_time: 0.0,
++ last_press_pos: None,
++ present_turn: None,
++ last_queue: Vec::new(),
++ hold_rasters: false,
++ viewport_w: 800.0,
++ viewport_h: 600.0,
++ page_count: info.pages.len(),
++ page_heights: info.pages.iter().map(|p| p.height).collect(),
++ screen_rects: Vec::new(),
++ screen_xforms: Vec::new(),
++ viewport_screen: Rect::NOTHING,
++ zoom_anchor: None,
++ shown: false,
++ auto_ocr_tried: Default::default(),
++ auto_ocr_skipped: Default::default(),
++ wheel: Default::default(),
++ precise_scroll_until: 0.0,
++ wheel_boost_x: Default::default(),
++ wheel_boost_y: Default::default(),
++ fling: Default::default(),
++ auto_scroll: Default::default(),
++ selected: BTreeSet::new(),
++ select_anchor: None,
++ organize_range: Default::default(),
++ pending_edit: None,
++ edit_lines: HashMap::new(),
++ line_editor: None,
++ applied_editor: None,
++ pending_text_open: None,
++ text_background: Default::default(),
++ move_background: Default::default(),
++ edit_images: HashMap::new(),
++ objects: Default::default(),
++ image_selection: None,
++ picked_image: None,
++ block_drag: None,
++ pending_action: None,
++ insert_at: None,
++ grid_gap: None,
++ pages_panel: None,
++ panel_gap: None,
++ panel_followed: None,
++ thumbnail_zoom: 1.0,
++ grid_zoom: 1.0,
++ grid_zoom_request: None,
++ grid_anchor: None,
++ grid_reveal: None,
++ grid_pages: HashMap::new(),
++ last_scale: 0.0,
++ sharp_hold_until: 0.0,
++ last_scroll: Vec2::ZERO,
++ sync_scroll: None,
++ page_widget: None,
++ pan_hold_until: 0.0,
++ slow_pages: HashSet::new(),
++ comments: Default::default(),
++ measure: Default::default(),
++ forms: Default::default(),
++ prepare: Default::default(),
++ calc_order: Default::default(),
++ content: Default::default(),
++ links: Default::default(),
++ redact_drag: None,
++ pending_redaction: None,
++ crop_drag: None,
++ camera: None,
++ camera_done: None,
++ sign: Default::default(),
++ org_drag: None,
++ panel_drag: None,
++ pages_focus_id: None,
++ marquee: None,
++ marquee_done: None,
++ fill_text: None,
++ fill_signature_page: None,
++ signature_drag: Default::default(),
++ cursor: Default::default(),
++ router: Default::default(),
++ press_cancelled: false,
++ }
++ }
++
++ /// The document's content or page list changed (an edit, undo or redo): drop caches and
++ /// adopt the new page geometry, keeping the reader's place where possible.
++ pub fn document_changed(&mut self, info: &DocInfo) {
++ self.invalidate_content();
++ self.frame_geometry_changed = true;
++ self.page_count = info.pages.len();
++ self.page_heights = info.pages.iter().map(|p| p.height).collect();
++ let last = self.page_count.saturating_sub(1);
++ self.current = self.current.min(last);
++ self.selected.retain(|p| *p <= last);
++ // Text being typed on a page that no longer exists goes with the page.
++ if self.content.draft.as_ref().is_some_and(|d| d.page > last || self.page_count == 0) {
++ self.content.draft = None;
++ }
++ if self.select_anchor.is_some_and(|a| a > last) {
++ self.select_anchor = None;
++ }
++ self.goto = None;
++ self.goto_point = None;
++ self.zoom_anchor = None;
++ self.flash = None;
++ // Thumbnail drags hold page indexes from before the change.
++ self.panel_drag = None;
++ self.org_drag = None;
++ self.reset_coarse_state();
++ }
++
++ /// The document was replaced by a newer version of its file (#431). The reader's place
++ /// stays: zoom, layout, rotation, page, scroll, full screen, view history and the find bar,
++ /// the snapshot marker and its pending close, and the page images on screen until their
++ /// replacements are drawn, so nothing flashes. Everything that pointed into the old version (a selected
++ /// comment, a field being typed in, text being edited, a drag, page selection) starts
++ /// afresh, so no stale index reaches the new one.
++ pub fn document_replaced(&mut self, info: &DocInfo) {
++ let mut fresh = Self::new(self.id, info, ViewDefaults::default());
++ fresh.zoom = self.zoom;
++ fresh.fit = self.fit;
++ fresh.layout = self.layout;
++ fresh.continuous = self.continuous;
++ fresh.show_page_gaps = self.show_page_gaps;
++ fresh.rotation = self.rotation;
++ fresh.current = self.current;
++ fresh.organize = self.organize;
++ fresh.grid_zoom = self.grid_zoom;
++ fresh.thumbnail_zoom = self.thumbnail_zoom;
++ // Same document id, so the scroll offset egui keeps for it is still this document's.
++ fresh.shown = self.shown;
++ fresh.highlight_fields = self.highlight_fields;
++ fresh.notice_dismissed = self.notice_dismissed;
++ fresh.snapshot = self.snapshot.clone();
++ fresh.close_requested = self.close_requested;
++ fresh.cover = self.cover;
++ // Still presenting, with the display to go back to on leaving.
++ fresh.presentation = self.presentation.take();
++ fresh.back = std::mem::take(&mut self.back);
++ fresh.forward = std::mem::take(&mut self.forward);
++ fresh.find = self.find.take();
++ fresh.pages = std::mem::take(&mut self.pages);
++ fresh.thumbs = std::mem::take(&mut self.thumbs);
++ fresh.viewport_w = self.viewport_w;
++ fresh.viewport_h = self.viewport_h;
++ fresh.viewport_screen = self.viewport_screen;
++ *self = fresh;
++ // Marks the kept images out of date and clamps the page to the new page count.
++ self.document_changed(info);
++ self.page_input = self.current_page_label(info.pages.get(self.current).map(|page| page.label.as_str()));
++ self.page_input_reselect = true;
++ }
++
++ /// Select pages in the organize grid (tests, automation). Empty clears the selection.
++ pub(crate) fn page_count(&self) -> usize {
++ self.page_count
++ }
++
++ /// Drops wheel state after a discontinuous jump so the destination cannot
++ /// inherit momentum from the previous viewport.
++ fn reset_coarse_state(&mut self) {
++ self.wheel.clear_motion();
++ self.wheel_boost_x.reset();
++ self.wheel_boost_y.reset();
++ }
++}
++
++/// The renders a frame wants (page, scale, tag, tile), and the pages visible.
++type PageRenders = (Vec<(usize, f32, u64, Option<Tile>)>, Vec<usize>);
++
++/// What the page-scroll closure reads: this frame's zoom, scrolling, tool and document state.
++#[derive(Clone, Copy)]
++struct PageEnv<'a> {
++ added: &'a Vec<pdfcraft_engine::Added>,
++ allowed: bool,
++ author: &'a String,
++ auto_delta: Vec2,
++ avail: Rect,
++ boost_extra: Vec2,
++ by_line: &'a String,
++ calc_order: bool,
++ can_copy: bool,
++ can_crop: bool,
++ can_fill: bool,
++ can_modify: bool,
++ comments_hidden: bool,
++ content_h: f32,
++ content_w: f32,
++ custom_stamp: &'a Option<crate::stamps_ui::CustomStamp>,
++ date_text: &'a Result<String, String>,
++ doc: &'a pdfcraft_engine::Document,
++ doc_links: &'a Vec<pdfcraft_engine::LinkItem>,
++ edge_delta: Vec2,
++ editing_content: bool,
++ form: &'a Arc<Vec<pdfcraft_engine::FormField>>,
++ generation: u64,
++ glide: Vec2,
++ hand: bool,
++ info: &'a DocInfo,
++ initials: &'a Option<crate::fill_sign::SavedSig>,
++ key_scroll: f32,
++ middle_gesture: bool,
++ modal: bool,
++ navigation_enabled: bool,
++ paper: Color32,
++ ppp: f32,
++ precise_scroll: Vec2,
++ prefs: &'a comments::CommentPrefs,
++ preparing: bool,
++ rects: &'a Vec<Rect>,
++ redact_mark: ([f64; 3], f64),
++ scale: f32,
++ selection_wheel: bool,
++ selects_text: bool,
++ signature: &'a Option<crate::fill_sign::SavedSig>,
++ single_paging: bool,
++ t: &'a Tokens,
++ table_preview: &'a Option<crate::field_table_ui::Preview>,
++ tag: u64,
++ text_style: &'a pdfcraft_engine::AddedText,
++ tool: QuickTool,
++ units: crate::units::Unit,
++ unobstructed: bool,
++ visible_pages: &'a Vec<usize>,
++ y_shift: f32,
++ fill_text_size: f64,
++ fill_text_color: [f64; 3],
++ today: (i64, u32, u32),
++ can_paste_image: bool,
++}
++
++/// What the page-scroll closure produces for the rest of the frame.
++#[derive(Default)]
++struct PageOut {
++ canvas_action: Option<comments::CanvasAction>,
++ clicked_link: Option<LinkTarget>,
++ copy_selected: bool,
++ field_menu: Option<FieldMenu>,
++ field_placed: bool,
++ field_props: bool,
++ hover_text: Option<(Pos2, String)>,
++ image_action: Option<crate::edit_text_ui::ImageAction>,
++ open_boxes: bool,
++ open_initials: bool,
++ open_signature: bool,
++ open_table: Option<(usize, [f64; 4])>,
++ page_action: Option<PageMenuAction>,
++ primary_down: bool,
++ refused: Option<String>,
++ stamp_placed: bool,
++}
++
++/// The page-scroll closure's body: lays out and draws the visible pages, their tools and overlays, and
++/// returns the renders wanted and the pages visible.
++/// The state of one pass over the visible pages: what is read, and what the pages add up to.
++struct PageFrame<'a> {
++ env: PageEnv<'a>,
++ resp: &'a egui::Response,
++ ptr: &'a PagePointer,
++ origin: Pos2,
++ visible: Rect,
++ pressed: bool,
++ pointer: Option<Pos2>,
++ signature_preview: &'a mut Option<(crate::fill_sign::SavedSig, egui::TextureHandle)>,
++ out: PageOut,
++ wanted: Vec<(usize, f32, u64, Option<Tile>)>,
++ visible_now: Vec<usize>,
++ current: usize,
++ best_overlap: f32,
++ current_overlap: f32,
++ current_height: f32,
++ pressed_page: Option<usize>,
++ calc_hover: Option<crate::calc_order_ui::Focus>,
++}
++
++/// Visible heights (points) closer than this are equal: one page size can meet the viewport a
++/// fraction of a point differently once the scroll offset is applied.
++const PAGE_TIE: f32 = 0.5;
++
++/// Draws page `i` of the visible pages: its raster, the tools' input and overlays.
++/// One visible page: its index, its rectangle on screen and the transform to and from the page.
++#[derive(Clone, Copy)]
++struct PageSlot {
++ i: usize,
++ r: Rect,
++ xf: PageXform,
++}
++
++/// Paints the page's paper, shadow and raster (or why it can't be shown), and asks for the renders
++/// it wants.
++#[allow(clippy::too_many_arguments)]
++fn paint_image_suppressed(
++ xf: &PageXform,
++ painter: &egui::Painter,
++ tex: egui::TextureId,
++ u0: f32,
++ v0: f32,
++ u1: f32,
++ v1: f32,
++ suppress: Option<[f32; 4]>,
++ is_full_page_tex: bool,
++) {
++ let Some([su0, sv0, su1, sv1]) = suppress else {
++ if is_full_page_tex {
++ xf.paint_page_texture_rect(painter, tex, u0, v0, u1, v1);
++ } else {
++ xf.paint_image(painter, tex, u0, v0, u1, v1);
++ }
++ return;
++ };
++ let iu0 = u0.max(su0);
++ let iu1 = u1.min(su1);
++ let iv0 = v0.max(sv0);
++ let iv1 = v1.min(sv1);
++ if iu0 >= iu1 || iv0 >= iv1 {
++ if is_full_page_tex {
++ xf.paint_page_texture_rect(painter, tex, u0, v0, u1, v1);
++ } else {
++ xf.paint_image(painter, tex, u0, v0, u1, v1);
++ }
++ return;
++ }
++ let map_uv = |pu: f32, pv: f32| -> (f32, f32) {
++ if is_full_page_tex {
++ (pu, pv)
++ } else {
++ let du = (u1 - u0).max(1e-6);
++ let dv = (v1 - v0).max(1e-6);
++ ((pu - u0) / du, (pv - v0) / dv)
++ }
++ };
++ let draw_strip = |pu0: f32, pv0: f32, pu1: f32, pv1: f32| {
++ if pu1 <= pu0 || pv1 <= pv0 {
++ return;
++ }
++ let (tu0, tv0) = map_uv(pu0, pv0);
++ let (tu1, tv1) = map_uv(pu1, pv1);
++ xf.paint_image_region(painter, tex, pu0, pv0, pu1, pv1, tu0, tv0, tu1, tv1);
++ };
++
++ if iv0 > v0 {
++ draw_strip(u0, v0, u1, iv0);
++ }
++ if v1 > iv1 {
++ draw_strip(u0, iv1, u1, v1);
++ }
++ if iu0 > u0 {
++ draw_strip(u0, iv0, iu0, iv1);
++ }
++ if u1 > iu1 {
++ draw_strip(iu1, iv0, u1, iv1);
++ }
++}
++
++fn paint_page_surface(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot) {
++ let PageEnv { paper, ppp, scale, t, tag, .. } = frame.env;
++ let PageSlot { i, r, xf } = *slot;
++ let visible = frame.visible;
++ let info = frame.env.info;
++ let painter = ui.painter();
++ // Shadows would darken the preceding page when pages touch.
++ if view.show_page_gaps {
++ painter.add(egui::epaint::Shadow { offset: [0, 3], blur: 14, spread: 0, color: t.page_shadow }.as_shape(r, CornerRadius::ZERO));
++ }
++ painter.rect_filled(r, CornerRadius::ZERO, paper);
++ if let Some(err) = view.errors.get(&i) {
++ painter.rect_filled(r, CornerRadius::ZERO, Color32::from_rgb(0xFB, 0xF4, 0xF4));
++ icons::paint(
++ ui,
++ Rect::from_center_size(r.center() - vec2(0.0, 26.0), vec2(28.0, 28.0)),
++ "triangle-alert",
++ 26.0,
++ Color32::from_rgb(0xC8, 0x3A, 0x3A),
++ );
++ let message = crate::i18n::fmt(tl!("This page couldn't be displayed.\n{e}"), &[("e", err)]);
++ let msg = ui.fonts_mut(|f| f.layout(message, theme::regular(12.5), Color32::from_rgb(0x6A, 0x2A, 0x2A), (r.width() - 40.0).max(80.0)));
++ painter.galley(pos2(r.center().x - msg.size().x / 2.0, r.center().y), msg, Color32::BLACK);
++ } else {
++ let (pw_pt, ph_pt) = (info.pages[i].width.max(1.0), info.pages[i].height.max(1.0));
++ // A GPU may take smaller textures than these (OpenGL drivers report as little as
++ // 2048 pixels, and uploading a bigger one aborts), so both stay within its limit.
++ let max_side = ui.ctx().input(|inp| inp.max_texture_side) as f32;
++ let tiled = pw_pt.max(ph_pt) * scale > TILE_THRESHOLD.min(max_side);
++ // Whole-page raster: sharp when small, a low-res backdrop when tiled.
++ let (want_scale, want_tag) = if tiled {
++ let bs = BASE_SIDE.min(max_side) / pw_pt.max(ph_pt);
++ (bs, scale_tag(bs))
++ } else {
++ (scale, tag)
++ };
++ // Under tiles, a whole-page raster sharper than the backdrop (from before the page
++ // grew past the tiling size) is a better backdrop: keep demanding it as it is.
++ match view.pages.get(&i).filter(|p| tiled && p.tag != STALE_TAG && tag_scale(p.tag) >= want_scale) {
++ Some(p) => frame.wanted.push((i, tag_scale(p.tag), p.tag, None)),
++ None => frame.wanted.push((i, want_scale, want_tag, None)),
++ }
++ let doc = frame.env.doc;
++ let active_drag_suppress = if let Some(sel) = view.image_selection.as_ref()
++ && sel.page == i
++ && sel.drag.is_some()
++ {
++ let images = doc.page_images(i);
++ if let Some(img) = images.get(sel.index) {
++ let obb = ImageObb::from_image(img, &xf, info, i);
++ let uvs = obb.corners().map(|c| xf.screen_to_norm(c));
++ let u0 = uvs.iter().map(|(u, _)| *u).fold(1.0f32, f32::min).clamp(0.0, 1.0);
++ let u1 = uvs.iter().map(|(u, _)| *u).fold(0.0f32, f32::max).clamp(0.0, 1.0);
++ let v0 = uvs.iter().map(|(_, v)| *v).fold(1.0f32, f32::min).clamp(0.0, 1.0);
++ let v1 = uvs.iter().map(|(_, v)| *v).fold(0.0f32, f32::max).clamp(0.0, 1.0);
++ if u1 > u0 && v1 > v0 { Some([u0, v0, u1, v1]) } else { None }
++ } else {
++ None
++ }
++ } else {
++ None
++ };
++ match view.pages.get(&i) {
++ Some(p) => {
++ if !tiled && p.tag == want_tag {
++ paint_image_suppressed(
++ &xf.texel_aligned(p.tex.size(), ppp),
++ painter,
++ p.tex.id(),
++ 0.0,
++ 0.0,
++ 1.0,
++ 1.0,
++ active_drag_suppress,
++ true,
++ );
++ } else {
++ // A backdrop, or a raster at an older scale until the new one arrives.
++ paint_image_suppressed(&xf, painter, p.tex.id(), 0.0, 0.0, 1.0, 1.0, active_drag_suppress, true);
++ }
++ }
++ None => {
++ let now = ui.input(|inp| inp.time);
++ let since = *view.waiting_since.entry(i).or_insert(now);
++ let msg = if now - since > 6.0 { "Still rendering — this page is unusually complex…" } else { "Rendering…" };
++ painter.text(r.center(), Align2::CENTER_CENTER, tl!(msg), theme::regular(12.0), t.text_faint);
++ }
++ }
++ if tiled && r.intersects(visible) {
++ paint_page_tiles(ui, view, frame, slot, (pw_pt, ph_pt, max_side), active_drag_suppress);
++ }
++ }
++}
++
++/// Paints the tiles of a page too big to draw whole: the tiles of earlier zooms first, stretched,
++/// then the visible tiles of this one, nearest the viewport centre first, asking for those missing.
++fn paint_page_tiles(ui: &egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot, (pw_pt, ph_pt, max_side): (f32, f32, f32), active_drag_suppress: Option<[f32; 4]>) {
++ let PageEnv { ppp, scale, tag, .. } = frame.env;
++ let PageSlot { i, r, xf } = *slot;
++ let visible = frame.visible;
++ let painter = ui.painter();
++ // Device-pixel geometry of the scaled page, and the visible part of it
++ // (found by mapping the visible screen corners back into the page). Sides round
++ // up as whole-page rasters do, so the last partial row and column get tiles.
++ let (dw, dh) = (device_pixels(pw_pt, scale), device_pixels(ph_pt, scale));
++ // Adaptive tile side for this zoom and page, within the GPU texture limit:
++ // a zoomed viewport or a giant sheet is covered by a few large tiles
++ // instead of many small ones, and each tile costs one full page interpret.
++ // Never zero (u32 division below).
++ let ts = tile_side(scale, dw.max(dh)).min(max_side as u32).max(64);
++ let vis = r.intersect(visible);
++ let corners = [vis.left_top(), vis.right_top(), vis.right_bottom(), vis.left_bottom()].map(|c| xf.screen_to_norm(c));
++ let (u0, u1) = corners.iter().fold((1.0f32, 0.0f32), |(a, b), c| (a.min(c.0), b.max(c.0)));
++ let (v0, v1) = corners.iter().fold((1.0f32, 0.0f32), |(a, b), c| (a.min(c.1), b.max(c.1)));
++ let (vx0, vy0) = ((u0.max(0.0) * dw as f32) as u32, (v0.max(0.0) * dh as f32) as u32);
++ let (vx1, vy1) = (((u1.min(1.0) * dw as f32).ceil() as u32).min(dw), ((v1.min(1.0) * dh as f32).ceil() as u32).min(dh));
++ // Tiles of earlier zooms first, stretched, the nearest scale last (on top):
++ // until the current tiles arrive, a zoom shows them rather than the backdrop.
++ let mut older: Vec<(f32, u32, u32, &TextureHandle)> =
++ view.tiles.iter().filter(|((p, t, _, _), _)| *p == i && *t != tag).map(|((_, t, ox, oy), tex)| (tag_scale(*t), *ox, *oy, tex)).collect();
++ older.sort_by(|a, b| (b.0 / scale).ln().abs().total_cmp(&(a.0 / scale).ln().abs()));
++ for (s, ox, oy, tex) in older {
++ let (ow, oh) = (device_pixels(pw_pt, s) as f32, device_pixels(ph_pt, s) as f32);
++ let [w, h] = tex.size();
++ let (x, y) = (ox as f32, oy as f32);
++ paint_image_suppressed(
++ &xf,
++ painter,
++ tex.id(),
++ x / ow,
++ y / oh,
++ (x + w as f32) / ow,
++ (y + h as f32) / oh,
++ active_drag_suppress,
++ false,
++ );
++ }
++ // Visible tiles, nearest the viewport centre first: each tile costs a full
++ // page interpret, so the workers sharpen what is looked at before the edges.
++ let (vcx, vcy) = ((vx0 + vx1) as f32 / 2.0, (vy0 + vy1) as f32 / 2.0);
++ let mut coords: Vec<(u32, u32)> = Vec::new();
++ for ty in vy0 / ts..=(vy1.saturating_sub(1)) / ts {
++ for tx in vx0 / ts..=(vx1.saturating_sub(1)) / ts {
++ coords.push((tx, ty));
++ }
++ }
++ coords.sort_by(|a, b| {
++ let da = ((a.0 * ts + ts / 2) as f32 - vcx).abs() + ((a.1 * ts + ts / 2) as f32 - vcy).abs();
++ let db = ((b.0 * ts + ts / 2) as f32 - vcx).abs() + ((b.1 * ts + ts / 2) as f32 - vcy).abs();
++ da.total_cmp(&db)
++ });
++ for (tx, ty) in coords {
++ let (x, y) = (tx * ts, ty * ts);
++ let (w, h) = (ts.min(dw.saturating_sub(x)), ts.min(dh.saturating_sub(y)));
++ if w == 0 || h == 0 {
++ continue;
++ }
++ frame.wanted.push((i, scale, tag, Some(Tile { x, y, w, h })));
++ if let Some(tex) = view.tiles.get(&(i, tag, x, y)) {
++ let (fw, fh) = (dw as f32, dh as f32);
++ paint_image_suppressed(
++ &xf.texel_aligned([dw as usize, dh as usize], ppp),
++ painter,
++ tex.id(),
++ x as f32 / fw,
++ y as f32 / fh,
++ (x + w) as f32 / fw,
++ (y + h) as f32 / fh,
++ active_drag_suppress,
++ false,
++ );
++ }
++ }
++}
++
++/// What the page's tools did with the pointer this frame.
++struct PageInput {
++ /// A tool, a field or a comment took the pointer: text selection must leave it alone.
++ consumed: bool,
++ /// Redact or Highlight draws boxes off text.
++ area_tool: bool,
++}
++
++/// Common geometry and pointer state for a tool acting on one page.
++#[derive(Clone, Copy)]
++pub(crate) struct PageToolContext<'a> {
++ pub ui: &'a egui::Ui,
++ pub resp: &'a egui::Response,
++ pub xf: &'a PageXform,
++ pub ptr: &'a PagePointer,
++ pub page: usize,
++ pub doc: &'a pdfcraft_engine::Document,
++ pub info: &'a DocInfo,
++}
++
++/// Comments: tools, selection, moving and resizing come before text selection. Form fields take
++/// clicks first with the Select tool (as Acrobat fills fields in every viewing mode); then
++/// comments; then text selection.
++fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot) -> PageInput {
++ let PageEnv {
++ added,
++ allowed,
++ author,
++ by_line,
++ can_crop,
++ can_fill,
++ can_modify,
++ comments_hidden,
++ custom_stamp,
++ date_text,
++ doc,
++ doc_links,
++ editing_content,
++ form,
++ info,
++ initials,
++ prefs,
++ preparing,
++ redact_mark,
++ signature,
++ table_preview,
++ text_style,
++ tool,
++ units,
++ fill_text_size,
++ fill_text_color,
++ ..
++ } = frame.env;
++ let PageSlot { i, xf, .. } = *slot;
++ let PageFrame { resp, ptr, pointer, .. } = *frame;
++ let cx = PageToolContext { ui, resp, xf: &xf, ptr, page: i, doc, info };
++ let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
++ let (lines, images) =
++ if matches!(tool, QuickTool::AddText | QuickTool::EditText) && can_modify { edit_inventory(view, doc, i) } else { Default::default() };
++ let field_tool = match tool {
++ QuickTool::Field(f) => Some(f),
++ _ => None,
++ };
++ // While a table is being set up, nothing else is drawn or moved.
++ let prepare_allowed = can_modify && table_preview.is_none() && frame.out.open_table.is_none();
++ let hits = PageHits {
++ page: i,
++ xf: &xf,
++ info,
++ tool,
++ pcx: &pcx,
++ form,
++ can_fill,
++ can_modify,
++ editing_content,
++ preparing,
++ field_tool,
++ prepare_allowed,
++ added,
++ lines: &lines,
++ images: &images,
++ clip: ui.clip_rect(),
++ };
++ if let Some(o) = ptr.pressed().filter(|o| xf.rect.contains(*o)) {
++ let at = hits.at(view, o);
++ view.router.press(i, at, tool);
++ }
++ if let QuickTool::Measure(measure_tool) = tool {
++ crate::measure_ui::page_input(ui, resp, doc, view, &pcx, measure_tool, units);
++ }
++
++ // Form fields take clicks first with the Select tool (as Acrobat fills fields in
++ // every viewing mode); then comments; then text selection.
++ if let QuickTool::Stamp(kind) = tool
++ && allowed
++ && let Some(p) = ptr.within(xf.rect)
++ {
++ view.cursor.claim(Layer::ToolArea, egui::CursorIcon::Crosshair);
++ // The tool's own click: nothing drawn over the page here holds the press.
++ if resp.clicked() && view.router.owner(i).is_none() {
++ // Centred on the click, upright as the page is shown.
++ let (vx, vy) = xf.screen_to_view(p);
++ let (w, h) = kind.size();
++ let corners = [(vx as f64 - w / 2.0, vy as f64 - h / 2.0), (vx as f64 + w / 2.0, vy as f64 + h / 2.0)];
++ let u: Vec<[f32; 2]> = corners.iter().map(|(x, y)| info.pages[i].view_to_user(*x as f32, *y as f32)).collect();
++ let rect = [u[0][0].min(u[1][0]) as f64, u[0][1].min(u[1][1]) as f64, u[0][0].max(u[1][0]) as f64, u[0][1].max(u[1][1]) as f64];
++ let by = (kind.group() == pdfcraft_engine::StampGroup::Dynamic).then(|| by_line.clone());
++ let shape = pdfcraft_engine::Shape::Stamp { rect, stamp: kind, by };
++ view.pending_edit = Some(pdfcraft_engine::Edit::AddAnnotation(pdfcraft_engine::NewAnnotation {
++ page: i,
++ style: pdfcraft_engine::Style::default_for(&shape),
++ shape,
++ contents: String::new(),
++ author: author.clone(),
++ }));
++ frame.out.stamp_placed = true;
++ }
++ }
++ if let Some(cs) = custom_stamp.as_ref()
++ && allowed
++ && let Some(p) = ptr.within(xf.rect)
++ {
++ view.cursor.claim(Layer::ToolArea, egui::CursorIcon::Crosshair);
++ // The tool's own click: nothing drawn over the page here holds the press.
++ if resp.clicked() && view.router.owner(i).is_none() {
++ // Centred on the click at its natural size (the engine sizes it).
++ let (vx, vy) = xf.screen_to_view(p);
++ let u = info.pages[i].view_to_user(vx, vy);
++ let (x, y) = (u[0] as f64, u[1] as f64);
++ view.pending_edit = Some(pdfcraft_engine::Edit::AddCustomStamp {
++ page: i,
++ rect: [x, y, x, y],
++ name: cs.name.clone(),
++ file: pdfcraft_engine::MarkFile { name: cs.file.clone(), bytes: cs.data.clone(), page: cs.page },
++ author: author.clone(),
++ });
++ frame.out.stamp_placed = true;
++ }
++ }
++ // Over a mark already placed, the Fill & Sign tool picks it up (comments, below).
++ if let QuickTool::Fill(ft) = tool
++ && allowed
++ && !comments::fill_grabs(&pcx, view)
++ {
++ match crate::fill_sign::page_input(
++ cx,
++ crate::fill_sign::PageInput {
++ tool: ft,
++ text_size: fill_text_size,
++ text_color: fill_text_color,
++ signature: signature.as_ref(),
++ initials: initials.as_ref(),
++ preview: frame.signature_preview,
++ author,
++ date_text,
++ },
++ view,
++ ) {
++ Some(crate::fill_sign::FillAction::Edit(e)) => view.pending_edit = Some(*e),
++ Some(crate::fill_sign::FillAction::Signature(e)) => {
++ view.pending_edit = Some(*e);
++ view.fill_signature_page = Some(i);
++ }
++ Some(crate::fill_sign::FillAction::CreateSignature) => frame.out.open_signature = true,
++ Some(crate::fill_sign::FillAction::CreateInitials) => frame.out.open_initials = true,
++ Some(crate::fill_sign::FillAction::Refused(why)) => frame.out.refused = Some(why),
++ None => {}
++ }
++ }
++ if matches!(tool, QuickTool::SignArea { .. }) {
++ crate::sign_ui::page_input(ui, resp, &xf, ptr, i, info, view);
++ }
++ if matches!(tool, QuickTool::MarqueeZoom | QuickTool::Snapshot) {
++ crate::zoom_snap::page_input(ui, resp, &xf, ptr, i, view);
++ }
++ if tool == QuickTool::EnhanceCamera {
++ crate::camera_ui::page_input(resp, &xf, ptr, i, view, can_modify);
++ }
++ if tool == QuickTool::Crop
++ && crate::crop::page_input(ui, crate::crop::CropPageInput { resp, xf: &xf, ptr, page: i, info, view, allowed: can_crop })
++ {
++ view.current = i;
++ frame.out.open_boxes = true;
++ }
++ let on_field = if preparing {
++ let o = crate::prepare::page_input(cx, form, field_tool, prepare_allowed, view);
++ frame.out.field_props |= o.properties;
++ frame.out.field_placed |= o.placed;
++ frame.out.open_table = frame.out.open_table.or(o.table);
++ o.consumed || field_tool.is_some()
++ } else {
++ tool == QuickTool::Select && crate::forms_ui::page_input(cx, form, can_fill, view)
++ };
++ // Redact draws boxes off text; so does Highlight (an area highlight, as in Acrobat).
++ let area_tool = tool == QuickTool::Redact && can_modify || tool == QuickTool::Comment(comments::CommentTool::Highlight) && allowed;
++ let boxing = area_tool && {
++ let text = view.page_text(i);
++ let over_text = |p: Pos2| {
++ let (vx, vy) = xf.screen_to_view(p);
++ text.as_ref().is_some_and(|t| t.glyphs.iter().any(|g| vx >= g.rect[0] && vx <= g.rect[2] && vy >= g.rect[1] && vy <= g.rect[3]))
++ };
++ crate::redact_ui::page_input(cx, over_text, view)
++ };
++ let on_objects = tool == QuickTool::EditText && can_modify && crate::object_ui::page_input(cx, view);
++ // Add text acts on the page's own paragraphs over them, and on the one its press was on until
++ // the button is up: a drag that leaves the box still ends, and no new box takes it over.
++ let over_source = tool == QuickTool::AddText
++ && (matches!(view.router.owner(i), Some(crate::page_router::Owner::Paragraph { .. }))
++ || pointer.is_some_and(|p| {
++ !crate::content_ui::hits_added(&xf, info, i, added, p) && crate::edit_text_ui::over_paragraph(&xf, info, i, &lines, p)
++ }));
++ let source_blocked = over_source && view.content.hold_blocked();
++ if over_source && resp.clicked() && !source_blocked {
++ if let Some(edit) = view.content.finish_draft(added) {
++ view.pending_edit = Some(edit);
++ }
++ view.content.selected = None;
++ }
++ let on_content = !on_objects && editing_content && can_modify && {
++ source_blocked
++ || crate::content_ui::page_input(
++ cx,
++ added,
++ tool == QuickTool::AddText && !over_source,
++ matches!(tool, QuickTool::AddText | QuickTool::EditText),
++ text_style,
++ view,
++ )
++ || tool == QuickTool::AddText && !over_source
++ };
++ // Added content owns its gesture; its source text must not queue a second edit.
++ // Edit text & images boxes every paragraph and image whenever the tool is on. Only the
++ // input waits while selected objects or added content own the pointer, or while page
++ // input is blocked (another tab, a dialog, the window unfocused): the boxes stay drawn
++ // instead of vanishing until the pointer comes back.
++ let others_own_pointer = on_objects || on_content;
++ let on_edit_text = (tool == QuickTool::EditText || over_source) && can_modify && {
++ // Editable text takes precedence over background/overlapping artwork. Both
++ // inventories are painted every frame, even when text owns the pointer.
++ let text_used = crate::edit_text_ui::page_input(cx, &lines, view, !others_own_pointer);
++ let image_cx = crate::edit_text_ui::ImageCx {
++ ui,
++ resp,
++ xf: &xf,
++ ptr,
++ page: i,
++ info,
++ allow_interaction: !others_own_pointer && (matches!(view.router.owner(i), Some(crate::page_router::Owner::Picture { .. })) || !text_used),
++ };
++ let image_used = crate::edit_text_ui::image_input(&image_cx, &images, view, &mut frame.out.image_action);
++ !others_own_pointer && (text_used || image_used)
++ };
++ let on_link = tool == QuickTool::Link && can_modify && crate::link_ui::page_input(cx, doc_links, view);
++ if tool == QuickTool::Select
++ && resp.secondary_clicked()
++ && can_modify
++ && let Some(p) = pointer.filter(|p| xf.rect.contains(*p))
++ && let Some(l) = doc.links().iter().find(|l| l.page == i && crate::link_ui::screen_rect(&xf, info, i, l.rect).expand(2.0).contains(p))
++ {
++ view.links.context = Some((i, l.index));
++ }
++ let consumed = on_objects || on_edit_text || on_link || on_content || boxing || on_field || comments::page_input(ui, resp, &pcx, view);
++ // The cursor and the tip from what lies under the pointer, after the tools claimed theirs (the
++ // last claim on a layer stands).
++ if frame.env.unobstructed
++ && let Some(p) = pointer
++ {
++ for h in hits.at(view, p) {
++ view.cursor.claim_with_tip(h.layer, h.icon, h.tip);
++ }
++ }
++ PageInput { consumed, area_tool }
++}
++
++/// Edit text's paragraphs and pictures on page `i`, kept while the document is unchanged.
++fn edit_inventory(
++ view: &mut DocView,
++ doc: &pdfcraft_engine::Document,
++ i: usize,
++) -> (Vec<pdfcraft_engine::TextBlock>, Vec<pdfcraft_engine::PageImage>) {
++ let generation = doc.edit_generation();
++ let lines = match view.edit_lines.get(&i) {
++ Some((g, l)) if *g == generation => l.clone(),
++ _ => {
++ let l = doc.text_blocks(i);
++ view.edit_lines.insert(i, (generation, l.clone()));
++ l
++ }
++ };
++ let images = match view.edit_images.get(&i) {
++ Some((g, l)) if *g == generation => l.clone(),
++ _ => {
++ let l = doc.page_images(i);
++ view.edit_images.insert(i, (generation, l.clone()));
++ l
++ }
++ };
++ (lines, images)
++}
++
++/// What the page modules have under a point on one page, for the page router.
++struct PageHits<'a> {
++ page: usize,
++ xf: &'a PageXform,
++ info: &'a DocInfo,
++ tool: QuickTool,
++ pcx: &'a comments::PageCx<'a>,
++ form: &'a [pdfcraft_engine::FormField],
++ can_fill: bool,
++ can_modify: bool,
++ /// Edit a PDF is open: added text and pictures can be picked up.
++ editing_content: bool,
++ preparing: bool,
++ field_tool: Option<crate::prepare::FieldTool>,
++ /// Prepare may draw and move fields (not while a table is being set up).
++ prepare_allowed: bool,
++ added: &'a [pdfcraft_engine::Added],
++ lines: &'a [pdfcraft_engine::TextBlock],
++ images: &'a [pdfcraft_engine::PageImage],
++ /// The pages' visible area (the picture's rotate knob stays inside it).
++ clip: Rect,
++}
++
++impl PageHits<'_> {
++ /// The hits at `p`, bottom up: links, comments; Edit text's pictures under its paragraphs (text
++ /// on top of artwork takes the press), added content, the objects selected together and the
++ /// open paragraph's frame, the selected picture's grips over them all; then form fields
++ /// (Prepare's, being moved, over the comments; filled ones under added content and the
++ /// comments that move).
++ fn at(&self, view: &DocView, p: Pos2) -> Vec<crate::page_router::Hit> {
++ let PageHits {
++ page,
++ xf,
++ info,
++ tool,
++ pcx,
++ form,
++ can_fill,
++ can_modify,
++ editing_content,
++ preparing,
++ field_tool,
++ prepare_allowed,
++ added,
++ lines,
++ images,
++ clip,
++ } = *self;
++ let fields = if preparing {
++ crate::prepare::hit(xf, info, page, form, field_tool, prepare_allowed, &view.prepare, p)
++ } else if tool == QuickTool::Select {
++ crate::forms_ui::hit(xf, info, page, form, can_fill, p)
++ } else {
++ None
++ };
++ // With Add text, only the paragraphs and pictures not under added content.
++ let editing =
++ can_modify && (tool == QuickTool::EditText || tool == QuickTool::AddText && !crate::content_ui::hits_added(xf, info, page, added, p));
++ let (pictures, paragraphs, objects, grips) = if editing {
++ let open = view.line_editor.as_ref().filter(|e| e.page == page);
++ let frame = open
++ .filter(|e| !e.has_changes())
++ .and_then(|e| lines.get(e.block))
++ .map(|l| xf.user_rect(info, page, l.rect.map(|v| v as f32)).expand(2.0));
++ // Add text acts on the page's paragraphs only in their boxes, edges included (it
++ // finishes the box being typed there, `over_source`): not on pictures, nor beside a
++ // paragraph, where a click starts a new box.
++ let edit_text = tool == QuickTool::EditText;
++ let on_paragraphs = edit_text || crate::edit_text_ui::over_paragraph(xf, info, page, lines, p);
++ (
++ edit_text.then(|| crate::edit_text_ui::picture_hit(xf, page, info, images, view, p)).flatten(),
++ on_paragraphs.then(|| crate::edit_text_ui::hit(xf, info, page, lines, open.map(|e| e.block), p)).flatten(),
++ edit_text.then(|| crate::object_ui::hit(view, xf, info, page, frame, p)).flatten(),
++ edit_text.then(|| crate::edit_text_ui::picture_handle_hit(clip, xf, page, info, images, view, p)).flatten(),
++ )
++ } else {
++ (None, None, None, None)
++ };
++ // Added text and pictures, over the page's own paragraphs and under the objects selected
++ // together.
++ let picks_up = matches!(tool, QuickTool::Select | QuickTool::AddText | QuickTool::EditText);
++ let added = (editing_content && can_modify && picks_up).then(|| crate::content_ui::hit(xf, info, page, added, view, p)).flatten();
++ [link_hit(info, xf, page, p), comments::hit(pcx, view, p), pictures, paragraphs, added, objects, grips, fields]
++ .into_iter()
++ .flatten()
++ .collect()
++ }
++}
++
++/// Text layer: find matches, selection, I-beam and drag-to-select.
++fn text_layer(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot, input: &PageInput) {
++ let PageEnv { info, selects_text, tool, .. } = frame.env;
++ let PageSlot { i, r, xf } = *slot;
++ let PageFrame { resp, ptr, pointer, .. } = *frame;
++ if let Some(text) = view.texts.get(&i).cloned() {
++ paint_text_marks(ui, view, &text, i, xf, resp.dragged());
++ if selects_text
++ && !input.consumed
++ && let Some(p) = pointer.filter(|p| r.contains(*p))
++ {
++ text_pointer_input(ui, view, &text, TextPointer { page: i, xf, rect: r, info, tool, resp, ptr: ptr.clone(), pointer: p });
++ }
++ }
++}
++
++fn paint_text_marks(ui: &egui::Ui, view: &DocView, text: &PageText, page: usize, xf: PageXform, dragging: bool) {
++ let painter = ui.painter();
++ let to_screen = |g: [f32; 4]| xf.view_rect(g);
++ if let Some(find) = &view.find {
++ for (index, (match_page, range)) in find.matches.iter().enumerate() {
++ if *match_page != page {
++ continue;
++ }
++ let fill = if find.current == Some(index) {
++ Color32::from_rgba_unmultiplied(255, 140, 0, 110)
++ } else {
++ Color32::from_rgba_unmultiplied(255, 214, 0, 90)
++ };
++ for line_rect in text.line_rects(range.clone()) {
++ painter.rect_filled(to_screen(line_rect).expand(1.0), CornerRadius::same(2), fill);
++ }
++ }
++ }
++ if view.all_text {
++ for line_rect in text.line_rects(0..text.glyphs.len()) {
++ painter.rect_filled(to_screen(line_rect), CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x3A, 0x7B, 0xF0, 70));
++ }
++ }
++ if let Some(selection) = view.selection.filter(|selection| selection.page == page) {
++ for line_rect in selection.rects(text) {
++ painter.rect_filled(to_screen(line_rect), CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x3A, 0x7B, 0xF0, 70));
++ }
++ if let Span::Column { rect } = selection.span
++ && dragging
++ {
++ let outline = Stroke::new(1.0, Color32::from_rgb(0x3A, 0x7B, 0xF0));
++ painter.rect_stroke(to_screen(rect), CornerRadius::ZERO, outline, egui::StrokeKind::Middle);
++ }
++ }
++}
++
++struct TextPointer<'a> {
++ page: usize,
++ xf: PageXform,
++ rect: Rect,
++ info: &'a DocInfo,
++ tool: QuickTool,
++ resp: &'a egui::Response,
++ ptr: PagePointer,
++ pointer: Pos2,
++}
++
++struct TextGesture<'a> {
++ page: usize,
++ xf: PageXform,
++ rect: Rect,
++ tool: QuickTool,
++ ptr: PagePointer,
++ resp: &'a egui::Response,
++ pointer: Pos2,
++ vx: f32,
++ vy: f32,
++ over_text: bool,
++ over_link: bool,
++ column: bool,
++}
++
++fn text_pointer_input(ui: &mut egui::Ui, view: &mut DocView, text: &PageText, pointer: TextPointer<'_>) {
++ let TextPointer { page, xf, rect, info, tool, resp, ptr, pointer: screen_pointer } = pointer;
++ let (vx, vy) = xf.screen_to_view(screen_pointer);
++ let over_text = text.glyphs.iter().any(|glyph| vx >= glyph.rect[0] && vx <= glyph.rect[2] && vy >= glyph.rect[1] && vy <= glyph.rect[3]);
++ let over_link = info.links.iter().any(|link| link.page == page && xf.user_rect(info, page, link.rect).contains(screen_pointer));
++ let column = tool == QuickTool::ColumnSelect || ui.input(|input| input.modifiers.alt);
++ let gesture = TextGesture { page, xf, rect, tool, ptr, resp, pointer: screen_pointer, vx, vy, over_text, over_link, column };
++ update_text_cursor(view, &gesture);
++ update_selection_on_press(ui, view, text, &gesture);
++ update_dragged_selection(view, text, &gesture);
++ finish_text_pointer_gesture(ui, view, text, &gesture);
++}
++
++fn update_text_cursor(view: &mut DocView, gesture: &TextGesture<'_>) {
++ // A press on a link selects nothing (it is the link's), so no cursor here says it would.
++ if gesture.column && !gesture.over_link {
++ view.cursor.claim(Layer::Text, egui::CursorIcon::Crosshair);
++ } else if gesture.over_text && !gesture.over_link {
++ view.cursor.claim(Layer::Text, egui::CursorIcon::Text);
++ }
++}
++
++fn update_selection_on_press(ui: &egui::Ui, view: &mut DocView, text: &PageText, gesture: &TextGesture<'_>) {
++ // A press held by what keeps its drag (a link, a comment that moves) selects no text; one on
++ // text markup lets its drag select the text underneath.
++ let press_here = gesture.ptr.origin.is_some_and(|origin| gesture.rect.contains(origin));
++ if gesture.ptr.pressed().is_some() && press_here && !view.router.keeps_drag() {
++ let now = ui.input(|input| input.time);
++ let (max_delay, max_dist) = ui.ctx().options(|options| (options.input_options.max_double_click_delay, options.input_options.max_click_dist));
++ let consecutive = now - view.last_press_time <= max_delay && view.last_press_pos.is_some_and(|pos| pos.distance(gesture.pointer) <= max_dist);
++ view.clicks = if consecutive { view.clicks.saturating_add(1) } else { 1 };
++ view.last_press_time = now;
++ view.last_press_pos = Some(gesture.pointer);
++ let clicks = selection_click_count(view.clicks, gesture.tool);
++ if clicks >= 2
++ && gesture.over_text
++ && let Some((first, last)) = text.nearest(gesture.vx, gesture.vy).and_then(|anchor| click_unit(text, clicks, anchor))
++ {
++ view.selection = Some(Selection::run(gesture.page, first, last));
++ }
++ }
++ if gesture.resp.drag_started() && press_here && !view.router.keeps_drag() {
++ view.actions_dismissed = None;
++ view.selection_drag = true;
++ let origin = gesture.ptr.origin.unwrap_or(gesture.pointer);
++ let (ox, oy) = gesture.xf.screen_to_view(origin);
++ if view.clicks < 2 {
++ view.selection = if gesture.column {
++ Some(Selection { page: gesture.page, span: Span::Column { rect: [ox, oy, gesture.vx, gesture.vy] } })
++ } else {
++ text.nearest(ox, oy).map(|anchor| Selection::run(gesture.page, anchor, anchor))
++ };
++ }
++ }
++}
++
++fn selection_click_count(clicks: u32, tool: QuickTool) -> u32 {
++ if clicks > 2 && tool != QuickTool::Select { 1 } else { clicks }
++}
++
++fn update_dragged_selection(view: &mut DocView, text: &PageText, gesture: &TextGesture<'_>) {
++ let clicks = selection_click_count(view.clicks, gesture.tool);
++ if gesture.resp.dragged()
++ && let Some(selection) = view.selection.as_mut().filter(|selection| selection.page == gesture.page)
++ {
++ match &mut selection.span {
++ Span::Run { anchor, head } if clicks >= 2 => {
++ if let Some(hit) = text.nearest(gesture.vx, gesture.vy)
++ && let Some((first, last)) = click_unit(text, clicks, *anchor)
++ {
++ let (to_first, to_last) = click_unit(text, clicks, hit).unwrap_or((hit, hit));
++ (*anchor, *head) = if to_first < first { (last, to_first) } else { (first, to_last.max(last)) };
++ }
++ }
++ Span::Run { head, .. } => {
++ if let Some(hit) = text.nearest(gesture.vx, gesture.vy) {
++ *head = hit;
++ }
++ }
++ Span::Column { rect } => {
++ rect[2] = gesture.vx;
++ rect[3] = gesture.vy;
++ }
++ }
++ }
++ if gesture.resp.drag_stopped()
++ && view.selection.is_some_and(|selection| {
++ selection.page == gesture.page && matches!(selection.span, Span::Column { .. }) && selection.text(text).is_empty()
++ })
++ {
++ view.selection = None;
++ }
++}
++
++fn finish_text_pointer_gesture(ui: &egui::Ui, view: &mut DocView, text: &PageText, gesture: &TextGesture<'_>) {
++ let release_clicks = if gesture.resp.triple_clicked() {
++ view.clicks.max(2).saturating_add(1)
++ } else if gesture.resp.double_clicked() {
++ view.clicks.max(2)
++ } else {
++ 0
++ };
++ if release_clicks > 0 {
++ view.clicks = release_clicks;
++ }
++ let clicks = selection_click_count(view.clicks, gesture.tool);
++ if release_clicks > 0
++ && clicks >= 2
++ && gesture.over_text
++ && view.selection.is_none()
++ && let Some((first, last)) = text.nearest(gesture.vx, gesture.vy).and_then(|anchor| click_unit(text, clicks, anchor))
++ {
++ view.selection = Some(Selection::run(gesture.page, first, last));
++ } else if gesture.resp.clicked() && view.clicks < 2 && !view.router.keeps_drag() {
++ let shift = ui.input(|input| input.modifiers.shift);
++ match (
++ view.selection.as_mut().filter(|selection| shift && selection.page == gesture.page).map(|selection| &mut selection.span),
++ text.nearest(gesture.vx, gesture.vy),
++ ) {
++ (Some(Span::Run { head, .. }), Some(hit)) => *head = hit,
++ _ => view.selection = None,
++ }
++ }
++}
++
++/// Everything drawn over the page after its text: pictures, comments, content, links, fields, marks
++/// and badges.
++fn page_overlays(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, slot: &PageSlot, input: &PageInput) {
++ let PageEnv {
++ added,
++ allowed,
++ calc_order,
++ can_modify,
++ comments_hidden,
++ doc,
++ doc_links,
++ editing_content,
++ form,
++ generation,
++ info,
++ prefs,
++ preparing,
++ redact_mark,
++ table_preview,
++ tool,
++ unobstructed,
++ ..
++ } = frame.env;
++ let PageSlot { i, xf, .. } = *slot;
++ let PageFrame { resp, ptr, pointer, .. } = *frame;
++ let painter = ui.painter();
++ let tool_cx = PageToolContext { ui, resp, xf: &xf, ptr, page: i, doc, info };
++ let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
++ if tool == QuickTool::Select && !input.consumed {
++ crate::image_pick::page_input(resp, &xf, i, info, doc, pointer, view);
++ }
++ crate::image_pick::drop_stale(doc, view);
++ crate::image_pick::paint(painter, &xf, i, info, view);
++ // A cancelled press ends no selection: no markup or redaction is made from it.
++ if !view.press_cancelled {
++ comments::page_after_text(resp, &pcx, view);
++ if tool == QuickTool::Redact && can_modify {
++ crate::redact_ui::after_text(resp, i, info, view);
++ }
++ }
++ if input.area_tool {
++ crate::redact_ui::paint(painter, i, view, pointer);
++ }
++ comments::paint_page(ui, painter, &pcx, view);
++ if editing_content {
++ crate::content_ui::paint_page(painter, &xf, i, info, added, view, pointer);
++ }
++ if tool == QuickTool::EditText {
++ crate::object_ui::paint(painter, &xf, i, info, &view.objects);
++ }
++ if tool == QuickTool::EnhanceCamera {
++ crate::camera_ui::paint(painter, &xf, i, view);
++ }
++ if tool == QuickTool::Link {
++ crate::link_ui::paint(painter, &xf, i, info, doc_links, view, pointer);
++ }
++ if preparing {
++ crate::prepare::paint_page(tool_cx, form, view);
++ if let Some(preview) = &table_preview {
++ crate::field_table_ui::paint_preview(painter, &xf, i, preview);
++ }
++ if calc_order {
++ let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
++ if let Some(field) = crate::calc_order_ui::paint_page(tool_cx, form, &view.calc_order, generation, selected) {
++ frame.calc_hover = Some(field);
++ }
++ }
++ } else {
++ crate::forms_ui::paint_page(painter, &xf, i, info, form, view, pointer);
++ }
++
++ // Form-field highlight (Acrobat's "Highlight existing fields"); required fields get a
++ // red border. Radio buttons and round XFA check boxes are round, and so is theirs (as
++ // in Acrobat). Only what can be filled is marked: a read-only widget (its field's, or a
++ // radio button's own) is left as it is drawn.
++ paint_field_highlights(painter, &xf, info, form.as_slice(), i, view.highlight_fields);
++ // Links: the hand and the link's target, which count only for the tools that follow or
++ // edit links (`Layer::reached_by`). Tips draw above the palette, so none shows while it is
++ // open, even beside it. A click follows the link its press was on, released over it: one that
++ // slips off the link first follows nothing, as a button's does.
++ if unobstructed
++ && resp.clicked()
++ && tool == QuickTool::Select
++ && !input.consumed
++ && let Some(owner @ crate::page_router::Owner::Link { index }) = view.router.owner(i)
++ && pointer.and_then(|p| link_hit(info, &xf, i, p)).is_some_and(|h| h.owner == owner)
++ && let Some(l) = info.links.get(index)
++ {
++ frame.out.clicked_link = Some(l.target.clone());
++ }
++ // Comments say what they are with any tool, beside its cursor, as Acrobat's do.
++ comments::describe(&pcx, view);
++ for (mp, mr, mc) in &view.compare_marks {
++ if *mp == i {
++ let sr = xf.user_rect(info, i, *mr).expand(1.5);
++ painter.rect_filled(sr, CornerRadius::same(2), mc.gamma_multiply(0.28));
++ }
++ }
++ if let Some((fp, fr, t0)) = view.flash
++ && fp == i
++ {
++ let now = ui.input(|inp| inp.time);
++ let t0 = if t0 == 0.0 { now } else { t0 };
++ view.flash = Some((fp, fr, t0));
++ let age = (now - t0) as f32;
++ if age < 1.6 {
++ let a = ((1.6 - age) / 1.6 * 255.0) as u8;
++ let sr = xf.user_rect(info, i, fr).expand(4.0);
++ painter.rect_stroke(
++ sr,
++ CornerRadius::same(3),
++ Stroke::new(2.5, Color32::from_rgba_unmultiplied(0x1B, 0x63, 0xE0, a)),
++ egui::StrokeKind::Outside,
++ );
++ ui.ctx().request_repaint();
++ } else {
++ view.flash = None;
++ }
++ }
++ // Validation is viewer chrome over the signer's original artwork, which
++ // may still say "Signature Not Verified". These badges never enter document
++ // streams, image/text exports, vector printing or Print as Image.
++ for badge in signature_badges(&doc.signatures, i) {
++ let [x0, y0, x1, y1] = badge.rect;
++ let sr = xf.user_rect(info, i, [x0 as f32, y0 as f32, x1 as f32, y1 as f32]);
++ let (icon, color) = crate::sign_ui::signature_status_style(badge.status);
++ painter.rect_stroke(sr, CornerRadius::same(2), Stroke::new(1.5, color), egui::StrokeKind::Outside);
++ // A widget too small for the mark keeps its outline: the verdict still shows.
++ if sr.width() < 4.0 || sr.height() < 4.0 {
++ continue;
++ }
++ // Big enough to read at a glance, small enough to leave the signer's own
++ // text visible around it.
++ let side = (sr.width().min(sr.height()) * 0.8).clamp(24.0, 192.0);
++ let mark = Rect::from_center_size(sr.center(), vec2(side, side));
++ icons::paint(ui, mark, icon, side * 0.64, color);
++ // Overlapping widgets paint in order, so the topmost badge's verdict is the tip, with
++ // any tool; a link's or a comment's tip wins over any badge.
++ if pointer.is_some_and(|p| mark.expand(2.0).contains(p)) {
++ view.cursor.describe(Subject::SignatureBadge, badge.summary.to_string());
++ }
++ }
++}
++
++fn paint_field_highlights(painter: &egui::Painter, xf: &PageXform, info: &DocInfo, form: &[pdfcraft_engine::FormField], page: usize, enabled: bool) {
++ if !enabled {
++ return;
++ }
++ for field in form.iter() {
++ let required = field.has(pdfcraft_engine::field_flags::REQUIRED);
++ for widget in field.widgets.iter().filter(|widget| widget.page == Some(page) && !widget.hidden && !widget.read_only) {
++ let rect = widget.rect;
++ let screen_rect = xf.user_rect(info, page, [rect[0] as f32, rect[1] as f32, rect[2] as f32, rect[3] as f32]);
++ let tint = Color32::from_rgba_unmultiplied(0x6E, 0x8E, 0xF5, 48);
++ let (width, color) = if required { (2.0, Color32::from_rgb(0xE3, 0x22, 0x22)) } else { (1.0, Color32::from_rgb(0x6E, 0x8E, 0xF5)) };
++ if widget.round {
++ let radius = screen_rect.width().min(screen_rect.height()) / 2.0;
++ painter.circle_filled(screen_rect.center(), radius, tint);
++ painter.circle_stroke(screen_rect.center(), radius - width / 2.0, Stroke::new(width, color));
++ } else {
++ painter.rect_filled(screen_rect, CornerRadius::same(1), tint);
++ painter.rect_stroke(screen_rect, CornerRadius::same(1), Stroke::new(width, color), egui::StrokeKind::Inside);
++ }
++ }
++ }
++}
++
++fn draw_page(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, i: usize) {
++ let PageEnv { allowed, comments_hidden, doc, editing_content, info, ppp, prefs, rects, redact_mark, scale, t, tool, .. } = frame.env;
++ let PageFrame { ptr, origin, visible, pressed, pointer, .. } = *frame;
++ let r = snap_to_pixels(rects[i].translate(origin.to_vec2()), ppp);
++ if !r.intersects(visible.expand(400.0)) {
++ return;
++ }
++ if pressed && frame.pressed_page.is_none() && pointer.is_some_and(|p| r.contains(p)) {
++ frame.pressed_page = Some(i);
++ }
++ if r.intersects(visible) {
++ frame.visible_now.push(i);
++ view.screen_rects.push((i, r));
++ view.screen_xforms.push((i, PageXform { rect: r, rot: view.rotation, pw: info.pages[i].width.max(1.0), ph: info.pages[i].height.max(1.0) }));
++ }
++ let overlap = r.intersect(visible).height();
++ if i == view.current {
++ frame.current_overlap = overlap;
++ frame.current_height = r.height();
++ }
++ // Pages shown equally (two rows wholly on screen) differ only by rounding: the
++ // topmost one counts.
++ if overlap > frame.best_overlap + PAGE_TIE {
++ frame.best_overlap = overlap;
++ frame.current = i;
++ }
++ let xf = PageXform { rect: r, rot: view.rotation, pw: info.pages[i].width.max(1.0), ph: info.pages[i].height.max(1.0) };
++ paint_page_surface(ui, view, frame, &PageSlot { i, r, xf });
++ let painter = ui.painter();
++ view.text_background.paint(painter, &xf, i, view.line_editor.as_ref(), view.content.draft.as_ref());
++ if editing_content {
++ view.move_background.paint(painter, &xf, i, info, &view.content, pointer);
++ }
++ if view.show_page_gaps {
++ painter.rect_stroke(r, CornerRadius::ZERO, Stroke::new(0.5, t.border.gamma_multiply(0.8)), egui::StrokeKind::Outside);
++ }
++
++ let slot = PageSlot { i, r, xf };
++ // A cancelled press takes nothing until the button is up.
++ let input = if view.press_cancelled { PageInput { consumed: true, area_tool: false } } else { page_tool_input(ui, view, frame, &slot) };
++ let painter = ui.painter();
++ let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
++ let preview_target = (matches!(tool, QuickTool::Select | QuickTool::Fill(_)) && !comments_hidden).then_some(view.comments.selected).flatten();
++ view.signature_drag.prepare(ui.ctx(), doc, preview_target, scale);
++ view.signature_drag.paint(painter, &pcx, &view.comments, view.pending_edit.as_ref());
++ // Any other comment being moved: drawn where it goes, over the page that still shows it
++ // where it was.
++ let stale = view.signature_drag.moving_page().is_some_and(|p| view.page_out_of_date(p));
++ view.signature_drag.prepare_move(ui.ctx(), doc, &view.comments, view.pending_edit.as_ref(), stale, scale);
++ view.signature_drag.paint_move(painter, &pcx, &view.comments, view.pending_edit.as_ref());
++
++ // Any press on the page ends Select All of the document: text read or not, any tool,
++ // on a link too.
++ // A press that lands on something drawn over the page (a menu, a dialog) is not one.
++ if view.all_text && frame.pressed_page == Some(i) {
++ view.all_text = false;
++ }
++ text_layer(ui, view, frame, &slot, &input);
++ page_overlays(ui, view, frame, &slot, &input);
++}
++
++/// The link under `p` on `page`: the hand, and where a click goes. Only the Select tool follows
++/// links and the Link tool edits them (`Layer::reached_by`).
++fn link_hit(info: &DocInfo, xf: &PageXform, page: usize, p: Pos2) -> Option<crate::page_router::Hit> {
++ let (index, l) = info.links.iter().enumerate().rfind(|(_, l)| l.page == page && xf.user_rect(info, page, l.rect).contains(p))?;
++ let label = match &l.target {
++ LinkTarget::Page(n, _) => crate::i18n::fmt(tl!("Go to page {p}"), &[("p", info.pages.get(*n).map(|p| p.label.as_str()).unwrap_or("?"))]),
++ LinkTarget::Uri(u) => u.clone(),
++ LinkTarget::SetLayers { .. } => tl!("Set layer visibility").to_string(),
++ LinkTarget::RemoteGoTo { file, .. } => file.clone(),
++ LinkTarget::Other(s) => crate::i18n::fmt(tl!("{s} action"), &[("s", s)]),
++ };
++ let owner = crate::page_router::Owner::Link { index };
++ Some(crate::page_router::Hit { tip: Some(label), ..crate::page_router::Hit::new(owner, Layer::Link, egui::CursorIcon::PointingHand) })
++}
++
++/// Escape while the pages hold a press abandons the gesture it drives, whichever tool's: nothing is
++/// placed, drawn, moved, resized or opened, and the press is ignored until the button is up. The
++/// frame of the release still belongs to the cancelled press.
++fn cancel_on_escape(ctx: &egui::Context, view: &mut DocView, ptr: &PagePointer) {
++ // Over once the button is up and its release seen, or when a new press comes (its release and
++ // the next press can share a frame).
++ let new_press = matches!(ptr.edges.last(), Some(crate::page_pointer::Edge::Press(_)));
++ if view.press_cancelled && (new_press || !ptr.any_down && ptr.edges.is_empty()) {
++ view.press_cancelled = false;
++ }
++ if !view.press_cancelled && ptr.origin.is_some() && ptr.primary_down && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))
++ {
++ // A text selection being dragged goes with its drag: the markup, redaction or Replace
++ // Text that its end would make is not made.
++ if view.selection_drag {
++ view.clear_selection();
++ }
++ view.cancel_pointer_gestures();
++ view.press_cancelled = true;
++ }
++}
++
++#[allow(clippy::cognitive_complexity)]
++fn draw_pages(
++ ui: &mut egui::Ui,
++ viewport: Rect,
++ view: &mut DocView,
++ env: &PageEnv<'_>,
++ out: &mut PageOut,
++ signature_preview: &mut Option<(crate::fill_sign::SavedSig, egui::TextureHandle)>,
++) -> PageRenders {
++ let PageEnv {
++ allowed,
++ auto_delta,
++ avail,
++ boost_extra,
++ calc_order,
++ can_copy,
++ can_modify,
++ content_h,
++ content_w,
++ doc,
++ edge_delta,
++ form,
++ generation,
++ glide,
++ hand,
++ info,
++ key_scroll,
++ middle_gesture,
++ modal,
++ navigation_enabled,
++ precise_scroll,
++ prefs,
++ preparing,
++ selection_wheel,
++ ppp,
++ rects,
++ single_paging,
++ tool,
++ unobstructed,
++ visible_pages,
++ y_shift,
++ ..
++ } = *env;
++ // egui's drag responses accept every pointer button. Keep a wheel gesture from also
++ // selecting text, drawing a mark, or panning with the Hand tool.
++ if middle_gesture {
++ let opacity = ui.opacity();
++ ui.disable();
++ // Only interactions pause; the document must keep its original colours.
++ ui.set_opacity(opacity);
++ }
++ // Middle-button auto-scroll, the keyboard, precise touchpad input (#759) and its glide
++ // (#531), accelerated wheel surplus (#1072), and a drawing's edge scroll; positive y
++ // moves the content down. All scroll without easing, unlike egui's wheel default.
++ // ScrollArea ignores wheel input during a widget drag. Defer the wheel contribution
++ // until after the text widgets when a selection owns that drag.
++ let wheel_delta = if selection_wheel { Vec2::ZERO } else { precise_scroll + boost_extra };
++ let delta = auto_delta + edge_delta - vec2(0.0, key_scroll) + wheel_delta + glide;
++ if delta != Vec2::ZERO && delta.is_finite() {
++ ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
++ }
++ let (resp_rect, resp) = ui.allocate_exact_size(vec2(content_w, content_h), Sense::click_and_drag());
++ view.page_widget = Some(resp.id);
++ // A drag lasts only while egui reports it, so the mark can't outlive one.
++ if !resp.dragged_by(egui::PointerButton::Primary) {
++ view.selection_drag = false;
++ }
++ // The Hand tool pans by this widget's drag, before the pages are laid out (above).
++ if hand {
++ if resp.dragged() {
++ view.cursor.claim(Layer::Drag, egui::CursorIcon::Grabbing);
++ } else if resp.hovered() {
++ view.cursor.claim(Layer::ToolArea, egui::CursorIcon::Grab);
++ }
++ }
++ let origin = resp_rect.min - vec2(0.0, y_shift);
++ let visible = viewport.translate(resp_rect.min.to_vec2());
++ view.screen_rects.clear();
++ view.screen_xforms.clear();
++ // The pointer as the pages see it: nothing over them (a dialog from the frame it opens),
++ // or a press they hold. The page modules read it rather than egui's raw pointer state.
++ let ptr = PagePointer::read(ui, &resp, modal);
++ cancel_on_escape(ui.ctx(), view, &ptr);
++ view.router.settle(ptr.any_down, !ptr.edges.is_empty());
++ // A new press is no longer the last one's, wherever it lands (the page it is on gives it
++ // its owner; in the gap between pages it has none).
++ if ptr.pressed().is_some() {
++ view.router.cancel();
++ // The page widget is not focusable. Explicitly return keyboard ownership from Pages
++ // when the user presses the document, without stealing focus through a modal.
++ if let Some(id) = view.pages_focus_id {
++ ui.memory_mut(|m| m.surrender_focus(id));
++ }
++ }
++ let pointer = ptr.pos;
++ // A press on a page makes it the current one (as in Acrobat), so a page command with
++ // no selection (Rotate, Delete) acts on the page the reader pointed at, not on a page
++ // gone to earlier that still shows whole beside it.
++ let pressed = unobstructed && ptr.pressed().is_some();
++ if resp.secondary_clicked() {
++ // Set again below when the right-click lands on a link.
++ view.links.context = None;
++ let mut over_image = false;
++ if tool == QuickTool::EditText {
++ let pointer_pos = ptr.pos;
++ if let Some(p) = pointer_pos {
++ for &page_idx in visible_pages {
++ let r = snap_to_pixels(rects[page_idx].translate(origin.to_vec2()), ppp);
++ if !r.intersects(visible) {
++ continue;
++ }
++ let xf = PageXform { rect: r, rot: view.rotation, pw: info.pages[page_idx].width.max(1.0), ph: info.pages[page_idx].height.max(1.0) };
++ let images = doc.page_images(page_idx);
++ for (idx, img) in images.iter().enumerate().rev() {
++ let obb = ImageObb::from_image(img, &xf, info, page_idx);
++ let b = image_screen_rect(img, &xf, info, page_idx);
++ if (b.is_positive() && b.contains(p)) || crate::edit_text_ui::quad_contains(obb.corners(), p) {
++ over_image = true;
++ if view.image_selection.as_ref().is_none_or(|s| s.page != page_idx || s.index != idx) {
++ view.image_selection = Some(crate::edit_text_ui::ImageSelection {
++ page: page_idx,
++ index: idx,
++ drag: None,
++ crop: None,
++ crop_mode: false,
++ crop_rect: None,
++ turning: false,
++ settled: None,
++ });
++ }
++ break;
++ }
++ }
++ if over_image {
++ break;
++ }
++ }
++ }
++ }
++ if !over_image && tool == QuickTool::EditText {
++ view.image_selection = None;
++ }
++ }
++ let mut frame = PageFrame {
++ env: *env,
++ resp: &resp,
++ ptr: &ptr,
++ origin,
++ visible,
++ pressed,
++ pointer,
++ signature_preview,
++ out: std::mem::take(out),
++ wanted: Vec::new(),
++ visible_now: Vec::new(),
++ current: view.current,
++ best_overlap: -1.0,
++ current_overlap: -1.0,
++ current_height: f32::INFINITY,
++ pressed_page: None,
++ calc_hover: None,
++ };
++ for &i in visible_pages {
++ draw_page(ui, view, &mut frame, i);
++ }
++ let PageFrame {
++ wanted, visible_now, mut current, best_overlap, current_overlap, current_height, pressed_page, calc_hover, out: page_out, ..
++ } = frame;
++ let PageOut {
++ mut canvas_action,
++ clicked_link,
++ copy_selected,
++ mut field_menu,
++ field_placed,
++ field_props,
++ hover_text: _,
++ mut image_action,
++ open_boxes,
++ open_initials,
++ open_signature,
++ open_table,
++ page_action: _,
++ primary_down: _,
++ refused,
++ stamp_placed,
++ } = page_out;
++ let mut page_action: Option<PageMenuAction> = None;
++ let painter = ui.painter();
++ if calc_order {
++ view.calc_order.set_hover_page(calc_hover);
++ let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
++ crate::calc_order_ui::paint_trace(ui, painter, visible, &view.calc_order, generation, selected);
++ }
++ // The pages' one cursor, from the claim on the highest layer the tool reaches, and the tip.
++ let hover_text = pointer.filter(|_| unobstructed).zip(view.cursor.resolve(ui.ctx(), tool));
++ // ScrollArea ignores the wheel while any widget is dragged, so a text-selection drag
++ // scrolls the page itself (#721). After the page loop, so it keeps scrolling once the
++ // pointer leaves the selection's page for the gap or the next page; over the whole
++ // document area, like #759. Combine direct and smoothed motion in one request so mixed
++ // Point and Line events retain their full contribution.
++ if selection_wheel {
++ let smooth = if view.precise_scroll_until == 0.0 { ui.input_mut(|input| std::mem::take(&mut input.smooth_scroll_delta)) } else { Vec2::ZERO };
++ let delta = precise_scroll + boost_extra + smooth;
++ if delta != Vec2::ZERO && delta.is_finite() {
++ ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
++ }
++ }
++ // Held at the top or bottom edge, or dragged past it, a text selection scrolls the view
++ // so it can reach text off screen (#1670); the selection follows the pointer on its page.
++ if view.selection_drag
++ && navigation_enabled
++ && unobstructed
++ && !single_paging
++ && let Some(p) = ptr.pos
++ {
++ let dy = crate::autoscroll::edge_delta(p.y, avail, ui.input(|i| i.stable_dt));
++ if dy != 0.0 {
++ view.end_find_following();
++ ui.scroll_with_delta_animation(vec2(0.0, dy), egui::style::ScrollAnimation::none());
++ // Keep scrolling while the pointer rests there and sends no events.
++ ui.ctx().request_repaint();
++ }
++ }
++ // The page navigated to stays current while no page shows more of itself, so the next
++ // page step starts from it rather than from a page further down the screen (#188).
++ // It also stays current while it is wholly on screen: a short page (a cheque) gone to
++ // from the Pages panel shows less of itself than the tall page below it, and must not
++ // hand the highlight to that page.
++ if current_overlap >= best_overlap - PAGE_TIE || current_overlap >= current_height - PAGE_TIE {
++ current = view.current;
++ }
++ if let Some(p) = pressed_page {
++ current = p;
++ }
++ if view.continuous {
++ view.current = current;
++ }
++ resp.context_menu(|ui| {
++ // Preparing a form: the selected field's menu.
++ if preparing && let Some((name, wi)) = view.prepare.selected.clone() {
++ // Several fields: Align, Center, Distribute, Set Fields to Same Size.
++ if !view.prepare.also.is_empty() {
++ use crate::prepare::Arrange as A;
++ let others = view.prepare.also.clone();
++ let anchor = (name.clone(), wi);
++ let mut pick = |ui: &mut egui::Ui, op: A, label: &str| {
++ if ui.add_enabled(can_modify, egui::Button::new(tl!(label))).clicked() {
++ view.pending_edit = crate::prepare::arrange(form, &anchor, &others, op);
++ ui.close();
++ }
++ };
++ ui.menu_button(tl!("Align"), |ui| {
++ pick(ui, A::AlignLeft, "Left");
++ pick(ui, A::AlignRight, "Right");
++ pick(ui, A::AlignTop, "Top");
++ pick(ui, A::AlignBottom, "Bottom");
++ pick(ui, A::AlignCenterV, "Center vertically");
++ pick(ui, A::AlignCenterH, "Center horizontally");
++ });
++ ui.menu_button(tl!("Distribute"), |ui| {
++ pick(ui, A::DistributeH, "Horizontally");
++ pick(ui, A::DistributeV, "Vertically");
++ });
++ ui.menu_button(tl!("Set Fields to Same Size"), |ui| {
++ pick(ui, A::SameHeight, "Height");
++ pick(ui, A::SameWidth, "Width");
++ pick(ui, A::SameSize, "Both");
++ });
++ ui.separator();
++ }
++ if ui.button(tl!("Properties…")).clicked() {
++ field_menu = Some(FieldMenu::Properties);
++ ui.close();
++ }
++ if ui.add_enabled(can_modify, egui::Button::new(tl!("Duplicate…"))).clicked() {
++ field_menu = Some(FieldMenu::Duplicate(name.clone()));
++ ui.close();
++ }
++ ui.separator();
++ let delete = if view.prepare.also.is_empty() { "Delete" } else { "Delete selected fields" };
++ if ui.add_enabled(can_modify, egui::Button::new(tl!(delete))).clicked() {
++ view.pending_edit = crate::prepare::delete_selected(view);
++ ui.close();
++ }
++ return;
++ }
++ if let Some((page, index)) = view.links.context {
++ if ui.button(tl!("Properties…")).clicked() {
++ view.links.open_existing = Some((page, index));
++ ui.close();
++ }
++ if ui.add_enabled(can_modify, egui::Button::new(tl!("Delete"))).clicked() {
++ view.pending_edit = Some(Edit::DeleteLink { page, index });
++ view.links.selected = None;
++ view.links.context = None;
++ ui.close();
++ }
++ ui.separator();
++ if ui.add_enabled(can_modify, egui::Button::new(tl!("Remove all links"))).clicked() {
++ view.pending_edit = Some(Edit::RemoveLinks { pages: None });
++ view.links.selected = None;
++ view.links.context = None;
++ ui.close();
++ }
++ return;
++ }
++ let can_paste_image = env.can_paste_image;
++ let is_image_menu = tool == QuickTool::EditText
++ && (ui.ctx().data_mut(|d| d.get_persisted::<bool>(crate::edit_text_ui::image_menu_id())).unwrap_or(false)
++ || view.image_selection.as_ref().is_some_and(|sel| {
++ let pointer_pos = ptr.pos;
++ let Some(p) = pointer_pos else { return false };
++ let Some((_, xf)) = view.screen_xforms.iter().find(|(page_idx, _)| *page_idx == sel.page) else { return false };
++ let images = doc.page_images(sel.page);
++ let Some(img) = images.get(sel.index) else { return false };
++ let obb = ImageObb::from_image(img, xf, info, sel.page);
++ let b = image_screen_rect(img, xf, info, sel.page);
++ (b.is_positive() && b.contains(p)) || crate::edit_text_ui::quad_contains(obb.corners(), p)
++ }));
++ if is_image_menu && let Some(sel) = view.image_selection.clone() {
++ let images = doc.page_images(sel.page);
++ let at = view.comments.context_at.map(|(_, at)| at);
++ crate::edit_text_ui::image_context_menu(ui, view, &images, sel.page, sel.index, can_paste_image, &mut image_action, at);
++ return;
++ }
++ if view.comments.selected.is_some() {
++ canvas_action = comments::context_menu(ui, view, info, &|page, index| doc.annotation_deletable(page, index), prefs, allowed, can_modify);
++ return;
++ }
++ // Over a picture (and no comment): Copy Image, as in Acrobat's page menu.
++ let mut copy_image = None;
++ if let Some((page, at)) = view.comments.context_at {
++ // The page's content is parsed once per right-click, not on every frame the menu is open.
++ let key = egui::Id::unique("copy-image-under-menu");
++ let cached = ui.data(|d| d.get_temp::<(usize, [f64; 2], Option<usize>)>(key)).filter(|c| c.0 == page && c.1 == at);
++ let hit = match cached {
++ Some(c) => c.2,
++ None => {
++ let hit = doc.page_image_at(page, at);
++ ui.data_mut(|d| d.insert_temp(key, (page, at, hit)));
++ hit
++ }
++ };
++ if let Some(image) = hit {
++ if ui.button(tl!("Copy Image")).clicked() {
++ copy_image = Some(comments::CanvasAction::CopyImage(page, image));
++ ui.close();
++ }
++ if ui.button(tl!("Copy Image in Original Format")).clicked() {
++ copy_image = Some(comments::CanvasAction::CopyImageFile(page, image));
++ ui.close();
++ }
++ ui.separator();
++ }
++ }
++ let has_copy_image = copy_image.is_some();
++ if copy_image.is_some() {
++ canvas_action = copy_image;
++ }
++ let (page, at) = view.comments.context_at.unwrap_or((view.current, [0.0, 0.0]));
++ let can_copy_items = can_copy && (view.image_selection.is_some() || view.selected_text().is_some() || has_copy_image);
++ let can_paste = can_paste_image && can_modify;
++ let perms = PageMenuPerms {
++ can_modify,
++ can_assemble: doc.allows_assembly(),
++ can_annotate: allowed,
++ can_print: doc.allows_printing(),
++ can_copy: can_copy_items,
++ can_paste,
++ has_annotations: !info.annotations.is_empty(),
++ };
++ default_page_context_menu(ui, page, at, perms, &mut page_action);
++ });
++ if !resp.context_menu_opened() {
++ ui.ctx().data_mut(|d| d.insert_persisted(crate::edit_text_ui::image_menu_id(), false));
++ }
++ *out = PageOut {
++ canvas_action,
++ clicked_link,
++ copy_selected,
++ field_menu,
++ field_placed,
++ field_props,
++ hover_text,
++ image_action,
++ open_boxes,
++ open_initials,
++ open_signature,
++ open_table,
++ page_action,
++ primary_down: ptr.primary_down,
++ refused,
++ stamp_placed,
++ };
++ (wanted, visible_now)
++}
++
++/// A zoom the fit mode changed keeps the page at the top of the view in place.
++fn anchor_zoom_change(view: &mut DocView, avail_width: f32, old_zoom: f32) {
++ if view.shown && view.zoom_anchor.is_none() && view.goto.is_none() && view.goto_point.is_none() && (view.zoom - old_zoom).abs() > 1e-4 {
++ let prev_viewport = view.viewport_screen;
++ let top_y = prev_viewport.min.y;
++ let anchor = view
++ .screen_rects
++ .iter()
++ .find(|(_, r)| r.min.y <= top_y && r.max.y > top_y)
++ .map(|(p, r)| {
++ let fy = if r.height() > 0.0 { ((top_y - r.min.y) / r.height()).clamp(0.0, 1.0) } else { 0.0 };
++ (*p, fy, 0.0f32)
++ })
++ .or_else(|| view.screen_rects.iter().find(|(_, r)| r.min.y >= top_y).map(|(p, r)| (*p, 0.0f32, (r.min.y - top_y).max(0.0))))
++ .or_else(|| view.screen_rects.last().map(|(p, r)| (*p, 1.0f32, (r.max.y - top_y).max(0.0))));
++ if let Some((page, fy, rel_y)) = anchor {
++ view.zoom_anchor = Some((page, 0.5, fy, vec2(avail_width / 2.0, rel_y)));
++ } else {
++ view.zoom_anchor = Some((view.current, 0.5, 0.0, vec2(avail_width / 2.0, MARGIN)));
++ }
++ }
++}
++
++/// What the pager needs to know about the page row on screen.
++#[derive(Clone, Copy)]
++struct PagingCx<'a> {
++ row: &'a [usize],
++ rects: &'a [Rect],
++ avail: Rect,
++ middle_gesture: bool,
++ unobstructed: bool,
++}
++
++/// Not scrolling: when the whole page or spread fits, the wheel turns pages instead. Returns whether
++/// paging owns the wheel, and whether it turned the page at an edge.
++fn single_page_paging(ui: &egui::Ui, view: &mut DocView, cx: &PagingCx<'_>) -> (bool, bool) {
++ let (mut single_paging, mut edge_turned) = (false, false);
++ let PagingCx { row, rects, avail, middle_gesture, unobstructed } = *cx;
++ if !view.continuous {
++ // The row's bounding box on screen (one page, or a two-page spread).
++ let row_rect = row.iter().filter_map(|&i| rects.get(i)).copied().reduce(Rect::union);
++ // Within a point, so layout rounding can't stop a fitting page from turning.
++ let fits = row_rect.is_some_and(|r| r.height() + 2.0 * MARGIN <= avail.height() + 1.0);
++ let can_turn = !middle_gesture && unobstructed && ui.rect_contains_pointer(avail);
++ single_paging = can_turn && fits;
++ // Every wheel event goes to the pager, so it follows each trackpad touch to its end
++ // even while the page can't turn.
++ ui.input(|i| {
++ for e in &i.events {
++ if let egui::Event::MouseWheel { unit, delta, phase, modifiers, .. } = e {
++ // Zooming (⌘ or Ctrl) and sideways scrolling (Shift) never turn pages.
++ let dy = if modifiers.command || modifiers.ctrl || modifiers.shift { 0.0 } else { delta.y };
++ if fits {
++ view.single_page_wheel(*unit, dy, *phase, i.time, can_turn);
++ } else {
++ edge_turned |= view.single_page_wheel_edge(*unit, dy, *phase, i.time, can_turn);
++ }
++ }
++ }
++ });
++ if single_paging {
++ // Paging owns vertical wheel motion here. A diagonal gesture keeps its sideways
++ // part, as `ScrollArea` itself handles each axis.
++ ui.input_mut(|i| i.smooth_scroll_delta.y = 0.0);
++ }
++ }
++ (single_paging, edge_turned)
++}
++
++/// This frame's wheel motion over the page view, after paging and point/line suppression.
++struct Wheel {
++ precise_scroll: Vec2,
++ line_base: Vec2,
++ boost_extra: Vec2,
++ has_page: bool,
++ /// The pager owns the vertical axis (single-page view, or the rest of a gesture that turned the page).
++ paging_owns_vertical: bool,
++}
++
++/// Point input bypasses egui smoothing for 1:1 motion. Line input keeps egui's configured, smoothed
++/// base and adds only the accelerated surplus. While point suppression is active, line and page
++/// input are reconstructed so current coarse events survive without exposing an older eased tail.
++fn read_wheel(ui: &mut egui::Ui, view: &mut DocView, avail: Rect, single_paging: bool, edge_turned: bool) -> Wheel {
++ let now = ui.input(|i| i.time);
++ let input = if ui.rect_contains_pointer(avail) {
++ // Match the line distance used by egui's other scroll areas.
++ let live = ui.ctx().options(|o| o.input_options.line_scroll_speed);
++ let line_points = if live.is_finite() && live > 0.0 { live } else { crate::scroll_speed::EGUI_LINE_POINTS };
++ ui.input(|i| crate::wheel_boost::accumulate(&i.events, line_points, single_paging))
++ } else {
++ crate::wheel_boost::FrameInput::default()
++ };
++ let mut precise_scroll = input.precise;
++ let mut line_base = input.line;
++ let mut boost_extra = crate::wheel_boost::boost_extra(input, &mut view.wheel_boost_x, &mut view.wheel_boost_y, now);
++ let has_page = input.page != Vec2::ZERO;
++ if now.is_finite() && precise_scroll != Vec2::ZERO && !has_page {
++ view.precise_scroll_until = now + SUPPRESS_SECS;
++ }
++ if edge_turned {
++ // The turn leaves egui's eased copy of the notch to decay for a few frames; it must not
++ // scroll the page just shown away from its top (or bottom).
++ view.precise_scroll_until = now + 0.25;
++ view.fling.stop();
++ }
++ // Paging owns the vertical axis in single-page view; leave it to the pager.
++ // The rest of a gesture that turned the page is swallowed too, or it would pan the new page
++ // away from the edge it was shown at.
++ let paging_owns_vertical = single_paging || (!view.continuous && view.wheel.gesture_turned());
++ if paging_owns_vertical {
++ precise_scroll.y = 0.0;
++ line_base.y = 0.0;
++ boost_extra.y = 0.0;
++ }
++ // egui smooths a Point event that moved at least 8 points, keeping part of it for later
++ // passes. Suppression must outlive that whole action: once it expired after a fixed 0.25 s,
++ // the last few eased points leaked through the ScrollArea after a precise 1:1 scroll (held
++ // point input measured 33 points for a 30-point event). Keep hiding the eased copy until
++ // egui says the action is over, then forget the suppression.
++ let scrolling = ui.input(|i| i.is_scrolling());
++ if now < view.precise_scroll_until || (view.precise_scroll_until != 0.0 && scrolling) {
++ if scrolling {
++ // Keep hiding egui's unprocessed copy until it has drained. The short rolling grace
++ // avoids a delayed duplicate without prolonging suppression after the tail is gone.
++ view.precise_scroll_until = view.precise_scroll_until.max(now + SUPPRESS_TAIL_SECS);
++ }
++ ui.input_mut(|i| i.smooth_scroll_delta = Vec2::ZERO);
++ // The point path suppresses every eased wheel delta. Restore the line base through the
++ // same un-eased path as its surplus; paging-owned axes were cleared above.
++ boost_extra = crate::wheel_boost::cap_frame(boost_extra + line_base);
++ if has_page {
++ // Page units normally stay on egui's viewport-sized path. During suppression only,
++ // reproduce the current event directly so an older hidden tail stays hidden.
++ let page_scale = ui.input(|i| i.viewport_rect().height());
++ let mut page_delta =
++ if page_scale.is_finite() && page_scale > 0.0 { crate::wheel_boost::cap_frame(input.page * page_scale) } else { Vec2::ZERO };
++ if paging_owns_vertical {
++ page_delta.y = 0.0;
++ }
++ boost_extra = crate::wheel_boost::cap_frame(boost_extra + page_delta);
++ }
++ } else {
++ precise_scroll = Vec2::ZERO;
++ // Selection handling below uses zero to distinguish an inactive window. Clearing a past
++ // deadline also keeps a later line/page action from being mistaken for the old tail.
++ view.precise_scroll_until = 0.0;
++ }
++ if has_page && paging_owns_vertical {
++ // Page input belongs to egui on the free axis; the pager still owns vertical motion.
++ ui.input_mut(|i| i.smooth_scroll_delta.y = 0.0);
++ }
++ Wheel { precise_scroll, line_base, boost_extra, has_page, paging_owns_vertical }
++}
++
++/// What decides which renders a frame asks for.
++#[derive(Clone, Copy)]
++struct RenderPlan<'a> {
++ visible_pages: &'a [usize],
++ tag: u64,
++ scale: f32,
++ sharp_held: bool,
++ pan_held: bool,
++ hand: bool,
++}
++
++/// Queues the renders for the pages and tiles `renders` wants (nearest the view first), prefetches the
++/// rows either side, frees tiles of far pages and old zooms, and asks for the text layers.
++fn plan_renders(ui: &egui::Ui, view: &mut DocView, info: &DocInfo, renders: PageRenders, plan: &RenderPlan<'_>) {
++ let (mut wanted, visible_now) = renders;
++ let RenderPlan { visible_pages, tag, scale, sharp_held, pan_held, hand } = *plan;
++ // Single-page and two-page views draw only the current row, so the next one was first asked
++ // for on the turn and showed blank paper until it rendered. Prefetch the rows either side,
++ // as continuous scrolling does for the pages just off screen.
++ if !view.continuous
++ && let (Some(&first), Some(&last)) = (visible_pages.first(), visible_pages.last())
++ {
++ let n = if view.layout == PageLayout::Single { 1 } else { 2 };
++ let max_side = ui.ctx().input(|inp| inp.max_texture_side) as f32;
++ for i in (last.saturating_add(1)..=last.saturating_add(n)).chain(first.saturating_sub(n)..first) {
++ let Some(p) = info.pages.get(i).filter(|_| !view.errors.contains_key(&i)) else { continue };
++ // As drawn: the whole page at this scale, or the backdrop of a tiled page.
++ let side = p.width.max(p.height).max(1.0);
++ wanted.push(if side * scale > TILE_THRESHOLD.min(max_side) {
++ let bs = BASE_SIDE.min(max_side) / side;
++ (i, bs, scale_tag(bs), None)
++ } else {
++ (i, scale, tag, None)
++ });
++ }
++ }
++ let cur = view.current;
++ wanted.sort_by_key(|w| (!visible_now.contains(&w.0), w.0.abs_diff(cur), w.3.is_some()));
++ // Tiles of far-away pages are useless: free them. Tiles of an earlier zoom stay for a page
++ // that is on screen, whether or not the page is tiled at this zoom (#2196): a zoom out below
++ // the tiling threshold used to drop the tiles of the zoom just left, so coming back to it
++ // was a cold miss and the page re-blurred through the backdrop while every tile was
++ // interpreted again. `OLD_TILES` still bounds how many older tiles are kept, nearest scale
++ // first, and admission keeps them inside `TILE_BYTES`.
++ let missing: HashSet<usize> = wanted.iter().filter(|w| w.3.is_some()).map(|w| w.0).collect();
++ let on_screen: HashSet<usize> = visible_now.iter().copied().collect();
++ view.tiles.retain(|(p, t, _, _), _| p.abs_diff(cur) <= 2 && (*t == tag || missing.contains(p) || on_screen.contains(p)));
++ let mut older: Vec<(f32, (usize, u64, u32, u32))> =
++ view.tiles.keys().filter(|k| k.1 != tag).map(|k| ((tag_scale(k.1) / scale).ln().abs(), *k)).collect();
++ // Tiles of earlier zooms are kept nearest scale first, and bounded by count *and* bytes: a
++ // large scan renders a tile up to 4096² (about 67 MiB), so a count alone let a few tiles of
++ // a few zooms run far past the tile allowance. #2196 keeps a zoom already visited, which
++ // makes that reachable in ordinary zooming, so the byte bound has to be here too.
++ older.sort_by(|a, b| a.0.total_cmp(&b.0));
++ let mut kept = 0usize;
++ let mut kept_bytes = 0usize;
++ let mut drop: Vec<(usize, u64, u32, u32)> = Vec::new();
++ for (_dist, key) in older {
++ let bytes = view.tiles.get(&key).map_or(0, texture_bytes);
++ if kept >= OLD_TILES || kept_bytes.saturating_add(bytes) > TILE_BYTES {
++ drop.push(key);
++ } else {
++ kept = kept.saturating_add(1);
++ kept_bytes = kept_bytes.saturating_add(bytes);
++ }
++ }
++ for key in drop {
++ view.tiles.remove(&key);
++ }
++ let mut queue: Vec<RenderRequest> = wanted
++ .iter()
++ .map(|&(page, wanted_scale, wanted_tag, tile)| RenderRequest {
++ page,
++ kind: RequestKind::Pixels,
++ tile,
++ scale: wanted_scale,
++ tag: wanted_tag,
++ // The low-res backdrop under tiles is a preview: tiny images are skipped for it.
++ // It is the whole-page request whose scale is not the current one; sharp tiles
++ // and whole-page finals always draw everything.
++ preview: tile.is_none() && (wanted_scale != scale || wanted_tag != tag),
++ })
++ .collect();
++ // While the zoom or pan is settling, hold back sharp re-renders of pages that already
++ // show something (a stale whole-page raster or older tiles, drawn stretched): each new
++ // scale would cancel the running full-page interpret and restart it from scratch, and
++ // each pan frame would queue a storm of full-page tile interprets.
++ // Pages with nothing on screen yet still queue, so panning to a fresh page draws it.
++ if sharp_held || pan_held {
++ let main_tag = tag;
++ queue.retain(|req| {
++ // Fast pages render straight through: the hold only protects heavy pages
++ // whose full-page interpret costs seconds.
++ if !view.slow_pages.contains(&req.page) {
++ return true;
++ }
++ if req.tile.is_some() {
++ let has_current_tile = view.tiles.keys().any(|(p, t, _, _)| *p == req.page && *t == req.tag);
++ if has_current_tile {
++ return false;
++ }
++ let has_any_surface = view.pages.contains_key(&req.page) || view.tiles.keys().any(|(p, _, _, _)| *p == req.page);
++ !has_any_surface
++ } else if req.tag != main_tag {
++ // The low-res backdrop under tiles is zoom-independent: let it through,
++ // it is the stretched surface the hold relies on.
++ true
++ } else {
++ // The stale raster stays stretched until the zoom or pan settles; a page with
++ // nothing on screen yet still renders.
++ !view.pages.contains_key(&req.page)
++ }
++ });
++ }
++ // Text layers: visible pages for selection, every page while a search is active. (Select All
++ // of the document needs no more: shown pages highlight as their text comes, and Copy reads
++ // the rest, as text far away is let go to bound memory.)
++ let need_text = |p: &usize| !view.texts.contains_key(p) && !view.text_failed.contains(p);
++ let mut text_pages: Vec<usize> = if hand { Vec::new() } else { visible_now.iter().copied().filter(need_text).collect() };
++ if view.find.as_ref().is_some_and(|f| !f.case_query.trim().is_empty()) {
++ let n = info.pages.len();
++ let rest: Vec<usize> = (0..n).map(|k| (cur + k) % n).filter(need_text).filter(|p| !text_pages.contains(p)).collect();
++ text_pages.extend(rest);
++ }
++ queue.extend(text_pages.into_iter().map(|page| RenderRequest {
++ page,
++ kind: RequestKind::Text,
++ tile: None,
++ scale: 1.0,
++ tag: TEXT_TAG,
++ preview: false,
++ }));
++ view.frame_queue = queue;
++ view.frame_visible = visible_now.iter().copied().collect();
++}
++
++/// Acts on the notice bar button that was clicked.
++fn apply_notice(app: &mut PdfCraftApp, index: usize, notice: Option<Notice>) {
++ match notice {
++ #[cfg(not(target_arch = "wasm32"))]
++ Some(Notice::Reload) => app.watch.reload_request = app.views.get(index).map(|view| view.id),
++ #[cfg(target_arch = "wasm32")]
++ Some(Notice::Reload) => {}
++ Some(Notice::Repairs) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Advanced)),
++ Some(Notice::Security) => app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Security)),
++ Some(Notice::Unlock) => {
++ if let Some(doc) = app.views.get(index).and_then(|view| app.session.get(view.id)) {
++ app.unlock_prompt = Some(crate::UnlockPrompt::new(doc, None));
++ }
++ }
++ // Flags the view: the tab closes at the top of the next frame, where no document
++ // borrow is held.
++ Some(Notice::Close) => {
++ if let Some(view) = app.views.get_mut(index) {
++ view.close_requested = true;
++ }
++ }
++ Some(Notice::Signatures) => app.right = Some(RightPanel::Signatures),
++ Some(Notice::FieldHighlights(on)) => app.view_defaults.highlight_fields = on,
++ Some(Notice::Console) => app.dialog = Some(crate::Dialog::JsConsole),
++ // Back on in Preferences ▸ General.
++ Some(Notice::HideSecured) => app.show_secured_notice = false,
++ None => {}
++ }
++}
++
++/// The snapshot notice bar's Close was clicked: close here, where no document borrow is held. The
++/// tab is looked up by id: indices may have shifted since the click, and the flag rides its own
++/// view, so a stale flag can never close the wrong tab. Whether a tab was closed (or asked about).
++fn close_requested_tab(app: &mut PdfCraftApp, index: usize) -> bool {
++ let pending = app.views.get(index).filter(|v| v.close_requested).map(|v| v.id).and_then(|id| app.views.iter().position(|v| v.id == id));
++ let Some(i) = pending else { return false };
++ app.views[i].close_requested = false;
++ if app.has_unsaved_work(i) {
++ app.close_request = Some(crate::CloseRequest::Tab(app.views[i].id));
++ } else {
++ app.close_tab(i);
++ }
++ true
++}
++
++/// Where the pages are laid out, for placing the scroll area.
++#[derive(Clone, Copy)]
++struct ScrollGeometry<'a> {
++ rects: &'a [Rect],
++ y_shift: f32,
++ content_w: f32,
++ content_h: f32,
++ avail: Rect,
++ middle_gesture: bool,
++ hand: bool,
++}
++
++/// The page view's scroll area, with the offset this frame's jumps, drags and zoom anchor ask for.
++fn page_scroll_area(ui: &egui::Ui, view: &mut DocView, geometry: &ScrollGeometry<'_>) -> egui::ScrollArea {
++ let ScrollGeometry { rects, y_shift, content_w, content_h, avail, middle_gesture, hand } = *geometry;
++ let mut scroll = egui::ScrollArea::both().auto_shrink([false, false]).scroll_source(egui::scroll_area::ScrollSource {
++ drag: if middle_gesture {
++ egui::scroll_area::DragScroll::Never
++ } else if hand {
++ egui::scroll_area::DragScroll::Always
++ } else {
++ egui::scroll_area::DragScroll::OnTouch
++ },
++ ..Default::default()
++ });
++ // Each document keeps its own scroll position when switching tabs (#189).
++ scroll = scroll.id_salt(("page-view", view.id));
++ // Every scroll here asks for no animation. An animated area would still queue a zero-length
++ // offset target for the next frame, which overwrites the jump a page turn just set.
++ scroll = scroll.animated(false);
++ // A newly opened document starts at the top. eframe saves egui's memory with the settings
++ // and document ids start again at 1 every launch, so the offset stored under this id can be
++ // where an earlier session left another document.
++ if !std::mem::replace(&mut view.shown, true) {
++ scroll = scroll.scroll_offset(Vec2::ZERO);
++ }
++ // The Hand tool pans here, before the pages are laid out, so the page moves with the pointer
++ // in the frame the pointer moves (#924). Scrolling by the drag from inside the area moved the
++ // page only after that frame was drawn, a frame behind. The frame egui tells the drag from a
++ // click, the page catches up with all the pointer has moved since the press. A jump asked for
++ // below still wins.
++ if hand
++ && !middle_gesture
++ && let Some(id) = view.page_widget.filter(|id| ui.ctx().is_being_dragged(*id))
++ {
++ let moved = if ui.ctx().drag_started_id() == Some(id) {
++ ui.input(|i| i.pointer.total_drag_delta()).unwrap_or(Vec2::ZERO)
++ } else {
++ ui.input(|i| i.pointer.delta())
++ };
++ if moved != Vec2::ZERO {
++ view.end_find_following();
++ }
++ // Within the content, as egui keeps it: past an end the page would show there for a
++ // frame and spring back. `max` is never NaN or below zero, so the clamp can't panic.
++ let max = vec2((content_w - avail.width()).max(0.0), (content_h - avail.height()).max(0.0));
++ scroll = scroll.scroll_offset((view.last_scroll - moved).clamp(Vec2::ZERO, max));
++ }
++ if let Some(v) = view.sync_scroll.take() {
++ scroll = scroll.scroll_offset(v);
++ }
++ if let Some((page, fx, fy, rel)) = view.zoom_anchor.take() {
++ let r = rects[page.min(rects.len() - 1)];
++ let point = pos2(r.left() + fx * r.width(), r.top() - y_shift + fy * r.height());
++ scroll = scroll.scroll_offset(anchored_scroll(point, rel, vec2(content_w, content_h), avail.size()));
++ } else if let Some((page, frac)) = view.goto.take() {
++ let page = page.min(rects.len() - 1);
++ let r = rects[page];
++ // Clamped here: egui clamps only after drawing, so a page that fits was drawn MARGIN - page_gap()
++ // too high for a frame and then hopped into place. (`avail` is the viewport, as the
++ // scroll bars float.)
++ let max_y = (content_h - avail.height()).max(0.0);
++ scroll = scroll.vertical_scroll_offset((r.top() - y_shift - view.page_gap() + frac * r.height()).max(0.0).min(max_y));
++ // Horizontally too, as Acrobat does (#2665): beside a much wider page the others sit off
++ // to one side. A page already shown whole, or one filling the window's width, keeps the
++ // scroll, so paging through a zoomed-in document stays on the same column. Otherwise a
++ // page that fits is centred, and a wider one scrolled just far enough to fill the window.
++ // The page counts with its side gutters, which keep its edge clear of the floating
++ // quick-action bar as the layout does.
++ let (x, w) = (view.last_scroll.x, avail.width());
++ let (left, right) = (r.left() - SIDE, r.right() + SIDE);
++ let target = if right - left <= w { if left >= x && right <= x + w { x } else { r.center().x - w / 2.0 } } else { x.clamp(left, right - w) };
++ if (target - x).abs() > 0.5 {
++ scroll = scroll.horizontal_scroll_offset(target.clamp(0.0, (content_w - w).max(0.0)));
++ }
++ }
++ scroll
++}
++
++/// Notes how far the page view scrolled this frame: reader-owned scrolling ends search following,
++/// a pan holds back fresh tile renders until it settles, and a glide keeps going or stops. Whether
++/// a pan is still settling.
++fn track_scroll<T>(ui: &egui::Ui, view: &mut DocView, out: &egui::containers::scroll_area::ScrollAreaOutput<T>, glide: Vec2) -> bool {
++ let scroll_offset = out.state.clamped_offset();
++ // The document ScrollArea's own scroll surfaces move the offset without the wheel deltas
++ // above: its two scroll-bar tracks/handles and, for touch or the Hand tool, its drag
++ // background. The output gives us the area id, and egui's pinned ScrollArea registers those
++ // widgets under `id.with(0)` (horizontal), `id.with(1)` (vertical) and `id.with("area")`.
++ // Ask for their exact responses instead of treating any held drag as scrolling: a text
++ // selection, a panel splitter or another widget's drag must not excuse the programmatic jump
++ // a search flash makes, while a track click (which moves the offset with no pointer motion)
++ // must still count as the reader scrolling.
++ let scroll_surface = |id: egui::Id| ui.ctx().read_response(id).is_some_and(|r| r.is_pointer_button_down_on() || r.dragged() || r.clicked());
++ let pointer_scrolling = scroll_surface(out.id.with(0usize)) || scroll_surface(out.id.with(1usize)) || scroll_surface(out.id.with("area"));
++ if pointer_scrolling && (scroll_offset - view.last_scroll).length() > 2.0 {
++ view.end_find_following();
++ }
++ let now_settle = ui.input(|i| i.time);
++ if (scroll_offset - view.last_scroll).length() > 2.0 {
++ view.pan_hold_until = now_settle + 0.25;
++ ui.ctx().request_repaint_after(std::time::Duration::from_millis(280));
++ }
++ if glide != Vec2::ZERO {
++ view.fling.moved((scroll_offset - view.last_scroll).length() > 0.01);
++ }
++ view.last_scroll = scroll_offset;
++ now_settle < view.pan_hold_until
++}
++
++/// The hover tip, find bar, size hint and the in-place editors drawn over the pages. Whether the
++/// text editor was finished.
++fn frame_overlays(ui: &mut egui::Ui, view: &mut DocView, env: &PageEnv<'_>, primary_down: bool, hover_text: Option<(Pos2, String)>) -> bool {
++ let PageEnv { added, author, avail, can_modify, doc, form, info, prefs, t, today, tool, units, unobstructed, .. } = *env;
++ if let Some((pos, text)) = hover_text {
++ // Click-through, as a tooltip is: the pages lose the pointer under anything that isn't.
++ let tip = egui::Area::new(egui::Id::unique("canvas-hover")).order(egui::Order::Tooltip).interactable(false);
++ tip.fixed_pos(pos + vec2(14.0, 16.0)).show(ui.ctx(), |ui| {
++ egui::Frame::popup(ui.style()).show(ui, |ui| {
++ ui.set_max_width(320.0);
++ ui.label(text);
++ });
++ });
++ }
++ find_bar(view, info.pages.len(), avail, ui, t);
++ page_size_hint(view, info, units, avail, unobstructed, ui, t);
++ if tool == QuickTool::EnhanceCamera {
++ crate::camera_ui::bar(ui.ctx(), view, avail, t, can_modify);
++ }
++ if let Some(e) = comments::composer(ui.ctx(), view, info, prefs) {
++ view.pending_edit = Some(e);
++ }
++ if let Some(e) = crate::edit_text_ui::overlay(ui.ctx(), view, info) {
++ view.pending_edit = Some(e);
++ }
++ let text_box = crate::forms_ui::text_box(doc, view, form);
++ if let Some(e) = crate::forms_ui::overlay(ui.ctx(), view, info, form, primary_down, text_box.as_ref(), today) {
++ view.pending_edit = Some(e);
++ }
++ if let Some(e) = crate::fill_sign::type_box(ui.ctx(), view, info, author) {
++ view.pending_edit = Some(e);
++ }
++ let (typed, text_done) = crate::content_ui::editor(ui.ctx(), view, info, added);
++ if typed.is_some() {
++ view.pending_edit = typed;
++ }
++ text_done
++}
++
++/// Full screen: the click turns the page unless it did something else first. It waits out the
++/// double-click time: turning on the first click's release sent a double-click's second click,
++/// meant to select a word, to the next page (review on #2070). A selection cancels it, and so do a
++/// link followed meanwhile and leaving full screen; a double-click elsewhere still turns once.
++/// `click` is `Some(back)` on the frame of a click, `back` when it was a shift-click.
++fn schedule_presentation_turn(ui: &egui::Ui, view: &mut DocView, click: Option<bool>, link_followed: bool) {
++ let now = ui.input(|i| i.time);
++ if let Some(back) = click
++ && !link_followed
++ && view.selection.is_none()
++ && view.clicks < 2
++ {
++ let delay = ui.ctx().options(|o| o.input_options.max_double_click_delay);
++ view.present_turn = Some((now + delay, !back));
++ }
++ if let Some((due, forward)) = view.present_turn {
++ if link_followed || view.selection.is_some() || view.presentation.is_none() {
++ view.present_turn = None;
++ } else if now >= due {
++ view.present_turn = None;
++ view.step_page(forward);
++ } else {
++ ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64((due - now).max(0.0)));
++ }
++ }
++}
++
++/// Follows the link that was clicked. A link to another file comes back, to be opened once the
++/// document is no longer borrowed.
++fn follow_link(
++ app: &mut PdfCraftApp,
++ index: usize,
++ target: Option<LinkTarget>,
++) -> Option<(pdfcraft_engine::DocId, String, pdfcraft_render::RemoteDestination)> {
++ let view_id = app.views[index].id;
++ match target? {
++ LinkTarget::Page(p, dest) => {
++ let view = &mut app.views[index];
++ if let Some(doc) = app.session.get(view.id) {
++ view.go_to_dest(p, dest, &doc.info);
++ }
++ }
++ LinkTarget::Uri(u) => app.request_document_url(&u, crate::LinkOrigin::Link),
++ LinkTarget::SetLayers { changes, preserve_rb } => set_layers(app, index, &changes, preserve_rb),
++ LinkTarget::RemoteGoTo { file, destination, .. } => return Some((view_id, file, destination)),
++ LinkTarget::Other(s) => app.notify_fmt("This PDF uses the unsupported {s} action.", &[("s", &s)]),
++ }
++ None
++}
++
++/// A finished marquee zoom, a signature rectangle drawn, or an empty signature field clicked.
++/// Returns the tool to go on with.
++fn finish_sign_requests(app: &mut PdfCraftApp, index: usize, mut tool: QuickTool) -> QuickTool {
++ if let Some(done) = app.views[index].marquee_done.take() {
++ app.finish_marquee(index, done);
++ }
++ // A signature rectangle was drawn, or an empty signature field clicked.
++ let drawn = app.views[index].sign.drawn.take();
++ if let (Some((page, rect)), QuickTool::SignArea { certify }) = (drawn, tool) {
++ tool = QuickTool::Select;
++ app.quick_tool = tool;
++ app.start_signing(page, Some(rect), None, certify.then_some(2));
++ }
++ if let Some(field) = app.views[index].sign.field.take() {
++ let signed = app.session.get(app.views[index].id).is_some_and(|d| d.signatures.iter().any(|s| s.field == field && s.signed));
++ if signed {
++ app.right = Some(RightPanel::Signatures);
++ if !app.sig_expanded.contains(&field) {
++ app.sig_expanded.push(field);
++ }
++ } else {
++ let page = app.views[index].current;
++ app.start_signing(page, None, Some(field), None);
++ }
++ }
++ tool
++}
++
++/// When not scrolling continuously, only one row is shown at a time: the current page, or the
++/// current two-page spread (with the cover rule). These indices drive paging, which pages are
++/// drawn, and the content height (#752). Empty for a document with no pages.
++fn shown_row(view: &DocView, rects: &[Rect]) -> Vec<usize> {
++ if rects.is_empty() {
++ return Vec::new();
++ }
++ let last = rects.len() - 1;
++ let cur = view.current.min(last);
++ match view.layout {
++ PageLayout::Single => vec![cur],
++ PageLayout::TwoUp if view.cover && cur == 0 => vec![0],
++ PageLayout::TwoUp => {
++ let first = if view.cover { cur - (cur + 1) % 2 } else { cur - cur % 2 };
++ if first < last { vec![first, first + 1] } else { vec![first] }
++ }
++ }
++}
++
++/// Full screen: a click on the document goes forward a page and a shift-click back, as in
++/// Acrobat, unless the click did something else (a link, a word selected). Decided once the pages
++/// have had the click. `Some(back)` on the frame of a click, `back` when it was a shift-click.
++fn presentation_click_state(ui: &egui::Ui, view: &DocView, navigation_rect: Rect, enabled: bool) -> Option<bool> {
++ let clicked = view.presentation.is_some() && enabled && presentation_click(ui, navigation_rect);
++ // Shift as held now, or as the click's own event carried it (a synthetic click).
++ clicked.then(|| {
++ ui.input(|i| {
++ i.modifiers.shift
++ || i.events.iter().any(|e| {
++ matches!(e, egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: false, modifiers, .. } if modifiers.shift)
++ })
++ })
++ })
++}
++
++/// Pinch / ctrl+scroll zoom, anchored on the pointer or the current page.
++fn pinch_zoom(ui: &egui::Ui, view: &mut DocView, zoom_delta: f32, navigation_rect: Rect, enabled: bool) {
++ if zoom_delta.is_finite() && zoom_delta > 0.0 && zoom_delta != 1.0 && enabled && ui.rect_contains_pointer(navigation_rect) {
++ let z = view.zoom * zoom_delta;
++ match zoom_anchor(ui) {
++ Some(p) => view.zoom_at(z, p),
++ None => view.set_zoom(z),
++ }
++ }
++}
++
++/// The middle-button auto-scroll's delta, and the edge scroll a drawing in progress asks for: the
++/// pointer in a band along any edge (or just past it) scrolls the view that way, so a point
++/// beyond what is shown can be reached without letting go of the tool.
++fn auto_scroll_deltas(ui: &egui::Ui, view: &mut DocView, avail: Rect, unobstructed: bool) -> (Vec2, Vec2) {
++ let auto_delta = if unobstructed {
++ view.auto_scroll.update(ui, avail, false)
++ } else {
++ view.auto_scroll.cancel();
++ Vec2::ZERO
++ };
++
++ // Drawing a comment: the pointer in a band along any edge (or just past it) scrolls the view
++ // that way, so a point beyond what is shown can be reached without letting go of the tool.
++ let edge_delta = if unobstructed && matches!(view.comments.gesture, Some(comments::Gesture::Draw { .. })) {
++ let (pointer, dt) = drawing_edge_pointer(ui);
++ crate::autoscroll::edge_scroll(pointer, avail, dt)
++ } else {
++ Vec2::ZERO
++ };
++ if edge_delta != Vec2::ZERO {
++ ui.ctx().request_repaint();
++ }
++ (auto_delta, edge_delta)
++}
++
++/// What decides whether touchpad momentum goes on.
++#[derive(Clone, Copy)]
++struct GlideCx {
++ now: f64,
++ may_glide: bool,
++ key_scroll: f32,
++ auto_delta: Vec2,
++ zoom_delta: f32,
++ edge_delta: Vec2,
++ wheel_scrolled: bool,
++}
++
++/// Touchpad momentum (#531): a swipe over the page lifted at speed glides on and slows to a stop,
++/// through the same un-eased path. Never while the pager owns the wheel, a gesture or dialog has
++/// the input, or animations are off; any click, key, zoom or other scrolling ends it. Any
++/// reader-owned motion ends automatic search following, even when it stays on the same page; the
++/// automatic flash uses `goto`, not these deltas, so its programmatic scroll is never mistaken for
++/// a reader action. Returns this frame's glide.
++fn step_glide(ui: &egui::Ui, view: &mut DocView, avail: Rect, cx: &GlideCx) -> Vec2 {
++ let GlideCx { now, may_glide, key_scroll, auto_delta, zoom_delta, edge_delta, wheel_scrolled } = *cx;
++ let over_page = ui.rect_contains_pointer(avail);
++ ui.input(|i| view.fling.read(&i.events, now, may_glide && over_page));
++ let interrupted = glide_interrupted(ui);
++ if !may_glide || interrupted || key_scroll != 0.0 || auto_delta != Vec2::ZERO || zoom_delta != 1.0 {
++ view.fling.stop();
++ }
++ let glide = view.fling.step(now);
++ if view.fling.gliding() {
++ ui.ctx().request_repaint();
++ }
++ if wheel_scrolled || auto_delta != Vec2::ZERO || edge_delta != Vec2::ZERO || glide != Vec2::ZERO {
++ view.end_find_following();
++ }
++ glide
++}
++
++/// Text selection runs for the Select tool and for the markup tools (highlight…).
++fn tool_selects_text(tool: QuickTool, preparing: bool) -> bool {
++ match tool {
++ QuickTool::Comment(t) => t.markup().is_some() || t == comments::CommentTool::ReplaceText,
++ QuickTool::Select => !preparing,
++ QuickTool::Redact | QuickTool::ColumnSelect => true,
++ QuickTool::Hand
++ | QuickTool::Measure(_)
++ | QuickTool::Crop
++ | QuickTool::EnhanceCamera
++ | QuickTool::Fill(_)
++ | QuickTool::Field(_)
++ | QuickTool::AddText
++ | QuickTool::EditText
++ | QuickTool::Stamp(_)
++ | QuickTool::CustomStamp(_)
++ | QuickTool::Link
++ | QuickTool::SignArea { .. }
++ | QuickTool::MarqueeZoom
++ | QuickTool::Snapshot => false,
++ }
++}
++
++/// Holds sharp re-renders while the zoom is still moving: a new scale every frame would cancel the
++/// in-flight full-page interpret and restart it, so on a heavy page (a CAD plot with ~1000 images
++/// renders for seconds) the sharp raster never arrives and the view looks stuck on a soft image.
++/// The stale raster stays stretched meanwhile; the sharp request goes out once the zoom settles.
++fn hold_sharp_renders(ui: &egui::Ui, view: &mut DocView, scale: f32) -> bool {
++ let now_scale = ui.input(|i| i.time);
++ if (scale - view.last_scale).abs() > f32::EPSILON {
++ view.last_scale = scale;
++ view.sharp_hold_until = now_scale + 0.35;
++ ui.ctx().request_repaint_after(std::time::Duration::from_millis(380));
++ }
++ now_scale < view.sharp_hold_until
++}
++
++/// A destination's position (bookmarks, links) refines `goto`: its point at the window's top-left,
++/// or the centre of a /FitR rectangle at the window's centre.
++fn refine_goto_point(mut scroll: egui::ScrollArea, view: &mut DocView, geometry: &ScrollGeometry<'_>) -> egui::ScrollArea {
++ let ScrollGeometry { rects, y_shift, content_w, content_h, avail, .. } = *geometry;
++ if let Some((page, fx, fy, rel)) = view.goto_point.take() {
++ let r = rects[page.min(rects.len() - 1)];
++ // Within the content, so an unreachable point never shows a frame scrolled past the end.
++ let (max_x, max_y) = ((content_w - avail.width()).max(0.0), (content_h - avail.height()).max(0.0));
++ if let Some(fx) = fx {
++ scroll = scroll.horizontal_scroll_offset((r.left() + fx * r.width() - rel.x).clamp(0.0, max_x));
++ }
++ if let Some(fy) = fy {
++ scroll = scroll.vertical_scroll_offset((r.top() - y_shift + fy * r.height() - rel.y).clamp(0.0, max_y));
++ }
++ }
++ scroll
++}
++
++/// Draws the document tab at `index`: its notice bars, then its pages (or the Organize grid) in a
++/// scroll area with this frame's zoom, scrolling, tool input, overlays and render requests.
++pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
++ let t = Tokens::get(ui.ctx());
++ let can_paste_key = egui::Id::unique("can-paste-image");
++ let can_paste_image = if ui.input(|i| i.pointer.secondary_pressed()) {
++ let can = app.can_paste_image();
++ ui.data_mut(|d| d.insert_temp(can_paste_key, can));
++ can
++ } else {
++ ui.data(|d| d.get_temp(can_paste_key)).unwrap_or(false)
++ };
++ // Keyboard scrolling applies to this frame's page view only, never later.
++ let key_scroll = std::mem::take(&mut app.views[index].key_scroll);
++ if key_scroll != 0.0 {
++ app.views[index].end_find_following();
++ }
++ // The Search panel closed: its search moves to the find bar.
++ let search_open = app.right == Some(crate::RightPanel::Search);
++ if let Some(f) = app.views[index].find.as_mut()
++ && f.in_panel
++ && !search_open
++ {
++ f.in_panel = false;
++ }
++ if close_requested_tab(app, index) {
++ return;
++ }
++ let Some(doc) = app.session.get(app.views[index].id) else { return };
++ let info = &doc.info;
++ let paper = app.session.reading_colors(app.views[index].id).and_then(|c| c.params).map_or(Color32::WHITE, |p| {
++ let [r, g, b] = p.background;
++ Color32::from_rgb(r, g, b)
++ });
++ if info.pages.is_empty() {
++ app.views[index].auto_scroll.cancel();
++ ui.centered_and_justified(|ui| ui.label(tl!("This document has no pages.")));
++ return;
++ }
++ // The print dialog queues its own rasters later in the frame. Closing it drops them.
++ if app.dialog != Some(crate::Dialog::Print) {
++ app.views[index].print_pages.clear();
++ }
++ // The Prepare a form panel is open (or a field tool is picked): fields are edited, not filled.
++ let preparing = app.is_preparing();
++ // Prepare a form ▸ Calculation order: the order's steps and links on the page.
++ let calc_order = preparing && app.left_open && app.left == crate::LeftPanel::CalculationOrder;
++ // Edit a PDF: added text and images can be selected, moved and edited.
++ let editing_content =
++ (app.left_open && app.left == crate::LeftPanel::Tool("edit")) || matches!(app.quick_tool, QuickTool::AddText | QuickTool::EditText);
++ let text_style = app.text_style.clone();
++ let show_secured_notice = app.show_secured_notice;
++ let view = &mut app.views[index];
++ // Opened without the owner password and something is restricted. The notice says so unless
++ // the user turned it off (#4035): the restrictions themselves, and Document Properties ▸
++ // Security, stay as they are.
++ let secured = show_secured_notice && doc.security_summary().is_some_and(|s| !(s.owner || (s.permissions.modify() && s.permissions.assemble())));
++ let repaired = !doc.repair_log().is_empty();
++ let notice = notices(view, doc, secured, repaired, crate::sign_ui::banner(&doc.signatures), ui, &t);
++ apply_notice(app, index, notice);
++ let Some(doc) = app.session.get(app.views[index].id) else { return };
++ let info = &doc.info;
++ // No dialog, close prompt or palette over the page: only then does page input count.
++ let modal = app.modal_open();
++ let unobstructed = !modal && !app.palette_open;
++ let organize_zoom = app.organize_zoom;
++ let view = &mut app.views[index];
++ if view.organize {
++ // One remembered grid size for every document, kept in the settings.
++ view.set_grid_zoom(organize_zoom);
++ organize_grid(view, info, doc.allows_assembly(), doc.dirty, unobstructed, ui, &t);
++ app.organize_zoom = view.grid_zoom();
++ return;
++ }
++
++ let avail = ui.available_rect_before_wrap();
++ view.viewport_w = avail.width();
++ view.viewport_h = avail.height();
++ let old_zoom = view.zoom;
++ view.fit_zoom(info);
++ anchor_zoom_change(view, avail.width(), old_zoom);
++
++ // Pinch / ctrl+scroll zoom, anchored on the current page.
++ let zoom_delta = crate::motion::zoom_delta(ui.ctx());
++ // Exclude the previous frame's scrollbar tracks from gesture activation.
++ let navigation_rect = if view.viewport_screen.is_positive() { avail.intersect(view.viewport_screen) } else { avail };
++ let navigation_enabled = app.dialog.is_none() && app.close_request.is_none() && !app.palette_open && ui.is_enabled() && ui.input(|i| i.focused);
++ let present = presentation_click_state(ui, view, navigation_rect, navigation_enabled && app.quick_tool == QuickTool::Select);
++ pinch_zoom(ui, view, zoom_delta, navigation_rect, navigation_enabled);
++ view.viewport_screen = avail;
++ let (auto_delta, edge_delta) = auto_scroll_deltas(ui, view, avail, unobstructed);
++
++ let content_w = view.content_width(info, avail.width());
++ let rects = view.layout(info, content_w, ui.ctx().pixels_per_point());
++ // When not scrolling continuously, only one row is shown at a time: the current page, or the
++ // current two-page spread (with the cover rule). These indices drive paging, which pages are
++ // drawn, and the content height (#752). Empty for a document with no pages.
++ let row = shown_row(view, &rects);
++ let middle_gesture = view.auto_scroll.blocks_input();
++ // Not scrolling: when the whole page or spread fits, the wheel would do nothing, so it turns
++ // pages instead; zoomed in far enough to pan, it pans, and at the edge it turns the page on
++ // (#1924, `single_page_wheel_edge`). Touch drags are untouched: with
++ // nothing to pan, touch users turn pages with the rail buttons, the page box and the arrow
++ // keys. Runs before `visible_pages` so the frame that turns the page draws it.
++ let (single_paging, edge_turned) = single_page_paging(ui, view, &PagingCx { row: &row, rects: &rects, avail, middle_gesture, unobstructed });
++ // Point input bypasses egui smoothing for 1:1 motion. Line input keeps egui's configured,
++ // smoothed base and adds only the accelerated surplus. While point suppression is active,
++ // reconstruct line and page input here so current coarse events survive without exposing an
++ // older eased tail.
++ let now = ui.input(|i| i.time);
++ let Wheel { precise_scroll, line_base, boost_extra, has_page, paging_owns_vertical } = read_wheel(ui, view, avail, single_paging, edge_turned);
++ let animates = app.animation_effects.animates(app.motion.reduced);
++ let may_glide = !paging_owns_vertical && !middle_gesture && navigation_enabled && unobstructed && animates;
++ let wheel_scrolled = precise_scroll != Vec2::ZERO || line_base != Vec2::ZERO || boost_extra != Vec2::ZERO || has_page;
++ let motion = GlideCx { now, may_glide, key_scroll, auto_delta, zoom_delta, edge_delta, wheel_scrolled };
++ let glide = step_glide(ui, view, avail, &motion);
++ // The wheel may have turned the page above: draw the row it turned to. Measured from the old
++ // row, the jump to the new page scrolled both out of view, a blank frame on every turn forward.
++ let row = shown_row(view, &rects);
++ let visible_pages: Vec<usize> = if view.continuous { (0..rects.len()).collect() } else { row.clone() };
++ let (y_shift, content_h) = if view.continuous {
++ (0.0, rects.iter().map(|r| r.bottom()).reduce(f32::max).map(|bottom| bottom + MARGIN).unwrap_or(0.0))
++ } else {
++ // Only the current row scrolls into view; the content is its bounding box plus margins.
++ match row.iter().filter_map(|&i| rects.get(i)).copied().reduce(Rect::union) {
++ Some(r) => (r.top() - MARGIN, r.height() + 2.0 * MARGIN),
++ None => (0.0, 0.0),
++ }
++ };
++
++ let mut scroll = page_scroll_area(
++ ui,
++ view,
++ &ScrollGeometry { rects: &rects, y_shift, content_w, content_h, avail, middle_gesture, hand: app.quick_tool == QuickTool::Hand },
++ );
++ let ppp = ui.ctx().pixels_per_point();
++ let scale = view.render_scale(ppp);
++ let tag = scale_tag(scale);
++ // Hold sharp re-renders while the zoom is still moving: a new scale every frame would
++ // cancel the in-flight full-page interpret and restart it, so on a heavy page (a CAD
++ // plot with ~1000 images renders for seconds) the sharp raster never arrives and the
++ // view looks stuck on a soft image. The stale raster stays stretched meanwhile; the
++ // sharp request goes out once the zoom settles.
++ let sharp_held = hold_sharp_renders(ui, view, scale);
++ view.current_scale_tag = tag;
++ let hand = app.quick_tool == QuickTool::Hand;
++ let tool = app.quick_tool;
++ // Text selection runs for the Select tool and for the markup tools (highlight…).
++ let selects_text = tool_selects_text(tool, preparing);
++ let prefs = &app.comment_prefs;
++ let redact_mark = (app.redact_prefs.hover_fill, app.redact_prefs.opacity);
++ let units = app.units;
++ let allowed = doc.allows_annotation();
++ let can_copy = doc.allows_copying();
++ let comments_hidden = doc.comments_hidden();
++ let form = doc.form.clone();
++ let generation = doc.edit_generation();
++ let can_fill = doc.allows_form_filling();
++ let can_crop = doc.allows_assembly();
++ let signature = app.signature.clone();
++ let initials = app.initials.clone();
++ let author = app.comment_prefs.author.clone();
++ let today = app.session.today();
++ // In Preferences ▸ Date format, or why it can't be stamped into the PDF.
++ let date_text = crate::date_text_for_pdf(&app.session);
++ let by_line = app.session.stamp_by_line(&author);
++ let custom_stamp = match app.quick_tool {
++ QuickTool::CustomStamp(i) => app.custom_stamps.get(i).cloned(),
++ _ => None,
++ };
++ let mut open_props: Option<(usize, usize)> = None;
++ // The table being set up in its window, shown on its own document's page.
++ let table_preview = app.field_table_draft.as_mut().filter(|d| d.doc == view.id).and_then(|d| Some(d.preview(info.pages.get(d.page)?, &form)));
++ let can_modify = doc.allows_modification();
++ if preparing {
++ crate::prepare::after_refresh(view, &form);
++ } else {
++ view.prepare.selected = None;
++ }
++ view.objects.prepare(doc, tool == QuickTool::EditText && can_modify, ui.ctx());
++ view.text_background.prepare(ui.ctx(), doc, view.line_editor.as_ref(), view.content.draft.as_ref(), scale);
++ let added = if editing_content { doc.added().to_vec() } else { Vec::new() };
++ let doc_links = if tool == QuickTool::Link { doc.links().to_vec() } else { Vec::new() };
++ if editing_content {
++ crate::content_ui::after_refresh(view, &added);
++ } else {
++ view.content.selected = None;
++ }
++ view.move_background.prepare(ui.ctx(), doc, &view.content, scale);
++
++ scroll = refine_goto_point(scroll, view, &ScrollGeometry { rects: &rects, y_shift, content_w, content_h, avail, middle_gesture, hand });
++ let selection_wheel = view.selection_drag && navigation_enabled && unobstructed && !single_paging && ui.rect_contains_pointer(avail);
++ let env = PageEnv {
++ added: &added,
++ allowed,
++ author: &author,
++ auto_delta,
++ avail,
++ boost_extra,
++ by_line: &by_line,
++ calc_order,
++ can_copy,
++ can_crop,
++ can_fill,
++ can_modify,
++ comments_hidden,
++ content_h,
++ content_w,
++ custom_stamp: &custom_stamp,
++ date_text: &date_text,
++ doc,
++ doc_links: &doc_links,
++ edge_delta,
++ editing_content,
++ form: &form,
++ generation,
++ glide,
++ hand,
++ info,
++ initials: &initials,
++ key_scroll,
++ middle_gesture,
++ modal,
++ navigation_enabled,
++ paper,
++ ppp,
++ precise_scroll,
++ prefs,
++ preparing,
++ rects: &rects,
++ redact_mark,
++ scale,
++ selection_wheel,
++ selects_text,
++ signature: &signature,
++ single_paging,
++ t: &t,
++ table_preview: &table_preview,
++ tag,
++ text_style: &text_style,
++ tool,
++ units,
++ unobstructed,
++ visible_pages: &visible_pages,
++ y_shift,
++ fill_text_size: app.fill_text_size,
++ fill_text_color: app.fill_text_color,
++ today,
++ can_paste_image,
++ };
++ let mut page_out = PageOut::default();
++ let out = page_scroll(ui, scroll, egui::Id::unique(("page-view-scope", view.id)), |ui, viewport| {
++ draw_pages(ui, viewport, view, &env, &mut page_out, &mut app.signature_preview)
++ });
++ let PageOut {
++ canvas_action,
++ clicked_link,
++ mut copy_selected,
++ field_menu,
++ field_placed,
++ mut field_props,
++ hover_text,
++ mut image_action,
++ open_boxes,
++ open_initials,
++ open_signature,
++ open_table,
++ page_action,
++ primary_down,
++ mut refused,
++ stamp_placed,
++ } = page_out;
++ view.single_edges = visible_pages.first().copied().filter(|_| !view.continuous).map(|page| {
++ // At the top once the page's top edge shows where going to a page puts it (a gap
++ // below the window's top). Within a point, as for wheel paging, so layout rounding
++ // can't hide an edge.
++ let max_y = (out.content_size.y - out.inner_rect.height()).max(0.0);
++ (page, out.state.clamped_offset().y <= MARGIN - view.page_gap() + 1.0, out.state.clamped_offset().y >= max_y - 1.0)
++ });
++
++ view.viewport_screen = out.inner_rect;
++ view.auto_scroll.paint(ui, avail);
++ if unobstructed && matches!(tool, QuickTool::Select | QuickTool::ColumnSelect) {
++ copy_selected |= selection_actions(ui, avail, view, info, prefs, allowed && !comments_hidden, can_copy);
++ }
++
++ // Collect all main-page demand, including cached surfaces, for byte admission later.
++ // Pan-settle debounce for tiled pages: each tile costs a full page interpret, so holding
++ // fresh tiles back while the scroll offset is still moving keeps a drag on the stretched
++ // backdrop/older tiles instead of queueing a storm of interprets (one per frame) that
++ // the workers cannot cancel mid-operator. Fresh tiles go out once the pan settles;
++ // pages with nothing on screen yet still queue.
++ let pan_held = track_scroll(ui, view, &out, glide);
++ plan_renders(ui, view, info, out.inner, &RenderPlan { visible_pages: &visible_pages, tag, scale, sharp_held, pan_held, hand });
++
++ let text_done = frame_overlays(ui, view, &env, primary_down, hover_text);
++ if let Some(why) = view.objects.notice.take() {
++ refused = Some(why);
++ }
++ let form_notice = view.forms.notice.take();
++ // One crop, then back to selecting (as Acrobat does).
++ let cropped = view.pending_edit.as_ref().is_some_and(|e| matches!(e, pdfcraft_engine::Edit::SetPageBox { .. }));
++ let mut tool = app.quick_tool;
++ comments::keys(ui.ctx(), view, &mut tool, allowed);
++ if preparing {
++ crate::prepare::keys(ui.ctx(), view);
++ }
++ if editing_content {
++ crate::content_ui::keys(ui.ctx(), view);
++ }
++ if tool == QuickTool::Link {
++ crate::link_ui::keys(ui.ctx(), view);
++ } else {
++ view.links.selected = None;
++ }
++ if tool != QuickTool::EditText {
++ view.image_selection = None;
++ }
++ if stamp_placed {
++ tool = QuickTool::Select;
++ }
++ // Text: each click on the page starts a box (#74); Done in the editor goes back to selecting.
++ if text_done && tool == QuickTool::AddText {
++ tool = QuickTool::Select;
++ }
++ // One field, then back to selecting (Acrobat's default without "Keep tools pinned").
++ if field_placed {
++ tool = QuickTool::Select;
++ }
++ if matches!(field_menu, Some(FieldMenu::Properties)) {
++ field_props = true;
++ }
++ let field_props = field_props.then(|| view.prepare.selected.clone()).flatten();
++ match canvas_action {
++ Some(comments::CanvasAction::Edit(e)) => view.pending_edit = Some(*e),
++ // `choose_right_panel` would borrow all of `app` while `view` is held; same effect.
++ Some(comments::CanvasAction::OpenComments) => {
++ app.right = Some(RightPanel::Comments);
++ app.comments_panel_closed = false;
++ }
++ Some(comments::CanvasAction::Properties(p, i)) => open_props = Some((p, i)),
++ Some(comments::CanvasAction::CopyImage(p, i)) => image_action = Some(crate::edit_text_ui::ImageAction::Copy(p, i)),
++ Some(comments::CanvasAction::CopyImageFile(p, i)) => image_action = Some(crate::edit_text_ui::ImageAction::CopyFile(p, i)),
++ None => {}
++ }
++ schedule_presentation_turn(ui, view, present, clicked_link.is_some());
++ let remote_open = follow_link(app, index, clicked_link);
++ if tool == QuickTool::Crop && cropped {
++ tool = QuickTool::Select;
++ }
++ let tool = finish_sign_requests(app, index, tool);
++ app.quick_tool = tool;
++ let requests = FrameRequests {
++ open_props,
++ field_props,
++ field_menu,
++ image_action,
++ page_action,
++ refused,
++ open_signature,
++ open_initials,
++ open_boxes,
++ open_table,
++ form_notice,
++ copy_selected,
++ remote_open,
++ form,
++ generation,
++ avail,
++ };
++ apply_frame_requests(app, index, ui, requests);
++}
++
++/// What the frame's input asked the app to do, once the document is no longer borrowed.
++struct FrameRequests {
++ open_props: Option<(usize, usize)>,
++ field_props: Option<(String, usize)>,
++ field_menu: Option<FieldMenu>,
++ image_action: Option<crate::edit_text_ui::ImageAction>,
++ page_action: Option<PageMenuAction>,
++ refused: Option<String>,
++ open_signature: bool,
++ open_initials: bool,
++ open_boxes: bool,
++ open_table: Option<(usize, [f64; 4])>,
++ form_notice: Option<crate::forms_ui::FormNotice>,
++ copy_selected: bool,
++ remote_open: Option<(pdfcraft_engine::DocId, String, pdfcraft_render::RemoteDestination)>,
++ form: Arc<Vec<pdfcraft_engine::FormField>>,
++ generation: u64,
++ avail: Rect,
++}
++
++/// Opens the dialogs and runs the app commands the frame asked for.
++#[allow(clippy::cognitive_complexity)]
++fn apply_frame_requests(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui, requests: FrameRequests) {
++ let FrameRequests {
++ open_props,
++ field_props,
++ field_menu,
++ image_action,
++ page_action,
++ refused,
++ open_signature,
++ open_initials,
++ open_boxes,
++ open_table,
++ form_notice,
++ copy_selected,
++ remote_open,
++ form,
++ generation,
++ avail,
++ } = requests;
++ if let Some((page, at)) = app.views[index].comments.attach_at.take() {
++ app.attach_file_comment(page, at);
++ }
++ if let Some((p, i)) = open_props {
++ app.open_comment_props(p, i);
++ }
++ if let Some((p, i)) = app.views[index].comments.default_request.take() {
++ app.make_comment_default(p, i);
++ }
++ if let Some((p, i, open)) = app.views[index].comments.attachment_request.take() {
++ let doc = app.views[index].id;
++ app.comment_attachment_action(doc, p, i, open);
++ }
++ if let Some((name, w)) = field_props {
++ app.open_field_props(&name, w);
++ }
++ if let Some(FieldMenu::Duplicate(name)) = field_menu {
++ let pages = app.session.get(app.views[index].id).map_or(1, |d| d.info.pages.len());
++ app.duplicate_draft = Some(crate::DuplicateDraft { name, all: true, from: 1, to: pages });
++ app.dialog = Some(crate::Dialog::DuplicateField);
++ }
++ if let Some((page, rect)) = app.views[index].links.open_new.take() {
++ app.open_link_props(page, Some(rect), None);
++ }
++ if let Some((page, i)) = app.views[index].links.open_existing.take() {
++ app.open_link_props(page, None, Some(i));
++ }
++ if let Some((page, quads)) = app.views[index].pending_redaction.take() {
++ let author = app.comment_prefs.author.clone();
++ app.views[index].pending_edit = Some(if app.quick_tool == QuickTool::Comment(comments::CommentTool::Highlight) {
++ // An area highlight: a highlight over the box.
++ let style = app.comment_prefs.style(comments::CommentTool::Highlight);
++ pdfcraft_engine::Edit::AddAnnotation(pdfcraft_engine::NewAnnotation {
++ page,
++ shape: pdfcraft_engine::Shape::TextMarkup { kind: pdfcraft_engine::Markup::Highlight, quads },
++ style,
++ contents: String::new(),
++ author,
++ })
++ } else {
++ app.redact_prefs.mark(page, quads, &author)
++ });
++ }
++ match image_action {
++ Some(crate::edit_text_ui::ImageAction::Replace(page, index)) => app.replace_page_image_dialog(page, index),
++ Some(crate::edit_text_ui::ImageAction::Save(page, index)) => app.save_page_image(page, index),
++ Some(crate::edit_text_ui::ImageAction::Copy(page, index)) => {
++ app.copy_page_image(page, index);
++ }
++ Some(crate::edit_text_ui::ImageAction::CopyFile(page, index)) => app.copy_page_image_file(page, index),
++ Some(crate::edit_text_ui::ImageAction::Cut(page, image)) => {
++ app.views[index].image_selection = None;
++ app.cut_page_image(page, image);
++ }
++ Some(crate::edit_text_ui::ImageAction::Paste(page, at)) => {
++ if let Some(pt) = at {
++ app.paste_image_at(page, pt);
++ } else {
++ app.paste_image(page, None);
++ }
++ }
++ Some(crate::edit_text_ui::ImageAction::DrawFreehand) => {
++ app.quick_tool = crate::QuickTool::Comment(crate::comments::CommentTool::Ink);
++ }
++ Some(crate::edit_text_ui::ImageAction::Redact(page, index)) => app.redact_page_image(page, index),
++ Some(crate::edit_text_ui::ImageAction::Link(page, index)) => app.link_page_image(page, index),
++ Some(crate::edit_text_ui::ImageAction::Recognize(page)) => app.recognize_text_on_page(page),
++ Some(crate::edit_text_ui::ImageAction::AddComment(page, i)) => {
++ if let Some(d) = app.session.get(app.views[index].id) {
++ let images = d.page_images(page);
++ if let Some(img) = images.get(i) {
++ let r = img.visible_rect.unwrap_or(img.rect);
++ let center = [(r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0];
++ app.views[index].comments.context_at = Some((page, center));
++ app.views[index].comments.composer = Some(crate::comments::Composer {
++ page,
++ at: center,
++ kind: crate::comments::ComposerKind::Note,
++ text: String::new(),
++ focus: true,
++ });
++ }
++ }
++ }
++ Some(crate::edit_text_ui::ImageAction::Crop) => {
++ let doc_id = app.views[index].id;
++ if let Some(s) = app.views[index].image_selection.as_mut()
++ && let Some(d) = app.session.get(doc_id)
++ {
++ let images = d.page_images(s.page);
++ if let Some(im) = images.get(s.index) {
++ s.crop = Some(crate::edit_text_ui::CropMode::new(im.visible_rect.unwrap_or(im.rect)));
++ s.crop_mode = true;
++ s.crop_rect = None;
++ }
++ }
++ }
++ None => {}
++ }
++ match page_action {
++ Some(PageMenuAction::Copy) => {
++ if let Some(sel) = app.views[index].image_selection.clone() {
++ app.copy_page_image(sel.page, sel.index);
++ } else if app.views[index].selected_text().is_some() {
++ app.execute("edit.copy");
++ } else if let Some((page, at)) = app.views[index].comments.context_at
++ && let Some(doc) = app.session.get(app.views[index].id)
++ && let Some(image) = doc.page_image_at(page, at)
++ {
++ app.copy_page_image(page, image);
++ }
++ }
++ Some(PageMenuAction::Paste(page, at)) => {
++ app.paste_image_at(page, at);
++ }
++ Some(PageMenuAction::RotateCCW(page)) => {
++ let _ = page;
++ app.views[index].rotate_view(false);
++ }
++ Some(PageMenuAction::RotateCW(page)) => {
++ let _ = page;
++ app.views[index].rotate_view(true);
++ }
++ Some(PageMenuAction::Print) => {
++ app.open_print();
++ }
++ Some(PageMenuAction::SelectTool) => {
++ app.quick_tool = QuickTool::Select;
++ }
++ Some(PageMenuAction::HandTool) => {
++ app.quick_tool = QuickTool::Hand;
++ }
++ Some(PageMenuAction::MarqueeZoom) => {
++ app.quick_tool = QuickTool::MarqueeZoom;
++ }
++ Some(PageMenuAction::AddComment(page, at)) => {
++ app.views[index].comments.composer =
++ Some(crate::comments::Composer { page, at, kind: crate::comments::ComposerKind::Note, text: String::new(), focus: true });
++ }
++ Some(PageMenuAction::FlattenComments) => {
++ app.views[index].comments.selected = None;
++ app.views[index].pending_edit = Some(pdfcraft_engine::Edit::Flatten { comments: true, fields: false });
++ }
++ Some(PageMenuAction::AddBookmark(_page)) => {
++ app.bookmark_action(crate::panels::BmAction::New);
++ }
++ Some(PageMenuAction::TakeSnapshot) => {
++ app.quick_tool = QuickTool::Snapshot;
++ }
++ Some(PageMenuAction::EditPdf) => {
++ app.quick_tool = QuickTool::EditText;
++ }
++ Some(PageMenuAction::AddText) => {
++ app.quick_tool = QuickTool::AddText;
++ }
++ Some(PageMenuAction::AddImage) => {
++ app.add_image_dialog();
++ }
++ Some(PageMenuAction::DeletePage(page)) => {
++ app.views[index].pending_edit = Some(pdfcraft_engine::Edit::DeletePages { pages: vec![page] });
++ }
++ Some(PageMenuAction::ExportPdf) => {
++ app.select_tool("export");
++ }
++ Some(PageMenuAction::Find) => {
++ app.views[index].open_find();
++ }
++ Some(PageMenuAction::DocProperties) => {
++ app.dialog = Some(crate::Dialog::Properties(crate::PropsTab::Description));
++ }
++ None => {}
++ }
++ if let Some(why) = refused {
++ app.notify_error(why);
++ }
++ if open_signature || open_initials {
++ app.signature_draft = crate::fill_sign::SigDraft::new(open_initials, &app.comment_prefs.author);
++ app.dialog = Some(crate::Dialog::Signature);
++ }
++ if open_boxes {
++ app.boxes_draft.range = crate::pageboxes::Range::Current;
++ app.boxes_draft.seeded = None;
++ app.dialog = Some(crate::Dialog::PageBoxes);
++ }
++ if let Some((page, rect)) = open_table {
++ app.field_table_draft = Some(crate::field_table_ui::FieldTableDraft::new(app.views[index].id, page, rect, &form, generation));
++ app.dialog = Some(crate::Dialog::FieldTable);
++ // The window lasts while the form is prepared. Drawn with the tool picked but its panel
++ // closed, the tool goes back to Select above, so the panel opens to keep it prepared.
++ app.left = crate::LeftPanel::Tool("form");
++ app.reveal_tools();
++ }
++ if let Some(n) = form_notice {
++ match n {
++ crate::forms_ui::FormNotice::Security => app.notify_tr("The document's security settings don't allow filling in form fields"),
++ crate::forms_ui::FormNotice::ReadOnly(name) => app.notify_fmt("{name} is read-only", &[("name", &name)]),
++ crate::forms_ui::FormNotice::NoAction(name) => app.notify_fmt("{name} has no action", &[("name", &name)]),
++ }
++ }
++ if let Some((name, action)) = app.views[index].forms.button.take() {
++ run_button(app, index, &name, action);
++ }
++ if copy_selected {
++ app.execute("edit.copy");
++ }
++ // Read mode hides the tools (#2646), and full screen is the page and nothing else (#1459);
++ // the quick tools come back with the window.
++ if app.mode != crate::Mode::Read && !app.full_screen {
++ quick_bar(app, avail, ui);
++ }
++ if let Some((source, file, destination)) = remote_open {
++ app.open_remote_link(source, file, destination);
++ }
++}
++
++/// The prepare-mode field menu's choices.
++enum FieldMenu {
++ Properties,
++ Duplicate(String),
++}
++
++pub use crate::image_engine::{HandleId, MIN_RECT_SIZE, apply_crop, apply_resize, apply_resize_proportional, reset_crop};
++#[cfg(test)]
++mod tests;
diff --cc crates/ui-egui/src/dialogs.rs
index 059e54c26,bfb0da686..fc6f0d492
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@@ -120,1405 -128,1490 +128,1599 @@@ fn dialogs(app: &mut PdfCraftApp, ctx:
Dialog::Print => 820.0,
Dialog::FieldProps | Dialog::BulkFieldProps => 600.0,
Dialog::About => 780.0,
+ Dialog::PinTool => 540.0,
_ => 520.0,
});
- match dialog {
- Dialog::WordCount => {
- close = crate::word_count_ui::body(ui, app);
- }
- Dialog::CreateImages => {
- let (go, cancel) = crate::create_ui::image_import_body(ui, app);
- images_now = go;
- close = go || cancel;
- return;
- }
- Dialog::Properties(tab) => {
- ui.label(egui::RichText::new(tl!("Document Properties")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- ui.horizontal_wrapped(|ui| {
- for (tb, label) in [
- (PropsTab::Description, tl!("Description")),
- (PropsTab::InitialView, tl!("Initial View")),
- (PropsTab::Security, tl!("Security")),
- (PropsTab::Fonts, tl!("Fonts")),
- (PropsTab::Custom, tl!("Custom")),
- (PropsTab::Advanced, tl!("Advanced")),
- (PropsTab::SpaceAudit, tl!("Space Audit")),
- ] {
- if widgets::mode_tab(ui, label, tab == tb).clicked() {
- next = Dialog::Properties(tb);
- }
- }
- });
- ui.separator();
- let units = app.units;
- let Some((_, id)) = app.active_ids() else { return };
- let Some(doc) = app.session.get(id) else { return };
- let i = &doc.info;
- // Description shows plain values among text fields; they start where the fields' text does.
- let editable_info = tab == PropsTab::Description && doc.allows_modification() && app.props_draft.is_some();
- let inset = if editable_info { widgets::FIELD_TEXT_INSET } else { 0.0 };
- let row = |ui: &mut egui::Ui, k: &str, v: String| {
- // Row labels are UI chrome; values are document data and stay as they are.
- ui.label(egui::RichText::new(tl!(k)).color(t.text_muted));
- ui.horizontal(|ui| {
- ui.add_space(inset);
- ui.add(
- egui::Label::new(if v.is_empty() { egui::RichText::new("—").color(t.text_faint) } else { egui::RichText::new(v) }).wrap(),
- );
- });
- ui.end_row();
- };
- // A file name or path is read left to right even in an RTL UI: the extension
- // stays at its visual end, as in the tab strip.
- let path_row = |ui: &mut egui::Ui, k: &str, v: String| {
- ui.label(egui::RichText::new(tl!(k)).color(t.text_muted));
- ui.scope(|ui| {
- ui.set_max_width(420.0);
- // Align path text with editable fields while keeping its LTR text order.
- ui.add_space(inset);
- if v.is_empty() {
- ui.label(egui::RichText::new("—").color(t.text_faint));
- } else {
- crate::widgets::label_ltr(ui, v, crate::theme::regular(13.0), t.text);
- }
- });
- ui.end_row();
- };
- egui::ScrollArea::vertical().max_height(460.0).auto_shrink([false, true]).show(ui, |ui| {
- if tab == PropsTab::SpaceAudit {
- ui.add_space(4.0);
- crate::optimize_ui::audit_table(ui, &app.space_audit, &t);
- return;
- }
- egui::Grid::new("props").num_columns(2).spacing([18.0, 8.0]).min_col_width(140.0).show(ui, |ui| match tab {
- PropsTab::Description => {
- path_row(ui, "File", doc.name.clone());
- match app.props_draft.as_mut() {
- Some((_, draft)) if editable_info => {
- for (k, v) in INFO_KEYS.iter().zip(draft.iter_mut()) {
- let l = ui.label(egui::RichText::new(tl!(k)).color(t.text_muted));
- // Subject and Keywords are often a sentence or a list: they wrap and grow
- // instead of hiding most of the value past the field's edge.
- let field =
- if matches!(*k, "Subject" | "Keywords") { widgets::block(v).desired_rows(1) } else { widgets::line(v) };
- ui.add(field.desired_width(f32::INFINITY).background_color(t.field).id_salt(("info", *k))).labelled_by(l.id);
- ui.end_row();
- }
- }
- _ => {
- row(ui, "Title", i.title.clone().unwrap_or_default());
- row(ui, "Author", i.author.clone().unwrap_or_default());
- row(ui, "Subject", i.subject.clone().unwrap_or_default());
- row(ui, "Keywords", i.keywords.clone().unwrap_or_default());
- }
- }
- row(ui, "Application", i.creator.clone().unwrap_or_default());
- row(ui, "PDF producer", i.producer.clone().unwrap_or_default());
- }
- PropsTab::InitialView => {
- use pdfcraft_engine::{InitialLayout as L, Magnification as M, Navigation as N};
- let editable = doc.allows_modification();
- let pages = i.pages.len();
- let Some((_, v)) = app.view_draft.as_mut() else { return };
- ui.label(egui::RichText::new(tl!("Layout and Magnification")).font(theme::semibold(12.5)));
- ui.end_row();
- let label = ui.label(tl!("Navigation tab"));
- ui.add_enabled_ui(editable, |ui| {
- egui::ComboBox::from_id_salt("iv-nav")
- .selected_text(tl!(format!("{:?}", v.navigation)
- .replace("PageOnly", "Page Only")
- .replace("Pages", "Pages Panel and Page")
- .as_str()))
- .show_ui(ui, |ui| {
- for (n, l) in [
- (N::PageOnly, tl!("Page Only")),
- (N::Bookmarks, tl!("Bookmarks Panel and Page")),
- (N::Pages, tl!("Pages Panel and Page")),
- (N::Attachments, tl!("Attachments Panel and Page")),
- (N::Layers, tl!("Layers Panel and Page")),
- ] {
- ui.selectable_value(&mut v.navigation, n, l);
- }
- })
- .response
- .labelled_by(label.id);
- });
- ui.end_row();
- let label = ui.label(tl!("Page layout"));
- ui.add_enabled_ui(editable, |ui| {
- let names = [
- (L::Default, tl!("Default")),
- (L::SinglePage, tl!("Single Page")),
- (L::SinglePageContinuous, tl!("Single Page Continuous")),
- (L::TwoUp, tl!("Two-Up (Facing)")),
- (L::TwoUpContinuous, tl!("Two-Up Continuous (Facing)")),
- (L::TwoUpCoverPage, tl!("Two-Up (Cover Page)")),
- (L::TwoUpContinuousCoverPage, tl!("Two-Up Continuous (Cover Page)")),
- ];
- let shown = names.iter().find(|(l, _)| *l == v.layout).map_or(tl!("Default"), |(_, n)| n);
- egui::ComboBox::from_id_salt("iv-layout")
- .selected_text(shown)
- .show_ui(ui, |ui| {
- for (l, n) in names {
- ui.selectable_value(&mut v.layout, l, n);
- }
- })
- .response
- .labelled_by(label.id);
- });
- ui.end_row();
- let label = ui.label(tl!("Magnification"));
- ui.add_enabled_ui(editable, |ui| {
- ui.horizontal(|ui| {
- let names = [
- (M::Default, tl!("Default")),
- (M::ActualSize, tl!("Actual Size")),
- (M::FitPage, tl!("Fit Page")),
- (M::FitWidth, tl!("Fit Width")),
- (M::FitHeight, tl!("Fit Height")),
- (M::FitVisible, tl!("Fit Visible")),
- ];
- let shown = match v.magnification {
- M::Percent(p) => format!("{p:.0}%"),
- m => names.iter().find(|(x, _)| *x == m).map_or(tl!("Default").to_string(), |(_, n)| n.to_string()),
- };
- egui::ComboBox::from_id_salt("iv-mag")
- .selected_text(shown)
- .show_ui(ui, |ui| {
- for (m, n) in names {
- ui.selectable_value(&mut v.magnification, m, n);
- }
- for p in [50.0, 75.0, 125.0, 150.0, 200.0] {
- ui.selectable_value(&mut v.magnification, M::Percent(p), format!("{p:.0}%"));
- }
- })
- .response
- .labelled_by(label.id);
- });
- });
- ui.end_row();
- let label = ui.label(tl!("Open to page"));
- ui.add_enabled_ui(editable, |ui| {
- let mut p = v.page + 1;
- if ui.add(egui::DragValue::new(&mut p).range(1..=pages.max(1))).labelled_by(label.id).changed() {
- v.page = p - 1;
- }
- ui.label(crate::i18n::fmt(tl!("of {pages}"), &[("pages", &pages.to_string())]));
- });
- ui.end_row();
- ui.label(egui::RichText::new(tl!("Window Options")).font(theme::semibold(12.5)));
- ui.end_row();
- ui.label("");
- ui.add_enabled_ui(editable, |ui| {
- ui.vertical(|ui| {
- ui.add(widgets::checkbox(&mut v.fit_window, tl!("Resize window to initial page")));
- ui.add(widgets::checkbox(&mut v.center_window, tl!("Center window on screen")));
- ui.add(widgets::checkbox(&mut v.full_screen, tl!("Open in Full Screen mode")));
- ui.horizontal(|ui| {
- ui.label(tl!("Show:"));
- widgets::radio_value(ui, &mut v.display_title, false, tl!("File Name"));
- widgets::radio_value(ui, &mut v.display_title, true, tl!("Document Title"));
- });
- });
- });
- ui.end_row();
- ui.label(egui::RichText::new(tl!("User Interface Options")).font(theme::semibold(12.5)));
- ui.end_row();
- ui.label("");
- ui.add_enabled_ui(editable, |ui| {
- ui.vertical(|ui| {
- ui.add(widgets::checkbox(&mut v.hide_menubar, tl!("Hide menu bar")));
- ui.add(widgets::checkbox(&mut v.hide_toolbar, tl!("Hide toolbars")));
- ui.add(widgets::checkbox(&mut v.hide_window_ui, tl!("Hide window controls")));
- });
- });
- ui.end_row();
- }
- PropsTab::Security => match doc.security_summary() {
- None => {
- row(ui, "Security method", tl!("No security").to_string());
- row(ui, "Restrictions", tl!("None — everything is allowed").to_string());
- if doc.allows_security_change() && ui.button(tl!("Protect using password…")).clicked() {
- link_command = Some("protect.password");
- }
- }
- Some(sec) => {
- row(ui, "Security method", tl!("Password security").to_string());
- row(ui, "Encryption", sec.method.clone());
- row(
- ui,
- "Opened with",
- if sec.pending {
- tl!("— (protection is applied when you save)").to_string()
- } else if sec.owner {
- tl!("Owner password (no restrictions apply)").to_string()
- } else {
- tl!("User password").to_string()
- },
- );
- // A restricted document asks for its permissions password first.
- if doc.allows_security_change() || doc.can_unlock() {
- ui.horizontal(|ui| {
- if doc.can_unlock() && ui.button(tl!("Unlock…")).clicked() {
- link_command = Some("protect.unlock");
- }
- if ui.button(tl!("Change settings…")).clicked() {
- link_command = Some("protect.password");
- }
- if ui.button(tl!("Remove security")).clicked() {
- link_command = Some("protect.remove");
- }
- });
- }
- let p = sec.permissions;
- let yes = |b: bool| tl!(if b { "Allowed" } else { "Not allowed" }).to_string();
- row(
- ui,
- "Printing",
- if !p.print() {
- tl!("Not allowed").to_string()
- } else if p.print_high_quality() {
- tl!("High resolution").to_string()
- } else {
- tl!("Low resolution").to_string()
- },
- );
- row(ui, "Changing the document", yes(p.modify()));
- row(ui, "Document assembly", yes(p.assemble()));
- row(ui, "Content copying", yes(p.copy()));
- row(ui, "Content copying for accessibility", yes(p.extract_for_accessibility()));
- row(ui, "Commenting", yes(p.annotate()));
- row(ui, "Filling of form fields", yes(p.fill_forms()));
- }
- },
- PropsTab::Fonts => {
- if i.fonts.is_empty() {
- row(ui, "Fonts", tl!("No fonts are referenced by the pages.").to_string());
- }
- for f in &i.fonts {
- let mut detail = f.kind.clone();
- if let Some(e) = &f.encoding {
- detail.push_str(&format!(" · {e}"));
- }
- detail.push_str(if f.subset {
- " · Embedded subset"
- } else if f.embedded {
- " · Embedded"
- } else {
- " · Not embedded (substituted)"
- });
- row(ui, &f.name, detail);
- }
- }
- PropsTab::Custom => {
- // Add takes a new name, Update changes the value of the listed property the
- // Name field names, Delete removes it; OK writes the changes as one step.
- let editable = doc.allows_modification();
- let Some((_, d)) = app.custom_draft.as_mut() else { return };
- let l = ui.label(egui::RichText::new(tl!("Name:")).color(t.text_muted));
- ui.add_enabled(
- editable,
- widgets::line(&mut d.name).desired_width(420.0).background_color(t.field).id_salt("custom-name"),
- )
- .labelled_by(l.id);
- ui.end_row();
- let l = ui.label(egui::RichText::new(tl!("Value:")).color(t.text_muted));
- ui.add_enabled(
- editable,
- widgets::line(&mut d.value).desired_width(420.0).background_color(t.field).id_salt("custom-value"),
- )
- .labelled_by(l.id);
- ui.end_row();
- // Untrimmed: a file's `/Project#20` is not `/Project`, and a typed space is refused.
- let name = d.name.clone();
- let value = d.value.trim().to_owned();
- let listed = d.props.iter().position(|(k, _)| *k == name);
- // An entry the list leaves out (not text, only padding, or past its 256) keeps its value.
- let file_props = doc.custom_info();
- let unlisted = doc.has_info(&name) && !file_props.iter().any(|(k, _)| *k == name);
- let refused = pdfcraft_engine::check_custom_key(&name).err().filter(|_| !name.is_empty()).map(|e| e.to_string());
- ui.label("");
- // A file can hold a custom name that `check_custom_key` refuses, such as one
- // with a space: the tab lists it, and Add, Update and Delete stay off for it.
- let why = |b: egui::Response| match &refused {
- Some(why) => b.on_disabled_hover_text(crate::i18n::words(why)),
- None => b,
- };
- // A name that isn't UTF-8 is listed with U+FFFD and can't be written back as it is.
- let editable = editable && refused.is_none() && !name.contains(char::REPLACEMENT_CHARACTER);
- // `custom_info` lists at most 256 properties, and a new name goes after the file's
- // entries, so Add is off once the draft or the file's own list holds 256.
- let room = d.props.len() < 256 && file_props.len() < 256;
- ui.horizontal(|ui| {
- let add = editable && room && listed.is_none() && !unlisted && !name.is_empty() && !value.is_empty();
- if why(ui.add_enabled(add, egui::Button::new(tl!("Add")))).clicked() {
- d.props.push((name.clone(), value.clone()));
- d.name.clear();
- d.value.clear();
- }
- let update = editable && !value.is_empty() && listed.and_then(|n| d.props.get(n)).is_some_and(|(_, v)| *v != value);
- if why(ui.add_enabled(update, egui::Button::new(tl!("Update")))).clicked()
- && let Some(p) = listed.and_then(|n| d.props.get_mut(n))
- {
- p.1 = value.clone();
- }
- if why(ui.add_enabled(editable && listed.is_some(), egui::Button::new(tl!("Delete")))).clicked()
- && let Some(n) = listed.filter(|n| *n < d.props.len())
- {
- d.props.remove(n);
- d.name.clear();
- d.value.clear();
- }
- });
- ui.end_row();
- if d.props.is_empty() {
- ui.label("");
- ui.label(egui::RichText::new(tl!("None")).color(t.text_faint));
- ui.end_row();
- }
- // A click on a property fills the fields with it; values are document data.
- let mut picked = None;
- for (k, v) in &d.props {
- if ui.selectable_label(*k == name, k.as_str()).clicked() {
- picked = Some((k.clone(), v.clone()));
- }
- ui.scope(|ui| {
- ui.set_max_width(420.0);
- ui.add(egui::Label::new(v.as_str()).wrap());
- });
- ui.end_row();
- }
- if let Some((k, v)) = picked {
- d.name = k;
- d.value = v;
- }
- }
- PropsTab::Advanced => {
- row(ui, "PDF version", i.pdf_version.clone());
- path_row(ui, "Location", doc.path.clone().unwrap_or_default());
- row(ui, "File size", format!("{} ({})", human_size(i.file_size), crate::panels::byte_count(i.file_size)));
- let p = &i.pages[0];
- row(ui, "Page size", units.size(f64::from(p.width), f64::from(p.height)));
- row(ui, "Number of pages", i.pages.len().to_string());
- row(ui, "Tagged PDF", yes(i.tagged));
- row(ui, "Form fields", i.fields.len().to_string());
- row(ui, "Comments", i.annotations.len().to_string());
- row(ui, "Layers", i.layers.len().to_string());
- row(ui, "Attachments", i.attachments.len().to_string());
- row(ui, "JavaScript", yes(i.has_javascript));
- // Each incremental update is a revision; earlier ones open as their own document.
- let ends = doc.revision_ends();
- if ends.len() > 1 {
- ui.label(egui::RichText::new(tl!("Revisions")).color(t.text_muted));
- // Reserve the full wrapped height before the grid starts the next row.
- ui.vertical(|ui| {
- ui.horizontal_wrapped(|ui| {
- ui.label(ends.len().to_string());
- for n in (1..ends.len()).rev().take(12) {
- if ui
- .small_button(crate::i18n::fmt(tl!("View revision {n}"), &[("n", &n.to_string())]))
- .on_hover_text(tl!("Open the file as it was saved then"))
- .clicked()
- {
- open_revision = Some(n);
- }
- }
- });
- });
- ui.end_row();
- }
- // Reading Options: binding and language.
- if let Some((_, v)) = app.view_draft.as_mut() {
- let editable = doc.allows_modification();
- ui.label(egui::RichText::new(tl!("Binding")).color(t.text_muted));
- ui.add_enabled_ui(editable, |ui| {
- ui.horizontal(|ui| {
- widgets::radio_value(ui, &mut v.right_to_left, false, tl!("Left Edge"));
- widgets::radio_value(ui, &mut v.right_to_left, true, tl!("Right Edge"));
- });
- });
- ui.end_row();
- let l = ui.label(egui::RichText::new(tl!("Language")).color(t.text_muted));
- let mut lang = v.language.clone().unwrap_or_default();
- if ui
- .add_enabled(editable, widgets::line(&mut lang).hint_text(tl!("e.g. en-US")).desired_width(160.0))
- .labelled_by(l.id)
- .changed()
- {
- v.language = (!lang.trim().is_empty()).then(|| lang.trim().to_string());
- }
- ui.end_row();
- }
- // What was repaired while reading a damaged file (fidelity: never silent).
- let repairs = doc.repair_log();
- row(ui, "Repairs", if repairs.is_empty() { tl!("None").to_string() } else { repairs.len().to_string() });
- if !repairs.is_empty() {
- ui.label("");
- egui::CollapsingHeader::new(tl!("Repair log")).show(ui, |ui| {
- for r in &repairs {
- ui.add(egui::Label::new(egui::RichText::new(r).small()).wrap());
- }
- });
- ui.end_row();
- }
- }
- // Laid out above, outside the two-column grid.
- PropsTab::SpaceAudit => {}
- });
- });
+ if dialog_body(ui, app, dialog, &mut req) {
+ dialog_footer(ui, app, dialog, &mut req);
+ }
+ });
+ finish_dialog(app, dialog, modal.should_close(), &mut req);
+ }
+
+ /// The dialog's content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_body(ui: &mut egui::Ui, app: &mut PdfCraftApp, dialog: Dialog, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ match dialog {
+ Dialog::WordCount => {
+ req.close = crate::word_count_ui::body(ui, app);
+ }
+ Dialog::CreateImages => {
+ let (go, cancel) = crate::create_ui::image_import_body(ui, app);
+ req.images_now = go;
+ req.close = go || cancel;
+ return false;
+ }
+ Dialog::Properties(tab) => {
+ if !dialog_properties(ui, app, req, tab) {
+ return false;
}
- Dialog::Split => {
- ui.label(egui::RichText::new(tl!("Split document")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- let Some((vi, id)) = app.active_ids() else { return };
- let n = app.session.get(id).map(|d| d.info.pages.len()).unwrap_or(0);
- let selected: Vec<usize> = app.views[vi].selected.iter().copied().filter(|p| *p > 0).collect();
- let marks = app.session.bookmark_splits(id);
- let draft = &mut app.split_draft;
- use crate::SplitMode as M;
- if selected.is_empty() && draft.mode == M::Selection {
- draft.mode = M::Pages;
- }
- widgets::radio_value(ui, &mut draft.mode, M::Pages, tl!("Number of pages"));
- ui.add_enabled_ui(draft.mode == M::Pages, |ui| {
- ui.horizontal(|ui| {
- ui.add_space(24.0);
- ui.label(tl!("Pages per file"));
- ui.add(egui::DragValue::new(&mut draft.every).range(1..=n.max(1)));
- });
- });
- widgets::radio_value(ui, &mut draft.mode, M::Size, tl!("File size"));
- ui.add_enabled_ui(draft.mode == M::Size, |ui| {
- ui.horizontal(|ui| {
- ui.add_space(24.0);
- ui.label(tl!("At most"));
- ui.add(egui::DragValue::new(&mut draft.size_mb).range(0.05..=2000.0).speed(0.1).suffix(" MB"));
- });
- });
- ui.add_enabled_ui(!marks.is_empty(), |ui| {
- widgets::radio_value(
- ui,
- &mut draft.mode,
- M::Bookmarks,
- crate::i18n::fmt(tl!("Top-level bookmarks ({n})"), &[("n", &marks.len().to_string())]),
- )
- });
- ui.add_enabled_ui(!selected.is_empty(), |ui| {
- widgets::radio_value(ui, &mut draft.mode, M::Selection, tl!("Before each selected page (select pages in Organize)"))
- });
- let plan = match draft.mode {
- M::Pages => crate::SplitPlan::By(pdfcraft_engine::SplitBy::PageCount(draft.every)),
- M::Selection => crate::SplitPlan::By(pdfcraft_engine::SplitBy::Before(selected)),
- M::Size => crate::SplitPlan::Size((draft.size_mb * 1_048_576.0) as usize),
- M::Bookmarks => crate::SplitPlan::Bookmarks,
- };
- let files = match &plan {
- crate::SplitPlan::By(by) => Some(pdfcraft_engine::split_ranges(n, by).len()),
- crate::SplitPlan::Bookmarks => {
- Some(pdfcraft_engine::split_ranges(n, &pdfcraft_engine::SplitBy::Before(marks.iter().map(|m| m.0).collect())).len())
- }
- crate::SplitPlan::Size(_) => None,
- };
- ui.add_space(8.0);
- let text = match files {
- Some(f) => {
- if f == 1 {
- crate::i18n::fmt(tl!("Creates 1 file from {n} pages."), &[("n", &n.to_string())])
- } else {
- crate::i18n::fmt(tl!("Creates {f} files from {n} pages."), &[("f", &f.to_string()), ("n", &n.to_string())])
- }
- }
- None => crate::i18n::fmt(tl!("Each file holds as many of the {n} pages as fit."), &[("n", &n.to_string())]),
- };
- ui.label(egui::RichText::new(text).color(t.text_muted));
- if files.is_none_or(|f| f > 1) {
- split_ready = Some(plan);
- }
+ }
+ Dialog::Split => {
+ if !dialog_split(ui, app, req) {
+ return false;
}
- Dialog::ReplacePages => {
- let count = app.active_ids().and_then(|(_, id)| app.session.get(id)).map(|d| d.info.pages.len()).unwrap_or(1).max(1);
- let Some(d) = app.replace_draft.as_mut() else {
- close = true;
- return;
- };
- ui.label(egui::RichText::new(tl!("Replace Pages")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- d.to = d.to.clamp(1, count);
- d.from = d.from.clamp(1, d.to);
- ui.horizontal(|ui| {
- let from_label = ui.label(tl!("Original: replace pages"));
- let from = ui.add(egui::DragValue::new(&mut d.from).range(1..=count)).labelled_by(from_label.id);
- let to_label = ui.label(tl!("to"));
- let to = ui.add(egui::DragValue::new(&mut d.to).range(1..=count)).labelled_by(to_label.id);
- if from.changed() || to.changed() {
- d.range_edited = true;
- }
- ui.label(egui::RichText::new(crate::i18n::fmt(tl!("of {name}"), &[("name", &count.to_string())])).color(t.text_muted));
- });
- let n = if d.range_edited { d.to - d.from + 1 } else { d.pages.len().max(1) };
- let max_start = d.src_pages.saturating_sub(n) + 1;
- d.src_from = d.src_from.clamp(1, max_start.max(1));
- ui.horizontal(|ui| {
- let replacement_label = ui.label(crate::i18n::fmt(tl!("Replacement: pages of {name}"), &[("name", &d.name)]));
- ui.add(egui::DragValue::new(&mut d.src_from).range(1..=max_start.max(1))).labelled_by(replacement_label.id);
- ui.label(crate::i18n::fmt(tl!("to {n}"), &[("n", &(d.src_from + n - 1).to_string())]));
- ui.label(egui::RichText::new(crate::i18n::fmt(tl!("of {name}"), &[("name", &d.src_pages.to_string())])).color(t.text_muted));
- });
- let fits = n <= d.src_pages;
- ui.add_space(6.0);
- ui.label(
- egui::RichText::new(if fits {
- tl!("Only the page content changes: links, comments, form fields and bookmarks on the original pages stay.")
- } else {
- tl!("The replacement file doesn't have that many pages.")
- })
- .small()
- .color(t.text_faint),
- );
- ui.add_space(10.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if ui.add_enabled_ui(fits, |ui| widgets::pill_button(ui, tl!("OK"), true)).inner.clicked() {
- replace_now = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ }
+ Dialog::ReplacePages => {
+ if !dialog_replace_pages(ui, app, req) {
+ return false;
}
- Dialog::RedactPages => {
- let n = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
- let (ok, cancel) = crate::redact_ui::pages_body(ui, &mut app.redact_pages_draft, n, &t);
- if ok {
- redact_now = Some(dialog);
- }
- close = ok || cancel;
- return;
+ }
+ Dialog::RedactPages => {
+ let n = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
+ let (ok, cancel) = crate::redact_ui::pages_body(ui, &mut app.redact_pages_draft, n, &t);
+ if ok {
+ req.redact_now = Some(dialog);
}
- Dialog::RedactSearch => {
- let (go, cancel, import) = crate::redact_ui::search_body(ui, &mut app.redact_search, &t);
- if go {
- redact_now = Some(dialog);
- }
- import_words = import;
- close = cancel;
- return;
+ req.close = ok || cancel;
+ return false;
+ }
+ Dialog::RedactSearch => {
+ let (go, cancel, import) = crate::redact_ui::search_body(ui, &mut app.redact_search, &t);
+ if go {
+ req.redact_now = Some(dialog);
}
- Dialog::RedactProps => {
- let mut prefs = app.redact_prefs.clone();
- let (ok, cancel) = crate::redact_ui::props_body(ui, &mut prefs, &t);
- app.redact_prefs = prefs;
- if ok && app.redact_prefs.remember {
- app.redact_defaults = app.redact_prefs.clone();
- }
- if cancel && let Some(before) = app.redact_before.take() {
- app.redact_prefs = before;
- }
- if ok {
- app.redact_before = None;
- }
- close = ok || cancel;
- return;
+ req.import_words = import;
+ req.close = cancel;
+ return false;
+ }
+ Dialog::RedactProps => {
+ if !dialog_redact_props(ui, app, req) {
+ return false;
}
- Dialog::RedactApply => {
- let marks = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(0, |d| d.redaction_marks());
- let (ok, cancel) = crate::redact_ui::apply_body(ui, marks, &t);
- if ok {
- redact_now = Some(dialog);
- }
- close = ok || cancel;
- return;
+ }
+ Dialog::RedactApply => {
+ let marks = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(0, |d| d.redaction_marks());
+ let (ok, cancel) = crate::redact_ui::apply_body(ui, marks, &t);
+ if ok {
+ req.redact_now = Some(dialog);
}
- Dialog::LinkProps => {
- let pages = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
- let Some(d) = app.link_draft.as_mut() else {
- close = true;
- return;
- };
- let (ok, cancel) = crate::link_ui::body(ui, d, pages, &t);
- if ok {
- link_now = Some(crate::link_ui::edit_for(d));
- }
- close = ok || cancel;
- return;
+ req.close = ok || cancel;
+ return false;
+ }
+ Dialog::LinkProps => {
+ let pages = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
+ let Some(d) = app.link_draft.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ let (ok, cancel) = crate::link_ui::body(ui, d, pages, &t);
+ if ok {
+ req.link_now = Some(crate::link_ui::edit_for(d));
}
- Dialog::Extract => {
- let (selected, total) = app.active_ids().map_or((Vec::new(), 0), |(i, _)| (app.views[i].target_pages(), app.views[i].page_count()));
- ui.label(egui::RichText::new(tl!("Extract pages")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- // Page ranges to take instead of the selection (#3593); the hint is the selection.
- let hint = match crate::combine_ui::range_of(&selected, total) {
- all if all.is_empty() && total > 1 => format!("1-{total}"),
- all if all.is_empty() => "1".to_string(),
- some => some,
- };
- ui.horizontal(|ui| {
- let l = ui.label(tl!("Pages"));
- ui.add(egui::TextEdit::singleline(&mut app.extract_draft.pages).hint_text(hint).desired_width(240.0)).labelled_by(l.id);
- });
- let chosen = app.extract_draft.pages_to_extract(selected, total);
- match &chosen {
- Ok(pages) if pages.len() == 1 => {
- ui.label(tl!("1 page selected."));
- }
- Ok(pages) => {
- ui.label(crate::i18n::fmt(tl!("{n} pages selected."), &[("n", &pages.len().to_string())]));
- }
- Err(e) => {
- ui.colored_label(ui.visuals().error_fg_color, e);
- }
- }
- ui.add_space(4.0);
- // The name the pages are saved under (#737); separate files add " (page N)".
- ui.horizontal(|ui| {
- let l = ui.label(tl!("File name"));
- ui.add(egui::TextEdit::singleline(&mut app.extract_draft.name).desired_width(240.0)).labelled_by(l.id);
- });
- ui.add(widgets::checkbox(&mut app.extract_draft.delete, tl!("Delete pages after extracting")));
- ui.add(widgets::checkbox(&mut app.extract_draft.separate, tl!("Extract pages as separate files")));
- ui.add_space(12.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if ui.add_enabled_ui(chosen.is_ok(), |ui| widgets::pill_button(ui, tl!("Extract"), true)).inner.clicked() {
- extract_now = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ req.close = ok || cancel;
+ return false;
+ }
+ Dialog::Extract => {
+ if !dialog_extract(ui, app, req) {
+ return false;
}
- Dialog::RotatePages => {
- use pdfcraft_engine::{PageOrientation as O, PageParity as P};
- let n = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
- let d = &mut app.rotate_draft;
- ui.label(egui::RichText::new(tl!("Rotate Pages")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- egui::Grid::new("rotate-pages").num_columns(2).spacing([12.0, 8.0]).show(ui, |ui| {
- ui.label(tl!("Direction:"));
- egui::ComboBox::from_id_salt("rotate-dir")
- .selected_text(match d.degrees {
- 270 => tl!("Counterclockwise 90 degrees"),
- 180 => tl!("180 degrees"),
- _ => tl!("Clockwise 90 degrees"),
- })
- .show_ui(ui, |ui| {
- ui.selectable_value(&mut d.degrees, 90, tl!("Clockwise 90 degrees"));
- ui.selectable_value(&mut d.degrees, 270, tl!("Counterclockwise 90 degrees"));
- ui.selectable_value(&mut d.degrees, 180, tl!("180 degrees"));
- });
- ui.end_row();
- ui.label(tl!("Pages:"));
- ui.vertical(|ui| {
- widgets::radio_value(ui, &mut d.which, 0, tl!("All"));
- widgets::radio_value(ui, &mut d.which, 1, tl!("Selection"));
- ui.horizontal(|ui| {
- widgets::radio_value(ui, &mut d.which, 2, tl!("From"));
- ui.add_enabled(d.which == 2, egui::DragValue::new(&mut d.from).range(1..=n));
- ui.label(tl!("to"));
- ui.add_enabled(d.which == 2, egui::DragValue::new(&mut d.to).range(1..=n));
- ui.label(crate::i18n::fmt(tl!("of {name}"), &[("name", &n.to_string())]));
- });
- });
- ui.end_row();
- ui.label(tl!("Rotate:"));
- egui::ComboBox::from_id_salt("rotate-parity")
- .selected_text(match d.parity {
- P::Both => tl!("Even and Odd Pages"),
- P::Even => tl!("Even Pages Only"),
- P::Odd => tl!("Odd Pages Only"),
- })
- .show_ui(ui, |ui| {
- ui.selectable_value(&mut d.parity, P::Both, tl!("Even and Odd Pages"));
- ui.selectable_value(&mut d.parity, P::Even, tl!("Even Pages Only"));
- ui.selectable_value(&mut d.parity, P::Odd, tl!("Odd Pages Only"));
- });
- ui.end_row();
- ui.label("");
- egui::ComboBox::from_id_salt("rotate-orient")
- .selected_text(match d.orientation {
- O::Both => tl!("Landscape and Portrait Pages"),
- O::Landscape => tl!("Landscape Pages"),
- O::Portrait => tl!("Portrait Pages"),
- })
- .show_ui(ui, |ui| {
- ui.selectable_value(&mut d.orientation, O::Both, tl!("Landscape and Portrait Pages"));
- ui.selectable_value(&mut d.orientation, O::Landscape, tl!("Landscape Pages"));
- ui.selectable_value(&mut d.orientation, O::Portrait, tl!("Portrait Pages"));
- });
- ui.end_row();
- });
- d.to = d.to.max(d.from);
- ui.add_space(12.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if widgets::pill_button(ui, tl!("OK"), true).clicked() {
- rotate_now = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ }
+ Dialog::RotatePages => {
+ if !dialog_rotate_pages(ui, app, req) {
+ return false;
}
- Dialog::DuplicateField => {
- let n = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
- let Some(d) = app.duplicate_draft.as_mut() else {
- close = true;
- return;
- };
- ui.label(egui::RichText::new(tl!("Duplicate Field")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- ui.label(crate::i18n::fmt(tl!("Duplicate \"{name}\" onto:"), &[("name", &d.name)]));
- widgets::radio_value(ui, &mut d.all, true, tl!("All pages"));
- ui.horizontal(|ui| {
- widgets::radio_value(ui, &mut d.all, false, tl!("From"));
- ui.add_enabled(!d.all, egui::DragValue::new(&mut d.from).range(1..=n));
- ui.label(tl!("to"));
- ui.add_enabled(!d.all, egui::DragValue::new(&mut d.to).range(1..=n));
- ui.label(crate::i18n::fmt(tl!("of {name}"), &[("name", &n.to_string())]));
- });
- d.to = d.to.max(d.from);
- ui.add_space(12.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if widgets::pill_button(ui, tl!("OK"), true).clicked() {
- let pages: Vec<usize> = if d.all { (0..n).collect() } else { (d.from - 1..d.to.min(n)).collect() };
- duplicate_now = Some(Edit::DuplicateField { name: d.name.clone(), pages });
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ }
+ Dialog::PageProps => {
+ let Some(d) = app.page_props_draft.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ let (ok, cancel) = crate::organize_panel::page_props_body(ui, d);
+ // An order every page already has adds no undo step.
+ if ok && let Some(order) = d.tab_order.filter(|o| d.before != Some(*o)) {
+ req.page_props_now = Some((d.doc, d.generation, Edit::SetTabOrder { pages: d.pages.clone(), order }));
}
- // Never reached: `dialogs` shows this movable window above and returns before the modal.
- // The arm keeps the match exhaustive.
- Dialog::FieldTable => close = true,
- Dialog::Optimize => {
- let (ok, cancel) = crate::optimize_ui::body(ui, &mut app.optimize_draft, &t);
- optimize_now = ok;
- close = ok || cancel;
- if std::mem::take(&mut app.optimize_draft.audit) {
- app.refresh_space_audit();
- next = Dialog::AuditSpace;
- }
- return;
+ req.close = ok || cancel;
+ return false;
+ }
+ Dialog::DuplicateField => {
+ if !dialog_duplicate_field(ui, app, req) {
+ return false;
}
- Dialog::CertificateViewer => {
- ui.set_width(700.0);
- let trusted = app.session.trusted_certificates().to_vec();
- let Some(v) = app.cert_viewer.as_mut() else {
- close = true;
- return;
- };
- let (done, action) = crate::sign_ui::cert_viewer(ui, v, &trusted, &t);
- close = done;
- match action {
- Some(crate::sign_ui::CertAction::Trust(c)) => app.trust_certificate(*c),
- Some(crate::sign_ui::CertAction::Export(c)) => {
- app.export_certificate(&c);
- }
- None => {}
- }
- return;
+ }
+ // Never reached: `dialogs` shows this movable window above and returns before the modal.
+ // The arm keeps the match exhaustive.
+ Dialog::FieldTable => req.close = true,
+ Dialog::Optimize => {
+ let (ok, cancel) = crate::optimize_ui::body(ui, &mut app.optimize_draft, &t);
+ req.optimize_now = ok;
+ req.close = ok || cancel;
+ if std::mem::take(&mut app.optimize_draft.audit) {
+ app.refresh_space_audit();
+ req.next = Some(Dialog::AuditSpace);
}
- Dialog::AuditSpace => {
- if crate::optimize_ui::audit_body(ui, &app.space_audit, &t) {
- next = Dialog::Optimize;
- }
- return;
+ return false;
+ }
+ Dialog::CertificateViewer => {
+ if !dialog_certificate_viewer(ui, app, req) {
+ return false;
}
- Dialog::Sign => {
- if crate::sign_ui::dialog(ui, app, &t) {
- app.sign_draft = None;
- close = true;
- }
- return;
+ }
+ Dialog::AuditSpace => {
+ if crate::optimize_ui::audit_body(ui, &app.space_audit, &t) {
+ req.next = Some(Dialog::Optimize);
}
- Dialog::SummarizeComments => {
- ui.label(egui::RichText::new(tl!("Summarize Options")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- ui.label(tl!("Choose a layout:"));
- let _ = ui.add(widgets::radio(true, tl!("Comments only")));
- ui.add_space(6.0);
- ui.horizontal(|ui| {
- ui.label(tl!("Sort comments by:"));
- egui::ComboBox::from_id_salt("summary-sort").selected_text(tl!(app.summary_sort.name())).show_ui(ui, |ui| {
- for s in pdfcraft_engine::SummarySort::ALL {
- ui.selectable_value(&mut app.summary_sort, s, tl!(s.name()));
- }
- });
- });
- ui.add_space(12.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if widgets::pill_button(ui, tl!("Create PDF Comment Summary"), true).clicked() {
- summarize_now = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ return false;
+ }
+ Dialog::Sign => {
+ if crate::sign_ui::dialog(ui, app, &t) {
+ app.sign_draft = None;
+ req.close = true;
}
- Dialog::Revert => {
- let name = app.active_ids().and_then(|(_, id)| app.session.get(id)).map(|d| d.name.clone()).unwrap_or_default();
- ui.label(egui::RichText::new(tl!("Revert")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- ui.label(crate::i18n::fmt(
- tl!("Revert to the previously saved version of “{name}”? Changes since then can't be undone afterwards."),
- &[("name", &name)],
- ));
- ui.add_space(12.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if widgets::pill_button(ui, tl!("Revert"), true).clicked() {
- revert_now = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ return false;
+ }
+ Dialog::SummarizeComments => {
+ if !dialog_summarize_comments(ui, app, req) {
+ return false;
}
- Dialog::OverwriteChanged => {
- #[cfg(not(target_arch = "wasm32"))]
- let name = app.pending_overwrite.as_ref().and_then(|p| app.session.get(p.id)).map(|d| d.name.clone()).unwrap_or_default();
- #[cfg(target_arch = "wasm32")]
- let name = String::new();
- ui.label(egui::RichText::new(tl!("Save over the changed file?")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- ui.label(crate::i18n::fmt(
- tl!("“{name}” changed on disk after it was opened or last saved. Saving now replaces that newer version with this one."),
- &[("name", &name)],
- ));
- ui.add_space(12.0);
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if widgets::pill_button(ui, tl!("Save anyway"), true).clicked() {
- overwrite_now = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Save as…"), false).clicked() {
- overwrite_elsewhere = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- });
- return;
+ }
+ Dialog::Revert => {
+ if !dialog_revert(ui, app, req) {
+ return false;
}
- Dialog::Print => {
- let Some((i, id)) = app.active_ids() else {
- close = true;
- return;
- };
- if app.print_draft.handling == crate::print_ui::Handling::Label {
- app.read_label_crops(id);
- }
- let Some(doc) = app.session.get(id) else { return };
- let sizes: Vec<(f64, f64)> = doc.info.pages.iter().map(|p| (p.width as f64, p.height as f64)).collect();
- let labels: Vec<String> = doc.info.pages.iter().map(|p| p.label.clone()).collect();
- let rasters = crate::print_ui::preview_rasters(
- &app.print_draft,
- &sizes,
- &labels,
- ui.ctx().pixels_per_point(),
- ui.ctx().input(|i| i.max_texture_side) as f32,
- );
- let view = &mut app.views[i];
- view.set_print_grayscale(app.print_draft.grayscale);
- view.queue_print_previews(&rasters);
- let (go, cancel) = crate::print_ui::body(ui, &mut app.print_draft, &t, &sizes, &labels, &mut |p| {
- view.need_thumbnail(p, true);
- view.page_preview(p)
- });
- print_go = go;
- close = go || cancel;
- return;
+ }
+ Dialog::OverwriteChanged => {
+ if !dialog_overwrite_changed(ui, app, req) {
+ return false;
}
- Dialog::RemoveHidden => {
- let (ok, cancel) = crate::redact_ui::hidden_body(ui, &mut app.hidden_draft, &t);
- if ok {
- redact_now = Some(dialog);
- }
- close = ok || cancel;
- return;
+ }
+ Dialog::Print => {
+ if !dialog_print(ui, app, req) {
+ return false;
}
- Dialog::Sanitize => {
- let (ok, cancel) = crate::redact_ui::sanitize_body(ui, &t);
- if ok {
- redact_now = Some(dialog);
- }
- close = ok || cancel;
- return;
+ }
+ Dialog::RemoveHidden => {
+ let (ok, cancel) = crate::redact_ui::hidden_body(ui, &mut app.hidden_draft, &t);
+ if ok {
+ req.redact_now = Some(dialog);
}
- Dialog::FieldProps => {
- let Some(d) = app.field_props.as_mut() else {
- close = true;
- return;
- };
- let (apply, cancel) = crate::prepare::body(ui, d, &t);
- field_props_now = apply;
- close = apply || cancel;
- return;
+ req.close = ok || cancel;
+ return false;
+ }
+ Dialog::Sanitize => {
+ let (ok, cancel) = crate::redact_ui::sanitize_body(ui, &t);
+ if ok {
+ req.redact_now = Some(dialog);
}
- Dialog::BulkFieldProps => {
- let Some(d) = app.bulk_field_props.as_mut() else {
- close = true;
- return;
- };
- let (apply, cancel) = crate::bulk_fields::body(ui, d, &t);
- bulk_field_props_now = apply;
- close = cancel;
- return;
+ req.close = ok || cancel;
+ return false;
+ }
+ Dialog::FieldProps => {
+ let Some(d) = app.field_props.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ let (go, cancel) = crate::prepare::body(ui, d, &t);
+ req.apply = go;
+ req.field_props_now = req.apply;
+ req.close = req.apply || cancel;
+ return false;
+ }
+ Dialog::BulkFieldProps => {
+ let Some(d) = app.bulk_field_props.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ let (go, cancel) = crate::bulk_fields::body(ui, d, &t);
+ req.apply = go;
+ req.bulk_field_props_now = req.apply;
+ req.close = cancel;
+ return false;
+ }
+ Dialog::CommentProps => {
+ let (go, cancel) = crate::comment_props::body(ui, app, &t);
+ req.apply = go;
+ req.props_now = req.apply;
+ req.close = req.apply || cancel;
+ return false;
+ }
+ Dialog::Signature => {
+ if !dialog_signature(ui, app, req) {
+ return false;
}
- Dialog::CommentProps => {
- let (apply, cancel) = crate::comment_props::body(ui, app, &t);
- props_now = apply;
- close = apply || cancel;
- return;
+ }
+ Dialog::AltText => {
+ let (save, cancel) = crate::a11y_ui::alt_body(ui, app, &t);
+ req.alt_now = save;
+ req.close = save || cancel;
+ return false;
+ }
+ Dialog::CreateStamp => {
+ let (save, cancel) = crate::stamps_ui::create_body(ui, app, &t);
+ req.stamp_now = save;
+ req.close = save || cancel;
+ return false;
+ }
+ Dialog::PdfA => {
+ req.close = crate::standards_ui::body(ui, app, &t);
+ return false;
+ }
+ Dialog::ActionWizard => {
+ ui.set_width(620.0);
+ req.close = crate::actions_ui::body(ui, app, &t);
+ return false;
+ }
+ Dialog::CompareFiles => {
+ let (go, cancel) = crate::compare_ui::body(ui, app, &t);
+ req.compare_now = go;
+ req.close = go || cancel;
+ return false;
+ }
+ Dialog::JsConsole => {
+ ui.set_width(640.0);
+ req.close = crate::js_ui::console_body(ui, app, &t);
+ return false;
+ }
+ Dialog::DocumentJs => {
+ ui.set_width(640.0);
+ req.close = crate::js_ui::document_js_body(ui, app, &t);
+ return false;
+ }
+ Dialog::Preferences => {
+ if !dialog_preferences(ui, app, req) {
+ return false;
}
- Dialog::Signature => {
- let (apply, cancel, browse) =
- crate::fill_sign::signature_pad(ui, &t, &mut app.signature_draft, &mut app.signature_preview, &mut app.signature_photo);
- if browse {
- app.pick_signature_image();
- }
- if apply {
- let d = std::mem::take(&mut app.signature_draft);
- let tool = if d.initials {
- app.initials = d.saved();
- crate::fill_sign::FillTool::Initials
- } else {
- app.signature = d.saved();
- crate::fill_sign::FillTool::Signature
- };
- app.quick_tool = crate::QuickTool::Fill(tool);
- app.toast = None;
- }
- close = apply || cancel;
- return;
+ }
+ Dialog::RecognizeText => {
+ let (go, cancel) = crate::ocr_ui::body(ui, app, &t);
+ req.ocr_now = go;
+ req.close = go || cancel;
+ return false;
+ }
+ Dialog::AccessibilityOptions => {
+ let (start, cancel) = crate::a11y_ui::options_body(ui, app, &t);
+ req.a11y_now = start;
+ req.close = start || cancel;
+ return false;
+ }
+ Dialog::Export(kind) => {
+ let (go, cancel) = crate::export_ui::body(ui, app, &t, kind);
+ req.apply = go;
+ req.export_now = req.apply;
+ req.close = req.apply || cancel;
+ return false;
+ }
+ Dialog::Marks(kind) => {
+ ui.set_width(720.0);
+ let (go, cancel) = crate::marks_ui::body(ui, app, &t, kind);
+ req.apply = go;
+ req.marks_now = req.apply;
+ req.close = req.apply || cancel;
+ return false;
+ }
+ Dialog::PageBoxes => {
+ let (go, cancel) = crate::pageboxes::body(ui, app, &t);
+ req.apply = go;
+ req.boxes_now = req.apply;
+ req.close = req.apply || cancel;
+ return false;
+ }
+ Dialog::Protect => {
+ let (go, cancel) = crate::protect::body(ui, app, &t);
+ req.apply = go;
+ req.protect_now = req.apply;
+ req.close = req.apply || cancel;
+ return false;
+ }
+ Dialog::BookmarksFromTextStyles => {
+ req.close = crate::bookmark_styles_ui::body(ui, app, &t);
+ return false;
+ }
+ Dialog::NumberPages => {
+ if !dialog_number_pages(ui, app, req) {
+ return false;
}
- Dialog::AltText => {
- let (save, cancel) = crate::a11y_ui::alt_body(ui, app, &t);
- alt_now = save;
- close = save || cancel;
- return;
+ }
+ Dialog::Recovery => {
+ if !dialog_recovery(ui, app) {
+ return false;
}
- Dialog::CreateStamp => {
- let (save, cancel) = crate::stamps_ui::create_body(ui, app, &t);
- stamp_now = save;
- close = save || cancel;
- return;
+ }
+ Dialog::Shortcuts => {
+ if !dialog_shortcuts(ui) {
+ return false;
}
- Dialog::PdfA => {
- close = crate::standards_ui::body(ui, app, &t);
- return;
+ }
+ Dialog::About => {
+ if !dialog_about(ui, req) {
+ return false;
}
- Dialog::ActionWizard => {
- ui.set_width(620.0);
- close = crate::actions_ui::body(ui, app, &t);
- return;
+ }
++ Dialog::PinTool => {
++ if !dialog_pin_tool(ui, app, req) {
++ return false;
+ }
- Dialog::CompareFiles => {
- let (go, cancel) = crate::compare_ui::body(ui, app, &t);
- compare_now = go;
- close = go || cancel;
- return;
++ }
+ }
+ true
+ }
+
+ /// `Dialog::Properties(tab)` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_properties(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending, tab: PropsTab) -> bool {
+ let t = Tokens::get(ui.ctx());
+ ui.label(egui::RichText::new(tl!("Document Properties")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ ui.horizontal_wrapped(|ui| {
+ for (tb, label) in [
+ (PropsTab::Description, tl!("Description")),
+ (PropsTab::InitialView, tl!("Initial View")),
+ (PropsTab::Security, tl!("Security")),
+ (PropsTab::Fonts, tl!("Fonts")),
+ (PropsTab::Custom, tl!("Custom")),
+ (PropsTab::Advanced, tl!("Advanced")),
+ (PropsTab::SpaceAudit, tl!("Space Audit")),
+ ] {
+ if widgets::mode_tab(ui, label, tab == tb).clicked() {
+ req.next = Some(Dialog::Properties(tb));
}
- Dialog::JsConsole => {
- ui.set_width(640.0);
- close = crate::js_ui::console_body(ui, app, &t);
- return;
+ }
+ });
+ ui.separator();
+ let units = app.units;
+ let Some((_, id)) = app.active_ids() else { return false };
+ let Some(doc) = app.session.get(id) else { return false };
+ let i = &doc.info;
+ // Description shows plain values among text fields; they start where the fields' text does.
+ let editable_info = tab == PropsTab::Description && doc.allows_modification() && app.props_draft.is_some();
+ let inset = if editable_info { widgets::FIELD_TEXT_INSET } else { 0.0 };
+ let row = |ui: &mut egui::Ui, k: &str, v: String| {
+ // Row labels are UI chrome; values are document data and stay as they are.
+ ui.label(egui::RichText::new(tl!(k)).color(t.text_muted));
+ ui.horizontal(|ui| {
+ ui.add_space(inset);
+ ui.add(egui::Label::new(if v.is_empty() { egui::RichText::new("—").color(t.text_faint) } else { egui::RichText::new(v) }).wrap());
+ });
+ ui.end_row();
+ };
+ // A file name or path is read left to right even in an RTL UI: the extension
+ // stays at its visual end, as in the tab strip.
+ let path_row = |ui: &mut egui::Ui, k: &str, v: String| {
+ ui.label(egui::RichText::new(tl!(k)).color(t.text_muted));
+ ui.scope(|ui| {
+ ui.set_max_width(420.0);
+ // Align path text with editable fields while keeping its LTR text order.
+ ui.add_space(inset);
+ if v.is_empty() {
+ ui.label(egui::RichText::new("—").color(t.text_faint));
+ } else {
+ crate::widgets::label_ltr(ui, v, crate::theme::regular(13.0), t.text);
}
- Dialog::DocumentJs => {
- ui.set_width(640.0);
- close = crate::js_ui::document_js_body(ui, app, &t);
- return;
+ });
+ ui.end_row();
+ };
+ egui::ScrollArea::vertical().max_height(460.0).auto_shrink([false, true]).show(ui, |ui| {
+ if tab == PropsTab::SpaceAudit {
+ ui.add_space(4.0);
+ crate::optimize_ui::audit_table(ui, &app.space_audit, &t);
+ return;
+ }
+ egui::Grid::new("props").num_columns(2).spacing([18.0, 8.0]).min_col_width(140.0).show(ui, |ui| match tab {
+ PropsTab::Description => {
+ path_row(ui, "File", doc.name.clone());
+ match app.props_draft.as_mut() {
+ Some((_, draft)) if editable_info => {
+ for (k, v) in INFO_KEYS.iter().zip(draft.iter_mut()) {
+ let l = ui.label(egui::RichText::new(tl!(k)).color(t.text_muted));
+ // Subject and Keywords are often a sentence or a list: they wrap and grow
+ // instead of hiding most of the value past the field's edge.
+ let field = if matches!(*k, "Subject" | "Keywords") { widgets::block(v).desired_rows(1) } else { widgets::line(v) };
+ ui.add(field.desired_width(f32::INFINITY).background_color(t.field).id_salt(("info", *k))).labelled_by(l.id);
+ ui.end_row();
+ }
+ }
+ _ => {
+ row(ui, "Title", i.title.clone().unwrap_or_default());
+ row(ui, "Author", i.author.clone().unwrap_or_default());
+ row(ui, "Subject", i.subject.clone().unwrap_or_default());
+ row(ui, "Keywords", i.keywords.clone().unwrap_or_default());
+ }
+ }
+ row(ui, "Application", i.creator.clone().unwrap_or_default());
+ row(ui, "PDF producer", i.producer.clone().unwrap_or_default());
}
- Dialog::Preferences => {
- // The settings outgrow a short window: scroll them rather than run off the screen.
- // The OK row stays below the scroll, in reach however far the settings are scrolled.
- let max = (ui.ctx().content_rect().height() - 120.0 - crate::js_ui::PREFERENCES_BUTTON_ROW).max(crate::js_ui::PREFERENCES_MIN_SCROLL);
- // A scroll area starts from the room the dialog offers it, and a modal offers more
- // each frame for the first few, so with OK outside the scroll the dialog would grow
- // over those frames. Asking for the full height as the least it may scroll in
- // (which only applies when the settings overflow it) opens the dialog at full size.
- // A scrollbar of its own: the floating one lies over the right edge of the theme
- // cards and other full-width rows (#3557).
- ui.scope(|ui| {
- ui.spacing_mut().scroll.floating = false;
- egui::ScrollArea::vertical().max_height(max).min_scrolled_height(max).show(ui, |ui| crate::js_ui::preferences_body(ui, app, &t));
+ PropsTab::InitialView => {
+ use pdfcraft_engine::{InitialLayout as L, Magnification as M, Navigation as N};
+ let editable = doc.allows_modification();
+ let pages = i.pages.len();
+ let Some((_, v)) = app.view_draft.as_mut() else { return };
+ ui.label(egui::RichText::new(tl!("Layout and Magnification")).font(theme::semibold(12.5)));
+ ui.end_row();
+ let label = ui.label(tl!("Navigation tab"));
+ ui.add_enabled_ui(editable, |ui| {
+ craft_ui::ComboBox::from_id_salt("iv-nav")
+ .selected_text(tl!(format!("{:?}", v.navigation)
+ .replace("PageOnly", "Page Only")
+ .replace("Pages", "Pages Panel and Page")
+ .as_str()))
+ .show_ui(ui, |ui| {
+ for (n, l) in [
+ (N::PageOnly, tl!("Page Only")),
+ (N::Bookmarks, tl!("Bookmarks Panel and Page")),
+ (N::Pages, tl!("Pages Panel and Page")),
+ (N::Attachments, tl!("Attachments Panel and Page")),
+ (N::Layers, tl!("Layers Panel and Page")),
+ ] {
+ ui.selectable_value(&mut v.navigation, n, l);
+ }
+ })
+ .response
+ .labelled_by(label.id);
});
- close = crate::js_ui::preferences_buttons(ui);
- return;
- }
- Dialog::RecognizeText => {
- let (go, cancel) = crate::ocr_ui::body(ui, app, &t);
- ocr_now = go;
- close = go || cancel;
- return;
- }
- Dialog::AccessibilityOptions => {
- let (start, cancel) = crate::a11y_ui::options_body(ui, app, &t);
- a11y_now = start;
- close = start || cancel;
- return;
- }
- Dialog::Export(kind) => {
- let (apply, cancel) = crate::export_ui::body(ui, app, &t, kind);
- export_now = apply;
- close = apply || cancel;
- return;
- }
- Dialog::Marks(kind) => {
- ui.set_width(720.0);
- let (apply, cancel) = crate::marks_ui::body(ui, app, &t, kind);
- marks_now = apply;
- close = apply || cancel;
- return;
- }
- Dialog::PageBoxes => {
- let (apply, cancel) = crate::pageboxes::body(ui, app, &t);
- boxes_now = apply;
- close = apply || cancel;
- return;
- }
- Dialog::Protect => {
- let (apply, cancel) = crate::protect::body(ui, app, &t);
- protect_now = apply;
- close = apply || cancel;
- return;
- }
- Dialog::NumberPages => {
- use pdfcraft_engine::LabelStyle as L;
- ui.label(egui::RichText::new(tl!("Number pages")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- let Some((_, id)) = app.active_ids() else { return };
- let n = app.session.get(id).map(|d| d.info.pages.len()).unwrap_or(1).max(1);
- let d = &mut app.number_draft;
- d.to = d.to.clamp(1, n);
- d.from = d.from.clamp(1, d.to);
- egui::Grid::new("number_pages").num_columns(2).spacing([12.0, 8.0]).show(ui, |ui| {
- ui.label(tl!("Pages"));
+ ui.end_row();
+ let label = ui.label(tl!("Page layout"));
+ ui.add_enabled_ui(editable, |ui| {
+ let names = [
+ (L::Default, tl!("Default")),
+ (L::SinglePage, tl!("Single Page")),
+ (L::SinglePageContinuous, tl!("Single Page Continuous")),
+ (L::TwoUp, tl!("Two-Up (Facing)")),
+ (L::TwoUpContinuous, tl!("Two-Up Continuous (Facing)")),
+ (L::TwoUpCoverPage, tl!("Two-Up (Cover Page)")),
+ (L::TwoUpContinuousCoverPage, tl!("Two-Up Continuous (Cover Page)")),
+ ];
+ let shown = names.iter().find(|(l, _)| *l == v.layout).map_or(tl!("Default"), |(_, n)| n);
+ craft_ui::ComboBox::from_id_salt("iv-layout")
+ .selected_text(shown)
+ .show_ui(ui, |ui| {
+ for (l, n) in names {
+ ui.selectable_value(&mut v.layout, l, n);
+ }
+ })
+ .response
+ .labelled_by(label.id);
+ });
+ ui.end_row();
+ let label = ui.label(tl!("Magnification"));
+ ui.add_enabled_ui(editable, |ui| {
ui.horizontal(|ui| {
- ui.add(egui::DragValue::new(&mut d.from).range(1..=n));
- ui.label(tl!("to"));
- ui.add(egui::DragValue::new(&mut d.to).range(1..=n));
- ui.label(egui::RichText::new(crate::i18n::fmt(tl!("of {name}"), &[("name", &n.to_string())])).color(t.text_muted));
+ let names = [
+ (M::Default, tl!("Default")),
+ (M::ActualSize, tl!("Actual Size")),
+ (M::FitPage, tl!("Fit Page")),
+ (M::FitWidth, tl!("Fit Width")),
+ (M::FitHeight, tl!("Fit Height")),
+ (M::FitVisible, tl!("Fit Visible")),
+ ];
+ let shown = match v.magnification {
+ M::Percent(p) => format!("{p:.0}%"),
+ m => names.iter().find(|(x, _)| *x == m).map_or(tl!("Default").to_string(), |(_, n)| n.to_string()),
+ };
+ craft_ui::ComboBox::from_id_salt("iv-mag")
+ .selected_text(shown)
+ .show_ui(ui, |ui| {
+ for (m, n) in names {
+ ui.selectable_value(&mut v.magnification, m, n);
+ }
+ for p in [50.0, 75.0, 125.0, 150.0, 200.0] {
+ ui.selectable_value(&mut v.magnification, M::Percent(p), format!("{p:.0}%"));
+ }
+ })
+ .response
+ .labelled_by(label.id);
});
- ui.end_row();
- ui.label(tl!("Style"));
- let styles = [
- (L::Decimal, "1, 2, 3"),
- (L::LowerRoman, "i, ii, iii"),
- (L::UpperRoman, "I, II, III"),
- (L::LowerAlpha, "a, b, c"),
- (L::UpperAlpha, "A, B, C"),
- (L::None, tl!("None (prefix only)")),
- ];
- let current = styles.iter().find(|(s, _)| *s == d.style).map_or("1, 2, 3", |(_, l)| *l);
- egui::ComboBox::from_id_salt("label_style").selected_text(current).show_ui(ui, |ui| {
- for (s, l) in styles {
- ui.selectable_value(&mut d.style, s, l);
- }
+ });
+ ui.end_row();
+ let label = ui.label(tl!("Open to page"));
+ ui.add_enabled_ui(editable, |ui| {
+ let mut p = v.page + 1;
+ if ui.add(egui::DragValue::new(&mut p).range(1..=pages.max(1))).labelled_by(label.id).changed() {
+ v.page = p - 1;
+ }
+ ui.label(crate::i18n::fmt(tl!("of {pages}"), &[("pages", &pages.to_string())]));
+ });
+ ui.end_row();
+ ui.label(egui::RichText::new(tl!("Window Options")).font(theme::semibold(12.5)));
+ ui.end_row();
+ ui.label("");
+ ui.add_enabled_ui(editable, |ui| {
+ ui.vertical(|ui| {
+ ui.add(widgets::checkbox(&mut v.fit_window, tl!("Resize window to initial page")));
+ ui.add(widgets::checkbox(&mut v.center_window, tl!("Center window on screen")));
+ ui.add(widgets::checkbox(&mut v.full_screen, tl!("Open in Full Screen mode")));
+ ui.horizontal(|ui| {
+ ui.label(tl!("Show:"));
+ widgets::radio_value(ui, &mut v.display_title, false, tl!("File Name"));
+ widgets::radio_value(ui, &mut v.display_title, true, tl!("Document Title"));
+ });
+ });
+ });
+ ui.end_row();
+ ui.label(egui::RichText::new(tl!("User Interface Options")).font(theme::semibold(12.5)));
+ ui.end_row();
+ ui.label("");
+ ui.add_enabled_ui(editable, |ui| {
+ ui.vertical(|ui| {
+ ui.add(widgets::checkbox(&mut v.hide_menubar, tl!("Hide menu bar")));
+ ui.add(widgets::checkbox(&mut v.hide_toolbar, tl!("Hide toolbars")));
+ ui.add(widgets::checkbox(&mut v.hide_window_ui, tl!("Hide window controls")));
+ });
+ });
+ ui.end_row();
+ }
+ PropsTab::Security => match doc.security_summary() {
+ None => {
+ row(ui, "Security method", tl!("No security").to_string());
+ row(ui, "Restrictions", tl!("None — everything is allowed").to_string());
+ if doc.allows_security_change() && ui.button(tl!("Protect using password…")).clicked() {
+ req.link_command = Some("protect.password");
+ }
+ }
+ Some(sec) => {
+ row(ui, "Security method", tl!("Password security").to_string());
+ row(ui, "Encryption", sec.method.clone());
+ row(
+ ui,
+ "Opened with",
+ if sec.pending {
+ tl!("— (protection is applied when you save)").to_string()
+ } else if sec.owner {
+ tl!("Owner password (no restrictions apply)").to_string()
+ } else {
+ tl!("User password").to_string()
+ },
+ );
+ // A restricted document asks for its permissions password first.
+ if doc.allows_security_change() || doc.can_unlock() {
+ ui.horizontal(|ui| {
+ if doc.can_unlock() && ui.button(tl!("Unlock…")).clicked() {
+ req.link_command = Some("protect.unlock");
+ }
+ if ui.button(tl!("Change settings…")).clicked() {
+ req.link_command = Some("protect.password");
+ }
+ if ui.button(tl!("Remove security")).clicked() {
+ req.link_command = Some("protect.remove");
+ }
+ });
+ }
+ let p = sec.permissions;
+ let yes = |b: bool| tl!(if b { "Allowed" } else { "Not allowed" }).to_string();
+ row(
+ ui,
+ "Printing",
+ if !p.print() {
+ tl!("Not allowed").to_string()
+ } else if p.print_high_quality() {
+ tl!("High resolution").to_string()
+ } else {
+ tl!("Low resolution").to_string()
+ },
+ );
+ row(ui, "Changing the document", yes(p.modify()));
+ row(ui, "Document assembly", yes(p.assemble()));
+ row(ui, "Content copying", yes(p.copy()));
+ row(ui, "Content copying for accessibility", yes(p.extract_for_accessibility()));
+ row(ui, "Commenting", yes(p.annotate()));
+ row(ui, "Filling of form fields", yes(p.fill_forms()));
+ }
+ },
+ PropsTab::Fonts => {
+ if i.fonts.is_empty() {
+ row(ui, "Fonts", tl!("No fonts are referenced by the pages.").to_string());
+ }
+ for f in &i.fonts {
+ let mut detail = f.kind.clone();
+ if let Some(e) = &f.encoding {
+ detail.push_str(&format!(" · {e}"));
+ }
+ detail.push_str(if f.subset {
+ " · Embedded subset"
+ } else if f.embedded {
+ " · Embedded"
+ } else {
+ " · Not embedded (substituted)"
});
- ui.end_row();
- let l = ui.label(tl!("Prefix"));
- ui.add(widgets::line(&mut d.prefix).desired_width(160.0)).labelled_by(l.id);
- ui.end_row();
- ui.label(tl!("Start"));
- ui.add(egui::DragValue::new(&mut d.start).range(1..=99_999));
- ui.end_row();
- });
- d.to = d.to.max(d.from);
- let label = |k: u32| format!("{}{}", d.prefix, d.style.format(k));
- ui.add_space(8.0);
- let preview = if d.from == d.to {
- label(d.start)
- } else {
- format!("{}, {} … {}", label(d.start), label(d.start + 1), label(d.start + (d.to - d.from) as u32))
- };
- ui.label(
- egui::RichText::new(crate::i18n::fmt(tl!("Labels: {preview}. Later pages keep their labels."), &[("preview", &preview)]))
- .color(t.text_muted),
- );
- number_now = Some(Edit::NumberPages { from: d.from - 1, to: d.to - 1, style: d.style, prefix: d.prefix.clone(), first: d.start });
+ row(ui, &f.name, detail);
+ }
}
- Dialog::Recovery => {
+ PropsTab::Custom => {
+ // Add takes a new name, Update changes the value of the listed property the
+ // Name field names, Delete removes it; OK writes the changes as one step.
+ let editable = doc.allows_modification();
+ let Some((_, d)) = app.custom_draft.as_mut() else { return };
+ let l = ui.label(egui::RichText::new(tl!("Name:")).color(t.text_muted));
+ ui.add_enabled(editable, widgets::line(&mut d.name).desired_width(420.0).background_color(t.field).id_salt("custom-name"))
+ .labelled_by(l.id);
+ ui.end_row();
+ let l = ui.label(egui::RichText::new(tl!("Value:")).color(t.text_muted));
+ ui.add_enabled(editable, widgets::line(&mut d.value).desired_width(420.0).background_color(t.field).id_salt("custom-value"))
+ .labelled_by(l.id);
+ ui.end_row();
+ // Untrimmed: a file's `/Project#20` is not `/Project`, and a typed space is refused.
+ let name = d.name.clone();
+ let value = d.value.trim().to_owned();
+ let listed = d.props.iter().position(|(k, _)| *k == name);
+ // An entry the list leaves out (not text, only padding, or past its 256) keeps its value.
+ let file_props = doc.custom_info();
+ let unlisted = doc.has_info(&name) && !file_props.iter().any(|(k, _)| *k == name);
+ let refused = pdfcraft_engine::check_custom_key(&name).err().filter(|_| !name.is_empty()).map(|e| e.to_string());
+ ui.label("");
+ // A file can hold a custom name that `check_custom_key` refuses, such as one
+ // with a space: the tab lists it, and Add, Update and Delete stay off for it.
+ let why = |b: egui::Response| match &refused {
+ Some(why) => b.on_disabled_hover_text(crate::i18n::words(why)),
+ None => b,
+ };
+ // A name that isn't UTF-8 is listed with U+FFFD and can't be written back as it is.
+ let editable = editable && refused.is_none() && !name.contains(char::REPLACEMENT_CHARACTER);
+ // `custom_info` lists at most 256 properties, and a new name goes after the file's
+ // entries, so Add is off once the draft or the file's own list holds 256.
+ let room = d.props.len() < 256 && file_props.len() < 256;
ui.horizontal(|ui| {
- ui.add(crate::icons::image("clock-3", 22.0, t.accent));
- ui.label(egui::RichText::new(tl!("Recover unsaved documents?")).font(theme::semibold(18.0)));
- });
- ui.add_space(6.0);
- ui.label(tl!("PdfCraft didn't shut down normally. These documents had changes that were autosaved:"));
- ui.add_space(8.0);
- let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);
- egui::Grid::new("recoverable").num_columns(2).spacing([18.0, 6.0]).show(ui, |ui| {
- for m in &app.recoverable {
- ui.label(egui::RichText::new(&m.name).font(theme::medium(13.0)));
- let mins = now.saturating_sub(m.saved_at) / 60;
- let when = match mins {
- 0 => tl!("just now").to_string(),
- 1..=59 => crate::i18n::fmt(tl!("{n} min ago"), &[("n", &mins.to_string())]),
- _ => crate::i18n::fmt(tl!("{n} h ago"), &[("n", &(mins / 60).to_string())]),
- };
- let lock = if m.encrypted { tl!(" · password-protected") } else { "" };
- ui.label(egui::RichText::new(format!("{when}{lock}")).color(t.text_muted));
- ui.end_row();
+ let add = editable && room && listed.is_none() && !unlisted && !name.is_empty() && !value.is_empty();
+ if why(ui.add_enabled(add, egui::Button::new(tl!("Add")))).clicked() {
+ d.props.push((name.clone(), value.clone()));
+ d.name.clear();
+ d.value.clear();
+ }
+ let update = editable && !value.is_empty() && listed.and_then(|n| d.props.get(n)).is_some_and(|(_, v)| *v != value);
+ if why(ui.add_enabled(update, egui::Button::new(tl!("Update")))).clicked()
+ && let Some(p) = listed.and_then(|n| d.props.get_mut(n))
+ {
+ p.1 = value.clone();
+ }
+ if why(ui.add_enabled(editable && listed.is_some(), egui::Button::new(tl!("Delete")))).clicked()
+ && let Some(n) = listed.filter(|n| *n < d.props.len())
+ {
+ d.props.remove(n);
+ d.name.clear();
+ d.value.clear();
}
});
- }
- Dialog::Shortcuts => {
- ui.label(egui::RichText::new(tl!("Keyboard shortcuts")).font(theme::semibold(18.0)));
- ui.add_space(8.0);
- use crate::commands::{
- ACTUAL_SIZE, COPY, FIND_NEXT, FIND_PREV, FIT_WIDTH, PAGE_LEVEL, PAGE_NEXT, PAGE_PREV, ROTATE_CCW, ROTATE_CW, SELECT_ALL, ZOOM_IN,
- ZOOM_OUT,
- };
- // Registered commands first (always in sync with the real bindings), then the
- // keys the document view handles itself.
- let mut rows: Vec<(String, String)> = pdfcraft_engine::commands::COMMANDS
- .iter()
- .filter_map(|c| {
- c.shortcut.map(|k| (crate::commands::shortcut_label(ui.ctx(), k), tl!(c.label).trim_end_matches('…').to_string()))
- })
- .collect();
- let key = |s: pdfcraft_engine::commands::Shortcut| crate::commands::shortcut_label(ui.ctx(), s);
- let pair = |a, b| format!("{} / {}", key(a), key(b));
- // Key names, translated where a catalog has them (not scanned as UI literals).
- let named = |k: &str| tl!(k).to_string();
- let scroll = crate::i18n::fmt(
- tl!("Zoom in / out (also pinch or {key}-scroll)"),
- &[("key", crate::commands::command_modifier_label(ui.ctx()))],
- );
- for (k, v) in [
- (pair(FIND_NEXT, FIND_PREV), tl!("Next / previous match").to_string()),
- (key(COPY), tl!("Copy selected text").to_string()),
- (named("Double-click"), tl!("Select a word").to_string()),
- (crate::i18n::key_name("Esc").to_string(), tl!("Clear selection / close find").to_string()),
- (key(ACTUAL_SIZE), tl!("Actual size").to_string()),
- (key(PAGE_LEVEL), tl!("Zoom to page level").to_string()),
- (key(FIT_WIDTH), tl!("Fit to width").to_string()),
- (pair(ZOOM_IN, ZOOM_OUT), scroll),
- (pair(ROTATE_CW, ROTATE_CCW), tl!("Rotate view").to_string()),
- (format!("{} / {}", crate::i18n::key_name("Home"), crate::i18n::key_name("End")), tl!("First / last page").to_string()),
- (format!("← / →, {}", pair(PAGE_PREV, PAGE_NEXT)), tl!("Previous / next page").to_string()),
- (named("V"), tl!("Select (V)").to_string()),
- (named("H / Space (hold)"), tl!("Hand (H)").to_string()),
- (crate::i18n::key_name("Delete").to_string(), tl!("Delete selected pages (Organize)").to_string()),
- (key(SELECT_ALL), tl!("Select all pages (Organize)").to_string()),
- ] {
- rows.push((k, v));
+ ui.end_row();
+ if d.props.is_empty() {
+ ui.label("");
+ ui.label(egui::RichText::new(tl!("None")).color(t.text_faint));
+ ui.end_row();
}
- egui::ScrollArea::vertical().max_height(460.0).show(ui, |ui| {
- egui::Grid::new("keys").num_columns(2).spacing([24.0, 6.0]).show(ui, |ui| {
- for (k, v) in &rows {
- ui.label(egui::RichText::new(k).font(egui::FontId::monospace(12.5)));
- ui.label(v);
- ui.end_row();
- }
+ // A click on a property fills the fields with it; values are document data.
+ let mut picked = None;
+ for (k, v) in &d.props {
+ if ui.selectable_label(*k == name, k.as_str()).clicked() {
+ picked = Some((k.clone(), v.clone()));
+ }
+ ui.scope(|ui| {
+ ui.set_max_width(420.0);
+ ui.add(egui::Label::new(v.as_str()).wrap());
});
- });
+ ui.end_row();
+ }
+ if let Some((k, v)) = picked {
+ d.name = k;
+ d.value = v;
+ }
}
- Dialog::About => {
- // Tabs About · Contributors · Models (craftrules standards/contributors.md).
- let tab_id = egui::Id::unique("about_tab");
- let mut tab = ui.data_mut(|d| d.get_temp::<u8>(tab_id)).unwrap_or(0);
- ui.horizontal_wrapped(|ui| {
- for (i, label) in ["About", "Contributors", "Models"].into_iter().enumerate() {
- let i = i as u8;
- if widgets::mode_tab(ui, tl!(label), tab == i).clicked() {
- tab = i;
- }
- }
- });
- ui.data_mut(|d| d.insert_temp(tab_id, tab));
- ui.separator();
- match tab {
- 1 => crate::credits::contributors_ui(ui),
- 2 => crate::credits::models_ui(ui),
- _ => {
- ui.horizontal(|ui| {
- widgets::artcraft_mark(ui, 40.0);
- ui.vertical(|ui| {
- ui.label(egui::RichText::new("PdfCraft").font(theme::semibold(20.0)));
- ui.label(crate::i18n::fmt(tl!("Version {v}"), &[("v", &pdfcraft_engine::build_info::version())]));
- });
+ PropsTab::Advanced => {
+ row(ui, "PDF version", i.pdf_version.clone());
+ path_row(ui, "Location", doc.path.clone().unwrap_or_default());
+ row(ui, "File size", format!("{} ({})", human_size(i.file_size), crate::panels::byte_count(i.file_size)));
+ let p = &i.pages[0];
+ row(ui, "Page size", units.size(f64::from(p.width), f64::from(p.height)));
+ row(ui, "Number of pages", i.pages.len().to_string());
+ row(ui, "Tagged PDF", yes(i.tagged));
+ row(ui, "Form fields", i.fields.len().to_string());
+ row(ui, "Comments", i.annotations.len().to_string());
+ row(ui, "Layers", i.layers.len().to_string());
+ row(ui, "Attachments", i.attachments.len().to_string());
+ row(ui, "JavaScript", yes(i.has_javascript));
+ // Each incremental update is a revision; earlier ones open as their own document.
+ let ends = doc.revision_ends();
+ if ends.len() > 1 {
+ ui.label(egui::RichText::new(tl!("Revisions")).color(t.text_muted));
+ // Reserve the full wrapped height before the grid starts the next row.
+ ui.vertical(|ui| {
+ ui.horizontal_wrapped(|ui| {
+ ui.label(ends.len().to_string());
+ for n in (1..ends.len()).rev().take(12) {
+ if ui
+ .small_button(crate::i18n::fmt(tl!("View revision {n}"), &[("n", &n.to_string())]))
+ .on_hover_text(tl!("Open the file as it was saved then"))
+ .clicked()
+ {
+ req.open_revision = Some(n);
+ }
+ }
});
- ui.add_space(6.0);
- ui.label(tl!("A clean-room, open-source PDF application written in Rust. MIT OR Apache-2.0."));
- ui.label(
- egui::RichText::new(
- "Rendering: hayro (bootstrap) · UI: egui · Icons: Lucide (ISC) · Fonts: Inter, JetBrains Mono, Dancing Script (OFL)",
- )
- .color(t.text_muted)
- .small(),
- );
- ui.add_space(12.0);
+ });
+ ui.end_row();
+ }
+ // Reading Options: binding and language.
+ if let Some((_, v)) = app.view_draft.as_mut() {
+ let editable = doc.allows_modification();
+ ui.label(egui::RichText::new(tl!("Binding")).color(t.text_muted));
+ ui.add_enabled_ui(editable, |ui| {
ui.horizontal(|ui| {
- ui.label(egui::RichText::new(tl!("Part of")).color(t.text_muted));
- widgets::artcraft_logo(ui, 16.0);
+ widgets::radio_value(ui, &mut v.right_to_left, false, tl!("Left Edge"));
+ widgets::radio_value(ui, &mut v.right_to_left, true, tl!("Right Edge"));
});
- ui.add_space(6.0);
- if let Some(cmd) = widgets::community_links(ui) {
- link_command = Some(cmd);
- }
+ });
+ ui.end_row();
+ let l = ui.label(egui::RichText::new(tl!("Language")).color(t.text_muted));
+ let mut lang = v.language.clone().unwrap_or_default();
+ if ui
+ .add_enabled(editable, widgets::line(&mut lang).hint_text(tl!("e.g. en-US")).desired_width(160.0))
+ .labelled_by(l.id)
+ .changed()
+ {
+ v.language = (!lang.trim().is_empty()).then(|| lang.trim().to_string());
}
+ ui.end_row();
}
- }
- Dialog::PinTool => {
- ui.horizontal(|ui| {
- ui.label(egui::RichText::new(tl!("Pin tools to Home")).font(theme::semibold(18.0)));
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if widgets::ghost_button(ui, "x", tl!("Close")).clicked() {
- close = true;
+ // What was repaired while reading a damaged file (fidelity: never silent).
+ let repairs = doc.repair_log();
+ row(ui, "Repairs", if repairs.is_empty() { tl!("None").to_string() } else { repairs.len().to_string() });
+ if !repairs.is_empty() {
+ ui.label("");
+ egui::CollapsingHeader::new(tl!("Repair log")).show(ui, |ui| {
+ for r in &repairs {
+ ui.add(egui::Label::new(egui::RichText::new(r).small()).wrap());
}
});
- });
- ui.add_space(4.0);
- ui.label(egui::RichText::new(tl!("Select tools to display on your Home page for quick access.")).color(t.text_muted));
- ui.add_space(10.0);
-
- ui.horizontal(|ui| {
- let (icon_rect, _) = ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::hover());
- crate::icons::paint(ui, icon_rect, "search", 16.0, t.text_muted);
- let search_resp = ui.add(
- egui::TextEdit::singleline(&mut app.pin_tool_search)
- .hint_text(tl!("Search tools…"))
- .desired_width(ui.available_width() - 32.0),
- );
- search_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::TextInput, ui.is_enabled(), tl!("Search tools…")));
- if !app.pin_tool_search.is_empty() && widgets::ghost_button(ui, "x", tl!("Clear search")).clicked() {
- app.pin_tool_search.clear();
- }
- });
- ui.add_space(8.0);
- ui.separator();
- ui.add_space(6.0);
+ ui.end_row();
+ }
+ }
+ // Laid out above, outside the two-column grid.
+ PropsTab::SpaceAudit => {}
+ });
+ });
+ true
+ }
- let query = app.pin_tool_search.trim().to_lowercase();
- egui::ScrollArea::vertical().max_height(380.0).auto_shrink([false, true]).show(ui, |ui| {
- let mut toggle = None;
- for g in pdfcraft_engine::catalog::TOOL_GROUPS {
- let label = tl!(g.label);
- let blurb = g
- .sections
- .first()
- .map(|s| s.items.iter().take(4).map(|i| tl!(i.label)).collect::<Vec<_>>().join(" · "))
- .unwrap_or_default();
- if !query.is_empty()
- && !label.to_lowercase().contains(&query)
- && !blurb.to_lowercase().contains(&query)
- && !g.id.to_lowercase().contains(&query)
- {
- continue;
- }
+ /// `Dialog::Split` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_split(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ ui.label(egui::RichText::new(tl!("Split document")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ let Some((vi, id)) = app.active_ids() else { return false };
+ let n = app.session.get(id).map(|d| d.info.pages.len()).unwrap_or(0);
+ let selected: Vec<usize> = app.views[vi].selected.iter().copied().filter(|p| *p > 0).collect();
+ let marks = app.session.bookmark_splits(id);
+ let draft = &mut app.split_draft;
+ use crate::SplitMode as M;
+ if selected.is_empty() && draft.mode == M::Selection {
+ draft.mode = M::Pages;
+ }
+ widgets::radio_value(ui, &mut draft.mode, M::Pages, tl!("Number of pages"));
+ ui.add_enabled_ui(draft.mode == M::Pages, |ui| {
+ ui.horizontal(|ui| {
+ ui.add_space(24.0);
+ ui.label(tl!("Pages per file"));
+ ui.add(egui::DragValue::new(&mut draft.every).range(1..=n.max(1)));
+ });
+ });
+ widgets::radio_value(ui, &mut draft.mode, M::Size, tl!("File size"));
+ ui.add_enabled_ui(draft.mode == M::Size, |ui| {
+ ui.horizontal(|ui| {
+ ui.add_space(24.0);
+ ui.label(tl!("At most"));
+ ui.add(egui::DragValue::new(&mut draft.size_mb).range(0.05..=2000.0).speed(0.1).suffix(" MB"));
+ });
+ });
+ ui.add_enabled_ui(!marks.is_empty(), |ui| {
+ widgets::radio_value(
+ ui,
+ &mut draft.mode,
+ M::Bookmarks,
+ crate::i18n::fmt(tl!("Top-level bookmarks ({n})"), &[("n", &marks.len().to_string())]),
+ )
+ });
+ ui.add_enabled_ui(!selected.is_empty(), |ui| {
+ widgets::radio_value(ui, &mut draft.mode, M::Selection, tl!("Before each selected page (select pages in Organize)"))
+ });
+ let plan = match draft.mode {
+ M::Pages => crate::SplitPlan::By(pdfcraft_engine::SplitBy::PageCount(draft.every)),
+ M::Selection => crate::SplitPlan::By(pdfcraft_engine::SplitBy::Before(selected)),
+ M::Size => crate::SplitPlan::Size((draft.size_mb * 1_048_576.0) as usize),
+ M::Bookmarks => crate::SplitPlan::Bookmarks,
+ };
+ let files = match &plan {
+ crate::SplitPlan::By(by) => Some(pdfcraft_engine::split_ranges(n, by).len()),
+ crate::SplitPlan::Bookmarks => {
+ Some(pdfcraft_engine::split_ranges(n, &pdfcraft_engine::SplitBy::Before(marks.iter().map(|m| m.0).collect())).len())
+ }
+ crate::SplitPlan::Size(_) => None,
+ };
+ ui.add_space(8.0);
+ let text = match files {
+ Some(f) => {
+ if f == 1 {
+ crate::i18n::fmt(tl!("Creates 1 file from {n} pages."), &[("n", &n.to_string())])
+ } else {
+ crate::i18n::fmt(tl!("Creates {f} files from {n} pages."), &[("f", &f.to_string()), ("n", &n.to_string())])
+ }
+ }
+ None => crate::i18n::fmt(tl!("Each file holds as many of the {n} pages as fit."), &[("n", &n.to_string())]),
+ };
+ ui.label(egui::RichText::new(text).color(t.text_muted));
+ if files.is_none_or(|f| f > 1) {
+ req.split_ready = Some(plan);
+ }
+ true
+ }
- let pinned = app.is_tool_pinned(g.id);
- let (rect, resp) = ui.allocate_exact_size(egui::vec2(ui.available_width(), 44.0), egui::Sense::click());
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, label));
- let press = theme::Press::track(ui, &resp);
- press.wash(ui, rect, 6, false);
- let body = rect.translate(press.offset());
+ /// `Dialog::ReplacePages` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_replace_pages(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ let count = app.active_ids().and_then(|(_, id)| app.session.get(id)).map(|d| d.info.pages.len()).unwrap_or(1).max(1);
+ let Some(d) = app.replace_draft.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ ui.label(egui::RichText::new(tl!("Replace Pages")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ d.to = d.to.clamp(1, count);
+ d.from = d.from.clamp(1, d.to);
+ ui.horizontal(|ui| {
+ let from_label = ui.label(tl!("Original: replace pages"));
+ let from = ui.add(egui::DragValue::new(&mut d.from).range(1..=count)).labelled_by(from_label.id);
+ let to_label = ui.label(tl!("to"));
+ let to = ui.add(egui::DragValue::new(&mut d.to).range(1..=count)).labelled_by(to_label.id);
+ if from.changed() || to.changed() {
+ d.range_edited = true;
+ }
+ ui.label(egui::RichText::new(crate::i18n::fmt(tl!("of {name}"), &[("name", &count.to_string())])).color(t.text_muted));
+ });
+ let n = if d.range_edited { d.to - d.from + 1 } else { d.pages.len().max(1) };
+ let max_start = d.src_pages.saturating_sub(n) + 1;
+ d.src_from = d.src_from.clamp(1, max_start.max(1));
+ ui.horizontal(|ui| {
+ let replacement_label = ui.label(crate::i18n::fmt(tl!("Replacement: pages of {name}"), &[("name", &d.name)]));
+ ui.add(egui::DragValue::new(&mut d.src_from).range(1..=max_start.max(1))).labelled_by(replacement_label.id);
+ ui.label(crate::i18n::fmt(tl!("to {n}"), &[("n", &(d.src_from + n - 1).to_string())]));
+ ui.label(egui::RichText::new(crate::i18n::fmt(tl!("of {name}"), &[("name", &d.src_pages.to_string())])).color(t.text_muted));
+ });
+ let fits = n <= d.src_pages;
+ ui.add_space(6.0);
+ ui.label(
+ egui::RichText::new(if fits {
+ tl!("Only the page content changes: links, comments, form fields and bookmarks on the original pages stay.")
+ } else {
+ tl!("The replacement file doesn't have that many pages.")
+ })
+ .small()
+ .color(t.text_faint),
+ );
+ ui.add_space(10.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if ui.add_enabled_ui(fits, |ui| widgets::pill_button(ui, tl!("OK"), true)).inner.clicked() {
+ req.replace_now = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
- let color = egui::Color32::from_rgb(g.hue[0], g.hue[1], g.hue[2]);
- crate::icons::paint(
- ui,
- egui::Rect::from_min_size(body.min + egui::vec2(8.0, 11.0), egui::vec2(22.0, 22.0)),
- g.icon,
- 20.0,
- color,
- );
+ /// `Dialog::RedactProps` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_redact_props(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ let mut prefs = app.redact_prefs.clone();
+ let (ok, cancel) = crate::redact_ui::props_body(ui, &mut prefs, &t);
+ app.redact_prefs = prefs;
+ if ok && app.redact_prefs.remember {
+ app.redact_defaults = app.redact_prefs.clone();
+ }
+ if cancel && let Some(before) = app.redact_before.take() {
+ app.redact_prefs = before;
+ }
+ if ok {
+ app.redact_before = None;
+ }
+ req.close = ok || cancel;
+ false
+ }
- let btn_w = 78.0;
- let btn_rect =
- egui::Rect::from_center_size(body.right_center() - egui::vec2(btn_w / 2.0 + 8.0, 0.0), egui::vec2(btn_w, 26.0));
- let btn_resp = ui.interact(btn_rect, ui.scope_id().with(("pin_modal_btn", g.id)), egui::Sense::click());
- let btn_hovered = btn_resp.hovered();
+ /// `Dialog::Extract` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_extract(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let (selected, total) = app.active_ids().map_or((Vec::new(), 0), |(i, _)| (app.views[i].target_pages(), app.views[i].page_count()));
+ ui.label(egui::RichText::new(tl!("Extract pages")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ // Page ranges to take instead of the selection (#3593); the hint is the selection.
+ let hint = match crate::combine_ui::range_of(&selected, total) {
+ all if all.is_empty() && total > 1 => format!("1-{total}"),
+ all if all.is_empty() => "1".to_string(),
+ some => some,
+ };
+ ui.horizontal(|ui| {
+ let l = ui.label(tl!("Pages"));
+ ui.add(egui::TextEdit::singleline(&mut app.extract_draft.pages).hint_text(hint).desired_width(240.0)).labelled_by(l.id);
+ });
+ let chosen = app.extract_draft.pages_to_extract(selected, total);
+ match &chosen {
+ Ok(pages) if pages.len() == 1 => {
+ ui.label(tl!("1 page selected."));
+ }
+ Ok(pages) => {
+ ui.label(crate::i18n::fmt(tl!("{n} pages selected."), &[("n", &pages.len().to_string())]));
+ }
+ Err(e) => {
+ ui.colored_label(ui.visuals().error_fg_color, e);
+ }
+ }
+ ui.add_space(4.0);
+ // The name the pages are saved under (#737); separate files add " (page N)".
+ ui.horizontal(|ui| {
+ let l = ui.label(tl!("File name"));
+ ui.add(egui::TextEdit::singleline(&mut app.extract_draft.name).desired_width(240.0)).labelled_by(l.id);
+ });
+ ui.add(widgets::checkbox(&mut app.extract_draft.delete, tl!("Delete pages after extracting")));
+ ui.add(widgets::checkbox(&mut app.extract_draft.separate, tl!("Extract pages as separate files")));
+ ui.add_space(12.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if ui.add_enabled_ui(chosen.is_ok(), |ui| widgets::pill_button(ui, tl!("Extract"), true)).inner.clicked() {
+ req.extract_now = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
- let (btn_fill, btn_stroke, text_color, btn_label) = if pinned {
- (t.accent, egui::Stroke::NONE, egui::Color32::WHITE, tl!("Pinned"))
- } else if btn_hovered {
- (t.hover, egui::Stroke::new(1.0, t.border), t.text, tl!("Pin"))
- } else {
- (egui::Color32::TRANSPARENT, egui::Stroke::new(1.0, t.border), t.text_muted, tl!("Pin"))
- };
- let tip = if pinned { tl!("Unpin from Home") } else { tl!("Pin to Home") };
- btn_resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, pinned, format!("{tip}: {label}")));
+ /// `Dialog::RotatePages` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_rotate_pages(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ use pdfcraft_engine::{PageOrientation as O, PageParity as P};
+ let n = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
+ let d = &mut app.rotate_draft;
+ ui.label(egui::RichText::new(tl!("Rotate Pages")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ egui::Grid::new("rotate-pages").num_columns(2).spacing([12.0, 8.0]).show(ui, |ui| {
+ ui.label(tl!("Direction:"));
+ craft_ui::ComboBox::from_id_salt("rotate-dir")
+ .selected_text(match d.degrees {
+ 270 => tl!("Counterclockwise 90 degrees"),
+ 180 => tl!("180 degrees"),
+ _ => tl!("Clockwise 90 degrees"),
+ })
+ .show_ui(ui, |ui| {
+ ui.selectable_value(&mut d.degrees, 90, tl!("Clockwise 90 degrees"));
+ ui.selectable_value(&mut d.degrees, 270, tl!("Counterclockwise 90 degrees"));
+ ui.selectable_value(&mut d.degrees, 180, tl!("180 degrees"));
+ });
+ ui.end_row();
+ ui.label(tl!("Pages:"));
+ ui.vertical(|ui| {
+ widgets::radio_value(ui, &mut d.which, 0, tl!("All"));
+ widgets::radio_value(ui, &mut d.which, 1, tl!("Selection"));
+ ui.horizontal(|ui| {
+ widgets::radio_value(ui, &mut d.which, 2, tl!("From"));
+ ui.add_enabled(d.which == 2, egui::DragValue::new(&mut d.from).range(1..=n));
+ ui.label(tl!("to"));
+ ui.add_enabled(d.which == 2, egui::DragValue::new(&mut d.to).range(1..=n));
+ ui.label(crate::i18n::fmt(tl!("of {name}"), &[("name", &n.to_string())]));
+ });
+ });
+ ui.end_row();
+ ui.label(tl!("Rotate:"));
+ craft_ui::ComboBox::from_id_salt("rotate-parity")
+ .selected_text(match d.parity {
+ P::Both => tl!("Even and Odd Pages"),
+ P::Even => tl!("Even Pages Only"),
+ P::Odd => tl!("Odd Pages Only"),
+ })
+ .show_ui(ui, |ui| {
+ ui.selectable_value(&mut d.parity, P::Both, tl!("Even and Odd Pages"));
+ ui.selectable_value(&mut d.parity, P::Even, tl!("Even Pages Only"));
+ ui.selectable_value(&mut d.parity, P::Odd, tl!("Odd Pages Only"));
+ });
+ ui.end_row();
+ ui.label("");
+ craft_ui::ComboBox::from_id_salt("rotate-orient")
+ .selected_text(match d.orientation {
+ O::Both => tl!("Landscape and Portrait Pages"),
+ O::Landscape => tl!("Landscape Pages"),
+ O::Portrait => tl!("Portrait Pages"),
+ })
+ .show_ui(ui, |ui| {
+ ui.selectable_value(&mut d.orientation, O::Both, tl!("Landscape and Portrait Pages"));
+ ui.selectable_value(&mut d.orientation, O::Landscape, tl!("Landscape Pages"));
+ ui.selectable_value(&mut d.orientation, O::Portrait, tl!("Portrait Pages"));
+ });
+ ui.end_row();
+ });
+ d.to = d.to.max(d.from);
+ ui.add_space(12.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if widgets::pill_button(ui, tl!("OK"), true).clicked() {
+ req.rotate_now = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
- ui.painter().rect(btn_rect, egui::CornerRadius::same(6), btn_fill, btn_stroke, egui::StrokeKind::Inside);
- ui.painter().text(btn_rect.center(), egui::Align2::CENTER_CENTER, btn_label, theme::medium(12.0), text_color);
+ /// `Dialog::DuplicateField` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_duplicate_field(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let n = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len());
+ let Some(d) = app.duplicate_draft.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ ui.label(egui::RichText::new(tl!("Duplicate Field")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ ui.label(crate::i18n::fmt(tl!("Duplicate \"{name}\" onto:"), &[("name", &d.name)]));
+ widgets::radio_value(ui, &mut d.all, true, tl!("All pages"));
+ ui.horizontal(|ui| {
+ widgets::radio_value(ui, &mut d.all, false, tl!("From"));
+ ui.add_enabled(!d.all, egui::DragValue::new(&mut d.from).range(1..=n));
+ ui.label(tl!("to"));
+ ui.add_enabled(!d.all, egui::DragValue::new(&mut d.to).range(1..=n));
+ ui.label(crate::i18n::fmt(tl!("of {name}"), &[("name", &n.to_string())]));
+ });
+ d.to = d.to.max(d.from);
+ ui.add_space(12.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if widgets::pill_button(ui, tl!("OK"), true).clicked() {
+ let pages: Vec<usize> = if d.all { (0..n).collect() } else { (d.from - 1..d.to.min(n)).collect() };
+ req.duplicate_now = Some(Edit::DuplicateField { name: d.name.clone(), pages });
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
- let max_w = (btn_rect.left() - body.left() - 44.0).max(10.0);
- widgets::row_text(ui, body.min + egui::vec2(38.0, 6.0), label, theme::semibold(13.5), t.text, max_w);
- widgets::row_text(ui, body.min + egui::vec2(38.0, 24.0), &blurb, theme::regular(11.0), t.text_muted, max_w);
+ /// `Dialog::CertificateViewer` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_certificate_viewer(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ ui.set_width(700.0);
+ let trusted = app.session.trusted_certificates().to_vec();
+ let Some(v) = app.cert_viewer.as_mut() else {
+ req.close = true;
+ return false;
+ };
+ let (done, action) = crate::sign_ui::cert_viewer(ui, v, &trusted, &t);
+ req.close = done;
+ match action {
+ Some(crate::sign_ui::CertAction::Trust(c)) => app.trust_certificate(*c),
+ Some(crate::sign_ui::CertAction::Export(c)) => {
+ app.export_certificate(&c);
+ }
+ None => {}
+ }
+ false
+ }
- if btn_resp.clicked() || resp.clicked() {
- toggle = Some(g.id);
- }
- }
- if let Some(id) = toggle {
- app.toggle_pin_tool(id);
- }
- });
+ /// `Dialog::SummarizeComments` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_summarize_comments(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ ui.label(egui::RichText::new(tl!("Summarize Options")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ ui.label(tl!("Choose a layout:"));
+ let _ = ui.add(widgets::radio(true, tl!("Comments only")));
+ ui.add_space(6.0);
+ ui.horizontal(|ui| {
+ ui.label(tl!("Sort comments by:"));
+ craft_ui::ComboBox::from_id_salt("summary-sort").selected_text(tl!(app.summary_sort.name())).show_ui(ui, |ui| {
+ for s in pdfcraft_engine::SummarySort::ALL {
+ ui.selectable_value(&mut app.summary_sort, s, tl!(s.name()));
}
+ });
+ });
+ ui.add_space(12.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if widgets::pill_button(ui, tl!("Create PDF Comment Summary"), true).clicked() {
+ req.summarize_now = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
+
+ /// `Dialog::Revert` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_revert(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let name = app.active_ids().and_then(|(_, id)| app.session.get(id)).map(|d| d.name.clone()).unwrap_or_default();
+ ui.label(egui::RichText::new(tl!("Revert")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ ui.label(crate::i18n::fmt(
+ tl!("Revert to the previously saved version of “{name}”? Changes since then can't be undone afterwards."),
+ &[("name", &name)],
+ ));
+ ui.add_space(12.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if widgets::pill_button(ui, tl!("Revert"), true).clicked() {
+ req.revert_now = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
+
+ /// `Dialog::OverwriteChanged` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_overwrite_changed(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ #[cfg(not(target_arch = "wasm32"))]
+ let name = app.pending_overwrite.as_ref().and_then(|p| app.session.get(p.id)).map(|d| d.name.clone()).unwrap_or_default();
+ #[cfg(target_arch = "wasm32")]
+ let name = String::new();
+ ui.label(egui::RichText::new(tl!("Save over the changed file?")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ ui.label(crate::i18n::fmt(
+ tl!("“{name}” changed on disk after it was opened or last saved. Saving now replaces that newer version with this one."),
+ &[("name", &name)],
+ ));
+ ui.add_space(12.0);
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if widgets::pill_button(ui, tl!("Save anyway"), true).clicked() {
+ req.overwrite_now = true;
+ req.close = true;
}
- ui.add_space(16.0);
- let changed = draft_changes(app).is_some_and(|c| !c.is_empty());
- // One row high: on its own, a right-to-left layout centres the buttons in all the height
- // left, which after a taller Properties tab is the room that tab left behind.
+ if widgets::pill_button(ui, tl!("Save as…"), false).clicked() {
+ req.overwrite_elsewhere = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ });
+ false
+ }
+
+ /// `Dialog::Print` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_print(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ let Some((i, id)) = app.active_ids() else {
+ req.close = true;
+ return false;
+ };
+ if app.print_draft.handling == crate::print_ui::Handling::Label {
+ app.read_label_crops(id);
+ }
+ let Some(doc) = app.session.get(id) else { return false };
+ let sizes: Vec<(f64, f64)> = doc.info.pages.iter().map(|p| (p.width as f64, p.height as f64)).collect();
+ let labels: Vec<String> = doc.info.pages.iter().map(|p| p.label.clone()).collect();
+ let rasters = crate::print_ui::preview_rasters(
+ &app.print_draft,
+ &sizes,
+ &labels,
+ ui.ctx().pixels_per_point(),
+ ui.ctx().input(|i| i.max_texture_side) as f32,
+ );
+ let view = &mut app.views[i];
+ view.set_print_grayscale(app.print_draft.grayscale);
+ view.queue_print_previews(&rasters);
+ let (go, cancel) = crate::print_ui::body(ui, &mut app.print_draft, &t, &sizes, &labels, &mut |p| {
+ view.need_thumbnail(p, true);
+ view.page_preview(p)
+ });
+ req.print_go = go;
+ req.close = go || cancel;
+ false
+ }
+
+ /// `Dialog::Signature` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_signature(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ let (go, cancel, browse) =
+ crate::fill_sign::signature_pad(ui, &t, &mut app.signature_draft, &mut app.signature_preview, &mut app.signature_photo);
+ req.apply = go;
+ if browse {
+ app.pick_signature_image();
+ }
+ if req.apply {
+ let d = std::mem::take(&mut app.signature_draft);
+ let tool = if d.initials {
+ app.initials = d.saved();
+ crate::fill_sign::FillTool::Initials
+ } else {
+ app.signature = d.saved();
+ crate::fill_sign::FillTool::Signature
+ };
+ app.quick_tool = crate::QuickTool::Fill(tool);
+ app.toast = None;
+ }
+ req.close = req.apply || cancel;
+ false
+ }
+
+ /// `Dialog::Preferences` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_preferences(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ // The settings outgrow a short window: scroll them rather than run off the screen.
+ // The OK row stays below the scroll, in reach however far the settings are scrolled.
+ let max = (ui.ctx().content_rect().height() - 120.0 - crate::js_ui::PREFERENCES_BUTTON_ROW).max(crate::js_ui::PREFERENCES_MIN_SCROLL);
+ // A scroll area starts from the room the dialog offers it, and a modal offers more
+ // each frame for the first few, so with OK outside the scroll the dialog would grow
+ // over those frames. Asking for the full height as the least it may scroll in
+ // (which only applies when the settings overflow it) opens the dialog at full size.
+ // A scrollbar of its own: the floating one lies over the right edge of the theme
+ // cards and other full-width rows (#3557).
+ ui.scope(|ui| {
+ ui.spacing_mut().scroll.floating = false;
+ egui::ScrollArea::vertical().max_height(max).min_scrolled_height(max).show(ui, |ui| crate::js_ui::preferences_body(ui, app, &t));
+ });
+ req.close = crate::js_ui::preferences_buttons(ui);
+ false
+ }
+
+ /// `Dialog::NumberPages` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_number_pages(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ use pdfcraft_engine::LabelStyle as L;
+ ui.label(egui::RichText::new(tl!("Number pages")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ let Some((_, id)) = app.active_ids() else { return false };
+ let n = app.session.get(id).map(|d| d.info.pages.len()).unwrap_or(1).max(1);
+ let d = &mut app.number_draft;
+ d.to = d.to.clamp(1, n);
+ d.from = d.from.clamp(1, d.to);
+ egui::Grid::new("number_pages").num_columns(2).spacing([12.0, 8.0]).show(ui, |ui| {
+ ui.label(tl!("Pages"));
ui.horizontal(|ui| {
- ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- if dialog == Dialog::Recovery {
- if widgets::pill_button(ui, tl!("Recover"), true).clicked() {
- recover = Some(true);
- close = true;
- }
- if widgets::pill_button(ui, tl!("Discard"), false).clicked() {
- recover = Some(false);
- close = true;
- }
- } else if dialog == Dialog::PinTool {
- if widgets::pill_button(ui, tl!("Done"), true).clicked() {
- close = true;
- }
- } else if dialog == Dialog::NumberPages {
- if widgets::pill_button(ui, tl!("OK"), true).clicked() {
- apply_number = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- } else if dialog == Dialog::Split {
- if ui.add_enabled_ui(split_ready.is_some(), |ui| widgets::pill_button(ui, tl!("Split"), true)).inner.clicked() {
- split_now = split_ready.clone();
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- } else if changed {
- if widgets::pill_button(ui, tl!("OK"), true).clicked() {
- apply = true;
- close = true;
- }
- if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
- close = true;
- }
- } else if widgets::pill_button(ui, tl!("Close"), true).clicked() {
- close = true;
- }
+ ui.add(egui::DragValue::new(&mut d.from).range(1..=n));
+ ui.label(tl!("to"));
+ ui.add(egui::DragValue::new(&mut d.to).range(1..=n));
+ ui.label(egui::RichText::new(crate::i18n::fmt(tl!("of {name}"), &[("name", &n.to_string())])).color(t.text_muted));
+ });
+ ui.end_row();
+ ui.label(tl!("Style"));
+ let styles = [
+ (L::Decimal, "1, 2, 3"),
+ (L::LowerRoman, "i, ii, iii"),
+ (L::UpperRoman, "I, II, III"),
+ (L::LowerAlpha, "a, b, c"),
+ (L::UpperAlpha, "A, B, C"),
+ (L::None, tl!("None (prefix only)")),
+ ];
+ let current = styles.iter().find(|(s, _)| *s == d.style).map_or("1, 2, 3", |(_, l)| *l);
+ craft_ui::ComboBox::from_id_salt("label_style").selected_text(current).show_ui(ui, |ui| {
+ for (s, l) in styles {
+ ui.selectable_value(&mut d.style, s, l);
+ }
+ });
+ ui.end_row();
+ let l = ui.label(tl!("Prefix"));
+ ui.add(widgets::line(&mut d.prefix).desired_width(160.0)).labelled_by(l.id);
+ ui.end_row();
+ ui.label(tl!("Start"));
+ ui.add(egui::DragValue::new(&mut d.start).range(1..=99_999));
+ ui.end_row();
+ });
+ d.to = d.to.max(d.from);
+ let label = |k: u32| format!("{}{}", d.prefix, d.style.format(k));
+ ui.add_space(8.0);
+ let preview = if d.from == d.to {
+ label(d.start)
+ } else {
+ format!("{}, {} … {}", label(d.start), label(d.start + 1), label(d.start + (d.to - d.from) as u32))
+ };
+ ui.label(
+ egui::RichText::new(crate::i18n::fmt(tl!("Labels: {preview}. Later pages keep their labels."), &[("preview", &preview)])).color(t.text_muted),
+ );
+ req.number_now = Some(Edit::NumberPages { from: d.from - 1, to: d.to - 1, style: d.style, prefix: d.prefix.clone(), first: d.start });
+ true
+ }
+
+ /// `Dialog::Recovery` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_recovery(ui: &mut egui::Ui, app: &mut PdfCraftApp) -> bool {
+ let t = Tokens::get(ui.ctx());
+ ui.horizontal(|ui| {
+ ui.add(crate::icons::image("clock-3", 22.0, t.accent));
+ ui.label(egui::RichText::new(tl!("Recover unsaved documents?")).font(theme::semibold(18.0)));
+ });
+ ui.add_space(6.0);
+ ui.label(tl!("PdfCraft didn't shut down normally. These documents had changes that were autosaved:"));
+ ui.add_space(8.0);
+ let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);
+ egui::Grid::new("recoverable").num_columns(2).spacing([18.0, 6.0]).show(ui, |ui| {
+ for m in &app.recoverable {
+ ui.label(egui::RichText::new(&m.name).font(theme::medium(13.0)));
+ let mins = now.saturating_sub(m.saved_at) / 60;
+ let when = match mins {
+ 0 => tl!("just now").to_string(),
+ 1..=59 => crate::i18n::fmt(tl!("{n} min ago"), &[("n", &mins.to_string())]),
+ _ => crate::i18n::fmt(tl!("{n} h ago"), &[("n", &(mins / 60).to_string())]),
+ };
+ let lock = if m.encrypted { tl!(" · password-protected") } else { "" };
+ ui.label(egui::RichText::new(format!("{when}{lock}")).color(t.text_muted));
+ ui.end_row();
+ }
+ });
+ true
+ }
+
+ /// `Dialog::Shortcuts` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_shortcuts(ui: &mut egui::Ui) -> bool {
+ ui.label(egui::RichText::new(tl!("Keyboard shortcuts")).font(theme::semibold(18.0)));
+ ui.add_space(8.0);
+ use crate::commands::{
+ ACTUAL_SIZE, COPY, FIND_NEXT, FIND_PREV, FIT_WIDTH, PAGE_LEVEL, PAGE_NEXT, PAGE_PREV, ROTATE_CCW, ROTATE_CW, SELECT_ALL, ZOOM_IN, ZOOM_OUT,
+ };
+ // Registered commands first (always in sync with the real bindings), then the
+ // keys the document view handles itself.
+ let mut rows: Vec<(String, String)> = pdfcraft_engine::commands::COMMANDS
+ .iter()
+ .filter_map(|c| c.shortcut.map(|k| (crate::commands::shortcut_label(ui.ctx(), k), tl!(c.label).trim_end_matches('…').to_string())))
+ .collect();
+ let key = |s: pdfcraft_engine::commands::Shortcut| crate::commands::shortcut_label(ui.ctx(), s);
+ let pair = |a, b| format!("{} / {}", key(a), key(b));
+ // Key names, translated where a catalog has them (not scanned as UI literals).
+ let named = |k: &str| tl!(k).to_string();
+ // Alt+← / Alt+→ (`canvas::shortcuts`): not a registered command, which has no Alt.
+ let alt_arrows = if crate::commands::mac_shortcuts(ui.ctx()) {
+ "⌥← / ⌥→".to_owned()
+ } else {
+ let alt = crate::i18n::key_name("Alt");
+ format!("{alt}+← / {alt}+→")
+ };
+ let scroll = crate::i18n::fmt(tl!("Zoom in / out (also pinch or {key}-scroll)"), &[("key", crate::commands::command_modifier_label(ui.ctx()))]);
+ for (k, v) in [
+ (pair(FIND_NEXT, FIND_PREV), tl!("Next / previous match").to_string()),
+ (key(COPY), tl!("Copy selected text").to_string()),
+ (named("Double-click"), tl!("Select a word").to_string()),
+ (crate::i18n::key_name("Esc").to_string(), tl!("Clear selection / close find").to_string()),
+ (key(ACTUAL_SIZE), tl!("Actual size").to_string()),
+ (key(PAGE_LEVEL), tl!("Zoom to page level").to_string()),
+ (key(FIT_WIDTH), tl!("Fit to width").to_string()),
+ (pair(ZOOM_IN, ZOOM_OUT), scroll),
+ (pair(ROTATE_CW, ROTATE_CCW), tl!("Rotate view").to_string()),
+ (format!("{} / {}", crate::i18n::key_name("Home"), crate::i18n::key_name("End")), tl!("First / last page").to_string()),
+ (format!("← / →, {}", pair(PAGE_PREV, PAGE_NEXT)), tl!("Previous / next page").to_string()),
+ (alt_arrows, format!("{} / {}", tl!("Previous view"), tl!("Next view"))),
+ (named("V"), tl!("Select (V)").to_string()),
+ (named("H / Space (hold)"), tl!("Hand (H)").to_string()),
+ (crate::i18n::key_name("Delete").to_string(), tl!("Delete selected pages (Organize)").to_string()),
+ (key(SELECT_ALL), tl!("Select all pages (Organize)").to_string()),
+ ] {
+ rows.push((k, v));
+ }
+ egui::ScrollArea::vertical().max_height(460.0).show(ui, |ui| {
+ egui::Grid::new("keys").num_columns(2).spacing([24.0, 6.0]).show(ui, |ui| {
+ for (k, v) in &rows {
+ ui.label(egui::RichText::new(k).font(egui::FontId::monospace(12.5)));
+ ui.label(v);
+ ui.end_row();
+ }
+ });
+ });
+ true
+ }
+
+ /// `Dialog::About` content. Returns `false` when it has its own buttons and no footer is wanted.
+ fn dialog_about(ui: &mut egui::Ui, req: &mut Pending) -> bool {
+ let t = Tokens::get(ui.ctx());
+ // Tabs About · Contributors · Models.
+ let tab_id = egui::Id::unique("about_tab");
+ let mut tab = ui.data_mut(|d| d.get_temp::<u8>(tab_id)).unwrap_or(0);
+ ui.horizontal_wrapped(|ui| {
+ for (i, label) in ["About", "Contributors", "Models"].into_iter().enumerate() {
+ let i = i as u8;
+ if widgets::mode_tab(ui, tl!(label), tab == i).clicked() {
+ tab = i;
+ }
+ }
+ });
+ ui.data_mut(|d| d.insert_temp(tab_id, tab));
+ ui.separator();
+ match tab {
+ 1 => crate::credits::contributors_ui(ui),
+ 2 => crate::credits::models_ui(ui),
+ _ => {
+ ui.horizontal(|ui| {
+ widgets::artcraft_mark(ui, 40.0);
+ ui.vertical(|ui| {
+ ui.label(egui::RichText::new("PdfCraft").font(theme::semibold(20.0)));
+ ui.label(crate::i18n::fmt(tl!("Version {v}"), &[("v", &pdfcraft_engine::build_info::version())]));
+ });
+ });
+ ui.add_space(6.0);
+ ui.label(tl!("A clean-room, open-source PDF application written in Rust. MIT OR Apache-2.0."));
+ ui.label(
+ egui::RichText::new(
+ "Rendering: hayro (bootstrap) · UI: egui · Icons: Lucide (ISC) · Fonts: Inter, JetBrains Mono, Dancing Script (OFL)",
+ )
+ .color(t.text_muted)
+ .small(),
+ );
+ ui.add_space(12.0);
+ ui.horizontal(|ui| {
+ ui.label(egui::RichText::new(tl!("Part of")).color(t.text_muted));
+ widgets::artcraft_logo(ui, 16.0);
});
+ ui.add_space(6.0);
+ if let Some(cmd) = widgets::community_links(ui) {
+ req.link_command = Some(cmd);
+ }
+ }
+ }
+ true
+ }
+
++fn dialog_pin_tool(ui: &mut egui::Ui, app: &mut PdfCraftApp, req: &mut Pending) -> bool {
++ let t = Tokens::get(ui.ctx());
++ ui.horizontal(|ui| {
++ ui.label(egui::RichText::new(tl!("Pin tools to Home")).font(theme::semibold(18.0)));
++ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
++ if widgets::ghost_button(ui, "x", tl!("Close")).clicked() {
++ req.close = true;
++ }
+ });
+ });
- if let Some(yes) = recover {
++ ui.add_space(4.0);
++ ui.label(egui::RichText::new(tl!("Select tools to display on your Home page for quick access.")).color(t.text_muted));
++ ui.add_space(10.0);
++
++ ui.horizontal(|ui| {
++ let (icon_rect, _) = ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::hover());
++ crate::icons::paint(ui, icon_rect, "search", 16.0, t.text_muted);
++ let search_resp = ui.add(
++ egui::TextEdit::singleline(&mut app.pin_tool_search)
++ .hint_text(tl!("Search tools…"))
++ .desired_width(ui.available_width() - 32.0),
++ );
++ search_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::TextInput, ui.is_enabled(), tl!("Search tools…")));
++ if !app.pin_tool_search.is_empty() && widgets::ghost_button(ui, "x", tl!("Clear search")).clicked() {
++ app.pin_tool_search.clear();
++ }
++ });
++ ui.add_space(8.0);
++ ui.separator();
++ ui.add_space(6.0);
++
++ let query = app.pin_tool_search.trim().to_lowercase();
++ egui::ScrollArea::vertical().max_height(380.0).auto_shrink([false, true]).show(ui, |ui| {
++ let mut toggle = None;
++ for g in pdfcraft_engine::catalog::TOOL_GROUPS {
++ let label = tl!(g.label);
++ let blurb = g
++ .sections
++ .first()
++ .map(|s| s.items.iter().take(4).map(|i| tl!(i.label)).collect::<Vec<_>>().join(" • "))
++ .unwrap_or_default();
++ if !query.is_empty()
++ && !label.to_lowercase().contains(&query)
++ && !blurb.to_lowercase().contains(&query)
++ && !g.id.to_lowercase().contains(&query)
++ {
++ continue;
++ }
++
++ let pinned = app.is_tool_pinned(g.id);
++ let (rect, resp) = ui.allocate_exact_size(egui::vec2(ui.available_width(), 44.0), egui::Sense::click());
++ resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, label));
++ let press = theme::Press::track(ui, &resp);
++ press.wash(ui, rect, 6, false);
++ let body = rect.translate(press.offset());
++
++ let color = egui::Color32::from_rgb(g.hue[0], g.hue[1], g.hue[2]);
++ crate::icons::paint(
++ ui,
++ egui::Rect::from_min_size(body.min + egui::vec2(8.0, 11.0), egui::vec2(22.0, 22.0)),
++ g.icon,
++ 20.0,
++ color,
++ );
++
++ let btn_w = 78.0;
++ let btn_rect =
++ egui::Rect::from_center_size(body.right_center() - egui::vec2(btn_w / 2.0 + 8.0, 0.0), egui::vec2(btn_w, 26.0));
++ let btn_resp = ui.interact(btn_rect, ui.scope_id().with(("pin_modal_btn", g.id)), egui::Sense::click());
++ let btn_hovered = btn_resp.hovered();
++
++ let (btn_fill, btn_stroke, text_color, btn_label) = if pinned {
++ (t.accent, egui::Stroke::NONE, egui::Color32::WHITE, tl!("Pinned"))
++ } else if btn_hovered {
++ (t.hover, egui::Stroke::new(1.0, t.border), t.text, tl!("Pin"))
++ } else {
++ (egui::Color32::TRANSPARENT, egui::Stroke::new(1.0, t.border), t.text_muted, tl!("Pin"))
++ };
++ let tip = if pinned { tl!("Unpin from Home") } else { tl!("Pin to Home") };
++ btn_resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, pinned, format!("{tip}: {label}")));
++
++ ui.painter().rect(btn_rect, egui::CornerRadius::same(6), btn_fill, btn_stroke, egui::StrokeKind::Inside);
++ ui.painter().text(btn_rect.center(), egui::Align2::CENTER_CENTER, btn_label, theme::medium(12.0), text_color);
++
++ let max_w = (btn_rect.left() - body.left() - 44.0).max(10.0);
++ widgets::row_text(ui, body.min + egui::vec2(38.0, 6.0), label, theme::semibold(13.5), t.text, max_w);
++ widgets::row_text(ui, body.min + egui::vec2(38.0, 24.0), &blurb, theme::regular(11.0), t.text_muted, max_w);
++
++ if btn_resp.clicked() || resp.clicked() {
++ toggle = Some(g.id);
++ }
++ }
++ if let Some(id) = toggle {
++ app.toggle_pin_tool(id);
++ }
++ });
++ true
++}
++
+ fn dialog_footer(ui: &mut egui::Ui, app: &mut PdfCraftApp, dialog: Dialog, req: &mut Pending) {
+ ui.add_space(16.0);
+ let changed = draft_changes(app).is_some_and(|c| !c.is_empty());
+ // One row high: on its own, a right-to-left layout centres the buttons in all the height
+ // left, which after a taller Properties tab is the room that tab left behind.
+ ui.horizontal(|ui| {
+ ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
+ if dialog == Dialog::Recovery {
+ if widgets::pill_button(ui, tl!("Recover"), true).clicked() {
+ req.recover = Some(true);
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Discard"), false).clicked() {
+ req.recover = Some(false);
+ req.close = true;
+ }
++ } else if dialog == Dialog::PinTool {
++ if widgets::pill_button(ui, tl!("Done"), true).clicked() {
++ req.close = true;
++ }
+ } else if dialog == Dialog::NumberPages {
+ if widgets::pill_button(ui, tl!("OK"), true).clicked() {
+ req.apply_number = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ } else if dialog == Dialog::Split {
+ if ui.add_enabled_ui(req.split_ready.is_some(), |ui| widgets::pill_button(ui, tl!("Split"), true)).inner.clicked() {
+ req.split_now = req.split_ready.clone();
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ } else if changed {
+ if widgets::pill_button(ui, tl!("OK"), true).clicked() {
+ req.apply = true;
+ req.close = true;
+ }
+ if widgets::pill_button(ui, tl!("Cancel"), false).clicked() {
+ req.close = true;
+ }
+ } else if widgets::pill_button(ui, tl!("Close"), true).clicked() {
+ req.close = true;
+ }
+ })
+ });
+ }
+
+ /// Acts on what the dialog asked for, once the modal is drawn.
+ fn finish_dialog(app: &mut PdfCraftApp, dialog: Dialog, modal_closed: bool, req: &mut Pending) {
+ finish_requests(app, dialog, modal_closed, req);
+ finish_actions(app, req);
+ finish_redactions_and_fields(app, req);
+ finish_edits(app, dialog, req);
+ finish_close(app, dialog, modal_closed, req);
+ }
+
+ /// Runs the one-shot requests the dialog made.
+ fn finish_requests(app: &mut PdfCraftApp, dialog: Dialog, modal_closed: bool, req: &mut Pending) {
+ if let Some(yes) = req.recover {
let keys: Vec<String> = app.recoverable.iter().map(|m| m.key.clone()).collect();
if yes {
app.recover(&keys);
diff --cc crates/ui-egui/src/editing.rs
index 52ec364e9,8def569f5..d4520a15b
--- a/crates/ui-egui/src/editing.rs
+++ b/crates/ui-egui/src/editing.rs
@@@ -27,6 -27,86 +27,86 @@@ fn new_text_typed(v: &crate::DocView) -
v.content.draft.as_ref().is_some_and(|d| d.index.is_none() && !d.text.trim().is_empty())
}
+ /// Keeps the pages (or comment) the user acted on selected, where they now are. `annots_before`
+ /// is a deleted annotation's page's annotations before the delete (empty for other edits).
+ fn select_after_edit(
+ view: &mut crate::DocView,
- edit: Edit,
++ edit: &Edit,
+ info: &pdfcraft_render::DocInfo,
+ annots_before: &[pdfcraft_render::Annotation],
+ quick_tool: crate::QuickTool,
+ ) {
+ match edit {
+ Edit::MovePages { pages, to } => {
+ let n = pages.iter().collect::<std::collections::BTreeSet<_>>().len();
- let to = to.min(info.pages.len() - n);
++ let to = (*to).min(info.pages.len() - n);
+ view.select_pages(&(to..to + n).collect::<Vec<_>>());
+ }
+ Edit::DuplicatePages { pages, at: Some(at) } => {
+ let n = pages.iter().collect::<std::collections::BTreeSet<_>>().len();
- let at = at.min(info.pages.len().saturating_sub(n));
++ let at = (*at).min(info.pages.len().saturating_sub(n));
+ view.select_pages(&(at..at + n).collect::<Vec<_>>());
+ }
- Edit::InsertBlankPage { at, .. } => view.select_pages(&[at.min(info.pages.len() - 1)]),
++ Edit::InsertBlankPage { at, .. } => view.select_pages(&[(*at).min(info.pages.len() - 1)]),
+ Edit::DeletePages { .. } => view.select_pages(&[]),
+ // A stroke drawn with the pen stays unselected, so the selection box and
+ // author popup don't sit over the next stroke (#429).
+ Edit::AddAnnotation(a)
+ if matches!(a.shape, pdfcraft_engine::Shape::Ink { .. })
+ && quick_tool == crate::QuickTool::Comment(crate::comments::CommentTool::Ink) =>
+ {
+ view.comments.selected = None;
+ }
+ Edit::AddAnnotation(a) => {
+ // Select the new comment (appended last among the page's comments).
+ let newest = info.annotations.iter().filter(|x| x.page == a.page && !x.is_reply()).map(|x| x.index).max();
+ view.comments.selected = newest.map(|n| (a.page, n));
+ view.comments.reveal = true;
+ // A new highlight opens its note for typing straight away, as in Acrobat,
+ // unless a note typed into another card is still unsaved.
+ if let (pdfcraft_engine::Shape::TextMarkup { kind: pdfcraft_engine::Markup::Highlight, .. }, Some(n)) = (&a.shape, newest)
+ && a.contents.is_empty()
+ && view.comments.editing.is_none()
+ {
+ view.comments.open_editor(a.page, n, String::new());
+ }
+ }
- Edit::DeleteAnnotation { page, index } => view.comments.annotation_deleted(annots_before, &info.annotations, (page, index)),
++ Edit::DeleteAnnotation { page, index } => view.comments.annotation_deleted(annots_before, &info.annotations, (*page, *index)),
+ _ => {}
+ }
+ }
+
+ /// The pages whose widgets differ between two form states, and the (field, page) pairs whose
+ /// value changed and so must not be drawn from the baked page.
+ fn form_edit_pages(
+ before: &[pdfcraft_engine::FormField],
+ after: &[pdfcraft_engine::FormField],
+ ) -> (std::collections::BTreeSet<usize>, Vec<(String, usize)>) {
+ let mut changed_pages = std::collections::BTreeSet::new();
+ let mut unbaked = Vec::new();
+ for f in after {
+ let pages = |field: &pdfcraft_engine::FormField| field.widgets.iter().filter_map(|w| w.page).collect::<Vec<_>>();
+ match before.iter().find(|old| old.name == f.name) {
+ Some(old) if old == f => {}
+ Some(old) => {
+ changed_pages.extend(pages(f));
+ changed_pages.extend(pages(old));
+ if old.value != f.value {
+ unbaked.extend(pages(f).into_iter().map(|p| (f.name.clone(), p)));
+ }
+ }
+ None => {
+ changed_pages.extend(pages(f));
+ unbaked.extend(pages(f).into_iter().map(|p| (f.name.clone(), p)));
+ }
+ }
+ }
+ for old in before.iter().filter(|old| !after.iter().any(|f| f.name == old.name)) {
+ changed_pages.extend(old.widgets.iter().filter_map(|w| w.page));
+ }
+ (changed_pages, unbaked)
+ }
+
impl PdfCraftApp {
/// Apply an edit to the active document. Returns `true` on success; failures are shown.
pub fn apply_edit(&mut self, edit: Edit) -> bool {
@@@ -140,48 -166,7 +185,11 @@@
view.comments.comment_added();
}
// Keep the pages the user acted on selected, where they now are.
- match &edit {
- Edit::MovePages { pages, to } => {
- let n = pages.iter().collect::<std::collections::BTreeSet<_>>().len();
- let to = *to.min(&(info.pages.len() - n));
- view.select_pages(&(to..to + n).collect::<Vec<_>>());
- }
- Edit::DuplicatePages { pages, at: Some(at) } => {
- let n = pages.iter().collect::<std::collections::BTreeSet<_>>().len();
- let at = *at.min(&(info.pages.len().saturating_sub(n)));
- view.select_pages(&(at..at + n).collect::<Vec<_>>());
- }
- Edit::InsertBlankPage { at, .. } => view.select_pages(&[*at.min(&(info.pages.len() - 1))]),
- Edit::DeletePages { .. } => view.select_pages(&[]),
- // A stroke drawn with the pen stays unselected, so the selection box and
- // author popup don't sit over the next stroke (#429).
- Edit::AddAnnotation(a)
- if matches!(a.shape, pdfcraft_engine::Shape::Ink { .. })
- && self.quick_tool == crate::QuickTool::Comment(crate::comments::CommentTool::Ink) =>
- {
- view.comments.selected = None;
- }
- Edit::AddAnnotation(a) => {
- // Select the new comment (appended last among the page's comments).
- let newest = info.annotations.iter().filter(|x| x.page == a.page && !x.is_reply()).map(|x| x.index).max();
- view.comments.selected = newest.map(|n| (a.page, n));
- view.comments.reveal = true;
- // A new highlight opens its note for typing straight away, as in Acrobat,
- // unless a note typed into another card is still unsaved.
- if let (pdfcraft_engine::Shape::TextMarkup { kind: pdfcraft_engine::Markup::Highlight, .. }, Some(n)) = (&a.shape, newest)
- && a.contents.is_empty()
- && view.comments.editing.is_none()
- {
- view.comments.open_editor(a.page, n, String::new());
- }
- }
- Edit::DeleteAnnotation { .. } => view.comments.selected = None,
- _ => {}
- }
- select_after_edit(view, edit, info, &annots_before, self.quick_tool);
++ select_after_edit(view, &edit, info, &annots_before, self.quick_tool);
+ if let (Some((target, ambiguous_before)), Edit::EditPageImage { page, .. }) = (target_arrange_image, &edit) {
+ let page_images = doc.page_images(*page);
+ re_resolve_image_selection(view, *page, &target, ambiguous_before, &page_images);
+ }
if std::mem::take(&mut view.comments.tool_done) && !self.comment_prefs.pinned {
self.quick_tool = crate::QuickTool::Select;
}
@@@ -242,36 -255,45 +281,66 @@@
}
}
+ /// Redo from the menu or toolbar while a box on the page has typing of its own: the step is
+ /// the box's, as the key's is. What Undo took back in the box comes back; typing the page
+ /// doesn't have yet stays as it is, since applying it first is a new edit, which drops the
+ /// document's Redo. True when the box took the step.
+ fn redo_in_page_box(&mut self) -> bool {
+ let Some(ctx) = self.ctx.clone() else { return false };
+ let Some(view) = self.active.and_then(|i| self.views.get_mut(i)) else { return false };
+ let doc = view.id;
+ if let Some(ed) = view.line_editor.as_mut() {
+ ed.claim_typing(&ctx, doc);
+ let Some(text) = typing_step(&ctx, crate::edit_text_ui::input_id(), &ed.text, false) else { return ed.has_unsaved_text() };
+ let before = std::mem::replace(&mut ed.text, text);
+ ed.selection = None;
+ ed.follow_typed_rows(&ctx, &before);
+ return true;
+ }
+ if let Some(d) = view.content.draft.as_mut() {
+ d.claim_typing(&ctx, doc);
+ let Some(text) = typing_step(&ctx, crate::content_ui::input_id(), &d.text, false) else { return d.has_unapplied_typing() };
+ let before = std::mem::replace(&mut d.text, text);
+ d.selection = None;
+ crate::edit_text_ui::follow_typing(&ctx, crate::content_ui::input_id(), &before, &d.text, &mut [&mut d.style.aligns]);
+ return true;
+ }
+ false
+ }
+
fn history_step(&mut self, undo: bool) {
let Some((i, id)) = self.active_ids() else { return };
+ if !self.close_text_editors(i, id) {
+ return;
+ }
+ let target_image = self.views.get(i).and_then(|v| v.image_selection.as_ref()).and_then(|s| {
+ let doc = self.session.get(id)?;
+ let images = doc.page_images(s.page);
+ let target = images.get(s.index).cloned()?;
+ let ambiguous =
+ images.iter().filter(|img| img.name == target.name && img.object == target.object && rect_matches(img.rect, target.rect)).count() > 1;
+ Some((s.page, target, ambiguous))
+ });
+ let had_selection = self.views.get(i).and_then(|v| v.image_selection.as_ref()).is_some();
-
let result = if undo { self.session.undo(id) } else { self.session.redo(id) };
match result {
Ok(label) => {
- if let Some(doc) = self.session.get(id) {
- self.views[i].document_changed(&doc.info);
+ if let Some(doc) = self.session.get(id)
+ && let Some(view) = self.views.get_mut(i)
+ {
+ view.document_changed(&doc.info);
+ view.picked_image = None;
+ if let Some((page, target, ambiguous_before)) = target_image {
+ let page_images = doc.page_images(page);
+ re_resolve_image_selection(view, page, &target, ambiguous_before, &page_images);
+ } else if had_selection {
+ view.image_selection = None;
+ }
+ }
+ if let Some(view) = self.views.get_mut(i) {
+ view.comments.selected = None;
++ view.comments.history_stepped();
}
- self.views[i].comments.history_stepped();
let label = crate::i18n::edit_label(&label);
if undo {
self.notify_fmt("Undid {label}", &[("label", &label)]);
diff --cc crates/ui-egui/src/lib.rs
index 1d8c4e49d,222bb988e..22a31064f
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -363,11 -372,18 +377,22 @@@ pub struct PdfCraftApp
open_prepare_gate: Option<std::sync::Arc<open_worker::PrepareGate>>,
/// Folders pinned to Home, and what they held when last listed.
pub pinned: folders_ui::PinnedFolders,
+ /// Tools pinned to Home, in user-defined order.
+ pub pinned_tools: Vec<String>,
+ /// Search query for the Pin a tool selection dialog.
+ pub pin_tool_search: String,
pub toast: Option<(String, f64)>,
+ /// The form script message the toast shows now, if it shows one: drawn with its title and
+ /// icon (#4293).
+ pub toast_script: Option<pdfcraft_engine::js::Message>,
+ /// Form script messages waiting for the toast, in the order the scripts showed them: each
+ /// takes the toast when the one before it is gone, so every one is seen (#4294).
+ pub script_messages: std::collections::VecDeque<pdfcraft_engine::js::Message>,
+ /// Form script messages the waiting queue had no room for, since the toast last said how many.
+ pub script_messages_dropped: u64,
+ /// The left-out count the toast is saying now ([`Self::next_script_message`]): put back to be
+ /// said again if another notice replaces the toast before it has been shown its time.
+ pub toast_dropped: u64,
/// Whether the macOS title bar is drawn by us (traffic lights over our tab strip).
pub integrated_titlebar: bool,
/// Set when the desktop leaves the window frame to the app (GNOME on Wayland): the tab strip
diff --cc crates/ui-egui/src/panels/edit.rs
index 43384c14f,e95db85a7..3c09e4ee7
--- a/crates/ui-egui/src/panels/edit.rs
+++ b/crates/ui-egui/src/panels/edit.rs
@@@ -263,11 -266,10 +266,13 @@@ fn format_section(app: &mut PdfCraftApp
.into_iter()
.map(|f| (f.resource, f.face))
.collect();
- let shown = ed.font_shown(&sources);
+ let fmt = ed.selection_formatting(&sources);
+ ed.sync_selection_look(&fmt);
+ let shown = fmt.font_shown;
+ let mixed_size = fmt.size.is_none();
- let out = crate::format_text_ui::panel(ui, t, &mut ed.look, Some(&mut ed.extras), Fonts { sources: Some(&sources), shown, mixed_size });
+ // A selection aligns the rows it touches; the caret alone, the whole paragraph.
+ let paragraphs = crate::format_text_ui::Paragraphs { text: &ed.text, selection: ed.selection.clone(), tidy: false };
- let out = crate::format_text_ui::panel(ui, t, &mut ed.look, Some(&mut ed.extras), Fonts { sources: Some(&sources), shown }, Some(paragraphs));
++ let out = crate::format_text_ui::panel(ui, t, &mut ed.look, Some(&mut ed.extras), Fonts { sources: Some(&sources), shown, mixed_size }, Some(paragraphs));
let mut changed = out.changed;
match out.font {
Some(FontChoice::KeepSource) => {
@@@ -344,10 -361,11 +364,11 @@@
}
}
- /// Format text for added text: the standard families, size, colour, style and alignment.
- fn added_text_format(ui: &mut egui::Ui, t: &Tokens, style: &mut pdfcraft_engine::AddedText) {
+ /// Format text for added text: the standard families, size, colour, style and alignment
+ /// (of the lines `paragraphs` selects, while the text is being typed).
+ fn added_text_format(ui: &mut egui::Ui, t: &Tokens, style: &mut pdfcraft_engine::AddedText, paragraphs: Option<crate::format_text_ui::Paragraphs>) {
let shown = style.family.label().to_string();
- let out = crate::format_text_ui::panel(ui, t, style, None, crate::format_text_ui::Fonts { sources: None, shown, mixed_size: false });
- let out = crate::format_text_ui::panel(ui, t, style, None, crate::format_text_ui::Fonts { sources: None, shown }, paragraphs);
++ let out = crate::format_text_ui::panel(ui, t, style, None, crate::format_text_ui::Fonts { sources: None, shown, mixed_size: false }, paragraphs);
if let Some(crate::format_text_ui::FontChoice::Family(family)) = out.font {
style.family = family;
}
diff --cc crates/ui-egui/src/panels/format_text_ui.rs
index e5b48a5a5,eccef2aa7..5dfb0fee4
--- a/crates/ui-egui/src/panels/format_text_ui.rs
+++ b/crates/ui-egui/src/panels/format_text_ui.rs
@@@ -38,9 -38,49 +38,50 @@@ pub(crate) struct Fonts<'a>
pub sources: Option<&'a [(String, String)]>,
/// The name the list shows for the current choice.
pub shown: String,
+ pub mixed_size: bool,
}
+ /// Text being typed in, for the alignment buttons: with some of it selected, they show and set the
+ /// alignment of the paragraphs (lines between `\n`s) the selection touches; with none, the box's.
+ pub(crate) struct Paragraphs<'a> {
+ pub text: &'a str,
+ /// A byte range of `text`; empty or `None` is nothing selected.
+ pub selection: Option<std::ops::Range<usize>>,
+ /// The box's own alignment is known (added text), so a paragraph set to it keeps no entry of its
+ /// own. An edited paragraph's alignment is only guessed from where its rows sit: its rows keep
+ /// every alignment set.
+ pub tidy: bool,
+ }
+
+ impl Paragraphs<'_> {
+ /// The paragraphs a selection touches, or `None` with nothing selected.
+ fn selected(&self) -> Option<std::ops::RangeInclusive<usize>> {
+ self.selection.clone().filter(|r| !r.is_empty()).map(|r| pdfcraft_engine::paragraph_align::selected(self.text, r))
+ }
+
+ /// The alignment the buttons show: the first selected paragraph's, or the box's.
+ fn shown(&self, look: &AddedText) -> TextAlign {
+ self.selected().map_or(look.align, |which| look.paragraph_align(*which.start()))
+ }
+
+ /// `align` picked: for the selected paragraphs, or the whole box (dropping theirs).
+ fn choose(&self, look: &mut AddedText, align: TextAlign) {
+ if !self.tidy && self.selected().is_some() {
+ return;
+ }
+ match self.selected() {
+ Some(which) => {
+ let count = pdfcraft_engine::paragraph_align::count(self.text);
+ look.aligns = pdfcraft_engine::paragraph_align::set(&look.aligns, count, which, align, self.tidy.then_some(look.align));
+ }
+ None => {
+ look.align = align;
+ look.aligns.clear();
+ }
+ }
+ }
+ }
+
/// What the panel changed: the style (in place) and a font picked from the list.
#[derive(Default)]
pub(crate) struct Outcome {
@@@ -78,9 -125,8 +126,9 @@@ pub(crate) fn panel
widgets::section_title(ui, tl!("Format text"));
// The font, across the panel.
+ ui.spacing_mut().combo_height = 360.0;
let width = ui.available_width();
- let font_resp = egui::ComboBox::from_id_salt("format-font")
- craft_ui::ComboBox::from_id_salt("format-font")
++ let font_resp = craft_ui::ComboBox::from_id_salt("format-font")
.selected_text(fonts.shown.as_str())
.width(width)
.show_ui(ui, |ui| {
@@@ -184,18 -176,7 +232,7 @@@
// Size and colour | bullets, numbering and alignment.
row(ui, |ui| {
- let size_str = if fonts.mixed_size || look.size <= 0.0 { tl!("Mixed").to_string() } else { number(look.size) };
- let size_resp = egui::ComboBox::from_id_salt("format-size")
- .selected_text(size_str)
- .width(64.0)
- .show_ui(ui, |ui| {
- for size in SIZES {
- ui.selectable_value(&mut look.size, size, number(size));
- }
- })
- .response;
- size_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, ui.is_enabled(), tl!("Font size")));
- size_resp.on_hover_text(tl!("Font size"));
- font_size_input(ui, &mut look.size);
++ font_size_input(ui, &mut look.size, fonts.mixed_size);
let swatch = colour_button(ui, t, look.color);
egui::Popup::menu(&swatch).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| {
if let Some(picked) = crate::comments::swatch_grid(ui, Some(look.color)) {
@@@ -264,17 -256,12 +312,17 @@@
labelled_icon(ui, t, "arrow-up-down", tl!("Line spacing"));
let own = tl!("Auto").to_string();
let shown = if e.line_spacing == 0.0 { own.clone() } else { number(e.line_spacing) };
- let spacing_resp = egui::ComboBox::from_id_salt("format-line-spacing")
- craft_ui::ComboBox::from_id_salt("format-line-spacing").selected_text(shown).width(half).show_ui(ui, |ui| {
- ui.selectable_value(&mut e.line_spacing, 0.0, own.as_str());
- for v in LINE_SPACINGS {
- ui.selectable_value(&mut e.line_spacing, v, number(v));
- }
- });
++ let spacing_resp = craft_ui::ComboBox::from_id_salt("format-line-spacing")
+ .selected_text(shown)
+ .width(half)
+ .show_ui(ui, |ui| {
+ ui.selectable_value(&mut e.line_spacing, 0.0, own.as_str());
+ for v in LINE_SPACINGS {
+ ui.selectable_value(&mut e.line_spacing, v, number(v));
+ }
+ })
+ .response;
+ spacing_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, ui.is_enabled(), tl!("Line spacing")));
ui.add_space(8.0);
ui.add_enabled_ui(false, |ui| {
labelled_icon(ui, t, "pilcrow", tl!("Paragraph spacing"));
@@@ -390,6 -376,50 +438,59 @@@ fn colour_button(ui: &mut egui::Ui, t:
crate::theme::hand(resp).on_hover_text(tl!("Colour"))
}
+ /// Commit a typed size once (Enter or leaving the field), rather than one undo step per key.
+ /// The bounds match AddedText validation, so the panel also accepts coloured text below 8 pt.
-fn font_size_input(ui: &mut egui::Ui, size: &mut f64) {
++fn font_size_input(ui: &mut egui::Ui, size: &mut f64, mixed: bool) {
+ ui.scope(|ui| {
+ ui.spacing_mut().item_spacing.x = 2.0;
+ let id = ui.make_persistent_id("font-size-input");
- let (previous, mut text) = ui.data(|d| d.get_temp::<(u64, String)>(id)).unwrap_or_else(|| (size.to_bits(), size.to_string()));
++ let initial_text = || {
++ if mixed || *size <= 0.0 {
++ tl!("Mixed").to_string()
++ } else {
++ size.to_string()
++ }
++ };
++ let (previous, mut text) = ui.data(|d| d.get_temp::<(u64, String)>(id)).unwrap_or_else(|| (size.to_bits(), initial_text()));
+ if previous != size.to_bits() {
- text = size.to_string();
++ text = initial_text();
+ }
+ let field = egui::TextEdit::singleline(&mut text).id(id).desired_width(40.0);
+ let response = ui.add_sized([40.0, ui.spacing().interact_size.y], field).accessible_name(tl!("Font size:"));
+ let cancel = (response.has_focus() || response.lost_focus()) && ui.input(|i| i.key_pressed(egui::Key::Escape));
+ if cancel {
- text = size.to_string();
++ text = initial_text();
+ response.surrender_focus();
+ } else if response.lost_focus() || (response.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))) {
+ if let Some(value) = typed_font_size(&text) {
+ *size = value;
++ text = size.to_string();
++ } else {
++ text = initial_text();
+ }
- text = size.to_string();
+ }
+ let before = *size;
+ let list = icons::button(ui, "chevron-down", PRESET_LIST, false, tl!("Font size"));
+ egui::Popup::menu(&list).show(|ui| {
+ for value in SIZES {
+ if ui.selectable_label(*size == value, format!("{value} pt")).clicked() {
+ *size = value;
+ ui.close();
+ }
+ }
+ });
+ if *size != before {
+ text = size.to_string();
+ }
+ ui.data_mut(|d| d.insert_temp(id, (size.to_bits(), text)));
+ });
+ }
+
+ fn typed_font_size(text: &str) -> Option<f64> {
+ let value = text.trim().parse::<f64>().ok()?;
+ (value.is_finite() && (1.0..=500.0).contains(&value)).then_some(value)
+ }
+
#[cfg(test)]
mod tests {
#[test]
@@@ -401,3 -431,86 +502,86 @@@
assert_eq!(super::number(1.15), "1.15");
}
}
+
+ #[cfg(test)]
+ mod typed_size_tests {
+ use super::*;
+ use egui_kittest::kittest::Queryable;
+
+ #[test]
+ fn typed_sizes_accept_small_fractional_values_and_reject_invalid_input() {
+ for value in ["1", "4", "6.5", " 13.25 ", "500"] {
+ assert_eq!(typed_font_size(value), Some(value.trim().parse().unwrap()));
+ }
+ for value in ["", "0", "-1", "0.5", "501", "NaN", "inf", "1e999", "small"] {
+ assert_eq!(typed_font_size(value), None, "{value}");
+ }
+ }
+
+ #[test]
+ fn typed_size_commits_once_and_invalid_input_preserves_the_style() {
+ let mut h = egui_kittest::Harness::new_ui_state(
+ |ui, state: &mut (f64, usize)| {
+ let before = state.0;
- ui.horizontal(|ui| font_size_input(ui, &mut state.0));
++ ui.horizontal(|ui| font_size_input(ui, &mut state.0, false));
+ if state.0 != before {
+ state.1 += 1;
+ }
+ },
+ (12.0, 0),
+ );
+ h.run();
+ let enter = || egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE };
+ let select_all =
+ || egui::Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND };
+ for (text, expected, changes) in [("6.5", 6.5, 1), ("NaN", 6.5, 1), ("0", 6.5, 1), ("13.25", 13.25, 2)] {
+ h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Font size:").click();
+ h.run();
+ h.event(select_all());
+ h.event(egui::Event::Text(text.into()));
+ h.run_steps(2);
+ assert_eq!(h.state().1, changes - usize::from(text == "6.5" || text == "13.25"), "typing alone does not commit");
+ h.event(enter());
+ h.run_steps(2);
+ assert_eq!(*h.state(), (expected, changes));
+ }
+ }
+
+ #[test]
+ fn typed_size_commits_on_focus_loss_and_escape_discards_the_draft() {
+ let mut h = egui_kittest::Harness::new_ui_state(
+ |ui, state: &mut (f64, usize)| {
+ let before = state.0;
- ui.horizontal(|ui| font_size_input(ui, &mut state.0));
++ ui.horizontal(|ui| font_size_input(ui, &mut state.0, false));
+ let _ = ui.button("Other control");
+ if state.0 != before {
+ state.1 += 1;
+ }
+ },
+ (12.0, 0),
+ );
+ h.run();
+ let select_all =
+ || egui::Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND };
+ h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Font size:").click();
+ h.run();
+ h.event(select_all());
+ h.event(egui::Event::Text("6.5".into()));
+ h.run_steps(2);
+ assert_eq!(*h.state(), (12.0, 0));
+ h.get_by_label("Other control").click();
+ h.run_steps(2);
+ assert_eq!(*h.state(), (6.5, 1));
+ h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Font size:").click();
+ h.run();
+ h.event(select_all());
+ h.event(egui::Event::Text("13.25".into()));
+ h.run_steps(2);
+ h.event(egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE });
+ h.run_steps(2);
+ assert_eq!(*h.state(), (6.5, 1), "Escape must not commit the draft");
+ h.get_by_label("Other control").click();
+ h.run_steps(2);
+ assert_eq!(*h.state(), (6.5, 1), "later focus loss must not revive the cancelled draft");
+ }
+ }
diff --cc crates/ui-egui/src/tools/content_ui.rs
index eff22d04d,e28f20e23..f3d93f685
--- a/crates/ui-egui/src/tools/content_ui.rs
+++ b/crates/ui-egui/src/tools/content_ui.rs
@@@ -332,18 -349,22 +352,22 @@@ fn dragged(r: Rect, grab: Grab, d: egui
}
}
- /// Added text and images keep pointer ownership over a form field while editing content.
- pub(crate) fn select_grabs(xf: &PageXform, ptr: &PagePointer, page: usize, info: &DocInfo, added: &[Added], view: &DocView) -> bool {
- let cv = &view.content;
- if cv.grab.is_some_and(|(grab_page, _, _)| grab_page == page) {
- return true;
- }
- let Some(p) = ptr.within(xf.rect) else { return false };
- on_page(added, page).into_iter().filter(|(_, item)| matches!(item.content, AddedContent::Text(_))).any(|(index, item)| {
- let rect = screen_rect(xf, info, page, item.content.rect());
- rect.expand(2.0).contains(p)
- || cv.selected == Some((page, index)) && handles(rect).into_iter().any(|(corner, _, _)| corner.distance(p) <= 6.0)
- })
+ /// What of the added text and pictures lies under `p` on `page`: the selected item's corners, then
+ /// the topmost item. Placed on the page, over a form field under it.
+ pub(crate) fn hit(xf: &PageXform, info: &DocInfo, page: usize, added: &[Added], view: &DocView, p: Pos2) -> Option<Hit> {
+ if !xf.rect.contains(p) {
+ return None;
+ }
- let items = on_page(added, page);
++ let items: Vec<(usize, &Added)> = on_page(added, page).into_iter().filter(|(_, a)| matches!(a.content, AddedContent::Text(_))).collect();
+ let selected = view.content.selected.filter(|s| s.0 == page).and_then(|(_, si)| items.iter().find(|(i, _)| *i == si));
+ if let Some((index, a)) = selected
+ && let Some((_, left, top)) = handles(screen_rect(xf, info, page, a.content.rect())).into_iter().find(|(c, ..)| c.distance(p) <= 6.0)
+ {
+ let icon = if left == top { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw };
+ return Some(Hit::new(Owner::Added { index: *index, corner: Some((left, top)) }, Layer::Handle, icon));
+ }
+ let (index, _) = items.iter().rev().find(|(_, a)| screen_rect(xf, info, page, a.content.rect()).expand(2.0).contains(p))?;
+ Some(Hit::new(Owner::Added { index: *index, corner: None }, Layer::Placed, egui::CursorIcon::Move))
}
/// Pointer input on one page in Edit mode (`adding_text` with the Text tool). `true` when it
diff --cc crates/ui-egui/src/tools/edit_text_ui.rs
index f47ebcedd,768af2059..4f696dbd5
--- a/crates/ui-egui/src/tools/edit_text_ui.rs
+++ b/crates/ui-egui/src/tools/edit_text_ui.rs
@@@ -1,1519 -1,1197 +1,1807 @@@
--//! Edit a PDF ▸ Edit text & images: boxes around the paragraphs and images already on the page.
--//! Click a paragraph to edit it in place (⌘Enter or clicking away applies and rewraps it to the
--//! box, Esc cancels); drag it to move it, or drag the handle on its left or right edge to rewrap
--//! it to a new width. Click an image to select it: drag to move, drag a corner to resize (keeping
--//! its proportions), right-click for rotate, flip, replace, save and delete; Delete removes it.
--
--use std::collections::BTreeMap;
--use std::sync::Arc;
--
- use egui::{Color32, CornerRadius, FontFamily, FontId, Pos2, Rect, Stroke, pos2};
-use egui::{Color32, CornerRadius, FontFamily, FontId, Pos2, Rect, Stroke};
--use pdfcraft_engine::{Edit, ObjectKind, ObjectTarget};
--use pdfcraft_render::DocInfo;
--
- use crate::canvas::{DocView, PageXform};
-use crate::canvas::{DocView, PageToolContext, PageXform};
-use crate::page_cursor::Layer;
--use crate::page_pointer::PagePointer;
-use crate::page_router::{Hit, Owner, ParagraphPart};
--
--mod background;
--mod image;
--mod layout;
--
--pub(crate) use background::TextBackground;
- pub use image::{CropMode, DragTarget, ImageAction, ImageDrag, ImageHandle, ImageSelection, image_menu_id};
- pub(crate) use image::{Matrix, image_context_menu, image_input, image_owns_pointer, quad_contains, user_to_screen};
-pub use image::{CropMode, ImageSelection};
-pub(crate) use image::{ImageAction, ImageCx, body_hit as picture_hit, handle_hit as picture_handle_hit, image_input};
--#[cfg(test)]
--use image::{rotate_knob, turn_degrees};
--#[cfg(test)]
--use layout::{DARK_EDITOR_FILL, contrast};
--pub(crate) use layout::{caret_color_on, editor_fill, editor_font, install_source_fonts};
- use layout::{color32, editor_galley, editor_job, look_of, luminance, source_look, unseen};
-use layout::{color32, editor_galley, editor_job, look_of, luminance, unseen};
--
--const ACCENT: Color32 = Color32::from_rgb(0x14, 0x73, 0xE6);
--
--#[derive(Clone, Copy, PartialEq)]
--enum TextTarget {
-- Block(usize),
-- Added(usize),
--}
--
--/// How many faint Edit-text outlines are drawn per page per frame. CAD plots can draw
--/// ~1000 tiny images on one page; stroking all of them every frame (plus a hover query
--/// per box) stalls the UI. Past this, only the selected and hovered boxes are outlined:
--/// hit-testing still covers every image, so nothing becomes unselectable.
- pub(crate) const MAX_OUTLINES: usize = 250;
-
- pub fn is_bulleted(text: &str) -> bool {
- let mut lines = text.lines();
- let Some(first) = lines.next() else { return false };
- first.trim_start().starts_with("• ") || first.trim_start().starts_with("- ")
- }
-
- pub fn is_numbered(text: &str) -> bool {
- let mut lines = text.lines();
- let Some(first) = lines.next() else { return false };
- let t = first.trim_start();
- let num_end = t.find('.').unwrap_or(0);
- num_end > 0 && t[..num_end].chars().all(|c| c.is_ascii_digit()) && t[num_end..].starts_with(". ")
- }
-
- pub fn toggle_bullet_list(text: &mut String) -> bool {
- let has_trailing_crlf = text.ends_with("\r\n");
- let has_trailing_newline = text.ends_with('\n');
- let lines: Vec<&str> = text.lines().collect();
- if lines.is_empty() {
- return false;
- }
- let all_bulleted = lines.iter().all(|l| {
- let t = l.trim_start();
- t.is_empty() || t.starts_with("• ") || t.starts_with("- ")
- });
- let mut new_text = String::with_capacity(text.len() + 16);
- for (i, line) in lines.iter().enumerate() {
- if i > 0 {
- new_text.push('\n');
- }
- let trimmed = line.trim_start();
- if all_bulleted {
- if let Some(rest) = trimmed.strip_prefix("• ").or_else(|| trimmed.strip_prefix("- ")) {
- new_text.push_str(rest);
- } else {
- new_text.push_str(line);
- }
- } else if trimmed.is_empty() || trimmed.starts_with("• ") {
- new_text.push_str(line);
- } else {
- new_text.push_str("• ");
- new_text.push_str(line);
- }
- }
- if has_trailing_crlf {
- new_text.push_str("\r\n");
- } else if has_trailing_newline {
- new_text.push('\n');
- }
- if new_text != *text {
- *text = new_text;
- true
- } else {
- false
- }
- }
-
- pub fn toggle_numbered_list(text: &mut String) -> bool {
- let has_trailing_crlf = text.ends_with("\r\n");
- let has_trailing_newline = text.ends_with('\n');
- let lines: Vec<&str> = text.lines().collect();
- if lines.is_empty() {
- return false;
- }
- let is_num_line = |l: &str| {
- let t = l.trim_start();
- if t.is_empty() {
- return true;
- }
- let dot_idx = t.find('.').unwrap_or(0);
- dot_idx > 0 && t[..dot_idx].chars().all(|c| c.is_ascii_digit()) && t[dot_idx..].starts_with(". ")
- };
- let all_numbered = lines.iter().all(|l| is_num_line(l));
- let mut new_text = String::with_capacity(text.len() + 16);
- let mut num = 1;
- for (i, line) in lines.iter().enumerate() {
- if i > 0 {
- new_text.push('\n');
- }
- let t = line.trim_start();
- if all_numbered {
- if let Some(dot_idx) = t.find('.')
- && dot_idx > 0
- && t[..dot_idx].chars().all(|c| c.is_ascii_digit())
- && t[dot_idx..].starts_with(". ")
- {
- new_text.push_str(t.get(dot_idx + 2..).unwrap_or(""));
- continue;
- }
- new_text.push_str(line);
- } else if t.is_empty() {
- new_text.push_str(line);
- } else {
- use std::fmt::Write as _;
- let _ = write!(new_text, "{num}. ");
- new_text.push_str(t);
- num += 1;
- }
- }
- if has_trailing_crlf {
- new_text.push_str("\r\n");
- } else if has_trailing_newline {
- new_text.push('\n');
- }
- if new_text != *text {
- *text = new_text;
- true
- } else {
- false
- }
- }
-
- pub fn char_to_superscript(c: char) -> char {
- match c {
- '0' => '⁰',
- '1' => '¹',
- '2' => '²',
- '3' => '³',
- '4' => '⁴',
- '5' => '⁵',
- '6' => '⁶',
- '7' => '⁷',
- '8' => '⁸',
- '9' => '⁹',
- '+' => '⁺',
- '-' | '−' => '⁻',
- '=' => '⁼',
- '(' => '⁽',
- ')' => '⁾',
- 'a' => 'ᵃ',
- 'b' => 'ᵇ',
- 'c' => 'ᶜ',
- 'd' => 'ᵈ',
- 'e' => 'ᵉ',
- 'f' => 'ᶠ',
- 'g' => 'ᵍ',
- 'h' => 'ʰ',
- 'i' => 'ⁱ',
- 'j' => 'ʲ',
- 'k' => 'ᵏ',
- 'l' => 'ˡ',
- 'm' => 'ᵐ',
- 'n' => 'ⁿ',
- 'o' => 'ᵒ',
- 'p' => 'ᵖ',
- 'r' => 'ʳ',
- 's' => 'ˢ',
- 't' => 'ᵗ',
- 'u' => 'ᵘ',
- 'v' => 'ᵛ',
- 'w' => 'ʷ',
- 'x' => 'ˣ',
- 'y' => 'ʸ',
- 'z' => 'ᶻ',
- 'A' => 'ᴬ',
- 'B' => 'ᴮ',
- 'D' => 'ᴰ',
- 'E' => 'ᴱ',
- 'G' => 'ᴳ',
- 'H' => 'ᴴ',
- 'I' => 'ᴵ',
- 'J' => 'ᴶ',
- 'K' => 'ᴷ',
- 'L' => 'ᴸ',
- 'M' => 'ᴹ',
- 'N' => 'ᴺ',
- 'O' => 'ᴼ',
- 'P' => 'ᴾ',
- 'R' => 'ᴿ',
- 'T' => 'ᵀ',
- 'U' => 'ᵁ',
- 'V' => 'ⱽ',
- 'W' => 'ᵂ',
- other => other,
- }
- }
-
- pub fn char_from_superscript(c: char) -> char {
- match c {
- '⁰' => '0',
- '¹' => '1',
- '²' => '2',
- '³' => '3',
- '⁴' => '4',
- '⁵' => '5',
- '⁶' => '6',
- '⁷' => '7',
- '⁸' => '8',
- '⁹' => '9',
- '⁺' => '+',
- '⁻' => '-',
- '⁼' => '=',
- '⁽' => '(',
- '⁾' => ')',
- 'ᵃ' => 'a',
- 'ᵇ' => 'b',
- 'ᶜ' => 'c',
- 'ᵈ' => 'd',
- 'ᵉ' => 'e',
- 'ᶠ' => 'f',
- 'ᵍ' => 'g',
- 'ʰ' => 'h',
- 'ⁱ' => 'i',
- 'ʲ' => 'j',
- 'ᵏ' => 'k',
- 'ˡ' => 'l',
- 'ᵐ' => 'm',
- 'ⁿ' => 'n',
- 'ᵒ' => 'o',
- 'ᵖ' => 'p',
- 'ʳ' => 'r',
- 'ˢ' => 's',
- 'ᵗ' => 't',
- 'ᵘ' => 'u',
- 'ᵛ' => 'v',
- 'ʷ' => 'w',
- 'ˣ' => 'x',
- 'ʸ' => 'y',
- 'ᶻ' => 'z',
- 'ᴬ' => 'A',
- 'ᴮ' => 'B',
- 'ᴰ' => 'D',
- 'ᴱ' => 'E',
- 'ᴳ' => 'G',
- 'ᴴ' => 'H',
- 'ᴵ' => 'I',
- 'ᴶ' => 'J',
- 'ᴷ' => 'K',
- 'ᴸ' => 'L',
- 'ᴹ' => 'M',
- 'ᴺ' => 'N',
- 'ᴼ' => 'O',
- 'ᴾ' => 'P',
- 'ᴿ' => 'R',
- 'ᵀ' => 'T',
- 'ᵁ' => 'U',
- 'ⱽ' => 'V',
- 'ᵂ' => 'W',
- other => other,
- }
- }
-
- pub fn char_to_subscript(c: char) -> char {
- match c {
- '0' => '₀',
- '1' => '₁',
- '2' => '₂',
- '3' => '₃',
- '4' => '₄',
- '5' => '₅',
- '6' => '₆',
- '7' => '₇',
- '8' => '₈',
- '9' => '₉',
- '+' => '₊',
- '-' | '−' => '₋',
- '=' => '₌',
- '(' => '₍',
- ')' => '₎',
- 'a' => 'ₐ',
- 'e' => 'ₑ',
- 'h' => 'ₕ',
- 'i' => 'ᵢ',
- 'j' => 'ⱼ',
- 'k' => 'ₖ',
- 'l' => 'ₗ',
- 'm' => 'ₘ',
- 'n' => 'ₙ',
- 'o' => 'ₒ',
- 'p' => 'ₚ',
- 'r' => 'ᵣ',
- 's' => 'ₛ',
- 't' => 'ₜ',
- 'u' => 'ᵤ',
- 'v' => 'ᵥ',
- 'x' => 'ₓ',
- other => other,
- }
- }
-
- pub fn char_from_subscript(c: char) -> char {
- match c {
- '₀' => '0',
- '₁' => '1',
- '₂' => '2',
- '₃' => '3',
- '₄' => '4',
- '₅' => '5',
- '₆' => '6',
- '₇' => '7',
- '₈' => '8',
- '₉' => '9',
- '₊' => '+',
- '₋' => '-',
- '₌' => '=',
- '₍' => '(',
- '₎' => ')',
- 'ₐ' => 'a',
- 'ₑ' => 'e',
- 'ₕ' => 'h',
- 'ᵢ' => 'i',
- 'ⱼ' => 'j',
- 'ₖ' => 'k',
- 'ₗ' => 'l',
- 'ₘ' => 'm',
- 'ₙ' => 'n',
- 'ₒ' => 'o',
- 'ₚ' => 'p',
- 'ᵣ' => 'r',
- 'ₛ' => 's',
- 'ₜ' => 't',
- 'ᵤ' => 'u',
- 'ᵥ' => 'v',
- 'ₓ' => 'x',
- other => other,
- }
- }
-
- fn snap_char_boundaries(s: &str, start: usize, end: usize) -> (usize, usize) {
- let mut s_idx = start.min(s.len());
- while s_idx > 0 && !s.is_char_boundary(s_idx) {
- s_idx -= 1;
- }
- let mut e_idx = end.min(s.len());
- while e_idx < s.len() && !s.is_char_boundary(e_idx) {
- e_idx += 1;
- }
- (s_idx, e_idx)
- }
-
- pub fn toggle_superscript(ed: &mut LineEditor) -> bool {
- let (s_pos, e_pos) = if let Some(r) = &ed.selection { (r.start.min(ed.text.len()), r.end.min(ed.text.len())) } else { (0, ed.text.len()) };
- let (start, end) = snap_char_boundaries(&ed.text, s_pos, e_pos);
- if start >= end {
- return false;
- }
- let target = &ed.text[start..end];
- let all_sup = target.chars().all(|c| char_from_superscript(c) != c);
- let converted: String =
- if all_sup { target.chars().map(char_from_superscript).collect() } else { target.chars().map(char_to_superscript).collect() };
- if converted != target {
- let conv_len = converted.len();
- ed.text.replace_range(start..end, &converted);
- if ed.selection.is_some() {
- ed.selection = Some(start..start + conv_len);
- }
- true
- } else {
- false
- }
- }
-
- pub fn toggle_subscript(ed: &mut LineEditor) -> bool {
- let (s_pos, e_pos) = if let Some(r) = &ed.selection { (r.start.min(ed.text.len()), r.end.min(ed.text.len())) } else { (0, ed.text.len()) };
- let (start, end) = snap_char_boundaries(&ed.text, s_pos, e_pos);
- if start >= end {
- return false;
- }
- let target = &ed.text[start..end];
- let all_sub = target.chars().all(|c| char_from_subscript(c) != c);
- let converted: String = if all_sub { target.chars().map(char_from_subscript).collect() } else { target.chars().map(char_to_subscript).collect() };
- if converted != target {
- let conv_len = converted.len();
- ed.text.replace_range(start..end, &converted);
- if ed.selection.is_some() {
- ed.selection = Some(start..start + conv_len);
- }
- true
- } else {
- false
- }
- }
-const MAX_OUTLINES: usize = 250;
--
--/// A paragraph being edited.
--#[derive(Clone, Debug, PartialEq)]
--pub struct LineEditor {
-- pub selection: Option<std::ops::Range<usize>>,
-- pub font_resource: Option<String>,
-- pub page: usize,
-- pub block: usize,
-- pub text: String,
-- original: String,
-- rect: Rect,
-- /// The original PDF rectangle, in user space. It is converted again each frame so the editor
-- /// stays attached while the page is zoomed, scrolled, or rotated.
-- source_rect: [f32; 4],
-- multiline: bool,
-- source: Option<(pdfcraft_engine::TextBlock, Vec<pdfcraft_engine::TextLine>)>,
-- pub(crate) fonts: BTreeMap<String, String>,
-- /// The draft extends outside the source container; its frame follows the live draft bounds.
-- pub overflow: bool,
-- /// The initial source container width in screen pixels.
-- max_width: f32,
-- /// Current screen-space size. Recomputed from the current page transform before painting.
-- size: f32,
-- /// Take the keyboard focus on the next frame.
-- pub(crate) focus: bool,
-- /// Whether the text box had the keyboard at the end of the editor's last frame
-- /// ([`lost_focus_once`]).
-- had_focus: bool,
-- /// The Format text panel's values, and what the paragraph had (to send only changes).
-- pub look: pdfcraft_engine::AddedText,
-- look0: pdfcraft_engine::AddedText,
-- /// A standard family was picked from the font list. The family guessed from a PDF font name
-- /// (Calibri reads as Helvetica) can be the one picked, so comparing `look` with `look0` alone
-- /// would miss the choice.
-- family_chosen: bool,
-- /// Underline, line spacing (× size; 0 = the paragraph's own), character spacing (pt) and
-- /// horizontal scale (%), and what they were.
-- pub extras: Extras,
-- extras0: Extras,
- /// The text the box's own Undo history starts from; see [`forget_typing_before`].
- typing_from: Option<String>,
-}
-
-/// The paragraph editor's text box.
-pub(crate) fn input_id() -> egui::Id {
- egui::Id::unique("edit-text-line-input")
-}
-
-/// The page text box `id` keeps its own Undo history, across every paragraph or box it has shown
-/// in every tab (they share the id). Once the page has `text` (the box opened on it, or formatting
-/// applied it), that history is cleared: Undo in the box takes back only what was typed since, and
-/// then reaches the document. Kept, it put back text from another paragraph, or the whole paragraph
-/// as it was before a format change, into a box showing the page as it is now. `from` is the box's
-/// own record of that; which document `doc` last used the history is kept beside it, since a box
-/// in another tab on the same text would otherwise take that document's typing with its Redo.
-/// When another document's box takes the text box, the box it leaves keeps its history (and
-/// caret) aside, and gets it back on its return, so its typing can still be undone there. Each
-/// box claims the text box when it is drawn, and Undo and Redo from the menu claim it first too,
-/// since they can run before the box is drawn again after a tab switch.
-pub(crate) fn forget_typing_before(ctx: &egui::Context, id: egui::Id, doc: pdfcraft_engine::DocId, from: &mut Option<String>, text: &str) {
- let owner = id.with("typing-doc");
- let last = ctx.data(|d| d.get_temp::<pdfcraft_engine::DocId>(owner));
- if from.as_deref() == Some(text) && last == Some(doc) {
- return;
- }
- let aside = |doc: pdfcraft_engine::DocId| id.with(("typing-aside", doc));
- let aligns = id.with(ALIGN_HISTORY);
- if let Some(left) = last.filter(|l| *l != doc)
- && let Some(state) = egui::TextEdit::load_state(ctx, id)
- {
- // A copy of its own: a state's history is shared between its clones, and the next box
- // clears it in place. The rows' alignments by text go with it.
- let mut kept = egui::text_edit::TextEditState::default();
- kept.cursor = state.cursor;
- kept.set_undoer(state.undoer());
- ctx.data_mut(|d| {
- let rows = d.get_temp::<AlignHistory>(aligns).unwrap_or_default();
- d.insert_temp(aside(left), (kept, rows));
- });
- }
- // Only the same box, still on the text it opened on, takes its history back.
- let back =
- ctx.data_mut(|d| d.remove_temp::<(egui::text_edit::TextEditState, AlignHistory)>(aside(doc))).filter(|_| from.as_deref() == Some(text));
- // A box new to the text box starts with nothing of the last one's: no history, caret,
- // selection or rows' alignments by text.
- let (state, rows) = back.unwrap_or_default();
- egui::TextEdit::store_state(ctx, id, state);
- ctx.data_mut(|d| {
- d.insert_temp(owner, doc);
- d.insert_temp(aligns, rows);
- });
- *from = Some(text.to_owned());
-}
-
-/// Each text a page box has shown through its own typing, with its rows' alignments then, oldest
-/// last: egui's history keeps only the text and the caret.
-type AlignHistory = Vec<(String, Vec<Vec<Option<pdfcraft_engine::TextAlign>>>)>;
-const ALIGN_HISTORY: &str = "aligns-by-text";
-/// As many texts as egui's own history keeps.
-const ALIGN_HISTORY_LEN: usize = 100;
-
-/// The page text box `id` went from `before` to `after` through its own typing (typing, Undo or
-/// Redo), and each list of row alignments in `aligns` follows. A text the box showed before gets
-/// back the alignments it had then, so Undo bringing back a deleted row brings back its
-/// alignment; a new text follows the edit ([`pdfcraft_engine::paragraph_align::remap`]). An
-/// alignment picked since makes the older texts' alignments stale, and they are dropped.
-pub(crate) fn follow_typing(
- ctx: &egui::Context,
- id: egui::Id,
- before: &str,
- after: &str,
- aligns: &mut [&mut Vec<Option<pdfcraft_engine::TextAlign>>],
-) {
- let now: Vec<Vec<_>> = aligns.iter().map(|a| a.to_vec()).collect();
- // Where the edit left the caret, as a byte of `after`: it says which of two same rows changed.
- let caret = egui::TextEdit::load_state(ctx, id)
- .and_then(|s| s.cursor.char_range())
- .map(|r| after.char_indices().nth(r.primary.index.into()).map_or(after.len(), |(at, _)| at));
- let next = ctx.data_mut(|d| {
- let seen = d.get_temp_mut_or_default::<AlignHistory>(id.with(ALIGN_HISTORY));
- if seen.iter().any(|(text, was)| text == before && *was != now) {
- seen.clear();
- }
- remember(seen, before, now.clone());
- let next = match seen.iter().rev().find(|(text, _)| text == after) {
- Some((_, was)) if was.len() == now.len() => was.clone(),
- _ => now.iter().map(|a| pdfcraft_engine::paragraph_align::remap_at(before, after, a, caret)).collect(),
- };
- remember(seen, after, next.clone());
- next
- });
- for (slot, a) in aligns.iter_mut().zip(next) {
- **slot = a;
- }
-}
-
-fn remember(seen: &mut AlignHistory, text: &str, aligns: Vec<Vec<Option<pdfcraft_engine::TextAlign>>>) {
- seen.retain(|(t, _)| t != text);
- seen.push((text.to_owned(), aligns));
- if seen.len() > ALIGN_HISTORY_LEN {
- seen.remove(0);
- }
-}
-
-/// Select `range` (bytes of `text`) in the page text box `id` again. egui keeps a box's selection
-/// only while it has the focus, so a click on Format text drops it; given back as formatting
-/// returns the focus, it stays shown, and a second choice reaches the same text.
-pub(crate) fn reselect(ctx: &egui::Context, id: egui::Id, text: &str, range: &std::ops::Range<usize>) {
- let chars = |at: usize| text.get(..at).map(|s| s.chars().count());
- let (Some(start), Some(end)) = (chars(range.start), chars(range.end)) else { return };
- if start < end {
- let mut state = egui::TextEdit::load_state(ctx, id).unwrap_or_default();
- state.cursor.set_char_range(Some(egui::text::CCursorRange::two(egui::text::CCursor::new(start), egui::text::CCursor::new(end))));
- egui::TextEdit::store_state(ctx, id, state);
- }
--}
--
--#[derive(Clone, Copy, Debug, PartialEq)]
--pub struct Extras {
-- pub underline: bool,
-- pub line_spacing: f64,
-- pub char_spacing: f64,
-- pub scale: f64,
--}
--
--impl Default for Extras {
-- fn default() -> Self {
-- Extras { underline: false, line_spacing: 0.0, char_spacing: 0.0, scale: 100.0 }
-- }
--}
--
--impl LineEditor {
-- /// What Format text's font list shows: the chosen page font, a standard family picked
-- /// instead, or the paragraph's own font (its `/BaseFont` without the subset tag).
-- pub(crate) fn font_shown(&self, sources: &[(String, String)]) -> String {
-- if let Some(face) = self.font_resource.as_ref().and_then(|r| sources.iter().find(|(res, _)| res == r)).map(|(_, face)| face) {
-- return face.clone();
-- }
-- if self.look.family != self.look0.family || self.family_chosen {
-- return self.look.family.label().to_string();
-- }
-- let base = self.source.as_ref().map(|(block, _)| block.base_font.as_str()).unwrap_or_default();
-- // "ABCDEF+Calibri" is a subset of Calibri.
-- let name = match base.split_once('+') {
-- Some((tag, rest)) if tag.len() == 6 && tag.bytes().all(|b| b.is_ascii_uppercase()) => rest,
-- _ => base,
-- };
-- if name.is_empty() { tl!("Keep source fonts").to_string() } else { name.to_string() }
-- }
--
-- /// Back to the paragraph's own font: no page font chosen and the family it started with.
-- pub(crate) fn keep_source_font(&mut self) {
-- self.font_resource = None;
-- self.look.family = self.look0.family;
-- self.family_chosen = false;
-- }
--
-- /// A standard family picked from the font list, replacing the paragraph's font even when it
-- /// is the family the paragraph's font name reads as.
-- pub(crate) fn choose_family(&mut self, family: pdfcraft_engine::FontFamily) {
-- self.font_resource = None;
-- self.look.family = family;
-- self.family_chosen = true;
-- }
--
-- pub(crate) fn has_changes(&self) -> bool {
-- self.changes_with(&self.text)
-- }
--
-- /// Whether the paragraph with `text` in it differs from what was opened.
-- fn changes_with(&self, text: &str) -> bool {
-- text != self.original || self.look != self.look0 || self.extras != self.extras0 || self.family_chosen
-- }
--
-- /// Extra width permitted by the editor. Source containers now remain fixed.
-- pub fn growth(&self) -> Option<f32> {
-- (self.max_width > self.rect.width()).then_some(self.max_width)
-- }
--
-- /// Whether the edited text differs from the original (unsaved changes).
-- pub fn has_unsaved_text(&self) -> bool {
-- self.text != self.original
-- }
--
-- /// The formatting the panel changed.
-- pub fn style(&self) -> pdfcraft_engine::BlockStyle {
-- let (l, o) = (&self.look, &self.look0);
-- let mut style = pdfcraft_engine::BlockStyle {
-- resource: self.font_resource.clone(),
-- family: (l.family != o.family || self.family_chosen || l.bold != o.bold || l.italic != o.italic).then_some((l.family, l.bold, l.italic)),
-- size: (l.size != o.size).then_some(l.size),
-- color: (l.color != o.color).then_some(l.color),
- align: (l.align != o.align).then_some(l.align),
- // The whole paragraph's alignment when it was picked, or when the rows' own were
- // dropped for it (the rows are where those put them, so leaving it out keeps them there).
- align: (l.align != o.align || l.aligns.is_empty() && !o.aligns.is_empty()).then_some(l.align),
-- underline: (self.extras.underline != self.extras0.underline).then_some(self.extras.underline),
-- line_spacing: (self.extras.line_spacing != self.extras0.line_spacing && self.extras.line_spacing > 0.0)
-- .then_some(self.extras.line_spacing),
-- char_spacing: (self.extras.char_spacing != self.extras0.char_spacing).then_some(self.extras.char_spacing),
-- scale: (self.extras.scale != self.extras0.scale).then_some(self.extras.scale),
-- ..Default::default()
-- };
- // Rows aligned on their own, sent with every rewrite of the paragraph while it is open
- // (a colour or size change too): nothing in the page records them, so a rewrite without
- // them would undo them. Alone, they are sent only when they changed.
- if l.aligns != o.aligns || self.text != self.original || style != pdfcraft_engine::BlockStyle::default() {
- style.aligns = l.aligns.clone();
- }
-- if let Some(range) = &self.selection
-- && (style.family.is_some()
-- || style.resource.is_some()
-- || style.size.is_some()
-- || style.color.is_some()
-- || style.underline.is_some()
-- || style.char_spacing.is_some()
-- || style.scale.is_some())
-- {
-- style.ranges.push(pdfcraft_engine::RangeStyle {
-- range: range.clone(),
-- family: style.family.take(),
-- resource: style.resource.take(),
-- size: style.size.take(),
-- color: style.color.take(),
-- underline: style.underline.take(),
-- char_spacing: style.char_spacing.take(),
-- scale: style.scale.take(),
-- });
-- }
-- style
-- }
--
- /// After applying formatting: the paragraph now has it.
- /// After applying formatting: the paragraph now has it. The selection stays, as the box still
- /// shows it, so a second alignment or format picked for it applies to the same rows.
-- pub fn applied(&mut self) {
-- self.font_resource = None;
-- self.family_chosen = false;
-- self.look0 = self.look.clone();
-- self.extras0 = self.extras;
-- self.original = self.text.clone();
- self.selection = None;
- // The page now has this text: the box's history restarts, even when the format left the
- // text as it was, so a Redo from before can't put undone typing back on top of it.
- self.typing_from = None;
-- self.focus = true;
- }
-
- /// Take the shared text box for this paragraph ([`forget_typing_before`]): its own history,
- /// or none once the page has its text.
- pub(crate) fn claim_typing(&mut self, ctx: &egui::Context, doc: pdfcraft_engine::DocId) {
- forget_typing_before(ctx, input_id(), doc, &mut self.typing_from, &self.original);
-- }
--
-- /// Adopt the rewritten paragraph's current geometry before the next overlay frame.
-- pub(crate) fn refresh_source(&mut self, block: &pdfcraft_engine::TextBlock, lines: Vec<pdfcraft_engine::TextLine>) {
-- self.source_rect = block.rect.map(|v| v as f32);
-- self.multiline = block.rows.len() > 1;
-- self.source = Some((block.clone(), lines));
- self.text = block.editor_text.clone();
- let before = std::mem::replace(&mut self.text, block.editor_text.clone());
- if self.text != before {
- // A byte range of the old text: it may name other characters, or none, in the new.
- self.selection = None;
- }
- self.follow_rows(&before);
-- self.original = self.text.clone();
-- }
-
- pub fn selection_formatting(&self, sources: &[(String, String)]) -> SelectionFormatting {
- let Some((block, lines)) = &self.source else {
- return SelectionFormatting {
- font_shown: self.font_shown(sources),
- size: Some(self.look.size),
- family: Some(self.look.family),
- bold: Some(self.look.bold),
- italic: Some(self.look.italic),
- color: Some(self.look.color),
- };
- };
-
- let spans = block.edited_spans(&self.text);
- let matching_lines: Vec<&pdfcraft_engine::TextLine> = match &self.selection {
- Some(range) if range.start < range.end => {
- spans.iter().filter(|(span, _)| span.start < range.end && span.end > range.start).filter_map(|(_, idx)| lines.get(*idx)).collect()
- }
- _ => spans.iter().filter_map(|(_, idx)| lines.get(*idx)).collect(),
- };
-
- if matching_lines.is_empty() {
- return SelectionFormatting {
- font_shown: self.font_shown(sources),
- size: Some(self.look.size),
- family: Some(self.look.family),
- bold: Some(self.look.bold),
- italic: Some(self.look.italic),
- color: Some(self.look.color),
- };
- }
-
- let line_font = |l: &pdfcraft_engine::TextLine| -> String {
- if let Some((_, face)) = sources.iter().find(|(res, _)| res == &l.font) {
- return face.clone();
- }
- let base = l.base_font.as_str();
- let name = match base.split_once('+') {
- Some((tag, rest)) if tag.len() == 6 && tag.bytes().all(|b| b.is_ascii_uppercase()) => rest,
- _ => base,
- };
- if let Some((_, face)) = sources.iter().find(|(_, face)| face.as_str() == name) {
- return face.clone();
- }
- let look = look_of_line(l);
- let lower = name.to_ascii_lowercase();
- if ["times", "serif", "roman", "courier", "helvetica", "arial"].iter().any(|s| lower.contains(s)) {
- return look.family.label().to_string();
- }
- if name.is_empty() { tl!("Keep source fonts").to_string() } else { name.to_string() }
- };
-
- let first_font = line_font(matching_lines[0]);
- let same_font = matching_lines.iter().skip(1).all(|l| line_font(l) == first_font);
- let font_shown = if same_font {
- if self.font_resource.is_some() || self.look.family != self.look0.family || self.family_chosen {
- self.font_shown(sources)
- } else {
- first_font
- }
- } else {
- tl!("Mixed").to_string()
- };
-
- let first_size = (matching_lines[0].size * 10.0).round() / 10.0;
- let same_size = matching_lines.iter().skip(1).all(|l| ((l.size * 10.0).round() / 10.0 - first_size).abs() < 0.05);
- let size = if same_size { Some(first_size) } else { None };
-
- let first_look = look_of_line(matching_lines[0]);
- let same_family = matching_lines.iter().skip(1).all(|l| look_of_line(l).family == first_look.family);
- let family = if same_family { Some(first_look.family) } else { None };
- let same_bold = matching_lines.iter().skip(1).all(|l| l.bold == matching_lines[0].bold);
- let bold = if same_bold { Some(matching_lines[0].bold) } else { None };
- let same_italic = matching_lines.iter().skip(1).all(|l| l.italic == matching_lines[0].italic);
- let italic = if same_italic { Some(matching_lines[0].italic) } else { None };
- let same_color = matching_lines.iter().skip(1).all(|l| l.color == matching_lines[0].color);
- let color = if same_color { Some(matching_lines[0].color) } else { None };
--
- SelectionFormatting { font_shown, size, family, bold, italic, color }
- /// The text changed from `before` through the box's own typing (typing, Undo, Redo): rows
- /// aligned on their own keep their alignment, and a row Undo brings back gets its own back.
- pub(crate) fn follow_typed_rows(&mut self, ctx: &egui::Context, before: &str) {
- follow_typing(ctx, input_id(), before, &self.text, &mut [&mut self.look.aligns, &mut self.look0.aligns]);
-- }
--
- pub(crate) fn sync_selection_look(&mut self, fmt: &SelectionFormatting) {
- if let Some(sz) = fmt.size {
- self.look.size = sz;
- self.look0.size = sz;
- } else {
- if self.look.size <= 0.0 {
- self.look.size = 12.0;
- }
- self.look0.size = self.look.size;
- }
- if let Some(f) = fmt.family {
- self.look.family = f;
- self.look0.family = f;
- }
- match fmt.bold {
- Some(b) => {
- self.look.bold = b;
- self.look0.bold = b;
- }
- None => {
- self.look.bold = false;
- self.look0.bold = false;
- }
- }
- match fmt.italic {
- Some(it) => {
- self.look.italic = it;
- self.look0.italic = it;
- }
- None => {
- self.look.italic = false;
- self.look0.italic = false;
- }
- }
- if let Some(c) = fmt.color {
- self.look.color = c;
- self.look0.color = c;
- /// The text changed from `before`: rows aligned on their own keep their alignment.
- pub(crate) fn follow_rows(&mut self, before: &str) {
- for look in [&mut self.look, &mut self.look0] {
- look.aligns = pdfcraft_engine::paragraph_align::remap(before, &self.text, &look.aligns);
-- }
-- }
- }
-
- #[derive(Clone, Debug, PartialEq)]
- pub struct SelectionFormatting {
- pub font_shown: String,
- pub size: Option<f64>,
- pub family: Option<pdfcraft_engine::FontFamily>,
- pub bold: Option<bool>,
- pub italic: Option<bool>,
- pub color: Option<[f64; 3]>,
- }
-
- fn look_of_line(l: &pdfcraft_engine::TextLine) -> pdfcraft_engine::AddedText {
- source_look(&l.base_font, l.size, l.color, l.bold, l.italic)
--}
--
--/// What a drag on a paragraph box takes hold of.
--#[derive(Clone, Copy, Debug, PartialEq)]
--enum Grip {
-- Move,
-- /// An edge (screen left or right), dragged to rewrap the paragraph.
-- Left,
-- Right,
--}
--
--/// A paragraph box being dragged: moved, or (from a handle on its left or right edge) resized.
--#[derive(Clone, Copy, Debug, PartialEq)]
--pub struct BlockDrag {
-- page: usize,
-- block: usize,
-- /// A paragraph index belongs to the document generation where the press was resolved.
-- generation: u64,
-- /// Where the drag started (screen).
-- start: Pos2,
-- grip: Grip,
--}
--
--/// How far an edge grip reaches either side of a box's edge (screen pixels).
--const EDGE_REACH: f32 = 6.0;
- /// How far outside an open paragraph's frame the pointer still means the frame (screen pixels).
- const FRAME_REACH: f32 = 4.0;
-/// How far outside an open paragraph's frame the pointer still means the frame (screen pixels):
-/// the frame's Move cursor shows, and a press there takes hold of the box (`object_ui`).
-pub(crate) const FRAME_REACH: f32 = 6.0;
--
--/// The rewrap handles drawn on a paragraph box's left and right edges (screen).
--fn width_handles(b: Rect) -> [Rect; 2] {
-- let h = (b.height() * 0.5).clamp(14.0, 28.0);
-- [b.left(), b.right()].map(|x| Rect::from_center_size(Pos2::new(x, b.center().y), egui::vec2(7.0, h)))
--}
--
--/// The edge of `b` that `p` grabs: anywhere along its height, within reach of the line. A narrow
--/// box keeps its middle for moving.
--fn edge_at(b: Rect, p: Pos2) -> Option<Grip> {
-- if p.y < b.top() - EDGE_REACH || p.y > b.bottom() + EDGE_REACH {
-- return None;
-- }
-- let inside = EDGE_REACH.min(b.width() / 4.0);
-- let (dl, dr) = (p.x - b.left(), b.right() - p.x);
-- match (dl >= -EDGE_REACH && dl <= inside, dr >= -EDGE_REACH && dr <= inside) {
-- (true, true) if dl <= dr => Some(Grip::Left),
-- (_, true) => Some(Grip::Right),
-- (true, false) => Some(Grip::Left),
-- _ => None,
-- }
--}
--
--/// The topmost box under `p` and what it grabs there; edges only on an upright page.
--fn grab_at(boxes: &[Rect], p: Pos2, upright: bool) -> Option<(usize, Grip)> {
-- boxes.iter().enumerate().rev().find_map(|(i, b)| match upright.then(|| edge_at(*b, p)).flatten() {
-- Some(g) => Some((i, g)),
-- None => b.contains(p).then_some((i, Grip::Move)),
-- })
--}
--
--/// A paragraph's box on screen: its text rect (user space), with a little room around it.
--fn paragraph_box(xf: &PageXform, info: &DocInfo, page: usize, rect: [f64; 4]) -> Rect {
-- xf.user_rect(info, page, rect.map(|v| v as f32)).expand(2.0)
-}
-
-/// Whether `p` lies in the box of one of the paragraphs `lines` on `page`, its edge grips there
-/// included, not beside it: where Add text acts on the page's own paragraphs.
-pub(crate) fn over_paragraph(xf: &PageXform, info: &DocInfo, page: usize, lines: &[pdfcraft_engine::TextBlock], p: Pos2) -> bool {
- lines.iter().any(|l| paragraph_box(xf, info, page, l.rect).contains(p))
--}
--
--/// The width handles need the page upright (or upside down): on a quarter-turned page the
--/// screen's horizontal is the paragraph's vertical.
--fn has_edges(info: &DocInfo, page: usize) -> bool {
-- info.pages.get(page).is_some_and(|p| p.rotation % 180 == 0)
--}
--
- /// Whether a press at `p` takes hold of the width handle of one of the paragraphs `rects` (user
- /// space). The handle is drawn half outside the box and grabs from a little further out still,
- /// where a press is not on the box itself, so area selection must leave it to [`page_input`].
- pub(crate) fn grabs_edge(xf: &PageXform, info: &DocInfo, page: usize, mut rects: impl Iterator<Item = [f64; 4]>, p: Pos2) -> bool {
- has_edges(info, page) && rects.any(|r| edge_at(paragraph_box(xf, info, page, r), p).is_some())
-/// What of the paragraphs `lines` lies under `p` on `page`: the one hit test of Edit text's
-/// paragraphs, for the cursor and the press. A closed paragraph moves when dragged (the four-way
-/// arrow says so; a click still opens it for typing) and its edge rewraps it, from a little way
-/// outside the box (the handle is drawn half outside it, #2276); the open one takes the text
-/// cursor. `active` is the paragraph open for typing on this page.
-pub(crate) fn hit(xf: &PageXform, info: &DocInfo, page: usize, lines: &[pdfcraft_engine::TextBlock], active: Option<usize>, p: Pos2) -> Option<Hit> {
- if !xf.rect.contains(p) {
- return None;
- }
- let boxes: Vec<Rect> = lines.iter().map(|l| paragraph_box(xf, info, page, l.rect)).collect();
- // Edges rewrap closed paragraphs only: around the open one lies its frame (`object_ui`).
- let (block, grip) = grab_at(&boxes, p, has_edges(info, page) && active.is_none())?;
- let paragraph = |part| Owner::Paragraph { block, part };
- Some(match (active, grip) {
- (None, Grip::Left) => Hit::new(paragraph(ParagraphPart::Left), Layer::Handle, egui::CursorIcon::ResizeHorizontal),
- (None, Grip::Right) => Hit::new(paragraph(ParagraphPart::Right), Layer::Handle, egui::CursorIcon::ResizeHorizontal),
- (None, Grip::Move) => Hit::new(paragraph(ParagraphPart::Body), Layer::Text, egui::CursorIcon::Move),
- (Some(open), _) if open == block => Hit::new(paragraph(ParagraphPart::Body), Layer::Editor, egui::CursorIcon::Text),
- (Some(_), _) => Hit::new(paragraph(ParagraphPart::Body), Layer::Text, egui::CursorIcon::Text),
- })
--}
--
- pub(crate) fn user_box(xf: &PageXform, info: &DocInfo, page: usize, r: Rect) -> [f64; 4] {
-fn user_box(xf: &PageXform, info: &DocInfo, page: usize, r: Rect) -> [f64; 4] {
-- let p = &info.pages[page];
-- let (a, b) = (xf.screen_to_view(r.min), xf.screen_to_view(r.max));
-- let (u, v) = (p.view_to_user(a.0, a.1), p.view_to_user(b.0, b.1));
-- [u[0].min(v[0]) as f64, u[1].min(v[1]) as f64, u[0].max(v[0]) as f64, u[1].max(v[1]) as f64]
--}
--
--/// Open `block` on `page` in the inline editor. The box's geometry and source are read from the
--/// `lines` handed in, so callers pass the document's current inventory.
- #[allow(clippy::too_many_arguments)]
- fn open_editor(
- ui: &egui::Ui,
- xf: &PageXform,
- doc: &pdfcraft_engine::Document,
- lines: &[pdfcraft_engine::TextBlock],
- boxes: &[Rect],
- page: usize,
- block: usize,
- view: &mut DocView,
- ) {
-fn open_editor(cx: PageToolContext<'_>, lines: &[pdfcraft_engine::TextBlock], boxes: &[Rect], block: usize, view: &mut DocView) {
- let PageToolContext { ui, xf, doc, page, .. } = cx;
-- view.pending_text_open = None;
-- let (Some(l), Some(b)) = (lines.get(block), boxes.get(block)) else { return };
-- view.image_selection = None;
-- let source_lines = doc.text_lines(page);
-- let text = l.editor_text.clone();
-- // Screen pixels per point, from the box's width.
-- let scale = (b.width() - 4.0) / ((l.rect[2] - l.rect[0]).max(1.0) as f32);
-- // How far the editor box may grow: a single-line paragraph's rewrite grows to the
-- // page's right edge, a multi-line paragraph rewraps to its own width.
-- let right = xf.rect.right().min(view.viewport_rect().right()) - 6.0;
-- let max_width = if l.rows.len() == 1 { (right - b.left()).max(b.width()) } else { b.width() };
-- view.line_editor = Some(LineEditor {
-- selection: None,
-- font_resource: None,
-- page,
-- block,
-- text: text.clone(),
-- original: text,
-- rect: *b,
-- source_rect: l.rect.map(|v| v as f32),
-- multiline: l.rows.len() > 1,
-- source: Some((l.clone(), source_lines)),
-- fonts: install_source_fonts(ui.ctx(), doc, page),
-- overflow: false,
-- max_width,
-- size: (l.size as f32 * scale).clamp(8.0, 72.0),
-- focus: true,
-- had_focus: false,
-- look: look_of(l),
-- look0: look_of(l),
-- family_chosen: false,
-- extras: Extras::default(),
-- extras0: Extras::default(),
- typing_from: None,
-- });
--}
--
--/// Lines and their screen boxes for a page; hover outlines, click opens the editor. Returns
--/// `true` when the pointer was used.
- #[allow(clippy::too_many_arguments)]
- pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- doc: &pdfcraft_engine::Document,
- lines: &[pdfcraft_engine::TextBlock],
- view: &mut DocView,
- allow_interaction: bool,
- ) -> bool {
-pub(crate) fn page_input(cx: PageToolContext<'_>, lines: &[pdfcraft_engine::TextBlock], view: &mut DocView, allow_interaction: bool) -> bool {
- let PageToolContext { ui, resp, xf, ptr, page, doc, .. } = cx;
-- let info = &doc.info;
-- if view.block_drag.is_some_and(|d| d.generation != doc.edit_generation()) {
-- view.block_drag = None;
-- return true;
-- }
-- let boxes: Vec<Rect> = lines.iter().map(|l| paragraph_box(xf, info, page, l.rect)).collect();
-- // Outlines stay on their page, like its text (#3061).
-- let painter = ui.painter().with_clip_rect(ui.clip_rect().intersect(xf.rect.expand(1.0)));
-- let active = view.line_editor.as_ref().filter(|e| e.page == page).map(|e| e.block);
-- for (i, b) in boxes.iter().enumerate() {
-- if active != Some(i) {
-- painter.rect_stroke(*b, CornerRadius::same(2), Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
-- }
-- }
-- // A box a switching click asked for opens now, once the edit queued with that click has
-- // applied and `lines`/`boxes` are the document's new inventory (#2022).
-- if allow_interaction
-- && active.is_none()
-- && let Some((open_page, key)) = view.pending_text_open.clone()
-- && open_page == page
-- {
-- // The edit may have regrouped the page: open the paragraph that was clicked, wherever it
-- // is now, and none if it was merged into another (#2516).
-- match key.find(lines, 0) {
- Some(block) => open_editor(ui, xf, doc, lines, &boxes, page, block, view),
- Some(block) => open_editor(cx, lines, &boxes, block, view),
-- None => view.pending_text_open = None,
-- }
-- }
-- if !allow_interaction {
-- return false;
-- }
-- let upright = has_edges(info, page);
-- let draw_handles = |b: Rect| {
-- for h in width_handles(b) {
-- painter.rect(h, CornerRadius::same(2), Color32::WHITE, Stroke::new(1.0, ACCENT), egui::StrokeKind::Middle);
-- }
-- };
-- // A drag in progress: the box follows the pointer (or the grabbed edge does); releasing applies it.
-- if let Some(d) = view.block_drag.filter(|d| d.page == page)
-- && let (Some(b), Some(l)) = (boxes.get(d.block).copied(), lines.get(d.block))
-- {
-- let p = ptr.pos.unwrap_or(d.start);
-- let dx = p.x - d.start.x;
-- let preview = match d.grip {
-- Grip::Move => b.translate(p - d.start),
-- Grip::Left => Rect::from_min_max(Pos2::new((b.left() + dx).min(b.right() - 12.0), b.min.y), b.max),
-- Grip::Right => Rect::from_min_max(b.min, Pos2::new((b.right() + dx).max(b.left() + 12.0), b.max.y)),
-- };
-- painter.rect_stroke(preview, CornerRadius::same(2), Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
-- let resizing = d.grip != Grip::Move;
-- if resizing {
-- draw_handles(preview);
-- }
- ui.ctx().set_cursor_icon(if resizing { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::Move });
- view.cursor.claim(Layer::Drag, if resizing { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::Move });
-- if resp.drag_stopped() || !ptr.any_down {
-- view.block_drag = None;
-- let (from, to) = (user_box(xf, info, page, b), user_box(xf, info, page, preview));
-- let edit = if resizing {
-- // The new width in user space: the box's change, added to the paragraph's own. A
-- // moved left side (in user space; on an upside-down page that's the screen's right
-- // edge) moves the paragraph with it.
-- let width = (l.rect[2] - l.rect[0]) + (to[2] - to[0]) - (from[2] - from[0]);
-- let shift = to[0] - from[0];
-- let style = pdfcraft_engine::BlockStyle { width: Some(width), offset: (shift != 0.0).then_some([shift, 0.0]), ..Default::default() };
- Edit::EditTextBlock { page, block: d.block, text: l.text.clone(), style }
- // `editor_text` has a newline for every visual row, so a wrap would come back as a
- // hard break and a wider box would not reflow it. `reflow_text` rejoins a wrap
- // and keeps a newline that was typed.
- Edit::EditTextBlock { page, block: d.block, text: l.reflow_text.clone(), style }
-- } else {
-- // Translation must keep the selected source operators. Rewriting unchanged text
-- // would also remove unrelated paragraphs that now overlap this one (#594).
-- Edit::MoveObjects {
-- page,
-- objects: vec![ObjectTarget { kind: ObjectKind::Text, index: d.block }],
-- offset: [to[0] - from[0], to[1] - from[1]],
-- }
-- };
-- if preview != b {
-- view.pending_edit = Some(edit);
-- }
-- }
-- return true;
-- }
-- // Dragging a box (not while a paragraph is open for typing) moves it; from an edge, resizes it.
- // What it grabs comes from where the button went down, not where the pointer is now: egui
- // What it grabs is what the press was on ([`hit`]), not what is under the pointer now: egui
-- // only calls it a drag once the pointer has moved a few pixels (or been held a moment), and a
-- // quick flick has left the edge by then.
- let owner = match view.router.owner(page) {
- Some(Owner::Paragraph { block, part }) => Some((block, part)),
- _ => None,
- };
-- if resp.drag_started()
-- && active.is_none()
-- && let Some(o) = ptr.origin
- && let Some((block, grip)) = grab_at(&boxes, o, upright)
- && let Some((block, part)) = owner
-- {
- let grip = match part {
- ParagraphPart::Body => Grip::Move,
- ParagraphPart::Left => Grip::Left,
- ParagraphPart::Right => Grip::Right,
- };
-- view.block_drag = Some(BlockDrag { page, block, generation: doc.edit_generation(), start: o, grip });
-- return true;
-- }
-- let Some(p) = ptr.within(xf.rect) else { return false };
- let Some((hit, grip)) = grab_at(&boxes, p, upright) else { return false };
- if active != Some(hit) {
- painter.rect_stroke(boxes[hit], CornerRadius::same(2), Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- // The paragraph under the pointer: outlined, with its width handles when closed.
- let Some((hovered, _)) = grab_at(&boxes, p, upright) else { return false };
- if active != Some(hovered) {
- painter.rect_stroke(boxes[hovered], CornerRadius::same(2), Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
-- }
- let on_edge = active.is_none() && grip != Grip::Move;
-- if upright && active.is_none() {
- draw_handles(boxes[hit]);
- }
- // A closed paragraph moves when dragged (the four-way arrow says so; a click still opens it
- // for typing); an edge rewraps it. The open one takes the text cursor. Only where the page
- // shows, so the mode bar and side panels over a paragraph scrolled under them keep their own
- // cursor (#2755).
- if resp.contains_pointer() {
- ui.ctx().set_cursor_icon(if on_edge {
- egui::CursorIcon::ResizeHorizontal
- } else if active.is_none() {
- egui::CursorIcon::Move
- } else {
- egui::CursorIcon::Text
- });
- draw_handles(boxes[hovered]);
-- }
- // A click opens the paragraph the press was on.
- let Some((hit, _)) = owner else { return true };
-- if resp.clicked() {
-- view.image_selection = None;
-- // A click that moves the editor to another paragraph applies the open one first, so its
-- // typed text is not dropped with the switched-away editor (#2022). A click on the open
-- // box's own frame, which the editor's overlay does not cover, keeps it open instead.
-- if let Some(open) = view.line_editor.as_ref().filter(|e| e.has_changes()) {
-- if open.page == page && open.block == hit {
-- return true;
-- }
-- if let Some(old) = view.line_editor.take() {
-- let style = old.style();
-- if old.text != old.original || style != pdfcraft_engine::BlockStyle::default() {
-- view.applied_editor = Some(old.clone());
-- view.pending_edit = Some(Edit::EditTextBlock { page: old.page, block: old.block, text: old.text, style });
-- }
-- }
-- // The clicked box opens next frame, from the document with that edit applied.
-- view.pending_text_open = lines.get(hit).map(|l| (page, pdfcraft_engine::BlockKey::of(l)));
-- return true;
-- }
- open_editor(ui, xf, doc, lines, &boxes, page, hit, view);
- open_editor(cx, lines, &boxes, hit, view);
-- }
-- true
--}
--
--/// The inline editor; returns the edit once the text is applied.
--pub(crate) fn overlay(ctx: &egui::Context, view: &mut DocView, info: &DocInfo) -> Option<Edit> {
- // Clicks outside the document (the Format text panel) keep the paragraph open. Where the
- // click landed is `interact_pos`: `latest_pos` is gone on the frame a tap lifts.
- let outside = ctx.input(|i| i.pointer.interact_pos()).is_some_and(|p| !view.viewport_rect().contains(p));
- let view_id = view.id;
- // Clicks outside the document (the Format text panel) keep the paragraph open, as does the
- // keyboard moving there. Where the click landed is `interact_pos`: `latest_pos` is gone on the
- // frame a tap lifts.
- let away = |p: egui::Pos2| !view.viewport_rect().contains(p);
- let outside = ctx.input(|i| i.pointer.interact_pos()).is_some_and(away) || focus_pos(ctx).is_some_and(away);
-- let page = view.line_editor.as_ref()?.page;
-- let xf = view.page_xform(page)?;
-- let hold = view.objects.hold_editor;
-- // An OCR word is invisible text over a scan: the textless background still shows the scanned
-- // word, so the editor covers it, as the applied edit will.
-- let background_ready = view
-- .line_editor
-- .as_ref()
-- .is_some_and(|ed| view.text_background.ready(ed.page, ed.block) && !ed.source.as_ref().is_some_and(|(b, _)| b.invisible));
-- // The measured extent of this paragraph's draft: a click straight from another paragraph
-- // opens this one after the frame's measuring, which was the other one's.
-- let draft_rect = view
-- .line_editor
-- .as_ref()
-- .and_then(|ed| view.text_background.draft_rect_for(ed.page, ed.block))
-- .map(|r| xf.user_rect(info, page, r.map(|v| v as f32)));
-- // The editor draws on its page only, as the page itself does: a paragraph running past the
-- // page's edge (a converter relying on the page clip) must not cover the next page or the
-- // tool bars (#3061).
-- let viewport = view.viewport_rect();
-- let page_clip = xf.rect.intersect(viewport);
-- let ed = view.line_editor.as_mut()?;
-- if hold {
-- ed.focus = true;
-- }
-- // Reproject the source box every frame. The page may have been zoomed, scrolled or rotated
-- // while the format panel was open.
-- // A font definition becomes active at the start of the next egui pass. Keep the source
-- // raster visible until then instead of flashing an unrelated interface face.
-- if !ctx.fonts(|f| ed.fonts.values().all(|name| f.definitions().families.contains_key(&FontFamily::Name(name.clone().into())))) {
-- ctx.request_repaint();
-- return None;
-- }
-- ed.rect = xf.user_rect(info, ed.page, ed.source_rect);
-- ed.max_width = ed.rect.width();
-- let scale = (ed.rect.width() / (ed.source_rect[2] - ed.source_rect[0]).abs().max(0.01)).max(0.01);
-- ed.size = (ed.look.size as f32 * scale).clamp(1.0, 300.0);
-- // egui draws the caret at least one row of this font tall. Each row already has its source
-- // row's height, so the font stays below every row (half the region's smallest size): on a
-- // 12 pt row under a 24 pt title the caret is as tall as the row, not the title.
-- let smallest =
-- ed.source.as_ref().and_then(|(block, lines)| block.lines.iter().filter_map(|i| lines.get(*i)).map(|l| l.size as f32).reduce(f32::min));
-- let font = editor_font(&ed.look, smallest.map_or(ed.size, |s| (s * scale * 0.5).clamp(1.0, 300.0).min(ed.size)));
-- let text_color = color32(ed.look.color);
-- let block = ed.block;
-- let mut done = None;
-- let mut reveal = false;
-- let mut look = None;
-- // No fade-in: while it faded, the page's own copy of the text showed through the cover.
-- // Over the page, so only where the document shows: a paragraph scrolled under the mode bar or
-- // a side panel must leave them their pointer (#2755). It stays at its paragraph, cut off as
-- // the page is, rather than pushed back into the window.
-- egui::Area::new(egui::Id::unique("edit-text-line"))
-- .order(egui::Order::Foreground)
-- .fade_in(false)
-- .fixed_pos(Pos2::new(ed.rect.left(), ed.rect.top()))
-- .constrain_to(viewport)
-- .constrain(false)
-- .show(ctx, |ui| {
-- ui.set_clip_rect(ui.clip_rect().intersect(page_clip));
-- // The editor opens at the source size. Once text changes, show the real draft
-- // extent: single rows grow toward the page edge, paragraphs rewrap at their
-- // source width, and the visible frame grows/shrinks vertically with their rows.
-- // An OCR phrase (several stretched words on one scanned line) also grows rightwards.
-- let single_run = ed.source.as_ref().is_some_and(|(block, _)| block.rows.len() == 1 || block.invisible);
-- let width = if !ed.multiline && single_run && !ed.text.contains('\n') && ed.has_changes() {
-- let job = editor_job(ui, ed, &ed.text, scale, f32::INFINITY);
-- let natural = ui.fonts_mut(|f| f.layout_job(job)).size().x;
-- natural.max(ed.size * 0.4).min((xf.rect.right() - ed.rect.left() - 36.0 * scale).max(ed.rect.width()))
-- } else {
-- ed.rect.width().max(1.0)
-- };
-- ui.set_width(width);
-- let measured = editor_galley(ui, ed, &ed.text, scale, width);
-- ed.overflow = ed.has_changes() && (measured.rect.width() > ed.rect.width() + 1.5 || measured.rect.height() > ed.rect.height() + 1.5);
-- let visible = Rect::from_min_size(ed.rect.min, measured.rect.size().max(egui::vec2(1.0, 1.0)));
-- // Unsupported source programs use an interface fallback whose glyph advances
-- // can differ from the PDF's. Keep every visible row inside the growing frame.
-- let live = draft_rect.map_or(visible, |bounds| bounds.union(visible));
-- ui.painter().rect_stroke(live.expand(2.0), CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
-- // On the frame, a press takes hold of the box (a click selects it, a drag moves it):
-- // the pointer says so there, and reads as text inside.
-- if let Some(p) = ui.input(|i| i.pointer.hover_pos())
-- && ui.clip_rect().contains(p)
-- && live.expand(2.0 + FRAME_REACH).contains(p)
-- && !live.expand(2.0).contains(p)
-- {
-- ui.ctx().set_cursor_icon(egui::CursorIcon::Move);
-- }
-- // White, unless the text is too pale to read on white (white text from a coloured
-- // banner, #913): then a dark box, with a caret that shows on it.
-- let fill = if background_ready { Color32::TRANSPARENT } else { editor_fill(text_color) };
-- // An unchanged paragraph is the page's own text: the editor draws only its frame and
-- // caret over it. Once it changes, the textless page goes under the editor's text or,
-- // until that background is ready, a cover over the entire source box, including lines
-- // the draft no longer occupies. Shrinking the text must never expose a copy. Both are
-- // decided after this frame's typing, in places kept under the editor's text.
-- let cover = ui.painter().add(egui::Shape::Noop);
-- let unchanged = !ed.has_changes();
-- egui::Frame::NONE.show(ui, |ui| {
-- // A textless PDF preview, or the page itself, keeps its real background through
-- // the transparent editor. The temporary opaque cover uses the shared caret-contrast rule.
-- ui.visuals_mut().text_cursor.stroke.color = if background_ready || unchanged {
-- if luminance(text_color) < 0.5 { Color32::BLACK } else { Color32::WHITE }
-- } else {
-- caret_color_on(fill)
-- };
-- let rows = 1;
-- let snapshot = ed.clone();
-- let mut layouter = |ui: &egui::Ui, text: &dyn egui::TextBuffer, wrap_width: f32| {
-- let galley = editor_galley(ui, &snapshot, text.as_str(), scale, wrap_width);
-- if snapshot.changes_with(text.as_str()) { galley } else { unseen(galley) }
-- };
- let before = ed.text.clone();
- let input = input_id();
- ed.claim_typing(ui.ctx(), view_id);
-- let output = egui::TextEdit::multiline(&mut ed.text)
- .id(egui::Id::unique("edit-text-line-input"))
- .id(input)
-- .font(font.clone())
-- .margin(egui::Margin::ZERO)
-- .layouter(&mut layouter)
-- .text_color(text_color)
-- .frame(egui::Frame::NONE)
-- .desired_width(width)
-- .desired_rows(rows)
-- .show(ui);
-- // The editor paints over the page with no visible label; name it for assistive tech.
-- output.response.response.clone().accessible_name(tl!("Text editing line"));
-- if ed.has_changes() {
-- if background_ready {
-- reveal = true;
-- } else {
-- ui.painter().set(cover, egui::Shape::rect_filled(ed.rect.expand(1.0).union(output.response.rect), CornerRadius::ZERO, fill));
-- }
-- }
-- look = Some((Arc::clone(&output.galley), output.galley_pos));
-- if let Some(cursor) = output.cursor_range {
-- let selected = cursor.as_sorted_char_range();
-- let byte = |index| ed.text.char_indices().nth(index).map_or(ed.text.len(), |(at, _)| at);
-- ed.selection = (!selected.is_empty()).then(|| byte(selected.start.into())..byte(selected.end.into()));
-- }
-- let r = output.response;
-- if r.changed() {
- ed.follow_typed_rows(ctx, &before);
-- // The next pass measures the updated draft with the Apply/Save rewrite.
-- ctx.request_repaint();
-- }
-- if ed.focus {
-- r.request_focus();
- if let Some(range) = &ed.selection {
- reselect(ui.ctx(), input, &ed.text, range);
- }
-- ed.focus = false;
-- }
-- let lost = lost_focus_once(&r, &mut ed.had_focus);
-- let esc = ui.input(|i| i.key_pressed(egui::Key::Escape));
-- let apply = ui.input(|i| i.key_pressed(egui::Key::Enter) && i.modifiers.command);
-- if esc {
-- done = Some(false);
-- } else if apply || (lost && !outside && !hold) {
-- done = Some(true);
-- }
-- });
-- });
-- if reveal {
-- view.text_background.reveal();
-- }
-- if let Some((galley, pos)) = look {
-- view.text_background.remember(&xf, page, block, galley, pos, text_color);
-- }
-- match done {
-- Some(apply) => {
-- let ed = view.line_editor.take()?;
-- if !apply {
- // Esc leaves the text but not the paragraph: its box is selected, as in Acrobat,
- // so Delete removes it without aiming for the frame. The draft is discarded.
- view.objects.select_paragraph_next(ed.page, ed.block);
-- return None;
-- }
-- view.applied_editor = Some(ed.clone());
-- finished_edit(ed)
-- }
-- None => None,
-- }
--}
--
--/// The edit a closed paragraph editor applies, or `None` when nothing in it changed.
--pub(crate) fn finished_edit(ed: LineEditor) -> Option<Edit> {
-- let style = ed.style();
-- (ed.text != ed.original || style != pdfcraft_engine::BlockStyle::default()).then_some(Edit::EditTextBlock {
-- page: ed.page,
-- block: ed.block,
-- text: ed.text,
-- style,
-- })
-}
-
-/// Where the keyboard is: the centre of the widget that has it. A text box that loses it to a
-/// widget reached by Tab or a screen reader (the Format text panel's controls) judges where it
-/// went by this, as it judges a click by where the click landed.
-pub(crate) fn focus_pos(ctx: &egui::Context) -> Option<egui::Pos2> {
- ctx.memory(|m| m.focused()).and_then(|id| ctx.read_response(id)).map(|r| r.rect.center())
--}
--
--/// Whether an editor's text box `r` lost the keyboard this frame, given whether it `had` it at
--/// the end of the editor's last frame (updated here). egui's `lost_focus` holds for a second
--/// frame as well, and an editor that judges a click away by where the click landed must judge it
--/// once, on the click's frame: on the next, a tap's position is gone (egui-winit sends
--/// `PointerGone` with every touch release) and a mouse may have moved on.
--pub(crate) fn lost_focus_once(r: &egui::Response, had: &mut bool) -> bool {
-- let has = r.has_focus();
-- std::mem::replace(had, has) && !has
--}
--
--#[cfg(test)]
--mod tests {
-- use super::*;
-
- /// Formatting applied restarts the rows' alignments by text with the box's history: a Redo
- /// past the format can't bring back an alignment from before it (FredApps on bfc6f8b1).
- #[test]
- fn formatting_applied_restarts_the_alignments_by_text() {
- use pdfcraft_engine::{DocId, TextAlign::Right};
- let (ctx, id) = (egui::Context::default(), egui::Id::unique("box"));
- let mut from = None;
- forget_typing_before(&ctx, id, DocId(1), &mut from, "A\nB");
- let mut aligns = vec![None, Some(Right)];
- follow_typing(&ctx, id, "A\nB", "A", &mut [&mut aligns]);
- // Formatting applied: the page now has "A", and the box's record of its text is reset.
- from = None;
- forget_typing_before(&ctx, id, DocId(1), &mut from, "A");
- follow_typing(&ctx, id, "A", "A\nB", &mut [&mut aligns]);
- assert_eq!(aligns, pdfcraft_engine::paragraph_align::remap("A", "A\nB", &[None]), "the row before the format doesn't come back aligned");
- }
-
- /// The rows' alignments by text go aside with the box's history when another tab's box takes
- /// the text box, and come back with it: Undo after the trip still restores a deleted row's
- /// alignment, while the other tab's box starts with none.
- #[test]
- fn alignments_by_text_survive_a_trip_to_another_tab() {
- use pdfcraft_engine::{DocId, TextAlign::Right};
- let (ctx, id) = (egui::Context::default(), egui::Id::unique("box"));
- let (mut a_from, mut b_from) = (None, None);
- forget_typing_before(&ctx, id, DocId(1), &mut a_from, "A\nB");
- let mut aligns = vec![None, Some(Right)];
- follow_typing(&ctx, id, "A\nB", "A", &mut [&mut aligns]);
- forget_typing_before(&ctx, id, DocId(2), &mut b_from, "A\nB");
- let mut other = vec![None, None];
- follow_typing(&ctx, id, "A\nB", "A", &mut [&mut other]);
- follow_typing(&ctx, id, "A", "A\nB", &mut [&mut other]);
- assert_eq!(other, [None, None], "tab B's box gets back its own alignments, not tab A's");
- forget_typing_before(&ctx, id, DocId(1), &mut a_from, "A\nB");
- follow_typing(&ctx, id, "A", "A\nB", &mut [&mut aligns]);
- assert_eq!(aligns, [None, Some(Right)], "back in tab A, Undo brings the row back right-aligned");
- }
-
- /// Undo back to a text the box showed brings its rows' alignments back, unless an alignment
- /// was picked since: then the older alignments are stale, and the edit is followed instead, so
- /// Undo never takes back the pick.
- #[test]
- fn typing_history_restores_alignments_unless_one_was_picked_since() {
- use pdfcraft_engine::TextAlign::{Center, Right};
- let (ctx, id) = (egui::Context::default(), egui::Id::unique("box"));
- let mut aligns = vec![None, Some(Center), Some(Right)];
- follow_typing(
- &ctx,
- id,
- "A
-B
-C",
- "A
-C",
- &mut [&mut aligns],
- );
- assert_eq!(aligns, [None, Some(Right)], "the deleted row's entry goes");
- follow_typing(
- &ctx,
- id,
- "A
-C",
- "A
-B
-C",
- &mut [&mut aligns],
- );
- assert_eq!(aligns, [None, Some(Center), Some(Right)], "Undo brings it back");
- follow_typing(
- &ctx,
- id,
- "A
-B
-C",
- "A
-C",
- &mut [&mut aligns],
- );
- // An alignment picked for the first row, with no typing.
- aligns = vec![Some(Right), Some(Right)];
- let picked = aligns.clone();
- follow_typing(
- &ctx,
- id,
- "A
-C",
- "A
-B
-C",
- &mut [&mut aligns],
- );
- assert_eq!(
- aligns,
- pdfcraft_engine::paragraph_align::remap(
- "A
-C", "A
-B
-C", &picked
- ),
- "the pick stays"
- );
- }
--
-- #[test]
-- fn the_rotate_knob_moves_below_when_there_is_no_room_above() {
-- let view = Rect::from_min_max(Pos2::new(0.0, 100.0), Pos2::new(500.0, 500.0));
-- let roomy = Rect::from_min_max(Pos2::new(100.0, 200.0), Pos2::new(200.0, 250.0));
-- assert_eq!(rotate_knob(roomy, view).0, Pos2::new(150.0, 178.0));
-- let at_top = Rect::from_min_max(Pos2::new(100.0, 100.0), Pos2::new(200.0, 150.0));
-- let (knob, stem) = rotate_knob(at_top, view);
-- assert_eq!((knob, stem), (Pos2::new(150.0, 172.0), Pos2::new(150.0, 150.0)));
-- }
--
-- #[test]
-- fn the_rotate_knob_measures_clockwise_degrees_from_straight_up() {
-- let c = Pos2::new(100.0, 100.0);
-- let at = |x: f32, y: f32| turn_degrees(c, Pos2::new(x, y), false);
-- assert!(at(100.0, 0.0).abs() < 1e-3);
-- assert!((at(200.0, 100.0) - 90.0).abs() < 1e-3, "right is a clockwise quarter");
-- assert!((at(0.0, 100.0) + 90.0).abs() < 1e-3);
-- assert!((at(100.0, 200.0).abs() - 180.0).abs() < 1e-3);
-- // Shift rounds to 15 degrees; the centre and non-finite points turn nothing.
-- assert_eq!(turn_degrees(c, Pos2::new(180.0, 50.0), true) % 15.0, 0.0);
-- assert_eq!(at(101.0, 101.0), 0.0);
-- assert_eq!(at(f32::NAN, 5.0), 0.0);
-- }
--
-- /// An app with one text document open, and an editor on its first paragraph.
-- fn preview_app() -> (crate::PdfCraftApp, LineEditor) {
-- let mut app = crate::PdfCraftApp::new();
-- let bytes = app.session.create_from_text("Preview", "Preview text").unwrap();
-- app.open_bytes("preview.pdf", None, bytes.as_ref().clone()).unwrap();
-- let doc = app.session.get(app.views[0].id).unwrap();
-- let block = doc.text_blocks(0).remove(0);
-- let ed = LineEditor {
-- selection: None,
-- font_resource: None,
-- page: 0,
-- block: 0,
-- text: block.text.clone(),
-- original: block.text.clone(),
-- rect: Rect::NOTHING,
-- source_rect: block.rect.map(|v| v as f32),
-- multiline: block.rows.len() > 1,
-- source: None,
-- fonts: BTreeMap::new(),
-- overflow: false,
-- max_width: 400.0,
-- size: block.size as f32,
-- focus: true,
-- had_focus: false,
-- look: look_of(&block),
-- look0: look_of(&block),
-- family_chosen: false,
-- extras: Extras::default(),
-- extras0: Extras::default(),
- typing_from: None,
-- };
-- (app, ed)
-- }
--
-- #[test]
-- fn picking_the_family_the_font_name_reads_as_still_changes_the_font() {
-- // A Calibri paragraph reads as Helvetica; picking Helvetica from the list used to leave
-- // `look` equal to what the paragraph had, so no family change was sent and Calibri stayed.
-- let (_app, mut ed) = preview_app();
-- let family = ed.look0.family;
-- assert_eq!(ed.style().family, None, "nothing chosen yet");
-- ed.choose_family(family);
-- assert_eq!(ed.style().family, Some((family, ed.look.bold, ed.look.italic)));
-- assert!(ed.has_changes());
-- assert_eq!(ed.font_shown(&[]), family.label());
-- ed.keep_source_font();
-- assert_eq!(ed.style().family, None, "Keep source fonts withdraws the choice");
-- ed.choose_family(family);
-- ed.applied();
-- assert_eq!(ed.style().family, None, "an applied choice is the paragraph's font now");
-- }
--
-- #[test]
-- fn a_loaded_font_is_applied_to_the_paragraph_being_edited() {
-- // Loading a font used to only register it on the page: the paragraph kept its source
-- // face until the font was found again in the document-font menu (#2591).
-- let (mut app, ed) = preview_app();
-- app.views[0].line_editor = Some(ed);
-- let inter = include_bytes!("../../../../assets/fonts/Inter-Regular.ttf").to_vec();
-- app.use_files(crate::files::FilePurpose::TextFont { page: 0 }, vec![("Inter-Regular.ttf".into(), inter)]);
-- let doc = app.session.get(app.views[0].id).unwrap();
-- let lines = doc.text_lines(0);
-- assert_eq!(lines.iter().map(|l| l.text.as_str()).collect::<String>(), "Preview text");
-- assert!(lines.iter().all(|l| l.base_font == "Inter-Regular"), "{:?}", lines.iter().map(|l| &l.base_font).collect::<Vec<_>>());
-- assert!(app.views[0].line_editor.is_some(), "the paragraph stays open for editing");
-- }
--
-- #[test]
-- fn save_applies_the_paragraph_open_in_the_editor() {
-- // Save wrote the file without the paragraph still open in the editor (#3496).
-- let (mut app, mut ed) = preview_app();
-- ed.text = "Text preview".into();
-- app.views[0].line_editor = Some(ed);
-- let dir = std::env::temp_dir().join(format!("pdfcraft-ui-open-paragraph-{}", std::process::id()));
-- let _ = std::fs::remove_dir_all(&dir);
-- std::fs::create_dir_all(&dir).unwrap();
-- app.save_override = Some(dir.join("out.pdf").to_string_lossy().into_owned());
--
-- assert!(app.save_active(crate::SaveTarget::InPlace));
-- assert!(app.views[0].line_editor.is_none());
-- let doc = app.session.get(app.views[0].id).unwrap();
-- assert_eq!(doc.text_lines(0).iter().map(|l| l.text.as_str()).collect::<String>(), "Text preview");
-- let _ = std::fs::remove_dir_all(&dir);
-- }
--
-- #[test]
-- fn a_refused_text_box_does_not_lose_the_open_paragraph() {
-- let (mut app, mut ed) = preview_app();
-- ed.text = "Text preview".into();
-- app.views[0].line_editor = Some(ed);
-- let draft = crate::content_ui::TextDraft {
-- page: 9,
-- index: None,
-- rect: [20.0, 20.0, 120.0, 40.0],
-- text: "Typed text".into(),
-- style: crate::content_ui::default_style(),
-- focus: true,
- selection: None,
- typing_from: None,
-- };
-- app.views[0].content.draft = Some(draft.clone());
--
-- assert!(!app.commit_typing_in(app.views[0].id));
-- assert_eq!(app.views[0].content.draft, Some(draft), "the refused text box stays open");
-- assert!(app.views[0].line_editor.is_none(), "the paragraph was applied");
-- let doc = app.session.get(app.views[0].id).unwrap();
-- assert_eq!(doc.text_lines(0).iter().map(|l| l.text.as_str()).collect::<String>(), "Text preview");
-- }
--
-- #[test]
-- fn a_refused_paragraph_stays_open_with_its_text() {
-- let (mut app, mut ed) = preview_app();
-- ed.text = "Text preview".into();
-- ed.page = 9;
-- app.views[0].line_editor = Some(ed.clone());
--
-- assert!(!app.commit_typing_in(app.views[0].id));
-- assert_eq!(app.views[0].line_editor, Some(ed), "the paragraph keeps its unsaved text");
-- }
--
-- #[test]
-- fn text_background_is_cached_without_editing_and_released_on_cancel() {
-- let (app, ed) = preview_app();
-- let doc = app.session.get(app.views[0].id).unwrap();
-- let source = doc.bytes.clone();
-- let generation = doc.edit_generation();
-- let ctx = egui::Context::default();
-- let mut background = TextBackground::default();
-- for _ in 0..100 {
-- background.prepare(&ctx, doc, Some(&ed), None, 1.0);
-- if background.ready(0, 0) {
-- break;
-- }
-- std::thread::sleep(std::time::Duration::from_millis(5));
-- }
-- assert!(background.ready(0, 0));
-- assert!(!background.pending());
-- let texture = background.texture.as_ref().unwrap().id();
-- background.prepare(&ctx, doc, Some(&ed), None, 1.0);
-- assert_eq!(background.texture.as_ref().unwrap().id(), texture);
-- assert_eq!(doc.bytes, source);
-- assert_eq!(doc.edit_generation(), generation);
-- assert!(!doc.dirty && doc.can_undo().is_none());
-- background.prepare(&ctx, doc, None, None, 1.0);
-- assert!(background.texture.is_none() && background.renderer.is_none());
-- }
--
-- #[test]
-- fn the_editing_background_stays_sharp_on_a_zoomed_page() {
-- // While a paragraph is open, the page around it is drawn from this one raster: it is
-- // asked for at the tiling threshold, not the 2048-pixel backdrop side, which went soft
-- // beyond about 1.2x on a Retina display.
-- let (app, ed) = preview_app();
-- let doc = app.session.get(app.views[0].id).unwrap();
-- let page = &doc.info.pages[0];
-- let ctx = egui::Context::default();
-- ctx.begin_pass(egui::RawInput { max_texture_side: Some(16384), ..Default::default() });
-- let mut background = TextBackground::default();
-- background.prepare(&ctx, doc, Some(&ed), None, 8.0);
-- let expected = 8.0f32.min(crate::canvas::TILE_THRESHOLD / page.width.max(page.height));
-- assert!(expected > crate::canvas::BASE_SIDE / page.width.max(page.height), "the test page needs the larger side");
-- assert_eq!(background.tag, Some((expected * 1000.0) as u64));
-- // A GPU that can't hold such a texture caps it.
-- ctx.end_pass().textures_delta.clear();
-- ctx.begin_pass(egui::RawInput { max_texture_side: Some(2048), ..Default::default() });
-- let mut small = TextBackground::default();
-- small.prepare(&ctx, doc, Some(&ed), None, 8.0);
-- assert_eq!(small.tag, Some(((2048.0 / page.width.max(page.height)) * 1000.0) as u64));
-- ctx.end_pass().textures_delta.clear();
-- }
--
-- #[test]
-- fn pale_text_is_edited_on_a_dark_box() {
-- // #913: white text from a coloured banner was typed onto a white box and vanished.
-- for pale in [Color32::WHITE, Color32::from_rgb(0xFF, 0xFF, 0x00), Color32::from_gray(0xE0), Color32::from_rgb(0xFF, 0xA5, 0x00)] {
-- assert_eq!(editor_fill(pale), DARK_EDITOR_FILL, "{pale:?}");
-- assert!(contrast(pale, editor_fill(pale)) >= 4.5, "{pale:?} is readable");
-- }
-- // Ordinary dark or saturated text keeps the white box.
-- for dark in [Color32::BLACK, Color32::from_rgb(0x1F, 0x2A, 0x6B), Color32::from_rgb(0xC0, 0x00, 0x00), Color32::from_gray(0x60)] {
-- assert_eq!(editor_fill(dark), Color32::WHITE, "{dark:?}");
-- }
-- }
--
-- #[test]
-- fn caret_contrasts_with_its_fill() {
-- assert_eq!(caret_color_on(Color32::WHITE), Color32::BLACK);
-- assert_eq!(caret_color_on(Color32::from_rgb(0xFF, 0xFF, 0xF4)), Color32::BLACK);
-- assert_eq!(caret_color_on(Color32::BLACK), Color32::WHITE);
-- assert_eq!(caret_color_on(DARK_EDITOR_FILL), Color32::WHITE);
- }
-
- #[test]
- fn bullet_list_toggles_and_preserves_content() {
- let mut text = "First line\nSecond line\n\nThird line".to_string();
- assert!(!is_bulleted(&text));
- assert!(toggle_bullet_list(&mut text));
- assert!(is_bulleted(&text));
- assert_eq!(text, "• First line\n• Second line\n\n• Third line");
-
- // Toggling again removes bullets
- assert!(toggle_bullet_list(&mut text));
- assert!(!is_bulleted(&text));
- assert_eq!(text, "First line\nSecond line\n\nThird line");
- }
-
- #[test]
- fn numbered_list_toggles_and_preserves_content() {
- let mut text = "Item A\nItem B\n\nItem C".to_string();
- assert!(!is_numbered(&text));
- assert!(toggle_numbered_list(&mut text));
- assert!(is_numbered(&text));
- assert_eq!(text, "1. Item A\n2. Item B\n\n3. Item C");
-
- // Toggling again removes numbering
- assert!(toggle_numbered_list(&mut text));
- assert!(!is_numbered(&text));
- assert_eq!(text, "Item A\nItem B\n\nItem C");
- }
-
- #[test]
- fn superscript_and_subscript_conversion() {
- let mut ed = LineEditor {
- page: 0,
- block: 0,
- text: "E = mc2".to_string(),
- original: "E = mc2".to_string(),
- rect: egui::Rect::ZERO,
- source_rect: [0.0; 4],
- max_width: 100.0,
- overflow: false,
- selection: Some(6..7), // selecting '2'
- font_resource: None,
- fonts: std::collections::BTreeMap::new(),
- source: None,
- multiline: false,
- size: 12.0,
- family_chosen: false,
- focus: true,
- had_focus: false,
- look: pdfcraft_engine::AddedText::default(),
- look0: pdfcraft_engine::AddedText::default(),
- extras: Extras::default(),
- extras0: Extras::default(),
- };
-
- assert!(toggle_superscript(&mut ed));
- assert_eq!(ed.text, "E = mc²");
- // Toggling superscript again returns to normal '2'
- assert!(toggle_superscript(&mut ed));
- assert_eq!(ed.text, "E = mc2");
-
- // Test subscript on H2O
- ed.text = "H2O".to_string();
- ed.selection = Some(1..2); // selecting '2'
- assert!(toggle_subscript(&mut ed));
- assert_eq!(ed.text, "H₂O");
- assert!(toggle_subscript(&mut ed));
- assert_eq!(ed.text, "H2O");
- }
-
- #[test]
- fn value_based_line_spacing_in_style() {
- let ed = LineEditor {
- page: 0,
- block: 0,
- text: "Paragraph text".to_string(),
- original: "Paragraph text".to_string(),
- rect: egui::Rect::ZERO,
- source_rect: [0.0; 4],
- max_width: 100.0,
- overflow: false,
- selection: None,
- font_resource: None,
- fonts: std::collections::BTreeMap::new(),
- source: None,
- multiline: false,
- size: 12.0,
- family_chosen: false,
- focus: true,
- had_focus: false,
- look: pdfcraft_engine::AddedText::default(),
- look0: pdfcraft_engine::AddedText::default(),
- extras: Extras { line_spacing: 1.33, underline: true, ..Default::default() },
- extras0: Extras::default(),
- };
- let style = ed.style();
- assert_eq!(style.line_spacing, Some(1.33));
- assert_eq!(style.underline, Some(true));
-- }
--}
++//! Edit a PDF ▸ Edit text & images: boxes around the paragraphs and images already on the page.
++//! Click a paragraph to edit it in place (⌘Enter or clicking away applies and rewraps it to the
++//! box, Esc cancels); drag it to move it, or drag the handle on its left or right edge to rewrap
++//! it to a new width. Click an image to select it: drag to move, drag a corner to resize (keeping
++//! its proportions), right-click for rotate, flip, replace, save and delete; Delete removes it.
++
++use std::collections::BTreeMap;
++use std::sync::Arc;
++
++use egui::{Color32, CornerRadius, FontFamily, FontId, Pos2, Rect, Stroke, pos2};
++use pdfcraft_engine::{Edit, ObjectKind, ObjectTarget};
++use pdfcraft_render::DocInfo;
++
++use crate::canvas::{DocView, PageToolContext, PageXform};
++use crate::page_cursor::Layer;
++use crate::page_pointer::PagePointer;
++use crate::page_router::{Hit, Owner, ParagraphPart};
++
++mod background;
++mod image;
++mod layout;
++
++pub(crate) use background::TextBackground;
++pub use image::{CropMode, DragTarget, ImageAction, ImageDrag, ImageHandle, ImageSelection, image_menu_id};
++pub(crate) use image::{
++ ImageCx, Matrix, body_hit as picture_hit, handle_hit as picture_handle_hit, image_context_menu, image_input,
++ quad_contains, user_to_screen,
++};
++#[cfg(test)]
++use image::{rotate_knob, turn_degrees};
++#[cfg(test)]
++use layout::{DARK_EDITOR_FILL, contrast};
++pub(crate) use layout::{caret_color_on, editor_fill, editor_font, install_source_fonts};
++use layout::{color32, editor_galley, editor_job, look_of, luminance, source_look, unseen};
++
++const ACCENT: Color32 = Color32::from_rgb(0x14, 0x73, 0xE6);
++
++#[derive(Clone, Copy, PartialEq)]
++enum TextTarget {
++ Block(usize),
++ Added(usize),
++}
++
++/// How many faint Edit-text outlines are drawn per page per frame. CAD plots can draw
++/// ~1000 tiny images on one page; stroking all of them every frame (plus a hover query
++/// per box) stalls the UI. Past this, only the selected and hovered boxes are outlined:
++/// hit-testing still covers every image, so nothing becomes unselectable.
++pub(crate) const MAX_OUTLINES: usize = 250;
++
++pub fn is_bulleted(text: &str) -> bool {
++ let mut lines = text.lines();
++ let Some(first) = lines.next() else { return false };
++ first.trim_start().starts_with("• ") || first.trim_start().starts_with("- ")
++}
++
++pub fn is_numbered(text: &str) -> bool {
++ let mut lines = text.lines();
++ let Some(first) = lines.next() else { return false };
++ let t = first.trim_start();
++ let num_end = t.find('.').unwrap_or(0);
++ num_end > 0 && t[..num_end].chars().all(|c| c.is_ascii_digit()) && t[num_end..].starts_with(". ")
++}
++
++pub fn toggle_bullet_list(text: &mut String) -> bool {
++ let has_trailing_crlf = text.ends_with("\r\n");
++ let has_trailing_newline = text.ends_with('\n');
++ let lines: Vec<&str> = text.lines().collect();
++ if lines.is_empty() {
++ return false;
++ }
++ let all_bulleted = lines.iter().all(|l| {
++ let t = l.trim_start();
++ t.is_empty() || t.starts_with("• ") || t.starts_with("- ")
++ });
++ let mut new_text = String::with_capacity(text.len() + 16);
++ for (i, line) in lines.iter().enumerate() {
++ if i > 0 {
++ new_text.push('\n');
++ }
++ let trimmed = line.trim_start();
++ if all_bulleted {
++ if let Some(rest) = trimmed.strip_prefix("• ").or_else(|| trimmed.strip_prefix("- ")) {
++ new_text.push_str(rest);
++ } else {
++ new_text.push_str(line);
++ }
++ } else if trimmed.is_empty() || trimmed.starts_with("• ") {
++ new_text.push_str(line);
++ } else {
++ new_text.push_str("• ");
++ new_text.push_str(line);
++ }
++ }
++ if has_trailing_crlf {
++ new_text.push_str("\r\n");
++ } else if has_trailing_newline {
++ new_text.push('\n');
++ }
++ if new_text != *text {
++ *text = new_text;
++ true
++ } else {
++ false
++ }
++}
++
++pub fn toggle_numbered_list(text: &mut String) -> bool {
++ let has_trailing_crlf = text.ends_with("\r\n");
++ let has_trailing_newline = text.ends_with('\n');
++ let lines: Vec<&str> = text.lines().collect();
++ if lines.is_empty() {
++ return false;
++ }
++ let is_num_line = |l: &str| {
++ let t = l.trim_start();
++ if t.is_empty() {
++ return true;
++ }
++ let dot_idx = t.find('.').unwrap_or(0);
++ dot_idx > 0 && t[..dot_idx].chars().all(|c| c.is_ascii_digit()) && t[dot_idx..].starts_with(". ")
++ };
++ let all_numbered = lines.iter().all(|l| is_num_line(l));
++ let mut new_text = String::with_capacity(text.len() + 16);
++ let mut num = 1;
++ for (i, line) in lines.iter().enumerate() {
++ if i > 0 {
++ new_text.push('\n');
++ }
++ let t = line.trim_start();
++ if all_numbered {
++ if let Some(dot_idx) = t.find('.')
++ && dot_idx > 0
++ && t[..dot_idx].chars().all(|c| c.is_ascii_digit())
++ && t[dot_idx..].starts_with(". ")
++ {
++ new_text.push_str(t.get(dot_idx + 2..).unwrap_or(""));
++ continue;
++ }
++ new_text.push_str(line);
++ } else if t.is_empty() {
++ new_text.push_str(line);
++ } else {
++ use std::fmt::Write as _;
++ let _ = write!(new_text, "{num}. ");
++ new_text.push_str(t);
++ num += 1;
++ }
++ }
++ if has_trailing_crlf {
++ new_text.push_str("\r\n");
++ } else if has_trailing_newline {
++ new_text.push('\n');
++ }
++ if new_text != *text {
++ *text = new_text;
++ true
++ } else {
++ false
++ }
++}
++
++pub fn char_to_superscript(c: char) -> char {
++ match c {
++ '0' => '⁰',
++ '1' => '¹',
++ '2' => '²',
++ '3' => '³',
++ '4' => '⁴',
++ '5' => '⁵',
++ '6' => '⁶',
++ '7' => '⁷',
++ '8' => '⁸',
++ '9' => '⁹',
++ '+' => '⁺',
++ '-' | '−' => '⁻',
++ '=' => '⁼',
++ '(' => '⁽',
++ ')' => '⁾',
++ 'a' => 'ᵃ',
++ 'b' => 'ᵇ',
++ 'c' => 'ᶜ',
++ 'd' => 'ᵈ',
++ 'e' => 'ᵉ',
++ 'f' => 'ᶠ',
++ 'g' => 'ᵍ',
++ 'h' => 'ʰ',
++ 'i' => 'ⁱ',
++ 'j' => 'ʲ',
++ 'k' => 'ᵏ',
++ 'l' => 'ˡ',
++ 'm' => 'ᵐ',
++ 'n' => 'ⁿ',
++ 'o' => 'ᵒ',
++ 'p' => 'ᵖ',
++ 'r' => 'ʳ',
++ 's' => 'ˢ',
++ 't' => 'ᵗ',
++ 'u' => 'ᵘ',
++ 'v' => 'ᵛ',
++ 'w' => 'ʷ',
++ 'x' => 'ˣ',
++ 'y' => 'ʸ',
++ 'z' => 'ᶻ',
++ 'A' => 'ᴬ',
++ 'B' => 'ᴮ',
++ 'D' => 'ᴰ',
++ 'E' => 'ᴱ',
++ 'G' => 'ᴳ',
++ 'H' => 'ᴴ',
++ 'I' => 'ᴵ',
++ 'J' => 'ᴶ',
++ 'K' => 'ᴷ',
++ 'L' => 'ᴸ',
++ 'M' => 'ᴹ',
++ 'N' => 'ᴺ',
++ 'O' => 'ᴼ',
++ 'P' => 'ᴾ',
++ 'R' => 'ᴿ',
++ 'T' => 'ᵀ',
++ 'U' => 'ᵁ',
++ 'V' => 'ⱽ',
++ 'W' => 'ᵂ',
++ other => other,
++ }
++}
++
++pub fn char_from_superscript(c: char) -> char {
++ match c {
++ '⁰' => '0',
++ '¹' => '1',
++ '²' => '2',
++ '³' => '3',
++ '⁴' => '4',
++ '⁵' => '5',
++ '⁶' => '6',
++ '⁷' => '7',
++ '⁸' => '8',
++ '⁹' => '9',
++ '⁺' => '+',
++ '⁻' => '-',
++ '⁼' => '=',
++ '⁽' => '(',
++ '⁾' => ')',
++ 'ᵃ' => 'a',
++ 'ᵇ' => 'b',
++ 'ᶜ' => 'c',
++ 'ᵈ' => 'd',
++ 'ᵉ' => 'e',
++ 'ᶠ' => 'f',
++ 'ᵍ' => 'g',
++ 'ʰ' => 'h',
++ 'ⁱ' => 'i',
++ 'ʲ' => 'j',
++ 'ᵏ' => 'k',
++ 'ˡ' => 'l',
++ 'ᵐ' => 'm',
++ 'ⁿ' => 'n',
++ 'ᵒ' => 'o',
++ 'ᵖ' => 'p',
++ 'ʳ' => 'r',
++ 'ˢ' => 's',
++ 'ᵗ' => 't',
++ 'ᵘ' => 'u',
++ 'ᵛ' => 'v',
++ 'ʷ' => 'w',
++ 'ˣ' => 'x',
++ 'ʸ' => 'y',
++ 'ᶻ' => 'z',
++ 'ᴬ' => 'A',
++ 'ᴮ' => 'B',
++ 'ᴰ' => 'D',
++ 'ᴱ' => 'E',
++ 'ᴳ' => 'G',
++ 'ᴴ' => 'H',
++ 'ᴵ' => 'I',
++ 'ᴶ' => 'J',
++ 'ᴷ' => 'K',
++ 'ᴸ' => 'L',
++ 'ᴹ' => 'M',
++ 'ᴺ' => 'N',
++ 'ᴼ' => 'O',
++ 'ᴾ' => 'P',
++ 'ᴿ' => 'R',
++ 'ᵀ' => 'T',
++ 'ᵁ' => 'U',
++ 'ⱽ' => 'V',
++ 'ᵂ' => 'W',
++ other => other,
++ }
++}
++
++pub fn char_to_subscript(c: char) -> char {
++ match c {
++ '0' => '₀',
++ '1' => '₁',
++ '2' => '₂',
++ '3' => '₃',
++ '4' => '₄',
++ '5' => '₅',
++ '6' => '₆',
++ '7' => '₇',
++ '8' => '₈',
++ '9' => '₉',
++ '+' => '₊',
++ '-' | '−' => '₋',
++ '=' => '₌',
++ '(' => '₍',
++ ')' => '₎',
++ 'a' => 'ₐ',
++ 'e' => 'ₑ',
++ 'h' => 'ₕ',
++ 'i' => 'ᵢ',
++ 'j' => 'ⱼ',
++ 'k' => 'ₖ',
++ 'l' => 'ₗ',
++ 'm' => 'ₘ',
++ 'n' => 'ₙ',
++ 'o' => 'ₒ',
++ 'p' => 'ₚ',
++ 'r' => 'ᵣ',
++ 's' => 'ₛ',
++ 't' => 'ₜ',
++ 'u' => 'ᵤ',
++ 'v' => 'ᵥ',
++ 'x' => 'ₓ',
++ other => other,
++ }
++}
++
++pub fn char_from_subscript(c: char) -> char {
++ match c {
++ '₀' => '0',
++ '₁' => '1',
++ '₂' => '2',
++ '₃' => '3',
++ '₄' => '4',
++ '₅' => '5',
++ '₆' => '6',
++ '₇' => '7',
++ '₈' => '8',
++ '₉' => '9',
++ '₊' => '+',
++ '₋' => '-',
++ '₌' => '=',
++ '₍' => '(',
++ '₎' => ')',
++ 'ₐ' => 'a',
++ 'ₑ' => 'e',
++ 'ₕ' => 'h',
++ 'ᵢ' => 'i',
++ 'ⱼ' => 'j',
++ 'ₖ' => 'k',
++ 'ₗ' => 'l',
++ 'ₘ' => 'm',
++ 'ₙ' => 'n',
++ 'ₒ' => 'o',
++ 'ₚ' => 'p',
++ 'ᵣ' => 'r',
++ 'ₛ' => 's',
++ 'ₜ' => 't',
++ 'ᵤ' => 'u',
++ 'ᵥ' => 'v',
++ 'ₓ' => 'x',
++ other => other,
++ }
++}
++
++fn snap_char_boundaries(s: &str, start: usize, end: usize) -> (usize, usize) {
++ let mut s_idx = start.min(s.len());
++ while s_idx > 0 && !s.is_char_boundary(s_idx) {
++ s_idx -= 1;
++ }
++ let mut e_idx = end.min(s.len());
++ while e_idx < s.len() && !s.is_char_boundary(e_idx) {
++ e_idx += 1;
++ }
++ (s_idx, e_idx)
++}
++
++pub fn toggle_superscript(ed: &mut LineEditor) -> bool {
++ let (s_pos, e_pos) = if let Some(r) = &ed.selection { (r.start.min(ed.text.len()), r.end.min(ed.text.len())) } else { (0, ed.text.len()) };
++ let (start, end) = snap_char_boundaries(&ed.text, s_pos, e_pos);
++ if start >= end {
++ return false;
++ }
++ let target = &ed.text[start..end];
++ let all_sup = target.chars().all(|c| char_from_superscript(c) != c);
++ let converted: String =
++ if all_sup { target.chars().map(char_from_superscript).collect() } else { target.chars().map(char_to_superscript).collect() };
++ if converted != target {
++ let conv_len = converted.len();
++ ed.text.replace_range(start..end, &converted);
++ if ed.selection.is_some() {
++ ed.selection = Some(start..start + conv_len);
++ }
++ true
++ } else {
++ false
++ }
++}
++
++pub fn toggle_subscript(ed: &mut LineEditor) -> bool {
++ let (s_pos, e_pos) = if let Some(r) = &ed.selection { (r.start.min(ed.text.len()), r.end.min(ed.text.len())) } else { (0, ed.text.len()) };
++ let (start, end) = snap_char_boundaries(&ed.text, s_pos, e_pos);
++ if start >= end {
++ return false;
++ }
++ let target = &ed.text[start..end];
++ let all_sub = target.chars().all(|c| char_from_subscript(c) != c);
++ let converted: String = if all_sub { target.chars().map(char_from_subscript).collect() } else { target.chars().map(char_to_subscript).collect() };
++ if converted != target {
++ let conv_len = converted.len();
++ ed.text.replace_range(start..end, &converted);
++ if ed.selection.is_some() {
++ ed.selection = Some(start..start + conv_len);
++ }
++ true
++ } else {
++ false
++ }
++}
++
++/// A paragraph being edited.
++#[derive(Clone, Debug, PartialEq)]
++pub struct LineEditor {
++ pub selection: Option<std::ops::Range<usize>>,
++ pub font_resource: Option<String>,
++ pub page: usize,
++ pub block: usize,
++ pub text: String,
++ original: String,
++ rect: Rect,
++ /// The original PDF rectangle, in user space. It is converted again each frame so the editor
++ /// stays attached while the page is zoomed, scrolled, or rotated.
++ source_rect: [f32; 4],
++ multiline: bool,
++ source: Option<(pdfcraft_engine::TextBlock, Vec<pdfcraft_engine::TextLine>)>,
++ pub(crate) fonts: BTreeMap<String, String>,
++ /// The draft extends outside the source container; its frame follows the live draft bounds.
++ pub overflow: bool,
++ /// The initial source container width in screen pixels.
++ max_width: f32,
++ /// Current screen-space size. Recomputed from the current page transform before painting.
++ size: f32,
++ /// Take the keyboard focus on the next frame.
++ pub(crate) focus: bool,
++ /// Whether the text box had the keyboard at the end of the editor's last frame
++ /// ([`lost_focus_once`]).
++ had_focus: bool,
++ /// The Format text panel's values, and what the paragraph had (to send only changes).
++ pub look: pdfcraft_engine::AddedText,
++ look0: pdfcraft_engine::AddedText,
++ /// A standard family was picked from the font list. The family guessed from a PDF font name
++ /// (Calibri reads as Helvetica) can be the one picked, so comparing `look` with `look0` alone
++ /// would miss the choice.
++ family_chosen: bool,
++ /// Underline, line spacing (× size; 0 = the paragraph's own), character spacing (pt) and
++ /// horizontal scale (%), and what they were.
++ pub extras: Extras,
++ extras0: Extras,
++ /// The text the box's own Undo history starts from; see [`forget_typing_before`].
++ typing_from: Option<String>,
++}
++
++/// The paragraph editor's text box.
++pub(crate) fn input_id() -> egui::Id {
++ egui::Id::unique("edit-text-line-input")
++}
++
++/// The page text box `id` keeps its own Undo history, across every paragraph or box it has shown
++/// in every tab (they share the id). Once the page has `text` (the box opened on it, or formatting
++/// applied it), that history is cleared: Undo in the box takes back only what was typed since, and
++/// then reaches the document. Kept, it put back text from another paragraph, or the whole paragraph
++/// as it was before a format change, into a box showing the page as it is now. `from` is the box's
++/// own record of that; which document `doc` last used the history is kept beside it, since a box
++/// in another tab on the same text would otherwise take that document's typing with its Redo.
++/// When another document's box takes the text box, the box it leaves keeps its history (and
++/// caret) aside, and gets it back on its return, so its typing can still be undone there. Each
++/// box claims the text box when it is drawn, and Undo and Redo from the menu claim it first too,
++/// since they can run before the box is drawn again after a tab switch.
++pub(crate) fn forget_typing_before(ctx: &egui::Context, id: egui::Id, doc: pdfcraft_engine::DocId, from: &mut Option<String>, text: &str) {
++ let owner = id.with("typing-doc");
++ let last = ctx.data(|d| d.get_temp::<pdfcraft_engine::DocId>(owner));
++ if from.as_deref() == Some(text) && last == Some(doc) {
++ return;
++ }
++ let aside = |doc: pdfcraft_engine::DocId| id.with(("typing-aside", doc));
++ let aligns = id.with(ALIGN_HISTORY);
++ if let Some(left) = last.filter(|l| *l != doc)
++ && let Some(state) = egui::TextEdit::load_state(ctx, id)
++ {
++ // A copy of its own: a state's history is shared between its clones, and the next box
++ // clears it in place. The rows' alignments by text go with it.
++ let mut kept = egui::text_edit::TextEditState::default();
++ kept.cursor = state.cursor;
++ kept.set_undoer(state.undoer());
++ ctx.data_mut(|d| {
++ let rows = d.get_temp::<AlignHistory>(aligns).unwrap_or_default();
++ d.insert_temp(aside(left), (kept, rows));
++ });
++ }
++ // Only the same box, still on the text it opened on, takes its history back.
++ let back =
++ ctx.data_mut(|d| d.remove_temp::<(egui::text_edit::TextEditState, AlignHistory)>(aside(doc))).filter(|_| from.as_deref() == Some(text));
++ // A box new to the text box starts with nothing of the last one's: no history, caret,
++ // selection or rows' alignments by text.
++ let (state, rows) = back.unwrap_or_default();
++ egui::TextEdit::store_state(ctx, id, state);
++ ctx.data_mut(|d| {
++ d.insert_temp(owner, doc);
++ d.insert_temp(aligns, rows);
++ });
++ *from = Some(text.to_owned());
++}
++
++/// Each text a page box has shown through its own typing, with its rows' alignments then, oldest
++/// last: egui's history keeps only the text and the caret.
++type AlignHistory = Vec<(String, Vec<Vec<Option<pdfcraft_engine::TextAlign>>>)>;
++const ALIGN_HISTORY: &str = "aligns-by-text";
++/// As many texts as egui's own history keeps.
++const ALIGN_HISTORY_LEN: usize = 100;
++
++/// The page text box `id` went from `before` to `after` through its own typing (typing, Undo or
++/// Redo), and each list of row alignments in `aligns` follows. A text the box showed before gets
++/// back the alignments it had then, so Undo bringing back a deleted row brings back its
++/// alignment; a new text follows the edit ([`pdfcraft_engine::paragraph_align::remap`]). An
++/// alignment picked since makes the older texts' alignments stale, and they are dropped.
++pub(crate) fn follow_typing(
++ ctx: &egui::Context,
++ id: egui::Id,
++ before: &str,
++ after: &str,
++ aligns: &mut [&mut Vec<Option<pdfcraft_engine::TextAlign>>],
++) {
++ let now: Vec<Vec<_>> = aligns.iter().map(|a| a.to_vec()).collect();
++ // Where the edit left the caret, as a byte of `after`: it says which of two same rows changed.
++ let caret = egui::TextEdit::load_state(ctx, id)
++ .and_then(|s| s.cursor.char_range())
++ .map(|r| after.char_indices().nth(r.primary.index.into()).map_or(after.len(), |(at, _)| at));
++ let next = ctx.data_mut(|d| {
++ let seen = d.get_temp_mut_or_default::<AlignHistory>(id.with(ALIGN_HISTORY));
++ if seen.iter().any(|(text, was)| text == before && *was != now) {
++ seen.clear();
++ }
++ remember(seen, before, now.clone());
++ let next = match seen.iter().rev().find(|(text, _)| text == after) {
++ Some((_, was)) if was.len() == now.len() => was.clone(),
++ _ => now.iter().map(|a| pdfcraft_engine::paragraph_align::remap_at(before, after, a, caret)).collect(),
++ };
++ remember(seen, after, next.clone());
++ next
++ });
++ for (slot, a) in aligns.iter_mut().zip(next) {
++ **slot = a;
++ }
++}
++
++fn remember(seen: &mut AlignHistory, text: &str, aligns: Vec<Vec<Option<pdfcraft_engine::TextAlign>>>) {
++ seen.retain(|(t, _)| t != text);
++ seen.push((text.to_owned(), aligns));
++ if seen.len() > ALIGN_HISTORY_LEN {
++ seen.remove(0);
++ }
++}
++
++/// Select `range` (bytes of `text`) in the page text box `id` again. egui keeps a box's selection
++/// only while it has the focus, so a click on Format text drops it; given back as formatting
++/// returns the focus, it stays shown, and a second choice reaches the same text.
++pub(crate) fn reselect(ctx: &egui::Context, id: egui::Id, text: &str, range: &std::ops::Range<usize>) {
++ let chars = |at: usize| text.get(..at).map(|s| s.chars().count());
++ let (Some(start), Some(end)) = (chars(range.start), chars(range.end)) else { return };
++ if start < end {
++ let mut state = egui::TextEdit::load_state(ctx, id).unwrap_or_default();
++ state.cursor.set_char_range(Some(egui::text::CCursorRange::two(egui::text::CCursor::new(start), egui::text::CCursor::new(end))));
++ egui::TextEdit::store_state(ctx, id, state);
++ }
++}
++
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub struct Extras {
++ pub underline: bool,
++ pub line_spacing: f64,
++ pub char_spacing: f64,
++ pub scale: f64,
++}
++
++impl Default for Extras {
++ fn default() -> Self {
++ Extras { underline: false, line_spacing: 0.0, char_spacing: 0.0, scale: 100.0 }
++ }
++}
++
++impl LineEditor {
++ /// What Format text's font list shows: the chosen page font, a standard family picked
++ /// instead, or the paragraph's own font (its `/BaseFont` without the subset tag).
++ pub(crate) fn font_shown(&self, sources: &[(String, String)]) -> String {
++ if let Some(face) = self.font_resource.as_ref().and_then(|r| sources.iter().find(|(res, _)| res == r)).map(|(_, face)| face) {
++ return face.clone();
++ }
++ if self.look.family != self.look0.family || self.family_chosen {
++ return self.look.family.label().to_string();
++ }
++ let base = self.source.as_ref().map(|(block, _)| block.base_font.as_str()).unwrap_or_default();
++ // "ABCDEF+Calibri" is a subset of Calibri.
++ let name = match base.split_once('+') {
++ Some((tag, rest)) if tag.len() == 6 && tag.bytes().all(|b| b.is_ascii_uppercase()) => rest,
++ _ => base,
++ };
++ if name.is_empty() { tl!("Keep source fonts").to_string() } else { name.to_string() }
++ }
++
++ /// Back to the paragraph's own font: no page font chosen and the family it started with.
++ pub(crate) fn keep_source_font(&mut self) {
++ self.font_resource = None;
++ self.look.family = self.look0.family;
++ self.family_chosen = false;
++ }
++
++ /// A standard family picked from the font list, replacing the paragraph's font even when it
++ /// is the family the paragraph's font name reads as.
++ pub(crate) fn choose_family(&mut self, family: pdfcraft_engine::FontFamily) {
++ self.font_resource = None;
++ self.look.family = family;
++ self.family_chosen = true;
++ }
++
++ pub(crate) fn has_changes(&self) -> bool {
++ self.changes_with(&self.text)
++ }
++
++ /// Whether the paragraph with `text` in it differs from what was opened.
++ fn changes_with(&self, text: &str) -> bool {
++ text != self.original || self.look != self.look0 || self.extras != self.extras0 || self.family_chosen
++ }
++
++ /// Extra width permitted by the editor. Source containers now remain fixed.
++ pub fn growth(&self) -> Option<f32> {
++ (self.max_width > self.rect.width()).then_some(self.max_width)
++ }
++
++ /// Whether the edited text differs from the original (unsaved changes).
++ pub fn has_unsaved_text(&self) -> bool {
++ self.text != self.original
++ }
++
++ /// The formatting the panel changed.
++ pub fn style(&self) -> pdfcraft_engine::BlockStyle {
++ let (l, o) = (&self.look, &self.look0);
++ let mut style = pdfcraft_engine::BlockStyle {
++ resource: self.font_resource.clone(),
++ family: (l.family != o.family || self.family_chosen || l.bold != o.bold || l.italic != o.italic).then_some((l.family, l.bold, l.italic)),
++ size: (l.size != o.size).then_some(l.size),
++ color: (l.color != o.color).then_some(l.color),
++ // The whole paragraph's alignment when it was picked, or when the rows' own were
++ // dropped for it (the rows are where those put them, so leaving it out keeps them there).
++ align: (l.align != o.align || l.aligns.is_empty() && !o.aligns.is_empty()).then_some(l.align),
++ underline: (self.extras.underline != self.extras0.underline).then_some(self.extras.underline),
++ line_spacing: (self.extras.line_spacing != self.extras0.line_spacing && self.extras.line_spacing > 0.0)
++ .then_some(self.extras.line_spacing),
++ char_spacing: (self.extras.char_spacing != self.extras0.char_spacing).then_some(self.extras.char_spacing),
++ scale: (self.extras.scale != self.extras0.scale).then_some(self.extras.scale),
++ ..Default::default()
++ };
++ // Rows aligned on their own, sent with every rewrite of the paragraph while it is open
++ // (a colour or size change too): nothing in the page records them, so a rewrite without
++ // them would undo them. Alone, they are sent only when they changed.
++ if l.aligns != o.aligns || self.text != self.original || style != pdfcraft_engine::BlockStyle::default() {
++ style.aligns = l.aligns.clone();
++ }
++ if let Some(range) = &self.selection
++ && (style.family.is_some()
++ || style.resource.is_some()
++ || style.size.is_some()
++ || style.color.is_some()
++ || style.underline.is_some()
++ || style.char_spacing.is_some()
++ || style.scale.is_some())
++ {
++ style.ranges.push(pdfcraft_engine::RangeStyle {
++ range: range.clone(),
++ family: style.family.take(),
++ resource: style.resource.take(),
++ size: style.size.take(),
++ color: style.color.take(),
++ underline: style.underline.take(),
++ char_spacing: style.char_spacing.take(),
++ scale: style.scale.take(),
++ });
++ }
++ style
++ }
++
++ /// After applying formatting: the paragraph now has it. The selection stays, as the box still
++ /// shows it, so a second alignment or format picked for it applies to the same rows.
++ pub fn applied(&mut self) {
++ self.font_resource = None;
++ self.family_chosen = false;
++ self.look0 = self.look.clone();
++ self.extras0 = self.extras;
++ self.original = self.text.clone();
++ self.selection = None;
++ // The page now has this text: the box's history restarts, even when the format left the
++ // text as it was, so a Redo from before can't put undone typing back on top of it.
++ self.typing_from = None;
++ self.focus = true;
++ }
++
++ /// Take the shared text box for this paragraph ([`forget_typing_before`]): its own history,
++ /// or none once the page has its text.
++ pub(crate) fn claim_typing(&mut self, ctx: &egui::Context, doc: pdfcraft_engine::DocId) {
++ forget_typing_before(ctx, input_id(), doc, &mut self.typing_from, &self.original);
++ }
++
++ /// Adopt the rewritten paragraph's current geometry before the next overlay frame.
++ pub(crate) fn refresh_source(&mut self, block: &pdfcraft_engine::TextBlock, lines: Vec<pdfcraft_engine::TextLine>) {
++ self.source_rect = block.rect.map(|v| v as f32);
++ self.multiline = block.rows.len() > 1;
++ self.source = Some((block.clone(), lines));
++ let before = std::mem::replace(&mut self.text, block.editor_text.clone());
++ if self.text != before {
++ // A byte range of the old text: it may name other characters, or none, in the new.
++ self.selection = None;
++ }
++ self.follow_rows(&before);
++ self.original = self.text.clone();
++ }
++
++ pub fn selection_formatting(&self, sources: &[(String, String)]) -> SelectionFormatting {
++ let Some((block, lines)) = &self.source else {
++ return SelectionFormatting {
++ font_shown: self.font_shown(sources),
++ size: Some(self.look.size),
++ family: Some(self.look.family),
++ bold: Some(self.look.bold),
++ italic: Some(self.look.italic),
++ color: Some(self.look.color),
++ };
++ };
++
++ let spans = block.edited_spans(&self.text);
++ let matching_lines: Vec<&pdfcraft_engine::TextLine> = match &self.selection {
++ Some(range) if range.start < range.end => {
++ spans.iter().filter(|(span, _)| span.start < range.end && span.end > range.start).filter_map(|(_, idx)| lines.get(*idx)).collect()
++ }
++ _ => spans.iter().filter_map(|(_, idx)| lines.get(*idx)).collect(),
++ };
++
++ if matching_lines.is_empty() {
++ return SelectionFormatting {
++ font_shown: self.font_shown(sources),
++ size: Some(self.look.size),
++ family: Some(self.look.family),
++ bold: Some(self.look.bold),
++ italic: Some(self.look.italic),
++ color: Some(self.look.color),
++ };
++ }
++
++ let line_font = |l: &pdfcraft_engine::TextLine| -> String {
++ if let Some((_, face)) = sources.iter().find(|(res, _)| res == &l.font) {
++ return face.clone();
++ }
++ let base = l.base_font.as_str();
++ let name = match base.split_once('+') {
++ Some((tag, rest)) if tag.len() == 6 && tag.bytes().all(|b| b.is_ascii_uppercase()) => rest,
++ _ => base,
++ };
++ if let Some((_, face)) = sources.iter().find(|(_, face)| face.as_str() == name) {
++ return face.clone();
++ }
++ let look = look_of_line(l);
++ let lower = name.to_ascii_lowercase();
++ if ["times", "serif", "roman", "courier", "helvetica", "arial"].iter().any(|s| lower.contains(s)) {
++ return look.family.label().to_string();
++ }
++ if name.is_empty() { tl!("Keep source fonts").to_string() } else { name.to_string() }
++ };
++
++ let first_font = line_font(matching_lines[0]);
++ let same_font = matching_lines.iter().skip(1).all(|l| line_font(l) == first_font);
++ let font_shown = if same_font {
++ if self.font_resource.is_some() || self.look.family != self.look0.family || self.family_chosen {
++ self.font_shown(sources)
++ } else {
++ first_font
++ }
++ } else {
++ tl!("Mixed").to_string()
++ };
++
++ let first_size = (matching_lines[0].size * 10.0).round() / 10.0;
++ let same_size = matching_lines.iter().skip(1).all(|l| ((l.size * 10.0).round() / 10.0 - first_size).abs() < 0.05);
++ let size = if same_size { Some(first_size) } else { None };
++
++ let first_look = look_of_line(matching_lines[0]);
++ let same_family = matching_lines.iter().skip(1).all(|l| look_of_line(l).family == first_look.family);
++ let family = if same_family { Some(first_look.family) } else { None };
++ let same_bold = matching_lines.iter().skip(1).all(|l| l.bold == matching_lines[0].bold);
++ let bold = if same_bold { Some(matching_lines[0].bold) } else { None };
++ let same_italic = matching_lines.iter().skip(1).all(|l| l.italic == matching_lines[0].italic);
++ let italic = if same_italic { Some(matching_lines[0].italic) } else { None };
++ let same_color = matching_lines.iter().skip(1).all(|l| l.color == matching_lines[0].color);
++ let color = if same_color { Some(matching_lines[0].color) } else { None };
++
++ SelectionFormatting { font_shown, size, family, bold, italic, color }
++ }
++
++ pub(crate) fn sync_selection_look(&mut self, fmt: &SelectionFormatting) {
++ if let Some(sz) = fmt.size {
++ self.look.size = sz;
++ self.look0.size = sz;
++ } else {
++ if self.look.size <= 0.0 {
++ self.look.size = 12.0;
++ }
++ self.look0.size = self.look.size;
++ }
++ if let Some(f) = fmt.family {
++ self.look.family = f;
++ self.look0.family = f;
++ }
++ match fmt.bold {
++ Some(b) => {
++ self.look.bold = b;
++ self.look0.bold = b;
++ }
++ None => {
++ self.look.bold = false;
++ self.look0.bold = false;
++ }
++ }
++ match fmt.italic {
++ Some(it) => {
++ self.look.italic = it;
++ self.look0.italic = it;
++ }
++ None => {
++ self.look.italic = false;
++ self.look0.italic = false;
++ }
++ }
++ if let Some(c) = fmt.color {
++ self.look.color = c;
++ self.look0.color = c;
++ }
++ }
++
++ /// The text changed from `before` through the box's own typing (typing, Undo, Redo): rows
++ /// aligned on their own keep their alignment, and a row Undo brings back gets its own back.
++ pub(crate) fn follow_typed_rows(&mut self, ctx: &egui::Context, before: &str) {
++ follow_typing(ctx, input_id(), before, &self.text, &mut [&mut self.look.aligns, &mut self.look0.aligns]);
++ }
++
++ /// The text changed from `before`: rows aligned on their own keep their alignment.
++ pub(crate) fn follow_rows(&mut self, before: &str) {
++ for look in [&mut self.look, &mut self.look0] {
++ look.aligns = pdfcraft_engine::paragraph_align::remap(before, &self.text, &look.aligns);
++ }
++ }
++}
++
++#[derive(Clone, Debug, PartialEq)]
++pub struct SelectionFormatting {
++ pub font_shown: String,
++ pub size: Option<f64>,
++ pub family: Option<pdfcraft_engine::FontFamily>,
++ pub bold: Option<bool>,
++ pub italic: Option<bool>,
++ pub color: Option<[f64; 3]>,
++}
++
++fn look_of_line(l: &pdfcraft_engine::TextLine) -> pdfcraft_engine::AddedText {
++ source_look(&l.base_font, l.size, l.color, l.bold, l.italic)
++}
++
++/// What a drag on a paragraph box takes hold of.
++#[derive(Clone, Copy, Debug, PartialEq)]
++enum Grip {
++ Move,
++ /// An edge (screen left or right), dragged to rewrap the paragraph.
++ Left,
++ Right,
++}
++
++/// A paragraph box being dragged: moved, or (from a handle on its left or right edge) resized.
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub struct BlockDrag {
++ page: usize,
++ block: usize,
++ /// A paragraph index belongs to the document generation where the press was resolved.
++ generation: u64,
++ /// Where the drag started (screen).
++ start: Pos2,
++ grip: Grip,
++}
++
++/// How far an edge grip reaches either side of a box's edge (screen pixels).
++const EDGE_REACH: f32 = 6.0;
++/// How far outside an open paragraph's frame the pointer still means the frame (screen pixels):
++/// the frame's Move cursor shows, and a press there takes hold of the box (`object_ui`).
++pub(crate) const FRAME_REACH: f32 = 6.0;
++
++/// The rewrap handles drawn on a paragraph box's left and right edges (screen).
++fn width_handles(b: Rect) -> [Rect; 2] {
++ let h = (b.height() * 0.5).clamp(14.0, 28.0);
++ [b.left(), b.right()].map(|x| Rect::from_center_size(Pos2::new(x, b.center().y), egui::vec2(7.0, h)))
++}
++
++/// The edge of `b` that `p` grabs: anywhere along its height, within reach of the line. A narrow
++/// box keeps its middle for moving.
++fn edge_at(b: Rect, p: Pos2) -> Option<Grip> {
++ if p.y < b.top() - EDGE_REACH || p.y > b.bottom() + EDGE_REACH {
++ return None;
++ }
++ let inside = EDGE_REACH.min(b.width() / 4.0);
++ let (dl, dr) = (p.x - b.left(), b.right() - p.x);
++ match (dl >= -EDGE_REACH && dl <= inside, dr >= -EDGE_REACH && dr <= inside) {
++ (true, true) if dl <= dr => Some(Grip::Left),
++ (_, true) => Some(Grip::Right),
++ (true, false) => Some(Grip::Left),
++ _ => None,
++ }
++}
++
++/// The topmost box under `p` and what it grabs there; edges only on an upright page.
++fn grab_at(boxes: &[Rect], p: Pos2, upright: bool) -> Option<(usize, Grip)> {
++ boxes.iter().enumerate().rev().find_map(|(i, b)| match upright.then(|| edge_at(*b, p)).flatten() {
++ Some(g) => Some((i, g)),
++ None => b.contains(p).then_some((i, Grip::Move)),
++ })
++}
++
++/// A paragraph's box on screen: its text rect (user space), with a little room around it.
++fn paragraph_box(xf: &PageXform, info: &DocInfo, page: usize, rect: [f64; 4]) -> Rect {
++ xf.user_rect(info, page, rect.map(|v| v as f32)).expand(2.0)
++}
++
++/// Whether `p` lies in the box of one of the paragraphs `lines` on `page`, its edge grips there
++/// included, not beside it: where Add text acts on the page's own paragraphs.
++pub(crate) fn over_paragraph(xf: &PageXform, info: &DocInfo, page: usize, lines: &[pdfcraft_engine::TextBlock], p: Pos2) -> bool {
++ lines.iter().any(|l| paragraph_box(xf, info, page, l.rect).contains(p))
++}
++
++/// The width handles need the page upright (or upside down): on a quarter-turned page the
++/// screen's horizontal is the paragraph's vertical.
++fn has_edges(info: &DocInfo, page: usize) -> bool {
++ info.pages.get(page).is_some_and(|p| p.rotation % 180 == 0)
++}
++
++/// What of the paragraphs `lines` lies under `p` on `page`: the one hit test of Edit text's
++/// paragraphs, for the cursor and the press. A closed paragraph moves when dragged (the four-way
++/// arrow says so; a click still opens it for typing) and its edge rewraps it, from a little way
++/// outside the box (the handle is drawn half outside it, #2276); the open one takes the text
++/// cursor. `active` is the paragraph open for typing on this page.
++pub(crate) fn hit(xf: &PageXform, info: &DocInfo, page: usize, lines: &[pdfcraft_engine::TextBlock], active: Option<usize>, p: Pos2) -> Option<Hit> {
++ if !xf.rect.contains(p) {
++ return None;
++ }
++ let boxes: Vec<Rect> = lines.iter().map(|l| paragraph_box(xf, info, page, l.rect)).collect();
++ // Edges rewrap closed paragraphs only: around the open one lies its frame (`object_ui`).
++ let (block, grip) = grab_at(&boxes, p, has_edges(info, page) && active.is_none())?;
++ let paragraph = |part| Owner::Paragraph { block, part };
++ Some(match (active, grip) {
++ (None, Grip::Left) => Hit::new(paragraph(ParagraphPart::Left), Layer::Handle, egui::CursorIcon::ResizeHorizontal),
++ (None, Grip::Right) => Hit::new(paragraph(ParagraphPart::Right), Layer::Handle, egui::CursorIcon::ResizeHorizontal),
++ (None, Grip::Move) => Hit::new(paragraph(ParagraphPart::Body), Layer::Text, egui::CursorIcon::Move),
++ (Some(open), _) if open == block => Hit::new(paragraph(ParagraphPart::Body), Layer::Editor, egui::CursorIcon::Text),
++ (Some(_), _) => Hit::new(paragraph(ParagraphPart::Body), Layer::Text, egui::CursorIcon::Text),
++ })
++}
++
++pub(crate) fn user_box(xf: &PageXform, info: &DocInfo, page: usize, r: Rect) -> [f64; 4] {
++ let p = &info.pages[page];
++ let (a, b) = (xf.screen_to_view(r.min), xf.screen_to_view(r.max));
++ let (u, v) = (p.view_to_user(a.0, a.1), p.view_to_user(b.0, b.1));
++ [u[0].min(v[0]) as f64, u[1].min(v[1]) as f64, u[0].max(v[0]) as f64, u[1].max(v[1]) as f64]
++}
++
++/// Open `block` on `page` in the inline editor. The box's geometry and source are read from the
++/// `lines` handed in, so callers pass the document's current inventory.
++fn open_editor(cx: PageToolContext<'_>, lines: &[pdfcraft_engine::TextBlock], boxes: &[Rect], block: usize, view: &mut DocView) {
++ let PageToolContext { ui, xf, doc, page, .. } = cx;
++ view.pending_text_open = None;
++ let (Some(l), Some(b)) = (lines.get(block), boxes.get(block)) else { return };
++ view.image_selection = None;
++ let source_lines = doc.text_lines(page);
++ let text = l.editor_text.clone();
++ // Screen pixels per point, from the box's width.
++ let scale = (b.width() - 4.0) / ((l.rect[2] - l.rect[0]).max(1.0) as f32);
++ // How far the editor box may grow: a single-line paragraph's rewrite grows to the
++ // page's right edge, a multi-line paragraph rewraps to its own width.
++ let right = xf.rect.right().min(view.viewport_rect().right()) - 6.0;
++ let max_width = if l.rows.len() == 1 { (right - b.left()).max(b.width()) } else { b.width() };
++ view.line_editor = Some(LineEditor {
++ selection: None,
++ font_resource: None,
++ page,
++ block,
++ text: text.clone(),
++ original: text,
++ rect: *b,
++ source_rect: l.rect.map(|v| v as f32),
++ multiline: l.rows.len() > 1,
++ source: Some((l.clone(), source_lines)),
++ fonts: install_source_fonts(ui.ctx(), doc, page),
++ overflow: false,
++ max_width,
++ size: (l.size as f32 * scale).clamp(8.0, 72.0),
++ focus: true,
++ had_focus: false,
++ look: look_of(l),
++ look0: look_of(l),
++ family_chosen: false,
++ extras: Extras::default(),
++ extras0: Extras::default(),
++ typing_from: None,
++ });
++}
++
++/// Lines and their screen boxes for a page; hover outlines, click opens the editor. Returns
++/// `true` when the pointer was used.
++pub(crate) fn page_input(cx: PageToolContext<'_>, lines: &[pdfcraft_engine::TextBlock], view: &mut DocView, allow_interaction: bool) -> bool {
++ let PageToolContext { ui, resp, xf, ptr, page, doc, .. } = cx;
++ let info = &doc.info;
++ if view.block_drag.is_some_and(|d| d.generation != doc.edit_generation()) {
++ view.block_drag = None;
++ return true;
++ }
++ let boxes: Vec<Rect> = lines.iter().map(|l| paragraph_box(xf, info, page, l.rect)).collect();
++ // Outlines stay on their page, like its text (#3061).
++ let painter = ui.painter().with_clip_rect(ui.clip_rect().intersect(xf.rect.expand(1.0)));
++ let active = view.line_editor.as_ref().filter(|e| e.page == page).map(|e| e.block);
++ for (i, b) in boxes.iter().enumerate() {
++ if active != Some(i) {
++ painter.rect_stroke(*b, CornerRadius::same(2), Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
++ }
++ }
++ // A box a switching click asked for opens now, once the edit queued with that click has
++ // applied and `lines`/`boxes` are the document's new inventory (#2022).
++ if allow_interaction
++ && active.is_none()
++ && let Some((open_page, key)) = view.pending_text_open.clone()
++ && open_page == page
++ {
++ // The edit may have regrouped the page: open the paragraph that was clicked, wherever it
++ // is now, and none if it was merged into another (#2516).
++ match key.find(lines, 0) {
++ Some(block) => open_editor(cx, lines, &boxes, block, view),
++ None => view.pending_text_open = None,
++ }
++ }
++ if !allow_interaction {
++ return false;
++ }
++ let upright = has_edges(info, page);
++ let draw_handles = |b: Rect| {
++ for h in width_handles(b) {
++ painter.rect(h, CornerRadius::same(2), Color32::WHITE, Stroke::new(1.0, ACCENT), egui::StrokeKind::Middle);
++ }
++ };
++ // A drag in progress: the box follows the pointer (or the grabbed edge does); releasing applies it.
++ if let Some(d) = view.block_drag.filter(|d| d.page == page)
++ && let (Some(b), Some(l)) = (boxes.get(d.block).copied(), lines.get(d.block))
++ {
++ let p = ptr.pos.unwrap_or(d.start);
++ let dx = p.x - d.start.x;
++ let preview = match d.grip {
++ Grip::Move => b.translate(p - d.start),
++ Grip::Left => Rect::from_min_max(Pos2::new((b.left() + dx).min(b.right() - 12.0), b.min.y), b.max),
++ Grip::Right => Rect::from_min_max(b.min, Pos2::new((b.right() + dx).max(b.left() + 12.0), b.max.y)),
++ };
++ painter.rect_stroke(preview, CornerRadius::same(2), Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
++ let resizing = d.grip != Grip::Move;
++ if resizing {
++ draw_handles(preview);
++ }
++ view.cursor.claim(Layer::Drag, if resizing { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::Move });
++ if resp.drag_stopped() || !ptr.any_down {
++ view.block_drag = None;
++ let (from, to) = (user_box(xf, info, page, b), user_box(xf, info, page, preview));
++ let edit = if resizing {
++ // The new width in user space: the box's change, added to the paragraph's own. A
++ // moved left side (in user space; on an upside-down page that's the screen's right
++ // edge) moves the paragraph with it.
++ let width = (l.rect[2] - l.rect[0]) + (to[2] - to[0]) - (from[2] - from[0]);
++ let shift = to[0] - from[0];
++ let style = pdfcraft_engine::BlockStyle { width: Some(width), offset: (shift != 0.0).then_some([shift, 0.0]), ..Default::default() };
++ // `editor_text` has a newline for every visual row, so a wrap would come back as a
++ // hard break and a wider box would not reflow it. `reflow_text` rejoins a wrap
++ // and keeps a newline that was typed.
++ Edit::EditTextBlock { page, block: d.block, text: l.reflow_text.clone(), style }
++ } else {
++ // Translation must keep the selected source operators. Rewriting unchanged text
++ // would also remove unrelated paragraphs that now overlap this one (#594).
++ Edit::MoveObjects {
++ page,
++ objects: vec![ObjectTarget { kind: ObjectKind::Text, index: d.block }],
++ offset: [to[0] - from[0], to[1] - from[1]],
++ }
++ };
++ if preview != b {
++ view.pending_edit = Some(edit);
++ }
++ }
++ return true;
++ }
++ // Dragging a box (not while a paragraph is open for typing) moves it; from an edge, resizes it.
++ // What it grabs is what the press was on ([`hit`]), not what is under the pointer now: egui
++ // only calls it a drag once the pointer has moved a few pixels (or been held a moment), and a
++ // quick flick has left the edge by then.
++ let owner = match view.router.owner(page) {
++ Some(Owner::Paragraph { block, part }) => Some((block, part)),
++ _ => None,
++ };
++ if resp.drag_started()
++ && active.is_none()
++ && let Some(o) = ptr.origin
++ && let Some((block, part)) = owner
++ {
++ let grip = match part {
++ ParagraphPart::Body => Grip::Move,
++ ParagraphPart::Left => Grip::Left,
++ ParagraphPart::Right => Grip::Right,
++ };
++ view.block_drag = Some(BlockDrag { page, block, generation: doc.edit_generation(), start: o, grip });
++ return true;
++ }
++ let Some(p) = ptr.within(xf.rect) else { return false };
++ // The paragraph under the pointer: outlined, with its width handles when closed.
++ let Some((hovered, _)) = grab_at(&boxes, p, upright) else { return false };
++ if active != Some(hovered) {
++ painter.rect_stroke(boxes[hovered], CornerRadius::same(2), Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
++ }
++ if upright && active.is_none() {
++ draw_handles(boxes[hovered]);
++ }
++ // A click opens the paragraph the press was on.
++ let Some((hit, _)) = owner else { return true };
++ if resp.clicked() {
++ view.image_selection = None;
++ // A click that moves the editor to another paragraph applies the open one first, so its
++ // typed text is not dropped with the switched-away editor (#2022). A click on the open
++ // box's own frame, which the editor's overlay does not cover, keeps it open instead.
++ if let Some(open) = view.line_editor.as_ref().filter(|e| e.has_changes()) {
++ if open.page == page && open.block == hit {
++ return true;
++ }
++ if let Some(old) = view.line_editor.take() {
++ let style = old.style();
++ if old.text != old.original || style != pdfcraft_engine::BlockStyle::default() {
++ view.applied_editor = Some(old.clone());
++ view.pending_edit = Some(Edit::EditTextBlock { page: old.page, block: old.block, text: old.text, style });
++ }
++ }
++ // The clicked box opens next frame, from the document with that edit applied.
++ view.pending_text_open = lines.get(hit).map(|l| (page, pdfcraft_engine::BlockKey::of(l)));
++ return true;
++ }
++ open_editor(cx, lines, &boxes, hit, view);
++ }
++ true
++}
++
++/// The inline editor; returns the edit once the text is applied.
++pub(crate) fn overlay(ctx: &egui::Context, view: &mut DocView, info: &DocInfo) -> Option<Edit> {
++ let view_id = view.id;
++ // Clicks outside the document (the Format text panel) keep the paragraph open, as does the
++ // keyboard moving there. Where the click landed is `interact_pos`: `latest_pos` is gone on the
++ // frame a tap lifts.
++ let away = |p: egui::Pos2| !view.viewport_rect().contains(p);
++ let outside = ctx.input(|i| i.pointer.interact_pos()).is_some_and(away) || focus_pos(ctx).is_some_and(away);
++ let page = view.line_editor.as_ref()?.page;
++ let xf = view.page_xform(page)?;
++ let hold = view.objects.hold_editor;
++ // An OCR word is invisible text over a scan: the textless background still shows the scanned
++ // word, so the editor covers it, as the applied edit will.
++ let background_ready = view
++ .line_editor
++ .as_ref()
++ .is_some_and(|ed| view.text_background.ready(ed.page, ed.block) && !ed.source.as_ref().is_some_and(|(b, _)| b.invisible));
++ // The measured extent of this paragraph's draft: a click straight from another paragraph
++ // opens this one after the frame's measuring, which was the other one's.
++ let draft_rect = view
++ .line_editor
++ .as_ref()
++ .and_then(|ed| view.text_background.draft_rect_for(ed.page, ed.block))
++ .map(|r| xf.user_rect(info, page, r.map(|v| v as f32)));
++ // The editor draws on its page only, as the page itself does: a paragraph running past the
++ // page's edge (a converter relying on the page clip) must not cover the next page or the
++ // tool bars (#3061).
++ let viewport = view.viewport_rect();
++ let page_clip = xf.rect.intersect(viewport);
++ let ed = view.line_editor.as_mut()?;
++ if hold {
++ ed.focus = true;
++ }
++ // Reproject the source box every frame. The page may have been zoomed, scrolled or rotated
++ // while the format panel was open.
++ // A font definition becomes active at the start of the next egui pass. Keep the source
++ // raster visible until then instead of flashing an unrelated interface face.
++ if !ctx.fonts(|f| ed.fonts.values().all(|name| f.definitions().families.contains_key(&FontFamily::Name(name.clone().into())))) {
++ ctx.request_repaint();
++ return None;
++ }
++ ed.rect = xf.user_rect(info, ed.page, ed.source_rect);
++ ed.max_width = ed.rect.width();
++ let scale = (ed.rect.width() / (ed.source_rect[2] - ed.source_rect[0]).abs().max(0.01)).max(0.01);
++ ed.size = (ed.look.size as f32 * scale).clamp(1.0, 300.0);
++ // egui draws the caret at least one row of this font tall. Each row already has its source
++ // row's height, so the font stays below every row (half the region's smallest size): on a
++ // 12 pt row under a 24 pt title the caret is as tall as the row, not the title.
++ let smallest =
++ ed.source.as_ref().and_then(|(block, lines)| block.lines.iter().filter_map(|i| lines.get(*i)).map(|l| l.size as f32).reduce(f32::min));
++ let font = editor_font(&ed.look, smallest.map_or(ed.size, |s| (s * scale * 0.5).clamp(1.0, 300.0).min(ed.size)));
++ let text_color = color32(ed.look.color);
++ let block = ed.block;
++ let mut done = None;
++ let mut reveal = false;
++ let mut look = None;
++ // No fade-in: while it faded, the page's own copy of the text showed through the cover.
++ // Over the page, so only where the document shows: a paragraph scrolled under the mode bar or
++ // a side panel must leave them their pointer (#2755). It stays at its paragraph, cut off as
++ // the page is, rather than pushed back into the window.
++ egui::Area::new(egui::Id::unique("edit-text-line"))
++ .order(egui::Order::Foreground)
++ .fade_in(false)
++ .fixed_pos(Pos2::new(ed.rect.left(), ed.rect.top()))
++ .constrain_to(viewport)
++ .constrain(false)
++ .show(ctx, |ui| {
++ ui.set_clip_rect(ui.clip_rect().intersect(page_clip));
++ // The editor opens at the source size. Once text changes, show the real draft
++ // extent: single rows grow toward the page edge, paragraphs rewrap at their
++ // source width, and the visible frame grows/shrinks vertically with their rows.
++ // An OCR phrase (several stretched words on one scanned line) also grows rightwards.
++ let single_run = ed.source.as_ref().is_some_and(|(block, _)| block.rows.len() == 1 || block.invisible);
++ let width = if !ed.multiline && single_run && !ed.text.contains('\n') && ed.has_changes() {
++ let job = editor_job(ui, ed, &ed.text, scale, f32::INFINITY);
++ let natural = ui.fonts_mut(|f| f.layout_job(job)).size().x;
++ natural.max(ed.size * 0.4).min((xf.rect.right() - ed.rect.left() - 36.0 * scale).max(ed.rect.width()))
++ } else {
++ ed.rect.width().max(1.0)
++ };
++ ui.set_width(width);
++ let measured = editor_galley(ui, ed, &ed.text, scale, width);
++ ed.overflow = ed.has_changes() && (measured.rect.width() > ed.rect.width() + 1.5 || measured.rect.height() > ed.rect.height() + 1.5);
++ let visible = Rect::from_min_size(ed.rect.min, measured.rect.size().max(egui::vec2(1.0, 1.0)));
++ // Unsupported source programs use an interface fallback whose glyph advances
++ // can differ from the PDF's. Keep every visible row inside the growing frame.
++ let live = draft_rect.map_or(visible, |bounds| bounds.union(visible));
++ ui.painter().rect_stroke(live.expand(2.0), CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
++ // On the frame, a press takes hold of the box (a click selects it, a drag moves it):
++ // the pointer says so there, and reads as text inside.
++ if let Some(p) = ui.input(|i| i.pointer.hover_pos())
++ && ui.clip_rect().contains(p)
++ && live.expand(2.0 + FRAME_REACH).contains(p)
++ && !live.expand(2.0).contains(p)
++ {
++ ui.ctx().set_cursor_icon(egui::CursorIcon::Move);
++ }
++ // White, unless the text is too pale to read on white (white text from a coloured
++ // banner, #913): then a dark box, with a caret that shows on it.
++ let fill = if background_ready { Color32::TRANSPARENT } else { editor_fill(text_color) };
++ // An unchanged paragraph is the page's own text: the editor draws only its frame and
++ // caret over it. Once it changes, the textless page goes under the editor's text or,
++ // until that background is ready, a cover over the entire source box, including lines
++ // the draft no longer occupies. Shrinking the text must never expose a copy. Both are
++ // decided after this frame's typing, in places kept under the editor's text.
++ let cover = ui.painter().add(egui::Shape::Noop);
++ let unchanged = !ed.has_changes();
++ egui::Frame::NONE.show(ui, |ui| {
++ // A textless PDF preview, or the page itself, keeps its real background through
++ // the transparent editor. The temporary opaque cover uses the shared caret-contrast rule.
++ ui.visuals_mut().text_cursor.stroke.color = if background_ready || unchanged {
++ if luminance(text_color) < 0.5 { Color32::BLACK } else { Color32::WHITE }
++ } else {
++ caret_color_on(fill)
++ };
++ let rows = 1;
++ let snapshot = ed.clone();
++ let mut layouter = |ui: &egui::Ui, text: &dyn egui::TextBuffer, wrap_width: f32| {
++ let galley = editor_galley(ui, &snapshot, text.as_str(), scale, wrap_width);
++ if snapshot.changes_with(text.as_str()) { galley } else { unseen(galley) }
++ };
++ let before = ed.text.clone();
++ let input = input_id();
++ ed.claim_typing(ui.ctx(), view_id);
++ let output = egui::TextEdit::multiline(&mut ed.text)
++ .id(input)
++ .font(font.clone())
++ .margin(egui::Margin::ZERO)
++ .layouter(&mut layouter)
++ .text_color(text_color)
++ .frame(egui::Frame::NONE)
++ .desired_width(width)
++ .desired_rows(rows)
++ .show(ui);
++ // The editor paints over the page with no visible label; name it for assistive tech.
++ output.response.response.clone().accessible_name(tl!("Text editing line"));
++ if ed.has_changes() {
++ if background_ready {
++ reveal = true;
++ } else {
++ ui.painter().set(cover, egui::Shape::rect_filled(ed.rect.expand(1.0).union(output.response.rect), CornerRadius::ZERO, fill));
++ }
++ }
++ look = Some((Arc::clone(&output.galley), output.galley_pos));
++ if let Some(cursor) = output.cursor_range {
++ let selected = cursor.as_sorted_char_range();
++ let byte = |index| ed.text.char_indices().nth(index).map_or(ed.text.len(), |(at, _)| at);
++ ed.selection = (!selected.is_empty()).then(|| byte(selected.start.into())..byte(selected.end.into()));
++ }
++ let r = output.response;
++ if r.changed() {
++ ed.follow_typed_rows(ctx, &before);
++ // The next pass measures the updated draft with the Apply/Save rewrite.
++ ctx.request_repaint();
++ }
++ if ed.focus {
++ r.request_focus();
++ if let Some(range) = &ed.selection {
++ reselect(ui.ctx(), input, &ed.text, range);
++ }
++ ed.focus = false;
++ }
++ let lost = lost_focus_once(&r, &mut ed.had_focus);
++ let esc = ui.input(|i| i.key_pressed(egui::Key::Escape));
++ let apply = ui.input(|i| i.key_pressed(egui::Key::Enter) && i.modifiers.command);
++ if esc {
++ done = Some(false);
++ } else if apply || (lost && !outside && !hold) {
++ done = Some(true);
++ }
++ });
++ });
++ if reveal {
++ view.text_background.reveal();
++ }
++ if let Some((galley, pos)) = look {
++ view.text_background.remember(&xf, page, block, galley, pos, text_color);
++ }
++ match done {
++ Some(apply) => {
++ let ed = view.line_editor.take()?;
++ if !apply {
++ // Esc leaves the text but not the paragraph: its box is selected, as in Acrobat,
++ // so Delete removes it without aiming for the frame. The draft is discarded.
++ view.objects.select_paragraph_next(ed.page, ed.block);
++ return None;
++ }
++ view.applied_editor = Some(ed.clone());
++ finished_edit(ed)
++ }
++ None => None,
++ }
++}
++
++/// The edit a closed paragraph editor applies, or `None` when nothing in it changed.
++pub(crate) fn finished_edit(ed: LineEditor) -> Option<Edit> {
++ let style = ed.style();
++ (ed.text != ed.original || style != pdfcraft_engine::BlockStyle::default()).then_some(Edit::EditTextBlock {
++ page: ed.page,
++ block: ed.block,
++ text: ed.text,
++ style,
++ })
++}
++
++/// Where the keyboard is: the centre of the widget that has it. A text box that loses it to a
++/// widget reached by Tab or a screen reader (the Format text panel's controls) judges where it
++/// went by this, as it judges a click by where the click landed.
++pub(crate) fn focus_pos(ctx: &egui::Context) -> Option<egui::Pos2> {
++ ctx.memory(|m| m.focused()).and_then(|id| ctx.read_response(id)).map(|r| r.rect.center())
++}
++
++/// Whether an editor's text box `r` lost the keyboard this frame, given whether it `had` it at
++/// the end of the editor's last frame (updated here). egui's `lost_focus` holds for a second
++/// frame as well, and an editor that judges a click away by where the click landed must judge it
++/// once, on the click's frame: on the next, a tap's position is gone (egui-winit sends
++/// `PointerGone` with every touch release) and a mouse may have moved on.
++pub(crate) fn lost_focus_once(r: &egui::Response, had: &mut bool) -> bool {
++ let has = r.has_focus();
++ std::mem::replace(had, has) && !has
++}
++
++#[cfg(test)]
++mod tests {
++ use super::*;
++
++ /// Formatting applied restarts the rows' alignments by text with the box's history: a Redo
++ /// past the format can't bring back an alignment from before it (FredApps on bfc6f8b1).
++ #[test]
++ fn formatting_applied_restarts_the_alignments_by_text() {
++ use pdfcraft_engine::{DocId, TextAlign::Right};
++ let (ctx, id) = (egui::Context::default(), egui::Id::unique("box"));
++ let mut from = None;
++ forget_typing_before(&ctx, id, DocId(1), &mut from, "A\nB");
++ let mut aligns = vec![None, Some(Right)];
++ follow_typing(&ctx, id, "A\nB", "A", &mut [&mut aligns]);
++ // Formatting applied: the page now has "A", and the box's record of its text is reset.
++ from = None;
++ forget_typing_before(&ctx, id, DocId(1), &mut from, "A");
++ follow_typing(&ctx, id, "A", "A\nB", &mut [&mut aligns]);
++ assert_eq!(aligns, pdfcraft_engine::paragraph_align::remap("A", "A\nB", &[None]), "the row before the format doesn't come back aligned");
++ }
++
++ /// The rows' alignments by text go aside with the box's history when another tab's box takes
++ /// the text box, and come back with it: Undo after the trip still restores a deleted row's
++ /// alignment, while the other tab's box starts with none.
++ #[test]
++ fn alignments_by_text_survive_a_trip_to_another_tab() {
++ use pdfcraft_engine::{DocId, TextAlign::Right};
++ let (ctx, id) = (egui::Context::default(), egui::Id::unique("box"));
++ let (mut a_from, mut b_from) = (None, None);
++ forget_typing_before(&ctx, id, DocId(1), &mut a_from, "A\nB");
++ let mut aligns = vec![None, Some(Right)];
++ follow_typing(&ctx, id, "A\nB", "A", &mut [&mut aligns]);
++ forget_typing_before(&ctx, id, DocId(2), &mut b_from, "A\nB");
++ let mut other = vec![None, None];
++ follow_typing(&ctx, id, "A\nB", "A", &mut [&mut other]);
++ follow_typing(&ctx, id, "A", "A\nB", &mut [&mut other]);
++ assert_eq!(other, [None, None], "tab B's box gets back its own alignments, not tab A's");
++ forget_typing_before(&ctx, id, DocId(1), &mut a_from, "A\nB");
++ follow_typing(&ctx, id, "A", "A\nB", &mut [&mut aligns]);
++ assert_eq!(aligns, [None, Some(Right)], "back in tab A, Undo brings the row back right-aligned");
++ }
++
++ /// Undo back to a text the box showed brings its rows' alignments back, unless an alignment
++ /// was picked since: then the older alignments are stale, and the edit is followed instead, so
++ /// Undo never takes back the pick.
++ #[test]
++ fn typing_history_restores_alignments_unless_one_was_picked_since() {
++ use pdfcraft_engine::TextAlign::{Center, Right};
++ let (ctx, id) = (egui::Context::default(), egui::Id::unique("box"));
++ let mut aligns = vec![None, Some(Center), Some(Right)];
++ follow_typing(
++ &ctx,
++ id,
++ "A
++B
++C",
++ "A
++C",
++ &mut [&mut aligns],
++ );
++ assert_eq!(aligns, [None, Some(Right)], "the deleted row's entry goes");
++ follow_typing(
++ &ctx,
++ id,
++ "A
++C",
++ "A
++B
++C",
++ &mut [&mut aligns],
++ );
++ assert_eq!(aligns, [None, Some(Center), Some(Right)], "Undo brings it back");
++ follow_typing(
++ &ctx,
++ id,
++ "A
++B
++C",
++ "A
++C",
++ &mut [&mut aligns],
++ );
++ // An alignment picked for the first row, with no typing.
++ aligns = vec![Some(Right), Some(Right)];
++ let picked = aligns.clone();
++ follow_typing(
++ &ctx,
++ id,
++ "A
++C",
++ "A
++B
++C",
++ &mut [&mut aligns],
++ );
++ assert_eq!(
++ aligns,
++ pdfcraft_engine::paragraph_align::remap(
++ "A
++C", "A
++B
++C", &picked
++ ),
++ "the pick stays"
++ );
++ }
++
++ #[test]
++ fn the_rotate_knob_moves_below_when_there_is_no_room_above() {
++ let view = Rect::from_min_max(Pos2::new(0.0, 100.0), Pos2::new(500.0, 500.0));
++ let roomy = Rect::from_min_max(Pos2::new(100.0, 200.0), Pos2::new(200.0, 250.0));
++ assert_eq!(rotate_knob(roomy, view).0, Pos2::new(150.0, 178.0));
++ let at_top = Rect::from_min_max(Pos2::new(100.0, 100.0), Pos2::new(200.0, 150.0));
++ let (knob, stem) = rotate_knob(at_top, view);
++ assert_eq!((knob, stem), (Pos2::new(150.0, 172.0), Pos2::new(150.0, 150.0)));
++ }
++
++ #[test]
++ fn the_rotate_knob_measures_clockwise_degrees_from_straight_up() {
++ let c = Pos2::new(100.0, 100.0);
++ let at = |x: f32, y: f32| turn_degrees(c, Pos2::new(x, y), false);
++ assert!(at(100.0, 0.0).abs() < 1e-3);
++ assert!((at(200.0, 100.0) - 90.0).abs() < 1e-3, "right is a clockwise quarter");
++ assert!((at(0.0, 100.0) + 90.0).abs() < 1e-3);
++ assert!((at(100.0, 200.0).abs() - 180.0).abs() < 1e-3);
++ // Shift rounds to 15 degrees; the centre and non-finite points turn nothing.
++ assert_eq!(turn_degrees(c, Pos2::new(180.0, 50.0), true) % 15.0, 0.0);
++ assert_eq!(at(101.0, 101.0), 0.0);
++ assert_eq!(at(f32::NAN, 5.0), 0.0);
++ }
++
++ /// An app with one text document open, and an editor on its first paragraph.
++ fn preview_app() -> (crate::PdfCraftApp, LineEditor) {
++ let mut app = crate::PdfCraftApp::new();
++ let bytes = app.session.create_from_text("Preview", "Preview text").unwrap();
++ app.open_bytes("preview.pdf", None, bytes.as_ref().clone()).unwrap();
++ let doc = app.session.get(app.views[0].id).unwrap();
++ let block = doc.text_blocks(0).remove(0);
++ let ed = LineEditor {
++ selection: None,
++ font_resource: None,
++ page: 0,
++ block: 0,
++ text: block.text.clone(),
++ original: block.text.clone(),
++ rect: Rect::NOTHING,
++ source_rect: block.rect.map(|v| v as f32),
++ multiline: block.rows.len() > 1,
++ source: None,
++ fonts: BTreeMap::new(),
++ overflow: false,
++ max_width: 400.0,
++ size: block.size as f32,
++ focus: true,
++ had_focus: false,
++ look: look_of(&block),
++ look0: look_of(&block),
++ family_chosen: false,
++ extras: Extras::default(),
++ extras0: Extras::default(),
++ typing_from: None,
++ };
++ (app, ed)
++ }
++
++ #[test]
++ fn picking_the_family_the_font_name_reads_as_still_changes_the_font() {
++ // A Calibri paragraph reads as Helvetica; picking Helvetica from the list used to leave
++ // `look` equal to what the paragraph had, so no family change was sent and Calibri stayed.
++ let (_app, mut ed) = preview_app();
++ let family = ed.look0.family;
++ assert_eq!(ed.style().family, None, "nothing chosen yet");
++ ed.choose_family(family);
++ assert_eq!(ed.style().family, Some((family, ed.look.bold, ed.look.italic)));
++ assert!(ed.has_changes());
++ assert_eq!(ed.font_shown(&[]), family.label());
++ ed.keep_source_font();
++ assert_eq!(ed.style().family, None, "Keep source fonts withdraws the choice");
++ ed.choose_family(family);
++ ed.applied();
++ assert_eq!(ed.style().family, None, "an applied choice is the paragraph's font now");
++ }
++
++ #[test]
++ fn a_loaded_font_is_applied_to_the_paragraph_being_edited() {
++ // Loading a font used to only register it on the page: the paragraph kept its source
++ // face until the font was found again in the document-font menu (#2591).
++ let (mut app, ed) = preview_app();
++ app.views[0].line_editor = Some(ed);
++ let inter = include_bytes!("../../../../assets/fonts/Inter-Regular.ttf").to_vec();
++ app.use_files(crate::files::FilePurpose::TextFont { page: 0 }, vec![("Inter-Regular.ttf".into(), inter)]);
++ let doc = app.session.get(app.views[0].id).unwrap();
++ let lines = doc.text_lines(0);
++ assert_eq!(lines.iter().map(|l| l.text.as_str()).collect::<String>(), "Preview text");
++ assert!(lines.iter().all(|l| l.base_font == "Inter-Regular"), "{:?}", lines.iter().map(|l| &l.base_font).collect::<Vec<_>>());
++ assert!(app.views[0].line_editor.is_some(), "the paragraph stays open for editing");
++ }
++
++ #[test]
++ fn save_applies_the_paragraph_open_in_the_editor() {
++ // Save wrote the file without the paragraph still open in the editor (#3496).
++ let (mut app, mut ed) = preview_app();
++ ed.text = "Text preview".into();
++ app.views[0].line_editor = Some(ed);
++ let dir = std::env::temp_dir().join(format!("pdfcraft-ui-open-paragraph-{}", std::process::id()));
++ let _ = std::fs::remove_dir_all(&dir);
++ std::fs::create_dir_all(&dir).unwrap();
++ app.save_override = Some(dir.join("out.pdf").to_string_lossy().into_owned());
++
++ assert!(app.save_active(crate::SaveTarget::InPlace));
++ assert!(app.views[0].line_editor.is_none());
++ let doc = app.session.get(app.views[0].id).unwrap();
++ assert_eq!(doc.text_lines(0).iter().map(|l| l.text.as_str()).collect::<String>(), "Text preview");
++ let _ = std::fs::remove_dir_all(&dir);
++ }
++
++ #[test]
++ fn a_refused_text_box_does_not_lose_the_open_paragraph() {
++ let (mut app, mut ed) = preview_app();
++ ed.text = "Text preview".into();
++ app.views[0].line_editor = Some(ed);
++ let draft = crate::content_ui::TextDraft {
++ page: 9,
++ index: None,
++ rect: [20.0, 20.0, 120.0, 40.0],
++ text: "Typed text".into(),
++ style: crate::content_ui::default_style(),
++ focus: true,
++ selection: None,
++ typing_from: None,
++ };
++ app.views[0].content.draft = Some(draft.clone());
++
++ assert!(!app.commit_typing_in(app.views[0].id));
++ assert_eq!(app.views[0].content.draft, Some(draft), "the refused text box stays open");
++ assert!(app.views[0].line_editor.is_none(), "the paragraph was applied");
++ let doc = app.session.get(app.views[0].id).unwrap();
++ assert_eq!(doc.text_lines(0).iter().map(|l| l.text.as_str()).collect::<String>(), "Text preview");
++ }
++
++ #[test]
++ fn a_refused_paragraph_stays_open_with_its_text() {
++ let (mut app, mut ed) = preview_app();
++ ed.text = "Text preview".into();
++ ed.page = 9;
++ app.views[0].line_editor = Some(ed.clone());
++
++ assert!(!app.commit_typing_in(app.views[0].id));
++ assert_eq!(app.views[0].line_editor, Some(ed), "the paragraph keeps its unsaved text");
++ }
++
++ #[test]
++ fn text_background_is_cached_without_editing_and_released_on_cancel() {
++ let (app, ed) = preview_app();
++ let doc = app.session.get(app.views[0].id).unwrap();
++ let source = doc.bytes.clone();
++ let generation = doc.edit_generation();
++ let ctx = egui::Context::default();
++ let mut background = TextBackground::default();
++ for _ in 0..100 {
++ background.prepare(&ctx, doc, Some(&ed), None, 1.0);
++ if background.ready(0, 0) {
++ break;
++ }
++ std::thread::sleep(std::time::Duration::from_millis(5));
++ }
++ assert!(background.ready(0, 0));
++ assert!(!background.pending());
++ let texture = background.texture.as_ref().unwrap().id();
++ background.prepare(&ctx, doc, Some(&ed), None, 1.0);
++ assert_eq!(background.texture.as_ref().unwrap().id(), texture);
++ assert_eq!(doc.bytes, source);
++ assert_eq!(doc.edit_generation(), generation);
++ assert!(!doc.dirty && doc.can_undo().is_none());
++ background.prepare(&ctx, doc, None, None, 1.0);
++ assert!(background.texture.is_none() && background.renderer.is_none());
++ }
++
++ #[test]
++ fn the_editing_background_stays_sharp_on_a_zoomed_page() {
++ // While a paragraph is open, the page around it is drawn from this one raster: it is
++ // asked for at the tiling threshold, not the 2048-pixel backdrop side, which went soft
++ // beyond about 1.2x on a Retina display.
++ let (app, ed) = preview_app();
++ let doc = app.session.get(app.views[0].id).unwrap();
++ let page = &doc.info.pages[0];
++ let ctx = egui::Context::default();
++ ctx.begin_pass(egui::RawInput { max_texture_side: Some(16384), ..Default::default() });
++ let mut background = TextBackground::default();
++ background.prepare(&ctx, doc, Some(&ed), None, 8.0);
++ let expected = 8.0f32.min(crate::canvas::TILE_THRESHOLD / page.width.max(page.height));
++ assert!(expected > crate::canvas::BASE_SIDE / page.width.max(page.height), "the test page needs the larger side");
++ assert_eq!(background.tag, Some((expected * 1000.0) as u64));
++ // A GPU that can't hold such a texture caps it.
++ ctx.end_pass().textures_delta.clear();
++ ctx.begin_pass(egui::RawInput { max_texture_side: Some(2048), ..Default::default() });
++ let mut small = TextBackground::default();
++ small.prepare(&ctx, doc, Some(&ed), None, 8.0);
++ assert_eq!(small.tag, Some(((2048.0 / page.width.max(page.height)) * 1000.0) as u64));
++ ctx.end_pass().textures_delta.clear();
++ }
++
++ #[test]
++ fn pale_text_is_edited_on_a_dark_box() {
++ // #913: white text from a coloured banner was typed onto a white box and vanished.
++ for pale in [Color32::WHITE, Color32::from_rgb(0xFF, 0xFF, 0x00), Color32::from_gray(0xE0), Color32::from_rgb(0xFF, 0xA5, 0x00)] {
++ assert_eq!(editor_fill(pale), DARK_EDITOR_FILL, "{pale:?}");
++ assert!(contrast(pale, editor_fill(pale)) >= 4.5, "{pale:?} is readable");
++ }
++ // Ordinary dark or saturated text keeps the white box.
++ for dark in [Color32::BLACK, Color32::from_rgb(0x1F, 0x2A, 0x6B), Color32::from_rgb(0xC0, 0x00, 0x00), Color32::from_gray(0x60)] {
++ assert_eq!(editor_fill(dark), Color32::WHITE, "{dark:?}");
++ }
++ }
++
++ #[test]
++ fn caret_contrasts_with_its_fill() {
++ assert_eq!(caret_color_on(Color32::WHITE), Color32::BLACK);
++ assert_eq!(caret_color_on(Color32::from_rgb(0xFF, 0xFF, 0xF4)), Color32::BLACK);
++ assert_eq!(caret_color_on(Color32::BLACK), Color32::WHITE);
++ assert_eq!(caret_color_on(DARK_EDITOR_FILL), Color32::WHITE);
++ }
++
++ #[test]
++ fn bullet_list_toggles_and_preserves_content() {
++ let mut text = "First line\nSecond line\n\nThird line".to_string();
++ assert!(!is_bulleted(&text));
++ assert!(toggle_bullet_list(&mut text));
++ assert!(is_bulleted(&text));
++ assert_eq!(text, "• First line\n• Second line\n\n• Third line");
++
++ // Toggling again removes bullets
++ assert!(toggle_bullet_list(&mut text));
++ assert!(!is_bulleted(&text));
++ assert_eq!(text, "First line\nSecond line\n\nThird line");
++ }
++
++ #[test]
++ fn numbered_list_toggles_and_preserves_content() {
++ let mut text = "Item A\nItem B\n\nItem C".to_string();
++ assert!(!is_numbered(&text));
++ assert!(toggle_numbered_list(&mut text));
++ assert!(is_numbered(&text));
++ assert_eq!(text, "1. Item A\n2. Item B\n\n3. Item C");
++
++ // Toggling again removes numbering
++ assert!(toggle_numbered_list(&mut text));
++ assert!(!is_numbered(&text));
++ assert_eq!(text, "Item A\nItem B\n\nItem C");
++ }
++
++ #[test]
++ fn superscript_and_subscript_conversion() {
++ let mut ed = LineEditor {
++ page: 0,
++ block: 0,
++ text: "E = mc2".to_string(),
++ original: "E = mc2".to_string(),
++ rect: egui::Rect::ZERO,
++ source_rect: [0.0; 4],
++ max_width: 100.0,
++ overflow: false,
++ selection: Some(6..7), // selecting '2'
++ font_resource: None,
++ fonts: std::collections::BTreeMap::new(),
++ source: None,
++ multiline: false,
++ size: 12.0,
++ family_chosen: false,
++ focus: true,
++ had_focus: false,
++ look: pdfcraft_engine::AddedText::default(),
++ look0: pdfcraft_engine::AddedText::default(),
++ extras: Extras::default(),
++ extras0: Extras::default(),
++ };
++
++ assert!(toggle_superscript(&mut ed));
++ assert_eq!(ed.text, "E = mc²");
++ // Toggling superscript again returns to normal '2'
++ assert!(toggle_superscript(&mut ed));
++ assert_eq!(ed.text, "E = mc2");
++
++ // Test subscript on H2O
++ ed.text = "H2O".to_string();
++ ed.selection = Some(1..2); // selecting '2'
++ assert!(toggle_subscript(&mut ed));
++ assert_eq!(ed.text, "H₂O");
++ assert!(toggle_subscript(&mut ed));
++ assert_eq!(ed.text, "H2O");
++ }
++
++ #[test]
++ fn value_based_line_spacing_in_style() {
++ let ed = LineEditor {
++ page: 0,
++ block: 0,
++ text: "Paragraph text".to_string(),
++ original: "Paragraph text".to_string(),
++ rect: egui::Rect::ZERO,
++ source_rect: [0.0; 4],
++ max_width: 100.0,
++ overflow: false,
++ selection: None,
++ font_resource: None,
++ fonts: std::collections::BTreeMap::new(),
++ source: None,
++ multiline: false,
++ size: 12.0,
++ family_chosen: false,
++ focus: true,
++ had_focus: false,
++ look: pdfcraft_engine::AddedText::default(),
++ look0: pdfcraft_engine::AddedText::default(),
++ extras: Extras { line_spacing: 1.33, underline: true, ..Default::default() },
++ extras0: Extras::default(),
++ };
++ let style = ed.style();
++ assert_eq!(style.line_spacing, Some(1.33));
++ assert_eq!(style.underline, Some(true));
++ }
++}
diff --cc crates/ui-egui/src/tools/edit_text_ui/image.rs
index 219bd55e8,6956e656a..fab048653
--- a/crates/ui-egui/src/tools/edit_text_ui/image.rs
+++ b/crates/ui-egui/src/tools/edit_text_ui/image.rs
@@@ -1,1201 -1,700 +1,1252 @@@
- use super::*;
- use crate::canvas::{ImageObb, PainterExt};
- use crate::image_engine::{CROP_ACCENT, HandleId, apply_crop, apply_resize, apply_resize_proportional, draw_handle, draw_rule_of_thirds};
-
- /// The 8 resize handles on an image (4 corners + 4 edge centers).
- #[derive(Clone, Copy, Debug, PartialEq, Eq)]
- pub enum ImageHandle {
- TopLeft,
- TopCenter,
- TopRight,
- RightCenter,
- BottomRight,
- BottomCenter,
- BottomLeft,
- LeftCenter,
- }
-
- #[derive(Clone, Copy, Debug, PartialEq, Eq)]
- pub enum DragTarget {
- Move,
- Handle(ImageHandle),
- CropHandle(ImageHandle),
- Rotate,
- }
-
- #[derive(Clone, Copy, Debug, PartialEq)]
- pub struct ImageDrag {
- pub start: Pos2,
- pub target: DragTarget,
- pub initial_rect: Rect,
- pub initial_matrix: [f64; 6],
- }
-use super::{ACCENT, Color32, CornerRadius, DocInfo, DocView, Edit, MAX_OUTLINES, PagePointer, PageXform, Pos2, Rect, Stroke, user_box};
-use crate::page_cursor::Layer;
-use crate::page_router::{Hit, Owner, PicturePart};
--
--/// A selected page image, and what the pointer is doing to it.
--#[derive(Clone, Debug, PartialEq)]
--pub struct ImageSelection {
-- pub page: usize,
-- pub index: usize,
- /// Dragging in progress.
- pub drag: Option<ImageDrag>,
- /// Crop mode: the box kept so far.
- /// Dragging: the start point and, for a corner, the opposite corner (screen).
- drag: Option<(Pos2, Option<Pos2>)>,
- /// Crop mode (Acrobat's crop handles): the box kept so far.
-- pub crop: Option<CropMode>,
- pub crop_mode: bool,
- pub crop_rect: Option<Rect>,
-- /// The drag is turning the picture by its rotate handle (the `drag` start is then unused).
- pub turning: bool,
- turning: bool,
-- /// A move or resize applied on release: the frame it had and the one it got, in the page's
-- /// 0..1 coordinates, while the page raster still has the picture where it was.
- pub settled: Option<(Rect, Rect)>,
- settled: Option<(Rect, Rect)>,
--}
--
--impl ImageSelection {
-- /// Cancel an in-progress text, rotation or crop drag and restore the crop it was previewing.
-- pub(crate) fn cancel_pointer_gesture(&mut self) {
-- self.drag = None;
-- self.turning = false;
-- if let Some(crop) = &mut self.crop
-- && let Some((_, _, before)) = crop.drag.take()
-- {
-- crop.keep = before;
-- }
- }
-
- pub(crate) fn dragging(&self) -> bool {
- self.drag.is_some()
-- }
--}
--
--/// Cropping a page image: the handles trim instead of resizing, the part that will go is
--/// dimmed, Enter or Done applies and Esc or Cancel leaves it.
--#[derive(Clone, Debug, PartialEq)]
--pub struct CropMode {
- /// The box to keep (user space or normalized UV).
- /// The box to keep (user space).
-- pub keep: [f64; 4],
- /// A drag in progress: the grip (0–7 handles, 8 inside), where it started and the box then.
- pub(crate) drag: Option<(usize, Pos2, [f64; 4])>,
- /// A drag in progress: the grip (0–3 corners, 4–7 edges, 8 the inside), where it started
- /// and the box then.
- drag: Option<(usize, Pos2, [f64; 4])>,
--}
--
--impl CropMode {
-- pub fn new(keep: [f64; 4]) -> Self {
-- Self { keep, drag: None }
- }
- }
-
- #[derive(Clone, Copy, Debug, PartialEq)]
- pub(crate) struct Matrix(pub(crate) [f64; 6]);
-
- impl Matrix {
- pub(crate) fn apply(&self, x: f64, y: f64) -> (f64, f64) {
- let [a, b, c, d, e, f] = self.0;
- (a * x + c * y + e, b * x + d * y + f)
- }
-
- pub(crate) fn invert(&self) -> Option<Self> {
- let [a, b, c, d, e, f] = self.0;
- let det = a * d - b * c;
- if det.abs() < 1e-12 || !det.is_finite() {
- return None;
- }
- let (ia, ib, ic, id) = (d / det, -b / det, -c / det, a / det);
- Some(Matrix([ia, ib, ic, id, -(e * ia + f * ic), -(e * ib + f * id)]))
- }
-
- pub(crate) fn then(&self, then: &Matrix) -> Matrix {
- let [a, b, c, d, e, f] = self.0;
- let [a2, b2, c2, d2, e2, f2] = then.0;
- Matrix([a * a2 + b * c2, a * b2 + b * d2, c * a2 + d * c2, c * b2 + d * d2, e * a2 + f * c2 + e2, e * b2 + f * d2 + f2])
-- }
- }
-
- pub(crate) fn user_to_screen(xf: &PageXform, info: &DocInfo, page: usize, x: f64, y: f64) -> Pos2 {
- let Some(p) = info.pages.get(page) else { return Pos2::ZERO };
- let v = p.user_to_view(x as f32, y as f32);
- xf.norm_to_screen(v[0] / xf.pw, v[1] / xf.ph)
- }
-
- fn screen_to_user(xf: &PageXform, info: &DocInfo, page: usize, pt: Pos2) -> (f64, f64) {
- let (vx, vy) = xf.screen_to_view(pt);
- let Some(p) = info.pages.get(page) else { return (0.0, 0.0) };
- let u = p.view_to_user(vx, vy);
- (f64::from(u[0]), f64::from(u[1]))
--}
--
- pub(crate) fn quad_contains(quad: [Pos2; 4], p: Pos2) -> bool {
- if !p.x.is_finite() || !p.y.is_finite() || quad.iter().any(|pt| !pt.x.is_finite() || !pt.y.is_finite()) {
- return false;
- }
- let mut sign = 0;
- for i in 0..4 {
- let p1 = quad[i];
- let p2 = quad[(i + 1) % 4];
- let cross = (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);
- if cross.abs() > 1e-4 {
- let s = if cross > 0.0 { 1 } else { -1 };
- if sign == 0 {
- sign = s;
- } else if sign != s {
- return false;
- }
- }
- }
- sign != 0
-/// What the pointer and the page are, for the image tools on one page.
-#[derive(Clone, Copy)]
-pub(crate) struct ImageCx<'a> {
- pub ui: &'a egui::Ui,
- pub resp: &'a egui::Response,
- pub xf: &'a PageXform,
- pub ptr: &'a PagePointer,
- pub page: usize,
- pub info: &'a DocInfo,
- pub allow_interaction: bool,
--}
--
- /// The screen positions of the crop's eight handles: corners, then the top, right, bottom and left edge midpoints.
-/// The screen boxes of the crop's eight handles: corners, then the top, right, bottom and left
-/// edge midpoints.
--fn crop_handles(k: Rect) -> [Pos2; 8] {
-- [k.left_top(), k.right_top(), k.left_bottom(), k.right_bottom(), k.center_top(), k.right_center(), k.center_bottom(), k.left_center()]
--}
--
--/// Crop mode for the selected image `i` whose visible frame is `b` (screen). Returns `true`
--/// (the pointer and keys are the crop's while it lasts).
- #[allow(clippy::too_many_arguments)]
- fn crop_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- view: &mut DocView,
- i: usize,
- image: &pdfcraft_engine::PageImage,
- allow_interaction: bool,
- ) -> bool {
-fn crop_input(cx: &ImageCx<'_>, view: &mut DocView, i: usize, b: Rect) -> bool {
- let ImageCx { ui, resp, xf, ptr, page, info, allow_interaction } = *cx;
-- let painter = ui.painter();
- let Some(sel) = view.image_selection.as_mut() else { return false };
- if let Some(rect) = sel.crop_rect
- && let Some(mode) = sel.crop.as_mut()
- {
- mode.keep = user_box(xf, info, page, rect);
- sel.crop_rect = None;
- }
- let Some(mode) = sel.crop.as_mut() else { return false };
- let Some(mode) = view.image_selection.as_mut().and_then(|s| s.crop.as_mut()) else { return false };
-- let hover_pos = ptr.pos;
- let full_b = xf.user_rect(info, page, image.rect.map(|v| v as f32));
- let b = if full_b.is_positive() { full_b } else { xf.user_rect(info, page, image.visible_rect.unwrap_or(image.rect).map(|v| v as f32)) };
-- let keep_screen = |keep: [f64; 4]| xf.user_rect(info, page, keep.map(|v| v as f32)).intersect(b);
- let k = keep_screen(mode.keep);
-
- // A drag in progress moves a grip; the box stays inside the picture and at least 6 px.
-- if allow_interaction
-- && resp.drag_started()
-- && let Some(o) = ptr.origin
-- {
- let grip = crop_handles(k).iter().position(|h| h.distance(o) < 10.0).or_else(|| k.contains(o).then_some(8));
- let k = keep_screen(mode.keep);
- let grip = crop_handles(k).iter().position(|h| h.distance(o) < 8.0).or_else(|| k.contains(o).then_some(8));
-- if let Some(g) = grip {
-- mode.drag = Some((g, o, mode.keep));
-- }
-- }
-
-- if let Some((grip, start, at_start)) = mode.drag
-- && let Some(p) = hover_pos
-- {
-- let d = p - start;
-- let k0 = keep_screen(at_start);
- if grip < 8 {
- let handle_id = match grip {
- 0 => HandleId::TopLeft,
- 1 => HandleId::TopRight,
- 2 => HandleId::BottomLeft,
- 3 => HandleId::BottomRight,
- 4 => HandleId::TopMid,
- 5 => HandleId::RightMid,
- 6 => HandleId::BottomMid,
- _ => HandleId::LeftMid,
- };
- let uv0 = if b.width() > 0.0 && b.height() > 0.0 {
- Rect::from_min_max(
- pos2(((k0.min.x - b.min.x) / b.width()).clamp(0.0, 1.0), ((k0.min.y - b.min.y) / b.height()).clamp(0.0, 1.0)),
- pos2(((k0.max.x - b.min.x) / b.width()).clamp(0.0, 1.0), ((k0.max.y - b.min.y) / b.height()).clamp(0.0, 1.0)),
- )
- } else {
- Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0))
- };
- let (new_screen, _) = apply_crop(k0, uv0, handle_id, d);
- let (min_w, min_h) = (6.0_f32.min(b.width()), 6.0_f32.min(b.height()));
- let clamped = Rect::from_min_max(
- pos2(new_screen.min.x.clamp(b.min.x, b.max.x - min_w), new_screen.min.y.clamp(b.min.y, b.max.y - min_h)),
- pos2(new_screen.max.x.clamp(b.min.x + min_w, b.max.x), new_screen.max.y.clamp(b.min.y + min_h, b.max.y)),
- );
- mode.keep = user_box(xf, info, page, clamped);
- } else {
- let shift = egui::vec2(d.x.clamp(b.min.x - k0.min.x, b.max.x - k0.max.x), d.y.clamp(b.min.y - k0.min.y, b.max.y - k0.max.y));
- mode.keep = user_box(xf, info, page, Rect::from_min_max(k0.min + shift, k0.max + shift));
- let (mut min, mut max) = (k0.min, k0.max);
- match grip {
- 0 => {
- min.x += d.x;
- min.y += d.y;
- }
- 1 => {
- max.x += d.x;
- min.y += d.y;
- }
- 2 => {
- min.x += d.x;
- max.y += d.y;
- }
- 3 => {
- max.x += d.x;
- max.y += d.y;
- }
- 4 => min.y += d.y,
- 5 => max.x += d.x,
- 6 => max.y += d.y,
- 7 => min.x += d.x,
- _ => {
- let shift = egui::vec2(d.x.clamp(b.min.x - k0.min.x, b.max.x - k0.max.x), d.y.clamp(b.min.y - k0.min.y, b.max.y - k0.max.y));
- min += shift;
- max += shift;
- }
-- }
- let (min_w, min_h) = (6.0_f32.min(b.width()), 6.0_f32.min(b.height()));
- min.x = min.x.clamp(b.min.x, b.max.x - min_w);
- min.y = min.y.clamp(b.min.y, b.max.y - min_h);
- max.x = max.x.clamp(min.x + min_w, b.max.x);
- max.y = max.y.clamp(min.y + min_h, b.max.y);
- mode.keep = user_box(xf, info, page, Rect::from_min_max(min, max));
-- if resp.drag_stopped() || !ptr.primary_down {
-- mode.drag = None;
-- }
-- }
-
- // Faint ghost outline of the full uncropped image underneath
- painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.0, Color32::from_rgba_unmultiplied(200, 200, 200, 80)), egui::StrokeKind::Outside);
-
- // Dim outer excluded bands
- let k = keep_screen(mode.keep);
- // What goes is dimmed; what stays keeps its frame and handles.
-- let dim = Color32::from_black_alpha(96);
- if k.min.y > b.min.y {
- painter.rect_filled(Rect::from_min_max(b.min, Pos2::new(b.max.x, k.min.y)), CornerRadius::ZERO, dim);
- }
- if k.max.y < b.max.y {
- painter.rect_filled(Rect::from_min_max(Pos2::new(b.min.x, k.max.y), b.max), CornerRadius::ZERO, dim);
- }
- if k.min.x > b.min.x {
- painter.rect_filled(Rect::from_min_max(Pos2::new(b.min.x, k.min.y), Pos2::new(k.min.x, k.max.y)), CornerRadius::ZERO, dim);
- }
- if k.max.x < b.max.x {
- painter.rect_filled(Rect::from_min_max(Pos2::new(k.max.x, k.min.y), Pos2::new(b.max.x, k.max.y)), CornerRadius::ZERO, dim);
- for r in [
- Rect::from_min_max(b.min, Pos2::new(b.max.x, k.min.y)),
- Rect::from_min_max(Pos2::new(b.min.x, k.max.y), b.max),
- Rect::from_min_max(Pos2::new(b.min.x, k.min.y), Pos2::new(k.min.x, k.max.y)),
- Rect::from_min_max(Pos2::new(k.max.x, k.min.y), Pos2::new(b.max.x, k.max.y)),
- ] {
- if r.is_positive() {
- painter.rect_filled(r.intersect(xf.rect), CornerRadius::ZERO, dim);
- }
-- }
-
- // Rule-of-Thirds Grid inside crop area
- draw_rule_of_thirds(painter, k, Stroke::new(1.0, CROP_ACCENT.gamma_multiply(0.45)));
-
- // Crop border stroke
- painter.rect_stroke(k, CornerRadius::ZERO, Stroke::new(1.5, CROP_ACCENT), egui::StrokeKind::Outside);
-
- // Crop handles
- for (idx, h) in crop_handles(k).iter().enumerate() {
- let is_active = mode.drag.as_ref().is_some_and(|d| d.0 == idx);
- let is_hovered = hover_pos.is_some_and(|p| p.distance(*h) < 10.0);
- draw_handle(painter, *h, is_active, is_hovered, true, CROP_ACCENT);
- painter.rect_stroke(k, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- for h in crop_handles(k) {
- painter.rect(
- Rect::from_center_size(h, egui::vec2(8.0, 8.0)),
- CornerRadius::ZERO,
- Color32::WHITE,
- Stroke::new(1.0, ACCENT),
- egui::StrokeKind::Middle,
- );
-- }
-
- // Action pills
- // Done and Cancel, painted under the box (above it near the bottom of the view).
-- let font = egui::FontId::proportional(12.5);
- let done_w = painter.layout_no_wrap(tl!("Done").to_string(), font.clone(), Color32::WHITE).size().x + 22.0;
- let cancel_w = painter.layout_no_wrap(tl!("Cancel").to_string(), font.clone(), Color32::WHITE).size().x + 22.0;
- let is_full_rect = (mode.keep[0] - image.rect[0]).abs() < 1e-3
- && (mode.keep[1] - image.rect[1]).abs() < 1e-3
- && (mode.keep[2] - image.rect[2]).abs() < 1e-3
- && (mode.keep[3] - image.rect[3]).abs() < 1e-3;
- let has_crop = !is_full_rect || image.visible_rect.is_some_and(|v| v != image.rect);
- let reset_w =
- if has_crop { Some(painter.layout_no_wrap(tl!("Remove Crop").to_string(), font.clone(), Color32::WHITE).size().x + 22.0) } else { None };
- let spacing = 6.0;
- let total_w = done_w + spacing + cancel_w + reset_w.map_or(0.0, |w| w + spacing);
-
- // Position pills below crop box if there's room, otherwise above
- let y = if k.max.y + 32.0 < xf.rect.max.y {
- k.max.y + 6.0
- } else if k.min.y - 28.0 > xf.rect.min.y {
- k.min.y - 28.0
- } else {
- k.max.y + 6.0
- let pill = |label: &str, right: f32, y: f32| -> Rect {
- let w = painter.layout_no_wrap(label.to_string(), font.clone(), Color32::WHITE).size().x + 20.0;
- Rect::from_min_size(Pos2::new(right - w, y), egui::vec2(w, 22.0))
-- };
-
- // Right-align pills with k.max.x, clamped inside the page bounds
- let mut right = k.max.x.min(xf.rect.max.x - 8.0);
- if right - total_w < xf.rect.min.x + 8.0 {
- right = (xf.rect.min.x + 8.0 + total_w).min(xf.rect.max.x - 8.0);
- }
-
- let done = Rect::from_min_size(Pos2::new(right - done_w, y), egui::vec2(done_w, 22.0));
- let cancel = Rect::from_min_size(Pos2::new(done.min.x - spacing - cancel_w, y), egui::vec2(cancel_w, 22.0));
- let reset = reset_w.map(|w| Rect::from_min_size(Pos2::new(cancel.min.x - spacing - w, y), egui::vec2(w, 22.0)));
-
- let done_id = ui.scope_id().with("crop_pill_done").with(page).with(i);
- let cancel_id = ui.scope_id().with("crop_pill_cancel").with(page).with(i);
- let reset_id = ui.scope_id().with("crop_pill_reset").with(page).with(i);
-
- let done_resp = ui.interact(done, done_id, egui::Sense::click());
- let cancel_resp = ui.interact(cancel, cancel_id, egui::Sense::click());
- let reset_resp = reset.map(|r| ui.interact(r, reset_id, egui::Sense::click()));
-
- let y = if k.max.y + 30.0 < xf.rect.max.y { k.max.y + 6.0 } else { k.min.y - 28.0 };
- let done = pill(tl!("Done"), k.max.x, y);
- let cancel = pill(tl!("Cancel"), done.min.x - 6.0, y);
-- let t = crate::theme::Tokens::get(ui.ctx());
-
- // Done button (accent colored)
- let done_bg = if done_resp.is_pointer_button_down_on() {
- ACCENT.gamma_multiply(0.8)
- } else if done_resp.hovered() {
- Color32::from_rgb(40, 145, 255)
- } else {
- ACCENT
- };
- painter.rect_filled(done, CornerRadius::same(11), done_bg);
- painter.rect_filled(done, CornerRadius::same(11), ACCENT);
-- painter.text(done.center(), egui::Align2::CENTER_CENTER, tl!("Done"), font.clone(), Color32::WHITE);
-
- // Cancel button (panel style with border)
- let cancel_bg = if cancel_resp.is_pointer_button_down_on() {
- t.pressed
- } else if cancel_resp.hovered() {
- t.hover
- } else {
- t.card
- };
- let cancel_stroke = Stroke::new(1.0, if cancel_resp.hovered() { ACCENT } else { t.border });
- let cancel_text = if cancel_resp.hovered() { ACCENT } else { t.text };
- painter.rect(cancel, CornerRadius::same(11), cancel_bg, cancel_stroke, egui::StrokeKind::Inside);
- painter.text(cancel.center(), egui::Align2::CENTER_CENTER, tl!("Cancel"), font.clone(), cancel_text);
-
- // Reset button (panel style with border)
- if let (Some(r), Some(r_resp)) = (reset, reset_resp.as_ref()) {
- let reset_bg = if r_resp.is_pointer_button_down_on() {
- t.pressed
- } else if r_resp.hovered() {
- t.hover
- } else {
- t.card
- };
- let reset_stroke = Stroke::new(1.0, if r_resp.hovered() { ACCENT } else { t.border });
- let reset_text = if r_resp.hovered() { ACCENT } else { t.text };
- painter.rect(r, CornerRadius::same(11), reset_bg, reset_stroke, egui::StrokeKind::Inside);
- painter.text(r.center(), egui::Align2::CENTER_CENTER, tl!("Remove Crop"), font.clone(), reset_text);
- }
-
- if let Some(p) = hover_pos {
- let icon = if done_resp.hovered()
- || cancel_resp.hovered()
- || reset_resp.as_ref().is_some_and(|r| r.hovered())
- || done.contains(p)
- || cancel.contains(p)
- || reset.is_some_and(|r| r.contains(p))
- {
- egui::CursorIcon::PointingHand
- } else if let Some(g) = crop_handles(k).iter().position(|h| h.distance(p) < 8.0) {
- painter.rect(cancel, CornerRadius::same(11), t.panel, Stroke::new(1.0, t.border), egui::StrokeKind::Inside);
- painter.text(cancel.center(), egui::Align2::CENTER_CENTER, tl!("Cancel"), font, t.text);
- // Cropping takes over its page's cursor, and the buttons' wherever they are; other pages
- // keep theirs.
- if let Some(p) = hover_pos.filter(|p| xf.rect.contains(*p) || done.contains(*p) || cancel.contains(*p)) {
- let icon = if let Some(g) = crop_handles(k).iter().position(|h| h.distance(p) < 8.0) {
-- match g {
- 0 => HandleId::TopLeft.cursor_icon(),
- 1 => HandleId::TopRight.cursor_icon(),
- 2 => HandleId::BottomLeft.cursor_icon(),
- 3 => HandleId::BottomRight.cursor_icon(),
- 4 => HandleId::TopMid.cursor_icon(),
- 5 => HandleId::RightMid.cursor_icon(),
- 6 => HandleId::BottomMid.cursor_icon(),
- _ => HandleId::LeftMid.cursor_icon(),
- 0 | 3 => egui::CursorIcon::ResizeNwSe,
- 1 | 2 => egui::CursorIcon::ResizeNeSw,
- 4 | 6 => egui::CursorIcon::ResizeVertical,
- _ => egui::CursorIcon::ResizeHorizontal,
-- }
- } else if done.contains(p) || cancel.contains(p) {
- egui::CursorIcon::PointingHand
-- } else if k.contains(p) {
- if mode.drag.is_some() { egui::CursorIcon::Grabbing } else { egui::CursorIcon::Grab }
- egui::CursorIcon::Move
-- } else {
-- egui::CursorIcon::Default
-- };
- ui.ctx().set_cursor_icon(icon);
- view.cursor.claim(Layer::Editor, icon);
-- }
-
- let clicked = |r: Rect| allow_interaction && resp.clicked() && hover_pos.is_some_and(|p| r.contains(p));
-- let keys = |key: egui::Key| allow_interaction && ui.input(|inp| inp.key_pressed(key)) && !ui.ctx().egui_wants_keyboard_input();
- let done_clicked = allow_interaction && (done_resp.clicked() || (resp.clicked() && hover_pos.is_some_and(|p| done.contains(p))));
- let cancel_clicked = allow_interaction && (cancel_resp.clicked() || (resp.clicked() && hover_pos.is_some_and(|p| cancel.contains(p))));
- let reset_clicked = allow_interaction
- && reset
- .is_some_and(|r| reset_resp.as_ref().is_some_and(|res| res.clicked()) || (resp.clicked() && hover_pos.is_some_and(|p| r.contains(p))));
-
- if reset_clicked {
- mode.keep = image.rect;
- mode.drag = None;
- if let Some(s) = view.image_selection.as_mut() {
- s.crop_rect = None;
- }
- } else if done_clicked || keys(egui::Key::Enter) {
- if clicked(done) || keys(egui::Key::Enter) {
-- let keep = mode.keep;
-- if let Some(s) = view.image_selection.as_mut() {
-- s.crop = None;
- s.crop_mode = false;
- s.crop_rect = None;
-- }
- let is_full = (keep[0] - image.rect[0]).abs() < 1e-3
- && (keep[1] - image.rect[1]).abs() < 1e-3
- && (keep[2] - image.rect[2]).abs() < 1e-3
- && (keep[3] - image.rect[3]).abs() < 1e-3;
- if is_full {
- if image.visible_rect.is_some_and(|v| v != image.rect) {
- view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::RemoveCrop });
- }
- } else if image.visible_rect != Some(keep) {
- if keep_screen(keep) != b {
-- view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Crop(keep) });
-- }
- } else if (cancel_clicked || keys(egui::Key::Escape))
- } else if (clicked(cancel) || keys(egui::Key::Escape))
-- && let Some(s) = view.image_selection.as_mut()
-- {
-- s.crop = None;
- s.crop_mode = false;
- s.crop_rect = None;
-- }
-- true
-}
-
-impl ImageSelection {
- pub(crate) fn dragging(&self) -> bool {
- self.drag.is_some()
- }
--}
--
--/// The page raster's picture of the box `from`, stretched into `to`, over blanks at `blank` (where
--/// the screen may still show the picture).
--fn stretched_picture(painter: &egui::Painter, xf: &PageXform, tex: egui::TextureId, blank: &[Rect], from: Rect, to: Rect) {
-- let clipped = painter.with_clip_rect(painter.clip_rect().intersect(xf.rect));
-- for r in blank {
-- clipped.rect_filled(*r, CornerRadius::ZERO, Color32::WHITE);
-- }
-- let mut mesh = egui::Mesh::with_texture(tex);
-- for (a, b) in [
-- (from.left_top(), to.left_top()),
-- (from.right_top(), to.right_top()),
-- (from.right_bottom(), to.right_bottom()),
-- (from.left_bottom(), to.left_bottom()),
-- ] {
-- let (u, v) = xf.screen_to_norm(a);
-- mesh.vertices.push(egui::epaint::Vertex { pos: b, uv: Pos2::new(u, v), color: Color32::WHITE });
-- }
-- mesh.add_triangle(0, 1, 2);
-- mesh.add_triangle(0, 2, 3);
-- clipped.add(egui::Shape::mesh(mesh));
--}
--
- /// Once a selected corner or the rotate knob is grabbed, text under it cannot take over the gesture. The same
- /// applies after the pointer leaves the image/page while dragging.
- pub(crate) fn image_owns_pointer(_ui: &egui::Ui, xf: &PageXform, ptr: &PagePointer, page: usize, info: &DocInfo, view: &DocView) -> bool {
- let Some(selection) = view.image_selection.as_ref().filter(|s| s.page == page) else { return false };
- if selection.drag.is_some() || selection.crop.is_some() || selection.crop_mode {
- return true;
-/// The selected picture's grips under `p`: its corners and its rotate knob. They are asked after
-/// the paragraphs, so text on top of a picture cannot take them; `clip` is the pages' visible
-/// area, which the knob stays inside.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn handle_hit(
- clip: Rect,
- xf: &PageXform,
- page: usize,
- info: &DocInfo,
- images: &[pdfcraft_engine::PageImage],
- view: &DocView,
- p: Pos2,
-) -> Option<Hit> {
- let s = view.image_selection.as_ref().filter(|s| s.page == page && s.crop.is_none())?;
- let b = images.get(s.index)?.visible_rect.map(|r| xf.user_rect(info, page, r.map(|v| v as f32)))?;
- let picture = |part| Owner::Picture { index: s.index, part };
- if rotate_knob(b, clip).0.distance(p) < 8.0 {
- return Some(Hit::new(picture(PicturePart::Knob), Layer::Handle, egui::CursorIcon::Grab));
-- }
- let Some(image) = view.edit_images.get(&page).and_then(|(_, images)| images.get(selection.index)) else { return false };
- let obb = ImageObb::from_image(image, xf, info, page);
- let p = if ptr.primary_down { ptr.origin.or(ptr.pos) } else { ptr.pos };
- let Some(p) = p else { return false };
- let corner = [b.left_top(), b.right_top(), b.left_bottom(), b.right_bottom()].iter().position(|c| c.distance(p) < 8.0)?;
- let icon = if matches!(corner, 0 | 3) { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw };
- Some(Hit::new(picture(PicturePart::Corner(corner)), Layer::Handle, icon))
-}
--
- obb.handles().iter().any(|(_, h)| h.distance(p) < 8.0) || obb.s_rot.distance(p) < 8.0
-/// The picture under `p` (its shape, not only its box): a click selects it, a drag moves the
-/// selected one. On the text layer, asked before the paragraphs: editable text on top of a
-/// picture takes the press.
-pub(crate) fn body_hit(xf: &PageXform, page: usize, info: &DocInfo, images: &[pdfcraft_engine::PageImage], view: &DocView, p: Pos2) -> Option<Hit> {
- if !xf.rect.contains(p) {
- return None;
- }
- let (x, y) = xf.screen_to_view(p);
- let user = info.pages.get(page)?.view_to_user(x, y).map(f64::from);
- let index = images
- .iter()
- .rposition(|im| im.visible_rect.is_some_and(|r| xf.user_rect(info, page, r.map(|v| v as f32)).contains(p)) && im.contains(user))?;
- let selected = view.image_selection.as_ref().is_some_and(|s| s.page == page && s.index == index);
- let icon = if selected { egui::CursorIcon::Move } else { egui::CursorIcon::PointingHand };
- Some(Hit::new(Owner::Picture { index, part: PicturePart::Body }, Layer::Text, icon))
--}
--
--/// What a right-click on a selected image asks for.
--#[derive(Clone, Copy, Debug, PartialEq)]
- pub enum ImageAction {
-pub(crate) enum ImageAction {
-- Replace(usize, usize),
-- Save(usize, usize),
-- Copy(usize, usize),
-- CopyFile(usize, usize),
-- /// Copy the picture, then remove it from the page.
-- Cut(usize, usize),
-- /// Mark the picture for redaction (applied later from Redact a PDF).
-- Redact(usize, usize),
-- /// Create a link over the picture.
-- Link(usize, usize),
-- /// Open Recognize Text for the picture's page.
-- Recognize(usize),
- Paste(usize, Option<[f64; 2]>),
- DrawFreehand,
- AddComment(usize, usize),
- Crop,
--}
--
--/// Where the rotate knob sits, and the edge point its stem starts from: above the middle of the
--/// top edge, or below the bottom edge when the area on screen (`view`) has no room above.
--pub(super) fn rotate_knob(b: Rect, view: Rect) -> (Pos2, Pos2) {
-- let above = b.center_top() - egui::vec2(0.0, 22.0);
-- if above.y - 6.0 >= view.top() { (above, b.center_top()) } else { (b.center_bottom() + egui::vec2(0.0, 22.0), b.center_bottom()) }
--}
--
--/// Degrees clockwise (-180..=180) that turn "straight up" from `centre` onto the pointer; `snap`
--/// rounds to steps of 15 degrees (Shift). A pointer on the centre turns nothing.
--pub(super) fn turn_degrees(centre: Pos2, pointer: Pos2, snap: bool) -> f32 {
-- let v = pointer - centre;
-- if !(v.x.is_finite() && v.y.is_finite()) || v.length() < 4.0 {
-- return 0.0;
-- }
-- // Screen y points down: up is (0, -1); clockwise is atan2(x, -y).
-- let d = v.x.atan2(-v.y).to_degrees();
-- if snap { (d / 15.0).round() * 15.0 } else { d }
--}
--
- #[doc(hidden)]
- pub fn image_menu_id() -> egui::Id {
- egui::Id::unique("image-menu")
-/// Which boxes are outlined on the page, and why.
-struct OutlineState {
- selected: Option<usize>,
- hovered_box: Option<usize>,
- /// The picture being dragged, whose outline would trail behind until the release.
- moving: Option<usize>,
- dragging: bool,
- /// Few images: faint outlines for all of them.
- faint_all: bool,
--}
--
- /// Images on a page: select, move, resize, right-click. Returns `true` when the pointer was used.
- #[allow(clippy::too_many_arguments)]
- pub(crate) fn image_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- images: &[pdfcraft_engine::PageImage],
- view: &mut DocView,
- action: &mut Option<ImageAction>,
- allow_interaction: bool,
- ) -> bool {
- let _ = &action;
- let boxes: Vec<Rect> = images.iter().map(|im| crate::canvas::image_screen_rect(im, xf, info, page)).collect();
- let painter = ui.painter();
- let hover_pos = ptr.pos;
- let image_contains = |index: usize, point: Pos2| {
- let (x, y) = xf.screen_to_view(point);
- let Some(p) = info.pages.get(page) else { return false };
- let user = p.view_to_user(x, y);
- images.get(index).is_some_and(|im| im.contains(user.map(f64::from)))
- };
- let selected = view.image_selection.as_ref().filter(|s| s.page == page).map(|s| s.index).filter(|i| *i < boxes.len());
- let dragging = view.image_selection.as_ref().is_some_and(|s| s.drag.is_some());
- let faint_all = boxes.len() <= MAX_OUTLINES;
- let moving = selected.filter(|_| dragging);
-/// Outlines the images: all of them faintly when there are few, else the selected and hovered
-/// ones; the hovered or selected one is emphasised.
-fn draw_outlines(painter: &egui::Painter, boxes: &[Rect], state: OutlineState) {
- let OutlineState { selected, hovered_box, moving, dragging, faint_all } = state;
-- if faint_all {
- for (idx, im) in images.iter().enumerate() {
- if moving == Some(idx) {
- continue;
- }
- if let Some(b) = boxes.get(idx).filter(|b| b.is_positive()) {
- let obb = ImageObb::from_image(im, xf, info, page);
- if obb.is_rotated {
- let pts = obb.corners();
- for k in 0..4 {
- painter.line_segment([pts[k], pts[(k + 1) % 4]], Stroke::new(0.75, ACCENT.gamma_multiply(0.35)));
- }
- } else {
- painter.rect_stroke(*b, CornerRadius::ZERO, Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
- }
- }
- for (_, b) in boxes.iter().enumerate().filter(|(i, b)| b.is_positive() && moving != Some(*i)) {
- painter.rect_stroke(*b, CornerRadius::ZERO, Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
-- }
-- }
- let hovered_box = hover_pos.and_then(|p| {
- boxes.iter().enumerate().rposition(|(i, b)| {
- b.is_positive()
- && images.get(i).is_some_and(|img| b.contains(p) || quad_contains(ImageObb::from_image(img, xf, info, page).corners(), p))
- && image_contains(i, p)
- })
- });
-- if !faint_all {
-- for i in selected.filter(|_| moving.is_none()).into_iter().chain(hovered_box.filter(|h| Some(*h) != selected)) {
- if let (Some(b), Some(img)) = (boxes.get(i).filter(|b| b.is_positive()), images.get(i)) {
- let obb = ImageObb::from_image(img, xf, info, page);
- if obb.is_rotated {
- let pts = obb.corners();
- for k in 0..4 {
- painter.line_segment([pts[k], pts[(k + 1) % 4]], Stroke::new(0.75, ACCENT.gamma_multiply(0.35)));
- }
- } else {
- painter.rect_stroke(*b, CornerRadius::ZERO, Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
- }
- if let Some(b) = boxes.get(i).filter(|b| b.is_positive()) {
- painter.rect_stroke(*b, CornerRadius::ZERO, Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
-- }
-- }
-- }
-- for i in selected.into_iter().chain(hovered_box).collect::<Vec<_>>() {
- let (Some(b), Some(img)) = (boxes.get(i).copied().filter(|b| b.is_positive()), images.get(i)) else { continue };
- let obb = ImageObb::from_image(img, xf, info, page);
- let Some(b) = boxes.get(i).copied().filter(|b| b.is_positive()) else { continue };
-- if selected == Some(i) {
- // The selected frame and handles are drawn below (or the drag preview instead).
-- if !dragging {
- if obb.is_rotated {
- painter.polygon(obb.corners().to_vec(), ACCENT.gamma_multiply(0.04), Stroke::NONE);
- } else {
- painter.rect_filled(b, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
- }
- painter.rect_filled(b, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
-- }
- } else if obb.is_rotated {
- painter.polygon(obb.corners().to_vec(), Color32::TRANSPARENT, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)));
-- } else {
-- painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)), egui::StrokeKind::Outside);
- }
- }
-}
-
-fn draw_handles(painter: &egui::Painter, rect: Rect) {
- for c in [rect.left_top(), rect.right_top(), rect.left_bottom(), rect.right_bottom()] {
- painter.rect(
- Rect::from_center_size(c, egui::vec2(8.0, 8.0)),
- CornerRadius::ZERO,
- Color32::WHITE,
- Stroke::new(1.0, ACCENT),
- egui::StrokeKind::Middle,
- );
- }
-}
-
-/// Turning the picture by its rotate handle: the turned outline as a preview (the engine redraws
-/// the pixels on release).
-fn turn_preview(cx: &ImageCx<'_>, view: &mut DocView, i: usize, b: Rect, knob: Pos2) {
- let ImageCx { ui, resp, ptr, page, .. } = *cx;
- let painter = ui.painter();
- let hover_pos = ptr.pos;
- let shift = ui.input(|i| i.modifiers.shift);
- let degrees = hover_pos.map_or(0.0, |p| turn_degrees(b.center(), p, shift));
- // The turned outline as a preview; the engine redraws the pixels on release.
- let centre = b.center();
- let (sin, cos) = degrees.to_radians().sin_cos();
- let turn = |c: Pos2| {
- let v = c - centre;
- centre + egui::vec2(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
- };
- let outline = [b.left_top(), b.right_top(), b.right_bottom(), b.left_bottom()].map(turn).to_vec();
- painter.add(egui::Shape::closed_line(outline, Stroke::new(1.5, ACCENT)));
- let galley = painter.layout_no_wrap(format!("{degrees:.0}\u{b0}"), egui::FontId::proportional(12.0), ACCENT);
- let label = Rect::from_center_size(turn(knob) - egui::vec2(0.0, 14.0), galley.size() + egui::vec2(8.0, 4.0));
- painter.rect(label, CornerRadius::same(3), Color32::WHITE, Stroke::new(1.0, ACCENT), egui::StrokeKind::Middle);
- painter.galley(label.center() - galley.size() / 2.0, galley, ACCENT);
- view.cursor.claim(Layer::Drag, egui::CursorIcon::Grabbing);
- if resp.drag_stopped() || !ptr.primary_down {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = None;
- s.turning = false;
- }
- if degrees.abs() >= 0.05 {
- view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::RotateBy(f64::from(degrees)) });
- }
- }
-}
-
-/// Where the screen may still show the picture from before an applied move, and whether the
-/// page's whole raster is the old one.
-#[derive(Clone, Copy)]
-struct Blanked {
- left_at: Option<Rect>,
- raster_old: bool,
-}
-
-/// A frame of the picture being moved or resized: the stretched picture, its frame and handles,
-/// and on release the edit.
-fn drag_preview(
- cx: &ImageCx<'_>,
- images: &[pdfcraft_engine::PageImage],
- view: &mut DocView,
- (i, b): (usize, Rect),
- (start, opposite, p): (Pos2, Option<Pos2>, Pos2),
- blanked: Blanked,
-) {
- let ImageCx { ui, resp, xf, ptr, page, info, .. } = *cx;
- let painter = ui.painter();
- let Blanked { left_at, raster_old } = blanked;
- let screen = |r: Rect| Rect::from_two_pos(xf.norm_to_screen(r.min.x, r.min.y), xf.norm_to_screen(r.max.x, r.max.y));
- let preview = match opposite {
- // Resize from the opposite corner, keeping the aspect ratio.
- Some(fixed) => {
- let (w0, h0) = (b.width().max(1.0), b.height().max(1.0));
- // Use the axis with the larger relative pointer change. Taking the maximum
- // absolute size freezes shrinking when the other axis hasn't moved. Measure
- // from the press point so grabbing a handle off-centre cannot cause a jump.
- let horizontal = (p.x - start.x) * (start.x - fixed.x).signum() / w0;
- let vertical = (p.y - start.y) * (start.y - fixed.y).signum() / h0;
- let delta = if horizontal.abs() > vertical.abs() { horizontal } else { vertical };
- let k = (1.0 + delta).max(0.05);
- let (w, h) = (w0 * k, h0 * k);
- let x = if start.x < fixed.x { fixed.x - w } else { fixed.x };
- let y = if start.y < fixed.y { fixed.y - h } else { fixed.y };
- Rect::from_min_size(Pos2::new(x, y), egui::vec2(w, h))
- }
- None => b.translate(p - start),
- };
- // Move/resize the full placement by the visible frame's change. The engine
- // transforms an isolated crop together with its image, preserving its contents.
- let raw = images.get(i).map_or(b, |im| xf.user_rect(info, page, im.rect.map(|v| v as f32)));
- // A zero-width or zero-height visible frame (or a degenerate ratio) keeps the
- // placement's scale instead of producing inf/NaN.
- let ratio = |new: f32, old: f32| if old.abs() > 1e-3 && (new / old).is_finite() { new / old } else { 1.0 };
- let factor = egui::vec2(ratio(preview.width(), b.width()), ratio(preview.height(), b.height()));
- let target = Rect::from_min_max(preview.min + (raw.min - b.min) * factor, preview.min + (raw.max - b.min) * factor);
- let visible = preview.intersect(xf.rect);
- if visible.is_positive() {
- // The pixels follow the frame: the page raster's picture of the image, stretched
- // into the preview over a blank where it was. Re-rendering every frame of a drag
- // would lag; the edit renders it properly on release.
- if preview != b
- && let Some(tex) = view.page_raster(page)
- {
- // Its pixels: where the whole-page raster has it.
- let source = left_at.filter(|_| raster_old).map_or(b, screen);
- let blank: Vec<Rect> = left_at.map(screen).into_iter().chain(std::iter::once(b)).collect();
- stretched_picture(painter, xf, tex, &blank, source, preview);
- }
- painter.rect_filled(visible, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
- painter.rect_stroke(visible, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- draw_handles(painter, preview);
- }
- if resp.drag_stopped() || !ptr.primary_down {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = None;
- let norm = |r: Rect| {
- let ((u0, v0), (u1, v1)) = (xf.screen_to_norm(r.min), xf.screen_to_norm(r.max));
- Rect::from_two_pos(Pos2::new(u0, v0), Pos2::new(u1, v1))
- };
- s.settled = (preview != b).then(|| (left_at.unwrap_or_else(|| norm(b)), norm(preview)));
- }
- if preview != b {
- view.pending_edit =
- Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Move(user_box(xf, info, page, target)) });
-- }
-- }
- view.cursor.claim(
- Layer::Drag,
- match opposite {
- Some(fixed) if (start.x - fixed.x) * (start.y - fixed.y) < 0.0 => egui::CursorIcon::ResizeNeSw,
- Some(_) => egui::CursorIcon::ResizeNwSe,
- None => egui::CursorIcon::Move,
- },
- );
-}
-
-/// Images on a page: select, move, resize, right-click. Returns `true` when the pointer was used.
-pub(crate) fn image_input(cx: &ImageCx<'_>, images: &[pdfcraft_engine::PageImage], view: &mut DocView, action: &mut Option<ImageAction>) -> bool {
- let ImageCx { ui, resp, xf, ptr, page, info, allow_interaction } = *cx;
- let boxes: Vec<Rect> = images.iter().map(|im| im.visible_rect.map_or(Rect::NOTHING, |r| xf.user_rect(info, page, r.map(|v| v as f32)))).collect();
- let painter = ui.painter();
- let hover_pos = ptr.pos;
- let image_contains = |index: usize, point: Pos2| {
- let (x, y) = xf.screen_to_view(point);
- let Some(p) = info.pages.get(page) else { return false };
- let user = p.view_to_user(x, y);
- images.get(index).is_some_and(|im| im.contains(user.map(f64::from)))
- };
- let selected = view.image_selection.as_ref().filter(|s| s.page == page).map(|s| s.index).filter(|i| *i < boxes.len());
- let dragging = view.image_selection.as_ref().is_some_and(|s| s.drag.is_some());
- // Few images: faint outlines for all of them, as before. Many: only the selected and
- // hovered boxes, so a ~1000-image CAD page stays interactive in Edit text & images.
- // Fully clipped placements (empty visible frames) get no outline.
- let faint_all = boxes.len() <= MAX_OUTLINES;
- // The picture being dragged has its frame at the pointer; its outline where it was would
- // trail behind until the release.
- let moving = selected.filter(|_| dragging);
- let hovered_box = hover_pos.and_then(|p| boxes.iter().enumerate().rposition(|(i, b)| b.is_positive() && b.contains(p) && image_contains(i, p)));
- draw_outlines(painter, &boxes, OutlineState { selected, hovered_box, moving, dragging, faint_all });
-- // The selected image: frame, corner handles, dragging.
-- if let Some(i) = selected.filter(|i| boxes.get(*i).is_some_and(|b| b.is_positive())) {
- let Some(b) = boxes.get(i).copied() else { return false };
- let Some(image) = images.get(i) else { return false };
- let obb = ImageObb::from_image(image, xf, info, page);
- let b = boxes[i];
- if view.image_selection.as_ref().is_some_and(|s| s.crop.is_some()) {
- return crop_input(cx, view, i, b);
- }
- let corners = [b.left_top(), b.right_top(), b.left_bottom(), b.right_bottom()];
- // Until the page is rendered again its raster has an applied move or resize's picture
- // where it was: it stays blanked there and drawn where it went.
- // The whole-page raster and its tiles are replaced separately: the old frame stays blanked
- // while anything still shows the picture there, but its pixels come from the old frame
- // only while the whole-page raster itself is the old one. Once that is fresh it has the
- // picture where it went, and that is where they come from.
-- let (out_of_date, raster_old, raster) = (view.page_out_of_date(page), view.page_raster_out_of_date(page), view.page_raster(page));
-- let screen = |r: Rect| Rect::from_two_pos(xf.norm_to_screen(r.min.x, r.min.y), xf.norm_to_screen(r.max.x, r.max.y));
- // Where the screen may still show the picture from before an applied move, while the page
- // isn't drawn again yet. Another drag keeps it blanked too.
-- let left_at = view.image_selection.as_ref().and_then(|s| s.settled).filter(|_| out_of_date).map(|(from, _)| from);
-- if let Some(s) = view.image_selection.as_mut().filter(|s| s.drag.is_none()) {
-- match (s.settled, raster) {
-- (Some((from, to)), Some(tex)) if out_of_date => {
-- let source = if raster_old { from } else { to };
-- stretched_picture(painter, xf, tex, &[screen(from)], screen(source), screen(to));
-- }
- _ if !out_of_date => s.settled = None,
- _ => {}
- _ => s.settled = None,
-- }
-- }
-
- if view.image_selection.as_ref().is_some_and(|s| s.crop.is_some() || s.crop_mode) {
- if let Some(s) = view.image_selection.as_mut()
- && s.crop.is_none()
- {
- let rect = images.get(i).and_then(|im| im.visible_rect).unwrap_or(images.get(i).map_or([0.0; 4], |im| im.rect));
- s.crop = Some(CropMode::new(rect));
- }
- return crop_input(ui, resp, xf, ptr, page, info, view, i, image, allow_interaction);
- if view.image_selection.as_ref().is_some_and(|s| s.drag.is_none()) {
- painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- draw_handles(painter, b);
-- }
- // The rotate handle: a knob above the top edge, joined to it by a short stem.
-- let (knob, stem) = rotate_knob(b, ui.clip_rect());
-- if view.image_selection.as_ref().is_some_and(|s| s.drag.is_none()) {
- if obb.is_rotated {
- painter.polygon(obb.corners().to_vec(), Color32::TRANSPARENT, Stroke::new(1.5, ACCENT));
- painter.line_segment([obb.s_tc, obb.s_rot], Stroke::new(1.2, ACCENT));
- painter.circle(obb.s_rot, 4.5, Color32::WHITE, Stroke::new(1.0, ACCENT));
- } else {
- painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- painter.line_segment([stem, knob], Stroke::new(1.0, ACCENT));
- painter.circle(knob, 5.0, Color32::WHITE, Stroke::new(1.0, ACCENT));
- }
- for (_, h) in obb.handles() {
- let is_hovered = hover_pos.is_some_and(|p| p.distance(h) < 10.0);
- draw_handle(painter, h, false, is_hovered, false, ACCENT);
- }
- }
-
- let mut handle_drag = None;
- for (h, h_pos, handle_id) in obb.handles_with_ids(ui.scope_id().with("img_handle")) {
- let handle_rect = Rect::from_center_size(h_pos, egui::vec2(18.0, 18.0));
- let handle_resp = ui.interact(handle_rect, handle_id, egui::Sense::drag());
- if allow_interaction && handle_resp.drag_started() {
- handle_drag = Some(h);
- }
- }
- let rot_id = ui.scope_id().with("img_handle").with(8);
- let rot_rect = Rect::from_center_size(if obb.is_rotated { obb.s_rot } else { knob }, egui::vec2(12.0, 12.0));
- let rot_resp = ui.interact(rot_rect, rot_id, egui::Sense::drag());
- let rot_drag = allow_interaction && rot_resp.drag_started();
-
- if allow_interaction && let Some(p) = hover_pos {
- if obb.s_rot.distance(p) < 8.0 || knob.distance(p) < 8.0 {
- ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);
- } else if let Some((h, _)) = obb.handles().iter().find(|(_, pos)| pos.distance(p) < 8.0) {
- let icon = match h {
- ImageHandle::TopLeft | ImageHandle::BottomRight => {
- if obb.is_rotated {
- let v = obb.handle_pos(*h) - obb.screen_center;
- if v.x * v.y > 0.0 { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw }
- } else {
- egui::CursorIcon::ResizeNwSe
- }
- }
- ImageHandle::TopRight | ImageHandle::BottomLeft => {
- if obb.is_rotated {
- let v = obb.handle_pos(*h) - obb.screen_center;
- if v.x * v.y > 0.0 { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw }
- } else {
- egui::CursorIcon::ResizeNeSw
- }
- }
- ImageHandle::TopCenter | ImageHandle::BottomCenter => {
- if obb.is_rotated && (obb.v_axis.0.abs() > obb.v_axis.1.abs()) {
- egui::CursorIcon::ResizeHorizontal
- } else {
- egui::CursorIcon::ResizeVertical
- }
- }
- ImageHandle::RightCenter | ImageHandle::LeftCenter => {
- if obb.is_rotated && (obb.u_axis.1.abs() > obb.u_axis.0.abs()) {
- egui::CursorIcon::ResizeVertical
- } else {
- egui::CursorIcon::ResizeHorizontal
- }
- }
- };
- ui.ctx().set_cursor_icon(icon);
- }
- painter.line_segment([stem, knob], Stroke::new(1.0, ACCENT));
- painter.circle(knob, 5.0, Color32::WHITE, Stroke::new(1.0, ACCENT));
-- }
-
- let origin = ptr.origin;
- if allow_interaction {
- if let Some(h) = handle_drag {
- if let Some(s) = view.image_selection.as_mut() {
- let o = origin.unwrap_or_else(|| obb.handle_pos(h));
- s.drag = Some(ImageDrag { start: o, target: DragTarget::Handle(h), initial_rect: b, initial_matrix: image.matrix });
- }
- } else if rot_drag {
- if let Some(s) = view.image_selection.as_mut() {
- let o = origin.unwrap_or(if obb.is_rotated { obb.s_rot } else { knob });
- s.drag = Some(ImageDrag { start: o, target: DragTarget::Rotate, initial_rect: b, initial_matrix: image.matrix });
- // A drag on what the press was on ([`handle_hit`], [`body_hit`]): the knob turns the
- // picture, a corner resizes it from the opposite one, the picture itself moves.
- if allow_interaction
- && resp.drag_started()
- && let Some(o) = ptr.origin
- && let Some(Owner::Picture { index, part }) = view.router.owner(page)
- && index == i
- && let Some(s) = view.image_selection.as_mut()
- {
- match part {
- PicturePart::Knob => {
- s.drag = Some((o, None));
-- s.turning = true;
- }
- } else if resp.drag_started()
- && let Some(o) = origin
- {
- if obb.s_rot.distance(o) < 8.0 || knob.distance(o) < 8.0 {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = Some(ImageDrag { start: o, target: DragTarget::Rotate, initial_rect: b, initial_matrix: image.matrix });
- s.turning = true;
- }
- } else if let Some((h, _)) = obb.handles().iter().find(|(_, pos)| pos.distance(o) < 8.0) {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = Some(ImageDrag { start: o, target: DragTarget::Handle(*h), initial_rect: b, initial_matrix: image.matrix });
- }
- } else if (image_contains(i, o) || quad_contains(obb.corners(), o))
- && let Some(s) = view.image_selection.as_mut()
- {
- s.drag = Some(ImageDrag { start: o, target: DragTarget::Move, initial_rect: b, initial_matrix: image.matrix });
-- }
- PicturePart::Corner(k) => s.drag = Some((o, 3usize.checked_sub(k).and_then(|opposite| corners.get(opposite)).copied())),
- PicturePart::Body => s.drag = Some((o, None)),
-- }
-- }
-
-- if view.image_selection.as_ref().is_some_and(|s| s.turning) {
- let shift = ui.input(|i| i.modifiers.shift);
- let degrees = hover_pos.map_or(0.0, |p| turn_degrees(b.center(), p, shift));
- let centre = b.center();
- let (sin, cos) = degrees.to_radians().sin_cos();
- let turn = |c: Pos2| {
- let v = c - centre;
- centre + egui::vec2(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
- };
- let outline = [b.left_top(), b.right_top(), b.right_bottom(), b.left_bottom()].map(turn).to_vec();
- painter.add(egui::Shape::closed_line(outline, Stroke::new(1.5, ACCENT)));
- let galley = painter.layout_no_wrap(format!("{degrees:.0}\u{b0}"), egui::FontId::proportional(12.0), ACCENT);
- let label = Rect::from_center_size(turn(knob) - egui::vec2(0.0, 14.0), galley.size() + egui::vec2(8.0, 4.0));
- painter.rect(label, CornerRadius::same(3), Color32::WHITE, Stroke::new(1.0, ACCENT), egui::StrokeKind::Middle);
- painter.galley(label.center() - galley.size() / 2.0, galley, ACCENT);
- ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
- if resp.drag_stopped() || !ptr.primary_down {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = None;
- s.turning = false;
- }
- if degrees.abs() >= 0.05 {
- view.pending_edit =
- Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::RotateBy(f64::from(degrees)) });
- }
- }
- turn_preview(cx, view, i, b, knob);
-- return true;
-- }
-
- if let Some(drag) = view.image_selection.as_ref().and_then(|s| s.drag)
- if let Some((start, opposite)) = view.image_selection.as_ref().and_then(|s| s.drag)
-- && let Some(p) = hover_pos
-- {
- if !obb.is_rotated && !matches!(drag.target, DragTarget::Rotate) {
- let preview = match drag.target {
- DragTarget::Handle(handle) => {
- let handle_id = HandleId::from(handle);
- let delta = p - drag.start;
- let is_corner = matches!(handle_id, HandleId::TopLeft | HandleId::TopRight | HandleId::BottomLeft | HandleId::BottomRight);
- let shift = ui.input(|inp| inp.modifiers.shift);
- if is_corner && !shift {
- apply_resize_proportional(drag.initial_rect, handle_id, delta)
- } else {
- apply_resize(drag.initial_rect, handle_id, delta)
- }
- }
- DragTarget::Move => drag.initial_rect.translate(p - drag.start),
- _ => drag.initial_rect,
- };
-
- let raw = images.get(i).map_or(b, |im| xf.user_rect(info, page, im.rect.map(|v| v as f32)));
- let ratio = |new: f32, old: f32| if old.abs() > 1e-3 && (new / old).is_finite() { new / old } else { 1.0 };
- let factor = egui::vec2(ratio(preview.width(), b.width()), ratio(preview.height(), b.height()));
- let f_min = egui::vec2((raw.min.x - b.min.x) * factor.x, (raw.min.y - b.min.y) * factor.y);
- let f_max = egui::vec2((raw.max.x - b.min.x) * factor.x, (raw.max.y - b.min.y) * factor.y);
- let target = Rect::from_min_max(preview.min + f_min, preview.min + f_max);
- let visible = preview.intersect(xf.rect);
-
- if visible.is_positive() {
- if preview != b
- && let Some(tex) = raster
- {
- let source = left_at.filter(|_| raster_old).map_or(b, screen);
- let blank: Vec<Rect> = left_at.map(screen).into_iter().chain(std::iter::once(b)).collect();
- stretched_picture(painter, xf, tex, &blank, source, preview);
- }
- painter.rect_filled(visible, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
- painter.rect_stroke(visible, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- for h in [
- preview.left_top(),
- preview.right_top(),
- preview.left_bottom(),
- preview.right_bottom(),
- preview.center_top(),
- preview.right_center(),
- preview.center_bottom(),
- preview.left_center(),
- ] {
- draw_handle(painter, h, false, false, false, ACCENT);
- }
- }
-
- if resp.drag_stopped() || !ptr.primary_down {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = None;
- let norm = |r: Rect| {
- let ((u0, v0), (u1, v1)) = (xf.screen_to_norm(r.min), xf.screen_to_norm(r.max));
- Rect::from_two_pos(Pos2::new(u0, v0), Pos2::new(u1, v1))
- };
- s.settled = (preview != b).then(|| (left_at.unwrap_or_else(|| norm(b)), norm(preview)));
- }
- if preview != b {
- view.pending_edit =
- Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Move(user_box(xf, info, page, target)) });
- }
- }
-
- let icon = match drag.target {
- DragTarget::Handle(handle) => HandleId::from(handle).cursor_icon(),
- DragTarget::Move => egui::CursorIcon::Grabbing,
- _ => egui::CursorIcon::Move,
- };
- ui.ctx().set_cursor_icon(icon);
- return true;
- }
- let m_init = Matrix(drag.initial_matrix);
- let [a0, b0, c0, d0, e0, f0] = drag.initial_matrix;
- let w0 = (a0 * a0 + b0 * b0).sqrt().max(1e-6);
- let h0 = (c0 * c0 + d0 * d0).sqrt().max(1e-6);
- let u_axis = (a0 / w0, b0 / w0);
- let v_axis = (c0 / h0, d0 / h0);
- let center_user = (e0 + 0.5 * (a0 + c0), f0 + 0.5 * (b0 + d0));
-
- let (sx, sy) = screen_to_user(xf, info, page, drag.start);
- let (px, py) = screen_to_user(xf, info, page, p);
-
- let (m_new, cursor_icon) = match drag.target {
- DragTarget::Rotate => {
- let v0 = (sx - center_user.0, sy - center_user.1);
- let v1 = (px - center_user.0, py - center_user.1);
- let theta0 = v0.1.atan2(v0.0);
- let theta1 = v1.1.atan2(v1.0);
- let mut delta = theta1 - theta0;
- if ui.input(|i| i.modifiers.shift) {
- let step = std::f64::consts::PI / 12.0;
- delta = (delta / step).round() * step;
- }
- let (cos_d, sin_d) = (delta.cos(), delta.sin());
- let t_rot = [
- cos_d,
- sin_d,
- -sin_d,
- cos_d,
- center_user.0 * (1.0 - cos_d) + center_user.1 * sin_d,
- center_user.1 * (1.0 - cos_d) - center_user.0 * sin_d,
- ];
- (m_init.then(&Matrix(t_rot)), egui::CursorIcon::Grab)
- }
- DragTarget::Move => {
- let (dx, dy) = (px - sx, py - sy);
- (Matrix([a0, b0, c0, d0, e0 + dx, f0 + dy]), egui::CursorIcon::Move)
- }
- DragTarget::Handle(handle) => {
- let p00 = (e0, f0);
- let p10 = (e0 + a0, f0 + b0);
- let p11 = (e0 + a0 + c0, f0 + b0 + d0);
- let p01 = (e0 + c0, f0 + d0);
- let p_curr = (px, py);
-
- let (new_w, new_h, p00_new, icon) = match handle {
- ImageHandle::TopRight => {
- let anchor = p00;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let du = d.0 * u_axis.0 + d.1 * u_axis.1;
- let dv = d.0 * v_axis.0 + d.1 * v_axis.1;
- let su = du / w0;
- let sv = dv / h0;
- let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
- let s = s.max(5.0 / w0).max(5.0 / h0);
- (w0 * s, h0 * s, anchor, egui::CursorIcon::ResizeNeSw)
- }
- ImageHandle::TopLeft => {
- let anchor = p10;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let du = d.0 * (-u_axis.0) + d.1 * (-u_axis.1);
- let dv = d.0 * v_axis.0 + d.1 * v_axis.1;
- let su = du / w0;
- let sv = dv / h0;
- let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
- let s = s.max(5.0 / w0).max(5.0 / h0);
- let nw = w0 * s;
- let nh = h0 * s;
- (nw, nh, (anchor.0 - nw * u_axis.0, anchor.1 - nw * u_axis.1), egui::CursorIcon::ResizeNwSe)
- }
- ImageHandle::BottomLeft => {
- let anchor = p11;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let du = d.0 * (-u_axis.0) + d.1 * (-u_axis.1);
- let dv = d.0 * (-v_axis.0) + d.1 * (-v_axis.1);
- let su = du / w0;
- let sv = dv / h0;
- let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
- let s = s.max(5.0 / w0).max(5.0 / h0);
- let nw = w0 * s;
- let nh = h0 * s;
- (
- nw,
- nh,
- (anchor.0 - nw * u_axis.0 - nh * v_axis.0, anchor.1 - nw * u_axis.1 - nh * v_axis.1),
- egui::CursorIcon::ResizeNeSw,
- )
- }
- ImageHandle::BottomRight => {
- let anchor = p01;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let du = d.0 * u_axis.0 + d.1 * u_axis.1;
- let dv = d.0 * (-v_axis.0) + d.1 * (-v_axis.1);
- let su = du / w0;
- let sv = dv / h0;
- let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
- let s = s.max(5.0 / w0).max(5.0 / h0);
- let nw = w0 * s;
- let nh = h0 * s;
- (nw, nh, (anchor.0 - nh * v_axis.0, anchor.1 - nh * v_axis.1), egui::CursorIcon::ResizeNwSe)
- }
- ImageHandle::RightCenter => {
- let anchor = p00;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let du = (d.0 * u_axis.0 + d.1 * u_axis.1).max(5.0);
- (du, h0, anchor, egui::CursorIcon::ResizeHorizontal)
- }
- ImageHandle::LeftCenter => {
- let anchor = p10;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let du = (d.0 * (-u_axis.0) + d.1 * (-u_axis.1)).max(5.0);
- (du, h0, (anchor.0 - du * u_axis.0, anchor.1 - du * u_axis.1), egui::CursorIcon::ResizeHorizontal)
- }
- ImageHandle::TopCenter => {
- let anchor = p00;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let dv = (d.0 * v_axis.0 + d.1 * v_axis.1).max(5.0);
- (w0, dv, anchor, egui::CursorIcon::ResizeVertical)
- }
- ImageHandle::BottomCenter => {
- let anchor = p01;
- let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
- let dv = (d.0 * (-v_axis.0) + d.1 * (-v_axis.1)).max(5.0);
- (w0, dv, (anchor.0 - dv * v_axis.0, anchor.1 - dv * v_axis.1), egui::CursorIcon::ResizeVertical)
- }
- };
- let mat = Matrix([new_w * u_axis.0, new_w * u_axis.1, new_h * v_axis.0, new_h * v_axis.1, p00_new.0, p00_new.1]);
- (mat, icon)
- }
- _ => (m_init, egui::CursorIcon::Default),
- };
-
- let preview_obb = ImageObb::new(m_new.0, xf, info, page);
- if let Some(tex) = view.page_raster(page) {
- let clipped = painter.with_clip_rect(painter.clip_rect().intersect(xf.rect));
- let mut mesh = egui::Mesh::with_texture(tex);
- let orig_corners = obb.corners();
- let new_corners = preview_obb.corners();
- for (from, to) in [
- (orig_corners[3], new_corners[3]),
- (orig_corners[2], new_corners[2]),
- (orig_corners[1], new_corners[1]),
- (orig_corners[0], new_corners[0]),
- ] {
- let (u, v) = xf.screen_to_norm(from);
- mesh.vertices.push(egui::epaint::Vertex { pos: to, uv: Pos2::new(u, v), color: Color32::WHITE });
- }
- mesh.add_triangle(0, 1, 2);
- mesh.add_triangle(0, 2, 3);
- clipped.add(egui::Shape::mesh(mesh));
- }
-
- if preview_obb.is_rotated {
- let pts = preview_obb.corners();
- painter.polygon(pts.to_vec(), ACCENT.gamma_multiply(0.04), Stroke::new(1.5, ACCENT));
- } else {
- let p_rect = Rect::from_two_pos(preview_obb.s01, preview_obb.s10);
- let visible = p_rect.intersect(xf.rect);
- if visible.is_positive() {
- painter.rect_filled(visible, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
- painter.rect_stroke(visible, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
- }
- }
- for (_, h) in preview_obb.handles() {
- draw_handle(painter, h, false, false, false, ACCENT);
- }
-
- if resp.drag_stopped() || !ptr.primary_down {
- if let Some(s) = view.image_selection.as_mut() {
- s.drag = None;
- }
- if m_new.0 != drag.initial_matrix {
- if matches!(drag.target, DragTarget::Move) {
- let (dx, dy) = (px - sx, py - sy);
- let new_rect = [image.rect[0] + dx, image.rect[1] + dy, image.rect[2] + dx, image.rect[3] + dy];
- view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Move(new_rect) });
- } else if let Some(inv) = Matrix(drag.initial_matrix).invert() {
- let t = inv.then(&m_new);
- view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Transform(t.0) });
- }
- }
- }
- ui.ctx().set_cursor_icon(cursor_icon);
- drag_preview(cx, images, view, (i, b), (start, opposite, p), Blanked { left_at, raster_old });
-- return true;
-- }
-- if allow_interaction
-- && ui.input(|inp| inp.key_pressed(egui::Key::Delete) || inp.key_pressed(egui::Key::Backspace))
-- && !ui.ctx().egui_wants_keyboard_input()
-- {
-- view.image_selection = None;
-- view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Delete });
-- return true;
-- }
-- }
-- if !allow_interaction {
-- return false;
-- }
- let menu_key = image_menu_id();
- // An open menu stays drawn wherever the pointer is: most of its items lie outside the picture
- // (a logo is smaller than its menu), and a menu that is not drawn for a frame cannot be clicked.
- // Only a menu opened by a right-click on the picture: one opened over text or empty page while
- // a picture stays selected is the page's own menu.
- let menu_key = resp.id.with("image-menu");
- if resp.secondary_clicked() {
- ui.ctx().data_mut(|d| d.insert_temp(menu_key, false));
- }
- let mut menu_drawn = false;
- if let Some(sel) = selected
- && resp.context_menu_opened()
- && ui.ctx().data(|d| d.get_temp::<bool>(menu_key)).unwrap_or(false)
- {
- resp.context_menu(|ui| image_menu(ui, page, sel, images, view, action));
- menu_drawn = true;
- }
-- let Some(p) = hover_pos.filter(|p| xf.rect.contains(*p)) else { return false };
- let Some(hit) = boxes.iter().enumerate().rposition(|(i, b)| {
- b.is_positive()
- && images.get(i).is_some_and(|img| b.contains(p) || quad_contains(ImageObb::from_image(img, xf, info, page).corners(), p))
- && image_contains(i, p)
- }) else {
- if resp.secondary_clicked() || resp.clicked() {
- view.image_selection = None;
- ui.ctx().data_mut(|d| d.insert_persisted(menu_key, false));
- }
- return false;
- };
- let Some(hit_img) = images.get(hit) else { return false };
- let hit_obb = ImageObb::from_image(hit_img, xf, info, page);
- let Some(hit) = boxes.iter().enumerate().rposition(|(i, b)| b.contains(p) && image_contains(i, p)) else { return false };
-- if selected != Some(hit) {
- if hit_obb.is_rotated {
- let pts = hit_obb.corners();
- for k in 0..4 {
- painter.line_segment([pts[k], pts[(k + 1) % 4]], Stroke::new(1.5, ACCENT.gamma_multiply(0.7)));
- }
- } else if let Some(b) = boxes.get(hit).copied() {
- painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)), egui::StrokeKind::Outside);
- }
- painter.rect_stroke(boxes[hit], CornerRadius::ZERO, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)), egui::StrokeKind::Outside);
-- }
- let corner = hit_obb.handles().iter().position(|(_, c)| c.distance(p) < 10.0);
- ui.ctx().set_cursor_icon(match (selected == Some(hit), corner) {
- (true, Some(0 | 4)) => egui::CursorIcon::ResizeNwSe,
- (true, Some(2 | 6)) => egui::CursorIcon::ResizeNeSw,
- (true, Some(1 | 5)) => egui::CursorIcon::ResizeVertical,
- (true, Some(3 | 7)) => egui::CursorIcon::ResizeHorizontal,
- (true, None) => egui::CursorIcon::Grab,
- _ => egui::CursorIcon::PointingHand,
- });
- if resp.clicked() || resp.secondary_clicked() {
- view.image_selection =
- Some(ImageSelection { page, index: hit, drag: None, crop: None, crop_mode: false, crop_rect: None, turning: false, settled: None });
- // A click selects the picture the press was on; a right-click the one under the pointer.
- let clicked = match view.router.owner(page) {
- Some(Owner::Picture { index, .. }) if resp.clicked() => Some(index),
- _ => resp.secondary_clicked().then_some(hit),
- };
- if let Some(index) = clicked {
- view.image_selection = Some(ImageSelection { page, index, drag: None, crop: None, turning: false, settled: None });
-- }
-- if resp.secondary_clicked() {
- ui.ctx().data_mut(|d| d.insert_persisted(menu_key, true));
- ui.ctx().data_mut(|d| d.insert_temp(menu_key, true));
- }
- if selected == Some(hit) && !menu_drawn {
- resp.context_menu(|ui| image_menu(ui, page, hit, images, view, action));
-- }
-- true
--}
--
- /// The right-click context menu for a selected image.
- #[allow(clippy::too_many_arguments)]
- pub(crate) fn image_context_menu(
-/// The right-click menu of the picture `hit` (its rotate, flip, replace, copy, cut, redact, link,
-/// recognize and delete items).
-fn image_menu(
-- ui: &mut egui::Ui,
- view: &mut DocView,
- images: &[pdfcraft_engine::PageImage],
-- page: usize,
-- hit: usize,
- can_paste: bool,
- images: &[pdfcraft_engine::PageImage],
- view: &mut DocView,
-- action: &mut Option<ImageAction>,
- at: Option<[f64; 2]>,
--) {
- let Some(img) = images.get(hit) else { return };
-- use pdfcraft_engine::ImageEdit as E;
- let raster = !img.is_form;
-
- let close = |ui: &mut egui::Ui| {
- ui.ctx().data_mut(|d| d.insert_persisted(image_menu_id(), false));
- egui::Popup::close_all(ui.ctx());
- ui.close();
- };
-
- // Top horizontal icon bar: Comment, Crop, Rotate CCW, Rotate CW
- ui.horizontal(|ui| {
- ui.spacing_mut().item_spacing.x = 4.0;
- if crate::icons::button(ui, "message-square", 24.0, false, tl!("Add a sticky note here")).clicked() {
- *action = Some(ImageAction::AddComment(page, hit));
- close(ui);
- }
- if raster && crate::icons::button(ui, "crop", 24.0, false, tl!("Crop")).clicked() {
- *action = Some(ImageAction::Crop);
- if let Some(s) = view.image_selection.as_mut() {
- let rect = img.visible_rect.unwrap_or(img.rect);
- s.crop = Some(CropMode::new(rect));
- s.crop_mode = true;
- s.crop_rect = None;
- }
- close(ui);
- let items: [(&str, Option<E>); 4] = [
- (tl!("Rotate Clockwise"), Some(E::Rotate(1))),
- (tl!("Rotate Counterclockwise"), Some(E::Rotate(3))),
- (tl!("Flip Horizontal"), Some(E::Flip { horizontal: true })),
- (tl!("Flip Vertical"), Some(E::Flip { horizontal: false })),
- ];
- for (label, change) in items {
- if ui.button(label).clicked() {
- view.pending_edit = change.map(|c| Edit::EditPageImage { page, index: hit, change: c });
- ui.close();
-- }
- if crate::icons::button(ui, "rotate-ccw", 24.0, false, tl!("Rotate Counterclockwise")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Rotate(3) });
- close(ui);
- }
- // Crop: the handles trim instead of resizing until Done.
- if ui.button(tl!("Crop Image")).clicked() {
- if let (Some(s), Some(im)) = (view.image_selection.as_mut(), images.get(hit)) {
- s.crop = Some(CropMode::new(im.visible_rect.unwrap_or(im.rect)));
-- }
- if crate::icons::button(ui, "rotate-cw", 24.0, false, tl!("Rotate Clockwise")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Rotate(1) });
- close(ui);
- ui.close();
- }
- let cropped = images.get(hit).is_some_and(|im| im.visible_rect.is_some_and(|v| v.iter().zip(im.rect.iter()).any(|(a, b)| (a - b).abs() > 0.01)));
- if ui.add_enabled(cropped, egui::Button::new(tl!("Remove Crop"))).clicked() {
- if let Some(im) = images.get(hit) {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Crop(im.rect) });
-- }
- });
-
- ui.separator();
-
- ui.close();
- }
- let raster = images.get(hit).is_some_and(|image| !image.is_form);
- if ui.add_enabled(raster, egui::Button::new(tl!("Replace Image…"))).clicked() {
- *action = Some(ImageAction::Replace(page, hit));
- ui.close();
- }
- if ui.add_enabled(raster, egui::Button::new(tl!("Save Image As…"))).clicked() {
- *action = Some(ImageAction::Save(page, hit));
- ui.close();
- }
- // Cutting and copying also find the picture inside grouped Form artwork.
-- if ui.button(tl!("Cut Image")).clicked() {
-- *action = Some(ImageAction::Cut(page, hit));
- close(ui);
- ui.close();
-- }
-- if ui.button(tl!("Copy Image")).clicked() {
-- *action = Some(ImageAction::Copy(page, hit));
- close(ui);
- }
- if ui.add_enabled(can_paste, egui::Button::new(tl!("Paste"))).clicked() {
- *action = Some(ImageAction::Paste(page, at));
- close(ui);
- }
-
- ui.separator();
-
- let is_cropped = img.visible_rect.is_some() && img.visible_rect != Some(img.rect);
- if raster && ui.add_enabled(is_cropped, egui::Button::new(tl!("Remove crop"))).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::RemoveCrop });
- close(ui);
- }
-
- if ui.button(tl!("Flip horizontal")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Flip { horizontal: true } });
- close(ui);
- }
- if ui.button(tl!("Flip vertical")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Flip { horizontal: false } });
- close(ui);
- }
-
- ui.menu_button(tl!("Arrange image"), |ui| {
- if ui.button(tl!("Bring to front")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::BringToFront) });
- close(ui);
- }
- if ui.button(tl!("Send to back")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::SendToBack) });
- close(ui);
- }
- if ui.button(tl!("Bring forward")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::BringForward) });
- close(ui);
- }
- if ui.button(tl!("Send backward")).clicked() {
- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::SendBackward) });
- close(ui);
- }
- });
-
- if ui.add_enabled(raster, egui::Button::new(tl!("Replace image"))).clicked() {
- *action = Some(ImageAction::Replace(page, hit));
- close(ui);
- ui.close();
-- }
- if ui.add_enabled(raster, egui::Button::new(tl!("Save Image As…"))).clicked() {
- *action = Some(ImageAction::Save(page, hit));
- close(ui);
- if ui.button(tl!("Copy Image in Original Format")).clicked() {
- *action = Some(ImageAction::CopyFile(page, hit));
- ui.close();
-- }
-
-- ui.separator();
-
- if ui.button(tl!("Draw freehand")).clicked() {
- *action = Some(ImageAction::DrawFreehand);
- close(ui);
- }
-- if ui.button(tl!("Redact Image")).clicked() {
-- *action = Some(ImageAction::Redact(page, hit));
- close(ui);
- }
- if ui.add_enabled(raster, egui::Button::new(tl!("Export image"))).clicked() {
- *action = Some(ImageAction::Save(page, hit));
- close(ui);
- ui.close();
-- }
-
- ui.separator();
-
-- if ui.button(tl!("Create Link…")).clicked() {
-- *action = Some(ImageAction::Link(page, hit));
- close(ui);
- ui.close();
-- }
-
- ui.menu_button(tl!("Edit image using"), |ui| {
- ui.add_enabled(false, egui::Button::new(tl!("Default image editor")));
- });
-
-- if ui.button(tl!("Recognize Text…")).clicked() {
-- *action = Some(ImageAction::Recognize(page));
- close(ui);
- ui.close();
-- }
-
-- ui.separator();
-
-- if ui.button(tl!("Delete")).clicked() {
-- view.image_selection = None;
-- view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Delete });
- close(ui);
- ui.close();
-- }
--}
++use super::{ACCENT, Color32, CornerRadius, DocInfo, DocView, Edit, MAX_OUTLINES, PagePointer, PageXform, Pos2, Rect, Stroke, pos2, user_box};
++use crate::canvas::{ImageObb, PainterExt};
++use crate::image_engine::{CROP_ACCENT, HandleId, apply_crop, apply_resize, apply_resize_proportional, draw_handle, draw_rule_of_thirds};
++use crate::page_cursor::Layer;
++use crate::page_router::{Hit, Owner, PicturePart};
++
++/// What the pointer and the page are, for the image tools on one page.
++#[derive(Clone, Copy)]
++pub(crate) struct ImageCx<'a> {
++ pub ui: &'a egui::Ui,
++ pub resp: &'a egui::Response,
++ pub xf: &'a PageXform,
++ pub ptr: &'a PagePointer,
++ pub page: usize,
++ pub info: &'a DocInfo,
++ pub allow_interaction: bool,
++}
++
++/// The 8 resize handles on an image (4 corners + 4 edge centers).
++#[derive(Clone, Copy, Debug, PartialEq, Eq)]
++pub enum ImageHandle {
++ TopLeft,
++ TopCenter,
++ TopRight,
++ RightCenter,
++ BottomRight,
++ BottomCenter,
++ BottomLeft,
++ LeftCenter,
++}
++
++#[derive(Clone, Copy, Debug, PartialEq, Eq)]
++pub enum DragTarget {
++ Move,
++ Handle(ImageHandle),
++ CropHandle(ImageHandle),
++ Rotate,
++}
++
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub struct ImageDrag {
++ pub start: Pos2,
++ pub target: DragTarget,
++ pub initial_rect: Rect,
++ pub initial_matrix: [f64; 6],
++}
++
++/// A selected page image, and what the pointer is doing to it.
++#[derive(Clone, Debug, PartialEq)]
++pub struct ImageSelection {
++ pub page: usize,
++ pub index: usize,
++ /// Dragging in progress.
++ pub drag: Option<ImageDrag>,
++ /// Crop mode: the box kept so far.
++ pub crop: Option<CropMode>,
++ pub crop_mode: bool,
++ pub crop_rect: Option<Rect>,
++ /// The drag is turning the picture by its rotate handle (the `drag` start is then unused).
++ pub turning: bool,
++ /// A move or resize applied on release: the frame it had and the one it got, in the page's
++ /// 0..1 coordinates, while the page raster still has the picture where it was.
++ pub settled: Option<(Rect, Rect)>,
++}
++
++impl ImageSelection {
++ /// Cancel an in-progress text, rotation or crop drag and restore the crop it was previewing.
++ pub(crate) fn cancel_pointer_gesture(&mut self) {
++ self.drag = None;
++ self.turning = false;
++ if let Some(crop) = &mut self.crop
++ && let Some((_, _, before)) = crop.drag.take()
++ {
++ crop.keep = before;
++ }
++ }
++
++ pub(crate) fn dragging(&self) -> bool {
++ self.drag.is_some()
++ }
++}
++
++/// Cropping a page image: the handles trim instead of resizing, the part that will go is
++/// dimmed, Enter or Done applies and Esc or Cancel leaves it.
++#[derive(Clone, Debug, PartialEq)]
++pub struct CropMode {
++ /// The box to keep (user space or normalized UV).
++ pub keep: [f64; 4],
++ /// A drag in progress: the grip (0–7 handles, 8 inside), where it started and the box then.
++ pub(crate) drag: Option<(usize, Pos2, [f64; 4])>,
++}
++
++impl CropMode {
++ pub fn new(keep: [f64; 4]) -> Self {
++ Self { keep, drag: None }
++ }
++}
++
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub(crate) struct Matrix(pub(crate) [f64; 6]);
++
++impl Matrix {
++ pub(crate) fn apply(&self, x: f64, y: f64) -> (f64, f64) {
++ let [a, b, c, d, e, f] = self.0;
++ (a * x + c * y + e, b * x + d * y + f)
++ }
++
++ pub(crate) fn invert(&self) -> Option<Self> {
++ let [a, b, c, d, e, f] = self.0;
++ let det = a * d - b * c;
++ if det.abs() < 1e-12 || !det.is_finite() {
++ return None;
++ }
++ let (ia, ib, ic, id) = (d / det, -b / det, -c / det, a / det);
++ Some(Matrix([ia, ib, ic, id, -(e * ia + f * ic), -(e * ib + f * id)]))
++ }
++
++ pub(crate) fn then(&self, then: &Matrix) -> Matrix {
++ let [a, b, c, d, e, f] = self.0;
++ let [a2, b2, c2, d2, e2, f2] = then.0;
++ Matrix([a * a2 + b * c2, a * b2 + b * d2, c * a2 + d * c2, c * b2 + d * d2, e * a2 + f * c2 + e2, e * b2 + f * d2 + f2])
++ }
++}
++
++pub(crate) fn user_to_screen(xf: &PageXform, info: &DocInfo, page: usize, x: f64, y: f64) -> Pos2 {
++ let Some(p) = info.pages.get(page) else { return Pos2::ZERO };
++ let v = p.user_to_view(x as f32, y as f32);
++ xf.norm_to_screen(v[0] / xf.pw, v[1] / xf.ph)
++}
++
++fn screen_to_user(xf: &PageXform, info: &DocInfo, page: usize, pt: Pos2) -> (f64, f64) {
++ let (vx, vy) = xf.screen_to_view(pt);
++ let Some(p) = info.pages.get(page) else { return (0.0, 0.0) };
++ let u = p.view_to_user(vx, vy);
++ (f64::from(u[0]), f64::from(u[1]))
++}
++
++pub(crate) fn quad_contains(quad: [Pos2; 4], p: Pos2) -> bool {
++ if !p.x.is_finite() || !p.y.is_finite() || quad.iter().any(|pt| !pt.x.is_finite() || !pt.y.is_finite()) {
++ return false;
++ }
++ let mut sign = 0;
++ for i in 0..4 {
++ let p1 = quad[i];
++ let p2 = quad[(i + 1) % 4];
++ let cross = (p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x);
++ if cross.abs() > 1e-4 {
++ let s = if cross > 0.0 { 1 } else { -1 };
++ if sign == 0 {
++ sign = s;
++ } else if sign != s {
++ return false;
++ }
++ }
++ }
++ sign != 0
++}
++
++/// The screen positions of the crop's eight handles: corners, then the top, right, bottom and left edge midpoints.
++fn crop_handles(k: Rect) -> [Pos2; 8] {
++ [k.left_top(), k.right_top(), k.left_bottom(), k.right_bottom(), k.center_top(), k.right_center(), k.center_bottom(), k.left_center()]
++}
++
++/// Crop mode for the selected image `i` whose visible frame is `b` (screen). Returns `true`
++/// (the pointer and keys are the crop's while it lasts).
++#[allow(clippy::cognitive_complexity)]
++fn crop_input(
++ cx: &ImageCx<'_>,
++ view: &mut DocView,
++ i: usize,
++ image: &pdfcraft_engine::PageImage,
++) -> bool {
++ let ImageCx { ui, resp, xf, ptr, page, info, allow_interaction } = *cx;
++ let painter = ui.painter();
++ let Some(sel) = view.image_selection.as_mut() else { return false };
++ if let Some(rect) = sel.crop_rect
++ && let Some(mode) = sel.crop.as_mut()
++ {
++ mode.keep = user_box(xf, info, page, rect);
++ sel.crop_rect = None;
++ }
++ let Some(mode) = sel.crop.as_mut() else { return false };
++ let hover_pos = ptr.pos;
++ let full_b = xf.user_rect(info, page, image.rect.map(|v| v as f32));
++ let b = if full_b.is_positive() { full_b } else { xf.user_rect(info, page, image.visible_rect.unwrap_or(image.rect).map(|v| v as f32)) };
++ let keep_screen = |keep: [f64; 4]| xf.user_rect(info, page, keep.map(|v| v as f32)).intersect(b);
++ let k = keep_screen(mode.keep);
++
++ if allow_interaction
++ && resp.drag_started()
++ && let Some(o) = ptr.origin
++ {
++ let grip = crop_handles(k).iter().position(|h| h.distance(o) < 10.0).or_else(|| k.contains(o).then_some(8));
++ if let Some(g) = grip {
++ mode.drag = Some((g, o, mode.keep));
++ }
++ }
++
++ if let Some((grip, start, at_start)) = mode.drag
++ && let Some(p) = hover_pos
++ {
++ let d = p - start;
++ let k0 = keep_screen(at_start);
++ if grip < 8 {
++ let handle_id = match grip {
++ 0 => HandleId::TopLeft,
++ 1 => HandleId::TopRight,
++ 2 => HandleId::BottomLeft,
++ 3 => HandleId::BottomRight,
++ 4 => HandleId::TopMid,
++ 5 => HandleId::RightMid,
++ 6 => HandleId::BottomMid,
++ _ => HandleId::LeftMid,
++ };
++ let uv0 = if b.width() > 0.0 && b.height() > 0.0 {
++ Rect::from_min_max(
++ pos2(((k0.min.x - b.min.x) / b.width()).clamp(0.0, 1.0), ((k0.min.y - b.min.y) / b.height()).clamp(0.0, 1.0)),
++ pos2(((k0.max.x - b.min.x) / b.width()).clamp(0.0, 1.0), ((k0.max.y - b.min.y) / b.height()).clamp(0.0, 1.0)),
++ )
++ } else {
++ Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0))
++ };
++ let (new_screen, _) = apply_crop(k0, uv0, handle_id, d);
++ let (min_w, min_h) = (6.0_f32.min(b.width()), 6.0_f32.min(b.height()));
++ let clamped = Rect::from_min_max(
++ pos2(new_screen.min.x.clamp(b.min.x, b.max.x - min_w), new_screen.min.y.clamp(b.min.y, b.max.y - min_h)),
++ pos2(new_screen.max.x.clamp(b.min.x + min_w, b.max.x), new_screen.max.y.clamp(b.min.y + min_h, b.max.y)),
++ );
++ mode.keep = user_box(xf, info, page, clamped);
++ } else {
++ let shift = egui::vec2(d.x.clamp(b.min.x - k0.min.x, b.max.x - k0.max.x), d.y.clamp(b.min.y - k0.min.y, b.max.y - k0.max.y));
++ mode.keep = user_box(xf, info, page, Rect::from_min_max(k0.min + shift, k0.max + shift));
++ }
++ if resp.drag_stopped() || !ptr.primary_down {
++ mode.drag = None;
++ }
++ }
++
++ // Faint ghost outline of the full uncropped image underneath
++ painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.0, Color32::from_rgba_unmultiplied(200, 200, 200, 80)), egui::StrokeKind::Outside);
++
++ // Dim outer excluded bands
++ let dim = Color32::from_black_alpha(96);
++ if k.min.y > b.min.y {
++ painter.rect_filled(Rect::from_min_max(b.min, Pos2::new(b.max.x, k.min.y)), CornerRadius::ZERO, dim);
++ }
++ if k.max.y < b.max.y {
++ painter.rect_filled(Rect::from_min_max(Pos2::new(b.min.x, k.max.y), b.max), CornerRadius::ZERO, dim);
++ }
++ if k.min.x > b.min.x {
++ painter.rect_filled(Rect::from_min_max(Pos2::new(b.min.x, k.min.y), Pos2::new(k.min.x, k.max.y)), CornerRadius::ZERO, dim);
++ }
++ if k.max.x < b.max.x {
++ painter.rect_filled(Rect::from_min_max(Pos2::new(k.max.x, k.min.y), Pos2::new(b.max.x, k.max.y)), CornerRadius::ZERO, dim);
++ }
++
++ // Rule-of-Thirds Grid inside crop area
++ draw_rule_of_thirds(painter, k, Stroke::new(1.0, CROP_ACCENT.gamma_multiply(0.45)));
++
++ // Crop border stroke
++ painter.rect_stroke(k, CornerRadius::ZERO, Stroke::new(1.5, CROP_ACCENT), egui::StrokeKind::Outside);
++
++ // Crop handles
++ for (idx, h) in crop_handles(k).iter().enumerate() {
++ let is_active = mode.drag.as_ref().is_some_and(|d| d.0 == idx);
++ let is_hovered = hover_pos.is_some_and(|p| p.distance(*h) < 10.0);
++ draw_handle(painter, *h, is_active, is_hovered, true, CROP_ACCENT);
++ }
++
++ // Action pills
++ let font = egui::FontId::proportional(12.5);
++ let done_w = painter.layout_no_wrap(tl!("Done").to_string(), font.clone(), Color32::WHITE).size().x + 22.0;
++ let cancel_w = painter.layout_no_wrap(tl!("Cancel").to_string(), font.clone(), Color32::WHITE).size().x + 22.0;
++ let is_full_rect = (mode.keep[0] - image.rect[0]).abs() < 1e-3
++ && (mode.keep[1] - image.rect[1]).abs() < 1e-3
++ && (mode.keep[2] - image.rect[2]).abs() < 1e-3
++ && (mode.keep[3] - image.rect[3]).abs() < 1e-3;
++ let has_crop = !is_full_rect || image.visible_rect.is_some_and(|v| v != image.rect);
++ let reset_w =
++ if has_crop { Some(painter.layout_no_wrap(tl!("Remove Crop").to_string(), font.clone(), Color32::WHITE).size().x + 22.0) } else { None };
++ let spacing = 6.0;
++ let total_w = done_w + spacing + cancel_w + reset_w.map_or(0.0, |w| w + spacing);
++
++ // Position pills below crop box if there's room, otherwise above
++ let y = if k.max.y + 32.0 < xf.rect.max.y {
++ k.max.y + 6.0
++ } else if k.min.y - 28.0 > xf.rect.min.y {
++ k.min.y - 28.0
++ } else {
++ k.max.y + 6.0
++ };
++
++ // Right-align pills with k.max.x, clamped inside the page bounds
++ let mut right = k.max.x.min(xf.rect.max.x - 8.0);
++ if right - total_w < xf.rect.min.x + 8.0 {
++ right = (xf.rect.min.x + 8.0 + total_w).min(xf.rect.max.x - 8.0);
++ }
++
++ let done = Rect::from_min_size(Pos2::new(right - done_w, y), egui::vec2(done_w, 22.0));
++ let cancel = Rect::from_min_size(Pos2::new(done.min.x - spacing - cancel_w, y), egui::vec2(cancel_w, 22.0));
++ let reset = reset_w.map(|w| Rect::from_min_size(Pos2::new(cancel.min.x - spacing - w, y), egui::vec2(w, 22.0)));
++
++ let done_id = ui.scope_id().with("crop_pill_done").with(page).with(i);
++ let cancel_id = ui.scope_id().with("crop_pill_cancel").with(page).with(i);
++ let reset_id = ui.scope_id().with("crop_pill_reset").with(page).with(i);
++
++ let done_resp = ui.interact(done, done_id, egui::Sense::click());
++ let cancel_resp = ui.interact(cancel, cancel_id, egui::Sense::click());
++ let reset_resp = reset.map(|r| ui.interact(r, reset_id, egui::Sense::click()));
++
++ let t = crate::theme::Tokens::get(ui.ctx());
++
++ // Done button (accent colored)
++ let done_bg = if done_resp.is_pointer_button_down_on() {
++ ACCENT.gamma_multiply(0.8)
++ } else if done_resp.hovered() {
++ Color32::from_rgb(40, 145, 255)
++ } else {
++ ACCENT
++ };
++ painter.rect_filled(done, CornerRadius::same(11), done_bg);
++ painter.text(done.center(), egui::Align2::CENTER_CENTER, tl!("Done"), font.clone(), Color32::WHITE);
++
++ // Cancel button (panel style with border)
++ let cancel_bg = if cancel_resp.is_pointer_button_down_on() {
++ t.pressed
++ } else if cancel_resp.hovered() {
++ t.hover
++ } else {
++ t.card
++ };
++ let cancel_stroke = Stroke::new(1.0, if cancel_resp.hovered() { ACCENT } else { t.border });
++ let cancel_text = if cancel_resp.hovered() { ACCENT } else { t.text };
++ painter.rect(cancel, CornerRadius::same(11), cancel_bg, cancel_stroke, egui::StrokeKind::Inside);
++ painter.text(cancel.center(), egui::Align2::CENTER_CENTER, tl!("Cancel"), font.clone(), cancel_text);
++
++ // Reset button (panel style with border)
++ if let (Some(r), Some(r_resp)) = (reset, reset_resp.as_ref()) {
++ let reset_bg = if r_resp.is_pointer_button_down_on() {
++ t.pressed
++ } else if r_resp.hovered() {
++ t.hover
++ } else {
++ t.card
++ };
++ let reset_stroke = Stroke::new(1.0, if r_resp.hovered() { ACCENT } else { t.border });
++ let reset_text = if r_resp.hovered() { ACCENT } else { t.text };
++ painter.rect(r, CornerRadius::same(11), reset_bg, reset_stroke, egui::StrokeKind::Inside);
++ painter.text(r.center(), egui::Align2::CENTER_CENTER, tl!("Remove Crop"), font.clone(), reset_text);
++ }
++
++ if let Some(p) = hover_pos {
++ let icon = if done_resp.hovered()
++ || cancel_resp.hovered()
++ || reset_resp.as_ref().is_some_and(|r| r.hovered())
++ || done.contains(p)
++ || cancel.contains(p)
++ || reset.is_some_and(|r| r.contains(p))
++ {
++ egui::CursorIcon::PointingHand
++ } else if let Some(g) = crop_handles(k).iter().position(|h| h.distance(p) < 8.0) {
++ match g {
++ 0 => HandleId::TopLeft.cursor_icon(),
++ 1 => HandleId::TopRight.cursor_icon(),
++ 2 => HandleId::BottomLeft.cursor_icon(),
++ 3 => HandleId::BottomRight.cursor_icon(),
++ 4 => HandleId::TopMid.cursor_icon(),
++ 5 => HandleId::RightMid.cursor_icon(),
++ 6 => HandleId::BottomMid.cursor_icon(),
++ _ => HandleId::LeftMid.cursor_icon(),
++ }
++ } else if k.contains(p) {
++ if mode.drag.is_some() { egui::CursorIcon::Grabbing } else { egui::CursorIcon::Grab }
++ } else {
++ egui::CursorIcon::Default
++ };
++ view.cursor.claim(Layer::Editor, icon);
++ }
++
++ let keys = |key: egui::Key| allow_interaction && ui.input(|inp| inp.key_pressed(key)) && !ui.ctx().egui_wants_keyboard_input();
++ let done_clicked = allow_interaction && (done_resp.clicked() || (resp.clicked() && hover_pos.is_some_and(|p| done.contains(p))));
++ let cancel_clicked = allow_interaction && (cancel_resp.clicked() || (resp.clicked() && hover_pos.is_some_and(|p| cancel.contains(p))));
++ let reset_clicked = allow_interaction
++ && reset
++ .is_some_and(|r| reset_resp.as_ref().is_some_and(|res| res.clicked()) || (resp.clicked() && hover_pos.is_some_and(|p| r.contains(p))));
++
++ if reset_clicked {
++ mode.keep = image.rect;
++ mode.drag = None;
++ if let Some(s) = view.image_selection.as_mut() {
++ s.crop_rect = None;
++ }
++ } else if done_clicked || keys(egui::Key::Enter) {
++ let keep = mode.keep;
++ if let Some(s) = view.image_selection.as_mut() {
++ s.crop = None;
++ s.crop_mode = false;
++ s.crop_rect = None;
++ }
++ let is_full = (keep[0] - image.rect[0]).abs() < 1e-3
++ && (keep[1] - image.rect[1]).abs() < 1e-3
++ && (keep[2] - image.rect[2]).abs() < 1e-3
++ && (keep[3] - image.rect[3]).abs() < 1e-3;
++ if is_full {
++ if image.visible_rect.is_some_and(|v| v != image.rect) {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::RemoveCrop });
++ }
++ } else if image.visible_rect != Some(keep) {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Crop(keep) });
++ }
++ } else if (cancel_clicked || keys(egui::Key::Escape))
++ && let Some(s) = view.image_selection.as_mut()
++ {
++ s.crop = None;
++ s.crop_mode = false;
++ s.crop_rect = None;
++ }
++ true
++}
++
++/// The page raster's picture of the box `from`, stretched into `to`, over blanks at `blank` (where
++/// the screen may still show the picture).
++fn stretched_picture(painter: &egui::Painter, xf: &PageXform, tex: egui::TextureId, blank: &[Rect], from: Rect, to: Rect) {
++ let clipped = painter.with_clip_rect(painter.clip_rect().intersect(xf.rect));
++ for r in blank {
++ clipped.rect_filled(*r, CornerRadius::ZERO, Color32::WHITE);
++ }
++ let mut mesh = egui::Mesh::with_texture(tex);
++ for (a, b) in [
++ (from.left_top(), to.left_top()),
++ (from.right_top(), to.right_top()),
++ (from.right_bottom(), to.right_bottom()),
++ (from.left_bottom(), to.left_bottom()),
++ ] {
++ let (u, v) = xf.screen_to_norm(a);
++ mesh.vertices.push(egui::epaint::Vertex { pos: b, uv: Pos2::new(u, v), color: Color32::WHITE });
++ }
++ mesh.add_triangle(0, 1, 2);
++ mesh.add_triangle(0, 2, 3);
++ clipped.add(egui::Shape::mesh(mesh));
++}
++
++/// Once a selected corner or the rotate knob is grabbed, text under it cannot take over the gesture. The same
++/// applies after the pointer leaves the image/page while dragging.
++#[allow(dead_code)]
++pub(crate) fn image_owns_pointer(_ui: &egui::Ui, xf: &PageXform, ptr: &PagePointer, page: usize, info: &DocInfo, view: &DocView) -> bool {
++ let Some(selection) = view.image_selection.as_ref().filter(|s| s.page == page) else { return false };
++ if selection.drag.is_some() || selection.crop.is_some() || selection.crop_mode {
++ return true;
++ }
++ let Some(image) = view.edit_images.get(&page).and_then(|(_, images)| images.get(selection.index)) else { return false };
++ let obb = ImageObb::from_image(image, xf, info, page);
++ let p = if ptr.primary_down { ptr.origin.or(ptr.pos) } else { ptr.pos };
++ let Some(p) = p else { return false };
++
++ obb.handles().iter().any(|(_, h)| h.distance(p) < 8.0) || obb.s_rot.distance(p) < 8.0
++}
++
++/// The selected picture's grips under `p`: its corners and its rotate knob. They are asked after
++/// the paragraphs, so text on top of a picture cannot take them; `clip` is the pages' visible
++/// area, which the knob stays inside.
++#[allow(clippy::too_many_arguments)]
++pub(crate) fn handle_hit(
++ clip: Rect,
++ xf: &PageXform,
++ page: usize,
++ info: &DocInfo,
++ images: &[pdfcraft_engine::PageImage],
++ view: &DocView,
++ p: Pos2,
++) -> Option<Hit> {
++ let s = view.image_selection.as_ref().filter(|s| s.page == page && s.crop.is_none())?;
++ let b = images.get(s.index)?.visible_rect.map(|r| xf.user_rect(info, page, r.map(|v| v as f32)))?;
++ let picture = |part| Owner::Picture { index: s.index, part };
++ if rotate_knob(b, clip).0.distance(p) < 8.0 {
++ return Some(Hit::new(picture(PicturePart::Knob), Layer::Handle, egui::CursorIcon::Grab));
++ }
++ let corner = [b.left_top(), b.right_top(), b.left_bottom(), b.right_bottom()].iter().position(|c| c.distance(p) < 8.0)?;
++ let icon = if matches!(corner, 0 | 3) { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw };
++ Some(Hit::new(picture(PicturePart::Corner(corner)), Layer::Handle, icon))
++}
++
++/// The picture under `p` (its shape, not only its box): a click selects it, a drag moves the
++/// selected one. On the text layer, asked before the paragraphs: editable text on top of a
++/// picture takes the press.
++pub(crate) fn body_hit(xf: &PageXform, page: usize, info: &DocInfo, images: &[pdfcraft_engine::PageImage], view: &DocView, p: Pos2) -> Option<Hit> {
++ if !xf.rect.contains(p) {
++ return None;
++ }
++ let (x, y) = xf.screen_to_view(p);
++ let user = info.pages.get(page)?.view_to_user(x, y).map(f64::from);
++ let index = images
++ .iter()
++ .rposition(|im| im.visible_rect.is_some_and(|r| xf.user_rect(info, page, r.map(|v| v as f32)).contains(p)) && im.contains(user))?;
++ let selected = view.image_selection.as_ref().is_some_and(|s| s.page == page && s.index == index);
++ let icon = if selected { egui::CursorIcon::Move } else { egui::CursorIcon::PointingHand };
++ Some(Hit::new(Owner::Picture { index, part: PicturePart::Body }, Layer::Text, icon))
++}
++
++/// What a right-click on a selected image asks for.
++#[derive(Clone, Copy, Debug, PartialEq)]
++pub enum ImageAction {
++ Replace(usize, usize),
++ Save(usize, usize),
++ Copy(usize, usize),
++ CopyFile(usize, usize),
++ /// Copy the picture, then remove it from the page.
++ Cut(usize, usize),
++ /// Mark the picture for redaction (applied later from Redact a PDF).
++ Redact(usize, usize),
++ /// Create a link over the picture.
++ Link(usize, usize),
++ /// Open Recognize Text for the picture's page.
++ Recognize(usize),
++ Paste(usize, Option<[f64; 2]>),
++ DrawFreehand,
++ AddComment(usize, usize),
++ Crop,
++}
++
++/// Where the rotate knob sits, and the edge point its stem starts from: above the middle of the
++/// top edge, or below the bottom edge when the area on screen (`view`) has no room above.
++pub(super) fn rotate_knob(b: Rect, view: Rect) -> (Pos2, Pos2) {
++ let above = b.center_top() - egui::vec2(0.0, 22.0);
++ if above.y - 6.0 >= view.top() { (above, b.center_top()) } else { (b.center_bottom() + egui::vec2(0.0, 22.0), b.center_bottom()) }
++}
++
++/// Degrees clockwise (-180..=180) that turn "straight up" from `centre` onto the pointer; `snap`
++/// rounds to steps of 15 degrees (Shift). A pointer on the centre turns nothing.
++pub(super) fn turn_degrees(centre: Pos2, pointer: Pos2, snap: bool) -> f32 {
++ let v = pointer - centre;
++ if !(v.x.is_finite() && v.y.is_finite()) || v.length() < 4.0 {
++ return 0.0;
++ }
++ // Screen y points down: up is (0, -1); clockwise is atan2(x, -y).
++ let d = v.x.atan2(-v.y).to_degrees();
++ if snap { (d / 15.0).round() * 15.0 } else { d }
++}
++
++#[doc(hidden)]
++pub fn image_menu_id() -> egui::Id {
++ egui::Id::unique("image-menu")
++}
++
++/// Images on a page: select, move, resize, right-click. Returns `true` when the pointer was used.
++#[allow(clippy::cognitive_complexity)]
++pub(crate) fn image_input(
++ cx: &ImageCx<'_>,
++ images: &[pdfcraft_engine::PageImage],
++ view: &mut DocView,
++ action: &mut Option<ImageAction>,
++) -> bool {
++ let ImageCx { ui, resp, xf, ptr, page, info, allow_interaction } = *cx;
++ let _ = &action;
++ let boxes: Vec<Rect> = images.iter().map(|im| crate::canvas::image_screen_rect(im, xf, info, page)).collect();
++ let painter = ui.painter();
++ let hover_pos = ptr.pos;
++ let image_contains = |index: usize, point: Pos2| {
++ let (x, y) = xf.screen_to_view(point);
++ let Some(p) = info.pages.get(page) else { return false };
++ let user = p.view_to_user(x, y);
++ images.get(index).is_some_and(|im| im.contains(user.map(f64::from)))
++ };
++ let selected = view.image_selection.as_ref().filter(|s| s.page == page).map(|s| s.index).filter(|i| *i < boxes.len());
++ let dragging = view.image_selection.as_ref().is_some_and(|s| s.drag.is_some());
++ let faint_all = boxes.len() <= MAX_OUTLINES;
++ let moving = selected.filter(|_| dragging);
++ if faint_all {
++ for (idx, im) in images.iter().enumerate() {
++ if moving == Some(idx) {
++ continue;
++ }
++ if let Some(b) = boxes.get(idx).filter(|b| b.is_positive()) {
++ let obb = ImageObb::from_image(im, xf, info, page);
++ if obb.is_rotated {
++ let pts = obb.corners();
++ for k in 0..4 {
++ painter.line_segment([pts[k], pts[(k + 1) % 4]], Stroke::new(0.75, ACCENT.gamma_multiply(0.35)));
++ }
++ } else {
++ painter.rect_stroke(*b, CornerRadius::ZERO, Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
++ }
++ }
++ }
++ }
++ let hovered_box = hover_pos.and_then(|p| {
++ boxes.iter().enumerate().rposition(|(i, b)| {
++ b.is_positive()
++ && images.get(i).is_some_and(|img| b.contains(p) || quad_contains(ImageObb::from_image(img, xf, info, page).corners(), p))
++ && image_contains(i, p)
++ })
++ });
++ if !faint_all {
++ for i in selected.filter(|_| moving.is_none()).into_iter().chain(hovered_box.filter(|h| Some(*h) != selected)) {
++ if let (Some(b), Some(img)) = (boxes.get(i).filter(|b| b.is_positive()), images.get(i)) {
++ let obb = ImageObb::from_image(img, xf, info, page);
++ if obb.is_rotated {
++ let pts = obb.corners();
++ for k in 0..4 {
++ painter.line_segment([pts[k], pts[(k + 1) % 4]], Stroke::new(0.75, ACCENT.gamma_multiply(0.35)));
++ }
++ } else {
++ painter.rect_stroke(*b, CornerRadius::ZERO, Stroke::new(0.75, ACCENT.gamma_multiply(0.35)), egui::StrokeKind::Outside);
++ }
++ }
++ }
++ }
++ for i in selected.into_iter().chain(hovered_box).collect::<Vec<_>>() {
++ let (Some(b), Some(img)) = (boxes.get(i).copied().filter(|b| b.is_positive()), images.get(i)) else { continue };
++ let obb = ImageObb::from_image(img, xf, info, page);
++ if selected == Some(i) {
++ if !dragging {
++ if obb.is_rotated {
++ painter.polygon(obb.corners().to_vec(), ACCENT.gamma_multiply(0.04), Stroke::NONE);
++ } else {
++ painter.rect_filled(b, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
++ }
++ }
++ } else if obb.is_rotated {
++ painter.polygon(obb.corners().to_vec(), Color32::TRANSPARENT, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)));
++ } else {
++ painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)), egui::StrokeKind::Outside);
++ }
++ }
++ // The selected image: frame, corner handles, dragging.
++ if let Some(i) = selected.filter(|i| boxes.get(*i).is_some_and(|b| b.is_positive())) {
++ let Some(b) = boxes.get(i).copied() else { return false };
++ let Some(image) = images.get(i) else { return false };
++ let obb = ImageObb::from_image(image, xf, info, page);
++ let (out_of_date, raster_old, raster) = (view.page_out_of_date(page), view.page_raster_out_of_date(page), view.page_raster(page));
++ let screen = |r: Rect| Rect::from_two_pos(xf.norm_to_screen(r.min.x, r.min.y), xf.norm_to_screen(r.max.x, r.max.y));
++ let left_at = view.image_selection.as_ref().and_then(|s| s.settled).filter(|_| out_of_date).map(|(from, _)| from);
++ if let Some(s) = view.image_selection.as_mut().filter(|s| s.drag.is_none()) {
++ match (s.settled, raster) {
++ (Some((from, to)), Some(tex)) if out_of_date => {
++ let source = if raster_old { from } else { to };
++ stretched_picture(painter, xf, tex, &[screen(from)], screen(source), screen(to));
++ }
++ _ if !out_of_date => s.settled = None,
++ _ => {}
++ }
++ }
++
++ if view.image_selection.as_ref().is_some_and(|s| s.crop.is_some() || s.crop_mode) {
++ if let Some(s) = view.image_selection.as_mut()
++ && s.crop.is_none()
++ {
++ let rect = images.get(i).and_then(|im| im.visible_rect).unwrap_or(images.get(i).map_or([0.0; 4], |im| im.rect));
++ s.crop = Some(CropMode::new(rect));
++ }
++ return crop_input(cx, view, i, image);
++ }
++ let (knob, stem) = rotate_knob(b, ui.clip_rect());
++ if view.image_selection.as_ref().is_some_and(|s| s.drag.is_none()) {
++ if obb.is_rotated {
++ painter.polygon(obb.corners().to_vec(), Color32::TRANSPARENT, Stroke::new(1.5, ACCENT));
++ painter.line_segment([obb.s_tc, obb.s_rot], Stroke::new(1.2, ACCENT));
++ painter.circle(obb.s_rot, 4.5, Color32::WHITE, Stroke::new(1.0, ACCENT));
++ } else {
++ painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
++ painter.line_segment([stem, knob], Stroke::new(1.0, ACCENT));
++ painter.circle(knob, 5.0, Color32::WHITE, Stroke::new(1.0, ACCENT));
++ }
++ for (_, h) in obb.handles() {
++ let is_hovered = hover_pos.is_some_and(|p| p.distance(h) < 10.0);
++ draw_handle(painter, h, false, is_hovered, false, ACCENT);
++ }
++ }
++
++ let mut handle_drag = None;
++ for (h, h_pos, handle_id) in obb.handles_with_ids(ui.scope_id().with("img_handle")) {
++ let handle_rect = Rect::from_center_size(h_pos, egui::vec2(18.0, 18.0));
++ let handle_resp = ui.interact(handle_rect, handle_id, egui::Sense::drag());
++ if allow_interaction && handle_resp.drag_started() {
++ handle_drag = Some(h);
++ }
++ }
++ let rot_id = ui.scope_id().with("img_handle").with(8);
++ let rot_rect = Rect::from_center_size(if obb.is_rotated { obb.s_rot } else { knob }, egui::vec2(12.0, 12.0));
++ let rot_resp = ui.interact(rot_rect, rot_id, egui::Sense::drag());
++ let rot_drag = allow_interaction && rot_resp.drag_started();
++
++ if allow_interaction && let Some(p) = hover_pos {
++ if obb.s_rot.distance(p) < 8.0 || knob.distance(p) < 8.0 {
++ view.cursor.claim(Layer::Handle, egui::CursorIcon::Grab);
++ } else if let Some((h, _)) = obb.handles().iter().find(|(_, pos)| pos.distance(p) < 8.0) {
++ let icon = match h {
++ ImageHandle::TopLeft | ImageHandle::BottomRight => {
++ if obb.is_rotated {
++ let v = obb.handle_pos(*h) - obb.screen_center;
++ if v.x * v.y > 0.0 { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw }
++ } else {
++ egui::CursorIcon::ResizeNwSe
++ }
++ }
++ ImageHandle::TopRight | ImageHandle::BottomLeft => {
++ if obb.is_rotated {
++ let v = obb.handle_pos(*h) - obb.screen_center;
++ if v.x * v.y > 0.0 { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw }
++ } else {
++ egui::CursorIcon::ResizeNeSw
++ }
++ }
++ ImageHandle::TopCenter | ImageHandle::BottomCenter => {
++ if obb.is_rotated && (obb.v_axis.0.abs() > obb.v_axis.1.abs()) {
++ egui::CursorIcon::ResizeHorizontal
++ } else {
++ egui::CursorIcon::ResizeVertical
++ }
++ }
++ ImageHandle::RightCenter | ImageHandle::LeftCenter => {
++ if obb.is_rotated && (obb.u_axis.1.abs() > obb.u_axis.0.abs()) {
++ egui::CursorIcon::ResizeVertical
++ } else {
++ egui::CursorIcon::ResizeHorizontal
++ }
++ }
++ };
++ view.cursor.claim(Layer::Handle, icon);
++ }
++ }
++
++ let origin = ptr.origin;
++ if allow_interaction {
++ if let Some(h) = handle_drag {
++ if let Some(s) = view.image_selection.as_mut() {
++ let o = origin.unwrap_or_else(|| obb.handle_pos(h));
++ s.drag = Some(ImageDrag { start: o, target: DragTarget::Handle(h), initial_rect: b, initial_matrix: image.matrix });
++ }
++ } else if rot_drag {
++ if let Some(s) = view.image_selection.as_mut() {
++ let o = origin.unwrap_or(if obb.is_rotated { obb.s_rot } else { knob });
++ s.drag = Some(ImageDrag { start: o, target: DragTarget::Rotate, initial_rect: b, initial_matrix: image.matrix });
++ s.turning = true;
++ }
++ } else if resp.drag_started()
++ && let Some(o) = origin
++ {
++ if obb.s_rot.distance(o) < 8.0 || knob.distance(o) < 8.0 {
++ if let Some(s) = view.image_selection.as_mut() {
++ s.drag = Some(ImageDrag { start: o, target: DragTarget::Rotate, initial_rect: b, initial_matrix: image.matrix });
++ s.turning = true;
++ }
++ } else if let Some((h, _)) = obb.handles().iter().find(|(_, pos)| pos.distance(o) < 8.0) {
++ if let Some(s) = view.image_selection.as_mut() {
++ s.drag = Some(ImageDrag { start: o, target: DragTarget::Handle(*h), initial_rect: b, initial_matrix: image.matrix });
++ }
++ } else if (image_contains(i, o) || quad_contains(obb.corners(), o))
++ && let Some(s) = view.image_selection.as_mut()
++ {
++ s.drag = Some(ImageDrag { start: o, target: DragTarget::Move, initial_rect: b, initial_matrix: image.matrix });
++ }
++ }
++ }
++
++ if view.image_selection.as_ref().is_some_and(|s| s.turning) {
++ let shift = ui.input(|i| i.modifiers.shift);
++ let degrees = hover_pos.map_or(0.0, |p| turn_degrees(b.center(), p, shift));
++ let centre = b.center();
++ let (sin, cos) = degrees.to_radians().sin_cos();
++ let turn = |c: Pos2| {
++ let v = c - centre;
++ centre + egui::vec2(v.x * cos - v.y * sin, v.x * sin + v.y * cos)
++ };
++ let outline = [b.left_top(), b.right_top(), b.right_bottom(), b.left_bottom()].map(turn).to_vec();
++ painter.add(egui::Shape::closed_line(outline, Stroke::new(1.5, ACCENT)));
++ let galley = painter.layout_no_wrap(format!("{degrees:.0}\u{b0}"), egui::FontId::proportional(12.0), ACCENT);
++ let label = Rect::from_center_size(turn(knob) - egui::vec2(0.0, 14.0), galley.size() + egui::vec2(8.0, 4.0));
++ painter.rect(label, CornerRadius::same(3), Color32::WHITE, Stroke::new(1.0, ACCENT), egui::StrokeKind::Middle);
++ painter.galley(label.center() - galley.size() / 2.0, galley, ACCENT);
++ view.cursor.claim(Layer::Drag, egui::CursorIcon::Grabbing);
++ if resp.drag_stopped() || !ptr.primary_down {
++ if let Some(s) = view.image_selection.as_mut() {
++ s.drag = None;
++ s.turning = false;
++ }
++ if degrees.abs() >= 0.05 {
++ view.pending_edit =
++ Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::RotateBy(f64::from(degrees)) });
++ }
++ }
++ return true;
++ }
++
++ if let Some(drag) = view.image_selection.as_ref().and_then(|s| s.drag)
++ && let Some(p) = hover_pos
++ {
++ if !obb.is_rotated && !matches!(drag.target, DragTarget::Rotate) {
++ let preview = match drag.target {
++ DragTarget::Handle(handle) => {
++ let handle_id = HandleId::from(handle);
++ let delta = p - drag.start;
++ let is_corner = matches!(handle_id, HandleId::TopLeft | HandleId::TopRight | HandleId::BottomLeft | HandleId::BottomRight);
++ let shift = ui.input(|inp| inp.modifiers.shift);
++ if is_corner && !shift {
++ apply_resize_proportional(drag.initial_rect, handle_id, delta)
++ } else {
++ apply_resize(drag.initial_rect, handle_id, delta)
++ }
++ }
++ DragTarget::Move => drag.initial_rect.translate(p - drag.start),
++ _ => drag.initial_rect,
++ };
++
++ let raw = images.get(i).map_or(b, |im| xf.user_rect(info, page, im.rect.map(|v| v as f32)));
++ let ratio = |new: f32, old: f32| if old.abs() > 1e-3 && (new / old).is_finite() { new / old } else { 1.0 };
++ let factor = egui::vec2(ratio(preview.width(), b.width()), ratio(preview.height(), b.height()));
++ let f_min = egui::vec2((raw.min.x - b.min.x) * factor.x, (raw.min.y - b.min.y) * factor.y);
++ let f_max = egui::vec2((raw.max.x - b.min.x) * factor.x, (raw.max.y - b.min.y) * factor.y);
++ let target = Rect::from_min_max(preview.min + f_min, preview.min + f_max);
++ let visible = preview.intersect(xf.rect);
++
++ if visible.is_positive() {
++ if preview != b
++ && let Some(tex) = raster
++ {
++ let source = left_at.filter(|_| raster_old).map_or(b, screen);
++ let blank: Vec<Rect> = left_at.map(screen).into_iter().chain(std::iter::once(b)).collect();
++ stretched_picture(painter, xf, tex, &blank, source, preview);
++ }
++ painter.rect_filled(visible, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
++ painter.rect_stroke(visible, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
++ for h in [
++ preview.left_top(),
++ preview.right_top(),
++ preview.left_bottom(),
++ preview.right_bottom(),
++ preview.center_top(),
++ preview.right_center(),
++ preview.center_bottom(),
++ preview.left_center(),
++ ] {
++ draw_handle(painter, h, false, false, false, ACCENT);
++ }
++ }
++
++ if resp.drag_stopped() || !ptr.primary_down {
++ if let Some(s) = view.image_selection.as_mut() {
++ s.drag = None;
++ let norm = |r: Rect| {
++ let ((u0, v0), (u1, v1)) = (xf.screen_to_norm(r.min), xf.screen_to_norm(r.max));
++ Rect::from_two_pos(Pos2::new(u0, v0), Pos2::new(u1, v1))
++ };
++ s.settled = (preview != b).then(|| (left_at.unwrap_or_else(|| norm(b)), norm(preview)));
++ }
++ if preview != b {
++ view.pending_edit =
++ Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Move(user_box(xf, info, page, target)) });
++ }
++ }
++
++ let icon = match drag.target {
++ DragTarget::Handle(handle) => HandleId::from(handle).cursor_icon(),
++ DragTarget::Move => egui::CursorIcon::Grabbing,
++ _ => egui::CursorIcon::Move,
++ };
++ view.cursor.claim(Layer::Drag, icon);
++ return true;
++ }
++ let m_init = Matrix(drag.initial_matrix);
++ let [a0, b0, c0, d0, e0, f0] = drag.initial_matrix;
++ let w0 = (a0 * a0 + b0 * b0).sqrt().max(1e-6);
++ let h0 = (c0 * c0 + d0 * d0).sqrt().max(1e-6);
++ let u_axis = (a0 / w0, b0 / w0);
++ let v_axis = (c0 / h0, d0 / h0);
++ let center_user = (e0 + 0.5 * (a0 + c0), f0 + 0.5 * (b0 + d0));
++
++ let (sx, sy) = screen_to_user(xf, info, page, drag.start);
++ let (px, py) = screen_to_user(xf, info, page, p);
++
++ let (m_new, cursor_icon) = match drag.target {
++ DragTarget::Rotate => {
++ let v0 = (sx - center_user.0, sy - center_user.1);
++ let v1 = (px - center_user.0, py - center_user.1);
++ let theta0 = v0.1.atan2(v0.0);
++ let theta1 = v1.1.atan2(v1.0);
++ let mut delta = theta1 - theta0;
++ if ui.input(|i| i.modifiers.shift) {
++ let step = std::f64::consts::PI / 12.0;
++ delta = (delta / step).round() * step;
++ }
++ let (cos_d, sin_d) = (delta.cos(), delta.sin());
++ let t_rot = [
++ cos_d,
++ sin_d,
++ -sin_d,
++ cos_d,
++ center_user.0 * (1.0 - cos_d) + center_user.1 * sin_d,
++ center_user.1 * (1.0 - cos_d) - center_user.0 * sin_d,
++ ];
++ (m_init.then(&Matrix(t_rot)), egui::CursorIcon::Grab)
++ }
++ DragTarget::Move => {
++ let (dx, dy) = (px - sx, py - sy);
++ (Matrix([a0, b0, c0, d0, e0 + dx, f0 + dy]), egui::CursorIcon::Move)
++ }
++ DragTarget::Handle(handle) => {
++ let p00 = (e0, f0);
++ let p10 = (e0 + a0, f0 + b0);
++ let p11 = (e0 + a0 + c0, f0 + b0 + d0);
++ let p01 = (e0 + c0, f0 + d0);
++ let p_curr = (px, py);
++
++ let (new_w, new_h, p00_new, icon) = match handle {
++ ImageHandle::TopRight => {
++ let anchor = p00;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let du = d.0 * u_axis.0 + d.1 * u_axis.1;
++ let dv = d.0 * v_axis.0 + d.1 * v_axis.1;
++ let su = du / w0;
++ let sv = dv / h0;
++ let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
++ let s = s.max(5.0 / w0).max(5.0 / h0);
++ (w0 * s, h0 * s, anchor, egui::CursorIcon::ResizeNeSw)
++ }
++ ImageHandle::TopLeft => {
++ let anchor = p10;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let du = d.0 * (-u_axis.0) + d.1 * (-u_axis.1);
++ let dv = d.0 * v_axis.0 + d.1 * v_axis.1;
++ let su = du / w0;
++ let sv = dv / h0;
++ let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
++ let s = s.max(5.0 / w0).max(5.0 / h0);
++ let nw = w0 * s;
++ let nh = h0 * s;
++ (nw, nh, (anchor.0 - nw * u_axis.0, anchor.1 - nw * u_axis.1), egui::CursorIcon::ResizeNwSe)
++ }
++ ImageHandle::BottomLeft => {
++ let anchor = p11;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let du = d.0 * (-u_axis.0) + d.1 * (-u_axis.1);
++ let dv = d.0 * (-v_axis.0) + d.1 * (-v_axis.1);
++ let su = du / w0;
++ let sv = dv / h0;
++ let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
++ let s = s.max(5.0 / w0).max(5.0 / h0);
++ let nw = w0 * s;
++ let nh = h0 * s;
++ (
++ nw,
++ nh,
++ (anchor.0 - nw * u_axis.0 - nh * v_axis.0, anchor.1 - nw * u_axis.1 - nh * v_axis.1),
++ egui::CursorIcon::ResizeNeSw,
++ )
++ }
++ ImageHandle::BottomRight => {
++ let anchor = p01;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let du = d.0 * u_axis.0 + d.1 * u_axis.1;
++ let dv = d.0 * (-v_axis.0) + d.1 * (-v_axis.1);
++ let su = du / w0;
++ let sv = dv / h0;
++ let s = if (su - 1.0).abs() >= (sv - 1.0).abs() { su } else { sv };
++ let s = s.max(5.0 / w0).max(5.0 / h0);
++ let nw = w0 * s;
++ let nh = h0 * s;
++ (nw, nh, (anchor.0 - nh * v_axis.0, anchor.1 - nh * v_axis.1), egui::CursorIcon::ResizeNwSe)
++ }
++ ImageHandle::RightCenter => {
++ let anchor = p00;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let du = (d.0 * u_axis.0 + d.1 * u_axis.1).max(5.0);
++ (du, h0, anchor, egui::CursorIcon::ResizeHorizontal)
++ }
++ ImageHandle::LeftCenter => {
++ let anchor = p10;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let du = (d.0 * (-u_axis.0) + d.1 * (-u_axis.1)).max(5.0);
++ (du, h0, (anchor.0 - du * u_axis.0, anchor.1 - du * u_axis.1), egui::CursorIcon::ResizeHorizontal)
++ }
++ ImageHandle::TopCenter => {
++ let anchor = p00;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let dv = (d.0 * v_axis.0 + d.1 * v_axis.1).max(5.0);
++ (w0, dv, anchor, egui::CursorIcon::ResizeVertical)
++ }
++ ImageHandle::BottomCenter => {
++ let anchor = p01;
++ let d = (p_curr.0 - anchor.0, p_curr.1 - anchor.1);
++ let dv = (d.0 * (-v_axis.0) + d.1 * (-v_axis.1)).max(5.0);
++ (w0, dv, (anchor.0 - dv * v_axis.0, anchor.1 - dv * v_axis.1), egui::CursorIcon::ResizeVertical)
++ }
++ };
++ let mat = Matrix([new_w * u_axis.0, new_w * u_axis.1, new_h * v_axis.0, new_h * v_axis.1, p00_new.0, p00_new.1]);
++ (mat, icon)
++ }
++ _ => (m_init, egui::CursorIcon::Default),
++ };
++
++ let preview_obb = ImageObb::new(m_new.0, xf, info, page);
++ if let Some(tex) = view.page_raster(page) {
++ let clipped = painter.with_clip_rect(painter.clip_rect().intersect(xf.rect));
++ let mut mesh = egui::Mesh::with_texture(tex);
++ let orig_corners = obb.corners();
++ let new_corners = preview_obb.corners();
++ for (from, to) in [
++ (orig_corners[3], new_corners[3]),
++ (orig_corners[2], new_corners[2]),
++ (orig_corners[1], new_corners[1]),
++ (orig_corners[0], new_corners[0]),
++ ] {
++ let (u, v) = xf.screen_to_norm(from);
++ mesh.vertices.push(egui::epaint::Vertex { pos: to, uv: Pos2::new(u, v), color: Color32::WHITE });
++ }
++ mesh.add_triangle(0, 1, 2);
++ mesh.add_triangle(0, 2, 3);
++ clipped.add(egui::Shape::mesh(mesh));
++ }
++
++ if preview_obb.is_rotated {
++ let pts = preview_obb.corners();
++ painter.polygon(pts.to_vec(), ACCENT.gamma_multiply(0.04), Stroke::new(1.5, ACCENT));
++ } else {
++ let p_rect = Rect::from_two_pos(preview_obb.s01, preview_obb.s10);
++ let visible = p_rect.intersect(xf.rect);
++ if visible.is_positive() {
++ painter.rect_filled(visible, CornerRadius::ZERO, ACCENT.gamma_multiply(0.04));
++ painter.rect_stroke(visible, CornerRadius::ZERO, Stroke::new(1.5, ACCENT), egui::StrokeKind::Outside);
++ }
++ }
++ for (_, h) in preview_obb.handles() {
++ draw_handle(painter, h, false, false, false, ACCENT);
++ }
++
++ if resp.drag_stopped() || !ptr.primary_down {
++ if let Some(s) = view.image_selection.as_mut() {
++ s.drag = None;
++ }
++ if m_new.0 != drag.initial_matrix {
++ if matches!(drag.target, DragTarget::Move) {
++ let (dx, dy) = (px - sx, py - sy);
++ let new_rect = [image.rect[0] + dx, image.rect[1] + dy, image.rect[2] + dx, image.rect[3] + dy];
++ view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Move(new_rect) });
++ } else if let Some(inv) = Matrix(drag.initial_matrix).invert() {
++ let t = inv.then(&m_new);
++ view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Transform(t.0) });
++ }
++ }
++ }
++ view.cursor.claim(Layer::Drag, cursor_icon);
++ return true;
++ }
++ if allow_interaction
++ && ui.input(|inp| inp.key_pressed(egui::Key::Delete) || inp.key_pressed(egui::Key::Backspace))
++ && !ui.ctx().egui_wants_keyboard_input()
++ {
++ view.image_selection = None;
++ view.pending_edit = Some(Edit::EditPageImage { page, index: i, change: pdfcraft_engine::ImageEdit::Delete });
++ return true;
++ }
++ }
++ if !allow_interaction {
++ return false;
++ }
++ let menu_key = image_menu_id();
++ let Some(p) = hover_pos.filter(|p| xf.rect.contains(*p)) else { return false };
++ let Some(hit) = boxes.iter().enumerate().rposition(|(i, b)| {
++ b.is_positive()
++ && images.get(i).is_some_and(|img| b.contains(p) || quad_contains(ImageObb::from_image(img, xf, info, page).corners(), p))
++ && image_contains(i, p)
++ }) else {
++ if resp.secondary_clicked() || resp.clicked() {
++ view.image_selection = None;
++ ui.ctx().data_mut(|d| d.insert_persisted(menu_key, false));
++ }
++ return false;
++ };
++ let Some(hit_img) = images.get(hit) else { return false };
++ let hit_obb = ImageObb::from_image(hit_img, xf, info, page);
++ if selected != Some(hit) {
++ if hit_obb.is_rotated {
++ let pts = hit_obb.corners();
++ for k in 0..4 {
++ painter.line_segment([pts[k], pts[(k + 1) % 4]], Stroke::new(1.5, ACCENT.gamma_multiply(0.7)));
++ }
++ } else if let Some(b) = boxes.get(hit).copied() {
++ painter.rect_stroke(b, CornerRadius::ZERO, Stroke::new(1.5, ACCENT.gamma_multiply(0.7)), egui::StrokeKind::Outside);
++ }
++ }
++ let corner = hit_obb.handles().iter().position(|(_, c)| c.distance(p) < 10.0);
++ let cursor = match (selected == Some(hit), corner) {
++ (true, Some(0 | 4)) => egui::CursorIcon::ResizeNwSe,
++ (true, Some(2 | 6)) => egui::CursorIcon::ResizeNeSw,
++ (true, Some(1 | 5)) => egui::CursorIcon::ResizeVertical,
++ (true, Some(3 | 7)) => egui::CursorIcon::ResizeHorizontal,
++ (true, None) => egui::CursorIcon::Grab,
++ _ => egui::CursorIcon::PointingHand,
++ };
++ view.cursor.claim(Layer::Text, cursor);
++ let clicked = match view.router.owner(page) {
++ Some(Owner::Picture { index, .. }) if resp.clicked() => Some(index),
++ _ => (resp.clicked() || resp.secondary_clicked()).then_some(hit),
++ };
++ if let Some(index) = clicked {
++ view.image_selection =
++ Some(ImageSelection { page, index, drag: None, crop: None, crop_mode: false, crop_rect: None, turning: false, settled: None });
++ }
++ if resp.secondary_clicked() {
++ ui.ctx().data_mut(|d| d.insert_persisted(menu_key, true));
++ }
++ true
++}
++
++/// The right-click context menu for a selected image.
++#[allow(clippy::too_many_arguments)]
++pub(crate) fn image_context_menu(
++ ui: &mut egui::Ui,
++ view: &mut DocView,
++ images: &[pdfcraft_engine::PageImage],
++ page: usize,
++ hit: usize,
++ can_paste: bool,
++ action: &mut Option<ImageAction>,
++ at: Option<[f64; 2]>,
++) {
++ let Some(img) = images.get(hit) else { return };
++ use pdfcraft_engine::ImageEdit as E;
++ let raster = !img.is_form;
++
++ let close = |ui: &mut egui::Ui| {
++ ui.ctx().data_mut(|d| d.insert_persisted(image_menu_id(), false));
++ egui::Popup::close_all(ui.ctx());
++ ui.close();
++ };
++
++ // Top horizontal icon bar: Comment, Crop, Rotate CCW, Rotate CW
++ ui.horizontal(|ui| {
++ ui.spacing_mut().item_spacing.x = 4.0;
++ if crate::icons::button(ui, "message-square", 24.0, false, tl!("Add a sticky note here")).clicked() {
++ *action = Some(ImageAction::AddComment(page, hit));
++ close(ui);
++ }
++ if raster && crate::icons::button(ui, "crop", 24.0, false, tl!("Crop")).clicked() {
++ *action = Some(ImageAction::Crop);
++ if let Some(s) = view.image_selection.as_mut() {
++ let rect = img.visible_rect.unwrap_or(img.rect);
++ s.crop = Some(CropMode::new(rect));
++ s.crop_mode = true;
++ s.crop_rect = None;
++ }
++ close(ui);
++ }
++ if crate::icons::button(ui, "rotate-ccw", 24.0, false, tl!("Rotate Counterclockwise")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Rotate(3) });
++ close(ui);
++ }
++ if crate::icons::button(ui, "rotate-cw", 24.0, false, tl!("Rotate Clockwise")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Rotate(1) });
++ close(ui);
++ }
++ });
++
++ ui.separator();
++
++ if ui.button(tl!("Cut Image")).clicked() {
++ *action = Some(ImageAction::Cut(page, hit));
++ close(ui);
++ }
++ if ui.button(tl!("Copy Image")).clicked() {
++ *action = Some(ImageAction::Copy(page, hit));
++ close(ui);
++ }
++ if ui.add_enabled(can_paste, egui::Button::new(tl!("Paste"))).clicked() {
++ *action = Some(ImageAction::Paste(page, at));
++ close(ui);
++ }
++
++ ui.separator();
++
++ let is_cropped = img.visible_rect.is_some() && img.visible_rect != Some(img.rect);
++ if raster && ui.add_enabled(is_cropped, egui::Button::new(tl!("Remove crop"))).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::RemoveCrop });
++ close(ui);
++ }
++
++ if ui.button(tl!("Flip horizontal")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Flip { horizontal: true } });
++ close(ui);
++ }
++ if ui.button(tl!("Flip vertical")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Flip { horizontal: false } });
++ close(ui);
++ }
++
++ ui.menu_button(tl!("Arrange image"), |ui| {
++ if ui.button(tl!("Bring to front")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::BringToFront) });
++ close(ui);
++ }
++ if ui.button(tl!("Send to back")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::SendToBack) });
++ close(ui);
++ }
++ if ui.button(tl!("Bring forward")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::BringForward) });
++ close(ui);
++ }
++ if ui.button(tl!("Send backward")).clicked() {
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Arrange(pdfcraft_engine::ArrangeOrder::SendBackward) });
++ close(ui);
++ }
++ });
++
++ if ui.add_enabled(raster, egui::Button::new(tl!("Replace image"))).clicked() {
++ *action = Some(ImageAction::Replace(page, hit));
++ close(ui);
++ }
++ if ui.add_enabled(raster, egui::Button::new(tl!("Save Image As…"))).clicked() {
++ *action = Some(ImageAction::Save(page, hit));
++ close(ui);
++ }
++
++ ui.separator();
++
++ if ui.button(tl!("Draw freehand")).clicked() {
++ *action = Some(ImageAction::DrawFreehand);
++ close(ui);
++ }
++ if ui.button(tl!("Redact Image")).clicked() {
++ *action = Some(ImageAction::Redact(page, hit));
++ close(ui);
++ }
++ if ui.add_enabled(raster, egui::Button::new(tl!("Export image"))).clicked() {
++ *action = Some(ImageAction::Save(page, hit));
++ close(ui);
++ }
++
++ ui.separator();
++
++ if ui.button(tl!("Create Link…")).clicked() {
++ *action = Some(ImageAction::Link(page, hit));
++ close(ui);
++ }
++
++ ui.menu_button(tl!("Edit image using"), |ui| {
++ ui.add_enabled(false, egui::Button::new(tl!("Default image editor")));
++ });
++
++ if ui.button(tl!("Recognize Text…")).clicked() {
++ *action = Some(ImageAction::Recognize(page));
++ close(ui);
++ }
++
++ ui.separator();
++
++ if ui.button(tl!("Delete")).clicked() {
++ view.image_selection = None;
++ view.pending_edit = Some(Edit::EditPageImage { page, index: hit, change: E::Delete });
++ close(ui);
++ }
++}
diff --cc crates/ui-egui/src/tools/edit_text_ui/layout.rs
index 497789005,ef004403b..194651920
--- a/crates/ui-egui/src/tools/edit_text_ui/layout.rs
+++ b/crates/ui-egui/src/tools/edit_text_ui/layout.rs
@@@ -1,7 -1,7 +1,7 @@@
- use super::*;
+ use super::{Arc, BTreeMap, Color32, FontFamily, FontId, LineEditor, Rect};
/// The look shown for a paragraph: the family and weight guessed from its PDF font name.
-fn source_look(base_font: &str, size: f64, color: [f64; 3], detected_bold: bool, detected_italic: bool) -> pdfcraft_engine::AddedText {
+pub(super) fn source_look(base_font: &str, size: f64, color: [f64; 3], detected_bold: bool, detected_italic: bool) -> pdfcraft_engine::AddedText {
use pdfcraft_engine::FontFamily as F;
let name = base_font.to_ascii_lowercase();
let family = if ["courier", "mono", "consolas", "menlo", "monaco", "lucida console"].iter().any(|s| name.contains(s)) {
diff --cc crates/ui-egui/src/tools/object_ui.rs
index 66266787b,17aea394f..67a077bc7
--- a/crates/ui-egui/src/tools/object_ui.rs
+++ b/crates/ui-egui/src/tools/object_ui.rs
@@@ -161,17 -182,23 +182,23 @@@ fn leave_single(view: &mut DocView)
view.block_drag = None;
}
+ /// What of the objects lies under `p` on `page`: the objects selected together (a drag moves
+ /// them), or the frame around the paragraph open for typing (`open`: its box on screen), which
+ /// takes hold of the box as in Acrobat: a click selects it, a drag moves it, its text keeps the
+ /// caret.
+ pub(crate) fn hit(view: &DocView, xf: &PageXform, info: &DocInfo, page: usize, open: Option<Rect>, p: Pos2) -> Option<Hit> {
+ let s = &view.objects;
+ if s.page == Some(page) && !s.selected.is_empty() && s.selected.iter().fold(Rect::NOTHING, |r, o| r.union(screen(xf, info, page, o))).contains(p)
+ {
+ return Some(Hit::new(Owner::Objects { part: ObjectsPart::Selection }, Layer::Placed, egui::CursorIcon::Move));
+ }
+ open.filter(|r| on_frame(r.shrink(2.0), p)).map(|_| Hit::new(Owner::Objects { part: ObjectsPart::Frame }, Layer::Handle, egui::CursorIcon::Move))
+ }
+
/// Returns true while group selection owns the interaction. Ordinary clicks still reach the
/// single-object text/resize tools. A text draft is finished or discarded through its editor.
- pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- doc: &Document,
- view: &mut DocView,
- ) -> bool {
+ pub(crate) fn page_input(cx: PageToolContext<'_>, view: &mut DocView) -> bool {
- let PageToolContext { ui, xf, ptr, page, .. } = cx;
++ let PageToolContext { ui, resp, xf, ptr, page, .. } = cx;
if view.objects.input_blocked {
return true;
}
diff --cc crates/ui-egui/tests/it/editing.rs
index 16a81cb9f,bdb0720dd..e5714a6ba
--- a/crates/ui-egui/tests/it/editing.rs
+++ b/crates/ui-egui/tests/it/editing.rs
@@@ -7731,252 -7036,460 +8297,706 @@@ fn mode_tabs_keep_the_hand_over_a_parag
assert_eq!(h.state().mode, pdfcraft_ui_egui::Mode::Convert, "the tab takes the click, not the paragraph's editor");
}
+#[test]
+fn copy_pasted_image_discards_coordinates_and_moves_without_offset() {
+ let mut png = Vec::new();
+ image::RgbImage::from_pixel(60, 60, image::Rgb([100, 150, 200])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
+
+ let mut h = harness(1, |_| {});
+ h.state_mut().system_clipboard = false;
+ h.run_steps(4);
+
+ // 1. Add image to page 0
+ h.state_mut().add_image("test_img.png".into(), png);
+ h.run_steps(3);
+
+ let id = h.state().views[0].id;
+ let images_before = h.state().session.get(id).unwrap().page_images(0);
+ assert_eq!(images_before.len(), 1);
+
+ // 2. Copy the image from page 0
+ assert!(h.state_mut().copy_page_image(0, 0));
+ // Verify coordinates data was discarded during copy!
+ assert!(h.state().last_copied_image_rect.is_none(), "coordinates data must be discarded during copy");
+ assert!(h.state().last_copied_image.is_some(), "image pixel data is kept");
+
+ // 3. Paste the image via Ctrl+V (paste_image with rect: None)
+ assert!(h.state_mut().paste_image(0, None));
+ h.run_steps(3);
+
+ // Verify after paste that coordinates data remains None
+ assert!(h.state().last_copied_image_rect.is_none());
+
+ let images_after_paste = h.state().session.get(id).unwrap().page_images(0);
+ assert_eq!(images_after_paste.len(), 2, "second image pasted");
+
+ // The pasted image should be centered on the page, not offset by (+20, -20) from the first image
+ let img1 = &images_after_paste[0];
+ let img2 = &images_after_paste[1];
+ assert_eq!(img1.rect, img2.rect, "pasted image placed using natural page centering");
+
+ // 4. Move the pasted image by an explicit Move change
+ let target = [50.0, 50.0, 110.0, 110.0];
+ h.state_mut().apply_edit(pdfcraft_engine::Edit::EditPageImage { page: 0, index: 1, change: pdfcraft_engine::ImageEdit::Move(target) });
+ h.run_steps(3);
+
+ let images_after_move = h.state().session.get(id).unwrap().page_images(0);
+ assert_eq!(images_after_move[1].rect, target, "moved image rect matches target exactly without offset");
+}
+
+#[test]
+fn clicking_after_image_context_menu_action_resets_menu_state_and_updates_selection() {
+ let mut png = Vec::new();
+ image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app.open_bytes("picture.png", None, png.clone()).expect("opens");
+ app
+ });
+ h.run_steps(4);
+ let id = h.state().views[0].id;
+ // Move the first image to left side: [10.0, 10.0, 30.0, 30.0]
+ h.state_mut().apply_edit(pdfcraft_engine::Edit::EditPageImage {
+ page: 0,
+ index: 0,
+ change: pdfcraft_engine::ImageEdit::Move([10.0, 10.0, 30.0, 30.0]),
+ });
+ // Add second image to right side: [50.0, 10.0, 70.0, 30.0]
+ let mut png2 = Vec::new();
+ image::RgbImage::from_pixel(20, 20, image::Rgb([40, 200, 40])).write_to(&mut std::io::Cursor::new(&mut png2), image::ImageFormat::Png).unwrap();
+ h.state_mut().apply_edit(pdfcraft_engine::Edit::AddImage {
+ page: 0,
+ rect: Some([50.0, 10.0, 70.0, 30.0]),
+ name: "img2.png".into(),
+ bytes: std::sync::Arc::new(png2),
+ });
+ h.run_steps(4);
+
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let info = h.state().session.get(id).unwrap().info.pages[0].clone();
+ let user_to_screen = move |u: [f64; 2]| {
+ let v = info.user_to_view(u[0] as f32, u[1] as f32);
+ let scale = r.width() / info.width;
+ egui::pos2(r.left() + v[0] * scale, r.top() + v[1] * scale)
+ };
+
+ let p0 = user_to_screen([20.0, 20.0]);
+ let p1 = user_to_screen([60.0, 20.0]);
+ let p_empty = user_to_screen([40.0, 35.0]);
+
+ // 1. Select image 0
+ h.hover_at(p0);
+ h.run_steps(1);
+ h.drag_at(p0);
+ h.run_steps(1);
+ h.drop_at(p0);
+ h.run_steps(2);
+ assert_eq!(h.state().views[0].image_selection.as_ref().map(|s| s.index), Some(0), "image 0 selected");
+
+ // 2. Select image 1 (switches selection)
+ h.hover_at(p1);
+ h.run_steps(1);
+ h.drag_at(p1);
+ h.run_steps(1);
+ h.drop_at(p1);
+ h.run_steps(2);
+ assert_eq!(h.state().views[0].image_selection.as_ref().map(|s| s.index), Some(1), "image 1 selected");
+
+ // 3. Click empty space (clears selection)
+ h.hover_at(p_empty);
+ h.run_steps(1);
+ h.drag_at(p_empty);
+ h.run_steps(1);
+ h.drop_at(p_empty);
+ h.run_steps(2);
+ assert!(h.state().views[0].image_selection.is_none(), "empty page clears selection");
+
+ // 4. Select image 0 again
+ h.hover_at(p0);
+ h.run_steps(1);
+ h.drag_at(p0);
+ h.run_steps(1);
+ h.drop_at(p0);
+ h.run_steps(2);
+ assert_eq!(h.state().views[0].image_selection.as_ref().map(|s| s.index), Some(0), "image 0 selected again");
+
+ // 5. Right-click image 0 to open context menu
+ h.hover_at(p0);
+ h.run_steps(1);
+ h.event(egui::Event::PointerButton { pos: p0, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
+ h.event(egui::Event::PointerButton { pos: p0, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
+ h.run_steps(2);
+
+ // 6. Click "Flip horizontal"
+ h.get_by_label("Flip horizontal").click();
+ h.run_steps(3);
+
+ let flag_persisted = h.ctx.data_mut(|d| d.get_persisted::<bool>(pdfcraft_ui_egui::edit_text_ui::image_menu_id()));
+ assert_eq!(flag_persisted, Some(false), "image-menu flag should be reset to false");
+
+ // 7. Click image 1: should switch selection to image 1
+ h.hover_at(p1);
+ h.run_steps(1);
+ h.drag_at(p1);
+ h.run_steps(1);
+ h.drop_at(p1);
+ h.run_steps(2);
+ assert_eq!(
+ h.state().views[0].image_selection.as_ref().map(|s| s.index),
+ Some(1),
+ "clicking other image switches selection after context menu action"
+ );
+
+ // 8. Click empty space: should clear selection
+ h.hover_at(p_empty);
+ h.run_steps(1);
+ h.drag_at(p_empty);
+ h.run_steps(1);
+ h.drop_at(p_empty);
+ h.run_steps(2);
+ assert!(h.state().views[0].image_selection.is_none(), "clicking empty page clears selection after context menu action");
+}
+
+#[test]
+fn format_panel_shows_selection_size_and_updates_after_font_applied() {
+ let mut objs: Vec<String> = vec![
+ "<< /Type /Catalog /Pages 2 0 R >>".into(),
+ "<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 200 300] >>".into(),
+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into(),
+ "<< /Type /Page /Parent 2 0 R /Contents 5 0 R /Resources << /Font << /F1 3 0 R >> >> >>".into(),
+ ];
+ let body = "BT /F1 24 Tf 20 200 Td (Heading) Tj /F1 12 Tf 0 -25 Td (Paragraph) Tj ET";
+ objs.push(format!("<< /Length {} >>\nstream\n{body}\nendstream", body.len()));
+ let mut pdf = Vec::from(b"%PDF-1.7\n");
+ let mut xref = Vec::new();
+ for (i, o) in objs.iter().enumerate() {
+ xref.push(pdf.len());
+ pdf.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
+ }
+ let xref_pos = pdf.len();
+ pdf.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
+ for o in xref {
+ pdf.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
+ }
+ pdf.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref_pos}\n%%EOF\n", objs.len() + 1).as_bytes());
+
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app.open_bytes("test.pdf", None, pdf).expect("fixture opens");
+ app
+ });
+ h.run_steps(4);
+
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 180.0) / 300.0 * r.height());
+ h.hover_at(at);
+ h.run_steps(1);
+ h.drag_at(at);
+ h.run_steps(1);
+ h.drop_at(at);
+ h.run_steps(3);
+
+ assert!(h.state().views[0].line_editor.is_some(), "editor opened");
+ let text = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ assert!(text.contains("Paragraph"), "text is {text}");
+
+ // Select the 12 pt paragraph text "Paragraph" via keyboard selection in the TextEdit
+ h.key_press_modifiers(Modifiers::COMMAND, Key::ArrowRight);
+ h.run_steps(1);
+ for _ in 0.."Paragraph".len() {
+ h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowLeft);
+ h.run_steps(1);
+ }
+ use egui::accesskit::Role;
- let size_node = h.get_by_role_and_label(Role::ComboBox, "Font size");
- assert_eq!(size_node.value().as_deref(), Some("12"), "size combo box value is 12");
++ let size_node = h.get_by_role_and_label(Role::TextInput, "Font size:");
++ assert_eq!(size_node.value().as_deref(), Some("12"), "size text input value is 12");
+
+ let font_node = h.get_by_role_and_label(Role::ComboBox, "Font");
+ assert_eq!(font_node.value().as_deref(), Some("Helvetica"), "initial font is Helvetica");
+
+ // Apply Times to the selection
+ font_node.click();
+ h.run_steps(2);
+ h.get_by_label("Times").click();
+ h.run_steps(4);
+
+ // Font combo box for the whole block is now Mixed because Heading is Helvetica and Paragraph is Times
+ let font_node = h.get_by_role_and_label(Role::ComboBox, "Font");
+ assert_eq!(font_node.value().as_deref(), Some("Mixed"), "whole block has mixed fonts");
+
+ // Apply Times to the whole block
+ font_node.click();
+ h.run_steps(2);
+ h.get_by_label("Times").click();
+ h.run_steps(4);
+
+ // Now all text is Times, so font combo box updates to Times
+ let font_node = h.get_by_role_and_label(Role::ComboBox, "Font");
+ assert_eq!(font_node.value().as_deref(), Some("Times"), "font combo box updates to Times");
+}
+
+ /// The paragraph opened for typing, as a person opens it: a click on its text.
+ fn open_paragraph(h: &mut Harness<'static, PdfCraftApp>) {
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
+ click_at(h, at);
+ assert!(h.state().views[0].line_editor.is_some(), "the paragraph opens for typing");
+ }
+
+ /// Ctrl+Z after a formatting change, with the paragraph still open: the change is undone and
+ /// the editor closes. The box's own Undo used to take the key, putting back the text it had
+ /// before the change into a box laid out for the page after it, and the document kept the change.
+ #[test]
+ fn undo_with_the_paragraph_open_undoes_the_formatting() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ {
+ let s = h.state();
+ let doc = s.session.get(s.views[0].id).unwrap();
+ assert_eq!(doc.can_undo(), Some("Edit text"));
+ assert_eq!(doc.text_blocks(0)[0].base_font, "Helvetica-Bold");
+ assert!(s.views[0].line_editor.is_some(), "still editing");
+ }
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(4);
+ let s = h.state();
+ let doc = s.session.get(s.views[0].id).unwrap();
+ assert_eq!(doc.can_undo(), None, "the formatting is undone");
+ assert_eq!(doc.text_blocks(0)[0].base_font, "Helvetica");
+ assert!(s.views[0].line_editor.is_none(), "the editor closed with the step it showed");
+ }
+
+ /// Undo while typing takes back the typing in the box first; the document is left alone.
+ #[test]
+ fn undo_while_typing_takes_back_the_typing_first() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ assert_ne!(h.state().views[0].line_editor.as_ref().unwrap().text, original);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ let s = h.state();
+ let ed = s.views[0].line_editor.as_ref().expect("still typing");
+ assert_eq!(ed.text, original, "the typing is taken back");
+ assert_eq!(s.session.get(s.views[0].id).unwrap().can_undo(), None, "the document is untouched");
+ }
+
+ /// A paragraph of a short, a wide and a short row, opened in the paragraph editor.
+ fn three_row_paragraph() -> Harness<'static, PdfCraftApp> {
+ let bytes = paragraph_drag_fixture("BT /F1 10 Tf 12 TL 60 400 Td (One two) Tj T* (three four five six seven eight) Tj T* (nine ten) Tj ET", 0, 1);
+ let mut h = paragraph_drag_harness(bytes, 0);
+ let (at, _) = paragraph_drag_points(&h, "One two three four five six seven eight nine ten", [0.0, 0.0]);
+ click_at(&mut h, at);
+ assert!(h.state().views[0].line_editor.is_some(), "the paragraph opens for typing");
+ h
+ }
+
+ /// Format text's alignment flyout, by pointer.
+ fn align_by_pointer(h: &mut Harness<'static, PdfCraftApp>, choice: &str) {
+ let button = h.get_by_label("Alignment").rect().center();
+ click_at(h, button);
+ let item = h.get_by_label(choice).rect().center();
+ click_at(h, item);
+ }
+
+ fn row_rect(h: &Harness<'static, PdfCraftApp>, starts: &str) -> [f64; 4] {
+ let s = h.state();
+ s.session.get(s.views[0].id).unwrap().text_lines(0).into_iter().find(|l| l.text.starts_with(starts)).expect("the row").rect
+ }
+
+ /// Existing-row alignment stays disabled until intent survives reopening; cancellation keeps all rows.
+ #[test]
+ fn selected_existing_rows_keep_alignment_disabled_and_the_complete_paragraph() {
+ let mut h = three_row_paragraph();
+ let before = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ let first = row_rect(&h, "One");
+ let last = row_rect(&h, "nine");
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.key_press_modifiers(Modifiers::SHIFT, Key::Home);
+ h.run_steps(2);
+ assert!(h.get_by_label("Alignment").accesskit_node().is_disabled());
+ let button = h.get_by_label("Alignment").rect().center();
+ click_at(&mut h, button);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, before);
+ assert_eq!(row_rect(&h, "One"), first);
+ assert_eq!(row_rect(&h, "nine"), last);
+ assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), None);
+ h.key_press(Key::Escape);
+ h.run_steps(3);
+ assert_eq!(row_rect(&h, "One"), first);
+ assert_eq!(row_rect(&h, "nine"), last);
+ }
+
+ /// Clearing the selection restores ordinary whole-paragraph alignment and its undo step.
+ #[test]
+ fn whole_paragraph_alignment_stays_available_without_a_row_selection() {
+ let mut h = three_row_paragraph();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.key_press_modifiers(Modifiers::SHIFT, Key::Home);
+ h.run_steps(2);
+ assert!(h.get_by_label("Alignment").accesskit_node().is_disabled());
+ h.key_press(Key::End);
+ h.run_steps(2);
+ assert!(!h.get_by_label("Alignment").accesskit_node().is_disabled());
+ align_by_pointer(&mut h, "Align right");
+ assert!(h.state().views[0].line_editor.is_some());
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), None);
+ }
+
+ /// Redo after Undo in the box puts the typing back, in the box: it doesn't reach the document,
+ /// whose own Redo (a formatting change waiting here) must stay waiting.
+ #[test]
+ fn redo_after_undoing_typing_puts_the_typing_back() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ // Undo the bold (closing the editor), so the document has it to redo.
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(4);
+ assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_redo(), Some("Edit text"));
+ open_paragraph(&mut h);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ let typed = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "the typing is taken back");
+ h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
+ h.run_steps(3);
+ let s = h.state();
+ let ed = s.views[0].line_editor.as_ref().expect("still typing");
+ assert_eq!(ed.text, typed, "Redo puts the typing back");
+ let doc = s.session.get(s.views[0].id).unwrap();
+ assert_eq!((doc.can_redo(), doc.text_blocks(0)[0].base_font.as_str()), (Some("Edit text"), "Helvetica"), "the document's Redo still waits");
+ }
+
+ /// Redo from the menu or toolbar with typing in the box the page doesn't have yet. Closing the
+ /// box first applied the typing, a new edit that dropped the document's waiting Redo, and the
+ /// Redo itself then failed: the step was gone. The typing now stays in the box and the Redo
+ /// waits; once Undo in the box takes the typing back, Redo from the menu puts it back there.
+ #[test]
+ fn redo_from_the_menu_with_typing_in_the_box_keeps_the_documents_redo() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ // Undo the bold (closing the editor), so the document has it to redo.
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(4);
+ open_paragraph(&mut h);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ let typed = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ assert!(h.state_mut().execute("edit.redo"));
+ h.run_steps(3);
+ {
+ let s = h.state();
+ let ed = s.views[0].line_editor.as_ref().expect("the box stays open");
+ assert_eq!(ed.text, typed, "the typing stays in the box, unapplied");
+ let doc = s.session.get(s.views[0].id).unwrap();
+ assert_eq!((doc.can_redo(), doc.text_blocks(0)[0].base_font.as_str()), (Some("Edit text"), "Helvetica"), "the document's Redo still waits");
+ }
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "Undo takes the typing back in the box");
+ assert!(h.state_mut().execute("edit.redo"));
+ h.run_steps(3);
+ let s = h.state();
+ assert_eq!(s.views[0].line_editor.as_ref().expect("still open").text, typed, "Redo from the menu puts the typing back");
+ assert_eq!(s.session.get(s.views[0].id).unwrap().can_redo(), Some("Edit text"), "the document's Redo still waits");
+ }
+
+ /// Undo from the menu or toolbar with typing in the box the page doesn't have yet: the box takes
+ /// the typing back, as the key does, and stays open, and the document's waiting Redo stays. The
+ /// menu applied the typing first (a new edit, which dropped that Redo), closed the box and undid
+ /// the typing all at once.
+ #[test]
+ fn undo_from_the_menu_takes_back_typing_in_the_box() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ // Undo the bold (closing the editor), so the document has it to redo and nothing to undo.
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(4);
+ open_paragraph(&mut h);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ h.key_press_modifiers(Modifiers::SHIFT, Key::Home);
+ h.run_steps(2);
+ assert!(h.state().views[0].line_editor.as_ref().unwrap().selection.is_some());
+ h.event(egui::Event::PointerMoved(egui::pos2(10.0, 10.0)));
+ h.ctx.memory_mut(|m| m.request_focus(egui::Id::unique("test-menu")));
+ assert!(h.state_mut().execute("edit.undo"), "Undo is enabled for the box's typing");
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().selection, None, "menu history clears the old byte range before the next frame");
+ h.run_steps(3);
+ let s = h.state();
+ let ed = s.views[0].line_editor.as_ref().expect("the box stays open");
+ assert_eq!(ed.text, original, "the typing is taken back in the box");
+ let doc = s.session.get(s.views[0].id).unwrap();
+ assert_eq!((doc.can_redo(), doc.can_undo()), (Some("Edit text"), None), "the document is untouched");
+ }
+
+ /// Redo from the menu after Undo in the box took the typing back, with nothing for the document
+ /// to redo: the menu item follows the box's own Redo too, and puts the typing back. It was
+ /// disabled, as it followed only the document's Redo, and only the key could do it.
+ #[test]
+ fn redo_from_the_menu_is_enabled_for_the_boxs_own_typing() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ let typed = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "the typing is taken back");
+ assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_redo(), None, "the document has nothing to redo");
+ // A range retained while the unfocused editor still showed its previous text.
+ h.state_mut().views[0].line_editor.as_mut().unwrap().selection = Some(0..1);
+ h.event(egui::Event::PointerMoved(egui::pos2(10.0, 10.0)));
+ h.ctx.memory_mut(|m| m.request_focus(egui::Id::unique("test-menu")));
+ assert!(h.state_mut().execute("edit.redo"), "Redo is enabled for the box's own step");
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().selection, None, "menu history clears the old byte range before the next frame");
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().expect("still open").text, typed, "Redo puts the typing back");
+ }
+
+ /// Typing in tab A's paragraph, a paragraph opened in tab B, then back to A: Edit ▸ Undo takes
+ /// the typing back in A's box. The tabs share one text box, and its history was cleared each time
+ /// another tab's box took it, so Undo found no step and closed A's paragraph instead
+ /// (skylartaylor's and FredApps's review of 08ec1db7).
+ #[test]
+ fn undo_after_a_round_trip_through_another_tab_takes_back_the_typing() {
+ let mut h = harness(1, |app| {
+ app.open_bytes("copy.pdf", None, fixture(1)).expect("the copy opens");
+ });
+ let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
+ h.state_mut().active = Some(tab);
+ h.run_steps(2);
+ if h.state().views[tab].line_editor.is_none() {
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ }
+ let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
+ click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
+ assert!(h.state().views[tab].line_editor.is_some(), "the paragraph opens for typing");
+ };
+ open_in(&mut h, 0);
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(4);
+ open_in(&mut h, 0);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ assert!(h.state().views[0].line_editor.as_ref().unwrap().text.ends_with(" more"));
+ open_in(&mut h, 1);
+ h.state_mut().active = Some(0);
+ h.run_steps(3);
+ assert!(h.state().views[0].line_editor.is_some(), "A's paragraph is still open after the round trip");
+ assert!(h.state_mut().execute("edit.undo"), "Undo is enabled");
+ h.run_steps(3);
+ let s = h.state();
+ let ed = s.views[0].line_editor.as_ref().expect("the box stays open: Undo takes back the typing");
+ assert_eq!(ed.text, original, "the typing is taken back in the box");
+ assert_eq!(s.session.get(s.views[0].id).unwrap().can_redo(), Some("Edit text"), "the document's Redo still waits");
+ }
+
+ /// Typing taken back with Ctrl+Z, then Bold applied with the paragraph still open: Ctrl+Shift+Z
+ /// must not bring the typing back on top of the bold, as the box's history is cleared once the
+ /// page has its text. The format left the text as it was, so the box kept its history and its
+ /// Redo put "Page 1 more" back (FredApps's review of abc378c0).
+ #[test]
+ fn redo_after_formatting_never_brings_back_typing_undone_before_it() {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ open_paragraph(&mut h);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "the typing is taken back");
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Edit text"), "the bold is applied");
+ h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
+ h.run_steps(3);
+ let text = h.state().views[0].line_editor.as_ref().map(|ed| ed.text.clone());
+ assert_eq!(text.as_deref(), Some(original.as_str()), "the typing undone before the bold stays undone");
+ // Nor through what the menu's Redo runs.
+ h.state_mut().redo();
+ h.run_steps(3);
+ let s = h.state();
+ let text = s.views[0].line_editor.as_ref().map(|ed| ed.text.clone());
+ assert!(!text.as_deref().unwrap_or_default().contains("more"), "the menu's Redo brings it back: {text:?}");
+ assert_eq!(s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].base_font, "Helvetica-Bold", "the bold stays");
+ }
+
+ /// Edit ▸ Undo chosen in the same frame as the switch back to tab A, before A's box is drawn
+ /// again: the shared text box still holds tab B's history then. Stepping through it put B's
+ /// typing into A's box; the box now claims its own history back first (CodeRabbit on abc378c0).
+ #[test]
+ fn undo_in_the_frame_of_the_switch_back_takes_back_the_typing() {
+ let mut h = harness(1, |app| {
+ app.open_bytes("copy.pdf", None, fixture(1)).expect("the copy opens");
+ });
+ let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
+ h.state_mut().active = Some(tab);
+ h.run_steps(2);
+ if h.state().views[tab].line_editor.is_none() {
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ }
+ let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
+ click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
+ assert!(h.state().views[tab].line_editor.is_some(), "the paragraph opens for typing");
+ };
+ open_in(&mut h, 0);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" more".into()));
+ h.run_steps(2);
+ // In B, a paragraph whose text differs from A's ("Page 1 B", applied with Bold), so B's
+ // history can't pass for A's.
+ open_in(&mut h, 1);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" B".into()));
+ h.run_steps(2);
+ h.get_by_label("Bold").click();
+ h.run_steps(4);
+ assert_eq!(h.state().views[1].line_editor.as_ref().map(|ed| ed.text.as_str()), Some("Page 1 B"), "B's paragraph is open on its own text");
+ // Back to A and Undo at once: no frame draws A's box in between.
+ h.state_mut().active = Some(0);
+ assert!(h.state_mut().execute("edit.undo"), "Undo is enabled");
+ h.run_steps(3);
+ let ed = h.state().views[0].line_editor.clone().expect("the box stays open: Undo takes back the typing");
+ assert_eq!(ed.text, original, "the typing is taken back in A's box");
+ }
+
+ /// Redo from the menu chosen in the same frame as a switch to tab B, whose paragraph is open on
+ /// the same text as A's: the shared text box still holds A's history then, with A's undone
+ /// typing to redo, so B's box claims its own history back before stepping.
+ #[test]
+ fn redo_in_the_frame_of_a_tab_switch_never_brings_another_tabs_typing() {
+ let mut h = harness(1, |app| {
+ app.open_bytes("copy.pdf", None, fixture(1)).expect("the copy opens");
+ });
+ let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
+ h.state_mut().active = Some(tab);
+ h.run_steps(2);
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
+ click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
+ assert!(h.state().views[tab].line_editor.is_some(), "the paragraph opens for typing");
+ };
+ open_in(&mut h, 1);
+ open_in(&mut h, 0);
+ let original = h.state().views[0].line_editor.as_ref().unwrap().text.clone();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" secret".into()));
+ h.run_steps(2);
+ assert!(h.state_mut().execute("edit.undo"));
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "taken back in A");
+ // To B and Redo at once: no frame draws B's box in between.
+ h.state_mut().active = Some(1);
+ h.state_mut().redo();
+ h.run_steps(3);
+ let s = h.state();
+ let b = s.views[1].line_editor.as_ref().map(|ed| ed.text.clone());
+ assert!(!b.as_deref().unwrap_or_default().contains("secret"), "A's typing came back in B: {b:?}");
+ assert!(!s.session.get(s.views[1].id).unwrap().text_blocks(0)[0].text.contains("secret"), "A's typing reached B's page");
+ }
+
+ /// Two copies of a document, the same paragraph open in each. The paragraph boxes share one text
+ /// box, and so one Undo history: a box opened on the same text as another tab's didn't clear it.
+ /// Typing in one tab, taken back with Undo, then came back with Redo (the key or the menu) in the
+ /// other tab.
+ #[test]
+ fn typing_in_one_tab_never_reaches_another_tabs_paragraph() {
+ let mut h = harness(1, |app| {
+ app.open_bytes("copy.pdf", None, fixture(1)).expect("the copy opens");
+ });
+ let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
+ h.state_mut().active = Some(tab);
+ h.run_steps(2);
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
+ click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
+ assert!(h.state().views[tab].line_editor.is_some(), "the paragraph opens for typing");
+ };
+ open_in(&mut h, 1);
+ let original = h.state().views[1].line_editor.as_ref().unwrap().text.clone();
+ open_in(&mut h, 0);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "the same paragraph");
+ h.key_press_modifiers(Modifiers::COMMAND, Key::End);
+ h.event(egui::Event::Text(" secret".into()));
+ h.run_steps(2);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].line_editor.as_ref().unwrap().text, original, "taken back");
+ // The copy, its paragraph still open: Redo there has nothing of its own to put back.
+ h.state_mut().active = Some(1);
+ h.run_steps(3);
+ assert!(h.state().views[1].line_editor.is_some(), "the copy's paragraph is still open");
+ h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
+ h.run_steps(3);
+ let text = h.state().views[1].line_editor.as_ref().expect("the copy's paragraph stays open through Redo").text.clone();
+ assert!(!text.contains("secret"), "the other tab's typing came in: {text:?}");
+ // Nor through what the menu's Redo runs, which reads the box's history itself (the menu item
+ // is off here, as the copy's document has nothing to redo). With no Redo of its own, the box
+ // passes the step to the document and closes, so the page's text is checked too.
+ h.state_mut().redo();
+ h.run_steps(3);
+ let s = h.state();
+ let text = s.views[1].line_editor.as_ref().map(|ed| ed.text.clone());
+ assert!(!text.as_deref().unwrap_or_default().contains("secret"), "the other tab's typing came in from the menu: {text:?}");
+ assert_eq!(s.session.get(s.views[1].id).unwrap().text_blocks(0)[0].text, s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].text);
+ }
+
/// A paragraph selected by its frame goes with one Delete (or Backspace), as in Acrobat, instead
/// of being emptied a typed character at a time, and undo brings it back.
#[test]
@@@ -8007,97 -7520,37 +9027,132 @@@ fn a_paragraph_selected_by_its_frame_de
}
}
+fn two_image_fixture() -> Vec<u8> {
+ let content = "q 20 0 0 20 100 100 cm /Im0 Do Q q 20 0 0 20 20 20 cm /Im0 Do Q";
+ let objs: Vec<String> = vec![
+ "<< /Type /Catalog /Pages 2 0 R >>".into(),
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".into(),
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Contents 4 0 R /Resources << /XObject << /Im0 5 0 R >> >> >>".into(),
+ format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),
+ "<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 4 >>\nstream\n\0\0\0\0\nendstream"
+ .into(),
+ ];
+ let mut out = b"%PDF-1.7\n".to_vec();
+ let mut offsets = Vec::new();
+ for (i, o) in objs.iter().enumerate() {
+ offsets.push(out.len());
+ out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
+ }
+ let xref = out.len();
+ out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
+ for o in offsets {
+ out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
+ }
+ out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
+ out
+}
+
+#[test]
+fn duplicate_xobject_at_different_rects_arrange_and_delete_keeps_other_instance_through_menu() {
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(|_cc| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app.open_bytes("dup.pdf", None, two_image_fixture()).expect("opens fixture");
+ app
+ });
+ h.run_steps(4);
+ let id = h.state().views[0].id;
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+
+ let xf = h.state().views[0].page_xform(0).expect("page 0 xform");
+ let imgs = h.state().session.get(id).unwrap().page_images(0);
+ // B is at index 0 [100, 100, 120, 120], in the back
+ let b0 = pdfcraft_ui_egui::canvas::image_screen_rect(&imgs[0], &xf, &h.state().session.get(id).unwrap().info, 0);
+ let p_b = b0.center();
+
+ // 1. Select B (image 0)
+ h.hover_at(p_b);
+ h.run_steps(1);
+ h.drag_at(p_b);
+ h.run_steps(1);
+ h.drop_at(p_b);
+ h.run_steps(2);
+ assert_eq!(h.state().views[0].image_selection.as_ref().map(|s| s.index), Some(0), "image 0 (B) selected");
+
+ // 2. Right-click B (image 0) to open context menu
+ h.hover_at(p_b);
+ h.run_steps(1);
+ h.event(egui::Event::PointerButton { pos: p_b, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
+ h.event(egui::Event::PointerButton { pos: p_b, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
+ h.run_steps(2);
+
+ // 3. Open "Arrange image" submenu and click "Bring to front"
+ h.get_by_label("Arrange image").click();
+ h.run_steps(2);
+ h.get_by_label("Bring to front").click();
+ h.run_steps(3);
+
+ // 4. Selection must be preserved after arrange: B is now at index 1 (in front)
+ let sel = h.state().views[0].image_selection.as_ref().map(|s| s.index);
+ assert_eq!(sel, Some(1), "selection must track B to index 1 after arrange");
+
+ // 5. Right-click on the selected image (B at index 1) again to open context menu
+ let sel_idx = sel.unwrap();
+ let xf_again = h.state().views[0].page_xform(0).expect("page 0 xform");
+ let cur_imgs = h.state().session.get(id).unwrap().page_images(0);
+ let b_rect = pdfcraft_ui_egui::canvas::image_screen_rect(&cur_imgs[sel_idx], &xf_again, &h.state().session.get(id).unwrap().info, 0);
+ let p_b2 = b_rect.center();
+ h.hover_at(p_b2);
+ h.run_steps(1);
+ h.event(egui::Event::PointerButton { pos: p_b2, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
+ h.event(egui::Event::PointerButton { pos: p_b2, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
+ h.run_steps(2);
+
+ // 6. Click "Delete" in context menu
+ h.get_by_label("Delete").click();
+ h.run_steps(3);
+
+ // 7. Save, reopen and verify: A remains at [20, 20, 40, 40], B is deleted
+ let saved = h.state().session.save_bytes(id).unwrap();
+ let mut reopened = pdfcraft_engine::Session::new();
+ let rid = reopened.open("again.pdf", None, saved, None).unwrap();
+ let imgs = reopened.get(rid).unwrap().page_images(0);
+ assert_eq!(imgs.len(), 1, "exactly one image must remain after deleting B");
+ assert!((imgs[0].rect[0] - 20.0).abs() < 1e-2, "remaining image must be A at [20, 20, 40, 40], was {:?}", imgs[0].rect);
+}
++
+ /// Esc in an open paragraph leaves its text and selects its box, as in Acrobat, so Esc and
+ /// Delete remove it without aiming for the thin frame; the unapplied draft is discarded.
+ #[test]
+ fn escape_in_an_open_paragraph_selects_its_box_for_delete() {
+ for draft in [None, Some("Page one")] {
+ let mut h = harness(1, |_| {});
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ let id = h.state().views[0].id;
+ let blocks = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().text_blocks(0);
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let k = r.width() / 200.0;
+ let b = blocks(&h)[0].clone();
+ click_at(
+ &mut h,
+ egui::pos2(r.left() + ((b.rect[0] + b.rect[2]) / 2.0) as f32 * k, r.top() + (300.0 - (b.rect[1] + b.rect[3]) / 2.0) as f32 * k),
+ );
+ assert!(h.state().views[0].line_editor.is_some(), "a click inside the paragraph opens it for typing");
+ if let Some(text) = draft {
+ h.state_mut().views[0].line_editor.as_mut().unwrap().text = text.into();
+ h.run_steps(1);
+ }
+ press(&mut h, Modifiers::NONE, Key::Escape);
+ assert!(h.state().views[0].line_editor.is_none(), "Esc leaves the text");
+ h.get_by_label("1 object selected");
+ assert_eq!(blocks(&h)[0].text, "Page 1", "the draft {draft:?} is discarded");
+ press(&mut h, Modifiers::NONE, Key::Delete);
+ assert!(blocks(&h).is_empty(), "Esc then Delete removes the paragraph: {:?}", blocks(&h));
+ // One undo step brings it back.
+ assert!(h.state_mut().execute("edit.undo"));
+ h.run_steps(2);
+ assert_eq!(blocks(&h)[0].text, "Page 1");
+ }
+ }