# This Makefile is part of CJK package Version 2.5.
#
# GNU Makefile for NTU2CJK v1.2 which produces 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 ---------------

# Directories:
#   TFM files
TEXDIR = /usr/lib/texmf
#     dir. where the file psfonts.map is taken from and installed to again.
#     It doesn't matter if there is no psfonts.map in $(PSMAPDIR).
PSMAPDIR = $(TEXDIR)/dvips
#     the directory where the created .tfm files are installed.
TFMDIR = $(TEXDIR)/fonts/chinese/tfm
#   the GS dir,
GSDIR = /usr/local/lib/ghostscript
#     the GS home dir, where the file Fontmap is taken from and installed to
#     again.
#     It doesn't matter if there is no Fontmap in $(GSMAPDIR); in this case
#     the makefile will create one for all fonts being created smoothly.
GSMAPDIR = $(GSDIR)/3.33
#     GS font dir, where the created CJK PostScript fonts are installed.
GSFONTDIR = $(GSDIR)/fonts

# Font:
#   Create extended fonts (1.2 times width)? See README for more information.
EXTENDED_FONTS = NO
#   The NTU TrueType font.
NTUTTF = /dosd/truetype/b5fs.ttf
#   Prefix of the font files. 
#     The output font files will be $FONTFL[01-55].pfb
FONTFL = b5fs
ifeq ($(EXTENDED_FONTS),YES)
#   prefix for the extended fonts (width is 1.2 times the normal font)
FONTxFL = b5fsx
endif
#   Prefix for the FontName. 
#     The FontName used in the Type 1 font will be $FONTNAME[01-55]
FONTNAME = FunSong
#   Prefix of the UniqueID number for Type 1 font.
#     The true UniqueID numbers will be $(FONTUID)[001-055].
#     Personally I use the following font UID prefix (no special rules) 
#     for different fonts:
#		kai	4701
#		ming	4702
#		li	4703
#		black	4704
#		song	4705
#		round	4706
#		...
#     You should change these prefix for your own use. Or just as above.  
FONTUID = 4705
#   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

# Commands:
#   the install command
INSTALL = cp
#   the copy command
CP = cp

# comment out the next line if you don't want to calculate the (probably) 
# 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 \
	distclean realclean allclean \
	chk_fontmaps generate_maps


# Font maps:
#   GS Fontmap
GSFONTMAP = Fontmap
#   DVIPS fontmap
PSFONTMAP = psfonts.map

CJKFONTS     = $(addprefix $(FONTFL),$(PLANES))
PSFONTS      = $(addsuffix .ps,$(CJKFONTS))
PFBS	     = $(addsuffix .pfb,$(CJKFONTS))
PFAS	     = $(addsuffix .pfa,$(CJKFONTS))
AFMS	     = $(addsuffix .afm,$(CJKFONTS))
TFMS	     = $(addsuffix .tfm,$(CJKFONTS))
ifeq ($(EXTENDED_FONTS),YES)
xTFMS	     = $(addsuffix .tfm,$(addprefix $(FONTxFL),$(PLANES)))
endif

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


# 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: chk_fontmaps $(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 $@ $^


chk_fontmaps: $(GSFONTMAP) $(PSFONTMAP) generate_maps

$(GSFONTMAP):
	-$(CP) $(GSMAPDIR)/$(GSFONTMAP) .

$(PSFONTMAP):
	-$(CP) $(PSMAPDIR)/$(PSFONTMAP) .

generate_maps: 
	gen_map $(FONTNAME) $(FONTFL) $(PSFONTMAP) $(GSFONTMAP) 
  ifeq ($(EXTENDED_FONTS),YES)
	gen_map $(FONTNAME) $(FONTxFL) $(PSFONTMAP) 
  endif

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

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

$(PFBS): %.pfb : %.pfa 
	t1binary $< $@

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

$(TFMS): %.tfm : %.afm
	afm2tfm $<
  ifeq ($(EXTENDED_FONTS),YES)
	afm2tfm $< -e 1.2 $(FONTxFILE)
  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) $(GSFONTMAP) $(PSFONTMAP)
	-$(INSTALL) $(TFMS) $(TFMDIR)
	-$(INSTALL) $(PFBS) $(GSFONTDIR)
	-$(INSTALL) $(GSFONTMAP) $(GSMAPDIR)
	-$(INSTALL) $(PSFONTMAP) $(PSMAPDIR)
  ifeq ($(EXTENDED_FONTS),YES)
	-@echo "Don't forget to rename UBg5.fdx to UBg5.fd and install the" 
	-@echo "UBg5.fd after modification."
  else
	-@echo "Don't forget to install UBg5.fd after modification."
  endif

# Cleaning only .afm and .tfm files; use this if GhostScript cannot produce
# .afm from .pfb.

clean:
	-$(RM) $(AFMS)
	-$(RM) $(TFMS)
  ifeq ($(EXTENDED_FONTS),YES)
	-$(RM) $(xTFMS)
  endif

# Cleaning all produced files except executables.

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

# Clean all.

allclean: distclean
	-$(RM) *.o *.obj *.ooo *.exe
	-$(RM) ntu2cjk t1asm t1binary vmmodify
	-$(RM) $(GSFONTMAP) $(PSFONTMAP)
