mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge pull request #19147
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
commit
b3a642bf0f
3 files changed
+195
-10
No files matched your search
@@ -519,7 +519,7 @@ fn global_cmd_shift_keys_still_work_outside_type_editing() {
|
||||
assert!(!char_style(&h, id, 0).faux_italic && char_style(&h, id, 0).caps == photocraft_doc::text::Caps::Normal);
|
||||
}
|
||||
|
||||
/// ⌥↑/↓ leading (2 pt, 10 with ⌘), ⇧⌥↑/↓ baseline shift and ⌥←/→ tracking over a selection
|
||||
/// ⌥↑/↓ leading (1 pt, 5 with ⌘), ⇧⌥↑/↓ baseline shift and ⌥←/→ tracking over a selection
|
||||
/// (20, 100 with ⌘): one step per press, from each character's own value, the selection kept.
|
||||
#[test]
|
||||
fn type_tool_format_keys_step_leading_baseline_and_tracking() {
|
||||
@@ -531,17 +531,17 @@ fn type_tool_format_keys_step_leading_baseline_and_tracking() {
|
||||
h.key_press_modifiers(m, k);
|
||||
h.run_steps(2);
|
||||
};
|
||||
// Auto leading is 120% of 10 pt: ⌥↓ makes it 14.
|
||||
// Auto leading is 120% of 10 pt: ⌥↓ makes it 13.
|
||||
press(&mut h, Modifiers::ALT, Key::ArrowDown);
|
||||
assert_eq!(char_style(&h, id, 0).leading_pt, Some(14.0));
|
||||
assert_eq!(char_style(&h, id, 0).leading_pt, Some(13.0));
|
||||
press(&mut h, Modifiers::ALT, Key::ArrowUp);
|
||||
assert_eq!(char_style(&h, id, 4).leading_pt, Some(12.0));
|
||||
press(&mut h, Modifiers::ALT | Modifiers::COMMAND, Key::ArrowDown);
|
||||
assert_eq!(char_style(&h, id, 0).leading_pt, Some(22.0));
|
||||
assert_eq!(char_style(&h, id, 0).leading_pt, Some(17.0));
|
||||
press(&mut h, Modifiers::ALT | Modifiers::SHIFT, Key::ArrowUp);
|
||||
assert_eq!(char_style(&h, id, 0).baseline_shift_pt, 2.0);
|
||||
assert_eq!(char_style(&h, id, 0).baseline_shift_pt, 1.0);
|
||||
press(&mut h, Modifiers::ALT | Modifiers::SHIFT | Modifiers::COMMAND, Key::ArrowDown);
|
||||
assert_eq!(char_style(&h, id, 0).baseline_shift_pt, -8.0);
|
||||
assert_eq!(char_style(&h, id, 0).baseline_shift_pt, -4.0);
|
||||
press(&mut h, Modifiers::ALT, Key::ArrowRight);
|
||||
assert_eq!(char_style(&h, id, 0).tracking, 20.0);
|
||||
press(&mut h, Modifiers::ALT | Modifiers::COMMAND, Key::ArrowLeft);
|
||||
|
||||
@@ -7,9 +7,9 @@ use craft::text::ui::editor::{CaretAffinity, CaretLine, EditKind, HostLine, Host
|
||||
use photocraft_doc::LayerId;
|
||||
use photocraft_text::warp::{Placement, Warp};
|
||||
|
||||
use super::editing::{kern_pair, step_char_metric, toggle_char_style, toggle_hyphenate};
|
||||
use super::editing::{kern_pair, selected_styles, step_char_metric, toggle_char_style, toggle_hyphenate};
|
||||
use super::layout::{doc_point, hit_at, is_vertical, layout, path_warp};
|
||||
use super::{Affine, Arc, PhotocraftApp, TextLayout, byte_of, insertion_style, text_layer};
|
||||
use super::{Affine, Arc, PhotocraftApp, TextLayer, TextLayout, byte_of, byte_range, insertion_style, text_layer};
|
||||
|
||||
/// A type layer's text, layout and formatting for one editing session. Geometry is in document units.
|
||||
pub(crate) struct LayerHost<'a> {
|
||||
@@ -23,6 +23,13 @@ impl<'a> LayerHost<'a> {
|
||||
Self { app, layer }
|
||||
}
|
||||
|
||||
/// PhotoCraft's measured horizontal selection shortcuts take 1/5 pt, rather than the shared editor's 2/10.
|
||||
/// Keep its existing behavior for mixed styles, vertical text and the empty-text typing style until measured.
|
||||
fn point_metric_step(&self, selection: TextSelection, by: f32) -> f32 {
|
||||
let Some(text) = self.app.session.active().and_then(|st| text_layer(&st.doc, self.layer)) else { return by };
|
||||
if is_vertical(self.app, self.layer) || !uniform_selection(text, selection.range()) { by } else { by * 0.5 }
|
||||
}
|
||||
|
||||
fn laid_out(&mut self) -> Option<Laid> {
|
||||
let (l, aff, text) = layout(self.app, self.layer)?;
|
||||
let warp = path_warp(self.app, self.layer, &l);
|
||||
@@ -30,6 +37,31 @@ impl<'a> LayerHost<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Only the selected runs decide whether values are mixed; unrelated text outside the selection does not.
|
||||
fn uniform_selection(text: &TextLayer, range: Range<usize>) -> bool {
|
||||
if range.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let styles = selected_styles(text, range.start, range.end);
|
||||
if styles.first().is_none_or(|first| styles.iter().any(|style| style != first)) {
|
||||
return false;
|
||||
}
|
||||
let (a, b) = byte_range(&text.text, range.start, range.end);
|
||||
let mut at = 0usize;
|
||||
let mut first = None;
|
||||
for run in text.paragraph_runs() {
|
||||
let end = at.saturating_add(run.len);
|
||||
if at < b && end > a {
|
||||
if first.as_ref().is_some_and(|style| style != &run.style) {
|
||||
return false;
|
||||
}
|
||||
first = Some(run.style);
|
||||
}
|
||||
at = end;
|
||||
}
|
||||
first.is_some()
|
||||
}
|
||||
|
||||
/// The layer's layout with what maps its line space to the document.
|
||||
struct Laid {
|
||||
l: Arc<TextLayout>,
|
||||
@@ -238,8 +270,14 @@ impl TextEditorHost for LayerHost<'_> {
|
||||
match key {
|
||||
StyleKey::Toggle(which) => toggle_char_style(self.app, which),
|
||||
StyleKey::Hyphenate => toggle_hyphenate(self.app),
|
||||
StyleKey::Leading(v) => step_char_metric(self.app, "leadingBy", v),
|
||||
StyleKey::BaselineShift(v) => step_char_metric(self.app, "baselineShiftBy", v),
|
||||
StyleKey::Leading(v) => {
|
||||
let by = self.point_metric_step(selection, v);
|
||||
step_char_metric(self.app, "leadingBy", by);
|
||||
}
|
||||
StyleKey::BaselineShift(v) => {
|
||||
let by = self.point_metric_step(selection, v);
|
||||
step_char_metric(self.app, "baselineShiftBy", by);
|
||||
}
|
||||
StyleKey::Tracking(v) => step_char_metric(self.app, "trackingBy", v),
|
||||
StyleKey::Kern(v) => kern_pair(self.app, self.layer, selection.caret, v),
|
||||
_ => return false,
|
||||
@@ -258,3 +296,7 @@ pub(crate) fn type_text(app: &mut PhotocraftApp, s: &str) {
|
||||
}
|
||||
LayerHost::new(app, layer).replace(range, s, EditKind::Typed);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "leading_baseline_step_tests.rs"]
|
||||
mod leading_baseline_step_tests;
|
||||
@@ -0,0 +1,143 @@
|
||||
//! #17902: the real native Type editor chords take 1/5 pt for leading and baseline, retaining 20/100 tracking/kern.
|
||||
use craft::text::ui::editor::{StyleKey, TextEditorHost, TextSelection};
|
||||
use egui::{Key, Modifiers};
|
||||
use egui_kittest::Harness;
|
||||
use photocraft_doc::LayerId;
|
||||
use photocraft_doc::text::CharStyle;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::tools::type_tool::{self, text_layer};
|
||||
|
||||
const TEXT: &str = "ABCDEFGH\nIJKLMNOP";
|
||||
|
||||
fn editing(vertical: bool, auto: f32) -> (Box<PhotocraftApp>, LayerId) {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 640, "height": 360})).unwrap();
|
||||
let made = app
|
||||
.run(
|
||||
"type.create",
|
||||
json!({"text": TEXT, "x": 50, "y": 100, "size": 48,
|
||||
"orientation": if vertical { "vertical" } else { "horizontal" }}),
|
||||
)
|
||||
.unwrap();
|
||||
let id = LayerId(made["layer"].as_u64().unwrap());
|
||||
app.run("type.setStyle", json!({"layer": id.0, "autoLeading": auto})).unwrap();
|
||||
app.sync_views();
|
||||
type_tool::commit_tests::edit_active(&mut app);
|
||||
(app, id)
|
||||
}
|
||||
|
||||
type H = Harness<'static, Box<PhotocraftApp>>;
|
||||
|
||||
/// Match input_tests' working eframe route: the full app hook owns native key input and editor focus.
|
||||
fn native_editing(auto: f32) -> (H, LayerId) {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 640, "height": 360})).unwrap();
|
||||
app.sync_views();
|
||||
let ctx = app.ctx().clone();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_context(ctx.clone()).with_max_steps(64).build_eframe(move |_cc| app);
|
||||
h.run_steps(4);
|
||||
let made = h.state_mut().run("type.create", json!({"text": TEXT, "x": 50, "y": 100, "size": 48})).unwrap();
|
||||
let id = LayerId(made["layer"].as_u64().unwrap());
|
||||
h.state_mut().run("type.setStyle", json!({"layer": id.0, "autoLeading": auto})).unwrap();
|
||||
type_tool::edit_active(h.state_mut(), &ctx).unwrap();
|
||||
h.run_steps(2);
|
||||
(h, id)
|
||||
}
|
||||
|
||||
fn key(h: &mut H, key: Key, modifiers: Modifiers) {
|
||||
h.key_press_modifiers(modifiers, key);
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
fn styles(app: &PhotocraftApp, id: LayerId) -> Vec<CharStyle> {
|
||||
text_layer(&app.session.active().unwrap().doc, id).unwrap().char_runs().into_iter().map(|r| r.style).collect()
|
||||
}
|
||||
|
||||
fn close(actual: f32, expected: f32) {
|
||||
assert!((actual - expected).abs() < 0.0001, "{actual} != {expected}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_leading_chords_start_from_effective_auto_and_take_one_or_five_points() {
|
||||
// A non-default paragraph factor catches implementations that hard-code 120% or the initial 57.6 pt.
|
||||
for auto in [120.0, 150.0] {
|
||||
for (key, far, delta) in [(Key::ArrowUp, false, -1.0), (Key::ArrowDown, false, 1.0), (Key::ArrowUp, true, -5.0), (Key::ArrowDown, true, 5.0)] {
|
||||
let (mut h, id) = native_editing(auto);
|
||||
let t = text_layer(&h.state().session.active().unwrap().doc, id).unwrap();
|
||||
let effective = t.char_runs()[0].style.size_pt * t.paragraph_runs()[0].style.auto_leading;
|
||||
assert!(styles(h.state(), id).iter().all(|s| s.leading_pt.is_none()));
|
||||
let mods = Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE };
|
||||
self::key(&mut h, key, mods);
|
||||
for style in styles(h.state(), id) {
|
||||
close(style.leading_pt.expect("the key switches auto leading off"), effective + delta);
|
||||
assert_eq!((style.tracking, style.baseline_shift_pt), (0.0, 0.0));
|
||||
}
|
||||
assert_eq!(h.state().ui.text_edit.as_ref().unwrap().caret.max(h.state().ui.text_edit.as_ref().unwrap().anchor), TEXT.chars().count());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_baseline_chords_raise_and_lower_by_one_or_five_points() {
|
||||
let (mut h, id) = native_editing(120.0);
|
||||
for (key, far, expected) in [(Key::ArrowUp, false, 1.0), (Key::ArrowDown, false, 0.0), (Key::ArrowUp, true, 5.0), (Key::ArrowDown, true, 0.0)] {
|
||||
self::key(&mut h, key, Modifiers::ALT | Modifiers::SHIFT | if far { Modifiers::COMMAND } else { Modifiers::NONE });
|
||||
for style in styles(h.state(), id) {
|
||||
close(style.baseline_shift_pt, expected);
|
||||
assert!(style.leading_pt.is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_tracking_and_caret_kerning_keep_twenty_and_one_hundred() {
|
||||
let (mut h, id) = native_editing(120.0);
|
||||
for (key, far, expected) in [(Key::ArrowRight, false, 20.0), (Key::ArrowLeft, false, 0.0), (Key::ArrowRight, true, 100.0), (Key::ArrowLeft, true, 0.0)] {
|
||||
self::key(&mut h, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE });
|
||||
assert!(styles(h.state(), id).iter().all(|style| style.tracking == expected));
|
||||
}
|
||||
let ed = h.state_mut().ui.text_edit.as_mut().unwrap();
|
||||
ed.caret = 1;
|
||||
ed.anchor = 1;
|
||||
h.run_steps(2);
|
||||
for (key, far, expected) in [(Key::ArrowRight, false, 20.0), (Key::ArrowLeft, false, 0.0), (Key::ArrowRight, true, 100.0), (Key::ArrowLeft, true, 0.0)] {
|
||||
self::key(&mut h, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE });
|
||||
close(styles(h.state(), id)[0].kern, expected);
|
||||
assert!(styles(h.state(), id).iter().all(|style| style.tracking == 0.0));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_keeps_unmeasured_vertical_and_mixed_selection_steps() {
|
||||
let selection = TextSelection::new(0, TEXT.chars().count());
|
||||
let (mut app, id) = editing(true, 120.0);
|
||||
let effective = 48.0 * 1.2;
|
||||
assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::Leading(2.0), selection));
|
||||
assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::BaselineShift(10.0), selection));
|
||||
for style in styles(&app, id) {
|
||||
close(style.leading_pt.unwrap(), effective + 2.0);
|
||||
close(style.baseline_shift_pt, 10.0);
|
||||
}
|
||||
|
||||
let (mut app, id) = editing(false, 120.0);
|
||||
app.run("type.setStyle", json!({"layer": id.0, "range": [0, 3], "leading": 60, "baselineShift": 7})).unwrap();
|
||||
assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::Leading(2.0), selection));
|
||||
assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::BaselineShift(10.0), selection));
|
||||
let runs = styles(&app, id);
|
||||
assert_eq!((runs[0].leading_pt, runs[0].baseline_shift_pt), (Some(62.0), 17.0));
|
||||
close(runs.last().unwrap().leading_pt.unwrap(), effective + 2.0);
|
||||
close(runs.last().unwrap().baseline_shift_pt, 10.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_runs_within_the_selection_decide_whether_the_metric_step_is_mixed() {
|
||||
let (mut app, id) = editing(false, 120.0);
|
||||
app.run("type.setStyle", json!({"layer": id.0, "range": [9, 17], "leading": 80, "autoLeading": 150})).unwrap();
|
||||
let first_line = TextSelection::new(0, 8);
|
||||
assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::Leading(2.0), first_line));
|
||||
let runs = styles(&app, id);
|
||||
close(runs[0].leading_pt.unwrap(), 48.0 * 1.2 + 1.0);
|
||||
assert_eq!(runs.last().unwrap().leading_pt, Some(80.0));
|
||||
}
|
||||
Reference in new issue
Block a user