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
6 changes: 6 additions & 0 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ pub fn picture_size(b64: &str) -> Option<(u32, u32)> {
picture::size(b64)
}

/// The pixel size a base64 picture is drawn from, `None` when a layout draws a placeholder instead.
/// Decodes the whole image, as drawing it does: [`picture_size`] reads only the header.
pub fn picture_drawn_size(b64: &str) -> Option<(u32, u32)> {
picture::decode(b64).map(|pm| (pm.width(), pm.height()))
}

pub use picture::{MARKER_PIXELS, MARKER_PIXELS_SIDE, MarkerRefusal, RUN_MARKER_BYTES, RUN_MARKER_PIXELS, SVG_DEPTH, SVG_XML_NODES};

/// Base64 `b64` as a picture marker's image (one kept already, or one before it is set): its
Expand Down
58 changes: 54 additions & 4 deletions crates/ui-egui/src/layoutview/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use frame::*;
use legend::*;
use north::*;
use rustgis_engine::map::layout::{
Element, ElementKind, INK, LAYOUT_TEXT_SIZE, Layout, MAX_BACKGROUND_PADDING, MAX_BACKGROUND_RADIUS, MAX_BACKGROUND_WIDTH, Orientation,
Element, ElementKind, FrameShape, INK, LAYOUT_TEXT_SIZE, Layout, MAX_BACKGROUND_PADDING, MAX_BACKGROUND_RADIUS, MAX_BACKGROUND_WIDTH,
Orientation,
};
use rustgis_engine::map::symbology::Dash;
use scalebar::*;
Expand Down Expand Up @@ -382,12 +383,15 @@ pub(super) fn choice(ui: &mut Ui, salt: (&str, u32), label: egui::Id, current: &
/// The ink an element starts with when Ink is turned on (#2257): white over what shows under its
/// text when that is dark, else the default dark grey, so its text never disappears. What shows is
/// read at the centre of its box: the page's colour (#2855), the fills drawn there by the visible
/// elements under it (#2953), then its own background.
/// elements under it (#2953), those under a map or a picture hidden by it, then its own background.
pub(super) fn first_ink(layout: &Layout, e: &Element) -> rustgis_engine::map::Rgba {
let centre = (e.x + e.w / 2.0, e.y + e.h / 2.0);
let page = layout.paper.unwrap_or(rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF));
let under = layout.elements.iter().take_while(|u| u.id != e.id).filter(|u| u.visible);
let fills = under.flat_map(|u| fills_at(u, centre)).chain(e.background.map(|b| b.fill));
let under = layout.elements.split(|u| u.id == e.id).next().unwrap_or_default();
// What lies under the topmost map or picture there doesn't show.
let from = under.iter().rposition(|u| u.visible && hides(u, centre)).map_or(0, |i| i + 1);
let shown_under = under.iter().skip(from).filter(|u| u.visible);
let fills = shown_under.flat_map(|u| fills_at(u, centre)).chain(e.background.map(|b| b.fill));
let shown = fills.fold(Color32::from_rgb(page.r(), page.g(), page.b()), |below, fill| {
let a = u16::from(fill.a());
let over = |c: u8, under: u8| u8::try_from((u16::from(c) * a + u16::from(under) * (255 - a)) / 255).unwrap_or(u8::MAX);
Expand All @@ -397,6 +401,52 @@ pub(super) fn first_ink(layout: &Layout, e: &Element) -> rustgis_engine::map::Rg
if crate::theme::contrast(shown, Color32::WHITE) > crate::theme::contrast(shown, crate::theme::c32(INK)) { white } else { INK }
}

/// Whether `e` is a map or a picture covering a page point (mm): what lies under it there doesn't
/// show, and it has no one colour of its own. A map covers its shape, a picture its image as drawn,
/// fitted and centred in its box (transparent pixels counted as covering), or the whole box where
/// it doesn't decode and a placeholder is drawn. Only a picture whose box holds the point is
/// decoded.
fn hides(e: &Element, (x, y): (f64, f64)) -> bool {
// The point in the box's unit square, and from the box's centre in millimetres.
let (u, v) = ((x - e.x) / e.w, (y - e.y) / e.h);
let (dx, dy) = (x - e.x - e.w / 2.0, y - e.y - e.h / 2.0);
let in_box = (0.0..=1.0).contains(&u) && (0.0..=1.0).contains(&v);
match &e.kind {
ElementKind::Picture { image } => {
in_box
&& rustgis_engine::render::picture_drawn_size(image).is_none_or(|(iw, ih)| {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let s = (e.w / f64::from(iw)).min(e.h / f64::from(ih));
dx.abs() <= f64::from(iw) * s / 2.0 && dy.abs() <= f64::from(ih) * s / 2.0
})
}
ElementKind::MapFrame { shape, .. } => match shape {
FrameShape::Rectangle => in_box,
FrameShape::Circle => dx.hypot(dy) <= e.w.min(e.h) / 2.0,
FrameShape::Ellipse => (dx / e.w).hypot(dy / e.h) <= 0.5,
FrameShape::Polygon(points) => winds_around(points, (u, v)),
},
_ => false,
}
}

/// Whether the closed ring `points` winds around the point, as a frame's shape is filled.
fn winds_around(points: &[(f64, f64)], (x, y): (f64, f64)) -> bool {
let edges = points.iter().zip(points.iter().cycle().skip(1));
let turns: i64 = edges
.map(|(&(ax, ay), &(bx, by))| {
let left = (bx - ax) * (y - ay) - (x - ax) * (by - ay);
if ay <= y && by > y && left > 0.0 {
1
} else if ay > y && by <= y && left < 0.0 {
-1
} else {
0
}
})
.sum();
turns != 0
}

/// The fills `e` draws at a page point (mm), the lower first: its background's, rounded corners
/// counted as square, then a rectangle's or an ellipse's own. Maps, pictures, text and lines have
/// no one fill to read.
Expand Down
77 changes: 67 additions & 10 deletions crates/ui-egui/src/layoutview/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
use crate::RustGisApp;
use egui::{Event, Key, Modifiers, PointerButton, Pos2, Rect, vec2};
use rustgis_engine::map::Rgba;
use rustgis_engine::map::layout::{Background, ElementKind, INK, Layout};
use rustgis_engine::map::layout::{Background, ElementKind, FrameShape, INK, Layout};
use serde_json::json;
use std::{cell::Cell, rc::Rc};

Expand Down Expand Up @@ -132,19 +132,76 @@ fn ink_reads_each_shape_as_drawn() {
assert_eq!(inked(PAGE, None, turned(0.0), nothing), INK);
}

/// A map or a picture under an element has no one fill to read: the page decides, as before.
/// A map or a picture under an element has no one fill to read, and hides whatever lies under it
/// where it covers: the page decides, as before.
#[test]
fn ink_reads_no_map_or_picture() {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let picture = |l: &mut Layout| {
l.add("Picture", (0.0, 0.0, 100.0, 100.0), ElementKind::Picture { image: String::new() });
let picture = |rect| {
move |l: &mut Layout| {
l.add("Picture", rect, ElementKind::Picture { image: String::new() });
}
};
let map = |rect, shape: FrameShape| {
move |l: &mut Layout| {
let mut kind: ElementKind =
serde_json::from_value(json!({"MapFrame": {"map": "Map", "center": {"x": 0.0, "y": 0.0}, "scale": 1000000.0}})).unwrap();
if let ElementKind::MapFrame { shape: s, .. } = &mut kind {
*s = shape;
}
l.add("Map", rect, kind);
}
};
let full = (0.0, 0.0, 100.0, 100.0);
assert_eq!(inked(PAGE, None, picture(full), nothing), INK);
assert_eq!(inked(NAVY, None, picture(full), nothing), PAGE);
assert_eq!(inked(NAVY, None, map(full, FrameShape::Rectangle), nothing), PAGE);
// Whether a dark box under `over` still counts.
let boxed = |over: Box<dyn FnOnce(&mut Layout)>| {
let under = |l: &mut Layout| {
covering(DARK)(l);
over(l);
};
inked(PAGE, None, under, nothing)
};
assert_eq!(inked(PAGE, None, picture, nothing), INK);
assert_eq!(inked(NAVY, None, picture, nothing), PAGE);
let map = |l: &mut Layout| {
let kind = serde_json::from_value(json!({"MapFrame": {"map": "Map", "center": {"x": 0.0, "y": 0.0}, "scale": 1000000.0}}));
l.add("Map", (0.0, 0.0, 100.0, 100.0), kind.unwrap());
assert_eq!(boxed(Box::new(picture(full))), INK, "hidden by a picture");
assert_eq!(boxed(Box::new(map(full, FrameShape::Rectangle))), INK, "hidden by a map");
assert_eq!(boxed(Box::new(picture((60.0, 60.0, 20.0, 20.0)))), PAGE, "a picture elsewhere");
// An image 4 by 1 pixels, drawn fitted and centred in its box: over (30, 30) in a box 60 mm
// tall, and leaving it bare in one 100 mm tall.
let image = |h: f64, data: &'static str| {
move |l: &mut Layout| {
l.add("Picture", (0.0, 0.0, 100.0, h), ElementKind::Picture { image: data.into() });
}
};
let wide = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAABCAIAAAB2XpiaAAAAC0lEQVR4nGP4jwQATb8L9davaZUAAAAASUVORK5CYII=";
assert_eq!(boxed(Box::new(image(60.0, wide))), INK, "hidden by the image");
assert_eq!(boxed(Box::new(image(100.0, wide))), PAGE, "beside the image, in its box");
// The same size declared over data that doesn't decode: its placeholder covers the box.
let broken = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAABCAIAAAB2XpiaAAAACElEQVRub3QgemxpYlVpEfcAAAAASUVORK5CYII=";
assert_eq!(boxed(Box::new(image(100.0, broken))), INK, "hidden by a placeholder");
// The centre (30, 30) is in this box, near its corner: outside its circle and its ellipse.
let corner = (28.0, 28.0, 60.0, 60.0);
assert_eq!(boxed(Box::new(map(corner, FrameShape::Circle))), PAGE, "outside the circle");
assert_eq!(boxed(Box::new(map(corner, FrameShape::Ellipse))), PAGE, "outside the ellipse");
assert_eq!(boxed(Box::new(map(full, FrameShape::Circle))), INK, "inside the circle");
assert_eq!(boxed(Box::new(map(full, FrameShape::Ellipse))), INK, "inside the ellipse");
// A circle is round in a wide box too: 100 by 40 mm about (52, 30), its circle spans x 32 to 72.
assert_eq!(boxed(Box::new(map((2.0, 10.0, 100.0, 40.0), FrameShape::Circle))), PAGE, "beside the circle");
let triangle = |side: f64| FrameShape::Polygon(vec![(0.0, 0.0), (side, 0.0), (0.0, side)]);
assert_eq!(boxed(Box::new(map(full, triangle(1.0)))), INK, "inside the polygon");
assert_eq!(boxed(Box::new(map(full, triangle(0.4)))), PAGE, "outside the polygon");
let reversed = FrameShape::Polygon(vec![(0.0, 0.0), (0.0, 1.0), (1.0, 0.0)]);
assert_eq!(boxed(Box::new(map(full, reversed))), INK, "inside a polygon wound the other way");
let hidden = |l: &mut Layout| {
map(full, FrameShape::Rectangle)(l);
l.elements.last_mut().unwrap().visible = false;
};
assert_eq!(boxed(Box::new(hidden)), PAGE, "a hidden map hides nothing");
let over_map = |l: &mut Layout| {
map(full, FrameShape::Rectangle)(l);
covering(DARK)(l);
};
assert_eq!(inked(NAVY, None, map, nothing), PAGE);
assert_eq!(inked(PAGE, None, over_map, nothing), PAGE, "a box over a map counts");
}

/// In the browser, a page drawn while its frames' tiles were on their way is drawn again as
Expand Down
15 changes: 8 additions & 7 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,14 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
**Ink** in the Format pane and pick its colour to read them on a dark page or over imagery. It
starts white when the colour shown at the element's centre is dark, and dark grey otherwise: that
colour is the page's, with the backgrounds, rectangles and ellipses under the element and its own
background over it (maps and pictures aren't read). Turned off it is the dark grey again. A
legend's title, layer names and class labels, a colorbar's title, outline, ticks and labels, a
table's text and a chart's title and labels take it; a chart's bars and a colorbar's colours keep
theirs. A table with an ink draws no white cells of its own, only its header and every other row
in a light tint of the ink, so it reads on whatever is behind it: give it a **Background** for a
box. From a command: `layout.update={"element":5,"color":"#FFFFFF"}`, and `"color":null` for the
default; `layout.addElement` takes it too.
background over it (a map or a picture there isn't read and hides what is under it). Turned off it
is the dark grey again. A legend's title, layer names and class labels, a colorbar's title,
outline, ticks and labels, a table's text and a chart's title and labels take it; a chart's bars
and a colorbar's colours keep theirs. A table with an ink draws no white cells of its own, only
its header and every other row in a light tint of the ink, so it reads on whatever is behind it:
give it a **Background** for a box. From a command:
`layout.update={"element":5,"color":"#FFFFFF"}`, and `"color":null` for the default;
`layout.addElement` takes it too.
- **Chart frame**: a bar chart of a layer of the frame's map. Its properties pick the **Layer**
and the numeric **Field**, and by default draw a bar per feature, the largest first, each
labelled by the **Label** field. **Group by** a field draws a bar per value of that field
Expand Down
Loading