Merge pull request #10281 from dotCooCoo/fix/issue-3211

Fix #3211: Text Wrap › Make wraps the area type selected with the object
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-30 10:30:57 -08:00
commit dc6beff197
3 files changed
+169 -14

No files matched your search

+167 -12
View File
@@ -4,6 +4,8 @@
//! resolves, for each area text object, the outlines of the wrap objects above it into
//! `TextObject::wrap` (text space); the layout engine subtracts them from each line's span.
use std::collections::{HashMap, HashSet};
use serde_json::{Value, json};
use vectorcraft_doc::{Document, NodeId, NodeKind, TextWrap};
@@ -17,7 +19,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Make",
["Object", "Text Wrap"],
None,
"{offset?: pt (6), invert?: bool, ids?} area type below the selected objects (same layer) wraps around them; offset -1000 to 1000 (a negative one lets the text that far into them); an offset outside that range or not a number, or an invert that is not true or false, is an error → {ids}",
"{offset?: pt (6), invert?: bool, ids?} area type below the selected objects (same layer) wraps around them; a selected type object with another selected object above it in its layer is the text to wrap, not a wrap object, and loses a wrap it had; offset -1000 to 1000 (a negative one lets the text that far into them); an offset outside that range or not a number, or an invert that is not true or false, is an error → {ids (the wrap objects), warning? (no area type lies below any of them)}",
has_selection,
make
),
@@ -27,13 +29,16 @@ pub fn specs() -> Vec<CommandSpec> {
"Text Wrap Options…",
["Object", "Text Wrap"],
None,
"{offset?: pt, invert?: bool, ids?} set the options of the selected wrap objects (offset -1000 to 1000; a negative one lets text into the objects); an offset outside that range or not a number, or an invert that is not true or false, is an error; no options (or null ones) → the current {offset, invert}",
"{offset?: pt, invert?: bool, ids?} set the options of the selected wrap objects, leaving out the type selected below them as Make does (offset -1000 to 1000; a negative one lets text into the objects); an offset outside that range or not a number, or an invert that is not true or false, is an error; no options (or null ones) → the current {offset, invert}",
has_selection,
options
),
]
}
/// The warning Make gives when no area type lies below any of its wrap objects in their layer.
pub const NO_AREA_TYPE_BELOW: &str = "Text Wrap: no area type lies below these objects in their layer, so no text wraps around them. Move the objects above the area type with Object › Arrange › Bring to Front. Point type does not wrap.";
/// The Offset a wrap object takes, in points.
pub const TEXT_WRAP_OFFSET_RANGE: std::ops::RangeInclusive<f64> = -1000.0..=1000.0;
@@ -51,20 +56,70 @@ fn wrap_param(p: &Value, base: TextWrap, cmd: &str) -> Result<TextWrap> {
Ok(TextWrap { offset, invert: switch_or(p, "invert", base.invert, cmd)? })
}
/// The type objects of `ids` that lie below another of `ids` in their layer: the text Make wraps
/// when the wrap objects and the type are selected together. Make leaves them out of the wrap
/// objects, and takes the wrap off any of them that has one.
fn type_to_wrap(d: &Document, ids: &[NodeId]) -> HashSet<NodeId> {
let wanted: HashSet<NodeId> = ids.iter().copied().collect();
// One pass in paint order: each selected type object with its layer and place, and the place of
// the topmost selected object other than a layer in each layer.
let mut texts = vec![];
let mut top: HashMap<usize, usize> = HashMap::new();
let mut at = 0usize;
for (layer, l) in d.layers.iter().enumerate() {
l.walk(&mut |n| {
if wanted.contains(&n.id) {
match n.kind {
NodeKind::Layer { .. } => {}
NodeKind::Text(_) => {
texts.push((n.id, layer, at));
top.insert(layer, at);
}
_ => {
top.insert(layer, at);
}
}
}
at += 1;
});
}
texts.into_iter().filter(|(_, layer, at)| top.get(layer).is_some_and(|t| t > at)).map(|(id, _, _)| id).collect()
}
fn make(s: &mut Session, p: &Value) -> Result<Value> {
let ids = checked_ids_or_selection(s, p, "object.textWrap.make")?;
let w = wrap_param(p, TextWrap::default(), "object.textWrap.make")?;
s.edit("Make Text Wrap", |d, _| {
for id in &ids {
let n = d.node_mut(*id).ok_or(EngineError::NoNode(*id))?;
if matches!(n.kind, NodeKind::Layer { .. }) {
let (objects, text, changed) = {
let d = &s.doc()?.doc;
let text = type_to_wrap(d, &ids);
let objects: Vec<NodeId> = ids.iter().copied().filter(|i| !text.contains(i)).collect();
for id in &objects {
if d.node(*id).is_some_and(|n| matches!(n.kind, NodeKind::Layer { .. })) {
return Err(EngineError::Other("layers can't be wrap objects".into()));
}
n.extras_mut().wrap = Some(w);
}
Ok(())
})?;
Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>() }))
let changed = objects.iter().any(|i| d.node(*i).is_some_and(|n| n.extras().wrap != Some(w)))
|| text.iter().any(|i| d.node(*i).is_some_and(|n| n.extras().wrap.is_some()));
(objects, text, changed)
};
if changed {
s.edit("Make Text Wrap", |d, _| {
for id in &objects {
d.node_mut(*id).ok_or(EngineError::NoNode(*id))?.extras_mut().wrap = Some(w);
}
for id in &text {
d.node_mut(*id).ok_or(EngineError::NoNode(*id))?.extras_mut().wrap = None;
}
Ok(())
})?;
}
let mut out = json!({ "ids": objects.iter().map(|i| i.0).collect::<Vec<_>>() });
let mut wraps = false;
vectorcraft_doc::text::wrap_sources(&s.doc()?.doc, |_, sources, _| wraps |= sources.iter().any(|n| objects.contains(&n.id)));
if !wraps {
out["warning"] = json!(NO_AREA_TYPE_BELOW);
}
Ok(out)
}
fn release(s: &mut Session, p: &Value) -> Result<Value> {
@@ -91,14 +146,25 @@ fn release(s: &mut Session, p: &Value) -> Result<Value> {
fn options(s: &mut Session, p: &Value) -> Result<Value> {
let ids = checked_ids_or_selection(s, p, "object.textWrap.options")?;
let current = {
// The type selected below the wrap objects is the text they wrap, as in Make.
let (ids, current) = {
let d = &s.doc()?.doc;
ids.iter().find_map(|i| d.node(*i).and_then(|n| n.extras().wrap)).unwrap_or_default()
let text = type_to_wrap(d, &ids);
let ids: Vec<NodeId> = ids.into_iter().filter(|i| !text.contains(i)).collect();
let current = ids.iter().find_map(|i| d.node(*i).and_then(|n| n.extras().wrap)).unwrap_or_default();
(ids, current)
};
if ["offset", "invert"].iter().all(|k| p.get(k).is_none_or(Value::is_null)) {
return Ok(json!({ "offset": current.offset, "invert": current.invert }));
}
let w = wrap_param(p, current, "object.textWrap.options")?;
let changed = {
let d = &s.doc()?.doc;
ids.iter().any(|i| d.node(*i).is_some_and(|n| !matches!(n.kind, NodeKind::Layer { .. }) && n.extras().wrap != Some(w)))
};
if !changed {
return Ok(json!({ "offset": w.offset, "invert": w.invert }));
}
s.edit("Text Wrap Options", |d, _| {
for id in &ids {
if let Some(n) = d.node_mut(*id)
@@ -287,6 +353,95 @@ mod tests {
assert_eq!(s.execute("object.textWrap.options", &json!({"invert": true})).unwrap(), json!({"offset": 5000.0, "invert": true}));
}
fn wrap_of(s: &Session, id: u64) -> Option<vectorcraft_doc::TextWrap> {
s.doc().unwrap().doc.node(NodeId(id)).unwrap().extras().wrap
}
fn area_type(s: &mut Session) -> u64 {
s.execute("text.create", &json!({"x": 0, "y": 0, "text": "word ".repeat(300), "size": 12, "area": {"width": 400, "height": 400}}))
.unwrap()["id"]
.as_u64()
.unwrap()
}
fn ellipse(s: &mut Session) -> u64 {
s.execute("shape.ellipse", &json!({"x": 150, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap()
}
/// Make with the wrap object and the area type below it selected together wraps the type: the
/// type is the text to wrap, not another wrap object (#3211).
#[test]
fn make_with_the_area_type_selected_wraps_it() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 600, "height": 600})).unwrap();
let t = area_type(&mut s);
let plain = lines(&s, t);
let c = ellipse(&mut s);
s.execute("select.set", &json!({"ids": [c, t]})).unwrap();
let r = s.execute("object.textWrap.make", &json!({})).unwrap();
assert_eq!(r, json!({"ids": [c]}), "{r}");
assert!(wrap_of(&s, c).is_some());
assert!(wrap_of(&s, t).is_none(), "the area type became a wrap object");
assert_ne!(lines(&s, t), plain, "the area type did not wrap");
// Make again with the same options changes nothing and records no undo step.
let steps = s.doc().unwrap().history.undo.len();
assert_eq!(s.execute("object.textWrap.make", &json!({})).unwrap(), json!({"ids": [c]}));
assert_eq!(s.doc().unwrap().history.undo.len(), steps);
// Text Wrap Options with both still selected sets the wrap object's options only, and the
// type keeps wrapping; the same options again record no undo step.
let wrapped = lines(&s, t);
assert_eq!(s.execute("object.textWrap.options", &json!({"offset": 30})).unwrap()["offset"], json!(30.0));
assert_eq!(wrap_of(&s, c).map(|w| w.offset), Some(30.0));
assert!(wrap_of(&s, t).is_none(), "Options made the area type a wrap object");
assert_ne!(lines(&s, t), wrapped, "the type did not follow the new offset");
assert_ne!(lines(&s, t), plain);
let steps = s.doc().unwrap().history.undo.len();
s.execute("object.textWrap.options", &json!({"offset": 30})).unwrap();
assert_eq!(s.doc().unwrap().history.undo.len(), steps);
s.execute("edit.undo", &json!({})).unwrap();
// One undo takes the wrap off again.
s.execute("edit.undo", &json!({})).unwrap();
assert!(wrap_of(&s, c).is_none());
assert_eq!(lines(&s, t), plain);
}
/// Make with the object below the area type warns that no text wraps. After Bring to Front,
/// Make with both selected wraps the type, and the type that the first Make made a wrap object
/// is the text to wrap again (#3211).
#[test]
fn make_warns_when_the_object_is_below_the_type_and_wraps_after_bring_to_front() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 600, "height": 600})).unwrap();
let c = ellipse(&mut s);
let t = area_type(&mut s);
let plain = lines(&s, t);
s.execute("select.set", &json!({"ids": [c, t]})).unwrap();
let r = s.execute("object.textWrap.make", &json!({})).unwrap();
assert_eq!(r["warning"], json!(super::NO_AREA_TYPE_BELOW), "{r}");
assert_eq!(lines(&s, t), plain);
s.execute("select.set", &json!({"ids": [c]})).unwrap();
s.execute("object.arrange.bringToFront", &json!({})).unwrap();
s.execute("select.set", &json!({"ids": [c, t]})).unwrap();
let r = s.execute("object.textWrap.make", &json!({})).unwrap();
assert!(r.get("warning").is_none(), "{r}");
assert!(wrap_of(&s, t).is_none(), "the area type is still a wrap object");
assert_ne!(lines(&s, t), plain, "the area type did not wrap");
}
/// Point type below a wrap object does not wrap, so Make warns.
#[test]
fn make_warns_over_point_type() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 600, "height": 600})).unwrap();
let p = s.execute("text.create", &json!({"x": 0, "y": 100, "text": "point type", "size": 12})).unwrap()["id"].as_u64().unwrap();
let r = s.execute("shape.rectangle", &json!({"x": 0, "y": 80, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [r, p]})).unwrap();
let out = s.execute("object.textWrap.make", &json!({})).unwrap();
assert_eq!(out["warning"], json!(super::NO_AREA_TYPE_BELOW), "{out}");
assert!(wrap_of(&s, r).is_some());
assert!(wrap_of(&s, p).is_none(), "the point type became a wrap object");
}
#[test]
fn text_flows_on_both_sides_of_a_centred_object() {
let mut s = Session::new();
+1 -1
View File
@@ -514,7 +514,7 @@ pub fn tool_definitions() -> Vec<Value> {
tool(
"text_wrap",
"Text wrap",
"Make the selected (or `ids`) objects wrap objects: area type below them in the same layer flows around them (offset in pt, invert = flow inside). `release: true` removes the wrap. An offset outside -1000 to 1000 or not a number, or an invert or release that is not true or false, is an error and changes nothing.",
"Make the selected (or `ids`) objects wrap objects: area type below them in the same layer flows around them (offset in pt, invert = flow inside). A type object in `ids` below another of them is the text to wrap, not a wrap object. A `warning` says when no area type lies below them. `release: true` removes the wrap. An offset outside -1000 to 1000 or not a number, or an invert or release that is not true or false, is an error and changes nothing.",
obj(
json!({
"ids": {"type": "array", "items": {"type": "integer"}},
+1 -1
View File
@@ -821,7 +821,7 @@ ends the wait, so the sampler can be shown again. The BSDs and headless sessions
| `pathfinder` | `{operation, ids?}` | `unite`, `minusFront`, `intersect`, `exclude`, `divide`, `trim`, `merge`, `crop`, `outline`, `minusBack`. For a live version, apply the `pathfinder.*` effect to a group (`effect.apply` groups several loose selected objects first). In remote desktop mode, `run_command` and `invoke_menu` can both call `pathfinder.repeat` with `{}`. Successful panel operations, direct `object.pathfinder.*` commands, and `command.batch` steps (including Actions panel playback) set its history. Repeat applies it to the current selection using current Pathfinder Options. Trap, compound-shape edits, live effects and `action.batch` do not set history. With no history it returns "no Pathfinder operation applied yet". |
| `transform` | `{ids?, dx?, dy?, rotate?, scale?, scaleX?, scaleY?, reflect?, shear?, origin?, copy?}` | Runs move, rotate, scale, reflect, shear in that order. `rotate` is in degrees counter-clockwise; a positive `shear` moves the top to the right. With `copy`, the first step duplicates. A `scale`, `reflect` or `shear` that its command would refuse returns that error before any step runs; nothing changes and no undo step is recorded. The same holds for a step that is neither a number nor null, a `reflect` that is not a string, a `copy` that is not true or false, and an `origin` that is not two numbers. A null `rotate`, `scale`, `scaleX`, `scaleY`, `shear`, `reflect`, `copy` or `origin` is left out; a null `dx` or `dy` counts as 0, so it still runs the move. |
| `create_graph` | `{type?, x, y, width, height, csv? \| series?, categories?, rows?}` | The nine Illustrator graph types: column, stacked column, bar, stacked bar, line, area, scatter, pie and radar. An empty CSV cell or a `null` in `rows` is a blank value; a number in straight quotes is a label. Edit later with `graph.setData` / `graph.setType` via `run_command`. `graph.column {design, columnType: vertical\|uniform\|repeating\|sliding, each, fractions: chop\|scale}` draws the columns or bars of the series picked with Group Selection (or `seriesIndexes`, or all of them) with a graph design saved by `graph.design`; with no `design` it returns the series' current design and options. Type in the design that holds a `%` followed by two digits shows each column's value, with at least the first digit's places before the decimal point and the second digit's places after it. |
| `text_wrap` | `{ids?, offset?, invert?, release?}` | Area type below the objects (same layer) flows around them. `offset` is in points, from -1000 to 1000 (default 6). An offset outside that range or not a number, or an `invert` or `release` that is not true or false, is an error and changes nothing; `object.textWrap.make` and `object.textWrap.options` refuse the same values. |
| `text_wrap` | `{ids?, offset?, invert?, release?}` | Area type below the objects (same layer) flows around them. A type object in `ids` with another of them above it in its layer is the text to wrap, not a wrap object, so the objects and the type can be selected together. The reply has a `warning` when no area type lies below any of the wrap objects. `offset` is in points, from -1000 to 1000 (default 6). An offset outside that range or not a number, or an `invert` or `release` that is not true or false, is an error and changes nothing; `object.textWrap.make` and `object.textWrap.options` refuse the same values. |
| `undo` / `redo` | `{}` | Many calls that change nothing record no undo step, for example `object.align` with nothing to move, `object.move` by 0 without `copy`, `path.moveAnchors` by 0, `layer.setProps` with every row already as given and `object.showAll` with nothing hidden. `undo` (`edit.undo`) after such a call undoes the step before it. Settings that change without an undo step stay as they are when `undo` or `redo` brings back a step that did not change them: the perspective grid's view options and active plane, the grid preferences (`gridlineEvery`, `gridSubdivisions`), the export settings (`assets.settings.set`), and the current brush and symbol. A step that changed one of them, such as a perspective grid preset that shows the grid, takes that change back. The point Automatic Plane Positioning holds comes back with the step, and is let go when the active plane kept is another one. `inspect_document` lists the undo steps as `history`. `run_command` with `edit.clearHistory` removes every undo and redo step of the active document and returns `{cleared}`, the number of steps removed; the document stays as it is, and the command records no undo step. `run_command` with `edit.newDocumentFromState` and `{index?, created?}` makes a new document holding the art as it was at that state and makes it the active document, and returns `{index, title}`. `index` is a row of the History panel, with 0 the oldest state kept, and an omitted or null `index` uses the current state. An `index` that names no state fails with `no history state N`, one that is not a whole number with `` `index` must be a whole number ``, and either changes nothing. `created` is File Info's created date in Unix seconds or null, and defaults to now. The new document takes the next `Untitled-N` name, is marked unsaved, has nothing selected and has no steps to undo or redo. It is out of symbol, opacity-mask and pattern editing, and the art drawn in a pattern's tile is stored in the pattern, as `object.pattern.done` stores it. The source document and its steps stay as they are, except that typing or a drag in progress is first kept as a step. When a command keeps a drag whose preview Real-time Drawing off had not applied yet (`edit.undo`, `edit.redo`, `edit.clearHistory`, `edit.newDocumentFromState` and the other commands that end a drag first), and applying that preview gives a warning, the command's result carries it as `previewWarning`, and as `warning` too when the command gives no warning of its own; the app's status bar shows `warning`. Any other command that changes the document during a tool's drag, from `run_command`, a panel or a menu, records its own undo step before the drag's. The command acts on the document without the drag's preview, and the drag then continues from the command's result: with Real-time Drawing on, the drag's preview is applied again at once, and with it off, the release applies it as before. The release records the drag's step after the command's, `undo` takes back the drag first and the command next, and Escape cancels only the drag. The drag acts on the objects it started with, so a selection the command makes (`select.all`, `edit.duplicate`) does not change what the drag moves, and the drag's release and Escape set the selection as they do without the command (with Real-time Drawing on, so does each move of the drag); a command that records no undo step, such as `select.none`, adds no step. When the command deletes or changes the art being dragged, the drag continues with the art the command left, and a drag left with nothing to change records no step. A warning or an error from the drag's preview applied again after the command is in the command's result as `previewWarning`. Typing in progress is kept as a step of its own before the command. Two cases run the command inside the drag, which then keeps the command's edit as part of its own step: a press of the Quick Pen (an edit to its path ends the path), and `object.transform` with `copy` during an Alt-drag, which copies the art as the drag's move left it. A command run while a dialog's or a panel's preview is open runs inside that preview. While the Type tool edits type, `undo` and `redo` put its caret, with no characters selected, at the end of the text the step changed: after the text it puts back or retypes, or where the text it takes out was. A step that changes no characters, such as a formatting change, leaves the caret and the selection where they are. |
For example, create editable Japanese area type with the engine's vertical writing mode: