mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 16:37:32 +00:00
Merge pull request #8459 from dotCooCoo/feat/profile-default-fill-stroke
New Document: a profile's redefined Default Graphic Style sets the starting fill and stroke
This commit is contained in:
@@ -7,12 +7,16 @@
|
||||
//! panel's New Art Has Basic Appearance off, the whole appearance of the last selection. With the
|
||||
//! option on (the default) an inherited appearance gives new art its top stroke's options only,
|
||||
//! and a change of selection loads only the proxies. `paint.default` resets it.
|
||||
//!
|
||||
//! A document whose Default Graphic Style no longer has the shipped look (a New Document profile
|
||||
//! redefined it) gives the reset that style's fills, strokes and effects instead, and `file.new`
|
||||
//! resets new art when it opens such a document.
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::{BlendMode, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, FillLayer, GraphicStyle, Node, NodeKind, StrokeLayer};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, ColorMode, DEFAULT_GRAPHIC_STYLE, Document, FillLayer, GraphicStyle, Node, NodeKind, StrokeLayer};
|
||||
use vectorcraft_tools::PaintDefaults;
|
||||
|
||||
use super::gradient::unplaced;
|
||||
@@ -263,7 +267,35 @@ fn describe(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// The defaults for new art reset (`paint.default`): white fill, 1 pt black stroke, no template.
|
||||
/// The defaults for new art reset (`paint.default`): white fill, 1 pt black stroke, no template;
|
||||
/// in a document whose Default Graphic Style was redefined, that style's fills, strokes and effects
|
||||
/// at full opacity, not linked to the style.
|
||||
pub(crate) fn reset(s: &mut Session) {
|
||||
s.paint = PaintDefaults::default();
|
||||
let style = s.doc().ok().and_then(|st| redefined_default_style(&st.doc)).cloned();
|
||||
if let Some(g) = style {
|
||||
let ap = if g.unit_box { g.appearance } else { unplace_all(g.appearance) };
|
||||
s.take_new_art(ap, 1.0, BlendMode::Normal, None, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// `d`'s Default Graphic Style when its fills, strokes, effects or transparency differ from the
|
||||
/// shipped style's (white fill, 1 pt black stroke), the shipped colors converted to `d`'s color mode
|
||||
/// as a New Document profile's are.
|
||||
pub(crate) fn redefined_default_style(d: &Document) -> Option<&GraphicStyle> {
|
||||
let g = d.graphic_styles.iter().find(|g| g.name == DEFAULT_GRAPHIC_STYLE)?;
|
||||
let same =
|
||||
|o: &GraphicStyle| (&o.appearance, o.opacity, o.blend, o.isolate, o.knockout) == (&g.appearance, g.opacity, g.blend, g.isolate, g.knockout);
|
||||
if same(&GraphicStyle::new(DEFAULT_GRAPHIC_STYLE, Appearance::default_art())) {
|
||||
return None;
|
||||
}
|
||||
if d.color_mode != ColorMode::Rgb {
|
||||
let mut shipped = Document::new_with_mode(1.0, 1.0, ColorMode::Rgb);
|
||||
shipped.graphic_styles.retain(|o| o.name == DEFAULT_GRAPHIC_STYLE);
|
||||
super::colormgmt::set_color_mode(&mut shipped, d.color_mode, true, None, super::colormgmt::Grays::Profile);
|
||||
if shipped.graphic_styles.first().is_some_and(same) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(g)
|
||||
}
|
||||
|
||||
@@ -814,6 +814,10 @@ pub(crate) fn file_new(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// The profile's links keep their relative paths, which name places from the profile's file, as
|
||||
// a new document from a history state keeps its source's.
|
||||
s.doc_mut()?.links_from = links_from;
|
||||
// A profile whose Default Graphic Style was redefined gives new art in the document that style.
|
||||
if super::newart::redefined_default_style(&s.doc()?.doc).is_some() {
|
||||
super::newart::reset(s);
|
||||
}
|
||||
// Overprint Preview is the engine's (the UI turns Pixel Preview on from `previewMode`). Both
|
||||
// are views of the whole app, so Default leaves them as they are.
|
||||
if settings.preview_mode == PreviewMode::Overprint {
|
||||
|
||||
@@ -48,7 +48,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Default Fill and Stroke",
|
||||
[],
|
||||
Some("D"),
|
||||
"{ids?|id?} white fill and 1 pt black stroke for the selection and the defaults; type gets black fill and no stroke. On a path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke, and on a graph label the black fill and no stroke of its characters for every label of its kind, and on a graph's axis line, tick mark or radar ring its rows for every line of its kind (exceptions: docs/mcp.md)",
|
||||
"{ids?|id?} white fill and 1 pt black stroke for the selection and the defaults (in a document whose Default Graphic Style differs from the shipped one, as a New Document profile can redefine it: that style's fills, strokes and effects, without its opacity or a link to it); type gets black fill and no stroke. On a path directly in a graph series, the graph also stores the paint, weight, opacity and blend mode of the topmost visible fill and stroke, and on a graph label the black fill and no stroke of its characters for every label of its kind, and on a graph's axis line, tick mark or radar ring its rows for every line of its kind (exceptions: docs/mcp.md)",
|
||||
has_doc,
|
||||
default_paint
|
||||
),
|
||||
@@ -1035,6 +1035,8 @@ fn default_paint(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if !ids.is_empty() {
|
||||
// When every object comes out as it was, the document and its undo history stay as they are.
|
||||
let before = s.doc()?.doc.clone();
|
||||
// A redefined Default Graphic Style gives other art its fills, strokes and effects, placed on each object.
|
||||
let style = super::newart::redefined_default_style(&before).cloned();
|
||||
super::overprint::edit_counted(s, "Default Fill and Stroke", |d| {
|
||||
for id in &ids {
|
||||
let Some(n) = d.node_mut(*id) else { continue };
|
||||
@@ -1045,7 +1047,12 @@ fn default_paint(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
r.style.stroke = Paint::None;
|
||||
}
|
||||
}
|
||||
_ => n.appearance = Appearance::default_art().into(),
|
||||
_ => {
|
||||
n.appearance = match &style {
|
||||
Some(g) => g.appearance_on(n).into_owned().into(),
|
||||
None => Appearance::default_art().into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let kept = super::appearance::capture_series_writes(&before, d, &ids, (true, true), (true, true), false, false);
|
||||
|
||||
@@ -637,6 +637,69 @@ fn an_edited_profile_starts_the_documents_of_its_category() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The fill, stroke and weight of `id` in the active document.
|
||||
fn look(s: &Session, id: u64) -> (vectorcraft_color::Paint, vectorcraft_color::Paint, Option<f64>) {
|
||||
let n = s.doc().unwrap().doc.node(vectorcraft_doc::NodeId(id)).cloned().unwrap();
|
||||
(n.appearance.fill_paint(), n.appearance.stroke_paint(), n.appearance.stroke().map(|st| st.width))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_redefined_default_graphic_style_in_a_profile_is_the_default_fill_and_stroke() {
|
||||
use vectorcraft_color::Paint;
|
||||
let (mut s, dir) = with_profiles("default-style");
|
||||
// The Print profile's Default Graphic Style becomes no fill and a 0.25 pt red stroke.
|
||||
s.execute("file.newProfiles.edit", &json!({"name": "Print"})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 40, "height": 40})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"none": true})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"color": "#ff0000"})).unwrap();
|
||||
s.execute("stroke.set", &json!({"weight": 0.25})).unwrap();
|
||||
s.execute("graphicStyle.redefine", &json!({"name": "Default Graphic Style"})).unwrap();
|
||||
s.execute("document.save", &json!({})).unwrap();
|
||||
let style = s.doc().unwrap().doc.graphic_styles[0].clone();
|
||||
let red = style.appearance.stroke_paint();
|
||||
assert!(matches!(red, Paint::Solid { .. }), "{red:?}");
|
||||
assert_eq!(style.appearance.stroke().map(|st| st.width), Some(0.25), "{style:?}");
|
||||
// The Mobile profile is saved as shipped.
|
||||
s.execute("file.newProfiles.edit", &json!({"name": "Mobile"})).unwrap();
|
||||
s.execute("document.save", &json!({})).unwrap();
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"color": "#00ff00"})).unwrap();
|
||||
let green = s.paint.fill.clone();
|
||||
|
||||
// A document from the Print profile starts with the style's fill, stroke and weight, and its art takes them.
|
||||
s.execute("file.new", &json!({"profile": "Print", "colorMode": "cmyk"})).unwrap();
|
||||
assert_eq!((s.paint.fill.clone(), s.paint.stroke.clone(), s.paint.stroke_width), (Paint::None, red.clone(), 0.25));
|
||||
let id = s.execute("shape.rectangle", &json!({"x": 20, "y": 20, "width": 60, "height": 60})).unwrap()["id"].as_u64().unwrap();
|
||||
assert_eq!(look(&s, id), (Paint::None, red.clone(), Some(0.25)));
|
||||
// Default Fill and Stroke gives the selection and the defaults that look again, as one undo step.
|
||||
s.execute("paint.setFill", &json!({"color": "#0000ff"})).unwrap();
|
||||
s.execute("stroke.set", &json!({"weight": 3})).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
s.execute("paint.default", &json!({})).unwrap();
|
||||
assert_eq!(look(&s, id), (Paint::None, red.clone(), Some(0.25)));
|
||||
assert_eq!((s.paint.fill.clone(), s.paint.stroke_width), (Paint::None, 0.25));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1);
|
||||
// Nothing to change: no undo step.
|
||||
let step = s.doc().unwrap().history.step();
|
||||
s.execute("paint.default", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.step(), step);
|
||||
// The new art is not linked to the style.
|
||||
assert!(s.doc().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().extras().graphic_style.is_none());
|
||||
|
||||
// A profile with the shipped style, here converted to CMYK, leaves the fill and stroke the user chose.
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"color": "#00ff00", "keepModel": true})).unwrap();
|
||||
s.execute("file.new", &json!({"profile": "Mobile", "colorMode": "cmyk"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.color_mode, ColorMode::Cmyk);
|
||||
assert_eq!(s.paint.fill, green);
|
||||
// So does a document without a profile, and Default Fill and Stroke there gives white and 1 pt black.
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
assert_eq!(s.paint.fill, green);
|
||||
s.execute("paint.default", &json!({})).unwrap();
|
||||
assert_eq!(s.paint, vectorcraft_tools::PaintDefaults::default());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_profile_follows_the_preset_access_and_reset() {
|
||||
let (mut s, dir) = with_profiles("choice");
|
||||
|
||||
Reference in New Issue
Block a user