PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge PR #4266: M7: added text takes character spacing from Format text
aiongg880723e3af34
diff --cc crates/engine/src/lib.rsindex 92b1a2da6,97a6d44f2..0a9db62cb--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@@ -55,9 -55,7 +55,10 @@@ pub use pdfcraft_organize::view::{Initipub use pdfcraft_cos::Algorithm;pub use pdfcraft_create::{CONVERTIBLE, CreateError, ImageResolution, SourceKind, decode_text, source_kind};-pub use pdfcraft_edit::added::{CHAR_SPACING_LIMIT as ADDED_CHAR_SPACING_LIMIT, text_rect as added_text_rect, text_width as added_text_width};+pub use pdfcraft_edit::added::{- paste_place as added_paste_place, paste_rect as added_paste_rect, text_rect as added_text_rect, text_width as added_text_width,++ CHAR_SPACING_LIMIT as ADDED_CHAR_SPACING_LIMIT, paste_place as added_paste_place, paste_rect as added_paste_rect, text_rect as added_text_rect,++ text_width as added_text_width,+};pub use pdfcraft_edit::{Added, AddedImage, AddedText, Align as TextAlign, Background, Content as AddedContent, Family as FontFamily, HeaderFooter, MarkKind, Watermark,};diff --cc crates/ui-egui/src/content_ui.rsindex 1622273e3,88a66579c..a54f1e406--- a/crates/ui-egui/src/content_ui.rs+++ b/crates/ui-egui/src/content_ui.rs@@@ -570,26 -560,26 +570,34 @@@ pub(crate) fn editor(ctx: &egui::Contex// The box is white in every theme, so the caret must not follow the theme (pale in dark mode) (#655).ui.visuals_mut().text_cursor.stroke.color = crate::edit_text_ui::caret_color_on(Color32::WHITE);let [cr, cg, cb] = t.style.color.map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8);+ // Laid out as the page draws it, so opening the box doesn't show it realigned.+ // shortcut: justified text is shown left-aligned here (TextEdit can't justify), until the editor gets its own layouter.+ let halign = match t.style.align {+ pdfcraft_engine::TextAlign::Center => egui::Align::Center,+ pdfcraft_engine::TextAlign::Right => egui::Align::Max,+ pdfcraft_engine::TextAlign::Left | pdfcraft_engine::TextAlign::Justify => egui::Align::Min,+ };+ // Character spacing shows as the page will draw it, scaled like the font.+ let letter_spacing = (t.style.char_spacing * f64::from(zoom)) as f32;+ let mut spaced = |ui: &egui::Ui, text: &dyn egui::TextBuffer, wrap_width: f32| {- let job = spaced_job(text.as_str(), &font, Color32::from_rgb(cr, cg, cb), letter_spacing, wrap_width);++ let job = spaced_job(text.as_str(), &font, Color32::from_rgb(cr, cg, cb), letter_spacing, wrap_width, halign);+ ui.fonts_mut(|f| f.layout_job(job))+ };ui.horizontal_top(|ui| {ui.spacing_mut().item_spacing.x = 4.0;- let resp = ui.add(- egui::TextEdit::multiline(&mut t.text)- .font(font)- .desired_width(width)- .desired_rows(1)- .horizontal_align(halign)- .background_color(if background_ready { Color32::TRANSPARENT } else { Color32::WHITE })- .text_color(Color32::from_rgb(cr, cg, cb))- .hint_text(tl!("Type text"))- .id_salt("added-text-edit"),- );+ let mut edit = egui::TextEdit::multiline(&mut t.text)+ .font(font.clone())- .desired_width(r.width().max(60.0))++ .desired_width(width)+ .desired_rows(1)++ .horizontal_align(halign)+ .background_color(if background_ready { Color32::TRANSPARENT } else { Color32::WHITE })+ .text_color(Color32::from_rgb(cr, cg, cb))+ .hint_text(tl!("Type text"))+ .id_salt("added-text-edit");+ if letter_spacing != 0.0 {+ edit = edit.layouter(&mut spaced);+ }+ let resp = ui.add(edit);if t.focus {resp.request_focus();t.focus = false;@@@ -945,6 -935,16 +953,17 @@@ pub(crate) fn image_panel(ui: &mut eguiout}+ /// The editor's layout of added text with character spacing. The page puts the spacing after every+ /// character, the last of a line included (`latin_width`), while epaint puts it only between+ /// characters: the wrap width gives up one spacing so both break lines at the same place.-fn spaced_job(text: &str, font: &egui::FontId, color: Color32, letter_spacing: f32, wrap_width: f32) -> egui::text::LayoutJob {++fn spaced_job(text: &str, font: &egui::FontId, color: Color32, letter_spacing: f32, wrap_width: f32, halign: egui::Align) -> egui::text::LayoutJob {+ let format = egui::TextFormat { font_id: font.clone(), color, extra_letter_spacing: letter_spacing, ..Default::default() };+ let mut job = egui::text::LayoutJob::single_section(text.to_owned(), format);+ job.wrap.max_width = (wrap_width - letter_spacing).max(0.0);++ job.halign = halign;+ job+ }+#[cfg(test)]mod tests {use egui_kittest::kittest::Queryable;@@@ -993,4 -993,44 +1012,44 @@@let i = flipped("Flip vertical");assert!(i.flip_v && !i.flip_h, "Flip vertical mirrors top and bottom");}++ /// #4245: the page counts the spacing after a line's last character too, so a line that fits+ /// only without it wraps in the saved text and must wrap in the editor.+ #[test]+ fn editor_wraps_where_the_saved_text_does_with_trailing_spacing() {+ let rows = |wrap: f32| {+ let mut n = 0;+ let mut h = egui_kittest::Harness::new_ui_state(+ move |ui, n: &mut usize| {+ let font = egui::FontId::proportional(14.0);- let job = spaced_job("AAAA BBBB", &font, Color32::BLACK, 10.0, wrap);++ let job = spaced_job("AAAA BBBB", &font, Color32::BLACK, 10.0, wrap, egui::Align::Min);+ *n = ui.fonts_mut(|f| f.layout_job(job)).rows.len();+ },+ n,+ );+ h.run();+ n = *h.state();+ n+ };+ // The text's width without the trailing spacing, as epaint measures it.+ let one_line = rows(f32::INFINITY);+ assert_eq!(one_line, 1);+ let width = {+ let mut w = 0.0;+ let mut h = egui_kittest::Harness::new_ui_state(+ |ui, w: &mut f32| {+ let font = egui::FontId::proportional(14.0);- let job = spaced_job("AAAA BBBB", &font, Color32::BLACK, 10.0, f32::INFINITY);++ let job = spaced_job("AAAA BBBB", &font, Color32::BLACK, 10.0, f32::INFINITY, egui::Align::Min);+ *w = ui.fonts_mut(|f| f.layout_job(job)).rect.width();+ },+ w,+ );+ h.run();+ w = *h.state();+ w+ };+ // Page width = width + 10 (spacing after the last character) > width + 5: the page wraps.+ assert_eq!(rows(width + 5.0), 2, "the saved text wraps here, so the editor must too");+ assert_eq!(rows(width + 10.0), 1, "and keeps the line when the spacing fits");+ }}diff --cc crates/ui-egui/tests/it/content.rsindex 82334fa12,7ec4b03dd..61272e944--- a/crates/ui-egui/tests/it/content.rs+++ b/crates/ui-egui/tests/it/content.rs@@@ -714,55 -714,27 +714,76 @@@ fn format_text_panel_sets_alignment_andassert!(h.state().text_style.bold);}+/// Opening right-aligned added text to edit it shows it right-aligned, as the page does, not at+/// the box's left edge, where it never was.+#[test]+fn the_text_editor_keeps_the_boxs_alignment() {+ let _gpu = gpu();+ let mut h = harness();+ let rect = [40.0, 195.0, 240.0, 210.0];+ let text = pdfcraft_engine::AddedText { rect, text: "Hi".into(), align: pdfcraft_engine::TextAlign::Right, ..Default::default() };+ assert!(h.state_mut().apply_edit(pdfcraft_engine::Edit::AddText { page: 0, text }));+ assert!(h.state_mut().execute("edit.text"));+ h.run_steps(3);+ let point = at(&h, 200.0, 195.0);+ click(&mut h, point);+ assert_eq!(h.state().views[0].content.draft.as_ref().and_then(|d| d.index), Some(0), "the editor opened on the text");+ h.run_steps(3);+ let r = item_on_screen(&h, rect);+ let pixels = h.render().unwrap();+ let left = ink(&pixels, egui::Rect::from_min_max(r.min, pos2(r.center().x, r.max.y)));+ let right = ink(&pixels, egui::Rect::from_min_max(pos2(r.center().x, r.min.y), r.max));+ assert!(left == 0 && right > 20, "the text is drawn at the right ({left} dark pixels left, {right} right)");+}++/// A box narrower than the editor's least width (60 points on screen): right-aligned text stays+/// at the box's right edge, not the wider editor's, which lies past it.+#[test]+fn the_text_editor_aligns_a_narrow_box_to_its_own_edge() {+ let _gpu = gpu();+ let mut h = harness();+ let rect = [40.0, 195.0, 60.0, 210.0];+ let text = pdfcraft_engine::AddedText { rect, text: "Hi".into(), align: pdfcraft_engine::TextAlign::Right, ..Default::default() };+ assert!(h.state_mut().apply_edit(pdfcraft_engine::Edit::AddText { page: 0, text }));+ assert!(h.state_mut().execute("edit.text"));+ h.run_steps(3);+ let point = at(&h, 50.0, 195.0);+ click(&mut h, point);+ assert_eq!(h.state().views[0].content.draft.as_ref().and_then(|d| d.index), Some(0), "the editor opened on the text");+ h.run_steps(3);+ let r = item_on_screen(&h, rect);+ assert!(r.width() < 60.0, "the box is narrower than the editor: {}", r.width());+ let pixels = h.render().unwrap();+ let inside = ink(&pixels, r);+ let edge = ink(&pixels, egui::Rect::from_min_max(pos2(r.right() - r.width() * 0.25, r.min.y), r.max));+ let past = ink(&pixels, egui::Rect::from_min_max(pos2(r.right() + 2.0, r.min.y), pos2(r.right() + 40.0, r.max.y)));+ assert!(+ inside > 20 && edge > 0 && past == 0,+ "the text is drawn in its box, up to its right edge ({inside} dark pixels in it, {edge} in its last quarter, {past} past it)"+ );+}++ /// Added text takes character spacing from Format text (#4245), typed like a paragraph's; its+ /// line spacing and horizontal scale stay disabled until added text has them.+ #[test]+ fn format_text_sets_character_spacing_for_added_text() {+ use egui::accesskit::Role;+ use egui_kittest::kittest::NodeT;+ let mut h = harness();+ assert!(h.state_mut().execute("edit.text"));+ h.run_steps(3);+ let disabled = |h: &Harness<'static, PdfCraftApp>, label: &str| h.get_by_role_and_label(Role::SpinButton, label).accesskit_node().is_disabled();+ assert!(disabled(&h, "Horizontal scale"), "added text has no scale yet");+ assert!(!disabled(&h, "Character spacing"), "added text takes character spacing");+ h.get_by_role_and_label(Role::SpinButton, "Character spacing").click();+ h.run_steps(2);+ h.event(egui::Event::Text("2.5".into()));+ h.run_steps(1);+ h.key_press(egui::Key::Enter);+ h.run_steps(4);+ assert_eq!(h.state().text_style.char_spacing, 2.5);+ }+/// Where a mode bar tab is, by its label.fn mode_tab(h: &Harness<'static, PdfCraftApp>, label: &str) -> egui::Rect {h.get_all_by_role_and_label(egui::accesskit::Role::Button, label).map(|n| n.rect()).find(|r| r.bottom() < 120.0).expect("a mode tab")