Skip to content

Commit 734c168

Browse files
committed
cleaner regex gen
1 parent 3af3070 commit 734c168

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

Cargo.lock

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ log = "0.4"
3737
martin-mbtiles = { path = "./martin-mbtiles", version = "0.6.0", default-features = false }
3838
martin-tile-utils = { path = "./martin-tile-utils", version = "0.1.0" }
3939
num_cpus = "1"
40-
pbf_font_tools = { version = "2.4.0", features = ["freetype"], path = "../sdf_font_tools/pbf_font_tools" }
40+
pbf_font_tools = { version = "2.5.0", features = ["freetype"] }
4141
pmtiles = { version = "0.3", features = ["mmap-async-tokio", "tilejson"] }
4242
postgis = "0.9"
4343
postgres = { version = "0.19", features = ["with-time-0_3", "with-uuid-1", "with-serde_json-1"] }

martin-mbtiles/src/errors.rs

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use sqlite_hashes::rusqlite;
55

66
use crate::MbtType;
77

8-
use crate::mbtiles::MbtType;
9-
108
#[derive(thiserror::Error, Debug)]
119
pub enum MbtError {
1210
#[error("The source and destination MBTiles files are the same: {}", .0.display())]

martin/src/fonts/mod.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use std::collections::HashMap;
33
use std::ffi::OsStr;
44
use std::fmt::Debug;
55
use std::path::{Path, PathBuf};
6+
use std::sync::OnceLock;
67

78
use bit_set::BitSet;
89
use log::{debug, info, warn};
910
use pbf_font_tools::freetype::{Face, Library};
1011
use pbf_font_tools::protobuf::Message;
1112
use pbf_font_tools::{render_sdf_glyph, Fontstack, Glyphs, PbfFontError};
13+
use regex::Regex;
1214
use serde::{Deserialize, Serialize};
1315

1416
use crate::fonts::FontError::IoError;
@@ -79,6 +81,8 @@ fn recurse_dirs(
7981
fonts: &mut HashMap<String, FontSource>,
8082
catalog: &mut HashMap<String, FontEntry>,
8183
) -> Result<(), FontError> {
84+
static RE_SPACES: OnceLock<Regex> = OnceLock::new();
85+
8286
for dir_entry in path
8387
.read_dir()
8488
.map_err(|e| IoError(e, path.to_path_buf()))?
@@ -115,10 +119,11 @@ fn recurse_dirs(
115119
name.push_str(style);
116120
}
117121
// Make sure font name has no slashes or commas, replacing them with spaces and de-duplicating spaces
118-
name = name
119-
.replace(['/', ','], " ")
120-
.replace(" ", " ")
121-
.replace(" ", " ");
122+
name = name.replace(['/', ','], " ");
123+
name = RE_SPACES
124+
.get_or_init(|| Regex::new(r"\s+").unwrap())
125+
.replace_all(name.as_str(), " ")
126+
.to_string();
122127

123128
match fonts.entry(name) {
124129
Entry::Occupied(v) => {
@@ -127,9 +132,13 @@ fn recurse_dirs(
127132
}
128133
Entry::Vacant(v) => {
129134
let key = v.key();
130-
let Some((codepoints, count, ranges )) = get_available_codepoints(&mut face) else {
131-
warn!("Ignoring font source {key} from {} because it has no available glyphs", path.display());
132-
continue
135+
let Some((codepoints, count, ranges)) = get_available_codepoints(&mut face)
136+
else {
137+
warn!(
138+
"Ignoring font source {key} from {} because it has no available glyphs",
139+
path.display()
140+
);
141+
continue;
133142
};
134143

135144
let start = ranges.first().map(|(s, _)| *s).unwrap();
@@ -328,7 +337,7 @@ impl FontSources {
328337
// and https://www.freetype.org/freetype2/docs/tutorial/step1.html for details.
329338
face.set_char_size(0, CHAR_HEIGHT, 0, 0)?;
330339

331-
for cp in ds.iter() {
340+
for cp in &ds {
332341
let glyph = render_sdf_glyph(&face, cp as u32, BUFFER_SIZE, RADIUS, CUTOFF)?;
333342
stack.glyphs.push(glyph);
334343
}

martin/src/srv/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn map_font_error(e: FontError) -> actix_web::Error {
7474
#[allow(clippy::enum_glob_use)]
7575
use FontError::*;
7676
match e {
77-
FontNotFound(_) => error::ErrorNotFound(e.to_string()),
77+
FontNotFound(_) => ErrorNotFound(e.to_string()),
7878
InvalidFontRangeStartEnd(_, _)
7979
| InvalidFontRangeStart(_)
8080
| InvalidFontRangeEnd(_)

0 commit comments

Comments
 (0)