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
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? clearance? callout? guide?
labels.placement layer? class? position? to? side? offset? repeat? lineOffset? otherPositions? fit? abbreviations? showAll? weight? clearance? removeDuplicates? 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", "clearance", "callout", "to", "side", "fit", "abbreviations", "guide"]
None if ["offset", "repeat", "lineOffset", "otherPositions", "showAll", "weight", "clearance", "removeDuplicates", "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 clearance, 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, removeDuplicates, 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 @@ -222,6 +222,15 @@ pub fn commands() -> Vec<C> {
};
let clearance_asked = clearance.filter(|c| !rustgis_map::symbology::LABEL_CLEARANCE.contains(c));
let clearance = clearance.map(rustgis_map::symbology::label_clearance);
// Duplicates removed within so far (#2847): 1 to 2000 points, false or null off. A
// number out of range is held to it, and says so.
let remove_duplicates = match p.get("removeDuplicates") {
None => None,
Some(Value::Null | Value::Bool(false)) => Some(None),
Some(v) => Some(Some(number(v).ok_or("removeDuplicates must be a number of points from 1 to 2000 (false or null: off)")?)),
};
let duplicates_asked = remove_duplicates.flatten().filter(|d| !rustgis_map::symbology::LABEL_DUPLICATES.contains(d));
let remove_duplicates = remove_duplicates.map(|d| d.map(rustgis_map::symbology::label_duplicates));
// Every label shown, over the others where they crowd.
let show_all = match p.get("showAll") {
None => None,
Expand Down Expand Up @@ -311,6 +320,9 @@ pub fn commands() -> Vec<C> {
if let Some(r) = clearance {
c.clearance = r;
}
if let Some(d) = remove_duplicates {
c.remove_duplicates = d;
}
if let Some(f) = fit {
c.fit = f;
}
Expand Down Expand Up @@ -400,12 +412,24 @@ pub fn commands() -> Vec<C> {
false,
);
}
if let Some(asked) = duplicates_asked {
let name = s.map().layer(id).map(|l| l.name.clone()).unwrap_or_default();
let kept = rustgis_map::symbology::label_duplicates(asked);
let asked = if asked.abs() < 1e6 { asked.to_string() } else { format!("{asked:e}") };
s.notify_text(
NoticeText::format(
"{name}: removeDuplicates {asked} adjusted to {kept} (duplicates are removed within 1 to 2000 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, 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)}"#,
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, removeDuplicates, 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), "removeDuplicates"?: points (1 to 2000: a label is left out within this distance of one placed before it for another feature of its layer and class with the same text; a label moved by hand always shows; false or null: off), "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 @@ -116,6 +116,7 @@ pub const CAPABILITIES: &[&str] = &[
"dggs-system",
"label-clearance",
"svg-markers",
"label-duplicates",
];
/// 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
29 changes: 28 additions & 1 deletion crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ impl Project {
{
required.push("label-clearance");
}
// And whether they remove duplicates (#2847), for the same reason.
if !required.contains(&"label-duplicates") && self.styles.iter().any(|s| s.labels.in_order().any(|c| c.remove_duplicates.is_some()))
{
required.push("label-duplicates");
}
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 @@ -153,8 +158,20 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[
("dggs-system", command_uses_dggs_system),
("label-weight", command_weighs_labels),
("label-clearance", command_clears_labels),
("label-duplicates", command_removes_duplicate_labels),
];

/// A saved `labels.placement` that has a class remove duplicates, read as the command reads it
/// (#2847): an older reader runs it without the key, saying nothing, so every label would show.
/// False or null is what an older reader keeps.
fn command_removes_duplicate_labels(id: &str, params: &Value) -> bool {
match id {
"labels.placement" => params.get("removeDuplicates").and_then(crate::cmd::labels::number).is_some(),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_removes_duplicate_labels)),
_ => false,
}
}

/// 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.
Expand Down Expand Up @@ -623,6 +640,12 @@ fn label_clearance(layer: &Layer) -> bool {
layer.labels.in_order().any(|c| c.clearance != rustgis_map::symbology::DEFAULT_LABEL_CLEARANCE)
}

/// Whether a layer's label classes remove duplicates (#2847): an older reader would drop the
/// setting, and place and save every label.
fn removes_duplicate_labels(layer: &Layer) -> bool {
layer.labels.in_order().any(|c| c.remove_duplicates.is_some())
}

/// The capabilities a layer needs, in [`map_capabilities`]'s order.
fn layer_capabilities(layer: &Layer) -> Vec<&'static str> {
[
Expand All @@ -635,6 +658,7 @@ fn layer_capabilities(layer: &Layer) -> Vec<&'static str> {
(label_guide(layer), "label-guide"),
(label_weight(layer), "label-weight"),
(label_clearance(layer), "label-clearance"),
(removes_duplicate_labels(layer), "label-duplicates"),
(held_natively(&layer.dataset), super::NATIVE_COORDINATES),
]
.into_iter()
Expand All @@ -661,14 +685,16 @@ 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, mut svg) = (false, false);
let (mut offline_area_id, mut paper, mut native, mut guided, mut weighed, mut cleared) = (false, false, false, false, false, false);
let (mut offline_area_id, mut paper, mut native, mut guided, mut weighed, mut cleared, mut deduped) =
(false, 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);
deduped |= map.layers.iter().any(removes_duplicate_labels);
// 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 @@ -718,6 +744,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
(guided, "label-guide"),
(weighed, "label-weight"),
(cleared, "label-clearance"),
(deduped, "label-duplicates"),
(offline_area_id, "offline-map-area-id"),
(paper, "blank-paper"),
(native, super::NATIVE_COORDINATES),
Expand Down
Loading
Loading