Skip to content
Draft
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
2 changes: 1 addition & 1 deletion crates/engine/command-surface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ labels.fonts search?
labels.lock on?
labels.move layer? class? feature to
labels.pause on?
labels.placement layer? class? position? to? side? offset? repeat? lineOffset? otherPositions? fit? abbreviations? showAll? weight? callout? guide?
labels.placement layer? class? position? to? side? offset? repeat? lineOffset? otherPositions? fit? abbreviations? showAll? weight? clearance? callout? guide?
labels.resetMoved layer? class? all?
labels.scaleRange layer? class? min? max? clear?
labels.style layer? class? size? color? halo? haloColor? haloWidth? spacing? stretch? bold? italic? font? background? wrapChar? wrapLength? lineAlign?
Expand Down
30 changes: 27 additions & 3 deletions crates/engine/src/cmd/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ pub fn commands() -> Vec<C> {
// A position that isn't a placement name is refused, not dropped for the others (null:
// none, as for the other keys).
None if p.get("position").is_some_and(|v| !v.is_null()) => return Err("position must be a placement name".into()),
None if ["offset", "repeat", "lineOffset", "otherPositions", "showAll", "weight", "callout", "to", "side", "fit", "abbreviations", "guide"]
None if ["offset", "repeat", "lineOffset", "otherPositions", "showAll", "weight", "clearance", "callout", "to", "side", "fit", "abbreviations", "guide"]
.iter()
.any(|k| p.get(k).is_some()) =>
{
None
}
None => {
return Err("position is required (or an offset, a repeat, a lineOffset, otherPositions, showAll, a weight, a callout, to, side, fit, abbreviations or a guide)".into());
return Err("position is required (or an offset, a repeat, a lineOffset, otherPositions, showAll, a weight, a clearance, a callout, to, side, fit, abbreviations or a guide)".into());
}
};
// Along a line placed curved, the label fitted to the room the line gives (#2265).
Expand Down Expand Up @@ -213,6 +213,15 @@ pub fn commands() -> Vec<C> {
};
let weight_asked = weight.filter(|w| w.fract() != 0.0 || !rustgis_map::symbology::LABEL_WEIGHT.contains(w));
let weight = weight.map(rustgis_map::symbology::label_weight);
// The room its labels keep free of other labels (#2828): 0 to 100 points, null back to
// 2. A number out of range is held to it, and says so.
let clearance = match p.get("clearance") {
None => None,
Some(Value::Null) => Some(f64::from(rustgis_map::symbology::DEFAULT_LABEL_CLEARANCE)),
Some(v) => Some(number(v).ok_or("clearance must be a number of points from 0 to 100 (null: 2)")?),
};
let clearance_asked = clearance.filter(|c| !rustgis_map::symbology::LABEL_CLEARANCE.contains(c));
let clearance = clearance.map(rustgis_map::symbology::label_clearance);
// Every label shown, over the others where they crowd.
let show_all = match p.get("showAll") {
None => None,
Expand Down Expand Up @@ -299,6 +308,9 @@ pub fn commands() -> Vec<C> {
if let Some(w) = weight {
c.weight = w;
}
if let Some(r) = clearance {
c.clearance = r;
}
if let Some(f) = fit {
c.fit = f;
}
Expand Down Expand Up @@ -376,12 +388,24 @@ pub fn commands() -> Vec<C> {
false,
);
}
if let Some(asked) = clearance_asked {
let name = s.map().layer(id).map(|l| l.name.clone()).unwrap_or_default();
let kept = rustgis_map::symbology::label_clearance(asked);
let asked = if asked.abs() < 1e6 { asked.to_string() } else { format!("{asked:e}") };
s.notify_text(
NoticeText::format(
"{name}: clearance {asked} adjusted to {kept} (a label's clearance is from 0 to 100 points)",
[("name", name.into()), ("asked", asked.into()), ("kept", kept.to_string().into())],
),
false,
);
}
Ok(done)
}).roles(&[]).effects(crate::policy::Effects::NONE)
.inputs(super::Inputs::single_layer)
.when(need_feature_layer)
.params(
r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "position"?: "auto"|"center"|"above"|"below"|"left"|"right"|"aboveRight"|"aboveLeft"|"belowRight"|"belowLeft"|"parallel" (along a line, straight)|"curved" (along a line, following its bends)|"perpendicular" (a point's, square to the nearest line of `to`, starting at the point; none that near: as auto) (required without offset, repeat, lineOffset, otherPositions, showAll, weight, callout, to, side, fit, abbreviations or guide), "to"?: {"layer": a line or polygon layer's name, "within"?: points (1 to 500; 20)} | null, "side"?: "away" (the side of the line the point isn't on; the default)|"left"|"right" (as the line runs), "offset"?: [right, down] points (-200 to 200 each) the label moves from where its position puts it ([0, 0]: none; perpendicular: [out along the normal, down as the text reads]; a label along a line isn't moved), "repeat"?: points (10 to 10000) between a line's labels, spaced out from the one at its middle, at most 32 a line (0 or null: the one at its middle; parallel and curved only), "lineOffset"?: points (-200 to 200) between a line and its label's nearest edge, above the line as the text reads, negative below it (0 or null: on the line; parallel and curved only), "otherPositions"?: bool (a label whose place is taken tries others: a point's round the point, corners first; a line's along the line, nearest its middle first, placed parallel or curved without a repeat; true is refused for any other line label and on polygons), "fit"?: "auto"|"off" (curved along a line: "auto" abbreviates a name that can't follow the line, sets one longer than a third of it on its straightest stretch, and spreads one over labels.style's stretch of a line more than three times its length; "off" or null: as placed), "abbreviations"?: {word: abbreviation} (whole words a fitted label puts short, ignoring case, over the built-in English, Portuguese and Spanish list, as {"Creek": "Cr.", "Córrego": "Cór."}; null or {}: the built-in list alone), "showAll"?: bool (every label drawn, over the others where they crowd), "weight"?: 0 to 10 (where labels compete for a place, the heavier class's wins, whatever its layer's place; equal weights: the layer above, then the class listed first; a fraction rounded; null: 5), "callout"?: true|false|points (5 to 500: a label crowded off its spot moves to a free one nearby, a line drawn back to its feature at most this long; true keeps its length, or 40; false, 0 or null: none; not labels along a line or with an icon), "guide"?: {"layer": a line layer's name (as ridge or valley lines; not drawn by this), "field"?: a field both layers have} | null (points and polygons, not perpendicular ones: each label curved along its guide line, the one with the feature's value in field, else the longest in view under the label inside its polygon, or the nearest to its point within the text's width; a feature with none its text fits along is labelled as without a guide; null: none)}"#,
r#"{"layer"?, "class"?: name (none: the Default class, by whatever name it has), "position"?: "auto"|"center"|"above"|"below"|"left"|"right"|"aboveRight"|"aboveLeft"|"belowRight"|"belowLeft"|"parallel" (along a line, straight)|"curved" (along a line, following its bends)|"perpendicular" (a point's, square to the nearest line of `to`, starting at the point; none that near: as auto) (required without offset, repeat, lineOffset, otherPositions, showAll, weight, clearance, callout, to, side, fit, abbreviations or guide), "to"?: {"layer": a line or polygon layer's name, "within"?: points (1 to 500; 20)} | null, "side"?: "away" (the side of the line the point isn't on; the default)|"left"|"right" (as the line runs), "offset"?: [right, down] points (-200 to 200 each) the label moves from where its position puts it ([0, 0]: none; perpendicular: [out along the normal, down as the text reads]; a label along a line isn't moved), "repeat"?: points (10 to 10000) between a line's labels, spaced out from the one at its middle, at most 32 a line (0 or null: the one at its middle; parallel and curved only), "lineOffset"?: points (-200 to 200) between a line and its label's nearest edge, above the line as the text reads, negative below it (0 or null: on the line; parallel and curved only), "otherPositions"?: bool (a label whose place is taken tries others: a point's round the point, corners first; a line's along the line, nearest its middle first, placed parallel or curved without a repeat; true is refused for any other line label and on polygons), "fit"?: "auto"|"off" (curved along a line: "auto" abbreviates a name that can't follow the line, sets one longer than a third of it on its straightest stretch, and spreads one over labels.style's stretch of a line more than three times its length; "off" or null: as placed), "abbreviations"?: {word: abbreviation} (whole words a fitted label puts short, ignoring case, over the built-in English, Portuguese and Spanish list, as {"Creek": "Cr.", "Córrego": "Cór."}; null or {}: the built-in list alone), "showAll"?: bool (every label drawn, over the others where they crowd), "weight"?: 0 to 10 (where labels compete for a place, the heavier class's wins, whatever its layer's place; equal weights: the layer above, then the class listed first; a fraction rounded; null: 5), "clearance"?: points (0 to 100: the room the class's labels keep free of other labels, two labels keeping both their clearances apart; null: 2), "callout"?: true|false|points (5 to 500: a label crowded off its spot moves to a free one nearby, a line drawn back to its feature at most this long; true keeps its length, or 40; false, 0 or null: none; not labels along a line or with an icon), "guide"?: {"layer": a line layer's name (as ridge or valley lines; not drawn by this), "field"?: a field both layers have} | null (points and polygons, not perpendicular ones: each label curved along its guide line, the one with the feature's value in field, else the longest in view under the label inside its polygon, or the nearest to its point within the text's width; a feature with none its text fits along is labelled as without a guide; null: none)}"#,
),
// Labels moved by hand (the Move Label tool, as QGIS's): kept by their feature's id.
C::new("labels.move", "Move Label", "Labeling › Label Placement", |s, p| {
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 @@ -113,6 +113,7 @@ pub const CAPABILITIES: &[&str] = &[
"label-weight",
PROJECT_CRS,
"dggs-system",
"label-clearance",
];
/// An offline area keeping the versions that lost a sync or a Bring Back
/// (`OfflineArea::conflicts`, #2468): an older reader would save it again without them.
Expand Down
32 changes: 31 additions & 1 deletion crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ impl Project {
{
required.push("label-weight");
}
// And their clearances (#2828), for the same reason.
if !required.contains(&"label-clearance")
&& self.styles.iter().any(|s| s.labels.in_order().any(|c| c.clearance != rustgis_map::symbology::DEFAULT_LABEL_CLEARANCE))
{
required.push("label-clearance");
}
let commands: Vec<&'static str> =
COMMAND_CAPABILITIES.iter().filter_map(|&(name, uses)| self.uses_commands(uses).then_some(name)).collect();
required.extend(commands.into_iter().filter(|name| !required.contains(name)).collect::<Vec<_>>());
Expand Down Expand Up @@ -141,8 +147,23 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[
("layout-text-size", command_uses_text_size),
("dggs-system", command_uses_dggs_system),
("label-weight", command_weighs_labels),
("label-clearance", command_clears_labels),
];

/// A saved `labels.placement` that gives a class a clearance other than 2, read as the command
/// reads it (#2828): an older reader runs it without the key, saying nothing, so the labels would
/// keep 2 points apart as before. 2 or null is what an older reader keeps.
fn command_clears_labels(id: &str, params: &Value) -> bool {
match id {
"labels.placement" => params
.get("clearance")
.and_then(crate::cmd::labels::number)
.is_some_and(|c| rustgis_map::symbology::label_clearance(c) != rustgis_map::symbology::DEFAULT_LABEL_CLEARANCE),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_clears_labels)),
_ => false,
}
}

/// A saved `labels.placement` that gives a class a weight other than 5, read as the command reads
/// it (#2803): an older reader runs it without the key, saying nothing, so the labels would compete
/// as before. 5 or null is what an older reader keeps.
Expand Down Expand Up @@ -546,6 +567,12 @@ fn label_weight(layer: &Layer) -> bool {
layer.labels.in_order().any(|c| c.weight != rustgis_map::symbology::DEFAULT_LABEL_WEIGHT)
}

/// Whether a layer's label classes keep other than the default room free (#2828): an older reader
/// would drop the clearances, and place and save the labels as without them.
fn label_clearance(layer: &Layer) -> bool {
layer.labels.in_order().any(|c| c.clearance != rustgis_map::symbology::DEFAULT_LABEL_CLEARANCE)
}

/// The capabilities a layer needs, in [`map_capabilities`]'s order.
fn layer_capabilities(layer: &Layer) -> Vec<&'static str> {
[
Expand All @@ -556,6 +583,7 @@ fn layer_capabilities(layer: &Layer) -> Vec<&'static str> {
(zarr_window(layer), "zarr-window"),
(label_guide(layer), "label-guide"),
(label_weight(layer), "label-weight"),
(label_clearance(layer), "label-clearance"),
(held_natively(&layer.dataset), super::NATIVE_COORDINATES),
]
.into_iter()
Expand All @@ -582,13 +610,14 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
mut grid_color,
) = (false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);
let mut polygon = false;
let (mut offline_area_id, mut paper, mut native, mut guided, mut weighed) = (false, false, false, false, false);
let (mut offline_area_id, mut paper, mut native, mut guided, mut weighed, mut cleared) = (false, false, false, false, false, false);
for map in maps {
offline_area_id |= map.offline_area_id.is_some();
// A blank basemap's paper colour (#2532), which an older reader would draw and save white.
paper |= map.paper.is_some();
guided |= map.layers.iter().any(label_guide);
weighed |= map.layers.iter().any(label_weight);
cleared |= map.layers.iter().any(label_clearance);
// An older reader doesn't know the MGRS graticule (#1042 piece 8): it refuses the
// project by name rather than failing to read the type.
mgrs |= map.graticule_type == rustgis_map::graticule::GraticuleType::Mgrs;
Expand Down Expand Up @@ -635,6 +664,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
(zarr, "zarr-window"),
(guided, "label-guide"),
(weighed, "label-weight"),
(cleared, "label-clearance"),
(offline_area_id, "offline-map-area-id"),
(paper, "blank-paper"),
(native, super::NATIVE_COORDINATES),
Expand Down
17 changes: 16 additions & 1 deletion crates/engine/src/project_label_losses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ struct Class {
#[serde(default)]
weight: Option<serde_json::Value>,
#[serde(default)]
clearance: Option<serde_json::Value>,
#[serde(default)]
stretch: Option<serde_json::Value>,
}

Expand All @@ -100,6 +102,8 @@ struct Labels {
#[serde(default)]
weight: Option<serde_json::Value>,
#[serde(default)]
clearance: Option<serde_json::Value>,
#[serde(default)]
square_to: Option<serde_json::Value>,
#[serde(default)]
guide: Option<serde_json::Value>,
Expand Down Expand Up @@ -162,6 +166,12 @@ fn adjusted_weight(value: Option<&serde_json::Value>) -> bool {
value.is_some_and(|v| !v.is_null() && v.as_f64().is_none_or(|n| n.fract() != 0.0 || !rustgis_map::symbology::LABEL_WEIGHT.contains(&n)))
}

/// A label clearance (#2828) that isn't a number from 0 to 100 (held to it, or 2 when it isn't a
/// number); null is 2.
fn adjusted_clearance(value: Option<&serde_json::Value>) -> bool {
value.is_some_and(|v| !v.is_null() && v.as_f64().is_none_or(|n| !rustgis_map::symbology::LABEL_CLEARANCE.contains(&n)))
}

/// How much of its line a fitted label is spread over (#2265) that isn't a number (read as the
/// default) or is out of its range (held to it); null is the default.
fn adjusted_stretch(value: Option<&serde_json::Value>) -> bool {
Expand Down Expand Up @@ -256,7 +266,7 @@ fn count<'a>(
let name = crate::project::short_name(name);
let mut classes = ClassLosses::default();
let (mut moves, mut callouts, mut offsets, mut flags, mut squares, mut stretches) = (0usize, 0usize, 0usize, 0usize, 0usize, 0usize);
let (mut guides, mut weights) = (0usize, 0usize);
let (mut guides, mut weights, mut clearances) = (0usize, 0usize, 0usize);
let mut diagrams = rustgis_map::diagram::DiagramAudit::default();
for layer in raw {
diagrams = diagrams.plus(layer.diagram);
Expand All @@ -267,6 +277,7 @@ fn count<'a>(
offsets = offsets.saturating_add(usize::from(adjusted_line_offset(labels.line_offset.as_ref())));
flags = flags.saturating_add(usize::from(damaged_flag(labels.show_all.as_ref())));
weights = weights.saturating_add(usize::from(adjusted_weight(labels.weight.as_ref())));
clearances = clearances.saturating_add(usize::from(adjusted_clearance(labels.clearance.as_ref())));
squares = squares.saturating_add(usize::from(adjusted_square_to(labels.square_to.as_ref())));
guides = guides.saturating_add(usize::from(dropped_guide(labels.guide.as_ref())));
stretches = stretches.saturating_add(usize::from(adjusted_stretch(labels.stretch.as_ref())));
Expand All @@ -276,6 +287,7 @@ fn count<'a>(
offsets = offsets.saturating_add(usize::from(adjusted_line_offset(class.line_offset.as_ref())));
flags = flags.saturating_add(usize::from(damaged_flag(class.show_all.as_ref())));
weights = weights.saturating_add(usize::from(adjusted_weight(class.weight.as_ref())));
clearances = clearances.saturating_add(usize::from(adjusted_clearance(class.clearance.as_ref())));
squares = squares.saturating_add(usize::from(adjusted_square_to(class.square_to.as_ref())));
guides = guides.saturating_add(usize::from(dropped_guide(class.guide.as_ref())));
stretches = stretches.saturating_add(usize::from(adjusted_stretch(class.stretch.as_ref())));
Expand All @@ -301,6 +313,9 @@ fn count<'a>(
if weights > 0 {
messages.push(format!("{name}: {weights} label weight(s) not a whole number from 0 to 10 adjusted while opening {what}"));
}
if clearances > 0 {
messages.push(format!("{name}: {clearances} label clearance(s) not a number from 0 to 100 points adjusted while opening {what}"));
}
if squares > 0 {
messages.push(format!(
"{name}: {squares} perpendicular label line setting(s) dropped (labelled as auto) or adjusted while opening {what}"
Expand Down
Loading
Loading