# $Id: Makefile,v 1.11 2003/09/10 02:54:11 s42335 Exp $
#  Makefile for Monafont TTF
#  by 1@2ch, * public domain *  2002/3

TTF_VERSION=2.90
BITMAP_VERSION=shinonome-0.9.9
BASE_OUTLINE_VERSION=kochi-subst-20030809
DATESTAMP=`date +%Y%m%d`

# tools & perl libraries

TTFTOOLS=../ttftools

PERL=perl -I$(TTFTOOLS)
CAT=cat
SED=sed
SORT=sort
UNIQ=uniq
AWK=awk
RM=rm -f
CP=cp
MV=mv
ZIP=zip -r

# pathnames

DESTDIR		=
X11BINDIR	= /usr/X11R6/bin
MKDIRHIER	= $(X11BINDIR)/mkdirhier	# 'mkdir -p' may also work.
TTFFONTDIR	= $(DESTDIR)/usr/share/fonts/ja/TrueType

MY_RPM_ROOT	= /tmp/rpm-root

# modify this:
#BASE_OUTLINE_TTF=/usr/share/fonts/ja/TrueType/kochi-gothic.ttf	# for RedHat
BASE_OUTLINE_TTF=/src/kochi-substitute/kochi-gothic-subst.ttf
MONA_OUTLINE_TTF=mona-outline.ttf

TTFPACK=$(PERL) $(TTFTOOLS)/ttfpack
TTFCAT=$(PERL) $(TTFTOOLS)/ttfcat
COMMONCHARS=$(PERL) $(TTFTOOLS)/commonchars
PICKCHARS=$(PERL) $(TTFTOOLS)/pickchars
JIS2ASCII=$(PERL) $(TTFTOOLS)/jis2ascii
JIS2SJIS=$(PERL) $(TTFTOOLS)/jis2sjis
JIS2UNICODE=$(PERL) $(TTFTOOLS)/jis2unicode
MAKE_CMAP=$(PERL) $(TTFTOOLS)/make_cmap
DISP_CMAP=$(PERL) $(TTFTOOLS)/disp_cmap
MAKE_HEAD=$(PERL) $(TTFTOOLS)/make_head
MAKE_HHEA=$(PERL) $(TTFTOOLS)/make_hhea
DISP_HHEA=$(PERL) $(TTFTOOLS)/disp_hhea
MAKE_HMTX=$(PERL) $(TTFTOOLS)/make_hmtx
DISP_HMTX=$(PERL) $(TTFTOOLS)/disp_hmtx
MAKE_HDMX=$(PERL) $(TTFTOOLS)/make_hdmx
MAKE_GASP=$(PERL) $(TTFTOOLS)/make_gasp
MAKE_VHEA=$(PERL) $(TTFTOOLS)/make_vhea
DISP_VHEA=$(PERL) $(TTFTOOLS)/disp_vhea
MAKE_VMTX=$(PERL) $(TTFTOOLS)/make_vmtx
MAKE_MAXP=$(PERL) $(TTFTOOLS)/make_maxp
DISP_MAXP=$(PERL) $(TTFTOOLS)/disp_maxp
MAKE_NAME=$(PERL) $(TTFTOOLS)/make_name
MAKE_OS2=$(PERL) $(TTFTOOLS)/make_OS_s2
MAKE_POST=$(PERL) $(TTFTOOLS)/make_post
MAKE_EBLC=$(PERL) $(TTFTOOLS)/make_EBLC
MAKE_EBDT=$(PERL) $(TTFTOOLS)/make_EBDT
MAKE_EBSC=$(PERL) $(TTFTOOLS)/make_EBSC
MAKE_LOCA_FAKE=$(PERL) $(TTFTOOLS)/make_loca_fake
REMAP_GLYF=$(PERL) $(TTFTOOLS)/remap_glyf
MERGE_GLYF=$(PERL) $(TTFTOOLS)/merge_glyf
FORCE_LOCA_LONG=$(PERL) $(TTFTOOLS)/force_loca_long
MKTESTSJIS=$(PERL) $(TTFTOOLS)/mktestsjis
MKTESTUNICODE=$(PERL) $(TTFTOOLS)/mktestunicode
GETFIELD=$(AWK) 'BEGIN{IGNORECASE=1;f=ARGV[1];ARGV[1]="";} $$1~f{print $$2;}'
GETUNIT=$(GETFIELD) unitsPerEm
GET_NUMHMETRICS=$(GETFIELD) numberOfHMetrics
GET_NUMGLYPHS=$(GETFIELD) numGlyphs
COUNTLINES=$(AWK) 'BEGIN{n=0;} {n++;} END{print n;}'
REPEATFILE=$(PERL) $(TTFTOOLS)/repeatfile
CMAP_REFORMAT=$(AWK) -F: '/^0x/ && $$2 != 0 {print $$1, $$2}'
COUNT_CHARS_FROM_LOCA=$(PERL) -e '@s=stat($$ARGV[0]); print $$s[7]/4-1,"\n";'
PLUS1=$(AWK) '{print $$1+1;}'	# very dirty...
FTVIEW=ftview			# for testing


# bdf sources

BDFSRC=../src
SRCBITA12=$(BDFSRC)/mona6x12a.bit1
SRCBITS12=$(BDFSRC)/monak12-base.bit $(BDFSRC)/monak12-ext.bit \
	  $(BDFSRC)/mona6x12.bit0 $(BDFSRC)/mona6x12r.bit1
SRCBITU12=$(BDFSRC)/monak12-uext.bit

SRCBITA14=$(BDFSRC)/mona7x14a.bit1
SRCBITS14=$(BDFSRC)/monak14-base.bit $(BDFSRC)/monak14-ext.bit \
	  $(BDFSRC)/mona7x14.bit0 $(BDFSRC)/mona7x14r.bit1
SRCBITU14=$(BDFSRC)/monak14-uext.bit

SRCBITA16=$(BDFSRC)/mona8x16a.bit1
SRCBITS16=$(BDFSRC)/monak16-base.bit $(BDFSRC)/monak16-ext.bit \
	  $(BDFSRC)/mona8x16.bit0 $(BDFSRC)/mona8x16r.bit1
SRCBITU16=$(BDFSRC)/monak16-uext.bit


###################################################

# targets

all: mona.ttf

# utilities

clean:
	-$(RM) mona.ttf *.zip *.bak *~ .*~ \
		mixed.enc ext.enc backslash.enc \
		cmap-ascii.src cmap-sjis.src cmap-unicode.src \
		base-outline-cmap base-outline-cmap-unicode.src \
		base-outline-glyf base-outline-loca base-outline-hmtx base-outline-nhmtx \
		base-outline-maxp.src base-outline-hhea.src \
		mona-outline-cmap mona-outline-cmap-unicode.src \
		mona-outline-glyf mona-outline-loca mona-outline-hmtx mona-outline-nhmtx \
		remapped-glyf remapped-loca remapped-hmtx remapped-nhmtx \
		mona12.bit mona14.bit mona16.bit \
		mona12k.bit mona14k.bit mona16k.bit \
		$(TTF_TABLES) test-sjis.html test-unicode.html

pack: mona.ttf README-ttf.txt
	$(ZIP) monafont-ttf-$(TTF_VERSION).zip README-ttf.txt mona.ttf

install: mona.ttf
	if [ ! -d $(TTFFONTDIR) ]; then \
		$(MKDIRHIER) $(TTFFONTDIR); \
	fi
	$(CP) mona.ttf $(TTFFONTDIR)

# test

test: mona.ttf
	$(FTVIEW) 16 mona.ttf
test-sjis.html: cmap-sjis.src
	$(SORT) cmap-sjis.src | $(MKTESTSJIS) > test-sjis.html
test-unicode.html: cmap-unicode.src
	$(SORT) cmap-unicode.src | $(MKTESTUNICODE) > test-unicode.html


###################################################

# ttf definitions

TTF_TABLES=cmap head hhea hmtx maxp name OS_s2 post \
	 EBLC EBDT fpgm gasp glyf loca cvt

mona.ttf: $(TTF_TABLES)
	$(TTFPACK) 0x00010000 $(TTF_TABLES) > mona.ttf


# tables

# get common characters in all point sizes
mixed.enc: $(SRCBITS12) $(SRCBITS14) $(SRCBITS16)
	$(COMMONCHARS) $(SRCBITS12) : $(SRCBITS14) : $(SRCBITS16) : > mixed.enc
UNICODE_KLUDGE=$(AWK) '{print $$1,$$2+'`$(COUNTLINES) mixed.enc`'}' # shift glyph IDs
ext.enc: mixed.enc $(SRCBITU12) $(SRCBITU14) $(SRCBITU16)
	$(COMMONCHARS) $(SRCBITU12) : $(SRCBITU14) : $(SRCBITU16) : | $(UNICODE_KLUDGE) > ext.enc
backslash.enc: mixed.enc ext.enc
	echo "0x005c" `$(COUNTLINES) mixed.enc ext.enc | $(PLUS1)` > backslash.enc

# create ascii/sjis/unicode charmap from jisx0208 encodings
cmap-ascii.src: mixed.enc
	$(JIS2ASCII) mixed.enc > cmap-ascii.src
# sjis+0x005c (JPY) is converted into u+0x00a5 (JPY).
# so we need to refer to u+0x005c (BACKSLASH).
cmap-unicode.src: mixed.enc ext.enc backslash.enc
	$(JIS2UNICODE) mixed.enc > cmap-unicode.src
	$(CAT) ext.enc backslash.enc >> cmap-unicode.src

# unify all glyphs (defaultchar, jisx0201, jisx0208) into one file
mona12.bit: mixed.enc ext.enc defaultchar.enc backslash.enc $(SRCBITA12) $(SRCBITS12) $(SRCBITU12)
	$(PICKCHARS) defaultchar.enc $(SRCBITS12) > mona12.bit
	$(PICKCHARS) mixed.enc $(SRCBITS12) >> mona12.bit
	$(PICKCHARS) ext.enc $(SRCBITU12) >> mona12.bit
	$(PICKCHARS) backslash.enc $(SRCBITA12) >> mona12.bit

mona14.bit: mixed.enc ext.enc defaultchar.enc backslash.enc $(SRCBITA14) $(SRCBITS14) $(SRCBITU14)
	$(PICKCHARS) defaultchar.enc $(SRCBITS14) > mona14.bit
	$(PICKCHARS) mixed.enc $(SRCBITS14) >> mona14.bit
	$(PICKCHARS) ext.enc $(SRCBITU14) >> mona14.bit
	$(PICKCHARS) backslash.enc $(SRCBITA14) >> mona14.bit

mona16.bit: mixed.enc ext.enc defaultchar.enc backslash.enc $(SRCBITA16) $(SRCBITS16) $(SRCBITU16)
	$(PICKCHARS) defaultchar.enc $(SRCBITS16) > mona16.bit
	$(PICKCHARS) mixed.enc $(SRCBITS16) >> mona16.bit
	$(PICKCHARS) ext.enc $(SRCBITU16) >> mona16.bit
	$(PICKCHARS) backslash.enc $(SRCBITA16) >> mona16.bit

# EBLC (Embedded Bitmap LoCation) table
EBLC: mona12.bit mona14.bit mona16.bit
	$(MAKE_EBLC) 12 mona12.bit 14 mona14.bit 16 mona16.bit > EBLC

# EBDT (Embedded Bitmap DaTa) table
EBDT: mona12.bit mona14.bit mona16.bit
	$(MAKE_EBDT) mona12.bit mona14.bit mona16.bit > EBDT

# cmap (Character MAPping) table
# 0 1 0    0 (Format0, Macintosh, Roman,    English)
# 4 3 1    0 (Format4, Microsoft, Unicode,  /)
cmap: cmap-ascii.src cmap-unicode.src
	$(MAKE_CMAP) cmap-ascii.src   0 1 0 0 \
		     cmap-unicode.src 4 3 1 0 \
		> cmap

##OBSOLETED:
# 0 1 0    0 (Format0, Macintosh, Roman,    English)
# 2 1 1    0 (Format2, Macintosh, Japanese, Japanese)
# 4 3 1    0 (Format4, Microsoft, Unicode,  /)
#cmap: cmap-ascii.src cmap-sjis.src cmap-unicode.src
#	$(MAKE_CMAP) cmap-ascii.src   0 1 0 0 \
#		     cmap-sjis.src    2 1 1 0 \
#		     cmap-unicode.src 4 3 1 0 \
#		> cmap

base-outline-hhea.src: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) hhea | $(DISP_HHEA) - > base-outline-hhea.src

base-outline-maxp.src: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) maxp | $(DISP_MAXP) - > base-outline-maxp.src

#base-outline-hmtx.src: $(BASE_OUTLINE_TTF) base-outline-hhea.src base-outline-maxp.src
#	$(TTFCAT) $(BASE_OUTLINE_TTF) hmtx | \
#	$(DISP_HMTX) - `$(GET_NUMHMETRICS) base-outline-hhea.src` `$(GET_NUMGLYPHS) base-outline-maxp.src` > \
#		base-outline-hmtx.src

base-outline-hmtx: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) hmtx > base-outline-hmtx

base-outline-cmap: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) cmap > base-outline-cmap

# we cannot replace this with a pipe, because cmap file should be seekable.
base-outline-cmap-unicode.src: base-outline-cmap
	$(DISP_CMAP) -e base-outline-cmap 0 | \
	$(CMAP_REFORMAT) > base-outline-cmap-unicode.src

base-outline-glyf: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) glyf > base-outline-glyf

base-outline-loca: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) loca | $(FORCE_LOCA_LONG) - > base-outline-loca

base-outline-nhmtx: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) hhea | $(DISP_HHEA) - | $(GET_NUMHMETRICS) > base-outline-nhmtx

# remapp base glyphs
remapped-glyf: base-outline-glyf base-outline-loca base-outline-hmtx base-outline-nhmtx base-outline-cmap-unicode.src cmap-unicode.src
	$(REMAP_GLYF) g base-outline-glyf base-outline-loca base-outline-hmtx `$(CAT) base-outline-nhmtx` \
			base-outline-cmap-unicode.src cmap-unicode.src > remapped-glyf

remapped-loca: base-outline-glyf base-outline-loca base-outline-hmtx base-outline-nhmtx base-outline-cmap-unicode.src cmap-unicode.src
	$(REMAP_GLYF) l base-outline-glyf base-outline-loca base-outline-hmtx `$(CAT) base-outline-nhmtx` \
			base-outline-cmap-unicode.src cmap-unicode.src > remapped-loca

remapped-hmtx: base-outline-glyf base-outline-loca base-outline-hmtx base-outline-nhmtx base-outline-cmap-unicode.src cmap-unicode.src
	$(REMAP_GLYF) h base-outline-glyf base-outline-loca base-outline-hmtx `$(CAT) base-outline-nhmtx` \
			base-outline-cmap-unicode.src cmap-unicode.src > remapped-hmtx

remapped-nhmtx: remapped-loca
	$(COUNT_CHARS_FROM_LOCA) remapped-loca > remapped-nhmtx

#
mona-outline-cmap: $(MONA_OUTLINE_TTF)
	$(TTFCAT) $(MONA_OUTLINE_TTF) cmap > mona-outline-cmap

mona-outline-cmap-unicode.src: mona-outline-cmap
	$(DISP_CMAP) -e mona-outline-cmap 0 | \
	$(CMAP_REFORMAT) > mona-outline-cmap-unicode.src

mona-outline-glyf: $(MONA_OUTLINE_TTF)
	$(TTFCAT) $(MONA_OUTLINE_TTF) glyf > mona-outline-glyf

mona-outline-loca: $(MONA_OUTLINE_TTF)
	$(TTFCAT) $(MONA_OUTLINE_TTF) loca | $(FORCE_LOCA_LONG) - > mona-outline-loca

mona-outline-hmtx: $(MONA_OUTLINE_TTF)
	$(TTFCAT) $(MONA_OUTLINE_TTF) hmtx > mona-outline-hmtx

mona-outline-nhmtx: $(MONA_OUTLINE_TTF)
	$(TTFCAT) $(MONA_OUTLINE_TTF) hhea | $(DISP_HHEA) - | $(GET_NUMHMETRICS) > mona-outline-nhmtx

glyf: remapped-glyf remapped-loca remapped-hmtx remapped-nhmtx cmap-unicode.src mona-outline-glyf mona-outline-loca mona-outline-hmtx mona-outline-nhmtx mona-outline-cmap-unicode.src
#	$(MERGE_GLYF) g remapped-glyf remapped-loca cmap-unicode.src > glyf
	$(MERGE_GLYF) g remapped-glyf remapped-loca remapped-hmtx `$(CAT) remapped-nhmtx` cmap-unicode.src \
			mona-outline-glyf mona-outline-loca mona-outline-hmtx `$(CAT) mona-outline-nhmtx` mona-outline-cmap-unicode.src > glyf

loca: remapped-glyf remapped-loca remapped-hmtx remapped-nhmtx cmap-unicode.src mona-outline-glyf mona-outline-loca mona-outline-hmtx mona-outline-nhmtx mona-outline-cmap-unicode.src
#	$(MERGE_GLYF) l remapped-glyf remapped-loca cmap-unicode.src > loca
	$(MERGE_GLYF) l remapped-glyf remapped-loca remapped-hmtx `$(CAT) remapped-nhmtx` cmap-unicode.src \
			mona-outline-glyf mona-outline-loca mona-outline-hmtx `$(CAT) mona-outline-nhmtx` mona-outline-cmap-unicode.src > loca

hmtx: remapped-glyf remapped-loca remapped-hmtx remapped-nhmtx cmap-unicode.src mona-outline-glyf mona-outline-loca mona-outline-hmtx mona-outline-nhmtx mona-outline-cmap-unicode.src
	$(MERGE_GLYF) h remapped-glyf remapped-loca remapped-hmtx `$(CAT) remapped-nhmtx` cmap-unicode.src \
			mona-outline-glyf mona-outline-loca mona-outline-hmtx `$(CAT) mona-outline-nhmtx` mona-outline-cmap-unicode.src > hmtx

# hhea (Horizontal metrics table HEAder) table
hhea: base-outline-hhea.src remapped-nhmtx
	$(MAKE_HHEA) base-outline-hhea.src numberOfHMetrics=`$(CAT) remapped-nhmtx` > hhea

# gasp (Grid-fitting And Scan-conversion Procedure)
gasp:
#	$(MAKE_GASP) 9 3 24 1 65535 3 > gasp
	$(MAKE_GASP) 65535 3 > gasp

# cvt (Control Value Table) - simply copying
cvt: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) 'cvt ' > cvt

# fpgm (Font ProGraM) - simply copying
fpgm: $(BASE_OUTLINE_TTF)
	$(TTFCAT) $(BASE_OUTLINE_TTF) fpgm > fpgm

# head (TTF HEADer) table
head: head.src
	$(MAKE_HEAD) head.src checkSumAdjustment=0 > head

# maxp (MAXimum Profile) table
maxp: base-outline-maxp.src loca
	$(MAKE_MAXP) base-outline-maxp.src numGlyphs=`$(COUNT_CHARS_FROM_LOCA) remapped-loca | $(PLUS1)` > maxp

# OS/2 table
OS_s2: OS_s2.src
	$(MAKE_OS2) OS_s2.src > OS_s2

# post (POSTscript) table
post: post.src
	$(MAKE_POST) post.src > post

# EBSC (Embedded Bitmap Scaling Control) table
EBSC: EBSC.src
	$(MAKE_EBSC) EBSC.src > EBSC

# name table
name: name.src
	$(SED) "s/@@VERSION@@/$(TTF_VERSION)/g; \
		s/@@DATESTAMP@@/$(DATESTAMP)/g; \
		s/@@BITMAP_VERSION@@/$(BITMAP_VERSION)/g; \
		s/@@BASE_OUTLINE_VERSION@@/$(BASE_OUTLINE_VERSION)/g; " name.src | \
		$(MAKE_NAME) - > name
