Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions crates/render/src/layout/legend/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,31 @@ impl Ink {
}

/// The symbol and stacked strokes a block draws for `s` with `strokes` at the reference factor
/// `k`. A marker with no outline that shows gets one in `ink`: its symbol's, or each mark of a
/// stack none of whose marks has one (a stack that outlines any is drawn as it is). A marker sized
/// zero stays without ink.
/// `k`. Each marker that shows and has no outline of its own gets one in `ink`: the symbol's, or
/// each such mark of its stack. One sized zero or with a fully transparent fill gets none.
fn edged<'a>(
s: &rustgis_map::Symbol,
strokes: &'a [rustgis_map::LineStroke],
k: f32,
ink: Rgba,
) -> (rustgis_map::Symbol, Cow<'a, [rustgis_map::LineStroke]>) {
let bare = |size: f32, fill: Rgba, outline: Option<Rgba>, width: f32| {
size > 0.0 && fill.a() > 0 && !(outline.is_some_and(|c| c.a() > 0) && width > 0.0)
};
let (mut symbol, mut stack) = (*s, Cow::Borrowed(strokes));
if k <= 0.0 {
return (symbol, stack);
}
if point_marks::plain(strokes, 0.0) {
if (s.stroke.a() == 0 || s.stroke_width <= 0.0) && s.size > 0.0 {
if bare(s.size, s.fill, Some(s.stroke), s.stroke_width) {
(symbol.stroke, symbol.stroke_width) = (ink, EDGE / k);
}
return (symbol, stack);
}
let outlined = strokes
.iter()
.any(|st| st.markers.is_some_and(|m| m.outline.is_some_and(|c| c.a() > 0)) && st.width.unwrap_or(s.stroke_width) > 0.0);
if !outlined {
for st in stack.to_mut() {
if let Some(m) = &mut st.markers
&& matches!(m.mark, rustgis_map::LineMark::Marker(_))
&& m.size > 0.0
{
(m.outline, st.width) = (Some(ink), Some(EDGE / k));
}
for st in stack.to_mut() {
let (fill, width) = (st.color.unwrap_or(s.fill), st.width.unwrap_or(s.stroke_width));
if let Some(m) = &mut st.markers
&& matches!(m.mark, rustgis_map::LineMark::Marker(_))
&& bare(m.size, fill, m.outline, width)
{
(m.outline, st.width) = (Some(ink), Some(EDGE / k));
}
}
(symbol, stack)
Expand Down Expand Up @@ -124,8 +119,8 @@ pub(super) fn places(sides: &[f32], line: f32) -> (f32, Vec<f32>, f32) {
/// Draws a block ([`block`]) of the layer `l` from `y` down: its markers in the column of swatches
/// `column` pixels wide from `x`, over one another from the largest, composited as one when the
/// layer is see-through (`opacity`); then each one's leader, from its top to its label at
/// `text_x`. A marker with no edge of its own gets one in the legend's `ink`, or nested ones of
/// one fill couldn't be told apart. `size` times the default, `ppp` pixels a point.
/// `text_x`. A marker with no edge of its own gets one in the legend's `ink`, so that nested
/// markers of one fill can be told apart. `size` times the default, `ppp` pixels a point.
pub(super) fn draw(
pm: &mut Canvas,
(x, y, column): (f32, f32, f32),
Expand Down
130 changes: 126 additions & 4 deletions crates/render/src/layout/legend/nested/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,100 @@ fn a_stacked_mark_sized_zero_takes_no_height() {
assert_eq!((block[0].1.height(), block[0].1.side), (0.0, 0.0));
}

/// A stroke of one red marker of `shape`, `size` points across, with the outline `outline` 1 point
/// wide.
fn stacked(shape: rustgis_map::LineMark, size: f32, outline: Option<Rgba>) -> rustgis_map::LineStroke {
let mark = rustgis_map::LineMarkers { mark: shape, size, outline, ..Default::default() };
rustgis_map::LineStroke { color: Some(RED), width: Some(1.0), markers: Some(mark), ..Default::default() }
}

/// A "+" of two hash marks, 10 points long and `width` wide.
fn plus(width: f32) -> Vec<rustgis_map::LineStroke> {
[0.0, 90.0]
.map(|angle| {
let mut stroke = stacked(rustgis_map::LineMark::Hash, 10.0, None);
stroke.width = Some(width);
if let Some(m) = &mut stroke.markers {
m.angle = angle;
}
stroke
})
.to_vec()
}

#[test]
fn every_mark_stands_on_the_base_and_its_leader_meets_its_top() {
use rustgis_map::{Glyph, LineMark, Marker};
let mut map = circles(10.0, 40.0, 0.0);
let mut shifted = stacked(LineMark::Marker(Marker::Circle), 20.0, Some(RED));
shifted.offset = 3.0;
let cases = [
("circle", vec![stacked(LineMark::Marker(Marker::Circle), 20.0, Some(RED))]),
("triangle", vec![stacked(LineMark::Marker(Marker::Triangle), 20.0, Some(RED))]),
("character", vec![stacked(LineMark::Marker(Marker::Char(Glyph::new('A', ""))), 20.0, Some(RED))]),
("offset", vec![shifted]),
("hash plus", plus(2.0)),
];
// The leaders and labels in no colour: all that shows is the marker.
let none = Rgba::new(0, 0, 0, 0);
for (name, strokes) in cases {
for reference in [None, Some(2e6)] {
map.layers[0].reference_scale = reference;
let l = &map.layers[0];
let mut entry = l.renderer.legend().remove(0);
entry.strokes = strokes.clone();
let LegendRow::Sizes(_, block) = block(l, vec![entry], 1e6) else { panic!() };
let (base, _, _) = places(&heights(&block), LINE);
let height = block[0].1.height();
for opacity in [1.0, 0.5] {
let mut pm = Pixmap::new(800, 800).unwrap();
pm.fill(tiny_skia::Color::WHITE);
crate::canvas::with_canvas(&mut pm, |c| {
draw(c, (0.0, 0.0, 400.0), (l, &block), (opacity, 1e6), (1.0, 4.0), (500.0, none));
});
let rows: Vec<u32> = (0..800).filter(|&y| (0..800).any(|x| pm.pixel(x, y).unwrap().green() < 235)).collect();
let (top, bottom) = (rows[0] as f32, rows[rows.len() - 1] as f32 + 1.0);
let want = ((base - height) * 4.0, base * 4.0);
assert!(
(top - want.0).abs() <= 2.0 && (bottom - want.1).abs() <= 2.0,
"{name}, k {reference:?}, opacity {opacity}: {top} {bottom}, want {want:?}"
);
}
}
}
}

#[test]
fn a_hash_marks_ink_is_as_long_as_it_is_drawn() {
let map = circles(10.0, 40.0, 0.0);
let l = &map.layers[0];
// Wide, and a hairline.
for width in [2.0, 0.0] {
let mut entry = l.renderer.legend().remove(0);
entry.strokes = plus(width);
let LegendRow::Sizes(_, block) = block(l, vec![entry], 1e6) else { panic!() };
let height = block[0].1.height();
assert!((height - 10.0).abs() < 0.1, "width {width}: {height}");
}
}

#[test]
fn a_stack_with_no_outline_gets_one_in_the_legends_ink_and_an_outlined_one_stays() {
fn a_see_through_block_is_composited_as_one() {
let mut map = circles(10.0, 40.0, 1.0);
map.layers[0].transparency = 50.0;
let pm = drawn_at(&map, LegendSizes::Nested, 4);
let reddish = |x, y| pm.pixel(x, y).is_some_and(|p| p.red() > p.green().saturating_add(40));
let rows: Vec<u32> = (0..pm.height()).filter(|&y| (0..pm.width()).any(|x| reddish(x, y))).collect();
let cols: Vec<u32> = (0..pm.width()).filter(|&x| (0..pm.height()).any(|y| reddish(x, y))).collect();
let (bottom, axis) = (rows[rows.len() - 1], (cols[0] + cols[cols.len() - 1]) / 2);
// Left of the axis, where no leader runs: a point in all three circles, 5 points above the base,
// and one in the largest alone, 30 points above it.
let (all, one) = (pm.pixel(axis - 8, bottom - 20).unwrap(), pm.pixel(axis - 12, bottom - 120).unwrap());
assert_eq!(all, one);
}

#[test]
fn a_mark_that_shows_and_has_no_outline_gets_one_in_the_legends_ink() {
use rustgis_map::{LineMark, LineMarkers, LineStroke, Marker};
let ink = Rgba::rgb(1, 2, 3);
let mark = |size, outline| LineStroke {
Expand All @@ -253,7 +345,37 @@ fn a_stack_with_no_outline_gets_one_in_the_legends_ink_and_an_outlined_one_stays
assert_eq!(edged_marks[0].width, Some(EDGE / 2.0));
// One sized zero stays without ink.
assert_eq!(edged_marks[1].markers.unwrap().outline, None);
// A stack that outlines any of its marks is drawn as it is.
let designed = [mark(20.0, Some(RED)), mark(10.0, None)];
assert_eq!(edged(&symbol, &designed, 1.0, ink).1.as_ref(), &designed);
// Beside an outlined mark too, which stays as it is.
let mixed = [mark(20.0, None), mark(6.0, Some(INK))];
let (_, edged_marks) = edged(&symbol, &mixed, 1.0, ink);
assert_eq!(edged_marks[0].markers.unwrap().outline, Some(ink));
assert_eq!(edged_marks[1], mixed[1]);
// An outline no width draws is none.
let mut hairless = mark(20.0, Some(RED));
hairless.width = Some(0.0);
assert_eq!(edged(&symbol, &[hairless], 1.0, ink).1[0].markers.unwrap().outline, Some(ink));
}

#[test]
fn a_mark_that_does_not_show_gets_no_edge() {
use rustgis_map::{LineMark, Marker};
let map = circles(10.0, 40.0, 0.0);
let l = &map.layers[0];
let circle = LineMark::Marker(Marker::Circle);
// A see-through spacer under a red dot: the block is as tall as the dot and its edge.
let mut spacer = stacked(circle, 30.0, None);
spacer.color = Some(Rgba::new(0, 0, 0, 0));
let mut dot = stacked(circle, 10.0, None);
dot.width = Some(0.0);
let mut entry = l.renderer.legend().remove(0);
entry.strokes = vec![spacer, dot];
assert_eq!(edged(&entry.symbol, &entry.strokes, 1.0, INK).1[0], spacer);
let LegendRow::Sizes(_, stack) = block(l, vec![entry], 1e6) else { panic!() };
assert!((stack[0].1.height() - 10.5).abs() < 0.1, "{}", stack[0].1.height());
// A plain marker with no fill and no outline: no ink.
let mut entry = l.renderer.legend().remove(0);
entry.symbol.fill = Rgba::new(0, 0, 0, 0);
assert_eq!(edged(&entry.symbol, &[], 1.0, INK).0, entry.symbol);
let LegendRow::Sizes(_, plain) = block(l, vec![entry], 1e6) else { panic!() };
assert_eq!(plain[0].1.height(), 0.0);
}
29 changes: 19 additions & 10 deletions crates/render/src/point_marks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ fn marks(sym: &Symbol, strokes: &[LineStroke]) -> Vec<Mark> {
.collect()
}

/// The line a mark is outlined with, `width` wide: a hash mark's ends are flat.
fn pen(width: f32, hash: bool) -> Stroke {
let line_cap = if hash { LineCap::Butt } else { LineCap::Round };
Stroke { width, line_join: LineJoin::Round, line_cap, ..Stroke::default() }
}

/// How far the ink of a point of symbol `sym` reaches above and below it (points), unturned, by
/// the paths [`draw`] draws: `strokes`' marks, else its marker. `None` when no mark has ink.
pub(crate) fn ink_reach(sym: &Symbol, strokes: &[LineStroke]) -> Option<(f32, f32)> {
Expand Down Expand Up @@ -92,13 +98,18 @@ pub(crate) fn ink_reach(sym: &Symbol, strokes: &[LineStroke]) -> Option<(f32, f3
pb.finish()
}
};
let outline = m.outline.filter(|(c, w)| c.a() > 0 && *w > 0.0);
if picture.is_none() && outline.is_none() && (hash || m.fill.a() == 0) {
return None;
}
let bounds = path?.transform(t)?.bounds();
let half = outline.filter(|_| picture.is_none()).map_or(0.0, |(_, w)| w / 2.0);
Some((half - bounds.top(), bounds.bottom() + half))
let path = path?;
let filled = picture.is_some() || (!hash && m.fill.a() > 0);
// The outline's own ink, as it is stroked: a hash's flat ends stop at its length. One
// of no width is drawn as a hairline.
let outline =
m.outline.filter(|(c, _)| picture.is_none() && c.a() > 0).and_then(|(_, w)| path.stroke(&pen(w.max(0.01), hash), 1.0));
[filled.then_some(path), outline]
.into_iter()
.flatten()
.filter_map(|p| p.transform(t)?.compute_tight_bounds())
.map(|b| (-b.top(), b.bottom()))
.reduce(|a, b| (a.0.max(b.0), a.1.max(b.1)))
})
.reduce(|a, b| (a.0.max(b.0), a.1.max(b.1)))
}
Expand Down Expand Up @@ -152,9 +163,7 @@ pub(crate) fn draw(pm: &mut Canvas, x: f32, y: f32, sym: &Symbol, strokes: &[Lin
pm.fill_path(&path, &paint(m.fill, 1.0), FillRule::Winding, t, None);
}
if let Some((c, w)) = m.outline {
let cap = if hash { LineCap::Butt } else { LineCap::Round };
let st = Stroke { width: (w * ppp).max(0.5), line_join: LineJoin::Round, line_cap: cap, ..Stroke::default() };
pm.stroke_path(&path, &paint(c, 1.0), &st, t, None);
pm.stroke_path(&path, &paint(c, 1.0), &pen((w * ppp).max(0.5), hash), t, None);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
layer's marker sizes, those of Proportional Symbols and those a layer sized by a field lists
after its classes. `"rows"`, the default, gives each its own row. `"nested"` draws a layer's
as one block: the markers one inside another, the largest first, on a common base, each with a
thin line from its top to its value. A marker with no outline (a stack of
markers, when none of them has one) gets a thin one in the legend's ink. The markers keep the
size the map draws them at, and the block is never split between columns. Class rows stay as
rows.
thin line from its top to its value. A marker that shows and has no
outline (each such mark of a stack of markers) gets a thin one in the legend's ink. The markers
keep the size the map draws them at, and the block is never split between columns. Class rows
stay as rows.
- **Layer names** (`"showLayerNames"`, on by default): off, the name over each layer's classes,
and over a web layer's legend picture, is left off. A layer drawn in one symbol keeps its name
beside its swatch, since that is what labels it; classes that carry no label of their own
Expand Down
Loading