Skip to content

Commit

Permalink
Add Italic support.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiWonder committed May 31, 2023
1 parent 0be5225 commit 48e6f91
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
| simsunb | 宋体-ExtB |

- `-wt` 字重(Weight),可选,可使用 `"Thin", "ExtraLight", "Light", "Semilight", "DemiLight", "Normal", "Regular", "Medium", "Demibold", "SemiBold", "Bold", "Black", "Heavy"`。如未指定字重,程序会自动判断字重。
- `-it` 斜体(Italic),可选,可使用 `-it y` 指定为斜体字体,`-it n` 指定为非斜体字体。如未指定,程序会使用输入字体的斜体属性。
- `-d` 字体保存目录(Output Directory),可选,如未指定,则使用当前目录。
- `-r` TTC 打包完成后移除 TTF,可选。

Expand Down
72 changes: 50 additions & 22 deletions gui/toWinFonts/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions gui/toWinFonts/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private void FormMain_Load(object sender, EventArgs e)
CheckForIllegalCrossThreadCalls = false;
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
}

private bool IsInPATH(string command)
Expand Down Expand Up @@ -79,6 +80,14 @@ private void ButtonStart_Click(object sender, EventArgs e)
{
args += " -r";
}
if (comboBox3.SelectedIndex == 1)
{
args += " -it y";
}
else if (comboBox3.SelectedIndex == 2)
{
args += " -it n";
}
panelMain.Enabled = false;
Cursor = Cursors.WaitCursor;
err = "";
Expand Down
63 changes: 60 additions & 3 deletions main/winfont.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
otfccbuild = os.path.join(pydir, 'otfcc/otfccbuild')
otf2otc = os.path.join(pydir, 'otf2otc.py')
outd=str()
it='a'
rmttf=False
if platform.system() in ('Mac', 'Darwin'):
otfccdump += '1'
Expand All @@ -31,9 +32,20 @@ def setuswt(font, wt):
uswt={'thin':100, 'extralight':250, 'light':300, 'semilight':350, 'demilight':350, 'normal':350, 'regular':400, 'medium':500, 'demibold':600, 'semibold':600, 'bold':700, 'black':900, 'heavy':900}
font['OS_2']['usWeightClass']=uswt[wt]
font['OS_2']['fsSelection']['bold']=wt=='bold'
font['OS_2']['fsSelection']['regular']=wt=='regular'
font['OS_2']['fsSelection']['regular']=(wt=='regular' and it!='y')
font['head']['macStyle']['bold']=wt=='bold'

def getit(font):
if 'macStyle' in font['head'] and 'italic' in font['head']['macStyle'] and font['head']['macStyle']['italic']:
return 'y'
return 'n'

def setit(font, isit):
font['OS_2']['fsSelection']['italic']=isit
if isit:
font['OS_2']['fsSelection']['regular']=False
font['head']['macStyle']['italic']=isit

def getver(nmo):
for n1 in nmo:
if n1['languageID']==1033 and n1['nameID']==5:
Expand Down Expand Up @@ -64,6 +76,32 @@ def wtbuil(nml, wt):
nwtnm.append(n2)
return nwtnm

def itbuil(nms):
nwtnm=list()
isbold=False
for n1 in nms:
if n1['nameID']==2 and 'Bold' in n1['nameString']:
isbold=True
break
for n1 in nms:
n2=dict(n1)
if n2['nameID']==2:
if 'Italic' in n2['nameString']:
return nms
if isbold:
n2['nameString']='Bold Italic'
else:
n2['nameString']='Italic'
elif n2['nameID'] in (3, 4, 17):
n2['nameString']+=' Italic'
elif n2['nameID']==6:
if '-' in n2['nameString']:
n2['nameString']+='Italic'
else:
n2['nameString']+='-Italic'
nwtnm.append(n2)
return nwtnm

def bldttfft(font, tgft, wt):
ncfg=json.load(open(os.path.join(pydir, f'names/{tgft}.json'), 'r', encoding = 'utf-8'))
font['OS_2']['ulCodePageRange1']=ncfg['ulCodePageRange1']
Expand All @@ -73,6 +111,9 @@ def bldttfft(font, tgft, wt):
if wt not in wts: nmslist=wtbuil(ncfg[tgft+'l'], wt)
else: nmslist=ncfg[tgft+end[wt]]
ttflist=otpth(tgft+end[wt]+'.ttf')
if it=='y':
nmslist=itbuil(nmslist)
ttflist=otpth(tgft+end[wt]+'It.ttf')
font['head']['fontRevision']=float(getver(nmslist))
font['name']=nmslist
print('正在生成字体...')
Expand All @@ -87,6 +128,7 @@ def bldttcft(font, tgft, wt):
ncfg=json.load(open(os.path.join(pydir, f'names/{tgft}.json'), 'r', encoding = 'utf-8'))
font['OS_2']['ulCodePageRange1']=ncfg['ulCodePageRange1']
spwt=dict()
isit=it=='y'
if tgft in ('msyh', 'msjh', 'meiryo'):
if wt not in ('Regular', 'Bold', 'Light'):
nmslist=[wtbuil(ncfg[tgft+'l'], wt), wtbuil(ncfg[tgft+'ui'+'l'], wt)]
Expand Down Expand Up @@ -162,6 +204,10 @@ def bldttcft(font, tgft, wt):
nmslist=[wtbuil(ncfg['yugothl'], wt), wtbuil(ncfg['yugothuil'], wt)]
ttflist=[otpth('YuGoth'+end[wt].upper()+'.ttf'), otpth('YuGothui'+end[wt].upper()+'.ttf')]
ttcfil=otpth('YuGoth'+end[wt].upper()+'.ttc')
if isit:
nmslist=[itbuil(nm) for nm in nmslist]
ttflist=[ttfl.replace('.ttf', 'It.ttf') for ttfl in ttflist]
ttcfil=ttcfil.replace('.ttc', 'It.ttc')
print('正在生成字体...')
tmpf=list()
wtcls=font['OS_2']['usWeightClass']
Expand Down Expand Up @@ -191,7 +237,7 @@ def bldttcft(font, tgft, wt):
print('完成!')

def parseArgs(args):
global outd, rmttf
global outd, rmttf, it
inFilePath, outDir, tarGet, weight=(str() for i in range(4))
i, argn = 0, len(args)
while i < argn:
Expand All @@ -206,6 +252,9 @@ def parseArgs(args):
elif arg == "-wt":
weight = args[i]
i += 1
elif arg == "-it":
it = args[i]
i += 1
elif arg == "-tg":
tarGet = args[i].lower()
i += 1
Expand All @@ -221,9 +270,12 @@ def parseArgs(args):
raise RuntimeError(f"You must specify target.{TG}")
elif tarGet not in TG:
raise RuntimeError(f"Unknown target \"{tarGet}\",please use {TG}.\n")

if it.lower() not in ('a', 'y', 'n'):
raise RuntimeError(f'Unknown italic setting "{it}",please use "y" or "n".\n')
if weight:
if weight.lower() not in WT:
raise RuntimeError(f'Unknown weight "{weight}",please use {tuple(end.keys())}\n')
raise RuntimeError(f'Unknown weight "{weight}",please use {tuple(end.keys())}.\n')
weight=weight.lower()
if weight=='extralight': weight='ExtraLight'
elif weight=='semibold': weight='SemiBold'
Expand All @@ -237,9 +289,14 @@ def parseArgs(args):
return inFilePath, tarGet, weight

def run(args):
global it
ftin, tg, setwt=parseArgs(args)
print('正在载入字体...')
font = json.loads(subprocess.check_output((otfccdump, '--no-bom', ftin)).decode("utf-8", "ignore"))
if it=='a':
it=getit(font)
else:
setit(font, it=='y')
if not setwt:
setwt=getwt(font)
else:
Expand Down

0 comments on commit 48e6f91

Please sign in to comment.