Type: Superscript and Subscript check the characters they change

At a Type tool caret with no characters selected, the Superscript and
Subscript buttons, menu items and commands change every character of
the text, but decided from the caret's style alone. Text that was only
partly superscript then lost its superscript everywhere when the caret
sat after a superscript character. They now read every character of
the text there.

The position compare now looks at the kind only, so superscripts in
different proportions (from an opened file) count as superscript and
a press turns them back to normal.
This commit is contained in:
dotCooCoo
2026-11-16 07:25:12 -08:00
parent f2bbcdacaa
commit 80b30485c2
5 changed files with 74 additions and 9 deletions
+2 -2
View File
@@ -404,13 +404,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"type.superscript",
"Superscript",
"Cmd+Shift+=",
"{} the Character panel's Superscript button: the selected type (or the Type tool's selection) becomes superscript, or Normal when all of it is superscript already (text.setFormat {position}). Without selected type the keys zoom in, as Cmd+= does → \"superscript\" | \"normal\"",
"{} the Character panel's Superscript button: the selected type (or the Type tool's selection, all of its text at a caret) becomes superscript, or Normal when all of it is superscript already (text.setFormat {position}). Without selected type the keys zoom in, as Cmd+= does → \"superscript\" | \"normal\"",
),
(
"type.subscript",
"Subscript",
"Cmd+Alt+Shift+=",
"{} the Character panel's Subscript button: the selected type (or the Type tool's selection) becomes subscript, or Normal when all of it is subscript already (text.setFormat {position}) → \"subscript\" | \"normal\"",
"{} the Character panel's Subscript button: the selected type (or the Type tool's selection, all of its text at a caret) becomes subscript, or Normal when all of it is subscript already (text.setFormat {position}) → \"subscript\" | \"normal\"",
),
(
"type.lineComposer",
+2 -2
View File
@@ -405,13 +405,13 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"type.superscript",
"Superscript",
"Cmd+Shift+=",
"{} the Character panel's Superscript button: the selected type (or the Type tool's selection) becomes superscript, or Normal when all of it is superscript already (text.setFormat {position}). Without selected type the keys zoom in, as Cmd+= does → \"superscript\" | \"normal\"",
"{} the Character panel's Superscript button: the selected type (or the Type tool's selection, all of its text at a caret) becomes superscript, or Normal when all of it is superscript already (text.setFormat {position}). Without selected type the keys zoom in, as Cmd+= does → \"superscript\" | \"normal\"",
),
(
"type.subscript",
"Subscript",
"Cmd+Alt+Shift+=",
"{} the Character panel's Subscript button: the selected type (or the Type tool's selection) becomes subscript, or Normal when all of it is subscript already (text.setFormat {position}) → \"subscript\" | \"normal\"",
"{} the Character panel's Subscript button: the selected type (or the Type tool's selection, all of its text at a caret) becomes subscript, or Normal when all of it is subscript already (text.setFormat {position}) → \"subscript\" | \"normal\"",
),
(
"type.lineComposer",
+22 -4
View File
@@ -549,8 +549,7 @@ pub(crate) fn toggle_underline(app: &mut VectorcraftApp) -> Result<Value, String
/// has: `"normal"` only when every character had `position`, else `position`.
pub(crate) fn toggle_position(app: &mut VectorcraftApp, position: &str) -> Result<Value, String> {
text_style(app).ok_or("select some type first")?;
let now = shared(app, |s| s.position).map(|p| p.id()).unwrap_or("");
let change = position_toggle(now, position);
let change = position_toggle(shared_position(app), position);
let to = change["position"].clone();
run_format(app, change)?;
Ok(to)
@@ -1451,7 +1450,7 @@ fn attributes(app: &mut VectorcraftApp, ui: &mut Ui, owner: crate::font_menu::Wh
// On only when every character the panel acts on has it; a mixed selection turns it on.
let all_caps = shared(app, |s| s.all_caps) == Some(true);
let small = shared(app, |s| s.small_caps.is_some()) == Some(true);
let position = shared(app, |s| s.position).map(|p| p.id()).unwrap_or("");
let position = shared_position(app);
let underline = shared(app, |s| s.underline) == Some(true);
let strike = shared(app, |s| s.strikethrough) == Some(true);
for (kind, tip, on, change) in [
@@ -1480,6 +1479,25 @@ fn attributes(app: &mut VectorcraftApp, ui: &mut Ui, owner: crate::font_menu::Wh
});
}
/// The position kind (`normal`, `superscript` or `subscript`) that every character a Superscript
/// or Subscript toggle changes has, else `""`: at a Type tool caret with no characters selected
/// every character of the text (of each frame of a threaded story), as the toggle changes all of
/// it there ([`run_format`], [`range_style`]), else the characters [`shared`] reads. Superscripts
/// in different proportions count as superscript.
fn shared_position(app: &VectorcraftApp) -> &'static str {
let Some((id, _, _)) = text_editing(app).filter(|&(_, a, b)| b <= a) else {
return shared(app, |s| s.position.id()).unwrap_or("");
};
let Some(st) = app.session.active() else { return "" };
let mut acc = Shared::None;
for frame in text_story(app).unwrap_or_else(|| vec![id]) {
if let Some(NodeKind::Text(t)) = st.doc.node(frame).map(|n| &n.kind) {
each_paragraph_run(t, None, |_, style, _| acc.add(style.position.id()));
}
}
acc.value().unwrap_or("")
}
/// The `text.setFormat` params of the Superscript or Subscript toggle `p` for text whose
/// position is `position`: `normal` when `p` is on, else `p`, which turns the other one off.
fn position_toggle(position: &str, p: &str) -> Value {
@@ -1627,7 +1645,7 @@ pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
}
}
let small = shared(app, |s| s.small_caps.is_some()) == Some(true);
let position = shared(app, |s| s.position).map(|p| p.id()).unwrap_or("");
let position = shared_position(app);
for (label, on, change) in [
(tl!("Small Caps"), small, json!({"smallCaps": !small})),
(tl!("Superscript"), position == "superscript", position_toggle(position, "superscript")),
@@ -583,6 +583,51 @@ fn small_caps_superscript_and_subscript_buttons_set_and_show_the_text() {
assert_eq!(script_buttons(&ctx, &mut app).1, [false; 3]);
}
/// Superscript and Subscript decide from the characters they change. At a Type tool caret they
/// change every character of the text, so text that is only partly superscript turns superscript
/// everywhere, with the caret after a superscript character too, and the button there shows off.
/// Superscripts in different proportions count as superscript, so a press turns them off.
#[test]
fn superscript_and_subscript_decide_from_the_characters_they_change() {
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": "x2", "size": 12})).unwrap()["id"].clone();
app.run("text.setRangeStyle", json!({"id": id, "start": 1, "end": 2, "position": "superscript"})).unwrap();
app.run("select.set", json!({"ids": [id.clone()]})).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": 2, "end": 2}));
assert_eq!(text_editing(&app).map(|(_, a, b)| (a, b)), Some((2, 2)));
let ctx = egui::Context::default();
assert_eq!(script_buttons(&ctx, &mut app).1, [false; 3], "the text is not all superscript");
assert_eq!(app.run("type.superscript", json!({})).unwrap(), json!("superscript"));
assert!(scripts(&app, &id).iter().all(|(_, p)| *p == "superscript"), "{:?}", scripts(&app, &id));
app.run("type.superscript", json!({})).unwrap();
assert!(scripts(&app, &id).iter().all(|(_, p)| *p == "normal"), "{:?}", scripts(&app, &id));
// A file whose two superscript runs have different proportions.
app.select_tool("selection");
app.run("text.setRangeStyle", json!({"id": id, "position": "superscript"})).unwrap();
app.run("text.setRangeStyle", json!({"id": id, "start": 1, "end": 2, "underline": true})).unwrap();
let mut d = (*app.session.active().unwrap().doc).clone();
if let Some(NodeKind::Text(t)) = d.node_mut(NodeId(id.as_u64().unwrap())).map(|n| &mut n.kind) {
assert_eq!(t.runs.len(), 2);
t.runs[0].style.position = vectorcraft_doc::CharPosition::Superscript(vectorcraft_doc::ScriptMetrics { size: 50.0, position: 40.0 });
}
let data = vectorcraft_format::base64_encode(&vectorcraft_format::save_file(&d));
app.run("document.open", json!({"name": "scripts.vectorcraft", "dataBase64": data})).unwrap();
app.run("select.set", json!({"ids": [id.clone()]})).unwrap();
let runs = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id.as_u64().unwrap())).unwrap().kind {
NodeKind::Text(t) => t.runs.iter().map(|r| r.style.position).collect::<Vec<_>>(),
_ => panic!("text"),
};
let before = runs(&app);
assert!(before.len() == 2 && before[0] != before[1] && before.iter().all(|p| p.id() == "superscript"), "{before:?}");
assert_eq!(script_buttons(&ctx, &mut app).1, [false, true, false], "the button shows superscript");
assert_eq!(app.run("type.superscript", json!({})).unwrap(), json!("normal"));
assert!(scripts(&app, &id).iter().all(|(_, p)| *p == "normal"), "{:?}", scripts(&app, &id));
}
/// 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]
+3 -1
View File
@@ -7045,7 +7045,9 @@ Pen's second anchor at (150, 431), in line with the square's centre.
`type.superscript` (Cmd+Shift+=, `press_key {key: "=", mods: {cmd: true, shift: true}}`) and `type.subscript`
(Cmd+Alt+Shift+=) do what the Character panel's Superscript and Subscript buttons do on the same text: they set its
position through `text.setFormat {position}`, or set it back to `"normal"` when every character already has that
position, and return the position the text now has. Without selected type or text the Type tool edits, both fail
position, and return the position the text now has. At a Type tool caret with no characters selected, the buttons and
both commands act on, and check, every character of the text; superscripts in different proportions count as
superscript. Without selected type or text the Type tool edits, both fail
and are dimmed, and Cmd+Shift+= zooms in, as Cmd+= does. In general, when the command a Shift+= chord is bound to is
dimmed, the press runs what the same chord without Shift is bound to.
- `missingGlyphProtection` (on by default): `text.setStyle` and `text.setRangeStyle` changing the font leave the