Skip to content

Commit

Permalink
cleaner regex gen
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Oct 17, 2023
1 parent 3af3070 commit 734c168
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 0 additions & 2 deletions martin-mbtiles/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())]
Expand Down
25 changes: 17 additions & 8 deletions martin/src/fonts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +81,8 @@ fn recurse_dirs(
fonts: &mut HashMap<String, FontSource>,
catalog: &mut HashMap<String, FontEntry>,
) -> Result<(), FontError> {
static RE_SPACES: OnceLock<Regex> = OnceLock::new();

for dir_entry in path
.read_dir()
.map_err(|e| IoError(e, path.to_path_buf()))?
Expand Down Expand Up @@ -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) => {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion martin/src/srv/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down

0 comments on commit 734c168

Please sign in to comment.