diff --git a/crates/ui-contract/icons/ATTRIBUTION.md b/crates/ui-contract/icons/ATTRIBUTION.md index 7baac2bd35..ea58b4ebc4 100644 --- a/crates/ui-contract/icons/ATTRIBUTION.md +++ b/crates/ui-contract/icons/ATTRIBUTION.md @@ -1,6 +1,6 @@ # Icon sources -Each folder holds only the files `icons.toml` lists, unchanged from the pinned source (exception: the drawings named below the table), and the set's licence as `LICENSE`. To add an icon, copy its file from the same commit. +Each folder holds only the files `icons.toml` lists, unchanged from the pinned source (exception: the drawings named below the table), and the set's licence as `LICENSE`. To add an icon, copy its file from the npm package of the version in the table. | Folder | Set | Licence | Source (tag, commit) | Files taken from | |---|---|---|---|---| @@ -13,6 +13,8 @@ Carbon's licence file is the repository's root `LICENSE` (the icons package has Every set folder's files come from the npm package of the version in the table (`@phosphor-icons/core` 2.0.8, `lucide-static` 1.63.0, `@carbon/icons` 11.91.0, `@tabler/icons` 3.50.0), the same releases as the tags above. Phosphor files are `regular` weight, except `star-filled.svg`, `circle-filled.svg` and `warning-filled.svg`, which are Phosphor's `fill` weight (`star-fill.svg`, `circle-fill.svg`, `warning-fill.svg`). `panel-left.svg` is Phosphor's `sidebar-simple` unchanged, and `panel-right.svg` is the same glyph mirrored. +The transform and layout files added for the flip, scale, skew, padding and gap glyphs are these packages' copies, byte for byte, comments and classes included: Lucide's `flip-horizontal`, `flip-vertical`, `gap-horizontal` and `gap-vertical`; Tabler's `flip-horizontal`, `flip-vertical`, `skew-x`, `skew-y`, `arrow-autofit-width`, `arrow-autofit-height`, `spacing-horizontal` and `spacing-vertical`; and Phosphor's `arrows-out-line-horizontal` and `arrows-out-line-vertical`. + `phosphor/` also holds drawings made for the suite, for glyphs the pinned Phosphor release lacks. They follow Phosphor's regular style (256 view box, 16-unit round-capped strokes, some converted to filled outlines), take their shapes from PhotoCraft's glyphs, and carry the suite's own licence: - `bandage-sparkle`, `brush-replace`, `clip-below`, `clip-release`, `convert-point`, `direct-select`, `eraser-background`, `eraser-magic`, `font-similar` @@ -24,5 +26,8 @@ Every set folder's files come from the npm package of the version in the table ( - `sync-lock`, `mark-in`, `mark-out`, `insert-clip`, `overwrite-clip`, `lift-clip`, `extract-clip` (the timeline's lock and edit glyphs, after Premiere Pro's), `keyframe-linear`, `keyframe-bezier`, `keyframe-auto-bezier`, `keyframe-hold`, `keyframe-ease-in`, `keyframe-ease-out` (keyframe nodes after After Effects' shapes: diamond, square for Bezier, circle for Auto Bezier) - `key-linear`, `key-linear-filled`, `key-hold`, `key-bezier`, `key-auto-bezier`, `key-continuous-bezier`, `key-ease-in`, `key-ease-out`, `keyframe-easy-ease` (the timeline's single keyframe marks and the graph editor's Easy Ease button, after FilmCraft's diamond, square and circle and After Effects' keys; solid shapes except the outline diamond and the rings) - `font-variable`, `scaling`, `slice-select`, `square-dashed-mouse-pointer`, `text-warp`, `triangle-right`, `type-mask`, `type-mask-vertical` +- `flip-horizontal`, `flip-vertical`, `skew-horizontal`, `skew-vertical`, `padding-top`, `padding-right`, `padding-bottom`, `padding-left`, `gap-horizontal`, `gap-vertical` (the transform and layout panels' glyphs, drawn after the marks those panels make: a dashed axis between two triangles for a flip, a slanted rectangle for the skew along x and the same rectangle transposed for the skew along y, a frame with the space drawn solid on one side for a padding, and two items with a double arrow in the space between them for a gap) - `crop-overlay-*` (the crop overlay menu's glyphs), `text-align-top`, `text-align-middle`, `text-align-bottom` (the vertical type options), `dash-align-exact`, `dash-align-corners` (the dash alignment options) - `eye-outline` (Phosphor's `eye` without the pupil: the Outline view), `layer-template` (a dashed layer: the Template view) + +Where a pinned set already draws one of these meanings, that set uses its own file rather than the base set's: Lucide's `flip-horizontal`, `flip-vertical`, `gap-horizontal` and `gap-vertical`; Tabler's `flip-horizontal`, `flip-vertical`, `skew-x`, `skew-y`, `arrow-autofit-width`, `arrow-autofit-height`, `spacing-horizontal` and `spacing-vertical`; and Phosphor's own `arrows-out-line-horizontal` and `arrows-out-line-vertical` for the axis of scale. A set without one falls back to the base set's drawing, as it does for every other icon, so Lucide and Carbon draw Phosphor's two `arrows-out-line` marks for the axes of scale. The four paddings keep the base set's drawing in every set: the nearest marks the other packs have, Tabler's `box-padding` and `box-margin`, draw a uniform inset around the content, and that names no side. diff --git a/crates/ui-contract/icons/icons.toml b/crates/ui-contract/icons/icons.toml index 3eaaa30a39..12437c6172 100644 --- a/crates/ui-contract/icons/icons.toml +++ b/crates/ui-contract/icons/icons.toml @@ -35,14 +35,17 @@ stroke_drawings = [ "crop-overlay-thirds", "crop-overlay-triangle", "dash-align-corners", "dash-align-exact", "direct-select", "distribute-bottom", "distribute-hcenter", "distribute-horizontal", "distribute-left", "distribute-right", "distribute-top", "distribute-vcenter", "distribute-vertical", "eraser-background", - "eraser-magic", "extract-clip", "eye-outline", "font-similar", "fx", "gradient-angle", "gradient-diamond", "gradient-linear", + "eraser-magic", "extract-clip", "eye-outline", "flip-horizontal", "flip-vertical", "font-similar", "fx", + "gap-horizontal", "gap-vertical", "gradient-angle", "gradient-diamond", "gradient-linear", "gradient-radial", "gradient-reflected", "gradient-swatch", "insert-clip", "key-auto-bezier", "key-bezier", "key-continuous-bezier", "key-ease-in", "key-ease-out", "key-hold", "key-linear", "key-linear-filled", "keyframe-auto-bezier", "keyframe-bezier", "keyframe-ease-in", "keyframe-ease-out", "keyframe-easy-ease", "keyframe-hold", "keyframe-linear", "lasso", "lasso-magnetic", "lasso-select", "layer-mask", "layer-template", "lift-clip", "lollipop", "mark-in", - "mark-out", "marquee-column", "marquee-row", "overwrite-clip", "painting-symmetry", "pen-add-anchor", + "mark-out", "marquee-column", "marquee-row", "overwrite-clip", "padding-bottom", "padding-left", "padding-right", + "padding-top", "painting-symmetry", "pen-add-anchor", "pen-curvature", "pen-delete-anchor", "pen-freeform", "protractor", "quick-selection", "route-curved", "route-elbow", - "route-straight", "stroke-align-center", "stroke-align-inside", "stroke-align-outside", "stroke-cap-butt", + "route-straight", "skew-horizontal", "skew-vertical", "stroke-align-center", + "stroke-align-inside", "stroke-align-outside", "stroke-cap-butt", "stroke-cap-round", "stroke-cap-square", "stroke-join-bevel", "stroke-join-miter", "stroke-join-round", "symmetry-diagonal", "symmetry-dual", "symmetry-horizontal", "symmetry-vertical", "sync-lock", "text-align-bottom", "text-align-middle", "text-align-top" @@ -594,6 +597,55 @@ phosphor = "arrows-down-up" carbon = "arrows--vertical" tabler = "arrows-down-up" +# The transform panels' flip, scale and skew glyphs. Each name is the axis the change runs along: +# `flip_horizontal` mirrors left and right, so the axis it mirrors about is the vertical one the drawing shows. +# The `symmetry_*` names read the other way round: they name the axis line the drawing shows, so `symmetry_vertical` +# mirrors left and right. `skew_horizontal` and `skew_vertical` are the skew along x and along y. None of these is directional: a side or an axis of the +# screen stays where it is under right-to-left. Lucide and Tabler draw the flips, Tabler draws the skew pair and +# the two axes of scale (its `arrow-autofit-width` and `-height`: a box whose width or height is spanned by +# outward arrows), and Phosphor's `arrows-out-line-horizontal` and `-vertical` are the base set's marks for the +# axis of scale; Phosphor draws no flip and no skew, so the base set draws those (ATTRIBUTION.md). Tabler's file +# names read the other way round: its `skew-y` leans the sides and so draws the horizontal skew, and its `skew-x` +# leans the top and bottom and draws the vertical one. + +[[icon]] +name = "flip_horizontal" +directional = false +lucide = "flip-horizontal" +phosphor = "flip-horizontal" +tabler = "flip-horizontal" + +[[icon]] +name = "flip_vertical" +directional = false +lucide = "flip-vertical" +phosphor = "flip-vertical" +tabler = "flip-vertical" + +[[icon]] +name = "scale_horizontal" +directional = false +phosphor = "arrows-out-line-horizontal" +tabler = "arrow-autofit-width" + +[[icon]] +name = "scale_vertical" +directional = false +phosphor = "arrows-out-line-vertical" +tabler = "arrow-autofit-height" + +[[icon]] +name = "skew_horizontal" +directional = false +phosphor = "skew-horizontal" +tabler = "skew-y" + +[[icon]] +name = "skew_vertical" +directional = false +phosphor = "skew-vertical" +tabler = "skew-x" + [[icon]] name = "ban" directional = false @@ -768,6 +820,46 @@ name = "distribute_vertical" directional = false phosphor = "distribute-vertical" +# The layout panels' spacing glyphs: a frame with the space on one side, and the gap between two items. Each gap +# name is the axis the gap runs along, as the flip, scale and skew names are, and none of them mirrors under +# right-to-left: a side of the screen stays that side. Phosphor 2.0.8 draws neither a padding nor a gap, so the +# base set draws its own (ATTRIBUTION.md); Lucide's `gap-horizontal` and `gap-vertical` and Tabler's +# `spacing-horizontal` and `spacing-vertical` are the same meanings. + +[[icon]] +name = "gap_horizontal" +directional = false +lucide = "gap-horizontal" +phosphor = "gap-horizontal" +tabler = "spacing-horizontal" + +[[icon]] +name = "gap_vertical" +directional = false +lucide = "gap-vertical" +phosphor = "gap-vertical" +tabler = "spacing-vertical" + +[[icon]] +name = "padding_bottom" +directional = false +phosphor = "padding-bottom" + +[[icon]] +name = "padding_left" +directional = false +phosphor = "padding-left" + +[[icon]] +name = "padding_right" +directional = false +phosphor = "padding-right" + +[[icon]] +name = "padding_top" +directional = false +phosphor = "padding-top" + [[icon]] name = "droplet" directional = false diff --git a/crates/ui-contract/icons/lucide/flip-horizontal.svg b/crates/ui-contract/icons/lucide/flip-horizontal.svg new file mode 100644 index 0000000000..13ea0764dd --- /dev/null +++ b/crates/ui-contract/icons/lucide/flip-horizontal.svg @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/crates/ui-contract/icons/lucide/flip-vertical.svg b/crates/ui-contract/icons/lucide/flip-vertical.svg new file mode 100644 index 0000000000..2c3419eefd --- /dev/null +++ b/crates/ui-contract/icons/lucide/flip-vertical.svg @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/crates/ui-contract/icons/lucide/gap-horizontal.svg b/crates/ui-contract/icons/lucide/gap-horizontal.svg new file mode 100644 index 0000000000..7508748ef7 --- /dev/null +++ b/crates/ui-contract/icons/lucide/gap-horizontal.svg @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/crates/ui-contract/icons/lucide/gap-vertical.svg b/crates/ui-contract/icons/lucide/gap-vertical.svg new file mode 100644 index 0000000000..3b82f02f11 --- /dev/null +++ b/crates/ui-contract/icons/lucide/gap-vertical.svg @@ -0,0 +1,20 @@ + + + + + + + + + diff --git a/crates/ui-contract/icons/phosphor/arrows-out-line-horizontal.svg b/crates/ui-contract/icons/phosphor/arrows-out-line-horizontal.svg new file mode 100644 index 0000000000..095bc63406 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/arrows-out-line-horizontal.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/ui-contract/icons/phosphor/arrows-out-line-vertical.svg b/crates/ui-contract/icons/phosphor/arrows-out-line-vertical.svg new file mode 100644 index 0000000000..1873ef864b --- /dev/null +++ b/crates/ui-contract/icons/phosphor/arrows-out-line-vertical.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/ui-contract/icons/phosphor/flip-horizontal.svg b/crates/ui-contract/icons/phosphor/flip-horizontal.svg new file mode 100644 index 0000000000..ecb7c8c2ae --- /dev/null +++ b/crates/ui-contract/icons/phosphor/flip-horizontal.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/flip-vertical.svg b/crates/ui-contract/icons/phosphor/flip-vertical.svg new file mode 100644 index 0000000000..6b50bc1541 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/flip-vertical.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/gap-horizontal.svg b/crates/ui-contract/icons/phosphor/gap-horizontal.svg new file mode 100644 index 0000000000..0ecf39a6b8 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/gap-horizontal.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/gap-vertical.svg b/crates/ui-contract/icons/phosphor/gap-vertical.svg new file mode 100644 index 0000000000..9f3315539a --- /dev/null +++ b/crates/ui-contract/icons/phosphor/gap-vertical.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/padding-bottom.svg b/crates/ui-contract/icons/phosphor/padding-bottom.svg new file mode 100644 index 0000000000..2d04669048 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/padding-bottom.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/padding-left.svg b/crates/ui-contract/icons/phosphor/padding-left.svg new file mode 100644 index 0000000000..7cca7de645 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/padding-left.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/padding-right.svg b/crates/ui-contract/icons/phosphor/padding-right.svg new file mode 100644 index 0000000000..7c1a16ceac --- /dev/null +++ b/crates/ui-contract/icons/phosphor/padding-right.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/padding-top.svg b/crates/ui-contract/icons/phosphor/padding-top.svg new file mode 100644 index 0000000000..29473efd33 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/padding-top.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/skew-horizontal.svg b/crates/ui-contract/icons/phosphor/skew-horizontal.svg new file mode 100644 index 0000000000..6f314e1d34 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/skew-horizontal.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/phosphor/skew-vertical.svg b/crates/ui-contract/icons/phosphor/skew-vertical.svg new file mode 100644 index 0000000000..1ab1f19167 --- /dev/null +++ b/crates/ui-contract/icons/phosphor/skew-vertical.svg @@ -0,0 +1 @@ + diff --git a/crates/ui-contract/icons/tabler/arrow-autofit-height.svg b/crates/ui-contract/icons/tabler/arrow-autofit-height.svg new file mode 100644 index 0000000000..9ed55d6796 --- /dev/null +++ b/crates/ui-contract/icons/tabler/arrow-autofit-height.svg @@ -0,0 +1,19 @@ + + + + + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/arrow-autofit-width.svg b/crates/ui-contract/icons/tabler/arrow-autofit-width.svg new file mode 100644 index 0000000000..d38debd822 --- /dev/null +++ b/crates/ui-contract/icons/tabler/arrow-autofit-width.svg @@ -0,0 +1,19 @@ + + + + + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/flip-horizontal.svg b/crates/ui-contract/icons/tabler/flip-horizontal.svg new file mode 100644 index 0000000000..16eef11318 --- /dev/null +++ b/crates/ui-contract/icons/tabler/flip-horizontal.svg @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/flip-vertical.svg b/crates/ui-contract/icons/tabler/flip-vertical.svg new file mode 100644 index 0000000000..af4364ab63 --- /dev/null +++ b/crates/ui-contract/icons/tabler/flip-vertical.svg @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/skew-x.svg b/crates/ui-contract/icons/tabler/skew-x.svg new file mode 100644 index 0000000000..2601fac131 --- /dev/null +++ b/crates/ui-contract/icons/tabler/skew-x.svg @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/skew-y.svg b/crates/ui-contract/icons/tabler/skew-y.svg new file mode 100644 index 0000000000..b1aadd4fd6 --- /dev/null +++ b/crates/ui-contract/icons/tabler/skew-y.svg @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/spacing-horizontal.svg b/crates/ui-contract/icons/tabler/spacing-horizontal.svg new file mode 100644 index 0000000000..fda3b89a7f --- /dev/null +++ b/crates/ui-contract/icons/tabler/spacing-horizontal.svg @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/crates/ui-contract/icons/tabler/spacing-vertical.svg b/crates/ui-contract/icons/tabler/spacing-vertical.svg new file mode 100644 index 0000000000..449a417b02 --- /dev/null +++ b/crates/ui-contract/icons/tabler/spacing-vertical.svg @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/crates/ui-contract/src/primitives/mod.rs b/crates/ui-contract/src/primitives/mod.rs index 07651d967d..91e5b45f35 100644 --- a/crates/ui-contract/src/primitives/mod.rs +++ b/crates/ui-contract/src/primitives/mod.rs @@ -26,7 +26,7 @@ pub(crate) mod sensing; mod state_layer; mod surface; mod surround; -mod svg_set; +pub(crate) mod svg_set; #[doc(hidden)] pub mod svg_tree; pub(crate) mod text; diff --git a/crates/ui-contract/src/primitives/svg_set.rs b/crates/ui-contract/src/primitives/svg_set.rs index 556ce686d4..f78c15676b 100644 --- a/crates/ui-contract/src/primitives/svg_set.rs +++ b/crates/ui-contract/src/primitives/svg_set.rs @@ -8,6 +8,12 @@ use super::svg_tree; include!(concat!(env!("OUT_DIR"), "/icons.rs")); +// The trials of the catalogue's drawings: beside the generated `svg`, `stroke_width` and `DIRECTIONAL`, whose routing +// they read without a public accessor. The module is `pub(crate)` so the crate's `registry!()` reaches its trials by +// path. +#[cfg(all(feature = "testing", any(test, feature = "suite")))] +pub(crate) mod inspector_trials; + /// An app's own icon set: SVG documents drawn beside the built-in catalog in every `IconSet`. #[derive(Clone, Copy, Debug)] #[non_exhaustive] diff --git a/crates/ui-contract/src/primitives/svg_set/inspector_trials.rs b/crates/ui-contract/src/primitives/svg_set/inspector_trials.rs new file mode 100644 index 0000000000..5312aef38b --- /dev/null +++ b/crates/ui-contract/src/primitives/svg_set/inspector_trials.rs @@ -0,0 +1,302 @@ +//! The drawings the catalogue gained for the transform and layout panels, read from the generated catalogue itself: +//! each parses, its ink stays inside its view box, it draws along the axis or on the side its name gives in the +//! coordinates it renders in, it does not mirror, and no two of them are the same document in one set. +//! +//! `build.rs` checks the catalogue as data (its tables, a file for every listed name, no unlisted file, the stroke +//! lists) and reads no drawing. The catalogue's `svg` resolves a name and a set to the document the rasteriser draws, +//! the set's own file or the base set's, and this trial goes through that routing rather than a list of file names. + +#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)] + +use super::{DIRECTIONAL, IconSet, find, svg}; +use crate::primitives::svg_tree; +use crate::testing::trial; +use craft_svg::tree::usvg::tiny_skia_path::{self, PathSegment, Transform}; +use craft_svg::tree::usvg::{Group, Node, Path}; +use std::collections::BTreeMap; + +/// How far an edge may drift from an axis, in the coordinates the drawing renders in, for the trial to count it as +/// along that axis. +const STRAIGHT: f32 = 0.01; + +/// The slack the view-box check allows, in view-box units: a cap or a join just past the box is not a crop. +const SLACK: f32 = 1.0; + +/// The length of the bar the two fixtures below draw, in view-box units. +const BAR: f32 = 16.0; + +/// A drawing written the awkward way round: a bar that runs along x inside a group rotated 90 degrees about the centre +/// of its 24 unit box, inside another group that shifts it, so it draws down the box from the top to the bottom. Two +/// groups, because a reading that applied only the nearest transform and stopped there is a way to get this wrong. +const ROTATED: &str = r#""#; + +/// The same bar flattened: the edge written where those transforms land it, `M4 8h16` becoming `M18 6v16`, with no +/// transform at all. The two documents draw the same edge, so a reading of the drawing must give them the same axes. +const FLAT: &str = r#""#; + +/// What a glyph's drawing must show. Each name is the axis or the side of the screen its control changes, never a +/// reading direction. +#[derive(Clone, Copy)] +enum Reads { + /// The two marks the drawing is made of separate further along x than along y: a mirror about the vertical axis, + /// or the gap between two items side by side. The ink's own box says nothing here (Lucide's flip is taller than it + /// is wide), so the reading is the separation of those two marks. + AlongX, + /// The same along y. + AlongY, + /// The ink's box is wider than tall: one mark across the axis of a scale, either a bar with outward arrows or a box + /// whose width outward arrows span. + ExtentX, + /// The same along y. + ExtentY, + /// The longest straight edge runs along x: a skew, whose leaning sides leave the top and bottom level. Tabler draws + /// both of its skew files wider than tall, so their boxes cannot tell the pair apart; the straight edges can. + LeansX, + /// The same along y. + LeansY, + /// One filled band, off centre towards this side, which is a sign on x and y: a padding. + Side(i32, i32), +} + +/// The transform and layout glyphs this change adds, with the reading each one's drawing must have. +const ADDED: [(&str, Reads); 12] = [ + ("flip_horizontal", Reads::AlongX), + ("flip_vertical", Reads::AlongY), + ("scale_horizontal", Reads::ExtentX), + ("scale_vertical", Reads::ExtentY), + ("skew_horizontal", Reads::LeansX), + ("skew_vertical", Reads::LeansY), + ("padding_top", Reads::Side(0, -1)), + ("padding_right", Reads::Side(1, 0)), + ("padding_bottom", Reads::Side(0, 1)), + ("padding_left", Reads::Side(-1, 0)), + ("gap_horizontal", Reads::AlongX), + ("gap_vertical", Reads::AlongY), +]; + +/// One painted path of a drawing: its box including the stroke, its longest straight edge along x and along y, and +/// whether it is filled. +struct Painted { + rect: [f32; 4], + straight: [f32; 2], + filled: bool, +} + +impl Painted { + /// The centre of the box. + fn centre(&self) -> (f32, f32) { + ((self.rect[0] + self.rect[2]) / 2.0, (self.rect[1] + self.rect[3]) / 2.0) + } + + fn area(&self) -> f32 { + (self.rect[2] - self.rect[0]).max(0.0) * (self.rect[3] - self.rect[1]).max(0.0) + } +} + +/// The path nodes of `group` that paint, depth first. A path with neither a fill nor a stroke paints nothing (Tabler +/// ships one as a placeholder) and is left out, and so is one drawn hidden. +fn drawn_paths(group: &Group) -> Vec<&Path> { + fn walk<'a>(group: &'a Group, out: &mut Vec<&'a Path>) { + for node in group.children() { + match node { + Node::Group(inner) => walk(inner, out), + Node::Path(path) if path.is_visible() && (path.fill().is_some() || path.stroke().is_some()) => out.push(path.as_ref()), + _ => {} + } + } + } + let mut out = Vec::new(); + walk(group, &mut out); + out +} + +/// Every painted path of `group`, read: its box including the stroke, its straight edges, and whether it is filled. +fn painted(group: &Group) -> Vec { + drawn_paths(group) + .into_iter() + .map(|path| { + let rect = path.abs_stroke_bounding_box(); + Painted { rect: [rect.left(), rect.top(), rect.right(), rect.bottom()], straight: straight_runs(path), filled: path.fill().is_some() } + }) + .collect() +} + +/// The longest straight edge of `path` along x and along y, in the coordinates the drawing renders in: its segments +/// placed by the path's absolute transform, the one the renderer draws it with. +fn straight_runs(path: &Path) -> [f32; 2] { + straight_edges(path.data(), path.abs_transform()) +} + +/// The longest straight edge of `data` along x and along y once `transform` has placed its points, in the units that +/// transform gives. usvg keeps a path's segments in the element's own coordinates and leaves every transform above them, +/// a ``, the element's own and the root view box's scale, on the node's absolute transform for the renderer +/// to apply, so `data` alone is not where the drawing lands: `usvg::Path::data`'s "all segments are in absolute +/// coordinates" means that the commands are absolute (`M`, `L`, not `m`, `l`), never that they are in canvas coordinates. +/// An edge is along an axis when, drawn, one of its deltas is zero; a curve is read as its end point, which no edge of +/// these drawings depends on. An identity `transform` reads the document as written. This is the reading +/// `craft_svg::tree::document` takes when it converts a parsed tree into a document. +fn straight_edges(data: &tiny_skia_path::Path, transform: Transform) -> [f32; 2] { + let placed = |mut point: tiny_skia_path::Point| { + transform.map_point(&mut point); + (point.x, point.y) + }; + let mut runs = [0.0f32; 2]; + let mut from = (0.0f32, 0.0f32); + let mut start = from; + for segment in data.segments() { + let to = match segment { + PathSegment::MoveTo(point) => { + from = placed(point); + start = from; + continue; + } + PathSegment::Close => start, + PathSegment::LineTo(point) | PathSegment::QuadTo(.., point) | PathSegment::CubicTo(.., point) => placed(point), + }; + let (dx, dy) = ((to.0 - from.0).abs(), (to.1 - from.1).abs()); + if dy <= STRAIGHT { + runs[0] = runs[0].max(dx); + } else if dx <= STRAIGHT { + runs[1] = runs[1].max(dy); + } + from = to; + } + runs +} + +/// How far a drawing spreads along x and along y: the separation of its two largest painted paths (a flip and a gap are +/// two marks either side of the axis they name), or the box of its one path (a scale and a skew each draw one mark +/// across their axis). +fn spread(painted: &[Painted]) -> (f32, f32) { + let mut ranked: Vec<&Painted> = painted.iter().collect(); + ranked.sort_by(|a, b| b.area().total_cmp(&a.area())); + match ranked.as_slice() { + [first, second, ..] => { + let ((ax, ay), (bx, by)) = (first.centre(), second.centre()); + ((ax - bx).abs(), (ay - by).abs()) + } + [only] => (only.rect[2] - only.rect[0], only.rect[3] - only.rect[1]), + [] => (0.0, 0.0), + } +} + +/// Asserts that the drawing `name` gives in `set` reads as `reads` says, from its painted paths and its view box. +fn drawn_as(name: &str, set: &str, reads: Reads, ink: &[Painted], view: (f32, f32)) { + let tenth = view.0.max(view.1) / 10.0; + match reads { + Reads::AlongX | Reads::AlongY => { + let (dx, dy) = spread(ink); + let along_x = matches!(reads, Reads::AlongX); + let (along, across) = if along_x { (dx, dy) } else { (dy, dx) }; + assert!(along > across, "`{name}` in `{set}` spreads {dx} along x and {dy} along y, not along the axis its name gives"); + assert!(along >= tenth, "`{name}` in `{set}` spreads only {along} along its axis, under a tenth of the drawing"); + } + Reads::ExtentX | Reads::ExtentY => { + let left = ink.iter().map(|shape| shape.rect[0]).fold(f32::INFINITY, f32::min); + let top = ink.iter().map(|shape| shape.rect[1]).fold(f32::INFINITY, f32::min); + let right = ink.iter().map(|shape| shape.rect[2]).fold(f32::NEG_INFINITY, f32::max); + let bottom = ink.iter().map(|shape| shape.rect[3]).fold(f32::NEG_INFINITY, f32::max); + let (wide, tall) = (right - left, bottom - top); + let along_x = matches!(reads, Reads::ExtentX); + let (along, across) = if along_x { (wide, tall) } else { (tall, wide) }; + assert!(along > across, "`{name}` in `{set}` spans {wide} along x and {tall} along y, not the axis its name gives"); + assert!(along >= tenth, "`{name}` in `{set}` spans only {along} along its axis, under a tenth of the drawing"); + } + Reads::LeansX | Reads::LeansY => { + let leans_x = matches!(reads, Reads::LeansX); + let (mut along, mut across) = (0.0f32, 0.0f32); + for shape in ink { + let (long, short) = if leans_x { (shape.straight[0], shape.straight[1]) } else { (shape.straight[1], shape.straight[0]) }; + along = along.max(long); + across = across.max(short); + } + let axis = if leans_x { "x" } else { "y" }; + assert!( + along > across, + "`{name}` in `{set}` has its longest straight edges across the lean it names: {along} along {axis}, {across} the other way" + ); + assert!(along >= tenth, "`{name}` in `{set}` has no straight edge along {axis} a tenth of the drawing long"); + } + Reads::Side(dx, dy) => { + let bands: Vec<&Painted> = ink.iter().filter(|shape| shape.filled).collect(); + assert_eq!(bands.len(), 1, "`{name}` in `{set}` fills {} parts: the space it names is one solid band", bands.len()); + let band = bands[0]; + assert!(band.area() > 0.0, "`{name}` in `{set}` fills an empty band"); + let (x, y) = band.centre(); + let (centre_x, centre_y) = (view.0 / 2.0, view.1 / 2.0); + // A tenth of the drawing off centre towards its side, and a twentieth the other way: the band is on one + // side rather than in a corner. + let (reach, slack) = (view.0.min(view.1) / 10.0, view.0.min(view.1) / 20.0); + let towards = if dx != 0 { (x - centre_x) * dx as f32 } else { (y - centre_y) * dy as f32 }; + let across = if dx != 0 { y - centre_y } else { x - centre_x }; + assert!(towards >= reach, "`{name}` in `{set}` puts its band {towards} off centre towards its side, short of {reach}"); + assert!(across.abs() <= slack, "`{name}` in `{set}` puts its band {} off the other axis", across.abs()); + } + } +} + +/// The reading above is taken where a drawing lands, not where its `d` is written, which is what a glyph drawn sideways +/// inside a `` exposes: the bar written along x in `ROTATED` draws along y, and `straight_runs` must say so. +/// +/// The bar's own coordinates still read along x, and `straight_edges` with an identity transform is kept as the reading +/// of the document as written; that is the reading this trial would compare against if the segments were not placed by +/// the path's absolute transform, so this case fails the old measurement and passes the drawn one. `FLAT` writes that +/// same edge where the rotation lands it, with no transform to apply, and the two documents must read alike. +fn edges_are_read_in_the_coordinates_the_drawing_renders_in() { + let rotated = svg_tree::parse(ROTATED.as_bytes()).expect("the rotated fixture parses"); + let flat = svg_tree::parse(FLAT.as_bytes()).expect("the flattened fixture parses"); + let bars = drawn_paths(rotated.root()); + assert_eq!(bars.len(), 1, "the rotated fixture draws one bar"); + let drawn = straight_runs(bars[0]); + assert!( + drawn[1] >= BAR - STRAIGHT && drawn[0] <= STRAIGHT, + "a bar written along x inside two transformed groups that rotate it draws along y, but its edges read {drawn:?}" + ); + let written = straight_edges(bars[0].data(), Transform::identity()); + assert!( + written[0] >= BAR - STRAIGHT && written[1] <= STRAIGHT, + "the coordinates the rotated bar's `d` is written in read {written:?}, along x: that is not an axis the drawing has" + ); + let flats = drawn_paths(flat.root()); + assert_eq!(flats.len(), 1, "the flattened fixture draws one bar"); + let flattened = straight_runs(flats[0]); + assert!( + flattened[1] >= BAR - STRAIGHT && (flattened[0] - drawn[0]).abs() <= STRAIGHT && (flattened[1] - drawn[1]).abs() <= STRAIGHT, + "the bar written where the rotation lands it reads {flattened:?} and the rotated one {drawn:?}: both draw the same edge" + ); + // The catalogue's own check on that drawing, which only reads right once the segments are placed: it fails on the + // untransformed reading, where the longest straight edge is 16 along x and nothing along y. + let view = (rotated.size().width(), rotated.size().height()); + drawn_as("the rotated bar", "a fixture", Reads::LeansY, &painted(rotated.root()), view); +} + +#[trial] +fn the_added_transform_and_layout_glyphs_draw_where_their_names_say() { + edges_are_read_in_the_coordinates_the_drawing_renders_in(); + for set in IconSet::ALL { + let mut documents: BTreeMap<&'static [u8], &str> = BTreeMap::new(); + for (name, reads) in ADDED { + let index = find(name).unwrap_or_else(|| panic!("`{name}` is not in the catalogue")); + assert!(!DIRECTIONAL[index], "`{name}` names a side or an axis of the screen, so it must not mirror"); + let document = svg(index, set); + let tree = svg_tree::parse(document).unwrap_or_else(|error| panic!("`{name}` in `{}`: {error}", set.id())); + let view = tree.size(); + let ink = painted(tree.root()); + assert!(!ink.is_empty(), "`{name}` in `{}` paints nothing", set.id()); + // The ink, stroke and caps included: a cap past the view box is a cropped glyph. + let bounds = tree.root().abs_stroke_bounding_box(); + assert!( + bounds.left() >= -SLACK + && bounds.top() >= -SLACK + && bounds.right() <= view.width() + SLACK + && bounds.bottom() <= view.height() + SLACK, + "`{name}` in `{}` paints outside its view box: {bounds:?} of {view:?}", + set.id() + ); + drawn_as(name, set.id(), reads, &ink, (view.width(), view.height())); + if let Some(other) = documents.insert(document, name) { + panic!("`{other}` and `{name}` are the same document in `{}`: a copied drawing?", set.id()); + } + } + } +}