2018-07-09 16:20:40 +02:00
|
|
|
#!/usr/bin/env bash
|
2022-10-07 13:55:35 +02:00
|
|
|
# Nerd Fonts Version: 2.3.0-RC
|
2022-11-27 12:25:10 +01:00
|
|
|
# Script Version: 2.0.0
|
|
|
|
# Iterates over all [*] patched fonts directories
|
2016-11-06 04:09:12 +01:00
|
|
|
# to generate ruby cask files for homebrew-fonts (https://github.com/caskroom/homebrew-fonts)
|
2022-11-27 12:25:10 +01:00
|
|
|
# * Only adds non-Windows versions of the fonts
|
|
|
|
# * Needs the zip archives in archives/ (i.e. run `./archive-fonts.sh` first)
|
|
|
|
#
|
|
|
|
# [1] Accepts one parameter, a pattern which fonts to examine, if not given defaults
|
|
|
|
# to "*" which is all fonts. Example `./generate-casks.sh 'Hasklig'`
|
2016-11-06 04:09:12 +01:00
|
|
|
|
2016-11-19 02:28:47 +01:00
|
|
|
#set -x
|
2022-11-25 14:12:47 +01:00
|
|
|
set -e
|
2016-11-06 04:09:12 +01:00
|
|
|
|
2022-10-07 13:55:35 +02:00
|
|
|
version="2.3.0-RC"
|
2016-11-06 04:09:12 +01:00
|
|
|
homepage="https://github.com/ryanoasis/nerd-fonts"
|
2016-11-13 18:22:10 +01:00
|
|
|
downloadarchive="https://github.com/ryanoasis/nerd-fonts/releases/download/v#{version}/"
|
2017-05-14 00:02:53 +02:00
|
|
|
LINE_PREFIX="# [Nerd Fonts] "
|
2022-11-25 16:47:12 +01:00
|
|
|
scripts_root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/"
|
|
|
|
patched_parent_dir="${scripts_root_dir}/../../patched-fonts/"
|
2016-11-06 04:09:12 +01:00
|
|
|
|
2016-11-19 02:28:47 +01:00
|
|
|
cd $patched_parent_dir || {
|
2022-11-25 12:52:23 +01:00
|
|
|
echo >&2 "$LINE_PREFIX Could not find patched fonts directory"
|
|
|
|
exit 1
|
2016-11-06 04:09:12 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:56:52 +01:00
|
|
|
function clear_file {
|
2022-11-25 12:52:23 +01:00
|
|
|
local outputfile=$1
|
|
|
|
# clear output file (needed for multiple runs or updates):
|
|
|
|
true > "$outputfile" 2> /dev/null
|
2016-12-03 04:56:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function write_header {
|
2022-11-25 12:52:23 +01:00
|
|
|
local outputfile=$1
|
|
|
|
local caskname=$2
|
|
|
|
|
|
|
|
{
|
|
|
|
printf "cask \"%s\" do\\n" "$caskname"
|
|
|
|
printf " version \"%s\"\\n" "$version"
|
|
|
|
printf " sha256 \"%s\"\\n\\n" "$sha256sum"
|
|
|
|
printf " url \"%s%s.zip\"\\n" "$downloadarchive" "$basename"
|
|
|
|
} >> "$outputfile"
|
2016-12-03 04:56:52 +01:00
|
|
|
}
|
2016-11-06 04:09:12 +01:00
|
|
|
|
2022-11-27 12:07:29 +01:00
|
|
|
# Query all Family names of a font individually and return the first
|
|
|
|
# we found that has "Nerd" in it. We need this because some fonts have
|
|
|
|
# broken Family names.
|
|
|
|
function find_nerdish_family {
|
|
|
|
local fontfile=$1
|
|
|
|
local idx=0
|
|
|
|
while :; do
|
|
|
|
local fn=$(fc-query --format="%{family[${idx}]}" "${fontfile}")
|
|
|
|
if [ -z "$fn" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [[ "${fn}" == *Nerd* ]]; then
|
|
|
|
echo "${fn}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
idx=$((${idx} + 1))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-11-29 11:50:19 +01:00
|
|
|
# Return the longest common starting part of two strings
|
|
|
|
# This is the stem, the basic base name of the fonts
|
|
|
|
function find_common_stem {
|
|
|
|
local first=$1
|
|
|
|
local second=$2
|
|
|
|
for i in $(seq ${#first} -1 1); do
|
|
|
|
if [ "${first:0:$i}" == "${second:0:$i}" ]; then
|
|
|
|
echo "${first:0:$i}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:56:52 +01:00
|
|
|
function write_body {
|
2022-11-25 16:47:12 +01:00
|
|
|
local unpatchedname=$1
|
|
|
|
local outputfile=$2
|
|
|
|
shift; shift;
|
2022-11-25 12:52:23 +01:00
|
|
|
local fonts=("$@")
|
|
|
|
|
|
|
|
if [ "${fonts[0]}" ]; then
|
2022-11-25 15:02:21 +01:00
|
|
|
longest=-1
|
|
|
|
# Find longest filename for pretty printing
|
|
|
|
for i in "${!fonts[@]}"; do
|
|
|
|
basename=$(basename "${fonts[$i]}")
|
|
|
|
if [ ${#basename} -gt $longest ]; then
|
|
|
|
longest=${#basename}
|
|
|
|
fi
|
|
|
|
done
|
2022-11-27 12:07:29 +01:00
|
|
|
# Find familyname of non Mono variant (well, rather shortest because we can contain multiple families)
|
|
|
|
familyname=$(find_nerdish_family "${fonts[0]}")
|
2022-11-27 10:22:09 +01:00
|
|
|
for i in "${!fonts[@]}"; do
|
2022-11-27 12:07:29 +01:00
|
|
|
fn=$(find_nerdish_family "${fonts[$i]}")
|
2022-11-29 11:50:19 +01:00
|
|
|
familyname=$(find_common_stem "${fn}" "${familyname}")
|
2022-11-27 10:22:09 +01:00
|
|
|
done
|
2022-11-27 12:07:29 +01:00
|
|
|
if [ -z "${familyname}" ]; then
|
|
|
|
echo >&2 "${LINE_PREFIX} Can not determine family name"
|
|
|
|
exit 2
|
|
|
|
fi
|
2022-11-29 11:50:19 +01:00
|
|
|
# Family names differ in front of "Nerd Font" (stem is short)
|
|
|
|
if [[ "${familyname}" != *Nerd* ]]; then
|
|
|
|
familyname="${familyname} Nerd Font families"
|
|
|
|
fi
|
|
|
|
familyname="$(tr [:lower:] [:upper:] <<< ${familyname:0:1})${familyname:1}"
|
2022-11-25 15:02:21 +01:00
|
|
|
# Process font files
|
2022-11-25 12:52:23 +01:00
|
|
|
for i in "${!fonts[@]}"; do
|
|
|
|
individualfont=$(basename "${fonts[$i]}")
|
2022-11-25 15:02:21 +01:00
|
|
|
individualdir=$(dirname "${fonts[$i]}")
|
2022-11-25 12:52:23 +01:00
|
|
|
|
2022-11-25 15:02:21 +01:00
|
|
|
printf " %-${longest}s %s\\n" "${individualfont}" "${individualdir}"
|
2022-11-25 12:52:23 +01:00
|
|
|
|
|
|
|
if [ "$i" == 0 ]; then
|
|
|
|
{
|
2022-11-25 16:47:12 +01:00
|
|
|
printf " name \"%s (%s)\"\\n" "$familyname" "$unpatchedname"
|
2022-11-25 14:12:47 +01:00
|
|
|
printf " desc \"Developer targeted fonts with a high number of glyphs\"\\n"
|
2022-11-25 12:52:23 +01:00
|
|
|
printf " homepage \"%s\"" "$homepage"
|
|
|
|
printf "\\n\\n"
|
2022-11-25 14:12:47 +01:00
|
|
|
printf " livecheck do\\n"
|
|
|
|
printf " url :url\\n"
|
|
|
|
printf " strategy :github_latest\\n"
|
|
|
|
printf " end\\n\\n"
|
2022-11-25 12:52:23 +01:00
|
|
|
} >> "$outputfile"
|
|
|
|
fi
|
|
|
|
|
|
|
|
printf " font \"%s\"\\n" "$individualfont" >> "$outputfile"
|
|
|
|
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo "$LINE_PREFIX Did not find TTF or OTF"
|
|
|
|
fi
|
2016-12-03 04:56:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function write_footer {
|
2022-11-25 12:52:23 +01:00
|
|
|
local outputfile=$1
|
2016-11-06 04:09:12 +01:00
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
{
|
|
|
|
printf "end\\n"
|
|
|
|
} >> "$outputfile"
|
2016-12-03 04:56:52 +01:00
|
|
|
}
|
|
|
|
|
2022-08-25 08:39:08 +02:00
|
|
|
pattern=$1
|
|
|
|
if [ "$pattern" = "" ]; then
|
2022-11-25 12:52:23 +01:00
|
|
|
pattern=".*"
|
2022-08-25 08:39:08 +02:00
|
|
|
fi
|
|
|
|
|
2022-11-25 14:12:47 +01:00
|
|
|
find . -maxdepth 1 -mindepth 1 -type d -iregex "\./$pattern" | sort |
|
2022-11-25 12:52:23 +01:00
|
|
|
while read -r filename; do
|
2016-12-03 04:56:52 +01:00
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
dirname=$(dirname "$filename")
|
|
|
|
basename=$(basename "$filename")
|
2022-11-25 14:12:47 +01:00
|
|
|
if [ ! -f "../archives/${basename}.zip" ]; then
|
|
|
|
echo "${LINE_PREFIX} No archive for: ${basename}, skipping..."
|
|
|
|
continue
|
|
|
|
fi
|
2022-11-25 12:52:23 +01:00
|
|
|
sha256sum=$(sha256sum "../archives/${basename}.zip" | head -c 64)
|
|
|
|
searchdir=$filename
|
2016-12-03 04:56:52 +01:00
|
|
|
|
2022-11-29 12:12:50 +01:00
|
|
|
originalname=$(cat "${scripts_root_dir}/lib/fonts.json" | jq -r ".fonts[] | select(.folderName == "\"${basename}\"") | .unpatchedName" "${scripts_root_dir}/lib/fonts.json" | head -n 1)
|
|
|
|
caskbasename=$(cat "${scripts_root_dir}/lib/fonts.json" | jq -r ".fonts[] | select(.folderName == "\"${basename}\"") | .caskName" "${scripts_root_dir}/lib/fonts.json" | head -n 1)
|
2022-11-25 16:47:12 +01:00
|
|
|
if [ -z "$originalname" ]; then
|
|
|
|
echo "${LINE_PREFIX} Can not find ${basename} in fonts.json, skipping..."
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
FONTS=()
|
|
|
|
while IFS= read -d $'\0' -r file; do
|
|
|
|
FONTS=("${FONTS[@]}" "$file")
|
2022-11-29 15:40:00 +01:00
|
|
|
done < <(find "$searchdir" -type f -iwholename '*complete*' \( -iname '*.[o,t]tf' ! -wholename '*Windows Compatible*' \) -print0 | sort -z)
|
2016-12-03 04:56:52 +01:00
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
outputdir=$PWD/../casks
|
2016-12-03 04:56:52 +01:00
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
echo "$LINE_PREFIX Generating cask for: $basename"
|
2016-12-03 04:56:52 +01:00
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
[[ -d "$outputdir" ]] || mkdir -p "$outputdir"
|
2016-12-03 04:56:52 +01:00
|
|
|
|
casks: Hardcode cask names in fonts.json
[why]
We want to replace the existing casks. But they do not have a consistent
way to name the casks. Sometimes blanks are preserved (as dashes):
"unpatchedName": "DejaVu Sans Mono",
"patchedName": "DejaVuSansMono",
"caskName": "dejavu-sans-mono",
The above looks like cask-name == unpatched-name.
Here they introduce blanks out of thin air (the original name does not
have blanks):
"unpatchedName": "DaddyTimeMono",
"patchedName": "DaddyTimeMono",
"caskName": "daddy-time-mono",
Here they add something to the name??!:
"unpatchedName": "Gohu",
"patchedName": "Gohu",
"caskName": "gohufont",
Here the cask-name follows the RFN renaming:
"unpatchedName": "Hasklig",
"patchedName": "Hasklug",
"caskName": "hasklug",
Here they do not follow the RFN renaming:
"unpatchedName": "Liberation",
"patchedName": "LiterationMono",
"caskName": "liberation",
And there are a lot more ... strangenesses.
I guess the casks have been created by different people following
different ideas.
[how]
All these inconsistencies makes setting up rules how to determine the
cask name from the other names very complicated and brittle.
If we want to preserve the existing cask names the simplest and most
stable approach is to explicitely specify them in the fonts.json
database:
Introduce a new data field "caskName", like shown above.
Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
2022-11-26 18:00:17 +01:00
|
|
|
caskname="font-${caskbasename}-nerd-font"
|
2022-11-25 12:52:23 +01:00
|
|
|
to="$outputdir/${caskname}.rb"
|
2022-11-29 12:12:50 +01:00
|
|
|
echo "@@@$caskname@@@"
|
2016-12-03 04:56:52 +01:00
|
|
|
|
2022-11-25 12:52:23 +01:00
|
|
|
clear_file "$to"
|
|
|
|
write_header "$to" "$caskname"
|
2022-11-25 16:47:12 +01:00
|
|
|
write_body "$originalname" "$to" "${FONTS[@]}"
|
2022-11-25 12:52:23 +01:00
|
|
|
write_footer "$to"
|
2022-08-30 10:18:42 +02:00
|
|
|
|
2022-11-26 17:16:19 +01:00
|
|
|
echo "## Created casks: $(realpath ${to})"
|
2016-11-06 04:09:12 +01:00
|
|
|
done
|