#!/usr/bin/env fontforge -script
SetPrefs("CoverageFormatsAllowed", 1)
# usage:  fontforge -script this.pe <MPLUS_FONT>

coreName = "Ueten"
copyright = "Copyright(c) 2014 M+ FONTS PROJECT\nM+ TESTFLIGHT 058: M+ FONTS PROJECT\n"
copyright_rounded = "Rounded-X M+ 1.058.20140226: MM\n"
copyright_newfont = coreName+" "+Strftime("%Y%m%d",0,'C')+": itouhiro"
theArg = $1
argWord = StrSplit(theArg, '/')
tmp1 = argWord[SizeOf(argWord) - 1]
fontfile_mplus = Strsub(tmp1, 0, Strrstr(tmp1, "."))  #rounded-x-mplus-2m-medium
Print("fontfile_mplus:"+fontfile_mplus)


#ひらがな・カタカナのcodepoint
# あいうえお
# かきくけこ
# さしすせそ
# たちつてと
#
# なにぬねの
# はひふへほ
# まみむめも
# やゆよ
#
# らりるれろ
# わゐゑをん
# アイウエオ
# カキクケコ
#
# サシスセソ
# タチツテト
# ナニヌネノ
# ハヒフヘホ
#
# マミムメモ
# ヤユヨ
# ラリルレロ
# ワヰヱヲン
kana = [ \
0u3042, 0u3044, 0u3046, 0u3048, 0u304a, \
0u304b, 0u304d, 0u304f, 0u3051, 0u3053, \
0u3055, 0u3057, 0u3059, 0u305b, 0u305d, \
0u305f, 0u3061, 0u3064, 0u3066, 0u3068, \
\
0u306a, 0u306b, 0u306c, 0u306d, 0u306e, \
0u306f, 0u3072, 0u3075, 0u3078, 0u307b, \
0u307e, 0u307f, 0u3080, 0u3081, 0u3082, \
0u3084, 0u3086, 0u3088, \
\
0u3089, 0u308a, 0u308b, 0u308c, 0u308d, \
0u308f, 0u3090, 0u3091, 0u3092, 0u3093, \
0u30a2, 0u30a4, 0u30a6, 0u30a8, 0u30aa, \
0u30ab, 0u30ad, 0u30af, 0u30b1, 0u30b3, \
\
0u30b5, 0u30b7, 0u30b9, 0u30bb, 0u30bd, \
0u30bf, 0u30c1, 0u30c4, 0u30c6, 0u30c8, \
0u30ca, 0u30cb, 0u30cc, 0u30cd, 0u30ce, \
0u30cf, 0u30d2, 0u30d5, 0u30d8, 0u30db, \
\
0u30de, 0u30df, 0u30e0, 0u30e1, 0u30e2, \
0u30e4, 0u30e6, 0u30e8, \
0u30e9, 0u30ea, 0u30eb, 0u30ec, 0u30ed, \
0u30ef, 0u30f0, 0u30f1, 0u30f2, 0u30f3  \
]

#濁点・半濁点の縦位置微調整
adjust = [ \
 0, 20, 70, 20,  0, \
40,  0,  0,  0,  0, \
 0,100,  0,  0,  0, \
40, 20, 40,  0, 80, \
\
 0, 20,  0, 20, 40, \
40, 40, 20,120,  0, \
 0,  0,  0,  0, 40, \
40, 20, 20,  \
\
20,  0,  0, 60,  0, \
40, 70,  0,  0,120, \
 0,  0, 40,  0,  0, \
40, 40, 40, 60, 20, \
\
 0, 60,  0, 20,  0, \
20,  0,  0,  0,120, \
 0,  0,  0,  0,  0, \
40, 70,  0,120, 20, \
\
 0,  0, 80,  0,  0, \
 0,  0,  0, \
 0,  0,  0, 80,  0, \
 0,  0,  0,  0, 40  \
]

#濁点をつける文字のcodepoint
# （一部はUnicode「私用領域」U+E100-U+E22Aを使用。「GL-アンチック」フォントと同じcodepoint）
dakutenPairs = [ \
0ue100, 0ue101, 0u3094, 0ue103, 0ue104, \
0u304c, 0u304e, 0u3050, 0u3052, 0u3054, \
0u3056, 0u3058, 0u305a, 0u305c, 0u305e, \
0u3060, 0u3062, 0u3065, 0u3067, 0u3069, \
\
0ue105, 0ue106, 0ue107, 0ue108, 0ue109, \
0u3070, 0u3073, 0u3076, 0u3079, 0u307c, \
0ue10a, 0ue10b, 0ue10c, 0ue10d, 0ue10e, \
0ue10f, 0ue110, 0ue111, \
\
0ue112, 0ue113, 0ue114, 0ue115, 0ue116, \
0ue117, 0ue118, 0ue119, 0ue11a, 0ue11b, \
0ue1e0, 0ue1e1, 0u30f4, 0ue1e3, 0ue1e4, \
0u30ac, 0u30ae, 0u30b0, 0u30b2, 0u30b4, \
\
0u30b6, 0u30b8, 0u30ba, 0u30bc, 0u30be, \
0u30c0, 0u30c2, 0u30c5, 0u30c7, 0u30c9, \
0ue1e5, 0ue1e6, 0ue1e7, 0ue1e8, 0ue1e9, \
0u30d0, 0u30d3, 0u30d6, 0u30d9, 0u30dc, \
\
0ue1ea, 0ue1eb, 0ue1ec, 0ue1ed, 0ue1ee, \
0ue1ef, 0ue1f0, 0ue1f1, \
0ue1f2, 0ue1f3, 0ue1f4, 0ue1f5, 0ue1f6, \
0u30f7, 0u30f8, 0u30f9, 0u30fa, 0ue1f7  \
]

#半濁点をつける文字のcodepoint
# （一部はUnicode「私用領域」を使用。「GL-アンチック」フォントと同じcodepoint）
handakutenPairs = [ \
0ue120, 0ue121, 0ue122, 0ue123, 0ue124, \
0ue125, 0ue126, 0ue127, 0ue128, 0ue129, \
0ue12a, 0ue12b, 0ue12c, 0ue12d, 0ue12e, \
0ue12f, 0ue130, 0ue131, 0ue132, 0ue133, \
\
0ue134, 0ue135, 0ue136, 0ue137, 0ue138, \
0u3071, 0u3074, 0u3077, 0u307a, 0u307d, \
0ue139, 0ue13a, 0ue13b, 0ue13c, 0ue13d, \
0ue13e, 0ue13f, 0ue140, \
\
0ue141, 0ue142, 0ue143, 0ue144, 0ue145, \
0ue146, 0ue147, 0ue148, 0ue149, 0ue14a, \
0ue200, 0ue201, 0ue202, 0ue203, 0ue204, \
0ue205, 0ue206, 0ue207, 0ue208, 0ue209, \
\
0ue20a, 0ue20b, 0ue20c, 0ue20d, 0ue20e, \
0ue20f, 0ue210, 0ue211, 0ue212, 0ue213, \
0ue214, 0ue215, 0ue216, 0ue217, 0ue218, \
0u30d1, 0u30d4, 0u30d7, 0u30da, 0u30dd, \
\
0ue219, 0ue21a, 0ue21b, 0ue21c, 0ue21d, \
0ue21e, 0ue21f, 0ue220, \
0ue221, 0ue222, 0ue223, 0ue224, 0ue225, \
0ue226, 0ue227, 0ue228, 0ue229, 0ue22a  \
]


# open mplus font
file_in1 = theArg
Print("Open:" + file_in1)
Open(file_in1)
Reencode("unicode4")

# 横93%に縮小
SelectWorthOutputting()
foreach
    u = GlyphInfo('Unicode')
    w = GlyphInfo('Width')
    if (u >= 0)
      Select(u)
      Scale(93.0,100.0, 0,0)
      SetWidth(Int(w * 0.93))
    endif
endloop

#濁点・半濁点を拡大
//Select(0u3099) //゛
//Scale(130)
//Select(0u309a) //゜
//Scale(130)

#濁点・半濁点つき文字を生成
j=0; while (j < SizeOf(kana))
  Select(kana[j]); Copy()
  Select(dakutenPairs[j]); Paste()
  Select(0u3099); Copy() //゛
  Select(dakutenPairs[j]); PasteWithOffset(630,154-adjust[j])

  Select(kana[j]); Copy()
  Select(handakutenPairs[j]); Paste()
  Select(0u309a); Copy() //゜
  Select(handakutenPairs[j]); PasteWithOffset(630,154-adjust[j])
j+=1; endloop

# M+が既に持っているカタカナ半濁点のligatureを削除
# （build-ttf.pyのset_ccmp()でやってることを逆実行）
alreadyExistLigature = [ \
  "uni304B_uni309A", "uni304D_uni309A", "uni304F_uni309A", "uni3051_uni309A", \
  "uni3053_uni309A", "uni30AB_uni309A", "uni30AD_uni309A", "uni30AF_uni309A", \
  "uni30B1_uni309A", "uni30B3_uni309A", "uni30BB_uni309A", "uni30C4_uni309A", \
  "uni30C8_uni309A", "uni31F7_uni309A" \
]
# （M+で使われてるSubTable名）
theSubTable = "kana semi-voiced table"
j=0; while(j < SizeOf(alreadyExistLigature))
  Select(alreadyExistLigature[j] + ".liga")
  RemovePosSub(theSubTable)
  Clear()
j+=1; endloop

#濁点・半濁点つき文字にligatureを追加
Select(0u3099); glypeNameDakuten = GlyphInfo("Name") //゛
Select(0u309a); glypeNameHandakuten = GlyphInfo("Name") //゜
j=0; while(j < SizeOf(kana))
  Select(kana[j])
  glyphNameKana = GlyphInfo("Name")

  Select(dakutenPairs[j])
  AddPosSub(theSubTable, glyphNameKana+" "+glypeNameDakuten)

  Select(handakutenPairs[j])
  AddPosSub(theSubTable, glyphNameKana+" "+glypeNameHandakuten)
j+=1; endloop

#文字の縦幅（line-height 行高）を変更
SetOS2Value("WinAscentIsOffset" , 0)
SetOS2Value("WinDescentIsOffset" , 0)
SetOS2Value("HHeadAscentIsOffset" , 0)
SetOS2Value("HHeadDescentIsOffset", 0)
SetOS2Value("WinAscent" , 1330)
#SetOS2Value("WinAscent" , 1280)
SetOS2Value("WinDescent" , 220)
SetOS2Value("TypoLineGap" , 0)
SetOS2Value("HHeadAscent" , 1330)
#SetOS2Value("HHeadAscent" , 1280)
SetOS2Value("HHeadDescent", -220)
SetOS2Value("HHeadLineGap", 0)

# 文字修正
## delete '.notdef' character
Select(".notdef")
DetachAndRemoveGlyphs()

## add zero-width character
Select(0u200B) # ZERO WIDTH SPACE
SetWidth(0)
Select(0u2060) # WORD JOINER (zero width non-breaking space)
SetWidth(0)
Select(0uFEFF) # ZERO WIDTH NO-BREAK SPACE (Byte Order Mark)
SetWidth(0)

## EM DASH should be wider as HORIZONTAL BAR
Select(0u2015)
Copy()
Select(0u2014)
Paste()

## edit data
SelectWorthOutputting(); RemoveOverlap(); RoundToInt()

# NameIDs
# http://partners.adobe.com/public/developer/opentype/index_name.html#enc4
# ttfWord[0] = 'rounded'
# ttfWord[1] = 'x'
# ttfWord[2] = 'mplus'
# ttfWord[3] = '1m'
# ttfWord[4] = 'medium'
ttfWord = StrSplit(fontfile_mplus, "-")

Print("SetFontNames")
copyright = copyright+copyright_rounded+copyright_newfont           #0
family = coreName+" "+ttfWord[3]+" "+ttfWord[4]                     #1
weight = GetTTFName(0x409,2)                                        #2
uniqueID = "FontForge 2.0 : "+family+" "+ttfWord[4]+" : "+Strftime("%-d-%-m-%Y",0,'C') #3
fullFamily = family                                                 #4
#version = "1.056."+Strftime("%Y",0,'C')+"."+Strftime("%m%d",0,'C') #5
version = Strftime("%Y",0,'C')+"."+Strftime("%m%d",0,'C')           #5
postscriptName = ToLower(coreName)+"-"+ttfWord[3]+"-"+ttfWord[4]    #6
preferredFamily = coreName+" "+ttfWord[3]                           #16
#preferredSubFamily                                                 #17
newfilename = postscriptName

## rename
SetFontNames(postscriptName, family, fullFamily, weight, copyright, version)
SetTTFName(0x409, 3,  uniqueID)
SetTTFName(0x409, 5,  "Version "+version)
SetTTFName(0x409, 16, preferredFamily)
SetTTFName(0x411, 16, preferredFamily)
SetTTFName(0x409, 11, "http://mix-mplus-ipa.sourceforge.jp")
SetOS2Value("VendorID", "Mig ")

## save font
Generate(newfilename+".ttf", "", 0x84)
# Generate(newfilename+".ttf", "", 0x800084)
Print("Generate:"+newfilename+".ttf")
## End
