From 39bbb15a5c144a48581b9075ff8a60ea61380ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 3 Sep 2024 10:27:33 +0200 Subject: [PATCH] feat: fix font size with custom script --- flake.nix | 65 ++++++++++++++++++++++++++++++++++++++---------- static/embed.go | 2 +- static/style.css | 13 +++++++--- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/flake.nix b/flake.nix index 6902b9d..a2b628e 100644 --- a/flake.nix +++ b/flake.nix @@ -10,6 +10,7 @@ system: let pkgs = import inputs.nixpkgs { inherit system; }; + inherit (pkgs) lib; mkWOFF2From = name: pkg: ext: @@ -25,17 +26,12 @@ mkdir -p "$WOFF2_DIR" for file in ${pkg}/share/fonts/truetype/*.${ext}; do NAME="$(basename $file .${ext})" - fontforge --lang=ff \ - -c 'Open($1); Generate($2);' \ - "$file" \ - "$WOFF2_DIR/$NAME.woff2" & + ${lib.getExe reduceFont} "$file" "$WOFF2_DIR/$NAME.woff2" & done wait ''; }; - iosevka-comfy-woff2 = mkWOFF2From "iosevka-comfy-fixed" pkgs.iosevka-comfy.comfy-fixed "ttf"; - rundev = pkgs.writeShellApplication { name = "rundev"; runtimeInputs = [ @@ -63,9 +59,38 @@ ''; }; + reduceFont = + pkgs.writers.writePython3Bin "reduce-font" { libraries = with pkgs.python3.pkgs; [ fontforge ]; } + '' + import fontforge + import sys + + if len(sys.argv) != 3: + print("Usage: python input.ttf output.woff2") + sys.exit(1) + + input_font = sys.argv[1] + output_font = sys.argv[2] + + font = fontforge.open(input_font) + + ascii_glyphs = range(0, 256) + for glyph in font.glyphs(): + if glyph.unicode not in ascii_glyphs: + font.removeGlyph(glyph) + else: + glyph.simplify() + + font.autoHint() + font.removeOverlap() + + font.generate(output_font, flags=("tfm")) + + font.close() + ''; in rec { - formatter = pkgs.alejandra; + formatter = pkgs.nixfmt-rfc-style; devShell = pkgs.mkShell { inputsFrom = builtins.attrValues self.packages.${system}; @@ -83,12 +108,26 @@ tailwindcss makeWrapper ]; - preBuild = '' - install -D ${iosevka-comfy-woff2}/share/fonts/woff2/iosevka-comfy-fixed-regular.woff2 static/ - install -D ${iosevka-comfy-woff2}/share/fonts/woff2/iosevka-comfy-fixed-bold.woff2 static/ - templ generate - tailwindcss -i static/style.css -o static/generated.css - ''; + preBuild = + let + targets = [ + "regular" + "bold" + "extrabold" + ]; + + font = mkWOFF2From "iosevka-comfy-fixed" pkgs.iosevka-comfy.comfy-fixed "ttf"; + inherit (builtins) length genList elemAt; + in + '' + ${builtins.concatStringsSep "\n" ( + genList ( + i: "install -D ${font}/share/fonts/woff2/iosevka-comfy-fixed-${elemAt targets i}.woff2 static/" + ) (length targets) + )} + templ generate + tailwindcss -i static/style.css -o static/generated.css + ''; }; docker = pkgs.dockerTools.buildImage { diff --git a/static/embed.go b/static/embed.go index 0914073..5481a1b 100644 --- a/static/embed.go +++ b/static/embed.go @@ -3,5 +3,5 @@ package static import "embed" // content holds our static articles -//go:embed robots.txt generated.css iosevka-comfy-fixed-bold.woff2 iosevka-comfy-fixed-regular.woff2 +//go:embed robots.txt generated.css *.woff2 var Files embed.FS diff --git a/static/style.css b/static/style.css index 28070f9..5dd65c5 100644 --- a/static/style.css +++ b/static/style.css @@ -16,6 +16,13 @@ src: url("/iosevka-comfy-fixed-bold.woff2") format("woff2"); } +@font-face { + font-family: "Iosevka Comfy"; + font-style: normal; + font-weight: 800; + src: url("/iosevka-comfy-fixed-extrabold.woff2") format("woff2"); +} + html { @apply text-lg } @@ -25,15 +32,15 @@ a { } h1 { - @apply text-3xl font-bold + @apply text-3xl font-extrabold } h2 { - @apply text-2xl + @apply text-2xl font-bold } h3, h4, h5, h6 { - @apply text-xl + @apply text-xl font-bold } ul {