-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathgotta-patch-em-all-font-patcher!.sh
executable file
·284 lines (238 loc) · 10.1 KB
/
gotta-patch-em-all-font-patcher!.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# Nerd Fonts Version: 2.0.0
# Script Version: 1.1.1
# used for debugging
#set -x
# for executing script to rebuild JUST the readmes:
# ./gotta-patch-em-all-font-patcher\!.sh "" info
LINE_PREFIX="# [Nerd Fonts] "
# Check for Fontforge
type fontforge >/dev/null 2>&1 || {
echo >&2 "$LINE_PREFIX FontForge must be installed before running this script."
echo >&2 "# Please see installation instructions at"
echo >&2 "# http://designwithfontforge.com/en-US/Installing_Fontforge.html"
exit 1
}
res1=$(date +%s)
parent_dir="${PWD}/../../"
# Set source and target directories
source_fonts_dir="${PWD}/../../src/unpatched-fonts"
like_pattern=''
complete_variations_per_family=4
font_typefaces_count=0
font_families_count=0
complete_variation_count=0
total_variation_count=0
total_count=0
last_parent_dir=""
unpatched_parent_dir="bin/scripts/../../src/unpatched-fonts"
patched_parent_dir="patched-fonts"
max_parallel_process=64
if [ $# -eq 1 ]
then
like_pattern=$1
echo "$LINE_PREFIX Parameter given, limiting search and patch to pattern '$like_pattern' given"
fi
# simple second param option to allow to regenerate font info without re-patching
if [ $# -eq 2 ]
then
info_only=$2
echo "$LINE_PREFIX 'Info Only' Parameter given, only generating font info (not patching)"
fi
# correct way to output find results into an array (when files have space chars, etc)
# source: https://stackoverflow.com/questions/8213328/bash-script-find-output-to-array
source_fonts=()
while IFS= read -d $'\0' -r file ; do
source_fonts=("${source_fonts[@]}" "$file")
done < <(find "$source_fonts_dir" -iname "$like_pattern*.[o,t]tf" -type f -print0)
# print total number of source fonts found
echo "$LINE_PREFIX Total source fonts found: ${#source_fonts[*]}"
function patch_font {
local f=$1; shift
local i=$1; shift
#echo $i
#echo $f
# take everything before the last slash (/) to start building the full path
local patched_font_dir="${f%/*}/"
# find replace unpatched parent dir with patched parent dir:
local patched_font_dir="${patched_font_dir/$unpatched_parent_dir/$patched_parent_dir}"
[[ -d "$patched_font_dir" ]] || mkdir -p "$patched_font_dir"
config_parent_dir=$( cd "$( dirname "$f" )" && cd ".." && pwd)
config_dir=$( cd "$( dirname "$f" )" && pwd)
# source the font config file if exists:
if [ -f "$config_dir/config.cfg" ]
then
# shellcheck source=/dev/null
source "$config_dir/config.cfg"
elif [ -f "$config_parent_dir/config.cfg" ]
then
# shellcheck source=/dev/null
source "$config_parent_dir/config.cfg"
fi
if [ -f "$config_parent_dir/config.json" ]
then
# load font configuration file and remove ligatures (for mono fonts):
font_config="--removeligatures --configfile $config_parent_dir/config.json"
else
font_config=""
fi
if [ "$post_process" ]
then
post_process="--postprocess=$post_process"
else
post_process=""
fi
# shellcheck disable=SC2154
# we know the '$config_has_powerline' is from the sourced file
if [ "$config_has_powerline" ]
then
powerline=""
combinations=$(printf "./font-patcher ${f##*/} %s\\n" {' --use-single-width-glyphs',}{' --windows',}{' --fontawesome',}{' --octicons',}{' --fontlinux',}{' --pomicons',}{' --powerlineextra',}{' --fontawesomeextension',}{' --powersymbols',}{' --weather',}{' --material',})
else
powerline="--powerline"
combinations=$(printf "./font-patcher ${f##*/} %s\\n" {' --powerline',}{' --use-single-width-glyphs',}{' --windows',}{' --fontawesome',}{' --octicons',}{' --fontlinux',}{' --pomicons',}{' --powerlineextra',}{' --fontawesomeextension',}{' --powersymbols',}{' --weather',}{' --material',})
fi
cd "$parent_dir" || {
echo >&2 "# Could not find project parent directory"
exit 1
}
fontforge -quiet -script ./font-patcher "$f" -q $powerline $post_process --complete --no-progressbars --outputdir "${patched_font_dir}complete/" 2>/dev/null
fontforge -quiet -script ./font-patcher "$f" -q -s ${font_config} $powerline $post_process --complete --no-progressbars --outputdir "${patched_font_dir}complete/" 2>/dev/null
fontforge -quiet -script ./font-patcher "$f" -q -w $powerline $post_process --complete --no-progressbars --outputdir "${patched_font_dir}complete/" 2>/dev/null
fontforge -quiet -script ./font-patcher "$f" -q -s ${font_config} -w $powerline $post_process --complete --no-progressbars --outputdir "${patched_font_dir}complete/" 2>/dev/null
# wait for this group of background processes to finish to avoid forking too many processes
# that can add up quickly with the number of combinations
#wait
}
function generate_info {
local f=$1; shift
local i=$1; shift
#echo $i
#echo $f
# take everything before the last slash (/) to start building the full path
local patched_font_dir="${f%/*}/"
# find replace unpatched parent dir with patched parent dir:
local patched_font_dir="${patched_font_dir/$unpatched_parent_dir/$patched_parent_dir}"
[[ -d "$patched_font_dir" ]] || mkdir -p "$patched_font_dir"
config_parent_dir=$( cd "$( dirname "$f" )" && cd ".." && pwd)
config_dir=$( cd "$( dirname "$f" )" && pwd)
config_parent_dir_name=$(basename "$config_parent_dir")
is_unpatched_fonts_root=0
if [ "$config_parent_dir_name" == "unpatched-fonts" ]
then
is_unpatched_fonts_root=1
font_typefaces_count=$((font_typefaces_count+1))
fi
# source the font config file if exists:
if [ -f "$config_dir/config.cfg" ]
then
# shellcheck source=/dev/null
source "$config_dir/config.cfg"
elif [ -f "$config_parent_dir/config.cfg" ]
then
# shellcheck source=/dev/null
source "$config_parent_dir/config.cfg"
fi
if [ "$config_has_powerline" ]
then
powerline=""
combinations=$(printf "./font-patcher ${f##*/} %s\\n" {' --use-single-width-glyphs',}{' --windows',}{' --fontawesome',}{' --octicons',}{' --fontlinux',}{' --pomicons',}{' --powerlineextra',}{' --fontawesomeextension',}{' --powersymbols',}{' --weather',}{' --material',})
else
powerline="--powerline"
combinations=$(printf "./font-patcher ${f##*/} %s\\n" {' --powerline',}{' --use-single-width-glyphs',}{' --windows',}{' --fontawesome',}{' --octicons',}{' --fontlinux',}{' --pomicons',}{' --powerlineextra',}{' --fontawesomeextension',}{' --powersymbols',}{' --weather',}{' --material',})
fi
font_families_count=$((font_families_count+1))
complete_variation_count=$((complete_variation_count+complete_variations_per_family))
combination_count=$(printf "%s" "$combinations" | wc -l)
# generate the readmes:
# if first time with this font then re-build parent dir readme, else skip:
if [[ $config_parent_dir != "$last_parent_dir" ]] && [ $is_unpatched_fonts_root == "0" ];
then
echo "$LINE_PREFIX Re-generate parent directory readme"
generate_readme "$patched_font_dir.." 0
fi
generate_readme "$patched_font_dir" 1
last_parent_dir=$config_parent_dir
total_variation_count=$((total_variation_count+combination_count))
total_count=$((total_count+complete_variations_per_family+combination_count))
}
# Re-generate all the readmes
# $1 = fontdir path
function generate_readme {
local patched_font_dir=$1
local generate_combinations=$2
local combinations_filename="$patched_font_dir/readme.md"
local font_info="$patched_font_dir/font-info.md"
# clear output file (needed for multiple runs or updates):
true > "$combinations_filename"
if [ -f "$font_info" ];
then
cat "$patched_font_dir/font-info.md" >> "$combinations_filename"
else
echo "$LINE_PREFIX Could not append font-info.md (file not found). Was standardize script run? It should be executed first"
echo "# looked for: $font_info"
fi
cat "$parent_dir/src/readme-per-directory-variations.md" >> "$combinations_filename"
if [ "$generate_combinations" == 1 ];
then
echo "$LINE_PREFIX Adding 'Possible Combinations' section"
# add to the file
{
printf "\`\`\`sh"
printf "\\n# %s Possible Combinations:\\n" "$combination_count"
printf "\\n"
printf "%s" "$combinations"
printf "\\n"
printf "\`\`\`"
} >> "$combinations_filename"
fi
}
if [ ! "$info_only" ]
then
# Iterate through source fonts
for i in "${!source_fonts[@]}"
do
patch_font "${source_fonts[$i]}" "$i" 2>/dev/null &
# un-comment to test this script (patch 1 font)
#break
# wait for this set of bg commands to finish: dont do too many at once!
# if we spawn a background process for each set of fonts it will
# end up using too many system resources
# however we want to run a certain number in parallel to decrease
# the amount of time patching all the fonts will take
# for now set a 'wait' for each X set of processes:
if [[ $((i % max_parallel_process)) == 0 ]];
then
echo "$LINE_PREFIX Complete Variation Count after max parallel process is $complete_variation_count"
wait
fi
done
# wait for all bg commands to finish
wait
fi
# update information in separate iteration (to avoid issues with bg processes and the counts):
# Iterate through source fonts
for i in "${!source_fonts[@]}"
do
# only output after last slash (/):
path=${source_fonts[$i]}
font_file=${path##*/}
echo "$LINE_PREFIX Generating info for '$font_file'"
generate_info "$path" "$i" 2>/dev/null
done
font_typefaces_count=$(find "${PWD}/../../${patched_parent_dir}/"* -maxdepth 0 -type d | wc -l)
res2=$(date +%s)
dt=$(echo "$res2 - $res1" | bc)
dd=$(echo "$dt/86400" | bc)
dt2=$(echo "$dt-86400*$dd" | bc)
dh=$(echo "$dt2/3600" | bc)
dt3=$(echo "$dt2-3600*$dh" | bc)
dm=$(echo "$dt3/60" | bc)
ds=$(echo "$dt3-60*$dm" | bc)
printf "$LINE_PREFIX Total runtime: %d:%02d:%02d:%02d\\n" "$dd" "$dh" "$dm" "$ds"
printf "# All fonts patched to sub-directories in \\t\\t\\t'%s'\\n" "$patched_parent_dir"
printf "# The total number of font typefaces patched was \\t\\t'%s'\\n" "$font_typefaces_count"
printf "# The total number of font families patched was \\t\\t'%s'\\n" "$font_families_count"
printf "# The total number of 'complete' patched fonts created was \\t'%s'\\n" "$complete_variation_count"
printf "# The total number of 'variation' patched fonts created was \\t'%s'\\n" "$total_variation_count"
printf "# The total number of patched fonts created was \\t\\t'%s'\\n" "$total_count"