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/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5565,7 +5565,7 @@ fn qgis_import_codes_only_the_notes_that_lose_data() {
// A memory layer opens empty, so its Z values and curves held no data: plain.
assert_eq!(json_loss("Heights", "Z and M"), Some(None));
assert_eq!(json_loss("Arcs", "curves read as lines"), Some(None));
assert_eq!(json_loss("", "print layouts' legends"), Some(None), "a layout is drawing, not data: no code");
assert_eq!(json_loss("", "legends whose map isn't on their page"), Some(None), "a layout is drawing, not data: no code");
let said = s.notices_since(mark);
let with = |code| said.iter().filter(|n| n.loss() == Some(code)).collect::<Vec<_>>();
assert!(with(LossCode::DroppedFeature)[0].text.contains("Unsupported"), "{said:?}");
Expand Down
32 changes: 32 additions & 0 deletions crates/engine/src/tests/qgis_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,35 @@ fn a_layouts_surrounds_show_the_imported_frames() {
crate::cmd::reads::read_land(&mut s, done).unwrap();
check(&s);
}

/// The corpus's legends come in naming the imported map's layers, showing its frame, and the
/// layout exports.
#[test]
fn a_layouts_legends_name_the_imported_layers() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../map/testdata/qgis/generated/qgs/layout-legends.qgs");
let mut s = Session::new(Project::default());
s.run("project.importQgis", &json!({"path": path.display().to_string()})).unwrap();
let l = s.project.layouts.iter().find(|l| l.name == "Legends").unwrap();
let map = s.project.maps.iter().find(|m| m.name == "Legends").unwrap();
let names: Vec<String> = map.layers.iter().map(|l| l.name.clone()).collect();
let frame = l.first_frame().unwrap();
let mut legends = 0;
for e in &l.elements {
if let ElementKind::Legend { frame: f, layer_order, exclude, labels, .. } = &e.kind {
legends += 1;
assert_eq!(*f, frame);
for n in layer_order.iter().chain(exclude).chain(labels.keys()) {
assert!(names.iter().any(|m| m.eq_ignore_ascii_case(n)), "{n} not in {names:?}");
}
}
}
assert_eq!(legends, 3);
let dir = std::env::temp_dir().join(format!("rustgis-qgis-legends-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let png = dir.join("legends.png");
let exported = s.run("layout.export", &json!({"layout": "Legends", "path": png.display().to_string(), "dpi": 36}));
let written = std::fs::metadata(&png).map(|m| m.len()).unwrap_or(0);
let _ = std::fs::remove_dir_all(&dir);
exported.unwrap();
assert!(written > 0);
}
120 changes: 104 additions & 16 deletions crates/map/src/qgis/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
//! rectangles and ellipses those shapes, and its lines and polygons straight lines along their
//! edges. Items are drawn in QGIS's order (its `zValue`), placed where QGIS draws them on their
//! page in millimetres, a turned label turned about its centre as QGIS turns it. Its scale bars,
//! north arrows and map overviews show their map's frame ([`surround`], piece 12b).
//! north arrows, map overviews ([`surround`]) and legends ([`legend`]) show their map's frame
//! (piece 12b).
//!
//! What a RustGIS layout has no place for is noted, counted over all the layouts: legends,
//! tables, HTML and the other item kinds, map grids, the atlas, groups, blending, and settings set
//! by expressions. A QGIS 2 composer isn't read.
//! What a RustGIS layout has no place for is noted, counted over all the layouts: tables, HTML
//! and the other item kinds, map grids, the atlas, groups, blending, and settings set by
//! expressions. A QGIS 2 composer isn't read.

use roxmltree::Node;

Expand Down Expand Up @@ -73,7 +74,6 @@ pub struct LayoutPicture {
/// What the layouts held that their RustGIS layouts don't carry over, counted over them all.
#[derive(Default)]
pub(super) struct Tally {
legends: usize,
north_arrows: usize,
tables: usize,
html: usize,
Expand Down Expand Up @@ -164,12 +164,45 @@ pub(super) struct Tally {
/// Overviews of a map another overview of the same item already outlines, kept once.
overviews_twice: usize,
bar_text_styles: usize,
/// Legends as RustGIS's: text sizes past a layout's, text styles of their own, groups listed
/// flat, classes' own labels, hidden layers listed, lists past RustGIS's, columns past its,
/// symbol sizes past its, spacing or layout of their own, and those given a size.
legend_text_sizes: usize,
legend_styles: usize,
legend_groups: usize,
legend_class_labels: usize,
legend_hidden: usize,
legend_cut: usize,
legend_columns: usize,
legend_patches: usize,
legend_layout: usize,
legend_sized: usize,
/// Legends of layers some with a category for all other values and some without, the row shown for all.
legend_other: usize,
/// Legends in several columns keeping each layer whole, their columns evened.
legend_split: usize,
/// Legends whose own list gives a layer its own rows, text, symbol size or split.
legend_rows: usize,
/// Legends listing one of two layers of a name.
legend_names: usize,
/// Legends of a map not on their page, showing their page's first.
legend_far: usize,
/// Legends' titles and layer labels past a label's characters, cut.
legend_long: usize,
/// Legends' own names for a layer sharing its name with another labelled one, the first kept.
legend_labels_dropped: usize,
/// The layer names all legends keep so far, and the legends left out past
/// [`legend::MAX_LEGEND_NAMES`].
legend_names_kept: usize,
legends_past: usize,
/// The import's layers, as a legend lists them.
layers: legend::ProjectLayers,
/// Pages whose fill is more than a colour (several layers, a pattern, another kind).
papers: usize,
/// Scale bars drawn with a symbol of several layers, patterned, dashed or of another kind.
bar_symbols: usize,
linked: Vec<surround::Linked>,
/// Scale bars and north arrows whose map isn't on their page, left out; overviews of a map
/// Scale bars, north arrows and legends whose map isn't on their page, left out; overviews of a map
/// not on theirs, not drawn.
unlinked: usize,
overviews_unlinked: usize,
Expand Down Expand Up @@ -198,6 +231,7 @@ pub(super) struct Tally {
bar_text_sizes: usize,
}

mod legend;
mod surround;

/// `text` written into a label as it reads: its braces as parentheses, as a brace starts one of
Expand All @@ -209,7 +243,12 @@ fn as_written(text: &str) -> String {
/// Reads the project's print layouts (`<Layouts>`), each page a [`ProjectLayout`], `dir` the
/// project's folder (pictures named relative to it), `title` the project's title. What can't be
/// carried over is noted.
pub fn read_layouts(root: Node, dir: &str, title: &str, report: &mut Report) -> Vec<ProjectLayout> {
pub fn read_layouts(
root: Node,
dir: &str,
(title, layers, annotations): (&str, &[super::ProjectLayer], Option<&super::ProjectLayer>),
report: &mut Report,
) -> Vec<ProjectLayout> {
let composers = root.children().filter(|c| c.has_tag_name("Composer")).count();
if composers > 0 {
report.skipped("", format!("its print composers of QGIS 2 ({composers})"));
Expand All @@ -220,6 +259,7 @@ pub fn read_layouts(root: Node, dir: &str, title: &str, report: &mut Report) ->
let cut = title.chars().nth(MAX_TEXT).is_some();
let title = Title { text: as_written(&title.chars().take(MAX_TEXT).collect::<String>()), cut, braced: title.contains(['{', '}']) };
tally.title_len = title.text.len();
tally.layers = legend::project_layers(layers, annotations);
let mut out = Vec::new();
let mut names = Vec::new();
let all: Vec<Node> = list.children().filter(|c| c.has_tag_name("Layout")).collect();
Expand Down Expand Up @@ -556,10 +596,6 @@ fn read_item(
let kind = item.attribute("type").unwrap_or_default();
match kind {
PAGE => return,
LEGEND => {
tally.legends += 1;
return;
}
GROUP => {
tally.groups += 1;
return;
Expand All @@ -577,7 +613,7 @@ fn read_item(
}
return;
}
MAP | PICTURE | LABEL | SHAPE | POLYGON | POLYLINE | SCALE_BAR => {}
MAP | PICTURE | LABEL | SHAPE | POLYGON | POLYLINE | SCALE_BAR | LEGEND => {}
other => {
tally.other += 1;
if !other.is_empty() && tally.other_kinds.len() < 8 && !tally.other_kinds.iter().any(|k| k == other) {
Expand All @@ -596,6 +632,9 @@ fn read_item(
if kind == SCALE_BAR && !surround::bar_on_page(item, &place, tally) {
return;
}
if kind == LEGEND && !legend::legend_on_page(&place, tally) {
return;
}
if kind != PICTURE && target.layout.elements.len() >= MAX_ELEMENTS {
tally.over += elements_needed(item, kind);
return;
Expand All @@ -618,6 +657,7 @@ fn read_item(
PICTURE => picture_item(item, &place, id, dir, fade, target, tally),
SHAPE => shape_item(item, &place, id, fade, target, tally),
SCALE_BAR => surround::scale_bar_item(item, &place, id, fade, target, tally),
LEGEND => legend::legend_item(item, &place, id, fade, target, tally),
_ => line_item(item, kind, &place, id, fade, target, tally),
}
// Counted for what came in: a picture's said by the engine once its image reads.
Expand All @@ -627,7 +667,11 @@ fn read_item(
let oversized = place.w > MAX_SIDE || place.h > MAX_SIDE;
// Narrower than an element can be across or down: widened to it (a line's edges are counted
// as they are drawn).
let tiny = !matches!(kind, POLYLINE | POLYGON) && (place.w < crate::layout::MIN_ELEMENT_MM || place.h < crate::layout::MIN_ELEMENT_MM);
// A legend sized to its content (QGIS saves no size until it draws it) is given one, said as that.
let sized = kind == LEGEND && item.attribute("resizeToContents") != Some("0");
let tiny = !matches!(kind, POLYLINE | POLYGON)
&& !sized
&& (place.w < crate::layout::MIN_ELEMENT_MM || place.h < crate::layout::MIN_ELEMENT_MM);
// A picture's notes wait for its image (a north arrow, also a QGIS picture, is not one).
let picture = target.layout.elements.get(first).is_some_and(|e| matches!(e.kind, ElementKind::Picture { .. }));
let mut counted = surround::Counts::default();
Expand Down Expand Up @@ -660,7 +704,7 @@ fn read_item(
if added
&& l.page == place.page
&& element == Some(l.element)
&& matches!(l.link, surround::Link::Surround | surround::Link::Numeric) =>
&& matches!(l.link, surround::Link::Surround | surround::Link::Numeric | surround::Link::FirstFrame) =>
{
l.counts.merge(counted);
}
Expand Down Expand Up @@ -1306,8 +1350,18 @@ fn edges(
impl Tally {
pub(super) fn say(&self, report: &mut Report) {
let skipped = [
(self.legends, "legends"),
(self.unlinked, "scale bars and north arrows whose map isn't on their page, or is left out"),
(self.unlinked, "scale bars, north arrows and legends whose map isn't on their page, or is left out"),
(self.legends_past, &format!("legends past the {} layer names an import's legends keep", legend::MAX_LEGEND_NAMES)),
(self.legend_groups, "legends' groups, their layers listed on their own"),
(self.legend_class_labels, "legends' own labels for their layers' classes, showing the layers'"),
(self.legend_hidden, "legends listing layers the map hides, which a RustGIS legend leaves out"),
(
self.legend_cut,
&format!(
"legends' lists of layers past the {} a legend keeps, or past the entries an import reads, cut there",
crate::layout::MAX_LEGEND_EXCLUDED
),
),
(self.overviews_unlinked, "map overviews of their own map, or of a map not on their page or left out"),
(self.overviews_off, "map overviews and grids switched off"),
(self.overviews_twice, "map overviews of a map their map item already outlines (kept once)"),
Expand Down Expand Up @@ -1369,6 +1423,34 @@ impl Tally {
"scale bars drawn with symbols of several layers, patterned, dashed, outlined or of another kind, in a plain colour",
),
(self.papers, "pages whose fill has several layers, a pattern, an outline or is of another kind, drawn as a plain colour"),
(self.legend_text_sizes, "legends' text sizes outside 4 to 100 pt, drawn at the nearest"),
(
self.legend_styles,
"legends' title, group and layer texts in sizes, faces or fonts of their own, in RustGIS's proportion to their rows",
),
(self.legend_columns, &format!("legends' columns brought within RustGIS's (1 to {})", crate::layout::MAX_LEGEND_COLUMNS)),
(self.legend_patches, "legends' symbol sizes brought within RustGIS's"),
(
self.legend_layout,
"legends' spacing, alignment, wrapping, symbol size limits, even columns or atlas filter, as RustGIS draws a legend",
),
(
self.legend_other,
"legends of layers drawn by categories, some with one for all other values and some without, showing that row for all",
),
(self.legend_split, "legends in several columns keeping each layer whole, their columns evened, a layer split between them"),
(
self.legend_rows,
"legends giving a layer rows of its own (classes left out or reordered), a text by an expression, a symbol size or a split, as RustGIS lists a layer",
),
(self.legend_names, "legends listing one of two layers of a name, which RustGIS's legend lists both of"),
(
self.legend_long,
&format!("legends' titles and layer labels longer than {} characters, cut there", crate::layout::MAX_LEGEND_LABEL_CHARS),
),
(self.legend_labels_dropped, "legends' own names for a layer sharing its name with another they name, the first kept"),
(self.legend_far, "legends of a map not on their page (or not read), showing their page's first map"),
(self.legend_sized, "legends sized to their content that QGIS hasn't sized yet, given room for a row a layer"),
(self.bar_text_styles, "scale bars' text bold, italic, underlined, buffered or shadowed, drawn plain"),
(self.numeric_formats, "numeric scale bars, printing the scale in RustGIS's format (1:25,000)"),
(self.numeric_sizes, "numeric scale bars' text sizes outside 1 to 400 pt, drawn at the nearest"),
Expand Down Expand Up @@ -1500,3 +1582,9 @@ fn unit_mm(unit: &str, dpi: f64) -> Option<f64> {
pub(super) fn surround_hairline() -> f64 {
surround::HAIRLINE_PT
}

/// The layer names all of an import's legends keep, for tests.
#[cfg(test)]
pub(super) fn legend_names_limit() -> usize {
legend::MAX_LEGEND_NAMES
}
Loading
Loading