mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:05:55 +01:00
Merge remote-tracking branch 'upstream/main' into fix/swatches-list-height-1638
# Conflicts: # crates/ui-egui/src/panels/swatches/tests.rs
This commit is contained in:
commit
20a7d0ea1e
114 files changed
+5948
-474
No files matched your search
@@ -17,7 +17,7 @@ mod wrap;
|
||||
|
||||
pub use area::{AreaFit, AreaOptions, PathAlign, PathEffect, TextFlow, TextWrap, VerticalAlign, WrapShape};
|
||||
use char_style::yes;
|
||||
pub use char_style::{CharAlign, CharPosition, CharStyle, Digits, FontHeightReference, GlyphChoice, ScriptMetrics};
|
||||
pub use char_style::{CharAlign, CharPosition, CharStyle, Digits, FontHeightReference, GlyphChoice, MAX_AKI, ScriptMetrics};
|
||||
pub use composite::{
|
||||
COMPOSITE_BASELINE, COMPOSITE_FONTS_MAX, COMPOSITE_NAME_MAX, COMPOSITE_SCALE, COMPOSITE_SIZE, CharClass, CompositeFont, CompositePart,
|
||||
CompositeParts,
|
||||
|
||||
@@ -34,6 +34,16 @@ pub struct CharStyle {
|
||||
/// before it whose [`Self::kerning`] could space it. Elsewhere it has no effect.
|
||||
#[serde(default, rename = "kerningBefore", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub kerning_before: f64,
|
||||
/// Insert Aki Left (Character panel, East Asian options): space added before the character in
|
||||
/// reading order, in 1/1000 em as [`Self::tracking`], 0 to 1000. Before is on the left in
|
||||
/// left-to-right type, on the right in right-to-left type and above in vertical type. A
|
||||
/// cluster takes the value of its first character, and full justification leaves it as it is.
|
||||
/// <https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html>
|
||||
#[serde(default, rename = "akiLeft", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub aki_left: f64,
|
||||
/// Insert Aki Right: space added after the character in reading order, as [`Self::aki_left`].
|
||||
#[serde(default, rename = "akiRight", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub aki_right: f64,
|
||||
/// Use glyph shapes for automatic pair spacing; false uses font metrics.
|
||||
#[serde(default, rename = "opticalKerning", skip_serializing_if = "crate::skip::is_default")]
|
||||
pub optical_kerning: bool,
|
||||
@@ -404,6 +414,9 @@ fn hundred() -> f64 {
|
||||
100.0
|
||||
}
|
||||
|
||||
/// The largest Insert Aki Left or Right ([`CharStyle::aki_left`]), in 1/1000 em: one em.
|
||||
pub const MAX_AKI: f64 = 1000.0;
|
||||
|
||||
pub(super) fn yes() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -420,6 +433,8 @@ impl Default for CharStyle {
|
||||
tracking: 0.0,
|
||||
kerning: None,
|
||||
kerning_before: 0.0,
|
||||
aki_left: 0.0,
|
||||
aki_right: 0.0,
|
||||
optical_kerning: false,
|
||||
baseline_shift: 0.0,
|
||||
h_scale: 100.0,
|
||||
@@ -468,7 +483,8 @@ impl CharStyle {
|
||||
/// becomes Auto, and a value past [`crate::MAX_COORD`] (a baseline shift past it either way)
|
||||
/// becomes the nearer end. A leading of 0, which legacy `.ai` import stores, stays, and a
|
||||
/// baseline shift that is not a number becomes 0. The Tate-chu-yoko Up/Down and Left/Right
|
||||
/// offsets are kept as the baseline shift is.
|
||||
/// offsets are kept as the baseline shift is. Insert Aki Left and Right are kept from 0 to
|
||||
/// [`MAX_AKI`], and one that is not a number becomes 0.
|
||||
pub fn keep_dimensions_in_range(&mut self) {
|
||||
let default = Self::default();
|
||||
let within = |v: f64, or: f64| if v > 0.0 { v.min(crate::MAX_COORD) } else { or };
|
||||
@@ -480,6 +496,9 @@ impl CharStyle {
|
||||
self.baseline_shift = shift(self.baseline_shift);
|
||||
self.tcy_up_down = shift(self.tcy_up_down);
|
||||
self.tcy_left_right = shift(self.tcy_left_right);
|
||||
let aki = |v: f64| if v.is_nan() { 0.0 } else { v.clamp(0.0, MAX_AKI) };
|
||||
self.aki_left = aki(self.aki_left);
|
||||
self.aki_right = aki(self.aki_right);
|
||||
}
|
||||
/// Do the characters draw a stroke (a paint and a positive weight)?
|
||||
pub fn has_stroke(&self) -> bool {
|
||||
|
||||
@@ -490,6 +490,8 @@ impl crate::Document {
|
||||
st.baseline_shift = num("baseline_shift").unwrap_or(st.baseline_shift);
|
||||
st.tcy_up_down = num("tcyUpDown").unwrap_or(st.tcy_up_down);
|
||||
st.tcy_left_right = num("tcyLeftRight").unwrap_or(st.tcy_left_right);
|
||||
st.aki_left = num("akiLeft").unwrap_or(st.aki_left);
|
||||
st.aki_right = num("akiRight").unwrap_or(st.aki_right);
|
||||
st.keep_dimensions_in_range();
|
||||
let kept = [
|
||||
("size", Some(st.size)),
|
||||
@@ -498,6 +500,8 @@ impl crate::Document {
|
||||
("baseline_shift", Some(st.baseline_shift)),
|
||||
("tcyUpDown", Some(st.tcy_up_down)),
|
||||
("tcyLeftRight", Some(st.tcy_left_right)),
|
||||
("akiLeft", Some(st.aki_left)),
|
||||
("akiRight", Some(st.aki_right)),
|
||||
("leading", st.leading),
|
||||
];
|
||||
for (k, v) in kept {
|
||||
|
||||
@@ -357,7 +357,7 @@ impl ParaList {
|
||||
|
||||
/// The style the marker is set in: `first` (the style of the paragraph's first character)
|
||||
/// without its baseline shift, rotation, superscript or subscript, tracking, manual kerning,
|
||||
/// tate-chu-yoko, underline and strikethrough.
|
||||
/// Insert Aki, tate-chu-yoko, underline and strikethrough.
|
||||
pub fn marker_style(first: &CharStyle) -> CharStyle {
|
||||
CharStyle {
|
||||
baseline_shift: 0.0,
|
||||
@@ -366,6 +366,8 @@ impl ParaList {
|
||||
tracking: 0.0,
|
||||
kerning: None,
|
||||
kerning_before: 0.0,
|
||||
aki_left: 0.0,
|
||||
aki_right: 0.0,
|
||||
optical_kerning: first.optical_kerning,
|
||||
tate_chu_yoko: false,
|
||||
underline: false,
|
||||
|
||||
@@ -92,6 +92,43 @@ fn font_height_reference_is_saved_only_when_not_the_em_box() {
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(old).unwrap().font_height_reference, FontHeightReference::EmBox);
|
||||
}
|
||||
|
||||
/// Insert Aki Left and Right are 0 by default and saved only when not 0, each side on its own;
|
||||
/// styles written before them read as 0, and a value out of range from a file is kept from 0 to one
|
||||
/// em.
|
||||
#[test]
|
||||
fn insert_aki_is_saved_only_when_set() {
|
||||
let st = CharStyle::default();
|
||||
assert_eq!((st.aki_left, st.aki_right), (0.0, 0.0));
|
||||
let saved = serde_json::to_value(&st).unwrap();
|
||||
assert!(saved.get("akiLeft").is_none() && saved.get("akiRight").is_none());
|
||||
for set in [
|
||||
CharStyle { aki_left: 250.0, ..CharStyle::default() },
|
||||
CharStyle { aki_right: 500.0, ..CharStyle::default() },
|
||||
CharStyle { aki_left: 125.5, aki_right: 1000.0, ..CharStyle::default() },
|
||||
] {
|
||||
let saved = serde_json::to_value(&set).unwrap();
|
||||
assert_eq!(saved.get("akiLeft").and_then(serde_json::Value::as_f64).unwrap_or(0.0), set.aki_left);
|
||||
assert_eq!(saved.get("akiRight").and_then(serde_json::Value::as_f64).unwrap_or(0.0), set.aki_right);
|
||||
assert_eq!(saved.get("akiLeft").is_some(), set.aki_left != 0.0);
|
||||
assert_eq!(serde_json::from_value::<CharStyle>(saved).unwrap(), set);
|
||||
}
|
||||
let old = serde_json::to_value(&st).unwrap();
|
||||
let read = serde_json::from_value::<CharStyle>(old).unwrap();
|
||||
assert_eq!((read.aki_left, read.aki_right), (0.0, 0.0));
|
||||
let mut damaged = CharStyle { aki_left: -20.0, aki_right: 4000.0, ..CharStyle::default() };
|
||||
damaged.keep_dimensions_in_range();
|
||||
assert_eq!((damaged.aki_left, damaged.aki_right), (0.0, MAX_AKI));
|
||||
let mut damaged = CharStyle { aki_left: f64::NAN, aki_right: f64::INFINITY, ..CharStyle::default() };
|
||||
damaged.keep_dimensions_in_range();
|
||||
assert_eq!((damaged.aki_left, damaged.aki_right), (0.0, MAX_AKI));
|
||||
// A character style from a file is kept in range the same way.
|
||||
let mut d = crate::Document::new(100.0, 100.0);
|
||||
let attrs = serde_json::json!({"akiLeft": 5000, "akiRight": -1});
|
||||
d.char_styles.push(TextStyleDef { name: "Spaced".into(), attrs: attrs.as_object().cloned().unwrap_or_default() });
|
||||
d.keep_type_values_in_range();
|
||||
assert_eq!((d.char_styles[0].attrs["akiLeft"].as_f64(), d.char_styles[0].attrs["akiRight"].as_f64()), (Some(MAX_AKI), Some(0.0)));
|
||||
}
|
||||
|
||||
/// Each Indic numeral system draws 0–9 (and the Arabic-Indic sets) as the ten digits of its
|
||||
/// script's block; every system saves under its id, which reads back, and other names are refused.
|
||||
#[test]
|
||||
|
||||
@@ -189,8 +189,8 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
|
||||
EXTRUDE,
|
||||
"Extrude & Bevel…",
|
||||
&["Effect", "3D and Materials"],
|
||||
"{depth: pt (0..2000, 50), twist: degrees (0..360, 0; the back turned against the front), taper: % (1..100, 100; the back's size), cap: bool (true: solid; false: hollow), bevel: none|classic|rounded|cove|step|ogee (none), bevelWidth: pt (0..1000, 4), bevelHeight: pt (0..1000, 4), bevelRepeat: integer (1..32, 1), bevelSpace: % (0..90, 0; flat between repeats), bevelInside: bool (false: added around the shape; true: carved into it), bevelBothSides: bool (false), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across)}. The front face is the art, extruded backwards about the centre of its bounds; painted with the top fill (else the top stroke).",
|
||||
json!({"depth":50.0,"twist":0.0,"taper":100.0,"cap":true,"bevel":"none","bevelWidth":4.0,"bevelHeight":4.0,"bevelRepeat":1,"bevelSpace":0.0,"bevelInside":false,"bevelBothSides":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0}),
|
||||
"{depth: pt (0..2000, 50), twist: degrees (0..360, 0; the back turned against the front), taper: % (1..100, 100; the back's size), cap: bool (true: solid; false: hollow), bevel: none|classic|rounded|cove|step|ogee (none), bevelWidth: pt (0..1000, 4), bevelHeight: pt (0..1000, 4), bevelRepeat: integer (1..32, 1), bevelSpace: % (0..90, 0; flat between repeats), bevelInside: bool (false: added around the shape; true: carved into it), bevelBothSides: bool (false), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across), graphics: array of 0..64 objects ([]; the 3D and Materials panel's Graphics on the caps, see threeD.mapGraphic: symbol: name of a document symbol, face: front|back (front; the back reads left to right as seen from behind), x: pt (0; the offset of the graphic's center from the center of the object's bounds, in the face's own plane), y: pt (0), scale: % (1..10000, 100; of the symbol's own size), rotation: degrees (-360..360, 0; counterclockwise); drawn turned, foreshortened and shaded with the cap and clipped to it, on a solid cap only)}. The front face is the art, extruded backwards about the centre of its bounds; painted with the top fill (else the top stroke).",
|
||||
json!({"depth":50.0,"twist":0.0,"taper":100.0,"cap":true,"bevel":"none","bevelWidth":4.0,"bevelHeight":4.0,"bevelRepeat":1,"bevelSpace":0.0,"bevelInside":false,"bevelBothSides":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0,"graphics":[]}),
|
||||
),
|
||||
g(
|
||||
INFLATE,
|
||||
@@ -203,8 +203,8 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
|
||||
ROTATE,
|
||||
"Rotate…",
|
||||
&["Effect", "3D and Materials"],
|
||||
"{rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across)}. Turns the flat art in 3D about the centre of its bounds; every fill and stroke keeps its paint, shaded as one plane.",
|
||||
json!({"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0}),
|
||||
"{rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth), shadow: bool (false; the first light casts a hard-edged shadow, black at 50% times the art's opacity), shadowPosition: behind|below (behind; behind: on a plane square to the view, below: on the floor under the object, turned with it), shadowDistance: % (0..100, 0; how far the plane is from the object, as a share of its largest size), shadowBounds: % (10..400, 100; how far the plane reaches: at 100 it is four times the object's largest size across), graphics: array of 0..64 objects ([]; the 3D and Materials panel's Graphics on the plane, as Extrude & Bevel's, with face front only)}. Turns the flat art in 3D about the centre of its bounds; every fill and stroke keeps its paint, shaded as one plane.",
|
||||
json!({"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0,"shadow":false,"shadowPosition":"behind","shadowDistance":0.0,"shadowBounds":100.0,"graphics":[]}),
|
||||
),
|
||||
g(
|
||||
CLASSIC_EXTRUDE,
|
||||
|
||||
@@ -87,15 +87,21 @@ pub use revolve::{
|
||||
CLASSIC_REVOLVE, REVOLVE, has_revolve, is_revolve, revolve_art, revolve_art_in, revolve_options, validate_revolve, validate_revolve_in,
|
||||
};
|
||||
pub use solids::{
|
||||
CLASSIC_EXTRUDE, CLASSIC_ROTATE, EXTRUDE, INFLATE, ROTATE, ROTATE_SURFACES, ROTATION_PRESETS, SHADING_COLORS, SOLIDS, SolidMesh, THREE_D_TYPES,
|
||||
classic_extrude_options, classic_shading, extrude_options, has_solid, inflate_options, is_classic_three_d, is_three_d, solid_art, solid_art_in,
|
||||
three_d_meshes, three_d_meshes_in, three_d_meshes_of, three_d_meshes_of_in, three_d_params_as, three_d_wireframe_in, validate_solid,
|
||||
validate_solid_in, validate_three_d, validate_three_d_at, validate_three_d_at_in, validate_three_d_in, validate_three_d_params, view_options,
|
||||
CLASSIC_EXTRUDE, CLASSIC_ROTATE, EXTRUDE, GRAPHICS, GraphicFrame, INFLATE, MAX_GRAPHICS, MappedGraphic, ROTATE, ROTATE_SURFACES,
|
||||
ROTATION_PRESETS, SHADING_COLORS, SOLIDS, SolidMesh, THREE_D_TYPES, classic_extrude_options, classic_shading, extrude_options, graphic_bounds,
|
||||
graphic_frame, graphic_symbols, has_solid, inflate_options, is_classic_three_d, is_three_d, mapped_graphics, missing_symbol,
|
||||
names_graphic_symbol, rename_graphic_symbol, rename_graphic_symbol_in, solid_art, solid_art_in, takes_graphics, three_d_meshes,
|
||||
three_d_meshes_in, three_d_meshes_of, three_d_meshes_of_in, three_d_params_as, three_d_wireframe_in, validate_solid, validate_solid_in,
|
||||
validate_three_d, validate_three_d_at, validate_three_d_at_in, validate_three_d_in, validate_three_d_params, view_options, with_graphic,
|
||||
};
|
||||
pub use solids::{
|
||||
GRAPHIC_MAX_OFFSET, GRAPHIC_SCALE_RANGE, appearance_graphic_symbols, graphic_frame_at, graphics_reshaped_after, rename_appearance_graphic_symbol,
|
||||
};
|
||||
pub use stylize::{ScribbleSetting, scribble_setting_id, scribble_settings};
|
||||
pub use svg_filter::SVG_FILTER;
|
||||
pub use trap::TRAP;
|
||||
pub use vectorcraft_three_d::Mesh;
|
||||
pub use vectorcraft_three_d::PlanarFace;
|
||||
pub use warp::{WARP_NET, WarpNet, WarpStyle, map_version, warp_point, warp_version};
|
||||
|
||||
mod catalog;
|
||||
|
||||
@@ -620,15 +620,17 @@ fn art(doc: Option<&Document>, n: &Node, expand: bool) -> Option<Node> {
|
||||
let path = geometry(&path, &n.appearance.effects, &ctx);
|
||||
remaining = crate::without_geometry(&remaining);
|
||||
// The object's Transform effects, applied to the path above, transform the items' raster
|
||||
// effects too.
|
||||
let mut items: Vec<AppearanceItem> = n.appearance.items.iter().filter(|i| crate::paints(i)).cloned().collect();
|
||||
// effects too. Each copy keeps its item beside it, which plug-in effects are told they sit
|
||||
// on.
|
||||
let originals: Vec<&AppearanceItem> = n.appearance.items.iter().filter(|i| crate::paints(i)).collect();
|
||||
let mut items: Vec<AppearanceItem> = originals.iter().map(|i| (*i).clone()).collect();
|
||||
crate::raster::follow_paints(&mut items, &n.appearance.effects);
|
||||
// Every item's shadow goes behind every item's art.
|
||||
let mut shadows = vec![];
|
||||
for item in &items {
|
||||
for (original, item) in originals.into_iter().zip(&items) {
|
||||
if let Some(index) = item.effects().iter().position(|e| e.visible && is_revolve(&e.id)) {
|
||||
let fx = item.effects().get(index)?;
|
||||
let p = geometry(&path, item.effects().get(..index)?, &ctx.item(item));
|
||||
let p = geometry(&path, item.effects().get(..index)?, &ctx.item(original));
|
||||
let expand = expand && !item.effects().iter().skip(index + 1).any(|e| e.visible && is_geometry(&e.id));
|
||||
let mut art = object_surface(n, &p, fx, item, expand, &mut copies)?;
|
||||
art.appearance.effects = item.effects().iter().skip(index + 1).filter(|e| !is_revolve(&e.id)).cloned().collect();
|
||||
|
||||
+289
-24
@@ -6,7 +6,7 @@ use serde_json::Value;
|
||||
use vectorcraft_doc::color::{BlendMode, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, Effect, Node, NodeId, NodeKind, OpacityMask, StrokeLayer};
|
||||
use vectorcraft_geom::{FillRule, PathData, Rect};
|
||||
use vectorcraft_three_d::{Bevel, BevelShape, Extrude, Facet, Inflate, Mesh, View};
|
||||
use vectorcraft_three_d::{Bevel, BevelShape, Extrude, Facet, Inflate, Mesh, PlanarFace, View};
|
||||
|
||||
#[cfg(test)]
|
||||
use vectorcraft_doc::color::Color;
|
||||
@@ -20,6 +20,15 @@ pub use classic::{CLASSIC_EXTRUDE, CLASSIC_ROTATE, ROTATE_SURFACES, SHADING_COLO
|
||||
mod source;
|
||||
pub(crate) use source::{resolve, resolve_for};
|
||||
|
||||
mod graphics;
|
||||
pub use graphics::{
|
||||
GRAPHICS, GraphicFrame, MAX_GRAPHICS, MappedGraphic, graphic_symbols, mapped_graphics, missing_symbol, names_graphic_symbol,
|
||||
rename_graphic_symbol, rename_graphic_symbol_in, takes_graphics, with_graphic,
|
||||
};
|
||||
pub use graphics::{
|
||||
MAX_OFFSET as GRAPHIC_MAX_OFFSET, SCALE_RANGE as GRAPHIC_SCALE_RANGE, appearance_graphic_symbols, rename_appearance_graphic_symbol,
|
||||
};
|
||||
|
||||
pub(crate) mod shadow;
|
||||
|
||||
pub(crate) mod wireframe;
|
||||
@@ -70,6 +79,12 @@ pub fn three_d_params_as(from: &str, params: &Value, to: &str) -> Value {
|
||||
}
|
||||
}
|
||||
}
|
||||
// A plane has only a front, so graphics on the back cap are dropped.
|
||||
if to == ROTATE
|
||||
&& let Some(Value::Array(list)) = out.get_mut(GRAPHICS)
|
||||
{
|
||||
list.retain(|g| g.get("face").and_then(Value::as_str) != Some(PlanarFace::Back.id()));
|
||||
}
|
||||
Value::Object(out)
|
||||
}
|
||||
|
||||
@@ -212,6 +227,7 @@ fn validate_solid_params(id: &str, p: &Value) -> Result<(), String> {
|
||||
}
|
||||
crate::lighting::validate(p)?;
|
||||
shadow::validate(p)?;
|
||||
graphics::validate(id, p)?;
|
||||
if let Some(v) = p.get("bevel")
|
||||
&& !v.as_str().is_some_and(|s| s == "none" || BevelShape::parse(s).is_some())
|
||||
{
|
||||
@@ -241,6 +257,9 @@ pub fn validate_solid_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value)
|
||||
/// `item`, whose shadow is cast by that item's solid alone.
|
||||
fn validate_solid_for(doc: Option<&Document>, n: &Node, id: &str, p: &Value, item: Option<usize>) -> Result<(), String> {
|
||||
validate_solid_params(id, p)?;
|
||||
if let Some(doc) = doc {
|
||||
graphics::validate_symbols(doc, p)?;
|
||||
}
|
||||
let label = label_of(id);
|
||||
let n = &*source::resolve_for(doc, n, label, is_plane(id))?;
|
||||
if is_container(n) {
|
||||
@@ -434,6 +453,7 @@ pub fn validate_three_d(n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
|
||||
/// [`validate_three_d`] with the symbols of `doc` ([`validate_solid_in`]).
|
||||
pub fn validate_three_d_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value) -> Result<(), String> {
|
||||
graphics::validate(id, p)?;
|
||||
if crate::revolve::is_revolve(id) { crate::revolve::validate_in(doc, n, id, p) } else { validate_solid_in(doc, n, id, p) }
|
||||
}
|
||||
|
||||
@@ -449,6 +469,7 @@ pub fn validate_three_d_at(n: &Node, id: &str, p: &Value, at: Option<(usize, usi
|
||||
/// [`validate_three_d_at`] with the symbols of `doc` ([`validate_solid_in`]). A symbol instance, a
|
||||
/// blend and a clipping group take a 3D effect as groups do: on the object itself.
|
||||
pub fn validate_three_d_at_in(doc: Option<&Document>, n: &Node, id: &str, p: &Value, at: Option<(usize, usize)>) -> Result<(), String> {
|
||||
graphics::validate(id, p)?;
|
||||
match at {
|
||||
Some(_) if is_container(n) || source::resolves(n) => {
|
||||
Err("A 3D effect on a group or layer goes on the group itself, not on its fill or stroke".into())
|
||||
@@ -463,6 +484,7 @@ pub fn validate_three_d_at_in(doc: Option<&Document>, n: &Node, id: &str, p: &Va
|
||||
/// build the art, for an edit that hides the effect: hidden, it draws nothing, so its object does
|
||||
/// not need to fit.
|
||||
pub fn validate_three_d_params(id: &str, p: &Value) -> Result<(), String> {
|
||||
graphics::validate(id, p)?;
|
||||
if crate::revolve::is_revolve(id) { crate::revolve::validate_params_of(id, p) } else { validate_solid_params(id, p) }
|
||||
}
|
||||
|
||||
@@ -712,8 +734,8 @@ type ShadowAndSurfaces = (Vec<Arc<Node>>, Vec<Arc<Node>>);
|
||||
/// blending mode, and those of a group it is in, apply to the surfaces of the members it holds
|
||||
/// ([`masked`]). The art comes in two parts: the effect's shadow ([`cast_shadow`], none with it
|
||||
/// off), to be drawn behind every surface of the object, and the surfaces.
|
||||
fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<ShadowAndSurfaces> {
|
||||
let art = surfaces_for(n, fx, shapes)?;
|
||||
fn art_for(doc: Option<&Document>, n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<ShadowAndSurfaces> {
|
||||
let art = surfaces_for(doc, n, fx, shapes)?;
|
||||
// A shadow past the limits draws the source path, as a solid past them does; commands report
|
||||
// the error ([`validate_solid_in`]).
|
||||
let shadow = match cast_shadow(&fx.id, &crate::merged_params(&fx.id, &fx.params), shapes).ok()? {
|
||||
@@ -789,8 +811,139 @@ fn cast_shadow<'a>(id: &str, p: &Value, shapes: &'a [Profile]) -> Result<Option<
|
||||
/// its member's scopes and ink ([`cast_shadow`]).
|
||||
type Cast<'a> = (vectorcraft_three_d::ShadowCaster, vectorcraft_three_d::Shadow, Vec<shadow::Cast<'a>>);
|
||||
|
||||
/// [`art_for`] without the shadow: the solids' surfaces.
|
||||
fn surfaces_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>> {
|
||||
/// The graphics of a 3D effect with parameters `p`, drawn on its faces ([`graphics::face_art`]):
|
||||
/// for each planar face with a graphic, the art and the face. `caps` are the faces the viewer sees
|
||||
/// of the ends of the object's solids, each with its end; `bounds` are the bounds the solids were
|
||||
/// built with; `opacity` is the opacity of the fill or stroke those solids are painted with, which
|
||||
/// the graphics take too. `elements` is the effect's count of drawn path elements, shared by every
|
||||
/// call for one effect ([`graphics::face_art`]).
|
||||
fn graphic_art(
|
||||
doc: Option<&Document>,
|
||||
id: NodeId,
|
||||
(effect, p): (&str, &Value),
|
||||
bounds: Rect,
|
||||
caps: &[&Facet],
|
||||
opacity: f32,
|
||||
elements: &mut usize,
|
||||
) -> Vec<(PlanarFace, Arc<Node>)> {
|
||||
let list = graphics::mapped_graphics(p);
|
||||
if list.is_empty() || !takes_graphics(effect) {
|
||||
return vec![];
|
||||
}
|
||||
let view = view_options(p);
|
||||
PlanarFace::ALL
|
||||
.into_iter()
|
||||
.filter_map(|face| {
|
||||
let on: Vec<&Facet> = caps.iter().copied().filter(|f| f.end == Some(face)).collect();
|
||||
let map = if effect == ROTATE {
|
||||
vectorcraft_three_d::plane_face_map(bounds, view)
|
||||
} else {
|
||||
vectorcraft_three_d::extrude_face_map(bounds, extrude_options(p), view, face)
|
||||
}?;
|
||||
graphics::face_art(doc, id, &list, (face, &map), bounds.center(), &on, elements).map(|mut art| {
|
||||
art.opacity *= opacity;
|
||||
(face, Arc::new(art))
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A solid (a fill's or a stroke's) with ends the viewer sees: the index of its surface, its
|
||||
/// member, the ends, the opacity and blending mode of its fill or stroke, and whether its surface
|
||||
/// carries an opacity mask (a gradient's transparency under a colored light, [`surface`]).
|
||||
struct Filled {
|
||||
at: usize,
|
||||
member: usize,
|
||||
caps: Vec<Facet>,
|
||||
opacity: f32,
|
||||
blend: BlendMode,
|
||||
masked: bool,
|
||||
}
|
||||
|
||||
/// Does a surface in `paint` carry an opacity mask ([`surface`])?
|
||||
fn masks_surface(paint: &Paint, colored: bool) -> bool {
|
||||
colored && crate::lighting::opaque_paint(paint).1.is_some()
|
||||
}
|
||||
|
||||
/// The graphics of the solids `filled` ([`graphic_art`]), each with the index of the surface it is
|
||||
/// drawn right after (the last of those solids holding its face) and its face. With `per_member`
|
||||
/// (a member's opacity mask or blending mode applies to its surfaces) each member's solids get
|
||||
/// graphics of their own, clipped to that member's caps, so they stay in that member's scopes.
|
||||
/// All the groups share one limit on the path elements drawn: once it is reached, later groups
|
||||
/// draw no graphics.
|
||||
fn placed_graphics(
|
||||
doc: Option<&Document>,
|
||||
id: NodeId,
|
||||
effect: (&str, &Value),
|
||||
bounds: Rect,
|
||||
filled: &[Filled],
|
||||
per_member: bool,
|
||||
) -> Vec<(usize, PlanarFace, Arc<Node>)> {
|
||||
let mut groups: Vec<Vec<&Filled>> = vec![];
|
||||
for f in filled {
|
||||
// Solids whose fills or strokes differ in opacity or blending mode get graphics of their
|
||||
// own, each drawn as its fill or stroke is. A solid whose surface carries an opacity mask
|
||||
// gets its own too, which takes that mask ([`with_graphics`]).
|
||||
let joins = |first: &Filled| {
|
||||
(!per_member || first.member == f.member) && first.opacity == f.opacity && first.blend == f.blend && !first.masked && !f.masked
|
||||
};
|
||||
match groups.iter_mut().find(|g| g.first().is_some_and(|first| joins(first))) {
|
||||
Some(g) => g.push(f),
|
||||
None => groups.push(vec![f]),
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
let mut elements = 0usize;
|
||||
for group in groups {
|
||||
let caps: Vec<&Facet> = group.iter().flat_map(|f| &f.caps).collect();
|
||||
let opacity = group.first().map_or(1.0, |f| f.opacity);
|
||||
let blend = group.first().map_or(BlendMode::Normal, |f| f.blend);
|
||||
for (face, mut art) in graphic_art(doc, id, effect, bounds, &caps, opacity, &mut elements) {
|
||||
if let Some(f) = group.iter().rev().find(|f| f.caps.iter().any(|c| c.end == Some(face))) {
|
||||
Arc::make_mut(&mut art).blend = blend;
|
||||
out.push((f.at, face, art));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// One solid's surface as [`surfaces_for`] builds it: its faces' depths, its art, its member's
|
||||
/// scopes, and for each face the end of a solid it is, when the viewer sees it.
|
||||
type BuiltSurface<'s> = (Vec<Placed>, Node, &'s [Scope], Vec<Option<PlanarFace>>);
|
||||
|
||||
/// [`with_graphics`] of built surfaces, each drawn whole.
|
||||
fn whole_surfaces<'s>(surfaces: Vec<BuiltSurface<'s>>, arts: &[(usize, PlanarFace, Arc<Node>)]) -> Vec<(&'s [Scope], Arc<Node>)> {
|
||||
with_graphics(surfaces.into_iter().map(|(_, s, scopes, _)| (scopes, Arc::new(s))).collect(), arts)
|
||||
}
|
||||
|
||||
/// `surfaces` drawn whole, each with its scopes, and the graphics `arts` ([`placed_graphics`]) each
|
||||
/// right after its surface, with that surface's scopes and its opacity mask (a gradient's
|
||||
/// transparency), so a graphic is hidden where its cap is.
|
||||
fn with_graphics<'s>(surfaces: Vec<(&'s [Scope], Arc<Node>)>, arts: &[(usize, PlanarFace, Arc<Node>)]) -> Vec<(&'s [Scope], Arc<Node>)> {
|
||||
let mut out = Vec::with_capacity(surfaces.len() + arts.len());
|
||||
for (k, (scopes, s)) in surfaces.into_iter().enumerate() {
|
||||
let mask = s.extras().mask.clone();
|
||||
out.push((scopes, s));
|
||||
out.extend(arts.iter().filter(|(at, ..)| *at == k).map(|(.., art)| {
|
||||
let mut art = art.clone();
|
||||
if let Some(mask) = &mask {
|
||||
Arc::make_mut(&mut art).extras_mut().mask = Some(mask.clone());
|
||||
}
|
||||
(scopes, art)
|
||||
}));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The ends of `f` the viewer sees, for a fill's solid or a stroke's: where its graphics go.
|
||||
fn seen_ends(f: &[Facet]) -> impl Iterator<Item = &Facet> {
|
||||
f.iter().filter(|f| f.end.is_some() && !f.away)
|
||||
}
|
||||
|
||||
/// [`art_for`] without the shadow: the solids' surfaces, with the graphics mapped onto their planar
|
||||
/// faces drawn right after the last of the solids' surfaces that holds that face.
|
||||
fn surfaces_for(doc: Option<&Document>, n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>> {
|
||||
let b = union_bounds(shapes)?;
|
||||
let p = crate::merged_params(&fx.id, &fx.params);
|
||||
let classic = fx.id == CLASSIC_EXTRUDE || fx.id == CLASSIC_ROTATE;
|
||||
@@ -798,11 +951,14 @@ fn surfaces_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Nod
|
||||
let masks = has_masks(shapes);
|
||||
// The paint copies of every solid together: past the allowance the source path is drawn.
|
||||
let mut paint_bytes = 0usize;
|
||||
// The caps are copied only when the effect has graphics to draw on them.
|
||||
let graphics_on = !classic && takes_graphics(&fx.id) && !mapped_graphics(&p).is_empty();
|
||||
if is_plane(&fx.id) {
|
||||
// Every part turns about the centre of the object's own bounds (a group's members about
|
||||
// the centre of theirs), so they stay together.
|
||||
let mut out = vec![];
|
||||
for s in shapes {
|
||||
let mut filled = vec![];
|
||||
for (member, s) in shapes.iter().enumerate() {
|
||||
for item in &s.items {
|
||||
let (paint, opacity, blend) = paint_of(item, s.opacity);
|
||||
let (area, rule) = region(&s.path, s.rule, item);
|
||||
@@ -810,10 +966,18 @@ fn surfaces_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Nod
|
||||
if !crate::revolve::paint_copies_add(&mut paint_bytes, facet_copies(&f, &paint, colored), &paint) {
|
||||
return None;
|
||||
}
|
||||
if graphics_on {
|
||||
let caps: Vec<Facet> = seen_ends(&f).cloned().collect();
|
||||
if !caps.is_empty() {
|
||||
filled.push(Filled { at: out.len(), member, caps, opacity, blend, masked: masks_surface(&paint, colored) });
|
||||
}
|
||||
}
|
||||
let art = if classic { classic::surface(n, f, &paint, opacity, blend, &p) } else { surface(n, f, &paint, opacity, blend, colored) };
|
||||
out.push((s.scopes.as_slice(), Arc::new(art)));
|
||||
}
|
||||
}
|
||||
let arts = placed_graphics(doc, n.id, (ROTATE, &p), b, &filled, masks);
|
||||
let out = with_graphics(out, &arts);
|
||||
return Some(if masks { masked(n.id, out) } else { out.into_iter().map(|(_, s)| s).collect() });
|
||||
}
|
||||
let (parts, bounds) = solid_parts(shapes, b);
|
||||
@@ -845,36 +1009,56 @@ fn surfaces_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Nod
|
||||
let (_, opacity, blend) = paint_of(item, *member);
|
||||
opacity >= 1.0 && blend == BlendMode::Normal
|
||||
});
|
||||
let surfaces: Vec<(Vec<Placed>, Node, &[Scope])> = solids
|
||||
let arts = if !graphics_on {
|
||||
vec![]
|
||||
} else {
|
||||
let filled: Vec<Filled> = solids
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(at, (item, member, _, f, shape))| {
|
||||
let caps: Vec<Facet> = seen_ends(f).cloned().collect();
|
||||
let (paint, opacity, blend) = paint_of(item, *member);
|
||||
let masked = masks_surface(&paint, colored);
|
||||
(!caps.is_empty()).then_some(Filled { at, member: *shape, caps, opacity, blend, masked })
|
||||
})
|
||||
.collect();
|
||||
placed_graphics(doc, n.id, (&fx.id, &p), bounds, &filled, masks)
|
||||
};
|
||||
let surfaces: Vec<BuiltSurface> = solids
|
||||
.into_iter()
|
||||
.map(|(item, member, scopes, f, shape)| {
|
||||
let closed = shapes.get(shape).is_some_and(|s| s.path.subpaths.iter().all(|p| p.closed));
|
||||
let paints = Paints::of(item, closed);
|
||||
let depths: Vec<Placed> = f.iter().map(|f| Placed::of(f, shape, paints)).collect();
|
||||
let ends = f.iter().map(|f| f.end.filter(|_| !f.away)).collect();
|
||||
let (paint, opacity, blend) = paint_of(item, member);
|
||||
let art = if classic { classic::surface(n, f, &paint, opacity, blend, &p) } else { surface(n, f, &paint, opacity, blend, colored) };
|
||||
(depths, art, scopes)
|
||||
(depths, art, scopes, ends)
|
||||
})
|
||||
.collect();
|
||||
if masks {
|
||||
return Some(masked(n.id, surfaces.into_iter().map(|(_, s, scopes)| (scopes, Arc::new(s))).collect()));
|
||||
return Some(masked(n.id, whole_surfaces(surfaces, &arts)));
|
||||
}
|
||||
// One order takes a plain group of faces from each solid, one face for each facet. With
|
||||
// colored lights a surface can instead be one clipped composite of all its facets. A composite
|
||||
// has no faces to order, so when any surface is one, every surface of the solid, masked ones
|
||||
// included, draws whole in member order.
|
||||
let plain = surfaces.iter().all(|(depths, s, _)| matches!(&s.kind, NodeKind::Group { children, clip: false } if children.len() == depths.len()));
|
||||
if !opaque || !plain {
|
||||
return Some(surfaces.into_iter().map(|(_, s, _)| Arc::new(s)).collect());
|
||||
}
|
||||
let plain =
|
||||
surfaces.iter().all(|(depths, s, _, _)| matches!(&s.kind, NodeKind::Group { children, clip: false } if children.len() == depths.len()));
|
||||
// A surface in a gradient with transparent stops carries an opacity mask: its faces go in runs
|
||||
// ([`in_runs`]), and each run carries the mask.
|
||||
let masks: Vec<Option<Box<OpacityMask>>> = surfaces.iter().map(|(_, s, _)| s.extras().mask.clone()).collect();
|
||||
let (mut placed, mut faces) = (vec![], vec![]);
|
||||
for (surface, (depths, s, _)) in surfaces.into_iter().enumerate() {
|
||||
// ([`in_runs`]), and each run carries the mask. With graphics as well, the surfaces draw whole,
|
||||
// each graphic right after its surface.
|
||||
let masks: Vec<Option<Box<OpacityMask>>> = surfaces.iter().map(|(_, s, _, _)| s.extras().mask.clone()).collect();
|
||||
let masked_faces = masks.iter().any(Option::is_some);
|
||||
if !opaque || !plain || (masked_faces && !arts.is_empty()) {
|
||||
return Some(whole_surfaces(surfaces, &arts).into_iter().map(|(_, s)| s).collect());
|
||||
}
|
||||
let (mut placed, mut faces, mut ends) = (vec![], vec![], vec![]);
|
||||
for (surface, (depths, s, _, e)) in surfaces.into_iter().enumerate() {
|
||||
if let NodeKind::Group { children, .. } = s.kind {
|
||||
placed.extend(depths);
|
||||
faces.extend(children.into_iter().map(|face| Some((surface, face))));
|
||||
ends.extend(e);
|
||||
}
|
||||
}
|
||||
let sealed = match fx.id.as_str() {
|
||||
@@ -883,8 +1067,18 @@ fn surfaces_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Nod
|
||||
_ => extrude_options(&p).cap,
|
||||
};
|
||||
let order = draw_order(&placed, sealed);
|
||||
let faces: Vec<(usize, Arc<Node>)> = order.into_iter().filter_map(|k| faces.get_mut(k).and_then(Option::take)).collect();
|
||||
let children = if masks.iter().all(Option::is_none) { faces.into_iter().map(|(_, face)| face).collect() } else { in_runs(n.id, faces, &masks) };
|
||||
if masked_faces {
|
||||
let faces: Vec<(usize, Arc<Node>)> = order.into_iter().filter_map(|k| faces.get_mut(k).and_then(Option::take)).collect();
|
||||
return Some(vec![Arc::new(Node::group(n.id, in_runs(n.id, faces, &masks)))]);
|
||||
}
|
||||
// Each face's graphics go right after the last of the faces they are drawn on.
|
||||
let last: Vec<Option<usize>> =
|
||||
arts.iter().map(|(_, face, _)| order.iter().rposition(|k| ends.get(*k).copied().flatten() == Some(*face))).collect();
|
||||
let mut children = Vec::with_capacity(order.len() + arts.len());
|
||||
for (at, k) in order.into_iter().enumerate() {
|
||||
children.extend(faces.get_mut(k).and_then(Option::take).map(|(_, face)| face));
|
||||
children.extend(last.iter().zip(&arts).filter(|(l, _)| **l == Some(at)).map(|(_, (.., art))| art.clone()));
|
||||
}
|
||||
Some(vec![Arc::new(Node::group(n.id, children))])
|
||||
}
|
||||
|
||||
@@ -969,7 +1163,7 @@ pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
}
|
||||
let n = &*resolve(doc, n, "").ok()?;
|
||||
if is_container(n) {
|
||||
return group_solid_art(n);
|
||||
return group_solid_art(doc, n);
|
||||
}
|
||||
let (path, rule) = profile(n)?;
|
||||
let ctx = GeomContext::of(n);
|
||||
@@ -978,7 +1172,7 @@ pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
let remaining: Vec<Effect>;
|
||||
if let Some((index, fx)) = solid(&n.appearance.effects) {
|
||||
let path = geometry(&path, n.appearance.effects.get(..index)?, &ctx);
|
||||
let (shadow, surfaces) = art_for(n, fx, &[Profile { path, rule, items: paints, opacity: 1.0, scopes: vec![] }])?;
|
||||
let (shadow, surfaces) = art_for(doc, n, fx, &[Profile { path, rule, items: paints, opacity: 1.0, scopes: vec![] }])?;
|
||||
children.extend(shadow.into_iter().chain(surfaces));
|
||||
// Later geometry effects act on the projected art as a whole.
|
||||
remaining = past_effect(&n.appearance.effects, index);
|
||||
@@ -994,7 +1188,7 @@ pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
for item in &paints {
|
||||
if let Some((index, fx)) = solid(item.effects()) {
|
||||
let p = geometry(&path, item.effects().get(..index)?, &ctx.item(item));
|
||||
let (shadow, surfaces) = art_for(n, fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0, scopes: vec![] }])?;
|
||||
let (shadow, surfaces) = art_for(doc, n, fx, &[Profile { path: p, rule, items: vec![item.clone()], opacity: 1.0, scopes: vec![] }])?;
|
||||
let later: Vec<Effect> = item.effects().iter().skip(index + 1).filter(|e| !is_three_d(&e.id)).cloned().collect();
|
||||
let with_later = |mut art: Arc<Node>| {
|
||||
Arc::make_mut(&mut art).appearance.effects = later.clone();
|
||||
@@ -1018,11 +1212,11 @@ pub fn solid_art_in(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
/// geometry effects before it reshape the members, those after it bend the result as a whole; the
|
||||
/// group's own fills and strokes are not drawn. `None` when the effect is only on one of the
|
||||
/// group's own fills or strokes, or when its members can't become solids.
|
||||
fn group_solid_art(n: &Node) -> Option<Node> {
|
||||
fn group_solid_art(doc: Option<&Document>, n: &Node) -> Option<Node> {
|
||||
let (index, fx) = solid(&n.appearance.effects)?;
|
||||
let mut shapes = members(n, "").ok()?;
|
||||
reshape_members(n, n.appearance.effects.get(..index)?, &mut shapes);
|
||||
let (shadow, surfaces) = art_for(n, fx, &shapes)?;
|
||||
let (shadow, surfaces) = art_for(doc, n, fx, &shapes)?;
|
||||
let children = shadow.into_iter().chain(surfaces).collect();
|
||||
let remaining = past_effect(&n.appearance.effects, index);
|
||||
let mut art = n.clone();
|
||||
@@ -1031,6 +1225,77 @@ fn group_solid_art(n: &Node) -> Option<Node> {
|
||||
Some(art)
|
||||
}
|
||||
|
||||
/// Where the graphics of the 3D effect of `n` (on the object itself with `item` `None`, else on its
|
||||
/// fill or stroke `item`) are drawn: the bounds its solids are built with and the maps of their
|
||||
/// planar faces ([`graphics::GraphicFrame`]). `None` when `n` has no visible Extrude & Bevel or
|
||||
/// Rotate there, or its art can't be built.
|
||||
pub fn graphic_frame(doc: Option<&Document>, n: &Node, item: Option<usize>) -> Option<graphics::GraphicFrame> {
|
||||
frame_of(doc, n, item, &solid)
|
||||
}
|
||||
|
||||
/// [`graphic_frame`] of the 3D effect at `index` in that list, which must be a visible Extrude &
|
||||
/// Bevel or Rotate.
|
||||
pub fn graphic_frame_at(doc: Option<&Document>, n: &Node, item: Option<usize>, index: usize) -> Option<graphics::GraphicFrame> {
|
||||
frame_of(doc, n, item, &|fx: &[Effect]| fx.get(index).filter(|e| e.visible && SOLIDS.contains(&e.id.as_str())).map(|e| (index, e)))
|
||||
}
|
||||
|
||||
/// Does a visible geometry effect come after the 3D effect at `index` in the effects of `n` (on the
|
||||
/// object itself with `item` `None`, else on its fill or stroke `item`)? Such an effect moves or
|
||||
/// bends the drawn art, graphics included, while [`graphic_frame_at`] places the graphics on the
|
||||
/// art before it.
|
||||
pub fn graphics_reshaped_after(n: &Node, item: Option<usize>, index: usize) -> bool {
|
||||
n.appearance.effects_at(item).and_then(|fx| fx.get(index + 1..)).is_some_and(crate::has_geometry)
|
||||
}
|
||||
|
||||
/// Finds a 3D effect in a list of effects, with its index there.
|
||||
type Pick<'p> = dyn for<'e> Fn(&'e [Effect]) -> Option<(usize, &'e Effect)> + 'p;
|
||||
|
||||
/// [`graphic_frame`] of the 3D effect `pick` finds in the effects of `n` or its `item`.
|
||||
fn frame_of(doc: Option<&Document>, n: &Node, item: Option<usize>, pick: &Pick<'_>) -> Option<graphics::GraphicFrame> {
|
||||
let n = &*resolve(doc, n, "").ok()?;
|
||||
let (fx, shapes) = if is_container(n) {
|
||||
let (index, fx) = pick(&n.appearance.effects).filter(|_| item.is_none())?;
|
||||
let mut shapes = members(n, "").ok()?;
|
||||
reshape_members(n, n.appearance.effects.get(..index)?, &mut shapes);
|
||||
(fx, shapes)
|
||||
} else {
|
||||
let (path, rule) = profile(n)?;
|
||||
let ctx = GeomContext::of(n);
|
||||
match item {
|
||||
None => {
|
||||
let (index, fx) = pick(&n.appearance.effects)?;
|
||||
let path = geometry(&path, n.appearance.effects.get(..index)?, &ctx);
|
||||
(fx, vec![Profile { path, rule, items: solid_paints(n), opacity: 1.0, scopes: vec![] }])
|
||||
}
|
||||
Some(k) => {
|
||||
let it = n.appearance.items.get(k)?;
|
||||
let (index, fx) = pick(it.effects())?;
|
||||
let path = geometry(&geometry(&path, &n.appearance.effects, &ctx), it.effects().get(..index)?, &ctx.item(it));
|
||||
(fx, vec![Profile { path, rule, items: vec![it.clone()], opacity: 1.0, scopes: vec![] }])
|
||||
}
|
||||
}
|
||||
};
|
||||
if !takes_graphics(&fx.id) {
|
||||
return None;
|
||||
}
|
||||
let p = crate::merged_params(&fx.id, &fx.params);
|
||||
let b = union_bounds(&shapes)?;
|
||||
let view = view_options(&p);
|
||||
if fx.id == ROTATE {
|
||||
let plane = vectorcraft_three_d::plane_face_map(b, view);
|
||||
return Some(graphics::GraphicFrame::new(b, plane, None));
|
||||
}
|
||||
let (_, bounds) = solid_parts(&shapes, b);
|
||||
let options = extrude_options(&p);
|
||||
let map = |face| vectorcraft_three_d::extrude_face_map(bounds, options, view, face);
|
||||
Some(graphics::GraphicFrame::new(bounds, map(PlanarFace::Front), map(PlanarFace::Back)))
|
||||
}
|
||||
|
||||
/// The bounds of the art symbol `name` of `doc` draws as a graphic, in the symbol's coordinates.
|
||||
pub fn graphic_bounds(doc: &Document, name: &str) -> Option<Rect> {
|
||||
graphics::art_bounds(doc, name)
|
||||
}
|
||||
|
||||
/// `effects` without the 3D effect at `index` and the geometry effects before it, which shape the
|
||||
/// art it projects. Each raster effect before it follows the Transform effects among those
|
||||
/// ([`crate::without_geometry`]), unless a Transform effect after it makes copies or moves at
|
||||
|
||||
@@ -0,0 +1,631 @@
|
||||
//! Graphics mapped onto a 3D object's planar faces (Window › 3D and Materials › Materials ›
|
||||
//! Graphics): the art of a document symbol drawn on Extrude & Bevel's front or back cap, or on a
|
||||
//! plane, turned, foreshortened and shaded with that face and clipped to it.
|
||||
//!
|
||||
//! The effect keeps its graphics in its `graphics` parameter, each `{symbol, face, x, y, scale,
|
||||
//! rotation}`: the symbol's name, the face, the offset of the graphic's center from the center of
|
||||
//! the object's bounds in the face's own plane (points, x to the right and y down as the face is
|
||||
//! seen from the front of that face), its size as a share of the symbol's own, and its turn
|
||||
//! (degrees, counterclockwise). On the back cap, x runs to the right as the back is seen from
|
||||
//! behind, so the graphic reads the right way round there.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
use vectorcraft_doc::color::{BlendMode, Paint};
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, Node, NodeId, NodeKind, Symbol};
|
||||
use vectorcraft_geom::kurbo::{CubicBez, ParamCurve, PathEl, Shape};
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule, PathData, Point, Rect, SubPath};
|
||||
use vectorcraft_three_d::{FaceMap, Facet, PlanarFace};
|
||||
|
||||
/// The effect parameter that holds the graphics.
|
||||
pub const GRAPHICS: &str = "graphics";
|
||||
|
||||
/// The most graphics one 3D effect maps.
|
||||
pub const MAX_GRAPHICS: usize = 64;
|
||||
|
||||
/// The most fills and stroke outlines the art of one symbol draws as a graphic.
|
||||
const MAX_PIECES: usize = 4096;
|
||||
|
||||
/// The most path elements the graphics of one 3D effect draw together, after a perspective cuts
|
||||
/// their curves.
|
||||
const MAX_ELEMENTS: usize = 400_000;
|
||||
|
||||
/// The pieces each curve of a graphic is cut into under a perspective map, which bends it.
|
||||
const PERSPECTIVE_PIECES: usize = 8;
|
||||
|
||||
/// How deep symbol instances in a graphic's art may nest.
|
||||
const MAX_NESTING: usize = 8;
|
||||
|
||||
/// How deep the objects of a graphic's art may be, counting groups and the art of the symbols it
|
||||
/// places: as deep as an object may be drawn.
|
||||
const MAX_LEVELS: usize = 4 * vectorcraft_doc::MAX_DEPTH;
|
||||
|
||||
/// The size range of a graphic, as a percentage of its symbol's own size.
|
||||
pub const SCALE_RANGE: (f64, f64) = (1.0, 10_000.0);
|
||||
|
||||
/// How far a graphic's center may be from the center of its object, in points.
|
||||
pub const MAX_OFFSET: f64 = 1e6;
|
||||
|
||||
/// One graphic mapped onto a face of a 3D object.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MappedGraphic {
|
||||
pub symbol: String,
|
||||
pub face: PlanarFace,
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
/// Percent of the symbol's own size.
|
||||
pub scale: f64,
|
||||
/// Degrees, counterclockwise.
|
||||
pub rotation: f64,
|
||||
}
|
||||
|
||||
impl MappedGraphic {
|
||||
pub fn to_value(&self) -> Value {
|
||||
json!({"symbol": self.symbol, "face": self.face.id(), "x": self.x, "y": self.y, "scale": self.scale, "rotation": self.rotation})
|
||||
}
|
||||
|
||||
/// The graphic `v` describes. Only `symbol` is required: `face` defaults to front, the offsets
|
||||
/// and the rotation to 0 and the scale to 100.
|
||||
pub fn from_value(v: &Value) -> Result<Self, String> {
|
||||
let Value::Object(m) = v else { return Err(format!("each of `{GRAPHICS}` must be an object")) };
|
||||
if let Some(k) = m.keys().find(|k| !matches!(k.as_str(), "symbol" | "face" | "x" | "y" | "scale" | "rotation")) {
|
||||
return Err(format!("`{GRAPHICS}` entries take symbol, face, x, y, scale and rotation, not `{k}`"));
|
||||
}
|
||||
let symbol = match m.get("symbol") {
|
||||
Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
|
||||
_ => return Err(format!("each of `{GRAPHICS}` needs `symbol`, the name of a symbol")),
|
||||
};
|
||||
let face = match m.get("face") {
|
||||
None => PlanarFace::Front,
|
||||
Some(v) => v.as_str().and_then(PlanarFace::parse).ok_or_else(|| format!("`{GRAPHICS}` face must be front or back"))?,
|
||||
};
|
||||
let number = |key: &str, default: f64, (lo, hi): (f64, f64)| -> Result<f64, String> {
|
||||
match m.get(key) {
|
||||
None => Ok(default),
|
||||
Some(v) => v
|
||||
.as_f64()
|
||||
.filter(|n| n.is_finite() && (lo..=hi).contains(n))
|
||||
.ok_or_else(|| format!("`{GRAPHICS}` {key} must be a number from {lo} to {hi}")),
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
symbol,
|
||||
face,
|
||||
x: number("x", 0.0, (-MAX_OFFSET, MAX_OFFSET))?,
|
||||
y: number("y", 0.0, (-MAX_OFFSET, MAX_OFFSET))?,
|
||||
scale: number("scale", 100.0, SCALE_RANGE)?,
|
||||
rotation: number("rotation", 0.0, (-360.0, 360.0))?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Where the graphic's art goes in the face's plane, for symbol art whose bounds are `art` on an
|
||||
/// object whose bounds have center `center`.
|
||||
fn placement(&self, art: Rect, center: Point) -> Affine {
|
||||
let place = Affine::translate((center.x + self.x, center.y + self.y))
|
||||
* Affine::rotate(-self.rotation.to_radians())
|
||||
* Affine::scale(self.scale / 100.0)
|
||||
* Affine::translate(-art.center().to_vec2());
|
||||
match self.face {
|
||||
PlanarFace::Front => place,
|
||||
// Seen from behind, the back's right is the front's left.
|
||||
PlanarFace::Back => {
|
||||
Affine::translate(center.to_vec2()) * Affine::scale_non_uniform(-1.0, 1.0) * Affine::translate(-center.to_vec2()) * place
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The 3D effects that take graphics: Extrude & Bevel (its caps) and Rotate (the plane), the two
|
||||
/// object types of the 3D and Materials panel with planar faces.
|
||||
pub fn takes_graphics(id: &str) -> bool {
|
||||
id == super::EXTRUDE || id == super::ROTATE
|
||||
}
|
||||
|
||||
/// The graphics in parameters `p`, leaving out entries that are not valid (a file may hold
|
||||
/// anything; commands check every entry with [`validate`]).
|
||||
pub fn mapped_graphics(p: &Value) -> Vec<MappedGraphic> {
|
||||
match p.get(GRAPHICS) {
|
||||
Some(Value::Array(list)) => list.iter().take(MAX_GRAPHICS).filter_map(|v| MappedGraphic::from_value(v).ok()).collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Are the graphics in parameters `p` of 3D effect `id` valid? Only Extrude & Bevel and Rotate take
|
||||
/// graphics, and a plane has only a front.
|
||||
pub fn validate(id: &str, p: &Value) -> Result<(), String> {
|
||||
let list = match p.get(GRAPHICS) {
|
||||
None => return Ok(()),
|
||||
Some(Value::Array(list)) => list,
|
||||
Some(_) => return Err(format!("`{GRAPHICS}` must be an array")),
|
||||
};
|
||||
if list.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if !takes_graphics(id) {
|
||||
return Err("Graphics map onto planar faces: use the Extrude or Plane object type".into());
|
||||
}
|
||||
if list.len() > MAX_GRAPHICS {
|
||||
return Err(format!("`{GRAPHICS}` takes at most {MAX_GRAPHICS} graphics"));
|
||||
}
|
||||
for v in list {
|
||||
let g = MappedGraphic::from_value(v)?;
|
||||
if id == super::ROTATE && g.face == PlanarFace::Back {
|
||||
return Err("A plane has one face: map its graphics onto the front".into());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The first symbol the graphics in `p` name that `doc` does not have, leaving out the symbols the
|
||||
/// graphics in `old` (the effect's parameters before an edit) already name: a graphic whose symbol
|
||||
/// was deleted may stay.
|
||||
pub fn missing_symbol(doc: &Document, p: &Value, old: Option<&Value>) -> Option<String> {
|
||||
let kept = old.map(mapped_graphics).unwrap_or_default();
|
||||
mapped_graphics(p)
|
||||
.into_iter()
|
||||
.map(|g| g.symbol)
|
||||
.find(|name| !doc.symbols.iter().any(|s| s.name == *name) && !kept.iter().any(|g| g.symbol == *name))
|
||||
}
|
||||
|
||||
/// Does the art of the symbols the graphics in `p` name fit the limits? A graphic whose symbol
|
||||
/// `doc` does not have draws nothing and is not counted. With a perspective, each curve counts as
|
||||
/// the [`PERSPECTIVE_PIECES`] pieces it is cut into; without one, it counts once.
|
||||
pub fn validate_symbols(doc: &Document, p: &Value) -> Result<(), String> {
|
||||
let curve = if super::view_options(p).perspective > 0.0 { PERSPECTIVE_PIECES } else { 1 };
|
||||
let mut elements = 0usize;
|
||||
for g in mapped_graphics(p) {
|
||||
let Some(symbol) = doc.symbols.iter().find(|s| s.name == g.symbol) else { continue };
|
||||
let pieces = pieces_of(doc, symbol)?;
|
||||
elements = elements.saturating_add(pieces.iter().map(|p| drawn_elements(&p.path, curve)).sum());
|
||||
if elements > MAX_ELEMENTS {
|
||||
return Err("The graphics are too complex: simplify the symbols' art".into());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The path elements `path` draws when each curve draws `curve` elements, before curves near the
|
||||
/// eye are split further: one for each straight line, move and close.
|
||||
fn drawn_elements(path: &PathData, curve: usize) -> usize {
|
||||
let segments = |s: &SubPath| (0..s.segment_count()).map(|i| if s.segment_is_line(i) { 1 } else { curve }).sum::<usize>();
|
||||
path.subpaths.iter().map(|s| 1 + usize::from(s.closed) + segments(s)).sum()
|
||||
}
|
||||
|
||||
/// One area of a graphic's art, drawn with one paint: a fill, or a stroke's outline.
|
||||
struct Piece {
|
||||
path: PathData,
|
||||
rule: FillRule,
|
||||
paint: Paint,
|
||||
opacity: f32,
|
||||
blend: BlendMode,
|
||||
}
|
||||
|
||||
/// The areas the art of `symbol` paints, back to front, at the symbol's natural size: the fills of
|
||||
/// its paths, compound paths and type (as outlines), and its strokes' outlines, through the symbol
|
||||
/// instances it holds (with their changes to dynamic symbols, and through 9-slice guides). Hidden objects, guides, clipping paths and images are left out, and clipping
|
||||
/// groups draw their art unclipped.
|
||||
fn pieces_of(doc: &Document, symbol: &Symbol) -> Result<Vec<Piece>, String> {
|
||||
fn walk(doc: &Document, n: &Node, outer: f32, xf: Affine, (depth, level): (usize, usize), out: &mut Vec<Piece>) -> Result<(), String> {
|
||||
if !n.visible || n.is_guide() {
|
||||
return Ok(());
|
||||
}
|
||||
if level > MAX_LEVELS {
|
||||
return Err(format!("A graphic's art may be nested at most {MAX_LEVELS} levels deep: simplify the symbol's art"));
|
||||
}
|
||||
let opacity = outer * n.opacity;
|
||||
let shape = match &n.kind {
|
||||
NodeKind::Path { path, rule, clipping: false, .. } => Some((path.clone(), *rule)),
|
||||
NodeKind::Compound { children, rule } => {
|
||||
Some((PathData::new(children.iter().filter_map(|c| c.path_data()).flat_map(|p| p.subpaths.iter().cloned()).collect()), *rule))
|
||||
}
|
||||
NodeKind::Text(_) => {
|
||||
if let Some(mut outlined) = crate::outline_text(n) {
|
||||
// The outlines take the type's place, with its opacity.
|
||||
outlined.visible = true;
|
||||
outlined.opacity = n.opacity;
|
||||
walk(doc, &outlined, outer, xf, (depth, level + 1), out)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
NodeKind::SymbolInstance { symbol, xf: at } => {
|
||||
if let Some(s) = doc.symbols.iter().find(|s| s.name == *symbol) {
|
||||
if depth >= MAX_NESTING {
|
||||
return Err(format!("A graphic's art may nest symbols at most {MAX_NESTING} levels deep: simplify the symbol's art"));
|
||||
}
|
||||
// The instance shows its own changes to a dynamic symbol's art, and a symbol
|
||||
// with 9-slice guides is placed through them, as the canvas draws it. The
|
||||
// pieces are not clipped, so paths that cross a guide are cut at it.
|
||||
let art = vectorcraft_doc::symbol_overrides::dynamic_art(&s.art, n);
|
||||
if doc.symbol_slice_map(symbol, *at).is_some() {
|
||||
let Some(placed) = doc.place_symbol_art_unclipped(symbol, &art, *at) else {
|
||||
return Err("A graphic's art holds too many objects: simplify the symbol's art".into());
|
||||
};
|
||||
walk(doc, &placed, opacity, xf, (depth + 1, level + 1), out)?;
|
||||
} else {
|
||||
walk(doc, &art, opacity, xf * *at, (depth + 1, level + 1), out)?;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
_ => {
|
||||
for c in n.children().into_iter().flatten() {
|
||||
walk(doc, c, opacity, xf, (depth, level + 1), out)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
};
|
||||
let Some((path, rule)) = shape else { return Ok(()) };
|
||||
// Symbol art keeps its strokes' widths as drawn while its geometry is scaled to the
|
||||
// symbol's square, so a stroke is outlined after the art takes its size.
|
||||
let path = path.transformed(xf);
|
||||
for item in n.appearance.items.iter().filter(|i| i.visible()) {
|
||||
let (area, rule, paint, item_opacity, blend) = match item {
|
||||
AppearanceItem::Fill(f) => (path.clone(), rule, f.paint.clone(), f.opacity, f.blend),
|
||||
AppearanceItem::Stroke(s) if s.width > 0.0 && s.width.is_finite() => {
|
||||
(crate::stroke::outline_region(&path, rule, s), FillRule::EvenOdd, s.paint.clone(), s.opacity, s.blend)
|
||||
}
|
||||
AppearanceItem::Stroke(_) => continue,
|
||||
};
|
||||
if matches!(paint, Paint::None) || area.bounds().is_none() {
|
||||
continue;
|
||||
}
|
||||
if out.len() >= MAX_PIECES {
|
||||
return Err(format!("A graphic draws at most {MAX_PIECES} fills and strokes: simplify the symbol's art"));
|
||||
}
|
||||
// A placed gradient or pattern takes the same size and place as its path.
|
||||
let mut paint = paint;
|
||||
match &mut paint {
|
||||
Paint::Gradient(g) => g.transform(xf),
|
||||
Paint::Pattern { xf: placed, .. } => *placed = xf * *placed,
|
||||
Paint::None | Paint::Solid { .. } => {}
|
||||
}
|
||||
out.push(Piece { path: area, rule, paint, opacity: opacity * item_opacity, blend });
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
let mut out = vec![];
|
||||
walk(doc, &symbol.art, 1.0, doc.symbol_natural_xf(&symbol.name), (0, 1), &mut out)?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `path` through `map`: exactly for an affine map, otherwise with each curve cut into
|
||||
/// [`PERSPECTIVE_PIECES`] pieces whose control points go through it (straight lines stay straight
|
||||
/// under a perspective). Fails when part of it has no page point, or when it takes more than `room`
|
||||
/// elements.
|
||||
fn map_path(path: &PathData, map: &FaceMap, affine: Option<Affine>, room: usize) -> Result<BezPath, NotMapped> {
|
||||
let bp = path.to_bezpath();
|
||||
if let Some(a) = affine {
|
||||
return if bp.elements().len() <= room { Ok(a * bp) } else { Err(NotMapped::Full) };
|
||||
}
|
||||
let to = |p: Point| map.to_page(p).ok_or(NotMapped::Behind);
|
||||
let mut out = BezPath::new();
|
||||
let mut at = Point::ZERO;
|
||||
for el in bp.elements() {
|
||||
if out.elements().len() > room {
|
||||
return Err(NotMapped::Full);
|
||||
}
|
||||
match *el {
|
||||
PathEl::MoveTo(p) => {
|
||||
out.move_to(to(p)?);
|
||||
at = p;
|
||||
}
|
||||
PathEl::LineTo(p) => {
|
||||
out.line_to(to(p)?);
|
||||
at = p;
|
||||
}
|
||||
PathEl::QuadTo(c, p) => {
|
||||
cubic_to(&mut out, vectorcraft_geom::kurbo::QuadBez::new(at, c, p).raise(), map).ok_or(NotMapped::Behind)?;
|
||||
at = p;
|
||||
}
|
||||
PathEl::CurveTo(c1, c2, p) => {
|
||||
cubic_to(&mut out, CubicBez::new(at, c1, c2, p), map).ok_or(NotMapped::Behind)?;
|
||||
at = p;
|
||||
}
|
||||
PathEl::ClosePath => out.close_path(),
|
||||
}
|
||||
}
|
||||
if out.elements().len() <= room { Ok(out) } else { Err(NotMapped::Full) }
|
||||
}
|
||||
|
||||
/// Why [`map_path`] drew nothing.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum NotMapped {
|
||||
/// Part of the path has no page point: it is at or behind the eye.
|
||||
Behind,
|
||||
/// The path takes more elements than it has room for.
|
||||
Full,
|
||||
}
|
||||
|
||||
/// Curve `c` through `map`, cut into [`PERSPECTIVE_PIECES`] pieces. A piece that lies in front of
|
||||
/// the eye while a control point does not is split further ([`piece_to`]). `None` when part of a
|
||||
/// piece has no page point.
|
||||
fn cubic_to(out: &mut BezPath, c: CubicBez, map: &FaceMap) -> Option<()> {
|
||||
for k in 0..PERSPECTIVE_PIECES {
|
||||
let (t0, t1) = (k as f64 / PERSPECTIVE_PIECES as f64, (k + 1) as f64 / PERSPECTIVE_PIECES as f64);
|
||||
let s = c.subsegment(t0..t1);
|
||||
if !map.maps_curve([s.p0, s.p1, s.p2, s.p3]) {
|
||||
return None;
|
||||
}
|
||||
piece_to(out, s, map, MAX_SPLITS)?;
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// How many times [`piece_to`] halves a piece whose control points are behind the eye.
|
||||
const MAX_SPLITS: usize = 4;
|
||||
|
||||
/// Piece `s`, which lies in front of the eye, through `map`: halved until the control points of
|
||||
/// each half have page points, at most `splits` times, and drawn straight past that.
|
||||
fn piece_to(out: &mut BezPath, s: CubicBez, map: &FaceMap, splits: usize) -> Option<()> {
|
||||
let end = map.to_page(s.p3)?;
|
||||
match (map.to_page(s.p1), map.to_page(s.p2)) {
|
||||
(Some(c1), Some(c2)) => out.curve_to(c1, c2, end),
|
||||
_ if splits == 0 => out.line_to(end),
|
||||
_ => {
|
||||
let (a, b) = s.subdivide();
|
||||
piece_to(out, a, map, splits - 1)?;
|
||||
piece_to(out, b, map, splits - 1)?;
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// The graphics `graphics` lists for `face`, drawn on caps `caps` (the faces of that end of the
|
||||
/// object's solids that the viewer sees) through `map`, for an object whose bounds have
|
||||
/// center `center`: one clipping group of every such graphic, shaded with the first cap's light.
|
||||
/// Pattern tiles keep their colors: the document's pattern draws them. A graphic whose symbol
|
||||
/// `doc` does not have draws nothing. `elements` counts the path elements
|
||||
/// the effect's graphics have drawn so far, after a perspective cuts their curves, and is shared by
|
||||
/// every call for one effect. `None` when there is nothing to draw or that count passes
|
||||
/// [`MAX_ELEMENTS`].
|
||||
pub(super) fn face_art(
|
||||
doc: Option<&Document>,
|
||||
id: NodeId,
|
||||
graphics: &[MappedGraphic],
|
||||
(face, map): (PlanarFace, &FaceMap),
|
||||
center: Point,
|
||||
caps: &[&Facet],
|
||||
elements: &mut usize,
|
||||
) -> Option<Node> {
|
||||
let doc = doc?;
|
||||
let light = caps.first()?.illumination;
|
||||
let affine = map.local_affine(center).filter(|_| map.is_affine());
|
||||
let mut children = vec![];
|
||||
for g in graphics.iter().filter(|g| g.face == face) {
|
||||
let Some(symbol) = doc.symbols.iter().find(|s| s.name == g.symbol) else { continue };
|
||||
// A symbol past the limits is left out; the other graphics still draw.
|
||||
let Ok(pieces) = pieces_of(doc, symbol) else { continue };
|
||||
let Some(bounds) = pieces.iter().filter_map(|p| p.path.bounds()).reduce(|a, b| a.union(b)) else { continue };
|
||||
let place = g.placement(bounds, center);
|
||||
// Gradients follow the face's map as it is near the graphic's center.
|
||||
let middle = place * bounds.center();
|
||||
// When the center is at or behind the eye, they follow it near the middle of the part that
|
||||
// is in front; a graphic with no part in front is left out.
|
||||
let local = map.local_affine(middle).or_else(|| {
|
||||
let part = map.mapped_part(place.transform_rect_bbox(bounds))?;
|
||||
let sum = part.iter().fold(Point::ZERO, |s, p| s + p.to_vec2());
|
||||
map.local_affine(Point::new(sum.x / part.len() as f64, sum.y / part.len() as f64))
|
||||
});
|
||||
let Some(local) = local else { continue };
|
||||
let gradient_map = local * place;
|
||||
for piece in pieces {
|
||||
let placed = piece.path.transformed(place);
|
||||
// Each anchor draws at least one element. A perspective cuts curves into more, and
|
||||
// straight lines stay one each: the path may take what is left of the limit, and is
|
||||
// counted as it is drawn. Past the limit, the face draws nothing.
|
||||
let least: usize = placed.subpaths.iter().map(|s| s.anchors.len()).sum();
|
||||
let before = *elements;
|
||||
if before.saturating_add(least) > MAX_ELEMENTS {
|
||||
return None;
|
||||
}
|
||||
let room = MAX_ELEMENTS - before;
|
||||
let mut rule = piece.rule;
|
||||
let mapped = match map_path(&placed, map, affine, room) {
|
||||
Err(NotMapped::Behind) => {
|
||||
// Part of it is at or behind the eye: the part in front is drawn.
|
||||
let Some(part) = placed.bounds().and_then(|b| map.mapped_part(b)) else { continue };
|
||||
let Some(&first) = part.first() else { continue };
|
||||
let mut front = BezPath::new();
|
||||
front.move_to(first);
|
||||
part.iter().skip(1).for_each(|p| front.line_to(*p));
|
||||
front.close_path();
|
||||
let cut = vectorcraft_pathops::boolean(
|
||||
&placed,
|
||||
piece.rule,
|
||||
&PathData::from_bezpath(&front),
|
||||
FillRule::NonZero,
|
||||
vectorcraft_pathops::BoolOp::Intersect,
|
||||
);
|
||||
rule = FillRule::NonZero;
|
||||
map_path(&cut, map, affine, room)
|
||||
}
|
||||
done => done,
|
||||
};
|
||||
let mapped = match mapped {
|
||||
Ok(mapped) => mapped,
|
||||
Err(NotMapped::Full) => return None,
|
||||
Err(NotMapped::Behind) => continue,
|
||||
};
|
||||
*elements = before.saturating_add(mapped.elements().len());
|
||||
let mut paint = piece.paint;
|
||||
vectorcraft_doc::recolor::map_paint(&mut paint, &|c| crate::lighting::shade(c, light));
|
||||
// A pattern's tiles follow the graphic onto the face, as its gradients do below.
|
||||
if let Paint::Pattern { xf, .. } = &mut paint {
|
||||
*xf = gradient_map * *xf;
|
||||
}
|
||||
let mut fill = FillLayer::new(paint);
|
||||
fill.opacity = piece.opacity;
|
||||
fill.blend = piece.blend;
|
||||
let mut appearance = Appearance { items: vec![AppearanceItem::Fill(fill)], ..Default::default() };
|
||||
appearance.transform_gradients(gradient_map);
|
||||
let mut node = Node::path(id, PathData::from_bezpath(&mapped), appearance);
|
||||
if let NodeKind::Path { rule: r, .. } = &mut node.kind {
|
||||
*r = rule;
|
||||
}
|
||||
children.push(Arc::new(node));
|
||||
}
|
||||
}
|
||||
if children.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut clip = BezPath::new();
|
||||
for cap in caps {
|
||||
// A cap's holes are contours of the same path wound against its outline, so turning the
|
||||
// whole path keeps them holes under the nonzero rule.
|
||||
let mut outline = cap.path().to_bezpath();
|
||||
if outline.area() < 0.0 {
|
||||
outline = outline.reverse_subpaths();
|
||||
}
|
||||
clip.extend(outline.iter());
|
||||
}
|
||||
let clip = Node::path(id, PathData::from_bezpath(&clip), Appearance::default());
|
||||
Some(Node::new(id, NodeKind::Group { children: vec![Arc::new(clip), Arc::new(Node::group(id, children))], clip: true }))
|
||||
}
|
||||
|
||||
/// The bounds of the art symbol `name` of `doc` draws as a graphic, in the symbol's coordinates.
|
||||
pub(super) fn art_bounds(doc: &Document, name: &str) -> Option<Rect> {
|
||||
let symbol = doc.symbols.iter().find(|s| s.name == name)?;
|
||||
pieces_of(doc, symbol).ok()?.iter().filter_map(|p| p.path.bounds()).reduce(|a, b| a.union(b))
|
||||
}
|
||||
|
||||
/// Where a 3D object's graphics are drawn: the bounds its solids are built with (the graphics'
|
||||
/// offsets are from their center) and the maps of its front and back faces, when it has them.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct GraphicFrame {
|
||||
pub bounds: Rect,
|
||||
front: Option<FaceMap>,
|
||||
back: Option<FaceMap>,
|
||||
}
|
||||
|
||||
impl GraphicFrame {
|
||||
pub(super) fn new(bounds: Rect, front: Option<FaceMap>, back: Option<FaceMap>) -> Self {
|
||||
Self { bounds, front, back }
|
||||
}
|
||||
|
||||
pub fn map(&self, face: PlanarFace) -> Option<&FaceMap> {
|
||||
match face {
|
||||
PlanarFace::Front => self.front.as_ref(),
|
||||
PlanarFace::Back => self.back.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The point of `face`'s plane that offset `(x, y)` names, as [`MappedGraphic`] places a
|
||||
/// graphic's center.
|
||||
fn art_point(&self, face: PlanarFace, (x, y): (f64, f64)) -> Point {
|
||||
let c = self.bounds.center();
|
||||
match face {
|
||||
PlanarFace::Front => Point::new(c.x + x, c.y + y),
|
||||
PlanarFace::Back => Point::new(c.x - x, c.y + y),
|
||||
}
|
||||
}
|
||||
|
||||
/// Where graphic `g`'s center lands on the page, and whether its face is turned toward the
|
||||
/// viewer. `None` when the object has no such face or the point has no page point.
|
||||
pub fn handle(&self, g: &MappedGraphic) -> Option<(Point, bool)> {
|
||||
let map = self.map(g.face)?;
|
||||
Some((map.to_page(self.art_point(g.face, (g.x, g.y)))?, map.visible))
|
||||
}
|
||||
|
||||
/// The offset of a graphic on `face` whose center lands on page point `page`.
|
||||
pub fn offset_at(&self, face: PlanarFace, page: Point) -> Option<(f64, f64)> {
|
||||
let art = self.map(face)?.to_art(page)?;
|
||||
let c = self.bounds.center();
|
||||
let (x, y) = match face {
|
||||
PlanarFace::Front => (art.x - c.x, art.y - c.y),
|
||||
PlanarFace::Back => (c.x - art.x, art.y - c.y),
|
||||
};
|
||||
(x.abs() <= MAX_OFFSET && y.abs() <= MAX_OFFSET).then_some((x, y))
|
||||
}
|
||||
|
||||
/// The scale (percent) at which art with bounds `art` fits inside the object's bounds, at most
|
||||
/// 100: where a newly mapped graphic starts.
|
||||
pub fn fitting_scale(&self, art: Rect) -> f64 {
|
||||
let fit = (self.bounds.width() / art.width().max(1e-9)).min(self.bounds.height() / art.height().max(1e-9));
|
||||
(fit * 100.0).clamp(SCALE_RANGE.0, 100.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The symbols the graphics of `n`'s 3D effects name (its own effects and its fills' and strokes').
|
||||
pub fn graphic_symbols(n: &Node) -> Vec<String> {
|
||||
appearance_graphic_symbols(&n.appearance)
|
||||
}
|
||||
|
||||
/// The symbols the graphics of the 3D effects in `appearance` name, as [`graphic_symbols`]: for a
|
||||
/// graphic style's appearance.
|
||||
pub fn appearance_graphic_symbols(appearance: &Appearance) -> Vec<String> {
|
||||
std::iter::once(&appearance.effects)
|
||||
.chain(appearance.items.iter().map(AppearanceItem::effects))
|
||||
.flatten()
|
||||
.filter(|e| takes_graphics(&e.id))
|
||||
.flat_map(|e| mapped_graphics(&e.params))
|
||||
.map(|g| g.symbol)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Makes the graphics of `n`'s 3D effects that name symbol `from` name `to` instead. Returns
|
||||
/// whether any changed.
|
||||
pub fn rename_graphic_symbol(n: &mut Node, from: &str, to: &str) -> bool {
|
||||
// Checked first, so an appearance that names another symbol is not copied.
|
||||
graphic_symbols(n).iter().any(|s| s == from) && rename_appearance_graphic_symbol(&mut n.appearance, from, to)
|
||||
}
|
||||
|
||||
/// [`rename_graphic_symbol`] for the 3D effects in `appearance`: for a graphic style's appearance.
|
||||
pub fn rename_appearance_graphic_symbol(appearance: &mut Appearance, from: &str, to: &str) -> bool {
|
||||
if !appearance_graphic_symbols(appearance).iter().any(|s| s == from) {
|
||||
return false;
|
||||
}
|
||||
let mut lists: Vec<&mut Vec<vectorcraft_doc::Effect>> = vec![];
|
||||
let (effects, items) = (&mut appearance.effects, &mut appearance.items);
|
||||
lists.push(effects);
|
||||
lists.extend(items.iter_mut().map(AppearanceItem::effects_mut));
|
||||
for e in lists.into_iter().flatten().filter(|e| takes_graphics(&e.id)) {
|
||||
if let Some(Value::Array(list)) = e.params.get_mut(GRAPHICS) {
|
||||
for g in list.iter_mut() {
|
||||
if g.get("symbol").and_then(Value::as_str) == Some(from) {
|
||||
g["symbol"] = json!(to);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Does `n` or any object in it, opacity masks included, have a graphic that names symbol `from`?
|
||||
pub fn names_graphic_symbol(n: &Node, from: &str) -> bool {
|
||||
let mut found = false;
|
||||
vectorcraft_doc::swatches::walk_with_masks(n, &mut |c| found |= graphic_symbols(c).iter().any(|s| s == from));
|
||||
found
|
||||
}
|
||||
|
||||
/// [`rename_graphic_symbol`] for `n` and every object in it, at any depth, opacity masks included.
|
||||
/// Only the parts that name `from` are copied. Returns whether any changed.
|
||||
pub fn rename_graphic_symbol_in(n: &mut Node, from: &str, to: &str) -> bool {
|
||||
let names = names_graphic_symbol;
|
||||
let mut changed = rename_graphic_symbol(n, from, to);
|
||||
if let Some(mask) = n.extras_if_any().and_then(|x| x.mask.as_deref_mut()).filter(|m| names(&m.art, from)) {
|
||||
changed |= rename_graphic_symbol_in(Arc::make_mut(&mut mask.art), from, to);
|
||||
}
|
||||
if let Some(children) = n.children_mut() {
|
||||
for c in children.iter_mut().filter(|c| names(c, from)) {
|
||||
changed |= rename_graphic_symbol_in(Arc::make_mut(c), from, to);
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
/// `p` with graphic `g` added at the end of its list.
|
||||
pub fn with_graphic(p: &Value, g: &MappedGraphic) -> Value {
|
||||
let mut out = p.as_object().cloned().unwrap_or_else(Map::new);
|
||||
let mut list = match out.get(GRAPHICS) {
|
||||
Some(Value::Array(list)) => list.clone(),
|
||||
_ => vec![],
|
||||
};
|
||||
list.push(g.to_value());
|
||||
out.insert(GRAPHICS.into(), Value::Array(list));
|
||||
Value::Object(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tests_graphics.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,659 @@
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::color::Color;
|
||||
use vectorcraft_doc::{StrokeLayer, Symbol};
|
||||
use vectorcraft_geom::shapes;
|
||||
|
||||
use super::super::{EXTRUDE, INFLATE, ROTATE, graphic_frame, solid_art_in, three_d_params_as, validate_three_d_in, validate_three_d_params};
|
||||
use super::*;
|
||||
|
||||
fn red() -> Color {
|
||||
Color::rgb(1.0, 0.0, 0.0)
|
||||
}
|
||||
|
||||
fn square(id: u64, r: Rect, paint: Paint) -> Node {
|
||||
Node::path(NodeId(id), shapes::rectangle(r), Appearance::basic(paint, Paint::None, 0.0))
|
||||
}
|
||||
|
||||
/// A document with symbol "Mark": a red 10 × 10 square.
|
||||
fn document() -> Document {
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
d.symbols.push(Symbol {
|
||||
dynamic: false,
|
||||
name: "Mark".into(),
|
||||
art: Arc::new(square(0, Rect::new(500.0, 500.0, 510.0, 510.0), Paint::solid(red()))),
|
||||
});
|
||||
d
|
||||
}
|
||||
|
||||
/// A gray 100 × 100 square at (0, 0) with effect `id`, its parameters `params` over a view
|
||||
/// straight on.
|
||||
fn object(id: &str, params: Value) -> Node {
|
||||
let mut p = json!({"rotationX": 0.0, "rotationY": 0.0, "rotationZ": 0.0, "shade": false});
|
||||
if let (Some(p), Some(extra)) = (p.as_object_mut(), params.as_object()) {
|
||||
p.extend(extra.clone());
|
||||
}
|
||||
let mut n = square(7, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)));
|
||||
n.appearance.effects.push(crate::new_effect(id, &p).unwrap());
|
||||
n
|
||||
}
|
||||
|
||||
/// The graphics clip groups in `art`, in drawing order: each one's mapped paths.
|
||||
fn graphics_in(art: &Node) -> Vec<Vec<(PathData, Paint)>> {
|
||||
let mut out = vec![];
|
||||
art.walk(&mut |n| {
|
||||
if let NodeKind::Group { children, clip: true } = &n.kind
|
||||
&& let [_, inner] = children.as_slice()
|
||||
&& let NodeKind::Group { children: paths, clip: false } = &inner.kind
|
||||
{
|
||||
let paths: Vec<(PathData, Paint)> = paths
|
||||
.iter()
|
||||
.filter_map(|p| {
|
||||
let fill = p.appearance.items.iter().find_map(|i| match i {
|
||||
AppearanceItem::Fill(f) => Some(f.paint.clone()),
|
||||
AppearanceItem::Stroke(_) => None,
|
||||
})?;
|
||||
Some((p.path_data()?.clone(), fill))
|
||||
})
|
||||
.collect();
|
||||
if paths.iter().any(|(_, p)| p.color().is_some_and(|c| c.to_rgb()[1] < 0.1 && c.to_rgb()[0] > 0.3)) {
|
||||
out.push(paths);
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
fn bounds_of(paths: &[(PathData, Paint)]) -> Rect {
|
||||
paths.iter().filter_map(|(p, _)| p.bounds()).reduce(|a, b| a.union(b)).unwrap()
|
||||
}
|
||||
|
||||
fn close(a: Rect, b: Rect) -> bool {
|
||||
[(a.x0, b.x0), (a.y0, b.y0), (a.x1, b.x1), (a.y1, b.y1)].iter().all(|(p, q)| (p - q).abs() < 1e-6)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_graphic_is_drawn_on_the_front_cap_at_its_offset_size_and_turn() {
|
||||
let d = document();
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Mark", "x": 20.0, "y": -10.0, "scale": 200.0}]}));
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let found = graphics_in(&art);
|
||||
assert_eq!(found.len(), 1);
|
||||
// Straight on, the front cap is the art: a 20 × 20 square centered 20 right of and 10 above (50, 50).
|
||||
assert!(close(bounds_of(&found[0]), Rect::new(60.0, 30.0, 80.0, 50.0)), "{:?}", bounds_of(&found[0]));
|
||||
// Turned 45°, the square's corners reach out to its half diagonal.
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Mark", "rotation": 45.0}]}));
|
||||
let b = bounds_of(&graphics_in(&solid_art_in(Some(&d), &n).unwrap())[0]);
|
||||
assert!((b.width() - 10.0 * 2f64.sqrt()).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_the_document_or_its_symbol_nothing_is_drawn() {
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Mark"}]}));
|
||||
let art = solid_art_in(None, &n).unwrap();
|
||||
assert!(graphics_in(&art).is_empty());
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Gone"}]}));
|
||||
assert!(graphics_in(&solid_art_in(Some(&document()), &n).unwrap()).is_empty());
|
||||
// Without graphics, the art is the same as before they existed.
|
||||
let plain = object(EXTRUDE, json!({}));
|
||||
assert_eq!(solid_art_in(Some(&document()), &plain), solid_art_in(None, &plain));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_graphic_is_shaded_with_its_face() {
|
||||
let d = document();
|
||||
let n = object(EXTRUDE, json!({"shade": true, "lightIntensity": 0.0, "ambient": 50.0, "graphics": [{"symbol": "Mark"}]}));
|
||||
let found = graphics_in(&solid_art_in(Some(&d), &n).unwrap());
|
||||
let c = found[0][0].1.color().unwrap().to_rgb();
|
||||
assert!((c[0] - 0.5).abs() < 1e-3 && c[1].abs() < 1e-6 && c[2].abs() < 1e-6, "{c:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_graphic_goes_right_after_its_cap_and_is_clipped_to_it() {
|
||||
let d = document();
|
||||
let n = object(EXTRUDE, json!({"rotationX": -20.0, "rotationY": 25.0, "graphics": [{"symbol": "Mark", "scale": 2000.0}]}));
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let NodeKind::Group { children, .. } = &art.kind else { panic!("a group") };
|
||||
let NodeKind::Group { children: faces, .. } = &children.last().unwrap().kind else { panic!("the ordered faces") };
|
||||
let last = faces.last().unwrap();
|
||||
assert!(matches!(&last.kind, NodeKind::Group { clip: true, .. }), "the graphic is drawn last, over the front cap");
|
||||
let NodeKind::Group { children: parts, .. } = &last.kind else { panic!() };
|
||||
let clip = parts[0].path_data().unwrap().bounds().unwrap();
|
||||
let cap = faces[faces.len() - 2].path_data().unwrap().bounds().unwrap();
|
||||
assert!(close(clip, cap), "clipped to the cap: {clip:?} against {cap:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_back_cap_reads_the_right_way_round_from_behind() {
|
||||
let d = document();
|
||||
let behind = json!({"rotationY": 180.0, "graphics": [{"symbol": "Mark", "face": "back", "x": 20.0}]});
|
||||
let n = object(EXTRUDE, behind);
|
||||
let found = graphics_in(&solid_art_in(Some(&d), &n).unwrap());
|
||||
assert_eq!(found.len(), 1);
|
||||
let b = bounds_of(&found[0]);
|
||||
// Seen from behind, x runs to the right on the page too.
|
||||
assert!((b.center().x - 70.0).abs() < 1e-6, "{b:?}");
|
||||
let frame = graphic_frame(Some(&d), &n, None).unwrap();
|
||||
let g = MappedGraphic::from_value(&json!({"symbol": "Mark", "face": "back", "x": 20.0})).unwrap();
|
||||
let (at, visible) = frame.handle(&g).unwrap();
|
||||
assert!(visible && (at.x - 70.0).abs() < 1e-6);
|
||||
let (x, y) = frame.offset_at(PlanarFace::Back, Point::new(40.0, 55.0)).unwrap();
|
||||
assert!((x + 10.0).abs() < 1e-6 && (y - 5.0).abs() < 1e-6);
|
||||
// From the front the back cap is hidden, and so is its graphic.
|
||||
let front = object(EXTRUDE, json!({"graphics": [{"symbol": "Mark", "face": "back"}]}));
|
||||
assert!(graphics_in(&solid_art_in(Some(&d), &front).unwrap()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_perspective_foreshortens_the_graphic_with_its_face() {
|
||||
let d = document();
|
||||
let params = json!({"rotationX": -30.0, "rotationY": 35.0, "perspectiveAngle": 100.0, "graphics": [{"symbol": "Mark", "scale": 400.0}]});
|
||||
let n = object(EXTRUDE, params);
|
||||
let found = graphics_in(&solid_art_in(Some(&d), &n).unwrap());
|
||||
let frame = graphic_frame(Some(&d), &n, None).unwrap();
|
||||
let map = frame.map(PlanarFace::Front).unwrap();
|
||||
// The corners of the 40 × 40 square centered on the object land where the face's map takes them.
|
||||
let path = &found[0][0].0;
|
||||
let corners: Vec<Point> = [(30.0, 30.0), (70.0, 30.0), (70.0, 70.0), (30.0, 70.0)].map(|(x, y)| map.to_page(Point::new(x, y)).unwrap()).into();
|
||||
for c in corners {
|
||||
let near = path.subpaths.iter().flat_map(|s| &s.anchors).map(|a| a.p.distance(c)).fold(f64::INFINITY, f64::min);
|
||||
assert!(near < 1e-6, "{c:?} is {near} from the drawn graphic");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_graphic_behind_the_eye_leaves_the_others_drawn() {
|
||||
let d = document();
|
||||
let view = json!({"rotationX": -60.0, "rotationY": 0.0, "perspectiveAngle": 160.0});
|
||||
let n = object(EXTRUDE, view.clone());
|
||||
let frame = graphic_frame(Some(&d), &n, None).unwrap();
|
||||
let map = frame.map(PlanarFace::Front).unwrap();
|
||||
let c = frame.bounds.center();
|
||||
// An offset whose point of the front's plane is at or behind the eye.
|
||||
let y = [1.0, -1.0]
|
||||
.into_iter()
|
||||
.flat_map(|s| (1..60).map(move |k| s * 10f64.powf(f64::from(k) / 10.0)))
|
||||
.find(|y| map.local_affine(Point::new(c.x, c.y + y)).is_none())
|
||||
.expect("an offset behind the eye");
|
||||
let mut params = view;
|
||||
params["graphics"] = json!([{"symbol": "Mark"}, {"symbol": "Mark", "y": y}]);
|
||||
let found = graphics_in(&solid_art_in(Some(&d), &object(EXTRUDE, params)).unwrap());
|
||||
assert_eq!(found.len(), 1, "the graphic that projects is still drawn");
|
||||
// A graphic centered there but large enough to reach over the cap draws the part in front.
|
||||
let mut params = json!({"rotationX": -60.0, "rotationY": 0.0, "perspectiveAngle": 160.0});
|
||||
params["graphics"] = json!([{"symbol": "Mark", "y": y, "scale": (20.0 * (y.abs() + 50.0)).clamp(1.0, 10_000.0)}]);
|
||||
let found = graphics_in(&solid_art_in(Some(&d), &object(EXTRUDE, params)).unwrap());
|
||||
assert_eq!(found.len(), 1, "the part in front is drawn (offset {y})");
|
||||
assert!(!found[0].is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_graphic_takes_the_opacity_of_its_fill() {
|
||||
let d = document();
|
||||
for id in [EXTRUDE, ROTATE] {
|
||||
let mut n = object(id, json!({"graphics": [{"symbol": "Mark"}]}));
|
||||
if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() {
|
||||
f.opacity = 0.25;
|
||||
}
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut opacities = vec![];
|
||||
art.walk(&mut |c| {
|
||||
if matches!(c.kind, NodeKind::Group { clip: true, .. }) {
|
||||
opacities.push(c.opacity);
|
||||
}
|
||||
});
|
||||
assert_eq!(opacities, vec![0.25], "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_graphic_takes_the_blending_mode_of_its_fill() {
|
||||
let d = document();
|
||||
for id in [EXTRUDE, ROTATE] {
|
||||
let mut n = object(id, json!({"graphics": [{"symbol": "Mark"}]}));
|
||||
if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() {
|
||||
f.blend = BlendMode::Multiply;
|
||||
}
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut blends = vec![];
|
||||
art.walk(&mut |c| {
|
||||
if matches!(c.kind, NodeKind::Group { clip: true, .. }) {
|
||||
blends.push(c.blend);
|
||||
}
|
||||
});
|
||||
assert_eq!(blends, vec![BlendMode::Multiply], "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_fill_of_a_plane_gives_its_graphic_its_own_opacity() {
|
||||
let d = document();
|
||||
let mut n = object(ROTATE, json!({"graphics": [{"symbol": "Mark"}]}));
|
||||
if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() {
|
||||
f.opacity = 0.25;
|
||||
}
|
||||
let mut second = FillLayer::new(Paint::solid(Color::rgb(0.2, 0.2, 0.2)));
|
||||
second.opacity = 0.75;
|
||||
n.appearance.items.push(AppearanceItem::Fill(second));
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut opacities = vec![];
|
||||
art.walk(&mut |c| {
|
||||
if matches!(c.kind, NodeKind::Group { clip: true, .. }) {
|
||||
opacities.push(c.opacity);
|
||||
}
|
||||
});
|
||||
opacities.sort_by(f32::total_cmp);
|
||||
assert_eq!(opacities, vec![0.25, 0.75]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbols_gradient_takes_the_size_of_its_art() {
|
||||
use vectorcraft_doc::color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||||
let mut d = document();
|
||||
// Symbol art kept in its 20 × 20 square, drawn 40 × 20.
|
||||
let mut g = GradientPaint::new(Gradient::new(GradientKind::Linear, vec![GradientStop::new(0.0, red()), GradientStop::new(1.0, Color::BLACK)]));
|
||||
g.geom = Some(GradientGeom { start: Point::new(-10.0, 0.0), end: Point::new(10.0, 0.0), aspect: 1.0, focal: None });
|
||||
let art = square(0, Rect::new(-10.0, -10.0, 10.0, 10.0), Paint::Gradient(Box::new(g)));
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Ramp".into(), art: Arc::new(art) });
|
||||
d.unknown.insert(vectorcraft_doc::SYMBOL_SIZES.into(), json!({"Ramp": [40.0, 20.0]}));
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Ramp"}]}));
|
||||
let out = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut found = None;
|
||||
out.walk(&mut |c| {
|
||||
if let (Some(p), Some(AppearanceItem::Fill(f))) = (c.path_data(), c.appearance.items.first())
|
||||
&& let Paint::Gradient(g) = &f.paint
|
||||
{
|
||||
found = Some((p.bounds().unwrap(), g.geom.unwrap()));
|
||||
}
|
||||
});
|
||||
let (bounds, geom) = found.unwrap();
|
||||
assert!(close(bounds, Rect::new(30.0, 40.0, 70.0, 60.0)), "{bounds:?}");
|
||||
assert!(geom.start.distance(Point::new(30.0, 50.0)) < 1e-6 && geom.end.distance(Point::new(70.0, 50.0)) < 1e-6, "{geom:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbols_pattern_follows_the_graphic() {
|
||||
let mut d = document();
|
||||
let tiles = Paint::Pattern { pattern: "Dots".into(), xf: Affine::IDENTITY };
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Tiles".into(), art: Arc::new(square(0, Rect::new(-10.0, -10.0, 10.0, 10.0), tiles)) });
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Tiles", "scale": 200.0}]}));
|
||||
let out = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut found = None;
|
||||
out.walk(&mut |c| {
|
||||
if let Some(AppearanceItem::Fill(f)) = c.appearance.items.first()
|
||||
&& let Paint::Pattern { xf, .. } = &f.paint
|
||||
{
|
||||
found = Some((c.path_data().unwrap().bounds().unwrap(), *xf));
|
||||
}
|
||||
});
|
||||
let (bounds, xf) = found.unwrap();
|
||||
// The 20 × 20 art drawn 40 × 40 on the middle of the front: its tiles go with it.
|
||||
assert!(close(bounds, Rect::new(30.0, 30.0, 70.0, 70.0)), "{bounds:?}");
|
||||
assert!((xf * Point::new(-10.0, -10.0)).distance(Point::new(30.0, 30.0)) < 1e-6, "{xf:?}");
|
||||
assert!((xf * Point::new(10.0, 10.0)).distance(Point::new(70.0, 70.0)) < 1e-6, "{xf:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_curve_in_front_of_the_eye_maps_though_its_control_points_do_not() {
|
||||
let bounds = Rect::new(10.0, 20.0, 70.0, 60.0);
|
||||
let view = vectorcraft_three_d::View { rotation_x: -89.0, rotation_y: 40.0, rotation_z: 10.0, perspective: 160.0, ..Default::default() };
|
||||
let map = vectorcraft_three_d::plane_face_map(bounds, view).unwrap();
|
||||
// Where the page map stops along the line x = `x`, between the center and a point it does not map.
|
||||
let edge = |x: f64| {
|
||||
let inside = Point::new(x, bounds.center().y);
|
||||
let far = [Point::new(x, -1e6), Point::new(x, 1e6)].into_iter().find(|p| map.to_page(*p).is_none()).unwrap();
|
||||
let (mut a, mut b) = (inside, far);
|
||||
for _ in 0..200 {
|
||||
let m = a.midpoint(b);
|
||||
if map.to_page(m).is_some() { a = m } else { b = m }
|
||||
}
|
||||
a
|
||||
};
|
||||
let (b1, b2) = (edge(40.0), edge(60.0));
|
||||
let u = (b2 - b1).normalize();
|
||||
let mut n = vectorcraft_geom::Vec2::new(-u.y, u.x);
|
||||
if map.to_page(b1 + n * 10.0).is_none() {
|
||||
n = -n;
|
||||
}
|
||||
// Ends 219.17 in front of that line and control points 100 and 42 past it: the curve stays
|
||||
// about 1 in front, while a control point of one of the pieces it is cut into falls behind.
|
||||
let at = |s: f64, t: f64| b1 + n * s + u * t;
|
||||
let mut curve = BezPath::new();
|
||||
curve.move_to(at(219.17, 0.0));
|
||||
curve.curve_to(at(-100.0, 5.0), at(-42.0, 15.0), at(219.17, 20.0));
|
||||
let c = CubicBez::new(at(219.17, 0.0), at(-100.0, 5.0), at(-42.0, 15.0), at(219.17, 20.0));
|
||||
assert!((0..=1000).all(|k| map.to_page(c.eval(k as f64 / 1000.0)).is_some()), "the curve itself is in front");
|
||||
let pieces = (0..PERSPECTIVE_PIECES).map(|k| c.subsegment(k as f64 / 8.0..(k + 1) as f64 / 8.0));
|
||||
assert!(pieces.flat_map(|s| [s.p1, s.p2]).any(|p| map.to_page(p).is_none()), "a piece's control point is behind");
|
||||
let mapped = map_path(&PathData::from_bezpath(&curve), &map, None, usize::MAX).expect("the curve maps");
|
||||
// The split pieces count toward the room the path is given: without room, the path is full,
|
||||
// not behind the eye, so it is not clipped.
|
||||
assert!(mapped.elements().len() > 1 + PERSPECTIVE_PIECES);
|
||||
assert_eq!(map_path(&PathData::from_bezpath(&curve), &map, None, PERSPECTIVE_PIECES + 1).err(), Some(NotMapped::Full));
|
||||
assert!(mapped.elements().iter().all(|e| e.end_point().is_none_or(|p| p.x.is_finite() && p.y.is_finite())));
|
||||
// That piece is split until its halves' control points map, so it stays a curve.
|
||||
assert!(mapped.elements().iter().all(|e| !matches!(e, PathEl::LineTo(_))), "{mapped:?}");
|
||||
// With its ends at 214.25 instead, the curve dips about 0.24 behind the line between the ends
|
||||
// of two pieces, which still map: it has no page point there, and is left to the clipping.
|
||||
let c = CubicBez::new(at(214.25, 0.0), at(-100.0, 5.0), at(-42.0, 15.0), at(214.25, 20.0));
|
||||
assert!((0..=PERSPECTIVE_PIECES).all(|k| map.to_page(c.eval(k as f64 / 8.0)).is_some()));
|
||||
assert!((0..=1000).any(|k| map.to_page(c.eval(k as f64 / 1000.0)).is_none()));
|
||||
let mut dips = BezPath::new();
|
||||
dips.move_to(c.p0);
|
||||
dips.curve_to(c.p1, c.p2, c.p3);
|
||||
assert_eq!(map_path(&PathData::from_bezpath(&dips), &map, None, usize::MAX).err(), Some(NotMapped::Behind));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn members_with_their_own_blending_get_graphics_of_their_own() {
|
||||
let d = document();
|
||||
let a = square(1, Rect::new(0.0, 0.0, 50.0, 100.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)));
|
||||
let mut b = square(2, Rect::new(50.0, 0.0, 100.0, 100.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)));
|
||||
b.blend = BlendMode::Multiply;
|
||||
let mut group = Node::group(NodeId(3), vec![Arc::new(a), Arc::new(b)]);
|
||||
let p = json!({"rotationX": 0.0, "rotationY": 0.0, "rotationZ": 0.0, "shade": false, "graphics": [{"symbol": "Mark", "scale": 1000.0}]});
|
||||
group.appearance.effects.push(crate::new_effect(EXTRUDE, &p).unwrap());
|
||||
let art = solid_art_in(Some(&d), &group).unwrap();
|
||||
let found = graphics_in(&art);
|
||||
// One graphic clipped to each member's cap: the second stays inside the member's Multiply group.
|
||||
assert_eq!(found.len(), 2);
|
||||
let mut blended = 0;
|
||||
art.walk(&mut |c| {
|
||||
if c.blend == BlendMode::Multiply {
|
||||
blended += graphics_in(c).len();
|
||||
}
|
||||
});
|
||||
assert_eq!(blended, 1);
|
||||
}
|
||||
|
||||
/// [`document`] with symbol "Dense" too: a red polygon of 30,000 sides, straight or `curved`.
|
||||
fn dense_document(curved: bool) -> Document {
|
||||
let mut d = document();
|
||||
let mut poly = BezPath::new();
|
||||
let mut last = Point::ZERO;
|
||||
for k in 0..30_000 {
|
||||
let a = k as f64 / 30_000.0 * std::f64::consts::TAU;
|
||||
let p = Point::new(5.0 + 5.0 * a.cos(), 5.0 + 5.0 * a.sin());
|
||||
if k == 0 {
|
||||
poly.move_to(p);
|
||||
} else if curved {
|
||||
poly.curve_to(last.lerp(p, 1.0 / 3.0), last.lerp(p, 2.0 / 3.0), p);
|
||||
} else {
|
||||
poly.line_to(p);
|
||||
}
|
||||
last = p;
|
||||
}
|
||||
poly.close_path();
|
||||
let art = Node::path(NodeId(0), PathData::from_bezpath(&poly), Appearance::basic(Paint::solid(red()), Paint::None, 0.0));
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Dense".into(), art: Arc::new(art) });
|
||||
d
|
||||
}
|
||||
|
||||
/// The graphics drawn on a group of two members, each with its own graphic of symbol "Dense"
|
||||
/// ([`dense_document`]), seen under a perspective.
|
||||
fn dense_graphics_drawn(curved: bool) -> usize {
|
||||
let d = dense_document(curved);
|
||||
let a = square(1, Rect::new(0.0, 0.0, 50.0, 100.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)));
|
||||
let mut b = square(2, Rect::new(50.0, 0.0, 100.0, 100.0), Paint::solid(Color::rgb(0.5, 0.5, 0.5)));
|
||||
b.blend = BlendMode::Multiply;
|
||||
let mut group = Node::group(NodeId(3), vec![Arc::new(a), Arc::new(b)]);
|
||||
let p = json!({"rotationX": 0.0, "rotationY": 0.0, "rotationZ": 0.0, "perspectiveAngle": 60.0, "shade": false, "graphics": [{"symbol": "Dense", "scale": 1000.0}]});
|
||||
group.appearance.effects.push(crate::new_effect(EXTRUDE, &p).unwrap());
|
||||
graphics_in(&solid_art_in(Some(&d), &group).unwrap()).len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn members_with_graphics_of_their_own_share_one_element_limit() {
|
||||
// A perspective cuts each of the 30,000 curves into 8: one copy fits the limit, two do not.
|
||||
// The first member's graphic is drawn; the second would pass the limit, so it draws none.
|
||||
assert_eq!(dense_graphics_drawn(true), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn straight_sides_count_once_under_a_perspective() {
|
||||
// A perspective keeps each of the 30,000 straight sides one element: both copies fit the limit.
|
||||
assert_eq!(dense_graphics_drawn(false), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_symbol_check_counts_what_the_view_draws() {
|
||||
// Two graphics of the 30,000-sided polygon under a perspective: straight, they fit the limit;
|
||||
// curved, they do not.
|
||||
let two = json!({"perspectiveAngle": 60.0, "graphics": [{"symbol": "Dense"}, {"symbol": "Dense", "face": "back"}]});
|
||||
let n = object(EXTRUDE, two.clone());
|
||||
assert!(validate_three_d_in(Some(&dense_document(false)), &n, EXTRUDE, &two).is_ok());
|
||||
let e = validate_three_d_in(Some(&dense_document(true)), &n, EXTRUDE, &two).unwrap_err();
|
||||
assert!(e.contains("too complex"), "{e}");
|
||||
// Without a perspective each curve draws once: curved, they fit too.
|
||||
let flat = json!({"perspectiveAngle": 0.0, "graphics": two["graphics"].clone()});
|
||||
assert!(validate_three_d_in(Some(&dense_document(true)), &n, EXTRUDE, &flat).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_plane_and_a_hollow_solid() {
|
||||
let d = document();
|
||||
let plane = object(ROTATE, json!({"rotationY": 30.0, "graphics": [{"symbol": "Mark"}]}));
|
||||
assert_eq!(graphics_in(&solid_art_in(Some(&d), &plane).unwrap()).len(), 1);
|
||||
let hollow = object(EXTRUDE, json!({"cap": false, "graphics": [{"symbol": "Mark"}]}));
|
||||
assert!(graphics_in(&solid_art_in(Some(&d), &hollow).unwrap()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stroked_graphic_draws_its_stroke_as_an_outline() {
|
||||
let mut d = document();
|
||||
let mut ring = square(0, Rect::new(0.0, 0.0, 10.0, 10.0), Paint::None);
|
||||
ring.appearance = Appearance { items: vec![AppearanceItem::Stroke(StrokeLayer::new(Paint::solid(red()), 2.0))], ..Default::default() }.into();
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Ring".into(), art: Arc::new(ring) });
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Ring"}]}));
|
||||
let found = graphics_in(&solid_art_in(Some(&d), &n).unwrap());
|
||||
// The 10 × 10 square's 2 pt stroke reaches 1 pt outside it.
|
||||
assert!(close(bounds_of(&found[0]), Rect::new(44.0, 44.0, 56.0, 56.0)), "{:?}", bounds_of(&found[0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn graphics_are_checked() {
|
||||
let n = object(EXTRUDE, json!({}));
|
||||
let check = |id: &str, graphics: Value| validate_three_d_in(None, &n, id, &json!({"graphics": graphics}));
|
||||
assert!(check(EXTRUDE, json!([{"symbol": "Mark", "face": "back", "x": 3.0, "scale": 50.0, "rotation": -90.0}])).is_ok());
|
||||
assert!(check(EXTRUDE, json!([])).is_ok());
|
||||
for bad in [
|
||||
json!("Mark"),
|
||||
json!([{"face": "front"}]),
|
||||
json!([{"symbol": ""}]),
|
||||
json!([{"symbol": "Mark", "face": "side"}]),
|
||||
json!([{"symbol": "Mark", "scale": 0.0}]),
|
||||
json!([{"symbol": "Mark", "rotation": 400.0}]),
|
||||
json!([{"symbol": "Mark", "x": "1"}]),
|
||||
json!([{"symbol": "Mark", "repeat": true}]),
|
||||
json!(vec![json!({"symbol": "Mark"}); MAX_GRAPHICS + 1]),
|
||||
] {
|
||||
assert!(check(EXTRUDE, bad.clone()).is_err(), "{bad}");
|
||||
}
|
||||
assert!(check(ROTATE, json!([{"symbol": "Mark", "face": "back"}])).is_err());
|
||||
assert!(check(INFLATE, json!([{"symbol": "Mark"}])).is_err());
|
||||
assert!(validate_three_d_params(crate::REVOLVE, &json!({"graphics": [{"symbol": "Mark"}]})).is_err());
|
||||
// A loaded file's invalid entries are left out when drawn.
|
||||
assert_eq!(mapped_graphics(&json!({"graphics": [{"symbol": "Mark"}, {"face": "back"}, 3]})).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_frame_at_an_index_is_that_effects() {
|
||||
let d = document();
|
||||
let mut n = object(EXTRUDE, json!({}));
|
||||
n.appearance.effects.push(crate::new_effect(ROTATE, &json!({})).unwrap());
|
||||
// The first 3D effect is the Extrude & Bevel, with a back cap; the second is a plane, without.
|
||||
assert!(graphic_frame(Some(&d), &n, None).unwrap().map(PlanarFace::Back).is_some());
|
||||
assert!(super::super::graphic_frame_at(Some(&d), &n, None, 0).unwrap().map(PlanarFace::Back).is_some());
|
||||
assert!(super::super::graphic_frame_at(Some(&d), &n, None, 1).unwrap().map(PlanarFace::Back).is_none());
|
||||
assert!(super::super::graphic_frame_at(Some(&d), &n, None, 2).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_change_to_a_plane_drops_the_back_caps_graphics() {
|
||||
let p = json!({"graphics": [{"symbol": "Mark", "face": "front"}, {"symbol": "Mark", "face": "back"}]});
|
||||
let plane = three_d_params_as(EXTRUDE, &p, ROTATE);
|
||||
assert_eq!(plane["graphics"], json!([{"symbol": "Mark", "face": "front"}]));
|
||||
assert!(validate_three_d_params(ROTATE, &plane).is_ok());
|
||||
// Back to Extrude & Bevel, the front's graphic stays.
|
||||
assert_eq!(three_d_params_as(ROTATE, &plane, EXTRUDE)["graphics"], plane["graphics"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stroked_solid_carries_its_graphic_too() {
|
||||
let d = document();
|
||||
let mut n = Node::path(
|
||||
NodeId(7),
|
||||
shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)),
|
||||
Appearance::basic(Paint::None, Paint::solid(Color::rgb(0.5, 0.5, 0.5)), 20.0),
|
||||
);
|
||||
let p = json!({"rotationX": 0.0, "rotationY": 0.0, "rotationZ": 0.0, "shade": false, "graphics": [{"symbol": "Mark", "scale": 2000.0}]});
|
||||
n.appearance.effects.push(crate::new_effect(EXTRUDE, &p).unwrap());
|
||||
assert_eq!(graphics_in(&solid_art_in(Some(&d), &n).unwrap()).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn art_nested_too_deep_is_refused_and_not_drawn() {
|
||||
let mut d = document();
|
||||
let mut art = square(0, Rect::new(0.0, 0.0, 10.0, 10.0), Paint::solid(red()));
|
||||
for k in 0..=MAX_LEVELS {
|
||||
art = Node::group(NodeId(k as u64 + 1), vec![Arc::new(art)]);
|
||||
}
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Deep".into(), art: Arc::new(art) });
|
||||
let n = object(EXTRUDE, json!({"graphics": [{"symbol": "Deep"}]}));
|
||||
let e = validate_three_d_in(Some(&d), &n, EXTRUDE, &json!({"graphics": [{"symbol": "Deep"}]})).unwrap_err();
|
||||
assert!(e.contains("nested"), "{e}");
|
||||
assert!(graphics_in(&solid_art_in(Some(&d), &n).unwrap()).is_empty());
|
||||
// A loaded file draws its other graphics.
|
||||
let both = object(EXTRUDE, json!({"graphics": [{"symbol": "Deep"}, {"symbol": "Mark"}]}));
|
||||
assert_eq!(graphics_in(&solid_art_in(Some(&d), &both).unwrap()).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbols_nested_too_deep_are_refused_and_not_drawn() {
|
||||
let mut d = document();
|
||||
// Symbol `N{k}` holds an instance of `N{k-1}`, and `N0` is a square.
|
||||
d.symbols.push(Symbol { dynamic: false, name: "N0".into(), art: Arc::new(square(0, Rect::new(0.0, 0.0, 10.0, 10.0), Paint::solid(red()))) });
|
||||
for k in 1..=MAX_NESTING + 1 {
|
||||
let instance = Node::new(NodeId(k as u64), NodeKind::SymbolInstance { symbol: format!("N{}", k - 1), xf: Affine::IDENTITY });
|
||||
d.symbols.push(Symbol { dynamic: false, name: format!("N{k}"), art: Arc::new(instance) });
|
||||
}
|
||||
let deepest = format!("N{MAX_NESTING}");
|
||||
let p = json!({"graphics": [{"symbol": deepest}]});
|
||||
assert!(validate_three_d_in(Some(&d), &object(EXTRUDE, p.clone()), EXTRUDE, &p).is_ok());
|
||||
assert_eq!(graphics_in(&solid_art_in(Some(&d), &object(EXTRUDE, p)).unwrap()).len(), 1);
|
||||
let past = format!("N{}", MAX_NESTING + 1);
|
||||
let p = json!({"graphics": [{"symbol": past}]});
|
||||
let e = validate_three_d_in(Some(&d), &object(EXTRUDE, p.clone()), EXTRUDE, &p).unwrap_err();
|
||||
assert!(e.contains("nest"), "{e}");
|
||||
assert!(graphics_in(&solid_art_in(Some(&d), &object(EXTRUDE, p)).unwrap()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_solid_in_a_transparent_gradient_keeps_its_graphic() {
|
||||
use vectorcraft_doc::color::{Gradient, GradientKind, GradientPaint, GradientStop};
|
||||
let d = document();
|
||||
let mut stop = GradientStop::new(1.0, red());
|
||||
stop.opacity = 0.25;
|
||||
let gradient = GradientPaint::new(Gradient::new(GradientKind::Linear, vec![GradientStop::new(0.0, red()), stop]));
|
||||
// Under a colored light the faces are drawn opaque and masked by the gradient's transparency.
|
||||
let mut n = object(EXTRUDE, json!({"shade": true, "lights": [{"color": "#ff8080"}], "graphics": [{"symbol": "Mark"}]}));
|
||||
if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() {
|
||||
f.paint = Paint::Gradient(Box::new(gradient));
|
||||
}
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut masked = false;
|
||||
art.walk(&mut |c| masked |= c.extras().mask.is_some());
|
||||
assert!(masked);
|
||||
assert_eq!(graphics_in(&art).len(), 1);
|
||||
// Each graphic clip group in `art`, in drawing order: whether it carries an opacity mask.
|
||||
let masks = |art: &Node| {
|
||||
let mut out = vec![];
|
||||
art.walk(&mut |c| {
|
||||
if let NodeKind::Group { children, clip: true } = &c.kind
|
||||
&& let [_, inner] = children.as_slice()
|
||||
&& matches!(inner.kind, NodeKind::Group { clip: false, .. })
|
||||
{
|
||||
out.push(c.extras().mask.is_some());
|
||||
}
|
||||
});
|
||||
out
|
||||
};
|
||||
// The graphic carries the cap's mask, so it is hidden where the gradient is transparent.
|
||||
assert_eq!(masks(&art), vec![true]);
|
||||
// With an opaque stroke drawn after the fill, the fill's cap keeps a masked graphic of its own
|
||||
// and the stroke's outline gets an unmasked one.
|
||||
n.appearance.items.push(AppearanceItem::Stroke(StrokeLayer::new(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), 4.0)));
|
||||
assert_eq!(masks(&solid_art_in(Some(&d), &n).unwrap()), vec![true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cap_with_a_hole_keeps_the_hole_out_of_the_graphic() {
|
||||
let d = document();
|
||||
let outer = shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)).to_bezpath();
|
||||
let hole = shapes::rectangle(Rect::new(30.0, 30.0, 70.0, 70.0)).to_bezpath().reverse_subpaths();
|
||||
let mut ring = outer;
|
||||
ring.extend(hole.iter());
|
||||
for flip in [false, true] {
|
||||
let ring = if flip { ring.reverse_subpaths() } else { ring.clone() };
|
||||
let mut n =
|
||||
Node::path(NodeId(7), PathData::from_bezpath(&ring), Appearance::basic(Paint::solid(Color::rgb(0.5, 0.5, 0.5)), Paint::None, 0.0));
|
||||
let p = json!({"rotationX": 0.0, "rotationY": 0.0, "rotationZ": 0.0, "shade": false, "graphics": [{"symbol": "Mark", "scale": 2000.0}]});
|
||||
n.appearance.effects.push(crate::new_effect(EXTRUDE, &p).unwrap());
|
||||
let art = solid_art_in(Some(&d), &n).unwrap();
|
||||
let mut clips = vec![];
|
||||
art.walk(&mut |c| {
|
||||
if let NodeKind::Group { children, clip: true } = &c.kind {
|
||||
clips.push(children[0].path_data().unwrap().to_bezpath());
|
||||
}
|
||||
});
|
||||
assert_eq!(clips.len(), 1, "flip {flip}");
|
||||
assert_ne!(clips[0].winding(Point::new(10.0, 10.0)), 0, "flip {flip}: the ring is inside the clip");
|
||||
assert_eq!(clips[0].winding(Point::new(50.0, 50.0)), 0, "flip {flip}: the hole is outside it");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_graphic_starts_inside_its_object() {
|
||||
let d = document();
|
||||
let frame = graphic_frame(Some(&d), &object(EXTRUDE, json!({})), None).unwrap();
|
||||
assert_eq!(frame.fitting_scale(Rect::new(0.0, 0.0, 10.0, 10.0)), 100.0);
|
||||
assert!((frame.fitting_scale(Rect::new(0.0, 0.0, 400.0, 200.0)) - 25.0).abs() < 1e-9);
|
||||
assert_eq!(super::super::graphic_bounds(&d, "Mark"), Some(Rect::new(500.0, 500.0, 510.0, 510.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_instance_in_a_graphic_draws_as_the_canvas_shows_it() {
|
||||
let mut d = document();
|
||||
let blue = Paint::solid(Color::rgb(0.0, 0.0, 1.0));
|
||||
// A dynamic symbol whose instance in "Changed" fills its square blue instead of red.
|
||||
d.symbols.push(Symbol { dynamic: true, name: "Dynamic".into(), art: Arc::new(square(0, Rect::new(0.0, 0.0, 10.0, 10.0), Paint::solid(red()))) });
|
||||
let mut changed = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "Dynamic".into(), xf: Affine::IDENTITY });
|
||||
changed.extras_mut().symbol_overrides =
|
||||
vec![vectorcraft_doc::symbol_overrides::SymbolOverride { fill: Some(blue.clone()), ..Default::default() }];
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Changed".into(), art: Arc::new(changed) });
|
||||
let pieces = pieces_of(&d, d.symbols.iter().find(|s| s.name == "Changed").unwrap()).unwrap();
|
||||
assert_eq!(pieces.iter().map(|p| p.paint.clone()).collect::<Vec<_>>(), [blue]);
|
||||
// A symbol with 9-slice guides, a 100 × 100 square with a 10 × 10 one in its corner, placed
|
||||
// three times as wide: the corner keeps its width while the rest stretches.
|
||||
let corner = square(2, Rect::new(0.0, 0.0, 10.0, 10.0), Paint::solid(red()));
|
||||
let whole = square(3, Rect::new(0.0, 0.0, 100.0, 100.0), Paint::solid(red()));
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Sliced".into(), art: Arc::new(Node::group(NodeId(4), vec![Arc::new(whole), Arc::new(corner)])) });
|
||||
d.set_symbol_nine_slice("Sliced", Some(vectorcraft_doc::SymbolNineSlice::default()));
|
||||
let wide = Node::new(NodeId(5), NodeKind::SymbolInstance { symbol: "Sliced".into(), xf: Affine::scale_non_uniform(3.0, 1.0) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Wide".into(), art: Arc::new(wide) });
|
||||
let pieces = pieces_of(&d, d.symbols.iter().find(|s| s.name == "Wide").unwrap()).unwrap();
|
||||
let widths: Vec<f64> = pieces.iter().map(|p| p.path.bounds().unwrap().width()).collect();
|
||||
assert_eq!(widths.len(), 2);
|
||||
assert!((widths[1] / widths[0] - 1.0 / 30.0).abs() < 1e-6, "{widths:?}");
|
||||
// With a gradient, the square crossing the guides is cut at them: its pieces cover the
|
||||
// placed 300 × 100 once, not one whole copy for each cell.
|
||||
use vectorcraft_doc::color::{Gradient, GradientPaint};
|
||||
let gradient = Paint::Gradient(Box::new(GradientPaint::new(Gradient::default())));
|
||||
let shaded = square(6, Rect::new(0.0, 0.0, 100.0, 100.0), gradient);
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Shaded".into(), art: Arc::new(Node::group(NodeId(7), vec![Arc::new(shaded)])) });
|
||||
d.set_symbol_nine_slice("Shaded", Some(vectorcraft_doc::SymbolNineSlice::default()));
|
||||
let wide = Node::new(NodeId(8), NodeKind::SymbolInstance { symbol: "Shaded".into(), xf: Affine::scale_non_uniform(3.0, 1.0) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: "WideShaded".into(), art: Arc::new(wide) });
|
||||
let pieces = pieces_of(&d, d.symbols.iter().find(|s| s.name == "WideShaded").unwrap()).unwrap();
|
||||
let area: f64 = pieces.iter().map(|p| p.path.to_bezpath().area().abs()).sum();
|
||||
assert!((area - 30_000.0).abs() < 1e-3, "{area}");
|
||||
}
|
||||
@@ -426,8 +426,13 @@ pub(crate) fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
|
||||
pub(crate) fn f64_or(p: &Value, key: &str, default: f64) -> f64 {
|
||||
p.get(key).and_then(Value::as_f64).unwrap_or(default)
|
||||
}
|
||||
/// The number `p` gives for `key`. A missing key or null is "missing number `{key}`"; any other
|
||||
/// value, such as `"40"`, is "`{key}` must be a number, not {v}", as [`number_or`] words it.
|
||||
pub(crate) fn f64_req(p: &Value, key: &str, cmd: &str) -> Result<f64> {
|
||||
p.get(key).and_then(Value::as_f64).ok_or_else(|| bad(cmd, format!("missing number `{key}`")))
|
||||
match p.get(key) {
|
||||
None | Some(Value::Null) => Err(bad(cmd, format!("missing number `{key}`"))),
|
||||
Some(v) => v.as_f64().ok_or_else(|| bad(cmd, format!("`{key}` must be a number, not {v}"))),
|
||||
}
|
||||
}
|
||||
/// The number `p` gives for `key`, or `default` when `p` leaves it out or gives null. Any other
|
||||
/// value, such as `"40"`, is an error: "`{key}` must be a number, not {v}".
|
||||
|
||||
@@ -12,7 +12,7 @@ use vectorcraft_doc::{CharStyle, Document, Justify, Node, NodeId, NodeKind, Para
|
||||
use vectorcraft_geom::{Affine, Point, Vec2};
|
||||
use vectorcraft_text::{FontDb, FontMatch, TextLayout, edit};
|
||||
|
||||
use crate::cmd::fonts::{edit_symbol_texts, symbol_texts};
|
||||
use crate::cmd::fonts::{art_texts_of, edit_art_texts_of};
|
||||
use crate::cmd::textedit::{editing_range, editing_story};
|
||||
use crate::cmd::typecmd::refresh_bounds;
|
||||
use crate::cmd::{CommandSpec, bad, checked_ids_or_selection, cmd, has_doc, has_selection, switch_or, text_param};
|
||||
@@ -108,7 +108,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Replace Missing Fonts…",
|
||||
["Object", "Plug-ins", "Artwork Cleanup"],
|
||||
None,
|
||||
"{font?: family (default: the default type font, Source Sans 3; any of its names)} set every character of the document's type (the type in the art of its symbols included) whose font family isn't available in `font`, in the style it has closest to the one each had, as one undo step → {runs (style runs changed), font}",
|
||||
"{font?: family (default: the default type font, Source Sans 3; any of its names)} set every character of the document's type (the type in its opacity masks and in the art of its symbols and patterns included, hidden or locked type and disabled masks too) whose font family isn't available in `font`, in the style it has closest to the one each had, as one undo step → {runs (style runs changed), font}",
|
||||
has_doc,
|
||||
replace_fonts
|
||||
),
|
||||
@@ -982,8 +982,9 @@ fn replace_fonts(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
});
|
||||
}
|
||||
// The type in the symbols' art too, which exports name when its font is missing.
|
||||
if ids.is_empty() && !symbol_texts(d, None).into_iter().any(|(t, _)| lacks(t)) {
|
||||
// The type in opacity masks and in the art of symbols and patterns too, hidden or locked type
|
||||
// included, which exports name when its font is missing.
|
||||
if ids.is_empty() && !art_texts_of(d).into_iter().any(|(_, t, _)| lacks(t)) {
|
||||
return Err(EngineError::Other("No fonts are missing".into()));
|
||||
}
|
||||
let runs = s.edit("Replace Missing Fonts", |d, _| {
|
||||
@@ -1008,7 +1009,7 @@ fn replace_fonts(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
replace(t);
|
||||
}
|
||||
}
|
||||
edit_symbol_texts(d, None, &|t, _| lacks(t), &mut replace);
|
||||
edit_art_texts_of(d, &lacks, &mut replace);
|
||||
Ok(runs)
|
||||
})?;
|
||||
Ok(json!({ "runs": runs, "font": family }))
|
||||
|
||||
@@ -415,3 +415,53 @@ fn replace_missing_fonts_reaches_the_type_in_symbols() {
|
||||
s.execute("symbol.exitEditing", &json!({})).unwrap();
|
||||
assert_eq!(crate::cmd::fonts::substitution_warning(&s.doc().unwrap().doc), None);
|
||||
}
|
||||
|
||||
/// Replace Missing Fonts reaches the type in opacity masks (of a hidden object, disabled, in a
|
||||
/// symbol's art) and in a pattern swatch nothing paints with, and the export warning, the missing
|
||||
/// fonts list and Find Font name that type's font until it is replaced.
|
||||
#[test]
|
||||
fn replace_missing_fonts_reaches_the_type_in_masks_and_patterns() {
|
||||
let mut s = session();
|
||||
let mask = |s: &mut Session, owner: NodeId, art: NodeId| {
|
||||
s.edit("Mask", |d, _| {
|
||||
let art = d.remove(art)?;
|
||||
d.node_mut(owner).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new((*art).clone(), true)));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
};
|
||||
let lost = |s: &mut Session| text(s, json!({"x": 40, "y": 60, "text": "Lost", "size": 20, "font": "No Such Font Family"}));
|
||||
let rect =
|
||||
|s: &mut Session| NodeId(s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 20})).unwrap()["id"].as_u64().unwrap());
|
||||
let (hidden, disabled, in_symbol) = (rect(&mut s), rect(&mut s), rect(&mut s));
|
||||
for owner in [hidden, disabled, in_symbol] {
|
||||
let t = lost(&mut s);
|
||||
mask(&mut s, owner, t);
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [hidden.0]})).unwrap();
|
||||
s.execute("object.hide", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [disabled.0]})).unwrap();
|
||||
s.execute("transparency.disableOpacityMask", &json!({})).unwrap();
|
||||
s.execute("symbol.new", &json!({"name": "Mark", "ids": [in_symbol.0]})).unwrap();
|
||||
let t = lost(&mut s);
|
||||
s.execute("object.pattern.make", &json!({"ids": [t.0], "name": "Unused", "edit": false})).unwrap();
|
||||
select(&mut s, &[t]);
|
||||
s.execute("edit.clear", &json!({})).unwrap();
|
||||
let mut on_layers = 0;
|
||||
s.doc().unwrap().doc.walk(|n| on_layers += usize::from(matches!(n.kind, NodeKind::Text(_))));
|
||||
assert_eq!(on_layers, 0, "the type is only in masks, a symbol and a pattern");
|
||||
let missing = |s: &Session| crate::cmd::fontfiles::missing_fonts(&s.doc().unwrap().doc).into_iter().map(|f| f.family).collect::<Vec<_>>();
|
||||
assert_eq!(missing(&s), ["No Such Font Family"]);
|
||||
assert!(crate::cmd::fonts::substitution_warning(&s.doc().unwrap().doc).is_some_and(|w| w.contains("No Such Font Family")));
|
||||
let listed = s.execute("text.fonts", &json!({})).unwrap();
|
||||
assert_eq!((listed[0]["family"].as_str(), listed[0]["objects"].as_u64()), (Some("No Such Font Family"), Some(4)), "{listed}");
|
||||
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let before = steps(&s);
|
||||
let r = s.execute("addon.cleanup.replaceFonts", &json!({})).unwrap();
|
||||
assert_eq!(r["runs"].as_u64(), Some(4), "{r}");
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
assert!(missing(&s).is_empty());
|
||||
assert_eq!(crate::cmd::fonts::substitution_warning(&s.doc().unwrap().doc), None);
|
||||
assert_eq!(other(s.execute("addon.cleanup.replaceFonts", &json!({}))), "No fonts are missing");
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
}
|
||||
@@ -208,7 +208,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Symbol Options…",
|
||||
[],
|
||||
None,
|
||||
"{name?, newName?, registration?: 0..8, exportType?: movieClip|graphic, nineSlice?: bool, nineSliceGuides?: array, type?: static|dynamic} rename a symbol and/or set Symbol Options › Registration, Export Type, 9-slice guides (nineSliceGuides [left, top, right, bottom] fractions 0..1) and Symbol Type (a dynamic symbol's instances may change the appearance of objects in its art and stay linked; making it static drops those changes); default name: current → {name, registration, exportType, nineSlice, type}",
|
||||
"{name?, newName?, registration?: 0..8, exportType?: movieClip|graphic, nineSlice?: bool, nineSliceGuides?: array, type?: static|dynamic} rename a symbol (its instances and the graphics mapped onto 3D objects follow the new name) and/or set Symbol Options › Registration, Export Type, 9-slice guides (nineSliceGuides [left, top, right, bottom] fractions 0..1) and Symbol Type (a dynamic symbol's instances may change the appearance of objects in its art and stay linked; making it static drops those changes); default name: current → {name, registration, exportType, nineSlice, type}",
|
||||
has_doc,
|
||||
symbol_options
|
||||
),
|
||||
@@ -326,7 +326,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Select All Unused",
|
||||
[],
|
||||
None,
|
||||
"{} → {names: [the symbols nothing places, in panel order]} (the Symbols panel selects them): instances in the art (opacity masks included), graph designs, pattern tiles (with the tiles that pattern editing's Cancel puts back) and brush art count, and so do instances in the art of a symbol that is used",
|
||||
"{} → {names: [the symbols nothing places, in panel order]} (the Symbols panel selects them): instances in the art (opacity masks included), graphics mapped onto 3D objects (threeD.mapGraphic), graph designs, pattern tiles (with the tiles that pattern editing's Cancel puts back) and brush art count, and so do instances in the art of a symbol that is used",
|
||||
has_doc,
|
||||
symbol_unused
|
||||
),
|
||||
|
||||
@@ -115,6 +115,8 @@ fn art_symbols(n: &Node, out: &mut Vec<String>) {
|
||||
if let NodeKind::SymbolInstance { symbol, .. } = &m.kind {
|
||||
out.push(symbol.clone());
|
||||
}
|
||||
// A graphic on a 3D object draws its symbol's art too.
|
||||
out.extend(vectorcraft_render::effects::graphic_symbols(m));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -129,6 +131,8 @@ pub(super) fn symbol_unused(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let patterns = d.patterns.iter().chain(d.pattern_edit.iter().filter_map(|pe| pe.original.as_ref()));
|
||||
let trees = d.layers.iter().map(|l| &**l).chain(d.graph_designs.iter().map(|g| &*g.art));
|
||||
trees.chain(patterns.flat_map(|p| p.art.iter().map(|n| &**n))).chain(lib.iter().flat_map(|b| b.art())).for_each(|n| art_symbols(n, &mut todo));
|
||||
// A graphic in a graphic style draws its symbol's art wherever the style is applied.
|
||||
todo.extend(d.graphic_styles.iter().flat_map(|g| vectorcraft_render::effects::appearance_graphic_symbols(&g.appearance)));
|
||||
let mut used = std::collections::BTreeSet::new();
|
||||
while let Some(name) = todo.pop() {
|
||||
if !used.contains(&name) {
|
||||
@@ -290,6 +294,33 @@ pub(super) fn symbol_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
*symbol = n.clone();
|
||||
}
|
||||
}
|
||||
// The graphics mapped onto 3D objects follow the new name: in the layers, the art of
|
||||
// symbols, patterns, graph designs and brushes, their opacity masks, and graphic styles.
|
||||
let patterns = d.patterns.iter_mut().chain(d.pattern_edit.iter_mut().filter_map(|pe| pe.original.as_mut()));
|
||||
let trees = d
|
||||
.layers
|
||||
.iter_mut()
|
||||
.chain(d.symbols.iter_mut().map(|sym| &mut sym.art))
|
||||
.chain(d.graph_designs.iter_mut().map(|g| &mut g.art))
|
||||
.chain(patterns.flat_map(|p| p.art.iter_mut()));
|
||||
for art in trees.filter(|art| vectorcraft_render::effects::names_graphic_symbol(art, &name)) {
|
||||
vectorcraft_render::effects::rename_graphic_symbol_in(Arc::make_mut(art), &name, n);
|
||||
}
|
||||
let styled = |g: &vectorcraft_doc::GraphicStyle| vectorcraft_render::effects::appearance_graphic_symbols(&g.appearance).contains(&name);
|
||||
if d.graphic_styles.iter().any(styled) {
|
||||
for style in d.graphic_styles.iter_mut() {
|
||||
vectorcraft_render::effects::rename_appearance_graphic_symbol(&mut style.appearance, &name, n);
|
||||
}
|
||||
}
|
||||
// The brush library is stored again only when a brush's art changed.
|
||||
let mut lib = brush::library(d);
|
||||
let mut brushes = false;
|
||||
for art in lib.iter_mut().flat_map(|b| b.art_mut()) {
|
||||
brushes |= vectorcraft_render::effects::rename_graphic_symbol_in(art, &name, n);
|
||||
}
|
||||
if brushes {
|
||||
brush::store(d, &lib);
|
||||
}
|
||||
rename_natural_size(d, &name, n);
|
||||
d.rename_symbol_registration(&name, n);
|
||||
d.rename_symbol_export_type(&name, n);
|
||||
|
||||
@@ -86,6 +86,10 @@ fn paint_refs(p: &Paint, f: Visit) {
|
||||
}
|
||||
|
||||
fn appearance_refs(a: &Appearance, f: Visit) {
|
||||
// The graphics mapped onto the faces of its 3D effects name symbols.
|
||||
for s in vectorcraft_render::effects::appearance_graphic_symbols(a) {
|
||||
f(Ref::Name(Res::Symbol, &s));
|
||||
}
|
||||
for it in &a.items {
|
||||
paint_refs(it.paint(), f);
|
||||
if let AppearanceItem::Stroke(l) = it
|
||||
@@ -620,6 +624,17 @@ impl Relink {
|
||||
}
|
||||
}
|
||||
}
|
||||
// A 3D effect's mapped graphics follow their symbols' names in the document. Each name is
|
||||
// looked up once, so renames that chain (A to B, B to C) do not apply twice.
|
||||
let items = a.items.iter_mut().flat_map(|i| i.effects_mut().iter_mut());
|
||||
for e in a.effects.iter_mut().chain(items).filter(|e| vectorcraft_render::effects::takes_graphics(&e.id)) {
|
||||
let list = e.params.get_mut(vectorcraft_render::effects::GRAPHICS).and_then(Value::as_array_mut);
|
||||
for g in list.into_iter().flatten() {
|
||||
if let Some(Value::String(s)) = g.get_mut("symbol") {
|
||||
self.name(Res::Symbol, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
for it in &mut a.items {
|
||||
self.paint(it.paint_mut());
|
||||
if let AppearanceItem::Stroke(l) = it
|
||||
@@ -697,15 +712,6 @@ fn same_brush(r: &Relink, ours: &Brush, theirs: &Brush) -> bool {
|
||||
relinked(&Relink::default(), ours) == relinked(r, theirs)
|
||||
}
|
||||
|
||||
/// Does brush `b`'s art show symbols or patterns?
|
||||
fn shows_definitions(b: &Brush) -> bool {
|
||||
let mut found = false;
|
||||
for a in b.art() {
|
||||
node_refs(a, &mut |r| found |= matches!(r, Ref::Name(Res::Symbol | Res::Pattern, _)));
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
/// A symbol's Symbol Options, which its document keeps by symbol name: copied with the symbol and
|
||||
/// given to it under the name pasting adds it as.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -739,7 +745,7 @@ impl SymbolOptions {
|
||||
}
|
||||
|
||||
/// A character or paragraph style, symbol (with the Symbol Options copied with it), pattern or
|
||||
/// brush (placed with the others only when they are placed in use order) to place
|
||||
/// brush to place
|
||||
/// ([`Clipboard::import_used`]).
|
||||
#[derive(Clone, Copy)]
|
||||
enum Def<'c> {
|
||||
@@ -1055,49 +1061,40 @@ impl Clipboard {
|
||||
}
|
||||
|
||||
/// Add the resources the objects use to `d` (inside the paste's undo step) and work out how
|
||||
/// pasted copies relink to them. An error, with `d` unchanged, when a pasted SVG filter finds no
|
||||
/// free name ([`Self::filter_links`]).
|
||||
/// pasted copies relink to them. Each style, symbol, pattern and brush is placed after those it
|
||||
/// uses, as [`import_text_styles`] places them, so that a style that paints with a pattern added
|
||||
/// under a free name ("Dots 2") is compared and added with that name. An error, with `d`
|
||||
/// unchanged, when a pasted SVG filter finds no free name ([`Self::filter_links`]).
|
||||
pub(crate) fn import_into(&self, d: &mut Document, choices: &SwatchChoices, same_doc: bool) -> Result<Imported> {
|
||||
self.import_used(d, &self.used(), choices, same_doc, false)
|
||||
self.import_used(d, &self.used(), choices, same_doc)
|
||||
}
|
||||
|
||||
/// [`Self::import_into`] for the resources `used` names. The character and paragraph styles
|
||||
/// come before the symbols and patterns, whose art may set type in them; with `in_use_order`,
|
||||
/// each style, symbol, pattern and brush comes after the styles, symbols, patterns and brushes it uses
|
||||
/// ([`use_order`]), so that it is compared and relinked with their names here, and each loop
|
||||
/// of definitions that use each other is matched on its own ([`loop_groups`]).
|
||||
fn import_used(&self, d: &mut Document, used: &Used, choices: &SwatchChoices, same_doc: bool, in_use_order: bool) -> Result<Imported> {
|
||||
/// [`Self::import_into`] for the resources `used` names. Each style, symbol, pattern and brush
|
||||
/// comes after the styles, symbols, patterns and brushes it uses ([`use_order`]), so that it is
|
||||
/// compared and relinked with their names here, and each loop of definitions that use each
|
||||
/// other is matched on its own ([`loop_groups`]). Pasted back into its own document
|
||||
/// (`same_doc`), each definition of a loop is the document's of its name.
|
||||
fn import_used(&self, d: &mut Document, used: &Used, choices: &SwatchChoices, same_doc: bool) -> Result<Imported> {
|
||||
let mut out = Imported::default();
|
||||
out.relink.filters = self.filter_links(d, same_doc)?;
|
||||
self.import_images(d, used, same_doc, &mut out);
|
||||
self.import_swatches(d, used, choices, same_doc, &mut out);
|
||||
// Brushes whose art shows no symbols or patterns, before the symbols and patterns whose art
|
||||
// they may brush; the others once those are in. With `in_use_order`, the brushes are
|
||||
// placed in use order with the others.
|
||||
let mut added_brushes: HashSet<String> =
|
||||
if in_use_order { HashSet::new() } else { self.import_brushes(d, used, same_doc, false, &mut out).into_iter().collect() };
|
||||
let mut defs: Vec<Def<'_>> = vec![];
|
||||
for (r, list) in [(Res::CharStyle, &self.char_styles), (Res::ParaStyle, &self.para_styles)] {
|
||||
defs.extend(list.iter().filter(|s| used.has(r, &s.name)).map(|s| Def::Style(r, s)));
|
||||
}
|
||||
defs.extend(self.symbols.iter().filter(|s| used.has(Res::Symbol, &s.name)).map(|s| Def::Symbol(s, self.symbol_options.get(&s.name))));
|
||||
defs.extend(self.patterns.iter().filter(|p| used.has(Res::Pattern, &p.name)).map(Def::Pattern));
|
||||
if in_use_order {
|
||||
defs.extend(self.brushes.iter().filter(|b| used.has(Res::Brush, &b.name)).map(Def::Brush));
|
||||
}
|
||||
defs.extend(self.brushes.iter().filter(|b| used.has(Res::Brush, &b.name)).map(Def::Brush));
|
||||
// Groups to place in order; true for a loop of definitions that use each other.
|
||||
let groups: Vec<(Vec<Def<'_>>, bool)> = if in_use_order {
|
||||
let (order, rest) = use_order(defs);
|
||||
let loops = loop_groups(&rest).into_iter().map(|(at, looped)| (at.into_iter().filter_map(|i| rest.get(i).copied()).collect(), looped));
|
||||
std::iter::once((order, false)).chain(loops).collect()
|
||||
} else {
|
||||
vec![(defs, false)]
|
||||
};
|
||||
// Styles (their colors relinked first), symbols and patterns (compared once relinked,
|
||||
// symbols with their options too). The symbols and patterns added are relinked at the end,
|
||||
// when every new name and graphic style id is known; with `in_use_order`, so are the
|
||||
// styles added, which a loop of uses may place before a pattern they paint with.
|
||||
let (mut added_symbols, mut added_patterns, mut added_styles) = (HashSet::new(), HashSet::new(), vec![]);
|
||||
let (order, rest) = use_order(defs);
|
||||
let loops = loop_groups(&rest).into_iter().map(|(at, looped)| (at.into_iter().filter_map(|i| rest.get(i).copied()).collect(), looped));
|
||||
let groups: Vec<(Vec<Def<'_>>, bool)> = std::iter::once((order, false)).chain(loops).collect();
|
||||
// Styles (their colors relinked first), symbols, patterns and brushes (compared once
|
||||
// relinked, symbols with their options too). The styles, symbols, patterns and brushes
|
||||
// added are relinked at the end, when every new name and graphic style id is known: a loop
|
||||
// of uses may place one before a definition it uses.
|
||||
let (mut added_symbols, mut added_patterns, mut added_brushes, mut added_styles) = (HashSet::new(), HashSet::new(), HashSet::new(), vec![]);
|
||||
// Each definition, whether it is in a loop, and the loop it starts.
|
||||
let steps = groups
|
||||
.iter()
|
||||
@@ -1107,8 +1104,8 @@ impl Clipboard {
|
||||
let mut fresh = false;
|
||||
// The names in the document of the loop being placed, when it has them all at once.
|
||||
let mut matched: BTreeMap<(Res, String), String> = BTreeMap::new();
|
||||
// The document's brush library, read the first time a brush is placed in use order and
|
||||
// stored again after each brush added; nothing else in the loop changes it.
|
||||
// The document's brush library, read the first time a brush is placed and stored again
|
||||
// after each brush added; nothing else in the loop changes it.
|
||||
let mut library: Option<Vec<Brush>> = None;
|
||||
for (def, looped, starts) in steps {
|
||||
fresh &= looped;
|
||||
@@ -1116,8 +1113,9 @@ impl Clipboard {
|
||||
matched.clear();
|
||||
}
|
||||
// A loop of uses that the document has all at once is known under those names before
|
||||
// its definitions are compared.
|
||||
if let Some(group) = starts {
|
||||
// its definitions are compared. In its own document, each definition is the
|
||||
// document's of its name, as outside a loop.
|
||||
if let Some(group) = starts.filter(|_| !same_doc) {
|
||||
matched.clear();
|
||||
match names_of_loop(d, group, &out.relink) {
|
||||
Some(names) => {
|
||||
@@ -1146,7 +1144,7 @@ impl Clipboard {
|
||||
out.relink.attrs(&mut s.attrs);
|
||||
let theirs = if r == Res::CharStyle { &mut d.char_styles } else { &mut d.para_styles };
|
||||
let p = place(theirs, &s, same_doc, |a, b| !fresh && a == b);
|
||||
if in_use_order && let Some(name) = p.new_name(&def.name) {
|
||||
if let Some(name) = p.new_name(&def.name) {
|
||||
added_styles.push((r, name, def));
|
||||
}
|
||||
out.placed(r, &def.name, &p);
|
||||
@@ -1200,9 +1198,6 @@ impl Clipboard {
|
||||
}
|
||||
}
|
||||
}
|
||||
if !in_use_order {
|
||||
added_brushes.extend(self.import_brushes(d, used, same_doc, true, &mut out));
|
||||
}
|
||||
self.import_graphic_styles(d, used, same_doc, &mut out);
|
||||
for (r, name, def) in added_styles {
|
||||
let theirs = if r == Res::CharStyle { &mut d.char_styles } else { &mut d.para_styles };
|
||||
@@ -1323,28 +1318,6 @@ impl Clipboard {
|
||||
}
|
||||
}
|
||||
|
||||
/// The brushes whose art shows symbols or patterns (`late`), or the others: each one the
|
||||
/// document has (its art compared once relinked) is used, the others are added. → the names of
|
||||
/// those added, whose art is relinked at the end.
|
||||
fn import_brushes(&self, d: &mut Document, used: &Used, same_doc: bool, late: bool, out: &mut Imported) -> Vec<String> {
|
||||
let brushes: Vec<&Brush> = self.brushes.iter().filter(|b| used.has(Res::Brush, &b.name) && shows_definitions(b) == late).collect();
|
||||
if brushes.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
let mut lib = vectorcraft_brush::library(d);
|
||||
let mut added = vec![];
|
||||
for b in brushes {
|
||||
let r = &out.relink;
|
||||
let p = place(&mut lib, b, same_doc, |ours, theirs| same_brush(r, ours, theirs));
|
||||
added.extend(p.new_name(&b.name));
|
||||
out.placed(Res::Brush, &b.name, &p);
|
||||
}
|
||||
if !added.is_empty() {
|
||||
vectorcraft_brush::store(d, &lib);
|
||||
}
|
||||
added
|
||||
}
|
||||
|
||||
/// Global and spot colours (a name conflict merges or adds as `choices` say), then the tint
|
||||
/// swatches of the tints used and the gradient swatches.
|
||||
fn import_swatches(&self, d: &mut Document, used: &Used, choices: &SwatchChoices, same_doc: bool, out: &mut Imported) {
|
||||
@@ -1429,7 +1402,7 @@ pub(crate) fn import_text_styles(src: &Document, d: &mut Document, chars: bool,
|
||||
let used = Used::of_text_styles(&Defs::of(src, &library), chars, paras);
|
||||
let clip = Clipboard::with_used(src, &used, &library, vec![]);
|
||||
let before = [&d.char_styles, &d.para_styles].map(|l| l.iter().map(|s| s.name.clone()).collect::<BTreeSet<_>>());
|
||||
let out = clip.import_used(d, &used, &SwatchChoices::default(), false, true)?;
|
||||
let out = clip.import_used(d, &used, &SwatchChoices::default(), false)?;
|
||||
let mut placed: Vec<LoadedStyle> = vec![];
|
||||
for (para, r, list) in [(false, Res::CharStyle, &clip.char_styles), (true, Res::ParaStyle, &clip.para_styles)] {
|
||||
for s in list {
|
||||
|
||||
@@ -18,7 +18,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Document Info",
|
||||
["Window", "Document Info"],
|
||||
None,
|
||||
"{selectionOnly?, category?: document|objects|graphicStyles|brushes|spotColors|patterns|gradients|symbols|fonts|fontDetails|linkedImages|embeddedImages|repeats (only that one in sections and the text), format?: \"text\" (→ {text}: the report Document Info › Save… and Package write)} → {document, objects: {paths, compoundPaths, groups, …}, pathLength: pt (the paths' total length, compound paths' included, guides left out), fonts, images, swatches, graphicStyleNames (with selectionOnly: the styles the selected objects are linked to), brushNames (the brushes the art's drawn strokes use, each once; with selectionOnly: those the selected objects' strokes use), brushes: [{name, type: calligraphic|scatter|art|pattern|bristle|null}] (those brushes with their types; null: not in the Brushes panel), panelBrushNames (the Brushes panel's brushes), repeatObjects: [{id, name, type: radial|grid|mirror, instances, rows?, cols?}] (the Repeat objects, those in groups and other repeats included; with selectionOnly: those in the selection), spotObjects: [{id, name, spotColors}] (the objects filled or stroked with a spot color swatch, as a solid color or in the stops of a gradient with a stop above opacity 0, at a tint above 0, and grayscale images colorized with one, each once, by the rules patternObjects follows, with the spot colors they use; Registration and process colors are not spot colors; objects.spots counts them), spotColorNames and spotColors (the spot colors those objects use), patternObjects: [{id, name, patterns}] (the objects filled or stroked with a pattern, each once, those in groups and type whose drawn characters use one included, hidden fills and strokes, fills and strokes at an opacity of 0, strokes with no width, guides, compound path members' own fills and strokes, images' fills and strokes without a shape and characters that draw nothing left out, with the patterns they use, which objects.patterns counts; with selectionOnly: those in the selection), patternNames (the patterns those objects use), gradientObjects: [{id, name, gradients}] (the objects filled or stroked with a gradient, by the same rules, with the gradients they use: a linked gradient swatch's name, else Linear gradient, Radial gradient or Freeform gradient; objects.gradients counts them), gradientNames (the gradients those objects use), …, sections: [{id, title, rows: [[label, value]]}] (the panel's categories; fontDetails: each font's file and whether its license lets it be embedded)}. An image linked to a file outside the automation read roots has link null, and its Linked Images row names the file alone",
|
||||
"{selectionOnly?, category?: document|objects|graphicStyles|brushes|spotColors|patterns|gradients|symbols|fonts|fontDetails|linkedImages|embeddedImages|repeats (only that one in sections and the text), format?: \"text\" (→ {text}: the report Document Info › Save… and Package write)} → {document, objects: {paths, compoundPaths, groups, …}, pathLength: pt (the paths' total length, compound paths' included, guides left out), fonts (the type in opacity masks and in the art of symbols and patterns included; with selectionOnly: the selected type's), images, swatches, graphicStyleNames (with selectionOnly: the styles the selected objects are linked to), brushNames (the brushes the art's drawn strokes use, each once; with selectionOnly: those the selected objects' strokes use), brushes: [{name, type: calligraphic|scatter|art|pattern|bristle|null}] (those brushes with their types; null: not in the Brushes panel), panelBrushNames (the Brushes panel's brushes), repeatObjects: [{id, name, type: radial|grid|mirror, instances, rows?, cols?}] (the Repeat objects, those in groups and other repeats included; with selectionOnly: those in the selection), spotObjects: [{id, name, spotColors}] (the objects filled or stroked with a spot color swatch, as a solid color or in the stops of a gradient with a stop above opacity 0, at a tint above 0, and grayscale images colorized with one, each once, by the rules patternObjects follows, with the spot colors they use; Registration and process colors are not spot colors; objects.spots counts them), spotColorNames and spotColors (the spot colors those objects use), patternObjects: [{id, name, patterns}] (the objects filled or stroked with a pattern, each once, those in groups and type whose drawn characters use one included, hidden fills and strokes, fills and strokes at an opacity of 0, strokes with no width, guides, compound path members' own fills and strokes, images' fills and strokes without a shape and characters that draw nothing left out, with the patterns they use, which objects.patterns counts; with selectionOnly: those in the selection), patternNames (the patterns those objects use), gradientObjects: [{id, name, gradients}] (the objects filled or stroked with a gradient, by the same rules, with the gradients they use: a linked gradient swatch's name, else Linear gradient, Radial gradient or Freeform gradient; objects.gradients counts them), gradientNames (the gradients those objects use), …, sections: [{id, title, rows: [[label, value]]}] (the panel's categories; fontDetails: each font's file and whether its license lets it be embedded)}. An image linked to a file outside the automation read roots has link null, and its Linked Images row names the file alone",
|
||||
has_doc,
|
||||
info
|
||||
),
|
||||
@@ -627,16 +627,22 @@ fn info(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
});
|
||||
}
|
||||
// The document's fonts include those of the type in opacity masks and in the art of its
|
||||
// symbols and patterns, as Find Font lists them.
|
||||
if !selection_only {
|
||||
fonts.extend(super::fonts::art_texts_of(d).into_iter().flat_map(|(_, t, _)| t.runs.iter().map(|r| super::fonts::font_of(&r.style))));
|
||||
}
|
||||
let font_names: Vec<String> = fonts.iter().map(super::fonts::font_label).collect();
|
||||
// Each brush the art uses with its type, found by name in the Brushes panel as the renderer finds it (the last of two
|
||||
// brushes with one name); a brush the panel does not hold draws as a plain stroke and has no type.
|
||||
// brushes with one name); a brush the panel does not hold draws as a plain stroke and has no type. A row names the type
|
||||
// as the New Brush dialog does (`Art Brush`), and the panel translates these values when it shows them.
|
||||
let kinds: HashMap<&str, &vectorcraft_brush::BrushKind> = library.iter().map(|b| (b.name.as_str(), &b.kind)).collect();
|
||||
let brush_entries: Vec<Value> =
|
||||
brushes.iter().map(|name| json!({ "name": name, "type": kinds.get(name.as_str()).map(|k| k.type_id()) })).collect();
|
||||
let brush_rows: Vec<(String, String)> = brushes
|
||||
.iter()
|
||||
.map(|name| {
|
||||
(name.clone(), kinds.get(name.as_str()).map_or_else(|| "missing from the Brushes panel".to_string(), |k| format!("{} brush", k.label())))
|
||||
(name.clone(), kinds.get(name.as_str()).map_or_else(|| "missing from the Brushes panel".to_string(), |k| format!("{} Brush", k.label())))
|
||||
})
|
||||
.collect();
|
||||
let mut out = json!({
|
||||
@@ -755,6 +761,40 @@ mod tests {
|
||||
assert!(i["objects"]["textObjects"].is_null() && i["objects"]["paths"] == 1);
|
||||
}
|
||||
|
||||
/// Document Info's fonts include the type in an opacity mask, in a symbol's art and in a
|
||||
/// pattern swatch that nothing paints with; with `selectionOnly`, only the selected type's.
|
||||
#[test]
|
||||
fn info_fonts_include_masks_symbols_and_patterns() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap();
|
||||
let id = |v: Value| NodeId(v["id"].as_u64().unwrap());
|
||||
let text = |s: &mut Session, font: &str| id(s.execute("text.create", &json!({"x": 10, "y": 40, "text": "Hi", "font": font})).unwrap());
|
||||
let rect = |s: &mut Session| id(s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap());
|
||||
let (masked, in_symbol) = (rect(&mut s), rect(&mut s));
|
||||
for (owner, font) in [(masked, "Mask Family"), (in_symbol, "Symbol Mask Family")] {
|
||||
let t = text(&mut s, font);
|
||||
s.edit("Mask", |d, _| {
|
||||
let art = d.remove(t)?;
|
||||
d.node_mut(owner).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new((*art).clone(), true)));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
s.execute("symbol.new", &json!({"name": "Mark", "ids": [in_symbol.0]})).unwrap();
|
||||
let t = text(&mut s, "Pattern Family");
|
||||
s.execute("object.pattern.make", &json!({"ids": [t.0], "name": "Unused", "edit": false})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [t.0]})).unwrap();
|
||||
s.execute("edit.clear", &json!({})).unwrap();
|
||||
let plain = text(&mut s, "Source Sans 3");
|
||||
let fonts = |s: &mut Session, p: Value| s.execute("document.info", &p).unwrap()["fonts"].clone();
|
||||
assert_eq!(
|
||||
fonts(&mut s, json!({})),
|
||||
json!(["Mask Family Regular", "Pattern Family Regular", "Source Sans 3 Regular", "Symbol Mask Family Regular"])
|
||||
);
|
||||
s.execute("select.set", &json!({"ids": [plain.0]})).unwrap();
|
||||
assert_eq!(fonts(&mut s, json!({"selectionOnly": true})), json!(["Source Sans 3 Regular"]));
|
||||
}
|
||||
|
||||
/// Document Info › Objects gives the paths' total length in the document's units (#1958): an
|
||||
/// open line, a closed rectangle's perimeter and a compound path's pieces count, a guide doesn't.
|
||||
#[test]
|
||||
@@ -805,10 +845,10 @@ mod tests {
|
||||
let i = brushes(&mut s, json!({"category": "brushes"}));
|
||||
assert_eq!(i["brushNames"], json!(["Charcoal", "Dots"]));
|
||||
assert_eq!(i["brushes"], json!([{"name": "Charcoal", "type": "art"}, {"name": "Dots", "type": "scatter"}]));
|
||||
assert_eq!(i["sections"][0]["rows"], json!([["Charcoal", "Art brush"], ["Dots", "Scatter brush"]]));
|
||||
assert_eq!(i["sections"][0]["rows"], json!([["Charcoal", "Art Brush"], ["Dots", "Scatter Brush"]]));
|
||||
assert!(panel.contains(&json!("3 pt. Round")) && i["panelBrushNames"] == json!(panel));
|
||||
let text = brushes(&mut s, json!({"category": "brushes", "format": "text"}))["text"].as_str().unwrap().to_string();
|
||||
assert!(text.contains("\nBRUSHES\nCharcoal: Art brush\nDots: Scatter brush\n") && !text.contains("3 pt. Round"), "{text}");
|
||||
assert!(text.contains("\nBRUSHES\nCharcoal: Art Brush\nDots: Scatter Brush\n") && !text.contains("3 pt. Round"), "{text}");
|
||||
assert!(!text.contains("OBJECTS"), "{text}");
|
||||
// Selection Only: the brushes the selected objects' strokes use.
|
||||
let selected = |s: &mut Session| brushes(s, json!({"selectionOnly": true, "category": "brushes"}));
|
||||
@@ -818,10 +858,10 @@ mod tests {
|
||||
let i = selected(&mut s);
|
||||
assert_eq!(
|
||||
(i["brushes"].clone(), i["sections"][0]["rows"].clone()),
|
||||
(json!([{"name": "Dots", "type": "scatter"}]), json!([["Dots", "Scatter brush"]]))
|
||||
(json!([{"name": "Dots", "type": "scatter"}]), json!([["Dots", "Scatter Brush"]]))
|
||||
);
|
||||
let text = brushes(&mut s, json!({"selectionOnly": true, "category": "brushes", "format": "text"}))["text"].as_str().unwrap().to_string();
|
||||
assert!(text.contains("\nBRUSHES\nDots: Scatter brush\n") && !text.contains("Charcoal"), "{text}");
|
||||
assert!(text.contains("\nBRUSHES\nDots: Scatter Brush\n") && !text.contains("Charcoal"), "{text}");
|
||||
// A stroke without a brush lists none.
|
||||
let plain = s.execute("shape.rectangle", &json!({"x": 60, "y": 0, "width": 10, "height": 10})).unwrap()["id"].clone();
|
||||
s.execute("select.set", &json!({"ids": [plain]})).unwrap();
|
||||
|
||||
@@ -34,8 +34,15 @@ fn svg_filter_name(s: &Session, xml: &str, cmd: &str) -> Result<String> {
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
/// The effect commands of the Effect menu and the Appearance panel.
|
||||
/// The effect commands of the Effect menu and the Appearance panel, and the 3D and Materials
|
||||
/// panel's Graphics.
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
let mut specs = effect_specs();
|
||||
specs.extend(graphics::specs());
|
||||
specs
|
||||
}
|
||||
|
||||
fn effect_specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"effect.apply",
|
||||
@@ -454,6 +461,9 @@ pub(crate) fn apply(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let on = at.and_then(|at| at.effects_item(&n.appearance, C).ok()).flatten();
|
||||
let on = on.and_then(|i| Some((i, n.appearance.items.get(i)?.effects().len())));
|
||||
effects::validate_three_d_at_in(Some(d), n, id, &effect.params, on).map_err(|e| bad(C, e))?;
|
||||
if let Some(name) = effects::missing_symbol(d, &effect.params, None) {
|
||||
return Err(bad(C, format!("no symbol named `{name}`")));
|
||||
}
|
||||
if id == effects::REVOLVE && effects::has_revolve(n) {
|
||||
return Err(bad(C, "A profile supports one Revolve effect. Edit the existing effect's options instead."));
|
||||
}
|
||||
@@ -758,6 +768,9 @@ fn set_params(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let (Some(cur), Value::Object(new)) = (prospective.as_object_mut(), effects::upgrade_params(to, ¶ms)) {
|
||||
cur.extend(new);
|
||||
}
|
||||
if let Some(name) = effects::missing_symbol(d, &prospective, Some(&e.params)) {
|
||||
return Err(bad(C, format!("no symbol named `{name}`")));
|
||||
}
|
||||
if visible != Some(false) {
|
||||
effects::validate_three_d_at_in(Some(d), n, to, &prospective, target.map(|item| (item, index))).map_err(|e| bad(C, e))?;
|
||||
} else {
|
||||
@@ -1201,6 +1214,8 @@ fn expand_appearance(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
mod graphics;
|
||||
|
||||
#[cfg(test)]
|
||||
mod lighting_presets_tests;
|
||||
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
//! Window › 3D and Materials › Materials › Graphics: art kept as a document symbol (Add as Single
|
||||
//! Graphic) and mapped onto the planar faces of 3D objects (Extrude & Bevel's caps, a plane). The
|
||||
//! graphics an object shows are the `graphics` parameter of its 3D effect, which `effect.setParams`
|
||||
//! also edits.
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
use vectorcraft_doc::{Effect, Node};
|
||||
use vectorcraft_render::effects;
|
||||
|
||||
use super::super::appearance::{appearance_targets, index_param, item_target};
|
||||
use super::super::brushsym::{normalize, selection_art, store_symbol};
|
||||
use super::super::{CommandSpec, EngineError, NodeId, Result, Session, bad, cmd, has_doc, has_selection, str_param};
|
||||
use super::{effect_targets, ids_json};
|
||||
|
||||
pub(super) fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"threeD.addGraphic",
|
||||
"Add as Single Graphic",
|
||||
[],
|
||||
None,
|
||||
"{name?: string (default Graphic; made unique), ids?|id?} keep the selection's art as a new document symbol, listed among the 3D and Materials panel's Graphics; the selection stays as it is → {name}",
|
||||
has_selection,
|
||||
add_graphic
|
||||
),
|
||||
cmd!(
|
||||
"threeD.mapGraphic",
|
||||
"Map Graphic",
|
||||
[],
|
||||
None,
|
||||
"{symbol: name of a document symbol, face?: front|back (front; Plane has only a front; the back reads left to right as seen from behind), x?: pt, y?: pt (0, 0: the offset of the graphic's center from the center of the object's bounds, in the face's own plane), scale?: % (1..10000; default 100, or less so the graphic fits inside the object), rotation?: degrees (-360..360, 0; counterclockwise), index?: int (the 3D effect's position; default: each object's first 3D effect, its own before its fills' and strokes'), item?: appearance item index|null (refused without index; as effect.setParams), ids?|id?} add a graphic to the 3D effect of each selected object, drawn on that face turned, foreshortened and shaded with it and clipped to it; every object's 3D effect must be Extrude & Bevel (on a solid cap) or Rotate (the Plane type); one undo step → {ids, graphic}",
|
||||
has_doc,
|
||||
map_graphic
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// `threeD.addGraphic`: stores the selection's art as a symbol without replacing it with an
|
||||
/// instance.
|
||||
fn add_graphic(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "threeD.addGraphic";
|
||||
if let Some(key) = p.as_object().and_then(|m| m.keys().find(|k| !["name", "ids", "id"].contains(&k.as_str()))) {
|
||||
return Err(bad(C, format!("unknown parameter `{key}`")));
|
||||
}
|
||||
if p.get("name").is_some_and(|v| v.as_str().is_none_or(|n| n.trim().is_empty())) {
|
||||
return Err(bad(C, "`name` must be a name that is not empty"));
|
||||
}
|
||||
let mut art = selection_art(s, p)?.ok_or_else(|| EngineError::Other("nothing selected".into()))?;
|
||||
crate::cmd::threads::keep_shown_text(&s.doc()?.doc, &mut art);
|
||||
let (art, size, _) = normalize(art)?;
|
||||
let name = s.doc()?.doc.unique_symbol_name(str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()).unwrap_or("Graphic"));
|
||||
s.edit("Add as Single Graphic", |d, _| {
|
||||
store_symbol(d, &art, &name, size);
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({ "name": name }))
|
||||
}
|
||||
|
||||
/// The 3D effect of `n` that a graphic goes on when none is named: its own first, then its fills'
|
||||
/// and strokes', as the 3D and Materials panel finds it.
|
||||
fn first_three_d(n: &Node) -> Option<(Option<usize>, usize)> {
|
||||
let at = |fx: &[Effect]| fx.iter().position(|e| effects::is_three_d(&e.id));
|
||||
at(&n.appearance.effects)
|
||||
.map(|i| (None, i))
|
||||
.or_else(|| n.appearance.items.iter().enumerate().find_map(|(k, item)| at(item.effects()).map(|i| (Some(k), i))))
|
||||
}
|
||||
|
||||
/// `threeD.mapGraphic`: appends a graphic to the 3D effect of each target, after checking that every
|
||||
/// target takes it.
|
||||
fn map_graphic(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "threeD.mapGraphic";
|
||||
const KEYS: [&str; 10] = ["symbol", "face", "x", "y", "scale", "rotation", "index", "item", "ids", "id"];
|
||||
if let Some(key) = p.as_object().and_then(|m| m.keys().find(|k| !KEYS.contains(&k.as_str()))) {
|
||||
return Err(bad(C, format!("unknown parameter `{key}`")));
|
||||
}
|
||||
// An item names where the effect is in that item's list, so it takes an index too.
|
||||
if p.get("item").is_some() && p.get("index").is_none() {
|
||||
return Err(bad(C, "`item` needs `index`"));
|
||||
}
|
||||
// The graphic's own settings, checked as the effect checks them.
|
||||
let mut given = Map::new();
|
||||
for key in ["symbol", "face", "x", "y", "scale", "rotation"] {
|
||||
if let Some(v) = p.get(key) {
|
||||
given.insert(key.into(), v.clone());
|
||||
}
|
||||
}
|
||||
let base = effects::MappedGraphic::from_value(&Value::Object(given)).map_err(|e| bad(C, e))?;
|
||||
let d = &s.doc()?.doc;
|
||||
if !d.symbols.iter().any(|sym| sym.name == base.symbol) {
|
||||
return Err(bad(C, format!("no symbol named `{}`", base.symbol)));
|
||||
}
|
||||
let targets: Vec<(NodeId, Option<usize>, usize)> = if p.get("index").is_some() {
|
||||
let index = index_param(p, "index", C)?;
|
||||
let item = item_target(s, p, C)?;
|
||||
effect_targets(s, p, item, index, C)?
|
||||
} else {
|
||||
let mut out = vec![];
|
||||
for id in appearance_targets(s, p)? {
|
||||
let (item, index) = d.node(id).and_then(first_three_d).ok_or_else(|| bad(C, format!("object {} needs a 3D effect", id.0)))?;
|
||||
out.push((id, item, index));
|
||||
}
|
||||
out
|
||||
};
|
||||
if targets.is_empty() {
|
||||
return Err(bad(C, "select an object with a 3D effect"));
|
||||
}
|
||||
let mut edits = Vec::with_capacity(targets.len());
|
||||
for &(id, item, index) in &targets {
|
||||
let n = d.node(id).ok_or(EngineError::NoNode(id))?;
|
||||
let Some(effect) = n.appearance.effects_at(item).and_then(|fx| fx.get(index)).filter(|e| effects::is_three_d(&e.id)) else {
|
||||
return Err(bad(C, format!("object {} needs a 3D effect at index {index}", id.0)));
|
||||
};
|
||||
if !effects::takes_graphics(&effect.id) {
|
||||
return Err(bad(C, format!("object {}: Graphics map onto planar faces: use the Extrude or Plane object type", id.0)));
|
||||
}
|
||||
// A hollow Extrude & Bevel keeps the graphics it has, to show when Cap is turned on again,
|
||||
// but has no cap to show a new one on.
|
||||
let merged = effects::merged_params(&effect.id, &effect.params);
|
||||
let extrude = (effect.id == effects::EXTRUDE).then(|| effects::extrude_options(&merged));
|
||||
if extrude.is_some_and(|o| o.depth > 0.0 && !o.cap) {
|
||||
return Err(bad(C, format!("object {}: a hollow Extrude & Bevel has no caps: turn on Cap to map a graphic", id.0)));
|
||||
}
|
||||
// With a depth of 0 the art is a plane, which has only a front.
|
||||
if base.face == effects::PlanarFace::Back && extrude.is_some_and(|o| o.depth <= 0.0) {
|
||||
return Err(bad(C, format!("object {}: with a depth of 0 the object has one face: map its graphics onto the front", id.0)));
|
||||
}
|
||||
let mut graphic = base.clone();
|
||||
if p.get("scale").is_none()
|
||||
&& let (Some(frame), Some(art)) = (effects::graphic_frame_at(Some(d), n, item, index), effects::graphic_bounds(d, &graphic.symbol))
|
||||
{
|
||||
graphic.scale = frame.fitting_scale(art);
|
||||
}
|
||||
let params = effects::with_graphic(&effects::merged_params(&effect.id, &effect.params), &graphic);
|
||||
effects::validate_three_d_at_in(Some(d), n, &effect.id, ¶ms, item.map(|i| (i, index))).map_err(|e| bad(C, e))?;
|
||||
edits.push((id, item, index, graphic));
|
||||
}
|
||||
let first = edits.first().map(|(.., g)| g.to_value());
|
||||
let ids = s.edit("Map Graphic", |d, _| {
|
||||
let mut done = vec![];
|
||||
for (id, item, index, graphic) in &edits {
|
||||
let Some(e) = d.node_mut(*id).and_then(|n| n.appearance.effects_mut(*item)).and_then(|fx| fx.get_mut(*index)) else { continue };
|
||||
let current = effects::upgrade_params(&e.id, &e.params);
|
||||
e.params = effects::with_graphic(¤t, graphic);
|
||||
done.push(*id);
|
||||
}
|
||||
Ok(done)
|
||||
})?;
|
||||
Ok(json!({ "ids": ids_json(&ids), "graphic": first }))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "graphics_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,289 @@
|
||||
#![allow(clippy::unwrap_used, clippy::panic)]
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::NodeId;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// A 100 × 100 square with `effect`, and a 10 × 20 red rectangle beside it kept as graphic "Mark".
|
||||
fn setup(effect: &str) -> (Session, NodeId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width":400,"height":400})).unwrap();
|
||||
let mark = s.execute("path.create", &json!({"d":"M300 300L310 300L310 320L300 320Z"})).unwrap()["id"].clone();
|
||||
s.execute("paint.setFill", &json!({"color":"#ff0000"})).unwrap();
|
||||
let made = s.execute("threeD.addGraphic", &json!({"name": "Mark", "ids": [mark]})).unwrap();
|
||||
assert_eq!(made["name"], "Mark");
|
||||
let id = NodeId(s.execute("path.create", &json!({"d":"M100 100L200 100L200 200L100 200Z"})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setFill", &json!({"color":"#338ad6"})).unwrap();
|
||||
s.execute("effect.apply", &json!({"effect":effect,"item":null,"params":{"rotationX":0.0,"rotationY":0.0,"rotationZ":0.0}})).unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
fn graphics(s: &Session, id: NodeId) -> Value {
|
||||
s.doc().unwrap().doc.node(id).unwrap().appearance.effects[0].params["graphics"].clone()
|
||||
}
|
||||
|
||||
fn undo_len(s: &Session) -> usize {
|
||||
s.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_as_single_graphic_keeps_the_art_and_makes_a_symbol() {
|
||||
let (s, _) = setup("threeD.extrude");
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert!(d.symbols.iter().any(|sym| sym.name == "Mark"));
|
||||
// The art stays a plain path: no instance replaced it.
|
||||
let mut instances = 0;
|
||||
for l in &d.layers {
|
||||
l.walk(&mut |n| instances += usize::from(matches!(n.kind, vectorcraft_doc::NodeKind::SymbolInstance { .. })));
|
||||
}
|
||||
assert_eq!(instances, 0);
|
||||
// The 10 × 20 rectangle and its 1 pt stroke.
|
||||
assert_eq!(vectorcraft_render::effects::graphic_bounds(d, "Mark").map(|r| (r.width(), r.height())), Some((11.0, 21.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_graphic_adds_to_the_selected_object_in_one_undo_step_and_undoes() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let history = undo_len(&s);
|
||||
let r = s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "x": 5.0, "rotation": 30.0})).unwrap();
|
||||
assert_eq!(r["ids"], json!([id.0]));
|
||||
assert_eq!(graphics(&s, id), json!([{"symbol": "Mark", "face": "front", "x": 5.0, "y": 0.0, "scale": 100.0, "rotation": 30.0}]));
|
||||
assert_eq!(undo_len(&s), history + 1);
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "face": "back"})).unwrap();
|
||||
assert_eq!(graphics(&s, id).as_array().unwrap().len(), 2);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.layers, before.layers);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_large_graphic_starts_inside_its_object() {
|
||||
let (mut s, id) = setup("threeD.rotate");
|
||||
let big = s.execute("path.create", &json!({"d":"M0 0L400 0L400 200L0 200Z"})).unwrap()["id"].clone();
|
||||
s.execute("threeD.addGraphic", &json!({"name": "Big", "ids": [big]})).unwrap();
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Big", "ids": [id.0]})).unwrap();
|
||||
// 401 pt wide with its stroke, in a 100 pt square.
|
||||
let scale = graphics(&s, id)[0]["scale"].as_f64().unwrap();
|
||||
assert!((scale - 100.0 * 100.0 / 401.0).abs() < 1e-9, "{scale}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_graphic_refuses_what_cannot_take_it_and_changes_nothing() {
|
||||
let (mut s, id) = setup("threeD.inflate");
|
||||
let history = undo_len(&s);
|
||||
let e = s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "ids": [id.0]})).unwrap_err().to_string();
|
||||
assert!(e.contains("Extrude or Plane"), "{e}");
|
||||
let (mut s, id) = setup("threeD.rotate");
|
||||
for (p, says) in [
|
||||
(json!({"symbol": "Gone"}), "no symbol named"),
|
||||
(json!({"face": "front"}), "needs `symbol`"),
|
||||
(json!({"symbol": "Mark", "face": "back"}), "one face"),
|
||||
(json!({"symbol": "Mark", "scale": 0.0}), "scale"),
|
||||
(json!({"symbol": "Mark", "tile": true}), "`tile`"),
|
||||
(json!({"symbol": "Mark", "item": 0}), "`item` needs `index`"),
|
||||
(json!({"symbol": "Mark", "item": null}), "`item` needs `index`"),
|
||||
] {
|
||||
let mut p = p;
|
||||
p["ids"] = json!([id.0]);
|
||||
let e = s.execute("threeD.mapGraphic", &p).unwrap_err().to_string();
|
||||
assert!(e.contains(says), "{p}: {e}");
|
||||
}
|
||||
assert_eq!(undo_len(&s), history);
|
||||
// An object without a 3D effect.
|
||||
let plain = s.execute("path.create", &json!({"d":"M0 0L10 0L10 10Z"})).unwrap()["id"].clone();
|
||||
let e = s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "ids": [plain]})).unwrap_err().to_string();
|
||||
assert!(e.contains("3D effect"), "{e}");
|
||||
// A hollow Extrude & Bevel has no cap to show a new graphic on.
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("effect.setParams", &json!({"index": 0, "item": null, "params": {"cap": false}})).unwrap();
|
||||
let history = undo_len(&s);
|
||||
let e = s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "ids": [id.0]})).unwrap_err().to_string();
|
||||
assert!(e.contains("turn on Cap"), "{e}");
|
||||
assert_eq!(undo_len(&s), history);
|
||||
// With a depth of 0 there is no back.
|
||||
s.execute("effect.setParams", &json!({"index": 0, "item": null, "params": {"cap": true, "depth": 0.0}})).unwrap();
|
||||
let history = undo_len(&s);
|
||||
let e = s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "face": "back", "ids": [id.0]})).unwrap_err().to_string();
|
||||
assert!(e.contains("one face"), "{e}");
|
||||
assert_eq!(undo_len(&s), history);
|
||||
assert!(s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "ids": [id.0]})).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_params_edits_graphics_and_checks_them() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark"})).unwrap();
|
||||
let moved = json!([{"symbol": "Mark", "face": "front", "x": -12.0, "y": 8.0, "scale": 150.0, "rotation": 0.0}]);
|
||||
s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "params": {"graphics": moved}})).unwrap();
|
||||
assert_eq!(graphics(&s, id), moved);
|
||||
let bad = json!({"ids": [id.0], "index": 0, "item": null, "params": {"graphics": [{"symbol": "Mark", "face": "top"}]}});
|
||||
assert!(s.execute("effect.setParams", &bad).is_err());
|
||||
let null = json!({"ids": [id.0], "index": 0, "item": null, "params": {"graphics": null}});
|
||||
assert!(s.execute("effect.setParams", &null).is_err());
|
||||
assert_eq!(graphics(&s, id), moved);
|
||||
// Extrude to Plane keeps the graphics; to Inflate drops them.
|
||||
s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "threeDType": "plane"})).unwrap();
|
||||
assert_eq!(graphics(&s, id), moved);
|
||||
s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "threeDType": "inflate"})).unwrap();
|
||||
assert_eq!(graphics(&s, id), Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effect_commands_refuse_an_unknown_symbol_but_keep_a_deleted_ones_graphic() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
let unknown = json!([{"symbol": "Nowhere"}]);
|
||||
let history = undo_len(&s);
|
||||
assert!(s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "params": {"graphics": unknown}})).is_err());
|
||||
s.execute("path.create", &json!({"d":"M0 0L50 0L50 50L0 50Z"})).unwrap();
|
||||
let created = undo_len(&s);
|
||||
assert!(s.execute("effect.apply", &json!({"effect": "threeD.extrude", "item": null, "params": {"graphics": unknown}})).is_err());
|
||||
assert_eq!((history + 1, undo_len(&s)), (created, created));
|
||||
// A graphic whose symbol was deleted stays while the effect's other settings change.
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "ids": [id.0]})).unwrap();
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).symbols.retain(|sym| sym.name != "Mark");
|
||||
s.execute("effect.setParams", &json!({"ids": [id.0], "index": 0, "item": null, "params": {"depth": 30.0}})).unwrap();
|
||||
assert_eq!(graphics(&s, id)[0]["symbol"], "Mark");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_a_graphic_uses_is_used_and_follows_a_rename() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark"})).unwrap();
|
||||
let unused = s.execute("symbol.unused", &json!({})).unwrap();
|
||||
assert!(!unused["names"].as_array().unwrap().iter().any(|n| n == "Mark"), "{unused}");
|
||||
// A 3D object with the graphic kept in another symbol's art follows the rename too.
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("edit.copy", &json!({})).unwrap();
|
||||
s.execute("edit.paste", &json!({})).unwrap();
|
||||
s.execute("symbol.new", &json!({"name": "Holder"})).unwrap();
|
||||
s.execute("symbol.options", &json!({"name": "Mark", "newName": "Label"})).unwrap();
|
||||
assert_eq!(graphics(&s, id)[0]["symbol"], "Label");
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let holder = d.symbols.iter().find(|sym| sym.name == "Holder").unwrap();
|
||||
let mut held = vec![];
|
||||
holder.art.walk(&mut |n| held.extend(vectorcraft_render::effects::graphic_symbols(n)));
|
||||
assert_eq!(held, vec!["Label".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rename_reaches_graphics_in_graph_designs_and_opacity_masks() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark"})).unwrap();
|
||||
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let held = d.node(id).unwrap().clone();
|
||||
d.graph_designs.push(vectorcraft_doc::GraphDesign { name: "Bars".into(), art: std::sync::Arc::new(held.clone()) });
|
||||
d.graphic_styles.push(vectorcraft_doc::GraphicStyle::new("Boxed", (*held.appearance).clone()));
|
||||
let mut lib = vectorcraft_brush::library(d);
|
||||
let scatter = lib.iter_mut().find(|b| !b.art().is_empty()).unwrap();
|
||||
*scatter.art_mut()[0] = held.clone();
|
||||
vectorcraft_brush::store(d, &lib);
|
||||
d.node_mut(id).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(held, true)));
|
||||
s.execute("symbol.options", &json!({"name": "Mark", "newName": "Label"})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let style = d.graphic_styles.iter().find(|g| g.name == "Boxed").unwrap();
|
||||
assert_eq!(vectorcraft_render::effects::appearance_graphic_symbols(&style.appearance), vec!["Label".to_string()]);
|
||||
let brush: Vec<String> =
|
||||
vectorcraft_brush::library(d).iter().flat_map(|b| b.art()).flat_map(vectorcraft_render::effects::graphic_symbols).collect();
|
||||
assert_eq!(brush, vec!["Label".to_string()]);
|
||||
let names = |n: &vectorcraft_doc::Node| {
|
||||
let mut out = vec![];
|
||||
n.walk(&mut |c| out.extend(vectorcraft_render::effects::graphic_symbols(c)));
|
||||
out
|
||||
};
|
||||
assert_eq!(names(&d.graph_designs[0].art), vec!["Label".to_string()]);
|
||||
let mask = d.node(id).unwrap().extras().mask.as_deref().map(|m| names(&m.art));
|
||||
assert_eq!(mask, Some(vec!["Label".to_string()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pasted_into_another_document_a_graphic_brings_its_symbol() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark"})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("edit.copy", &json!({})).unwrap();
|
||||
// The symbol the pasted object's graphic names, and the size of that symbol's art.
|
||||
let pasted = |s: &mut Session| {
|
||||
s.execute("edit.paste", &json!({})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
let n = st.doc.node(st.selection.objects[0]).unwrap();
|
||||
let name = n.appearance.effects[0].params["graphics"][0]["symbol"].as_str().unwrap().to_string();
|
||||
let size = vectorcraft_render::effects::graphic_bounds(&st.doc, &name).map(|r| (r.width(), r.height()));
|
||||
(name, size)
|
||||
};
|
||||
// A document without the symbol gets it.
|
||||
s.execute("file.new", &json!({"width":400,"height":400})).unwrap();
|
||||
assert_eq!(pasted(&mut s), ("Mark".to_string(), Some((11.0, 21.0))));
|
||||
// A document whose symbol "Mark" is other art gets the copied symbol under a free name.
|
||||
s.execute("file.new", &json!({"width":400,"height":400})).unwrap();
|
||||
let other = s.execute("path.create", &json!({"d":"M0 0L50 0L50 50L0 50Z"})).unwrap()["id"].clone();
|
||||
s.execute("threeD.addGraphic", &json!({"name": "Mark", "ids": [other]})).unwrap();
|
||||
let (name, size) = pasted(&mut s);
|
||||
assert_ne!(name, "Mark");
|
||||
assert_eq!(size, Some((11.0, 21.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_only_a_graphic_style_uses_is_used() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark"})).unwrap();
|
||||
let appearance = (*s.doc().unwrap().doc.node(id).unwrap().appearance).clone();
|
||||
s.execute("effect.setParams", &json!({"index": 0, "item": null, "params": {"graphics": []}})).unwrap();
|
||||
let unused = |s: &mut Session| s.execute("symbol.unused", &json!({})).unwrap()["names"].clone();
|
||||
assert!(unused(&mut s).as_array().unwrap().iter().any(|n| n == "Mark"));
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).graphic_styles.push(vectorcraft_doc::GraphicStyle::new("Boxed", appearance));
|
||||
assert!(!unused(&mut s).as_array().unwrap().iter().any(|n| n == "Mark"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_graphic_refuses_bad_parameters_and_changes_nothing() {
|
||||
let (mut s, _) = setup("threeD.extrude");
|
||||
let art = s.execute("path.create", &json!({"d":"M0 0L10 0L10 10Z"})).unwrap()["id"].clone();
|
||||
let (symbols, history) = (s.doc().unwrap().doc.symbols.len(), undo_len(&s));
|
||||
for p in [
|
||||
json!({"name": 5, "ids": [art]}),
|
||||
json!({"name": null, "ids": [art]}),
|
||||
json!({"name": "", "ids": [art]}),
|
||||
json!({"name": " ", "ids": [art]}),
|
||||
json!({"tile": true, "ids": [art]}),
|
||||
] {
|
||||
assert!(s.execute("threeD.addGraphic", &p).is_err(), "{p}");
|
||||
}
|
||||
assert_eq!((s.doc().unwrap().doc.symbols.len(), undo_len(&s)), (symbols, history));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_graphic_refuses_a_selection_with_an_object_without_a_3d_effect() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
let plain = s.execute("path.create", &json!({"d":"M0 0L10 0L10 10Z"})).unwrap()["id"].as_u64().unwrap();
|
||||
let history = undo_len(&s);
|
||||
let e = s.execute("threeD.mapGraphic", &json!({"symbol": "Mark", "ids": [id.0, plain]})).unwrap_err().to_string();
|
||||
assert!(e.contains(&format!("object {plain} needs a 3D effect")), "{e}");
|
||||
assert_eq!(undo_len(&s), history);
|
||||
assert_eq!(graphics(&s, id).as_array().map_or(0, Vec::len), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_graphic_is_drawn_on_the_object() {
|
||||
let (mut s, id) = setup("threeD.extrude");
|
||||
s.execute("threeD.mapGraphic", &json!({"symbol": "Mark"})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let art = vectorcraft_render::effects::solid_art_in(Some(d), d.node(id).unwrap()).unwrap();
|
||||
let mut red = None;
|
||||
art.walk(&mut |n| {
|
||||
if let (Some(p), Some(vectorcraft_doc::AppearanceItem::Fill(f))) = (n.path_data(), n.appearance.items.first())
|
||||
&& f.paint.color().is_some_and(|c| c.to_rgb()[0] > 0.3 && c.to_rgb()[1] < 0.1)
|
||||
{
|
||||
red = p.bounds();
|
||||
}
|
||||
});
|
||||
// Seen straight on, the 10 × 20 graphic sits at the center of the square.
|
||||
let r = red.unwrap();
|
||||
assert!(
|
||||
(r.center().x - 150.0).abs() < 1e-6
|
||||
&& (r.center().y - 150.0).abs() < 1e-6
|
||||
&& (r.width() - 10.0).abs() < 1e-6
|
||||
&& (r.height() - 20.0).abs() < 1e-6,
|
||||
"{r:?}"
|
||||
);
|
||||
}
|
||||
@@ -408,15 +408,31 @@ pub(crate) fn anti_alias(id: &str) -> std::result::Result<AntiAlias, String> {
|
||||
|
||||
/// The `useArtboards` param of a PDF, AI, DXF or raster export (SVG has its own): `true` writes every
|
||||
/// chosen artboard (default all), `false` the bounds of the visible art instead, absent one
|
||||
/// artboard (raster, DXF) or the chosen pages (PDF, AI).
|
||||
fn use_artboards(p: &Value) -> Result<Option<bool>> {
|
||||
/// artboard (raster, DXF) or the chosen pages (PDF, AI). Errors name the command `cmd`.
|
||||
pub(super) fn use_artboards(cmd: &str, p: &Value) -> Result<Option<bool>> {
|
||||
match p.get("useArtboards") {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(Value::Bool(b)) => Ok(Some(*b)),
|
||||
Some(v) => Err(bad(C, format!("useArtboards must be true or false, not {v}"))),
|
||||
Some(v) => Err(bad(cmd, format!("useArtboards must be true or false, not {v}"))),
|
||||
}
|
||||
}
|
||||
|
||||
/// With `useArtboards: false` in `p`: `doc` without its opacity-mask or symbol editing layer and
|
||||
/// with one artboard, the bounds of its visible art, and `p` without the artboard choice. `None`
|
||||
/// when `useArtboards` is absent, null or true. A document with no visible art is refused.
|
||||
pub(super) fn art_page(cmd: &str, doc: &Document, p: &Value) -> Result<Option<(Document, Value)>> {
|
||||
if use_artboards(cmd, p)? != Some(false) {
|
||||
return Ok(None);
|
||||
}
|
||||
let doc = &*doc.without_edit_modes();
|
||||
let bounds = vectorcraft_render::encode::art_bounds(doc).ok_or_else(|| bad(cmd, "nothing to export: the document has no visible art"))?;
|
||||
let mut q = super::export::without_artboards(p);
|
||||
if let Some(o) = q.as_object_mut() {
|
||||
o.remove("useArtboards");
|
||||
}
|
||||
Ok(Some((single_artboard(doc, bounds, "Art"), q)))
|
||||
}
|
||||
|
||||
/// The `rect` param of a raster export: `[x, y, width, height]` in points, y down. `None` when
|
||||
/// absent; an error when it is not four finite numbers with a positive size, when the format isn't
|
||||
/// a raster one, or when it comes with an artboard choice (`rect` is the area, so those would
|
||||
@@ -596,7 +612,7 @@ fn encode_files(doc: &Document, f: &Format, p: &Value) -> Result<Encoded> {
|
||||
let doc = &*doc.without_edit_modes();
|
||||
let n = doc.artboards.len();
|
||||
// SVG reads its own `useArtboards` (an SVG option).
|
||||
let use_artboards = if f.raster || matches!(f.id, "pdf" | "ai" | "dxf" | "emf" | "wmf" | "hpgl") { use_artboards(p)? } else { None };
|
||||
let use_artboards = if f.raster || matches!(f.id, "pdf" | "ai" | "dxf" | "emf" | "wmf" | "hpgl") { use_artboards(C, p)? } else { None };
|
||||
// `rect`: that area of the page, as if it were the only artboard (the document is not changed).
|
||||
if let Some(rect) = page_rect(f, p)? {
|
||||
let mut q = super::export::without_artboards(p);
|
||||
@@ -606,13 +622,17 @@ fn encode_files(doc: &Document, f: &Format, p: &Value) -> Result<Encoded> {
|
||||
}
|
||||
return encode_files(&single_artboard(doc, rect, "Rect"), f, &q);
|
||||
}
|
||||
if use_artboards == Some(false) {
|
||||
let bounds = vectorcraft_render::encode::art_bounds(doc).ok_or_else(|| bad(C, "nothing to export: the document has no visible art"))?;
|
||||
let mut q = super::export::without_artboards(p);
|
||||
if let Some(o) = q.as_object_mut() {
|
||||
o.remove("useArtboards");
|
||||
}
|
||||
return encode_files(&single_artboard(doc, bounds, "Art"), f, &q);
|
||||
// A PDF of the art's bounds carries no editing data (it would replace the artboards).
|
||||
if use_artboards == Some(false)
|
||||
&& f.id == "pdf"
|
||||
&& let Some((bytes, notes)) = super::pdf::encode_art_page(C, doc, p)?
|
||||
{
|
||||
return Ok(Encoded::one_with_notes(bytes, notes));
|
||||
}
|
||||
if use_artboards == Some(false)
|
||||
&& let Some((art, q)) = art_page(C, doc, p)?
|
||||
{
|
||||
return encode_files(&art, f, &q);
|
||||
}
|
||||
let bytes = match f.id {
|
||||
"vectorcraft" => super::native::encode(C, f, doc, p)?,
|
||||
|
||||
@@ -782,9 +782,9 @@ pub fn open_extensions() -> &'static [&'static str] {
|
||||
|
||||
/// File › Open's filters: "All readable files" first, then one per readable format, then one per
|
||||
/// importer plug-in (under its name, with the extensions [`open_extensions`] takes from it), then
|
||||
/// swatch libraries (which open in the library panel), Libraries panel libraries, flattener, PDF,
|
||||
/// print and export presets (imported), scripts (checked without running them) and plug-ins
|
||||
/// (installed).
|
||||
/// swatch and graphic style libraries (which open in their library panels), Libraries panel
|
||||
/// libraries, flattener, PDF, print, perspective grid and export presets and composite fonts
|
||||
/// (imported), shortcut sets, scripts (checked without running them) and plug-ins (installed).
|
||||
pub fn open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
|
||||
let (all, importers) = importer_open_part();
|
||||
std::iter::once(("All readable files", all)).chain(format_filters()).chain(importers).chain(other_open_filters())
|
||||
@@ -797,14 +797,17 @@ pub fn builtin_open_filters() -> impl Iterator<Item = (&'static str, &'static [&
|
||||
std::iter::once(("All readable files", OPEN_EXTS)).chain(format_filters()).chain(other_open_filters())
|
||||
}
|
||||
|
||||
/// The open-dialog filters after the document formats: swatch libraries, Libraries panel
|
||||
/// libraries, presets, composite fonts, shortcut sets, scripts and plug-ins.
|
||||
/// The open-dialog filters after the document formats: swatch and graphic style libraries,
|
||||
/// Libraries panel libraries, presets (perspective grid presets among them), composite fonts,
|
||||
/// shortcut sets, scripts and plug-ins.
|
||||
fn other_open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
|
||||
std::iter::once(("Swatch libraries", crate::cmd::swatchlib::LIBRARY_EXTS))
|
||||
.chain(std::iter::once(("Graphic Style Libraries", std::slice::from_ref(&vectorcraft_doc::style_libs::STYLES_EXT))))
|
||||
.chain(std::iter::once(("Libraries", crate::cmd::library::LIBRARY_EXTS)))
|
||||
.chain(std::iter::once(("Flattener presets", crate::cmd::flatten::PRESET_EXTS)))
|
||||
.chain(std::iter::once(("PDF presets", crate::cmd::pdfcmds::PRESET_EXTS)))
|
||||
.chain(std::iter::once(("Print presets", crate::cmd::printpresets::PRESET_EXTS)))
|
||||
.chain(std::iter::once(("Perspective grid presets", crate::cmd::perspgrid::PRESET_EXTS)))
|
||||
.chain(std::iter::once(("Export presets", screens::PRESET_FILE_EXTS)))
|
||||
.chain(std::iter::once(("Composite fonts", crate::cmd::compositefonts::EXTS)))
|
||||
.chain(std::iter::once(("Shortcut sets", crate::cmd::shortcutset::EXTS)))
|
||||
@@ -815,13 +818,13 @@ fn other_open_filters() -> impl Iterator<Item = (&'static str, &'static [&'stati
|
||||
/// Open-dialog filters for Swatch Libraries and Graphic Style Libraries Other Library… and Locate
|
||||
/// Library…. The library formats come first so Linux file portals (which grey out files that don't
|
||||
/// match the default filter) still list `.ase` and the other library extensions; then the same
|
||||
/// filters as [`builtin_open_filters`] so a document can be picked for its swatches or graphic
|
||||
/// styles.
|
||||
/// filters as [`builtin_open_filters`], without a second copy of the library filter, so a document
|
||||
/// can be picked for its swatches or graphic styles.
|
||||
pub fn library_open_filters(
|
||||
library_label: &'static str,
|
||||
library_exts: &'static [&'static str],
|
||||
) -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
|
||||
std::iter::once((library_label, library_exts)).chain(builtin_open_filters())
|
||||
std::iter::once((library_label, library_exts)).chain(builtin_open_filters().filter(move |f| *f != (library_label, library_exts)))
|
||||
}
|
||||
|
||||
/// File → Place dialog filters: "All placeable files", then one per placeable format, then text,
|
||||
|
||||
@@ -128,7 +128,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Export PDF",
|
||||
[],
|
||||
None,
|
||||
"{path?, preset?: \"VectorCraft Default\" (built-in or saved: pdf.preset.list), artboard? | artboards?: [i…] | range?: \"1-3, 5\" (1-based; default all, one page each), standard?: none|pdfA2b|pdfX1a (CMYK, grey and spot colours only: colours and images converted to output.destination or the CMYK output intent, untagged; transparency flattened with flattenerPreset; PDF 1.3)|pdfX3 (colours ICC-tagged; transparency flattened; PDF 1.3)|pdfX4 (transparency and layers kept; colours ICC-tagged; PDF 1.6 at most) (PDF/X: a /GTS_PDFX output intent, by default the CMYK profile in effect, embedded; TrimBox, Trapped, GTS_PDFXVersion, title and dates; fonts embedded or outlined), compatibility?: 1.3 (no transparency: it is flattened with flattenerPreset, without the layers the file leaves out; images keep no see-through pixels)|1.4|1.5|1.6|1.7 (default)|2.0 (choosing a standard sets its version: 1.7 for pdfA2b, 1.6 for pdfX4, 1.3 for pdfX1a and pdfX3, which write 1.4 as 1.3 too; pdfA2b refuses 1.3; a page with a side longer than 14,400 pt keeps its size with a UserUnit from 1.6 on: 1.5 is then written as 1.6, with 1.5's encryption, and so is 1.4 in pdfA2b files, with a warning; PDF/X files and other 1.3 and 1.4 files write it in points, and 1.3 and 1.4 (pdfX1a and pdfX3 too) refuse a page past 32,767 pt whose size or art needs a real number past 32,767; a page whose short side is 3 pt or longer but would be under 3 units at that UserUnit gets a smaller one that keeps the short side at 3 units; a warning gives each such page's size), longPagesInPoints?: false (true: such a page is written at its size in points, without a UserUnit, at every version, and 1.5 stays 1.5, in files without a standard (PDF/A files keep the UserUnit); apps that keep to the limit cut it to 200 in), preserveEditing? (on in VectorCraft Default: the native document as an embedded file, which document.open restores; choosing a standard turns it off, a standard refuses it), thumbnails? (each page drawn as a /Thumb image, 106 px on its long side), fastWebView? (a linearised file: the first page shows while the rest downloads; it stays linearised when encrypted), viewAfterSaving? (the app opens the written file), createLayers? (each layer and sublayer but template layers as a PDF layer, an optional content group listed under its layer's: hidden layers off, non-printing ones /PrintState /OFF, locked ones locked; needs 1.5 or later; they reopen as layers; choosing pdfX1a or pdfX3 turns it off; they refuse it), includeNonPrinting? (keep layers whose Print option is off; left out by default unless createLayers), flattenerPreset?: \"\" (High Resolution) | high | medium | low | a saved preset (flattener.presets.list): what PDF 1.3 files flatten transparency with, flattener?: {object.flattenTransparency options over it}, compression?: {color?, gray?: {downsample: none|average|subsample|bicubic, ppi: 300, abovePpi: 450, compression: none|zip|jpeg|jpeg2000|auto, quality: minimum|low|medium|high|maximum}, mono?: {downsample, ppi: 1200, abovePpi: 1800, compression: none|ccittG3|ccittG4|zip|runLength} (black-and-white images), compressText?: true} (images above abovePpi are resampled to ppi; auto keeps JPEGs JPEG, the others lossless; JPEG needs opaque images; none, jpeg2000, CCITT and runLength are written as ZIP with a warning), marks?: {trim, registration, colorBars, pageInfo, kind: roman|japanese, weight: 0.25 (trim marks and targets), offset: 6 (pt from the artboard, at least the bleed)} (printer's marks in [Registration], every plate: trim marks, registration targets mid-side, CMYK/spot/black-tint colour bars on top, page information (title, artboard, date UTC) below), bleed?: {useDocument (the document's bleed, document.setup), top, bottom, left, right} (pt; art in the bleed is kept; a PDF/X file with no bleed on a side where the document's bleed has one comes back with a warning). Each page: TrimBox = artboard, BleedBox = artboard + bleed, MediaBox = that + the marks' room (art clipped to the BleedBox), output?: {conversion: none (default)|destination (every colour into the destination profile's model, CMYK or RGB, also colours of that model in another profile)|preserveNumbers (only colours of the other model and Lab; those in the destination's model keep their numbers) (grey stays grey; images are converted too, printer's marks are not), destination: profile name (edit.colorSettings lists them; default: the document's profile for its colour mode), profiles: none (device colours)|all|destination|taggedSource (documents assigned a profile: edit.assignProfile) (ICC-based colours: CMYK with the destination or document CMYK profile, RGB as sRGB, grey with the sRGB tone curve; PDF/A, pdfX3 and pdfX4 always, whatever profiles says), outputIntent: profile name (embedded as the catalog's /GTS_PDFX output intent; a name that isn't a profile is only named, which pdfX1a and pdfX3 allow only with a registry and pdfX4 refuses; pdfX1a needs a CMYK one), out
Line truncated
|
||||
"{path?, preset?: \"VectorCraft Default\" (built-in or saved: pdf.preset.list), artboard? | artboards?: [i…] | range?: \"1-3, 5\" (1-based; default all, one page each), useArtboards?: true (false: one page, the bounds of the visible art, as document.export writes it; artboard, artboards and range are then ignored, Preserve Editing is left out (with a warning when it is on), and a document with no visible art is refused), standard?: none|pdfA2b|pdfX1a (CMYK, grey and spot colours only: colours and images converted to output.destination or the CMYK output intent, untagged; transparency flattened with flattenerPreset; PDF 1.3)|pdfX3 (colours ICC-tagged; transparency flattened; PDF 1.3)|pdfX4 (transparency and layers kept; colours ICC-tagged; PDF 1.6 at most) (PDF/X: a /GTS_PDFX output intent, by default the CMYK profile in effect, embedded; TrimBox, Trapped, GTS_PDFXVersion, title and dates; fonts embedded or outlined), compatibility?: 1.3 (no transparency: it is flattened with flattenerPreset, without the layers the file leaves out; images keep no see-through pixels)|1.4|1.5|1.6|1.7 (default)|2.0 (choosing a standard sets its version: 1.7 for pdfA2b, 1.6 for pdfX4, 1.3 for pdfX1a and pdfX3, which write 1.4 as 1.3 too; pdfA2b refuses 1.3; a page with a side longer than 14,400 pt keeps its size with a UserUnit from 1.6 on: 1.5 is then written as 1.6, with 1.5's encryption, and so is 1.4 in pdfA2b files, with a warning; PDF/X files and other 1.3 and 1.4 files write it in points, and 1.3 and 1.4 (pdfX1a and pdfX3 too) refuse a page past 32,767 pt whose size or art needs a real number past 32,767; a page whose short side is 3 pt or longer but would be under 3 units at that UserUnit gets a smaller one that keeps the short side at 3 units; a warning gives each such page's size), longPagesInPoints?: false (true: such a page is written at its size in points, without a UserUnit, at every version, and 1.5 stays 1.5, in files without a standard (PDF/A files keep the UserUnit); apps that keep to the limit cut it to 200 in), preserveEditing? (on in VectorCraft Default: the native document as an embedded file, which document.open restores; choosing a standard turns it off, a standard refuses it), thumbnails? (each page drawn as a /Thumb image, 106 px on its long side), fastWebView? (a linearised file: the first page shows while the rest downloads; it stays linearised when encrypted), viewAfterSaving? (the app opens the written file), createLayers? (each layer and sublayer but template layers as a PDF layer, an optional content group listed under its layer's: hidden layers off, non-printing ones /PrintState /OFF, locked ones locked; needs 1.5 or later; they reopen as layers; choosing pdfX1a or pdfX3 turns it off; they refuse it), includeNonPrinting? (keep layers whose Print option is off; left out by default unless createLayers), flattenerPreset?: \"\" (High Resolution) | high | medium | low | a saved preset (flattener.presets.list): what PDF 1.3 files flatten transparency with, flattener?: {object.flattenTransparency options over it}, compression?: {color?, gray?: {downsample: none|average|subsample|bicubic, ppi: 300, abovePpi: 450, compression: none|zip|jpeg|jpeg2000|auto, quality: minimum|low|medium|high|maximum}, mono?: {downsample, ppi: 1200, abovePpi: 1800, compression: none|ccittG3|ccittG4|zip|runLength} (black-and-white images), compressText?: true} (images above abovePpi are resampled to ppi; auto keeps JPEGs JPEG, the others lossless; JPEG needs opaque images; none, jpeg2000, CCITT and runLength are written as ZIP with a warning), marks?: {trim, registration, colorBars, pageInfo, kind: roman|japanese, weight: 0.25 (trim marks and targets), offset: 6 (pt from the artboard, at least the bleed)} (printer's marks in [Registration], every plate: trim marks, registration targets mid-side, CMYK/spot/black-tint colour bars on top, page information (title, artboard, date UTC) below), bleed?: {useDocument (the document's bleed, document.setup), top, bottom, left, right} (pt; art in the bleed is kept; a PDF/X file with no bleed on a side where the document's bleed has one comes back with a warning). Each page: TrimBox = artboard, BleedBox = artboard + bleed, MediaBox = that + the marks' room (art clipped to the BleedBox), output?: {conversion: none (default)|destination (every colour into the destination profile's model, CMYK or RGB, also colours of that model in another profile)|preserveNumbers (only colours of the other model and Lab; those in the destination's model keep their numbers) (grey stays grey; images are converted too, printer's marks are not), destination: profile name (edit.colorSettings lists them; default: the document's profile for its colour mode), profiles: none (device colours)|all|destination|taggedSource (documents assigned a profile: edit.assignProfile) (ICC-based colours: CMYK with the destination or document CMYK profile, RGB as sRGB, grey with the sRGB tone curve; PDF/A, pdfX3 and pdf
Line truncated
|
||||
has_doc,
|
||||
export_pdf
|
||||
),
|
||||
@@ -137,7 +137,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"PDF Settings",
|
||||
[],
|
||||
None,
|
||||
"{preset?, includeDocument?: false, …options of document.exportPdf} → {settings (the preset with the options applied), presets: [name…], changed: [{option: \"compression.compressText\", value}] (what differs from the preset), warnings}; includeDocument also exports the active document in memory and adds its warnings (knockout groups approximated, effects left out…). A key PDF doesn't take, and an unknown key inside one of the option objects, are refused",
|
||||
"{preset?, includeDocument?: false, …options of document.exportPdf} → {settings (the preset with the options applied), presets: [name…], changed: [{option: \"compression.compressText\", value}] (what differs from the preset), warnings}; includeDocument also exports the active document in memory (one page of the art's bounds with useArtboards: false) and adds its warnings (knockout groups approximated, effects left out…). A key PDF doesn't take, and an unknown key inside one of the option objects, are refused",
|
||||
always,
|
||||
pdf_settings
|
||||
),
|
||||
@@ -280,6 +280,27 @@ fn options_at(cmd: &str, doc: &Document, p: &Value, now: Option<i64>) -> Result<
|
||||
pub const EDITING_NEEDS_EVERY_ARTBOARD: &str =
|
||||
"Preserve editing was left out: it needs a PDF of every artboard, in order (reopened, this one would show the others too)";
|
||||
|
||||
/// Why a PDF of the art's bounds (`useArtboards: false`) carries no editing data.
|
||||
pub const EDITING_NEEDS_THE_ARTBOARDS: &str =
|
||||
"Preserve editing was left out: it needs a PDF of the artboards (reopened, this one would replace them with one artboard around the art)";
|
||||
|
||||
/// With `useArtboards: false` in `p`: the PDF of one page, the bounds of `doc`'s visible art
|
||||
/// ([`super::encode::art_page`]) → (bytes, notes). It carries no editing data: with Preserve
|
||||
/// Editing on, the notes end with [`EDITING_NEEDS_THE_ARTBOARDS`]. `None` when `p` asks for the
|
||||
/// artboards.
|
||||
pub(super) fn encode_art_page(cmd: &str, doc: &Document, p: &Value) -> Result<Option<(Vec<u8>, Vec<Note>)>> {
|
||||
let Some((art, mut q)) = super::encode::art_page(cmd, doc, p)? else { return Ok(None) };
|
||||
let left_out = settings(cmd, &q)?.preserve_editing;
|
||||
if left_out && let Some(o) = q.as_object_mut() {
|
||||
o.insert("preserveEditing".into(), json!(false));
|
||||
}
|
||||
let (bytes, mut notes) = encode(cmd, &art, &q)?;
|
||||
if left_out {
|
||||
notes.push(Note::setting(EDITING_NEEDS_THE_ARTBOARDS));
|
||||
}
|
||||
Ok(Some((bytes, notes)))
|
||||
}
|
||||
|
||||
/// The warning for a PDF/X file that leaves out the document's bleed
|
||||
/// ([`vectorcraft_pdf::PdfSettings::leaves_out_bleed`]).
|
||||
pub const BLEED_LEFT_OUT: &str = "the document sets a bleed that this PDF/X file leaves out: turn on Use document bleed settings";
|
||||
@@ -482,7 +503,12 @@ fn export_pdf(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
} else {
|
||||
q = json!({ "_displayDocTitle": display_doc_title });
|
||||
}
|
||||
let (bytes, notes) = encode(C, titled.as_ref().unwrap_or(&st.doc), &q)?;
|
||||
// The opacity-mask or symbol editing layer is left out.
|
||||
let doc = titled.as_ref().unwrap_or(&st.doc).without_edit_modes();
|
||||
let (bytes, notes) = match encode_art_page(C, &doc, &q)? {
|
||||
Some(encoded) => encoded,
|
||||
None => encode(C, &doc, &q)?,
|
||||
};
|
||||
write_or_return(str_param(p, "path"), &bytes, json!({ "warnings": note_texts(¬es) }))
|
||||
}
|
||||
|
||||
@@ -513,9 +539,18 @@ fn pdf_settings(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const Q: &str = "document.pdfSettings";
|
||||
let saved = &s.prefs.pdf_presets;
|
||||
let set = settings_with(Q, p, saved)?;
|
||||
super::encode::use_artboards(Q, p)?;
|
||||
let diff = changes(&set, &preset_settings(Q, p, saved)?);
|
||||
let warnings = match s.active() {
|
||||
Some(st) if bool_or(p, "includeDocument", false) => note_texts(&encode(Q, &st.doc, &*expand_preset(s, Q, p)?)?.1),
|
||||
Some(st) if bool_or(p, "includeDocument", false) => {
|
||||
let q = expand_preset(s, Q, p)?;
|
||||
let doc = st.doc.without_edit_modes();
|
||||
let (_, notes) = match encode_art_page(Q, &doc, &q)? {
|
||||
Some(encoded) => encoded,
|
||||
None => encode(Q, &doc, &q)?,
|
||||
};
|
||||
note_texts(¬es)
|
||||
}
|
||||
_ => set.warnings(),
|
||||
};
|
||||
let v = serde_json::to_value(&set).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
|
||||
@@ -390,9 +390,20 @@ fn open_exts_cover_every_readable_format() {
|
||||
assert_eq!(filters[0], ("All readable files", OPEN_EXTS));
|
||||
assert_eq!(
|
||||
filters.len(),
|
||||
11 + FORMATS.iter().filter(|f| f.read).count(),
|
||||
"and swatch libraries, libraries, flattener, PDF, print and export presets, composite fonts, shortcut sets, scripts, plug-ins"
|
||||
13 + FORMATS.iter().filter(|f| f.read).count(),
|
||||
"and swatch and graphic style libraries, libraries, flattener, PDF, print, perspective grid and export presets, composite \
|
||||
fonts, shortcut sets, scripts, plug-ins"
|
||||
);
|
||||
let at = |label: &str| filters.iter().position(|f| f.0 == label).unwrap_or_else(|| panic!("File › Open has no {label} filter"));
|
||||
let styles: &[&str] = &[vectorcraft_doc::style_libs::STYLES_EXT];
|
||||
assert_eq!(filters[at("Graphic Style Libraries")].1, styles, "File › Open opens graphic style libraries");
|
||||
assert_eq!(at("Graphic Style Libraries"), at("Swatch libraries") + 1, "next to the swatch libraries");
|
||||
assert_eq!(filters[at("Perspective grid presets")].1, crate::cmd::perspgrid::PRESET_EXTS, "File › Open imports perspective grid presets");
|
||||
assert_eq!(at("Perspective grid presets"), at("Print presets") + 1, "among the presets");
|
||||
assert_eq!(at("Export presets"), at("Perspective grid presets") + 1, "among the presets");
|
||||
for e in [vectorcraft_doc::style_libs::STYLES_EXT].iter().chain(crate::cmd::perspgrid::PRESET_EXTS) {
|
||||
assert!(!filters[0].1.contains(e), "All readable files lists documents only, not .{e}");
|
||||
}
|
||||
assert!(filters.contains(&("Export presets", SCREEN_EXPORT_PRESET_EXTS)), "File › Open imports export presets");
|
||||
assert!(filters.contains(&("Scripts", crate::cmd::scripts::EXTS)), "File › Open checks scripts");
|
||||
assert_eq!(filters.last(), Some(&("Plug-ins", crate::cmd::plugin::EXTS)), "File › Open installs plug-ins");
|
||||
@@ -404,6 +415,11 @@ fn library_open_filters_list_library_formats_first() {
|
||||
assert_eq!(filters[0], ("Swatch libraries", crate::cmd::swatchlib::LIBRARY_EXTS));
|
||||
assert!(filters[0].1.contains(&"ase"));
|
||||
assert_eq!(filters[1], ("All readable files", OPEN_EXTS));
|
||||
let styles: &'static [&'static str] = std::slice::from_ref(&vectorcraft_doc::style_libs::STYLES_EXT);
|
||||
for (label, exts) in [("Swatch libraries", crate::cmd::swatchlib::LIBRARY_EXTS), ("Graphic Style Libraries", styles)] {
|
||||
let filters: Vec<_> = library_open_filters(label, exts).collect();
|
||||
assert_eq!(filters.iter().filter(|f| f.0 == label).count(), 1, "{label} is listed once");
|
||||
}
|
||||
}
|
||||
|
||||
/// The extensions usage texts list for export: one per writable format, each picking that format
|
||||
|
||||
@@ -56,6 +56,126 @@ fn export_pdf_writes_the_compatibility_header_and_the_range() {
|
||||
assert!(b64(&v).starts_with(b"%PDF-1.6"));
|
||||
}
|
||||
|
||||
/// `useArtboards: false` writes one page, the bounds of the visible art, as `document.export`
|
||||
/// does, whatever artboards are chosen. Art longer than 200 in keeps its size with
|
||||
/// `longPagesInPoints`. A document with no visible art and a value that isn't a boolean are
|
||||
/// refused.
|
||||
#[test]
|
||||
fn use_artboards_false_writes_one_page_of_the_arts_bounds() {
|
||||
let mut s = session(3);
|
||||
let page = |bytes: &[u8]| {
|
||||
let d = vectorcraft_pdf::import(bytes).unwrap();
|
||||
(d.artboards.len(), d.artboards[0].rect.size())
|
||||
};
|
||||
let through_export = page(&b64(&s.execute("document.export", &as_export(&json!({"useArtboards": false}))).unwrap()));
|
||||
assert!(through_export.0 == 1 && through_export.1.width < 50.0 && through_export.1.height < 40.0, "{through_export:?}");
|
||||
for p in [
|
||||
json!({"useArtboards": false}),
|
||||
json!({"useArtboards": false, "range": "2"}),
|
||||
json!({"useArtboards": false, "artboards": [0, 1]}),
|
||||
json!({"useArtboards": false, "artboard": 2}),
|
||||
] {
|
||||
assert_eq!(page(&b64(&export(&mut s, p.clone()))), through_export, "{p}");
|
||||
}
|
||||
assert_eq!(pages(&b64(&export(&mut s, json!({"useArtboards": true})))), 3);
|
||||
assert_eq!(pages(&b64(&export(&mut s, json!({"useArtboards": null})))), 3);
|
||||
|
||||
// A 10 m drawing on a small artboard: its page is 10 m long, in points.
|
||||
s.execute("shape.rectangle", &json!({"x": 0, "y": 200, "width": 28_346.46, "height": 100})).unwrap();
|
||||
let v = export(&mut s, json!({"useArtboards": false, "longPagesInPoints": true}));
|
||||
let pdf = b64(&v);
|
||||
let (n, size) = page(&pdf);
|
||||
assert!(n == 1 && size.width > 28_346.0 && size.width < 28_350.0, "{size:?}");
|
||||
assert!(!String::from_utf8_lossy(&pdf).contains("/UserUnit"));
|
||||
assert!(warnings(&v).iter().any(|w| w.contains("written without a UserUnit, in points")), "{v}");
|
||||
// The Summary's export in memory writes the same page.
|
||||
let mut long = |p: Value| warnings(&s.execute("document.pdfSettings", &p).unwrap()).iter().any(|w| w.contains("longer than PDF's page limit"));
|
||||
assert!(long(json!({"includeDocument": true, "useArtboards": false})));
|
||||
assert!(!long(json!({"includeDocument": true})), "the artboards are small");
|
||||
let e = s.execute("document.pdfSettings", &json!({"useArtboards": "no"})).unwrap_err();
|
||||
assert!(matches!(e, crate::EngineError::BadParams { .. }), "without includeDocument too: {e}");
|
||||
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
s.execute("edit.clear", &json!({})).unwrap();
|
||||
for p in [json!({"useArtboards": false}), json!({"useArtboards": "no"})] {
|
||||
let e = s.execute("document.exportPdf", &p).unwrap_err();
|
||||
assert!(matches!(e, crate::EngineError::BadParams { .. }), "{p}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A PDF of the art's bounds carries no Preserve Editing data, through `document.exportPdf`,
|
||||
/// `document.export` and the Summary's export in memory, and says so in a warning: reopened, its
|
||||
/// editing data would replace the document's artboards with the one around the art. A PDF of the
|
||||
/// artboards keeps the data, and it reopens with every artboard. With Preserve Editing off, there
|
||||
/// is no warning.
|
||||
#[test]
|
||||
fn a_pdf_of_the_arts_bounds_carries_no_editing_data() {
|
||||
let mut s = session(3);
|
||||
let left_out = |v: &Value| warnings(v).iter().any(|w| w == super::pdf::EDITING_NEEDS_THE_ARTBOARDS);
|
||||
let art = json!({"useArtboards": false});
|
||||
for v in [export(&mut s, art.clone()), s.execute("document.export", &as_export(&art)).unwrap()] {
|
||||
let pdf = b64(&v);
|
||||
assert!(vectorcraft_pdf::editing(&pdf).is_none(), "{:?}", warnings(&v));
|
||||
assert!(left_out(&v), "{:?}", warnings(&v));
|
||||
assert_eq!(pages(&pdf), 1);
|
||||
}
|
||||
let summary = s.execute("document.pdfSettings", &json!({"includeDocument": true, "useArtboards": false})).unwrap();
|
||||
assert!(left_out(&summary), "{summary}");
|
||||
let quiet = export(&mut s, json!({"useArtboards": false, "preserveEditing": false}));
|
||||
assert!(!left_out(&quiet), "{:?}", warnings(&quiet));
|
||||
|
||||
for v in [export(&mut s, json!({})), export(&mut s, json!({"useArtboards": true}))] {
|
||||
let editing = vectorcraft_pdf::editing(&b64(&v)).expect("editing data");
|
||||
let reopened = vectorcraft_format::load(&editing.data).unwrap();
|
||||
assert_eq!(reopened.artboards.len(), 3);
|
||||
assert!(!left_out(&v), "{:?}", warnings(&v));
|
||||
}
|
||||
}
|
||||
|
||||
/// While an opacity mask is edited, a PDF of the art's bounds measures the art without the
|
||||
/// editing layer, as `document.export` does: a mask shape drawn past the masked object doesn't
|
||||
/// widen the page.
|
||||
#[test]
|
||||
fn a_pdf_of_the_art_leaves_out_the_mask_editing_layer() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 300, "height": 300})).unwrap();
|
||||
let obj = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 200, "height": 200})).unwrap()["id"].clone();
|
||||
let mask = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 100, "height": 200})).unwrap()["id"].clone();
|
||||
s.execute("paint.setFill", &json!({"color": "#ffffff", "ids": [mask]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [obj, mask]})).unwrap();
|
||||
s.execute("transparency.makeOpacityMask", &json!({})).unwrap();
|
||||
s.execute("transparency.editOpacityMask", &json!({})).unwrap();
|
||||
s.execute("shape.ellipse", &json!({"x": 250, "y": 250, "width": 40, "height": 40})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.mask_edit.is_some());
|
||||
let width = |bytes: &[u8]| vectorcraft_pdf::import(bytes).unwrap().artboards[0].rect.width();
|
||||
let through_export = width(&b64(&s.execute("document.export", &as_export(&json!({"useArtboards": false}))).unwrap()));
|
||||
assert!(through_export < 250.0, "{through_export}");
|
||||
assert_eq!(width(&b64(&export(&mut s, json!({"useArtboards": false})))), through_export);
|
||||
}
|
||||
|
||||
/// While an opacity mask is edited, a PDF of the artboards leaves out the editing layer too, as
|
||||
/// `document.export` does: from the drawn page and from the Preserve Editing data.
|
||||
#[test]
|
||||
fn a_pdf_of_the_artboards_leaves_out_the_mask_editing_layer() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 300, "height": 300})).unwrap();
|
||||
let obj = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 200, "height": 200})).unwrap()["id"].clone();
|
||||
let mask = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 100, "height": 200})).unwrap()["id"].clone();
|
||||
s.execute("paint.setFill", &json!({"color": "#ffffff", "ids": [mask]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [obj, mask]})).unwrap();
|
||||
s.execute("transparency.makeOpacityMask", &json!({})).unwrap();
|
||||
s.execute("transparency.editOpacityMask", &json!({})).unwrap();
|
||||
s.execute("shape.ellipse", &json!({"x": 250, "y": 250, "width": 40, "height": 40})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.mask_edit.is_some());
|
||||
let nodes = |bytes: &[u8]| vectorcraft_pdf::import(bytes).unwrap().node_count();
|
||||
let plain = json!({"preserveEditing": false});
|
||||
let through_export = nodes(&b64(&s.execute("document.export", &as_export(&plain)).unwrap()));
|
||||
assert_eq!(nodes(&b64(&export(&mut s, plain))), through_export, "the drawn page");
|
||||
let layers = |v: &Value| vectorcraft_format::load(&vectorcraft_pdf::editing(&b64(v)).unwrap().data).unwrap().layers;
|
||||
let through_export = layers(&s.execute("document.export", &as_export(&json!({}))).unwrap());
|
||||
assert_eq!(layers(&export(&mut s, json!({}))), through_export, "the Preserve Editing data");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_options_are_refused() {
|
||||
let mut s = session(2);
|
||||
|
||||
@@ -67,7 +67,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Missing Fonts",
|
||||
[],
|
||||
None,
|
||||
"{} → {fonts: [{family, style, status: missing|substitute, resolved: {family, style}}] (sorted by name; at most 1000), count, fontsNextToDocument?} the fonts the active document's type uses, in its layers and its symbols (for type in a composite font, the fonts of the parts its characters are drawn in), that aren't available as named: missing (the family is unknown and the fallback family stands in) or substitute (the family is there but not the style; resolved names the stand-in), as text.fonts says. CSS generic families such as sans-serif and names longer than 256 bytes are left out. fontsNextToDocument: the Fonts folder next to the saved document (File › Package writes it), when there is one and a search may start there (not in another app's folders, such as a mail app's attachments)",
|
||||
"{} → {fonts: [{family, style, status: missing|substitute, resolved: {family, style}}] (sorted by name; at most 1000), count, fontsNextToDocument?} the fonts the active document's type uses, in its layers, its symbols and its patterns, opacity masks included (for type in a composite font, the fonts of the parts its characters are drawn in), that aren't available as named: missing (the family is unknown and the fallback family stands in) or substitute (the family is there but not the style; resolved names the stand-in), as text.fonts says. CSS generic families such as sans-serif and names longer than 256 bytes are left out. fontsNextToDocument: the Fonts folder next to the saved document (File › Package writes it), when there is one and a search may start there (not in another app's folders, such as a mail app's attachments)",
|
||||
has_doc,
|
||||
missing
|
||||
),
|
||||
@@ -103,7 +103,7 @@ pub struct MissingFont {
|
||||
pub resolved: Option<(String, String)>,
|
||||
}
|
||||
|
||||
/// The fonts the type of `d` uses, in its layers and its symbols (a composite font's parts, those
|
||||
/// The fonts the type of `d` uses, in its layers, symbols and patterns, opacity masks included (a composite font's parts, those
|
||||
/// its characters are drawn in, in place of its name), that aren't available as named (sorted by family and style, each once whatever version its type names; at most 1000). CSS generic families such as `sans-serif`
|
||||
/// ([`GENERIC_FAMILIES`]) and names longer than 256 bytes are left out.
|
||||
pub fn missing_fonts(d: &Document) -> Vec<MissingFont> {
|
||||
|
||||
+617
-177
@@ -21,7 +21,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Fonts in Document",
|
||||
[],
|
||||
None,
|
||||
"{selectionOnly?} (the type in the art of the symbols in scope included: all of the document's, or those the selected instances show) → [{family, style, runs, objects, missing, status: exact|substitute|missing, resolved: {family, style}, missingGlyphs, version?: the installed version the type names (when it names one)}] sorted by name. A font is found by any of its names (ヒラギノ角ゴシック = Hiragino Sans) among the loaded and installed fonts; substitute: the family is there but not the style (resolved names the stand-in); missing: the family is unknown (the fallback family stands in); missingGlyphs: characters of its text the resolved font lacks (drawn by a fallback font). For a composite font, status is the worst among the parts its characters are drawn in, and missingGlyphs counts the characters their part's font lacks",
|
||||
"{selectionOnly?} (the type in the opacity masks of the objects in scope and in the art of the symbols and patterns in scope included: all of the document's, a pattern swatch that nothing paints included, or those the selected objects show through their instances and paints; disabled masks too) → [{family, style, runs, objects, missing, status: exact|substitute|missing, resolved: {family, style}, missingGlyphs, version?: the installed version the type names (when it names one)}] sorted by name. A font is found by any of its names (ヒラギノ角ゴシック = Hiragino Sans) among the loaded and installed fonts; substitute: the family is there but not the style (resolved names the stand-in); missing: the family is unknown (the fallback family stands in); missingGlyphs: characters of its text the resolved font lacks (drawn by a fallback font). For a composite font, status is the worst among the parts its characters are drawn in, and missingGlyphs counts the characters their part's font lacks",
|
||||
has_doc,
|
||||
fonts
|
||||
),
|
||||
@@ -30,7 +30,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Replace Font",
|
||||
[],
|
||||
None,
|
||||
"{from: {family, style?}, to: {family (any of its names; stored as the font's own family name, a composite font's as its own name), style?}, selectionOnly?} (style omitted: every style of the family / keep the closest style); the type in the art of the symbols in scope changes too (all of the document's, or those the selected instances show, so every instance of them follows); text that is hidden or locked, in a hidden or locked group or layer (or group of a symbol's art), or on a locked artboard keeps its font; when no run changes (text already set in the new font, or none to change), the document and its undo history stay as they are → {runs: the runs changed, skipped?: the text objects left in the font, warning?: text (some text was left in its font, or the style set isn't available: its type is drawn in the family's closest style)}",
|
||||
"{from: {family, style?}, to: {family (any of its names; stored as the font's own family name, a composite font's as its own name), style?}, selectionOnly?} (style omitted: every style of the family / keep the closest style); the type in the opacity masks of the objects in scope (disabled masks too) and in the art of the symbols and patterns in scope changes too (all of the document's, or those the selected objects show through their instances and paints, so every instance of a symbol and every object a pattern paints follows); text that is hidden or locked, in a hidden or locked group or layer (or group of a symbol's, pattern's or mask's art), in the mask of a hidden or locked object, or on a locked artboard keeps its font; when no run changes (text already set in the new font, or none to change), the document and its undo history stay as they are → {runs: the runs changed, skipped?: the text objects left in the font, warning?: text (some text was left in its font, or the style set isn't available: its type is drawn in the family's closest style)}",
|
||||
has_doc,
|
||||
replace
|
||||
),
|
||||
@@ -39,7 +39,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Select Text by Font",
|
||||
[],
|
||||
None,
|
||||
"{family, style?, scope?: \"document\" (default) | {artboards: [index, …] (0-based)} | {layers: [id, …] (layers or sublayers)}} select the text objects using the font, and the instances of the symbols whose art uses it, that are not hidden or locked, in a hidden or locked group or layer, or on a locked artboard; with scope, only those on one of the artboards (as All on Active Artboard takes them: their bounds overlap it or their center is on it) or in one of the layers → {count}",
|
||||
"{family, style?, scope?: \"document\" (default) | {artboards: [index, …] (0-based)} | {layers: [id, …] (layers or sublayers)}} select the text objects using the font, the instances of the symbols whose art uses it, the objects painted with a pattern whose art uses it and the objects whose opacity mask uses it (in type that is not hidden or locked in that art), that are not hidden or locked, in a hidden or locked group or layer, or on a locked artboard; a pattern swatch that nothing paints, or a layer's mask, selects nothing; with scope, only those on one of the artboards (as All on Active Artboard takes them: their bounds overlap it or their center is on it) or in one of the layers → {count}",
|
||||
has_doc,
|
||||
select_font
|
||||
),
|
||||
@@ -64,15 +64,23 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
]
|
||||
}
|
||||
|
||||
/// Text objects in scope: the selection's (and their descendants') or the whole document's.
|
||||
fn scope(s: &Session, selection_only: bool, selected: Option<&[NodeId]>) -> Result<Vec<NodeId>> {
|
||||
/// Text objects in scope: the selection's (and their descendants') or the whole document's, and
|
||||
/// the objects among them that have an opacity mask (not the object whose mask is being edited:
|
||||
/// its art is on the editing layer).
|
||||
fn scope(s: &Session, selection_only: bool, selected: Option<&[NodeId]>) -> Result<(Vec<NodeId>, Vec<NodeId>)> {
|
||||
let st = s.doc()?;
|
||||
let mut v = vec![];
|
||||
let editing = st.doc.mask_edit.map(|m| m.object);
|
||||
let (mut texts, mut masked) = (vec![], vec![]);
|
||||
let mut seen = HashSet::new();
|
||||
let mut add = |n: &vectorcraft_doc::Node| {
|
||||
n.walk(&mut |c| {
|
||||
if matches!(c.kind, NodeKind::Text(_)) && seen.insert(c.id) {
|
||||
v.push(c.id);
|
||||
if seen.insert(c.id) {
|
||||
if matches!(c.kind, NodeKind::Text(_)) {
|
||||
texts.push(c.id);
|
||||
}
|
||||
if c.extras().mask.is_some() && Some(c.id) != editing {
|
||||
masked.push(c.id);
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
@@ -87,7 +95,7 @@ fn scope(s: &Session, selection_only: bool, selected: Option<&[NodeId]>) -> Resu
|
||||
add(l);
|
||||
}
|
||||
}
|
||||
Ok(v)
|
||||
Ok((texts, masked))
|
||||
}
|
||||
|
||||
/// Whether `name` is the symbol being edited, whose art is on the editing layer.
|
||||
@@ -95,106 +103,269 @@ fn being_edited(d: &Document, name: &str) -> bool {
|
||||
d.symbol_edit.as_ref().is_some_and(|e| e.symbol == name)
|
||||
}
|
||||
|
||||
/// The symbols that the selected objects of `s` show: those of the instances among them and their
|
||||
/// descendants, and of the instances in those symbols' art that are not hidden or locked, nor in a
|
||||
/// hidden or locked group of the art. Returned as (all of them, those shown by instances the
|
||||
/// Selection tool can reach: not hidden or locked, as `Document::editable` says).
|
||||
fn selected_symbols(s: &Session, selected: Option<&[NodeId]>) -> Result<(BTreeSet<String>, BTreeSet<String>)> {
|
||||
fn open_instances(n: &Node, out: &mut BTreeSet<String>) {
|
||||
/// Whether `name` is the pattern being edited, whose tile art is on the editing layer.
|
||||
fn pattern_being_edited(d: &Document, name: &str) -> bool {
|
||||
d.pattern_edit.as_ref().is_some_and(|e| e.pattern == name)
|
||||
}
|
||||
|
||||
/// The patterns that the fills and strokes `n` draws name (of a text object, also those of its
|
||||
/// characters): visible ones, and strokes wider than 0.
|
||||
fn paint_patterns(n: &Node) -> impl Iterator<Item = &str> {
|
||||
let runs = match &n.kind {
|
||||
NodeKind::Text(t) => t.runs.as_slice(),
|
||||
_ => &[],
|
||||
};
|
||||
n.appearance
|
||||
.items
|
||||
.iter()
|
||||
.filter(|i| i.visible() && !matches!(i, AppearanceItem::Stroke(s) if s.width <= 0.0))
|
||||
.map(AppearanceItem::paint)
|
||||
.chain(runs.iter().flat_map(|r| [Some(&r.style.fill), (r.style.stroke_width > 0.0).then_some(&r.style.stroke)]).flatten())
|
||||
.filter_map(|p| match p {
|
||||
Paint::Pattern { pattern, .. } => Some(pattern.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Symbols and patterns, by name: those whose art is in scope.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct Resources {
|
||||
symbols: BTreeSet<String>,
|
||||
patterns: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl Resources {
|
||||
/// Adds what `n` itself shows: the symbol of an instance, the patterns its paints name, and
|
||||
/// what the art of its opacity mask shows (not hidden or locked objects of that art).
|
||||
fn add_own(&mut self, n: &Node) {
|
||||
if let NodeKind::SymbolInstance { symbol, .. } = &n.kind {
|
||||
self.symbols.insert(symbol.clone());
|
||||
}
|
||||
self.patterns.extend(paint_patterns(n).map(str::to_string));
|
||||
if let Some(m) = n.extras().mask.as_deref() {
|
||||
self.add(&m.art);
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds what `n` and its descendants show, leaving out hidden or locked objects and what they
|
||||
/// hold.
|
||||
fn add(&mut self, n: &Node) {
|
||||
if !n.visible || n.locked {
|
||||
return;
|
||||
}
|
||||
if let NodeKind::SymbolInstance { symbol, .. } = &n.kind {
|
||||
out.insert(symbol.clone());
|
||||
}
|
||||
self.add_own(n);
|
||||
for c in n.children().map(Vec::as_slice).unwrap_or_default() {
|
||||
open_instances(c, out);
|
||||
self.add(c);
|
||||
}
|
||||
}
|
||||
let expand = |names: &mut BTreeSet<String>, d: &Document| loop {
|
||||
let mut more = BTreeSet::new();
|
||||
for sym in d.symbols.iter().filter(|sym| names.contains(&sym.name)) {
|
||||
open_instances(&sym.art, &mut more);
|
||||
|
||||
/// Adds what the art of the symbols and patterns named shows, until nothing more is added; each
|
||||
/// symbol and pattern is read once, so art that shows itself ends.
|
||||
fn expand(&mut self, d: &Document) {
|
||||
let mut symbols: Vec<String> = self.symbols.iter().cloned().collect();
|
||||
let mut patterns: Vec<String> = self.patterns.iter().cloned().collect();
|
||||
while !symbols.is_empty() || !patterns.is_empty() {
|
||||
let mut more = Resources::default();
|
||||
for name in symbols.drain(..) {
|
||||
if let Some(sym) = d.symbols.iter().find(|sym| sym.name == name) {
|
||||
more.add(&sym.art);
|
||||
}
|
||||
}
|
||||
for name in patterns.drain(..) {
|
||||
for art in d.pattern(&name).map(|p| p.art.as_slice()).unwrap_or_default() {
|
||||
more.add(art);
|
||||
}
|
||||
}
|
||||
symbols.extend(more.symbols.into_iter().filter(|name| self.symbols.insert(name.clone())));
|
||||
patterns.extend(more.patterns.into_iter().filter(|name| self.patterns.insert(name.clone())));
|
||||
}
|
||||
let before = names.len();
|
||||
names.extend(more);
|
||||
if names.len() == before {
|
||||
break;
|
||||
}
|
||||
|
||||
/// The symbols and patterns of `self` that are not in `other`.
|
||||
fn difference(&self, other: &Self) -> Self {
|
||||
Self {
|
||||
symbols: self.symbols.difference(&other.symbols).cloned().collect(),
|
||||
patterns: self.patterns.difference(&other.patterns).cloned().collect(),
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// The symbols and patterns that the selected objects of `s` show: those of the instances among
|
||||
/// them and their descendants and the patterns their paints name (also in the art of their
|
||||
/// opacity masks), and those that the art of these symbols and patterns shows through objects that
|
||||
/// are not hidden or locked, nor in a hidden or locked group of the art. Returned as (all of them,
|
||||
/// those shown by objects the Selection tool can reach: not hidden or locked, as
|
||||
/// `Document::editable` says).
|
||||
fn selected_art(s: &Session, selected: Option<&[NodeId]>) -> Result<(Resources, Resources)> {
|
||||
let st = s.doc()?;
|
||||
let (mut all, mut open) = (BTreeSet::new(), BTreeSet::new());
|
||||
let (mut all, mut open) = (Resources::default(), Resources::default());
|
||||
let editable = st.doc.editable();
|
||||
for id in selected.unwrap_or(&st.selection.objects) {
|
||||
if let Some(n) = st.doc.node(*id) {
|
||||
n.walk(&mut |c| {
|
||||
if let NodeKind::SymbolInstance { symbol, .. } = &c.kind {
|
||||
all.insert(symbol.clone());
|
||||
if editable(c.id) {
|
||||
open.insert(symbol.clone());
|
||||
}
|
||||
all.add_own(c);
|
||||
if editable(c.id) {
|
||||
open.add_own(c);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
drop(editable);
|
||||
expand(&mut all, &st.doc);
|
||||
expand(&mut open, &st.doc);
|
||||
all.expand(&st.doc);
|
||||
open.expand(&st.doc);
|
||||
Ok((all, open))
|
||||
}
|
||||
|
||||
/// The text objects in the art of `d`'s symbols (with `only`, of those it names), each with
|
||||
/// whether it is open: neither it nor a group around it in the art is hidden or locked. The symbol
|
||||
/// being edited is left out: its art is on the editing layer.
|
||||
pub(super) fn symbol_texts<'a>(d: &'a Document, only: Option<&BTreeSet<String>>) -> Vec<(&'a TextObject, bool)> {
|
||||
fn visit<'a>(n: &'a Node, open: bool, out: &mut Vec<(&'a TextObject, bool)>) {
|
||||
let open = open && n.visible && !n.locked;
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
out.push((&**t, open));
|
||||
}
|
||||
for c in n.children().map(Vec::as_slice).unwrap_or_default() {
|
||||
visit(c, open, out);
|
||||
}
|
||||
/// A text object found in art: its id, the text and whether it is open (neither it nor a group
|
||||
/// around it, nor an object whose opacity mask holds it, is hidden or locked).
|
||||
pub(super) type ArtText<'a> = (NodeId, &'a TextObject, bool);
|
||||
|
||||
/// Adds the text objects of `n` and its descendants, the art of their opacity masks included, to
|
||||
/// `out`; each is open when `open` holds and [`ArtText`] says so.
|
||||
fn art_texts<'a>(n: &'a Node, open: bool, out: &mut Vec<ArtText<'a>>) {
|
||||
let open = open && n.visible && !n.locked;
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
out.push((n.id, &**t, open));
|
||||
}
|
||||
if let Some(m) = n.extras().mask.as_deref() {
|
||||
art_texts(&m.art, open, out);
|
||||
}
|
||||
for c in n.children().map(Vec::as_slice).unwrap_or_default() {
|
||||
art_texts(c, open, out);
|
||||
}
|
||||
}
|
||||
|
||||
/// The text objects in the art of the opacity masks of the objects `masked` (each with whether
|
||||
/// the object is open: when it isn't, none of its mask's text is).
|
||||
fn mask_texts<'a>(d: &'a Document, masked: &[(NodeId, bool)]) -> Vec<ArtText<'a>> {
|
||||
let mut out = vec![];
|
||||
for s in d.symbols.iter().filter(|s| !being_edited(d, &s.name) && only.is_none_or(|o| o.contains(&s.name))) {
|
||||
visit(&s.art, true, &mut out);
|
||||
for (id, open) in masked {
|
||||
if let Some(m) = d.node(*id).and_then(|n| n.extras().mask.as_deref()) {
|
||||
art_texts(&m.art, *open, &mut out);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Calls `f` on each text object in the art of `d`'s symbols (with `only`, of those it names) for
|
||||
/// which `wants(text, open)` holds (open as in [`symbol_texts`]), the symbol being edited left out.
|
||||
/// Art without such text is left as it is.
|
||||
pub(super) fn edit_symbol_texts(
|
||||
d: &mut Document,
|
||||
only: Option<&BTreeSet<String>>,
|
||||
wants: &impl Fn(&TextObject, bool) -> bool,
|
||||
f: &mut impl FnMut(&mut TextObject),
|
||||
) {
|
||||
fn wanted(n: &Node, open: bool, wants: &impl Fn(&TextObject, bool) -> bool) -> bool {
|
||||
let open = open && n.visible && !n.locked;
|
||||
matches!(&n.kind, NodeKind::Text(t) if wants(t, open)) || n.children().is_some_and(|ch| ch.iter().any(|c| wanted(c, open, wants)))
|
||||
/// The text objects in the art of `d`'s symbols and patterns (with `only`, of those it names, a
|
||||
/// pattern's art read once however many objects it paints), opacity masks in that art included.
|
||||
/// The symbol and the pattern being edited are left out: their art is on the editing layer.
|
||||
fn resource_texts<'a>(d: &'a Document, only: Option<&Resources>) -> Vec<ArtText<'a>> {
|
||||
let mut out = vec![];
|
||||
for s in d.symbols.iter().filter(|s| !being_edited(d, &s.name) && only.is_none_or(|o| o.symbols.contains(&s.name))) {
|
||||
art_texts(&s.art, true, &mut out);
|
||||
}
|
||||
fn visit(n: &mut Node, open: bool, wants: &impl Fn(&TextObject, bool) -> bool, f: &mut impl FnMut(&mut TextObject)) {
|
||||
let open = open && n.visible && !n.locked;
|
||||
if let NodeKind::Text(t) = &mut n.kind
|
||||
&& wants(t, open)
|
||||
{
|
||||
f(t);
|
||||
for p in d.patterns.iter().filter(|p| !pattern_being_edited(d, &p.name) && only.is_none_or(|o| o.patterns.contains(&p.name))) {
|
||||
for art in &p.art {
|
||||
art_texts(art, true, &mut out);
|
||||
}
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
if wanted(c, open, wants) {
|
||||
visit(Arc::make_mut(c), open, wants, f);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The text objects in the art of `d`'s symbols (with `only`, of those it names), as
|
||||
/// [`resource_texts`] finds them, each with whether it is open.
|
||||
#[cfg(test)]
|
||||
pub(super) fn symbol_texts<'a>(d: &'a Document, only: Option<&BTreeSet<String>>) -> Vec<(&'a TextObject, bool)> {
|
||||
let mut out = vec![];
|
||||
for s in d.symbols.iter().filter(|s| !being_edited(d, &s.name) && only.is_none_or(|o| o.contains(&s.name))) {
|
||||
art_texts(&s.art, true, &mut out);
|
||||
}
|
||||
out.into_iter().map(|(_, t, open)| (t, open)).collect()
|
||||
}
|
||||
|
||||
/// Whether `n` or a descendant (the art of their opacity masks included) holds a text object for
|
||||
/// which `wants(text, open)` holds, open as [`ArtText`] says.
|
||||
fn wanted(n: &Node, open: bool, wants: &impl Fn(&TextObject, bool) -> bool) -> bool {
|
||||
let open = open && n.visible && !n.locked;
|
||||
matches!(&n.kind, NodeKind::Text(t) if wants(t, open))
|
||||
|| n.extras().mask.as_deref().is_some_and(|m| wanted(&m.art, open, wants))
|
||||
|| n.children().is_some_and(|ch| ch.iter().any(|c| wanted(c, open, wants)))
|
||||
}
|
||||
|
||||
/// Calls `f` on each text object of `n` and its descendants (the art of their opacity masks
|
||||
/// included) for which `wants(text, open)` holds; only the nodes on the way to such text are
|
||||
/// copied.
|
||||
fn edit_art(n: &mut Node, open: bool, wants: &impl Fn(&TextObject, bool) -> bool, f: &mut impl FnMut(&mut TextObject)) {
|
||||
let open = open && n.visible && !n.locked;
|
||||
if let NodeKind::Text(t) = &mut n.kind
|
||||
&& wants(t, open)
|
||||
{
|
||||
f(t);
|
||||
}
|
||||
if let Some(m) = n.extras_if_any().and_then(|e| e.mask.as_mut())
|
||||
&& wanted(&m.art, open, wants)
|
||||
{
|
||||
edit_art(Arc::make_mut(&mut m.art), open, wants, f);
|
||||
}
|
||||
for c in n.children_mut().into_iter().flatten() {
|
||||
if wanted(c, open, wants) {
|
||||
edit_art(Arc::make_mut(c), open, wants, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls `f` on each text object in the opacity masks of the objects `masked` (as [`mask_texts`]
|
||||
/// finds them) for which `wants(text, open)` holds. Masks without such text are left as they are.
|
||||
fn edit_mask_texts(d: &mut Document, masked: &[(NodeId, bool)], wants: &impl Fn(&TextObject, bool) -> bool, f: &mut impl FnMut(&mut TextObject)) {
|
||||
for (id, open) in masked {
|
||||
if d.node(*id).and_then(|n| n.extras().mask.as_deref()).is_some_and(|m| wanted(&m.art, *open, wants))
|
||||
&& let Some(m) = d.node_mut(*id).and_then(|n| n.extras_if_any()).and_then(|e| e.mask.as_mut())
|
||||
{
|
||||
edit_art(Arc::make_mut(&mut m.art), *open, wants, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls `f` on each text object in the art of `d`'s symbols and patterns (as [`resource_texts`]
|
||||
/// finds them) for which `wants(text, open)` holds. Art without such text is left as it is.
|
||||
fn edit_resource_texts(d: &mut Document, only: Option<&Resources>, wants: &impl Fn(&TextObject, bool) -> bool, f: &mut impl FnMut(&mut TextObject)) {
|
||||
let symbol = d.symbol_edit.as_ref().map(|e| e.symbol.clone());
|
||||
let pattern = d.pattern_edit.as_ref().map(|e| e.pattern.clone());
|
||||
for s in &mut d.symbols {
|
||||
if symbol.as_ref() != Some(&s.name) && only.is_none_or(|o| o.symbols.contains(&s.name)) && wanted(&s.art, true, wants) {
|
||||
edit_art(Arc::make_mut(&mut s.art), true, wants, f);
|
||||
}
|
||||
}
|
||||
for p in &mut d.patterns {
|
||||
if pattern.as_ref() != Some(&p.name) && only.is_none_or(|o| o.patterns.contains(&p.name)) {
|
||||
for art in &mut p.art {
|
||||
if wanted(art, true, wants) {
|
||||
edit_art(Arc::make_mut(art), true, wants, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let edited = d.symbol_edit.as_ref().map(|e| e.symbol.clone());
|
||||
for s in &mut d.symbols {
|
||||
if edited.as_ref() != Some(&s.name) && only.is_none_or(|o| o.contains(&s.name)) && wanted(&s.art, true, wants) {
|
||||
visit(Arc::make_mut(&mut s.art), true, wants, f);
|
||||
}
|
||||
|
||||
/// Every text object of `d` outside the objects of its layers: in the opacity masks of the
|
||||
/// objects on its layers, and in the art of its symbols and patterns (as [`mask_texts`] and
|
||||
/// [`resource_texts`] find them, each mask's text open).
|
||||
pub(super) fn art_texts_of(d: &Document) -> Vec<ArtText<'_>> {
|
||||
let masked = masked_objects(d);
|
||||
let mut out = mask_texts(d, &masked);
|
||||
out.extend(resource_texts(d, None));
|
||||
out
|
||||
}
|
||||
|
||||
/// Calls `f` on each text object of [`art_texts_of`] for which `wants(text)` holds.
|
||||
pub(super) fn edit_art_texts_of(d: &mut Document, wants: &impl Fn(&TextObject) -> bool, f: &mut impl FnMut(&mut TextObject)) {
|
||||
let masked = masked_objects(d);
|
||||
edit_mask_texts(d, &masked, &|t, _| wants(t), f);
|
||||
edit_resource_texts(d, None, &|t, _| wants(t), f);
|
||||
}
|
||||
|
||||
/// The objects on the layers of `d` that have an opacity mask (not the object whose mask is being
|
||||
/// edited), each as open.
|
||||
fn masked_objects(d: &Document) -> Vec<(NodeId, bool)> {
|
||||
let editing = d.mask_edit.map(|m| m.object);
|
||||
let mut out = vec![];
|
||||
d.walk(|n| {
|
||||
if n.extras().mask.is_some() && Some(n.id) != editing {
|
||||
out.push((n.id, true));
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// A font as type names it: family, style and, when it names one, the installed version.
|
||||
@@ -218,14 +389,16 @@ pub(super) fn run_fonts(r: &TextRun) -> Vec<UsedFont> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The fonts (family, style, version) the type in the layers and symbols of `d` is drawn in, a
|
||||
/// composite font's parts in place of its name ([`run_fonts`]).
|
||||
/// The fonts (family, style, version) the type in the layers, symbols and patterns of `d` is drawn
|
||||
/// in, the type in opacity masks included, a composite font's parts in place of its name
|
||||
/// ([`run_fonts`]).
|
||||
pub(super) fn used_fonts(d: &Document) -> BTreeSet<UsedFont> {
|
||||
art_fonts(d, &[])
|
||||
}
|
||||
|
||||
/// The fonts (family, style, version) of the type in the layers and symbols of `d` and in `art`
|
||||
/// (art read from a file, its resources in `d`).
|
||||
/// The fonts (family, style, version) of the type in the layers, symbols and patterns of `d` and
|
||||
/// in `art` (art read from a file, its resources in `d`), the type in opacity masks included. The
|
||||
/// pattern being edited is read from the editing layer only.
|
||||
pub(super) fn art_fonts(d: &Document, art: &[Node]) -> BTreeSet<UsedFont> {
|
||||
let mut fonts = BTreeSet::new();
|
||||
let mut add = |n: &Node| {
|
||||
@@ -233,12 +406,9 @@ pub(super) fn art_fonts(d: &Document, art: &[Node]) -> BTreeSet<UsedFont> {
|
||||
fonts.extend(t.runs.iter().flat_map(run_fonts));
|
||||
}
|
||||
};
|
||||
d.walk(&mut add);
|
||||
for s in &d.symbols {
|
||||
s.art.walk(&mut add);
|
||||
}
|
||||
for n in art {
|
||||
n.walk(&mut add);
|
||||
let patterns = d.patterns.iter().filter(|p| !pattern_being_edited(d, &p.name)).flat_map(|p| p.art.iter().map(|a| &**a));
|
||||
for n in d.layers.iter().map(|l| &**l).chain(d.symbols.iter().map(|s| &*s.art)).chain(patterns).chain(art) {
|
||||
vectorcraft_doc::swatches::walk_with_masks(n, &mut add);
|
||||
}
|
||||
fonts
|
||||
}
|
||||
@@ -513,50 +683,78 @@ impl Session {
|
||||
pub fn skipped_font_ids(&self, params: &Value, selected: Option<&[NodeId]>) -> Result<Vec<NodeId>> {
|
||||
let (family, style) = font_param(params, "from", "text.replaceFont")?;
|
||||
let selection_only = bool_or(params, "selectionOnly", false);
|
||||
let selected_symbols = selection_only.then(|| selected_symbols(self, selected)).transpose()?;
|
||||
let d = &self.doc()?.doc;
|
||||
let other: Vec<NodeId> = {
|
||||
let editable = d.editable();
|
||||
scope(self, selection_only, selected)?.into_iter().filter(|id| !editable(*id)).collect()
|
||||
};
|
||||
let in_scope = ReplaceScope::read(self, selection_only, selected)?;
|
||||
let uses = |t: &TextObject| {
|
||||
t.runs.iter().any(|r| {
|
||||
r.style.font_family.eq_ignore_ascii_case(&family) && style.as_ref().is_none_or(|name| r.style.font_style.eq_ignore_ascii_case(name))
|
||||
})
|
||||
};
|
||||
let mut ids = other.into_iter().filter(|id| text(d, *id).is_some_and(uses)).collect::<Vec<_>>();
|
||||
fn visit(n: &Node, open: bool, closed: bool, uses: &impl Fn(&TextObject) -> bool, ids: &mut Vec<NodeId>) {
|
||||
let open = open && n.visible && !n.locked;
|
||||
if let NodeKind::Text(t) = &n.kind
|
||||
&& (!open || closed)
|
||||
&& uses(t)
|
||||
{
|
||||
ids.push(n.id);
|
||||
}
|
||||
for child in n.children().map(Vec::as_slice).unwrap_or_default() {
|
||||
visit(child, open, closed, uses, ids);
|
||||
}
|
||||
Ok(in_scope.skipped(&self.doc()?.doc, &uses))
|
||||
}
|
||||
}
|
||||
|
||||
/// What Replace Font looks at: the text objects in scope that the Selection tool can reach and
|
||||
/// those it can't, the objects with an opacity mask (each with whether it can reach them), and the
|
||||
/// symbols and patterns whose art is in scope (`None`: all of the document's) with those shown
|
||||
/// only by hidden or locked objects of the selection.
|
||||
struct ReplaceScope {
|
||||
ids: Vec<NodeId>,
|
||||
other: Vec<NodeId>,
|
||||
masked: Vec<(NodeId, bool)>,
|
||||
resources: Option<Resources>,
|
||||
closed: Resources,
|
||||
}
|
||||
|
||||
impl ReplaceScope {
|
||||
fn read(s: &Session, selection_only: bool, selected: Option<&[NodeId]>) -> Result<Self> {
|
||||
// With selectionOnly: the symbols and patterns the selection shows; those shown only by
|
||||
// hidden or locked objects keep their type, counted in `skipped`.
|
||||
let (resources, closed) = if selection_only {
|
||||
let (all, open) = selected_art(s, selected)?;
|
||||
let closed = all.difference(&open);
|
||||
(Some(open), closed)
|
||||
} else {
|
||||
(None, Resources::default())
|
||||
};
|
||||
let d = &s.doc()?.doc;
|
||||
let editable = d.editable();
|
||||
let (texts, masked) = scope(s, selection_only, selected)?;
|
||||
let (ids, other): (Vec<NodeId>, Vec<NodeId>) = texts.into_iter().partition(|id| editable(*id));
|
||||
let masked = masked.into_iter().map(|id| (id, editable(id))).collect();
|
||||
Ok(Self { ids, other, masked, resources, closed })
|
||||
}
|
||||
|
||||
/// The text objects in the art of opacity masks, symbols and patterns in scope.
|
||||
fn art<'a>(&self, d: &'a Document) -> Vec<ArtText<'a>> {
|
||||
let mut texts = mask_texts(d, &self.masked);
|
||||
texts.extend(resource_texts(d, self.resources.as_ref()));
|
||||
texts
|
||||
}
|
||||
|
||||
/// The text objects in scope set in a font `uses` matches that Replace Font leaves as they are:
|
||||
/// those the Selection tool can't reach, hidden or locked text in art, and the text of the
|
||||
/// symbols and patterns shown only by hidden or locked objects of the selection.
|
||||
fn skipped(&self, d: &Document, uses: &impl Fn(&TextObject) -> bool) -> Vec<NodeId> {
|
||||
let mut ids: Vec<NodeId> = self.other.iter().copied().filter(|id| text(d, *id).is_some_and(uses)).collect();
|
||||
ids.extend(self.art(d).into_iter().filter(|(_, t, open)| !open && uses(t)).map(|(id, ..)| id));
|
||||
if !self.closed.symbols.is_empty() || !self.closed.patterns.is_empty() {
|
||||
ids.extend(resource_texts(d, Some(&self.closed)).into_iter().filter(|(_, t, _)| uses(t)).map(|(id, ..)| id));
|
||||
}
|
||||
for symbol in d
|
||||
.symbols
|
||||
.iter()
|
||||
.filter(|symbol| !being_edited(d, &symbol.name) && selected_symbols.as_ref().is_none_or(|(all, _)| all.contains(&symbol.name)))
|
||||
{
|
||||
let closed = selected_symbols.as_ref().is_some_and(|(_, open)| !open.contains(&symbol.name));
|
||||
visit(&symbol.art, true, closed, &uses, &mut ids);
|
||||
}
|
||||
Ok(ids)
|
||||
ids
|
||||
}
|
||||
}
|
||||
|
||||
fn font_inventory(s: &Session, selected: Option<&[NodeId]>) -> Result<Value> {
|
||||
let ids = scope(s, selected.is_some(), selected)?;
|
||||
let (ids, masked) = scope(s, selected.is_some(), selected)?;
|
||||
let d = &s.doc()?.doc;
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
// The type in the art of the symbols in scope too: all of them, or those the selection shows.
|
||||
let symbols = if selected.is_some() { Some(selected_symbols(s, selected)?.0) } else { None };
|
||||
// The type in the art of the opacity masks of the objects in scope, and of the symbols and
|
||||
// patterns in scope: all of them, or those the selection shows.
|
||||
let resources = if selected.is_some() { Some(selected_art(s, selected)?.0) } else { None };
|
||||
let mut texts: Vec<&TextObject> = ids.iter().filter_map(|id| text(d, *id)).collect();
|
||||
texts.extend(symbol_texts(d, symbols.as_ref()).into_iter().map(|(t, _)| t));
|
||||
let masked: Vec<(NodeId, bool)> = masked.into_iter().map(|id| (id, true)).collect();
|
||||
texts.extend(mask_texts(d, &masked).into_iter().map(|(_, t, _)| t));
|
||||
texts.extend(resource_texts(d, resources.as_ref()).into_iter().map(|(_, t, _)| t));
|
||||
// Each font's runs, objects and the text of its runs.
|
||||
let mut found: BTreeMap<UsedFont, (usize, usize, Vec<&str>)> = BTreeMap::new();
|
||||
for t in texts {
|
||||
@@ -617,21 +815,6 @@ fn font_param(p: &Value, k: &str, c: &str) -> Result<(String, Option<String>)> {
|
||||
Ok((family.to_string(), v.get("style").and_then(Value::as_str).map(str::to_string)))
|
||||
}
|
||||
|
||||
fn skipped_font_texts(
|
||||
d: &Document,
|
||||
other: &[NodeId],
|
||||
symbols: &[(&TextObject, bool)],
|
||||
closed: &BTreeSet<String>,
|
||||
uses: &impl Fn(&TextObject) -> bool,
|
||||
) -> usize {
|
||||
let mut skipped = other.iter().filter(|id| text(d, **id).is_some_and(uses)).count();
|
||||
skipped += symbols.iter().filter(|(t, open)| !open && uses(t)).count();
|
||||
if !closed.is_empty() {
|
||||
skipped += symbol_texts(d, Some(closed)).into_iter().filter(|(t, _)| uses(t)).count();
|
||||
}
|
||||
skipped
|
||||
}
|
||||
|
||||
fn replace(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "text.replaceFont";
|
||||
let (ff, fs) = font_param(p, "from", C)?;
|
||||
@@ -660,35 +843,25 @@ fn replace(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
// Only text the Selection tool can reach changes: not hidden or locked, in a hidden or locked
|
||||
// group or layer, or on a locked artboard. The text left in the font is counted. The type in
|
||||
// the art of the symbols in scope (all of them, or those the selection shows) changes too,
|
||||
// unless it or a group around it in the art is hidden or locked.
|
||||
// the opacity masks of the objects in scope, and in the art of the symbols and patterns in
|
||||
// scope (all of them, or those the selection shows), changes too, unless it, a group around it
|
||||
// in the art or the object whose mask holds it is hidden or locked.
|
||||
let selection_only = bool_or(p, "selectionOnly", false);
|
||||
// With selectionOnly: the symbols the selection shows; those shown only by hidden or locked
|
||||
// instances keep their type, counted in `skipped`.
|
||||
let (symbols, closed) = if selection_only {
|
||||
let (all, open) = selected_symbols(s, None)?;
|
||||
let closed: BTreeSet<String> = all.difference(&open).cloned().collect();
|
||||
(Some(open), closed)
|
||||
} else {
|
||||
(None, BTreeSet::new())
|
||||
};
|
||||
let uses = |t: &TextObject| t.runs.iter().any(|r| matches(&r.style));
|
||||
// Whether a run of `t` changes.
|
||||
let changes = |t: &TextObject| t.runs.iter().any(|r| new_style(&r.style).is_some());
|
||||
let (ids, skipped, in_symbols) = {
|
||||
let in_scope = ReplaceScope::read(s, selection_only, None)?;
|
||||
let (ids, skipped, in_art) = {
|
||||
let d = &s.doc()?.doc;
|
||||
let editable = d.editable();
|
||||
let (ids, other): (Vec<NodeId>, Vec<NodeId>) = scope(s, selection_only, None)?.into_iter().partition(|id| editable(*id));
|
||||
let texts = symbol_texts(d, symbols.as_ref());
|
||||
let skipped = skipped_font_texts(d, &other, &texts, &closed, &uses);
|
||||
let skipped = in_scope.skipped(d, &uses).len();
|
||||
// The text that changes: when none does, the document and its undo history stay as they are.
|
||||
let ids: Vec<NodeId> = ids.into_iter().filter(|id| text(d, *id).is_some_and(changes)).collect();
|
||||
let in_symbols = texts.iter().any(|(t, open)| *open && changes(t));
|
||||
(ids, skipped, in_symbols)
|
||||
let ids: Vec<NodeId> = in_scope.ids.iter().copied().filter(|id| text(d, *id).is_some_and(changes)).collect();
|
||||
let in_art = in_scope.art(d).iter().any(|(_, t, open)| *open && changes(t));
|
||||
(ids, skipped, in_art)
|
||||
};
|
||||
// The fonts the replaced runs are set in, for the warning.
|
||||
let mut set = BTreeSet::new();
|
||||
let n = if ids.is_empty() && !in_symbols {
|
||||
let n = if ids.is_empty() && !in_art {
|
||||
0
|
||||
} else {
|
||||
s.edit("Replace Font", |d, _| {
|
||||
@@ -717,7 +890,9 @@ fn replace(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
replace_in(t);
|
||||
}
|
||||
}
|
||||
edit_symbol_texts(d, symbols.as_ref(), &|t, open| open && changes(t), &mut replace_in);
|
||||
let wants = |t: &TextObject, open: bool| open && changes(t);
|
||||
edit_mask_texts(d, &in_scope.masked, &wants, &mut replace_in);
|
||||
edit_resource_texts(d, in_scope.resources.as_ref(), &wants, &mut replace_in);
|
||||
Ok(n)
|
||||
})?
|
||||
};
|
||||
@@ -734,13 +909,26 @@ fn replace(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Whether `n` or a descendant that is not hidden or locked, nor in a hidden or locked group,
|
||||
/// shows type in a font `uses` matches: text set in it, an instance of a symbol in `found`, a paint
|
||||
/// naming a pattern in `found`, or such type in the art of its opacity mask.
|
||||
fn reaches(n: &Node, uses: &impl Fn(&TextObject) -> bool, found: &Resources) -> bool {
|
||||
n.visible
|
||||
&& !n.locked
|
||||
&& (matches!(&n.kind, NodeKind::Text(t) if uses(t))
|
||||
|| matches!(&n.kind, NodeKind::SymbolInstance { symbol, .. } if found.symbols.contains(symbol))
|
||||
|| paint_patterns(n).any(|p| found.patterns.contains(p))
|
||||
|| n.extras().mask.as_deref().is_some_and(|m| reaches(&m.art, uses, found))
|
||||
|| n.children().is_some_and(|ch| ch.iter().any(|c| reaches(c, uses, found))))
|
||||
}
|
||||
|
||||
/// What Replace Font says when it left text in the font that it can't reach.
|
||||
const REPLACE_SKIPPED: &str = "some text is hidden or locked, so its font wasn't replaced";
|
||||
|
||||
fn select_font(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let family = str_param(p, "family").ok_or_else(|| bad("select.font", "missing `family`"))?.to_string();
|
||||
let style = str_param(p, "style").map(str::to_string);
|
||||
let ids = scope(s, false, None)?;
|
||||
let (ids, masked) = scope(s, false, None)?;
|
||||
let d = &s.doc()?.doc;
|
||||
let within = FontScope::read(d, p)?;
|
||||
// Only text the Selection tool can reach: not hidden or locked, in a hidden or locked group
|
||||
@@ -752,37 +940,40 @@ fn select_font(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.any(|r| r.style.font_family.eq_ignore_ascii_case(&family) && style.as_ref().is_none_or(|x| r.style.font_style.eq_ignore_ascii_case(x)))
|
||||
};
|
||||
let mut hits: Vec<NodeId> = ids.into_iter().filter(|id| editable(*id) && text(d, *id).is_some_and(uses)).collect();
|
||||
// The symbols whose art uses the font in type that is not hidden or locked, nor in a hidden or
|
||||
// locked group of the art, also through the instances of other symbols it holds.
|
||||
fn reaches(n: &Node, uses: &impl Fn(&TextObject) -> bool, symbols: &BTreeSet<&str>) -> bool {
|
||||
n.visible
|
||||
&& !n.locked
|
||||
&& match &n.kind {
|
||||
NodeKind::Text(t) => uses(t),
|
||||
NodeKind::SymbolInstance { symbol, .. } => symbols.contains(symbol.as_str()),
|
||||
_ => n.children().is_some_and(|ch| ch.iter().any(|c| reaches(c, uses, symbols))),
|
||||
}
|
||||
}
|
||||
let mut symbols: BTreeSet<&str> = BTreeSet::new();
|
||||
// The symbols and patterns whose art uses the font in type that is not hidden or locked, nor in
|
||||
// a hidden or locked group of the art, also through the instances, patterns and opacity masks
|
||||
// it holds. The pattern being edited is left out: its art is on the editing layer.
|
||||
let mut found = Resources::default();
|
||||
loop {
|
||||
let found: Vec<&str> = d
|
||||
let symbols: Vec<String> = d
|
||||
.symbols
|
||||
.iter()
|
||||
.filter(|sym| !symbols.contains(sym.name.as_str()) && reaches(&sym.art, &uses, &symbols))
|
||||
.map(|sym| sym.name.as_str())
|
||||
.filter(|sym| !found.symbols.contains(&sym.name) && reaches(&sym.art, &uses, &found))
|
||||
.map(|sym| sym.name.clone())
|
||||
.collect();
|
||||
if found.is_empty() {
|
||||
let patterns: Vec<String> = d
|
||||
.patterns
|
||||
.iter()
|
||||
.filter(|p| !pattern_being_edited(d, &p.name) && !found.patterns.contains(&p.name) && p.art.iter().any(|a| reaches(a, &uses, &found)))
|
||||
.map(|p| p.name.clone())
|
||||
.collect();
|
||||
if symbols.is_empty() && patterns.is_empty() {
|
||||
break;
|
||||
}
|
||||
symbols.extend(found);
|
||||
found.symbols.extend(symbols);
|
||||
found.patterns.extend(patterns);
|
||||
}
|
||||
// Their instances the Selection tool can reach.
|
||||
if !symbols.is_empty() {
|
||||
// The objects the Selection tool can reach that show such type: the instances of those
|
||||
// symbols, the objects that those patterns paint, and the objects whose opacity mask holds it
|
||||
// (not layers, which the Selection tool doesn't select).
|
||||
let masked: HashSet<NodeId> = masked.into_iter().collect();
|
||||
if !found.symbols.is_empty() || !found.patterns.is_empty() || !masked.is_empty() {
|
||||
let texts: HashSet<NodeId> = hits.iter().copied().collect();
|
||||
d.walk(&mut |n: &Node| {
|
||||
if let NodeKind::SymbolInstance { symbol, .. } = &n.kind
|
||||
&& symbols.contains(symbol.as_str())
|
||||
&& editable(n.id)
|
||||
{
|
||||
let shows = matches!(&n.kind, NodeKind::SymbolInstance { symbol, .. } if found.symbols.contains(symbol))
|
||||
|| paint_patterns(n).any(|p| found.patterns.contains(p))
|
||||
|| (masked.contains(&n.id) && n.extras().mask.as_deref().is_some_and(|m| reaches(&m.art, &uses, &found)));
|
||||
if shows && !n.is_layer() && !texts.contains(&n.id) && editable(n.id) {
|
||||
hits.push(n.id);
|
||||
}
|
||||
});
|
||||
@@ -1311,6 +1502,255 @@ mod tests {
|
||||
assert_eq!(s.execute("text.replaceFont", &serif_to_sans).unwrap(), json!({"runs": 0}));
|
||||
}
|
||||
|
||||
/// Moves object `art` of the layers into the opacity mask of `owner` (a layer or an object).
|
||||
fn mask_with(s: &mut Session, owner: u64, art: u64) {
|
||||
s.edit("Mask", |d, _| {
|
||||
let art = d.remove(NodeId(art))?;
|
||||
d.node_mut(NodeId(owner)).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new((*art).clone(), true)));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// The font families of the first runs of the text in the opacity mask of `owner`.
|
||||
fn mask_families(s: &Session, owner: u64) -> Vec<String> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
mask_texts(d, &[(NodeId(owner), true)]).iter().map(|(_, t, _)| t.runs[0].style.font_family.clone()).collect()
|
||||
}
|
||||
|
||||
/// The font families of the first runs of the text in the art of pattern `name`.
|
||||
fn pattern_families(s: &Session, name: &str) -> Vec<String> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let mut out = vec![];
|
||||
for art in &d.pattern(name).unwrap().art {
|
||||
art_texts(art, true, &mut out);
|
||||
}
|
||||
out.iter().map(|(_, t, _)| t.runs[0].style.font_family.clone()).collect()
|
||||
}
|
||||
|
||||
/// The rows of `text.fonts` with `p`: family, runs and objects.
|
||||
fn font_rows(s: &mut Session, p: Value) -> Vec<(String, u64, u64)> {
|
||||
let fonts = s.execute("text.fonts", &p).unwrap();
|
||||
fonts
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|f| (f["family"].as_str().unwrap().to_string(), f["runs"].as_u64().unwrap(), f["objects"].as_u64().unwrap()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Find Font lists, selects and changes the type in opacity masks: on objects, on a layer,
|
||||
/// disabled and inside the art of another mask. The type in the mask of a locked object is
|
||||
/// listed and left in its font; Find selects the objects whose mask holds the type.
|
||||
#[test]
|
||||
fn find_font_reaches_the_type_in_opacity_masks() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
|
||||
let text = |s: &mut Session, y: i32| {
|
||||
s.execute("text.create", &json!({"x": 10, "y": y, "text": "Serif", "font": "Source Serif 4"})).unwrap()["id"].as_u64().unwrap()
|
||||
};
|
||||
let rect = |s: &mut Session, y: i32| {
|
||||
s.execute("shape.rectangle", &json!({"x": 0, "y": y, "width": 50, "height": 20})).unwrap()["id"].as_u64().unwrap()
|
||||
};
|
||||
let mut owners = vec![];
|
||||
for y in [20, 50, 80] {
|
||||
let owner = rect(&mut s, y);
|
||||
let t = text(&mut s, y);
|
||||
mask_with(&mut s, owner, t);
|
||||
owners.push(owner);
|
||||
}
|
||||
let (open, locked, disabled) = (owners[0], owners[1], owners[2]);
|
||||
s.execute("select.set", &json!({"ids": [locked]})).unwrap();
|
||||
s.execute("object.lock", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [disabled]})).unwrap();
|
||||
s.execute("transparency.disableOpacityMask", &json!({})).unwrap();
|
||||
// A mask whose art is an object with a mask of its own, and a layer's mask.
|
||||
let nested = rect(&mut s, 110);
|
||||
let inner = rect(&mut s, 110);
|
||||
let t = text(&mut s, 110);
|
||||
mask_with(&mut s, inner, t);
|
||||
mask_with(&mut s, nested, inner);
|
||||
let layer = s.doc().unwrap().doc.layers[0].id.0;
|
||||
let t = text(&mut s, 140);
|
||||
mask_with(&mut s, layer, t);
|
||||
let mut on_layers = 0;
|
||||
s.doc().unwrap().doc.walk(|n| on_layers += usize::from(matches!(n.kind, NodeKind::Text(_))));
|
||||
assert_eq!(on_layers, 0, "the type is only in masks");
|
||||
assert_eq!(font_rows(&mut s, json!({})), [("Source Serif 4".to_string(), 5, 5)]);
|
||||
s.execute("select.set", &json!({"ids": [open]})).unwrap();
|
||||
assert_eq!(font_rows(&mut s, json!({"selectionOnly": true})), [("Source Serif 4".to_string(), 1, 1)]);
|
||||
// Find selects the objects whose mask holds the type, not the locked one or the layer.
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 3);
|
||||
let mut selected = s.doc().unwrap().selection.objects.clone();
|
||||
selected.sort();
|
||||
assert_eq!(selected, [NodeId(open), NodeId(disabled), NodeId(nested)]);
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4", "scope": {"artboards": [0]}})).unwrap()["count"], 3);
|
||||
// Change All: one undo step, the locked object's mask left in its font and counted.
|
||||
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let before = steps(&s);
|
||||
let serif_to_sans = json!({"from": {"family": "Source Serif 4"}, "to": {"family": "Source Sans 3"}});
|
||||
let r = s.execute("text.replaceFont", &serif_to_sans).unwrap();
|
||||
assert_eq!((r["runs"].as_u64(), r["skipped"].as_u64()), (Some(4), Some(1)), "{r}");
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
for owner in [open, disabled, nested, layer] {
|
||||
assert_eq!(mask_families(&s, owner), ["Source Sans 3"], "the mask of {owner}");
|
||||
}
|
||||
assert_eq!(mask_families(&s, locked), ["Source Serif 4"]);
|
||||
let r = s.execute("text.replaceFont", &serif_to_sans).unwrap();
|
||||
assert_eq!(r, json!({"runs": 0, "skipped": 1, "warning": REPLACE_SKIPPED}));
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
for owner in [open, disabled, nested, layer] {
|
||||
assert_eq!(mask_families(&s, owner), ["Source Serif 4"], "the mask of {owner} after Undo");
|
||||
}
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(mask_families(&s, nested), ["Source Sans 3"]);
|
||||
// While a mask is edited, its type on the editing layer is counted and changed once.
|
||||
s.execute("transparency.editOpacityMask", &json!({"id": open})).unwrap();
|
||||
assert_eq!(font_rows(&mut s, json!({})), [("Source Sans 3".to_string(), 4, 4), ("Source Serif 4".to_string(), 1, 1)]);
|
||||
let r = s.execute("text.replaceFont", &json!({"from": {"family": "Source Sans 3"}, "to": {"family": "Inter"}})).unwrap();
|
||||
assert_eq!(r["runs"].as_u64(), Some(4), "{r}");
|
||||
let families: Vec<String> = used_fonts(&s.doc().unwrap().doc).into_iter().map(|(family, ..)| family).collect();
|
||||
assert_eq!(families, ["Inter", "Source Serif 4"], "the edited mask is written back after each edit");
|
||||
s.execute("transparency.stopEditingOpacityMask", &json!({})).unwrap();
|
||||
assert_eq!(mask_families(&s, open), ["Inter"]);
|
||||
}
|
||||
|
||||
/// Find Font lists the type in pattern swatches once, also when nothing paints with them;
|
||||
/// Find selects the objects the pattern paints, and Change changes the pattern's art once for
|
||||
/// every object it paints. While a pattern is edited, its type is taken from the editing layer.
|
||||
#[test]
|
||||
fn find_font_reaches_the_type_in_patterns() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
|
||||
let pattern = |s: &mut Session, name: &str, font: &str| {
|
||||
let t = s.execute("text.create", &json!({"x": 10, "y": 20, "text": "Ab", "font": font})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("object.pattern.make", &json!({"ids": [t], "name": name, "edit": false})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [t]})).unwrap();
|
||||
s.execute("edit.clear", &json!({})).unwrap();
|
||||
};
|
||||
pattern(&mut s, "Dots", "Source Serif 4");
|
||||
pattern(&mut s, "Unused", "Inter");
|
||||
let mut on_layers = 0;
|
||||
s.doc().unwrap().doc.walk(|n| on_layers += usize::from(matches!(n.kind, NodeKind::Text(_))));
|
||||
assert_eq!(on_layers, 0, "the type is only in patterns");
|
||||
let all = [("Inter".to_string(), 1, 1), ("Source Serif 4".to_string(), 1, 1)];
|
||||
assert_eq!(font_rows(&mut s, json!({})), all);
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 0);
|
||||
let mut painted = vec![];
|
||||
for y in [20, 60] {
|
||||
painted.push(s.execute("shape.rectangle", &json!({"x": 0, "y": y, "width": 50, "height": 20})).unwrap()["id"].as_u64().unwrap());
|
||||
}
|
||||
s.execute("paint.setFill", &json!({"swatch": "Dots", "ids": painted})).unwrap();
|
||||
assert_eq!(font_rows(&mut s, json!({})), all, "a pattern's type counts once");
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 2);
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [NodeId(painted[0]), NodeId(painted[1])]);
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Inter"})).unwrap()["count"], 0);
|
||||
// Only the selection: the patterns the selected objects paint with.
|
||||
s.execute("select.set", &json!({"ids": [painted[0]]})).unwrap();
|
||||
assert_eq!(font_rows(&mut s, json!({"selectionOnly": true})), [("Source Serif 4".to_string(), 1, 1)]);
|
||||
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let before = steps(&s);
|
||||
let r = s
|
||||
.execute("text.replaceFont", &json!({"from": {"family": "Source Serif 4"}, "to": {"family": "Source Sans 3"}, "selectionOnly": true}))
|
||||
.unwrap();
|
||||
assert_eq!(r, json!({"runs": 1}));
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
assert_eq!(pattern_families(&s, "Dots"), ["Source Sans 3"]);
|
||||
assert_eq!(pattern_families(&s, "Unused"), ["Inter"]);
|
||||
// No run changes: no undo step.
|
||||
let r = s.execute("text.replaceFont", &json!({"from": {"family": "Source Sans 3"}, "to": {"family": "Source Sans 3"}})).unwrap();
|
||||
assert_eq!(r, json!({"runs": 0}));
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
// Change All reaches the swatch nothing paints with.
|
||||
let r = s.execute("text.replaceFont", &json!({"from": {"family": "Inter"}, "to": {"family": "Source Serif 4"}})).unwrap();
|
||||
assert_eq!(r, json!({"runs": 1}));
|
||||
assert_eq!(pattern_families(&s, "Unused"), ["Source Serif 4"]);
|
||||
// A pattern in a hidden fill doesn't show: Find doesn't select the object for its type.
|
||||
let hidden = s.execute("shape.rectangle", &json!({"x": 100, "y": 20, "width": 50, "height": 20})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Unused", "ids": [hidden]})).unwrap();
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 1);
|
||||
s.edit("Hide the fill", |d, _| {
|
||||
for it in d.node_mut(NodeId(hidden)).unwrap().appearance.items.iter_mut() {
|
||||
if let AppearanceItem::Fill(f) = it {
|
||||
f.visible = false;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 0);
|
||||
// Nor does a pattern on a stroke of no width.
|
||||
s.execute("paint.setStroke", &json!({"swatch": "Unused", "ids": [hidden]})).unwrap();
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 1);
|
||||
s.edit("Thin the stroke", |d, _| {
|
||||
for it in d.node_mut(NodeId(hidden)).unwrap().appearance.items.iter_mut() {
|
||||
if let AppearanceItem::Stroke(st) = it {
|
||||
st.width = 0.0;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Serif 4"})).unwrap()["count"], 0);
|
||||
// A locked painted object: its pattern's type is listed and left in its font.
|
||||
s.execute("select.set", &json!({"ids": [painted[1]]})).unwrap();
|
||||
s.execute("object.lock", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [painted[1]]})).unwrap();
|
||||
let r =
|
||||
s.execute("text.replaceFont", &json!({"from": {"family": "Source Sans 3"}, "to": {"family": "Inter"}, "selectionOnly": true})).unwrap();
|
||||
assert_eq!((r["runs"].as_u64(), r["skipped"].as_u64()), (Some(0), Some(1)), "{r}");
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Source Sans 3"})).unwrap()["count"], 1);
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [NodeId(painted[0])]);
|
||||
// While the pattern is edited, its type on the editing layer is counted and changed once.
|
||||
s.execute("object.pattern.edit", &json!({"name": "Dots"})).unwrap();
|
||||
let rows = font_rows(&mut s, json!({}));
|
||||
assert_eq!(rows, [("Source Sans 3".to_string(), 1, 1), ("Source Serif 4".to_string(), 1, 1)]);
|
||||
let r = s.execute("text.replaceFont", &json!({"from": {"family": "Source Sans 3"}, "to": {"family": "Inter"}})).unwrap();
|
||||
assert_eq!(r, json!({"runs": 1}));
|
||||
let families: Vec<String> = used_fonts(&s.doc().unwrap().doc).into_iter().map(|(family, ..)| family).collect();
|
||||
assert_eq!(families, ["Inter", "Source Serif 4"], "the pattern's stored art is left out while it is edited");
|
||||
s.execute("object.pattern.done", &json!({})).unwrap();
|
||||
assert_eq!(pattern_families(&s, "Dots"), ["Inter"]);
|
||||
}
|
||||
|
||||
/// A pattern whose art holds a symbol instance, painting objects inside and outside a symbol:
|
||||
/// Find Font lists each pattern's and symbol's type once and reaches the symbol's type through
|
||||
/// the pattern.
|
||||
#[test]
|
||||
fn find_font_reaches_symbols_through_patterns() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 200, "height": 200})).unwrap();
|
||||
let inter = s.execute("text.create", &json!({"x": 10, "y": 20, "text": "In", "font": "Inter"})).unwrap()["id"].clone();
|
||||
s.execute("symbol.new", &json!({"name": "Mark", "ids": [inter]})).unwrap();
|
||||
let instance = s.doc().unwrap().selection.objects[0].0;
|
||||
let serif = s.execute("text.create", &json!({"x": 10, "y": 60, "text": "Se", "font": "Source Serif 4"})).unwrap()["id"].as_u64().unwrap();
|
||||
let tile = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 80})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("object.pattern.make", &json!({"ids": [tile, serif, instance], "name": "Loop", "edit": false})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [tile, serif, instance]})).unwrap();
|
||||
s.execute("edit.clear", &json!({})).unwrap();
|
||||
// Another object paints with the pattern too: its type is still read once.
|
||||
let other = s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 50, "height": 20})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Loop", "ids": [other]})).unwrap();
|
||||
s.execute("symbol.new", &json!({"name": "Outer", "ids": [other]})).unwrap();
|
||||
let outer = s.doc().unwrap().selection.objects[0];
|
||||
let painted = s.execute("shape.rectangle", &json!({"x": 0, "y": 100, "width": 50, "height": 20})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Loop", "ids": [painted]})).unwrap();
|
||||
let both = [("Inter".to_string(), 1, 1), ("Source Serif 4".to_string(), 1, 1)];
|
||||
assert_eq!(font_rows(&mut s, json!({})), both);
|
||||
s.execute("select.set", &json!({"ids": [painted]})).unwrap();
|
||||
assert_eq!(font_rows(&mut s, json!({"selectionOnly": true})), both);
|
||||
// Find reaches the symbol's type through the pattern, and through a symbol whose art paints
|
||||
// with the pattern.
|
||||
assert_eq!(s.execute("select.font", &json!({"family": "Inter"})).unwrap()["count"], 2);
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [outer, NodeId(painted)]);
|
||||
s.execute("select.set", &json!({"ids": [painted]})).unwrap();
|
||||
let r =
|
||||
s.execute("text.replaceFont", &json!({"from": {"family": "Inter"}, "to": {"family": "Source Sans 3"}, "selectionOnly": true})).unwrap();
|
||||
assert_eq!(r, json!({"runs": 1}));
|
||||
assert_eq!(symbol_texts(&s.doc().unwrap().doc, None)[0].0.runs[0].style.font_family, "Source Sans 3");
|
||||
}
|
||||
|
||||
/// The font family of text object `id`'s first run.
|
||||
fn text_family(s: &Session, id: u64) -> String {
|
||||
text(&s.doc().unwrap().doc, NodeId(id)).unwrap().runs[0].style.font_family.clone()
|
||||
|
||||
@@ -67,7 +67,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Character / Paragraph",
|
||||
[],
|
||||
None,
|
||||
"{ids?|id?, kerning?: 1/1000 em|\"auto\"|\"optical\", baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), underline?, strikethrough?, allCaps?, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), antiAlias?: \"none\"|\"sharp\"|\"crisp\"|\"strong\" (Character panel › Anti-aliasing Method, how the type's edges are drawn as pixels; it sets each targeted type object, with or without a range; each frame of a thread keeps its own method; graph labels keep it for every label of their kind when the graph is drawn again; new type: \"sharp\"), leftIndent?, rightIndent?, firstLineIndent?, spaceBefore?, spaceAfter?: pt (±1296), list?: \"none\"|\"bullet\"|\"number\", bullet?: \"none\"|\"dot\"|\"dash\"|\"circle\"|\"square\", number?: \"none\"|\"arabic\"|\"upperLetters\"|\"lowerLetters\"|\"upperRoman\"|\"lowerRoman\"|\"twoDigits\"|\"threeDigits\", listAlign?: \"left\"|\"right\", listLevel?: 1..9 (Paragraph panel › Bullets and Numbered List: each paragraph of a list starts with its marker, drawn in the font, size and color of the paragraph's first character, and its text starts at the next tab stop after the marker, the left indent counting as a stop; a bulleted list's marker is its bullet, and a numbered list's is the paragraph's number and a period: 1, 2, 3; A to Z, then AA, BB; a to z, then aa, bb; I, II, III; i, ii, iii; 01 to 99, then 100; 001 to 999, then 1000; letters and roman numerals past 3999 in Arabic digits; the numbers count the story's numbered paragraphs, its threaded frames included, from 1 after a paragraph in no list, and an empty or bulleted paragraph is not counted; list \"bullet\" keeps a list's bullet, else a dot, and list \"number\" keeps a numbered list's style, else arabic; without list, a number style makes a numbered list and a bullet makes a bulleted one; \"none\" in any of the three turns the list off; a paragraph that joins a list with left and first-line indents of 0 gets 18 and -18 unless the call sets an indent; a horizontal right-to-left list paragraph has its indents mirrored, measured from the right; listAlign \"right\" (Bullets and Numbering…'s Alignment) puts the marker against the text, and \"left\" (the default) puts it at the first line's start, the text starting at the same place with either; it sets the paragraphs that are in a list or that the call puts in one, and a list keeps it when it changes between bullets and numbers; listLevel (Tab and Shift+Tab at the start of a list paragraph, Bullets and Numbering…'s Level) is a whole number from 1 to 9 that sets the paragraphs in a list or that the call puts in one, a list keeping it when it changes between bullets and numbers, and a list turned on without listLevel starting at level 1; each level past the first sets the paragraph 18 pt further in than its left indent, and each level counts its own numbers: a list paragraph with text sets the counts of the levels deeper than its own back to 0, so a deeper numbered paragraph after it counts from 1, and a paragraph back at a shallower level goes on with that level's count), hyphenate?: bool, wordsLongerThan?: letters 1..25 (5), afterFirst?, beforeLast?: letters 1..15 (3), hyphenLimit?: lines 0..25 (0: no limit), hyphenationZone?: pt 0..1296 (0; Single-line Composer only), hyphenateCapitalized?: bool (true) (Paragraph panel menu › Hyphenation…: only words longer than wordsLongerThan break, keeping afterFirst letters before the hyphen and beforeLast after it; at most hyphenLimit lines in a row end with a hyphen; a line whose text already ends within hyphenationZone of its right edge breaks before the next word instead of hyphenating it; the counts are rounded, and every value is clamped to its range), wordSpacing?: % 0..1000 (80, 100, 133), letterSpacing?: % of a word space -100..500 (0, 0, 0), glyphScaling?: % 50..200 (100, 100, 100): each [min, desired, max] or {min?, desired?, max?} (Paragraph panel menu › Justification…: Desired applies to every line, Minimum and Maximum bound what justified lines may use; values are clamped to their range, and Minimum ≤ Desired ≤ Maximum must hold afterwards or nothing changes), autoLeading?: % of the size 0..500 (120; the leading of characters whose leading is Auto), singleWordJustification?: \"fullJustify\"|\"alignLeft\"|\"alignCenter\"|\"alignRight\" (a justified line holding one word), mojikumi?: \"none\"|\"lineEndHalf\" (Japanese punctuation spacing), kinsoku?: \"none\"|\"hard\"|\"soft\" (Kinsoku Set: Soft lets 々, ー and small kana start a line), kinsokuType?: \"pushInFirst\"|\"pushOutFirst\"|\"pushOutOnly\" (Paragraph panel menu › Kinsoku Shori Type: how area type keeps those characters off a line's ends; pushInFirst sets them on the line when its gaps next to CJK characters can close up enough, by at most a tenth of an em each, pushOutFirst does so only when moving the character before them to the next line would leave
Line truncated
|
||||
"{ids?|id?, kerning?: 1/1000 em|\"auto\"|\"optical\", baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), underline?, strikethrough?, allCaps?, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), antiAlias?: \"none\"|\"sharp\"|\"crisp\"|\"strong\" (Character panel › Anti-aliasing Method, how the type's edges are drawn as pixels; it sets each targeted type object, with or without a range; each frame of a thread keeps its own method; graph labels keep it for every label of their kind when the graph is drawn again; new type: \"sharp\"), leftIndent?, rightIndent?, firstLineIndent?, spaceBefore?, spaceAfter?: pt (±1296), list?: \"none\"|\"bullet\"|\"number\", bullet?: \"none\"|\"dot\"|\"dash\"|\"circle\"|\"square\", number?: \"none\"|\"arabic\"|\"upperLetters\"|\"lowerLetters\"|\"upperRoman\"|\"lowerRoman\"|\"twoDigits\"|\"threeDigits\", listAlign?: \"left\"|\"right\", listLevel?: 1..9 (Paragraph panel › Bullets and Numbered List: each paragraph of a list starts with its marker, drawn in the font, size and color of the paragraph's first character, and its text starts at the next tab stop after the marker, the left indent counting as a stop; a bulleted list's marker is its bullet, and a numbered list's is the paragraph's number and a period: 1, 2, 3; A to Z, then AA, BB; a to z, then aa, bb; I, II, III; i, ii, iii; 01 to 99, then 100; 001 to 999, then 1000; letters and roman numerals past 3999 in Arabic digits; the numbers count the story's numbered paragraphs, its threaded frames included, from 1 after a paragraph in no list, and an empty or bulleted paragraph is not counted; list \"bullet\" keeps a list's bullet, else a dot, and list \"number\" keeps a numbered list's style, else arabic; without list, a number style makes a numbered list and a bullet makes a bulleted one; \"none\" in any of the three turns the list off; a paragraph that joins a list with left and first-line indents of 0 gets 18 and -18 unless the call sets an indent; a horizontal right-to-left list paragraph has its indents mirrored, measured from the right; listAlign \"right\" (Bullets and Numbering…'s Alignment) puts the marker against the text, and \"left\" (the default) puts it at the first line's start, the text starting at the same place with either; it sets the paragraphs that are in a list or that the call puts in one, and a list keeps it when it changes between bullets and numbers; listLevel (Tab and Shift+Tab at the start of a list paragraph, Bullets and Numbering…'s Level) is a whole number from 1 to 9 that sets the paragraphs in a list or that the call puts in one, a list keeping it when it changes between bullets and numbers, and a list turned on without listLevel starting at level 1; each level past the first sets the paragraph 18 pt further in than its left indent, and each level counts its own numbers: a list paragraph with text sets the counts of the levels deeper than its own back to 0, so a deeper numbered paragraph after it counts from 1, and a paragraph back at a shallower level goes on with that level's count), hyphenate?: bool, wordsLongerThan?: letters 1..25 (5), afterFirst?, beforeLast?: letters 1..15 (3), hyphenLimit?: lines 0..25 (0: no limit), hyphenationZone?: pt 0..1296 (0; Single-line Composer only), hyphenateCapitalized?: bool (true) (Paragraph panel menu › Hyphenation…: only words longer than wordsLongerThan break, keeping afterFirst letters before the hyphen and beforeLast after it; at most hyphenLimit lines in a row end with a hyphen; a line whose text already ends within hyphenationZone of its right edge breaks before the next word instead of hyphenating it; the counts are rounded, and every value is clamped to its range), wordSpacing?: % 0..1000 (80, 100, 133), letterSpacing?: % of a word space -100..500 (0, 0, 0), glyphScaling?: % 50..200 (100, 100, 100): each [min, desired, max] or {min?, desired?, max?} (Paragraph panel menu › Justification…: Desired applies to every line, Minimum and Maximum bound what justified lines may use; values are clamped to their range, and Minimum ≤ Desired ≤ Maximum must hold afterwards or nothing changes), autoLeading?: % of the size 0..500 (120; the leading of characters whose leading is Auto), singleWordJustification?: \"fullJustify\"|\"alignLeft\"|\"alignCenter\"|\"alignRight\" (a justified line holding one word), mojikumi?: \"none\"|\"lineEndHalf\" (Japanese punctuation spacing), kinsoku?: \"none\"|\"hard\"|\"soft\" (Kinsoku Set: Soft lets 々, ー and small kana start a line), kinsokuType?: \"pushInFirst\"|\"pushOutFirst\"|\"pushOutOnly\" (Paragraph panel menu › Kinsoku Shori Type: how area type keeps those characters off a line's ends; pushInFirst sets them on the line when its gaps next to CJK characters can close up enough, by at most a tenth of an em each, pushOutFirst does so only when moving the character before them to the next line would leave
Line truncated
|
||||
has_doc,
|
||||
set_format
|
||||
),
|
||||
@@ -571,6 +571,8 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"tateChuYoko",
|
||||
"tcyUpDown",
|
||||
"tcyLeftRight",
|
||||
"akiLeft",
|
||||
"akiRight",
|
||||
"standardVerticalRoman",
|
||||
"noBreak",
|
||||
"digits",
|
||||
@@ -613,6 +615,8 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let font_height_reference = super::textedit::font_height_reference_param(p, C)?;
|
||||
let tcy_up_down = super::textedit::tcy_offset_param(p, "tcyUpDown", C)?;
|
||||
let tcy_left_right = super::textedit::tcy_offset_param(p, "tcyLeftRight", C)?;
|
||||
let aki_left = super::textedit::aki_param(p, "akiLeft", C)?;
|
||||
let aki_right = super::textedit::aki_param(p, "akiRight", C)?;
|
||||
let mojikumi = match p.get("mojikumi") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(match v.as_str() {
|
||||
@@ -773,6 +777,12 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(v) = tcy.1 {
|
||||
st.tcy_left_right = v;
|
||||
}
|
||||
if let Some(v) = aki_left {
|
||||
st.aki_left = v;
|
||||
}
|
||||
if let Some(v) = aki_right {
|
||||
st.aki_right = v;
|
||||
}
|
||||
if let Some(v) = flag("standardVerticalRoman") {
|
||||
st.standard_vertical_roman = v;
|
||||
}
|
||||
|
||||
@@ -627,6 +627,8 @@ pub(super) const ALLOWED: &[&str] = &[
|
||||
"text.thread.releaseSelection",
|
||||
"text.thread.remove",
|
||||
"text.touchType",
|
||||
"threeD.addGraphic",
|
||||
"threeD.mapGraphic",
|
||||
"transparency.disableOpacityMask",
|
||||
"transparency.editOpacityMask",
|
||||
"transparency.enableOpacityMask",
|
||||
|
||||
@@ -40,7 +40,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Character",
|
||||
[],
|
||||
None,
|
||||
"{id? (default: the one selected type object), start?: byte, end?: byte (default: all text), story?: bool = false (all the text of every frame of the threaded story `id` is a frame of, in one undo step; no `start` or `end`), font?, style?, size?: pt 0.1..1296 (outside it: an error), leading?: pt|\"auto\", tracking?, kerning?: 1/1000 em|\"auto\"|\"optical\" (the space after each character), kerningBefore?: 1/1000 em -1000..10000 (the space before a character that starts a line by itself: the first character of the text, of a paragraph or after a forced line break; no effect elsewhere; 0 by default), baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), fill?: colour|\"none\", stroke?: colour|\"none\" (#rrggbbaa and #rgba are refused in both), keepModel?: bool (fill and stroke, as paint.setFill), strokeWidth?: pt, strokeOptions?: {weight?, cap?, join?, miterLimit?, align?, dash?, dashOffset?, alignDashes?} (as stroke.set: the character stroke), underline?, strikethrough?, allCaps?: bool, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), features?: [\"dlig\", \"-liga\", \"salt=2\", …] (swsh, ss01–ss20, salt, titl, pnum, lnum, sups, subs, numr, dnom and aalt take the font's alternate number as `tag=N`, N 0–65535: \"salt=2\" draws the second alternate of each glyph that has one, \"salt\" is \"salt=1\", \"-salt\" and \"salt=0\" are off; the range is shaped with its word, so a letter's alternate keeps it joined to its neighbours), featureChanges?: [\"tnum\", \"-onum\", \"salt=2\", …] (OpenType features turned on, or with - off, on top of each character's own, a value replacing the feature's last one; its other features are kept), charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\", proportionalMetrics?: bool (full-width glyphs on the font's proportional widths: `palt` in horizontal type, `vpal` in vertical type), tateChuYoko?: bool (Character panel menu › Tate-chu-yoko, with Show East Asian Options: in vertical type those characters stand side by side across one em of the column; kept on the characters and ignored in horizontal type), tcyUpDown?, tcyLeftRight?: pt -1296..1296 (Character panel menu › Tate-chu-yoko Settings: in vertical type a block of tate-chu-yoko characters moves up by a positive Up/Down and down by a negative one, right by a positive Left/Right and left by a negative one, as one piece with the values of its first character; on a vertical path a positive Up/Down moves it toward the path's start and a positive Left/Right to the path's left; 0 by default; automatic digit blocks and horizontal type do not move; neither turns tateChuYoko on), standardVerticalRoman?: bool (Character panel menu › Standard Vertical Roman Alignment, with Show East Asian Options: in vertical type those half-width characters, such as Latin letters and digits, stand upright one below the other instead of being rotated; spaces and tate-chu-yoko blocks are not changed; kept on the characters and ignored in horizontal type; off by default), noBreak?: bool (Character panel menu › No Break: these characters stay on one line, including across a space inside the run), kashidas?: bool (Character panel menu › Kashidas: selected characters allow insertion only when both joined neighbors have it on; on by default), charDirection?: \"default\"|\"leftToRight\"|\"rightToLeft\" (Character panel menu › Character Direction: the characters are set left to right or right to left inside their paragraph, as a Unicode bidi embedding; default: as the bidi algorithm sets them), digits?: \"default\"|\"arabic\"|\"hindi\"|\"farsi\"|\"devanagari\"|\"bengali\"|\"gurmukhi\"|\"gujarati\"|\"odia\"|\"tamil\"|\"telugu\"|\"kannada\"|\"malayalam\" (Character panel menu › Digits: the numeral system every digit is drawn in, whatever was typed: Western, Arabic-Indic (hindi), Eastern Arabic-Indic (farsi) or an Indic script's own digits), fontHeightReference?: \"emBox\"|\"capHeight\"|\"xHeight\"|\"icfBox\" (Character panel › Set Font Height Reference: the height of the font that size measures, the em box by default; with a font that has no such height, or one outside 0.1 to 2 em, the em box), language?: name|tag (Character panel › Language: one of Document Setup's languages by name or BCP 47 tag, case-insensitive, such as \"Turkish\" or \"tr\"; the characters are shaped in it, so the font uses that language's forms (OpenType `locl`); saved as the tag, English: USA, the default, as none; new type takes the document's language), fontAxes?: {\"wght\": 650, …} (Character panel › Variable Font: the setting of each variation axis named, in the axis's units and clamped to its range; null: back to the style's own setting; other axes keep theirs; tags the font has no axis for are left out; a font or style change clears them; text.fontList {family, style} lists the axes), glyphAlternate?: null (takes
Line truncated
|
||||
"{id? (default: the one selected type object), start?: byte, end?: byte (default: all text), story?: bool = false (all the text of every frame of the threaded story `id` is a frame of, in one undo step; no `start` or `end`), font?, style?, size?: pt 0.1..1296 (outside it: an error), leading?: pt|\"auto\", tracking?, kerning?: 1/1000 em|\"auto\"|\"optical\" (the space after each character), kerningBefore?: 1/1000 em -1000..10000 (the space before a character that starts a line by itself: the first character of the text, of a paragraph or after a forced line break; no effect elsewhere; 0 by default), baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg counter-clockwise (0: upright), fill?: colour|\"none\", stroke?: colour|\"none\" (#rrggbbaa and #rgba are refused in both), keepModel?: bool (fill and stroke, as paint.setFill), strokeWidth?: pt, strokeOptions?: {weight?, cap?, join?, miterLimit?, align?, dash?, dashOffset?, alignDashes?} (as stroke.set: the character stroke), underline?, strikethrough?, allCaps?: bool, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), features?: [\"dlig\", \"-liga\", \"salt=2\", …] (swsh, ss01–ss20, salt, titl, pnum, lnum, sups, subs, numr, dnom and aalt take the font's alternate number as `tag=N`, N 0–65535: \"salt=2\" draws the second alternate of each glyph that has one, \"salt\" is \"salt=1\", \"-salt\" and \"salt=0\" are off; the range is shaped with its word, so a letter's alternate keeps it joined to its neighbours), featureChanges?: [\"tnum\", \"-onum\", \"salt=2\", …] (OpenType features turned on, or with - off, on top of each character's own, a value replacing the feature's last one; its other features are kept), charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\", proportionalMetrics?: bool (full-width glyphs on the font's proportional widths: `palt` in horizontal type, `vpal` in vertical type), tateChuYoko?: bool (Character panel menu › Tate-chu-yoko, with Show East Asian Options: in vertical type those characters stand side by side across one em of the column; kept on the characters and ignored in horizontal type), tcyUpDown?, tcyLeftRight?: pt -1296..1296 (Character panel menu › Tate-chu-yoko Settings: in vertical type a block of tate-chu-yoko characters moves up by a positive Up/Down and down by a negative one, right by a positive Left/Right and left by a negative one, as one piece with the values of its first character; on a vertical path a positive Up/Down moves it toward the path's start and a positive Left/Right to the path's left; 0 by default; automatic digit blocks and horizontal type do not move; neither turns tateChuYoko on), akiLeft?, akiRight?: 1/1000 em 0..1000 (Character panel › Insert Aki Left and Right, with Show East Asian Options: space before and after each character in reading order, on its left and right in left-to-right type, on its right and left in right-to-left type, above and below it in vertical type; a ligature or a letter with its marks takes the values of its first character; line wrapping counts it, alignment keeps it at a line's ends and justification does not stretch or narrow it; 0 by default; a value that is not a number or is outside the range is refused, naming the key), standardVerticalRoman?: bool (Character panel menu › Standard Vertical Roman Alignment, with Show East Asian Options: in vertical type those half-width characters, such as Latin letters and digits, stand upright one below the other instead of being rotated; spaces and tate-chu-yoko blocks are not changed; kept on the characters and ignored in horizontal type; off by default), noBreak?: bool (Character panel menu › No Break: these characters stay on one line, including across a space inside the run), kashidas?: bool (Character panel menu › Kashidas: selected characters allow insertion only when both joined neighbors have it on; on by default), charDirection?: \"default\"|\"leftToRight\"|\"rightToLeft\" (Character panel menu › Character Direction: the characters are set left to right or right to left inside their paragraph, as a Unicode bidi embedding; default: as the bidi algorithm sets them), digits?: \"default\"|\"arabic\"|\"hindi\"|\"farsi\"|\"devanagari\"|\"bengali\"|\"gurmukhi\"|\"gujarati\"|\"odia\"|\"tamil\"|\"telugu\"|\"kannada\"|\"malayalam\" (Character panel menu › Digits: the numeral system every digit is drawn in, whatever was typed: Western, Arabic-Indic (hindi), Eastern Arabic-Indic (farsi) or an Indic script's own digits), fontHeightReference?: \"emBox\"|\"capHeight\"|\"xHeight\"|\"icfBox\" (Character panel › Set Font Height Reference: the height of the font that size measures, the em box by default; with a font that has no such height, or one outside 0.1 to 2 em, the em box), language?: name|tag (Character panel › Language: one of Document Setup's languages by name or BCP 47 tag, case-insensitive, such a
Line truncated
|
||||
has_doc,
|
||||
set_range_style
|
||||
),
|
||||
@@ -459,6 +459,9 @@ pub(crate) struct CharChange {
|
||||
/// Tate-chu-yoko Settings › Up/Down and Left/Right, in points.
|
||||
tcy_up_down: Option<f64>,
|
||||
tcy_left_right: Option<f64>,
|
||||
/// Insert Aki Left and Right, in 1/1000 em.
|
||||
aki_left: Option<f64>,
|
||||
aki_right: Option<f64>,
|
||||
standard_vertical_roman: Option<bool>,
|
||||
no_break: Option<bool>,
|
||||
digits: Option<vectorcraft_doc::Digits>,
|
||||
@@ -492,6 +495,15 @@ pub(crate) fn tcy_offset_param(p: &Value, key: &str, cmd: &str) -> Result<Option
|
||||
if (lo..=hi).contains(&v) { Ok(Some(v)) } else { Err(bad(cmd, format!("`{key}` must be from {lo} to {hi}"))) }
|
||||
}
|
||||
|
||||
/// `akiLeft` or `akiRight` (Insert Aki Left and Right): a number of thousandths of an em from 0 to
|
||||
/// [`vectorcraft_doc::text::MAX_AKI`]. Any other value is an error.
|
||||
pub(crate) fn aki_param(p: &Value, key: &str, cmd: &str) -> Result<Option<f64>> {
|
||||
let Some(v) = p.get(key).filter(|v| !v.is_null()) else { return Ok(None) };
|
||||
let v = v.as_f64().filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, format!("`{key}` must be a number")))?;
|
||||
let (lo, hi) = (0.0, vectorcraft_doc::text::MAX_AKI);
|
||||
if (lo..=hi).contains(&v) { Ok(Some(v)) } else { Err(bad(cmd, format!("`{key}` must be from {lo} to {hi}"))) }
|
||||
}
|
||||
|
||||
/// `digits: "default"|"arabic"|"hindi"|"farsi"|"devanagari"|…` ([`Digits::id`]) → the numeral
|
||||
/// system the digits are drawn in.
|
||||
///
|
||||
@@ -754,6 +766,8 @@ impl CharChange {
|
||||
tate_chu_yoko: flag("tateChuYoko"),
|
||||
tcy_up_down: tcy_offset_param(p, "tcyUpDown", cmd)?,
|
||||
tcy_left_right: tcy_offset_param(p, "tcyLeftRight", cmd)?,
|
||||
aki_left: aki_param(p, "akiLeft", cmd)?,
|
||||
aki_right: aki_param(p, "akiRight", cmd)?,
|
||||
standard_vertical_roman: flag("standardVerticalRoman"),
|
||||
no_break: flag("noBreak"),
|
||||
digits: digits_param(p, cmd)?,
|
||||
@@ -811,6 +825,8 @@ impl CharChange {
|
||||
&& self.tate_chu_yoko.is_none()
|
||||
&& self.tcy_up_down.is_none()
|
||||
&& self.tcy_left_right.is_none()
|
||||
&& self.aki_left.is_none()
|
||||
&& self.aki_right.is_none()
|
||||
&& self.standard_vertical_roman.is_none()
|
||||
&& self.no_break.is_none()
|
||||
&& self.digits.is_none()
|
||||
@@ -877,6 +893,12 @@ impl CharChange {
|
||||
if let Some(v) = self.tcy_left_right {
|
||||
st.tcy_left_right = v;
|
||||
}
|
||||
if let Some(v) = self.aki_left {
|
||||
st.aki_left = v;
|
||||
}
|
||||
if let Some(v) = self.aki_right {
|
||||
st.aki_right = v;
|
||||
}
|
||||
if let Some(v) = self.standard_vertical_roman {
|
||||
st.standard_vertical_roman = v;
|
||||
}
|
||||
|
||||
@@ -215,6 +215,8 @@ pub fn char_attrs(v: &CharStyle) -> Map<String, Value> {
|
||||
("tateChuYoko", json!(v.tate_chu_yoko)),
|
||||
("tcyUpDown", json!(v.tcy_up_down)),
|
||||
("tcyLeftRight", json!(v.tcy_left_right)),
|
||||
("akiLeft", json!(v.aki_left)),
|
||||
("akiRight", json!(v.aki_right)),
|
||||
("standardVerticalRoman", json!(v.standard_vertical_roman)),
|
||||
("noBreak", json!(v.no_break)),
|
||||
("digits", json!(v.digits)),
|
||||
@@ -558,6 +560,12 @@ fn attrs_param(d: &Document, p: &Value, kind: Kind, verb: &str, kept: Option<&Ma
|
||||
return Err(bad(&c, format!("`{key}` must be from {} to {shift_max}", -shift_max)));
|
||||
}
|
||||
}
|
||||
let (lo, hi) = (0.0, vectorcraft_doc::text::MAX_AKI);
|
||||
for (key, value) in [("akiLeft", st.aki_left), ("akiRight", st.aki_right)] {
|
||||
if changed(key) && !(lo..=hi).contains(&value) {
|
||||
return Err(bad(&c, format!("`{key}` must be from {lo} to {hi}")));
|
||||
}
|
||||
}
|
||||
// A language of the list, stored as its tag (as text.setFormat takes it).
|
||||
if let Some(l) = &st.language
|
||||
&& kept.and_then(|k| k.get("language")) != m.get("language")
|
||||
@@ -1042,17 +1050,19 @@ fn unused(s: &mut Session, kind: Kind) -> Result<Value> {
|
||||
|
||||
/// The styles of `kind` in `src` that load, with their source names so that art in the file keeps
|
||||
/// referring to them: not the Normal style, and not one whose attributes don't read, whose sizes,
|
||||
/// scales, leading, shifts or stroke width are out of the range `charStyle.new` takes, whose
|
||||
/// scales, leading, shifts, Insert Aki or stroke width are out of the range `charStyle.new` takes, whose
|
||||
/// indents, spacing, Hyphenation or Justification options or tab stops are out of the range
|
||||
/// `paraStyle.new` takes, or whose name is empty, which are skipped. → (the styles, the names in
|
||||
/// `src` of those skipped)
|
||||
fn loadable(src: &Document, kind: Kind) -> (Vec<TextStyleDef>, Vec<String>) {
|
||||
const RANGED: [&str; 16] = [
|
||||
const RANGED: [&str; 18] = [
|
||||
"size",
|
||||
"h_scale",
|
||||
"v_scale",
|
||||
"leading",
|
||||
"baseline_shift",
|
||||
"akiLeft",
|
||||
"akiRight",
|
||||
"tcyUpDown",
|
||||
"tcyLeftRight",
|
||||
"stroke_width",
|
||||
|
||||
@@ -395,6 +395,8 @@ fn character_style_dimensions_are_checked_before_a_definition_changes() {
|
||||
json!({"baseline_shift": 1296.1}),
|
||||
json!({"tcyUpDown": 1296.1}),
|
||||
json!({"tcyLeftRight": -1296.1}),
|
||||
json!({"akiLeft": -0.1}),
|
||||
json!({"akiRight": 1000.1}),
|
||||
] {
|
||||
let key = attrs.as_object().unwrap().keys().next().unwrap();
|
||||
for cmd in ["charStyle.edit", "charStyle.setAttrs", "charStyle.new"] {
|
||||
@@ -416,7 +418,8 @@ fn character_style_dimensions_are_checked_before_a_definition_changes() {
|
||||
"size": 1296.0, "leading": 5000.0,
|
||||
"h_scale": 1.0, "v_scale": 10000.0,
|
||||
"baseline_shift": -1296.0,
|
||||
"tcyUpDown": 1296.0, "tcyLeftRight": -1296.0
|
||||
"tcyUpDown": 1296.0, "tcyLeftRight": -1296.0,
|
||||
"akiLeft": 0.0, "akiRight": 1000.0
|
||||
}}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -1663,6 +1663,63 @@ fn tate_chu_yoko_settings_are_character_attributes_saved_only_when_set() {
|
||||
assert_eq!(offsets(&s), before);
|
||||
}
|
||||
|
||||
/// Insert Aki Left and Right (Character panel, East Asian options): character attributes in
|
||||
/// thousandths of an em, 0 by default and saved only when not 0. Both commands set either side
|
||||
/// alone, keeping the other side of every run, in one undo step; a value that is not a number or
|
||||
/// lies outside 0 to 1000 is refused, naming the key, before anything changes; a character style
|
||||
/// captured from the characters carries them.
|
||||
#[test]
|
||||
fn insert_aki_is_a_character_attribute_set_one_side_at_a_time() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
|
||||
let id = s.execute("text.create", &json!({"x": 10, "y": 50, "text": "(日本)"})).unwrap()["id"].as_u64().unwrap();
|
||||
let runs = |s: &Session| match &s.doc().unwrap().doc.node(NodeId(id)).unwrap().kind {
|
||||
NodeKind::Text(t) => t.runs.clone(),
|
||||
_ => panic!("text"),
|
||||
};
|
||||
let aki = |s: &Session| runs(s).iter().map(|r| (r.text.clone(), r.style.aki_left, r.style.aki_right)).collect::<Vec<_>>();
|
||||
let saved = serde_json::to_value(&runs(&s)[0].style).unwrap();
|
||||
assert!(saved.get("akiLeft").is_none() && saved.get("akiRight").is_none());
|
||||
s.execute("select.set", &json!({"ids": [id]})).unwrap();
|
||||
s.execute("text.setFormat", &json!({"akiLeft": 250})).unwrap();
|
||||
assert_eq!(aki(&s), [("(日本)".to_string(), 250.0, 0.0)]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(aki(&s), [("(日本)".to_string(), 0.0, 0.0)], "one undo step");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(aki(&s), [("(日本)".to_string(), 250.0, 0.0)]);
|
||||
|
||||
// One side on part of the text: the runs keep their other side.
|
||||
let paren = "(".len();
|
||||
s.execute("text.setRangeStyle", &json!({"id": id, "start": 0, "end": paren, "akiRight": 500})).unwrap();
|
||||
assert_eq!(aki(&s), [("(".to_string(), 250.0, 500.0), ("日本)".to_string(), 250.0, 0.0)]);
|
||||
s.execute("text.setFormat", &json!({"akiLeft": 0})).unwrap();
|
||||
assert_eq!(aki(&s), [("(".to_string(), 0.0, 500.0), ("日本)".to_string(), 0.0, 0.0)]);
|
||||
let saved = serde_json::to_value(&runs(&s)[0].style).unwrap();
|
||||
assert_eq!((saved.get("akiLeft"), saved["akiRight"].clone()), (None, json!(500.0)));
|
||||
// Saved and reopened as they were.
|
||||
let back = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
|
||||
let NodeKind::Text(t) = &back.node(NodeId(id)).unwrap().kind else { panic!("text") };
|
||||
let reopened: Vec<_> = t.runs.iter().map(|r| (r.text.clone(), r.style.aki_left, r.style.aki_right)).collect();
|
||||
assert_eq!(reopened, aki(&s));
|
||||
let attrs = super::super::textstyles::char_attrs(&runs(&s)[0].style);
|
||||
assert_eq!((attrs["akiLeft"].clone(), attrs["akiRight"].clone()), (json!(0.0), json!(500.0)));
|
||||
|
||||
let before = aki(&s);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
for (cmd, p, want) in [
|
||||
("text.setFormat", json!({"akiLeft": "250"}), "`akiLeft` must be a number"),
|
||||
("text.setFormat", json!({"akiRight": -1}), "`akiRight` must be from 0 to 1000"),
|
||||
("text.setFormat", json!({"akiLeft": 250, "akiRight": 1000.5}), "`akiRight` must be from 0 to 1000"),
|
||||
("text.setRangeStyle", json!({"id": id, "akiRight": true}), "`akiRight` must be a number"),
|
||||
("text.setRangeStyle", json!({"id": id, "start": 0, "end": paren, "akiLeft": 2000}), "`akiLeft` must be from 0 to 1000"),
|
||||
] {
|
||||
let e = s.execute(cmd, &p).unwrap_err();
|
||||
assert!(matches!(&e, EngineError::BadParams { msg, .. } if msg == want), "{cmd} {p}: {e}");
|
||||
}
|
||||
assert_eq!(aki(&s), before);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step for a refused call");
|
||||
}
|
||||
|
||||
/// Standard Vertical Roman Alignment (Character panel menu, East Asian options): a character
|
||||
/// attribute, off by default and saved only when on. Documents written before it read as off; a
|
||||
/// character style captured from the characters carries it.
|
||||
|
||||
@@ -492,6 +492,222 @@ fn pasting_again_reuses_what_an_earlier_paste_added() {
|
||||
assert!(doc(&s).swatch("Brand 3").is_none() && doc(&s).char_styles.len() == 1);
|
||||
}
|
||||
|
||||
/// The active document, to edit its definitions directly.
|
||||
fn doc_mut(s: &mut Session) -> &mut Document {
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc)
|
||||
}
|
||||
|
||||
/// The pattern a paint paints with.
|
||||
fn pattern_of(p: &Paint) -> Option<&str> {
|
||||
match p {
|
||||
Paint::Pattern { pattern, .. } => Some(pattern),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The pattern the fill of the active document's character style `name` paints with.
|
||||
fn style_pattern(s: &Session, name: &str) -> Option<String> {
|
||||
let attrs = &doc(s).char_styles.iter().find(|x| x.name == name)?.attrs;
|
||||
pattern_of(&serde_json::from_value::<Paint>(attrs.get("fill")?.clone()).ok()?).map(str::to_string)
|
||||
}
|
||||
|
||||
/// A pattern "Dots" made from a `size` square of `color` in the active document.
|
||||
fn dots(s: &mut Session, size: f64, color: &str) {
|
||||
let square = rect(s, 300.0, 200.0, size, size);
|
||||
s.execute("paint.setFill", &json!({"ids": [square.0], "color": color})).unwrap();
|
||||
s.execute("object.pattern.make", &json!({"name": "Dots", "edit": false, "ids": [square.0]})).unwrap();
|
||||
}
|
||||
|
||||
/// Type "Hi" filled with the pattern "Dots" (a small red square), set in a character style
|
||||
/// "Patterned" whose fill is that pattern.
|
||||
fn patterned_type(s: &mut Session) -> NodeId {
|
||||
dots(s, 10.0, "#ff0000");
|
||||
let t = id_of(&s.execute("text.create", &json!({"x": 10, "y": 50, "text": "Hi"})).unwrap());
|
||||
s.execute("paint.setFill", &json!({"ids": [t.0], "swatch": "Dots"})).unwrap();
|
||||
s.execute("charStyle.new", &json!({"name": "Patterned", "id": t.0})).unwrap();
|
||||
s.execute("charStyle.apply", &json!({"name": "Patterned", "id": t.0})).unwrap();
|
||||
t
|
||||
}
|
||||
|
||||
/// The character style name of a type object's first run, and the pattern that run paints with.
|
||||
fn type_links(s: &Session, id: NodeId) -> (Option<String>, Option<String>) {
|
||||
let NodeKind::Text(tx) = node(s, id).kind else { panic!("not type") };
|
||||
(tx.runs[0].style.style_name.clone(), pattern_of(&tx.runs[0].style.fill).map(str::to_string))
|
||||
}
|
||||
|
||||
/// Type set in a style that paints with a pattern, pasted into a document with another pattern of
|
||||
/// that name: the pattern comes in as "Dots 2", and the style paints with "Dots 2" too, so applying
|
||||
/// it later paints with the pasted pattern. Pasting again adds nothing.
|
||||
#[test]
|
||||
fn pasted_styles_paint_with_the_renamed_pattern_they_use() {
|
||||
let mut s = session();
|
||||
let t = patterned_type(&mut s);
|
||||
copy(&mut s, &[t]);
|
||||
new_doc(&mut s);
|
||||
dots(&mut s, 30.0, "#0000ff");
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert!(doc(&s).pattern("Dots 2").is_some(), "{v}");
|
||||
assert_eq!(style_pattern(&s, "Patterned").as_deref(), Some("Dots 2"), "{v}");
|
||||
let named = |n: &str| Some(n.to_string());
|
||||
assert_eq!(type_links(&s, ids_of(&v)[0]), (named("Patterned"), named("Dots 2")));
|
||||
let other = id_of(&s.execute("text.create", &json!({"x": 10, "y": 150, "text": "Yo"})).unwrap());
|
||||
s.execute("charStyle.apply", &json!({"name": "Patterned", "id": other.0})).unwrap();
|
||||
assert_eq!(type_links(&s, other).1.as_deref(), Some("Dots 2"), "the applied style paints with the pasted pattern");
|
||||
let counts = |s: &Session| (doc(s).patterns.len(), doc(s).char_styles.len());
|
||||
let before = counts(&s);
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(v["added"], 0, "{v}");
|
||||
assert_eq!(counts(&s), before);
|
||||
assert_eq!(type_links(&s, ids_of(&v)[0]), (named("Patterned"), named("Dots 2")));
|
||||
}
|
||||
|
||||
/// A document whose "Patterned" style has the pasted style's attributes but whose "Dots" is
|
||||
/// another pattern: that style paints with the document's "Dots", so the pasted one comes in as
|
||||
/// "Patterned 2", painting with "Dots 2", and the document's own is left as it was. Pasting again
|
||||
/// uses the numbered copies.
|
||||
#[test]
|
||||
fn pasted_styles_are_not_matched_to_styles_that_paint_with_another_pattern() {
|
||||
let mut s = session();
|
||||
let t = patterned_type(&mut s);
|
||||
let style = doc(&s).char_styles.iter().find(|x| x.name == "Patterned").unwrap().clone();
|
||||
copy(&mut s, &[t]);
|
||||
new_doc(&mut s);
|
||||
dots(&mut s, 30.0, "#0000ff");
|
||||
doc_mut(&mut s).char_styles.push(style);
|
||||
let own = (doc(&s).char_styles.clone(), doc(&s).pattern("Dots").cloned());
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(style_pattern(&s, "Patterned 2").as_deref(), Some("Dots 2"), "{v}");
|
||||
let d = doc(&s);
|
||||
assert_eq!((&d.char_styles[..own.0.len()], d.pattern("Dots")), (&own.0[..], own.1.as_ref()), "the document's own are unchanged");
|
||||
let named = |n: &str| Some(n.to_string());
|
||||
let links = (named("Patterned 2"), named("Dots 2"));
|
||||
assert_eq!(type_links(&s, ids_of(&v)[0]), links);
|
||||
let counts = |s: &Session| (doc(s).patterns.len(), doc(s).char_styles.len());
|
||||
let before = counts(&s);
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(v["added"], 0, "{v}");
|
||||
assert_eq!(counts(&s), before);
|
||||
assert_eq!(type_links(&s, ids_of(&v)[0]), links);
|
||||
}
|
||||
|
||||
/// A style that paints with a pattern whose tile sets type in that same style is pasted with the
|
||||
/// pattern: when the pattern comes in as "Dots 2", the style paints with "Dots 2", and the tile's
|
||||
/// type is set in the style. Pasting again adds nothing. The commands do not make that loop, so the
|
||||
/// source is a document edited directly.
|
||||
#[test]
|
||||
fn a_pasted_style_and_the_pattern_whose_tile_uses_it_come_in_together() {
|
||||
let mut s = session();
|
||||
let t = id_of(&s.execute("text.create", &json!({"x": 10, "y": 50, "text": "Tile"})).unwrap());
|
||||
s.execute("charStyle.new", &json!({"name": "Accent", "attrs": {"kerning": 40}})).unwrap();
|
||||
s.execute("charStyle.apply", &json!({"name": "Accent", "id": t.0})).unwrap();
|
||||
s.execute("object.pattern.make", &json!({"name": "Dots", "edit": false, "ids": [t.0]})).unwrap();
|
||||
let other = id_of(&s.execute("text.create", &json!({"x": 10, "y": 90, "text": "B"})).unwrap());
|
||||
s.execute("charStyle.apply", &json!({"name": "Accent", "id": other.0})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"swatch": "Dots", "ids": [other.0]})).unwrap();
|
||||
s.execute("charStyle.new", &json!({"name": "Painted", "id": other.0})).unwrap();
|
||||
let d = doc_mut(&mut s);
|
||||
let painted = d.char_styles.iter().find(|x| x.name == "Painted").unwrap().attrs.clone();
|
||||
d.char_styles.retain(|x| x.name != "Painted");
|
||||
d.char_styles.iter_mut().find(|x| x.name == "Accent").unwrap().attrs = painted;
|
||||
assert_eq!(style_pattern(&s, "Accent").as_deref(), Some("Dots"));
|
||||
copy(&mut s, &[other]);
|
||||
new_doc(&mut s);
|
||||
dots(&mut s, 30.0, "#0000ff");
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert!(doc(&s).pattern("Dots 2").is_some(), "{v}");
|
||||
assert_eq!(style_pattern(&s, "Accent").as_deref(), Some("Dots 2"), "{v}");
|
||||
let NodeKind::Text(tile) = &doc(&s).pattern("Dots 2").unwrap().art[0].kind else { panic!("the tile is type") };
|
||||
assert_eq!(tile.runs[0].style.style_name.as_deref(), Some("Accent"));
|
||||
let after = doc(&s).clone();
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(v["added"], 0, "{v}");
|
||||
assert!(doc(&s).patterns == after.patterns && doc(&s).char_styles == after.char_styles, "nothing is added");
|
||||
// Back in the source document, the loop is the document's own.
|
||||
s.execute("document.activate", &json!({"index": 0})).unwrap();
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!((v["added"].as_u64(), v["renamed"].as_array().map(Vec::len)), (Some(0), Some(0)), "{v}");
|
||||
}
|
||||
|
||||
/// A symbol whose art paints with a pattern is not matched to the document's symbol of that name
|
||||
/// and art when the document's pattern of that name is another one: it comes in as "Mark 2",
|
||||
/// painting with "Dots 2".
|
||||
#[test]
|
||||
fn pasted_symbols_are_not_matched_to_symbols_that_paint_with_another_pattern() {
|
||||
let mut s = session();
|
||||
dots(&mut s, 10.0, "#ff0000");
|
||||
let r = rect(&mut s, 20.0, 20.0, 50.0, 50.0);
|
||||
s.execute("paint.setFill", &json!({"ids": [r.0], "swatch": "Dots"})).unwrap();
|
||||
sel(&mut s, &[r]);
|
||||
let mark = id_of(&s.execute("symbol.new", &json!({"name": "Mark"})).unwrap());
|
||||
copy(&mut s, &[mark]);
|
||||
// The document gets "Mark" and "Dots" from a first paste; then its "Dots" gets another tile.
|
||||
new_doc(&mut s);
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(v["renamed"], json!([]), "{v}");
|
||||
let tile = &mut doc_mut(&mut s).patterns.iter_mut().find(|p| p.name == "Dots").unwrap().tile;
|
||||
*tile = Rect::new(tile.x0, tile.y0, tile.x1 + 30.0, tile.y1 + 30.0);
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert!(renamed(&v).contains(&("symbol".to_string(), "Mark".to_string(), "Mark 2".to_string())), "{v}");
|
||||
let art = |s: &Session, name: &str| fill_of(&doc(s).symbols.iter().find(|x| x.name == name).unwrap().art);
|
||||
assert_eq!((art(&s, "Mark").as_deref(), art(&s, "Mark 2").as_deref()), (Some("Dots"), Some("Dots 2")));
|
||||
assert!(matches!(node(&s, ids_of(&v)[0]).kind, NodeKind::SymbolInstance { ref symbol, .. } if symbol == "Mark 2"));
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(v["added"], 0, "{v}");
|
||||
}
|
||||
|
||||
/// The pattern the first filled object in `n` paints with.
|
||||
fn fill_of(n: &Node) -> Option<String> {
|
||||
let mut found = None;
|
||||
n.walk(&mut |m| {
|
||||
if found.is_none() {
|
||||
found = pattern_of(&m.appearance.fill_paint()).map(str::to_string);
|
||||
}
|
||||
});
|
||||
found
|
||||
}
|
||||
|
||||
/// A brush whose art sets type in a character style is not matched to the document's brush of
|
||||
/// that name and art when the document's style of that name is another one: the style comes in as
|
||||
/// "Accent 2", and the brush as a copy whose type is set in "Accent 2".
|
||||
#[test]
|
||||
fn pasted_brushes_are_not_matched_to_brushes_that_use_another_style() {
|
||||
let mut s = session();
|
||||
let t = id_of(&s.execute("text.create", &json!({"x": 10, "y": 50, "text": "Ink"})).unwrap());
|
||||
s.execute("charStyle.new", &json!({"name": "Accent", "attrs": {"kerning": 40}})).unwrap();
|
||||
s.execute("charStyle.apply", &json!({"name": "Accent", "id": t.0})).unwrap();
|
||||
let name = s.execute("brush.new", &json!({"type": "art", "name": "Lettering", "ids": [t.0]})).unwrap()["name"].as_str().unwrap().to_string();
|
||||
let path = rect(&mut s, 200.0, 200.0, 40.0, 10.0);
|
||||
s.execute("brush.apply", &json!({"name": name, "ids": [path.0]})).unwrap();
|
||||
let brush = vectorcraft_brush::find(doc(&s), &name).unwrap();
|
||||
copy(&mut s, &[path]);
|
||||
new_doc(&mut s);
|
||||
s.execute("charStyle.new", &json!({"name": "Accent", "attrs": {"kerning": 10}})).unwrap();
|
||||
let d = doc_mut(&mut s);
|
||||
let mut lib = vectorcraft_brush::library(d);
|
||||
lib.push(brush);
|
||||
vectorcraft_brush::store(d, &lib);
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
let names = renamed(&v);
|
||||
let copied = names.iter().find(|(k, from, _)| k == "brush" && *from == name).map(|(_, _, to)| to.clone());
|
||||
assert!(names.contains(&("charStyle".to_string(), "Accent".to_string(), "Accent 2".to_string())), "{v}");
|
||||
let copied = copied.unwrap_or_else(|| panic!("the brush is added under a free name: {v}"));
|
||||
let style_of = |s: &Session, b: &str| {
|
||||
let b = vectorcraft_brush::find(doc(s), b).unwrap();
|
||||
let mut found = None;
|
||||
for a in b.art() {
|
||||
a.walk(&mut |m| {
|
||||
if let NodeKind::Text(tx) = &m.kind {
|
||||
found = found.take().or_else(|| tx.runs[0].style.style_name.clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
found
|
||||
};
|
||||
assert_eq!((style_of(&s, &name).as_deref(), style_of(&s, &copied).as_deref()), (Some("Accent"), Some("Accent 2")));
|
||||
let v = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(v["added"], 0, "{v}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brushes_travel_with_their_strokes() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -77,10 +77,12 @@ fn saturate_needs_a_number_intensity() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, json!("#cc6666"));
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
for p in [json!({}), json!({"intensity": "40"}), json!({"intensity": null})] {
|
||||
for p in [json!({}), json!({"intensity": null})] {
|
||||
let e = s.execute("edit.colors.saturate", &p).unwrap_err().to_string();
|
||||
assert!(e.contains("missing number `intensity`"), "{p}: {e}");
|
||||
}
|
||||
let e = s.execute("edit.colors.saturate", &json!({"intensity": "40"})).unwrap_err().to_string();
|
||||
assert!(e.contains("`intensity` must be a number, not \"40\""), "{e}");
|
||||
assert_eq!(fill_of(&s, a).to_hex(), "#cc6666");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");
|
||||
s.execute("edit.colors.saturate", &json!({"intensity": 100})).unwrap();
|
||||
|
||||
@@ -484,6 +484,57 @@ fn a_malformed_object_id_is_not_reported_as_missing() {
|
||||
assert_eq!(undo(&s), before);
|
||||
}
|
||||
|
||||
/// A required number given as something other than a number (`"40"`, `true`, `[]`, `{}`) is refused
|
||||
/// as the wrong type, naming its key and showing the value as given, not as a missing number; one left
|
||||
/// out or given as null is still a missing number. Neither changes the document or records an undo step.
|
||||
#[test]
|
||||
fn a_malformed_required_number_is_not_reported_as_missing() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let with = |base: &serde_json::Value, key: &str, v: Option<serde_json::Value>| {
|
||||
let mut p = base.clone();
|
||||
let o = p.as_object_mut().unwrap();
|
||||
match v {
|
||||
Some(v) => o.insert(key.into(), v),
|
||||
None => o.remove(key),
|
||||
};
|
||||
p
|
||||
};
|
||||
let cases = [
|
||||
("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10}), "x"),
|
||||
("edit.colors.saturate", json!({"intensity": 40}), "intensity"),
|
||||
("object.envelope.setMeshPoint", json!({"id": 1, "index": 0, "x": 0, "y": 0}), "index"),
|
||||
("object.envelope.setMeshPoint", json!({"id": 1, "index": 0, "x": 0, "y": 0}), "x"),
|
||||
("object.mesh.movePoint", json!({"id": 1, "index": 0, "x": 0, "y": 0}), "y"),
|
||||
("guide.add", json!({"pos": 40}), "pos"),
|
||||
];
|
||||
for (cmd, base, key) in &cases {
|
||||
for (v, shown) in [(json!("40"), "\"40\""), (json!(true), "true"), (json!([]), "[]"), (json!({}), "{}")] {
|
||||
let p = with(base, key, Some(v));
|
||||
match s.execute(cmd, &p) {
|
||||
Err(EngineError::BadParams { cmd: c, msg }) => {
|
||||
assert_eq!((c.as_str(), msg), (*cmd, format!("`{key}` must be a number, not {shown}")), "{p}");
|
||||
}
|
||||
other => panic!("{cmd} {p}: {other:?}"),
|
||||
}
|
||||
}
|
||||
for p in [with(base, key, None), with(base, key, Some(json!(null)))] {
|
||||
match s.execute(cmd, &p) {
|
||||
Err(EngineError::BadParams { cmd: c, msg }) => assert_eq!((c.as_str(), msg), (*cmd, format!("missing number `{key}`")), "{p}"),
|
||||
other => panic!("{cmd} {p}: {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().doc, doc);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
// A whole JSON number is a number for a fractional parameter.
|
||||
s.execute("guide.add", &json!({"pos": 40})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
}
|
||||
|
||||
/// A command that takes the keys of one that isn't checked yet isn't checked either.
|
||||
#[test]
|
||||
fn a_reference_to_an_unchecked_command_is_unchecked() {
|
||||
|
||||
@@ -329,6 +329,29 @@ fn expanding_a_fill_gives_its_plug_in_effect_the_fill_it_sits_on() {
|
||||
assert_eq!(clip.path_data().unwrap().bounds(), triangle().bounds(), "the triangle the canvas fills");
|
||||
}
|
||||
|
||||
/// A plug-in effect on a fill, before the fill's own 3D Revolve, is given the fill it sits on
|
||||
/// (`_context.item`), so the fill revolves the shape the plug-in returns for it.
|
||||
#[test]
|
||||
fn a_fills_revolve_gives_the_plug_in_effect_before_it_the_fill_it_sits_on() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 10.0, "#ff0000");
|
||||
let tri = json!({"objects": [{"path": triangle()}]}).to_string();
|
||||
let on_fill = tpl::when_given(&tpl::manifest("test.engine.revolvefill", "effect", "{}"), tpl::Given::Params, r#""item":{"fill":0}"#, &tri);
|
||||
install_wat(&mut s, &on_fill);
|
||||
let fill = node(&s, id).appearance.items.iter().position(|i| i.is_fill()).unwrap();
|
||||
run(&mut s, "effect.apply", json!({"ids": [id.0], "effect": "plugin.test.engine.revolvefill", "item": fill}));
|
||||
run(&mut s, "effect.apply", json!({"ids": [id.0], "effect": "threeD.revolve", "item": fill}));
|
||||
let n = node(&s, id);
|
||||
// The same object with the triangle as its path and only the Revolve on its fill.
|
||||
let mut expected = n.clone();
|
||||
if let vectorcraft_doc::NodeKind::Path { path, .. } = &mut expected.kind {
|
||||
*path = triangle();
|
||||
}
|
||||
expected.appearance.items[fill].effects_mut().retain(|e| e.id == "threeD.revolve");
|
||||
let art = vectorcraft_render::effects::revolve_art(&n).unwrap();
|
||||
assert_eq!(art, vectorcraft_render::effects::revolve_art(&expected).unwrap(), "the fill revolves the triangle");
|
||||
}
|
||||
|
||||
/// The manifest of a test live effect `id` that declares `paintOutput` (at `api` 2), or that
|
||||
/// doesn't (`paints` off).
|
||||
fn painting(id: &str, paints: bool) -> String {
|
||||
|
||||
@@ -1248,7 +1248,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.savePdfDialog",
|
||||
"Save PDF Dialog",
|
||||
"",
|
||||
"{path?, preset?, range?, …document.exportPdf options} open the Save PDF dialog with these options (fields = the options, sections are objects; OK writes path, else asks)",
|
||||
"{path?, preset?, range?, useArtboards?, …document.exportPdf options} open the Save PDF dialog with these options (fields = the options, sections are objects; OK writes path, else asks); useArtboards: false: one page of the visible art's bounds, with no Artboards section",
|
||||
),
|
||||
(
|
||||
"file.exportAs",
|
||||
|
||||
@@ -126,21 +126,49 @@ fn path_is_finite(path: &PathData) -> bool {
|
||||
/// Fallible [`offset_path`] that returns an error when the input is non-finite or exceeds
|
||||
/// the complexity bound ([`MAX_OFFSET_SEGMENT_PAIRS`]), or when planar arrangement fails.
|
||||
pub fn try_offset_path(path: &PathData, delta: f64, join: Join, miter_limit: f64) -> Result<PathData, PathOpsError> {
|
||||
let src = std::cell::LazyCell::new(|| SegmentTree::new(boxed_segments(&path.to_bezpath())));
|
||||
offset_with_check(path, delta, join, miter_limit, |p, r| any_within(&src, p, r))
|
||||
offset_with_check(path, delta, join, miter_limit, |part| {
|
||||
let bp = part.to_bezpath();
|
||||
let src = std::cell::LazyCell::new(move || SegmentTree::new(boxed_segments(&bp)));
|
||||
move |p, r| any_within(&src, p, r)
|
||||
})
|
||||
}
|
||||
|
||||
/// [`try_offset_path`] with the test of whether some segment of `path` lies closer than a radius to a point.
|
||||
fn offset_with_check(
|
||||
/// [`try_offset_path`] with `within_of(part)`, the test of whether some segment of `part` lies
|
||||
/// closer than a radius to a point.
|
||||
fn offset_with_check<W: Fn(kurbo::Point, f64) -> bool>(
|
||||
path: &PathData,
|
||||
delta: f64,
|
||||
join: Join,
|
||||
miter_limit: f64,
|
||||
within_of: impl Fn(&PathData) -> W,
|
||||
) -> Result<PathData, PathOpsError> {
|
||||
if !delta.is_finite() || !path_is_finite(path) {
|
||||
return Err(PathOpsError::NonFinite);
|
||||
}
|
||||
// An inset keeps only faces inside the closed fill, so it would drop the outline of every
|
||||
// open subpath (#6427). Inset the closed subpaths, outline the open ones as a positive offset
|
||||
// does, and unite the two.
|
||||
if delta < 0.0 && path.subpaths.iter().any(|s| s.anchors.len() > 1 && !close_if_ends_meet(s).closed) {
|
||||
let (closed, open): (Vec<SubPath>, Vec<SubPath>) = path.subpaths.iter().cloned().partition(|s| close_if_ends_meet(s).closed);
|
||||
let (closed, open) = (PathData::new(closed), PathData::new(open));
|
||||
let inset = offset_region(&closed, delta, join, miter_limit, within_of(&closed))?;
|
||||
let outline = offset_region(&open, -delta, join, miter_limit, within_of(&open))?;
|
||||
if inset.is_empty() {
|
||||
return Ok(outline);
|
||||
}
|
||||
return crate::boolean::try_unite_all(&[(&inset, FillRule::NonZero), (&outline, FillRule::NonZero)]);
|
||||
}
|
||||
offset_region(path, delta, join, miter_limit, within_of(path))
|
||||
}
|
||||
|
||||
/// [`offset_with_check`] of a finite `path` with the `within` test of its own segments.
|
||||
fn offset_region(
|
||||
path: &PathData,
|
||||
delta: f64,
|
||||
join: Join,
|
||||
miter_limit: f64,
|
||||
within: impl Fn(kurbo::Point, f64) -> bool,
|
||||
) -> Result<PathData, PathOpsError> {
|
||||
if !delta.is_finite() || !path_is_finite(path) {
|
||||
return Err(PathOpsError::NonFinite);
|
||||
}
|
||||
if path.is_empty() {
|
||||
return Ok(PathData::default());
|
||||
}
|
||||
@@ -630,14 +658,17 @@ mod tests {
|
||||
fn offsets_with_the_tree_are_identical_to_offsets_with_the_scan() {
|
||||
let (mut checks, mut hits, mut misses) = (0, 0, 0);
|
||||
for path in offset_inputs() {
|
||||
let segs = boxed_segments(&path.to_bezpath());
|
||||
for delta in [0.0, 0.05, 1.0, 5.0, 60.0, -0.05, -1.0, -5.0, -60.0] {
|
||||
for join in [Join::Round, Join::Miter, Join::Bevel] {
|
||||
let found = std::cell::Cell::new((0, 0));
|
||||
let by_scan = offset_with_check(&path, delta, join, 4.0, |p, r| {
|
||||
let v = any_within_by_scan(&segs, p, r);
|
||||
found.set((found.get().0 + usize::from(v), found.get().1 + usize::from(!v)));
|
||||
v
|
||||
let by_scan = offset_with_check(&path, delta, join, 4.0, |part| {
|
||||
let segs = boxed_segments(&part.to_bezpath());
|
||||
let found = &found;
|
||||
move |p, r| {
|
||||
let v = any_within_by_scan(&segs, p, r);
|
||||
found.set((found.get().0 + usize::from(v), found.get().1 + usize::from(!v)));
|
||||
v
|
||||
}
|
||||
});
|
||||
checks += 1;
|
||||
hits += found.get().0;
|
||||
|
||||
@@ -316,6 +316,24 @@ fn offset_of_an_open_path_whose_ends_meet_is_the_closed_shape_offset() {
|
||||
assert_eq!(o.subpaths.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_negative_offset_outlines_an_open_path_as_a_positive_one_does() {
|
||||
// Issue #6427 (A4): an inset of an open path was empty. Open subpaths are outlined with a
|
||||
// stroke of width 2|delta| whichever way the offset goes.
|
||||
let line = PathData::single(SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(100.0, 0.0)], false));
|
||||
for delta in [2.0, -2.0] {
|
||||
let o = offset_path(&line, delta, Join::Miter, 4.0);
|
||||
assert!(close(ar(&o), 400.0, 1e-6), "delta {delta}: {}", ar(&o));
|
||||
assert_eq!(o.subpaths.len(), 1, "delta {delta}");
|
||||
}
|
||||
// With a closed shape beside it, the shape is inset and the line still outlined.
|
||||
let mut both = rect(0.0, 0.0, 20.0, 10.0);
|
||||
both.subpaths.push(SubPath::polyline(&[Point::new(100.0, 0.0), Point::new(150.0, 0.0)], false));
|
||||
let o = offset_path(&both, -2.0, Join::Miter, 4.0);
|
||||
assert!(close(ar(&o), 16.0 * 6.0 + 50.0 * 4.0, 1e-6), "{}", ar(&o));
|
||||
assert_eq!(o.subpaths.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outline_stroke_of_line() {
|
||||
let l = PathData::single(SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(30.0, 40.0)], false));
|
||||
|
||||
@@ -272,6 +272,12 @@ impl Writer<'_> {
|
||||
self.warn("type on a path with space before a character after a line break is outlined to keep its look");
|
||||
return self.text_outlines(n, t);
|
||||
}
|
||||
// A `<textPath>` places its characters one after the other: Insert Aki keeps its look as
|
||||
// outlines.
|
||||
if t.runs.iter().any(|r| r.style.aki_left != 0.0 || r.style.aki_right != 0.0) {
|
||||
self.warn("type on a path with Insert Aki is outlined to keep its look");
|
||||
return self.text_outlines(n, t);
|
||||
}
|
||||
// A `<textPath>` holds the characters of the text, not the bullets or numbers drawn
|
||||
// before them.
|
||||
if !lay.markers.is_empty() {
|
||||
@@ -703,11 +709,14 @@ const PLACE_TOLERANCE: f64 = 1e-6;
|
||||
/// caps are upper case. Unless `fewer`, a line that is justified or composed with Mojikumi also
|
||||
/// starts a new segment at each character the layout draws elsewhere than a reader continuing
|
||||
/// the segment would ([`reader_advances`]): a Justify All line's letter gaps, the gaps between
|
||||
/// the characters of a justified Japanese line, and the spacing Mojikumi changes.
|
||||
/// the characters of a justified Japanese line, and the spacing Mojikumi changes. Text with Insert
|
||||
/// Aki does so on every line, with `fewer` too.
|
||||
fn text_lines(t: &TextObject, lay: &vectorcraft_text::TextLayout, fewer: bool) -> Vec<Line> {
|
||||
let plain = t.plain_text();
|
||||
let mut lines = Vec::with_capacity(lay.lines.len());
|
||||
let (mut kerned, mut unkerned) = (None, None);
|
||||
// Insert Aki moves characters from where a reader draws them, with `fewer` too.
|
||||
let aki = t.runs.iter().any(|r| r.style.aki_left != 0.0 || r.style.aki_right != 0.0);
|
||||
for line in &lay.lines {
|
||||
let para = plain.as_bytes().get(..line.start).map_or(0, |b| b.iter().filter(|&&c| c == b'\n').count());
|
||||
let style = t.para_at(para);
|
||||
@@ -716,7 +725,7 @@ fn text_lines(t: &TextObject, lay: &vectorcraft_text::TextLayout, fewer: bool) -
|
||||
// Justification…'s Desired word and letter spacing and glyph scaling move characters from
|
||||
// where a reader draws them, on every line (Block's word gap among them).
|
||||
let spaced = !fewer && !t.layout_justification(style).desired_is_default();
|
||||
let reader = if split_words || spaced || (!fewer && !style.mojikumi.is_none()) {
|
||||
let reader = if aki || split_words || spaced || (!fewer && !style.mojikumi.is_none()) {
|
||||
Some(&*kerned.get_or_insert_with(|| reader_advances(t, lay, true)))
|
||||
} else {
|
||||
None
|
||||
@@ -871,9 +880,11 @@ fn reader_advances(t: &TextObject, lay: &vectorcraft_text::TextLayout, kerning:
|
||||
if !kerning {
|
||||
r.style.kerning.get_or_insert(0.0);
|
||||
}
|
||||
// A reader puts no space before a line's first character (`kerningBefore`): the segment
|
||||
// starts that much further along instead.
|
||||
// A reader puts no space before a line's first character (`kerningBefore`) and no Insert
|
||||
// Aki: the segment starts that much further along instead.
|
||||
r.style.kerning_before = 0.0;
|
||||
r.style.aki_left = 0.0;
|
||||
r.style.aki_right = 0.0;
|
||||
}
|
||||
let relaid;
|
||||
let glyphs = if set == *t {
|
||||
|
||||
@@ -163,6 +163,39 @@ fn the_space_before_the_first_character_on_a_path_is_its_dx() {
|
||||
assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("line break")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
/// Insert Aki: each character the layout moves by its Aki starts a `<tspan>` where the character is
|
||||
/// drawn, so a reader draws the text where the canvas does, on left-aligned, centered and
|
||||
/// right-aligned lines and with fewer `<tspan>`s asked for too. On a path the type is outlined.
|
||||
#[test]
|
||||
fn insert_aki_places_each_moved_character() {
|
||||
// 250/1000 em of 12 pt before the O (3 pt) and 500/1000 after it (6 pt), then a run with Aki
|
||||
// after every character, and a second line.
|
||||
let spaced = CharStyle { aki_left: 250.0, aki_right: 500.0, ..style(12.0) };
|
||||
let runs = vec![run("H", style(12.0)), run("O", spaced), run("H ", style(12.0)), run("OHO\nHO", CharStyle { aki_right: 500.0, ..style(12.0) })];
|
||||
// A centered or right-aligned line whose characters the Aki moves is placed segment by
|
||||
// segment, not anchored.
|
||||
for justify in [Justify::Center, Justify::Right] {
|
||||
assert_drawn_in_place(&area_doc(runs.clone(), 200.0, justify));
|
||||
}
|
||||
let d = area_doc(runs, 200.0, Justify::Left);
|
||||
assert_drawn_in_place(&d);
|
||||
let plain = tspans(&export(&area_doc(vec![run("HOH OHO\nHO", style(12.0))], 200.0, Justify::Left), &ExportOptions::default())).len();
|
||||
let fewer = tspans(&export(&d, &ExportOptions { fewer_tspans: true, ..Default::default() })).len();
|
||||
assert!(fewer > plain, "each moved character is placed with fewer tspans too: {plain} {fewer}");
|
||||
let mut t = TextObject::point(Point::ZERO, "", style(12.0));
|
||||
t.runs = vec![run("HO", CharStyle { aki_left: 250.0, ..style(12.0) })];
|
||||
let mut line = BezPath::new();
|
||||
line.move_to((20.0, 100.0));
|
||||
line.line_to((280.0, 100.0));
|
||||
t.kind = TextKind::OnPath { path: PathData::from_bezpath(&line), start: 0.0, end: None };
|
||||
let mut d = Document::new(300.0, 200.0);
|
||||
let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), 0, n).unwrap();
|
||||
let out = vectorcraft_svg::export_full(&d, &ExportOptions::default(), None);
|
||||
assert!(!out.svg.contains("<textPath") && out.warnings.iter().any(|w| w.contains("Insert Aki")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
/// Proportional Metrics (#966) is written as the `palt` feature, beside the character's own.
|
||||
#[test]
|
||||
fn proportional_metrics_write_palt() {
|
||||
|
||||
@@ -54,6 +54,8 @@ pub(crate) fn paint_only(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
tracking,
|
||||
kerning,
|
||||
kerning_before: _,
|
||||
aki_left: _,
|
||||
aki_right: _,
|
||||
optical_kerning,
|
||||
baseline_shift,
|
||||
h_scale,
|
||||
|
||||
@@ -148,25 +148,27 @@ pub(crate) fn apply_desired(sg: &mut [SGlyph], j: &JustificationOptions, scale_g
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// Insert Aki keeps its width: only the rest of the advance is spaced or scaled.
|
||||
let aki = if g.aki.is_finite() { g.aki } else { 0.0 };
|
||||
if is_word_space(g) {
|
||||
// The font's word space (with its tracking), set at Desired.
|
||||
let base = g.adv;
|
||||
g.adv = base * w_des;
|
||||
let base = g.adv - aki;
|
||||
g.adv = base * w_des + aki;
|
||||
f.word = (base * (w_max - w_des).max(0.0), base * (w_des - w_min).max(0.0));
|
||||
continue;
|
||||
}
|
||||
if g.is_space() || g.inline.is_some() || g.ch == '\u{2028}' {
|
||||
continue;
|
||||
}
|
||||
let base = g.adv;
|
||||
let base = g.adv - aki;
|
||||
if g_des != 1.0 {
|
||||
g.adv *= g_des;
|
||||
g.adv = base * g_des + aki;
|
||||
g.tracking *= g_des;
|
||||
g.sx *= g_des;
|
||||
g.dx *= g_des;
|
||||
g.kern_shift *= g_des;
|
||||
g.kern_shift = (g.kern_shift - g.aki_shift) * g_des + g.aki_shift;
|
||||
}
|
||||
f.width = g.adv;
|
||||
f.width = g.adv - aki;
|
||||
f.scale = g_des;
|
||||
f.glyph = (base * (g_max - g_des).max(0.0), base * (g_des - g_min).max(0.0));
|
||||
if gap {
|
||||
|
||||
@@ -241,7 +241,7 @@ impl Ctx<'_> {
|
||||
/// Layout on or Fractional Widths off, whole-point advances.
|
||||
fn finish_shaping<'s>(&self, v: &mut [SGlyph], style: impl Fn(&SGlyph) -> &'s CharStyle) {
|
||||
if self.vertical {
|
||||
tate_chu_yoko(v, |g| {
|
||||
tate_chu_yoko(v, self.system_layout || !self.fractional_widths, |g| {
|
||||
let st = style(g);
|
||||
TcyStyle { size: st.size, marked: st.tate_chu_yoko, up: st.tcy_up_down, right: st.tcy_left_right }
|
||||
});
|
||||
@@ -318,6 +318,8 @@ impl Ctx<'_> {
|
||||
tab.dx = 0.0;
|
||||
tab.dy = 0.0;
|
||||
tab.kern_shift = 0.0;
|
||||
tab.aki_shift = 0.0;
|
||||
tab.aki = 0.0;
|
||||
tab.tcy = None;
|
||||
tab.vpal = None;
|
||||
let left = para.laid_out_left_indent();
|
||||
@@ -335,6 +337,19 @@ impl Ctx<'_> {
|
||||
n
|
||||
}
|
||||
|
||||
/// Does glyph `g`'s own width start where its placement sets it ([`PositionedGlyph::width_start`])?
|
||||
/// Not for an inline graphic, nor in vertical type for an upright or tate-chu-yoko glyph, which
|
||||
/// start their cell at the start of their advance.
|
||||
fn places_own_width(&self, g: &SGlyph) -> bool {
|
||||
g.inline.is_none() && !(self.vertical && (g.tcy.is_some() || (takes_cell(g) && stands_upright(g))))
|
||||
}
|
||||
|
||||
/// How far glyph `g`'s own width moves when the glyph is drawn `shift` further along its
|
||||
/// advance: `shift` when its placement sets where that width starts, otherwise 0.
|
||||
fn own_width_shift(&self, g: &SGlyph, shift: f64) -> f64 {
|
||||
if self.places_own_width(g) { shift } else { 0.0 }
|
||||
}
|
||||
|
||||
fn emit(&mut self, g: &SGlyph, pre: Affine, origin: Point, angle: f64, advance: f64, line: usize) {
|
||||
// Only the glyph right after an upright glyph can continue its cluster.
|
||||
let cluster = self.upright_cluster.take();
|
||||
@@ -538,7 +553,7 @@ impl Ctx<'_> {
|
||||
let [a, b, ..] = pre.as_coeffs();
|
||||
a.hypot(b)
|
||||
};
|
||||
let width_start = if g.inline.is_none() && !upright && !(self.vertical && g.tcy.is_some()) {
|
||||
let width_start = if self.places_own_width(g) {
|
||||
Some((g.kern_shift + g.dx - g.lead) * along).filter(|s| s.is_finite()).unwrap_or(0.0)
|
||||
} else if upright {
|
||||
Some(g.kern_shift * along).filter(|s| s.is_finite()).unwrap_or(0.0)
|
||||
@@ -617,6 +632,8 @@ impl Ctx<'_> {
|
||||
k.dx = 0.0;
|
||||
k.dy = 0.0;
|
||||
k.kern_shift = 0.0;
|
||||
k.aki_shift = 0.0;
|
||||
k.aki = 0.0;
|
||||
k.len = 0;
|
||||
k.lead = 0.0;
|
||||
k.ch = kashida::TATWEEL;
|
||||
@@ -674,6 +691,8 @@ mod emit_kashida_mark_last {
|
||||
dx: 0.0,
|
||||
dy: 0.0,
|
||||
kern_shift: 0.0,
|
||||
aki_shift: 0.0,
|
||||
aki: 0.0,
|
||||
sx: face.upem.recip() * 48.0,
|
||||
sy: face.upem.recip() * 48.0,
|
||||
bshift: 0.0,
|
||||
@@ -737,6 +756,8 @@ mod emit_kashida_mark_last {
|
||||
dx: 0.0,
|
||||
dy: 0.0,
|
||||
kern_shift: 0.0,
|
||||
aki_shift: 0.0,
|
||||
aki: 0.0,
|
||||
sx: face.upem.recip() * 48.0,
|
||||
sy: face.upem.recip() * 48.0,
|
||||
bshift: 0.0,
|
||||
|
||||
@@ -1030,7 +1030,10 @@ fn emit_flow_line(cx: &mut Ctx<'_>, pen: &mut Pen<'_>, paragraph: &ParagraphData
|
||||
.map(|item| item.1);
|
||||
advance += kashida.unwrap_or(0.0);
|
||||
let y = line.position.baseline - align_shift(glyph, cx.style_at(glyph.byte).char_align, line_em, cx.vertical);
|
||||
let transform = if scale != 1.0 { Affine::translate((x, y)) * Affine::scale_non_uniform(scale, 1.0) } else { Affine::translate((x, y)) };
|
||||
// A glyph scaled to fill the line keeps its Insert Aki before it at its width.
|
||||
let aki = if scale != 1.0 && glyph.aki_shift.is_finite() { glyph.aki_shift * (1.0 - scale) } else { 0.0 };
|
||||
let transform =
|
||||
if scale != 1.0 { Affine::translate((x + aki, y)) * Affine::scale_non_uniform(scale, 1.0) } else { Affine::translate((x, y)) };
|
||||
if glyph_index < paragraph.marker {
|
||||
let at = cx.out.glyphs.len();
|
||||
marker_glyphs = Some(marker_glyphs.map_or(at..at + 1, |range: Range<usize>| range.start.min(at)..range.end.max(at + 1)));
|
||||
@@ -1039,8 +1042,11 @@ fn emit_flow_line(cx: &mut Ctx<'_>, pen: &mut Pen<'_>, paragraph: &ParagraphData
|
||||
}
|
||||
}
|
||||
cx.emit(glyph, transform, Point::new(x, y), 0.0, advance, line_index);
|
||||
// Its own width starts where the glyph is drawn, with the Aki kept at its width.
|
||||
let own_shift = cx.own_width_shift(glyph, aki);
|
||||
if let Some(out) = cx.out.glyphs.last_mut() {
|
||||
out.scale = paragraph.flex.get(glyph_index).map_or(1.0, |flex| flex.scale) * scale;
|
||||
out.width_start += own_shift;
|
||||
}
|
||||
if !leader.is_empty() {
|
||||
// The leader belongs to the tab's glyph: the gap stays one character for the
|
||||
|
||||
@@ -7,18 +7,23 @@ use super::{Affine, Ctx, Punct, Range, Rect, SGlyph, Tcy, TextLayout, TextObject
|
||||
/// tate-chu-yoko continuation, a zero-width mark) stays zero. Right to left, a cluster with manual
|
||||
/// kerning moves by the difference the kerning makes to its rounded advance, so the rounding
|
||||
/// changes the space before the next character in reading order and not the space after the one
|
||||
/// before it.
|
||||
/// before it. Each side of the Insert Aki is rounded to whole points on its own and added to the
|
||||
/// rounded advance, so justification keeps it at that width.
|
||||
pub(super) fn whole_character_widths(glyphs: &mut [SGlyph]) {
|
||||
let whole = |adv: f64| if adv.is_finite() && adv > 0.0 { adv.round().max(1.0) } else { adv };
|
||||
let points = |aki: f64| if aki.is_finite() { aki.round() } else { 0.0 };
|
||||
for cluster in glyphs.chunk_by_mut(|a, b| continues_cluster(Some(a), Some(b))) {
|
||||
// The cluster's last glyph carries the manual kerning in its advance.
|
||||
let (adv, shift) = cluster.last().map_or((0.0, 0.0), |g| (g.adv, g.kern_shift));
|
||||
// The cluster's last glyph carries the manual kerning and the Insert Aki in its advance.
|
||||
let (adv, shift) = cluster.last().map_or((0.0, 0.0), |g| (g.adv - g.aki, g.kern_shift - g.aki_shift));
|
||||
let rounded_shift = Some(whole(adv) - whole(adv - shift)).filter(|s| s.is_finite()).unwrap_or(shift);
|
||||
for g in cluster {
|
||||
g.adv = whole(g.adv);
|
||||
if shift != 0.0 {
|
||||
g.kern_shift = rounded_shift;
|
||||
}
|
||||
// The space before the character and the space after it are each rounded.
|
||||
let aki_shift = points(g.aki_shift);
|
||||
let aki = if g.aki == 0.0 { 0.0 } else { aki_shift + points(g.aki - g.aki_shift) };
|
||||
g.adv = whole(g.adv - g.aki) + aki;
|
||||
g.kern_shift = if shift != 0.0 { rounded_shift } else { g.kern_shift - g.aki_shift } + aki_shift;
|
||||
g.aki = aki;
|
||||
g.aki_shift = aki_shift;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +38,10 @@ pub(super) fn whole_character_widths(glyphs: &mut [SGlyph]) {
|
||||
/// before the automatic digit rule. A marked block moves by the Tate-chu-yoko Settings Up/Down
|
||||
/// and Left/Right of its first character; an automatic block does not move.
|
||||
/// <https://helpx.adobe.com/illustrator/using/formatting-asian-characters.html>
|
||||
pub(super) fn tate_chu_yoko(g: &mut [SGlyph], style: impl Fn(&SGlyph) -> TcyStyle) {
|
||||
///
|
||||
/// With `whole` (System Layout on or Fractional Widths off), each side of a block's Insert Aki is
|
||||
/// set in whole points ([`whole_character_widths`]).
|
||||
pub(super) fn tate_chu_yoko(g: &mut [SGlyph], whole: bool, style: impl Fn(&SGlyph) -> TcyStyle) {
|
||||
let manual: Vec<bool> = g.iter().map(|gl| gl.ch != '\u{2028}' && style(gl).marked).collect();
|
||||
let digits: Vec<bool> = g.iter().map(|gl| gl.inline.is_none() && gl.ch.is_ascii_digit()).collect();
|
||||
let mut i = 0;
|
||||
@@ -44,7 +52,7 @@ pub(super) fn tate_chu_yoko(g: &mut [SGlyph], style: impl Fn(&SGlyph) -> TcyStyl
|
||||
end += 1;
|
||||
}
|
||||
let first = style(&g[i]);
|
||||
place_tcy_block(&mut g[i..end], first.size, (first.up, first.right));
|
||||
place_tcy_block(&mut g[i..end], first.size, (first.up, first.right), whole);
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
@@ -58,7 +66,7 @@ pub(super) fn tate_chu_yoko(g: &mut [SGlyph], style: impl Fn(&SGlyph) -> TcyStyl
|
||||
}
|
||||
if (2..=3).contains(&(end - i)) {
|
||||
let em = style(&g[i]).size;
|
||||
place_tcy_block(&mut g[i..end], em, (0.0, 0.0));
|
||||
place_tcy_block(&mut g[i..end], em, (0.0, 0.0), whole);
|
||||
}
|
||||
i = end;
|
||||
}
|
||||
@@ -74,15 +82,35 @@ pub(super) struct TcyStyle {
|
||||
}
|
||||
|
||||
/// Lay `g` across one em of the column, moved by `(up, right)` points. The first glyph takes the
|
||||
/// em; the rest share its cell.
|
||||
fn place_tcy_block(g: &mut [SGlyph], em: f64, (up, right): (f64, f64)) {
|
||||
let width: f64 = g.iter().map(|gl| gl.adv).sum();
|
||||
/// em; the rest share its cell. The Insert Aki of the block's first character is space above and
|
||||
/// below the block, and the Insert Aki of its other characters is left out: the block is set
|
||||
/// down the column that much further, and its last glyph carries the space in its advance. With
|
||||
/// `whole`, the space above and the space below are each rounded to whole points.
|
||||
fn place_tcy_block(g: &mut [SGlyph], em: f64, (up, right): (f64, f64), whole: bool) {
|
||||
let Some(first) = g.first() else { return };
|
||||
let (byte, before) = (first.byte, first.aki_shift);
|
||||
let aki: f64 = g.iter().filter(|gl| gl.byte == byte).map(|gl| gl.aki).sum();
|
||||
let points = |v: f64| {
|
||||
if !whole {
|
||||
v
|
||||
} else if v.is_finite() {
|
||||
v.round()
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
};
|
||||
let (before, aki) = (points(before), points(before) + points(aki - before));
|
||||
let width: f64 = g.iter().map(|gl| gl.adv - gl.aki).sum();
|
||||
let squeeze = if width > em && width > 0.0 { em / width } else { 1.0 };
|
||||
let last = g.len() - 1;
|
||||
let mut ink = 0.0;
|
||||
for (k, gl) in g.iter_mut().enumerate() {
|
||||
let adv = gl.adv;
|
||||
gl.tcy = Some(Tcy { pen: if k == 0 { 0.0 } else { em }, ink, width, squeeze, up, right });
|
||||
gl.adv = if k == 0 { em } else { 0.0 };
|
||||
let adv = gl.adv - gl.aki;
|
||||
gl.tcy = Some(Tcy { pen: if k == 0 { 0.0 } else { em }, ink, width, squeeze, up: up - before, right });
|
||||
gl.kern_shift -= gl.aki_shift;
|
||||
gl.aki_shift = 0.0;
|
||||
gl.aki = if k == last { aki } else { 0.0 };
|
||||
gl.adv = if k == 0 { em } else { 0.0 } + gl.aki;
|
||||
ink += adv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ fn synthetic_glyphs(specs: &[(usize, usize, f64, char)]) -> Vec<SGlyph> {
|
||||
dx: 0.0,
|
||||
dy: 0.0,
|
||||
kern_shift: 0.0,
|
||||
aki_shift: 0.0,
|
||||
aki: 0.0,
|
||||
sx: 1.0,
|
||||
sy: 1.0,
|
||||
bshift: 0.0,
|
||||
|
||||
@@ -54,7 +54,7 @@ pub use svg_glyph::{SvgGlyphReader, set_svg_glyph_reader};
|
||||
pub use kept::{KeptLayoutsFrame, LayoutStamp, keep_layout, take_kept_layout};
|
||||
use kurbo::{Affine, BezPath, Point, Rect, Shape, Vec2};
|
||||
pub use layout::{decorations, drawn, fit_first_line, fit_source_lines, layout, layout_with, scaled};
|
||||
pub use shape::{inseparable, line_start_space, spacing_scale};
|
||||
pub use shape::{aki_space, inseparable, line_start_space, spacing_scale};
|
||||
pub use vectorcraft_doc::TextObject;
|
||||
|
||||
pub use vectorcraft_doc::{AreaFit, FirstBaseline, TextFlow, VerticalAlign};
|
||||
|
||||
+66
-20
@@ -13,7 +13,7 @@ use kurbo::Shape;
|
||||
use skrifa::MetadataProvider;
|
||||
use skrifa::charmap::Charmap;
|
||||
use skrifa::instance::Size;
|
||||
use vectorcraft_doc::text::INLINE_CHAR;
|
||||
use vectorcraft_doc::text::{INLINE_CHAR, MAX_AKI};
|
||||
use vectorcraft_doc::{CharStyle, Digits, InlineArt};
|
||||
|
||||
use crate::features::{OtFeatures, ligatures_suppressed_by};
|
||||
@@ -41,8 +41,14 @@ pub(crate) struct SGlyph {
|
||||
/// outside its Character Rotation; with whole-point advances, the difference the kerning makes
|
||||
/// to the cluster's rounded advance. Left to right and in vertical type: the space before a
|
||||
/// line's first character ([`CharStyle::kerning_before`]), which moves it along the line the
|
||||
/// same way. 0 otherwise.
|
||||
/// same way. Insert Aki ([`aki`]) adds the space before the cluster in reading order: left to
|
||||
/// right and in vertical type the Aki Left, right to left the Aki Right. 0 otherwise.
|
||||
pub kern_shift: f64,
|
||||
/// The Insert Aki `kern_shift` holds, in points, on every glyph of its cluster.
|
||||
pub aki_shift: f64,
|
||||
/// The Insert Aki Left and Right `adv` holds, in points: on the last glyph of its cluster, 0
|
||||
/// on the others. Justification does not stretch or narrow it.
|
||||
pub aki: f64,
|
||||
/// Outline scale (font units -> points).
|
||||
pub sx: f64,
|
||||
pub sy: f64,
|
||||
@@ -193,6 +199,8 @@ pub(crate) fn hyphen_glyph(g: &SGlyph) -> SGlyph {
|
||||
h.dx = 0.0;
|
||||
h.dy = 0.0;
|
||||
h.kern_shift = 0.0;
|
||||
h.aki_shift = 0.0;
|
||||
h.aki = 0.0;
|
||||
h.byte = g.byte + g.len;
|
||||
h.len = 0;
|
||||
h.ch = '-';
|
||||
@@ -606,6 +614,22 @@ pub fn line_start_space(text: &str, byte: usize, st: &CharStyle, vertical: bool)
|
||||
space_before(text, byte, st, glyph_size(st, small) * spacing_scale(st, vertical))
|
||||
}
|
||||
|
||||
/// Insert Aki Left and Right ([`CharStyle::aki_left`], [`CharStyle::aki_right`]) of characters in
|
||||
/// style `st`, in points of `spacing_em`: the space before and after each cluster in reading order,
|
||||
/// each from 0 to [`MAX_AKI`] thousandths of it. Its cluster takes both in its advance.
|
||||
fn aki(st: &CharStyle, spacing_em: f64) -> (f64, f64) {
|
||||
let side = |v: f64| Some(v.clamp(0.0, MAX_AKI) / 1000.0 * spacing_em).filter(|s| s.is_finite()).unwrap_or(0.0);
|
||||
(side(st.aki_left), side(st.aki_right))
|
||||
}
|
||||
|
||||
/// The Insert Aki before and after the character at `byte` of `text` in style `st`, in points, as
|
||||
/// the layout sets it ([`CharStyle::aki_left`]): its thousandths of the em the character is set in.
|
||||
/// For writers that place characters themselves.
|
||||
pub fn aki_space(text: &str, byte: usize, st: &CharStyle, vertical: bool) -> (f64, f64) {
|
||||
let small = st.small_caps.is_some() && !st.all_caps && text.get(byte..).and_then(|s| s.chars().next()).is_some_and(char::is_lowercase);
|
||||
aki(st, glyph_size(st, small) * spacing_scale(st, vertical))
|
||||
}
|
||||
|
||||
/// The size characters in style `st` are shaped at: Superscript, Subscript and synthesized Small
|
||||
/// Caps (`small`: a lowercase letter in them) shrink the glyphs; line metrics keep the full size.
|
||||
fn glyph_size(st: &CharStyle, small: bool) -> f64 {
|
||||
@@ -783,7 +807,8 @@ fn optical_pairs(db: &FontDb, text: &str, glyphs: &mut [SGlyph], runs: &[(Range<
|
||||
let mut cache = HashMap::new();
|
||||
let mut corrections: HashMap<[u64; 15], Option<f64>> = HashMap::new();
|
||||
let mut references: HashMap<u32, Option<f64>> = HashMap::new();
|
||||
let shaped_advances: Vec<f64> = glyphs.iter().map(|g| g.adv).collect();
|
||||
// The Insert Aki a cluster's last glyph holds does not make a mark its base.
|
||||
let shaped_advances: Vec<f64> = glyphs.iter().map(|g| g.adv - g.aki).collect();
|
||||
for i in 0..glyphs.len().saturating_sub(1) {
|
||||
let Some((a, b)) = glyphs.get(i).zip(glyphs.get(i + 1)) else { continue };
|
||||
if a.level != b.level {
|
||||
@@ -841,8 +866,10 @@ fn optical_pairs(db: &FontDb, text: &str, glyphs: &mut [SGlyph], runs: &[(Range<
|
||||
continue;
|
||||
}
|
||||
// Neither glyph is rotated, so a right-to-left manual kerning shift adds to the offset.
|
||||
let left_x = left.dx + left.kern_shift;
|
||||
let right_x = right.dx + right.kern_shift;
|
||||
// Insert Aki is measured as no space: the pair is spaced as it would be without it, and
|
||||
// the Aki stays on top of that.
|
||||
let left_x = left.dx + left.kern_shift - left.aki_shift;
|
||||
let right_x = right.dx + right.kern_shift - right.aki_shift;
|
||||
let left_y = left.dy - left.bshift;
|
||||
let right_y = right.dy - right.bshift;
|
||||
let Some(left_style) = runs.get(left.run).map(|(_, s)| *s) else { continue };
|
||||
@@ -851,7 +878,7 @@ fn optical_pairs(db: &FontDb, text: &str, glyphs: &mut [SGlyph], runs: &[(Range<
|
||||
let tracking = left_style.tracking / 1000.0 * em * spacing_scale(left_style, false);
|
||||
// A manual kerning value on the left glyph is part of its advance and, right to left, of
|
||||
// its offset too (it spaces the left glyph from its left neighbor): it is not taken off.
|
||||
let advance = glyphs[cluster_start..=cluster_end].iter().map(|g| g.adv).sum::<f64>() - tracking;
|
||||
let advance = glyphs[cluster_start..=cluster_end].iter().map(|g| g.adv - g.aki).sum::<f64>() - tracking;
|
||||
let key = [
|
||||
u64::from(left.face.id()),
|
||||
u64::from(left.gid),
|
||||
@@ -911,8 +938,9 @@ fn source_run_at(runs: &[(Range<usize>, &CharStyle)], span: &Range<usize>, byte:
|
||||
/// Can one glyph draw characters in styles `a` and `b`? OpenType features already apply to their
|
||||
/// own character ranges during shaping, so feature-only differences do not split a cluster.
|
||||
/// Horizontal type does not read Standard Vertical Roman Alignment or the tate-chu-yoko offsets.
|
||||
/// The space before a line's first character ([`space_before`]) comes from the cluster's first
|
||||
/// character, so it does not split a ligature either.
|
||||
/// The space before a line's first character ([`space_before`]) and Insert Aki ([`aki`]) come
|
||||
/// from the cluster's first character, so they do not split a ligature or a letter from its marks
|
||||
/// either.
|
||||
fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
// Exhaustive, like `shapes_alike`: compare borrowed fields without copying feature lists.
|
||||
let CharStyle {
|
||||
@@ -925,6 +953,8 @@ fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
tracking,
|
||||
kerning,
|
||||
kerning_before,
|
||||
aki_left,
|
||||
aki_right,
|
||||
optical_kerning,
|
||||
baseline_shift,
|
||||
h_scale,
|
||||
@@ -965,6 +995,8 @@ fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
let offsets = (standard_vertical_roman, tcy_up_down, tcy_left_right);
|
||||
let other_offsets = if vertical { (&b.standard_vertical_roman, &b.tcy_up_down, &b.tcy_left_right) } else { offsets };
|
||||
kerning_before == &a.kerning_before
|
||||
&& aki_left == &a.aki_left
|
||||
&& aki_right == &a.aki_right
|
||||
&& offsets == other_offsets
|
||||
&& font_family == &b.font_family
|
||||
&& font_style == &b.font_style
|
||||
@@ -1012,9 +1044,9 @@ fn drawn_alike(a: &CharStyle, b: &CharStyle, vertical: bool) -> bool {
|
||||
/// character style's name, leading, baseline shift, rotation, Character Alignment, Character
|
||||
/// Direction (text is shaped apart where its bidi level changes), Kashidas, the kerning (each glyph takes its
|
||||
/// run's, see [`shape_segment`]), the space before a line's first character ([`space_before`]),
|
||||
/// the OpenType features (each run's apply to its own characters, as features over a range of the
|
||||
/// one buffer) and the tracking, as long as the tracking keeps or drops standard ligatures alike
|
||||
/// ([`crate::features::ligatures_suppressed_by`]).
|
||||
/// Insert Aki ([`aki`]), the OpenType features (each run's apply to its own characters, as features
|
||||
/// over a range of the one buffer) and the tracking, as long as the tracking keeps or drops
|
||||
/// standard ligatures alike ([`crate::features::ligatures_suppressed_by`]).
|
||||
fn shapes_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
// Every field is named, so that a new attribute has to be sorted into one group or the other.
|
||||
let CharStyle {
|
||||
@@ -1027,6 +1059,8 @@ fn shapes_alike(a: &CharStyle, b: &CharStyle) -> bool {
|
||||
tracking,
|
||||
kerning: _,
|
||||
kerning_before: _,
|
||||
aki_left: _,
|
||||
aki_right: _,
|
||||
optical_kerning: _,
|
||||
baseline_shift: _,
|
||||
h_scale,
|
||||
@@ -1128,9 +1162,9 @@ fn shaping_script(c: char) -> Script {
|
||||
/// The art is scaled uniformly to `scale` x the font size tall, its left edge on the pen and its
|
||||
/// vertical centre on the middle of the cap height raised by the inline's shift (see
|
||||
/// [`InlineArt`]). Its advance is the scaled art width plus tracking, its character's manual
|
||||
/// kerning and the space before it at a line's start ([`space_before`]). Right to left in
|
||||
/// horizontal type the manual kerning also moves the art right by the value, as it
|
||||
/// moves a letter's glyph. The art doesn't take the run's Horizontal Scale; the tracking and the
|
||||
/// kerning, the space before it at a line's start ([`space_before`]) and its Insert Aki ([`aki`]).
|
||||
/// Right to left in horizontal type the manual kerning also moves the art right by the value, as it
|
||||
/// moves a letter's glyph, and Insert Aki moves it as it moves a letter's glyph. The art doesn't take the run's Horizontal Scale; the tracking and the
|
||||
/// kerning do, as after a letter ([`spacing_scale`]). The glyph's ascent and descent grow to the
|
||||
/// art's extent so a tall graphic opens up its line. A missing symbol takes a square of the same
|
||||
/// height and draws nothing.
|
||||
@@ -1164,7 +1198,9 @@ fn inline_glyph(
|
||||
let tracking = Some(st.tracking / 1000.0 * spacing_em).filter(|t| t.is_finite()).unwrap_or(0.0);
|
||||
let manual_kern = st.kerning.map(|v| v / 1000.0 * spacing_em).filter(|k| k.is_finite()).unwrap_or(0.0);
|
||||
let before = space_before(text, range.start, st, spacing_em);
|
||||
let adv = width + tracking + manual_kern + before;
|
||||
let (aki_left, aki_right) = aki(st, spacing_em);
|
||||
let adv = width + tracking + manual_kern + before + aki_left + aki_right;
|
||||
let aki_shift = if level.is_rtl() && !vertical { aki_right } else { aki_left };
|
||||
SGlyph {
|
||||
face: face.clone(),
|
||||
gid: 0,
|
||||
@@ -1175,7 +1211,9 @@ fn inline_glyph(
|
||||
tracking: if adv.is_finite() { tracking } else { 0.0 },
|
||||
dx: 0.0,
|
||||
dy: 0.0,
|
||||
kern_shift: if level.is_rtl() && !vertical { manual_kern } else { before },
|
||||
kern_shift: if level.is_rtl() && !vertical { manual_kern } else { before } + aki_shift,
|
||||
aki_shift,
|
||||
aki: if adv.is_finite() { aki_left + aki_right } else { 0.0 },
|
||||
// Not used to draw (the art has its own transform), but they make the glyph's em box
|
||||
// its run's em: Character Alignment and Top-to-Top leading treat an inline graphic as a
|
||||
// character of its run's size, whatever its scale (see `layout::glyph_em`).
|
||||
@@ -1624,23 +1662,29 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
};
|
||||
let mut kern_shift = 0.0;
|
||||
let mut tracked = 0.0;
|
||||
let (mut aki_shift, mut aki_in_adv) = (0.0, 0.0);
|
||||
if ch == SOFT_HYPHEN || ch == '\u{2028}' {
|
||||
adv = 0.0;
|
||||
} else {
|
||||
let manual_kern = gst.kerning.map(|v| v / 1000.0 * spacing_em).unwrap_or(0.0);
|
||||
let before = space_before(text, cl, gst, spacing_em);
|
||||
let (aki_left, aki_right) = aki(gst, spacing_em);
|
||||
// Manual kerning spaces a character from the next one in reading order. Right to left,
|
||||
// that one is drawn to its left: the cluster's glyphs move right along the baseline by
|
||||
// the value, so that the space its last glyph's advance gains is at the cluster's left.
|
||||
// The space before a line's first character moves them the other way.
|
||||
// The space before a line's first character moves them the other way. Insert Aki moves
|
||||
// them past the space before them in reading order: Aki Left, or right to left Aki Right.
|
||||
if level.is_rtl() && !feats.vertical {
|
||||
kern_shift = manual_kern;
|
||||
aki_shift = aki_right;
|
||||
kern_shift = manual_kern + aki_shift;
|
||||
} else {
|
||||
kern_shift = before;
|
||||
aki_shift = aki_left;
|
||||
kern_shift = before + aki_shift;
|
||||
}
|
||||
if last_in_cluster {
|
||||
tracked = gst.tracking / 1000.0 * spacing_em;
|
||||
adv += tracked + manual_kern + before;
|
||||
aki_in_adv = aki_left + aki_right;
|
||||
adv += tracked + manual_kern + before + aki_in_adv;
|
||||
}
|
||||
}
|
||||
out.push(SGlyph {
|
||||
@@ -1654,6 +1698,8 @@ fn shape_segment(text: &str, context: &Range<usize>, seg: &Segment, feats: &OtFe
|
||||
dx: xo as f64 * k * hs,
|
||||
dy: -(yo as f64) * k * vs,
|
||||
kern_shift,
|
||||
aki_shift,
|
||||
aki: aki_in_adv,
|
||||
sx: k * hs,
|
||||
sy: k * vs,
|
||||
bshift: gst.baseline_shift + pos_shift,
|
||||
|
||||
@@ -3,6 +3,7 @@ use kurbo::{Affine, Shape};
|
||||
use vectorcraft_doc::{CharStyle, Justify, TextKind, TextRun};
|
||||
use vectorcraft_geom::PathData;
|
||||
|
||||
mod aki;
|
||||
mod alternates;
|
||||
mod axes;
|
||||
mod bidi;
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
//! Insert Aki Left and Right (`CharStyle::aki_left`, `CharStyle::aki_right`): space before and
|
||||
//! after a character in reading order, in thousandths of its em, that wrapping counts, alignment
|
||||
//! keeps at a line's end and justification does not stretch.
|
||||
use super::{area, db, point, serif, style};
|
||||
use crate::*;
|
||||
use kurbo::Shape;
|
||||
use vectorcraft_doc::{CharStyle, Justify};
|
||||
|
||||
/// Where glyph `byte` is drawn along a horizontal line.
|
||||
fn drawn_x(l: &TextLayout, byte: usize) -> f64 {
|
||||
l.glyphs.iter().find(|g| g.byte == byte).map(|g| g.xf.as_coeffs()[4]).unwrap()
|
||||
}
|
||||
|
||||
/// `t` with `f` applied to the style of the character at `byte`.
|
||||
fn with(t: &TextObject, byte: usize, f: impl Fn(&mut CharStyle)) -> TextObject {
|
||||
let mut t = t.clone();
|
||||
let end = edit::next_char(&t.plain_text(), byte);
|
||||
edit::style_range(&mut t.runs, byte, end, f);
|
||||
t
|
||||
}
|
||||
|
||||
/// How far each of `bytes` moved along the line from `a` to `b`, laid out in `fonts`.
|
||||
fn moves_in(fonts: &FontDb, a: &TextObject, b: &TextObject, bytes: &[usize]) -> Vec<f64> {
|
||||
let (la, lb) = (layout(fonts, a), layout(fonts, b));
|
||||
bytes.iter().map(|&i| drawn_x(&lb, i) - drawn_x(&la, i)).collect()
|
||||
}
|
||||
|
||||
fn moves(a: &TextObject, b: &TextObject, bytes: &[usize]) -> Vec<f64> {
|
||||
moves_in(db(), a, b, bytes)
|
||||
}
|
||||
|
||||
fn near(values: &[f64], expected: &[f64]) -> bool {
|
||||
values.len() == expected.len() && values.iter().zip(expected).all(|(v, e)| (v - e).abs() < 1e-6)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aki_left_spaces_before_a_character_and_aki_right_after_it() {
|
||||
let plain = point("HOH", style(100.0));
|
||||
// 250/1000 em of 100 pt before the O: 25 pt, the O and the H after it move.
|
||||
let left = moves(&plain, &with(&plain, 1, |s| s.aki_left = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&left, &[0.0, 25.0, 25.0]), "{left:?}");
|
||||
// After the O: only the H after it moves.
|
||||
let right = moves(&plain, &with(&plain, 1, |s| s.aki_right = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&right, &[0.0, 0.0, 25.0]), "{right:?}");
|
||||
// Both sides add up, and a value above one em is kept to one em.
|
||||
let both = moves(&plain, &with(&plain, 1, |s| (s.aki_left, s.aki_right) = (250.0, 500.0)), &[0, 1, 2]);
|
||||
assert!(near(&both, &[0.0, 25.0, 75.0]), "{both:?}");
|
||||
let most = moves(&plain, &with(&plain, 1, |s| s.aki_right = 5000.0), &[2]);
|
||||
assert!(near(&most, &[100.0]), "{most:?}");
|
||||
let not_a_number = moves(&plain, &with(&plain, 1, |s| s.aki_left = f64::NAN), &[1, 2]);
|
||||
assert!(near(¬_a_number, &[0.0, 0.0]), "{not_a_number:?}");
|
||||
assert_eq!(aki_space("HOH", 1, &with(&plain, 1, |s| s.aki_left = 500.0).runs[1].style, false), (50.0, 0.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aki_takes_the_em_of_the_character_as_tracking_does() {
|
||||
let narrow = point("HOH", CharStyle { h_scale: 50.0, ..style(100.0) });
|
||||
let moved = moves(&narrow, &with(&narrow, 1, |s| s.aki_left = 250.0), &[1, 2]);
|
||||
assert!(near(&moved, &[12.5, 12.5]), "{moved:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optical_kerning_spaces_the_pair_as_without_the_aki() {
|
||||
let optical = point("AVA", CharStyle { optical_kerning: true, ..style(100.0) });
|
||||
let moved = moves(&optical, &with(&optical, 1, |s| s.aki_left = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&moved, &[0.0, 25.0, 25.0]), "{moved:?}");
|
||||
let moved = moves(&optical, &with(&optical, 0, |s| s.aki_right = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&moved, &[0.0, 25.0, 25.0]), "{moved:?}");
|
||||
// A letter with a combining mark: the pair is measured from the letter, not from the mark
|
||||
// whose advance holds the cluster's Aki.
|
||||
let marked = point("H\u{301}V", CharStyle { optical_kerning: true, ..style(100.0) });
|
||||
let after = "H\u{301}".len();
|
||||
let moved = moves(&marked, &with(&marked, 0, |s| s.aki_right = 1000.0), &[0, after]);
|
||||
assert!(near(&moved, &[0.0, 100.0]), "{moved:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_aligned_line_keeps_the_aki_after_its_last_character() {
|
||||
let mut right = point("HOH", style(100.0));
|
||||
right.para.justify = Justify::Right;
|
||||
// Unlike the tracking after a line's last character, the Aki is part of the line.
|
||||
let moved = moves(&right, &with(&right, 2, |s| s.aki_right = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&moved, &[-25.0, -25.0, -25.0]), "{moved:?}");
|
||||
let tracked = moves(&right, &with(&right, 2, |s| s.tracking = 250.0), &[0, 2]);
|
||||
assert!(near(&tracked, &[0.0, 0.0]), "{tracked:?}");
|
||||
// At the line's start the Aki Left is kept too.
|
||||
let mut left = point("HOH", style(100.0));
|
||||
left.para.justify = Justify::Left;
|
||||
let moved = moves(&left, &with(&left, 0, |s| s.aki_left = 250.0), &[0, 2]);
|
||||
assert!(near(&moved, &[25.0, 25.0]), "{moved:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_line_too_wide_with_its_aki_wraps() {
|
||||
let one = layout(db(), &point("HOH HOH", style(50.0)));
|
||||
let wide = one.lines[0].x1 - one.lines[0].x0;
|
||||
let line_of = |t: &TextObject, byte: usize| layout(db(), t).glyphs.iter().find(|g| g.byte == byte).unwrap().line;
|
||||
let fits = area("HOH HOH", style(50.0), Rect::new(0.0, 0.0, wide + 5.0, 400.0), Justify::Left);
|
||||
assert_eq!(line_of(&fits, 4), 0, "both words fit");
|
||||
// 250/1000 em of 50 pt is 12.5 pt, more than the 5 pt the frame has left.
|
||||
assert_eq!(line_of(&with(&fits, 1, |s| s.aki_right = 250.0), 4), 1, "the second word wraps");
|
||||
// The Aki after the last letter of a wrapped line stays on that line: the next line starts
|
||||
// where it did.
|
||||
let narrow = area("HOH HOH", style(50.0), Rect::new(0.0, 0.0, wide * 0.6, 400.0), Justify::Left);
|
||||
let spaced = with(&narrow, 2, |s| s.aki_right = 250.0);
|
||||
assert_eq!((line_of(&narrow, 4), line_of(&spaced, 4)), (1, 1));
|
||||
let moved = moves(&narrow, &spaced, &[0, 4, 5]);
|
||||
assert!(near(&moved, &[0.0, 0.0, 0.0]), "{moved:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_does_not_stretch_or_narrow_the_aki() {
|
||||
// Desired Glyph Scaling 150% widens every glyph, and Word Spacing 200% every word space; the Aki
|
||||
// stays 25 pt wide.
|
||||
let mut scaled = point("HOH", style(100.0));
|
||||
scaled.para.justification.glyph_scaling = [150.0, 150.0, 150.0];
|
||||
let moved = moves(&scaled, &with(&scaled, 0, |s| s.aki_right = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&moved, &[0.0, 25.0, 25.0]), "{moved:?}");
|
||||
let moved = moves(&scaled, &with(&scaled, 1, |s| s.aki_left = 250.0), &[1, 2]);
|
||||
assert!(near(&moved, &[25.0, 25.0]), "{moved:?}");
|
||||
// A glyph that justification widens to fill the line keeps the Aki before it at its width,
|
||||
// and its own width, which ligature carets stand in, starts where the glyph is drawn.
|
||||
let mut filled = area("HOH", style(100.0), Rect::new(0.0, 0.0, 600.0, 400.0), Justify::JustifyAll);
|
||||
filled.para.justification.glyph_scaling = [100.0, 100.0, 300.0];
|
||||
let l = layout(db(), &with(&filled, 1, |s| s.aki_left = 250.0));
|
||||
let g = l.glyphs.iter().find(|g| g.byte == 1).unwrap();
|
||||
assert!(g.scale > 1.0 + 1e-6, "the glyph is widened: {}", g.scale);
|
||||
let drawn = g.xf.as_coeffs()[4];
|
||||
assert!((g.origin.x + g.width_start - drawn).abs() < 1e-6, "{} {} {drawn}", g.origin.x, g.width_start);
|
||||
let mut spaced = point("H H", style(100.0));
|
||||
spaced.para.justification.word_spacing = [200.0, 200.0, 200.0];
|
||||
let moved = moves(&spaced, &with(&spaced, 1, |s| s.aki_right = 250.0), &[0, 2]);
|
||||
assert!(near(&moved, &[0.0, 25.0]), "{moved:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_whole_point_widths_the_aki_is_whole_points_and_justification_keeps_it() {
|
||||
// 250/1000 em of 10 pt is 2.5 pt, set as 3 pt with Fractional Widths off, with or without
|
||||
// Desired Glyph Scaling 150%.
|
||||
for scaling in [100.0, 150.0] {
|
||||
let mut plain = point("HOH", style(10.0));
|
||||
plain.fractional_widths = false;
|
||||
plain.para.justification.glyph_scaling = [scaling; 3];
|
||||
let left = moves(&plain, &with(&plain, 1, |s| s.aki_left = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&left, &[0.0, 3.0, 3.0]), "Glyph Scaling {scaling}: {left:?}");
|
||||
let right = moves(&plain, &with(&plain, 1, |s| s.aki_right = 250.0), &[0, 1, 2]);
|
||||
assert!(near(&right, &[0.0, 0.0, 3.0]), "Glyph Scaling {scaling}: {right:?}");
|
||||
// 1.5 pt on each side: each side is set as 2 pt.
|
||||
let both = moves(&plain, &with(&plain, 1, |s| (s.aki_left, s.aki_right) = (150.0, 150.0)), &[0, 1, 2]);
|
||||
assert!(near(&both, &[0.0, 2.0, 4.0]), "Glyph Scaling {scaling}: {both:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ligature_and_a_letter_with_its_mark_take_the_aki_of_their_first_character() {
|
||||
let plain = point("fit", serif(100.0));
|
||||
let glyphs = |t: &TextObject| layout(db(), t).glyphs.len();
|
||||
assert_eq!(glyphs(&plain), 2, "the font joins fi");
|
||||
let spaced = with(&plain, 0, |s| (s.aki_left, s.aki_right) = (250.0, 250.0));
|
||||
assert_eq!(glyphs(&spaced), 2, "still joined");
|
||||
let moved = moves(&plain, &spaced, &[0, 2]);
|
||||
assert!(near(&moved, &[25.0, 50.0]), "{moved:?}");
|
||||
// On the i alone it is not read: the ligature's first character is the f.
|
||||
let on_i = with(&plain, 1, |s| s.aki_right = 250.0);
|
||||
assert_eq!(glyphs(&on_i), 2);
|
||||
let moved = moves(&plain, &on_i, &[0, 2]);
|
||||
assert!(near(&moved, &[0.0, 0.0]), "{moved:?}");
|
||||
// A combining acute after e: the letter's Aki spaces the pair, the mark's own is not read.
|
||||
let marked = point("e\u{301}a", style(100.0));
|
||||
let after = "e\u{301}".len();
|
||||
let moved = moves(&marked, &with(&marked, 0, |s| s.aki_right = 250.0), &[0, after]);
|
||||
assert!(near(&moved, &[0.0, 25.0]), "{moved:?}");
|
||||
let moved = moves(&marked, &with(&marked, 1, |s| s.aki_right = 250.0), &[0, after]);
|
||||
assert!(near(&moved, &[0.0, 0.0]), "{moved:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn right_to_left_aki_left_is_on_the_right_of_the_character() {
|
||||
// Reading order א ב ג is drawn right to left: Aki Left on ב widens the gap between א and ב,
|
||||
// Aki Right the gap between ב and ג.
|
||||
let fonts = FontDb::global();
|
||||
let plain = point("אבג", style(100.0));
|
||||
let (a, b, c) = (0, "א".len(), "אב".len());
|
||||
let gaps = |t: &TextObject| {
|
||||
let l = layout(fonts, t);
|
||||
(drawn_x(&l, a) - drawn_x(&l, b), drawn_x(&l, b) - drawn_x(&l, c))
|
||||
};
|
||||
let base = gaps(&plain);
|
||||
let left = gaps(&with(&plain, b, |s| s.aki_left = 250.0));
|
||||
assert!((left.0 - base.0 - 25.0).abs() < 1e-6 && (left.1 - base.1).abs() < 1e-6, "{base:?} {left:?}");
|
||||
let right = gaps(&with(&plain, b, |s| s.aki_right = 250.0));
|
||||
assert!((right.0 - base.0).abs() < 1e-6 && (right.1 - base.1 - 25.0).abs() < 1e-6, "{base:?} {right:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_vertical_type_aki_left_is_above_the_character_and_aki_right_below() {
|
||||
// An upright glyph (§) and letters lying on their side: 250/1000 em of 20 pt is 5 pt down the
|
||||
// column.
|
||||
for text in ["§§", "ab"] {
|
||||
let mut plain = point(text, style(20.0));
|
||||
plain.xf = Affine::IDENTITY;
|
||||
plain.vertical = true;
|
||||
let centers = |t: &TextObject| layout(db(), t).glyphs.iter().map(|g| g.outline.bounding_box().center()).collect::<Vec<_>>();
|
||||
let base = centers(&plain);
|
||||
let second = edit::next_char(text, 0);
|
||||
// [first, second] glyph moved down the column, for Aki on one character.
|
||||
for (spaced, down) in [
|
||||
(with(&plain, 0, |s| s.aki_left = 250.0), [5.0, 5.0]),
|
||||
(with(&plain, 0, |s| s.aki_right = 250.0), [0.0, 5.0]),
|
||||
(with(&plain, second, |s| s.aki_left = 250.0), [0.0, 5.0]),
|
||||
(with(&plain, second, |s| s.aki_right = 250.0), [0.0, 0.0]),
|
||||
] {
|
||||
let moved = centers(&spaced);
|
||||
for i in 0..2 {
|
||||
assert!((moved[i].y - base[i].y - down[i]).abs() < 1e-6, "{text} {i}: {base:?} {moved:?}");
|
||||
assert!((moved[i].x - base[i].x).abs() < 1e-6, "{text} {i}: {base:?} {moved:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A tate-chu-yoko block takes the Insert Aki of its first character above and below it, and
|
||||
/// keeps its characters across the column as without it.
|
||||
#[test]
|
||||
fn a_tate_chu_yoko_block_is_spaced_by_the_aki_of_its_first_character() {
|
||||
let mut plain = point("a12b", style(20.0));
|
||||
plain.xf = Affine::IDENTITY;
|
||||
plain.vertical = true;
|
||||
let centers = |t: &TextObject| layout(db(), t).glyphs.iter().map(|g| g.outline.bounding_box().center()).collect::<Vec<_>>();
|
||||
let base = centers(&plain);
|
||||
assert_eq!(base.len(), 4);
|
||||
// [a, 1, 2, b] moved down the column: 250/1000 em of 20 pt is 5 pt.
|
||||
for (spaced, down) in [
|
||||
(with(&plain, 1, |s| s.aki_left = 250.0), [0.0, 5.0, 5.0, 5.0]),
|
||||
(with(&plain, 1, |s| s.aki_right = 250.0), [0.0, 0.0, 0.0, 5.0]),
|
||||
(with(&plain, 1, |s| (s.aki_left, s.aki_right) = (250.0, 500.0)), [0.0, 5.0, 5.0, 15.0]),
|
||||
(with(&plain, 2, |s| (s.aki_left, s.aki_right) = (250.0, 500.0)), [0.0; 4]),
|
||||
] {
|
||||
let moved = centers(&spaced);
|
||||
for i in 0..4 {
|
||||
assert!((moved[i].y - base[i].y - down[i]).abs() < 1e-6, "{i}: {base:?} {moved:?}");
|
||||
assert!((moved[i].x - base[i].x).abs() < 1e-6, "{i}: {base:?} {moved:?}");
|
||||
}
|
||||
}
|
||||
// With Fractional Widths off, 150/1000 em of 10 pt on each side (1.5 pt) is set as 2 pt above
|
||||
// the block and 2 pt below it.
|
||||
let mut whole = point("a12b", style(10.0));
|
||||
whole.xf = Affine::IDENTITY;
|
||||
whole.vertical = true;
|
||||
whole.fractional_widths = false;
|
||||
let base = centers(&whole);
|
||||
let moved = centers(&with(&whole, 1, |s| (s.aki_left, s.aki_right) = (150.0, 150.0)));
|
||||
let down = [0.0, 2.0, 2.0, 4.0];
|
||||
for i in 0..4 {
|
||||
assert!((moved[i].y - base[i].y - down[i]).abs() < 1e-6, "{i}: {base:?} {moved:?}");
|
||||
assert!((moved[i].x - base[i].x).abs() < 1e-6, "{i}: {base:?} {moved:?}");
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,10 @@
|
||||
|
||||
mod solid;
|
||||
mod visibility;
|
||||
pub use solid::{Bevel, BevelShape, Extrude, Facet, Inflate, View, extrude, inflate, inflate_together, plane};
|
||||
pub use solid::{
|
||||
Bevel, BevelShape, Extrude, FaceMap, Facet, Inflate, PlanarFace, View, extrude, extrude_face_map, inflate, inflate_together, plane,
|
||||
plane_face_map,
|
||||
};
|
||||
pub use visibility::{VisibleFace, visible_faces};
|
||||
mod shading;
|
||||
pub use shading::{ClassicShading, ClassicSurface, LightGradient, RgbGradient, ShadeGradient};
|
||||
|
||||
@@ -14,6 +14,7 @@ use vectorcraft_geom::{BezPath, FillRule, ParamCurve, PathData, Point, Rect, Vec
|
||||
use crate::{MAX_FACES, MAX_SOLID_FACES, Revolve, V3, eye_distance, perspective_scale};
|
||||
|
||||
mod build;
|
||||
mod face_map;
|
||||
mod inflate;
|
||||
mod membrane;
|
||||
mod outline;
|
||||
@@ -23,6 +24,7 @@ mod rings;
|
||||
use build::{Camera, cuts, edge_distance, newell, section};
|
||||
pub(crate) use build::{Face3, extrude_faces, plane_faces};
|
||||
pub use build::{extrude, plane};
|
||||
pub use face_map::{FaceMap, extrude_face_map, plane_face_map};
|
||||
#[cfg(test)]
|
||||
use inflate::GRID_WORK;
|
||||
#[cfg(test)]
|
||||
@@ -242,6 +244,29 @@ impl Default for Inflate {
|
||||
}
|
||||
}
|
||||
|
||||
/// A planar face of a solid that a graphic can be mapped onto: Extrude & Bevel's front cap (the
|
||||
/// art itself, or a plane's only face) or its back cap.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum PlanarFace {
|
||||
Front,
|
||||
Back,
|
||||
}
|
||||
|
||||
impl PlanarFace {
|
||||
pub const ALL: [PlanarFace; 2] = [Self::Front, Self::Back];
|
||||
|
||||
pub fn id(self) -> &'static str {
|
||||
match self {
|
||||
Self::Front => "front",
|
||||
Self::Back => "back",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(s: &str) -> Option<Self> {
|
||||
Self::ALL.into_iter().find(|f| f.id() == s)
|
||||
}
|
||||
}
|
||||
|
||||
/// One projected polygon. Holes are contours of their own (nonzero winding).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Facet {
|
||||
@@ -263,6 +288,9 @@ pub struct Facet {
|
||||
/// The facet is turned away from the viewer: a closed solid keeps it (seen edge-on, or with
|
||||
/// Draw Hidden Faces) and draws it behind the facets turned toward the viewer.
|
||||
pub away: bool,
|
||||
/// The planar end of the solid the facet is: Extrude & Bevel's front or back cap, or a plane
|
||||
/// (its front). `None` for every other facet.
|
||||
pub end: Option<PlanarFace>,
|
||||
/// The contours in view space, before projection, for tests that measure depths.
|
||||
#[cfg(test)]
|
||||
view: Vec<Vec<V3>>,
|
||||
@@ -307,6 +335,8 @@ impl Facet {
|
||||
rgb_gradient: None,
|
||||
outline: face.outline,
|
||||
side: !face.cap,
|
||||
// A revolved cap is not one of the planar faces graphics map onto.
|
||||
end: None,
|
||||
away: false,
|
||||
#[cfg(test)]
|
||||
view: vec![],
|
||||
|
||||
@@ -6,7 +6,7 @@ use super::rings::{
|
||||
solid_rings_traced,
|
||||
};
|
||||
use super::{
|
||||
Bevel, Extrude, Facet, FillRule, MAX_FACES, MAX_SOLID_FACES, ParamCurve, PathData, Point, Rect, Revolve, V3, View, eye_distance,
|
||||
Bevel, Extrude, Facet, FillRule, MAX_FACES, MAX_SOLID_FACES, ParamCurve, PathData, PlanarFace, Point, Rect, Revolve, V3, View, eye_distance,
|
||||
perspective_scale,
|
||||
};
|
||||
use crate::shadow::Projection;
|
||||
@@ -49,6 +49,11 @@ impl Camera {
|
||||
pub(super) fn brightness(&self, n: V3) -> f64 {
|
||||
crate::brightness(&self.view, n, self.light)
|
||||
}
|
||||
/// The document point the view's center projects to, and the eye's distance in front of it
|
||||
/// (`None` without perspective).
|
||||
pub(super) fn center_and_eye(&self) -> (Point, Option<f64>) {
|
||||
(self.origin, self.eye)
|
||||
}
|
||||
/// The rotation and projection this camera draws with, for a shadow cast from its faces.
|
||||
pub(super) fn projection(&self) -> Projection {
|
||||
Projection { view: self.view, origin: self.origin, eye: self.eye }
|
||||
@@ -150,6 +155,7 @@ impl Builder<'_> {
|
||||
outline: None,
|
||||
side: false,
|
||||
away: behind,
|
||||
end: None,
|
||||
#[cfg(test)]
|
||||
view,
|
||||
});
|
||||
@@ -255,6 +261,17 @@ impl Builder<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark the facet that [`Builder::face`] added, when it added one after `before` facets, as the
|
||||
/// planar end `end` of the solid.
|
||||
pub(super) fn end(&mut self, before: usize, end: PlanarFace) {
|
||||
if self.mesh.is_some() || self.facets.len() <= before {
|
||||
return;
|
||||
}
|
||||
if let Some(f) = self.facets.last_mut() {
|
||||
f.end = Some(end);
|
||||
}
|
||||
}
|
||||
|
||||
/// How many faces the builder holds: facets, or faces kept for a mesh.
|
||||
pub(super) fn len(&self) -> usize {
|
||||
self.mesh.as_ref().map_or(self.facets.len(), Vec::len)
|
||||
@@ -301,6 +318,10 @@ impl Placing {
|
||||
self.at_centre.2 + self.x.2 * dx + self.y.2 * dy,
|
||||
)
|
||||
}
|
||||
/// The map as `(x, y, c)`: an art point `p` goes to `x · p.x + y · p.y + c` in view space.
|
||||
pub(super) fn frame(&self) -> (V3, V3, V3) {
|
||||
(self.x, self.y, self.place(Point::ZERO))
|
||||
}
|
||||
}
|
||||
|
||||
/// The point `w` points to the left of `span`'s curve at `t`, as the ring runs along it (toward
|
||||
@@ -598,11 +619,12 @@ pub(crate) fn extrude_faces(path: &PathData, rule: FillRule, bounds: Rect, optio
|
||||
if o.cap {
|
||||
// A cap turned toward the viewer covers the walls it overlaps, even those nearer than its
|
||||
// center: twisted walls, and the walls of a fill inside the solid of its stroke.
|
||||
let caps = [(steps.first(), along.first(), 1.0), (steps.last(), along.last(), -1.0)];
|
||||
for (s, along, n) in caps {
|
||||
let caps = [(steps.first(), along.first(), 1.0, PlanarFace::Front), (steps.last(), along.last(), -1.0, PlanarFace::Back)];
|
||||
for (s, along, n, end) in caps {
|
||||
let Some(s) = s else { continue };
|
||||
let before = b.len();
|
||||
b.face(rings.iter().map(|r| ring_at(r, s)).collect(), camera.rotate(V3(0.0, 0.0, n)), Order::Last)?;
|
||||
b.end(before, end);
|
||||
if let Some(along) = along {
|
||||
let traced: Vec<&Ring> = traced.iter().collect();
|
||||
b.outline(before, |c| outline::face(&traced, c, along, &mut budget));
|
||||
@@ -630,6 +652,7 @@ pub(crate) fn plane_faces(path: &PathData, rule: FillRule, bounds: Rect, view: V
|
||||
let up = camera.rotate(V3(0.0, 0.0, 1.0));
|
||||
let n = if camera.facing(V3::default(), up) < 0.0 { V3(-up.0, -up.1, -up.2) } else { up };
|
||||
b.face(contours, n, Order::Last)?;
|
||||
b.end(0, PlanarFace::Front);
|
||||
let m = Placing::new(&camera, bounds.center(), Turn::new(bounds, 0.0, 1.0, 0.0), 0.0);
|
||||
let at = |span: &Span, t: f64| camera.project(m.place(span.curve.eval(t)));
|
||||
let map = |p: Point| camera.project(m.place(p));
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
//! Where the points of a planar face's own plane land on the page: the map a graphic drawn on
|
||||
//! Extrude & Bevel's caps or on a plane goes through, with the same rotation, twist, taper and
|
||||
//! perspective as the face itself.
|
||||
|
||||
use vectorcraft_geom::{Affine, Point, Rect};
|
||||
|
||||
use super::build::{Camera, Placing, Turn, check_bounds};
|
||||
use super::{Extrude, PlanarFace, V3, View};
|
||||
|
||||
/// A planar face's map from the art's plane (document coordinates before the 3D effect) to the
|
||||
/// page: a projective map, affine without perspective.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct FaceMap {
|
||||
/// Rows of the map in homogeneous coordinates: a point `p` goes to `origin + (r0 · q, r1 · q) /
|
||||
/// (r2 · q)` with `q = (p.x, p.y, 1)`.
|
||||
rows: [[f64; 3]; 3],
|
||||
origin: Point,
|
||||
/// The eye's distance in front of the view's center; `None` without perspective.
|
||||
eye: Option<f64>,
|
||||
/// The face is turned toward the viewer. A plane, seen from either side, always is.
|
||||
pub visible: bool,
|
||||
}
|
||||
|
||||
/// The smallest share of the eye's distance a point of the face may lie in front of the eye at:
|
||||
/// the same limit [`crate::perspective_scale`] holds parts reaching past the eye to.
|
||||
const NEAREST: f64 = 1e-3;
|
||||
|
||||
impl FaceMap {
|
||||
fn new(camera: &Camera, placing: &Placing, visible: bool) -> Self {
|
||||
let (x, y, c) = placing.frame();
|
||||
let (origin, eye) = camera.center_and_eye();
|
||||
let rows = match eye {
|
||||
Some(e) => [[e * x.0, e * y.0, e * c.0], [e * x.1, e * y.1, e * c.1], [-x.2, -y.2, e - c.2]],
|
||||
None => [[x.0, y.0, c.0], [x.1, y.1, c.1], [0.0, 0.0, 1.0]],
|
||||
};
|
||||
Self { rows, origin, eye, visible }
|
||||
}
|
||||
|
||||
fn homogeneous(&self, p: Point) -> [f64; 3] {
|
||||
self.rows.map(|r| r[0] * p.x + r[1] * p.y + r[2])
|
||||
}
|
||||
|
||||
/// The homogeneous depth a mapped point must lie beyond: the eye's distance times [`NEAREST`]
|
||||
/// with perspective, 0 without.
|
||||
fn nearest(&self) -> f64 {
|
||||
self.eye.map_or(0.0, |e| e * NEAREST)
|
||||
}
|
||||
|
||||
/// Where art point `p` lands on the page. `None` when it lies at or behind the eye, or the
|
||||
/// result is not finite.
|
||||
pub fn to_page(&self, p: Point) -> Option<Point> {
|
||||
let [u, v, w] = self.homogeneous(p);
|
||||
if w.is_nan() || w <= self.nearest() {
|
||||
return None;
|
||||
}
|
||||
let q = Point::new(self.origin.x + u / w, self.origin.y + v / w);
|
||||
(q.x.is_finite() && q.y.is_finite()).then_some(q)
|
||||
}
|
||||
|
||||
/// The art point that lands on page point `q`. `None` when the face is seen edge-on there, the
|
||||
/// point lies beyond the face's horizon, or it lies nearer the eye than [`FaceMap::to_page`]
|
||||
/// maps.
|
||||
pub fn to_art(&self, q: Point) -> Option<Point> {
|
||||
let (u, v) = (q.x - self.origin.x, q.y - self.origin.y);
|
||||
let [r0, r1, r2] = self.rows;
|
||||
let (a, b, c) = (r0[0] - u * r2[0], r0[1] - u * r2[1], -(r0[2] - u * r2[2]));
|
||||
let (d, e, f) = (r1[0] - v * r2[0], r1[1] - v * r2[1], -(r1[2] - v * r2[2]));
|
||||
let det = a * e - b * d;
|
||||
let scale = (a.abs() + b.abs()).max(d.abs() + e.abs());
|
||||
if det.is_nan() || det.abs() <= 1e-12 * scale * scale {
|
||||
return None;
|
||||
}
|
||||
let p = Point::new((c * e - b * f) / det, (a * f - c * d) / det);
|
||||
// A point of the plane behind the eye projects to the same page point as one in front, and
|
||||
// `to_page` maps no point nearer the eye than its limit.
|
||||
(p.x.is_finite() && p.y.is_finite() && self.homogeneous(p)[2] > self.nearest()).then_some(p)
|
||||
}
|
||||
|
||||
/// Does every point of the cubic Bézier curve with control points `c` lie far enough in front
|
||||
/// of the eye for [`FaceMap::to_page`] to map it? Its control points need not.
|
||||
pub fn maps_curve(&self, c: [Point; 4]) -> bool {
|
||||
let [w0, w1, w2, w3] = c.map(|p| self.homogeneous(p)[2]);
|
||||
let depth = |t: f64| {
|
||||
let s = 1.0 - t;
|
||||
s * s * s * w0 + 3.0 * s * s * t * w1 + 3.0 * s * t * t * w2 + t * t * t * w3
|
||||
};
|
||||
// The depth is least at an end or where its derivative, a t² + b t + k (times 3), is 0.
|
||||
let (a, b, k) = (w3 - 3.0 * w2 + 3.0 * w1 - w0, 2.0 * (w2 - 2.0 * w1 + w0), w1 - w0);
|
||||
// A missing root is NaN, which the range check leaves out.
|
||||
let mut at = [0.0, 1.0, f64::NAN, f64::NAN];
|
||||
if a.abs() > 1e-12 {
|
||||
let root = (b * b - 4.0 * a * k).sqrt();
|
||||
at[2] = (-b + root) / (2.0 * a);
|
||||
at[3] = (-b - root) / (2.0 * a);
|
||||
} else if b.abs() > 1e-12 {
|
||||
at[2] = -k / b;
|
||||
}
|
||||
let limit = self.nearest();
|
||||
at.into_iter().filter(|t| (0.0..=1.0).contains(t)).all(|t| depth(t) > limit)
|
||||
}
|
||||
|
||||
/// The part of rectangle `r` of the face's plane whose points [`FaceMap::to_page`] maps, kept
|
||||
/// back from the eye by twice its nearest distance, as a convex polygon: all of `r` without
|
||||
/// perspective. `None` when no part of `r` maps.
|
||||
pub fn mapped_part(&self, r: Rect) -> Option<Vec<Point>> {
|
||||
let corners = vec![Point::new(r.x0, r.y0), Point::new(r.x1, r.y0), Point::new(r.x1, r.y1), Point::new(r.x0, r.y1)];
|
||||
let Some(e) = self.eye else { return Some(corners) };
|
||||
let [a, b, c] = self.rows[2];
|
||||
let limit = 2.0 * e * NEAREST;
|
||||
let side = |p: Point| a * p.x + b * p.y + c - limit;
|
||||
// The polygon cut by the line where `side` is 0 (one step of Sutherland and Hodgman's clip).
|
||||
let mut out = vec![];
|
||||
for (k, &p) in corners.iter().enumerate() {
|
||||
let q = corners[(k + 1) % corners.len()];
|
||||
let (sp, sq) = (side(p), side(q));
|
||||
if sp >= 0.0 {
|
||||
out.push(p);
|
||||
}
|
||||
if (sp >= 0.0) != (sq >= 0.0) {
|
||||
out.push(p.lerp(q, sp / (sp - sq)));
|
||||
}
|
||||
}
|
||||
(out.len() >= 3 && out.iter().all(|p| p.x.is_finite() && p.y.is_finite())).then_some(out)
|
||||
}
|
||||
|
||||
/// Without perspective, the map is affine.
|
||||
pub fn is_affine(&self) -> bool {
|
||||
self.eye.is_none()
|
||||
}
|
||||
|
||||
/// The affine map nearest the face's map around art point `p` (exact without perspective):
|
||||
/// what a gradient or a stroke on a graphic is drawn with. `None` where [`FaceMap::to_page`]
|
||||
/// has no point.
|
||||
pub fn local_affine(&self, p: Point) -> Option<Affine> {
|
||||
let at = self.to_page(p)?;
|
||||
let [u, v, w] = self.homogeneous(p);
|
||||
let [r0, r1, r2] = self.rows;
|
||||
// The derivatives of u / w and v / w along x and y.
|
||||
let d = |r: [f64; 3], n: f64, k: usize| (r[k] * w - n * r2[k]) / (w * w);
|
||||
let (a, b, c, e) = (d(r0, u, 0), d(r1, v, 0), d(r0, u, 1), d(r1, v, 1));
|
||||
let m = Affine::new([a, b, c, e, at.x - a * p.x - c * p.y, at.y - b * p.x - e * p.y]);
|
||||
m.as_coeffs().iter().all(|v| v.is_finite()).then_some(m)
|
||||
}
|
||||
}
|
||||
|
||||
/// The map of Extrude & Bevel's cap `face` for a solid built with `bounds`, `options` and `view`,
|
||||
/// as [`super::extrude`] builds it. With a depth of 0 the art is a plane, whose only face is the
|
||||
/// front. `None` for a hollow solid (no caps), for the back of a plane, and for invalid bounds.
|
||||
pub fn extrude_face_map(bounds: Rect, options: Extrude, view: View, face: PlanarFace) -> Option<FaceMap> {
|
||||
check_bounds(bounds).ok()?;
|
||||
let o = options.sanitized();
|
||||
if o.depth <= 0.0 {
|
||||
return (face == PlanarFace::Front).then(|| plane_face_map(bounds, view)).flatten();
|
||||
}
|
||||
if !o.cap {
|
||||
return None;
|
||||
}
|
||||
let camera = Camera::new(view, bounds, o.depth);
|
||||
let (t, side) = match face {
|
||||
PlanarFace::Front => (0.0, 1.0),
|
||||
PlanarFace::Back => (1.0, -1.0),
|
||||
};
|
||||
let placing = Placing::new(&camera, bounds.center(), Turn::new(bounds, o.twist, o.taper, t), o.depth / 2.0 - o.depth * t);
|
||||
let at = placing.place(bounds.center());
|
||||
let visible = camera.facing(at, camera.rotate(V3(0.0, 0.0, side))) > 0.0;
|
||||
Some(FaceMap::new(&camera, &placing, visible))
|
||||
}
|
||||
|
||||
/// The map of the plane [`super::plane`] turns art with `bounds` into, with `view`.
|
||||
pub fn plane_face_map(bounds: Rect, view: View) -> Option<FaceMap> {
|
||||
check_bounds(bounds).ok()?;
|
||||
let camera = Camera::new(view, bounds, 0.0);
|
||||
let placing = Placing::new(&camera, bounds.center(), Turn::new(bounds, 0.0, 1.0, 0.0), 0.0);
|
||||
Some(FaceMap::new(&camera, &placing, true))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tests_face_map.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,160 @@
|
||||
use vectorcraft_geom::{BezPath, FillRule, PathData, Point, Rect};
|
||||
|
||||
use super::super::{Extrude, Facet, PlanarFace, View, extrude, plane};
|
||||
use super::{FaceMap, NEAREST, extrude_face_map, plane_face_map};
|
||||
|
||||
fn square() -> (PathData, Rect) {
|
||||
let mut b = BezPath::new();
|
||||
b.move_to((10.0, 20.0));
|
||||
b.line_to((70.0, 20.0));
|
||||
b.line_to((70.0, 60.0));
|
||||
b.line_to((10.0, 60.0));
|
||||
b.close_path();
|
||||
let p = PathData::from_bezpath(&b);
|
||||
let r = p.bounds().unwrap();
|
||||
(p, r)
|
||||
}
|
||||
|
||||
fn turned(perspective: f64) -> View {
|
||||
View { rotation_x: -30.0, rotation_y: 40.0, rotation_z: 10.0, perspective, ..View::default() }
|
||||
}
|
||||
|
||||
fn corners(r: Rect) -> [Point; 4] {
|
||||
[Point::new(r.x0, r.y0), Point::new(r.x1, r.y0), Point::new(r.x1, r.y1), Point::new(r.x0, r.y1)]
|
||||
}
|
||||
|
||||
/// Every corner of `face` is where the map takes one of the art's corners.
|
||||
fn lands_on(face: &Facet, map: &FaceMap, art: [Point; 4]) {
|
||||
for c in face.contours.iter().flatten() {
|
||||
let near = art.iter().filter_map(|p| map.to_page(*p)).map(|q| q.distance(*c)).fold(f64::INFINITY, f64::min);
|
||||
assert!(near < 1e-6, "facet corner {c:?} is {near} from every mapped corner");
|
||||
}
|
||||
}
|
||||
|
||||
fn end(facets: &[Facet], end: PlanarFace) -> Option<&Facet> {
|
||||
facets.iter().find(|f| f.end == Some(end))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_front_cap_is_where_the_map_takes_the_art() {
|
||||
let (path, bounds) = square();
|
||||
for perspective in [0.0, 60.0, 140.0] {
|
||||
let view = turned(perspective);
|
||||
let facets = extrude(&path, FillRule::NonZero, bounds, Extrude::default(), view).unwrap();
|
||||
let map = extrude_face_map(bounds, Extrude::default(), view, PlanarFace::Front).unwrap();
|
||||
let front = end(&facets, PlanarFace::Front).unwrap();
|
||||
assert!(map.visible && !front.away);
|
||||
lands_on(front, &map, corners(bounds));
|
||||
assert_eq!(map.is_affine(), perspective == 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_back_cap_follows_the_twist_and_taper() {
|
||||
let (path, bounds) = square();
|
||||
let options = Extrude { twist: 35.0, taper: 0.5, ..Extrude::default() };
|
||||
// Seen from behind, the back cap faces the viewer and the front does not.
|
||||
let view = View { rotation_y: 160.0, perspective: 50.0, ..turned(0.0) };
|
||||
let facets = extrude(&path, FillRule::NonZero, bounds, options, view).unwrap();
|
||||
let back = end(&facets, PlanarFace::Back).unwrap();
|
||||
let map = extrude_face_map(bounds, options, view, PlanarFace::Back).unwrap();
|
||||
assert!(map.visible);
|
||||
assert!(!extrude_face_map(bounds, options, view, PlanarFace::Front).unwrap().visible);
|
||||
assert!(end(&facets, PlanarFace::Front).is_none_or(|f| f.away));
|
||||
lands_on(back, &map, corners(bounds));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_plane_has_only_a_front_and_a_hollow_solid_none() {
|
||||
let (path, bounds) = square();
|
||||
let view = turned(80.0);
|
||||
let facets = plane(&path, FillRule::NonZero, bounds, view).unwrap();
|
||||
let map = plane_face_map(bounds, view).unwrap();
|
||||
lands_on(end(&facets, PlanarFace::Front).unwrap(), &map, corners(bounds));
|
||||
let flat = Extrude { depth: 0.0, ..Extrude::default() };
|
||||
assert_eq!(extrude_face_map(bounds, flat, view, PlanarFace::Front), Some(map));
|
||||
assert_eq!(extrude_face_map(bounds, flat, view, PlanarFace::Back), None);
|
||||
let hollow = Extrude { cap: false, ..Extrude::default() };
|
||||
assert_eq!(extrude_face_map(bounds, hollow, view, PlanarFace::Front), None);
|
||||
let facets = extrude(&path, FillRule::NonZero, bounds, hollow, view).unwrap();
|
||||
assert!(facets.iter().all(|f| f.end.is_none()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_points_map_back_to_the_art() {
|
||||
let (_, bounds) = square();
|
||||
for perspective in [0.0, 120.0] {
|
||||
let map = extrude_face_map(bounds, Extrude::default(), turned(perspective), PlanarFace::Front).unwrap();
|
||||
for p in [Point::new(10.0, 20.0), Point::new(45.0, 33.0), Point::new(-200.0, 400.0)] {
|
||||
let q = map.to_page(p).unwrap();
|
||||
let back = map.to_art(q).unwrap();
|
||||
assert!(back.distance(p) < 1e-6, "{p:?} came back as {back:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_local_affine_map_matches_the_face_near_its_point() {
|
||||
let (_, bounds) = square();
|
||||
let flat = extrude_face_map(bounds, Extrude::default(), turned(0.0), PlanarFace::Front).unwrap();
|
||||
let m = flat.local_affine(Point::new(40.0, 40.0)).unwrap();
|
||||
for p in corners(bounds) {
|
||||
assert!((m * p).distance(flat.to_page(p).unwrap()) < 1e-9);
|
||||
}
|
||||
let deep = extrude_face_map(bounds, Extrude::default(), turned(120.0), PlanarFace::Front).unwrap();
|
||||
let at = Point::new(40.0, 40.0);
|
||||
let [a, b, c, d, ..] = deep.local_affine(at).unwrap().as_coeffs();
|
||||
let h = 1e-4;
|
||||
let slope = |dx: f64, dy: f64| {
|
||||
let (p, q) = (deep.to_page(Point::new(at.x + dx, at.y + dy)).unwrap(), deep.to_page(Point::new(at.x - dx, at.y - dy)).unwrap());
|
||||
((p.x - q.x) / (2.0 * h), (p.y - q.y) / (2.0 * h))
|
||||
};
|
||||
let ((ax, bx), (cy, dy)) = (slope(h, 0.0), slope(0.0, h));
|
||||
for (got, want) in [(a, ax), (b, bx), (c, cy), (d, dy)] {
|
||||
assert!((got - want).abs() < 1e-6, "{got} against {want}");
|
||||
}
|
||||
assert!((deep.local_affine(at).unwrap() * at).distance(deep.to_page(at).unwrap()) < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_point_behind_the_eye_has_no_page_point() {
|
||||
let (_, bounds) = square();
|
||||
// Seen almost edge-on at the widest perspective, far points of the plane pass behind the eye.
|
||||
let view = View { rotation_x: -89.0, perspective: 160.0, ..turned(0.0) };
|
||||
let map = plane_face_map(bounds, view).unwrap();
|
||||
assert!(map.to_page(Point::new(40.0, -1e6)).is_none() || map.to_page(Point::new(40.0, 1e6)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_page_point_nearer_the_eye_than_the_page_map_allows_has_no_art_point() {
|
||||
let (_, bounds) = square();
|
||||
let view = View { rotation_x: -89.0, perspective: 160.0, ..turned(0.0) };
|
||||
let map = plane_face_map(bounds, view).unwrap();
|
||||
let limit = map.eye.unwrap() * NEAREST;
|
||||
// An art point in front of the eye, but at half the nearest depth `to_page` maps.
|
||||
let [a, b, c] = map.rows[2];
|
||||
let p0 = bounds.center();
|
||||
let t = (limit / 2.0 - (a * p0.x + b * p0.y + c)) / (a * a + b * b);
|
||||
let p = Point::new(p0.x + t * a, p0.y + t * b);
|
||||
let [u, v, w] = map.homogeneous(p);
|
||||
assert!(w > 0.0 && w < limit, "{w}");
|
||||
assert_eq!(map.to_page(p), None);
|
||||
let q = Point::new(map.origin.x + u / w, map.origin.y + v / w);
|
||||
assert_eq!(map.to_art(q), None, "the inverse map takes {q:?} back to an art point the page map has no point for");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_mapped_part_of_a_rectangle_stops_before_the_eye() {
|
||||
let (_, bounds) = square();
|
||||
let view = View { rotation_x: -89.0, perspective: 160.0, ..turned(0.0) };
|
||||
let map = plane_face_map(bounds, view).unwrap();
|
||||
let r = Rect::new(-1e6, -1e6, 1e6, 1e6);
|
||||
let part = map.mapped_part(r).unwrap();
|
||||
// Some of the rectangle passes behind the eye, and every corner of what is left maps.
|
||||
assert!((3..=5).contains(&part.len()));
|
||||
assert!(part.iter().all(|p| map.to_page(*p).is_some()), "{part:?}");
|
||||
assert!(corners(r).iter().any(|p| !part.contains(p)));
|
||||
// Without perspective, all of it maps.
|
||||
let flat = plane_face_map(bounds, View { perspective: 0.0, ..view }).unwrap();
|
||||
assert_eq!(flat.mapped_part(r).map(|p| p.len()), Some(4));
|
||||
}
|
||||
@@ -410,6 +410,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
draw_overlays(&painter, &xf, &overlays, &t, HandleLook::of(&app.session.prefs));
|
||||
if !revolve_edit {
|
||||
shaper_widget_overlay(app, &painter, &xf);
|
||||
crate::panels::three_d::paint_graphic_handles(app, &painter, &xf);
|
||||
}
|
||||
}
|
||||
ime_geometry(app, ui.ctx(), &xf);
|
||||
@@ -444,14 +445,17 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
}
|
||||
|
||||
/// The pointer on the canvas: the tool's input, unless the context menu or the 3D gizmo has the press.
|
||||
fn canvas_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Response, rect: egui::Rect) {
|
||||
// A press on the canvas while the context menu is open only closes it (a drag too, which egui
|
||||
// alone would leave open); the tool doesn't get it.
|
||||
pub(crate) fn canvas_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Response, rect: egui::Rect) {
|
||||
if app.ui.dialog.as_ref().is_some_and(crate::dialogs::revolve_gizmo::active) {
|
||||
// The artboard's 3D gizmo owns these gestures while its options are open.
|
||||
// Do not let a ring drag also move or deselect the editable source path.
|
||||
} else if resp.context_menu_opened() && resp.hovered() && ui.input(|i| i.pointer.any_pressed()) {
|
||||
// A press on the canvas while the context menu is open only closes it (a drag too, which
|
||||
// egui alone would leave open); neither the tool nor a graphic's handle gets it.
|
||||
egui::Popup::close_all(ui.ctx());
|
||||
} else if crate::panels::three_d::graphic_handles_input(app, ui, resp, rect) {
|
||||
// A mapped graphic's handle, shown while the 3D and Materials panel's Materials tab is
|
||||
// open, has the press: the drag moves the graphic, not the object.
|
||||
} else {
|
||||
handle_input(app, ui, resp, rect);
|
||||
// `hover_doc` is the document point under the pointer in the view `handle_input` left. The
|
||||
|
||||
@@ -132,6 +132,7 @@ pub mod tate_chu_yoko;
|
||||
mod text_export;
|
||||
pub mod text_import;
|
||||
mod three_d_classic;
|
||||
pub(crate) mod three_d_materials;
|
||||
mod tiff_bmp_tga;
|
||||
pub mod tile_edge_color;
|
||||
mod tools;
|
||||
|
||||
@@ -26,9 +26,12 @@ const NUMBERS: &[&str] = &["size", "leading", "kerning", "tracking"];
|
||||
/// The on, off or unset checkboxes.
|
||||
const SWITCHES: &[&str] = &["underline", "strikethrough", "standardVerticalRoman"];
|
||||
/// The attributes of Advanced Character Formats, in the order the section shows them.
|
||||
const ADVANCED: &[&str] = &["h_scale", "v_scale", "baseline_shift", "tcyUpDown", "tcyLeftRight", "rotation", "language"];
|
||||
const ADVANCED: &[&str] = &["h_scale", "v_scale", "baseline_shift", "tcyUpDown", "tcyLeftRight", "rotation", "akiLeft", "akiRight", "language"];
|
||||
/// The number fields of Advanced Character Formats.
|
||||
const ADVANCED_NUMBERS: &[&str] = &["h_scale", "v_scale", "baseline_shift", "tcyUpDown", "tcyLeftRight", "rotation"];
|
||||
const ADVANCED_NUMBERS: &[&str] = &["h_scale", "v_scale", "baseline_shift", "tcyUpDown", "tcyLeftRight", "rotation", "akiLeft", "akiRight"];
|
||||
/// The Insert Aki Left and Right fields, which take the Character panel's presets and stop at
|
||||
/// [`vectorcraft_doc::text::MAX_AKI`].
|
||||
const AKI: [&str; 2] = ["akiLeft", "akiRight"];
|
||||
/// The attributes of Character Color.
|
||||
const COLOR: &[&str] = &["fill", "stroke", "stroke_width", "overprint_fill", "overprint_stroke"];
|
||||
/// The fields of Character Color: the fill and stroke paints, their tints in percent, the stroke
|
||||
@@ -81,6 +84,8 @@ pub(super) const SPEC: DialogSpec = DialogSpec {
|
||||
"tcyUpDown",
|
||||
"tcyLeftRight",
|
||||
"rotation",
|
||||
"akiLeft",
|
||||
"akiRight",
|
||||
"language",
|
||||
"fill",
|
||||
"fill_tint",
|
||||
@@ -268,6 +273,8 @@ fn optional_number(d: &Dialog, key: &str) -> Result<Option<Value>, String> {
|
||||
"tcyUpDown" => tl!("Tate-chu-yoko Up/Down"),
|
||||
"tcyLeftRight" => tl!("Tate-chu-yoko Left/Right"),
|
||||
"rotation" => tl!("Character Rotation"),
|
||||
"akiLeft" => tl!("Insert Aki Left"),
|
||||
"akiRight" => tl!("Insert Aki Right"),
|
||||
"fill_tint" => tl!("Fill Tint"),
|
||||
"stroke_tint" => tl!("Stroke Tint"),
|
||||
"stroke_width" => tl!("Weight"),
|
||||
@@ -312,6 +319,9 @@ fn optional_number(d: &Dialog, key: &str) -> Result<Option<Value>, String> {
|
||||
}
|
||||
// An angle from -180 to 180 degrees, as text.setFormat stores it.
|
||||
let n = if key == "rotation" { (n + 180.0).rem_euclid(360.0) - 180.0 } else { n };
|
||||
// Insert Aki stops at 0 and at the largest value the style takes, as the Character
|
||||
// panel's fields do.
|
||||
let n = if AKI.contains(&key) { n.clamp(0.0, vectorcraft_doc::text::MAX_AKI) } else { n };
|
||||
Ok(Some(json!(n)))
|
||||
}
|
||||
}
|
||||
@@ -590,7 +600,7 @@ pub(crate) fn reset_basic(d: &mut Dialog) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset Panel in Advanced Character Formats: its seven attributes become unset.
|
||||
/// Reset Panel in Advanced Character Formats: its nine attributes become unset.
|
||||
pub(crate) fn reset_advanced(d: &mut Dialog) {
|
||||
d.fields.insert("__resetAdvanced".into(), json!(true));
|
||||
for key in ADVANCED_NUMBERS {
|
||||
@@ -745,7 +755,8 @@ pub(crate) fn basic_body(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
}
|
||||
|
||||
/// Advanced Character Formats: Horizontal Scale, Vertical Scale, Baseline Shift, the Tate-chu-yoko
|
||||
/// Up/Down and Left/Right, Character Rotation and Language, each empty (unset) or set.
|
||||
/// Up/Down and Left/Right, Character Rotation, Insert Aki Left and Right (with the Character
|
||||
/// panel's presets) and Language, each empty (unset) or set.
|
||||
pub(crate) fn advanced_body(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
egui::Grid::new("character-style-advanced").num_columns(2).spacing([18.0, 8.0]).show(ui, |ui| {
|
||||
for (key, caption, unit) in [
|
||||
@@ -766,6 +777,16 @@ pub(crate) fn advanced_body(ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
});
|
||||
ui.end_row();
|
||||
}
|
||||
let presets: Vec<String> = crate::panels::character::AKI_PRESETS.iter().map(f64::to_string).collect();
|
||||
let presets: Vec<&str> = presets.iter().map(String::as_str).collect();
|
||||
for (key, caption) in [("akiLeft", tl!("Insert Aki Left")), ("akiRight", tl!("Insert Aki Right"))] {
|
||||
let row = ui.label(caption);
|
||||
let value = if d.fields.get(key).is_none_or(Value::is_null) { String::new() } else { d.str(key) };
|
||||
if let Some(v) = ui.label_inputs_by(row.id, |ui| crate::widgets::text_presets(ui, (KIND, key), &value, &presets, 160.0)) {
|
||||
d.fields.insert(key.into(), json!(v));
|
||||
}
|
||||
ui.end_row();
|
||||
}
|
||||
let row = ui.label(tl!("Language:"));
|
||||
let names: Vec<&str> = std::iter::once(" ").chain(vectorcraft_doc::setup::LANGUAGES.iter().map(|(name, _, _)| *name)).collect();
|
||||
let current = d.fields.get("language").and_then(Value::as_str).unwrap_or("").to_string();
|
||||
@@ -1684,6 +1705,60 @@ mod tests {
|
||||
assert_eq!(def.attrs["tcyLeftRight"], json!(6.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_aki_fields_show_set_reset_and_stop_at_the_character_panel_limits() {
|
||||
let d = dialog_for(json!({"akiLeft": 250.0, "size": 12.0}));
|
||||
assert_eq!((d.fields["akiLeft"].clone(), d.fields["akiRight"].clone()), (json!(250.0), json!("")));
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"akiLeft": 250.0, "size": 12.0})));
|
||||
|
||||
let mut d = dialog_for(json!({"akiLeft": 250.0}));
|
||||
d.fields.insert("akiLeft".into(), json!(""));
|
||||
d.fields.insert("akiRight".into(), json!("500"));
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"akiRight": 500.0})));
|
||||
// A typed value stops at 0 and at 1000, as in the Character panel's fields.
|
||||
for (typed, kept) in [("5000", 1000.0), ("-5", 0.0), ("1000", 1000.0)] {
|
||||
d.fields.insert("akiRight".into(), json!(typed));
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"akiRight": kept})), "{typed}");
|
||||
}
|
||||
d.fields.insert("akiLeft".into(), json!("wide"));
|
||||
assert_eq!(edited_attrs(&d).unwrap_err(), "Insert Aki Left must be a number");
|
||||
|
||||
let mut d = dialog_for(json!({"akiLeft": 250.0, "akiRight": 500.0, "size": 12.0}));
|
||||
d.fields.insert("section".into(), json!("advanced"));
|
||||
reset_section(&mut d);
|
||||
assert_eq!(edited_attrs(&d).unwrap(), attrs(json!({"size": 12.0})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_aki_fields_are_drawn_and_commit_to_the_style_and_its_text() {
|
||||
let mut app = app();
|
||||
app.run("charStyle.new", json!({"name": "Body", "attrs": {"size": 24.0}})).unwrap();
|
||||
let id = app.run("text.create", json!({"x": 10, "y": 40, "text": "Text"})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("charStyle.apply", json!({"name": "Body", "id": id})).unwrap();
|
||||
open(&mut app, "Body").unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
crate::theme::install_fonts(&ctx);
|
||||
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(900.0, 900.0));
|
||||
let mut d = app.ui.dialog.clone().unwrap();
|
||||
let input = egui::RawInput { screen_rect: Some(screen), ..Default::default() };
|
||||
let mut out = ctx.run_ui(input, |ui| advanced_body(ui, &mut d));
|
||||
let texts = texts_of(&out);
|
||||
out.textures_delta.clear();
|
||||
for label in ["Insert Aki Left", "Insert Aki Right"] {
|
||||
assert!(texts.iter().any(|(text, _)| text == label), "{label} is drawn: {texts:?}");
|
||||
}
|
||||
{
|
||||
let d = app.ui.dialog.as_mut().unwrap();
|
||||
d.fields.insert("akiLeft".into(), json!("250"));
|
||||
d.fields.insert("akiRight".into(), json!("2000"));
|
||||
}
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
let state = app.session.doc().unwrap();
|
||||
assert_eq!(state.doc.char_styles[0].attrs, attrs(json!({"size": 24.0, "akiLeft": 250.0, "akiRight": 1000.0})));
|
||||
let vectorcraft_doc::NodeKind::Text(t) = &state.doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind else { panic!("text") };
|
||||
assert!(t.runs.iter().all(|r| (r.style.aki_left, r.style.aki_right) == (250.0, 1000.0)));
|
||||
}
|
||||
|
||||
/// The negated tags of the twenty stylistic sets: an explicit empty Stylistic Sets choice.
|
||||
fn no_sets() -> Vec<String> {
|
||||
vectorcraft_text::EXTRA_FEATURES[..20].iter().map(|s| format!("-{s}")).collect()
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! artboard, after a central file of the whole document for EPS with two or more when art shows,
|
||||
//! or one page each in a PDF); without artboards the export covers the visible art. OK
|
||||
//! picks the file, then the format's options dialog follows (PNG/JPEG/WebP/PNG-8/GIF Options, SVG
|
||||
//! Options, Text Export Options, DXF Options, EPS Options, and Save PDF for artboards as pages),
|
||||
//! Options, Text Export Options, DXF Options, EPS Options, and Save PDF),
|
||||
//! else the file is written. DWG is listed, greyed out, with what to use instead. The formats of
|
||||
//! installed exporter plug-ins come after the built-in ones: they write the whole document, and one
|
||||
//! with parameters asks for them before writing (`plugin::open_export`).
|
||||
@@ -169,8 +169,9 @@ fn options_or_write(
|
||||
}
|
||||
Ok(Value::Null)
|
||||
}
|
||||
// Artboards as pages go on in Save PDF; the art's bounds are written straight away.
|
||||
// Save PDF writes the artboards as pages, or one page of the art's bounds.
|
||||
"pdf" if use_artboards => save_pdf::open(app, &json!({ "path": path, "range": (!all).then_some(range) })),
|
||||
"pdf" => save_pdf::open(app, &json!({ "path": path, "useArtboards": false })),
|
||||
"txt" => {
|
||||
text_export::open(app, &path);
|
||||
Ok(Value::Null)
|
||||
|
||||
@@ -37,6 +37,14 @@ pub(super) fn body(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
let id = d.str("__effect");
|
||||
let defaults = vectorcraft_effects::default_params(&id).unwrap_or(json!({}));
|
||||
// Reset all resets the shape, view and light; the mapped graphics stay.
|
||||
let defaults = match defaults {
|
||||
Value::Object(mut m) => {
|
||||
m.remove(vectorcraft_effects::GRAPHICS);
|
||||
Value::Object(m)
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
let changed = defaults.as_object().is_some_and(|m| {
|
||||
m.iter().any(|(k, v)| v.as_f64().map_or_else(|| d.fields.get(k) != Some(v), |default| (d.f64(k, default) - default).abs() > 1e-6))
|
||||
});
|
||||
@@ -71,14 +79,24 @@ pub(crate) fn controls(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog)
|
||||
return solid_controls(app, ui, d, &id);
|
||||
}
|
||||
let lighting = super::revolve_gizmo::lighting(d);
|
||||
if let Some(tab) = widgets::tab_bar(ui, &[tl!("Rotation"), tl!("Lighting")], usize::from(lighting)) {
|
||||
d.fields.insert("__gizmo".into(), json!(if tab == 1 { "light" } else { "rotation" }));
|
||||
// The panel adds the Materials tab between Rotation and Lighting.
|
||||
let panel = d.bool("__panel");
|
||||
let tabs: Vec<&str> = if panel { vec![tl!("Rotation"), tl!("Materials"), tl!("Lighting")] } else { vec![tl!("Rotation"), tl!("Lighting")] };
|
||||
let current = if lighting { tabs.len() - 1 } else { usize::from(materials(d)) };
|
||||
if let Some(tab) = widgets::tab_bar(ui, &tabs, current) {
|
||||
let light = tab + 1 == tabs.len();
|
||||
d.fields.insert("__gizmo".into(), json!(if light { "light" } else { "rotation" }));
|
||||
d.fields.insert("__tab".into(), json!(if panel && tab == 1 && !light { "materials" } else { "rotation" }));
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
if super::revolve_gizmo::lighting(d) {
|
||||
lighting_controls(ui, d);
|
||||
return;
|
||||
}
|
||||
if materials(d) {
|
||||
super::three_d_materials::controls(app, ui, d);
|
||||
return;
|
||||
}
|
||||
let unit = app.session.general_unit();
|
||||
heading(ui, tl!("Shape"));
|
||||
number(ui, d, "angle", tl!("Revolve angle"), (0.0, 360.0), "°", 1, None);
|
||||
@@ -140,6 +158,11 @@ pub(crate) fn controls(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog)
|
||||
});
|
||||
}
|
||||
|
||||
/// Is the 3D and Materials panel's Materials tab open (the dialog has none)?
|
||||
pub(crate) fn materials(d: &Dialog) -> bool {
|
||||
d.bool("__panel") && d.str("__tab") == "materials" && !super::revolve_gizmo::lighting(d)
|
||||
}
|
||||
|
||||
/// `s` as the 3D effects mean it (a solid's cap, not a stroke's; a bevelled edge, not a join).
|
||||
pub(super) fn c3(s: &'static str) -> &'static str {
|
||||
crate::i18n::tr_ctx(crate::i18n::current(), "3d", s)
|
||||
@@ -155,18 +178,34 @@ fn solid_controls(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, id: &
|
||||
if object {
|
||||
tabs.push(tl!("Object"));
|
||||
}
|
||||
tabs.extend([tl!("Rotation"), tl!("Lighting")]);
|
||||
tabs.push(tl!("Rotation"));
|
||||
let rotation_at = tabs.len() - 1;
|
||||
// The panel adds the Materials tab before Lighting (Extrude & Bevel (Classic) has none).
|
||||
let materials_at = (d.bool("__panel") && id != CLASSIC_EXTRUDE).then(|| {
|
||||
tabs.push(tl!("Materials"));
|
||||
tabs.len() - 1
|
||||
});
|
||||
tabs.push(tl!("Lighting"));
|
||||
let current = if super::revolve_gizmo::lighting(d) {
|
||||
tabs.len() - 1
|
||||
} else if let Some(at) = materials_at.filter(|_| materials(d)) {
|
||||
at
|
||||
} else if object && d.str("__tab") != "rotation" {
|
||||
0
|
||||
} else {
|
||||
tabs.len() - 2
|
||||
rotation_at
|
||||
};
|
||||
if let Some(tab) = widgets::tab_bar(ui, &tabs, current) {
|
||||
let light = tab + 1 == tabs.len();
|
||||
d.fields.insert("__gizmo".into(), json!(if light { "light" } else { "rotation" }));
|
||||
d.fields.insert("__tab".into(), json!(if object && tab == 0 { "object" } else { "rotation" }));
|
||||
let kind = if object && tab == 0 {
|
||||
"object"
|
||||
} else if Some(tab) == materials_at {
|
||||
"materials"
|
||||
} else {
|
||||
"rotation"
|
||||
};
|
||||
d.fields.insert("__tab".into(), json!(kind));
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
if super::revolve_gizmo::lighting(d) {
|
||||
@@ -175,6 +214,8 @@ fn solid_controls(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, id: &
|
||||
} else {
|
||||
lighting_controls(ui, d);
|
||||
}
|
||||
} else if materials_at.is_some() && materials(d) {
|
||||
super::three_d_materials::controls(app, ui, d);
|
||||
} else if object && d.str("__tab") != "rotation" {
|
||||
let unit = app.session.general_unit();
|
||||
if id == CLASSIC_EXTRUDE {
|
||||
@@ -534,7 +575,11 @@ fn rail_width(ui: &egui::Ui) -> f32 {
|
||||
/// A value can be typed, dragged, scrubbed by its label, or moved on its slider.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(super) fn number(ui: &mut egui::Ui, d: &mut Dialog, key: &str, name: &str, range: (f64, f64), suffix: &str, decimals: usize, unit: Option<Unit>) {
|
||||
let default = vectorcraft_effects::default_params(&d.str("__effect")).and_then(|p| p.get(key).and_then(Value::as_f64)).unwrap_or(0.0);
|
||||
// Values that are not the effect's own parameters (a mapped graphic's) bring their defaults.
|
||||
let default = vectorcraft_effects::default_params(&d.str("__effect"))
|
||||
.and_then(|p| p.get(key).and_then(Value::as_f64))
|
||||
.or_else(|| d.fields.get("__defaults").and_then(|m| m.get(key)).and_then(Value::as_f64))
|
||||
.unwrap_or(0.0);
|
||||
let from = |n| unit.map_or(n, |u| u.from_pt(n));
|
||||
let to = |n| unit.map_or(n, |u| u.to_pt(n));
|
||||
let typed = unit.map(|u| widgets::typed_unit(ui.ctx(), u));
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
//! plus `preset`, `range`, `path?` and UI-only `__` keys, so agents fill the dialog with
|
||||
//! `ui.dialog.set`. OK runs `document.exportPdf` through [`io::export_pdf`]; the Summary is
|
||||
//! `document.pdfSettings`. Save Preset… names the settings as a preset (`pdf.preset.save`).
|
||||
//! Opened from Export As with Use Artboards off (`__artBounds`), the dialog has no Artboards
|
||||
//! section, and OK writes one page of the visible art's bounds through [`io::export`].
|
||||
//!
|
||||
//! The same dialog edits a preset (kind `pdfPreset`, from Edit → PDF Presets): a `name` and
|
||||
//! `description` instead of the preset and artboards, and OK saves the preset.
|
||||
@@ -45,6 +47,9 @@ pub(super) const PRESET_SPEC: DialogSpec = DialogSpec {
|
||||
const EDITING: &str = "__editing";
|
||||
/// Save PDF's field holding the name typed for Save Preset… (present while that row shows).
|
||||
const SAVE_AS: &str = "__savePresetAs";
|
||||
/// Save PDF's field that is true when the page is the bounds of the visible art (Export As with
|
||||
/// Use Artboards off) instead of the artboards.
|
||||
const ART_BOUNDS: &str = "__artBounds";
|
||||
|
||||
pub(super) const SECTIONS: [&str; 7] = ["General", "Compression", "Marks and Bleeds", "Output", "Advanced", "Security", "Summary"];
|
||||
|
||||
@@ -76,8 +81,13 @@ pub(super) const LIST_WIDTH: f32 = 150.0;
|
||||
pub(super) const TOP_LABEL_WIDTH: f32 = LIST_WIDTH + 26.0;
|
||||
|
||||
/// Open the dialog with `params` (`document.exportPdf` options and `path?`) applied over their
|
||||
/// preset.
|
||||
/// preset. `useArtboards: false` makes the page the bounds of the visible art.
|
||||
pub fn open(app: &mut VectorcraftApp, params: &Value) -> Result<Value, String> {
|
||||
let art_bounds = match params.get("useArtboards") {
|
||||
None | Some(Value::Null) => false,
|
||||
Some(Value::Bool(b)) => !b,
|
||||
Some(v) => return Err(format!("useArtboards must be true or false, not {v}")),
|
||||
};
|
||||
let mut fields = settings_fields(app, params)?;
|
||||
let s = |k: &str| params.get(k).and_then(Value::as_str);
|
||||
fields.insert("preset".into(), json!(s("preset").unwrap_or(pdf::DEFAULT_PRESET)));
|
||||
@@ -88,6 +98,9 @@ pub fn open(app: &mut VectorcraftApp, params: &Value) -> Result<Value, String> {
|
||||
if let Some(path) = s("path") {
|
||||
fields.insert("path".into(), json!(path));
|
||||
}
|
||||
if art_bounds {
|
||||
fields.insert(ART_BOUNDS.into(), json!(true));
|
||||
}
|
||||
app.ui.dialog = Some(Dialog { kind: KIND.into(), fields });
|
||||
Ok(Value::Null)
|
||||
}
|
||||
@@ -117,10 +130,14 @@ fn settings_fields(app: &VectorcraftApp, params: &Value) -> Result<Map<String, V
|
||||
/// The `document.exportPdf` params the dialog stands for.
|
||||
fn params(d: &Dialog) -> Value {
|
||||
let mut p = form::params(d);
|
||||
if (d.bool("__allArtboards") || d.str("range").trim().is_empty())
|
||||
&& let Some(o) = p.as_object_mut()
|
||||
{
|
||||
o.remove("range");
|
||||
let art = d.bool(ART_BOUNDS);
|
||||
if let Some(o) = p.as_object_mut() {
|
||||
if art || d.bool("__allArtboards") || d.str("range").trim().is_empty() {
|
||||
o.remove("range");
|
||||
}
|
||||
if art {
|
||||
o.insert("useArtboards".into(), json!(false));
|
||||
}
|
||||
}
|
||||
p
|
||||
}
|
||||
@@ -137,14 +154,24 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
}
|
||||
let r = match SaveMode::of(&d.str("__save")) {
|
||||
Some(mode) => io::save_pdf(app, mode, params(d)),
|
||||
None if d.bool(ART_BOUNDS) => export_art(app, params(d)),
|
||||
None => io::export_pdf(app, params(d)),
|
||||
};
|
||||
if r.is_ok() {
|
||||
// A question the write asked (replace the file the document was opened from?) stays open.
|
||||
if r.is_ok() && app.ui.dialog.as_ref().is_some_and(|o| o.kind == KIND) {
|
||||
app.ui.dialog = None;
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/// A page of the art's bounds (Export As with Use Artboards off): written as Export As writes
|
||||
/// other formats ([`io::export`]), with the dialog's options and a saved preset written out.
|
||||
fn export_art(app: &mut VectorcraftApp, mut p: Value) -> Result<Value, String> {
|
||||
let path = p.as_object_mut().and_then(|o| o.remove("path")).and_then(|v| v.as_str().map(str::to_string));
|
||||
let p = pdf::expand_preset(&app.session, "file.exportAs", &p).map_err(|e| e.to_string())?.into_owned();
|
||||
io::export(app, Some("pdf"), path, &p)
|
||||
}
|
||||
|
||||
/// The settings as `pdf.preset.save` params (without the artboards and path).
|
||||
fn preset_params(d: &Dialog) -> Value {
|
||||
let mut p = form::params(d);
|
||||
@@ -490,6 +517,10 @@ fn general(ui: &mut egui::Ui, d: &mut Dialog, editor: bool) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
// A page of the art's bounds has no artboards to choose.
|
||||
if d.bool(ART_BOUNDS) {
|
||||
return;
|
||||
}
|
||||
heading(ui, tl!("Artboards"));
|
||||
let mut all = d.bool("__allArtboards");
|
||||
ui.horizontal(|ui| {
|
||||
|
||||
@@ -92,6 +92,112 @@ fn confirm_runs_export_pdf_with_the_dialog_options() {
|
||||
assert!(app.ui.status.starts_with("Saved /tmp/out.pdf"), "{}", app.ui.status);
|
||||
}
|
||||
|
||||
/// Opened for a page of the art's bounds (Export As with Use Artboards off), the dialog has no
|
||||
/// Artboards section, and OK writes one page of the art's bounds with its options, a saved preset
|
||||
/// and View PDF after Saving included.
|
||||
#[test]
|
||||
fn a_page_of_the_arts_bounds_has_no_artboards_section_and_takes_the_options() {
|
||||
let (mut app, written, opened) = app();
|
||||
app.run("pdf.preset.save", json!({"name": "Mine", "compatibility": "1.5"})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
ctx.enable_accesskit();
|
||||
theme::install_fonts(&ctx);
|
||||
let has_range = |app: &mut VectorcraftApp| {
|
||||
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| super::super::show(app, ui.ctx()));
|
||||
out.textures_delta.clear();
|
||||
let nodes = &out.platform_output.accesskit_update.as_ref().expect("an accessibility tree").nodes;
|
||||
nodes.iter().any(|(_, n)| n.label() == Some("Range:") && n.role() == egui::accesskit::Role::RadioButton)
|
||||
};
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/artboards.pdf"})).unwrap();
|
||||
has_range(&mut app);
|
||||
assert!(has_range(&mut app), "the artboards' dialog has the section");
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/art.pdf", "preset": "Mine", "useArtboards": false})).unwrap();
|
||||
assert!(app.ui.dialog.as_ref().unwrap().bool(ART_BOUNDS));
|
||||
has_range(&mut app);
|
||||
assert!(!has_range(&mut app), "no artboards to choose");
|
||||
set_field(&mut app, "viewAfterSaving", json!(true));
|
||||
let r = super::super::confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none(), "closes after saving");
|
||||
assert_eq!(r["path"], "/tmp/art.pdf");
|
||||
let w = written.borrow();
|
||||
assert_eq!(w.len(), 1);
|
||||
assert!(w[0].1.starts_with(b"%PDF-1.5"), "the saved preset reaches the writer");
|
||||
let pdf = vectorcraft_pdf::import(&w[0].1).unwrap();
|
||||
assert_eq!(pdf.artboards.len(), 1);
|
||||
let size = pdf.artboards[0].rect.size();
|
||||
assert!((50.0..52.0).contains(&size.width) && (40.0..42.0).contains(&size.height), "the rectangle's bounds with its stroke: {size:?}");
|
||||
assert_eq!(opened.borrow().len(), 1, "View PDF after Saving opens the file");
|
||||
assert!(opened.borrow()[0].0.ends_with("/tmp/art.pdf"));
|
||||
}
|
||||
|
||||
/// `useArtboards` other than true, false or null is refused, as `document.exportPdf` refuses it,
|
||||
/// and no dialog opens.
|
||||
#[test]
|
||||
fn a_use_artboards_that_is_not_true_or_false_is_refused() {
|
||||
let (mut app, _, _) = app();
|
||||
for bad in [json!("false"), json!(0), json!({})] {
|
||||
let e = app.run("ui.savePdfDialog", json!({"path": "/tmp/x.pdf", "useArtboards": bad})).unwrap_err();
|
||||
assert!(e.contains("useArtboards must be true or false"), "{bad}: {e}");
|
||||
assert!(app.ui.dialog.is_none(), "{bad}: no dialog opens");
|
||||
}
|
||||
for ok in [json!(true), json!(null)] {
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/x.pdf", "useArtboards": ok})).unwrap();
|
||||
assert!(!app.ui.dialog.take().unwrap().bool(ART_BOUNDS), "{ok}: the artboards' dialog");
|
||||
}
|
||||
}
|
||||
|
||||
/// With Background Export on, a page of the art's bounds is written in the background, as Export
|
||||
/// As writes its other formats.
|
||||
#[test]
|
||||
fn a_page_of_the_arts_bounds_is_written_in_the_background() {
|
||||
let written = std::sync::Arc::new(std::sync::Mutex::new(Vec::<(String, Vec<u8>)>::new()));
|
||||
let w = written.clone();
|
||||
let services = Services {
|
||||
write_shared: Some(std::sync::Arc::new(move |p: &str, b: &[u8]| {
|
||||
w.lock().unwrap().push((p.to_string(), b.to_vec()));
|
||||
Ok(())
|
||||
})),
|
||||
..Default::default()
|
||||
};
|
||||
let mut app = VectorcraftApp::new(Session::new(), services);
|
||||
app.session.prefs.background_export = true;
|
||||
app.run("file.new", json!({"width": 120, "height": 90})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap();
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/bg.pdf", "useArtboards": false})).unwrap();
|
||||
let r = super::super::confirm(&mut app).unwrap();
|
||||
assert_eq!(r["background"], true, "{r}");
|
||||
assert!(app.ui.dialog.is_none());
|
||||
crate::background::wait_all(&mut app);
|
||||
let w = written.lock().unwrap();
|
||||
assert_eq!(w.len(), 1);
|
||||
assert_eq!(w[0].0, "/tmp/bg.pdf");
|
||||
assert_eq!(vectorcraft_pdf::import(&w[0].1).unwrap().artboards.len(), 1);
|
||||
}
|
||||
|
||||
/// A document without a title of its own, saved to a file, is titled after that file in the PDF,
|
||||
/// and viewers show the file name, with or without Use Artboards; one with its own title shows
|
||||
/// that title.
|
||||
#[test]
|
||||
fn a_page_of_the_arts_bounds_shows_the_title_only_when_it_is_the_documents_own() {
|
||||
let (mut app, written, _) = app();
|
||||
app.session.doc_mut().unwrap().path = Some("/docs/Poster.vectorcraft".into());
|
||||
for artboards in [true, false] {
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/t.pdf", "useArtboards": artboards, "compression": {"compressText": false}})).unwrap();
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
}
|
||||
std::sync::Arc::make_mut(&mut app.session.doc_mut().unwrap().doc).title = "Spring Sale".into();
|
||||
app.run("ui.savePdfDialog", json!({"path": "/tmp/t.pdf", "useArtboards": false, "compression": {"compressText": false}})).unwrap();
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
let w = written.borrow();
|
||||
assert_eq!(w.len(), 3);
|
||||
let texts: Vec<String> = w.iter().map(|(_, b)| String::from_utf8_lossy(b).into_owned()).collect();
|
||||
for (pdf, artboards) in texts[..2].iter().zip([true, false]) {
|
||||
assert!(pdf.contains("/Title(Poster)"), "useArtboards {artboards}");
|
||||
assert!(!pdf.contains("/DisplayDocTitle true"), "useArtboards {artboards}");
|
||||
}
|
||||
assert!(texts[2].contains("/Title(Spring Sale)") && texts[2].contains("/DisplayDocTitle true"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warnings_reach_the_status_and_errors_keep_the_dialog_open() {
|
||||
let (mut app, written, opened) = app();
|
||||
|
||||
@@ -190,7 +190,7 @@ fn an_empty_export_as_range_shows_an_example() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_continues_in_svg_options_and_pdf_art_is_written_directly() {
|
||||
fn svg_continues_in_svg_options() {
|
||||
let (mut app, written) = app(2);
|
||||
app.run("file.exportAs", json!({"format": "svg", "useArtboards": true})).unwrap();
|
||||
confirm(&mut app).unwrap();
|
||||
@@ -201,12 +201,35 @@ fn svg_continues_in_svg_options_and_pdf_art_is_written_directly() {
|
||||
confirm(&mut app).unwrap();
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(names(&written), ["/out/Untitled-1-Artboard-1.svg", "/out/Untitled-1-Artboard-2.svg"]);
|
||||
// Without artboards: one file of the art's bounds, with no options dialog for a PDF.
|
||||
}
|
||||
|
||||
/// Without Use Artboards, a PDF continues in Save PDF too, without its artboard choice, and the
|
||||
/// file is one page of the art's bounds. Its options apply: a drawing 10 m long is written at
|
||||
/// full size in points with "Write pages longer than 200 in at full size in points".
|
||||
#[test]
|
||||
fn pdf_art_continues_in_the_save_pdf_dialog() {
|
||||
let (mut app, written) = app(2);
|
||||
app.run("file.exportAs", json!({"format": "pdf"})).unwrap();
|
||||
let r = confirm(&mut app).unwrap();
|
||||
assert_eq!(r["path"], "/out/Untitled-1.pdf");
|
||||
let pdf = vectorcraft_pdf::import(&written.borrow()[2].1).unwrap();
|
||||
confirm(&mut app).unwrap();
|
||||
assert_eq!(kind(&app), Some("savePdf"), "the PDF options follow");
|
||||
assert_eq!(app.ui.dialog.as_ref().unwrap().str("path"), "/out/Untitled-1.pdf");
|
||||
assert!(written.borrow().is_empty());
|
||||
frame(&mut app);
|
||||
assert_eq!(confirm(&mut app).unwrap()["path"], "/out/Untitled-1.pdf");
|
||||
let pdf = vectorcraft_pdf::import(&written.borrow()[0].1).unwrap();
|
||||
assert_eq!(pdf.artboards.len(), 1);
|
||||
assert_eq!((pdf.artboards[0].rect.width().round(), pdf.artboards[0].rect.height().round()), (30.0, 20.0), "the ellipse's bounds");
|
||||
|
||||
app.run("shape.rectangle", json!({"x": 0, "y": 100, "width": 28_346.46, "height": 50})).unwrap();
|
||||
app.run("file.exportAs", json!({"format": "pdf"})).unwrap();
|
||||
confirm(&mut app).unwrap();
|
||||
set(&mut app, "longPagesInPoints", json!(true));
|
||||
frame(&mut app);
|
||||
confirm(&mut app).unwrap();
|
||||
let bytes = written.borrow()[1].1.clone();
|
||||
assert!(!String::from_utf8_lossy(&bytes).contains("/UserUnit"));
|
||||
let width = vectorcraft_pdf::import(&bytes).unwrap().artboards[0].rect.width();
|
||||
assert!((28_346.0..28_350.0).contains(&width), "{width}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -330,8 +353,17 @@ fn the_enter_that_closed_the_save_panel_does_not_confirm_the_options() {
|
||||
let mut out = ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| show(app, ui.ctx()));
|
||||
out.textures_delta.clear();
|
||||
};
|
||||
// Each format whose options follow the panel (PDF's with Use Artboards: Save PDF).
|
||||
let formats = [("png", false), ("svg", false), ("pdf", true), ("txt", false), ("dxf", false), ("eps", false), ("plugin.test.ui.held", false)];
|
||||
// Each format whose options follow the panel (PDF's: Save PDF, with Use Artboards on or off).
|
||||
let formats = [
|
||||
("png", false),
|
||||
("svg", false),
|
||||
("pdf", true),
|
||||
("pdf", false),
|
||||
("txt", false),
|
||||
("dxf", false),
|
||||
("eps", false),
|
||||
("plugin.test.ui.held", false),
|
||||
];
|
||||
for (format, artboards) in formats {
|
||||
let (mut app, written) = app(1);
|
||||
app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&module)})).unwrap();
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
//! The 3D and Materials panel's Materials tab: the document's Graphics (its symbols) as thumbnails,
|
||||
//! Add as Single Graphic, and the graphics mapped onto the selected 3D object's planar faces with
|
||||
//! their face, position, size and turn. Picking a thumbnail maps that graphic onto the selected
|
||||
//! objects (`threeD.mapGraphic`); art dropped on the thumbnails becomes a graphic
|
||||
//! (`threeD.addGraphic`). Editing a mapped graphic changes the effect's `graphics` parameter, which
|
||||
//! the panel sends as it sends every other setting.
|
||||
|
||||
use egui::{Sense, Stroke, StrokeKind, vec2};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_effects::{GRAPHICS, MappedGraphic, PlanarFace, ROTATE};
|
||||
|
||||
use super::revolve::{heading, label, number};
|
||||
use crate::VectorcraftApp;
|
||||
use crate::state::Dialog;
|
||||
use crate::theme::Tokens;
|
||||
use crate::widgets::{self, dim_label};
|
||||
|
||||
/// The field of [`Dialog::fields`] that holds what the panel runs after drawing the tab
|
||||
/// ([`Action`]), as JSON. It is UI state and never enters the effect's parameters.
|
||||
pub(crate) const ACTION: &str = "__graphicAction";
|
||||
/// The field that holds the index of the mapped graphic the tab edits.
|
||||
pub(crate) const SELECTED: &str = "__graphic";
|
||||
|
||||
/// What a click in the Graphics list asks the panel to do.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum Action {
|
||||
/// Map the graphic of this symbol onto the selected 3D objects.
|
||||
Map(String),
|
||||
/// Keep art as a new graphic: the objects dropped on the list, or the selection (`None`).
|
||||
Add(Option<Vec<u64>>),
|
||||
}
|
||||
|
||||
impl Action {
|
||||
pub(crate) fn to_value(&self) -> Value {
|
||||
match self {
|
||||
Action::Map(name) => json!({"map": name}),
|
||||
Action::Add(ids) => json!({"add": ids}),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_value(v: &Value) -> Option<Self> {
|
||||
if let Some(name) = v.get("map").and_then(Value::as_str) {
|
||||
return Some(Action::Map(name.to_string()));
|
||||
}
|
||||
let ids = v.get("add")?;
|
||||
Some(Action::Add(ids.as_array().map(|a| a.iter().filter_map(Value::as_u64).collect())))
|
||||
}
|
||||
}
|
||||
|
||||
/// The document's graphics as thumbnails, with Add as Single Graphic below them. A thumbnail
|
||||
/// clicked while `apply` (a 3D object is selected) maps that graphic. Returns the action asked for.
|
||||
pub(crate) fn library(app: &VectorcraftApp, ui: &mut egui::Ui, apply: bool) -> Option<Action> {
|
||||
heading(ui, tl!("Graphics"));
|
||||
let st = app.session.active()?;
|
||||
let names: Vec<String> = st.doc.symbols.iter().map(|s| s.name.clone()).collect();
|
||||
let keys = crate::panels::symbols::document_symbols(st);
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let mut action = None;
|
||||
let area = ui
|
||||
.horizontal_wrapped(|ui| {
|
||||
for name in &names {
|
||||
let (r, resp) = ui.allocate_exact_size(vec2(44.0, 44.0), Sense::click());
|
||||
crate::panels::symbols::chip(ui, &st.doc, &keys, r.shrink(2.0), name);
|
||||
ui.painter().rect_stroke(r, 0.0, Stroke::new(1.0, t.border), StrokeKind::Inside);
|
||||
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), name));
|
||||
let tip = if apply { name.clone() } else { format!("{name}: {}", tl!("Select a 3D object to map the graphic onto it.")) };
|
||||
if resp.on_hover_text(tip).clicked() && apply {
|
||||
action = Some(Action::Map(name.clone()));
|
||||
}
|
||||
}
|
||||
if names.is_empty() {
|
||||
dim_label(ui, tl!("Add art with Add as Single Graphic, or drop art here."));
|
||||
}
|
||||
})
|
||||
.response
|
||||
.rect;
|
||||
let zone = ui.interact(area, ui.scope_id().with("three-d-graphics-drop"), Sense::hover());
|
||||
if let Some(ids) = widgets::art_drop(ui, &zone) {
|
||||
action = Some(Action::Add(Some(ids)));
|
||||
}
|
||||
let selected = !st.selection.objects.is_empty();
|
||||
if ui.add_enabled(selected, egui::Button::new(tl!("Add as Single Graphic"))).clicked() {
|
||||
action = Some(Action::Add(None));
|
||||
}
|
||||
action
|
||||
}
|
||||
|
||||
/// The Materials tab of the 3D effect in `d.fields["__effect"]`: the Graphics list, then the
|
||||
/// graphics mapped onto the object, the selected one with its settings. Edits `d.fields`; a click
|
||||
/// in the list is left in [`ACTION`].
|
||||
pub(crate) fn controls(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
|
||||
let effect = d.str("__effect");
|
||||
let takes = vectorcraft_effects::takes_graphics(&effect);
|
||||
if let Some(action) = library(app, ui, takes) {
|
||||
d.fields.insert(ACTION.into(), action.to_value());
|
||||
}
|
||||
ui.add_space(10.0);
|
||||
if !takes {
|
||||
dim_label(ui, tl!("Graphics map onto planar faces: pick the Plane or Extrude object type."));
|
||||
return;
|
||||
}
|
||||
let raw = d.fields.get(GRAPHICS).cloned().unwrap_or(Value::Null);
|
||||
if raw.is_null() && d.fields.contains_key(GRAPHICS) {
|
||||
dim_label(ui, tl!("The selected objects have different graphics."));
|
||||
return;
|
||||
}
|
||||
let mut list: Vec<MappedGraphic> = vectorcraft_effects::mapped_graphics(&json!({ GRAPHICS: raw }));
|
||||
heading(ui, tl!("Mapped graphics"));
|
||||
if list.is_empty() {
|
||||
dim_label(ui, tl!("Pick a graphic above to map it onto the object."));
|
||||
return;
|
||||
}
|
||||
let mut index = d.fields.get(SELECTED).and_then(Value::as_u64).map_or(0, |i| i as usize).min(list.len() - 1);
|
||||
for (i, g) in list.iter().enumerate() {
|
||||
let face = if g.face == PlanarFace::Back { tl!("Back") } else { tl!("Front") };
|
||||
if ui.selectable_label(i == index, format!("{} · {face}", g.symbol)).clicked() {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
d.fields.insert(SELECTED.into(), json!(index));
|
||||
ui.add_space(6.0);
|
||||
let Some(g) = list.get(index).cloned() else { return };
|
||||
let mut edited = g.clone();
|
||||
if effect != ROTATE {
|
||||
ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = 6.0;
|
||||
label(ui, tl!("Face"));
|
||||
let labels = [tl!("Front"), tl!("Back")];
|
||||
let at = usize::from(g.face == PlanarFace::Back);
|
||||
if let Some(k) = widgets::dropdown(ui, "three-d-graphic-face", labels[at], &labels, 120.0) {
|
||||
edited.face = PlanarFace::ALL[k.min(1)];
|
||||
}
|
||||
});
|
||||
}
|
||||
let unit = app.session.general_unit();
|
||||
let mut fields = Dialog { kind: "threeDGraphic".into(), fields: Default::default() };
|
||||
fields.fields.insert("__defaults".into(), json!({"graphicX": 0.0, "graphicY": 0.0, "graphicScale": 100.0, "graphicRotation": 0.0}));
|
||||
fields.fields.insert("graphicX".into(), json!(g.x));
|
||||
fields.fields.insert("graphicY".into(), json!(g.y));
|
||||
fields.fields.insert("graphicScale".into(), json!(g.scale));
|
||||
fields.fields.insert("graphicRotation".into(), json!(g.rotation));
|
||||
// The ranges the effect takes, so every stored value shows as it is.
|
||||
let offsets = (-vectorcraft_effects::GRAPHIC_MAX_OFFSET, vectorcraft_effects::GRAPHIC_MAX_OFFSET);
|
||||
number(ui, &mut fields, "graphicX", "X", offsets, "", 1, Some(unit));
|
||||
number(ui, &mut fields, "graphicY", "Y", offsets, "", 1, Some(unit));
|
||||
number(ui, &mut fields, "graphicScale", tl!("Scale"), vectorcraft_effects::GRAPHIC_SCALE_RANGE, "%", 1, None);
|
||||
number(ui, &mut fields, "graphicRotation", tl!("Rotation"), (-360.0, 360.0), "°", 1, None);
|
||||
edited.x = fields.f64("graphicX", g.x);
|
||||
edited.y = fields.f64("graphicY", g.y);
|
||||
edited.scale = fields.f64("graphicScale", g.scale);
|
||||
edited.rotation = fields.f64("graphicRotation", g.rotation);
|
||||
let remove = ui.button(tl!("Remove Graphic")).clicked();
|
||||
if remove {
|
||||
list.remove(index);
|
||||
d.fields.insert(SELECTED.into(), json!(index.saturating_sub(1)));
|
||||
} else if edited != g {
|
||||
list[index] = edited;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
d.fields.insert(GRAPHICS.into(), Value::Array(list.iter().map(MappedGraphic::to_value).collect()));
|
||||
}
|
||||
@@ -2103,6 +2103,7 @@ font-technology Variable Променливи
|
||||
Art Brush Артистична четка
|
||||
Bristle Brush Четка с косъм
|
||||
Pattern Brush Четка с мостра
|
||||
missing from the Brushes panel липсва в панела „Четки“
|
||||
Select art to make a scatter, art or pattern brush. Изберете графика, за да създадете разпръсната четка, артистична четка или четка с мостра.
|
||||
Brush Art Графика на четка
|
||||
New Calligraphic Brush Нова калиграфска четка
|
||||
@@ -2542,6 +2543,8 @@ font-technology Variable Променливи
|
||||
Proof Setup Настройка на пробен печат
|
||||
Properties Свойства
|
||||
Proportional Metrics Пропорционални метрики
|
||||
Insert Aki Left Вмъкване на аки отляво
|
||||
Insert Aki Right Вмъкване на аки отдясно
|
||||
Character Direction Посока на знаците
|
||||
Digits Цифри
|
||||
Arabic Арабски
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1558,6 +1558,8 @@ lighting Light Svjetlo
|
||||
Leading Prored
|
||||
No Break Bez preloma
|
||||
Proportional Metrics Proporcionalna metrika
|
||||
Insert Aki Left Umetni aki lijevo
|
||||
Insert Aki Right Umetni aki desno
|
||||
Character Direction Smjer znakova
|
||||
Digits Cifre
|
||||
Arabic Arapski
|
||||
@@ -3322,6 +3324,7 @@ theme Appearance Izgled
|
||||
Art Brush Umjetnička četkica
|
||||
Bristle Brush Četkica s čekinjama
|
||||
Pattern Brush Četkica uzorka
|
||||
missing from the Brushes panel nedostaje u panelu Četkice
|
||||
Select art to make a scatter, art or pattern brush. Odaberite crtež da napravite raspršenu, umjetničku četkicu ili četkicu uzorka.
|
||||
Brush Art Crtež četkice
|
||||
New Symbol Novi simbol
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -3090,6 +3090,7 @@ digits Malayalam Malaiàlam
|
||||
Pattern Motiu
|
||||
Pattern Borders Vores de motiu
|
||||
Pattern Brush Pinzell de motiu
|
||||
missing from the Brushes panel absent del tauler Pinzells
|
||||
Pattern Brush Options Opcions del pinzell de motiu
|
||||
Pattern Editing Edició de motius
|
||||
Pattern Options Opcions del motiu
|
||||
@@ -3304,6 +3305,8 @@ digits Malayalam Malaiàlam
|
||||
Proof Setup Configuració de prova
|
||||
Properties Propietats
|
||||
Proportional Metrics Mètriques proporcionals
|
||||
Insert Aki Left Insereix aki a l'esquerra
|
||||
Insert Aki Right Insereix aki a la dreta
|
||||
Proportional Lining Xifres proporcionals alineades
|
||||
Position: Posició:
|
||||
Default Position Posició predeterminada
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1586,6 +1586,8 @@ lighting Light Světlo
|
||||
Leading Proklad
|
||||
No Break Nezalamovat
|
||||
Proportional Metrics Proporcionální metrika
|
||||
Insert Aki Left Vložit aki vlevo
|
||||
Insert Aki Right Vložit aki vpravo
|
||||
Character Direction Směr znaků
|
||||
Digits Číslice
|
||||
Arabic Arabské
|
||||
@@ -3372,6 +3374,7 @@ theme Appearance Motiv
|
||||
Art Brush Obrazový štětec
|
||||
Bristle Brush Štětinový štětec
|
||||
Pattern Brush Vzorkový štětec
|
||||
missing from the Brushes panel chybí v panelu Štětce
|
||||
Select art to make a scatter, art or pattern brush. Vyberte kresbu, abyste mohli vytvořit rozptylový, obrazový nebo vzorkový štětec.
|
||||
Brush Art Kresba štětce
|
||||
New Symbol Nový symbol
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1494,6 +1494,8 @@ lighting Light Lys
|
||||
Leading Linjeafstand
|
||||
No Break Ingen ombrydning
|
||||
Proportional Metrics Proportionale metrikker
|
||||
Insert Aki Left Indsæt aki til venstre
|
||||
Insert Aki Right Indsæt aki til højre
|
||||
Character Direction Tegnretning
|
||||
Digits Cifre
|
||||
Arabic Arabisk
|
||||
@@ -3107,6 +3109,7 @@ theme Appearance Udseende
|
||||
Art Brush Grafikpensel
|
||||
Bristle Brush Børstepensel
|
||||
Pattern Brush Mønsterpensel
|
||||
missing from the Brushes panel mangler i panelet Pensler
|
||||
Select art to make a scatter, art or pattern brush. Markér grafik for at oprette en spredningspensel, grafikpensel eller mønsterpensel.
|
||||
Brush Art Penselgrafik
|
||||
New Symbol Nyt symbol
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -2137,6 +2137,7 @@ file-filter Templates Vorlagen
|
||||
Art Brush Bildpinsel
|
||||
Bristle Brush Borstenpinsel
|
||||
Pattern Brush Musterpinsel
|
||||
missing from the Brushes panel fehlt im Bedienfeld „Pinsel“
|
||||
Select art to make a scatter, art or pattern brush. Wählen Sie eine Grafik aus, um einen Spezial-, Bild- oder Musterpinsel zu erstellen.
|
||||
Brush Art Pinselgrafik
|
||||
New Calligraphic Brush Neuer Kalligrafiepinsel
|
||||
@@ -2586,6 +2587,8 @@ mark-style Japanese Japanisch
|
||||
Proof Setup Proof einrichten
|
||||
Properties Eigenschaften
|
||||
Proportional Metrics Proportionale Metrik
|
||||
Insert Aki Left Aki links einfügen
|
||||
Insert Aki Right Aki rechts einfügen
|
||||
Character Direction Zeichenrichtung
|
||||
Digits Ziffern
|
||||
Arabic Arabisch
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1814,6 +1814,7 @@ font-technology Variable Μεταβλητές
|
||||
Art Brush Πινέλο σχεδίου
|
||||
Bristle Brush Πινέλο με τρίχες
|
||||
Pattern Brush Πινέλο μοτίβου
|
||||
missing from the Brushes panel λείπει από τον πίνακα Πινέλα
|
||||
Select art to make a scatter, art or pattern brush. Επιλέξτε σχέδιο για να δημιουργήσετε πινέλο διασποράς, σχεδίου ή μοτίβου.
|
||||
Brush Art Σχέδιο πινέλου
|
||||
New Layer… Νέο επίπεδο…
|
||||
@@ -3284,6 +3285,8 @@ theme Appearance Εμφάνιση
|
||||
Profile inclusion: Συμπερίληψη προφίλ:
|
||||
Projecting Cap Προεξέχον άκρο
|
||||
Proportional Metrics Αναλογικές μετρικές
|
||||
Insert Aki Left Εισαγωγή aki αριστερά
|
||||
Insert Aki Right Εισαγωγή aki δεξιά
|
||||
Arabic Αραβικά
|
||||
Hindi Χίντι
|
||||
Farsi Περσικά
|
||||
|
||||
|
Can't render this file because it is too large.
|
@@ -2175,6 +2175,7 @@ font-technology Variable Variables
|
||||
Art Brush Pincel de arte
|
||||
Bristle Brush Pincel de cerdas
|
||||
Pattern Brush Pincel de motivo
|
||||
missing from the Brushes panel falta en el panel Pinceles
|
||||
Select art to make a scatter, art or pattern brush. Selecciona una ilustración para crear un pincel de dispersión, de arte o de motivo.
|
||||
Brush Art Ilustración del pincel
|
||||
New Calligraphic Brush Nuevo pincel caligráfico
|
||||
@@ -2620,6 +2621,8 @@ font-technology Variable Variables
|
||||
Proof Setup Ajuste de prueba
|
||||
Properties Propiedades
|
||||
Proportional Metrics Métrica proporcional
|
||||
Insert Aki Left Insertar aki a la izquierda
|
||||
Insert Aki Right Insertar aki a la derecha
|
||||
Character Direction Dirección de caracteres
|
||||
Digits Dígitos
|
||||
Arabic Árabes
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1561,6 +1561,8 @@ lighting Light Valo
|
||||
Leading Riviväli
|
||||
No Break Ei katkoa
|
||||
Proportional Metrics Suhteelliset metriikat
|
||||
Insert Aki Left Lisää aki vasemmalle
|
||||
Insert Aki Right Lisää aki oikealle
|
||||
Character Direction Merkkien suunta
|
||||
Digits Numerot
|
||||
Arabic Arabia
|
||||
@@ -3311,6 +3313,7 @@ theme Appearance Ulkoasu
|
||||
Art Brush Taidesivellin
|
||||
Bristle Brush Harjassivellin
|
||||
Pattern Brush Kuviosivellin
|
||||
missing from the Brushes panel puuttuu Siveltimet-paneelista
|
||||
Select art to make a scatter, art or pattern brush. Valitse kuvitus, jotta voit luoda sirote-, taide- tai kuviosiveltimen.
|
||||
Brush Art Siveltimen kuvitus
|
||||
New Symbol Uusi symboli
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -2167,6 +2167,7 @@ font-technology Variable Variables
|
||||
Art Brush Forme artistique
|
||||
Bristle Brush Forme Soies
|
||||
Pattern Brush Forme de motif
|
||||
missing from the Brushes panel absente du panneau Formes
|
||||
Select art to make a scatter, art or pattern brush. Sélectionnez une illustration pour créer une forme diffuse, artistique ou de motif.
|
||||
Brush Art Illustration de la forme
|
||||
New Calligraphic Brush Nouvelle forme calligraphique
|
||||
@@ -2612,6 +2613,8 @@ font-technology Variable Variables
|
||||
Proof Setup Format d’épreuve
|
||||
Properties Propriétés
|
||||
Proportional Metrics Métrique proportionnelle
|
||||
Insert Aki Left Insérer un aki à gauche
|
||||
Insert Aki Right Insérer un aki à droite
|
||||
Character Direction Direction des caractères
|
||||
Digits Chiffres
|
||||
Arabic Arabes
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -3356,6 +3356,7 @@ antiAlias Sharp शार्प
|
||||
Pattern पैटर्न
|
||||
Pattern Borders पैटर्न सीमा
|
||||
Pattern Brush पैटर्न ब्रश
|
||||
missing from the Brushes panel ब्रश पैनल में अनुपलब्ध
|
||||
Pattern Brush Options पैटर्न ब्रश ऑप्शन्स
|
||||
Pattern Editing पैटर्न संपादन
|
||||
Pattern Options पैटर्न ऑप्शन्स
|
||||
@@ -3607,6 +3608,8 @@ antiAlias Sharp शार्प
|
||||
Properties प्रॉपर्टीज़
|
||||
Proportional Lining आनुपातिक लाइनिंग
|
||||
Proportional Metrics आनुपातिक मीट्रिक
|
||||
Insert Aki Left बाईं ओर आकी जोड़ें
|
||||
Insert Aki Right दाईं ओर आकी जोड़ें
|
||||
Proportional Oldstyle आनुपातिक ओल्डस्टाइल
|
||||
Protect Corners कॉर्नर को सुरक्षित रखें
|
||||
Proximity Guides निकटता गाइड
|
||||
|
||||
|
Can't render this file because it is too large.
|
@@ -2984,6 +2984,7 @@ file-filter Templates Sablonok
|
||||
Paths: {paths} Anchors: {anchors} Colors: {colors} Útvonalak: {paths} Horgonypontok: {anchors} Színek: {colors}
|
||||
Pattern Mintázat
|
||||
Pattern Brush Mintázatecset
|
||||
missing from the Brushes panel hiányzik az Ecsetek panelről
|
||||
Pattern Brush Options Mintázatecset beállításai
|
||||
Pattern Editing Mintázatszerkesztés
|
||||
Pattern Options Mintázat beállításai
|
||||
@@ -3170,6 +3171,8 @@ mark-style Japanese Japán
|
||||
Proof Setup Színpróba beállítása
|
||||
Properties Tulajdonságok
|
||||
Proportional Metrics Arányos metrika
|
||||
Insert Aki Left Aki beszúrása balra
|
||||
Insert Aki Right Aki beszúrása jobbra
|
||||
Proportional Lining Arányos modern
|
||||
Position: Pozíció:
|
||||
Default Position Alapértelmezett pozíció
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1494,6 +1494,8 @@ lighting Light Cahaya
|
||||
Leading Leading
|
||||
No Break Tanpa pemenggalan
|
||||
Proportional Metrics Metrik proporsional
|
||||
Insert Aki Left Sisipkan aki kiri
|
||||
Insert Aki Right Sisipkan aki kanan
|
||||
Character Direction Arah karakter
|
||||
Digits Digit
|
||||
Arabic Arab
|
||||
@@ -3107,6 +3109,7 @@ theme Appearance Tampilan
|
||||
Art Brush Kuas seni
|
||||
Bristle Brush Kuas bristle
|
||||
Pattern Brush Kuas pola
|
||||
missing from the Brushes panel tidak ada di panel Kuas
|
||||
Select art to make a scatter, art or pattern brush. Pilih karya seni untuk membuat kuas sebar, seni, atau pola.
|
||||
Brush Art Karya seni kuas
|
||||
New Symbol Simbol baru
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2405 and column 5.
|
@@ -2213,6 +2213,7 @@ file-filter Templates Modelli
|
||||
Art Brush Pennello artistico
|
||||
Bristle Brush Pennello a setole
|
||||
Pattern Brush Pennello motivo
|
||||
missing from the Brushes panel mancante nel pannello Pennelli
|
||||
Select art to make a scatter, art or pattern brush. Seleziona la grafica per creare un pennello a dispersione, artistico o motivo.
|
||||
Brush Art Grafica del pennello
|
||||
New Calligraphic Brush Nuovo pennello calligrafico
|
||||
@@ -2662,6 +2663,8 @@ mark-style Japanese Giapponese
|
||||
Proof Setup Impostazione prova
|
||||
Properties Proprietà
|
||||
Proportional Metrics Metrica proporzionale
|
||||
Insert Aki Left Inserisci aki a sinistra
|
||||
Insert Aki Right Inserisci aki a destra
|
||||
Character Direction Direzione caratteri
|
||||
Digits Cifre
|
||||
Arabic Arabe
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -50,6 +50,8 @@ font-technology Variable バリアブル
|
||||
x-Height x ハイト
|
||||
ICF Box 平均字面
|
||||
Proportional Metrics プロポーショナルメトリクス
|
||||
Insert Aki Left 前にアキを挿入
|
||||
Insert Aki Right 後ろにアキを挿入
|
||||
Character Direction 文字の方向
|
||||
Digits 数字
|
||||
Arabic アラビア数字
|
||||
@@ -2420,6 +2422,7 @@ antiAlias Sharp シャープ
|
||||
Art Brush アートブラシ
|
||||
Bristle Brush 剛毛ブラシ
|
||||
Pattern Brush パターンブラシ
|
||||
missing from the Brushes panel ブラシパネルにありません
|
||||
Select art to make a scatter, art or pattern brush. 散布ブラシ、アートブラシ、パターンブラシを作成するには、アートを選択します。
|
||||
Brush Art ブラシのアート
|
||||
New Calligraphic Brush 新規絵筆ブラシ
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1431,6 +1431,8 @@ lighting Light სინათლე
|
||||
Leading სტრიქონთა დაშორება
|
||||
No Break გადატანის გარეშე
|
||||
Proportional Metrics პროპორციული მეტრიკა
|
||||
Insert Aki Left აკის ჩასმა მარცხნივ
|
||||
Insert Aki Right აკის ჩასმა მარჯვნივ
|
||||
Character Direction სიმბოლოების მიმართულება
|
||||
Digits ციფრები
|
||||
Arabic არაბული
|
||||
@@ -2950,6 +2952,7 @@ theme Appearance იერსახე
|
||||
Art Brush მხატვრული ფუნჯი
|
||||
Bristle Brush ჯაგრის ფუნჯი
|
||||
Pattern Brush პატერნის ფუნჯი
|
||||
missing from the Brushes panel ფუნჯების პანელში არ არის
|
||||
Select art to make a scatter, art or pattern brush. მონიშნეთ ნახატი გაფანტვის, მხატვრული ან პატერნის ფუნჯის შესაქმნელად.
|
||||
Brush Art ფუნჯის ნახატი
|
||||
New Symbol ახალი სიმბოლო
|
||||
|
||||
|
Can't render this file because it is too large.
|
@@ -44,6 +44,8 @@ font-technology Variable 가변
|
||||
x-Height x 높이
|
||||
ICF Box ICF 박스
|
||||
Proportional Metrics 비례 메트릭
|
||||
Insert Aki Left 앞 아키 삽입
|
||||
Insert Aki Right 뒤 아키 삽입
|
||||
Character Direction 문자 방향
|
||||
Digits 숫자
|
||||
Arabic 아라비아어
|
||||
@@ -2410,6 +2412,7 @@ antiAlias Sharp 날카롭게
|
||||
Art Brush 아트 브러시
|
||||
Bristle Brush 강모 브러시
|
||||
Pattern Brush 패턴 브러시
|
||||
missing from the Brushes panel 브러시 패널에 없음
|
||||
Select art to make a scatter, art or pattern brush. 흩뿌리기, 아트 또는 패턴 브러시를 만들려면 아트웍을 선택하세요.
|
||||
Brush Art 브러시 아트
|
||||
New Calligraphic Brush 새 캘리그래피 브러시
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1508,6 +1508,8 @@ lighting Light Licht
|
||||
Leading Regelafstand
|
||||
No Break Niet afbreken
|
||||
Proportional Metrics Proportionele metingen
|
||||
Insert Aki Left Aki links invoegen
|
||||
Insert Aki Right Aki rechts invoegen
|
||||
Character Direction Tekenrichting
|
||||
Digits Cijfers
|
||||
Arabic Arabisch
|
||||
@@ -3121,6 +3123,7 @@ theme Appearance Vormgeving
|
||||
Art Brush Artpenseel
|
||||
Bristle Brush Borstelpenseel
|
||||
Pattern Brush Patroonpenseel
|
||||
missing from the Brushes panel ontbreekt in het deelvenster Penselen
|
||||
Select art to make a scatter, art or pattern brush. Selecteer illustratie om een verspreidings-, art- of patroonpenseel te maken.
|
||||
Brush Art Penseelillustratie
|
||||
New Symbol Nieuw symbool
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1499,6 +1499,8 @@ lighting Light Światło
|
||||
Leading Interlinia
|
||||
No Break Bez podziału
|
||||
Proportional Metrics Metryka proporcjonalna
|
||||
Insert Aki Left Wstaw aki z lewej
|
||||
Insert Aki Right Wstaw aki z prawej
|
||||
Character Direction Kierunek znaków
|
||||
Digits Cyfry
|
||||
Arabic Arabskie
|
||||
@@ -3251,6 +3253,7 @@ theme Appearance Wygląd
|
||||
Art Brush Pędzel artystyczny
|
||||
Bristle Brush Pędzel włosiankowy
|
||||
Pattern Brush Pędzel wzorka
|
||||
missing from the Brushes panel brak w panelu Pędzle
|
||||
Select art to make a scatter, art or pattern brush. Zaznacz kompozycję, aby utworzyć pędzel rozproszony, artystyczny lub wzorka.
|
||||
Brush Art Kompozycja pędzla
|
||||
New Symbol Nowy symbol
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -1788,6 +1788,7 @@ antiAlias Sharp Nítido
|
||||
Art Brush Pincel de Arte
|
||||
Bristle Brush Pincel de Cerdas
|
||||
Pattern Brush Pincel de Padrão
|
||||
missing from the Brushes panel ausente do painel Pincéis
|
||||
Select art to make a scatter, art or pattern brush. Selecione arte para criar um pincel de dispersão, de arte ou de padrão.
|
||||
Brush Art Arte do Pincel
|
||||
New Calligraphic Brush Novo Pincel Caligráfico
|
||||
@@ -2190,6 +2191,8 @@ antiAlias Sharp Nítido
|
||||
Proof Setup Configuração de Prova
|
||||
Properties Propriedades
|
||||
Proportional Metrics Métrica Proporcional
|
||||
Insert Aki Left Inserir Aki à Esquerda
|
||||
Insert Aki Right Inserir Aki à Direita
|
||||
Character Direction Direção de Caracteres
|
||||
Digits Dígitos
|
||||
Arabic Arábicos
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 6 and column 47.
|
@@ -2200,6 +2200,7 @@ file-filter Templates Шаблоны
|
||||
Art Brush Художественная кисть
|
||||
Bristle Brush Щетинная кисть
|
||||
Pattern Brush Узорная кисть
|
||||
missing from the Brushes panel отсутствует в панели «Кисти»
|
||||
Select art to make a scatter, art or pattern brush. Выделите рисунок, чтобы создать кисть-распыление, художественную или узорную кисть.
|
||||
Brush Art Рисунок кисти
|
||||
New Calligraphic Brush Новая каллиграфическая кисть
|
||||
@@ -2649,6 +2650,8 @@ mark-style Japanese Японский
|
||||
Proof Setup Настройка цветопробы
|
||||
Properties Свойства
|
||||
Proportional Metrics Пропорциональные метрики
|
||||
Insert Aki Left Вставить аки слева
|
||||
Insert Aki Right Вставить аки справа
|
||||
Character Direction Направление символов
|
||||
Digits Цифры
|
||||
Arabic Арабские
|
||||
|
||||
|
Can't render this file because it is too large.
|
@@ -2114,6 +2114,7 @@ font-technology Variable Variabla
|
||||
Art Brush Konstpensel
|
||||
Bristle Brush Borstpensel
|
||||
Pattern Brush Mönsterpensel
|
||||
missing from the Brushes panel saknas i panelen Penslar
|
||||
Select art to make a scatter, art or pattern brush. Markera grafik för att skapa en spridnings-, konst- eller mönsterpensel.
|
||||
Brush Art Penselgrafik
|
||||
New Calligraphic Brush Ny kalligrafisk pensel
|
||||
@@ -2557,6 +2558,8 @@ font-technology Variable Variabla
|
||||
Proof Setup Provtrycksinställningar
|
||||
Properties Egenskaper
|
||||
Proportional Metrics Proportionella mått
|
||||
Insert Aki Left Infoga aki till vänster
|
||||
Insert Aki Right Infoga aki till höger
|
||||
Character Direction Teckenriktning
|
||||
Digits Siffror
|
||||
Arabic Arabiska
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 5 and column 25.
|
@@ -2430,6 +2430,8 @@ shape Open Açık
|
||||
Proof Setup Prova Kurulumu
|
||||
Properties Özellikler
|
||||
Proportional Metrics Orantılı Metrikler
|
||||
Insert Aki Left Sola Aki Ekle
|
||||
Insert Aki Right Sağa Aki Ekle
|
||||
Digits Rakamlar
|
||||
Arabic Arapça
|
||||
Hindi Hintçe
|
||||
@@ -5916,6 +5918,7 @@ theme Appearance Görünüm
|
||||
Long Uzun
|
||||
New Brush Yeni Fırça
|
||||
Pattern Brush Desen Fırçası
|
||||
missing from the Brushes panel Fırçalar panelinde yok
|
||||
Pick Color From: Rengi Buradan Al:
|
||||
Scatter Brush Serpme Fırçası
|
||||
Select art to make a scatter, art or pattern brush. Bir serpme, resim veya desen fırçası oluşturmak için çizim seçin.
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 5 and column 25.
|
@@ -2205,6 +2205,7 @@ file-filter Templates Шаблони
|
||||
Art Brush Художній пензель
|
||||
Bristle Brush Щетинний пензель
|
||||
Pattern Brush Візерунковий пензель
|
||||
missing from the Brushes panel відсутній у панелі «Пензлі»
|
||||
Select art to make a scatter, art or pattern brush. Виділіть малюнок, щоб створити пензель-розпорошення, художній або візерунковий пензель.
|
||||
Brush Art Малюнок пензля
|
||||
New Calligraphic Brush Новий каліграфічний пензель
|
||||
@@ -2654,6 +2655,8 @@ mark-style Japanese Японський
|
||||
Proof Setup Налаштування кольоропроби
|
||||
Properties Властивості
|
||||
Proportional Metrics Пропорційні метрики
|
||||
Insert Aki Left Вставити акі ліворуч
|
||||
Insert Aki Right Вставити акі праворуч
|
||||
Character Direction Напрямок символів
|
||||
Digits Цифри
|
||||
Arabic Арабські
|
||||
|
||||
|
Can't render this file because it is too large.
|
@@ -2801,6 +2801,7 @@ digits Hindi Hindi
|
||||
Paths: {paths} Anchors: {anchors} Colors: {colors} Đường dẫn: {paths} Điểm neo: {anchors} Màu: {colors}
|
||||
Pattern Họa tiết
|
||||
Pattern Brush Cọ họa tiết
|
||||
missing from the Brushes panel không có trong bảng Cọ
|
||||
Pattern Brush Options Tùy chọn cọ họa tiết
|
||||
Pattern Editing Chỉnh sửa họa tiết
|
||||
Pattern Options Tùy chọn họa tiết
|
||||
@@ -2961,6 +2962,8 @@ mark-style Japanese Kiểu Nhật
|
||||
Proof Setup Thiết lập thử nghiệm
|
||||
Properties Thuộc tính
|
||||
Proportional Metrics Số liệu theo tỷ lệ
|
||||
Insert Aki Left Chèn aki bên trái
|
||||
Insert Aki Right Chèn aki bên phải
|
||||
Proportional Lining Số tỷ lệ đứng
|
||||
Position: Vị trí:
|
||||
Default Position Vị trí mặc định
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 204 and column 5.
|
@@ -2074,6 +2074,7 @@ file-filter Templates 模板
|
||||
Art Brush 艺术画笔
|
||||
Bristle Brush 鬃毛画笔
|
||||
Pattern Brush 图案画笔
|
||||
missing from the Brushes panel “画笔”面板中没有
|
||||
Select art to make a scatter, art or pattern brush. 选择图稿以创建散布画笔、艺术画笔或图案画笔。
|
||||
Brush Art 画笔图稿
|
||||
New Calligraphic Brush 新建书法画笔
|
||||
@@ -2521,6 +2522,8 @@ font-script Japanese 汉字
|
||||
Proof Setup 校样设置
|
||||
Properties 属性
|
||||
Proportional Metrics 比例度量
|
||||
Insert Aki Left 在前方插入空白
|
||||
Insert Aki Right 在后方插入空白
|
||||
Character Direction 字符方向
|
||||
Digits 数字
|
||||
Arabic 阿拉伯数字
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 73.
|
@@ -3177,6 +3177,7 @@ file-filter Templates 範本
|
||||
Art Brush 藝術筆刷
|
||||
Bristle Brush 鬃毛筆刷
|
||||
Pattern Brush 圖樣筆刷
|
||||
missing from the Brushes panel 「筆刷」面板中沒有
|
||||
Select art to make a scatter, art or pattern brush. 選取圖稿以製作散佈筆刷、藝術筆刷或圖樣筆刷。
|
||||
Brush Art 筆刷圖稿
|
||||
New Calligraphic Brush 新增書法筆刷
|
||||
@@ -3630,6 +3631,8 @@ font-script Japanese 漢字
|
||||
Proof Setup 校樣設定
|
||||
Properties 內容
|
||||
Proportional Metrics 比例度量
|
||||
Insert Aki Left 在前方插入空白
|
||||
Insert Aki Right 在後方插入空白
|
||||
Character Direction 字元方向
|
||||
Digits 數字
|
||||
Arabic 阿拉伯數字
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 6 and column 47.
|
@@ -44,6 +44,9 @@ pub fn export(app: &mut VectorcraftApp, format: Option<&str>, path: Option<Strin
|
||||
std::borrow::Cow::Borrowed(_) => st.doc.clone(),
|
||||
std::borrow::Cow::Owned(d) => std::sync::Arc::new(d),
|
||||
};
|
||||
// A PDF shows its title in the viewer's title bar only when it is the document's own, not the
|
||||
// file name it was titled after, as `document.export` writes it.
|
||||
let own_title = fileio::has_own_title(&doc);
|
||||
let doc = fileio::titled_for_export(&doc, st.path.as_deref(), f.id).map_or(doc, std::sync::Arc::new);
|
||||
let uid = st.uid;
|
||||
let path = export_path(app, path, f)?;
|
||||
@@ -62,9 +65,16 @@ pub fn export(app: &mut VectorcraftApp, format: Option<&str>, path: Option<Strin
|
||||
// Background Export: encoded and written from this snapshot while editing goes on. The
|
||||
// encoder is told the file it writes (an exporter plug-in gets its name and extension).
|
||||
let label = format!("Exporting {}", fileio::file_name(&path));
|
||||
// A PDF's View PDF after saving: the first file written opens in the system viewer, unless
|
||||
// automation asked for the export, also through a save panel it opened (checked now: a
|
||||
// background export ends after the request).
|
||||
let view = params.get("viewAfterSaving").and_then(Value::as_bool).unwrap_or(false) && !app.automation_asked();
|
||||
let (mut params, target) = (params.clone(), path.clone());
|
||||
if let Some(o) = params.as_object_mut() {
|
||||
o.insert("path".into(), json!(path));
|
||||
if f.id == "pdf" {
|
||||
o.insert("_displayDocTitle".into(), json!(own_title));
|
||||
}
|
||||
}
|
||||
// The progress dialog's counts and Cancel: a cancelled export stops drawing and never starts
|
||||
// writing.
|
||||
@@ -113,6 +123,9 @@ pub fn export(app: &mut VectorcraftApp, format: Option<&str>, path: Option<Strin
|
||||
Some(first) => exported_with_notes(&what, warnings.len(), first),
|
||||
None => format!("Exported {what}"),
|
||||
};
|
||||
if view && let Some(path) = r["path"].as_str() {
|
||||
view_file(app, path);
|
||||
}
|
||||
show_with_notes(app, status, warnings);
|
||||
Ok(r)
|
||||
})?;
|
||||
@@ -192,10 +205,30 @@ pub(crate) fn run_to_file(app: &mut VectorcraftApp, id: &str, ext: &str, mut par
|
||||
/// File → Save as PDF: `document.exportPdf` with `params` (the Save PDF dialog's options), written
|
||||
/// to `path` (else a picked or suggested name) → `{path, bytes, warnings}`. With
|
||||
/// `viewAfterSaving`, the written file opens in the system viewer (not on the web, which
|
||||
/// downloads it).
|
||||
pub fn export_pdf(app: &mut VectorcraftApp, params: Value) -> Result<Value, String> {
|
||||
/// downloads it). Writing over the file the document was opened from, when opening it left things
|
||||
/// out, asks first, for a given path and for one the save panel picked, and runs
|
||||
/// `file.export.pdf` again on OK.
|
||||
pub fn export_pdf(app: &mut VectorcraftApp, mut params: Value) -> Result<Value, String> {
|
||||
if let Some(path) = params.get("path").and_then(Value::as_str)
|
||||
&& let Some(r) = ask_before_losing(app, path, "file.export.pdf", ¶ms)
|
||||
{
|
||||
return Ok(r);
|
||||
}
|
||||
let view = params.get("viewAfterSaving").and_then(Value::as_bool).unwrap_or(false);
|
||||
let (path, mut v) = run_to_file(app, "document.exportPdf", "pdf", params)?;
|
||||
let given = params.as_object_mut().and_then(|o| o.remove("path")).and_then(|p| p.as_str().map(str::to_string));
|
||||
let mut v = app.session.execute("document.exportPdf", ¶ms).map_err(|e| e.to_string())?;
|
||||
let bytes = take_output(&mut v)?;
|
||||
let path = match given {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
let picked = target_path(app, None, "pdf")?;
|
||||
if let Some(r) = ask_before_losing(app, &picked, "file.export.pdf", ¶ms) {
|
||||
return Ok(r);
|
||||
}
|
||||
picked
|
||||
}
|
||||
};
|
||||
write_reporting(app, &path, &bytes)?;
|
||||
if view {
|
||||
view_file(app, &path);
|
||||
}
|
||||
|
||||
@@ -472,8 +472,9 @@ pub fn save_pdf(app: &mut VectorcraftApp, mode: SaveMode, mut params: Value) ->
|
||||
|
||||
/// Open a written file in the system viewer (not on the web, which downloads it).
|
||||
pub(super) fn view_file(app: &mut VectorcraftApp, path: &str) {
|
||||
// The viewer is another app: an export automation asked for is written but not opened.
|
||||
if !is_web(app) && app.refuse_automation("viewAfterSaving").is_ok() {
|
||||
// The viewer is another app: an export automation asked for is written but not opened, also
|
||||
// when it goes on from a save panel automation opened.
|
||||
if !is_web(app) && !app.automation_asked() {
|
||||
app.open_url(&file_url(path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1249,7 +1249,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.savePdfDialog",
|
||||
"Save PDF Dialog",
|
||||
"",
|
||||
"{path?, preset?, range?, …document.exportPdf options} open the Save PDF dialog with these options (fields = the options, sections are objects; OK writes path, else asks)",
|
||||
"{path?, preset?, range?, useArtboards?, …document.exportPdf options} open the Save PDF dialog with these options (fields = the options, sections are objects; OK writes path, else asks); useArtboards: false: one page of the visible art's bounds, with no Artboards section",
|
||||
),
|
||||
(
|
||||
"file.exportAs",
|
||||
|
||||
@@ -22,6 +22,9 @@ pub const SIZE_PRESETS: [f64; 15] = [6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 14.0,
|
||||
/// The Kerning and Tracking dropdowns' presets in 1/1000 em.
|
||||
pub const TRACKING_PRESETS: [f64; 14] = [-100.0, -75.0, -50.0, -25.0, -10.0, -5.0, 0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 200.0];
|
||||
pub const SCALE_PRESETS: [f64; 8] = [25.0, 50.0, 75.0, 90.0, 100.0, 110.0, 125.0, 150.0];
|
||||
/// Insert Aki Left and Right presets, in thousandths of an em: none, a quarter, half and a whole
|
||||
/// full-width space.
|
||||
pub const AKI_PRESETS: [f64; 4] = [0.0, 250.0, 500.0, 1000.0];
|
||||
|
||||
/// The stored character style: the selected range's (or the caret's) while the Type
|
||||
/// tool edits text, else the first selected text object's first run (including groups). With it, the paragraph
|
||||
@@ -766,7 +769,8 @@ fn cell(ui: &mut Ui, label: &str, tip: &str, add: impl FnOnce(&mut Ui)) {
|
||||
|
||||
/// The Character panel's grid of fields, `w` wide: Font Size, Leading, Kerning and Tracking,
|
||||
/// then with `more` the scales, Baseline Shift and Character Rotation, and a row with the
|
||||
/// Anti-aliasing Method dropdown under Character Rotation (`s`: the style shown). With Show Font
|
||||
/// Anti-aliasing Method dropdown under Character Rotation (`s`: the style shown), and with Show East
|
||||
/// Asian Options on a row with Insert Aki Left and Right. With Show Font
|
||||
/// Height Options on, a row under Font Size holds the Set Font Height Reference dropdown. The
|
||||
/// Properties panel shows its first rows.
|
||||
pub(crate) fn metrics_grid(app: &mut VectorcraftApp, ui: &mut Ui, id: &str, s: &CharStyle, w: f32, more: bool) {
|
||||
@@ -847,6 +851,22 @@ pub(crate) fn metrics_grid(app: &mut VectorcraftApp, ui: &mut Ui, id: &str, s: &
|
||||
ui.label("");
|
||||
cell(ui, "aa", tl!("Anti-aliasing Method"), |ui| anti_alias_method(app, ui, fw));
|
||||
ui.end_row();
|
||||
// Insert Aki Left and Right, with the East Asian options: space before and after each
|
||||
// character, in thousandths of an em.
|
||||
if app.session.prefs.show_east_asian_options {
|
||||
for (field, label, key, icon) in
|
||||
[("ch-aki-left", tl!("Insert Aki Left"), "akiLeft", "↔A"), ("ch-aki-right", tl!("Insert Aki Right"), "akiRight", "A↔")]
|
||||
{
|
||||
cell(ui, icon, label, |ui| {
|
||||
let value = shared(app, |s| if key == "akiLeft" { s.aki_left } else { s.aki_right });
|
||||
if let Some(v) = widgets::spin_plain(ui, field, value, "", 0, fw, 10.0, 0.0, &AKI_PRESETS) {
|
||||
// The field stops at the largest value the command takes.
|
||||
format(app, json!({ key: v.min(vectorcraft_doc::text::MAX_AKI) }));
|
||||
}
|
||||
});
|
||||
}
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1445,7 +1465,7 @@ fn fractional_widths_on(app: &VectorcraftApp) -> bool {
|
||||
/// kerning, no baseline shift, 100% scales, no rotation, no underline or strikethrough, All Caps
|
||||
/// and Small Caps off, the normal position (neither superscript nor subscript), Roman Baseline
|
||||
/// character alignment, Proportional Metrics, Tate-chu-yoko, Standard Vertical Roman Alignment and
|
||||
/// No Break off, Tate-chu-yoko Settings Up/Down and Left/Right 0, the Default character direction,
|
||||
/// No Break off, Tate-chu-yoko Settings Up/Down and Left/Right 0, Insert Aki Left and Right 0, the Default character direction,
|
||||
/// Kashidas on, the Default digits and the Em Box font height reference, whether or not Show East Asian
|
||||
/// Options, Show Indic Options and Show Font Height Options are on. Inside an undo group that is
|
||||
/// already open, the reset is part of that group's step.
|
||||
@@ -1464,7 +1484,7 @@ fn reset_panel(app: &mut VectorcraftApp) {
|
||||
"kerning": "auto", "baselineShift": 0, "hScale": 100, "vScale": 100, "rotation": 0, "underline": false, "strikethrough": false,
|
||||
"allCaps": false, "smallCaps": d.small_caps.is_some(), "position": d.position.id(), "charAlign": d.char_align,
|
||||
"proportionalMetrics": d.proportional_metrics, "tateChuYoko": d.tate_chu_yoko,
|
||||
"tcyUpDown": d.tcy_up_down, "tcyLeftRight": d.tcy_left_right,
|
||||
"tcyUpDown": d.tcy_up_down, "tcyLeftRight": d.tcy_left_right, "akiLeft": d.aki_left, "akiRight": d.aki_right,
|
||||
"standardVerticalRoman": d.standard_vertical_roman, "noBreak": d.no_break, "charDirection": "default", "kashidas": d.kashidas,
|
||||
"digits": d.digits, "fontHeightReference": d.font_height_reference
|
||||
}),
|
||||
|
||||
@@ -746,6 +746,40 @@ fn character_alignment_shows_with_the_east_asian_options() {
|
||||
assert_eq!(shown(&mut app), (true, true));
|
||||
}
|
||||
|
||||
/// Insert Aki Left and Right are fields of the panel with the East Asian options only, and show
|
||||
/// the selected characters' values, blank where they differ. A value typed past 1000 sets 1000.
|
||||
#[test]
|
||||
fn insert_aki_fields_show_with_the_east_asian_options() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
let id = app.run("text.create", json!({"x": 20, "y": 50, "text": "(日本)"})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("select.set", json!({"ids": [id]})).unwrap();
|
||||
app.run("text.setFormat", json!({"akiLeft": 250, "akiRight": 750})).unwrap();
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(!text.contains("↔A") && !text.contains("750"), "{text}");
|
||||
app.session.prefs.show_east_asian_options = true;
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(text.contains("↔A") && text.contains("A↔") && text.contains("250") && text.contains("750"), "{text}");
|
||||
// Only the first character has 750 after it: the Right field is blank.
|
||||
app.run("text.setRangeStyle", json!({"id": id, "start": 3, "end": 12, "akiRight": 0})).unwrap();
|
||||
let text = crate::tests_labels::painted_text(&mut app, show);
|
||||
assert!(text.contains("250") && !text.contains("750"), "{text}");
|
||||
use super::super::tests_appearance::{click, frame_events, text_rect};
|
||||
let ctx = egui::Context::default();
|
||||
let texts = frame_events(&ctx, &mut app, vec![], show);
|
||||
click(&ctx, &mut app, text_rect(&texts, "250").center(), show);
|
||||
let key = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers };
|
||||
frame_events(&ctx, &mut app, vec![key(egui::Key::A, egui::Modifiers::COMMAND)], show);
|
||||
frame_events(&ctx, &mut app, vec![egui::Event::Text("1500".into())], show);
|
||||
frame_events(&ctx, &mut app, vec![key(egui::Key::Enter, egui::Modifiers::NONE)], show);
|
||||
frame_events(&ctx, &mut app, vec![], show);
|
||||
let left = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(NodeId(id)).unwrap().kind {
|
||||
NodeKind::Text(t) => t.runs.iter().map(|r| r.style.aki_left).collect::<Vec<_>>(),
|
||||
_ => panic!("text"),
|
||||
};
|
||||
assert!(left(&app).iter().all(|v| *v == 1000.0), "{:?}", left(&app));
|
||||
}
|
||||
|
||||
/// The panel menu's Show Font Height Options shows the Set Font Height Reference dropdown,
|
||||
/// which shows the selected type's reference and sets the one picked (one undo step); Hide
|
||||
/// Font Height Options hides it again.
|
||||
@@ -1242,6 +1276,8 @@ fn reset_panel_resets_every_attribute_but_the_font() {
|
||||
s.tate_chu_yoko != d.tate_chu_yoko,
|
||||
s.tcy_up_down != d.tcy_up_down,
|
||||
s.tcy_left_right != d.tcy_left_right,
|
||||
s.aki_left != d.aki_left,
|
||||
s.aki_right != d.aki_right,
|
||||
s.standard_vertical_roman != d.standard_vertical_roman,
|
||||
s.no_break != d.no_break,
|
||||
s.char_direction != d.char_direction,
|
||||
@@ -1256,16 +1292,16 @@ fn reset_panel_resets_every_attribute_but_the_font() {
|
||||
"id": id, "tracking": 50, "leading": 30, "kerning": 20, "baselineShift": 3, "hScale": 120, "vScale": 90, "rotation": 15,
|
||||
"underline": true, "strikethrough": true, "allCaps": true, "smallCaps": true, "position": "superscript",
|
||||
"charAlign": "emBoxCenter", "proportionalMetrics": true, "tateChuYoko": true, "tcyUpDown": 2, "tcyLeftRight": -3,
|
||||
"standardVerticalRoman": true, "noBreak": true, "charDirection": "rightToLeft", "digits": "hindi", "fontHeightReference": "capHeight"
|
||||
"akiLeft": 250, "akiRight": 500, "standardVerticalRoman": true, "noBreak": true, "charDirection": "rightToLeft", "digits": "hindi", "fontHeightReference": "capHeight"
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
};
|
||||
let plain = runs(&app)[0].1.clone();
|
||||
assert_eq!(differs(&plain, &CharStyle::default()), [false; 22]);
|
||||
assert_eq!(differs(&plain, &CharStyle::default()), [false; 24]);
|
||||
custom(&mut app);
|
||||
let styled = runs(&app)[0].1.clone();
|
||||
assert_eq!(differs(&styled, &CharStyle::default()), [true; 22]);
|
||||
assert_eq!(differs(&styled, &CharStyle::default()), [true; 24]);
|
||||
app.run("select.set", json!({"ids": [id]})).unwrap();
|
||||
reset_panel(&mut app);
|
||||
assert_eq!(runs(&app), [("One 12".to_string(), plain.clone())]);
|
||||
|
||||
@@ -78,6 +78,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let label = super::label_or_name(label, !named.contains(&k));
|
||||
if value.is_empty() {
|
||||
widgets::dim_name(ui, label);
|
||||
} else if s["id"] == "brushes" {
|
||||
row(ui, label, brush_value(value).into());
|
||||
} else {
|
||||
row(ui, label, value.into());
|
||||
}
|
||||
@@ -86,6 +88,20 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
});
|
||||
}
|
||||
|
||||
/// A Brushes row's value in the interface language: the brush's type (`Art Brush`), or that the
|
||||
/// Brushes panel does not hold the brush. `document.info` and the text report keep them in English.
|
||||
fn brush_value(value: &str) -> &str {
|
||||
match value {
|
||||
"Calligraphic Brush" => tl!("Calligraphic Brush"),
|
||||
"Scatter Brush" => tl!("Scatter Brush"),
|
||||
"Art Brush" => tl!("Art Brush"),
|
||||
"Pattern Brush" => tl!("Pattern Brush"),
|
||||
"Bristle Brush" => tl!("Bristle Brush"),
|
||||
"missing from the Brushes panel" => tl!("missing from the Brushes panel"),
|
||||
_ => value,
|
||||
}
|
||||
}
|
||||
|
||||
/// The rows of Document Info section `id` labelled by a name (an artboard, a style, a swatch, a
|
||||
/// font, an image, a repeat object) rather than an interface label: every row of the lists, and in Document the
|
||||
/// artboards, which follow the Artboards count. Names are shown as they are.
|
||||
@@ -295,4 +311,30 @@ mod tests {
|
||||
let err = app.run("docInfo.save", json!({"path": "/tmp/info.txt", "category": "colors"})).unwrap_err();
|
||||
assert!(err.contains("category `colors`"), "{err}");
|
||||
}
|
||||
|
||||
/// Brushes shows each brush's type, and a brush the Brushes panel does not hold, in the
|
||||
/// interface language; the saved report keeps them in English.
|
||||
#[test]
|
||||
fn brush_types_show_in_the_interface_language() {
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
|
||||
let r = app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 30, "height": 20})).unwrap()["id"].clone();
|
||||
app.session.execute("brush.apply", &json!({"ids": [r], "name": "Charcoal"})).unwrap();
|
||||
let brushes = |app: &mut VectorcraftApp| {
|
||||
crate::tests_labels::painted_text(app, |app, ui| {
|
||||
set_pstate(ui.ctx(), "docinfo-category", "brushes".to_string());
|
||||
show(app, ui);
|
||||
})
|
||||
};
|
||||
let english = brushes(&mut app);
|
||||
assert!(english.contains("Charcoal") && english.contains("Art Brush"), "{english}");
|
||||
let Some(de) = crate::i18n::Lang::from_code("de") else { panic!("no German catalog") };
|
||||
crate::i18n::set_current(de);
|
||||
let (german, missing) = (brushes(&mut app), brush_value("missing from the Brushes panel").to_string());
|
||||
crate::i18n::set_current(crate::i18n::Lang::EN);
|
||||
assert!(german.contains("Charcoal") && german.contains("Bildpinsel") && !german.contains("Art Brush"), "{german}");
|
||||
assert_eq!(missing, "fehlt im Bedienfeld „Pinsel“");
|
||||
let report = app.session.execute("document.info", &json!({"category": "brushes", "format": "text"})).unwrap()["text"].to_string();
|
||||
assert!(report.contains("Charcoal: Art Brush"), "{report}");
|
||||
}
|
||||
}
|
||||
Loaded 100 of 114 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user