Last active
September 6, 2021 10:24
-
-
Save monokano/b0de42939e8ce3efde8333b7656b5856 to your computer and use it in GitHub Desktop.
InDesignでCID数字列をグリフにするJavaScript
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
/* | |
#################################################################### | |
CID2Glyph.jsx | |
InDesign用JavaScript。 | |
indd上のCID数字列をそのグリフにします。 | |
選択範囲内を処理対象にします。 | |
要件1 選択範囲内の文字列に「cid数字列」または「\数字列」がある。例)cid8017, \8017 | |
要件2 そのCID数字列に「そのCIDが存在するフォント」が適用されている。 | |
要件3 そのフォントのcid+1は欧文スペース(U+0020)である。 | |
#################################################################### | |
*/ | |
if (app.documents.length === 0) {exit();} | |
var myDoc = app.documents[0]; | |
var mySelections = myDoc.selection; | |
var mySelLength = mySelections.length; | |
if (mySelLength === 0) { | |
alert("選択してから実行してください。"); | |
exit(); | |
} | |
// 検索置換設定を整える | |
prepareFindChangeGrepOptions(); | |
// 実行 | |
for (var i=0; i<mySelLength; i++) { | |
runCID2Char(mySelections[i]) | |
} | |
// 後片付け | |
resetFindChangeGrepOptions() | |
// 終了通知 | |
alert("終了しました。"); | |
/////////////////////////////////////////////////////////////// | |
// function グリフ検索置換 | |
function runCID2Char(aSelection) { | |
var regSearchObj = {findWhat:"(cid|\\\\)[0-9]+"}; // | |
var regMachObj = regexFind(aSelection, regSearchObj); | |
var regMachObjLength = regMachObj.length; | |
if (regMachObjLength > 0) { | |
for (var i=0; i<regMachObjLength; i++) { | |
var aMachObj = regMachObj[i]; | |
// フォントを取得しておく | |
var myFontObj = aMachObj.characters[0].appliedFont; | |
// 欧文スペースに置換する | |
regexFindChange(aMachObj, "(cid|\\\\)", " ") | |
// 欧文スペースでsplitする | |
var arrTarget = aMachObj.contents.split(" "); | |
// CIDを取得 | |
var myCID = arrTarget[1]; | |
// 数字を削除する(aMachObjを欧文スペースのみにする) | |
regexFindChange(aMachObj, "[0-9]+", "") | |
// 取得したCIDに置換する | |
changeCIDtoGlyph(aMachObj, myFontObj, myCID) | |
} | |
} | |
} | |
function changeCIDtoGlyph(my_TextObj, my_FontObj, my_CID) { | |
try{ | |
// グリフ検索置換 | |
app.findGlyphPreferences.appliedFont = my_FontObj; | |
app.findGlyphPreferences.glyphID = 1; //欧文スペースのCID | |
app.changeGlyphPreferences.appliedFont = my_FontObj; | |
app.changeGlyphPreferences.glyphID = parseInt(my_CID, 10); | |
my_TextObj.changeGlyph(); | |
}catch(e){}; | |
} | |
/////////////////////////////////////////////////////////////// | |
// function その他 | |
function prepareFindChangeGrepOptions() { | |
//検索置換設定を整える | |
app.findGrepPreferences = NothingEnum.nothing; | |
app.changeGrepPreferences = NothingEnum.nothing; | |
with (app.findChangeGrepOptions) { | |
includeLockedLayersForFind = false; //ロックされたレイヤーとオブジェクト | |
includeLockedStoriesForFind = false; //ロックされたストーリー | |
includeHiddenLayers = false; //非表示のレイヤーとオブジェクト | |
includeMasterPages = false; //マスターページ | |
includeFootnotes = false; //脚注 | |
kanaSensitive = true; //平仮名と片仮名を区別 | |
widthSensitive = true; //全角・半角を区別 | |
} | |
app.changeGlyphPreferences = NothingEnum.nothing; | |
app.findGlyphPreferences = NothingEnum.nothing; | |
} | |
function resetFindChangeGrepOptions() { | |
// 後片付け | |
app.findGrepPreferences = NothingEnum.nothing; | |
app.changeGrepPreferences = NothingEnum.nothing; | |
app.changeGlyphPreferences = NothingEnum.nothing; | |
app.findGlyphPreferences = NothingEnum.nothing; | |
} | |
function regexFind(my_range, my_find) { | |
//検索のみ。マッチしたTextオブジェクトの配列を返す | |
app.findGrepPreferences.properties = my_find; //検索の設定 | |
return my_range.findGrep(true); //逆順 | |
} | |
function regexFindChange(my_range, my_find, my_change) { | |
//検索置換を実行 | |
app.findGrepPreferences.findWhat = my_find; //検索の設定 | |
app.changeGrepPreferences.changeTo = my_change; //置換の設定 | |
my_range.changeGrep(true); //逆順 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment