Last active
March 7, 2024 01:09
-
-
Save sttk3/8dd9f59d6dfcfeed1c50f0a0a5edabac to your computer and use it in GitHub Desktop.
Illustratorの合成フォントの内訳PostScript Nameを取得し、alertで表示するサンプル用ExtendScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file Illustratorの合成フォントの内訳PostScript Nameを取得し、alertで表示するサンプル用ExtendScript | |
* @version 1.0.1 | |
* @author sttk3.com | |
* @copyright © 2024 sttk3.com | |
*/ | |
//@target 'illustrator' | |
(function() { | |
try { | |
// 合成フォントファイルはここで自分で指定する設定 | |
var filePath = '~/Library/Application Support/Adobe/Adobe Illustrator 28/ja_JP/合成フォント/合成フォントファイル名' ; | |
var compositeFontFile = new File(filePath) ; | |
var fontNames = childFontNames(compositeFontFile) ; | |
alert(fontNames.join('\n')) ; | |
} catch(e) { | |
alert(e) ; | |
} | |
})() ; | |
/** | |
* 合成フォントに使われているフォントのPostScript Nameの配列を返す | |
* @param {File} compositeFontFile 合成フォントのファイル | |
* @return {Array<string?>} [漢字, かな, 全角約物, 全角記号, 半角欧文, 半角数字, 特例文字...] | |
*/ | |
function childFontNames(compositeFontFile) { | |
// これ以上読む必要がなくなる行の正規表現パターン | |
var patternEnd = /^%%IncludeResource:/i ; | |
// フォント名がある行の正規表現パターン。$1にPostScript Nameが入る | |
var patternFontName = /^%%\+ Font (.+?)(?:-UniJIS-UTF16-H)?$/i ; | |
var resultArray = [] ; | |
try { | |
var encoding = 'binary' ; | |
compositeFontFile.encoding = encoding ; | |
compositeFontFile.open('r') ; | |
// 合成フォントファイルの場合、先頭が'typ1'で始まる。そうでない場合はエラーを出す | |
var msgNoCompositeFont = 'Specify a composite font file.' ; | |
var fileLength = compositeFontFile.length ; | |
if(fileLength <= 4) { | |
throw new Error(msgNoCompositeFont) ; | |
} | |
var marker = compositeFontFile.read(4, encoding) ; | |
if(marker != 'typ1') { | |
throw new Error(msgNoCompositeFont) ; | |
} | |
// 合成フォントファイルを1行ずつ読んで解析する。 | |
// 1600byteは、このあたりを超えてフォント名が見つからなかったら多分合成フォントファイルではない、という早期離脱のためのマジックナンバー | |
var currentLine ; | |
while( !compositeFontFile.eof && (compositeFontFile.tell() <= 1600) ) { | |
currentLine = compositeFontFile.readln() ; | |
if(patternEnd.test(currentLine)) { | |
// 次の行を読む必要がなくなったらbreakする | |
break ; | |
} else { | |
// フォント名が取得できたらresultArrayに追加して次の行へ | |
var matchObj = currentLine.match(patternFontName) ; | |
if(matchObj) { | |
resultArray.push(matchObj[1]) ; | |
} | |
} | |
} | |
} catch(e) { | |
throw new Error(e) ; | |
} finally { | |
try {compositeFontFile.close() ;} catch(e) {} | |
} | |
return resultArray ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment