VectorCraft: An open-source, vector art tool written by scratch in pure Rust
use super::*;
use vectorcraft_engine::Session;
#[test]
fn scaled_dimensions_do_not_show_false_mixed_values_from_roundoff() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let a = app.run("text.create", json!({"x": 20, "y": 50, "text": "Axis", "size": 12})).unwrap()["id"].clone();
app.run("object.scale", json!({"sx": 120, "sy": 110})).unwrap();
let b = app.run("text.create", json!({"x": 20, "y": 90, "text": "Label", "size": 24})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [a, b]})).unwrap();
app.run("text.setStyle", json!({"size": 7, "leading": 9})).unwrap();
app.run("text.setFormat", json!({"hScale": 130, "baselineShift": 3})).unwrap();
for (actual, expected) in [
(shared_effective(&app, |s| s.size), 7.0),
(shared_effective(&app, CharStyle::effective_leading), 9.0),
(shared_effective(&app, |s| s.h_scale), 130.0),
(shared_effective(&app, |s| s.baseline_shift), 3.0),
] {
assert!((actual.expect("equal dimensions must not be mixed") - expected).abs() < 1e-8);
}
}
/// A final empty paragraph counts: with the object selected, its Auto Leading differing from
/// the paragraph before it shows Leading as mixed.
#[test]
fn leading_field_counts_the_final_empty_paragraph() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "One\n", "size": 10})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [id]})).unwrap();
assert!((shared_leading(&app).unwrap() - 12.0).abs() < 1e-9);
app.run("text.setFormat", json!({"autoLeading": 200, "start": 4, "end": 4})).unwrap();
assert_eq!(shared_leading(&app), None, "the empty last paragraph differs");
}
/// Auto leading shows the leading the paragraph's Justification › Auto Leading sets.
#[test]
fn leading_field_shows_the_paragraph_auto_leading() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "One\nTwo", "size": 10})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [id]})).unwrap();
assert!((shared_leading(&app).unwrap() - 12.0).abs() < 1e-9);
app.run("text.setFormat", json!({"autoLeading": 200, "start": 5, "end": 5})).unwrap();
assert_eq!(shared_leading(&app), None, "the paragraphs differ");
assert_eq!(shared(&app, |s| s.leading.is_none()), Some(true), "still Auto");
app.run("text.setFormat", json!({"autoLeading": 200})).unwrap();
assert!((shared_leading(&app).unwrap() - 20.0).abs() < 1e-9);
app.run("text.setStyle", json!({"leading": 15})).unwrap();
assert!((shared_leading(&app).unwrap() - 15.0).abs() < 1e-9, "a set leading is its own");
}
#[test]
fn scaled_collapsed_text_keeps_nondimensional_controls_available() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.run("text.create", json!({"x": 20, "y": 50, "text": "Axis", "size": 12})).unwrap();
app.run("object.transform", json!({"matrix": [0, 0, 0, 0, 0, 0]})).unwrap();
assert!(text_style(&app).is_some());
assert_eq!(shared(&app, |s| s.tracking), Some(0.0));
assert_eq!(shared_effective(&app, |s| s.size), None);
app.run("text.setStyle", json!({"tracking": 40})).unwrap();
app.run("text.setFormat", json!({"leftIndent": 4})).unwrap();
assert_eq!(shared(&app, |s| s.tracking), Some(40.0));
assert_eq!(text_style(&app).unwrap().1.left_indent, 4.0);
let ctx = egui::Context::default();
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], show);
assert!(!texts.iter().any(|(s, _)| s == "No text selected"));
}
#[test]
fn scaled_character_fields_show_document_sizes_and_mixed_group_values() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let a = app.run("text.create", json!({"x": 20, "y": 50, "text": "Axis", "size": 12})).unwrap()["id"].clone();
app.run("object.scale", json!({"sx": 200})).unwrap();
assert_eq!(shared_effective(&app, |s| s.size), Some(24.0));
assert_eq!(text_style(&app).unwrap().0.size, 12.0, "style definitions keep stored attributes");
let b = app.run("text.create", json!({"x": 20, "y": 90, "text": "Label", "size": 24})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [a, b]})).unwrap();
app.run("object.group", json!({})).unwrap();
assert_eq!(shared_effective(&app, |s| s.size), Some(24.0), "different local sizes, same displayed size");
app.run("object.scale", json!({"sx": 200, "sy": 100})).unwrap();
assert_eq!(shared_effective(&app, |s| s.h_scale), Some(200.0));
app.run("text.setStyle", json!({"size": 10})).unwrap();
assert_eq!(shared_effective(&app, |s| s.size), Some(10.0));
app.run("text.setFormat", json!({"hScale": 100})).unwrap();
assert_eq!(shared_effective(&app, |s| s.h_scale), Some(100.0));
}
/// The Leading field shows the Auto leading the characters are set with, their paragraph's Auto
/// Leading share (120% by default) of their em size: 20 pt type measuring a 0.7 em cap height
/// is set at 20 / 0.7 pt. Paragraphs whose Auto leading differs show a blank field, and an
/// explicit leading shows as it is.
#[test]
fn leading_shows_the_auto_leading_of_the_em_size() {
use vectorcraft_text::test_fonts::{HEIGHT_CAP, HEIGHT_FAMILY, height_font};
vectorcraft_text::FontDb::global().add_font(height_font(true).unwrap());
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Hx", "size": 20, "font": HEIGHT_FAMILY})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [id]})).unwrap();
let near = |l: Option<f64>, want: f64| l.is_some_and(|l| (l - want).abs() < 1e-9);
assert!(near(shared_leading(&app), 24.0), "{:?}", shared_leading(&app));
app.run("text.setFormat", json!({"fontHeightReference": "capHeight"})).unwrap();
let cap = f64::from(HEIGHT_CAP) / 1000.0;
assert!(near(shared_leading(&app), 1.2 * 20.0 / cap), "{:?}", shared_leading(&app));
app.run("text.setFormat", json!({"autoLeading": 150})).unwrap();
assert!(near(shared_leading(&app), 1.5 * 20.0 / cap), "{:?}", shared_leading(&app));
// The second paragraph ends in a two-byte character.
let two = app.run("text.create", json!({"x": 20, "y": 120, "text": "Hx\nHé", "size": 20, "font": HEIGHT_FAMILY})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [two]})).unwrap();
assert!(near(shared_leading(&app), 24.0), "{:?}", shared_leading(&app));
app.run("text.setFormat", json!({"id": two, "start": 3, "end": 6, "autoLeading": 150})).unwrap();
assert_eq!(shared_leading(&app), None, "the second paragraph's Auto leading is 30 pt");
app.run("select.set", json!({"ids": [id]})).unwrap();
app.run("text.setStyle", json!({"leading": 30})).unwrap();
assert!(near(shared_leading(&app), 30.0), "{:?}", shared_leading(&app));
}
/// An inline graphic keeps its size whatever the font height reference, so the Leading field
/// shows 120% of that size for it: blank for 20 pt Cap Height type with a graphic in it, and
/// 24 pt for the graphic alone.
#[test]
fn leading_of_an_inline_graphic_follows_its_own_size() {
use vectorcraft_text::test_fonts::{HEIGHT_FAMILY, height_font};
vectorcraft_text::FontDb::global().add_font(height_font(true).unwrap());
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.run("shape.ellipse", json!({"x": 200, "y": 150, "width": 10, "height": 10})).unwrap();
app.run("symbol.new", json!({"name": "Dot"})).unwrap();
app.run("edit.clear", json!({})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Hx", "size": 20, "font": HEIGHT_FAMILY})).unwrap()["id"].clone();
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.run("text.insertInline", json!({"id": id, "at": 2, "symbol": "Dot"})).unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 0, "end": 5, "fontHeightReference": "capHeight"})).unwrap();
app.session.set_tool_option("select", &json!({"start": 0, "end": 5}));
assert_eq!(shared_leading(&app), None, "the type is set with 34.3 pt Auto leading and the graphic with 24 pt");
app.session.set_tool_option("select", &json!({"start": 2, "end": 5}));
assert_eq!(text_editing(&app).map(|(_, a, b)| (a, b)), Some((2, 5)));
let shown = shared_leading(&app);
assert!(shown.is_some_and(|l| (l - 24.0).abs() < 1e-9), "{shown:?}");
}
/// A selected text object that ends in a line break has an empty last paragraph, which the
/// Leading field takes in; a Type tool selection of all the text does not.
#[test]
fn leading_of_a_whole_object_takes_in_the_empty_last_paragraph() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Ab\n", "size": 20})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [id]})).unwrap();
let near = |l: Option<f64>, want: f64| l.is_some_and(|l| (l - want).abs() < 1e-9);
assert!(near(shared_leading(&app), 24.0), "{:?}", shared_leading(&app));
app.run("text.setFormat", json!({"id": id, "start": 3, "end": 3, "autoLeading": 150})).unwrap();
assert_eq!(shared_leading(&app), None, "the empty last paragraph's Auto leading is 30 pt");
app.session.select_tool("type", Default::default()).unwrap();
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 0, "end": 3}));
assert_eq!(text_editing(&app).map(|(_, a, b)| (a, b)), Some((0, 3)));
assert!(near(shared_leading(&app), 24.0), "a line break belongs to the paragraph it ends: {:?}", shared_leading(&app));
}
/// With the Type tool, the Leading field shows the Auto leading of the paragraphs the selection
/// covers (a line break belongs to the paragraph it ends), and at a caret that of the caret's
/// paragraph.
#[test]
fn leading_follows_the_paragraphs_the_type_tool_selection_covers() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
// é is two bytes, 4..6.
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Ab\nCé", "size": 20})).unwrap()["id"].clone();
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.run("text.setFormat", json!({"id": id, "start": 3, "end": 6, "autoLeading": 150})).unwrap();
let ranges = [((0, 2), Some(24.0)), ((0, 3), Some(24.0)), ((3, 6), Some(30.0)), ((1, 4), None), ((1, 6), None), ((4, 4), Some(30.0))];
for ((start, end), want) in ranges {
app.session.set_tool_option("select", &json!({"start": start, "end": end}));
assert_eq!(text_editing(&app).map(|(_, a, b)| (a, b)), Some((start, end)));
let shown = shared_leading(&app);
assert_eq!(shown.is_some(), want.is_some(), "{start}..{end}: {shown:?}");
assert!(shown.zip(want).is_none_or(|(l, w)| (l - w).abs() < 1e-9), "{start}..{end}: {shown:?}");
}
}
#[test]
fn scaled_range_fields_follow_the_type_tool_selection() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Axis label", "size": 12})).unwrap()["id"].clone();
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
assert!(text_editing(&app).is_some());
app.run("object.scale", json!({"sx": 200})).unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 0, "end": 4, "size": 10})).unwrap();
app.session.set_tool_option("selectAll", &json!(true));
assert_eq!(shared_effective(&app, |s| s.size), None);
app.session.set_tool_option("select", &json!({"start": 0, "end": 4}));
assert_eq!(shared_effective(&app, |s| s.size), Some(10.0));
assert_eq!(text_style(&app).unwrap().0.size, 5.0);
}
/// The Kerning field at a caret kerns that pair, not the whole text (#2217).
#[test]
fn kerning_at_a_caret_sets_the_pair_not_the_whole_text() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "AVAX", "size": 12})).unwrap()["id"].clone();
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 2, "end": 2}));
set_kerning(&mut app, json!(-50));
let kerning = |app: &VectorcraftApp, at: usize| {
let st = app.session.active().unwrap();
let Some(NodeKind::Text(t)) = st.doc.node(NodeId(id.as_u64().unwrap())).map(|n| &n.kind) else { panic!("text") };
vectorcraft_text::edit::style_at(&t.runs, at + 1).kerning // the character at `at`
};
assert_eq!(kerning(&app, 1), Some(-50.0), "the character before the caret");
assert_eq!(kerning(&app, 3), None, "the rest is untouched");
assert_eq!(kerning(&app, 0), None);
// A selected range still takes it whole.
app.session.set_tool_option("select", &json!({"start": 2, "end": 4}));
set_kerning(&mut app, json!(30));
assert_eq!((kerning(&app, 1), kerning(&app, 2), kerning(&app, 3)), (Some(-50.0), Some(30.0), Some(30.0)));
}
#[test]
fn character_kerning_setter_and_automatic_pair_value() {
let german = crate::i18n::Lang::from_code("de").unwrap();
assert!(crate::i18n::has(german, "Optical"));
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
app.run("text.create", json!({"x": 20, "y": 50, "text": "III", "size": 100, "font": "JetBrains Mono"})).unwrap();
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 1, "end": 1}));
set_kerning(&mut app, json!("optical"));
assert!(text_style(&app).unwrap().0.optical_kerning);
let ctx = egui::Context::default();
let mut shown = None;
ctx.run_ui(Default::default(), |ui| {
shown = caret_kerning(&app, ui);
})
.textures_delta
.clear();
let (value, automatic) = shown.unwrap();
assert!(automatic && value != 0.0);
set_kerning(&mut app, json!(50));
ctx.run_ui(Default::default(), |ui| {
shown = caret_kerning(&app, ui);
})
.textures_delta
.clear();
assert_eq!(shown, Some((50.0, false)));
app.session.set_tool_option("select", &json!({"start": 0, "end": 2}));
assert_eq!(shared(&app, |s| (s.optical_kerning, s.kerning)), None, "a mixed range shows a blank Kerning field");
}
/// Highlight Kerning and Highlight Tracking show the Character panel and give its field the
/// keyboard focus with the value selected: what is typed replaces it, and Enter applies it.
/// Highlight Font Name opens the font family menu with its search field focused. Without
/// selected type the panel shows and nothing takes the focus.
#[test]
fn highlight_commands_focus_the_font_kerning_and_tracking_fields() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "AVAX", "size": 12})).unwrap()["id"].clone();
let ctx = egui::Context::default();
let frame = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
let mut out = ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| show(app, ui));
out.textures_delta.clear();
};
let typed = |text: &str| {
let enter = egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() };
vec![egui::Event::Text(text.into()), enter]
};
let spacing = |app: &VectorcraftApp| text_style(app).map(|(s, _)| (s.kerning, s.tracking)).unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
assert_eq!(app.run("type.highlightKerning", json!({})).unwrap(), json!({"field": "kerning"}));
assert!(crate::panel_docking::showing(&app.ui, "character"), "the panel shows");
frame(&mut app, vec![]);
assert_eq!(app.character_focus, None, "the Kerning field took the focus");
frame(&mut app, typed("-40"));
assert_eq!(spacing(&app), (Some(-40.0), 0.0), "typing replaced the kerning");
app.run("type.highlightTracking", json!({})).unwrap();
frame(&mut app, vec![]);
frame(&mut app, typed("75"));
assert_eq!(spacing(&app), (Some(-40.0), 75.0), "typing replaced the tracking");
// Highlighting a field again while it holds typed text that is not applied selects all of it.
app.run("type.highlightKerning", json!({})).unwrap();
frame(&mut app, vec![]);
frame(&mut app, vec![egui::Event::Text("12345".into())]);
app.run("type.highlightKerning", json!({})).unwrap();
frame(&mut app, vec![]);
frame(&mut app, typed("-20"));
assert_eq!(spacing(&app), (Some(-20.0), 75.0), "typing replaced the text typed before");
ctx.memory_mut(|m| m.stop_text_input());
app.run("type.highlightFontName", json!({})).unwrap();
frame(&mut app, vec![]);
frame(&mut app, vec![]);
assert!(egui::Popup::is_any_open(&ctx), "the font family menu is open");
assert!(ctx.memory(|m| m.focused()).is_some(), "its search field has the focus");
// No type selected: the panel shows, the command fails and nothing is armed.
app.run("select.none", json!({})).unwrap();
assert!(app.run("type.highlightKerning", json!({})).is_err());
assert_eq!(app.character_focus, None);
}
/// A variable font gets the Variable Font button, which opens one slider per axis named as the
/// font names it; a drag along one sets that axis of the selected type in one undo step.
#[test]
fn variable_fonts_get_a_slider_per_axis() {
use vectorcraft_text::test_fonts::{TWO_AXIS_FAMILY, two_axis_font};
let db = vectorcraft_text::FontDb::global();
db.add_font(two_axis_font().unwrap());
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let plain = app.run("text.create", json!({"x": 20, "y": 50, "text": "Static"})).unwrap()["id"].clone();
let id = app.run("text.create", json!({"x": 20, "y": 120, "text": "lIn", "font": TWO_AXIS_FAMILY})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [plain]})).unwrap();
assert!(!crate::tests_labels::painted_text(&mut app, show).contains("Variable Font"), "a static font");
app.run("select.set", json!({"ids": [id]})).unwrap();
assert!(crate::tests_labels::painted_text(&mut app, show).contains("Variable Font"));
let face = db.face(TWO_AXIS_FAMILY, "Regular").unwrap();
let text = crate::tests_labels::painted_text(&mut app, |app, ui| axis_sliders(app, ui, &face));
assert!(text.contains("Weight\n") && text.contains("Width\n") && text.contains("400") && text.contains("100"), "{text}");
// The labels are the font's names in every interface language, not catalog words (ja
// translates the interface's Weight as the stroke weight).
let ja = crate::i18n::Lang::from_code("ja").unwrap();
crate::i18n::set_current(ja);
let text = crate::tests_labels::painted_text(&mut app, |app, ui| axis_sliders(app, ui, &face));
crate::i18n::set_current(crate::i18n::Lang::EN);
let weight = crate::i18n::tr(ja, "Weight");
assert!(text.contains("Weight\n") && text.contains("Width\n") && (weight == "Weight" || !text.contains(weight)), "{text}");
// Screen readers name each slider by its axis.
let tree = crate::widgets::accessibility_tests::tree(|ui| axis_sliders(&mut app, ui, &face));
for name in ["Weight", "Width"] {
assert!(tree.iter().any(|n| n.role() == egui::accesskit::Role::Slider && n.label() == Some(name)), "{name}");
}
// ...with the number its field shows, on the axis's range.
use crate::widgets::accessibility_tests::slider_value;
let wght = face.axes().into_iter().find(|a| &a.tag == b"wght").unwrap();
let range = (Some(f64::from(wght.min)), Some(f64::from(wght.max)));
assert_eq!(slider_value(&tree, "Weight"), (Some(400.0), range.0, range.1, Some("400".into())));
// Drag the Weight slider from its start to past its end.
let ctx = egui::Context::default();
let frame = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
let mut slider = None;
let input = egui::RawInput { events, ..Default::default() };
ctx.run_ui(input, |ui| {
axis_sliders(app, ui, &face);
slider = ctx.read_response(ui.scope_id().with(("ch-axis", *b"wght"))).map(|r| r.rect);
})
.textures_delta
.clear();
slider
};
frame(&mut app, vec![]);
let r = frame(&mut app, vec![]).expect("the Weight slider is drawn");
let none = egui::Modifiers::NONE;
let (from, to) = (egui::pos2(r.left() + 4.0, r.center().y), egui::pos2(r.right() + 20.0, r.center().y));
let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: none };
frame(&mut app, vec![egui::Event::PointerMoved(from), button(from, true)]);
for i in 1..=4 {
frame(&mut app, vec![egui::Event::PointerMoved(from + (to - from) * i as f32 / 4.0)]);
}
frame(&mut app, vec![button(to, false)]);
frame(&mut app, vec![]);
let axes = |app: &VectorcraftApp| shared(app, |s| s.font_axes.clone()).unwrap();
assert_eq!(axes(&app).into_iter().collect::<Vec<_>>(), [("wght".to_string(), 700.0)]);
app.run("edit.undo", json!({})).unwrap();
assert!(axes(&app).is_empty(), "one undo step");
// Regular and Bold selected: the weights differ and the field is blank; the widths agree.
let bold = app.run("text.create", json!({"x": 20, "y": 160, "text": "lIn", "font": TWO_AXIS_FAMILY, "style": "Bold"})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [id, bold]})).unwrap();
let text = crate::tests_labels::painted_text(&mut app, |app, ui| axis_sliders(app, ui, &face));
assert!(!text.contains("400") && !text.contains("700") && text.contains("100"), "{text}");
let tree = crate::widgets::accessibility_tests::tree(|ui| axis_sliders(&mut app, ui, &face));
assert_eq!(slider_value(&tree, "Weight"), (None, None, None, None), "a blank field: no value");
assert_eq!(slider_value(&tree, "Width").0, Some(100.0));
// Reset Panel leaves the settings, as it leaves the font and style.
app.run("select.set", json!({"ids": [id]})).unwrap();
app.run("text.setFormat", json!({"fontAxes": {"wght": 650}})).unwrap();
reset_panel(&mut app);
assert_eq!(axes(&app).into_iter().collect::<Vec<_>>(), [("wght".to_string(), 650.0)]);
}
/// Bold and Italic pick the family's own faces by name, and toggle back (#724).
#[test]
fn bold_and_italic_pick_the_familys_own_faces() {
let styles: Vec<String> =
["Light", "Narrow", "Regular", "Italic", "Narrow Italic", "SemiBold", "Bold", "Bold Condensed", "Narrow Bold", "Bold Italic", "Black"]
.iter()
.map(|s| s.to_string())
.collect();
assert_eq!(pick_face(&styles, true, false, "Regular"), Some("Bold"), "not Bold Condensed, Narrow Bold or SemiBold");
assert_eq!(pick_face(&styles, true, true, "Italic"), Some("Bold Italic"));
assert_eq!(pick_face(&styles, false, true, "Regular"), Some("Italic"));
assert_eq!(pick_face(&styles, false, false, "Italic"), Some("Regular"), "upright again, not Narrow");
assert_eq!(pick_face(&styles, true, false, "Narrow"), Some("Narrow Bold"), "a Narrow face stays Narrow");
assert_eq!(pick_face(&styles, false, true, "Narrow"), Some("Narrow Italic"));
let oblique: Vec<String> = ["Book", "Oblique", "Bold-Oblique"].iter().map(|s| s.to_string()).collect();
assert_eq!(pick_face(&oblique, true, true, "Oblique"), Some("Bold-Oblique"));
assert_eq!(pick_face(&oblique, false, false, "Oblique"), None, "Book has a weight word: no regular face");
assert_eq!(face_of("Bold Italic"), (true, false, true, vec![]));
assert_eq!(face_of("SemiBold"), (false, false, false, vec![]));
assert_eq!(face_of("Narrow Italic"), (false, true, true, vec!["narrow".to_string()]));
// No type selected: a message, nothing changed.
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
assert!(toggle_face(&mut app, Face::Bold).is_err());
assert!(!crate::menus::enabled(&app, "type.bold"));
}
/// From one frame of the panel: a point inside each of the Small Caps, Superscript and
/// Subscript buttons, and which of them are drawn on.
fn script_buttons(ctx: &egui::Context, app: &mut VectorcraftApp) -> ([egui::Pos2; 3], [bool; 3]) {
let (points, on) = style_buttons(ctx, app);
([points[1], points[2], points[3]], [on[1], on[2], on[3]])
}
/// From one frame of the panel: a point inside each of the All Caps, Small Caps, Superscript,
/// Subscript, Underline and Strikethrough buttons, and which of them are drawn on. The two buttons
/// with a "1" are Superscript (the "1" up) and Subscript; the others sit one button apart from them
/// in that order.
fn style_buttons(ctx: &egui::Context, app: &mut VectorcraftApp) -> ([egui::Pos2; 6], [bool; 6]) {
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| show(app, ui));
out.textures_delta.clear();
fn walk(s: &egui::Shape, on: egui::Color32, ones: &mut Vec<egui::Pos2>, lit: &mut Vec<egui::Rect>) {
match s {
egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, on, ones, lit)),
egui::Shape::Text(t) if t.galley.text() == "1" => ones.push(t.visual_bounding_rect().center()),
egui::Shape::Rect(r) if r.fill == on && r.rect.size() == vec2(30.0, 26.0) => lit.push(r.rect),
_ => {}
}
}
let (mut ones, mut lit) = (vec![], vec![]);
for c in &out.shapes {
walk(&c.shape, Tokens::get(ctx).tool_active, &mut ones, &mut lit);
}
let [sup, sub] = ones[..] else { panic!("two script buttons: {ones:?}") };
assert!(sup.x < sub.x && sup.y < sub.y, "Superscript, then Subscript: {ones:?}");
// Each "1" is drawn 6 points right of its button's center and 5 points above or below it.
let y = (sup.y + sub.y) / 2.0;
let (sup, sub) = (egui::pos2(sup.x - 6.0, y), egui::pos2(sub.x - 6.0, y));
let step = vec2(sub.x - sup.x, 0.0);
let points = [sup - step * 2.0, sup - step, sup, sub, sub + step, sub + step * 2.0];
(points, points.map(|p| lit.iter().any(|r| r.contains(p))))
}
/// Each run's (small caps, position).
fn scripts(app: &VectorcraftApp, id: &Value) -> Vec<(Option<f64>, &'static str)> {
match &app.session.active().unwrap().doc.node(NodeId(id.as_u64().unwrap())).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| (r.style.small_caps, r.style.position.id())).collect(),
_ => panic!("text"),
}
}
/// The Small Caps, Superscript and Subscript buttons set the selected type's small caps and
/// position, with one undo step for each click, and show them; Superscript and Subscript turn
/// each other off. While the Type tool edits, they set and show the selected characters.
#[test]
fn small_caps_superscript_and_subscript_buttons_set_and_show_the_text() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "E=mc2", "size": 12})).unwrap()["id"].clone();
let ctx = egui::Context::default();
let (at, lit) = script_buttons(&ctx, &mut app);
assert_eq!(lit, [false; 3]);
let small = Some(app.session.active().unwrap().doc.setup.small_caps_size);
for (button, runs, shown) in [
(0, (small, "normal"), [true, false, false]),
(1, (small, "superscript"), [true, true, false]),
(2, (small, "subscript"), [true, false, true]),
(2, (small, "normal"), [true, false, false]),
(1, (small, "superscript"), [true, true, false]),
(1, (small, "normal"), [true, false, false]),
(0, (None, "normal"), [false; 3]),
] {
let undo = app.session.active().unwrap().history.undo.len();
super::super::tests_appearance::click(&ctx, &mut app, at[button], show);
assert_eq!(scripts(&app, &id), [runs], "button {button}");
assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1, "button {button}");
assert_eq!(script_buttons(&ctx, &mut app).1, shown, "button {button}");
}
// The Type tool's selection: "2" alone.
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 4, "end": 5}));
assert!(text_editing(&app).is_some());
super::super::tests_appearance::click(&ctx, &mut app, at[1], show);
assert_eq!(scripts(&app, &id), [(None, "normal"), (None, "superscript")]);
assert_eq!(script_buttons(&ctx, &mut app).1, [false, true, false]);
app.session.set_tool_option("select", &json!({"start": 0, "end": 4}));
assert_eq!(script_buttons(&ctx, &mut app).1, [false; 3]);
}
/// The panel menu's Small Caps, Superscript and Subscript set the selected type's small caps
/// and position and show a check mark when on; Superscript and Subscript turn each other off.
#[test]
fn small_caps_superscript_and_subscript_menu_items_set_and_check_the_text() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "E=mc2", "size": 12})).unwrap()["id"].clone();
let ctx = egui::Context::default();
let checked = |app: &mut VectorcraftApp| {
let texts = super::super::tests_appearance::frame_events(&ctx, app, vec![], menu);
["Small Caps", "Superscript", "Subscript"].map(|l| texts.iter().any(|(t, _)| *t == format!("✓ {l}")))
};
assert_eq!(checked(&mut app), [false; 3]);
let small = Some(app.session.active().unwrap().doc.setup.small_caps_size);
for (item, runs, shown) in [
("Small Caps", (small, "normal"), [true, false, false]),
("Superscript", (small, "superscript"), [true, true, false]),
("Subscript", (small, "subscript"), [true, false, true]),
("Subscript", (small, "normal"), [true, false, false]),
("Small Caps", (None, "normal"), [false; 3]),
] {
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == item).unwrap_or_else(|| panic!("{item}: {texts:?}")).1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert_eq!(scripts(&app, &id), [runs], "{item}");
assert_eq!(checked(&mut app), shown, "{item}");
}
}
/// Character panel menu › System Layout is off for a new document, and a click turns whole-point
/// spacing on and back off for every text object.
#[test]
fn system_layout_menu_item_toggles_the_document() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Hi", "size": 13})).unwrap()["id"].as_u64().unwrap();
let ctx = egui::Context::default();
let on = |app: &VectorcraftApp| {
let st = app.session.active().unwrap();
let object = match &st.doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.system_layout,
_ => panic!("text"),
};
(st.doc.setup.system_layout, object)
};
let row = |app: &mut VectorcraftApp| {
let texts = super::super::tests_appearance::frame_events(&ctx, app, vec![], menu);
texts.into_iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "System Layout")
};
assert_eq!(on(&app), (false, false));
assert!(row(&mut app).is_some_and(|(t, _)| !t.starts_with('✓')));
let at = row(&mut app).unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert_eq!(on(&app), (true, true));
assert!(row(&mut app).is_some_and(|(t, _)| t.starts_with('✓')));
let at = row(&mut app).unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert_eq!(on(&app), (false, false));
assert!(row(&mut app).is_some_and(|(t, _)| !t.starts_with('✓')));
}
/// Character panel menu › Fractional Widths is on for new type, and a click turns it off and
/// back on for the whole type object.
#[test]
fn fractional_widths_menu_item_toggles_the_type_object() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Hi", "size": 13})).unwrap()["id"].as_u64().unwrap();
let ctx = egui::Context::default();
let flag = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.fractional_widths,
_ => panic!("text"),
};
let row = |app: &mut VectorcraftApp| {
let texts = super::super::tests_appearance::frame_events(&ctx, app, vec![], menu);
texts.into_iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "Fractional Widths")
};
assert!(flag(&app));
assert!(row(&mut app).is_some_and(|(t, _)| t.starts_with('✓')));
let at = row(&mut app).unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(!flag(&app));
assert!(row(&mut app).is_some_and(|(t, _)| !t.starts_with('✓')));
let at = row(&mut app).unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(flag(&app));
assert!(row(&mut app).is_some_and(|(t, _)| t.starts_with('✓')));
}
/// Character Alignment and Proportional Metrics are in the panel menu with the East Asian
/// options only (as Mojikumi Set and Top-to-Top Leading are in the Paragraph panel).
#[test]
fn character_alignment_shows_with_the_east_asian_options() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "雅楽"})).unwrap()["id"].clone();
app.session.execute("select.set", &json!({"ids": [id]})).unwrap();
let shown = |app: &mut VectorcraftApp| {
let text = crate::tests_labels::painted_text(app, menu);
(text.contains("Character Alignment"), text.contains("Proportional Metrics"))
};
assert_eq!(shown(&mut app), (false, false));
app.session.prefs.show_east_asian_options = true;
assert_eq!(shown(&mut app), (true, true));
}
/// The panel menu's Show Font Height Options shows the Set Font Height Reference dropdown,
/// which shows the selected type's reference and sets the one picked (one undo step); Hide
/// Font Height Options hides it again.
#[test]
fn show_font_height_options_shows_the_font_height_reference() {
use super::super::tests_appearance::{click, frame_events, text_rect};
use vectorcraft_doc::FontHeightReference as R;
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "Hx", "size": 12})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let reference = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| r.style.font_height_reference).collect::<Vec<_>>(),
_ => panic!("text"),
};
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
let texts = |ctx: &egui::Context, app: &mut VectorcraftApp, f: fn(&mut VectorcraftApp, &mut Ui)| {
frame_events(ctx, app, vec![], f);
frame_events(ctx, app, vec![], f)
};
// A menu item's label is drawn after its check column (a check mark or spaces).
let item = |texts: &[(String, egui::Rect)], s: &str| texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == s).map(|(_, r)| r.center());
assert!(item(&texts(&ctx, &mut app, show), "Em Box").is_none());
let at = item(&texts(&ctx, &mut app, menu), "Show Font Height Options").unwrap();
click(&ctx, &mut app, at, menu);
assert!(item(&texts(&ctx, &mut app, menu), "Hide Font Height Options").is_some());
let panel = texts(&ctx, &mut app, show);
assert!(item(&panel, "Em Box").is_some(), "{panel:?}");
// Pick Cap Height from the dropdown.
click(&ctx, &mut app, text_rect(&panel, "Em Box").center(), show);
let undo = app.session.active().unwrap().history.undo.len();
let open = texts(&ctx, &mut app, show);
click(&ctx, &mut app, text_rect(&open, "Cap Height").center(), show);
assert_eq!(reference(&app), [R::CapHeight]);
assert_eq!(app.session.active().unwrap().history.undo.len(), undo + 1);
assert!(item(&texts(&ctx, &mut app, show), "Cap Height").is_some());
let at = item(&texts(&ctx, &mut app, menu), "Hide Font Height Options").unwrap();
click(&ctx, &mut app, at, menu);
assert!(item(&texts(&ctx, &mut app, show), "Cap Height").is_none());
}
/// Tate-chu-yoko is in the panel menu with the East Asian options, and choosing it marks the
/// selected characters.
#[test]
fn tate_chu_yoko_menu_item_shows_with_east_asian_options_and_toggles() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "2026"})).unwrap()["id"].as_u64().unwrap();
app.session.execute("select.set", &json!({"ids": [id]})).unwrap();
let ctx = egui::Context::default();
let painted = |app: &mut VectorcraftApp| crate::tests_labels::painted_text(app, menu);
assert!(!painted(&mut app).contains("Tate-chu-yoko"));
app.session.prefs.show_east_asian_options = true;
let on = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().all(|r| r.style.tate_chu_yoko),
_ => panic!("text"),
};
assert!(!on(&app));
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "Tate-chu-yoko").unwrap_or_else(|| panic!("{texts:?}")).1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(on(&app));
assert!(painted(&mut app).contains("✓ Tate-chu-yoko"));
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "Tate-chu-yoko").unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(!on(&app));
}
/// Tate-chu-yoko Settings… is in the panel menu with the East Asian options, and choosing it opens
/// the dialog on the selected characters.
#[test]
fn tate_chu_yoko_settings_menu_item_shows_with_east_asian_options_and_opens_the_dialog() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "2026"})).unwrap()["id"].as_u64().unwrap();
app.session.execute("select.set", &json!({"ids": [id]})).unwrap();
let ctx = egui::Context::default();
assert!(!crate::tests_labels::painted_text(&mut app, menu).contains("Tate-chu-yoko Settings…"));
app.session.prefs.show_east_asian_options = true;
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim() == "Tate-chu-yoko Settings…").unwrap_or_else(|| panic!("{texts:?}")).1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some(crate::dialogs::tate_chu_yoko::KIND));
}
/// Standard Vertical Roman Alignment is in the panel menu with the East Asian options. Choosing it
/// marks the selected characters, one undo step each way; its check shows only while every
/// selected character has it, and a mixed selection turns it on.
#[test]
fn standard_vertical_roman_alignment_menu_item_toggles() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "ABC"})).unwrap()["id"].as_u64().unwrap();
app.session.execute("select.set", &json!({"ids": [id]})).unwrap();
let ctx = egui::Context::default();
let label = "Standard Vertical Roman Alignment";
assert!(!crate::tests_labels::painted_text(&mut app, menu).contains(label));
app.session.prefs.show_east_asian_options = true;
let on = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().all(|r| r.style.standard_vertical_roman),
_ => panic!("text"),
};
// Only the first letter has it: the item is unchecked.
app.session.execute("text.setRangeStyle", &json!({"id": id, "start": 0, "end": 1, "standardVerticalRoman": true})).unwrap();
let undo = |app: &VectorcraftApp| app.session.doc().unwrap().history.undo.len();
let before = undo(&app);
assert!(!on(&app));
assert!(!crate::tests_labels::painted_text(&mut app, menu).contains(&format!("✓ {label}")));
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == label).unwrap_or_else(|| panic!("{texts:?}")).1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(on(&app));
assert_eq!(undo(&app), before + 1);
assert!(crate::tests_labels::painted_text(&mut app, menu).contains(&format!("✓ {label}")));
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == label).unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(!on(&app));
assert_eq!(undo(&app), before + 2);
}
/// Digits is in the panel menu with the Middle Eastern (Indic) options only, as Paragraph
/// Direction is in the Paragraph panel.
#[test]
fn digits_show_with_the_indic_options() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "صفحة 3"})).unwrap()["id"].clone();
app.session.execute("select.set", &json!({"ids": [id]})).unwrap();
let shown = |app: &mut VectorcraftApp| crate::tests_labels::painted_text(app, menu).contains("Digits");
assert!(!shown(&mut app));
app.session.prefs.show_indic_options = true;
assert!(shown(&mut app));
}
/// The Digits submenu lists Default, the Arabic-script numeral systems and the Indic scripts'
/// digits (#4942), and checks the selected characters' system.
#[test]
fn digits_submenu_lists_the_indic_scripts_and_checks_the_selection() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let text = crate::tests_labels::painted_text(&mut app, |app, ui| digits_items(app, ui, Some(vectorcraft_doc::Digits::Tamil)));
let items: Vec<_> = text.lines().map(|l| l.trim().trim_start_matches("✓ ")).collect();
let want =
["Default", "Arabic", "Hindi", "Farsi", "Devanagari", "Bengali", "Gurmukhi", "Gujarati", "Odia", "Tamil", "Telugu", "Kannada", "Malayalam"];
assert!(want.iter().all(|w| items.contains(w)), "{items:?}");
assert_eq!(text.lines().filter_map(|l| l.strip_prefix("✓ ")).collect::<Vec<_>>(), ["Tamil"]);
}
/// Character Direction is in the panel menu with the Middle Eastern (Indic) options, beside
/// Digits. Its submenu checks the direction the characters the panel acts on share: the
/// selected type's, or the range the Type tool has selected. It checks no item when they differ.
#[test]
fn character_direction_checks_the_direction_of_the_selection() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "שלום 21 10 2026"})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let shown = |app: &mut VectorcraftApp| crate::tests_labels::painted_text(app, menu).contains("Character Direction");
assert!(!shown(&mut app));
app.session.prefs.show_indic_options = true;
assert!(shown(&mut app));
let checked = |app: &mut VectorcraftApp| {
let text = crate::tests_labels::painted_text(app, char_direction_items);
text.lines().filter_map(|l| l.strip_prefix("✓ ")).map(str::to_string).collect::<Vec<_>>()
};
assert_eq!(checked(&mut app), ["Default"]);
app.run("text.setRangeStyle", json!({"id": id, "start": 9, "end": 19, "charDirection": "leftToRight"})).unwrap();
// The object's characters differ: nothing is checked.
assert!(checked(&mut app).is_empty());
// The Type tool's selection: the date, then the Hebrew word.
app.select_tool("type");
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 9, "end": 19}));
assert_eq!(checked(&mut app), ["Left to Right"]);
app.session.set_tool_option("select", &json!({"start": 0, "end": 8}));
assert_eq!(checked(&mut app), ["Default"]);
}
#[test]
fn shared_text_flag_optical_margin_three_object_selections() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({})).unwrap();
let a = app.run("text.create", json!({"x": 10, "y": 40, "text": "a"})).unwrap()["id"].clone();
let b = app.run("text.create", json!({"x": 10, "y": 80, "text": "b"})).unwrap()["id"].clone();
let c = app.run("text.create", json!({"x": 10, "y": 120, "text": "c"})).unwrap()["id"].clone();
app.run("type.opticalMarginAlignment", json!({"on": true, "id": a})).unwrap();
app.run("type.opticalMarginAlignment", json!({"on": false, "id": b})).unwrap();
app.run("type.opticalMarginAlignment", json!({"on": false, "id": c})).unwrap();
app.run("select.set", json!({"ids": [a.clone(), b.clone(), c.clone()]})).unwrap();
assert_eq!(shared_text_flag(&app, |t| t.optical_margin), None);
app.run("type.opticalMarginAlignment", json!({"on": false, "id": a})).unwrap();
app.run("type.opticalMarginAlignment", json!({"on": true, "id": b})).unwrap();
app.run("type.opticalMarginAlignment", json!({"on": true, "id": c})).unwrap();
app.run("select.set", json!({"ids": [a.clone(), b.clone(), c.clone()]})).unwrap();
assert_eq!(shared_text_flag(&app, |t| t.optical_margin), None);
app.run("type.opticalMarginAlignment", json!({"on": true, "id": a})).unwrap();
app.run("type.opticalMarginAlignment", json!({"on": true, "id": b})).unwrap();
app.run("type.opticalMarginAlignment", json!({"on": true, "id": c})).unwrap();
app.run("select.set", json!({"ids": [a, b, c]})).unwrap();
assert_eq!(shared_text_flag(&app, |t| t.optical_margin), Some(true));
}
#[test]
fn shared_text_flag_vertical_three_object_selections() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({})).unwrap();
let v0 = app.run("text.create", json!({"x": 10, "y": 40, "text": "v0", "vertical": true})).unwrap()["id"].clone();
let h1 = app.run("text.create", json!({"x": 10, "y": 80, "text": "h1"})).unwrap()["id"].clone();
let h2 = app.run("text.create", json!({"x": 10, "y": 120, "text": "h2"})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [v0.clone(), h1.clone(), h2.clone()]})).unwrap();
assert_eq!(shared_text_flag(&app, |t| t.vertical), None);
let h0 = app.run("text.create", json!({"x": 10, "y": 160, "text": "h0"})).unwrap()["id"].clone();
let v1 = app.run("text.create", json!({"x": 10, "y": 200, "text": "v1", "vertical": true})).unwrap()["id"].clone();
let v2 = app.run("text.create", json!({"x": 10, "y": 240, "text": "v2", "vertical": true})).unwrap()["id"].clone();
app.run("select.set", json!({"ids": [h0.clone(), v1.clone(), v2.clone()]})).unwrap();
assert_eq!(shared_text_flag(&app, |t| t.vertical), None);
app.run("select.set", json!({"ids": [v0.clone(), v1.clone(), v2.clone()]})).unwrap();
assert_eq!(shared_text_flag(&app, |t| t.vertical), Some(true));
}
/// Small Caps, Superscript and Subscript buttons and menu items stay off when the selection mixes them.
#[test]
fn character_mixed_small_caps_and_position_show_off() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let a = app.run("text.create", json!({"x": 20, "y": 50, "text": "A"})).unwrap()["id"].clone();
let b = app.run("text.create", json!({"x": 20, "y": 90, "text": "B"})).unwrap()["id"].clone();
app.run("text.setRangeStyle", json!({"id": a, "start": 0, "end": 1, "smallCaps": true, "position": "superscript"})).unwrap();
app.run("text.setRangeStyle", json!({"id": b, "start": 0, "end": 1, "smallCaps": false, "position": "subscript"})).unwrap();
app.run("select.set", json!({"ids": [a, b]})).unwrap();
assert_eq!(shared(&app, |s| s.small_caps.is_some()), None);
assert_eq!(shared(&app, |s| s.position), None);
let ctx = egui::Context::default();
assert_eq!(script_buttons(&ctx, &mut app).1, [false, false, false]);
let checked = |app: &mut VectorcraftApp| {
let texts = super::super::tests_appearance::frame_events(&ctx, app, vec![], menu);
["Small Caps", "Superscript", "Subscript"].map(|l| texts.iter().any(|(t, _)| *t == format!("✓ {l}")))
};
assert_eq!(checked(&mut app), [false, false, false]);
}
/// The All Caps, Underline and Strikethrough buttons are drawn off when only some of the selected
/// characters have them, and a click turns them on for every character.
#[test]
fn mixed_all_caps_underline_and_strikethrough_buttons_show_off_and_turn_on() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "AB", "size": 12})).unwrap()["id"].as_u64().unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 0, "end": 1, "allCaps": true, "underline": true, "strikethrough": true})).unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let flags = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| [r.style.all_caps, r.style.underline, r.style.strikethrough]).collect::<Vec<_>>(),
_ => panic!("text"),
};
let ctx = egui::Context::default();
let (at, lit) = style_buttons(&ctx, &mut app);
assert_eq!([lit[0], lit[4], lit[5]], [false; 3]);
for (i, button) in [0, 4, 5].into_iter().enumerate() {
super::super::tests_appearance::click(&ctx, &mut app, at[button], show);
assert!(flags(&app).iter().all(|f| f[i]), "button {button}: {:?}", flags(&app));
assert!(style_buttons(&ctx, &mut app).1[button], "button {button}");
}
}
/// `type.underline` does what the Underline button does: with only some of the selected characters
/// underlined it underlines all of them, and with all of them underlined it turns underlining off.
#[test]
fn underline_command_underlines_a_mixed_selection() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "AB", "size": 12})).unwrap()["id"].as_u64().unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 0, "end": 1, "underline": true})).unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let underlined = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| r.style.underline).collect::<Vec<_>>(),
_ => panic!("text"),
};
assert_eq!(super::toggle_underline(&mut app), Ok(json!(true)));
assert!(underlined(&app).iter().all(|u| *u), "{:?}", underlined(&app));
assert_eq!(super::toggle_underline(&mut app), Ok(json!(false)));
assert!(underlined(&app).iter().all(|u| !*u), "{:?}", underlined(&app));
}
/// The panel menu's All Caps, Underline, Strikethrough, Proportional Metrics, Tate-chu-yoko and
/// No Break are unchecked when only some of the selected characters have them, and choosing one
/// turns it on for every character.
#[test]
fn mixed_check_items_show_unchecked_and_turn_on() {
type Attribute = fn(&CharStyle) -> bool;
let items: [(&str, &str, Attribute); 6] = [
("All Caps", "allCaps", |s| s.all_caps),
("Underline", "underline", |s| s.underline),
("Strikethrough", "strikethrough", |s| s.strikethrough),
("Proportional Metrics", "proportionalMetrics", |s| s.proportional_metrics),
("Tate-chu-yoko", "tateChuYoko", |s| s.tate_chu_yoko),
("No Break", "noBreak", |s| s.no_break),
];
let mut wrong = vec![];
for (label, key, has) in items {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.session.prefs.show_east_asian_options = true;
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "AB"})).unwrap()["id"].as_u64().unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 0, "end": 1, key: true})).unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let on = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| has(&r.style)).collect::<Vec<_>>(),
_ => panic!("text"),
};
assert_eq!(on(&app), [true, false], "{label}");
let ctx = egui::Context::default();
let row = |app: &mut VectorcraftApp| {
let texts = super::super::tests_appearance::frame_events(&ctx, app, vec![], menu);
texts.into_iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == label).unwrap_or_else(|| panic!("{label} missing"))
};
let (text, at) = row(&mut app);
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
let shown = row(&mut app).0;
if text.starts_with('✓') || !on(&app).iter().all(|o| *o) || !shown.starts_with('✓') {
wrong.push(format!("{label}: shown as {text:?}, then {:?} and {shown:?}", on(&app)));
}
}
assert!(wrong.is_empty(), "{wrong:#?}");
}
/// The panel menu's Character Alignment and Digits submenus check no item when the selected
/// characters differ, and check the value they share.
#[test]
fn character_alignment_and_digits_check_nothing_for_a_mixed_selection() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.session.prefs.show_east_asian_options = true;
app.session.prefs.show_indic_options = true;
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "A1"})).unwrap()["id"].as_u64().unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 0, "end": 1, "charAlign": "emBoxTop", "digits": "hindi"})).unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let press = |pos: egui::Pos2, pressed: bool| egui::Event::PointerButton {
pos,
button: egui::PointerButton::Primary,
pressed,
modifiers: Default::default(),
};
let ticked = |texts: &[(String, egui::Rect)]| texts.iter().filter_map(|(t, _)| t.strip_prefix("✓ ")).map(str::to_string).collect::<Vec<_>>();
// The items checked in the submenu `name` opens (not in the panel menu itself), in a fresh
// context, so that no other submenu is open.
let checked = |app: &mut VectorcraftApp, name: &str| {
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
let mut frame = |events: Vec<egui::Event>| super::super::tests_appearance::frame_events(&ctx, app, events, menu);
let texts = frame(vec![]);
let before = ticked(&texts);
let sub = texts.iter().find(|(t, _)| t.trim() == name).unwrap_or_else(|| panic!("{name} missing from {texts:?}")).1.center();
for e in [egui::Event::PointerMoved(sub), press(sub, true), press(sub, false)] {
frame(vec![e]);
}
let texts = frame(vec![]);
assert!(texts.iter().any(|(t, _)| t.contains("Roman Baseline") || t.contains("Hindi")), "{name} did not open: {texts:?}");
ticked(&texts).into_iter().filter(|t| !before.contains(t)).collect::<Vec<_>>()
};
let mixed = (checked(&mut app, "Character Alignment"), checked(&mut app, "Digits"));
assert_eq!(mixed, (vec![], vec![]));
app.run("text.setRangeStyle", json!({"id": id, "charAlign": "emBoxTop", "digits": "hindi"})).unwrap();
let same = (checked(&mut app, "Character Alignment"), checked(&mut app, "Digits"));
assert_eq!(same, (vec!["Em Box Top/Right".to_string()], vec!["Hindi".to_string()]));
}
/// At a caret in text the Type tool edits, with no characters selected, the Character
/// Direction items are disabled: a click on one leaves the direction of every character as it was.
#[test]
fn character_direction_does_nothing_at_an_empty_caret() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "שלום 21 10 2026"})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
app.select_tool("type");
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 3, "end": 3}));
let directions = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| (r.text.clone(), r.style.char_direction)).collect::<Vec<_>>(),
_ => panic!("text"),
};
let before = directions(&app);
let ctx = egui::Context::default();
let click_rtl = |app: &mut VectorcraftApp| {
let texts = super::super::tests_appearance::frame_events(&ctx, app, vec![], char_direction_items);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "Right to Left").unwrap_or_else(|| panic!("{texts:?}")).1;
super::super::tests_appearance::click(&ctx, app, at.center(), char_direction_items);
};
click_rtl(&mut app);
assert_eq!(directions(&app), before);
// With characters selected, the same click sets their direction.
app.session.set_tool_option("select", &json!({"start": 0, "end": 4}));
click_rtl(&mut app);
assert_ne!(directions(&app), before);
}
/// No Break is in the panel menu, and choosing it marks the selected characters.
#[test]
fn no_break_menu_item_toggles_the_selected_characters() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "Hello"})).unwrap()["id"].as_u64().unwrap();
app.session.execute("select.set", &json!({"ids": [id]})).unwrap();
let ctx = egui::Context::default();
let painted = |app: &mut VectorcraftApp| crate::tests_labels::painted_text(app, menu);
assert!(painted(&mut app).contains("No Break"));
assert!(!painted(&mut app).contains("✓ No Break"));
let on = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().all(|r| r.style.no_break),
_ => panic!("text"),
};
assert!(!on(&app));
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "No Break").unwrap_or_else(|| panic!("{texts:?}")).1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(on(&app));
assert!(painted(&mut app).contains("✓ No Break"));
let texts = super::super::tests_appearance::frame_events(&ctx, &mut app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "No Break").unwrap().1;
super::super::tests_appearance::click(&ctx, &mut app, at.center(), menu);
assert!(!on(&app));
}
/// Reset Panel sets every character attribute the panel shows, except the font family, style
/// and size, back to its default (Small Caps, the position, Character Alignment, Proportional
/// Metrics, Tate-chu-yoko and its settings, No Break, Character Direction, Digits and the font height reference
/// included), in the selected type and in the text the Type tool has selected while it edits.
#[test]
fn reset_panel_resets_every_attribute_but_the_font() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "One 12"})).unwrap()["id"].as_u64().unwrap();
let runs = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| (r.text.clone(), r.style.clone())).collect::<Vec<_>>(),
_ => panic!("text"),
};
let differs = |s: &CharStyle, d: &CharStyle| {
[
s.tracking != d.tracking,
s.leading != d.leading,
s.kerning != d.kerning,
s.baseline_shift != d.baseline_shift,
s.h_scale != d.h_scale,
s.v_scale != d.v_scale,
s.rotation != d.rotation,
s.underline != d.underline,
s.strikethrough != d.strikethrough,
s.all_caps != d.all_caps,
s.small_caps != d.small_caps,
s.position != d.position,
s.char_align != d.char_align,
s.proportional_metrics != d.proportional_metrics,
s.tate_chu_yoko != d.tate_chu_yoko,
s.tcy_up_down != d.tcy_up_down,
s.tcy_left_right != d.tcy_left_right,
s.standard_vertical_roman != d.standard_vertical_roman,
s.no_break != d.no_break,
s.char_direction != d.char_direction,
s.digits != d.digits,
s.font_height_reference != d.font_height_reference,
]
};
let custom = |app: &mut VectorcraftApp| {
app.run(
"text.setRangeStyle",
json!({
"id": id, "tracking": 50, "leading": 30, "kerning": 20, "baselineShift": 3, "hScale": 120, "vScale": 90, "rotation": 15,
"underline": true, "strikethrough": true, "allCaps": true, "smallCaps": true, "position": "superscript",
"charAlign": "emBoxCenter", "proportionalMetrics": true, "tateChuYoko": true, "tcyUpDown": 2, "tcyLeftRight": -3,
"standardVerticalRoman": true, "noBreak": true, "charDirection": "rightToLeft", "digits": "hindi", "fontHeightReference": "capHeight"
}),
)
.unwrap();
};
let plain = runs(&app)[0].1.clone();
assert_eq!(differs(&plain, &CharStyle::default()), [false; 22]);
custom(&mut app);
let styled = runs(&app)[0].1.clone();
assert_eq!(differs(&styled, &CharStyle::default()), [true; 22]);
app.run("select.set", json!({"ids": [id]})).unwrap();
reset_panel(&mut app);
assert_eq!(runs(&app), [("One 12".to_string(), plain.clone())]);
// While the Type tool edits text, Reset Panel resets only its selection.
custom(&mut app);
app.select_tool("type");
app.session.apply_actions(vec![vectorcraft_tools::Action::Notify("text.editNew".into())]).unwrap();
app.session.set_tool_option("select", &json!({"start": 4, "end": 6}));
assert!(text_editing(&app).is_some());
reset_panel(&mut app);
assert_eq!(runs(&app), [("One ".to_string(), styled), ("12".to_string(), plain)]);
}
/// #4232: the panel menu's Show Snap to Glyph Options shows the Snap to Glyph section (with no
/// type selected too) and Hide Snap to Glyph Options hides it; its checkboxes, dimmed while View ›
/// Snap to Glyph is off, set the options.
#[test]
fn snap_to_glyph_options_show_from_the_menu_and_set_the_options() {
use super::super::tests_appearance::{click, frame_events, text_rect};
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let ctx = egui::Context::default();
let toggle = |app: &mut VectorcraftApp, label: &str| {
let texts = frame_events(&ctx, app, vec![], menu);
let at = texts.iter().find(|(t, _)| t.trim() == label).unwrap_or_else(|| panic!("{label} in {texts:?}")).1;
click(&ctx, app, at.center(), menu);
};
let section = |app: &mut VectorcraftApp| frame_events(&ctx, app, vec![], show);
let labels = ["Baseline", "x-height", "Glyph Bounds", "Proximity Guides", "Angular Guides", "Anchor Point"];
assert!(!section(&mut app).iter().any(|(t, _)| t == "Proximity Guides"), "hidden at first");
toggle(&mut app, "Show Snap to Glyph Options");
let texts = section(&mut app);
assert!(labels.iter().all(|l| texts.iter().any(|(t, _)| t == l)), "{texts:?}");
let check = |app: &mut VectorcraftApp, label: &str| {
let at = text_rect(§ion(app), label).center();
click(&ctx, app, at, show);
};
check(&mut app, "Angular Guides");
assert!(app.session.prefs.glyph_snap.angular, "dimmed while Snap to Glyph is off");
app.ui.view.snap_to_glyph = true;
check(&mut app, "Angular Guides");
check(&mut app, "x-height");
let o = app.session.prefs.glyph_snap;
assert!(!o.angular && !o.x_height && o.baseline && o.bounds && o.proximity && o.anchors, "{o:?}");
check(&mut app, "Angular Guides");
assert!(app.session.prefs.glyph_snap.angular);
toggle(&mut app, "Hide Snap to Glyph Options");
assert!(!section(&mut app).iter().any(|(t, _)| t == "Proximity Guides"));
}