#!/usr/bin/make -f
# -*- makefile -*-

%:
	dh $@

# Build the fonts from the FontForge sources in <family>/sources.  Upstream
# also ships pre-built TrueType files next to them; we regenerate from source
# into debian/build-<family>.
#
# Three adjustments make FontForge's output agree with what upstream shipped:
#
#  * gasp - pin version 1.  FontForge otherwise emits the legacy 0.2, which
#    OpenType Sanitizer flags.
#  * OS/2 - the sources request USE_TYPO_METRICS, which also makes FontForge
#    emit an OS/2 version 4 table.  Turning it off restores the version 3
#    table and the plain fsSelection that upstream's fonts carry.
#  * post - FontForge records the top edge of the underline bar in
#    underlinePosition, while upstream recorded the bottom edge.  The two are
#    a full underline thickness apart.
override_dh_auto_build:
	set -e; \
	for family in Berenika Klaudia; do \
		mkdir -p debian/build-$$family; \
		for sfd in $$family/sources/*.sfd; do \
			name=$$(sed -n 's/^FontName: *//p' "$$sfd"); \
			ttf=debian/build-$$family/$$name.ttf; \
			echo "Building $$name from $$sfd"; \
			fontforge -lang=py -c 'import sys, fontforge; \
			    font = fontforge.open(sys.argv[1]); \
			    font.gasp_version = 1; \
			    font.os2_use_typo_metrics = 0; \
			    font.generate(sys.argv[2]); \
			    font.close()' "$$sfd" "$$ttf"; \
			python3 -c 'import sys; \
			    from fontTools.ttLib import TTFont; \
			    font = TTFont(sys.argv[1], recalcTimestamp=False); \
			    post = font["post"]; \
			    post.underlinePosition -= post.underlineThickness; \
			    font.save(sys.argv[1])' "$$ttf"; \
		done; \
		for ttf in $$family/*.ttf; do \
			name=$$(basename $$ttf); \
			echo "Comparing $$name with build output"; \
			sfddiff $$ttf debian/build-$$family/$$name || { \
				echo "Error: $$ttf differs from debian/build-$$family/$$name"; \
				exit 1; \
			}; \
		done; \
	done

execute_before_dh_installdocs:
	cp Berenika/FONTLOG.txt FONTLOG-Berenika.txt
	cp Klaudia/FONTLOG.txt  FONTLOG-Klaudia.txt
