Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e3b8fea2a5 | |||
| 83a84b7941 | |||
| 19e915f4e1 | |||
| ef39ce4046 | |||
| 5cfc4ea36f | |||
| efc9c37522 | |||
| f43edba38f | |||
| a736526056 | |||
| d4cce21701 | |||
| b96bb96a88 | |||
| 2a27486aca | |||
| 784a1e4f40 | |||
| 4797071ede | |||
| 5fc4d9b8d8 | |||
| f5aac0701f | |||
| 139b14dbf6 | |||
| bd9e4d99d5 | |||
| defd728985 |
73
.github/workflows/build.yml
vendored
Normal file
73
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
name: Build fonts
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, develop]
|
||||
tags: ["*"]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/nicoverbruggen/fntbld-oci:latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
|
||||
- name: Build fonts
|
||||
run: python3 build.py
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: Readerly
|
||||
path: out/ttf/*.ttf
|
||||
|
||||
- name: Upload Kobo artifact
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: KF_Readerly
|
||||
path: out/kf/*.ttf
|
||||
|
||||
- name: Zip TTFs for release
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
run: |
|
||||
cd out/ttf && zip -j ../../Readerly.zip *.ttf
|
||||
cd ../../out/kf && zip -j ../../KF_Readerly.zip *.ttf
|
||||
|
||||
- name: Upload release zips
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: Readerly-release
|
||||
path: |
|
||||
Readerly.zip
|
||||
KF_Readerly.zip
|
||||
|
||||
release:
|
||||
needs: build
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
steps:
|
||||
- uses: actions/download-artifact@v6
|
||||
with:
|
||||
name: Readerly-release
|
||||
|
||||
- name: Create release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
draft: false
|
||||
name: ${{ github.ref_name }}
|
||||
body: |
|
||||
> [!TIP]
|
||||
> **If you are using a Kobo device and reading books purchased from the Kobo Store or reading `kepub` files converted via Calibre**, you should download KF_Readerly.zip, which has fonts slightly altered for optimal kerning for the `kepub` renderer.
|
||||
|
||||
### Learn more
|
||||
|
||||
Readerly is part of the `ebook-fonts` collection. For more information about those fonts, screenshots and how to install them, please consult the [README](https://github.com/nicoverbruggen/ebook-fonts/blob/main/README.md). The FAQ also includes an entry on how to enable ligatures on Kobo devices, which is highly recommended.
|
||||
files: |
|
||||
Readerly.zip
|
||||
KF_Readerly.zip
|
||||
37
README.md
37
README.md
@@ -12,6 +12,13 @@ The goal was to get a metrically/visually similar font, without actually copying
|
||||
|
||||
To get to the final result, I decided to use the variable font and work on it. The original is located in `src` and is available under the same OFL as the end result, which is included in `LICENSE`.
|
||||
|
||||
## Downloads
|
||||
|
||||
Two versions are generated via the pipeline of the [latest release](../../releases/latest):
|
||||
|
||||
- **KF_Readerly.zip** — Kobo-optimized TrueType fonts with a legacy kern table and `KF` prefix. Use this if you have a Kobo e-reader, this version contains optimizations made with [Kobo Font Fix](https://github.com/nicoverbruggen/kobo-font-fix).
|
||||
- **Readerly.zip** — The standard, unmodified fonts, as TrueType files. Useful for other e-readers and use on your desktop computer or smartphone.
|
||||
|
||||
## Project structure
|
||||
|
||||
- `src`: Newsreader variable font TTFs
|
||||
@@ -22,7 +29,6 @@ To get to the final result, I decided to use the variable font and work on it. T
|
||||
|
||||
After running `build.py`, you should get:
|
||||
|
||||
- `out/sfd`: FontForge source files (generated)
|
||||
- `out/ttf`: final TTF fonts (generated)
|
||||
|
||||
## Prerequisites
|
||||
@@ -30,28 +36,53 @@ After running `build.py`, you should get:
|
||||
- **Python 3**
|
||||
- **[fontTools](https://github.com/fonttools/fonttools)** — install with `pip install fonttools`
|
||||
- **[FontForge](https://fontforge.org)** — the build script auto-detects FontForge from PATH, Flatpak, or the macOS app bundle
|
||||
- **[ttfautohint](https://freetype.org/ttfautohint/)** — required for proper rendering on Kobo e-readers
|
||||
|
||||
### Linux preparation
|
||||
|
||||
```
|
||||
sudo apt install ttfautohint # Debian/Ubuntu
|
||||
sudo dnf install ttfautohint # Fedora
|
||||
brew install ttfautohint # Bazzite (immutable Fedora)
|
||||
pip install fonttools
|
||||
flatpak install flathub org.fontforge.FontForge
|
||||
```
|
||||
|
||||
### macOS preparation
|
||||
|
||||
#### System Python
|
||||
|
||||
On macOS, if you're using the built-in version of Python (via Xcode), you may need to first add a folder to your `PATH` to make `font-line` available, like:
|
||||
|
||||
```bash
|
||||
echo 'export PATH="$HOME/Library/Python/3.9/bin:$PATH"' >> ~/.zshrc
|
||||
brew install fontforge
|
||||
brew install fontforge ttfautohint
|
||||
brew unlink python3 # ensure that python3 isn't linked via Homebrew
|
||||
pip3 install fonttools font-line
|
||||
source ~/.zshrc
|
||||
```
|
||||
|
||||
#### Homebrew Python
|
||||
|
||||
If you're using `brew install python`, pip requires a virtual environment:
|
||||
|
||||
```bash
|
||||
brew install fontforge ttfautohint
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install fonttools
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
**Note**: If you're using `venv`, you will need to activate it first:
|
||||
|
||||
```
|
||||
source .venv/bin/activate
|
||||
```
|
||||
|
||||
If you are just using the system Python, you can skip that step and simply run:
|
||||
|
||||
```
|
||||
python3 build.py
|
||||
```
|
||||
@@ -62,4 +93,4 @@ To customize the font family name, disable old-style kerning, or skip outline fi
|
||||
python3 build.py --customize
|
||||
```
|
||||
|
||||
The build script (`build.py`) uses `fontTools` and FontForge to transform the Newsreader variable fonts into Readerly. Configuration and step-by-step details live in the header comments of `build.py`.
|
||||
The build script (`build.py`) uses `fontTools` and FontForge to transform the Newsreader variable fonts into Readerly. After export, it post-processes the TTFs: clamping x-height overshoots that cause uneven rendering on e-ink, normalizing style flags, and autohinting with `ttfautohint` for Kobo's FreeType renderer. Configuration and step-by-step details live in the header comments of `build.py`.
|
||||
|
||||
388
build.py
388
build.py
@@ -7,7 +7,8 @@ Orchestrates the full font build pipeline:
|
||||
1. Instances variable fonts into static TTFs (fontTools.instancer)
|
||||
2. Applies vertical scale (scale.py) via FontForge
|
||||
3. Applies vertical metrics, line height, rename (metrics.py, lineheight.py, rename.py)
|
||||
4. Exports to SFD and TTF → ./out/sfd/ and ./out/ttf/
|
||||
4. Exports to TTF → ./out/ttf/
|
||||
5. Post-processes TTFs: style flags, kern pairs, autohinting
|
||||
|
||||
Uses FontForge (detected automatically).
|
||||
Run with: python3 build.py
|
||||
@@ -24,7 +25,7 @@ import textwrap
|
||||
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
#
|
||||
# Most of these values are safe to tweak. The --customize flag only toggles
|
||||
# a small subset at runtime (family name, old-style kerning, outline fixes).
|
||||
# a small subset at runtime (family name, outline fixes).
|
||||
#
|
||||
# Quick reference (what each knob does):
|
||||
# - REGULAR_VF / ITALIC_VF: input variable fonts from ./src
|
||||
@@ -35,13 +36,14 @@ import textwrap
|
||||
# - LINE_HEIGHT: Typo line height (default line spacing)
|
||||
# - SELECTION_HEIGHT: Win/hhea selection box height and clipping
|
||||
# - ASCENDER_RATIO: ascender share of total height
|
||||
# - KERN_PAIRS: explicit GPOS kern pairs (for devices without ligatures)
|
||||
# - STYLE_MAP: naming/weight metadata per style
|
||||
|
||||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
SRC_DIR = os.path.join(ROOT_DIR, "src")
|
||||
OUT_DIR = os.path.join(ROOT_DIR, "out")
|
||||
OUT_SFD_DIR = os.path.join(OUT_DIR, "sfd") # generated FontForge sources
|
||||
OUT_TTF_DIR = os.path.join(OUT_DIR, "ttf") # generated TTFs
|
||||
OUT_KF_DIR = os.path.join(OUT_DIR, "kf") # Kobo (KF) variants
|
||||
|
||||
REGULAR_VF = os.path.join(SRC_DIR, "Newsreader-VariableFont_opsz,wght.ttf")
|
||||
ITALIC_VF = os.path.join(SRC_DIR, "Newsreader-Italic-VariableFont_opsz,wght.ttf")
|
||||
@@ -87,6 +89,41 @@ LINE_HEIGHT = 1.0
|
||||
SELECTION_HEIGHT = 1.3
|
||||
ASCENDER_RATIO = 0.8
|
||||
|
||||
# Step 4: ttfautohint options (hinting for Kobo's FreeType renderer)
|
||||
# - Kobo uses FreeType grayscale, so the 1st char of --stem-width-mode
|
||||
# matters: n=natural (least distortion), q=quantized, s=strong.
|
||||
# - x-height snapping is disabled to avoid inconsistent glyph heights.
|
||||
AUTOHINT_OPTS = [
|
||||
"--no-info",
|
||||
"--stem-width-mode=nss",
|
||||
"--increase-x-height=0",
|
||||
'--x-height-snapping-exceptions=-',
|
||||
]
|
||||
|
||||
# Baseline alignment: deepen the bottom anti-aliasing of non-serifed
|
||||
# glyphs via hinting-only touch deltas (no outline changes). This
|
||||
# shifts their bottom points down during rasterization so they produce
|
||||
# more gray below the baseline, visually matching serifed characters.
|
||||
# Each entry is (shift_px, ppem_min, ppem_max). Shifts are in pixels
|
||||
# (multiples of 1/8, max 1.0). Set to empty list to disable.
|
||||
BASELINE_HINT_SHIFTS = [
|
||||
(0.125, 6, 53),
|
||||
]
|
||||
|
||||
# Per-glyph Y ceiling: cap the top of specific glyphs to reduce
|
||||
# oversized or awkward serifs. (glyph_name, max_y)
|
||||
# Points above max_y are clamped down to max_y.
|
||||
GLYPH_Y_CEILING = [
|
||||
("u", 1062), # flatten tiny top serif tips to platform level
|
||||
]
|
||||
|
||||
# Explicit kern pairs: (left_glyph, right_glyph, kern_value_in_units).
|
||||
# Negative values tighten spacing. These are added on top of any existing
|
||||
# kerning from the source variable font.
|
||||
KERN_PAIRS = [
|
||||
("f", "i", -100), # this emulates the `fi` ligature
|
||||
]
|
||||
|
||||
# Step 3: Naming and style metadata (used by the rename step)
|
||||
STYLE_MAP = {
|
||||
"Regular": ("Regular", "Book", 400),
|
||||
@@ -505,19 +542,15 @@ def ff_license_script():
|
||||
""")
|
||||
|
||||
|
||||
def build_export_script(sfd_path, ttf_path, old_kern=True):
|
||||
def build_export_script(sfd_path, ttf_path):
|
||||
"""Build a FontForge script that opens an .sfd and exports to TTF."""
|
||||
if old_kern:
|
||||
flags_line = 'flags = ("opentype", "old-kern", "no-FFTM-table", "winkern")'
|
||||
else:
|
||||
flags_line = 'flags = ("opentype", "no-FFTM-table")'
|
||||
return textwrap.dedent(f"""\
|
||||
import fontforge
|
||||
|
||||
f = fontforge.open({sfd_path!r})
|
||||
print("Exporting: " + f.fontname)
|
||||
|
||||
{flags_line}
|
||||
flags = ("opentype", "no-FFTM-table")
|
||||
f.generate({ttf_path!r}, flags=flags)
|
||||
|
||||
print(" -> " + {ttf_path!r})
|
||||
@@ -587,6 +620,7 @@ def clean_ttf_degenerate_contours(ttf_path):
|
||||
font.close()
|
||||
|
||||
|
||||
|
||||
def fix_ttf_style_flags(ttf_path, style_suffix):
|
||||
"""Normalize OS/2 fsSelection and head.macStyle for style linking."""
|
||||
try:
|
||||
@@ -621,10 +655,306 @@ def fix_ttf_style_flags(ttf_path, style_suffix):
|
||||
print(f" Normalized style flags for {style_suffix}")
|
||||
|
||||
|
||||
def add_kern_pairs(ttf_path):
|
||||
"""Prepend explicit kern pairs to the GPOS kern table.
|
||||
|
||||
Devices that don't support OpenType ligatures (e.g. some e-readers)
|
||||
fall back to individual glyphs. Without kern pairs, combinations
|
||||
like 'fi' render with a visible gap.
|
||||
|
||||
Pairs are inserted at the front of the first PairPos subtable so
|
||||
they take priority even on renderers that truncate the kern list.
|
||||
"""
|
||||
if not KERN_PAIRS:
|
||||
return
|
||||
|
||||
try:
|
||||
from fontTools.ttLib import TTFont
|
||||
from fontTools.ttLib.tables.otTables import PairValueRecord, ValueRecord
|
||||
except Exception:
|
||||
print(" [warn] Skipping kern pairs: fontTools not available", file=sys.stderr)
|
||||
return
|
||||
|
||||
font = TTFont(ttf_path)
|
||||
gpos = font.get("GPOS")
|
||||
if gpos is None:
|
||||
font.close()
|
||||
print(" [warn] No GPOS table, skipping kern pairs", file=sys.stderr)
|
||||
return
|
||||
|
||||
cmap = font.getBestCmap()
|
||||
# Map glyph names: try cmap first, fall back to glyph order
|
||||
glyph_order = set(font.getGlyphOrder())
|
||||
|
||||
def resolve(name):
|
||||
# If it's a single character, look up via cmap
|
||||
if len(name) == 1:
|
||||
cp = ord(name)
|
||||
if cp in cmap:
|
||||
return cmap[cp]
|
||||
# Otherwise treat as a glyph name
|
||||
if name in glyph_order:
|
||||
return name
|
||||
return None
|
||||
|
||||
# Build resolved pairs
|
||||
pairs = []
|
||||
for left, right, value in KERN_PAIRS:
|
||||
l = resolve(left)
|
||||
r = resolve(right)
|
||||
if l and r:
|
||||
pairs.append((l, r, value))
|
||||
else:
|
||||
print(f" [warn] Kern pair {left}+{right}: glyph not found", file=sys.stderr)
|
||||
|
||||
if not pairs:
|
||||
font.close()
|
||||
return
|
||||
|
||||
# Find the first Format 1 (individual pairs) PairPos subtable in kern
|
||||
pair_pos = None
|
||||
for lookup in gpos.table.LookupList.Lookup:
|
||||
if lookup.LookupType == 2: # PairPos
|
||||
for subtable in lookup.SubTable:
|
||||
if subtable.Format == 1:
|
||||
pair_pos = subtable
|
||||
break
|
||||
if pair_pos:
|
||||
break
|
||||
|
||||
if pair_pos is None:
|
||||
font.close()
|
||||
print(" [warn] No Format 1 PairPos subtable found, skipping kern pairs", file=sys.stderr)
|
||||
return
|
||||
|
||||
count = 0
|
||||
for left_glyph, right_glyph, value in pairs:
|
||||
# Find or create the PairSet for the left glyph
|
||||
try:
|
||||
idx = pair_pos.Coverage.glyphs.index(left_glyph)
|
||||
except ValueError:
|
||||
# Left glyph not in coverage — add it
|
||||
pair_pos.Coverage.glyphs.append(left_glyph)
|
||||
from fontTools.ttLib.tables.otTables import PairSet
|
||||
ps = PairSet()
|
||||
ps.PairValueRecord = []
|
||||
ps.PairValueCount = 0
|
||||
pair_pos.PairSet.append(ps)
|
||||
pair_pos.PairSetCount = len(pair_pos.PairSet)
|
||||
idx = len(pair_pos.Coverage.glyphs) - 1
|
||||
|
||||
pair_set = pair_pos.PairSet[idx]
|
||||
|
||||
# Remove existing pair for same right glyph
|
||||
pair_set.PairValueRecord = [
|
||||
pvr for pvr in pair_set.PairValueRecord
|
||||
if pvr.SecondGlyph != right_glyph
|
||||
]
|
||||
|
||||
# Prepend new pair so it appears first
|
||||
pvr = PairValueRecord()
|
||||
pvr.SecondGlyph = right_glyph
|
||||
vr = ValueRecord()
|
||||
vr.XAdvance = value
|
||||
pvr.Value1 = vr
|
||||
pair_set.PairValueRecord.insert(0, pvr)
|
||||
pair_set.PairValueCount = len(pair_set.PairValueRecord)
|
||||
count += 1
|
||||
|
||||
font.save(ttf_path)
|
||||
font.close()
|
||||
print(f" Added {count} kern pair(s) to GPOS")
|
||||
|
||||
|
||||
|
||||
def apply_glyph_y_ceiling(ttf_path):
|
||||
"""Clamp glyph points above a Y ceiling down to the ceiling value."""
|
||||
if not GLYPH_Y_CEILING:
|
||||
return
|
||||
|
||||
from fontTools.ttLib import TTFont
|
||||
font = TTFont(ttf_path)
|
||||
glyf = font["glyf"]
|
||||
modified = []
|
||||
|
||||
for glyph_name, max_y in GLYPH_Y_CEILING:
|
||||
g = glyf.get(glyph_name)
|
||||
if not g or not g.numberOfContours or g.numberOfContours <= 0:
|
||||
continue
|
||||
coords = g.coordinates
|
||||
clamped = 0
|
||||
for j in range(len(coords)):
|
||||
if coords[j][1] > max_y:
|
||||
coords[j] = (coords[j][0], max_y)
|
||||
clamped += 1
|
||||
if clamped:
|
||||
modified.append(f"{glyph_name}({clamped}pts)")
|
||||
|
||||
if modified:
|
||||
font.save(ttf_path)
|
||||
print(f" Clamped Y ceiling: {', '.join(modified)}")
|
||||
font.close()
|
||||
|
||||
|
||||
def _generate_baseline_shift_ctrl(ttf_path):
|
||||
"""Generate touch deltas to deepen bottom anti-aliasing of non-serifed glyphs.
|
||||
|
||||
For lowercase glyphs without a flat baseline (no serif foot), shifts
|
||||
the bottom-most points down during rasterization. Uses graduated
|
||||
shifts from BASELINE_HINT_SHIFTS — stronger at small ppem sizes
|
||||
where alignment is most noticeable. No outline changes.
|
||||
"""
|
||||
if not BASELINE_HINT_SHIFTS:
|
||||
return ""
|
||||
|
||||
from fontTools.ttLib import TTFont
|
||||
font = TTFont(ttf_path)
|
||||
glyf = font["glyf"]
|
||||
cmap = font.getBestCmap()
|
||||
lines = []
|
||||
|
||||
for char in "abcdefghijklmnopqrstuvwxyz":
|
||||
code = ord(char)
|
||||
if code not in cmap:
|
||||
continue
|
||||
name = cmap[code]
|
||||
g = glyf[name]
|
||||
if not g.numberOfContours or g.numberOfContours <= 0:
|
||||
continue
|
||||
coords = g.coordinates
|
||||
ys = set(c[1] for c in coords)
|
||||
if 0 in ys:
|
||||
continue # has serif baseline
|
||||
bottom_pts = [i for i, (x, y) in enumerate(coords) if y <= 0]
|
||||
if not bottom_pts:
|
||||
continue
|
||||
pts_str = ", ".join(str(p) for p in bottom_pts)
|
||||
for shift_px, ppem_min, ppem_max in BASELINE_HINT_SHIFTS:
|
||||
shift = -abs(shift_px)
|
||||
lines.append(
|
||||
f"{name} touch {pts_str} yshift {shift:.3f} @ {ppem_min}-{ppem_max}"
|
||||
)
|
||||
|
||||
font.close()
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def autohint_ttf(ttf_path):
|
||||
"""Run ttfautohint to add proper TrueType hinting.
|
||||
|
||||
Kobo uses FreeType for font rasterization. Without embedded hints,
|
||||
FreeType's auto-hinter computes "blue zones" from the outlines.
|
||||
When a glyph (e.g. italic 't') has a curved tail that dips just
|
||||
below the baseline, the auto-hinter snaps that edge up to y=0 —
|
||||
shifting the entire glyph upward relative to its neighbors. This
|
||||
is most visible at small sizes.
|
||||
|
||||
ttfautohint replaces FreeType's built-in auto-hinter with its own
|
||||
hinting, which may handle sub-baseline overshoots more gracefully.
|
||||
The resulting bytecode is baked into the font, so FreeType uses
|
||||
the TrueType interpreter instead of falling back to auto-hinting.
|
||||
|
||||
Additionally generates per-font touch deltas to deepen the
|
||||
baseline anti-aliasing of non-serifed glyphs.
|
||||
"""
|
||||
if not shutil.which("ttfautohint"):
|
||||
print(" [warn] ttfautohint not found, skipping", file=sys.stderr)
|
||||
return
|
||||
|
||||
# Generate control instructions for this specific font's points
|
||||
ctrl_text = _generate_baseline_shift_ctrl(ttf_path)
|
||||
ctrl_path = ttf_path + ".ctrl.tmp"
|
||||
ctrl_count = 0
|
||||
opts = list(AUTOHINT_OPTS)
|
||||
if ctrl_text:
|
||||
with open(ctrl_path, "w") as f:
|
||||
f.write(ctrl_text)
|
||||
opts += [f"--control-file={ctrl_path}"]
|
||||
ctrl_count = ctrl_text.count("\n") + 1
|
||||
|
||||
tmp_path = ttf_path + ".autohint.tmp"
|
||||
result = subprocess.run(
|
||||
["ttfautohint"] + opts + [ttf_path, tmp_path],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
|
||||
if os.path.exists(ctrl_path):
|
||||
os.remove(ctrl_path)
|
||||
|
||||
if result.returncode != 0:
|
||||
print(f" [warn] ttfautohint failed: {result.stderr.strip()}", file=sys.stderr)
|
||||
if os.path.exists(tmp_path):
|
||||
os.remove(tmp_path)
|
||||
return
|
||||
|
||||
os.replace(tmp_path, ttf_path)
|
||||
hint_msg = "Autohinted with ttfautohint"
|
||||
if ctrl_count:
|
||||
hint_msg += f" ({ctrl_count} serif control hints)"
|
||||
print(f" {hint_msg}")
|
||||
|
||||
|
||||
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
# MAIN
|
||||
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
def check_ttfautohint():
|
||||
"""Verify ttfautohint is installed before starting the build."""
|
||||
if shutil.which("ttfautohint"):
|
||||
return
|
||||
print(
|
||||
"ERROR: ttfautohint not found.\n"
|
||||
"\n"
|
||||
"ttfautohint is required for proper rendering on Kobo e-readers.\n"
|
||||
"Install it with:\n"
|
||||
" macOS/Bazzite: brew install ttfautohint\n"
|
||||
" Debian/Ubuntu: sudo apt install ttfautohint\n"
|
||||
" Fedora: sudo dnf install ttfautohint\n"
|
||||
" Arch: sudo pacman -S ttfautohint\n",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
KOBOFIX_URL = (
|
||||
"https://raw.githubusercontent.com/nicoverbruggen/kobo-font-fix/main/kobofix.py"
|
||||
)
|
||||
|
||||
|
||||
def _download_kobofix(dest):
|
||||
"""Download kobofix.py if not already cached."""
|
||||
if os.path.isfile(dest):
|
||||
print(f" Using cached kobofix.py")
|
||||
return
|
||||
import urllib.request
|
||||
print(f" Downloading kobofix.py ...")
|
||||
urllib.request.urlretrieve(KOBOFIX_URL, dest)
|
||||
print(f" Saved to {dest}")
|
||||
|
||||
|
||||
def _run_kobofix(kobofix_path, variant_names):
|
||||
"""Run kobofix.py --preset kf on built TTFs, move KF_ files to out/kf/."""
|
||||
ttf_files = [os.path.join(OUT_TTF_DIR, f"{n}.ttf") for n in variant_names]
|
||||
cmd = [sys.executable, kobofix_path, "--preset", "kf"] + ttf_files
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.stdout:
|
||||
print(result.stdout, end="")
|
||||
if result.returncode != 0:
|
||||
print("\nERROR: kobofix.py failed", file=sys.stderr)
|
||||
if result.stderr:
|
||||
print(result.stderr, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
os.makedirs(OUT_KF_DIR, exist_ok=True)
|
||||
import glob
|
||||
moved = 0
|
||||
for kf_file in glob.glob(os.path.join(OUT_TTF_DIR, "KF_*.ttf")):
|
||||
dest = os.path.join(OUT_KF_DIR, os.path.basename(kf_file))
|
||||
shutil.move(kf_file, dest)
|
||||
moved += 1
|
||||
print(f" Moved {moved} KF font(s) to {OUT_KF_DIR}/")
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print(" Readerly Build")
|
||||
@@ -632,22 +962,29 @@ def main():
|
||||
|
||||
ff_cmd = find_fontforge()
|
||||
print(f" FontForge: {' '.join(ff_cmd)}")
|
||||
check_ttfautohint()
|
||||
print(f" ttfautohint: {shutil.which('ttfautohint')}")
|
||||
|
||||
family = DEFAULT_FAMILY
|
||||
old_kern = False
|
||||
outline_fix = True
|
||||
|
||||
# --name "Foo" sets the family name directly
|
||||
if "--name" in sys.argv:
|
||||
idx = sys.argv.index("--name")
|
||||
if idx + 1 < len(sys.argv):
|
||||
family = sys.argv[idx + 1]
|
||||
else:
|
||||
print("ERROR: --name requires a value", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if "--customize" in sys.argv:
|
||||
print()
|
||||
family = input(f" Font family name [{DEFAULT_FAMILY}]: ").strip() or DEFAULT_FAMILY
|
||||
old_kern_input = input(" Export with old-style kerning? [y/N]: ").strip().lower()
|
||||
old_kern = old_kern_input in ("y", "yes")
|
||||
family = input(f" Font family name [{family}]: ").strip() or family
|
||||
outline_input = input(" Apply outline fixes (remove overlaps + zero-area cleanup)? [Y/n]: ").strip().lower()
|
||||
outline_fix = outline_input not in ("n", "no")
|
||||
|
||||
print()
|
||||
print(f" Family: {family}")
|
||||
print(f" Old kern: {'yes' if old_kern else 'no'}")
|
||||
print(f" Outline fix: {'yes' if outline_fix else 'no'}")
|
||||
print()
|
||||
|
||||
@@ -657,12 +994,12 @@ def main():
|
||||
os.makedirs(tmp_dir)
|
||||
|
||||
try:
|
||||
_build(tmp_dir, family=family, old_kern=old_kern, outline_fix=outline_fix)
|
||||
_build(tmp_dir, family=family, outline_fix=outline_fix)
|
||||
finally:
|
||||
shutil.rmtree(tmp_dir, ignore_errors=True)
|
||||
|
||||
|
||||
def _build(tmp_dir, family=DEFAULT_FAMILY, old_kern=True, outline_fix=True):
|
||||
def _build(tmp_dir, family=DEFAULT_FAMILY, outline_fix=True):
|
||||
variants = [(f"{family}-{style}", vf, wght, opsz)
|
||||
for style, vf, wght, opsz in VARIANT_STYLES]
|
||||
variant_names = [name for name, _, _, _ in variants]
|
||||
@@ -746,7 +1083,6 @@ def _build(tmp_dir, family=DEFAULT_FAMILY, old_kern=True, outline_fix=True):
|
||||
|
||||
# Step 4: Export to out/sfd and out/ttf
|
||||
print("\n── Step 4: Export ──\n")
|
||||
os.makedirs(OUT_SFD_DIR, exist_ok=True)
|
||||
os.makedirs(OUT_TTF_DIR, exist_ok=True)
|
||||
|
||||
for name in variant_names:
|
||||
@@ -754,22 +1090,28 @@ def _build(tmp_dir, family=DEFAULT_FAMILY, old_kern=True, outline_fix=True):
|
||||
ttf_path = os.path.join(OUT_TTF_DIR, f"{name}.ttf")
|
||||
style_suffix = name.split("-")[-1] if "-" in name else "Regular"
|
||||
|
||||
# Copy final SFD to out/sfd/
|
||||
shutil.copy2(sfd_path, os.path.join(OUT_SFD_DIR, f"{name}.sfd"))
|
||||
print(f" -> {OUT_SFD_DIR}/{name}.sfd")
|
||||
|
||||
# Export TTF
|
||||
script = build_export_script(sfd_path, ttf_path, old_kern=old_kern)
|
||||
script = build_export_script(sfd_path, ttf_path)
|
||||
run_fontforge_script(script)
|
||||
if outline_fix:
|
||||
clean_ttf_degenerate_contours(ttf_path)
|
||||
fix_ttf_style_flags(ttf_path, style_suffix)
|
||||
add_kern_pairs(ttf_path)
|
||||
apply_glyph_y_ceiling(ttf_path)
|
||||
autohint_ttf(ttf_path)
|
||||
|
||||
|
||||
# Step 5: Generate Kobo (KF) variants via kobofix.py
|
||||
print("\n── Step 5: Generate Kobo (KF) variants ──\n")
|
||||
|
||||
kobofix_path = os.path.join(tmp_dir, "kobofix.py")
|
||||
_download_kobofix(kobofix_path)
|
||||
_run_kobofix(kobofix_path, variant_names)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print(" Build complete!")
|
||||
print(f" SFD fonts are in: {OUT_SFD_DIR}/")
|
||||
print(f" TTF fonts are in: {OUT_TTF_DIR}/")
|
||||
print(f" KF fonts are in: {OUT_KF_DIR}/")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
|
||||
BIN
screenshot.png
Executable file → Normal file
BIN
screenshot.png
Executable file → Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 188 KiB After Width: | Height: | Size: 192 KiB |
Reference in New Issue
Block a user