@@ -3,12 +3,14 @@ use std::collections::HashMap;
3
3
use std:: ffi:: OsStr ;
4
4
use std:: fmt:: Debug ;
5
5
use std:: path:: { Path , PathBuf } ;
6
+ use std:: sync:: OnceLock ;
6
7
7
8
use bit_set:: BitSet ;
8
9
use log:: { debug, info, warn} ;
9
10
use pbf_font_tools:: freetype:: { Face , Library } ;
10
11
use pbf_font_tools:: protobuf:: Message ;
11
12
use pbf_font_tools:: { render_sdf_glyph, Fontstack , Glyphs , PbfFontError } ;
13
+ use regex:: Regex ;
12
14
use serde:: { Deserialize , Serialize } ;
13
15
14
16
use crate :: fonts:: FontError :: IoError ;
@@ -79,6 +81,8 @@ fn recurse_dirs(
79
81
fonts : & mut HashMap < String , FontSource > ,
80
82
catalog : & mut HashMap < String , FontEntry > ,
81
83
) -> Result < ( ) , FontError > {
84
+ static RE_SPACES : OnceLock < Regex > = OnceLock :: new ( ) ;
85
+
82
86
for dir_entry in path
83
87
. read_dir ( )
84
88
. map_err ( |e| IoError ( e, path. to_path_buf ( ) ) ) ?
@@ -115,10 +119,11 @@ fn recurse_dirs(
115
119
name. push_str ( style) ;
116
120
}
117
121
// 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 ( ) ;
122
127
123
128
match fonts. entry ( name) {
124
129
Entry :: Occupied ( v) => {
@@ -127,9 +132,13 @@ fn recurse_dirs(
127
132
}
128
133
Entry :: Vacant ( v) => {
129
134
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 ;
133
142
} ;
134
143
135
144
let start = ranges. first ( ) . map ( |( s, _) | * s) . unwrap ( ) ;
@@ -328,7 +337,7 @@ impl FontSources {
328
337
// and https://www.freetype.org/freetype2/docs/tutorial/step1.html for details.
329
338
face. set_char_size ( 0 , CHAR_HEIGHT , 0 , 0 ) ?;
330
339
331
- for cp in ds . iter ( ) {
340
+ for cp in & ds {
332
341
let glyph = render_sdf_glyph ( & face, cp as u32 , BUFFER_SIZE , RADIUS , CUTOFF ) ?;
333
342
stack. glyphs . push ( glyph) ;
334
343
}
0 commit comments