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
24 changes: 12 additions & 12 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use std::sync::LazyLock;

use rustgis_geom::{Coord, Extent};
use rustgis_map::layout::{
Arrange, ArrowStyle, Background, CHART_BAR, CHART_BARS, CHART_OUTLINE, ChartOrder, ChartOutline, ClassLabels, CoordFormat, ElementKind,
ExtentIndicator, FeatureCounts, FontFamily, FrameShape, GRID_LABEL_PT, Grid, GridEdges, GridKind, GridStyle, Guides, LAYOUT_TEXT_SIZE,
LINE_SPACING, LayerNames, Layout, LegendFit, LegendSizes, LineDir, LineHead, MAX_CHART_BARS, MAX_CHART_DECIMALS,
MAX_CHART_OUTLINE_WIDTH, MAX_COLORBAR_TICKS, MAX_GRID_WIDTH, MAX_GUIDES, MAX_LEGEND_COLUMNS, MAX_LEGEND_LABEL_CHARS, MAX_LEGEND_LABELS,
MAX_LINE_SPACING, MAX_SCALE_BEFORE_ZERO, MAX_SCALE_DECIMALS, MAX_SCALE_DIVISIONS, MAX_SCALE_SUBDIVISIONS, MAX_SECTIONS, MAX_TICK_MM,
MAX_UNIT_LABEL_CHARS, MIN_ELEMENT_MM, MIN_LINE_SPACING, MapSeries, Orientation, PAGES, SCALE_BAR_TEXT_PT, ScaleBarFit, ScaleBarStyle,
ScaleLabelPosition, ScaleUnits, Section, SectionLine, TABLE_TEXT_PT, TextAlign, TextCase, TextVAlign, TickSide, UnitLabelPosition,
fit_scale, line_angle, line_at_angle, line_length,
Arrange, ArrowStyle, Background, CHART_BAR, CHART_BARS, CHART_OUTLINE, COLORBAR_TEXT_PT, ChartOrder, ChartOutline, ClassLabels,
CoordFormat, ElementKind, ExtentIndicator, FeatureCounts, FontFamily, FrameShape, GRID_LABEL_PT, Grid, GridEdges, GridKind, GridStyle,
Guides, LAYOUT_TEXT_SIZE, LINE_SPACING, LayerNames, Layout, LegendFit, LegendSizes, LineDir, LineHead, MAX_CHART_BARS,
MAX_CHART_DECIMALS, MAX_CHART_OUTLINE_WIDTH, MAX_COLORBAR_DECIMALS, MAX_COLORBAR_TICKS, MAX_GRID_WIDTH, MAX_GUIDES, MAX_LEGEND_COLUMNS,
MAX_LEGEND_LABEL_CHARS, MAX_LEGEND_LABELS, MAX_LINE_SPACING, MAX_SCALE_BEFORE_ZERO, MAX_SCALE_DECIMALS, MAX_SCALE_DIVISIONS,
MAX_SCALE_SUBDIVISIONS, MAX_SECTIONS, MAX_TICK_MM, MAX_UNIT_LABEL_CHARS, MIN_ELEMENT_MM, MIN_LINE_SPACING, MapSeries, Orientation,
PAGES, SCALE_BAR_TEXT_PT, ScaleBarFit, ScaleBarStyle, ScaleLabelPosition, ScaleUnits, Section, SectionLine, TABLE_TEXT_PT, TextAlign,
TextCase, TextVAlign, TickSide, UnitLabelPosition, fit_scale, line_angle, line_at_angle, line_length,
};
use rustgis_map::report::StatKind;
use rustgis_map::symbology::{Dash, MAX_DASH_STEP, MAX_DASH_STEPS};
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn commands() -> Vec<C> {
r##""layerOrder"?: [layer names] (legend: these layers first, in this order, the others after them as the map draws them; the map is not changed), "##,
r##""columnBreaks"?: [layer names] (legend: each of these layers starts a column, 5 at most, so "columns" above 1 and the fits that add columns are refused beside them), "##,
r##""columns"?: 1..6 (legend: the columns its rows are laid in; default 1; with a fit that adds columns, the fewest it starts from), "##,
r##""textSize"?: points, 4 to 100 (legend: its layers' names and classes' labels, its title, rows, swatches and gaps following in proportion and a point symbol keeping the size the map draws it at; default 9; scaleBar: its labels, the room over the bar and between them following, the bar keeping its size; default 8; table: its text, its rows and columns following; default 8; refused outside that range, and for other kinds unless null, as showNoData is), "##,
r##""textSize"?: points, 4 to 100 (legend: its layers' names and classes' labels, its title, rows, swatches and gaps following in proportion and a point symbol keeping the size the map draws it at; default 9; scaleBar: its labels, the room over the bar and between them following, the bar keeping its size; default 8; table: its text, its rows and columns following; default 8; colorbar: its labels, the bar making room for them; default 8; refused outside that range, and for other kinds unless null, as showNoData is), "##,
r##""classLabels"?: {"<layer>": {"<class label>": "<text>"}} (legend: its own text for a class, by the label the layer shows for it, up to 200 characters; the layer keeps its label; a layer or class it can't find is refused), "##,
r##""sizes"?: "rows" (default) | "nested" (legend: a point layer's rows of marker sizes drawn as one block, the markers one inside another on a common base, each led to its value; refused for other kinds), "##,
r##""showCounts"?: bool (legend: each class row, and a one-symbol layer's row, ends in the number of features the layer's filters keep; default false; none for rasters, web layers, size and diagram rows, layers read in place other than from a local Shapefile, GeoPackage, FlatGeobuf or GeoParquet (each of those is read whole to count), or Unique Values by an expression over too many features and classes; refused for other kinds), "##,
Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn commands() -> Vec<C> {
r##""other"?: bool (chart: the bars past "bars" gathered in a last Other bar, its statistic over their features, their sum when not grouped, last whatever the order; default false: left out, which a warning says, as it says features with no number left out), "##,
r##""stack"?: a field (chart, grouped by another field, summing or counting: each bar split by this field's values, a stretch per value in the key over the bars, the values its bars show by their total, largest first (A to Z when ordered by category), past 8 values 7 and a grey Other, which a warning counts; in the palette diagrams take, or with classes in the colour each value's features share on the map, a value with several keeping its palette colour, which a warning counts; values written are the bar's total; default none), "##,
r##""values"?: bool (chart: each bar's value written past its end, its thousands apart with commas; default false), "##,
r##""decimals"?: "auto" (the default: a chart's values up to 2, the zeros they end in dropped; a scale bar's labels each the decimals it needs, as before)|0..6 (chart: its values' decimals)|0..3 (scaleBar: its labels' decimals, fixed, with the thousands set apart), "##,
r##""decimals"?: "auto" (the default: a chart's values up to 2, the zeros they end in dropped; a scale bar's labels each the decimals it needs, as before)|0..6 (chart: its values' decimals)|0..3 (scaleBar: its labels' decimals, fixed, with the thousands set apart)|0..6 (colorbar: its labels' decimals, fixed, with the thousands set apart; "auto", its default, shortens classes' and values' labels), "##,
r##""barColor"?: "#RRGGBB"|"#RRGGBBAA" (chart: its bars' colour, default #3B82C4), "##,
r##""classes"?: bool (chart: each bar in the colour its features have on the map, by the layer's symbology classes: a bar per feature its feature's, a grouped bar the colour its features share; a bar whose features have several, which a warning counts, and an Other bar keep barColor; default false), "##,
r##""labelAlign"?: "left" (the default)|"right" (chart: bars running across have their labels at the left of their column, or at its right against the bars), "##,
Expand Down Expand Up @@ -340,7 +340,7 @@ pub fn commands() -> Vec<C> {
r##""labels"?: {"<layer>": "<label>"|null} (legend: what it shows instead of a layer's name, up to 200 characters, a longer one is refused; each entry replaces that layer's label, null or "" shows its name again, {} every layer's; a layer the map hasn't is refused), "##,
r##""classLabels"?: {"<layer>": {"<class label>": "<text>"|null}|null} (legend, as in layout.addElement; each entry replaces that class's text, null or "" shows the layer's label again, a layer's null or {} all of its classes', {} every layer's), "##,
r##""columns"?: 1..6 (legend), "##,
r##""textSize"?: points, 4 to 100 (legend, scaleBar, table, as in layout.addElement, null giving each its default again; refused outside that range, and for other elements unless null), "##,
r##""textSize"?: points, 4 to 100 (legend, scaleBar, table, colorbar, as in layout.addElement, null giving each its default again; refused outside that range, and for other elements unless null), "##,
r##""showLayerNames"?: bool (legend, as in layout.addElement; null leaves it as it is), "##,
r##""sizes"?: "rows"|"nested" (legend, as in layout.addElement; null leaves it as it is), "##,
r##""showCounts"?: bool (legend, as in layout.addElement; null leaves it as it is), "##,
Expand Down Expand Up @@ -385,7 +385,7 @@ pub fn commands() -> Vec<C> {
r##""other"?, "##,
r##""stack"? (chart, as in layout.addElement: "" for a label, group or stack takes it away; on a new layer a label, group or stack field it hasn't is let go, a stack the chart can no longer draw (ungrouped, grouped by the stack field, or not summing or counting) too, and a field not named that it no longer measures gives way to the first numeric field, which warnings say; refused for other elements), "##,
r##""values"?: bool, "##,
r##""decimals"?: 0..6|"auto" (a scaleBar's 0..3|"auto", as in layout.addElement), "##,
r##""decimals"?: 0..6|"auto" (a scaleBar's 0..3|"auto" and a colorbar's, as in layout.addElement), "##,
r##""barColor"? (null giving the default again), "##,
r##""classes"?: bool, "##,
r##""labelAlign"?: "left"|"right", "##,
Expand Down
25 changes: 20 additions & 5 deletions crates/engine/src/cmd/layout/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub(in crate::cmd::layout) fn gives_chart_options(p: &Value) -> bool {
}

/// Whether `p` gives an element that isn't a chart the options only a chart takes: a scale bar
/// takes `decimals` as its own, in the same shape.
pub(in crate::cmd::layout) fn gives_chart_options_to(p: &Value, scale_bar: bool) -> bool {
gives_chart_data(p) || CHART_LOOK_KEYS.iter().any(|k| !(scale_bar && *k == "decimals") && gives(p, k))
/// and a colorbar (`decimals_too`) take `decimals` as their own, in the same shape.
pub(in crate::cmd::layout) fn gives_chart_options_to(p: &Value, decimals_too: bool) -> bool {
gives_chart_data(p) || CHART_LOOK_KEYS.iter().any(|k| !(decimals_too && *k == "decimals") && gives(p, k))
}

/// Whether `p` tells a chart what to draw, which it says its losses of again.
Expand Down Expand Up @@ -66,6 +66,21 @@ pub(in crate::cmd::layout) fn given_count_from(p: &Value, k: &str, lo: usize, hi
}
}

/// A scale bar's or colorbar's `"decimals"`, when given (and not null): `"auto"`, as none, or a
/// whole number from 0 to `most`, given as the counts are.
pub(in crate::cmd::layout) fn given_decimals(p: &Value, most: usize) -> Result<Option<Option<u8>>, String> {
match p.get("decimals").filter(|v| !v.is_null()) {
None => Ok(None),
Some(v) if v.as_str().is_some_and(|t| t.trim().eq_ignore_ascii_case("auto")) => Ok(Some(None)),
Some(_) => given_count(p, "decimals", most)
.ok()
.flatten()
.and_then(|n| u8::try_from(n).ok())
.map(|n| Some(Some(n)))
.ok_or_else(|| format!("decimals is \"auto\" or a whole number from 0 to {most}")),
}
}

/// The name given as `k`, when it's given (and not null): an error when it's not text. The caller
/// says which name it doesn't know, in its own words.
pub(in crate::cmd::layout) fn given_name<'a>(p: &'a Value, k: &str, names: &str) -> Result<Option<&'a str>, String> {
Expand Down Expand Up @@ -352,8 +367,8 @@ pub(in crate::cmd::layout) fn chart_update(
..
} = &e.kind
else {
let bar = matches!(e.kind, ElementKind::ScaleBar { .. });
return if gives_chart_options_to(p, bar) { Err(ONLY_CHART_OPTIONS.into()) } else { Ok(None) };
let decimals_too = matches!(e.kind, ElementKind::ScaleBar { .. } | ElementKind::Colorbar { .. });
return if gives_chart_options_to(p, decimals_too) { Err(ONLY_CHART_OPTIONS.into()) } else { Ok(None) };
};
if checked.is_none() && !gives_chart_options(p) && !changes_chart_look(p) {
return Ok(None);
Expand Down
8 changes: 6 additions & 2 deletions crates/engine/src/cmd/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu
if kind == "profile" {
return add_profile(s, p);
}
if !matches!(kind, "legend" | "scaleBar" | "table") && p.get("textSize").is_some_and(|v| !v.is_null()) {
if !matches!(kind, "legend" | "scaleBar" | "table" | "colorbar") && p.get("textSize").is_some_and(|v| !v.is_null()) {
return Err(ONLY_LAYOUT_TEXT_SIZE.into());
}
if kind != "table" && (gives(p, "fields") || gives(p, "header")) {
Expand Down Expand Up @@ -405,7 +405,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu
if !matches!(kind, "text" | "dynamicText") && gives_text_block(p) {
return Err(ONLY_TEXT_BLOCK.into());
}
if kind != "chart" && gives_chart_options_to(p, kind == "scaleBar") {
if kind != "chart" && gives_chart_options_to(p, matches!(kind, "scaleBar" | "colorbar")) {
return Err(ONLY_CHART_OPTIONS.into());
}
if kind != "section" && (gives(p, "direction") || gives(p, "at")) {
Expand Down Expand Up @@ -550,6 +550,8 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu
tick_side: tick_side(p)?.unwrap_or_default(),
reverse: b(p, "reverse").unwrap_or(false),
color: ink,
text_size: text_size_param(p, "textSize", COLORBAR_TEXT_PT)?.unwrap_or(COLORBAR_TEXT_PT),
decimals: given_decimals(p, usize::from(MAX_COLORBAR_DECIMALS))?.flatten(),
};
("Colorbar", place(p, l, rect)?, k)
}
Expand Down Expand Up @@ -1166,6 +1168,8 @@ const ADD_COLORBAR_KEYS: &[&str] = &[
"tickSide",
"reverse",
"color",
"textSize",
"decimals",
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
];

/// Keys read by `layout.addElement` for a text or a dynamic text (#3074).
Expand Down
2 changes: 2 additions & 0 deletions crates/engine/src/cmd/layout/publication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,8 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
tick_side: TickSide::End,
reverse: false,
color: None,
text_size: COLORBAR_TEXT_PT,
decimals: None,
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/layout/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub(crate) fn align_footer(
}
}

pub(in crate::cmd::layout) const ONLY_LAYOUT_TEXT_SIZE: &str = "only legends, scale bars and tables take textSize";
pub(in crate::cmd::layout) const ONLY_LAYOUT_TEXT_SIZE: &str = "only legends, scale bars, tables and colorbars take textSize";

/// Says how many lines of the text `id` of the layout `li` don't fit its box (#2346), as the
/// layout view and an export count them: on the series' current page, measured at 300 dpi as a
Expand Down
27 changes: 14 additions & 13 deletions crates/engine/src/cmd/layout/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,7 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result<O
}
// As a chart's: "auto", the decimals each label needs, or that many with the thousands
// apart, a whole number given as the counts are; null leaves them as they are.
let decimals = match p.get("decimals").filter(|v| !v.is_null()) {
None => *decimals,
Some(v) if v.as_str().is_some_and(|t| t.trim().eq_ignore_ascii_case("auto")) => None,
Some(_) => given_count(p, "decimals", MAX_SCALE_DECIMALS)
.ok()
.flatten()
.and_then(|n| u8::try_from(n).ok())
.map(Some)
.ok_or_else(|| format!("decimals is \"auto\" or a whole number from 0 to {MAX_SCALE_DECIMALS}"))?,
};
let decimals = given_decimals(p, MAX_SCALE_DECIMALS)?.unwrap_or(*decimals);
let label_position =
given_choice(p, "labelPosition", ScaleLabelPosition::from_name, "above or below")?.unwrap_or(*label_position);
let unit_label_position =
Expand Down Expand Up @@ -516,6 +507,8 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult {
let kind_of = s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind);
let is_legend = kind_of.is_some_and(|k| matches!(k, ElementKind::Legend { .. }));
let is_table = kind_of.is_some_and(|k| matches!(k, ElementKind::Table { .. }));
let is_colorbar = kind_of.is_some_and(|k| matches!(k, ElementKind::Colorbar { .. }));
let new_decimals = if is_colorbar { given_decimals(p, usize::from(MAX_COLORBAR_DECIMALS))? } else { None };
let sized = is_legend || kind_of.is_some_and(|k| matches!(k, ElementKind::ScaleBar { .. }));
// A legend's fit; a scale bar's is read with its other options (`restyle`), by its own names.
let new_fit = if is_legend { legend_fit_param(p)? } else { None };
Expand All @@ -536,18 +529,20 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult {
return Err(ONLY_LEGEND_SIZES.into());
}
let new_sizes = legend_sizes_param(p)?;
if p.get("textSize").is_some_and(|v| !v.is_null()) && !sized && !is_table {
if p.get("textSize").is_some_and(|v| !v.is_null()) && !sized && !is_table && !is_colorbar {
return Err(ONLY_LAYOUT_TEXT_SIZE.into());
}
// A grid's label size is one of its options, given in "grid".
if p.get("labelSize").is_some_and(|v| !v.is_null()) {
return Err(ONLY_GRID_LABEL_SIZE.into());
}
// A legend's or a table's; a scale bar's is set with its other options.
// A legend's, a table's or a colorbar's; a scale bar's is set with its other options.
let new_text_size = if is_legend {
text_size_param(p, "textSize", rustgis_map::layout::LEGEND_TEXT_PT)?
} else if is_table {
text_size_param(p, "textSize", TABLE_TEXT_PT)?
} else if is_colorbar {
text_size_param(p, "textSize", COLORBAR_TEXT_PT)?
} else {
None
};
Expand Down Expand Up @@ -754,7 +749,13 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult {
*show_no_data = v;
}
}
ElementKind::Colorbar { layer, title, orientation, ticks: n, tick_side, reverse, .. } => {
ElementKind::Colorbar { layer, title, orientation, ticks: n, tick_side, reverse, text_size, decimals, .. } => {
if let Some(v) = new_text_size {
*text_size = v;
}
if let Some(v) = new_decimals {
*decimals = v;
}
if let Some(t) = new_title {
*title = t;
}
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub const CAPABILITIES: &[&str] = &[
"layout-chart-facing",
"layout-chart-outline",
"layout-table-look",
"layout-colorbar-labels",
"field-generation",
"field-package",
FIELD_CONFLICTS,
Expand Down
Loading
Loading