# SPDX-License-Identifier: CC0-1.0

FONTBASES = bedstead bedstead-extended bedstead-semicondensed \
            bedstead-condensed bedstead-extracondensed \
            bedstead-ultracondensed \
            bedstead-bold bedstead-boldextended bedstead-boldsemicondensed \
            bedstead-boldcondensed bedstead-boldextracondensed \
            bedstead-boldultracondensed

TTXFILES = $(addsuffix .ttx, $(FONTBASES))
OTFFILES = $(addsuffix .otf, $(FONTBASES))

DISTFILES = bedstead.c Makefile CONTRIBUTING COPYING HACKING NEWS \
	df.ps rom.ps Fontmap \
	$(OTFFILES) \
	bedstead-10.bdf bedstead-20.bdf bedstead-bold-20.bdf \
	bedstead-10-df.png bedstead-20-df.png \
	bedstead-complement.pdf

all: $(DISTFILES)

.PHONY: all-web
all-web: all sample.png extended.png icon-16.png icon-32.png icon-64.png

.PHONY: experimental
experimental: bedstead-chiseltip.otf bedstead-plotter-thin.otf \
 bedstead-plotter-light.otf bedstead-plotter-medium.otf \
 bedstead-plotter-bold.otf plotter.png

# CWARNFLAGS contains a set of GCC/Clang warning flags under which
# bedstead.c is clean.  The "glyphs" array makes heavy use of the
# shortcuts disapproved by -Wmissing-field-initializers and
# -Wmissing-braces, so those are disabled.
CWARNFLAGS ?= -Wall -Wextra \
	-Wno-missing-field-initializers -Wno-missing-braces
CFLAGS += $(CWARNFLAGS)

bedstead.ttx: bedstead
	./bedstead > bedstead.ttx

bedstead-extended.ttx: bedstead
	./bedstead --extended > bedstead-extended.ttx

bedstead-semicondensed.ttx: bedstead
	./bedstead --semi-condensed > bedstead-semicondensed.ttx

bedstead-condensed.ttx: bedstead
	./bedstead --condensed > bedstead-condensed.ttx

bedstead-extracondensed.ttx: bedstead
	./bedstead --extra-condensed > bedstead-extracondensed.ttx

bedstead-ultracondensed.ttx: bedstead
	./bedstead --ultra-condensed > bedstead-ultracondensed.ttx

bedstead-bold.ttx: bedstead
	./bedstead --bold > bedstead-bold.ttx

bedstead-boldextended.ttx: bedstead
	./bedstead --bold --extended > bedstead-boldextended.ttx

bedstead-boldsemicondensed.ttx: bedstead
	./bedstead --bold --semi-condensed > bedstead-boldsemicondensed.ttx

bedstead-boldcondensed.ttx: bedstead
	./bedstead --bold --condensed > bedstead-boldcondensed.ttx

bedstead-boldextracondensed.ttx: bedstead
	./bedstead --bold --extra-condensed > bedstead-boldextracondensed.ttx

bedstead-boldultracondensed.ttx: bedstead
	./bedstead --bold --ultra-condensed > bedstead-boldultracondensed.ttx

bedstead-oc.ttx: bedstead
	./bedstead --plotter > bedstead-oc.ttx

bedstead-plotter-%.ttx: strokefont.py bedstead-oc.ttx
	fontforge -lang=py -script strokefont.py plotter-$* bedstead-oc.ttx \
		bedstead-plotter-$*.ttx

bedstead-chiseltip.ttx: strokefont.py bedstead-oc.ttx
	fontforge -lang=py -script strokefont.py chiseltip bedstead-oc.ttx \
		bedstead-chiseltip.ttx

bedstead-%.bdf.ps: bedstead
	./bedstead --bdfgen $* > $@

bedstead-bold-%.bdf.ps: bedstead
	./bedstead --bold --bdfgen $* > $@

# Dependency of all of OTFFILES could be narrowed to just the relevant
# one.
%.bdf: %.bdf.ps $(OTFFILES) Fontmap
	gs -P -q -dNOSAFER -dNODISPLAY -dBATCH -- $< $@

%.otf: %.ttx
	ttx --no-recalc-timestamp -o $@ $<

%.png: %.ps $(OTFFILES) Fontmap
	gs -P -q -dSAFER -sDEVICE=png16m -dTextAlphaBits=4 -o $@ $<

icon-%.png: icon.ps bedstead-extended.otf Fontmap
	gs -P -q -dSAFER -dsize=$* -sDEVICE=png16m -dTextAlphaBits=4 -o $@ $<

bedstead-%-df.png: df.ps bedstead.otf Fontmap
	gs -P -q -dSAFER -dsize=$* -sDEVICE=png16m -o $@ $<

bedstead-complement.ps: bedstead
	./bedstead --complement > bedstead-complement.ps

bedstead-complement.pdf: bedstead-complement.ps bedstead.otf Fontmap
	ps2pdf -P $< $@

EMCCFLAGS = -O2 -sEXIT_RUNTIME -sSTRICT \
	-sINCOMING_MODULE_JS_API='["arguments","print"]'
EMCCFLAGS += $(CWARNFLAGS)
%.mjs: %.c
	emcc $(EMCCFLAGS) -o $@ $<

.PHONY: clean
clean:
	rm -f bedstead *.ttx *.otf *.bdf *.bdf.ps *.png *.pdf *.mjs *.wasm \
		bedstead-complement.ps

.PHONY: install-user
install-user: $(OTFFILES)
	install -d "$${HOME}"/.fonts
	install -m 644 $(OTFFILES) "$${HOME}"/.fonts

VERSION != sed -n 's/^\#define VERSION "\(.*\)"/\1/p' < bedstead.c

.PHONY: dist
dist: $(DISTFILES)
	rm -rf bedstead-$(VERSION)
	mkdir bedstead-$(VERSION)
	ln $(DISTFILES) bedstead-$(VERSION)
	zip -r bedstead-$(VERSION).zip  bedstead-$(VERSION)
	rm -r bedstead-$(VERSION)

.DELETE_ON_ERROR:
