Skip to content

Commit

Permalink
feat: fix font size with custom script
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Sep 3, 2024
1 parent 624b609 commit ba96128
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
73 changes: 55 additions & 18 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
system:
let
pkgs = import inputs.nixpkgs { inherit system; };
inherit (pkgs) lib;

mkWOFF2From =
name: pkg: ext:
Expand All @@ -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 = [
Expand All @@ -44,11 +40,9 @@
(pkgs.writeShellApplication {
name = "buildapp";
text = ''
function buildapp() {
${self.defaultPackage.${system}.preBuild}
go build
}
buildapp && ./app
${self.defaultPackage.${system}.preBuild}
go build
./app
'';
})
];
Expand All @@ -63,9 +57,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};
Expand All @@ -83,12 +106,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 {
Expand Down
2 changes: 1 addition & 1 deletion static/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 10 additions & 3 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit ba96128

Please sign in to comment.