map_gui/tools/labels.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::OnceLock;
use regex::Regex;
use abstutil::Timer;
use geom::{Angle, Bounds, Distance, Polygon, Pt2D, QuadTree};
use map_model::{osm, Road, RoadID};
use widgetry::mapspace::PerZoom;
use widgetry::{Color, Drawable, EventCtx, GeomBatch, GfxCtx, Line, Text};
use crate::AppLike;
/// Labels roads when unzoomed. Label size and frequency depends on the zoom level.
///
/// By default, the text is white; it works well on dark backgrounds.
pub struct DrawRoadLabels {
per_zoom: RefCell<Option<PerZoom>>,
include_roads: Box<dyn Fn(&Road) -> bool>,
fg_color: Color,
outline_color: Color,
}
impl DrawRoadLabels {
/// Label roads that the predicate approves
pub fn new(include_roads: Box<dyn Fn(&Road) -> bool>) -> Self {
Self {
per_zoom: Default::default(),
include_roads,
fg_color: Color::WHITE,
outline_color: Color::BLACK,
}
}
/// Only label major roads
pub fn only_major_roads() -> Self {
Self::new(Box::new(|r| {
r.get_rank() != osm::RoadRank::Local && !r.is_light_rail()
}))
}
pub fn light_background(mut self) -> Self {
self.fg_color = Color::BLACK;
self.outline_color = Color::WHITE;
self
}
pub fn draw(&self, g: &mut GfxCtx, app: &dyn AppLike) {
let mut per_zoom = self.per_zoom.borrow_mut();
if per_zoom.is_none() {
*per_zoom = Some(PerZoom::new(g.canvas.settings.min_zoom_for_detail, 0.1));
}
let per_zoom = per_zoom.as_mut().unwrap();
let (zoom, idx) = per_zoom.discretize_zoom(g.canvas.cam_zoom);
let draw = &mut per_zoom.draw_per_zoom[idx];
if draw.is_none() {
*draw = Some(self.render(g, app, zoom));
}
g.redraw(draw.as_ref().unwrap());
}
fn render(&self, g: &mut GfxCtx, app: &dyn AppLike, zoom: f64) -> Drawable {
let mut batch = GeomBatch::new();
let map = app.map();
// We want the effective size of the text to stay around 1
// effective = zoom * text_scale
let text_scale = 1.0 / zoom;
let mut quadtree = QuadTree::new();
'ROAD: for r in map.all_roads() {
if !(self.include_roads)(r) || r.length() < Distance::meters(30.0) {
continue;
}
let name = if let Some(x) = simplify_name(r.get_name(app.opts().language.as_ref())) {
x
} else {
continue;
};
let (pt, angle) = r.center_pts.must_dist_along(r.length() / 2.0);
// Don't get too close to other labels.
let big_bounds = cheaply_overestimate_bounds(&name, text_scale, pt, angle);
if quadtree.query_bbox(big_bounds).next().is_some() {
continue 'ROAD;
}
quadtree.insert_with_box((), big_bounds);
// No other labels too close - proceed to render text.
let txt = Text::from(
Line(&name)
.big_heading_plain()
.fg(self.fg_color)
.outlined(self.outline_color),
);
batch.append(txt.render_autocropped(g).multi_transform(
text_scale,
pt,
angle.reorient(),
));
}
g.upload(batch)
}
}
static SIMPLIFY_PATTERNS: OnceLock<Vec<(Regex, String)>> = OnceLock::new();
// TODO Surely somebody has written one of these.
fn simplify_name(mut x: String) -> Option<String> {
// Skip unnamed roads and highway exits
if x == "???" || x.starts_with("Exit for ") {
return None;
}
for (search, replace_with) in SIMPLIFY_PATTERNS.get_or_init(simplify_patterns).iter() {
// TODO The string copies are probably avoidable...
x = search.replace(&x, replace_with).to_string();
}
Some(x)
}
fn simplify_patterns() -> Vec<(Regex, String)> {
let mut replace = Vec::new();
for (long, short) in [
("Northeast", "NE"),
("Northwest", "NW"),
("Southeast", "SE"),
("Southwest", "SW"),
// Order matters -- do the longer patterns first
("North", "N"),
("South", "S"),
("East", "E"),
("West", "W"),
] {
// Only replace directions at the start or end of the string
replace.push((
Regex::new(&format!("^{long} ")).unwrap(),
format!("{short} "),
));
replace.push((
Regex::new(&format!(" {long}$")).unwrap(),
format!(" {short}"),
));
}
for (long, short) in [
("Street", "St"),
("Boulevard", "Blvd"),
("Avenue", "Ave"),
("Place", "Pl"),
] {
// At the end is reasonable
replace.push((
Regex::new(&format!("{}$", long)).unwrap(),
short.to_string(),
));
// In the middle, surrounded by spaces
replace.push((
Regex::new(&format!(" {} ", long)).unwrap(),
format!(" {} ", short),
));
}
replace
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simplify_name() {
for (input, want) in [
("Northeast Northgate Way", "NE Northgate Way"),
("South 42nd Street", "S 42nd St"),
("Northcote Road", "Northcote Road"),
] {
let got = simplify_name(input.to_string()).unwrap();
if got != want {
panic!("simplify_name({}) = {}; expected {}", input, got, want);
}
}
}
}
fn cheaply_overestimate_bounds(text: &str, text_scale: f64, center: Pt2D, angle: Angle) -> Bounds {
// assume all chars are bigger than largest possible char
let letter_width = 30.0 * text_scale;
let letter_height = 30.0 * text_scale;
Polygon::rectangle_centered(
center,
Distance::meters(letter_width * text.len() as f64),
Distance::meters(letter_height),
)
.rotate(angle.reorient())
.get_bounds()
}
/// Draws labels in map-space that roughly fit on the roads. Don't change behavior during zooming;
/// labels are only meant to be legible when zoomed in.
pub struct DrawSimpleRoadLabels {
draw: Drawable,
include_roads: Box<dyn Fn(&Road) -> bool>,
fg_color: Color,
pub label_covers_road: HashMap<RoadID, (Distance, Distance)>,
}
impl DrawSimpleRoadLabels {
/// Label roads that the predicate approves
pub fn new(
ctx: &mut EventCtx,
app: &dyn AppLike,
fg_color: Color,
include_roads: Box<dyn Fn(&Road) -> bool>,
) -> Self {
let mut labels = Self {
draw: Drawable::empty(ctx),
include_roads,
fg_color,
label_covers_road: HashMap::new(),
};
ctx.loading_screen("label roads", |ctx, timer| {
labels.render(ctx, app, timer);
});
labels
}
pub fn empty(ctx: &EventCtx) -> Self {
Self {
draw: Drawable::empty(ctx),
include_roads: Box::new(|_| false),
fg_color: Color::CLEAR,
label_covers_road: HashMap::new(),
}
}
/// Only label major roads
pub fn only_major_roads(ctx: &mut EventCtx, app: &dyn AppLike, fg_color: Color) -> Self {
Self::new(
ctx,
app,
fg_color,
Box::new(|r| r.get_rank() != osm::RoadRank::Local && !r.is_light_rail()),
)
}
pub fn all_roads(ctx: &mut EventCtx, app: &dyn AppLike, fg_color: Color) -> Self {
Self::new(ctx, app, fg_color, Box::new(|_| true))
}
pub fn draw(&self, g: &mut GfxCtx) {
g.redraw(&self.draw);
}
fn render(&mut self, ctx: &mut EventCtx, app: &dyn AppLike, timer: &mut Timer) {
let mut batch = GeomBatch::new();
let map = app.map();
timer.start_iter("render roads", map.all_roads().len());
for r in map.all_roads() {
timer.next();
// Skip very short roads and tunnels
if !(self.include_roads)(r) || r.length() < Distance::meters(30.0) || r.zorder < 0 {
continue;
}
let name = if let Some(x) = simplify_name(r.get_name(app.opts().language.as_ref())) {
x
} else {
continue;
};
let txt_batch = Text::from(Line(&name)).render_autocropped(ctx);
if txt_batch.is_empty() {
// This happens when we don't have a font loaded with the right characters
continue;
}
let txt_bounds = txt_batch.get_bounds();
// The approach, part 1:
//
// We need to make the text fit in the road polygon. road_width gives us the height of
// the text, accounting for the outline around the road polygon and a buffer. If the
// road's length is short, the text could overflow into the intersections, so scale it
// down further.
//
// Since the text fits inside the road polygon, we don't need to do any kind of hitbox
// testing and make sure multiple labels don't overlap!
// The road has an outline of 1m, but also leave a slight buffer
let outline_thickness = Distance::meters(2.0);
let road_width = (r.get_width() - 2.0 * outline_thickness).inner_meters();
// Also a buffer from both ends of the road
let road_length = (0.9 * r.length()).inner_meters();
// Fit the text height in the road width perfectly
let mut scale = road_width / txt_bounds.height();
// If the road is short and we'll overflow, then scale down even more.
if txt_bounds.width() * scale > road_length {
scale = road_length / txt_bounds.width();
// TODO In this case, the vertical centering in the road polygon is wrong
}
// Record where the label would cover at this scale, if it was perfectly spaced out
// without curves
let center = r.length() / 2.0;
let half_width = Distance::meters(scale * txt_bounds.width() / 2.0);
self.label_covers_road
.insert(r.id, (center - half_width, center + half_width));
// The approach, part 2:
//
// But many roads are curved. We can use the SVG renderer to make text follow a curve.
// But use the scale / text size calculated assuming rectangles.
//
// Note we render the text twice here, and once again in render_curvey. This seems
// cheap enough so far. There's internal SVG caching in widgetry, but we could also
// consider caching a "road name -> txt_bounds" mapping through the whole app.
// The orientation of the text and the direction we vertically center depends on the
// direction the road points
let quadrant = r.center_pts.quadrant();
let shift_dir = if quadrant == 2 || quadrant == 3 {
-1.0
} else {
1.0
};
// The polyline passed to render_curvey will be used as the bottom of the text
// (glossing over whether or not this is a "baseline" or something else). We want to
// vertically center. SVG 1.1 has alignment-baseline, but usvg doesn't support this. So
// shift the road polyline.
let mut curve = r
.center_pts
.shift_either_direction(Distance::meters(shift_dir * road_width / 2.0))
.unwrap();
if quadrant == 2 || quadrant == 3 {
curve = curve.reversed();
}
batch.append(
Line(&name)
.fg(self.fg_color)
.render_curvey(ctx, &curve, scale),
);
}
self.draw = ctx.upload(batch);
}
}