mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 22:33:08 +00:00
Type: Forced Line Break inserts a line break in the paragraph, at the caret
Type > Insert Break Character > Forced Line Break (type.insert
{char: "forcedLineBreak"}) inserted a line feed, which starts a new
paragraph. It now inserts U+2028, the character Shift+Enter types, so the
new line stays in the same paragraph; paragraphReturn still inserts a line
feed.
While the Type tool edits text and no ids are given, type.insert (every
Insert Special, Whitespace and Break Character item) now replaces the
tool's selection or inserts at its caret, as one undo step, and moves the
caret after it. After Select All in threaded text it replaces the whole
story. With ids or id it appends to the objects they name, and without a
Type tool edit it appends to the selected type objects.
This commit is contained in:
@@ -270,7 +270,7 @@ fn range_of(p: &Value, len: usize, cmd: &str) -> Result<(usize, usize)> {
|
||||
Ok((a.min(b), a.max(b)))
|
||||
}
|
||||
|
||||
fn edit_range(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
pub(crate) fn edit_range(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "text.editRange";
|
||||
let id = object_param(p, "id", C)?.ok_or_else(|| bad(C, "missing `id`"))?;
|
||||
let t = text_ref(s, id)?;
|
||||
|
||||
@@ -10,8 +10,8 @@ use super::object::choice;
|
||||
use super::threads::ThreadIndex;
|
||||
use super::typecmd::{area_text, block_gaps, needed_room, refresh_bounds};
|
||||
use super::{
|
||||
CommandSpec, EngineError, Result, Session, bad, checked_ids_param, cmd, find_command, has_clipboard, has_doc, has_selection, number_or,
|
||||
switch_or, targets, text_param,
|
||||
CommandSpec, EngineError, Result, Session, bad, checked_ids_param, cmd, find_command, has_clipboard, has_doc, has_selection, named_ids,
|
||||
number_or, switch_or, targets, text_param,
|
||||
};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -111,7 +111,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Insert Character",
|
||||
["Type"],
|
||||
None,
|
||||
"{char: \"bullet\"|\"copyright\"|\"ellipsis\"|\"paragraph\"|\"registered\"|\"section\"|\"trademark\"|\"emDash\"|\"enDash\"|\"discretionaryHyphen\"|\"nonBreakingHyphen\"|\"doubleLeftQuote\"|\"doubleRightQuote\"|\"singleLeftQuote\"|\"singleRightQuote\"|\"emSpace\"|\"enSpace\"|\"hairSpace\"|\"sixthSpace\"|\"thinSpace\"|\"nonBreakingSpace\"|\"figureSpace\"|\"punctuationSpace\"|\"thirdSpace\"|\"quarterSpace\"|\"tab\"|\"forcedLineBreak\"|\"paragraphReturn\" | text: string | code: Windows Alt+0 code (0–255 in Windows-1252, a Unicode code point above: 176 → °, 151 → —) | unicode: hex code point (\"00B0\" or \"U+00B0\"), ids?|id?: object ids (default: the selection)} append to the selected text objects",
|
||||
"{char: \"bullet\"|\"copyright\"|\"ellipsis\"|\"paragraph\"|\"registered\"|\"section\"|\"trademark\"|\"emDash\"|\"enDash\"|\"discretionaryHyphen\"|\"nonBreakingHyphen\"|\"doubleLeftQuote\"|\"doubleRightQuote\"|\"singleLeftQuote\"|\"singleRightQuote\"|\"emSpace\"|\"enSpace\"|\"hairSpace\"|\"sixthSpace\"|\"thinSpace\"|\"nonBreakingSpace\"|\"figureSpace\"|\"punctuationSpace\"|\"thirdSpace\"|\"quarterSpace\"|\"tab\"|\"forcedLineBreak\"|\"paragraphReturn\" | text: string | code: Windows Alt+0 code (0–255 in Windows-1252, a Unicode code point above: 176 → °, 151 → —) | unicode: hex code point (\"00B0\" or \"U+00B0\"), ids?|id?: object ids (default: the selection)} insert at the Type tool's caret, replacing its selection, while it edits text and no ids are given; otherwise append to the selected text objects. forcedLineBreak is U+2028, a new line in the same paragraph; paragraphReturn is a line feed, which starts a new paragraph",
|
||||
has_selection,
|
||||
insert_char
|
||||
),
|
||||
@@ -1525,8 +1525,9 @@ pub(crate) fn special_char(name: &str) -> Option<&'static str> {
|
||||
"thirdSpace" => "\u{2004}",
|
||||
"quarterSpace" => "\u{2005}",
|
||||
"tab" => "\t",
|
||||
// Our text model breaks lines at paragraph returns only.
|
||||
"forcedLineBreak" | "paragraphReturn" => "\n",
|
||||
// A line break inside the paragraph, as Shift+Enter types it.
|
||||
"forcedLineBreak" => "\u{2028}",
|
||||
"paragraphReturn" => "\n",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -1576,6 +1577,11 @@ fn insert_char(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
(None, None, None, Some(u)) => unicode_char(u).ok_or_else(|| bad(C, format!("`{u}` is no Unicode character")))?.to_string(),
|
||||
(None, None, None, None) => return Err(bad(C, "give `char`, `text`, `code` or `unicode`")),
|
||||
};
|
||||
if named_ids(p)?.is_none()
|
||||
&& let Some(r) = insert_at_type_caret(s, &text)?
|
||||
{
|
||||
return Ok(r);
|
||||
}
|
||||
let ids = texts(s, p, C)?;
|
||||
// When `text` is empty and every object ends with a run of text, the document and its undo
|
||||
// history stay as they are.
|
||||
@@ -1606,6 +1612,40 @@ fn insert_char(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(ids_json(&ids))
|
||||
}
|
||||
|
||||
/// `type.insert` while the Type tool edits text: `text` replaces the tool's selection (inserts at
|
||||
/// its caret) as one undo step, and the caret goes after it. `None` when no text is being edited.
|
||||
fn insert_at_type_caret(s: &mut Session, text: &str) -> Result<Option<Value>> {
|
||||
let tool = s.tool_options();
|
||||
let Some(id) = tool.get("editing").and_then(Value::as_u64).map(NodeId) else { return Ok(None) };
|
||||
let d = &s.doc()?.doc;
|
||||
let Some(NodeKind::Text(t)) = d.node(id).map(|n| &n.kind) else { return Ok(None) };
|
||||
let len = vectorcraft_text::edit::runs_len(&t.runs);
|
||||
let g = |k: &str| tool.get(k).and_then(Value::as_u64).map_or(len, |v| (v as usize).min(len));
|
||||
let (x, y) = (g("start"), g("end"));
|
||||
let (a, b) = (x.min(y), x.max(y));
|
||||
// After Select All in threaded text the whole story is selected: the other frames' text goes
|
||||
// in the same edit, as when typing replaces it.
|
||||
let story: Vec<NodeId> = tool.get("story").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_u64).map(NodeId).collect();
|
||||
let clear: Vec<u64> = if a == 0 && b == len && story.contains(&id) {
|
||||
let has_text = |f: &NodeId| matches!(d.node(*f).map(|n| &n.kind), Some(NodeKind::Text(o)) if !o.runs.iter().all(|r| r.text.is_empty()));
|
||||
story.iter().filter(|f| **f != id && has_text(f)).map(|f| f.0).collect()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
if text.is_empty() && a == b && clear.is_empty() {
|
||||
return Ok(Some(ids_json(&[id])));
|
||||
}
|
||||
// A typing session ends first: the insertion is a step of its own.
|
||||
if tool.get("typing").and_then(Value::as_bool) == Some(true) {
|
||||
s.set_tool_option("commitTyping", &Value::Bool(true));
|
||||
s.commit_interaction()?;
|
||||
}
|
||||
let r = super::textedit::edit_range(s, &json!({"id": id.0, "start": a, "end": b, "insert": text, "clear": clear}))?;
|
||||
let caret = r.get("caret").cloned().unwrap_or(Value::Null);
|
||||
s.set_tool_option("select", &json!({"start": caret, "end": caret}));
|
||||
Ok(Some(ids_json(&[id])))
|
||||
}
|
||||
|
||||
// ---------- Find and Replace ----------
|
||||
|
||||
fn is_word(c: char) -> bool {
|
||||
|
||||
@@ -4103,6 +4103,76 @@ fn placeholder_and_insert_characters() {
|
||||
assert!(s.execute("type.insert", &json!({"char": "nope"})).is_err());
|
||||
}
|
||||
|
||||
/// Type › Insert Break Character › Forced Line Break (`type.insert {char: "forcedLineBreak"}`)
|
||||
/// starts a new line in the same paragraph (U+2028, as Shift+Enter types it), so the paragraph's
|
||||
/// space before does not come between the two lines; `paragraphReturn` starts a new paragraph.
|
||||
/// Each insert is one undo step.
|
||||
#[test]
|
||||
fn insert_forced_line_break_differs_from_paragraph_return() {
|
||||
fn second_line_gap(s: &Session, id: NodeId) -> f64 {
|
||||
let NodeKind::Text(t) = &node(s, id).kind else { panic!("not text") };
|
||||
let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
|
||||
assert_eq!(lay.lines.len(), 2, "{:?}", t.plain_text());
|
||||
lay.lines[1].baseline - lay.lines[0].baseline
|
||||
}
|
||||
let mut s = session();
|
||||
let mut gaps = Vec::new();
|
||||
for (c, want) in [("forcedLineBreak", "ab\u{2028}c"), ("paragraphReturn", "ab\nc")] {
|
||||
let t = text(&mut s, "ab");
|
||||
sel(&mut s, &[t]);
|
||||
s.execute("text.setFormat", &json!({"ids": [t.0], "spaceBefore": 30})).unwrap();
|
||||
let steps = undo_len(&s);
|
||||
s.execute("type.insert", &json!({"char": c})).unwrap();
|
||||
assert_eq!(undo_len(&s), steps + 1, "{c}");
|
||||
s.execute("type.insert", &json!({"text": "c"})).unwrap();
|
||||
assert_eq!(plain(&s, t), want, "{c}");
|
||||
gaps.push(second_line_gap(&s, t));
|
||||
}
|
||||
assert!(gaps[1] - gaps[0] > 29.0, "space before only at the paragraph return: {gaps:?}");
|
||||
// The same character by `unicode` and by `text`.
|
||||
let t = text(&mut s, "");
|
||||
sel(&mut s, &[t]);
|
||||
s.execute("type.insert", &json!({"unicode": "2028"})).unwrap();
|
||||
s.execute("type.insert", &json!({"text": "\u{2028}"})).unwrap();
|
||||
assert_eq!(plain(&s, t), "\u{2028}\u{2028}");
|
||||
}
|
||||
|
||||
/// While the Type tool edits text, the Insert Special, Whitespace and Break menu items
|
||||
/// (`type.insert` without `ids`) insert at its caret or replace its selection, as one undo step,
|
||||
/// and the caret goes after the insertion; `ids` still appends to those objects.
|
||||
#[test]
|
||||
fn insert_character_goes_to_the_type_tool_caret() {
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
||||
let mut s = session();
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("type", v).unwrap();
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(k, 50.0, 300.0), v).unwrap();
|
||||
}
|
||||
let t = s.doc().unwrap().selection.objects[0];
|
||||
s.tool_text("abcd", v).unwrap();
|
||||
for _ in 0..2 {
|
||||
s.tool_key(ToolKey::Left, Mods::default(), v).unwrap();
|
||||
}
|
||||
let steps = undo_len(&s);
|
||||
s.execute("type.insert", &json!({"text": ""})).unwrap();
|
||||
assert_eq!(undo_len(&s), steps, "an empty insert changes nothing");
|
||||
s.execute("type.insert", &json!({"char": "forcedLineBreak"})).unwrap();
|
||||
assert_eq!(plain(&s, t), "ab\u{2028}cd");
|
||||
assert_eq!(s.tool_options()["caret"], json!(5));
|
||||
s.tool_text("X", v).unwrap();
|
||||
assert_eq!(plain(&s, t), "ab\u{2028}Xcd");
|
||||
s.tool_key(ToolKey::Left, Mods { shift: true, ..Mods::default() }, v).unwrap();
|
||||
s.execute("type.insert", &json!({"char": "paragraphReturn"})).unwrap();
|
||||
assert_eq!(plain(&s, t), "ab\u{2028}\ncd");
|
||||
s.execute("type.insert", &json!({"ids": [t.0], "text": "!"})).unwrap();
|
||||
assert_eq!(plain(&s, t), "ab\u{2028}\ncd!");
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(plain(&s, t), "ab\u{2028}Xcd", "the replacement is one undo step");
|
||||
}
|
||||
|
||||
/// `type.insert` by code (#5108): `code` as a Windows Alt+0 code types (Windows-1252 for
|
||||
/// 128–159, the code point otherwise), `unicode` as a hex code point.
|
||||
#[test]
|
||||
|
||||
@@ -2289,6 +2289,20 @@ fn threaded_list_edited_in_its_second_frame() -> (Session, NodeId, NodeId, Strin
|
||||
(s, a, b, story)
|
||||
}
|
||||
|
||||
/// Type › Insert Break Character › Forced Line Break (`type.insert`) after Select All in threaded
|
||||
/// text replaces the whole story, every frame's text, as one undo step.
|
||||
#[test]
|
||||
fn insert_character_replaces_the_whole_story_that_select_all_selected() {
|
||||
let (mut s, a, b, story) = threaded_story_edited_in_its_second_frame();
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
let steps = undo_labels(&s).len();
|
||||
s.execute("type.insert", &json!({"char": "forcedLineBreak"})).unwrap();
|
||||
assert_eq!(undo_labels(&s).len(), steps + 1, "one undo step");
|
||||
assert_eq!(format!("{}{}", obj(&s, a).plain_text(), obj(&s, b).plain_text()), "\u{2028}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(format!("{}{}", obj(&s, a).plain_text(), obj(&s, b).plain_text()), story);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_breaks_takes_the_whole_story_that_select_all_selected() {
|
||||
let (mut s, a, b, story) = threaded_list_edited_in_its_second_frame();
|
||||
|
||||
+14
@@ -9637,6 +9637,20 @@ on again.
|
||||
{"name":"run_command","arguments":{"command":"type.convertToAreaType","params":{"ids":[42]}}}
|
||||
```
|
||||
|
||||
## Insert Special, Whitespace and Break Characters
|
||||
|
||||
`type.insert {char?, text?, code?, unicode?, ids?|id?}` inserts one character (`char` names it, `code` and `unicode` give
|
||||
its code) or the string `text`. While the Type tool edits text and neither `ids` nor `id` is given, it replaces the
|
||||
tool's selection, or inserts at its caret, and the caret goes after the insertion; otherwise it appends to the end of each
|
||||
selected text object. Each call is one undo step; an empty `text` at the Type tool's caret changes nothing and records no undo step.
|
||||
`char: "forcedLineBreak"` (Type › Insert Break Character › Forced Line Break) inserts U+2028, which starts a new line in
|
||||
the same paragraph, as Shift+Enter does; `char: "paragraphReturn"` inserts a line feed, which starts a new paragraph. A
|
||||
`text` that holds U+2028 inserts the same break.
|
||||
|
||||
```json
|
||||
{"name":"run_command","arguments":{"command":"type.insert","params":{"char":"forcedLineBreak"}}}
|
||||
```
|
||||
|
||||
## Smart Punctuation
|
||||
|
||||
`type.smartPunctuation` replaces keyboard characters in the selected type, or in every text object with `scope:
|
||||
|
||||
@@ -1460,6 +1460,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- **Vertical punctuation:** fullwidth `!` (U+FF01) and `?` (U+FF1F) use their nominal glyph upright when no vertical alternate is present, following the Tu fallback in [Unicode UAX #50 §3.1](https://www.unicode.org/reports/tr50/#vo). Code-built test fonts cover point and area type with and without `vert` alternates, retain the rotated fallback for a bracket and long vowel mark, and leave horizontal type unchanged. Tests: `crates/text/src/tests/vertical.rs`.
|
||||
- Free Distort's dialog (Effect › Distort & Transform › Free Distort…) shows a box in the proportions of the first selected object's bounds with a handle on each corner. Dragging a handle moves that corner. A handle focused by a click or Tab moves 1% of the box with each arrow key, and 10% with Shift. Reset puts the four corners back on the box. The preview and OK run `effect.apply`, or `effect.setParams` when the dialog edits an applied effect, and the Appearance panel's inline editor opens the dialog with Edit…. Behavior reference: Illustrator CC Help (2014, [PDF](https://help.adobe.com/archive/en/illustrator/cc/2014/illustrator_reference.pdf)), page 285 (PDF page 291).
|
||||
- **Kerning at the start of a line:** at the Type tool's caret before the first character of the text, of a paragraph, or after a forced line break, the Kerning field, Alt+←/→ (Alt+↓/↑ in vertical type) and Reset Tracking and Kerning set the space before the character after the caret (`kerningBefore`), as the reporter of #5111 describes the reference; the character moves along its line and the rest of the line follows, as one undo step (#5111). It used to do nothing there, or kern the invisible line break. Inside a line the caret still kerns the character before it. A line that wraps in area type takes no space before its first character, and Touch Type still moves a line's first character only up and down. SVG export starts the line where the character is drawn, and on a path writes the space as the first character's `dx`; vertical type moves the character down its column. A space on the first letter of a ligature keeps the ligature whole. Tests: `tests::caret_kerning` (engine), `tests::kerning_before` (text), `the_space_before_a_lines_first_character_moves_its_tspan` (SVG).
|
||||
- **Forced Line Break:** Type › Insert Break Character › Forced Line Break starts a new line without starting a new paragraph, as [the help page](https://helpx.adobe.com/illustrator/desktop/design-with-text/special-characters-glyphs/insert-white-space-and-break-characters.html) describes: it inserts U+2028, the character Shift+Enter types, so the paragraph's Space Before does not come between the two lines (`type.insert {char: "forcedLineBreak"}`; `paragraphReturn` still inserts a line feed). Storing the break as U+2028 is VectorCraft's own choice. While the Type tool edits text, this item and the other Insert Special, Whitespace and Break Character items insert at its caret or replace its selection as one undo step, with the caret after them; without a Type tool edit they append to the selected type objects. Tests: `tests::menucmds::insert_forced_line_break_differs_from_paragraph_return`, `tests::menucmds::insert_character_goes_to_the_type_tool_caret`.
|
||||
- **Optical kerning:** Character and Properties offer Optical beside Auto for automatic pair spacing. The side-profile spacing method is VectorCraft's own: each pair moves part of the way toward the font's own stem spacing (the gap of `nn`, else `HH` or `oo`), keeps at least part of that gap where the outlines come closest, and changes by at most 0.12 em tighter or 0.06 em looser, so ordinary text keeps the font's spacing (#5336). Ideographs, kana, hangul, fullwidth and CJK punctuation forms and dashes keep the font's spacing. Optical keeps its mode across cosmetic style runs. Choosing Optical clears manual pair values, as Auto does, and leaves additive tracking intact; a manual value typed afterwards replaces that pair's correction ([Adjust kerning and tracking](https://helpx.adobe.com/illustrator/desktop/design-with-text/edit-format-text/adjust-kerning-and-tracking.html)). Native documents retain the mode and pair overrides; old documents use Auto. Behavior reference: [Line and character spacing](https://web.archive.org/web/20260916050333/https://helpx.adobe.com/illustrator/using/line-character-spacing.html) (public documentation). Tests: `crates/text/src/tests/typo.rs`, `crates/engine/src/tests/textedit.rs`.
|
||||
- **Kerning across style runs:** characters that differ only in fill, stroke, underline, strikethrough, overprint, character style name, leading, baseline shift, rotation, Character Alignment, kerning, or tracking that keeps or drops ligatures alike are shaped together, so restyling one character keeps the font's kerning pairs with its neighbors. A character with manual kerning that follows one with automatic kerning keeps the font's kerning with it (unless All Caps or Small Caps sets it as two letters, as ß becomes SS), and its value replaces the font's kerning after it. A ligature does not form across a change of style, and a change of font, size or OpenType features still shapes the characters apart. Behavior reference: [Line and character spacing](https://web.archive.org/web/20260916050333/https://helpx.adobe.com/illustrator/using/line-character-spacing.html) (public documentation). Tests: `crates/text/src/tests/typo.rs`, `crates/text/src/tests/bidi.rs`.
|
||||
- **Manual kerning in right-to-left text:** a manual kerning value on a Hebrew or Arabic character changes the space between it and the next character in reading order, the one drawn to its left, under Auto and Optical kerning. The character before it keeps its place, and the caret between the pair stays beside the next character. The side the space goes on is VectorCraft's own rule. Behavior reference: [Line and character spacing](https://web.archive.org/web/20260916050333/https://helpx.adobe.com/illustrator/using/line-character-spacing.html). Test: `rtl_manual_kerning_spaces_the_character_from_the_next_in_reading_order`.
|
||||
|
||||
Reference in New Issue
Block a user