diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index d5853c2bb4..998dd74867 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -32,7 +32,7 @@ pub fn commands() -> Vec { r#"{"name"?: string, "page"?: "letter"|"legal"|"tabloid"|"a5"|"a4"|"a3"|"a2"|"a1"|"a0", "orientation"?: "landscape"|"portrait", "width"?: mm, "height"?: mm, "template"?: "map" (frame, title, legend, north arrow, scale bar; the default)|"blank"}"#, ), C::new("layout.publication", "New Publication Layout", "Publish › Layout", publication).inputs(publication_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( - r#"{"profile"?: "a4" (the default) | "a3" | "a2" (a sheet turned to portrait or landscape, whichever the area's map comes out larger on: its extent east to west against north to south, on the map's projection) | "a4-portrait"|"a4-landscape"|"a3-portrait"|"a3-landscape"|"a2-portrait"|"a2-landscape" (a sheet: frame, title, subtitle, the map, a boxed footer)|"figure-1col" (90 mm)|"figure-1.5col" (140 mm)|"figure-2col" (190 mm)|"thesis-full" (160 × 225 mm)|"thesis-half" (160 × 110 mm) (a figure: the map edge to edge, its title and sources left to the caption), "extent"?: [west, south, east, north] in degrees that must fit (default: the map view), "name"?: string, "title"?: text, "subtitle"?: text, "sources"?: text | [texts], "reference"?: text | [texts] (default: the projection, datum and graticule), "note"?: text, "author"?: text (default: "Made with RustGIS · {date}"; "" or [] for none), "headings"?: [the sources' heading, the reference's heading, an editorial column's heading], "locator"?: "globe" (a globe with the area outlined; the default on sheets) | "inset" (the surrounding region, the area outlined) | "none" (the default on figures), "decimal"?: "." (the default) | "," (graticule, scale bar and colorbar numbers and {scale} with a decimal comma: 0,05 and 1:6.300.000), "colorbar"?: "auto" (the default: the top visible raster with a colour scale, not drawn by classes, gets one, under the legend in its box; a raster drawn by classes has them in the legend's rows instead) | "none" (no colorbar, and the legend's box has no room for one), "veil"?: true (the default: when no shape of the legend fits, a map that runs on past the extent, with a long legend, has what is outside the extent veiled and its surrounds grouped there, at up to half the scale on a sheet) | false (never veiled, and drawn at its scale; the map may still move aside), "contextInLegend"?: false (the default: layers the placement takes as context around the data, polygons only outlined or, beside a layer drawn by classes or a raster, in one colour and running on past the frame, are left out of the legend, which a warning names) | true (the legend lists them), "northArrow"?: "auto" (the default: none when the extent spans 20° or more on its longer side, where the graticule gives north, which a warning says) | "always" | "none", "legendBox"?: "auto" (the default: over the map, the legend has no box where the map shows nothing under it but its plain paper or water, with no layer, context, basemap shape or graticule line there and no veil; the answer's "legendBox" says whether it has one) | "always", "style"?: "classic" (the default: the title over the map, the footer under it) | "banner" (the title and subtitle in a band of the accent colour across the top) | "panel" (the map at the left, a panel at the right with the title, locator, legend, sources and reference) | "editorial" (the title large in serif, a column at the right with the note, the locator and the legend) | "modular" (cards on a light page: the map, the locator and the legend each in one, the sources and reference in tinted ones) | "poster" (the page in the accent colour, the title in light text, the map in a light card, the footer in darker blocks) (a sheet's arrangement; figures leave it to the caption), "accent"?: a colour as #RRGGBB (the style's colour: its band, panel tint, headings and rules, a poster's page), "arrangement"?: "best" (the default: the favourite, each arrangement by name and each saved with layout.arrangement laid out, the one moving the least area off its corners kept, then the one leaving the least over the data, the favourite on a tie) | "favourite" (layout.arrangement's) | an arrangement by name (built in or saved) or corner by corner, as layout.arrangement takes (the corners the locator, north arrow, legend and scale bar are at home in; one covering what the map draws there still moves), "coverage"?: "*.png" (writes the coverage grid the placement searched, as it settled on it, three pixels a cell: what the map draws dark, the free cells white, both tinted blue in the strips the graticule's labels keep, the surrounds placed over the map outlined in red; the answer gives its path)} → a new layout of the active map at a round scale, its texts wrapped to their boxes; {projection}, {datum} and {grid} in the texts are filled from the map"#, + r#"{"profile"?: "a4" (the default) | "a3" | "a2" (a sheet turned to portrait or landscape, whichever the area's map comes out larger on: its extent east to west against north to south, on the map's projection) | "a4-portrait"|"a4-landscape"|"a3-portrait"|"a3-landscape"|"a2-portrait"|"a2-landscape" (a sheet: frame, title, subtitle, the map, a boxed footer)|"figure-1col" (90 mm)|"figure-1.5col" (140 mm)|"figure-2col" (190 mm)|"thesis-full" (160 × 225 mm)|"thesis-half" (160 × 110 mm) (a figure: the map edge to edge, its title and sources left to the caption), "extent"?: [west, south, east, north] in degrees that must fit (default: the map view), "name"?: string, "title"?: text, "subtitle"?: text, "sources"?: text | [texts], "reference"?: text | [texts] (default: the projection, datum and graticule), "note"?: text, "author"?: text (default: "Made with RustGIS · {date}"; "" or [] for none), "headings"?: [the sources' heading, the reference's heading, an editorial column's heading], "locator"?: "globe" (a globe with the area outlined; the default on sheets) | "inset" (the surrounding region, the area outlined) | "none" (the default on figures), "decimal"?: "." (the default) | "," (graticule, scale bar and colorbar numbers and {scale} with a decimal comma: 0,05 and 1:6.300.000), "colorbar"?: "auto" (the default: the top visible raster with a colour scale, not drawn by classes, gets one, under the legend in its box; a raster drawn by classes has them in the legend's rows instead) | "none" (no colorbar, and the legend's box has no room for one), "veil"?: true (the default: when no shape of the legend fits, a map that runs on past the extent, with a long legend, has what is outside the extent veiled and its surrounds grouped there, at up to half the scale on a sheet) | false (never veiled, and drawn at its scale; the map may still move aside), "contextInLegend"?: false (the default: layers the placement takes as context around the data, polygons only outlined or, beside a layer drawn by classes or a raster, in one colour and running on past the frame, are left out of the legend, which a warning names) | true (the legend lists them), "northArrow"?: "auto" (the default: none when the extent spans 20° or more on its longer side, where the graticule gives north, which a warning says) | "always" | "none", "legendBox"?: "auto" (the default: over the map, the legend has no box where the map shows nothing under it but its light plain paper or water, with no layer, context, basemap shape or graticule line there and no veil; the answer's "legendBox" says whether it has one) | "always", "style"?: "classic" (the default: the title over the map, the footer under it) | "banner" (the title and subtitle in a band of the accent colour across the top) | "panel" (the map at the left, a panel at the right with the title, locator, legend, sources and reference) | "editorial" (the title large in serif, a column at the right with the note, the locator and the legend) | "modular" (cards on a light page: the map, the locator and the legend each in one, the sources and reference in tinted ones) | "poster" (the page in the accent colour, the title in light text, the map in a light card, the footer in darker blocks) (a sheet's arrangement; figures leave it to the caption), "accent"?: a colour as #RRGGBB (the style's colour: its band, panel tint, headings and rules, a poster's page), "arrangement"?: "best" (the default: the favourite, each arrangement by name and each saved with layout.arrangement laid out, the one moving the least area off its corners kept, then the one leaving the least over the data, the favourite on a tie) | "favourite" (layout.arrangement's) | an arrangement by name (built in or saved) or corner by corner, as layout.arrangement takes (the corners the locator, north arrow, legend and scale bar are at home in; one covering what the map draws there still moves), "coverage"?: "*.png" (writes the coverage grid the placement searched, as it settled on it, three pixels a cell: what the map draws dark, the free cells white, both tinted blue in the strips the graticule's labels keep, the surrounds placed over the map outlined in red; the answer gives its path)} → a new layout of the active map at a round scale, its texts wrapped to their boxes; {projection}, {datum} and {grid} in the texts are filled from the map"#, ), C::new("layout.arrangement", "Favourite Arrangement", "Publish › Layout", arrangement).roles(&[]).effects(crate::policy::Effects::NONE).view().params( r#"{"favourite"?: "classic" (the locator top left, the north arrow top right, the legend bottom left, the scale bar bottom right) | "mirrored" | "legendRight" | "legendTop" | {"locator", "northArrow", "legend", "scaleBar": "top-left"|"top-right"|"bottom-left"|"bottom-right", each a corner of its own} | a saved arrangement's name, "save"?: a name (1 to 40 characters, not a built-in one nor "best", "favourite", "custom" or "veil"; one saved under it already, case aside, is replaced) to save "arrangement" under (at most 20 saved), "arrangement"?: what "save" saves: an arrangement by name, or a corner for each surround as "favourite" takes them (refused without "save"), "remove"?: a saved arrangement's name (a favourite named after it keeps its corners, unless "favourite" gives another)} → the favourite arrangement of publication layouts and those saved by name (kept with the app's preferences), the favourite's corners, the arrangements by name and the saved ones' corners; a removal, then a save, then the favourite; with none of them given, they are only shown"#, @@ -5548,13 +5548,13 @@ fn graticule_crosses(s: &Session, center: Coord, scale: f64, (fw, fh): (f64, f64 const N: usize = 8; let flat = s.map().flat(); let Some(at) = flat.forward_checked(center) else { return true }; - let mm = 1000.0 * flat.ground_per_unit(at) * s.map().body().ratio() / scale; + let paper_mm_per_unit = 1000.0 * flat.ground_per_unit(at) * s.map().body().ratio() / scale; let (mut lon, mut lat) = ((f64::INFINITY, f64::NEG_INFINITY), (f64::INFINITY, f64::NEG_INFINITY)); for i in 0..=N { for j in 0..=N { let x = under.0 + under.2 * i as f64 / N as f64 - fw / 2.0; let y = under.1 + under.3 * j as f64 / N as f64 - fh / 2.0; - let Some(ll) = flat.inverse(Coord::new(at.x + x / mm, at.y - y / mm)) else { return true }; + let Some(ll) = flat.inverse(Coord::new(at.x + x / paper_mm_per_unit, at.y - y / paper_mm_per_unit)) else { return true }; (lon, lat) = ((lon.0.min(ll.x), lon.1.max(ll.x)), (lat.0.min(ll.y), lat.1.max(ll.y))); } } diff --git a/crates/render/src/canvas.rs b/crates/render/src/canvas.rs index 7c7650d9bd..01bd2233cd 100644 --- a/crates/render/src/canvas.rs +++ b/crates/render/src/canvas.rs @@ -33,6 +33,7 @@ struct Recording { layer: bool, /// The glyphs kept are a halo this wide ([`Canvas::halo`]). halo: Option, + decorative: bool, } impl Canvas { @@ -50,7 +51,17 @@ impl Canvas { pub fn recording(width: u32, height: u32) -> Option { let pm = Pixmap::new(width, height)?; let drawing = Drawing::new(width as f32, height as f32); - Some(Canvas { pm, rec: Some(Box::new(Recording { drawing, dirty: false, ts: Transform::identity(), layer: false, halo: None })) }) + Some(Canvas { + pm, + rec: Some(Box::new(Recording { + drawing, + dirty: false, + ts: Transform::identity(), + layer: false, + halo: None, + decorative: false, + })), + }) } /// Whether it records. @@ -208,10 +219,24 @@ impl Canvas { self.keep(|r| { run.transform = r.ts.pre_concat(run.transform); run.halo = run.halo.or(r.halo); + run.decorative |= r.decorative; r.drawing.glyphs(run); }); } + /// Draws another visual strike of existing text. Its recorded glyphs remain visible but + /// are marked as decoration for PDF selection and SVG accessibility. + pub fn decoration(&mut self, f: impl FnOnce(&mut Canvas)) { + let old = self.rec.as_ref().map(|r| r.decorative); + if let Some(r) = &mut self.rec { + r.decorative = true; + } + f(self); + if let (Some(r), Some(old)) = (&mut self.rec, old) { + r.decorative = old; + } + } + /// Draws with `f`, what is kept moved by `t` (from `f`'s space to this canvas's); for drawing /// placed with a transform where a canvas that draws pixels would draw a picture and place it. /// What `f` draws on the pixels is not moved. @@ -496,6 +521,7 @@ mod tests { transform: Transform::identity(), glyphs: vec![crate::vector::Glyph { id: ab_glyph::Font::glyph_id(font, 'A'), x: 0.0, ch: 'A' }], halo: None, + decorative: false, }; for width in [0.0, -1.0, f32::NAN, f32::INFINITY] { let mut c = Canvas::recording(40, 20).unwrap(); diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index fb87609e7e..041283a809 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -2532,9 +2532,9 @@ pub fn frame_coverage_judged_on( } /// Whether a frame showing `map` at 1:`scale` around `center`, `size` (mm) wide and tall, shows -/// one plain colour under `under` (mm from the frame's top left), the basemap's water or land -/// colour, and nothing else: no layer, label or context layer, no outline of the basemap's. Drawn as the map is, on a -/// grid of about `cells` cells across the frame's longer side, its label lock set aside (and put +/// one light plain colour under `under` (mm from the frame's top left), the basemap's water or +/// land colour, and nothing else: no layer, label or context layer, no outline of the basemap's. +/// Drawn as the map is, on a grid of about `cells` cells across the frame's longer side, its label lock set aside (and put /// back) as [`frame_coverage`] does. Web tiles aren't fetched for it, so a map with any is not /// plain anywhere; nor is a frame that can't be drawn. For a publication's legend, which needs no /// box there (#1379). @@ -2589,7 +2589,10 @@ pub fn frame_plain_under( // one colour, data or context, isn't the background. let rgba = |p: tiny_skia::PremultipliedColorU8| (p.red(), p.green(), p.blue(), p.alpha()); let Some(first) = seen.next().map(rgba) else { return false }; - let background = [water, land].iter().any(|c| (c.0[0], c.0[1], c.0[2], 255) == first); + // Legend text stays dark (LEGEND_TEXT), so dark water or land must keep its paper backing. + let background = [water, land] + .iter() + .any(|c| (c.0[0], c.0[1], c.0[2], 255) == first && u16::from(c.r()) + u16::from(c.g()) + u16::from(c.b()) > 3 * 160); background && seen.all(|p| rgba(p) == first) } @@ -3437,6 +3440,19 @@ mod tests { assert!(!plain(&mut map, (2.0, 86.0, 12.0, 12.0)), "web tiles aren't fetched for it, so nothing is plain"); } + #[test] + fn plain_dark_basemaps_keep_the_legend_paper_while_light_ones_clear_it() { + for body in ["earth", "mars"] { + for basemap in [rustgis_map::Basemap::Midnight, rustgis_map::Basemap::Paper, rustgis_map::Basemap::Ocean] { + let mut map = Map::new("plain"); + (map.body, map.basemap, map.graticule) = (body.into(), basemap, false); + // The Earth frame is open Gulf of Guinea water; on Mars it is the palette's grey. + let plain = frame_plain_under(&mut map, Coord::new(0.0, 0.0), 1_000_000.0, (100.0, 100.0), (40.0, 40.0, 20.0, 20.0), 128); + assert_eq!(plain, basemap != rustgis_map::Basemap::Midnight, "{body}: {basemap:?}"); + } + } + } + /// A layer that only outlines its polygons isn't counted as drawn, unless nothing else is shown, /// and it is shown again after; one with labels is more than an outline. #[test] diff --git a/crates/render/src/pdf/content.rs b/crates/render/src/pdf/content.rs index 70bd085f86..b6fd3bd275 100644 --- a/crates/render/src/pdf/content.rs +++ b/crates/render/src/pdf/content.rs @@ -289,7 +289,8 @@ impl Writer<'_> { set_color(out, &mut st.stroke, run.color, "RG"); stroke_state(out, st, &GlyphRun::halo_stroke(w * scale)); } - if halo.is_some() { + let decorative = halo.is_some() || run.decorative; + if decorative { // The copy is decoration, marked so (an artifact) for readers that leave artifacts out // of what they copy and find. (An empty `ActualText` would hide it from every reader's // text, but VectorCraft then drops the halo altogether.) @@ -326,7 +327,14 @@ impl Writer<'_> { pen = x + p.advance * size; } // (The rendering mode is text state, kept past ET: set back for the text that follows.) - out.extend_from_slice(if halo.is_some() { b"] TJ\n0 Tr\nET\nEMC\n" } else { b"] TJ\nET\n" }); + out.extend_from_slice(b"] TJ\n"); + if halo.is_some() { + out.extend_from_slice(b"0 Tr\n"); + } + out.extend_from_slice(b"ET\n"); + if decorative { + out.extend_from_slice(b"EMC\n"); + } } /// Places a picture: an image object (with its alpha as a soft mask) drawn where it lies. @@ -749,6 +757,7 @@ mod tests { transform: Transform::from_translate(210.0, 40.0), glyphs, halo: None, + decorative: false, }); let mut pic = Pixmap::new(4, 4).unwrap(); pic.fill(Color::from_rgba8(0, 160, 0, 200)); @@ -834,7 +843,15 @@ mod tests { let x = font.h_advance_unscaled(a) / upm * 20.0 + 5.0; let mut d = Drawing::new(100.0, 100.0); let glyphs = vec![Glyph { id: a, x: 0.0, ch: 'A' }, Glyph { id: v, x, ch: 'V' }]; - d.glyphs(GlyphRun { font, size: 20.0, color: Color::BLACK, transform: Transform::from_translate(10.0, 50.0), glyphs, halo: None }); + d.glyphs(GlyphRun { + font, + size: 20.0, + color: Color::BLACK, + transform: Transform::from_translate(10.0, 50.0), + glyphs, + halo: None, + decorative: false, + }); let objs = objects(&write(&[Page::drawing(d, 10.0, 10.0)], "")); let content = objs.iter().map(|o| String::from_utf8_lossy(&o.1).into_owned()).find(|c| c.contains("TJ")).unwrap(); assert!(content.contains("1 0 0 -1 10 50 Tm\n") && content.contains("[<00>-250<01>] TJ"), "{content}"); diff --git a/crates/render/src/pdf/embed.rs b/crates/render/src/pdf/embed.rs index 5d3a5c68b8..189dc19964 100644 --- a/crates/render/src/pdf/embed.rs +++ b/crates/render/src/pdf/embed.rs @@ -236,6 +236,7 @@ mod tests { transform: Transform::from_row(1.0, 0.0, kx, 1.0, 10.0, 40.0), glyphs: text.chars().zip(0..).map(|(ch, i)| Glyph { id: font.glyph_id(ch), x: 11.0 * i as f32, ch }).collect(), halo: None, + decorative: false, }; let mut d = Drawing::new(200.0, 100.0); d.glyphs(run(0.0)); @@ -360,6 +361,7 @@ mod tests { transform: Transform::from_row(2.0, 0.0, 0.0, 2.0, 10.0, 40.0), glyphs: "Rio".chars().zip(0..).map(|(ch, i)| Glyph { id: font.glyph_id(ch), x: 11.0 * i as f32, ch }).collect(), halo, + decorative: false, }; let page = || { let mut d = Drawing::new(200.0, 100.0); @@ -387,6 +389,19 @@ mod tests { // In outlines: the halo as its glyphs' outlines, filled and stroked. let outlines = streams(&write_with(&[page()], "Halo", Options { text: Text::Outlines, ..Options::default() }).0); assert!(!outlines.contains(" Tr\n") && !outlines.contains("/Artifact") && outlines.contains("\nB\n"), "{outlines}"); + // A synthetic-bold copy is decoration too, without a halo's stroked rendering mode. + let mut drawing = Drawing::new(200.0, 100.0); + drawing.glyphs(run(None, Color::BLACK)); + let mut strike = run(None, Color::BLACK); + strike.transform.tx += 1.0; + strike.decorative = true; + drawing.glyphs(strike); + let pdf = write_with(&[Page::drawing(drawing, 100.0, 50.0)], "Bold", Options { text: Text::Fonts, ..Options::default() }).0; + let content = streams(&pdf); + let (before, artifact) = content.split_once("/Artifact BMC\n").unwrap(); + assert_eq!(before.matches("BT\n").count(), 1, "the actual text is kept once"); + let artifact = artifact.split("EMC\n").next().unwrap(); + assert!(artifact.starts_with("BT\n") && !artifact.contains(" Tr\n"), "{artifact}"); } /// A face whose licence forbids embedding (fsType 2) is set in outlines, and said. diff --git a/crates/render/src/svg/vectors.rs b/crates/render/src/svg/vectors.rs index 8967340301..47ed3839c3 100644 --- a/crates/render/src/svg/vectors.rs +++ b/crates/render/src/svg/vectors.rs @@ -206,7 +206,11 @@ fn glyphs(out: &mut String, defs: &mut Defs, run: &GlyphRun) { }); // The halo's copy is decoration: hidden from screen readers, which read the label once (as a // PDF marks it an artifact). - let label = if run.halo.is_some() { r#" aria-hidden="true""#.to_string() } else { format!(r#" aria-label="{}""#, super::esc(&text)) }; + let label = if run.halo.is_some() || run.decorative { + r#" aria-hidden="true""#.to_string() + } else { + format!(r#" aria-label="{}""#, super::esc(&text)) + }; let _ = writeln!(out, r#""#, paint("fill", run.color), transform_attr(run.transform)); for g in &run.glyphs { let id = match defs.glyphs.get(&(face, g.id.0)) { @@ -386,7 +390,15 @@ mod tests { let o = font.glyph_id('o'); let glyphs = vec![Glyph { id: o, x: 0.0, ch: 'o' }, Glyph { id: font.glyph_id(' '), x: 6.0, ch: ' ' }, Glyph { id: o, x: 9.0, ch: 'o' }]; - d.glyphs(GlyphRun { font, size: 12.0, color: Color::BLACK, transform: Transform::from_translate(60.0, 40.0), glyphs, halo: None }); + d.glyphs(GlyphRun { + font, + size: 12.0, + color: Color::BLACK, + transform: Transform::from_translate(60.0, 40.0), + glyphs, + halo: None, + decorative: false, + }); let mut pic = tiny_skia::Pixmap::new(2, 2).unwrap(); pic.fill(Color::from_rgba8(0, 128, 0, 255)); d.image(pic.as_ref(), Transform::from_scale(5.0, 5.0), 0.5, false); @@ -432,9 +444,20 @@ mod tests { let glyphs = vec![Glyph { id: ab_glyph::Font::glyph_id(font, 'H'), x: 0.0, ch: 'H' }]; let upm = ab_glyph::Font::units_per_em(font).unwrap(); let mut d = Drawing::new(40.0, 40.0); - let run = |color, halo| GlyphRun { font, size: 10.0, color, transform: Transform::identity(), glyphs: glyphs.clone(), halo }; + let run = |color, halo| GlyphRun { + font, + size: 10.0, + color, + transform: Transform::identity(), + glyphs: glyphs.clone(), + halo, + decorative: false, + }; d.glyphs(run(Color::WHITE, Some(2.0))); d.glyphs(run(Color::BLACK, None)); + let mut second_strike = run(Color::BLACK, None); + second_strike.decorative = true; + d.glyphs(second_strike); let (_, _, list) = d.finish(); let (mut svg, mut defs) = (String::new(), Defs::default()); items(&mut svg, &mut defs, &list).unwrap(); @@ -444,6 +467,7 @@ mod tests { let groups: Vec<&str> = svg.lines().filter(|l| l.starts_with("