flake-fonts/fetchfont.nix
2024-08-23 19:02:34 +02:00

105 lines
3.2 KiB
Nix

# vim: set noet sw=2 ts=2 sts=2:
{lib, pkgs, ...}:
({src ? null , srcs ? null, name ? "", pname ? "", version ? ""}:
with pkgs;
let
escapeShellArg = lib.strings.escapeShellArg;
name_ =
if "" != name
then name
else "${pname}-v${version}";
in stdenv.mkDerivation ({
name = name_;
nativeBuildInputs = [ sfnt2woff-zopfli unzip coreutils fontconfig woff2 ];
unpackPhase = ''
source_dir="$TMPDIR/src"
mkdir "$source_dir"
cd "$source_dir"
for s in $src $srcs
do
case "$s" in
*.zip|*.tar|*.tar.gz|*.tgz|*.tar.xz|*.txz|*.tar.bz2|*.tbz2)
unpackFile "$s"
;;
*)
cp -pr --reflink=auto -- "$s" "$(stripHash "$s")"
;;
esac
done
>"Makefile" echo ${escapeShellArg (builtins.readFile ./Makefile)}
>"fc.format" echo ${escapeShellArg (builtins.readFile ./fc.format)}
'';
buildPhase = ''
set -e
build_dir="$TMPDIR/build"
mkdir "$build_dir"
find "$source_dir" \( -name Makefile -or -name fc.format -or -name \*.otf -or -name \*.ttf -or -name \*.woff -or -name \*.woff2 \) -exec cp \{\} "$build_dir" \;
cd "$build_dir"
for f in *.ttf
do
[ -f "''${f%.ttf}.otf" ] || mv "$f" "''${f%.ttf}.otf"
done
make -j all
'';
# for s in *.otf *.ttf
# do
# bn="''${s%.otf}"
# bn="''${bn%.ttf}"
# if ! [ -e "$bn.woff" ]
# then
# echo "sfnt2woff-zopfli $s => $bn.woff"
# sfnt2woff-zopfli "$s"
# fi
# if ! [ -e "$bn.woff2" ]
# then
# echo "woff2_compress $s => $bn.woff2"
# woff2_compress "$s"
# fi
# done
#
# for s in *.otf *.ttf *.woff
# do
# fc="`dirname "$s"`/`basename "$s"`.fc"
# echo "fc-scan $s"
# LANG=C LC_NUMERIC=C fc-scan --format 'family=%{family}
#fullname=%{fullname}
#postscriptname=%{postscriptname}
#variable=%{variable}
#slant=%{slant}
#style=%{style}
#weight=%{weight}
#width=%{width}
#
#' "$s" > "$fc"
# done
# for s in *.woff2
# do
# o="`dirname "$s"`/`basename "$s" .woff2`.woff.fc"
# fc="`dirname "$s"`/`basename "$s"`.fc"
# cp "$o" "$fc"
# done
# '';
installPhase = ''
cd "$TMPDIR"
mkdir -p $out/share/{doc/${name_},fonts/{opentype,truetype,WOFF,WOFF2}{,/.fc}}
find "$build_dir" -name \*.otf -exec cp \{\} $out/share/fonts/opentype \;
find "$build_dir" -name \*.ttf -exec cp \{\} $out/share/fonts/truetype \;
find "$build_dir" -name \*.woff -exec cp \{\} $out/share/fonts/WOFF \;
find "$build_dir" -name \*.woff2 -exec cp \{\} $out/share/fonts/WOFF2 \;
find "$build_dir" -name \*.otf.fc -exec cp \{\} $out/share/fonts/opentype/.fc \;
find "$build_dir" -name \*.ttf.fc -exec cp \{\} $out/share/fonts/truetype/.fc \;
find "$build_dir" -name \*.woff.fc -exec cp \{\} $out/share/fonts/WOFF/.fc \;
find "$build_dir" -name \*.woff2.fc -exec cp \{\} $out/share/fonts/WOFF2/.fc \;
find "$source_dir" -name \*.txt -exec cp \{\} $out/share/doc/${name_} \;
find "$source_dir" -name \*.md -exec cp \{\} $out/share/doc/${name_} \;
find "$source_dir" -name \*.adoc -exec cp \{\} $out/share/doc/${name_} \;
find "$source_dir" -name \*.html -exec cp \{\} $out/share/doc/${name_} \;
find "$source_dir" -name \*.css -exec cp \{\} $out/share/doc/${name_} \;
'';
} // (
if null != src
then { src = src; }
else { srcs = srcs; }
))
)