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
46 changes: 33 additions & 13 deletions crates/ui-egui/src/layoutview/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use frame::*;
use legend::*;
use north::*;
use rustgis_engine::map::layout::{
Background, ElementKind, INK, LAYOUT_TEXT_SIZE, Layout, MAX_BACKGROUND_PADDING, MAX_BACKGROUND_RADIUS, MAX_BACKGROUND_WIDTH,
Orientation,
Element, ElementKind, 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 @@ -245,16 +244,15 @@ pub(super) fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li:
// A legend's, colorbar's, table's or chart's ink (#2257), on and off as a background
// is: off is the default dark grey, which isn't the same as a dark grey ink (a table
// keeps its white cells only without one). Turned on, it starts white over a dark
// background and dark grey otherwise, so its text never disappears.
// colour and dark grey otherwise (`first_ink`), so its text never disappears.
if e.kind.takes_ink() {
let ink = e.kind.ink();
let input_label = ui.label(tl!("Ink"));
ui.horizontal(|ui| {
let mut on = ink.is_some();
let hint = tl!("Its own colour for its text and lines; off, the default dark grey");
if ui.checkbox(&mut on, "").labelled_by(input_label.id).on_hover_text(hint).changed() {
let page = layout.paper.unwrap_or(rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF));
update(app, json!({"color": on.then(|| first_ink(e.background.as_ref(), page).hex())}));
update(app, json!({"color": on.then(|| first_ink(layout, &e).hex())}));
}
let Some(current) = ink else { return };
let mut c = crate::theme::c32(current);
Expand Down Expand Up @@ -382,18 +380,40 @@ 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, its background drawn on the page (`page`, the page's colour, #2855), when that is dark,
/// else the default dark grey, so its text never disappears.
pub(super) fn first_ink(background: Option<&Background>, page: rustgis_engine::map::Rgba) -> rustgis_engine::map::Rgba {
// No background: nothing drawn over the page, so the page itself shows under the text.
let fill = background.map_or(rustgis_engine::map::Rgba::new(0, 0, 0, 0), |b| b.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);
let shown = Color32::from_rgb(over(fill.r(), page.r()), over(fill.g(), page.g()), over(fill.b(), page.b()));
/// 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.
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);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let fills = 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);
Color32::from_rgb(over(fill.r(), below.r()), over(fill.g(), below.g()), over(fill.b(), below.b()))
});
let white = rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF);
if crate::theme::contrast(shown, Color32::WHITE) > crate::theme::contrast(shown, crate::theme::c32(INK)) { white } else { INK }
}

/// 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.
fn fills_at(e: &Element, (x, y): (f64, f64)) -> impl Iterator<Item = rustgis_engine::map::Rgba> {
let (x, y) = e.unturned(x, y);
let inside = |(bx, by, bw, bh): (f64, f64, f64, f64)| x >= bx && x <= bx + bw && y >= by && y <= by + bh;
let background = e.background.filter(|_| inside(e.background_box())).map(|b| b.fill);
// The point from the box's centre, in half widths and half heights: within 1 of it, the ellipse.
let from_centre = (((x - e.x) / e.w) * 2.0 - 1.0).hypot(((y - e.y) / e.h) * 2.0 - 1.0);
let shape = match e.kind {
ElementKind::Rectangle { fill, .. } if inside((e.x, e.y, e.w, e.h)) => Some(fill),
ElementKind::Ellipse { fill, .. } if from_centre <= 1.0 => Some(fill),
_ => None,
};
background.into_iter().chain(shape)
}

/// A row of a layout element's text size in points, within [`LAYOUT_TEXT_SIZE`], labelled `label`
/// (#2581): the size it settles on when it differs from `current`.
pub(super) fn text_size_row(ui: &mut egui::Ui, label: impl Into<egui::WidgetText>, key: egui::Id, current: f64) -> Option<f64> {
Expand Down
127 changes: 114 additions & 13 deletions crates/ui-egui/src/layoutview/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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 serde_json::json;
use std::{cell::Cell, rc::Rc};

Expand All @@ -24,26 +26,125 @@ fn frame_approximations_are_said_once_a_kind() {
assert_eq!(n, 1);
}

/// A table at (10, 10, 40, 40) mm, centred on (30, 30), on a page of `paper`: the element whose
/// ink starts, over whatever `under` adds before it and under whatever `over` adds after it.
fn inked(paper: Rgba, background: Option<Rgba>, under: impl FnOnce(&mut Layout), over: impl FnOnce(&mut Layout)) -> Rgba {
let mut layout = Layout::new("Ink", 297.0, 210.0);
layout.paper = Some(paper);
under(&mut layout);
let id = layout.add("Table", (10.0, 10.0, 40.0, 40.0), ElementKind::Table { frame: 1, layer: "Cities".into(), rows: 3, color: None });
layout.element_mut(id).unwrap().background = background.map(Background::filled);
over(&mut layout);
first_ink(&layout, layout.element(id).unwrap())
}

const PAGE: Rgba = Rgba::rgb(0xFF, 0xFF, 0xFF);
const NAVY: Rgba = Rgba::rgb(0x0B, 0x1B, 0x2B);
const DARK: Rgba = Rgba::rgb(0x1E, 0x29, 0x3B);
const CLEAR: Rgba = Rgba::new(0, 0, 0, 0);

fn rectangle(fill: Rgba) -> ElementKind {
ElementKind::Rectangle { fill, outline: INK, width: 0.0, radius: 0.0, dash: None }
}

fn nothing(_: &mut Layout) {}

/// A rectangle of `fill` covering the whole table.
fn covering(fill: Rgba) -> impl FnOnce(&mut Layout) {
move |l| {
l.add("Box", (0.0, 0.0, 100.0, 100.0), rectangle(fill));
}
}

/// Ink starts white only over a dark background, so turning it on never hides the text (#2257).
#[test]
fn ink_starts_readable_on_its_background() {
use rustgis_engine::map::Rgba;
use rustgis_engine::map::layout::{Background, INK};
let white = Rgba::rgb(0xFF, 0xFF, 0xFF);
let on = |fill: Rgba| first_ink(Some(&Background { fill, ..serde_json::from_value(json!({})).unwrap() }), white);
assert_eq!(first_ink(None, white), INK);
assert_eq!(on(white), INK);
assert_eq!(on(Rgba::rgb(0x1E, 0x29, 0x3B)), white);
let on = |fill| inked(PAGE, Some(fill), nothing, nothing);
assert_eq!(inked(PAGE, None, nothing, nothing), INK);
assert_eq!(on(PAGE), INK);
assert_eq!(on(DARK), PAGE);
// A dark fill seen through mostly: the page shows, so it reads as light.
assert_eq!(on(Rgba::new(0x1E, 0x29, 0x3B, 0x20)), INK);
assert_eq!(on(Rgba::new(0x1E, 0x29, 0x3B, 0xE0)), white);
assert_eq!(on(Rgba::new(0x1E, 0x29, 0x3B, 0xE0)), PAGE);
// On a dark page (#2855): white with no background, or over a fill the page shows through;
// dark grey over a light fill.
let navy = Rgba::rgb(0x0B, 0x1B, 0x2B);
let on_navy = |fill: Rgba| first_ink(Some(&Background { fill, ..serde_json::from_value(json!({})).unwrap() }), navy);
assert_eq!(first_ink(None, navy), white);
assert_eq!(on_navy(Rgba::new(0xFF, 0xFF, 0xFF, 0x20)), white);
assert_eq!(on_navy(white), INK);
assert_eq!(inked(NAVY, None, nothing, nothing), PAGE);
assert_eq!(inked(NAVY, Some(Rgba::new(0xFF, 0xFF, 0xFF, 0x20)), nothing, nothing), PAGE);
assert_eq!(inked(NAVY, Some(PAGE), nothing, nothing), INK);
}

/// The fills shown under an element, in drawing order, decide its starting ink (#2953).
#[test]
fn ink_starts_readable_over_the_elements_under_it() {
assert_eq!(inked(NAVY, None, covering(PAGE), nothing), INK, "a white box on a dark page");
assert_eq!(inked(PAGE, None, covering(DARK), nothing), PAGE, "a dark box on a white page");
assert_eq!(inked(PAGE, Some(PAGE), covering(DARK), nothing), INK, "its own background is over the box");
assert_eq!(inked(PAGE, None, covering(Rgba::new(0x1E, 0x29, 0x3B, 0x20)), nothing), INK, "the page shows through");
let two = |l: &mut Layout| {
covering(DARK)(l);
covering(PAGE)(l);
};
assert_eq!(inked(PAGE, None, two, nothing), INK, "the later box is over the earlier one");
assert_eq!(inked(PAGE, None, nothing, covering(DARK)), INK, "a box drawn over it isn't under its text");
let hidden = |l: &mut Layout| {
covering(DARK)(l);
l.elements[0].visible = false;
};
assert_eq!(inked(PAGE, None, hidden, nothing), INK, "a hidden box shows nothing");
let aside = |l: &mut Layout| {
l.add("Box", (0.0, 0.0, 25.0, 25.0), rectangle(DARK));
};
assert_eq!(inked(PAGE, None, aside, nothing), INK, "a box short of the centre");
}

/// An ellipse covers only its own shape, and a background reaches as far as its padding and
/// turns with its text (#2953).
#[test]
fn ink_reads_each_shape_as_drawn() {
let ellipse = |rect| {
move |l: &mut Layout| {
l.add("Ellipse", rect, ElementKind::Ellipse { fill: DARK, outline: INK, width: 0.0, dash: None });
}
};
assert_eq!(inked(PAGE, None, ellipse((0.0, 0.0, 60.0, 60.0)), nothing), PAGE, "the centre is inside");
// The centre (30, 30) is in this ellipse's box, near its corner, outside the ellipse.
assert_eq!(inked(PAGE, None, ellipse((28.0, 28.0, 60.0, 60.0)), nothing), INK, "only in its box");
let padded = |padding| {
move |l: &mut Layout| {
let id = l.add("Box", (0.0, 0.0, 25.0, 60.0), rectangle(CLEAR));
l.element_mut(id).unwrap().background = Some(Background { padding, ..Background::filled(DARK) });
}
};
assert_eq!(inked(PAGE, None, padded(10.0), nothing), PAGE, "reached by the padding");
assert_eq!(inked(PAGE, None, padded(0.0), nothing), INK);
// 10 by 60 mm centred on (50, 30): turned a quarter, 60 by 10, over (30, 30).
let turned = |turn: f64| {
move |l: &mut Layout| {
let mut kind = ElementKind::text("N", 8.0, INK, false);
if let ElementKind::Text { rotation, .. } = &mut kind {
*rotation = turn;
}
let id = l.add("Text", (45.0, 0.0, 10.0, 60.0), kind);
l.element_mut(id).unwrap().background = Some(Background::filled(DARK));
}
};
assert_eq!(inked(PAGE, None, turned(90.0), nothing), PAGE, "turned over the centre");
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.
#[test]
fn ink_reads_no_map_or_picture() {
let picture = |l: &mut Layout| {
l.add("Picture", (0.0, 0.0, 100.0, 100.0), ElementKind::Picture { image: String::new() });
};
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!(inked(NAVY, None, map, nothing), PAGE);
}

/// In the browser, a page drawn while its frames' tiles were on their way is drawn again as
Expand Down
21 changes: 11 additions & 10 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,16 +667,17 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
`layout.update={"element":4,"background":{"fill":"#FFFFFFCC","radius":1,"padding":1.5}}`; the keys
not given keep theirs, and `"background":null` removes it. `layout.addElement` takes it too. A
map frame draws its own map and paper, so a background on one is refused.
- **Ink**: a legend, a colorbar, a table and a chart draw their text and lines in dark grey; turn
on **Ink** in the Format pane and pick its colour to read them on a dark page or over imagery.
It starts white over a dark background and dark grey otherwise, and 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.
- **Ink**: a legend, a colorbar, a table and a chart draw their text and lines in dark grey; turn on
**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.
- **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