From f73ce76241ecc22e198bbc32ca01f3db71ca3d8a Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 15 Nov 2026 11:39:14 -0500 Subject: [PATCH 1/6] fix(render): bound overlapping terrain-water index references --- crates/render/src/terrain/water.rs | 48 ++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/crates/render/src/terrain/water.rs b/crates/render/src/terrain/water.rs index 6351b595c0..71211de6f9 100644 --- a/crates/render/src/terrain/water.rs +++ b/crates/render/src/terrain/water.rs @@ -22,6 +22,9 @@ const MAX_SHORE_SAMPLES: usize = 1 << 22; /// The most cells along each side of the grid that finds the lakes near a point. const MAX_GRID: usize = 256; +/// The most lake references duplicated into grid cells. A single-cell index needs one per lake, +/// so an input with more lakes than this keeps that unavoidable minimum without duplication. +const MAX_GRID_REFERENCES: usize = 1 << 20; /// The most references to its edges a polygon's strips hold, on average per edge. const EDGES_PER_STRIP_REF: usize = 16; @@ -184,12 +187,28 @@ impl Water { extent = extent.union(&e); lakes.push(Lake { edges: Slabs::new(&rings, &e), extent: e, level }); } - let side = ((lakes.len() as f64).sqrt().ceil() as usize * 2).clamp(1, MAX_GRID); + let mut side = ((lakes.len() as f64).sqrt().ceil() as usize * 2).clamp(1, MAX_GRID); + let (w, h) = (extent.width().max(f64::MIN_POSITIVE), extent.height().max(f64::MIN_POSITIVE)); + let cell = |v: f64, lo: f64, size: f64, n: usize| (((v - lo) / size * n as f64).floor().max(0.0) as usize).min(n - 1); + let cap = MAX_GRID_REFERENCES.max(lakes.len()); + // Overlapping boxes can each span every cell. Count before allocating references, and + // coarsen the index rather than making up to MAX_GRID² copies of every lake's index. + // Containment and first-row precedence still use the original polygons. + while side > 1 + && lakes + .iter() + .try_fold(0usize, |total, l| { + let cols = cell(l.extent.max_x, extent.min_x, w, side) - cell(l.extent.min_x, extent.min_x, w, side) + 1; + let rows = cell(l.extent.max_y, extent.min_y, h, side) - cell(l.extent.min_y, extent.min_y, h, side) + 1; + Some(total.saturating_add(cols.saturating_mul(rows))).filter(|n| *n <= cap) + }) + .is_none() + { + side /= 2; + } let (cols, rows) = (side, side); let mut cells = vec![Vec::new(); cols * rows]; if !extent.is_empty() { - let (w, h) = (extent.width().max(f64::MIN_POSITIVE), extent.height().max(f64::MIN_POSITIVE)); - let cell = |v: f64, lo: f64, size: f64, n: usize| (((v - lo) / size * n as f64).floor().max(0.0) as usize).min(n - 1); for (i, l) in lakes.iter().enumerate() { for r in cell(l.extent.min_y, extent.min_y, h, rows)..=cell(l.extent.max_y, extent.min_y, h, rows) { for c in cell(l.extent.min_x, extent.min_x, w, cols)..=cell(l.extent.max_x, extent.min_x, w, cols) { @@ -518,6 +537,29 @@ mod tests { assert_eq!(Ground::of(&m, &camera).unwrap().water_unlevelled(), 0); } + /// Every overlapping lake used to occupy every one of the 256² grid cells: 16,384 simple + /// polygons attempted a billion references. A coarser index bounds that expansion while + /// retaining the first overlapping polygon's level and ordinary containment. + #[test] + fn overlapping_lakes_do_not_expand_past_the_index_budget() { + let (map, camera) = hill_with_water(None); + let ground = Ground::of(&map, &camera).unwrap(); + let polygons = (0..16_384).map(|i| (vec![square(0.0, 0.0, 10.0)], Some(i as f64 + 5.0))).collect(); + let water = Water::build(polygons, &ground, MAX_SHORE_SAMPLES); + let references: usize = water.cells.iter().map(Vec::len).sum(); + assert!(references <= MAX_GRID_REFERENCES, "{references} references"); + assert_eq!(water.lakes.len(), 16_384, "the coarser index drops no lake"); + assert_eq!(water.level(Coord::new(5.0, 5.0)), Some(5.0)); + assert_eq!(water.level(Coord::new(11.0, 5.0)), None); + // Disjoint lakes keep a useful multi-cell index and their distinct levels. + let polygons = (0..100).map(|i| (vec![square(f64::from(i) * 20.0, 0.0, 10.0)], Some(f64::from(i)))).collect(); + let water = Water::build(polygons, &ground, MAX_SHORE_SAMPLES); + assert!(water.cols > 1); + for i in 0..100 { + assert_eq!(water.level(Coord::new(f64::from(i) * 20.0 + 5.0, 5.0)), Some(f64::from(i))); + } + } + #[test] fn a_long_shore_is_read_at_the_grounds_cell_and_its_budget_said() { let at = |lon, lat| lonlat_to_merc(Coord::new(lon, lat)); From 31321621517dcf658c9be3c1880a87a18b9312c9 Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 15 Nov 2026 13:02:53 -0500 Subject: [PATCH 2/6] Fix terrain water refresh and synthetic bold rendering --- crates/render/src/canvas.rs | 28 +++++- crates/render/src/lib.rs | 2 +- crates/render/src/pdf/content.rs | 23 ++++- crates/render/src/pdf/embed.rs | 15 ++++ crates/render/src/svg/vectors.rs | 30 ++++++- crates/render/src/terrain.rs | 13 +++ crates/render/src/terrain/water.rs | 136 +++++++++++++++++++++++------ crates/render/src/text.rs | 63 +++++++++++-- crates/render/src/vector.rs | 3 + crates/ui-egui/src/mapview.rs | 56 +++++++++++- 10 files changed, 326 insertions(+), 43 deletions(-) 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/lib.rs b/crates/render/src/lib.rs index 70f6936a41..938045196f 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -37,7 +37,7 @@ mod stroke_paths; pub mod svg; pub mod terrain; mod text; -pub use text::CALLOUT_WIDTH; +pub use text::{CALLOUT_WIDTH, thickening as label_thickening}; pub mod tile_mesh; mod tiles; pub mod vector; 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("