# This Makefile is part of CJK package Version 2.5.
#
# GNU Makefile for NTU2CJK which produce CJK .pfb fonts and .tfm files
# from NTU TrueType fonts.
#
# *Please modify those settings enclosed between "user-defined"*

default:
	@echo "Say                                       "
	@echo "                                          "
	@echo "    make TARGET [PLANES]                  "
	@echo "                                          "
	@echo "where TARGET can be one of the following: "
	@echo "  unix unixdebug                          "
	@echo "  os2 bound bounddebug                    "
	@echo "  dos dosdebug                            "
	@echo "  install                                 "
	@echo "  clean distclean allclean                "
	@echo "                                          "
	@echo "PLANES is optional and can e.g. be set as:"
	@echo "  PLANES=\"01 02 03\"                       "
	@echo "for processing only subfonts 01, 02, 03.  "
	@echo "                                          "
	@echo "For more information see the README file. "

# ------------- user-defined ---------------

# the NTU TrueType font
NTUTTF = NTU_BR.TTF

# directories.
#PREFIX = /usr/local

#   TFM files
#TEXDIR = $(PREFIX)/lib/texmf
#TFMDIR = $(TEXDIR)/fonts/public/big5/tfm
TFMDIR = .

#   the GS home dir.
#GSDIR = $(PREFIX)/lib/ghostscript
GSDIR = .

#   dir. for DVIPS config.ps etc.
#TEXCONFIG = $(TEXDIR)/dvips
TEXCONFIG = .

#   GS font dir.
#GSFONTDIR = $(GSDIR)/fonts
GSFONTDIR = .

#   GS Fontmap
GSFONTMAP = $(GSDIR)/Fontmap

#   DVIPS fontmap
PSFONTMAP = $(TEXCONFIG)/psfonts.map

# font
#   prefix for the output fonts
FONTFL = b5br

#   prefix for the extended fonts (width is 1.2 times the normal font)
FONTxFL = b5brx

#   Prefix of the UniqueID number for Type 1 font.
#   The true UniqueID numbers will be [$(FONTUID)001,$(FONTUID)055].
FONTNAME = BlackRound

#   Unique ID for Type 1 font
FONTUID = 4152

#   CJK sub-font planes [01,55] (compressed)
PLANES = 01 02 03 04 05 06 07 08 09 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

# under UNIX the "cat" command; under MSDOS and OS/2 "type"
CAT = cat

# under UNIX the "cp" command; under MSDOS and OS/2 "copy"
CP = cp

# the SHELL environment is only needed explicitly by GNU make for DOS
#SHELL = command.com

# comment out the next line if you don't want to calculate the correct
# %%VMusage field in the Type 1 PS fonts.
VM_MODIFY=YES

# ------------- user-defined ---------------

.SUFFIXES:
.PHONY: default all install clean \
	unix unixdebug os2 bound bounddebug dos dosdebug \
	make_pfb make_tfm \
	distclean realclean allclean


CJKFONTS     = $(addprefix $(FONTFL),$(PLANES))
PSFONTS      = $(addsuffix .ps,$(CJKFONTS))
PFBS	     = $(addsuffix .pfb,$(CJKFONTS))
PFAS	     = $(addsuffix .pfa,$(CJKFONTS))
AFMS	     = $(addsuffix .afm,$(CJKFONTS))
TFMS	     = $(addsuffix .tfm,$(CJKFONTS))

FONTF	     = $(basename $@)
PLANE	     = $(subst $(FONTFL),,$(FONTF))
FONTFILE     = $(FONTFL)$(PLANE)
FONTxFILE    = $(FONTxFL)$(PLANE)
CJKFONTNAME  = $(FONTNAME)$(PLANE)

GSFONTEXIST  = $(findstring $(FONTFILE),$(shell $(CAT) $(GSFONTMAP)))
PSFONTEXIST  = $(findstring $(FONTFILE),$(shell $(CAT) $(PSFONTMAP)))
PSFONTxEXIST = $(findstring $(FONTxFILE),$(shell $(CAT) $(PSFONTMAP)))


# for UNIX systems
unix:
	$(MAKE) all CC='gcc -Wall -O -s' O=.o FS=UNIX

# for UNIX debugging
unixdebug:
	$(MAKE) all CC='gcc -Wall -O -g' O=.o FS=UNIX

# for OS/2 only (using emx-gcc)
os2:
	$(MAKE) all CC='gcc -Wall -Zomf -Zmtd -O -s' O=.obj EXE=.exe FS=MSDOS

# for OS/2 and DOS (using emx-gcc)
bound:
	$(MAKE) all CC='gcc -Wall -O -s' O=.o EXE=.exe FS=MSDOS

# for OS/2 and DOS debugging (using emx-gcc)
bounddebug:
	$(MAKE) all CC='gcc -Wall -O -g' O=.o EXE=.exe FS=MSDOS

# for DOS (using djgpp)
dos:
	$(MAKE) all CC='gcc -Wall -O -s' O=.ooo EXE=.exe FS=MSDOS

# for DOS debugging (using djgpp)
dosdebug:
	$(MAKE) all CC='gcc -Wall -O -g' O=.ooo EXE=.exe FS=MSDOS


all: $(TFMS)


%$O: %.c
	$(CC) $(CFLAGS) -c -D$(FS) -o $@ $<

%.exe: %.ooo
	$(CC) $(CFLAGS) -o $* $^
	strip $*
	coff2exe $*
	-del $*

%.exe: %.obj
	$(CC) $(CFLAGS) -o $@ $^

%$(EXE): %.o
	$(CC) $(CFLAGS) -o $@ $^


$(PSFONTS): ntu2cjk$(EXE) t1asm$(EXE) t1binary$(EXE) vmmodify$(EXE)
	-@echo ----------------- Building $(FONTFILE) -------------------
	ntu2cjk $(NTUTTF) $(PLANE) $(FONTNAME) $(FONTUID) > $@

$(PFAS): %.pfa : %.ps
	t1asm $< $@
  ifeq ($(VM_MODIFY),YES)
	-vmusage $@
  endif
	-$(RM) $<

$(PFBS): %.pfb : %.pfa
	$(MAKE) make_pfb IN=$< OUT=$@ TEST=$(GSFONTEXIST) FNT=$(CJKFONTNAME)

make_pfb:
	t1binary $(IN) $(OUT)
  ifeq ($(strip $(TEST)),)
	echo /$(FNT) ($(OUT)) ; >> $(GSFONTMAP)
  endif

$(AFMS): %.afm : %.pfb getafm.ps
	-getafm $(CJKFONTNAME) $(FONTFILE)

$(TFMS): %.tfm : %.afm
	$(MAKE) make_tfm IN=$< TEST=$(PSFONTEXIST) TESTx=$(PSFONTxEXIST) Fx=$(FONTxFILE)

make_tfm:
  ifeq ($(strip $(TEST)),)
	-afm2tfm $(IN) >> $(PSFONTMAP)
  else
	-afm2tfm $(IN)
  endif

  ifeq ($(strip $(TESTx)),)
	-afm2tfm $(IN) -e 1.2 $(Fx) >> $(PSFONTMAP)
  else
	-afm2tfm $(IN) -e 1.2 $(Fx)
  endif


# If you have changed some entries in Fontmap by renaming .pfb to .pfa,
# you should install the corresponding .pfa fonts to $(GSFONTDIR) manually.

install: $(TFMS) $(PFBS)
	-$(CP) $(TFMS) $(TFMDIR)
	-$(CP) $(subst $(FONTFL),$(FONTxFL),$(TFMS)) $(TFMDIR)
	-$(CP) $(PFBS) $(GSFONTDIR)


# Cleaning only .afm and .tfm files, used when GhostScript cannot produce
# .afm from .pfb.

clean:
	-$(RM) $(AFMS)
	-$(RM) $(TFMS)
	-$(RM) $(subst $(FONTFL),$(FONTxFL),$(TFMS))

# Cleaning all produced files except executables.

distclean realclean: clean
	-$(RM) $(PFAS)
	-$(RM) $(PFBS)

# Clean all.

allclean: distclean
	-$(RM) ntu2cjk$O ntu2cjk$(EXE)
	-$(RM) t1asm$O t1asm$(EXE)
	-$(RM) t1binary$O t1binary$(EXE)
