summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: bc7a9c3ab6f4b980ab15d8861ee8be5af8b68a50 (plain)
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
# Maintainer: gaou-piou <i.am.piou@gmail.com>
# Contributor: GCMarvin <GCMarvin@users.noreply.github.com>

pkgname=ttf-google-sans-hinted
_nfver=3.4.0
pkgver=1
pkgrel=1
pkgdesc="Google Sans fonts (GoogleSans, GoogleSansDisplay), optionally Nerd Fonts patched and/or ttfautohint hinted"
url='https://flutter.googlesource.com/gallery-assets'
arch=('any')
license=('custom')
makedepends=('python' 'python-fonttools' 'parallel' 'fontforge' 'ttfautohint')
options=(!strip !zipman)
conflicts=('ttf-google-sans')
provides=('ttf-google-sans' 'ttf-font-nerd')

source=(
  'fonts.tar.gz::https://flutter.googlesource.com/gallery-assets/+archive/refs/heads/master/lib/fonts.tar.gz'
  "font-patcher-${_nfver}::https://github.com/ryanoasis/nerd-fonts/releases/download/v${_nfver}/FontPatcher.zip"
)
sha256sums=('SKIP' 'SKIP')

# ── Build-time options (all settable via env vars) ──────────────────────────
#
#  NERD_PATCH  true/false   Patch fonts with Nerd Fonts glyphs (default: false)
#
#  HINTING     true/false   Apply ttfautohint (default: false)
#
#  HINT_MODE   nnn|qqq|qsq|sss   3-char stem-width mode string for ttfautohint:
#                pos1=grayscale  pos2=GDI ClearType  pos3=DW ClearType
#                n=natural  q=quantized  s=strong
#                nnn=HiDPI  qqq=balanced  sss=aggressive low-DPI
#
#  Font family selection:
#  WANT_GOOGLE_SANS          true/false  (default: true)
#  WANT_GOOGLE_SANS_DISPLAY  true/false  (default: true)
#
#  Non-interactive examples:
#    NERD_PATCH=true HINTING=true HINT_MODE=qqq makepkg -si
#    NERD_PATCH=false HINTING=false WANT_GOOGLE_SANS_DISPLAY=false makepkg -si
# ────────────────────────────────────────────────────────────────────────────

_validate_hint_mode() {
  if [[ ! "$HINT_MODE" =~ ^[nqs]{3}$ ]]; then
    echo "Error: HINT_MODE='$HINT_MODE' is invalid."
    echo "  Must be 3 chars from: n (natural)  q (quantized)  s (strong)"
    echo "  Examples: nnn  qqq  qsq  sss"
    exit 1
  fi
}

_show()   { printf "  [%s] %s\n" "$1" "$2"; }
_ask_yn() { local _default="${2:-n}"; read -p "  $1? (default: $_default) " _yn; _yn="${_yn:-$_default}"; }

build() {
  # ══════════════════════════════════════════════════════════════════════════
  # Prompt section — all questions, only when env vars are not pre-set
  # ══════════════════════════════════════════════════════════════════════════
  echo ""
  echo "╔══════════════════════════════════════════════════════╗"
  echo "║         ttf-google-sans-hinted — build options       ║"
  echo "╚══════════════════════════════════════════════════════╝"

  # ── Nerd Fonts patching ──────────────────────────────────────────────────
  if [[ -z "${NERD_PATCH:-}" ]]; then
    _ask_yn "Patch fonts with Nerd Fonts glyphs" n
    [[ "$_yn" =~ ^[yY]$ ]] && NERD_PATCH=true || NERD_PATCH=false
  fi
  NERD_PATCH="${NERD_PATCH:-false}"

  # ── Hinting ──────────────────────────────────────────────────────────────
  if [[ -z "${HINTING:-}" ]]; then
    echo ""
    _ask_yn "Apply ttfautohint hinting" n
    [[ "$_yn" =~ ^[yY]$ ]] && HINTING=true || HINTING=false
  fi
  HINTING="${HINTING:-false}"

  if [[ "$HINTING" == true && -z "${HINT_MODE:-}" ]]; then
    echo ""
    echo "  Hinting stem-width mode:"
    select _choice in "Balanced (qqq)" "HiDPI (nnn)" "Aggressive (sss)" "Custom"; do
      case "$_choice" in
        "Balanced (qqq)")   HINT_MODE=qqq; break ;;
        "HiDPI (nnn)")      HINT_MODE=nnn; break ;;
        "Aggressive (sss)") HINT_MODE=sss; break ;;
        "Custom") read -p "  Enter 3-char mode (e.g. qsq): " HINT_MODE; break ;;
      esac
    done
  fi
  HINT_MODE="${HINT_MODE:-qqq}"
  [[ "$HINTING" == true ]] && _validate_hint_mode

  # ── Font family selection ────────────────────────────────────────────────
  if [[ -z "${WANT_GOOGLE_SANS:-}${WANT_GOOGLE_SANS_DISPLAY:-}" ]]; then
    echo ""
    echo "  ── Font families ────────────────────────────────────────"
    _ask_yn "Include Google Sans (Regular, Medium, Bold + Italics)" y
    [[ "$_yn" =~ ^[yY]$ ]] && WANT_GOOGLE_SANS=true || WANT_GOOGLE_SANS=false

    _ask_yn "Include Google Sans Display (Regular, Medium, Bold + Italics)" y
    [[ "$_yn" =~ ^[yY]$ ]] && WANT_GOOGLE_SANS_DISPLAY=true || WANT_GOOGLE_SANS_DISPLAY=false
  fi

  WANT_GOOGLE_SANS="${WANT_GOOGLE_SANS:-true}"
  WANT_GOOGLE_SANS_DISPLAY="${WANT_GOOGLE_SANS_DISPLAY:-true}"

  if [[ "$WANT_GOOGLE_SANS$WANT_GOOGLE_SANS_DISPLAY" != *true* ]]; then
    echo "Error: no font families selected. Aborting."; exit 1
  fi

  # ── Build summary ─────────────────────────────────────────────────────────
  echo ""
  echo "  ── Build summary ───────────────────────────────────────"
  printf "  NERD_PATCH:  %s\n" "$NERD_PATCH"
  printf "  HINTING:     %s%s\n" "$HINTING" "$( [[ "$HINTING" == true ]] && echo "  (mode=$HINT_MODE)" )"
  echo ""
  _show "$( [[ "$WANT_GOOGLE_SANS"         == true ]] && echo "x" || echo " " )" "Google Sans"
  _show "$( [[ "$WANT_GOOGLE_SANS_DISPLAY" == true ]] && echo "x" || echo " " )" "Google Sans Display"
  echo "  ────────────────────────────────────────────────────────"
  echo ""

  # ══════════════════════════════════════════════════════════════════════════
  # Step 1 — Select font files from the downloaded archive
  # ══════════════════════════════════════════════════════════════════════════
  rm -rf "$srcdir/selected" "$srcdir/patched"
  mkdir -p "$srcdir/selected" "$srcdir/patched"

  if [[ "$WANT_GOOGLE_SANS" == true ]]; then
    find "$srcdir" -maxdepth 1 -name 'GoogleSans-*.ttf' ! -path '*/._*' \
      -exec cp -n {} "$srcdir/selected/" \;
  fi

  if [[ "$WANT_GOOGLE_SANS_DISPLAY" == true ]]; then
    find "$srcdir" -maxdepth 1 -name 'GoogleSansDisplay-*.ttf' ! -path '*/._*' \
      -exec cp -n {} "$srcdir/selected/" \;
  fi

  local _sel_count; _sel_count=$(ls "$srcdir/selected/" | wc -l)
  echo "==> Selected $_sel_count font files for processing."
  if (( _sel_count == 0 )); then
    echo "Error: no fonts matched the selection."; exit 1
  fi

  # ══════════════════════════════════════════════════════════════════════════
  # Step 2 — Nerd Fonts patching (skipped when NERD_PATCH=false)
  # ══════════════════════════════════════════════════════════════════════════
  if [[ "$NERD_PATCH" == true ]]; then
    printf "%b" "\e[1;33m==> WARNING:\e[0m Patching with Nerd Fonts glyphs — this takes a while...\n"
    find "$srcdir/selected" -name '*.ttf' ! -path '*/._*' | sort | \
      parallel -j4 python "$srcdir/font-patcher-${_nfver}" \
        --variable-width-glyphs -q -c {} \
        -out "$srcdir/patched" &>/dev/null || true
    local _hintsrc="$srcdir/patched"
  else
    local _hintsrc="$srcdir/selected"
  fi

  # ══════════════════════════════════════════════════════════════════════════
  # Step 3 — ttfautohint hinting
  # ══════════════════════════════════════════════════════════════════════════
  if [[ "$HINTING" == true ]]; then
    local _xheight
    case "$HINT_MODE" in
      nnn) _xheight=12 ;;
      *)   _xheight=14 ;;
    esac
    printf "%b" "\e[1;32m==> INFO:\e[0m Applying ttfautohint (mode=$HINT_MODE, x-height=${_xheight}px)...\n"
    mkdir -p "$srcdir/patched/hinted"
    for _f in "$_hintsrc"/*.ttf; do
      [[ -f "$_f" ]] || continue
      ttfautohint \
        --hinting-range-min=8 \
        --hinting-range-max=50 \
        --hinting-limit=200 \
        --increase-x-height="$_xheight" \
        --x-height-snapping-exceptions="" \
        --fallback-script=none \
        --fallback-scaling \
        --stem-width-mode="$HINT_MODE" \
        --no-info \
        --ignore-restrictions \
        "$_f" "$srcdir/patched/hinted/$(basename "$_f")"
    done
  fi

  # Persist state for package()
  printf "%s\n%s\n" "$HINTING" "$NERD_PATCH" > "$srcdir/.build_state"
}

package() {
  local HINTING NERD_PATCH
  { read -r HINTING; read -r NERD_PATCH; } \
    < "$srcdir/.build_state" 2>/dev/null || { HINTING=false; NERD_PATCH=false; }

  install -d "$pkgdir/usr/share/fonts/TTF"

  if [[ "$HINTING" == true ]]; then
    install -m644 "$srcdir/patched/hinted/"*.ttf "$pkgdir/usr/share/fonts/TTF"
  elif [[ "$NERD_PATCH" == true ]]; then
    install -m644 "$srcdir/patched/"*.ttf "$pkgdir/usr/share/fonts/TTF"
  else
    install -m644 "$srcdir/selected/"*.ttf "$pkgdir/usr/share/fonts/TTF"
  fi

  install -d "$pkgdir/usr/share/licenses/$pkgname"
  cat > "$pkgdir/usr/share/licenses/$pkgname/LICENSE" <<'EOF'
Google Sans fonts are proprietary fonts owned by Google LLC.
They are distributed as part of Google's Flutter gallery assets.
See https://flutter.googlesource.com/gallery-assets for details.

Nerd Fonts glyphs are licensed under their respective open-source licenses.
See https://github.com/ryanoasis/nerd-fonts for details.
EOF
}