diff --git a/crates/ui-egui/src/editing.rs b/crates/ui-egui/src/editing.rs index 663cf1dcca..d4dc91ffc6 100644 --- a/crates/ui-egui/src/editing.rs +++ b/crates/ui-egui/src/editing.rs @@ -942,7 +942,16 @@ impl PdfCraftApp { /// field's typing and the field refused it (#166): the editor reopens with the text, and the /// user has been told why, so the typing isn't lost and the caller can stop. fn apply_queued_edit(&mut self, ctx: &egui::Context, i: usize) -> bool { - let Some(edit) = self.views.get_mut(i).and_then(|v| v.session.pending_edit.take()) else { return true }; + let Some(view) = self.views.get_mut(i) else { return true }; + let queued_text = view.session.content.queued.take(); + let Some(edit) = view.session.pending_edit.take() else { + if let Some(queued) = queued_text { + queued.restore(&mut view.session.content); + return false; + } + return true; + }; + let includes_text = queued_text.as_ref().is_some_and(|queued| queued.included_in(&edit)); let signature_page = self.views.get_mut(i).and_then(|v| v.session.fill_signature.take()); let doc_id = self.views.get(i).map(|v| v.id); let committed = self.views.get_mut(i).and_then(|v| v.session.forms.committed.take()); @@ -950,7 +959,23 @@ impl PdfCraftApp { (Edit::SetFieldValue { name, .. }, Some(draft)) => *name == draft.name, _ => false, }; - if self.apply_edit(ctx, edit) { + let applied = self.apply_edit(ctx, edit); + if applied && includes_text && queued_text.as_ref().is_some_and(|queued| queued.done) { + self.quick_tool = crate::QuickTool::Select; + } + let mut text_ok = true; + if let Some(queued) = queued_text + && !(applied && includes_text) + && let Some(view) = self.views.get_mut(i) + { + queued.restore(&mut view.session.content); + view.session.pending_text_open = None; + if view.session.line_editor.as_ref().is_some_and(|ed| !ed.has_changes()) { + view.session.line_editor = None; + } + text_ok = false; + } + if applied { if signature_page.is_some() && let Some(id) = doc_id { @@ -985,10 +1010,10 @@ impl PdfCraftApp { view.session.comments.reveal = true; self.quick_tool = crate::QuickTool::Select; } - return true; + return text_ok; } if !typed { - return true; + return text_ok; } if let (Some(draft), Some(view)) = (committed, self.views.get_mut(i)) { view.session.forms.focus = Some(draft); diff --git a/crates/ui-egui/src/editing/tests.rs b/crates/ui-egui/src/editing/tests.rs index c523216a6f..97446c245f 100644 --- a/crates/ui-egui/src/editing/tests.rs +++ b/crates/ui-egui/src/editing/tests.rs @@ -57,8 +57,11 @@ fn answer_save(h: &mut Harness<'static, crate::PdfCraftApp>, files: &ScriptedFil /// A one-page document with a proper xref table. fn one_page_pdf() -> Vec { - let objs = - ["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 200 300] >>", "<< /Type /Page /Parent 2 0 R >>"]; + page_pdf("<< /Type /Page /Parent 2 0 R >>") +} + +fn page_pdf(page: &str) -> Vec { + let objs = ["<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 200 300] >>", page]; let mut out = b"%PDF-1.7\n".to_vec(); let mut offsets = Vec::new(); for (i, o) in objs.iter().enumerate() { @@ -140,6 +143,43 @@ fn a_refused_text_box_is_put_back_whole() { assert_eq!(content.select_added, None, "nothing was added to select"); } +#[test] +fn refused_greek_in_a_winansi_times_resource_stays_editable_and_can_be_retried() { + let ctx = egui::Context::default(); + let mut app = crate::PdfCraftApp::for_unit_tests(); + app.set_option(&ctx, "language", "en").unwrap(); + let pdf = page_pdf( + "<< /Type /Page /Parent 2 0 R /Resources << /Font << /R8 << /Type /Font /Subtype /Type1 /BaseFont /Times-Roman /Encoding /WinAnsiEncoding >> >> >> >>", + ); + app.open_bytes(&ctx, "times.pdf", None, pdf).unwrap(); + let text = "Δοκιμή ελληνικού κειμένου"; + let rect = [20.0, 100.0, 180.0, 130.0]; + let style = pdfcraft_engine::AddedText { font_resource: Some("R8".into()), ..crate::content_ui::default_style() }; + let mut draft = crate::content_ui::TextDraft::new(0, None, rect, text.into(), style); + draft.focus = true; + let session = &mut app.views[0].session; + session.content.draft = Some(draft.clone()); + session.pending_edit = session.content.queue_draft(&[]); + // Clicking elsewhere can have opened another, empty box by the time the original edit runs. + session.content.draft = Some(crate::content_ui::TextDraft::new(0, None, [10.0; 4], String::new(), crate::content_ui::default_style())); + assert!(!app.process_pending_edits(&ctx)); + assert!(app.last_edit_refusal.as_deref().is_some_and(|msg| msg.contains("/R8") && msg.contains("U+0394"))); + let cv = &app.views[0].session.content; + assert_eq!(cv.draft.as_ref(), Some(&draft)); + assert_eq!(cv.select_added, None); + assert!(app.session.get(app.views[0].id).unwrap().added().is_empty()); + + app.use_files(&ctx, crate::files::FilePurpose::TextFont { page: 0 }, vec![("Inter.ttf".into(), pdfcraft_fonts::INTER_REGULAR.to_vec())]); + let session = &mut app.views[0].session; + session.pending_edit = session.content.queue_draft(&[]); + assert!(app.process_pending_edits(&ctx)); + assert!(app.views[0].session.content.draft.is_none()); + let saved = app.session.get(app.views[0].id).unwrap().added(); + assert!( + matches!(saved, [item] if matches!(&item.content, pdfcraft_engine::AddedContent::Text(t) if t.text == text && t.font_resource.as_deref().is_some_and(|font| font != "R8"))) + ); +} + /// Save commits the open text box before anything is written, even when it then asks where. #[test] fn save_commits_the_open_text_box() { diff --git a/crates/ui-egui/src/tools/content_ui.rs b/crates/ui-egui/src/tools/content_ui.rs index bc4e1f4750..a8a75bc87d 100644 --- a/crates/ui-egui/src/tools/content_ui.rs +++ b/crates/ui-egui/src/tools/content_ui.rs @@ -207,11 +207,36 @@ impl PartialEq for DraftPaint { } } +/// The editor a canvas click closed, until the corresponding document edit succeeds. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct QueuedText { + pub(crate) done: bool, + draft: TextDraft, + edit: Edit, + select_added: Option<(usize, usize)>, +} + +impl QueuedText { + pub(crate) fn included_in(&self, edit: &Edit) -> bool { + *edit == self.edit || matches!(edit, Edit::Batch { edits, .. } if edits.iter().any(|e| self.included_in(e))) + } + + pub(crate) fn restore(mut self, cv: &mut ContentView) { + self.draft.focus = true; + cv.selected = self.draft.index.map(|index| (self.draft.page, index)); + cv.draft = Some(self.draft); + cv.select_added = self.select_added; + cv.done = false; + cv.drawn = None; + } +} + #[derive(Clone, Debug, Default, PartialEq)] pub struct ContentView { /// The selected added item: (page, index among the page's items). pub selected: Option<(usize, usize)>, pub draft: Option, + pub(crate) queued: Option, /// Select the item an add creates (the page's item count before it). pub select_added: Option<(usize, usize)>, /// Done was pressed on the open box: the page body returns to Select ([`ContentView::take_done`]). @@ -241,6 +266,15 @@ impl ContentView { content.painter.galley(drawn.origin, Arc::clone(&drawn.galley), Color32::BLACK); } + /// Hand a canvas edit to the end-of-frame queue, retaining typing through the engine's result. + pub(crate) fn queue_draft(&mut self, added: &[Added]) -> Option { + let draft = self.draft.clone()?; + let select_added = self.select_added; + let edit = self.finish_draft(added)?; + self.queued = Some(QueuedText { done: std::mem::take(&mut self.done), draft, select_added, edit: edit.clone() }); + Some(edit) + } + pub(crate) fn finish_draft(&mut self, added: &[Added]) -> Option { self.draft.take().and_then(|d| finish(self, d, added)) } @@ -511,7 +545,7 @@ pub(crate) fn added_press(session: &mut PageSession, doc: &Document, pages: &[Pl if cv.hold_blocked() || cv.draft.as_ref().is_some_and(|d| d.page == page && d.index == Some(index)) { return None; } - let typed = cv.finish_draft(added); + let typed = cv.queue_draft(added); let mut draft = TextDraft::new(page, Some(index), text.rect, text.text.clone(), text.clone()); draft.focus = true; cv.draft = Some(draft); @@ -561,7 +595,7 @@ impl<'a> AddTextTool<'a> { } // A click elsewhere keeps the text being typed and starts a new box (#74). The editor ends its // session on the same background press, so whichever runs first finds the other done. - let typed = cv.finish_draft(added); + let typed = cv.queue_draft(added); // The Format panel styles the new text, not the box just kept. (cv.selected, cv.select_added) = (None, None); let at = frame.display(at); @@ -1084,8 +1118,7 @@ fn draft_session( } cv.done |= done; cv.drawn = None; - let draft = cv.draft.take()?; - finish(cv, draft, doc.added()) + cv.queue_draft(doc.added()) } /// Grows the draft's box to hold its text where the page would wrap it, remembering the answer for diff --git a/crates/ui-egui/src/tools/edit_text_ui.rs b/crates/ui-egui/src/tools/edit_text_ui.rs index 1d2f56772d..faec4b8f91 100644 --- a/crates/ui-egui/src/tools/edit_text_ui.rs +++ b/crates/ui-egui/src/tools/edit_text_ui.rs @@ -802,7 +802,7 @@ fn open_by_click(session: &mut PageSession, doc: &pdfcraft_engine::Document, pag if session.content.hold_blocked() { return None; } - finished = session.content.finish_draft(doc.added()); + finished = session.content.queue_draft(doc.added()); session.content.selected = None; } // A click that moves the editor to another paragraph applies the open one first, so its typed