diff --git a/crates/map/src/arcgis_style.rs b/crates/map/src/arcgis_style.rs index ce634868e6..bed3ce4431 100644 --- a/crates/map/src/arcgis_style.rs +++ b/crates/map/src/arcgis_style.rs @@ -491,26 +491,46 @@ fn symbol(s: &Value, g: GeometryType, picture: Option, pictures: &mut Pict // Sized by its width, as a picture marker is: its height follows the image's own // proportion, which a service draws its symbol at. Without a width above 0, its // height stands in as its width, said, since an image that isn't square is then - // drawn that wide, not that tall; with neither, RustGIS's size does. + // drawn that wide, not that tall; with neither (a height of 0 is none), RustGIS's + // size does. A height of 0 beside a width is said: the width alone draws it. Ok(p) => { + // A side that isn't a number is said as the circle says it, whether this uses it or not. + say_unreadable_sizes(s, notes); let wide = s.get("width").and_then(Value::as_f64).is_some_and(|w| w.is_finite() && w > 0.0); - if !wide { - notes.add(if s.get("height").is_some_and(|h| !h.is_null()) { - "picture markers with no width, drawn with their height as their width" - } else { - "picture markers with no size, drawn at RustGIS's" - }); - } - let size = size(s.get(if wide { "width" } else { "height" }), base.size, notes); + let height = s.get("height").filter(|h| !h.is_null()); + let no_height = height.and_then(Value::as_f64).is_some_and(|h| h <= 0.0); + let sized_by = if wide { + if no_height { + notes.add("picture markers of no height, drawn as tall as their image is"); + } + s.get("width") + } else if height.and_then(Value::as_f64).is_some_and(|h| h > 0.0) { + notes.add("picture markers with no width, drawn with their height as their width"); + height + } else { + notes.add("picture markers with no size, drawn at RustGIS's"); + None + }; + let size = size(sized_by, base.size, notes); return Some((Symbol { fill, size, marker: Marker::Picture(p), ..base }, None)); } Err(why) => notes.add(why), } // As a circle, the larger of its width and height, so that one past the largest is - // said once. - let points = |k: &str| s.get(k).and_then(Value::as_f64).filter(|v| v.is_finite()).unwrap_or(f64::NEG_INFINITY); - let larger = if points("height") > points("width") { s.get("height") } else { s.get("width") }; - let size = size(larger, base.size, notes); + // said once; each side that isn't a number is said, as `size()` says one it can't read. + let num = |k: &str| s.get(k).and_then(Value::as_f64).filter(|v| v.is_finite()); + say_unreadable_sizes(s, notes); + let top = match (num("height"), num("width")) { + (Some(h), Some(w)) => Some(h.max(w)), + (h, w) => h.or(w), + }; + let larger = if num("height") > num("width") { s.get("height") } else { s.get("width") }; + // With no size above 0 (a size of 0 isn't shown), or none given, RustGIS's, said as the drawn one is. + let none = top.is_none_or(|t| t <= 0.0); + if none { + notes.add("picture markers with no size, drawn at RustGIS's"); + } + let size = size(if none { None } else { larger }, base.size, notes); Some((Symbol { fill, size, ..base }, None)) } ("esriPFS", GeometryType::Polygon) => { @@ -621,6 +641,14 @@ fn size(v: Option<&Value>, default: f32, notes: &mut Notes) -> f32 { }) } +/// Says a picture marker's `width` or `height` that is given but isn't a number, once for either or both. +fn say_unreadable_sizes(s: &Value, notes: &mut Notes) { + let unreadable = |k: &str| s.get(k).is_some_and(|v| !v.is_null()) && !s.get(k).and_then(Value::as_f64).is_some_and(f64::is_finite); + if unreadable("width") || unreadable("height") { + notes.add("marker sizes RustGIS can't read, left out"); + } +} + /// A symbol's style (`style`), `None` when it names none; one that isn't text is noted and taken as /// none (ArcGIS's default style). fn style<'a>(s: &'a Value, notes: &mut Notes) -> Option<&'a str> { @@ -782,6 +810,58 @@ mod tests { assert_eq!(notes.text(), "markers' angles and offsets, left out"); } + /// A picture marker with a height of 0 isn't drawn at 0 (not shown) under a note saying it is + /// drawn as wide as its height: a height of 0 is no height. + #[test] + fn a_picture_marker_of_no_height_is_said_so() { + let image = "iVBORw0KGgoSIMPLE="; + let styled_pms = |width: Value, height: Value| { + let info = json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": image, "width": width, "height": height}}}); + let (l, notes) = styled(GeometryType::Point, info); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + (s, notes.text()) + }; + let Renderer::Single(base) = layer(GeometryType::Point).renderer else { panic!("a single symbol") }; + // With no width above 0 either, it has no size: RustGIS's. + let cases = [(json!(null), json!(0)), (json!(0), json!(0)), (json!(0), json!(-4))]; + for (width, height) in cases { + let (s, text) = styled_pms(width.clone(), height.clone()); + assert_eq!(s.size, base.size, "width {width}, height {height}"); + assert_eq!(text, "picture markers with no size, drawn at RustGIS's", "width {width}, height {height}"); + } + // A side that isn't a number can't size it either, and is said, as the circle says it. + for (width, height) in [(json!(null), json!("18")), (json!(0), json!("abc"))] { + let (s, text) = styled_pms(width.clone(), height.clone()); + assert_eq!(s.size, base.size, "width {width}, height {height}"); + assert_eq!( + text, "marker sizes RustGIS can't read, left out; picture markers with no size, drawn at RustGIS's", + "width {width}, height {height}" + ); + } + // So is one a width makes no use of, and a width that isn't a number beside a height that is. + let (s, text) = styled_pms(json!(15), json!("abc")); + assert_eq!(s.size, 15.0); + assert_eq!(text, "marker sizes RustGIS can't read, left out"); + let (s, text) = styled_pms(json!("abc"), json!(18)); + assert_eq!(s.size, 18.0); + assert_eq!( + text, + "marker sizes RustGIS can't read, left out; picture markers with no width, drawn with their height as their width" + ); + // With a width, it is drawn by it, and the height's 0 said. + let (s, text) = styled_pms(json!(15), json!(0)); + assert_eq!(s.size, 15.0); + assert_eq!(text, "picture markers of no height, drawn as tall as their image is"); + // A height below 0 is no height either: the width alone draws it, and the height is said. + let (s, text) = styled_pms(json!(15), json!(-4)); + assert_eq!(s.size, 15.0); + assert_eq!(text, "picture markers of no height, drawn as tall as their image is"); + // A height above 0 says nothing of the kind. + let (s, text) = styled_pms(json!(15), json!(18)); + assert_eq!(s.size, 15.0); + assert!(text.is_empty(), "{text}"); + } + #[test] fn a_picture_marker_rustgis_cant_draw_is_a_grey_circle_of_its_size_and_said_why() { let circle = |symbol: Value| { @@ -804,6 +884,75 @@ mod tests { // Its image only at its `url`, which RustGIS doesn't fetch. let url = json!({"type": "esriPMS", "url": "4d1f6b3c", "width": 15, "height": 18}); assert_eq!(circle(url), "picture markers whose image is only at a URL, drawn as circles"); + // A height that isn't a number, with no width, isn't lost silently: size() says it can't read it. + let (l, notes) = styled( + GeometryType::Point, + json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": gif, "width": null, "height": "18"}}}), + ); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + let Renderer::Single(base) = layer(GeometryType::Point).renderer else { panic!("a single symbol") }; + assert_eq!(s.size, base.size); + assert_eq!( + notes.text(), + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; marker sizes RustGIS can't read, left out; picture markers with no size, drawn at RustGIS's" + ); + // Each side that isn't a number is said, whichever one the circle takes its size from. + for (width, height, size, text) in [ + ( + json!("abc"), + json!(18), + 18.0, + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; marker sizes RustGIS can't read, left out", + ), + ( + json!(0), + json!("abc"), + 0.0, + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; marker sizes RustGIS can't read, left out; picture markers with no size, drawn at RustGIS's", + ), + // A size below 0 is no size (not drawn at 0, not shown), as it is for the drawn picture. + ( + json!(-5), + json!(null), + 0.0, + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; picture markers with no size, drawn at RustGIS's", + ), + // With neither side given at all, it has no size, as the drawn picture has none. + ( + json!(null), + json!(null), + 0.0, + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; picture markers with no size, drawn at RustGIS's", + ), + ( + json!("abc"), + json!("abc"), + 0.0, + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; marker sizes RustGIS can't read, left out; picture markers with no size, drawn at RustGIS's", + ), + ] { + let (l, notes) = styled( + GeometryType::Point, + json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": gif, "width": width, "height": height}}}), + ); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + let Renderer::Single(base) = layer(GeometryType::Point).renderer else { panic!("a single symbol") }; + let expected = if size == 0.0 { base.size } else { size }; + assert_eq!(s.size, expected, "width {width}, height {height}"); + assert_eq!(notes.text(), text, "width {width}, height {height}"); + } + // With no size above 0 either, the circle is RustGIS's size (not 0, not shown), said. + let (l, notes) = styled( + GeometryType::Point, + json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": gif, "width": 0, "height": 0}}}), + ); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + let Renderer::Single(base) = layer(GeometryType::Point).renderer else { panic!("a single symbol") }; + assert_eq!((s.marker, s.size), (Marker::Circle, base.size)); + assert_eq!( + notes.text(), + "picture markers whose image isn't a PNG, JPEG or SVG RustGIS reads, drawn as circles; picture markers with no size, drawn at RustGIS's" + ); assert_eq!(Picture::kept(gif), None, "an image that isn't drawn isn't kept"); } diff --git a/crates/map/src/qgis/source.rs b/crates/map/src/qgis/source.rs index 6cdea893db..03e7ce4d57 100644 --- a/crates/map/src/qgis/source.rs +++ b/crates/map/src/qgis/source.rs @@ -595,13 +595,24 @@ fn file_url_path(url: &str) -> Option<&str> { Some(if drive { path.get(1..).unwrap_or(path) } else { path }) } +/// A `file:` address's path (see [`file_url_path`]) decoded as any address's parts are; `None` for any +/// other address and for one with a host. +pub(super) fn file_address_path(url: &str) -> Option { + // Trimmed: what `absolute` trims after the checks must not decide them (`%20%2F%2Fhost` is `//host`). + let path = rustgis_data::address::percent_decode(file_url_path(url)?).trim().to_string(); + // A drive's colon may be encoded (`/C%3A/x`): the drive form is recognized once decoded. + let b = path.as_bytes(); + let drive = b.first() == Some(&b'/') && b.get(1).is_some_and(u8::is_ascii_alphabetic) && b.get(2) == Some(&b':'); + Some(if drive { path.get(1..).unwrap_or_default().to_string() } else { path }) +} + /// A tile archive's path: a `file://` address (`file:../x` when relative) as its path, a relative /// one from the project's folder; a web address as it is. fn archive_path(url: &str, dir: &str) -> String { let url = url.trim(); - match file_url_path(url) { + match file_address_path(url) { // (Its parts are encoded as in any address.) - Some(path) => absolute(&rustgis_data::address::percent_decode(path), dir), + Some(path) => absolute(&path, dir), None if url.contains("://") => url.to_string(), None => absolute(url, dir), } @@ -887,16 +898,11 @@ fn select_all(query: &str) -> Option<(String, Option)> { fn csv(datasource: &str, dir: &str) -> Source { let (path, q) = datasource.split_once('?').unwrap_or((datasource, "")); // file:///absolute/path, or a relative one as file:./path. - let path = file_url_path(path).unwrap_or(path); + // A `file:` address is its path decoded, a drive's colon encoded or not; anything else is decoded as it is. + let path = file_address_path(path).unwrap_or_else(|| rustgis_data::address::percent_decode(path)); let q = query(q); let get = |k: &str| q.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone()).filter(|v| !v.is_empty()); - Source::Csv { - path: absolute(&rustgis_data::address::percent_decode(path), dir), - x: get("xField"), - y: get("yField"), - wkt: get("wktField"), - crs: get("crs"), - } + Source::Csv { path: absolute(&path, dir), x: get("xField"), y: get("yField"), wkt: get("wktField"), crs: get("crs") } } /// The most `&`-joined parts of a web source, or of an address's query, read; the rest are @@ -1386,6 +1392,9 @@ mod tests { ); let Source::Csv { path, .. } = read("delimitedtext", "file:///C:/maps/p.csv?xField=x", dir) else { panic!() }; assert_eq!(path, "C:/maps/p.csv"); + // A drive's colon may be encoded, as an image's and an archive's is. + let Source::Csv { path, .. } = read("delimitedtext", "file:///C%3A/maps/p.csv?xField=x", dir) else { panic!() }; + assert_eq!(path, "C:/maps/p.csv"); // An encoded file address is decoded once (QGIS encodes the address again as a parameter). assert_eq!( read("mbtilesvectortiles", "type=mbtiles&url=file:///maps/my%2520tiles.mbtiles", dir), @@ -1817,4 +1826,17 @@ mod tests { assert_eq!(filtered("").as_deref(), Some(r#""kind" = 'a'"#)); assert!(matches!(unnamed("pts"), Some(VirtualFrom::Missing(_)))); } + + /// A tile archive at a `file:` address whose drive's colon is encoded is on that drive, as a + /// picture marker's image is (`file:///C%3A/…` is `C:/…`, not `/C:/…`). + #[test] + fn a_tile_archive_with_an_encoded_drive_colon_is_on_that_drive() { + for (url, path) in [ + ("file:///C%3A/maps/t.mbtiles", "C:/maps/t.mbtiles"), + ("file:///C:/maps/t.mbtiles", "C:/maps/t.mbtiles"), + ("file:///maps/t.mbtiles", "/maps/t.mbtiles"), + ] { + assert_eq!(archive_path(url, "/p"), path, "{url}"); + } + } } diff --git a/crates/map/src/qgis/symbol.rs b/crates/map/src/qgis/symbol.rs index f9de955686..6746d73436 100644 --- a/crates/map/src/qgis/symbol.rs +++ b/crates/map/src/qgis/symbol.rs @@ -663,7 +663,7 @@ fn along(l: Node, p: &[(String, String)], hash: bool, reference: Option, la let stroke = if hash { hash_mark(class, &sp, p, base, reference, layer, report) } else { - marker_mark(class, &sp, base, reference, layer, report) + marker_mark(class, &sp, class == "RasterMarker" && name_from_expression(sub), base, reference, layer, report) }; if let Some(mut st) = stroke { // An image mark is drawn at the layer's opacity alone. @@ -710,10 +710,12 @@ fn placements(p: &[(String, String)], layer: &str, report: &mut Report) -> Place } /// A marker line's marker layer `class` (its settings `p`) as a stroke of markers at `base`'s -/// places, or `None` for one RustGIS has no match for (noted). +/// places, or `None` for one RustGIS has no match for (noted). `from_expression`: its image path is +/// set by a field or an expression. fn marker_mark( class: &str, p: &[(String, String)], + from_expression: bool, base: LineMarkers, reference: Option, layer: &str, @@ -728,7 +730,7 @@ fn marker_mark( }) } "FontMarker" => font_char(p, layer, report)?, - "RasterMarker" => raster_image(p, layer, report).map_or(Marker::Circle, |(picture, _)| Marker::Picture(picture)), + "RasterMarker" => raster_image(p, from_expression, layer, report).map_or(Marker::Circle, |(picture, _)| Marker::Picture(picture)), "SvgMarker" | "EllipseMarker" | "FilledMarker" => { report.approximated(layer, format!("{} markers, drawn as circles in their colour", marker_name(class))); Marker::Circle @@ -833,7 +835,7 @@ fn marker(layers: &[(Node, Props)], reference: Option, still: bool, layer: Some(m) => m, None => continue, }, - "RasterMarker" => match raster_image(p, layer, report) { + "RasterMarker" => match raster_image(p, name_from_expression(*l), layer, report) { Some((picture, a)) => { aspect = a; Marker::Picture(picture) @@ -889,11 +891,26 @@ fn marker(layers: &[(Node, Props)], reference: Option, still: bool, layer: Converted { symbol, strokes, rotation: None, by: SymbolBy::default(), fills: Vec::new() } } +/// Whether `path` starts from a root (`/…`, `\\…`) or names a drive (`C:\…`, or `C:x`, as `is_relative` counts +/// it), not from the project's folder. +fn rooted(path: &str) -> bool { + let b = path.as_bytes(); + path.starts_with(['/', '\\']) || (b.first().is_some_and(u8::is_ascii_alphabetic) && b.get(1) == Some(&b':')) +} + +/// Whether image path `file` names a network share (`\\host\share`, `//host/share`), which isn't +/// opened: looking one up can sign in to the host, and a project may be opened on Windows, where +/// `//` is one (as `paths::is_absolute_local` refuses it on every platform). +fn network_share(file: &str) -> bool { + file.len() > 1 && file.chars().take(2).all(|c| c == '\\' || c == '/') +} + /// Reads the images of the raster image markers under `root` (a project or a style), each /// `imageFile` once (a file once for each spelling of its path from the folder: a leading `./` is /// dropped, so `./a.png` and `a.png` are one), into the report for [`raster_image`]: a `base64:` -/// one as it is, a file's through `read` (relative to `dir`). One at a web address isn't fetched, -/// nor one on a network share (`\\host\share`, `//host/share`), nor one kept inside a `.qgz` +/// one as it is, a file's through `read` (relative to `dir`; a `file:` address is the file it names). +/// One at a web address isn't fetched, nor one on a network share (`\\host\share`, `//host/share`, +/// `file://host/share`), nor one kept inside a `.qgz` /// (`attachment:`); one relative to no folder (or to a drive's working folder) isn't looked /// for, and those past the pictures a run keeps ([`MAX_PICTURES`], /// [`MAX_PICTURES_BYTES`]) aren't read. An image is kept when read, before its symbol is: one @@ -910,22 +927,44 @@ pub(super) fn read_images(root: Node, dir: &str, read: &mut ReadImage, report: & if report.images.contains_key(file) { continue; } + // A `file:` address is the file it names, decoded as a tile archive's address is; one with + // a host is a network share. + let address = file.starts_with("file:"); + let (path, share) = if address { + match super::source::file_address_path(file) { + Some(path) => (path, false), + None => (file.to_string(), true), + } + } else { + (file.to_string(), false) + }; let source = if file.starts_with("base64:") { Ok(None) } else if file.starts_with("attachment:") { Err("whose image is kept inside the .qgz project, which RustGIS doesn't read it from") - } else if file.contains("://") { + } else if path.contains("://") && (!address || file.trim_start_matches("file:").trim_start_matches('/').contains("://")) { Err("whose image is at a web address, which RustGIS doesn't fetch") - } else if file.len() > 1 && file.chars().take(2).all(|c| c == '\\' || c == '/') { + } else if share || network_share(&path) { // Looking a share up can sign in to the host the file names, which a style or // project chose (as an archive on one isn't opened either). Err("whose image is on a network share, which RustGIS doesn't open") - } else if super::is_drive_relative(file) { + } else if address && !rooted(&path) && path.contains("://") { + // `%3A%2F%2F` in a name is a name, but `absolute` would leave one from the project's folder to + // the working folder. + Err("whose image is a relative file name with `://` in it, which RustGIS doesn't read") + } else if super::is_drive_relative(&path) { Err("whose image file is relative to a drive's working folder, which no project folder tells") - } else if super::is_relative(file) && dir.is_empty() { + } else if super::is_relative(&path) && dir.is_empty() { Err("whose image file is relative to a folder the import wasn't given") } else { - Ok(Some(super::source::absolute(file, dir))) + let joined = super::source::absolute(&path, dir); + // `.//host/share` from a project at a drive's root (`/`) is `//host/share` once joined; a project + // that is itself on a share reads its own images. + if network_share(&joined) && !network_share(dir) { + Err("whose image is on a network share, which RustGIS doesn't open") + } else { + Ok(Some(joined)) + } }; if let Ok(Some(path)) = &source && let Some(got) = files.get(path) @@ -964,10 +1003,15 @@ const SEE_THROUGH_IMAGES: &str = "see-through raster image markers, drawn opaque /// A raster image marker's image (read before the symbols: [`read_images`]) and its height over /// its width, or `None` (noted, with why) for it to be drawn as a circle. What QGIS draws -/// otherwise than RustGIS is noted: a ratio of its own, see-through. -fn raster_image(p: &[(String, String)], layer: &str, report: &mut Report) -> Option<(Picture, f32)> { +/// otherwise than RustGIS is noted: a ratio of its own, see-through. `from_expression`: its image +/// path is set by a field or an expression. +fn raster_image(p: &[(String, String)], from_expression: bool, layer: &str, report: &mut Report) -> Option<(Picture, f32)> { let file = prop(p, "imageFile").map(str::trim).unwrap_or_default(); let why = match report.images.get(file) { + // QGIS's image path as a field or an expression (`name`) has nothing in `imageFile` to read. + _ if file.is_empty() && from_expression => { + "whose image comes from a field or an expression, which RustGIS doesn't evaluate".to_string() + } _ if file.is_empty() => "with no image".to_string(), Some(Ok(got)) => { let got = *got; @@ -1021,6 +1065,11 @@ fn point_offset( (x, y) } +/// Whether `node`'s image path (`name`) is set by a field or an expression. +fn name_from_expression(node: Node) -> bool { + active_properties(node).iter().any(|(name, _)| *name == "name") +} + /// The data-defined properties switched on in `node`'s own `data_defined_properties`, by name. fn active_properties<'a, 'i>(node: Node<'a, 'i>) -> Vec<(&'a str, Node<'a, 'i>)> { active_properties_in(node, "data_defined_properties") diff --git a/crates/map/src/qgis/tests.rs b/crates/map/src/qgis/tests.rs index b60cc0ef85..194faafe4e 100644 --- a/crates/map/src/qgis/tests.rs +++ b/crates/map/src/qgis/tests.rs @@ -1473,6 +1473,122 @@ fn raster_image_markers_draw_their_image_read_once_from_the_style_folder() { assert_eq!(notes, [("see-through raster image markers, drawn opaque", 1)]); } +/// A `file:` address is the file it names, read as a tile archive's address is (decoded, from the +/// folder when relative); with a host it is a network share. +#[test] +fn raster_image_markers_read_a_file_address_as_the_file_it_names() { + use crate::symbology::Picture; + let asked = std::cell::RefCell::new(Vec::new()); + let mut read = |source: &str| { + asked.borrow_mut().push(source.to_string()); + Ok(("QUJD".to_string(), 1.0)) + }; + let cases = [ + ("file:///data/my%20pin.png", "/data/my pin.png"), + ("file:///C:/data/a.png", "C:/data/a.png"), + // A drive's colon may be encoded. + ("file:///C%3A/data/a.png", "C:/data/a.png"), + ("file://C:/data/a.png", "C:/data/a.png"), + ("file:./pins/a.png", "/styles/pins/a.png"), + // `://` encoded in a name is a name, not a web address. + ("file:///data/a%3A%2F%2Fb.png", "/data/a://b.png"), + ]; + for (file, path) in cases { + asked.borrow_mut().clear(); + let mut report = Report::default(); + let s = super::read_style(&marker_qml("1", &raster_marker(file, "")), "Pins", "/styles", &mut read, &mut report).unwrap(); + let Some(Renderer::Single(sym)) = &s.renderer else { panic!("{file}") }; + assert_eq!(sym.marker, Marker::Picture(Picture::new("QUJD")), "{file}"); + assert_eq!(*asked.borrow(), [path], "{file}"); + assert!(report.notes.is_empty(), "{file}: {:?}", report.notes); + } + // A relative name with `://` encoded in it isn't read from wherever the program runs, though. + asked.borrow_mut().clear(); + let mut report = Report::default(); + let s = super::read_style(&marker_qml("1", &raster_marker("file:./a%3A%2F%2Fb.png", "")), "Pins", "/styles", &mut read, &mut report) + .unwrap(); + let Some(Renderer::Single(sym)) = &s.renderer else { panic!() }; + assert_eq!(sym.marker, Marker::Circle); + assert!(asked.borrow().is_empty(), "{:?}", asked.borrow()); + assert_eq!( + report.notes.iter().map(|n| n.text.as_str()).collect::>(), + ["raster image markers whose image is a relative file name with `://` in it, which RustGIS doesn't read, drawn as circles"] + ); + // A relative name that starts with a separator isn't joined to a project at a drive's root into a share + // (`.//host/share` from `/` is `//host/share`), though a project that is itself on a share reads its own. + for (file, dir, refused) in [ + ("file:.//host/share/a.png", "/", true), + ("file:.//host/share/a.png", "\\", true), + (".//host/share/a.png", "/", true), + ("file:./a.png", "/", false), + ("file:./a.png", "//server/proj", false), + ] { + asked.borrow_mut().clear(); + let mut report = Report::default(); + let s = super::read_style(&marker_qml("1", &raster_marker(file, "")), "Pins", dir, &mut read, &mut report).unwrap(); + let Some(Renderer::Single(sym)) = &s.renderer else { panic!("{file}") }; + if refused { + assert_eq!(sym.marker, Marker::Circle, "{file} from {dir}"); + assert!(asked.borrow().is_empty(), "{file} from {dir}: {:?}", asked.borrow()); + assert_eq!( + report.notes.iter().map(|n| n.text.as_str()).collect::>(), + ["raster image markers whose image is on a network share, which RustGIS doesn't open, drawn as circles"], + "{file} from {dir}" + ); + } else { + assert_eq!(sym.marker, Marker::Picture(crate::symbology::Picture::new("QUJD")), "{file} from {dir}"); + assert_eq!(asked.borrow().len(), 1, "{file} from {dir}"); + } + } +} + +/// A raster image marker whose image a field or an expression gives has no image of its own to +/// read; the note says that, not that it has none. +#[test] +fn a_raster_image_from_an_expression_is_noted_as_one_not_as_no_image() { + // A raster image marker layer of `file` whose image path is also set by an expression. + let by_expression = |file: &str| { + format!( + r#" + "# + ) + }; + let from = "raster image markers whose image comes from a field or an expression, which RustGIS doesn't evaluate, drawn as circles"; + let generic = "data-defined symbol settings (from fields or expressions)"; + let mut read = |_: &str| Ok(("QUJD".to_string(), 1.0)); + let mut report = Report::default(); + let s = super::read_style(&marker_qml("1", &by_expression("")), "Pins", "", &mut read, &mut report).unwrap(); + let Some(Renderer::Single(sym)) = &s.renderer else { panic!() }; + assert_eq!(sym.marker, Marker::Circle); + let notes: Vec<&str> = report.notes.iter().map(|n| n.text.as_str()).collect(); + assert!(notes.contains(&from), "{notes:?}"); + assert!(!notes.iter().any(|n| n.contains("no image")), "{notes:?}"); + // Along a line, the same. + let line = format!( + r#" + + {}"#, + by_expression("") + ); + let mut report = Report::default(); + super::read_style(&line, "Road", "", &mut read, &mut report).unwrap(); + let notes: Vec<&str> = report.notes.iter().map(|n| n.text.as_str()).collect(); + assert!(notes.contains(&from), "{notes:?}"); + // With an image of its own too, it is drawn, and the expression is said (the generic note). + let mut report = Report::default(); + let s = super::read_style(&marker_qml("1", &by_expression("base64:QUJD")), "Pins", "", &mut read, &mut report).unwrap(); + let Some(Renderer::Single(sym)) = &s.renderer else { panic!() }; + assert!(matches!(sym.marker, Marker::Picture(_)), "{:?}", sym.marker); + let notes: Vec<&str> = report.notes.iter().map(|n| n.text.as_str()).collect(); + assert!(notes.contains(&generic) && !notes.contains(&from), "{notes:?}"); + // With no expression, an empty image is still no image. + let mut report = Report::default(); + super::read_style(&marker_qml("1", &raster_marker("", "")), "Pins", "", &mut read, &mut report).unwrap(); + assert_eq!(report.notes.iter().map(|n| n.text.as_str()).collect::>(), ["raster image markers with no image, drawn as circles"]); +} + #[test] fn raster_image_markers_that_cant_be_drawn_are_circles_saying_why() { // Each kind of image the reader isn't asked for, or can't give, with what the note says. @@ -1498,6 +1614,25 @@ fn raster_image_markers_that_cant_be_drawn_are_circles_saying_why() { ), ("\\\\host\\share\\a.png", "raster image markers whose image is on a network share, which RustGIS doesn't open, drawn as circles"), ("//host/share/a.png", "raster image markers whose image is on a network share, which RustGIS doesn't open, drawn as circles"), + // A web address written after `file:` is still one, whatever else its form looks like. + ( + "file://https://example.invalid/a.png", + "raster image markers whose image is at a web address, which RustGIS doesn't fetch, drawn as circles", + ), + ( + "file:https://example.invalid/a.png", + "raster image markers whose image is at a web address, which RustGIS doesn't fetch, drawn as circles", + ), + ("file://host/share/a.png", "raster image markers whose image is on a network share, which RustGIS doesn't open, drawn as circles"), + // A share written so that decoding leaves a space before it, which `absolute` would trim. + ( + "file:%20%2F%2Fhost/share/a.png", + "raster image markers whose image is on a network share, which RustGIS doesn't open, drawn as circles", + ), + ( + "file:%09%5C%5Chost%5Cshare%5Ca.png", + "raster image markers whose image is on a network share, which RustGIS doesn't open, drawn as circles", + ), ("/data/gone.png", "raster image markers whose image file doesn't open, drawn as circles"), ]; for (file, note) in cases { diff --git a/docs/user-guide/workspace.md b/docs/user-guide/workspace.md index d28e820c46..de2a7ac6fa 100644 --- a/docs/user-guide/workspace.md +++ b/docs/user-guide/workspace.md @@ -432,7 +432,7 @@ holding `NULL`, which QGIS takes as unknown and RustGIS skips. What RustGIS does near as it can, or left out, and said: the import's notes list, layer by layer, what was approximated (vector tile styles, text matched ignoring letter case, a memory layer's fields of kinds RustGIS hasn't as text, gradient and shapeburst fills' offsets left out and colour ramps other than gradients drawn from their first colour to their last, SVG, raster and random fills and dense brushes as a tint of their colour, SVG markers as circles, raster image markers whose image RustGIS can't use as circles (one at a web address, which it -doesn't fetch, or kept inside a `.qgz`; a file that doesn't open or isn't a PNG, JPEG or SVG; one past +doesn't fetch, on a network share (`\\host\share`, `file://host/share`), a relative name with `://` in it, or kept inside a `.qgz`; one a field or an expression gives, which RustGIS doesn't evaluate; a file that doesn't open or isn't a PNG, JPEG or SVG; one past 2 MiB of base64, 2048 × 2048 pixels or as much to draw, an SVG with filters, text, images, animation or links to other files, the images it keeps or the pixels or memory of marker images it decodes until it restarts), those of a fixed aspect ratio in their image's own, see-through ones opaque and those anchored off their centre along lines centred,