mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Tests in vectorcraft-text and vectorcraft-doc that could pass on an empty result, or that only checked a result exists, now assert the values: - Blend: gradient steps assert their stop count before checking each stop's swatch, tint and plates; the odd dash blended to solid asserts the whole pattern; a copied step asserts its members. - Symbols: limiting depth keeps every symbol; a stroke override on type keeps the run it styles. - File fonts: an extreme descent clamps to the lowest table value and the Windows descent still fits. - Invert wrap on a tangled shape asserts the line still meets it. - Bidi, ligature carets, Thai and whitespace fallback: the glyph and line counts are asserted, Hebrew points stay in their letters' clusters, and every character of an isolate test has a glyph. - Glyph categories, trace views, compound shape modes and digit systems check written-out ids instead of a round trip over every value. Removed joins_on_a_circle_are_measured, which printed measurements and asserted nothing, and chord_placement_leaves_the_join_gap_above_the_baseline, which checked arithmetic done in the test itself; joined_glyphs_meet_on_a_curved_path covers joined type on a curve.
76 lines
2.8 KiB
Rust
76 lines
2.8 KiB
Rust
//! Compound shapes (Pathfinder panel › Make Compound Shape): a group whose members each have a
|
||
//! shape mode. It draws as the one area the modes make, from the backmost member up, painted with
|
||
//! the group's own appearance; the members stay editable. Evaluated in `vectorcraft-effects`.
|
||
|
||
use serde::{Deserialize, Deserializer, Serialize};
|
||
|
||
use crate::{Node, NodeKind};
|
||
|
||
/// How a compound shape's member combines with the area of the members below it. The backmost
|
||
/// member's mode does not matter: it starts the area.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub enum ShapeMode {
|
||
/// Adds the member's area.
|
||
#[default]
|
||
Add,
|
||
/// Takes the member's area away.
|
||
Subtract,
|
||
/// Keeps only what the member's area overlaps.
|
||
Intersect,
|
||
/// Keeps where exactly one of the area and the member's area is.
|
||
Exclude,
|
||
}
|
||
|
||
impl ShapeMode {
|
||
/// Every mode, in the Pathfinder panel's order.
|
||
pub const ALL: [ShapeMode; 4] = [ShapeMode::Add, ShapeMode::Subtract, ShapeMode::Intersect, ShapeMode::Exclude];
|
||
|
||
/// The mode's name in commands and files.
|
||
pub fn name(self) -> &'static str {
|
||
match self {
|
||
ShapeMode::Add => "add",
|
||
ShapeMode::Subtract => "subtract",
|
||
ShapeMode::Intersect => "intersect",
|
||
ShapeMode::Exclude => "exclude",
|
||
}
|
||
}
|
||
|
||
/// The mode named `s` ([`ShapeMode::name`]).
|
||
pub fn parse(s: &str) -> Option<Self> {
|
||
Self::ALL.into_iter().find(|m| m.name() == s)
|
||
}
|
||
}
|
||
|
||
/// Files are untrusted: a mode that isn't one of the names reads as Add.
|
||
impl<'de> Deserialize<'de> for ShapeMode {
|
||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||
let v = serde_json::Value::deserialize(d)?;
|
||
Ok(v.as_str().and_then(ShapeMode::parse).unwrap_or_default())
|
||
}
|
||
}
|
||
|
||
impl Node {
|
||
/// Is this a compound shape? Only a group that doesn't clip can be one: the mark on anything
|
||
/// else (from a file) is ignored.
|
||
pub fn is_compound_shape(&self) -> bool {
|
||
matches!(self.kind, NodeKind::Group { clip: false, .. }) && self.extras().compound_shape
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn modes_read_back_and_unknown_ones_are_add() {
|
||
assert_eq!(serde_json::to_value(ShapeMode::Subtract).unwrap(), serde_json::json!("subtract"));
|
||
assert_eq!(serde_json::from_value::<ShapeMode>(serde_json::json!("exclude")).unwrap(), ShapeMode::Exclude);
|
||
assert_eq!(ShapeMode::parse("intersect"), Some(ShapeMode::Intersect));
|
||
assert_eq!(ShapeMode::parse("Intersect"), None);
|
||
for v in [serde_json::json!("divide"), serde_json::json!(3), serde_json::json!(null), serde_json::json!({"a": [1]})] {
|
||
assert_eq!(serde_json::from_value::<ShapeMode>(v).unwrap(), ShapeMode::Add);
|
||
}
|
||
}
|
||
}
|