使用fontforge精简字体文件
生活随笔
收集整理的這篇文章主要介紹了
使用fontforge精简字体文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里寫自定義目錄標題
- 為什么用FontForge
- FontForge精簡字體文件腳本
- Unity中調用cmd執行上述腳本
為什么用FontForge
因為它是開源免費的,而且項目中我只需要精簡字體文件,或者增刪個別字形,沒有更深層次的需求;
FontForge精簡字體文件腳本
這個是核心,用的原生腳本,也就是pe文件,你也可以自行改成python文件,我不會python;代碼簡單,主要就是傳入字符的整型值,如果是-連接的就是范圍型選擇,否則是單個選擇,然后反選,刪除自行,再生成新的字體文件(這里可能不會覆蓋同名文件,沒測試)。
#調用命令樣例: "C:\Program Files (x86)\FontForgeBuilds\bin\fontforge.exe" -script C:\Users\Mike\Documents\FontCut.pe C:\Users\Mike\Documents\Volv-Bold.ttf 65-90 128if($argc <= 2)Print("no enough args!")return(0) endifargCnt=$argc - 2 totalArray=Array(argCnt) index=2 while(index < $argc)str = StrSplit($argv[index], "-", 2)strCnt = SizeOf(str)tmpArr = Array(strCnt)if(strCnt > 0)tmpArr[0] = Strtol(str[0])endifif(strCnt > 1)tmpArr[1] = Strtol(str[1])endiftotalArray[index - 2] = tmpArr++index endloopOpen($1) Reencode("unicode")totalSize = SizeOf(totalArray) index = 0 while(index < totalSize)tmpArr = totalArray[index]tmpCnt = SizeOf(tmpArr)if(tmpCnt > 1)SelectMore(tmpArr[0], tmpArr[1])elseif(tmpCnt > 0)SelectMore(tmpArr[0])endif++index endloopSelectInvert() DetachAndRemoveGlyphs() UnlinkReference() Generate("NewOutputFont.ttf") Clear() Close()Unity中調用cmd執行上述腳本
調用cmd代碼:
private void CutFont() {var arg = GetArguments();ProcessStartInfo psInfo = new ProcessStartInfo("cmd.exe");psInfo.CreateNoWindow = true;psInfo.UseShellExecute = false;psInfo.RedirectStandardError = true;psInfo.RedirectStandardInput = true;psInfo.RedirectStandardOutput = true;psInfo.ErrorDialog = true;psInfo.Arguments = string.Format("/k {0}", arg);psInfo.StandardOutputEncoding = Encoding.UTF8;psInfo.StandardErrorEncoding = Encoding.UTF8;Process ps = Process.Start(psInfo);//不可使用writeline的方式,輸入的命令會被解析為亂碼,用上面/k的方式// ps.StandardInput.WriteLine("ln");ps.StandardInput.AutoFlush = true;ps.WaitForExit();if (ps.ExitCode == 0){Debug.Log("處理成功,請查看處理后字體文件,并進行改名等后續處理. 路徑:" + outputPath + "/NewOutputFont.ttf");}else{Debug.Log(ps.StandardOutput.ReadToEnd());}ps.Close(); }構造參數代碼:
private string GetArguments() {var result = new StringBuilder();//fontforge腳本無法指定路徑generate,所以先cd到輸出目錄result.Append(string.Format("cd \"{0}\" && ", outputPath));//加入fontforge執行程序路徑result.Append(string.Format("\"{0}\" -script", FontForgeRootPath + "/bin/fontforge.exe"));result.Append(" ");//添加精簡腳本位置參數, 為防止路徑有空格,需要用雙引號括起來result.Append(string.Format("\"{0}\"", scriptPath));result.Append(" ");//添加源字體文件位置result.Append(string.Format("\"{0}\"", sourceFontPath));result.Append(" ");//添加字符參數foreach (var characterPair in _keepCharacters){if (characterPair.Length > 1){result.Append((int)characterPair[0] + "-" + (int)characterPair[1]);}else{result.Append((int)characterPair[0]);}result.Append(" ");}//最后加上退出代碼result.Append("&& exit");return result.ToString(); }總結
以上是生活随笔為你收集整理的使用fontforge精简字体文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab-K折交叉验证与分层K折交叉
- 下一篇: 如何制作二维码定时刷新的效果?