diff --git a/martin/src/fonts/mod.rs b/martin/src/fonts/mod.rs index dc079112b..9165d304c 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) => {