setlocal EnableExtensions EnableDelayedExpansion
@rem set PATH=%~dp0cygwin\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\Wbem
@rem set PATH=D:\Programs\fontforge-cygwin_2012_08_02\cygwin\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\Wbem;%PATH%
set PATH_PRE=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\Wbem;%PATH%
@rem Crashes, infinite loops:
@rem set FontForge_Version=2016_05_26
set FontForge_Version=2014_01_04
@rem The version of 2014 cannot merge files and generate: it omits FullName!
set FontForge_Version_Finish=2016_05_26
set PATH=D:\Programs\fontforge-cygwin_%FontForge_Version%\cygwin\bin;%PATH_PRE%
set CYGWIN=nodosfilewarning
set HOME=%~dp0
set LANG=C.UTF-8
set TZ=
set DISPLAY=:9.0
set AUTOTRACE=potrace
set unifontBindir=D:\ilya\unifont\
set unifontVer=9.0.01
set dejavuVer=2.36
@rem fontforge -lang=ff -c "Open($1); SelectAll(); ExpandStroke(64,1,1); RemoveOverlap(); SetFontOrder(2); Simplify(128,2.05); Simplify(128,2.05); RoundToInt(); RoundToInt(); Save($2);" %1 out-%1
@rem fontforge --help

@rem fontforge -lang=ff -c "Open($1); SelectAll(); ExpandStroke(64,1,1); RemoveOverlap(); SetFontOrder(2); Simplify(128,2.05); Simplify(128,2.05); RoundToInt(); RoundToInt(); AutoHint(); PrivateGuess(\"BlueScale\"); PrivateGuess(\"BlueValues\"); PrivateGuess(\"OtherBlues\"); PrivateGuess(\"StdHW\"); PrivateGuess(\"StdVW\"); PrivateGuess(\"StemSnapH\"); PrivateGuess(\"StemSnapV\"); AutoHint(); AutoInstr(); Save($2); Generate($3);" %1 out-%1 out-%1.ttf

@rem usage: %0 D:\tmp\unifoundry-5.1.20080907\font\precompiled\unifont.hex unifont-massaged
@rem D:\ilya\unifont\fontforge-make-font.cmd D:\tmp\unifoundry-5.1.20080907\font\precompiled\unifont.hex unifont-mono "--overridefile=D:\ilya\unifont\sub-super-braces.hex" "--wideskiprex=3[3-9a-f]..|4[0-9a-cef]..|4d[0-9ab].|[5-9]...|a[c-f]..|[b-e]...|f[0-9a].." "--skiprex=000[01]" --mono 2>&1 | tee 00do-mono
@rem usage: %0 FROM_FILE.HEX TO_FILE_BASENAME [--OPTIONS]
set from=%1
shift
set to=%1
shift
set nomono=
set do_Finish=

set _To_Process=0 1 2 3 4 5 6 7 8 9 a b c d e f .n
set To_Process=%_To_Process%
set doMergeFonts=MergeFonts($1 + '0.sfd'); MergeFonts($1 + '1.sfd'); MergeFonts($1 + '2.sfd'); MergeFonts($1 + '3.sfd'); MergeFonts($1 + '4.sfd'); MergeFonts($1 + '5.sfd'); MergeFonts($1 + '6.sfd'); MergeFonts($1 + '7.sfd'); MergeFonts($1 + '8.sfd'); MergeFonts($1 + '9.sfd'); MergeFonts($1 + 'a.sfd'); MergeFonts($1 + 'b.sfd'); MergeFonts($1 + 'c.sfd'); MergeFonts($1 + 'd.sfd'); MergeFonts($1 + 'e.sfd'); MergeFonts($1 + 'f.sfd');
@rem width=64 triggers a bug in fontforge, when an internal contour is reverted, causing a filled loop
@rem 64.1 works around most of these problems, except for uni25a6, which needs this:
set StrokeWidth=64.545
set doMerge=

:do_more_rounds
set more_rounds=
if .%1. == .--only-0. set To_Process=0 .n
if .%1. == .--only-0. set DoMergeFonts=MergeFonts($1 + '0.sfd'); 
if .%1. == .--only-0. set more_rounds=1
if .%1. == .--only-0. shift
if .%1. == .--only-1. set To_Process=1 .n
if .%1. == .--only-1. set DoMergeFonts=MergeFonts($1 + '1.sfd'); 
if .%1. == .--only-1. set more_rounds=1
if .%1. == .--only-1. shift
if .%1. == .--only-3. set To_Process=3 .n
if .%1. == .--only-3. set DoMergeFonts=MergeFonts($1 + '3.sfd'); 
if .%1. == .--only-3. set more_rounds=1
if .%1. == .--only-3. shift
if .%1. == .--only-a. set To_Process=a .n
if .%1. == .--only-a. set DoMergeFonts=MergeFonts($1 + 'a.sfd'); 
if .%1. == .--only-a. set more_rounds=1
if .%1. == .--only-a. shift

if .%1. == .--finish. set do_Finish=yes
if .%1. == .--finish. set more_rounds=1
if .%1. == .--finish. shift

if .%1. == .--useUpperPlanes. set doUpperPlanes=1
if .%1. == .--useUpperPlanes. set more_rounds=1
if .%1. == .--useUpperPlanes. shift

if not .%1. == .--stroke. goto endstroke
shift
set StrokeWidth=%1
shift
set doMerge=1
set more_rounds=1
:endstroke

if .%more_rounds%. == .1. goto do_more_rounds


set is_mono=0
perl -wle "$_ eq q(--mono) and exit 0 for @ARGV; exit 1" -- %1 %2 %3 %4 %5 %6 %7 %8 %9 && set is_mono=1
if %is_mono% == 1 copy %unifontBindir%base.fea %to%-base.fea

for /f "usebackq" %%a in (`perl -C31 %unifontBindir%hex_parse.pl --version`) do set parser_version=%%a

if .%doUpperPlanes%. == .. goto noUpperPlanes
set To_Process=%To_Process% 01.... 0E....
set doMergeFonts=%doMergeFonts% MergeFonts($1 + '01.....sfd'); MergeFonts($1 + '0E.....sfd');
:noUpperPlanes

if .%do_Finish%. == .yes. set To_Process=

@rem Memory required for individual files: up to 2.7GB; final merge needs 950MB (.n: ff is buggy mergefont()ing .notdef...)
for %%i in ( %To_Process% ) do (
  @rem create a dummy font without Lookup's
  if "%%i" == ".n" set nomono=--nolayouts
  perl -C31 %unifontBindir%hex_parse.pl --oversion=%unifontVer% --manglenotdef --sfd --only=%%i !nomono! %1 %2 %3 %4 %5 %6 %7 %8 %9 %from% >%to%-x%%i.sfd
	@rem Having AutoHints() disables showing of the font in UniScribe applications (work in GDI)
  @rem fontforge -lang=ff -c "Open($1); SelectAll(); ExpandStroke(64,1,1,0,1); RemoveOverlap(); SetFontOrder(2); Simplify(128,2.05); Simplify(128,2.05); RoundToInt(); RoundToInt(); AutoHint(); PrivateGuess('BlueScale'); PrivateGuess('BlueValues'); PrivateGuess('OtherBlues'); PrivateGuess('StdHW'); PrivateGuess('StdVW'); PrivateGuess('StemSnapH'); PrivateGuess('StemSnapV'); AutoHint(); AutoInstr(); Save($2);" %to%-x%%i.sfd out-%to%-x%%i.sfd
	@rem With no hints at all, the middle horizontal strokes of a/e disappear in size 8=11px (when 12=16px), dots in i/j disappear in size 6=8px; not very readable in size
  @rem fontforge -lang=ff -c "Open($1); SelectAll(); ExpandStroke(64,1,1,0,1); RemoveOverlap(); SetFontOrder(2); Simplify(128,2.05); Simplify(128,2.05); RoundToInt(); RoundToInt(); Save($2);" %to%-x%%i.sfd out-%to%-x%%i.sfd
	@rem   Discussion in the first comment to
	@rem	http://code.google.com/p/googlefontdirectory/wiki/HowToGenerateWebNativeFonts
	@rem		(also discusses DirectWrite vs GDI).
		@rem	not very readable in size 5=7px, bat at size 6=8px the LGC are clearly distinguishable
       fontforge -lang=ff -c "Open($1); SelectAll(); ExpandStroke(%StrokeWidth%,1,1,0,1); RemoveOverlap(); SetFontOrder(2); Simplify(128,2.05); Simplify(128,2.05); RoundToInt(); RoundToInt(); AddInstrs('prep', 1, 'PUSHW_1 511 SCANCTRL PUSHB_1 4 SCANTYPE'); Save($2);" %to%-x%%i.sfd out-%to%-x%%i.sfd
		@rem Stuff checked at 96dpi
  @rem SelectAll() does not cover "Encoding: -1 -1"; so we mangle it first, then restore back (TT instructions need binary mode!!!)
  if "%%i" == ".n" perl -i~ -wlpe "binmode ARGVOUT; s/(^Encoding:\s+)408\d(?=\s+-1\s)/$1-1/" out-%to%-x%%i.sfd
  if "%%i" == "0"  perl -i~ -wlpe "binmode ARGVOUT; s/(^Encoding:\s+)408\d(?=\s+-1\s)/$1-1/" out-%to%-x%%i.sfd
)

set doFeatures=
set ifNotBuggy=
if %is_mono% == 0 goto no_feature
@perl -wlne "BEGIN {my $b=shift; for (@ARGV) {$_ = qq($b$_.sfd)}} sub fmt {my $fill=shift; print qq($fill), join(q( ), splice @_, 0, 10) while @_} $m{$1}++ if /^#\s*is_comb\s+(\w+)/; END {print qq(\@combs = [); fmt q(  ), map qq(\\$_), sort keys %%m; print qq(];\n);}" %to%-x 0 1 2 3 4 5 6 7 8 9 a b c d e f .n >combsDef.fea
@rem 
set doFeatures=MergeFeature($2 + '-base.fea');
set ifNotBuggy=if not $c or $c ne 1
:no_feature

set PATH=D:\Programs\fontforge-cygwin_%FontForge_Version_Finish%\cygwin\bin;%PATH_PRE%

@rem Do merge ALL fonts with the dummy (this way Lookups are treated symmetrically, and do not use different names:
fontforge -lang=ff -c "Open($1 + '.n.sfd'); %doMergeFonts% Save($2);" out-%to%-x out-%to%.sfd
@rem If U+0001 is present, console uses it instead of .notdef
perl -i~ -wlpe "binmode ARGVOUT; $c=$2, next if /^StartChar:\s+((?=uni000(\d)\b)?[.\w]+)/; s/^(Encoding:\s+)6553\d\s+-1(?=\s)/$1-1 $c/ %ifNotBuggy%; s/^(Encoding:\s*)UnicodeBmp\b/${1}Custom/" out-%to%.sfd
fontforge -lang=ff -c "Open($1); %doFeatures% Generate($2 + '.ttf', '',0x4000);" out-%to%.sfd %to%

@rem 1.81 generates about 105MB of SFDs; they are converted to 400MB of expanded SFDs, merged into 400MB one
@rem TTF of 53MB is generated

if .%doMerge%. == .1. (
  @rem fontforge  -lang=ff -c "Open($1); ScaleToEm(1976,490); SelectAll(); Scale(100,90,0,0); RoundToInt(); RoundToInt(); Generate($2)" %to%.ttf scaled-%to%.ttf
  fontforge  -lang=ff -c "Open($1); ScaleToEm(1976,490); RoundToInt(); RoundToInt(); Generate($2)" %to%.ttf scaled-%to%.ttf
  @rem ≃ U+2243 is not shown well on Windows console on 16×8 grid
  @rem ⋢ 22e2 ⋣ 22e3 wrong glyphs
     @rem From README (removed cmap): U+066E, U+066F, U+067C, U+067D, U+0681, U+0682, U+0685, U+0692, U+06A1, U+06B5, U+06BA, U+06C6, U+06CE, and U+06D5
  @rem    cannot include combining chars: 'uni0308','uni030D','uni0315','uni0317','uni0318','uni0319','uni031C','uni031D','uni031F','uni0323','uni0324','uni0326','uni0329','uni032C','uni032E','uni0339','uni033D','uni0358'
     @rem Somehow the version is not changed by this (and similar) commands
       @rem Sometimes font previewer shows empty font preview; installing works fine (and installed version previews fine); renaming the file makes it previewable
		@rem This command is not working: traces of 06** remain, and make "Unencoded Unicode" of added replacements
@rem  fontforge  -lang=ff -c "Open($1); SelectSingletons('.notdef','uni223C','uni2243','uni27C2','uni208C','uni00A8','uni02CF','uni02D3','uni02D6','uni02D9','uniA71C'); Clear(); SelectSingletons('uni22E2','uni22E3','uni066E','uni066F','uni067C','uni067D','uni0681','uni0682','uni0685','uni0692','uni06A1','uni06B5','uni06BA','uni06C6','uni06CE','uni06D5'); Clear(); MergeFonts($2); SetFontNames('DejaVuSansMonoUnifont', 'DejaVu Sans Mono Unifont', 'DejaVu Sans Mono Unifont','','','%dejavuVer%-%unifontVer%-%parser_version%'); Generate($3)" ../DejaVuSansMono.ttf scaled-%to%.ttf merged-%to%.ttf
  fontforge  -lang=ff -c "Open($1); SelectSingletons('uni066E','uni066F','uni067C','uni067D','uni0681','uni0682','uni0685','uni0692','uni06A1','uni06B5','uni06BA','uni06C6','uni06CE','uni06D5'); Clear(); Generate($2)" ../DejaVuSansMono.ttf DejaVuSansMono-omit.ttf 
  fontforge  -lang=ff -c "Open($1); SelectSingletons('.notdef','uni223C','uni2243','uni27C2','uni208C','uni00A8','uni02CF','uni02D3','uni02D6','uni02D9','uniA71C','uni22E2','uni22E3'); Clear(); MergeFonts($2); SetFontNames('DejaVuSansMonoUnifont', 'DejaVu Sans Mono Unifont', 'DejaVu Sans Mono Unifont','','','%dejavuVer%-%unifontVer%-%parser_version%'); Generate($3,'',0x4000)" DejaVuSansMono-omit.ttf scaled-%to%.ttf merged-%to%.ttf
@rem  fontforge  -lang=ff -c "Open($1); Select('uni223C','uni2243','uni27C2'); Clear(); MergeFonts($2); SetFontNames('DejaVuSansMonoUnifont', 'DejaVu Sans Mono Unifont', 'DejaVu Sans Mono Unifont','','','2.34; 6.3.20140202--1.93'); Generate($3)" ../DejaVuSansMono.ttf scaled-%to%.ttf merged-%to%.ttf
)
