From 734c16861fbce9b45938051036dcd2bb39581871 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 16 Jul 2023 22:18:56 -0400 Subject: [PATCH] cleaner regex gen --- Cargo.lock | 8 ++++++-- Cargo.toml | 2 +- martin-mbtiles/src/errors.rs | 2 -- martin/src/fonts/mod.rs | 25 +++++++++++++++++-------- martin/src/srv/server.rs | 2 +- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e03c0724..07754cb62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2111,7 +2111,9 @@ checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" [[package]] name = "pbf_font_tools" -version = "2.4.0" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67768bb2719d708e2de28cec7271dae35c717122c0fa4d9f8558ef5e7fa83db7" dependencies = [ "futures", "glob", @@ -2823,7 +2825,9 @@ dependencies = [ [[package]] name = "sdf_glyph_renderer" -version = "0.6.0" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b05c114d181e20b509e03b05856cc5823bc6189d581c276fe37c5ebc5e3b3b9" dependencies = [ "freetype-rs", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index b53cfa48e..8e2b2f8cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ log = "0.4" martin-mbtiles = { path = "./martin-mbtiles", version = "0.6.0", default-features = false } martin-tile-utils = { path = "./martin-tile-utils", version = "0.1.0" } num_cpus = "1" -pbf_font_tools = { version = "2.4.0", features = ["freetype"], path = "../sdf_font_tools/pbf_font_tools" } +pbf_font_tools = { version = "2.5.0", features = ["freetype"] } pmtiles = { version = "0.3", features = ["mmap-async-tokio", "tilejson"] } postgis = "0.9" postgres = { version = "0.19", features = ["with-time-0_3", "with-uuid-1", "with-serde_json-1"] } diff --git a/martin-mbtiles/src/errors.rs b/martin-mbtiles/src/errors.rs index be9336df0..cb732b66e 100644 --- a/martin-mbtiles/src/errors.rs +++ b/martin-mbtiles/src/errors.rs @@ -5,8 +5,6 @@ use sqlite_hashes::rusqlite; use crate::MbtType; -use crate::mbtiles::MbtType; - #[derive(thiserror::Error, Debug)] pub enum MbtError { #[error("The source and destination MBTiles files are the same: {}", .0.display())] diff --git a/martin/src/fonts/mod.rs b/martin/src/fonts/mod.rs index dc079112b..a7019ad64 100644 --- a/martin/src/fonts/mod.rs +++ b/martin/src/fonts/mod.rs @@ -3,12 +3,14 @@ use std::collections::HashMap; use std::ffi::OsStr; use std::fmt::Debug; use std::path::{Path, PathBuf}; +use std::sync::OnceLock; use bit_set::BitSet; use log::{debug, info, warn}; use pbf_font_tools::freetype::{Face, Library}; use pbf_font_tools::protobuf::Message; use pbf_font_tools::{render_sdf_glyph, Fontstack, Glyphs, PbfFontError}; +use regex::Regex; use serde::{Deserialize, Serialize}; use crate::fonts::FontError::IoError; @@ -79,6 +81,8 @@ fn recurse_dirs( fonts: &mut HashMap, catalog: &mut HashMap, ) -> Result<(), FontError> { + static RE_SPACES: OnceLock = OnceLock::new(); + for dir_entry in path .read_dir() .map_err(|e| IoError(e, path.to_path_buf()))? @@ -115,10 +119,11 @@ fn recurse_dirs( name.push_str(style); } // Make sure font name has no slashes or commas, replacing them with spaces and de-duplicating spaces - name = name - .replace(['/', ','], " ") - .replace(" ", " ") - .replace(" ", " "); + name = name.replace(['/', ','], " "); + name = RE_SPACES + .get_or_init(|| Regex::new(r"\s+").unwrap()) + .replace_all(name.as_str(), " ") + .to_string(); match fonts.entry(name) { Entry::Occupied(v) => { @@ -127,9 +132,13 @@ fn recurse_dirs( } Entry::Vacant(v) => { let key = v.key(); - let Some((codepoints, count, ranges )) = get_available_codepoints(&mut face) else { - warn!("Ignoring font source {key} from {} because it has no available glyphs", path.display()); - continue + let Some((codepoints, count, ranges)) = get_available_codepoints(&mut face) + else { + warn!( + "Ignoring font source {key} from {} because it has no available glyphs", + path.display() + ); + continue; }; let start = ranges.first().map(|(s, _)| *s).unwrap(); @@ -328,7 +337,7 @@ impl FontSources { // and https://www.freetype.org/freetype2/docs/tutorial/step1.html for details. face.set_char_size(0, CHAR_HEIGHT, 0, 0)?; - for cp in ds.iter() { + for cp in &ds { let glyph = render_sdf_glyph(&face, cp as u32, BUFFER_SIZE, RADIUS, CUTOFF)?; stack.glyphs.push(glyph); } diff --git a/martin/src/srv/server.rs b/martin/src/srv/server.rs index c8cf41236..82a2cdabe 100755 --- a/martin/src/srv/server.rs +++ b/martin/src/srv/server.rs @@ -74,7 +74,7 @@ pub fn map_font_error(e: FontError) -> actix_web::Error { #[allow(clippy::enum_glob_use)] use FontError::*; match e { - FontNotFound(_) => error::ErrorNotFound(e.to_string()), + FontNotFound(_) => ErrorNotFound(e.to_string()), InvalidFontRangeStartEnd(_, _) | InvalidFontRangeStart(_) | InvalidFontRangeEnd(_)