mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
Part of #978: Show Opacity in the Color Wheel panel sets the opacity of the fill or stroke
The Color Wheel panel menu gets Show Opacity, kept with the preferences
(`ui.colorWheel {opacity}`). It adds a row under the wheel or the box with
a slider and a percent field. The row sets the opacity of the fill or
stroke item, the selected gradient stop or the selected freeform point
(the first stop or point when none is selected, as for a color pick), and
a drag is one undo step. The row is gray with nothing selected, for an
object selected in a symbol instance, and when the fill or stroke item it
would set is on type characters or missing.
- paint.setFill and paint.setStroke take `opacity` (a percentage, 0 to
100), with or without a new paint. An opacity that no target takes is
an error and leaves the paint undone too. paint.proxies answers
`fillOpacity` and `strokeOpacity` from the first row the command would
edit. A graph series keeps the opacity on regeneration.
- The opacity slider and the swatch chips draw the same checkerboard.
- New error and menu strings have rows in the catalogs.
Docs: docs/mcp.md, docs/control-protocol.md, docs/target-app-parity.md.
This commit is contained in:
1 parent
22b8c6ce5a
commit
4f065ccbbf
46 files changed
+1227
-110
No files matched your search
@@ -333,6 +333,25 @@ fn an_item_opacity_on_a_series_mark_survives_regeneration() {
|
||||
assert_eq!((kept.fill_opacity, kept.stroke_opacity), (Some(0.4), None));
|
||||
}
|
||||
|
||||
/// `paint.setFill {opacity}` and `paint.setStroke {opacity}` on one series mark are kept by the graph as the transparency
|
||||
/// commands' item opacity is: every path of that series has them after the graph is drawn again.
|
||||
#[test]
|
||||
fn a_paint_opacity_on_a_series_mark_survives_regeneration() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let bar = series(s.doc().unwrap().doc.node(id).unwrap(), 0).children().unwrap()[0].id;
|
||||
s.execute("paint.setFill", &json!({"ids": [bar.0], "opacity": 40})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"ids": [bar.0], "color": "#ff0000", "opacity": 60})).unwrap();
|
||||
let kept = spec(&s, id).series_paints[0].clone();
|
||||
assert_eq!((kept.fill_opacity, kept.stroke_opacity), (Some(0.4), Some(0.6)));
|
||||
s.execute("graph.setData", &json!({"rows": [[1, 2], [3, 4]]})).unwrap();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let marks = series(&n, 0).children().unwrap().to_vec();
|
||||
let opacities: Vec<(f32, f32)> = marks.iter().map(|m| (m.appearance.fill().unwrap().opacity, m.appearance.stroke().unwrap().opacity)).collect();
|
||||
assert!(opacities.iter().all(|o| *o == (0.4, 0.6)), "{opacities:?}");
|
||||
}
|
||||
|
||||
/// Clear Appearance and Reduce to Basic Appearance on a series mark take its fill and stroke opacity away, and the
|
||||
/// graph keeps none, so the series stays opaque when it is drawn again.
|
||||
#[test]
|
||||
|
||||
+219
-78
@@ -21,7 +21,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Fill",
|
||||
[],
|
||||
None,
|
||||
"{color?: \"#rrggbb\"|[r,g,b]|{c,m,y,k}|{gray}|{l,a,b} (CIE Lab; #rrggbbaa and #rgba are refused), none?: true, swatch?: name (a global or spot colour stays linked, so swatch edits recolour it; a tint swatch links to its base at its tint; a gradient swatch is recorded as the gradient's swatch and fits each object, keeping its aspect; the built-in \"[Registration]\" prints on every plate), tint?: 0..100 (% of a global or spot `swatch`; default 100, or a tint swatch's own), gradient?: {kind?: linear|radial|conic|diamond|freeform (conic, also accepted as angle or conical: the stops sweep once around the center `start`, clockwise from the vector; diamond: the stops run from the center `start` out to a square with a corner at `end`), method?: classic|perceptual|hue-clockwise|hue-counterclockwise (as paint.editGradient; default classic), spread?: pad|repeat|reflect (as paint.editGradient; default pad), repeats?: 1..100 (as paint.editGradient; default 1), stops?: [{offset 0..1, color, opacity? 0..1 (or 0..100), midpoint? 0.13..0.87, swatch?: global or spot colour (or tint) swatch the stop links to (its colour comes from the swatch), tint?: 0..100}] (at least 2; default white→black), angle?: deg counter-clockwise (0: the gradient runs left to right), start?: [x,y], end?: [x,y] (the vector in document coordinates, both or neither; type objects keep it in text space), aspect?: % (radial, conic and diamond; a negative one mirrors a conic's sweep, counterclockwise, and is an error for the other kinds; without start/end the gradient is placed on each object's bounds), focal?: [x,y] (radial, with start/end: the focal point, where the first stop sits; an error with a conic or diamond), dither?: bool (linear, radial, conic and diamond fills: drawn with fine noise that hides banding, and as pixels in SVG and PDF exports; strokes ignore it), swatch?: linked gradient swatch name}, item?: appearance item index|null (omitted: the Appearance panel's active item if it is a fill, else the top fill), ids?|id?, focus?: true (false keeps the active proxy), keepModel?: false (in a CMYK document, RGB colours and gradient stops are stored as CMYK unless true; Gray stays Gray), target?: \"object\"|\"contents\", span?: \"object\" (default: each target fits a gradient to its own bounds)|\"selection\" (a linear, radial, conic or diamond `gradient` without start/end is placed once on the union of the targets' bounds, so it runs across them)} sets the selection's fill and the default (new art fits a gradient to itself); a solid colour recolours only the gradient mesh point last clicked with the Mesh tool or Direct Selection; while the Type tool has characters selected (and no ids or item are given), the paint goes to those characters only; without `item`, a grayscale or 1-bit image takes a solid color as the color it is drawn in (white pixels transparent; `none` takes it off; an image with an alpha channel takes only a spot color, black or white; a colorized image whose link is missing takes a solid color as its new colorize color, and refuses a process color while its color is a spot color, black or white); with an object of a dynamic symbol instance selected by Direct Selection and no ids, id or item, that object's fill changes for this instance only (solid, none, gradient or pattern). On a path directly in a graph series, the graph also stores the paint of the topmost visible fill when the command wrote that row or changed it (exceptions: docs/mcp.md); on a graph label, the graph stores the paint for every label of its kind, as a character fill when the characters took it and with the label's rows when a row did → {objects: how many objects took the paint (the objects inside a group, unless an appearance item of the group's own is targeted; 0 when only the default changed)}",
|
||||
"{color?: \"#rrggbb\"|[r,g,b]|{c,m,y,k}|{gray}|{l,a,b} (CIE Lab; #rrggbbaa and #rgba are refused), none?: true, swatch?: name (a global or spot colour stays linked, so swatch edits recolour it; a tint swatch links to its base at its tint; a gradient swatch is recorded as the gradient's swatch and fits each object, keeping its aspect; the built-in \"[Registration]\" prints on every plate), tint?: 0..100 (% of a global or spot `swatch`; default 100, or a tint swatch's own), gradient?: {kind?: linear|radial|conic|diamond|freeform (conic, also accepted as angle or conical: the stops sweep once around the center `start`, clockwise from the vector; diamond: the stops run from the center `start` out to a square with a corner at `end`), method?: classic|perceptual|hue-clockwise|hue-counterclockwise (as paint.editGradient; default classic), spread?: pad|repeat|reflect (as paint.editGradient; default pad), repeats?: 1..100 (as paint.editGradient; default 1), stops?: [{offset 0..1, color, opacity? 0..1 (or 0..100), midpoint? 0.13..0.87, swatch?: global or spot colour (or tint) swatch the stop links to (its colour comes from the swatch), tint?: 0..100}] (at least 2; default white→black), angle?: deg counter-clockwise (0: the gradient runs left to right), start?: [x,y], end?: [x,y] (the vector in document coordinates, both or neither; type objects keep it in text space), aspect?: % (radial, conic and diamond; a negative one mirrors a conic's sweep, counterclockwise, and is an error for the other kinds; without start/end the gradient is placed on each object's bounds), focal?: [x,y] (radial, with start/end: the focal point, where the first stop sits; an error with a conic or diamond), dither?: bool (linear, radial, conic and diamond fills: drawn with fine noise that hides banding, and as pixels in SVG and PDF exports; strokes ignore it), swatch?: linked gradient swatch name}, item?: appearance item index|null (omitted: the Appearance panel's active item if it is a fill, else the top fill), opacity?: 0..100 (a percentage, as transparency.set's; the opacity of that fill item on each target, in the undo step of the paint, or alone without color, none, swatch or gradient, when it changes neither the defaults nor the recent colors; the gradient mesh points last clicked with the Mesh tool or Direct Selection take it themselves; with no paint, a target without that fill item is left as it is (a paint given with the opacity creates the item, which then takes it), and an opacity that no target takes is an error that also leaves the paint undone; type characters have no opacity of their own, so an opacity that only characters would take is an error, and so are one with nothing selected and no ids and one for an object selected in a symbol instance), ids?|id?, focus?: true (false keeps the active proxy), keepModel?: false (in a CMYK document, RGB colours and gradient stops are stored as CMYK unless true; Gray stays Gray), target?: \"object\"|\"contents\", span?: \"object\" (default: each target fits a gradient to its own bounds)|\"selection\" (a linear, radial, conic or diamond `gradient` without start/end is placed once on the union of the targets' bounds, so it runs across them)} sets the selection's fill and the default (new art fits a gradient to itself); a solid colour recolours only the gradient mesh point last clicked with the Mesh tool or Direct Selection; while the Type tool has characters selected (and no ids or item are given), the paint goes to those characters only; without `item`, a grayscale or 1-bit image takes a solid color as the color it is drawn in (white pixels transparent; `none` takes it off; an image with an alpha channel takes only a spot color, black or white; a colorized image whose link is missing takes a solid color as its new colorize color, and refuses a process color while its color is a spot color, black or white); with an object of a dynamic symbol instance selected by Direct Selection and no ids, id or item, that object's fill changes for this instance only (solid, none, gradient or pattern). On a path directly in a graph series, the graph also stores the paint of the topmost visible fill when the command wrote that row or changed it, and its opacity when `opacity` changed it (exceptions: docs/mcp.md); on a graph label, the graph stores the paint for every label of its kind, as a character fill when the characters took it and with the label's rows when a row did → {objects: how many objects took the paint (the objects inside a group, unless an appearance item of the group's own is targeted; 0 when only the default changed)}",
|
||||
has_doc,
|
||||
|s, p| set_paint(s, p, true)
|
||||
),
|
||||
@@ -30,7 +30,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Stroke",
|
||||
[],
|
||||
None,
|
||||
"{…as paint.setFill, item?: the stroke item to set} same as paint.setFill, for the stroke; with an object of a dynamic symbol instance selected by Direct Selection and no ids, id or item, that object's stroke changes for this instance only (solid, none, gradient or pattern). On a path directly in a graph series, the graph also stores the paint and weight of the topmost visible stroke when the command wrote that row or changed it (exceptions: docs/mcp.md); on a graph label, as paint.setFill",
|
||||
"{…as paint.setFill, item?: the stroke item to set, opacity?: 0..100 (the opacity of that stroke item, as paint.setFill's for the fill item)} same as paint.setFill, for the stroke; with an object of a dynamic symbol instance selected by Direct Selection and no ids, id or item, that object's stroke changes for this instance only (solid, none, gradient or pattern). On a path directly in a graph series, the graph also stores the paint and weight of the topmost visible stroke when the command wrote that row or changed it, and its opacity when `opacity` changed it (exceptions: docs/mcp.md); on a graph label, as paint.setFill",
|
||||
has_doc,
|
||||
|s, p| { set_paint(s, p, false) }
|
||||
),
|
||||
@@ -154,7 +154,7 @@ pub fn proxy_specs() -> Vec<CommandSpec> {
|
||||
"Fill and Stroke",
|
||||
[],
|
||||
None,
|
||||
"{} → {fill, stroke: the paints the Fill/Stroke proxies show (the Appearance panel's active item for the proxy of its kind, else the first selected object's, a group's first painted object's, type: its first run's; else the defaults for new art), fillActive, fillMixed, strokeMixed: the selected objects' (or a selected group's contents') fills (strokes) differ, shown as a \"?\" proxy}",
|
||||
"{} → {fill, stroke: the paints the Fill/Stroke proxies show (the Appearance panel's active item for the proxy of its kind, else the first selected object's, a group's first painted object's, type: its first run's; else the defaults for new art), fillActive, fillMixed, strokeMixed: the selected objects' (or a selected group's contents') fills (strokes) differ, shown as a \"?\" proxy, fillOpacity, strokeOpacity: 0..100, the opacity of the fill (stroke) item that paint.setFill {opacity} sets, from the first selected object (or painted object in a selected group) that has one (a gradient mesh's first picked point's), or null with nothing selected (the defaults for new art), for an object selected in a symbol instance, and when only type characters or objects without that item are selected}",
|
||||
always,
|
||||
proxies
|
||||
),
|
||||
@@ -208,6 +208,44 @@ impl Session {
|
||||
nodes.first().map(|n| (Cow::Borrowed(*n), None))
|
||||
}
|
||||
|
||||
/// The opacity (0..1) of the fill (or stroke) that `paint.setFill {opacity}` sets: the item's,
|
||||
/// or for a gradient mesh with points picked the first picked point's. The rows the command
|
||||
/// would edit are read in order (a group without an active Appearance row stands for its
|
||||
/// contents), and the first that exists answers. `None` with nothing selected, for an object
|
||||
/// selected in a symbol instance, and when only type characters or objects without that item
|
||||
/// are selected.
|
||||
pub fn proxy_opacity(&self, stroke: bool) -> Option<f32> {
|
||||
let st = self.active()?;
|
||||
if st.selection.symbol_part.is_some() {
|
||||
return None;
|
||||
}
|
||||
let item = self.appearance_item();
|
||||
let of = |n: &Node, item: Option<usize>| -> Option<f32> {
|
||||
if item.is_none() && matches!(n.kind, NodeKind::Text(_)) {
|
||||
return None;
|
||||
}
|
||||
if !stroke
|
||||
&& item.is_none()
|
||||
&& let NodeKind::Mesh(m) = &n.kind
|
||||
&& let Some((_, points)) = picked_mesh_points(self, &[n.id]).first()
|
||||
{
|
||||
return points.first().and_then(|i| m.points.get(*i)).map(|q| q.opacity);
|
||||
}
|
||||
if stroke { n.appearance.stroke_at(item).map(|s| s.opacity) } else { n.appearance.fill_at(item).map(|f| f.opacity) }
|
||||
};
|
||||
let subjects = st.selection.subjects();
|
||||
// As the command picks its rows (ItemTarget::of_kind): the active item when it is a row of
|
||||
// this kind in the first selected object's stack, read in the selected objects' own
|
||||
// stacks; else the topmost one of the painted leaves (a group's contents).
|
||||
let first = subjects.first().and_then(|id| st.doc.node(*id));
|
||||
let active = item.filter(|i| first.and_then(|n| n.appearance.items.get(*i)).is_some_and(|it| it.is_fill() == !stroke));
|
||||
if active.is_some() {
|
||||
return subjects.iter().filter_map(|id| st.doc.node(*id)).find_map(|n| of(n, active));
|
||||
}
|
||||
let leaves = super::leaf_targets(self, subjects).ok()?;
|
||||
leaves.iter().filter_map(|id| st.doc.node(*id)).find_map(|n| of(n, None))
|
||||
}
|
||||
|
||||
pub(crate) fn remember_paint_now(&mut self, p: &Paint) {
|
||||
match p {
|
||||
Paint::Solid { color, .. } => {
|
||||
@@ -321,7 +359,12 @@ impl Session {
|
||||
fn proxies(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let (fill, stroke) = s.proxy_paints();
|
||||
let (fill_mixed, stroke_mixed) = s.proxy_mixed();
|
||||
Ok(json!({"fill": fill, "stroke": stroke, "fillActive": s.fill_active, "fillMixed": fill_mixed, "strokeMixed": stroke_mixed}))
|
||||
// As transparency.info answers an opacity: a percentage to two places.
|
||||
let percent = |o: Option<f32>| o.map(|o| (f64::from(o) * 10_000.0).round() / 100.0);
|
||||
Ok(json!({
|
||||
"fill": fill, "stroke": stroke, "fillActive": s.fill_active, "fillMixed": fill_mixed, "strokeMixed": stroke_mixed,
|
||||
"fillOpacity": percent(s.proxy_opacity(false)), "strokeOpacity": percent(s.proxy_opacity(true)),
|
||||
}))
|
||||
}
|
||||
|
||||
/// `stroke` param, defaulting to the proxy that is in front.
|
||||
@@ -440,13 +483,34 @@ fn set_paint(s: &mut Session, p: &Value, fill: bool) -> Result<Value> {
|
||||
return Err(bad(cmd, "`tint` goes with a global or spot `swatch`"));
|
||||
}
|
||||
check_keep_model(p, cmd)?;
|
||||
let paint = paint_from(s, p)?.ok_or_else(|| bad(cmd, "give color, none, swatch or gradient"))?;
|
||||
let opacity = opacity_param(p, cmd)?;
|
||||
let paint = paint_from(s, p)?;
|
||||
if paint.is_none() && opacity.is_none() {
|
||||
return Err(bad(cmd, "give color, none, swatch, gradient or opacity"));
|
||||
}
|
||||
// As `paint.editGradient`: a `dither` given applies to gradient fills only. A gradient swatch
|
||||
// (named) keeps its own dither; `"swatch": null` names none.
|
||||
if !fill && matches!(&paint, Paint::Gradient(g) if g.gradient.dither) && p.get("swatch").and_then(Value::as_str).is_none() {
|
||||
if !fill && matches!(&paint, Some(Paint::Gradient(g)) if g.gradient.dither) && p.get("swatch").and_then(Value::as_str).is_none() {
|
||||
return Err(bad(cmd, "`dither` applies to gradient fills (paint.setFill)"));
|
||||
}
|
||||
apply_paint(s, p, paint, fill)
|
||||
apply_paint_opacity(s, p, paint, opacity, fill)
|
||||
}
|
||||
|
||||
/// The `opacity` param of `paint.setFill` and `paint.setStroke` (a percentage) as 0..1; `None`
|
||||
/// when it is left out or null.
|
||||
fn opacity_param(p: &Value, cmd: &str) -> Result<Option<f32>> {
|
||||
match p.get("opacity") {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(v) => match v.as_f64() {
|
||||
Some(o) if (0.0..=100.0).contains(&o) => Ok(Some((o / 100.0) as f32)),
|
||||
_ => Err(bad(cmd, "`opacity` must be a number from 0 to 100")),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// The error for an `opacity` that would go to type characters, which have no opacity of their own.
|
||||
fn characters_have_no_opacity(cmd: &str) -> EngineError {
|
||||
bad(cmd, "type characters have no opacity of their own: add a fill or stroke with the Appearance panel's Add New Fill or Add New Stroke")
|
||||
}
|
||||
|
||||
/// [`apply_paint`] on the proxy named by `stroke` (default: the active one), focusing it.
|
||||
@@ -459,15 +523,28 @@ fn apply_to_proxy(s: &mut Session, p: &Value, paint: Paint, cmd: &str) -> Result
|
||||
/// `paint.setFill`) and of the defaults for new art, focus its proxy unless `focus` is false, and
|
||||
/// remember the paint. Replies `{objects}`: how many objects took the paint.
|
||||
pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool) -> Result<Value> {
|
||||
apply_paint_opacity(s, p, Some(paint), None, fill)
|
||||
}
|
||||
|
||||
/// [`apply_paint`] with `paint` optional, which also sets the `opacity` (0..1) of the fill (or
|
||||
/// stroke) item each target takes it on, in the same undo step: the picked gradient mesh points'
|
||||
/// own opacity for a fill, nothing on a target without that item. The opacity alone changes neither
|
||||
/// the defaults for new art nor the remembered colors. An opacity that would go only to type
|
||||
/// characters, to an object selected in a symbol instance, or to no target at all is an error.
|
||||
fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option<Paint>, opacity: Option<f32>, fill: bool) -> Result<Value> {
|
||||
let cmd = if fill { "paint.setFill" } else { "paint.setStroke" };
|
||||
let focus = switch_or(p, "focus", true, cmd)?;
|
||||
let span = spans_targets(p, &paint).map_err(|e| bad(cmd, e))?;
|
||||
let span = match &paint {
|
||||
Some(paint) => spans_targets(p, paint).map_err(|e| bad(cmd, e))?,
|
||||
None => false,
|
||||
};
|
||||
// Characters selected with the Type tool take the paint, not the whole type object (#1063).
|
||||
if p.get("ids").is_none()
|
||||
&& p.get("item").is_none()
|
||||
&& let Some((id, a, b)) = super::textedit::editing_range(s)?
|
||||
&& a != b
|
||||
{
|
||||
let (Some(paint), None) = (paint, opacity) else { return Err(characters_have_no_opacity(cmd)) };
|
||||
// After Select All in threaded text, every frame's text takes it.
|
||||
let story = super::textedit::editing_story(s);
|
||||
let ranges: Vec<(NodeId, usize, usize)> =
|
||||
@@ -520,6 +597,9 @@ pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool)
|
||||
// An object of a dynamic symbol instance's art selected with Direct Selection: that object's
|
||||
// fill or stroke changes for this instance only (solid, none, gradient or pattern).
|
||||
if let Some((id, path)) = selected_part_target(s, p, cmd)? {
|
||||
let Some(paint) = paint.filter(|_| opacity.is_none()) else {
|
||||
return Err(bad(cmd, "an object selected in a symbol instance has no fill or stroke opacity of its own"));
|
||||
};
|
||||
super::brushsym::edit_part(s, id, &path, if fill { "Fill Color" } else { "Stroke Color" }, |o| {
|
||||
if fill {
|
||||
o.fill = Some(paint.clone());
|
||||
@@ -538,23 +618,124 @@ pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool)
|
||||
let item = item_target(s, p, cmd)?.of_kind(s, fill);
|
||||
let selected = item.targets(s, p)?;
|
||||
let ids = super::graph::paint_targets(&s.doc()?.doc, &selected);
|
||||
let paint = if span { placed_across(&s.doc()?.doc, &ids, paint, p, fill) } else { paint };
|
||||
// An opacity has nothing to go to without targets; with a paint, the defaults then stay too.
|
||||
if opacity.is_some() && ids.is_empty() {
|
||||
return Err(bad(cmd, "select objects or give ids"));
|
||||
}
|
||||
let paint = match paint {
|
||||
Some(paint) if span => Some(placed_across(&s.doc()?.doc, &ids, paint, p, fill)),
|
||||
paint => paint,
|
||||
};
|
||||
// The gradient mesh points picked with the Mesh tool or Direct Selection: a solid fill
|
||||
// recolours just those points of their meshes.
|
||||
let mesh_points = if fill && matches!(paint, Paint::Solid { .. }) { picked_mesh_points(s, &ids) } else { vec![] };
|
||||
// recolours just those points of their meshes, and a fill opacity fades just them.
|
||||
let mesh_points = if fill && matches!(paint, Some(Paint::Solid { .. }) | None) { picked_mesh_points(s, &ids) } else { vec![] };
|
||||
// Without `item`, unless the Appearance panel's active fill item takes the paint.
|
||||
let colorized = if fill && p.get("item").is_none() && item == ItemTarget::Top {
|
||||
colorized_images(&s.doc()?.doc, &ids, &paint).map_err(|e| bad(cmd, e))?
|
||||
} else {
|
||||
vec![]
|
||||
let colorized = match &paint {
|
||||
Some(paint) if fill && p.get("item").is_none() && item == ItemTarget::Top => {
|
||||
colorized_images(&s.doc()?.doc, &ids, paint).map_err(|e| bad(cmd, e))?
|
||||
}
|
||||
_ => vec![],
|
||||
};
|
||||
let label = match paint {
|
||||
None => "Opacity",
|
||||
Some(_) if fill => "Fill Color",
|
||||
Some(_) => "Stroke Color",
|
||||
};
|
||||
let label = if fill { "Fill Color" } else { "Stroke Color" };
|
||||
// Each object whose appearance took the paint, with the row it went to, for the paint a graph stores for its series
|
||||
// (a series mark is a path, so it always has a row here).
|
||||
let written = std::cell::RefCell::new(Vec::new());
|
||||
// The type objects whose characters took the paint (no row targeted): only those change what a graph's labels keep
|
||||
// for their characters; a row edit is kept with the label's rows.
|
||||
let characters = std::cell::RefCell::new(Vec::new());
|
||||
// Whether a target took the opacity, and whether one would have given it to its characters.
|
||||
let (took_opacity, characters_skipped) = (std::cell::Cell::new(false), std::cell::Cell::new(false));
|
||||
let paint_one = |n: &mut Node, index: Option<usize>, paint: &Paint| -> Result<()> {
|
||||
if index.is_none() && matches!(n.kind, NodeKind::Text(_)) {
|
||||
characters.borrow_mut().push(n.id);
|
||||
}
|
||||
if index.is_none() && colorized.contains(&n.id) {
|
||||
n.extras_mut().colorize = paint.color().map(|_| Box::new(paint.clone()));
|
||||
return Ok(());
|
||||
}
|
||||
if index.is_none()
|
||||
&& let Paint::Solid { color, .. } = paint
|
||||
&& let Some((_, points)) = mesh_points.iter().find(|(id, _)| *id == n.id)
|
||||
&& let NodeKind::Mesh(m) = &mut n.kind
|
||||
{
|
||||
for i in points {
|
||||
if let Some(q) = m.points.get_mut(*i) {
|
||||
q.color = *color;
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if index.is_none()
|
||||
&& let NodeKind::Text(t) = &mut n.kind
|
||||
{
|
||||
let (xf, lb) = (t.xf, t.local_bounds());
|
||||
for r in &mut t.runs {
|
||||
if !fill && !paint.is_none() {
|
||||
run_stroke_weight(&mut r.style);
|
||||
}
|
||||
let (cur, b) = run_paint_mut(r, !fill, lb);
|
||||
*cur = place_run_paint(paint, p, xf, b);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
// A missing top fill or stroke is created first: a new stroke's weight sizes the box its
|
||||
// gradient fits.
|
||||
if n.appearance.paint_at(index, fill).is_none() {
|
||||
n.appearance.set_paint_at(index, fill, Paint::None);
|
||||
}
|
||||
if !fill {
|
||||
restore_unpainted_stroke(&mut n.appearance, index, paint);
|
||||
}
|
||||
let new_paint = keep_pattern_xf(paint, p, n.appearance.paint_at(index, fill));
|
||||
let placed = place_paint(&new_paint, p, item_paint_bounds(n, index, fill));
|
||||
n.appearance.set_paint_at(index, fill, placed);
|
||||
if !fill
|
||||
&& paint.is_none()
|
||||
&& let Some(stroke) = n.appearance.stroke_at_mut(index)
|
||||
{
|
||||
// A stroke without paint has no weight; painting it again restores the 1 pt default.
|
||||
stroke.width = 0.0;
|
||||
}
|
||||
if !fill {
|
||||
super::object::snap_if_aligned(n);
|
||||
}
|
||||
let at = match index {
|
||||
Some(_) => n.appearance.item_of_kind(index, fill),
|
||||
None => n.appearance.items.iter().rposition(|it| it.is_fill() == fill),
|
||||
};
|
||||
written.borrow_mut().extend(at.map(|at| (n.id, at)));
|
||||
Ok(())
|
||||
};
|
||||
// The opacity goes to what the paint went to: the picked mesh points, else the fill or stroke
|
||||
// item. Characters, and targets without that item, are left as they are.
|
||||
let opacity_one = |n: &mut Node, index: Option<usize>, o: f32| {
|
||||
if index.is_none() && matches!(n.kind, NodeKind::Text(_)) {
|
||||
characters_skipped.set(true);
|
||||
return;
|
||||
}
|
||||
if index.is_none()
|
||||
&& let Some((_, points)) = mesh_points.iter().find(|(id, _)| *id == n.id)
|
||||
&& let NodeKind::Mesh(m) = &mut n.kind
|
||||
{
|
||||
for i in points {
|
||||
if let Some(q) = m.points.get_mut(*i) {
|
||||
q.opacity = o;
|
||||
took_opacity.set(true);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
let item =
|
||||
if fill { n.appearance.fill_at_mut(index).map(|f| &mut f.opacity) } else { n.appearance.stroke_at_mut(index).map(|s| &mut s.opacity) };
|
||||
if let Some(item) = item {
|
||||
*item = o;
|
||||
took_opacity.set(true);
|
||||
}
|
||||
};
|
||||
edit_items_then(
|
||||
s,
|
||||
&ids,
|
||||
@@ -563,87 +744,47 @@ pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool)
|
||||
label,
|
||||
fill,
|
||||
|n, index| {
|
||||
if index.is_none() && matches!(n.kind, NodeKind::Text(_)) {
|
||||
characters.borrow_mut().push(n.id);
|
||||
if let Some(paint) = &paint {
|
||||
paint_one(n, index, paint)?;
|
||||
}
|
||||
if index.is_none() && colorized.contains(&n.id) {
|
||||
n.extras_mut().colorize = paint.color().map(|_| Box::new(paint.clone()));
|
||||
return Ok(());
|
||||
if let Some(o) = opacity {
|
||||
opacity_one(n, index, o);
|
||||
}
|
||||
if index.is_none()
|
||||
&& let Paint::Solid { color, .. } = &paint
|
||||
&& let Some((_, points)) = mesh_points.iter().find(|(id, _)| *id == n.id)
|
||||
&& let NodeKind::Mesh(m) = &mut n.kind
|
||||
{
|
||||
for i in points {
|
||||
if let Some(q) = m.points.get_mut(*i) {
|
||||
q.color = *color;
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if index.is_none()
|
||||
&& let NodeKind::Text(t) = &mut n.kind
|
||||
{
|
||||
let (xf, lb) = (t.xf, t.local_bounds());
|
||||
for r in &mut t.runs {
|
||||
if !fill && !paint.is_none() {
|
||||
run_stroke_weight(&mut r.style);
|
||||
}
|
||||
let (cur, b) = run_paint_mut(r, !fill, lb);
|
||||
*cur = place_run_paint(&paint, p, xf, b);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
// A missing top fill or stroke is created first: a new stroke's weight sizes the box its
|
||||
// gradient fits.
|
||||
if n.appearance.paint_at(index, fill).is_none() {
|
||||
n.appearance.set_paint_at(index, fill, Paint::None);
|
||||
}
|
||||
if !fill {
|
||||
restore_unpainted_stroke(&mut n.appearance, index, &paint);
|
||||
}
|
||||
let new_paint = keep_pattern_xf(&paint, p, n.appearance.paint_at(index, fill));
|
||||
let placed = place_paint(&new_paint, p, item_paint_bounds(n, index, fill));
|
||||
n.appearance.set_paint_at(index, fill, placed);
|
||||
if !fill
|
||||
&& paint.is_none()
|
||||
&& let Some(stroke) = n.appearance.stroke_at_mut(index)
|
||||
{
|
||||
// A stroke without paint has no weight; painting it again restores the 1 pt default.
|
||||
stroke.width = 0.0;
|
||||
}
|
||||
if !fill {
|
||||
super::object::snap_if_aligned(n);
|
||||
}
|
||||
let at = match index {
|
||||
Some(_) => n.appearance.item_of_kind(index, fill),
|
||||
None => n.appearance.items.iter().rposition(|it| it.is_fill() == fill),
|
||||
};
|
||||
written.borrow_mut().extend(at.map(|at| (n.id, at)));
|
||||
Ok(())
|
||||
},
|
||||
|before, d, changed| {
|
||||
// An opacity that no target took is an error, which also takes back the paint given with it.
|
||||
if opacity.is_some() && !took_opacity.get() {
|
||||
if characters_skipped.get() {
|
||||
return Err(characters_have_no_opacity(cmd));
|
||||
}
|
||||
return Err(bad(cmd, "no target has that fill or stroke to take the opacity"));
|
||||
}
|
||||
// A graph keeps the opacity of a series mark's topmost visible fill and stroke for the whole series.
|
||||
let kept = opacity.is_some() && super::appearance::capture_series_changes(before, d, changed);
|
||||
let Some(paint) = &paint else { return Ok(kept) };
|
||||
let series = super::appearance::capture_series_paint_writes(before, d, changed, &written.borrow());
|
||||
let labels = super::graph::capture_label_styles(d, &characters.borrow(), &|st| {
|
||||
if fill {
|
||||
st.fill = unplaced(&paint);
|
||||
st.fill = unplaced(paint);
|
||||
} else {
|
||||
if !paint.is_none() {
|
||||
run_stroke_weight(st);
|
||||
}
|
||||
st.stroke = unplaced(&paint);
|
||||
st.stroke = unplaced(paint);
|
||||
}
|
||||
});
|
||||
Ok(series || labels)
|
||||
Ok(series || labels || kept)
|
||||
},
|
||||
)?;
|
||||
// Rejected edits leave the next shape's appearance and active proxy untouched.
|
||||
paint_new_art(s, &paint, fill);
|
||||
if focus {
|
||||
s.fill_active = fill;
|
||||
}
|
||||
s.remember_paint(&paint);
|
||||
// Rejected edits leave the next shape's appearance and active proxy untouched.
|
||||
if let Some(paint) = &paint {
|
||||
paint_new_art(s, paint, fill);
|
||||
s.remember_paint(paint);
|
||||
}
|
||||
Ok(json!({"objects": ids.iter().collect::<std::collections::BTreeSet<_>>().len()}))
|
||||
}
|
||||
|
||||
|
||||
@@ -116,6 +116,7 @@ mod outlineobject;
|
||||
pub(super) mod outlinestroke;
|
||||
mod overprint;
|
||||
mod package;
|
||||
mod paintopacity;
|
||||
mod paintproxy;
|
||||
mod panelcmds;
|
||||
mod paragraphs;
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
//! `opacity` on `paint.setFill` and `paint.setStroke`: the opacity of the fill or stroke item the
|
||||
//! paint goes to, and `fillOpacity` and `strokeOpacity` on `paint.proxies`.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::{Color, Paint};
|
||||
use vectorcraft_doc::{Node, NodeKind};
|
||||
use vectorcraft_tools::{PointerEvent, PointerKind};
|
||||
|
||||
use crate::tooling::ViewInfo;
|
||||
use crate::*;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn rect(s: &mut Session, x: f64) -> NodeId {
|
||||
let r = s.execute("shape.rectangle", &json!({"x": x, "y": 10, "width": 100, "height": 50})).unwrap();
|
||||
NodeId(r["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn text(s: &mut Session) -> NodeId {
|
||||
let r = s.execute("text.create", &json!({"x": 10, "y": 150, "text": "Hi"})).unwrap();
|
||||
NodeId(r["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn node(s: &Session, id: NodeId) -> Node {
|
||||
s.doc().unwrap().doc.node(id).unwrap().clone()
|
||||
}
|
||||
|
||||
fn select(s: &mut Session, ids: &[NodeId]) {
|
||||
s.execute("select.set", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})).unwrap();
|
||||
}
|
||||
|
||||
fn fill_opacity(s: &Session, id: NodeId) -> f32 {
|
||||
node(s, id).appearance.fill().unwrap().opacity
|
||||
}
|
||||
|
||||
fn undo_labels(s: &Session) -> Vec<String> {
|
||||
s.doc().unwrap().history.undo.iter().map(|e| e.label.clone()).collect()
|
||||
}
|
||||
|
||||
fn proxies(s: &mut Session) -> Value {
|
||||
s.execute("paint.proxies", &json!({})).unwrap()
|
||||
}
|
||||
|
||||
/// `paint.setFill {opacity}` alone sets the opacity of the selected object's top fill item, as one
|
||||
/// undo step named Opacity, and leaves its paint, its stroke and the object's own opacity as they
|
||||
/// were; `paint.setStroke {opacity}` does the same for the top stroke, and `paint.proxies` answers
|
||||
/// both.
|
||||
#[test]
|
||||
fn opacity_alone_sets_the_fill_or_stroke_item() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
let before = node(&s, id);
|
||||
let undo = undo_labels(&s).len();
|
||||
s.execute("paint.setFill", &json!({"opacity": 40})).unwrap();
|
||||
let n = node(&s, id);
|
||||
assert!((fill_opacity(&s, id) - 0.4).abs() < 1e-6);
|
||||
assert_eq!(n.appearance.fill_paint(), before.appearance.fill_paint());
|
||||
assert_eq!(n.appearance.stroke().unwrap().opacity, 1.0);
|
||||
assert_eq!(n.opacity, 1.0);
|
||||
let labels = undo_labels(&s);
|
||||
assert_eq!((labels.len(), labels.last().map(String::as_str)), (undo + 1, Some("Opacity")));
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 40.0);
|
||||
assert_eq!(proxies(&mut s)["strokeOpacity"], 100.0);
|
||||
// The defaults for new art keep their paint.
|
||||
assert_eq!(s.paint.fill, Paint::solid(Color::WHITE));
|
||||
|
||||
s.execute("paint.setStroke", &json!({"opacity": 25})).unwrap();
|
||||
assert!((node(&s, id).appearance.stroke().unwrap().opacity - 0.25).abs() < 1e-6);
|
||||
assert!((fill_opacity(&s, id) - 0.4).abs() < 1e-6);
|
||||
assert_eq!(proxies(&mut s)["strokeOpacity"], 25.0);
|
||||
}
|
||||
|
||||
/// A selected group stands for its contents: every member's fill takes the opacity, as one undo step.
|
||||
#[test]
|
||||
fn a_group_sets_every_member_in_one_step() {
|
||||
let mut s = session();
|
||||
let (a, b) = (rect(&mut s, 10.0), rect(&mut s, 200.0));
|
||||
select(&mut s, &[a, b]);
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
let undo = undo_labels(&s).len();
|
||||
s.execute("paint.setFill", &json!({"opacity": 60})).unwrap();
|
||||
assert!((fill_opacity(&s, a) - 0.6).abs() < 1e-6 && (fill_opacity(&s, b) - 0.6).abs() < 1e-6);
|
||||
assert_eq!(undo_labels(&s).len(), undo + 1);
|
||||
}
|
||||
|
||||
/// A group with a fill of its own: without an active Appearance row, `paint.proxies` answers the
|
||||
/// opacity of the contents' fills, which `paint.setFill {opacity}` sets, not the group's own fill.
|
||||
#[test]
|
||||
fn a_group_with_its_own_fill_answers_for_its_contents() {
|
||||
let mut s = session();
|
||||
let (a, b) = (rect(&mut s, 10.0), rect(&mut s, 200.0));
|
||||
select(&mut s, &[a, b]);
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
let g = s.doc().unwrap().selection.subjects()[0];
|
||||
select(&mut s, &[g]);
|
||||
s.execute("appearance.addFill", &json!({})).unwrap();
|
||||
let own = node(&s, g).appearance.items.iter().position(|it| it.is_fill()).unwrap();
|
||||
s.execute("paint.setFill", &json!({"item": own, "opacity": 30})).unwrap();
|
||||
select(&mut s, &[g]);
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 100.0);
|
||||
s.execute("paint.setFill", &json!({"opacity": 60})).unwrap();
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 60.0);
|
||||
assert!((node(&s, g).appearance.items[own].opacity() - 0.3).abs() < 1e-6, "the group's own fill keeps its opacity");
|
||||
}
|
||||
|
||||
/// With the Appearance panel's lower fill row active, that row takes the opacity and the top fill
|
||||
/// keeps its own.
|
||||
#[test]
|
||||
fn the_active_appearance_row_takes_the_opacity() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
s.execute("appearance.addFill", &json!({})).unwrap(); // [Fill, Stroke, Fill]
|
||||
s.execute("appearance.setActiveItem", &json!({"index": 0})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"opacity": 30})).unwrap();
|
||||
let ap = node(&s, id).appearance;
|
||||
assert!((ap.items[0].opacity() - 0.3).abs() < 1e-6);
|
||||
assert_eq!(ap.items[2].opacity(), 1.0);
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 30.0);
|
||||
}
|
||||
|
||||
/// A color and an opacity change together in one undo step, and the same call again records no
|
||||
/// step.
|
||||
#[test]
|
||||
fn color_and_opacity_are_one_step_and_a_repeat_records_none() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
let undo = undo_labels(&s).len();
|
||||
let p = json!({"color": "#ff0000", "opacity": 50});
|
||||
s.execute("paint.setFill", &p).unwrap();
|
||||
assert_eq!(node(&s, id).appearance.fill_paint(), Paint::solid(Color::rgb8(255, 0, 0)));
|
||||
assert!((fill_opacity(&s, id) - 0.5).abs() < 1e-6);
|
||||
assert_eq!(undo_labels(&s).len(), undo + 1);
|
||||
assert_eq!(undo_labels(&s).last().map(String::as_str), Some("Fill Color"));
|
||||
s.execute("paint.setFill", &p).unwrap();
|
||||
s.execute("paint.setFill", &json!({"opacity": 50})).unwrap();
|
||||
assert_eq!(undo_labels(&s).len(), undo + 1);
|
||||
}
|
||||
|
||||
/// Bad values, nothing selected, and characters selected with the Type tool answer errors and leave
|
||||
/// the document and the defaults for new art as they were.
|
||||
#[test]
|
||||
fn errors_leave_the_document_and_defaults_alone() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
for bad in [json!(101), json!(-1), json!("50"), json!(true)] {
|
||||
let e = s.execute("paint.setFill", &json!({"opacity": bad})).unwrap_err();
|
||||
assert!(e.to_string().contains("`opacity` must be a number from 0 to 100"), "{bad}: {e}");
|
||||
}
|
||||
let e = s.execute("paint.setFill", &json!({})).unwrap_err();
|
||||
assert!(e.to_string().contains("give color, none, swatch, gradient or opacity"), "{e}");
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
for p in [json!({"opacity": 40}), json!({"color": "#ff0000", "opacity": 40})] {
|
||||
let e = s.execute("paint.setFill", &p).unwrap_err();
|
||||
assert!(e.to_string().contains("select objects or give ids"), "{p}: {e}");
|
||||
}
|
||||
assert_eq!(s.paint.fill, Paint::solid(Color::WHITE));
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
|
||||
// Characters selected with the Type tool.
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("type", v).unwrap();
|
||||
for kind in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(kind, 50.0, 250.0), v).unwrap();
|
||||
}
|
||||
s.tool_text("Sample", v).unwrap();
|
||||
s.set_tool_option("select", &json!({"start": 0, "end": 3}));
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
for p in [json!({"opacity": 40}), json!({"color": "#ff0000", "opacity": 40})] {
|
||||
let e = s.execute("paint.setFill", &p).unwrap_err();
|
||||
assert!(e.to_string().contains("type characters have no opacity of their own"), "{p}: {e}");
|
||||
}
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], Value::Null);
|
||||
}
|
||||
|
||||
/// A type object whose characters take the paint is left alone beside a rectangle that takes the
|
||||
/// opacity, with no error; alone, it answers the characters error.
|
||||
#[test]
|
||||
fn type_beside_other_art_is_left_alone() {
|
||||
let mut s = session();
|
||||
let r = rect(&mut s, 10.0);
|
||||
let t = text(&mut s);
|
||||
let runs = |s: &Session| match &node(s, t).kind {
|
||||
NodeKind::Text(t) => t.runs.clone(),
|
||||
_ => panic!("not type"),
|
||||
};
|
||||
let before = (node(&s, t), runs(&s));
|
||||
select(&mut s, &[r, t]);
|
||||
s.execute("paint.setFill", &json!({"opacity": 40})).unwrap();
|
||||
assert!((fill_opacity(&s, r) - 0.4).abs() < 1e-6);
|
||||
assert_eq!((node(&s, t), runs(&s)), before);
|
||||
select(&mut s, &[t]);
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], Value::Null);
|
||||
let e = s.execute("paint.setFill", &json!({"opacity": 40})).unwrap_err();
|
||||
assert!(e.to_string().contains("type characters have no opacity of their own"), "{e}");
|
||||
assert_eq!((node(&s, t), runs(&s)), before);
|
||||
}
|
||||
|
||||
/// A color with an opacity on type alone answers the characters error and takes the color back too.
|
||||
#[test]
|
||||
fn type_alone_takes_back_the_paint_given_with_an_opacity() {
|
||||
let mut s = session();
|
||||
let t = text(&mut s);
|
||||
select(&mut s, &[t]);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let undo = undo_labels(&s).len();
|
||||
let e = s.execute("paint.setFill", &json!({"color": "#ff0000", "opacity": 40})).unwrap_err();
|
||||
assert!(e.to_string().contains("type characters have no opacity of their own"), "{e}");
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
assert_eq!(undo_labels(&s).len(), undo);
|
||||
assert_eq!(s.paint.fill, Paint::solid(Color::WHITE), "the defaults keep their paint");
|
||||
}
|
||||
|
||||
/// An opacity that no target takes (the only selected object has no fill item) is an error, with or
|
||||
/// without a paint, and leaves the document as it was; `paint.proxies` answers null for it, and
|
||||
/// beside an object with a fill item it answers that object's opacity.
|
||||
#[test]
|
||||
fn an_opacity_no_target_takes_is_an_error() {
|
||||
let mut s = session();
|
||||
let (a, b) = (rect(&mut s, 10.0), rect(&mut s, 200.0));
|
||||
select(&mut s, &[a]);
|
||||
s.execute("appearance.removeItem", &json!({"index": 0})).unwrap();
|
||||
assert!(node(&s, a).appearance.fill().is_none());
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], Value::Null);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let e = s.execute("paint.setFill", &json!({"opacity": 40})).unwrap_err();
|
||||
assert!(e.to_string().contains("no target has that fill or stroke to take the opacity"), "{e}");
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
// A paint given with the opacity creates the fill, which takes the opacity.
|
||||
s.execute("paint.setFill", &json!({"color": "#ff0000", "opacity": 40})).unwrap();
|
||||
assert!((fill_opacity(&s, a) - 0.4).abs() < 1e-6);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
|
||||
select(&mut s, &[b]);
|
||||
s.execute("paint.setFill", &json!({"opacity": 70})).unwrap();
|
||||
select(&mut s, &[a, b]);
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 70.0, "the first selected object with a fill item answers");
|
||||
s.execute("paint.setFill", &json!({"opacity": 20})).unwrap();
|
||||
assert!((fill_opacity(&s, b) - 0.2).abs() < 1e-6);
|
||||
assert!(node(&s, a).appearance.fill().is_none());
|
||||
}
|
||||
|
||||
/// An object selected in a dynamic symbol instance has no opacity of its own: `opacity` is an error
|
||||
/// that leaves the document alone, and `paint.proxies` answers null.
|
||||
#[test]
|
||||
fn an_object_in_a_symbol_instance_refuses_an_opacity() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0);
|
||||
select(&mut s, &[id]);
|
||||
let inst = s.execute("symbol.new", &json!({"name": "One", "ids": [id.0], "type": "dynamic"})).unwrap()["id"].clone();
|
||||
s.execute("symbol.instance.selectPart", &json!({"id": inst, "path": []})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
for p in [json!({"opacity": 40}), json!({"color": "#ff0000", "opacity": 40})] {
|
||||
let e = s.execute("paint.setFill", &p).unwrap_err();
|
||||
assert!(e.to_string().contains("an object selected in a symbol instance has no fill or stroke opacity of its own"), "{p}: {e}");
|
||||
}
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], Value::Null);
|
||||
}
|
||||
|
||||
/// A gradient mesh point picked with the Mesh tool takes a fill opacity itself, and the proxy shows
|
||||
/// it; the object's fill item and the other points stay as they were.
|
||||
#[test]
|
||||
fn a_picked_mesh_point_takes_the_fill_opacity() {
|
||||
let mut s = session();
|
||||
let m = rect(&mut s, 10.0);
|
||||
select(&mut s, &[m]);
|
||||
s.execute("object.mesh.create", &json!({"rows": 2, "cols": 2})).unwrap();
|
||||
select(&mut s, &[m]);
|
||||
let v = ViewInfo::default();
|
||||
let NodeKind::Mesh(g) = &node(&s, m).kind else { panic!("not a mesh") };
|
||||
let q = g.points[4].p;
|
||||
s.select_tool("mesh", v).unwrap();
|
||||
for kind in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(kind, q.x, q.y), v).unwrap();
|
||||
}
|
||||
s.execute("paint.setFill", &json!({"opacity": 30})).unwrap();
|
||||
let NodeKind::Mesh(g) = &node(&s, m).kind else { panic!("not a mesh") };
|
||||
let o: Vec<f32> = g.points.iter().map(|p| p.opacity).collect();
|
||||
assert!((o[4] - 0.3).abs() < 1e-6, "{o:?}");
|
||||
assert!(o.iter().enumerate().all(|(i, a)| i == 4 || *a == 1.0), "{o:?}");
|
||||
assert_eq!(proxies(&mut s)["fillOpacity"], 30.0);
|
||||
}
|
||||
Reference in new issue
Block a user