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
use std::cell::RefCell;
use aabb_quadtree::QuadTree;
use lazy_static::lazy_static;
use regex::Regex;
use geom::{Distance, Pt2D};
use map_model::osm;
use widgetry::{Color, Drawable, GeomBatch, GfxCtx, Line, Text};
use crate::app::App;
pub struct DrawRoadLabels {
per_zoom: RefCell<[Option<Drawable>; 11]>,
}
impl DrawRoadLabels {
pub fn new() -> DrawRoadLabels {
DrawRoadLabels {
per_zoom: Default::default(),
}
}
pub fn draw(&self, g: &mut GfxCtx, app: &App) {
let (zoom, idx) = DrawRoadLabels::discretize_zoom(g.canvas.cam_zoom);
let value = &mut self.per_zoom.borrow_mut()[idx];
if value.is_none() {
debug!("computing DrawRoadLabels(zoom: {}, idx: {})", zoom, idx);
*value = Some(DrawRoadLabels::render(g, app, zoom));
}
g.redraw(value.as_ref().unwrap());
}
fn discretize_zoom(zoom: f64) -> (f64, usize) {
if zoom >= 1.0 {
return (1.0, 10);
}
let rounded = (zoom * 10.0).round();
let idx = rounded as usize;
(rounded / 10.0, idx)
}
fn render(g: &mut GfxCtx, app: &App, zoom: f64) -> Drawable {
let mut batch = GeomBatch::new();
let map = &app.primary.map;
let text_scale = 1.0 + 2.0 * (1.0 - zoom);
let mut quadtree = QuadTree::default(map.get_bounds().as_bbox());
'ROAD: for r in map.all_roads() {
if r.get_rank() == osm::RoadRank::Local
|| r.is_light_rail()
|| r.center_pts.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.center_pts.length() / 2.0);
fn cheaply_overestimate_bounds(
text: &str,
text_scale: f64,
center: Pt2D,
angle: geom::Angle,
) -> geom::Bounds {
let letter_width = 30.0 * text_scale;
let letter_height = 30.0 * text_scale;
geom::Polygon::rectangle_centered(
center,
Distance::meters(letter_width * text.len() as f64),
Distance::meters(letter_height),
)
.rotate(angle.reorient())
.get_bounds()
}
let big_bounds = cheaply_overestimate_bounds(&name, text_scale, pt, angle);
if !quadtree.query(big_bounds.as_bbox()).is_empty() {
continue 'ROAD;
}
quadtree.insert_with_box((), big_bounds.as_bbox());
let txt = Text::from(
Line(&name)
.big_heading_plain()
.fg(Color::WHITE)
.outlined(Color::BLACK),
);
let txt_batch = txt
.render_autocropped(g)
.scale(text_scale)
.centered_on(pt)
.rotate_around_batch_center(angle.reorient());
batch.append(txt_batch);
}
g.upload(batch)
}
}
fn simplify_name(mut x: String) -> Option<String> {
if x == "???" || x.starts_with("Exit for ") {
return None;
}
lazy_static! {
static ref SIMPLIFY_PATTERNS: Vec<(Regex, String)> = simplify_patterns();
}
for (search, replace_with) in SIMPLIFY_PATTERNS.iter() {
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"),
("North", "N"),
("South", "S"),
("East", "E"),
("West", "W"),
] {
replace.push((
Regex::new(&format!("^{}", long)).unwrap(),
short.to_string(),
));
replace.push((
Regex::new(&format!("{}$", long)).unwrap(),
short.to_string(),
));
}
for (long, short) in [
("Street", "St"),
("Boulevard", "Blvd"),
("Avenue", "Ave"),
("Place", "Pl"),
] {
replace.push((
Regex::new(&format!("{}$", long)).unwrap(),
short.to_string(),
));
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"),
] {
let got = simplify_name(input.to_string()).unwrap();
if got != want {
panic!("simplify_name({}) = {}; expected {}", input, got, want);
}
}
}
}