GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! Graphic style libraries: named, read-only sets of graphic styles (Window → Graphic Style
//! Libraries). The built-in ones are generated here in code ([`STYLE_LIBRARIES`]); user libraries
//! are `.vcstyles` files ([`write`], [`read`]): JSON holding the styles and the pattern
//! definitions they paint with.
//!
//! Library styles stand alone: no id, and no links to the swatches of the document they came from
//! ([`StyleLibrary::from_document`]).

use std::sync::{Arc, OnceLock};

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use vectorcraft_color::{BlendMode, Color, Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop, Paint};
use vectorcraft_geom::Point;

use crate::{
    Appearance, AppearanceItem, Arrowhead, Dash, Document, Effect, FillLayer, GraphicStyle, Knockout, LineCap, LineJoin, PatternDef, StrokeAlign,
    StrokeLayer, WidthProfile,
};

/// A graphic style library: its name, its styles and the patterns they paint with.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct StyleLibrary {
    pub name: String,
    #[serde(default)]
    pub styles: Vec<GraphicStyle>,
    /// The pattern swatches the styles' fills and strokes paint with.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub patterns: Vec<PatternDef>,
}

impl StyleLibrary {
    pub fn len(&self) -> usize {
        self.styles.len()
    }
    pub fn is_empty(&self) -> bool {
        self.styles.is_empty()
    }
    pub fn style(&self, name: &str) -> Option<&GraphicStyle> {
        self.styles.iter().find(|g| g.name == name)
    }

    /// The document's styles `names` (empty: all of them, in panel order) as a library named
    /// `name`, standing alone ([`GraphicStyle::standalone`]) with the patterns they paint with.
    pub fn from_document(d: &Document, names: &[String], name: String) -> Result<Self, String> {
        if let Some(n) = names.iter().find(|n| d.graphic_style(n).is_none()) {
            return Err(format!("no graphic style `{n}`"));
        }
        let styles: Vec<GraphicStyle> =
            d.graphic_styles.iter().filter(|g| names.is_empty() || names.contains(&g.name)).map(GraphicStyle::standalone).collect();
        let mut patterns: Vec<PatternDef> = vec![];
        for p in styles.iter().flat_map(GraphicStyle::patterns) {
            if !patterns.iter().any(|x| x.name == p)
                && let Some(def) = d.pattern(p)
            {
                patterns.push(def.clone());
            }
        }
        Ok(Self { name, styles, patterns })
    }
}

impl GraphicStyle {
    /// Do both styles look the same (appearance and transparency; names and ids aside)?
    pub fn same_look(&self, other: &GraphicStyle) -> bool {
        (&self.appearance, self.opacity, self.blend, self.isolate, self.knockout, self.unit_box)
            == (&other.appearance, other.opacity, other.blend, other.isolate, other.knockout, other.unit_box)
    }

    /// The style as a library holds it: no id, and its paints unlinked from swatches.
    pub fn standalone(&self) -> GraphicStyle {
        let mut g = GraphicStyle { id: 0, ..self.clone() };
        for it in &mut g.appearance.items {
            match it.paint_mut() {
                // The colour shown stays, unlinked.
                Paint::Solid { swatch, tint, .. } => (*swatch, *tint) = (None, 1.0),
                Paint::Gradient(gp) => {
                    gp.swatch = None;
                    gp.gradient.stops.iter_mut().for_each(|st| st.set_color(st.color, None));
                }
                Paint::None | Paint::Pattern { .. } => {}
            }
        }
        g
    }

    /// The names of the patterns the style's fills and strokes paint with.
    pub fn patterns(&self) -> impl Iterator<Item = &str> {
        self.appearance.items.iter().filter_map(|it| match it.paint() {
            Paint::Pattern { pattern, .. } => Some(pattern.as_str()),
            _ => None,
        })
    }
}

// ---------- .vcstyles files ----------

/// The extension (and `format` header) of graphic style library files.
pub const STYLES_EXT: &str = "vcstyles";

/// The version of the `.vcstyles` files [`write`] writes. Version 2: a positive Twist angle turns
/// clockwise ([`crate::twist`]).
const STYLES_VERSION: u32 = crate::twist::TWIST_CLOCKWISE_SINCE;

/// The file: a header around the library.
#[derive(Serialize, Deserialize)]
struct NativeFile {
    format: String,
    version: u32,
    #[serde(flatten)]
    library: StyleLibrary,
}

/// `lib` as a `.vcstyles` file.
pub fn write(lib: &StyleLibrary) -> String {
    let file = NativeFile { format: STYLES_EXT.into(), version: STYLES_VERSION, library: lib.clone() };
    serde_json::to_string_pretty(&file).unwrap_or_default()
}

/// Read a `.vcstyles` file; an unnamed library is called `name`. A version 1 file was written by
/// an app that turned Twist counterclockwise: its Twist angles are negated.
pub fn read(text: &str, name: &str) -> Result<StyleLibrary, String> {
    let f: NativeFile = serde_json::from_str(text.trim_start_matches('\u{feff}')).map_err(|e| format!("not a graphic style library: {e}"))?;
    if f.format != STYLES_EXT {
        return Err(format!("not a graphic style library (format `{}`)", f.format));
    }
    let mut lib = f.library;
    if f.version < STYLES_VERSION {
        crate::twist::negate_library(&mut lib);
    }
    if lib.name.trim().is_empty() {
        lib.name = name.into();
    }
    Ok(lib)
}

/// Does `text` look like a `.vcstyles` file (its header names the format)?
pub fn sniff(text: &str) -> bool {
    let t = text.trim_start_matches('\u{feff}').trim_start();
    let head = t.char_indices().nth(256).map_or(t, |(i, _)| &t[..i]);
    t.starts_with('{') && head.contains(&format!("\"{STYLES_EXT}\""))
}

// ---------- built-in libraries ----------

/// A built-in library: a stable id, its menu name and the function that makes its styles.
pub struct BuiltinStyleLibrary {
    pub id: &'static str,
    pub name: &'static str,
    make: fn() -> Vec<GraphicStyle>,
}

/// The built-in graphic style libraries, in menu order.
pub const STYLE_LIBRARIES: &[BuiltinStyleLibrary] = &[
    BuiltinStyleLibrary { id: "shadows-glows", name: "Shadows and Glows", make: shadows_glows },
    BuiltinStyleLibrary { id: "outlines-rules", name: "Outlines and Rules", make: outlines_rules },
    BuiltinStyleLibrary { id: "hand-drawn", name: "Hand-Drawn", make: hand_drawn },
    BuiltinStyleLibrary { id: "textures", name: "Textures", make: textures },
    BuiltinStyleLibrary { id: "painted-sketched", name: "Painted and Sketched", make: painted_sketched },
    BuiltinStyleLibrary { id: "paper-cutouts", name: "Paper Cutouts", make: paper_cutouts },
    BuiltinStyleLibrary { id: "gradient-finishes", name: "Gradient Finishes", make: gradient_finishes },
    BuiltinStyleLibrary { id: "shape-effects", name: "Shape Effects", make: shape_effects },
    BuiltinStyleLibrary { id: "blends-transparency", name: "Blends and Transparency", make: blends_transparency },
    BuiltinStyleLibrary { id: "game-hud", name: "Game HUD", make: game_hud },
    BuiltinStyleLibrary { id: "status-effects", name: "Status Effects", make: status_effects },
    BuiltinStyleLibrary { id: "neon-glow", name: "Neon & Glow", make: neon_glow },
];

/// Built-in library `id`, made once.
pub fn builtin_style_library(id: &str) -> Option<Arc<StyleLibrary>> {
    static LIBS: OnceLock<Vec<Arc<StyleLibrary>>> = OnceLock::new();
    let libs = LIBS.get_or_init(|| {
        STYLE_LIBRARIES.iter().map(|b| Arc::new(StyleLibrary { name: b.name.into(), styles: (b.make)(), patterns: vec![] })).collect()
    });
    STYLE_LIBRARIES.iter().position(|b| b.id == id).map(|i| libs[i].clone())
}

fn hex(h: &str) -> Color {
    Color::from_hex(h).unwrap_or(Color::BLACK)
}

fn solid(h: &str) -> Paint {
    Paint::solid(hex(h))
}

fn fill(h: &str) -> FillLayer {
    FillLayer::new(solid(h))
}

fn stroke(h: &str, width: f64) -> StrokeLayer {
    StrokeLayer::new(solid(h), width)
}

/// A stroke with round caps and joins.
fn round(h: &str, width: f64) -> StrokeLayer {
    StrokeLayer { cap: LineCap::Round, join: LineJoin::Round, ..stroke(h, width) }
}

/// Live effect `id` with `params` (the others keep the effect's defaults). A Warp draws with its
/// style's Bézier net, as a new Warp does.
fn fx(id: &str, mut params: Value) -> Effect {
    if id.starts_with("warp.")
        && let Value::Object(m) = &mut params
    {
        m.entry("version").or_insert(json!(crate::live::WARP_NET));
    }
    Effect { id: id.into(), params, visible: true }
}

/// A gradient of `(offset, colour, opacity)` stops.
fn gradient(kind: GradientKind, stops: &[(f32, &str, f32)]) -> Gradient {
    Gradient::new(kind, stops.iter().map(|&(offset, c, opacity)| GradientStop { opacity, ..GradientStop::new(offset, hex(c)) }).collect())
}

/// A linear gradient fitted to each object's box at `angle` degrees (90: upwards).
fn linear(angle: f64, stops: &[(f32, &str, f32)]) -> Paint {
    Paint::Gradient(Box::new(GradientPaint { angle, ..GradientPaint::new(gradient(GradientKind::Linear, stops)) }))
}

/// A radial gradient placed in the unit box (each object gets it at the same place relative to its
/// bounds): centred at `(x, y)` with `radius`.
fn radial_at(x: f64, y: f64, radius: f64, stops: &[(f32, &str, f32)]) -> Paint {
    let geom = GradientGeom { start: Point::new(x, y), end: Point::new(x + radius, y), aspect: 1.0, focal: None };
    Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..GradientPaint::new(gradient(GradientKind::Radial, stops)) }))
}

/// A style of fills and strokes `items` (painted first to last) and object `effects`.
fn style(name: &str, items: Vec<AppearanceItem>, effects: Vec<Effect>) -> GraphicStyle {
    GraphicStyle::new(name, Appearance { items, effects, contents_index: None })
}

use AppearanceItem::{Fill as F, Stroke as S};

fn shadows_glows() -> Vec<GraphicStyle> {
    let shadow = |opacity: f64, x: f64, y: f64, blur: f64, color: &str| {
        fx("stylize.dropShadow", json!({"mode": "multiply", "opacity": opacity, "x": x, "y": y, "blur": blur, "color": color}))
    };
    vec![
        style("Soft Shadow", vec![F(fill("#f4f1ea"))], vec![shadow(45.0, 4.0, 4.0, 6.0, "#1d1b2a")]),
        style(
            "Hard Shadow",
            vec![F(fill("#ffd166")), S(stroke("#1d1b2a", 2.0))],
            vec![fx("stylize.dropShadow", json!({"mode": "normal", "opacity": 100.0, "x": 5.0, "y": 5.0, "blur": 0.0, "color": "#1d1b2a"}))],
        ),
        style("Lifted Card", vec![F(fill("#ffffff")), S(stroke("#d9d9e3", 0.5))], vec![shadow(30.0, 0.0, 8.0, 12.0, "#2b2d42")]),
        style(
            "Halo",
            vec![F(fill("#2b2d42"))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 90.0, "blur": 8.0, "color": "#8ecae6"}))],
        ),
        style(
            "Neon Tube",
            vec![S(round("#ff4fd8", 4.0)), S(round("#ffe8fb", 1.5))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 85.0, "blur": 7.0, "color": "#ff4fd8"}))],
        ),
        style(
            "Inner Light",
            vec![F(fill("#3a86ff"))],
            vec![fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 70.0, "blur": 9.0, "color": "#ffffff", "source": "center"}))],
        ),
        style(
            "Ember",
            vec![F(fill("#e85d04"))],
            vec![
                fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 80.0, "blur": 6.0, "color": "#ffba08", "source": "edge"})),
                fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 70.0, "blur": 5.0, "color": "#dc2f02"})),
            ],
        ),
        style("Feathered", vec![F(fill("#8338ec"))], vec![fx("stylize.feather", json!({"radius": 6.0}))]),
        style("Haze", vec![F(FillLayer { opacity: 0.8, ..fill("#06d6a0") })], vec![fx("blur.gaussian", json!({"radius": 3.0}))]),
    ]
}

fn outlines_rules() -> Vec<GraphicStyle> {
    let dashed =
        |h: &str, width: f64, pattern: Vec<f64>| StrokeLayer { dash: Some(Dash { pattern, offset: 0.0, align_corners: true }), ..round(h, width) };
    let keyline = |h: &str, width: f64, offset: f64| StrokeLayer {
        effects: vec![fx("path.offsetPath", json!({"offset": offset, "joins": "round"}))],
        ..stroke(h, width)
    };
    vec![
        style("Double Rule", vec![F(fill("#ffffff")), S(stroke("#14213d", 6.0)), S(stroke("#ffffff", 3.0))], vec![]),
        style("Triple Rule", vec![F(fill("#fefae0")), S(stroke("#283618", 10.0)), S(stroke("#fefae0", 6.0)), S(stroke("#283618", 2.0))], vec![]),
        style("Dashed Border", vec![F(fill("#e9f5f2")), S(dashed("#264653", 2.0, vec![6.0, 4.0]))], vec![]),
        style("Dotted Border", vec![F(fill("#fff4e6")), S(dashed("#e76f51", 3.0, vec![0.0, 6.0]))], vec![]),
        style("Offset Keyline", vec![F(fill("#e9c46a")), S(keyline("#264653", 1.5, 4.0))], vec![]),
        style("Inset Keyline", vec![F(fill("#2a9d8f")), S(keyline("#ffffff", 1.5, -4.0))], vec![]),
        style("Outside Stroke", vec![F(fill("#f4a261")), S(StrokeLayer { align: StrokeAlign::Outside, ..stroke("#264653", 4.0) })], vec![]),
        style("Tapered Ink", vec![S(StrokeLayer { profile: Some(WidthProfile::lens()), ..round("#1b1b1b", 5.0) })], vec![]),
        style("Pointer", vec![S(StrokeLayer { end_arrow: Some(Arrowhead::Triangle), ..round("#e76f51", 2.0) })], vec![]),
        style("Highlighter", vec![S(StrokeLayer { opacity: 0.6, blend: BlendMode::Multiply, ..round("#ffd60a", 10.0) })], vec![]),
    ]
}

fn hand_drawn() -> Vec<GraphicStyle> {
    let rough = |h: &str, width: f64, seed: u32| StrokeLayer {
        effects: vec![fx("distort.roughen", json!({"size": 1.5, "relative": true, "detail": 8.0, "points": "smooth", "seed": seed}))],
        ..round(h, width)
    };
    let scribble = |h: &str, angle: f64, opacity: f32| FillLayer {
        opacity,
        effects: vec![fx("stylize.scribble", json!({"angle": angle, "strokeWidth": 1.5, "spacing": 4.0, "curviness": 5.0, "variation": 0.5}))],
        ..fill(h)
    };
    vec![
        style("Sketchy Line", vec![F(fill("#fefae0")), S(rough("#1d3557", 1.25, 1)), S(rough("#1d3557", 0.75, 7))], vec![]),
        style("Scribble Fill", vec![F(scribble("#e63946", 30.0, 1.0)), S(rough("#1d3557", 1.25, 3))], vec![]),
        style("Crosshatch", vec![F(scribble("#457b9d", 45.0, 0.8)), F(scribble("#457b9d", -45.0, 0.8)), S(rough("#1d3557", 1.0, 5))], vec![]),
        style(
            "Wobble",
            vec![F(fill("#a8dadc")), S(round("#1d3557", 2.0))],
            vec![fx("distort.tweak", json!({"h": 3.0, "v": 3.0, "relative": true, "seed": 5}))],
        ),
        style(
            "Zig Zag Edge",
            vec![
                F(fill("#f1faee")),
                S(StrokeLayer {
                    effects: vec![fx("distort.zigZag", json!({"size": 3.0, "relative": false, "ridges": 6, "points": "corner"}))],
                    ..stroke("#e63946", 1.5)
                }),
            ],
            vec![],
        ),
        style(
            "Wavy Edge",
            vec![F(fill("#bde0fe")), S(round("#3a5a9b", 1.5))],
            vec![fx("distort.zigZag", json!({"size": 3.0, "relative": false, "ridges": 5, "points": "smooth"}))],
        ),
        style("Puffy", vec![F(fill("#ffc8dd")), S(round("#c9184a", 1.5))], vec![fx("distort.puckerBloat", json!({"amount": 25.0}))]),
        style("Starburst", vec![F(fill("#ffbe0b")), S(stroke("#fb5607", 1.0))], vec![fx("distort.puckerBloat", json!({"amount": -40.0}))]),
    ]
}

/// Painted and Sketched: one catalog paint or sketch effect over the whole appearance, so it
/// repaints the fill and the stroke together (watercolor, dry brush, pastels, charcoal and ink).
fn painted_sketched() -> Vec<GraphicStyle> {
    vec![
        style(
            "Washed Blue",
            vec![F(fill("#8ab9c9"))],
            vec![fx("artistic.watercolor", json!({"brushDetail": 8.0, "shadowIntensity": 2.0, "texture": 2.0}))],
        ),
        style(
            "Bristle Ochre",
            vec![F(fill("#c4975a")), S(stroke("#76533d", 1.0))],
            vec![fx("artistic.dryBrush", json!({"brushSize": 4.0, "brushDetail": 7.0, "texture": 2.0}))],
        ),
        style(
            "Pastel Heather",
            vec![F(fill("#a78ca5"))],
            vec![fx(
                "artistic.roughPastels",
                json!({
                    "strokeLength": 8.0,
                    "strokeDetail": 9.0,
                    "texture": "canvas",
                    "scaling": 100.0,
                    "relief": 5.0,
                    "lightDirection": "topLeft",
                    "invert": false
                }),
            )],
        ),
        style(
            "Charcoal Shade",
            vec![F(fill("#666666"))],
            vec![fx("sketch.charcoal", json!({"thickness": 3.0, "detail": 3.0, "lightDarkBalance": 55.0}))],
        ),
        style(
            "Ink Contour",
            vec![F(fill("#d9e2df")), S(stroke("#26323b", 1.0))],
            vec![fx("brushStrokes.inkOutlines", json!({"strokeLength": 6.0, "darkIntensity": 24.0, "lightIntensity": 8.0}))],
        ),
        style(
            "Wet Ink",
            vec![F(fill("#637b85"))],
            vec![fx("brushStrokes.sumiE", json!({"strokeWidth": 7.0, "strokePressure": 5.0, "contrast": 20.0}))],
        ),
        style(
            "Fine Pen",
            vec![F(fill("#d8d4c6"))],
            vec![fx("sketch.graphicPen", json!({"strokeLength": 6.0, "lightDarkBalance": 55.0, "strokeDirection": "rightDiagonal"}))],
        ),
    ]
}

/// Paper Cutouts: original layered paper colors with visible cut edges and crisp cast shadows.
fn paper_cutouts() -> Vec<GraphicStyle> {
    let cutout = |name: &str, face: &str, edge: &str, rim: &str, shadow: &str, x: f64, y: f64| {
        style(
            name,
            vec![F(fill(face)), S(stroke(edge, 7.0)), S(stroke(rim, 2.0))],
            vec![fx("stylize.dropShadow", json!({"mode": "normal", "opacity": 100.0, "x": x, "y": y, "blur": 0.0, "color": shadow}))],
        )
    };
    vec![
        cutout("Coral Layers", "#ff6b6b", "#f9c74f", "#fff4e6", "#6d597a", 5.0, 5.0),
        cutout("Mint Layers", "#80cbc4", "#2a9d8f", "#e9f5db", "#355070", 4.0, 6.0),
        cutout("Midnight Layers", "#3d5a80", "#ee6c4d", "#e0fbfc", "#1b263b", 6.0, 4.0),
        cutout("Sunflower Sheet", "#ffbe0b", "#fb5607", "#fff3b0", "#6a4c93", 3.0, 6.0),
        cutout("Rose Layers", "#f28482", "#84a59d", "#f7ede2", "#4a4e69", 6.0, 3.0),
        cutout("Monochrome Stack", "#f5f5f5", "#444444", "#ffffff", "#222222", 5.0, 5.0),
    ]
}

/// Textures: fills and strokes painted with the catalog's texture effects (grain, canvas and
/// burlap surfaces, craquelure, tiles, patchwork, stained glass and water paper).
fn textures() -> Vec<GraphicStyle> {
    let surface = |texture: &str, scaling: f64, relief: f64, light: &str| {
        fx("texture.texturizer", json!({"texture": texture, "scaling": scaling, "relief": relief, "lightDirection": light, "invert": false}))
    };
    vec![
        style(
            "Fine Grain",
            vec![F(fill("#c9ada7")), S(stroke("#4a4e69", 1.0))],
            vec![fx("texture.grain", json!({"intensity": 35.0, "contrast": 55.0, "grainType": "regular"}))],
        ),
        style("Canvas Weave", vec![F(fill("#e9d8a6"))], vec![surface("canvas", 100.0, 6.0, "topLeft")]),
        style("Burlap Sack", vec![F(fill("#bc8a5f")), S(stroke("#6f4518", 1.5))], vec![surface("burlap", 120.0, 8.0, "top")]),
        style(
            "Cracked Glaze",
            vec![F(fill("#94d2bd")), S(stroke("#005f73", 1.0))],
            vec![fx("texture.craquelure", json!({"crackSpacing": 12.0, "crackDepth": 5.0, "crackBrightness": 8.0}))],
        ),
        style(
            "Mosaic Floor",
            vec![F(fill("#0a9396"))],
            vec![fx("texture.mosaicTiles", json!({"tileSize": 10.0, "groutWidth": 3.0, "lightenGrout": 8.0}))],
        ),
        style("Quilted Squares", vec![F(fill("#ee9b00"))], vec![fx("texture.patchwork", json!({"squareSize": 5.0, "relief": 10.0}))]),
        style(
            "Leaded Glass",
            vec![F(FillLayer::new(linear(-45.0, &[(0.0, "#ae2012", 1.0), (0.5, "#ee9b00", 1.0), (1.0, "#005f73", 1.0)])))],
            vec![fx("texture.stainedGlass", json!({"cellSize": 8.0, "borderThickness": 4.0, "lightIntensity": 3.0}))],
        ),
        style(
            "Damp Paper",
            vec![F(fill("#4895ef")), S(stroke("#3f37c9", 1.0))],
            vec![fx("sketch.waterPaper", json!({"fiberLength": 12.0, "brightness": 55.0, "contrast": 60.0}))],
        ),
    ]
}

fn gradient_finishes() -> Vec<GraphicStyle> {
    let rim = |h: &str| S(stroke(h, 1.0));
    vec![
        style(
            "Glossy",
            vec![
                F(FillLayer::new(linear(-90.0, &[(0.0, "#7ab8ff", 1.0), (1.0, "#1d4ed8", 1.0)]))),
                F(FillLayer::new(linear(-90.0, &[(0.0, "#ffffff", 0.7), (0.5, "#ffffff", 0.0)]))),
                rim("#1e3a8a"),
            ],
            vec![],
        ),
        style(
            "Brushed Metal",
            vec![
                F(FillLayer::new(linear(
                    0.0,
                    &[(0.0, "#bfbfbf", 1.0), (0.3, "#f5f5f5", 1.0), (0.55, "#a6a6a6", 1.0), (0.8, "#e6e6e6", 1.0), (1.0, "#8c8c8c", 1.0)],
                ))),
                rim("#595959"),
            ],
            vec![],
        ),
        style(
            "Spotlight",
            vec![F(FillLayer::new(radial_at(0.35, 0.3, 0.9, &[(0.0, "#ffffff", 1.0), (0.35, "#ffbe0b", 1.0), (1.0, "#fb5607", 1.0)])))],
            vec![],
        ),
        style(
            "Sunset Band",
            vec![F(FillLayer::new(linear(-90.0, &[(0.0, "#ffbe0b", 1.0), (0.35, "#fb5607", 1.0), (0.7, "#ff006e", 1.0), (1.0, "#8338ec", 1.0)])))],
            vec![],
        ),
        style(
            "Sea Glass",
            vec![
                F(FillLayer {
                    opacity: 0.85,
                    ..FillLayer::new(linear(-45.0, &[(0.0, "#caf0f8", 1.0), (0.5, "#48cae4", 1.0), (1.0, "#0077b6", 1.0)]))
                }),
                rim("#03045e"),
            ],
            vec![],
        ),
        style(
            "Pearl",
            vec![
                F(FillLayer::new(radial_at(0.4, 0.35, 0.8, &[(0.0, "#ffffff", 1.0), (0.5, "#e8e1f0", 1.0), (1.0, "#b8b0c8", 1.0)]))),
                S(stroke("#8a7fa0", 0.75)),
            ],
            vec![],
        ),
        style(
            "Chrome Rim",
            vec![F(fill("#22223b")), S(StrokeLayer::new(linear(-90.0, &[(0.0, "#f8f9fa", 1.0), (0.5, "#6c757d", 1.0), (1.0, "#dee2e6", 1.0)]), 4.0))],
            vec![],
        ),
        style(
            "Glow Orb",
            vec![F(FillLayer::new(radial_at(0.5, 0.5, 0.5, &[(0.0, "#ffffff", 1.0), (0.4, "#80ffdb", 1.0), (1.0, "#5390d9", 1.0)])))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 60.0, "blur": 6.0, "color": "#80ffdb"}))],
        ),
    ]
}

fn shape_effects() -> Vec<GraphicStyle> {
    let basic = |f: &str, s: &str, w: f64| vec![F(fill(f)), S(round(s, w))];
    vec![
        style("Rounded", basic("#90be6d", "#43aa8b", 2.0), vec![fx("stylize.roundCorners", json!({"radius": 10.0}))]),
        style(
            "Pill Badge",
            vec![F(fill("#f94144"))],
            vec![fx("convertToShape.roundedRectangle", json!({"relative": true, "extraW": 12.0, "extraH": 6.0, "radius": 40.0}))],
        ),
        style(
            "Round Badge",
            vec![F(fill("#577590")), S(stroke("#ffffff", 2.0))],
            vec![fx("convertToShape.ellipse", json!({"relative": true, "extraW": 10.0, "extraH": 10.0}))],
        ),
        style("Arc", basic("#f8961e", "#f3722c", 1.5), vec![fx("warp.arc", json!({"bend": 30.0}))]),
        style("Flag Wave", basic("#277da1", "#1d4e89", 1.5), vec![fx("warp.flag", json!({"bend": 25.0}))]),
        style("Bulge", basic("#f9c74f", "#f8961e", 1.5), vec![fx("warp.bulge", json!({"bend": 30.0}))]),
        style("Twisted", basic("#4d908e", "#264653", 1.5), vec![fx("distort.twist", json!({"angle": 30.0}))]),
        style(
            "Echo",
            vec![F(FillLayer { opacity: 0.55, ..fill("#43aa8b") }), S(stroke("#264653", 1.0))],
            vec![fx("distort.transform", json!({"copies": 3, "moveH": 4.0, "moveV": 4.0}))],
        ),
        style(
            "Spiral Copies",
            vec![F(FillLayer { opacity: 0.35, ..fill("#f94144") }), S(stroke("#9d0208", 0.75))],
            vec![fx("distort.transform", json!({"copies": 5, "rotate": 12.0, "scaleH": 90.0, "scaleV": 90.0}))],
        ),
    ]
}

fn blends_transparency() -> Vec<GraphicStyle> {
    let with =
        |g: GraphicStyle, opacity: f32, blend: BlendMode, isolate: bool, knockout: Knockout| GraphicStyle { opacity, blend, isolate, knockout, ..g };
    let one = |name: &str, h: &str| style(name, vec![F(fill(h))], vec![]);
    // A fill blending with the fills under it, so the style shows on any backdrop.
    let over = |paint: Paint, blend: BlendMode| F(FillLayer { blend, ..FillLayer::new(paint) });
    vec![
        with(one("Multiply Tint", "#ffbe0b"), 1.0, BlendMode::Multiply, false, Knockout::Neutral),
        with(one("Darken Veil", "#4cc9f0"), 0.85, BlendMode::Darken, false, Knockout::Neutral),
        with(style("Ghosted", vec![F(fill("#7209b7")), S(stroke("#3a0ca3", 1.0))], vec![]), 0.4, BlendMode::Normal, false, Knockout::Neutral),
        with(one("Invert", "#ffffff"), 1.0, BlendMode::Difference, false, Knockout::Neutral),
        style("Two Tone", vec![F(fill("#4361ee")), F(FillLayer { opacity: 0.6, blend: BlendMode::Multiply, ..fill("#f72585") })], vec![]),
        style(
            "Overlay Glaze",
            vec![F(fill("#f72585")), over(linear(-90.0, &[(0.0, "#ffffff", 1.0), (1.0, "#000000", 1.0)]), BlendMode::Overlay)],
            vec![],
        ),
        style(
            "Screen Light",
            vec![F(fill("#3a0ca3")), over(radial_at(0.5, 0.4, 0.6, &[(0.0, "#4cc9f0", 1.0), (1.0, "#000000", 1.0)]), BlendMode::Screen)],
            vec![],
        ),
        with(
            style(
                "Soft Light Wash",
                vec![F(fill("#0096c7")), over(linear(0.0, &[(0.0, "#ffffff", 1.0), (1.0, "#03045e", 1.0)]), BlendMode::SoftLight)],
                vec![],
            ),
            1.0,
            BlendMode::Normal,
            true,
            Knockout::Neutral,
        ),
        with(style("Knockout Group", vec![F(fill("#4895ef")), S(stroke("#3f37c9", 1.0))], vec![]), 0.7, BlendMode::Normal, true, Knockout::On),
    ]
}

/// Game HUD: fills and glows for health, mana, damage and quest chrome (original arcade/HUD look).
fn game_hud() -> Vec<GraphicStyle> {
    vec![
        style("Health Fill", vec![F(fill("#e74c3c")), S(stroke("#5c1a1a", 1.25))], vec![]),
        style(
            "Mana Glow",
            vec![F(fill("#3498db"))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 85.0, "blur": 7.0, "color": "#5dade2"}))],
        ),
        style(
            "Damage Flash",
            vec![F(fill("#ff6b35"))],
            vec![fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 80.0, "blur": 8.0, "color": "#ffe66d", "source": "edge"}))],
        ),
        style("Quest Outline", vec![F(fill("#f7f3e8")), S(round("#2c3e50", 3.0))], vec![]),
        style(
            "Coin Shine",
            vec![F(fill("#f1c40f")), S(stroke("#b7950b", 1.5))],
            vec![fx("stylize.dropShadow", json!({"mode": "multiply", "opacity": 40.0, "x": 2.0, "y": 3.0, "blur": 4.0, "color": "#1d1b2a"}))],
        ),
        style(
            "Boss Alert",
            vec![F(fill("#1a1a2e")), S(round("#e94560", 2.5))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 75.0, "blur": 6.0, "color": "#e94560"}))],
        ),
    ]
}

/// Status Effects: buffs and debuffs for RPG/gamedev art (original, catalogue stylize effects only).
fn status_effects() -> Vec<GraphicStyle> {
    let stealth = GraphicStyle {
        opacity: 0.45,
        blend: BlendMode::Normal,
        ..style("Stealth Fade", vec![F(fill("#7f8c8d")), S(stroke("#2c3e50", 1.0))], vec![])
    };
    vec![
        style(
            "Poison Tint",
            vec![F(fill("#2ecc71"))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 80.0, "blur": 6.0, "color": "#58d68d"}))],
        ),
        style(
            "Freeze Edge",
            vec![F(fill("#aed6f1")), S(stroke("#5dade2", 2.0))],
            vec![fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 70.0, "blur": 5.0, "color": "#d6eaf8", "source": "edge"}))],
        ),
        style(
            "Burn Aura",
            vec![F(fill("#e67e22"))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 85.0, "blur": 8.0, "color": "#f39c12"}))],
        ),
        style(
            "Stun Pulse",
            vec![F(fill("#f4d03f"))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 75.0, "blur": 7.0, "color": "#f7dc6f"}))],
        ),
        stealth,
        style(
            "Shield Buff",
            vec![F(fill("#85c1e9")), S(round("#2874a6", 2.0))],
            vec![fx("stylize.outerGlow", json!({"mode": "normal", "opacity": 70.0, "blur": 5.0, "color": "#5dade2"}))],
        ),
    ]
}

/// Neon & Glow: original multi-layer procedural styles with neon tubes, laser beams, and glowing halos.
fn neon_glow() -> Vec<GraphicStyle> {
    vec![
        style(
            "Cyan Neon",
            vec![S(StrokeLayer { opacity: 0.7, ..round("#00e5ff", 6.0) }), S(round("#ffffff", 1.5))],
            vec![fx("stylize.dropShadow", json!({"mode": "screen", "opacity": 85.0, "x": 0.0, "y": 0.0, "blur": 8.0, "color": "#00e5ff"}))],
        ),
        GraphicStyle {
            blend: BlendMode::Screen,
            ..style(
                "Magenta Laser",
                vec![S(StrokeLayer { blend: BlendMode::Screen, ..round("#ff007f", 5.0) }), S(round("#ffffff", 1.5))],
                vec![fx("stylize.outerGlow", json!({"mode": "screen", "opacity": 90.0, "blur": 8.0, "color": "#ff007f"}))],
            )
        },
        style(
            "Amber Tube",
            vec![S(StrokeLayer { opacity: 0.8, ..round("#ff8c00", 6.0) }), S(round("#ffea00", 1.5))],
            vec![fx("stylize.outerGlow", json!({"mode": "screen", "opacity": 85.0, "blur": 8.0, "color": "#ff8c00"}))],
        ),
        style(
            "Electric Lime",
            vec![S(StrokeLayer { opacity: 0.75, ..round("#39ff14", 6.0) }), S(round("#ffffff", 1.5))],
            vec![fx("stylize.outerGlow", json!({"mode": "screen", "opacity": 90.0, "blur": 8.0, "color": "#39ff14"}))],
        ),
        style(
            "Pulse Violet",
            vec![S(StrokeLayer { opacity: 0.75, ..round("#7b2cbf", 5.0) }), S(round("#e0aaff", 1.5))],
            vec![
                fx("stylize.innerGlow", json!({"mode": "screen", "opacity": 75.0, "blur": 6.0, "color": "#c77dff", "source": "edge"})),
                fx("stylize.outerGlow", json!({"mode": "screen", "opacity": 85.0, "blur": 8.0, "color": "#7b2cbf"})),
            ],
        ),
        style("Cobalt Phosphor", vec![S(round("#0047ab", 6.0)), S(round("#ffffff", 1.5))], vec![fx("blur.gaussian", json!({"radius": 2.0}))]),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builtin_libraries_are_non_empty_with_unique_names() {
        let mut ids: Vec<&str> = STYLE_LIBRARIES.iter().map(|b| b.id).collect();
        ids.sort_unstable();
        ids.dedup();
        assert_eq!(ids.len(), STYLE_LIBRARIES.len(), "library ids are unique");
        for b in STYLE_LIBRARIES {
            let lib = builtin_style_library(b.id).unwrap();
            assert_eq!(lib.name, b.name);
            assert!(lib.len() >= 6, "{}", b.id);
            let mut names: Vec<&str> = lib.styles.iter().map(|g| g.name.as_str()).collect();
            names.sort_unstable();
            names.dedup();
            assert_eq!(names.len(), lib.len(), "style names in {} are unique", b.id);
            assert!(lib.styles.iter().all(|g| g.id == 0 && g.unit_box && !g.appearance.items.is_empty()));
        }
        assert!(Arc::ptr_eq(&builtin_style_library("hand-drawn").unwrap(), &builtin_style_library("hand-drawn").unwrap()), "made once");
        assert!(builtin_style_library("nope").is_none());
        let game = builtin_style_library("game-hud").unwrap();
        assert_eq!(game.name, "Game HUD");
        let names: Vec<&str> = game.styles.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(names, ["Health Fill", "Mana Glow", "Damage Flash", "Quest Outline", "Coin Shine", "Boss Alert"]);
        let status = builtin_style_library("status-effects").unwrap();
        assert_eq!(status.name, "Status Effects");
        let status_names: Vec<&str> = status.styles.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(status_names, ["Poison Tint", "Freeze Edge", "Burn Aura", "Stun Pulse", "Stealth Fade", "Shield Buff"]);
        let textures = builtin_style_library("textures").unwrap();
        assert_eq!(textures.name, "Textures");
        let texture_names: Vec<&str> = textures.styles.iter().map(|g| g.name.as_str()).collect();
        let expected =
            ["Fine Grain", "Canvas Weave", "Burlap Sack", "Cracked Glaze", "Mosaic Floor", "Quilted Squares", "Leaded Glass", "Damp Paper"];
        assert_eq!(texture_names, expected);
        for b in STYLE_LIBRARIES.iter().filter(|b| b.id != "textures") {
            let lib = builtin_style_library(b.id).unwrap();
            assert!(lib.styles.iter().all(|g| !expected.contains(&g.name.as_str())), "{} shares a name with Textures", b.id);
        }
        let ids: Vec<&str> = STYLE_LIBRARIES.iter().map(|b| b.id).collect();
        assert_eq!(ids.iter().position(|&i| i == "textures"), ids.iter().position(|&i| i == "hand-drawn").map(|i| i + 1), "after Hand-Drawn");
    }

    #[test]
    fn paper_cutouts_have_layered_edges_and_crisp_shadows() {
        let lib = builtin_style_library("paper-cutouts").unwrap();
        assert_eq!(lib.name, "Paper Cutouts");
        let names: Vec<&str> = lib.styles.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(names, ["Coral Layers", "Mint Layers", "Midnight Layers", "Sunflower Sheet", "Rose Layers", "Monochrome Stack"]);
        for g in &lib.styles {
            assert_eq!(g.appearance.items.len(), 3, "{} has a face and two cut edges", g.name);
            assert!(matches!(g.appearance.items[0], AppearanceItem::Fill(_)));
            assert!(matches!(g.appearance.items[1], AppearanceItem::Stroke(_)));
            assert!(matches!(g.appearance.items[2], AppearanceItem::Stroke(_)));
            assert_eq!(g.appearance.effects.len(), 1);
            let shadow = &g.appearance.effects[0];
            assert_eq!(shadow.id, "stylize.dropShadow");
            assert_eq!(shadow.params["blur"], 0.0);
            assert_eq!(shadow.params["opacity"], 100.0);
        }
        assert_eq!(read(&write(&lib), "Fallback").unwrap(), *lib);
    }

    /// The built-in styles' Warps draw with the map a new Warp takes.
    #[test]
    fn builtin_warp_styles_take_the_nets_map() {
        let mut warps = 0;
        for b in STYLE_LIBRARIES {
            let lib = builtin_style_library(b.id).unwrap();
            for g in &lib.styles {
                let mut effects: Vec<&Effect> = g.appearance.effects.iter().collect();
                effects.extend(g.appearance.items.iter().flat_map(|i| i.effects()));
                for e in effects.into_iter().filter(|e| e.id.starts_with("warp.")) {
                    assert_eq!(e.params["version"], json!(crate::live::WARP_NET), "{} › {}: {}", b.id, g.name, e.id);
                    warps += 1;
                }
            }
        }
        assert!(warps >= 3, "{warps}");
    }

    #[test]
    fn painted_and_sketched_follows_textures_with_one_effect_per_style() {
        let ids: Vec<&str> = STYLE_LIBRARIES.iter().map(|b| b.id).collect();
        let at = ids.iter().position(|&id| id == "painted-sketched").expect("listed");
        assert_eq!(ids[at - 1], "textures");
        let lib = builtin_style_library("painted-sketched").unwrap();
        assert_eq!(lib.name, "Painted and Sketched");
        let names: Vec<&str> = lib.styles.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(names, ["Washed Blue", "Bristle Ochre", "Pastel Heather", "Charcoal Shade", "Ink Contour", "Wet Ink", "Fine Pen"]);
        let effects: Vec<&str> = lib.styles.iter().flat_map(|g| g.appearance.effects.iter().map(|e| e.id.as_str())).collect();
        assert_eq!(
            effects,
            [
                "artistic.watercolor",
                "artistic.dryBrush",
                "artistic.roughPastels",
                "sketch.charcoal",
                "brushStrokes.inkOutlines",
                "brushStrokes.sumiE",
                "sketch.graphicPen"
            ]
        );
        assert!(lib.styles.iter().all(|g| g.appearance.items.iter().all(|i| i.effects().is_empty())), "no effects on a fill or stroke");
        // No other built-in library holds a style of the same name.
        for b in STYLE_LIBRARIES.iter().filter(|b| b.id != "painted-sketched") {
            let other = builtin_style_library(b.id).unwrap();
            assert!(other.styles.iter().all(|g| !names.contains(&g.name.as_str())), "{}", b.id);
        }
    }

    #[test]
    fn vcstyles_round_trip() {
        let mut lib = (*builtin_style_library("gradient-finishes").unwrap()).clone();
        lib.styles.extend(builtin_style_library("blends-transparency").unwrap().styles.iter().cloned());
        let text = write(&lib);
        assert!(sniff(&text) && !sniff("{\"format\": \"vectorcraft\"}") && !sniff("GIMP Palette"));
        assert_eq!(read(&text, "Fallback").unwrap(), lib);
        // An unnamed library takes the file's name; other JSON isn't a library.
        let unnamed = write(&StyleLibrary { name: " ".into(), ..lib.clone() });
        assert_eq!(read(&unnamed, "Mine").unwrap().name, "Mine");
        assert!(read("{\"format\": \"vcswatches\", \"version\": 1, \"name\": \"x\"}", "x").is_err());
    }

    #[test]
    fn a_library_from_a_document_stands_alone_and_keeps_its_patterns() {
        let mut d = Document::new(100.0, 100.0);
        let red = Paint::Solid { color: Color::rgb(1.0, 0.0, 0.0), swatch: Some("Red".into()), tint: 0.5 };
        let dots = Paint::Pattern { pattern: "Dots".into(), xf: Default::default() };
        d.patterns.push(PatternDef::new("Dots", vec![]));
        let mut g = GraphicStyle::new("Linked", Appearance::basic(red, dots, 2.0));
        g.id = 9;
        d.graphic_styles.push(g);
        let lib = StyleLibrary::from_document(&d, &["Linked".into()], "Mine".into()).unwrap();
        assert_eq!(lib.len(), 1);
        let s = &lib.styles[0];
        assert_eq!((s.id, s.appearance.fill_paint()), (0, Paint::solid(Color::rgb(1.0, 0.0, 0.0))), "unlinked, no id");
        assert_eq!(lib.patterns.iter().map(|p| p.name.as_str()).collect::<Vec<_>>(), ["Dots"]);
        assert!(s.same_look(&GraphicStyle { name: "Other".into(), id: 3, ..s.clone() }));
        assert!(!s.same_look(&GraphicStyle { opacity: 0.5, ..s.clone() }));
        assert_eq!(StyleLibrary::from_document(&d, &[], "All".into()).unwrap().len(), d.graphic_styles.len());
        assert!(StyleLibrary::from_document(&d, &["nope".into()], "x".into()).is_err());
        // Gradients lose their swatch and their stops' links too.
        let mut gp = GradientPaint::new(gradient(GradientKind::Linear, &[(0.0, "#ff0000", 1.0), (1.0, "#0000ff", 1.0)]));
        gp.swatch = Some("Sunset".into());
        gp.gradient.stops[0].set_color(hex("#ff0000"), Some(("Red".into(), 0.5)));
        let g = GraphicStyle::new("G", Appearance::basic(Paint::Gradient(Box::new(gp)), Paint::None, 1.0)).standalone();
        let Paint::Gradient(gp) = g.appearance.fill_paint() else { panic!("a gradient") };
        assert!(gp.swatch.is_none() && gp.gradient.stops.iter().all(|s| s.swatch.is_none() && s.tint == 1.0));
    }

    #[test]
    fn neon_and_glow_styles_are_defined_with_multi_strokes_and_effects() {
        let lib = builtin_style_library("neon-glow").expect("neon-glow library exists");
        assert_eq!(lib.name, "Neon & Glow");
        assert_eq!(lib.len(), 6);
        let names: Vec<&str> = lib.styles.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(names, ["Cyan Neon", "Magenta Laser", "Amber Tube", "Electric Lime", "Pulse Violet", "Cobalt Phosphor"]);
        for g in &lib.styles {
            let strokes = g.appearance.items.iter().filter(|i| matches!(i, AppearanceItem::Stroke(_))).count();
            assert!(strokes >= 2, "{} has {} strokes, expected >= 2", g.name, strokes);
            assert!(!g.appearance.effects.is_empty(), "{} has effects", g.name);
        }
        let cyan = lib.style("Cyan Neon").unwrap();
        assert_eq!(cyan.appearance.effects[0].id, "stylize.dropShadow");
        let magenta = lib.style("Magenta Laser").unwrap();
        assert_eq!(magenta.blend, BlendMode::Screen);
        let pulse = lib.style("Pulse Violet").unwrap();
        let pulse_fx: Vec<&str> = pulse.appearance.effects.iter().map(|e| e.id.as_str()).collect();
        assert_eq!(pulse_fx, ["stylize.innerGlow", "stylize.outerGlow"]);
        let cobalt = lib.style("Cobalt Phosphor").unwrap();
        assert_eq!(cobalt.appearance.effects[0].id, "blur.gaussian");
    }

    #[test]
    fn neon_glow_round_trips_through_vcstyles() {
        let lib = builtin_style_library("neon-glow").unwrap();
        let text = write(&lib);
        assert!(sniff(&text));
        let read_back = read(&text, "Fallback").unwrap();
        assert_eq!(*lib, read_back);
    }
}