forked from GitBruno/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlyph_Replacer.jsx
More file actions
executable file
·305 lines (267 loc) · 12 KB
/
Glyph_Replacer.jsx
File metadata and controls
executable file
·305 lines (267 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// Glyph_Replacer.jsx
// A InDesign javascript to place photoletters in InDesign
//
// Bruno Herfst 2013
//
// This script needs a folder containing psd files (glyphs)
// that carry the unicode decimal number, for example: 65.psd (Capital A)
// You can find all decibel codepoints here: http://unicodelookup.com/
// Or even better if you’re on the mac: http://toolbox.brunoherfst.com/2012/unicodechecker/
//
// Every font folder needs at least the unicode replacement character:
// 65533.psd (REPLACEMENT CHARACTER) which will be used when a character is not found in the fontfolder.
//
// Wishlist:
// Add baseline offset
// Place glyphs at point size of text or measure unit
// Add CSV kern table// Let the user start from insertion point too
// Add alternates with PS layers
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#target InDesign;
//presets
var preset = { minRes : 300, //int, dpi
warning : true, //Boolean, set to false to not get warnings about resolution
Hscale : 100, //float, Horizontal scale (percent 100% is no scaling)
Vscale : 100, //float, Vertical scale (percent 100% is no scaling)
multiply : true, //Boolean, Use the width of the selected frame as max width, creating linebreaks
shift : 0 }; //Int, The baseline shift value in points
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if (app.documents.length != 0){
// Global variables
var myDoc = app.activeDocument, myFont = undefined, tf = undefined;
var myDocXMP = myDoc.metadataPreferences;
var destNamespace = "http://brunoherfst.com/";
var destContName = "Glyph_Replacer_Settings";
if (app.selection.length == 1){
alert(app.selection[0].constructor.name);
switch (app.selection[0].constructor.name){
//add case for insertion point
case "TextFrame":
tf = app.selection[0];
showDialog();
break;
case "Text":
case "Character":
case "Word":
tf = app.selection[0].textFrames.add();
tf.contents = app.selection[0].contents;
tf.fit(FitOptions.FRAME_TO_CONTENT);
app.selection[0].contents = "";
showDialog();
break;
default:
alert("Select some text or a textframe and try again.");
break;
}
} else {
alert("Select a textframe and try again.");
}
} else {
alert("No documents are open. Please open a document, select some text, and try again.");
}
///////////////////////////////////////////////// F U N C T I O N S /////////////////////////////////////////////////
function showDialog(){
//before we continue see if we can find last used settings
//if so we can see if the folder is still valid
myFont = { folder : null ,
fontExt : ".psd",
charStr : tf.contents,
glyphSrt : null,
nullFile : null, };
if(!loadSettings()){
if(!loadFont()){
exit();
return;
}
}
if(loadFont()){
if(myFont.charStr != null && myFont.charStr.length > 0){
////////////
// DIALOG //
////////////
var column1 = 120;
var column2 = 60;
var s = 10;
var myWindow = new Window ("dialog", "Glyph Replacer");
myWindow.alignChildren = "left";
myWindow.spacing = s; //design
var fontGroup = myWindow.add ("group");
var myFontName = getFontName(myFont.folder);
var presetFont = fontGroup.add ("dropdownlist", undefined, [ myFontName,"Load different font..."]);
presetFont.selection = 0;
presetFont.preferredSize.width = column1+column2+s; //design
presetFont.onChange = function () {
if(this.selection > 0){
if(!loadFont()){
myWindow.close();
}
presetFont.selection = 0;
presetFont.selection.text = getFontName(myFont.folder);
}
}
var HscaleGroup = myWindow.add ("group");
var ST1 = HscaleGroup.add ("statictext", undefined, "Horizontal Scale:");
ST1.preferredSize.width = column1; //design
var Hscale = HscaleGroup.add ("edittext", undefined, preset.Hscale+"%");
Hscale.preferredSize.width = column2; //design
Hscale.onChange = function () {
this.text = NaN20(parseFloat(this.text))+"%";
}
var VscaleGroup = myWindow.add ("group");
var ST2 = VscaleGroup.add ("statictext", undefined, "Vertical Scale:");
ST2.preferredSize.width = column1; //design
var Vscale = VscaleGroup.add ("edittext", undefined, preset.Vscale+"%");
Vscale.preferredSize.width = column2; //design
Vscale.onChange = function () {
this.text = NaN20(parseFloat(this.text))+"%";
}
var multiplyGroup = myWindow.add ("group");
var multiplyCB = multiplyGroup.add ("checkbox", undefined, "Set glyphs to multiply");
multiplyCB.value = preset.multiply;
var warnGroup = myWindow.add ("group");
var warnCB = warnGroup.add ("checkbox", undefined, "Warn if below:");
warnCB.preferredSize.width = column1; //design
warnCB.value = preset.warning; //design
var dpiET = warnGroup.add ("edittext", undefined, preset.minRes+"DPI");
dpiET.preferredSize.width = column2; //design
dpiET.onChange = function () {
this.text = NaN20(parseInt(this.text))+"DPI";
}
var butGroup = myWindow.add ("group");
var cancelBut = butGroup.add ("button", undefined, "Cancel");
cancelBut.preferredSize.width = ((column1+column2)/2); //design
var okBut = butGroup.add ("button", undefined, "OK");
okBut.preferredSize.width = ((column1+column2)/2); //design
var myResult = myWindow.show();
if(myResult == true){
//update settings
preset.Hscale = NaN20(parseFloat(Hscale.text));
preset.Vscale = NaN20(parseFloat(Vscale.text));
preset.minRes = NaN20(parseInt(dpiET.text));
preset.multiply = multiplyCB.value;
preset.warning = warnCB.value;
safeSettings();
//now do it!
main();
}
}
}
}
function loadFont(){
myFont.folder = new Folder().selectDlg ("Where is the font folder?");
if (myFont.folder != null) {
myFont.folder = myFont.folder.absoluteURI + "/";
}
return checkFont(true);
}
function checkFont(warn){
var nullPath = myFont.folder + "65533" + myFont.fontExt;
myFont.nullFile = new File(nullPath);
if(myFont.nullFile.exists){
return true;
}
if(warn){
alert("Not a valid font!\nCan't find Unicode Character 'REPLACEMENT CHARACTER' (65533)");
}
return false;
}
function getFontName(path){
var FNreg = new RegExp("[^/]+(?=\\/$)","gi");
return FNreg.exec( path );
}
function main(){
myFont.glyphSrt = buildGlyphString(myFont);
replaceGlyphs(myFont);
}
function safeSettings(){
var myXML = new XML("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description xmlns:GlyphReplacer=\"http://brunoherfst.com/\"><GlyphReplacer:contact>mail@brunoherfst.com</GlyphReplacer:contact></rdf:Description></rdf:RDF></x:xmpmeta>");
var myXMLfile = File('TEMP_XMP.xml');
if ( myXMLfile.open('e') ){
myXMLfile.write(myXML);
myXMLfile.close();
myDoc.metadataPreferences.append(myXMLfile);
myXMLfile.remove();
}
myDocXMP.createContainerItem(destNamespace, destContName, undefined, ContainerType.BAG);
myDocXMP.setProperty(destNamespace, destContName + "[1]", String(preset.minRes));
myDocXMP.setProperty(destNamespace, destContName + "[2]", String(preset.warning));
myDocXMP.setProperty(destNamespace, destContName + "[3]", String(preset.Hscale));
myDocXMP.setProperty(destNamespace, destContName + "[4]", String(preset.Vscale));
myDocXMP.setProperty(destNamespace, destContName + "[5]", String(preset.multiply));
myDocXMP.setProperty(destNamespace, destContName + "[6]", String(myFont.folder));
}
function loadSettings(){
myFont.folder = myDocXMP.getProperty(destNamespace,destContName + "[6]");
if(myFont.folder != ""){
preset.minRes = parseInt( myDocXMP.getProperty(destNamespace,destContName + "[1]") );
preset.warning = ( myDocXMP.getProperty(destNamespace,destContName + "[2]") == "true" );
preset.Hscale = parseFloat( myDocXMP.getProperty(destNamespace,destContName + "[3]") );
preset.Vscale = parseFloat( myDocXMP.getProperty(destNamespace,destContName + "[4]") );
preset.multiply = ( myDocXMP.getProperty(destNamespace,destContName + "[5]") == "true" );
return checkFont(false);
} else {
return false;
}
}
function between(x, min, max) {
return x >= min && x <= max;
}
function NaN20(no){
if(isNaN(no)){
return 0;
} else {
return no;
}
}
function buildGlyphString(myFont){
var glyphs = new Array();
for (var i = myFont.charStr.length - 1; i >= 0; i--) {
var charCode = myFont.charStr.charCodeAt(i);
var glyph = { charcode : charCode , // codepoint
img : myFont.folder + charCode + myFont.fontExt }; // link to glyph file
glyphs.push(glyph);
};
return glyphs;
}
function replaceGlyphs(myFont){
//empty the frame
tf.contents = "";
var lowres = false, fffound = true;
for (var i = 0; i < myFont.glyphSrt.length; i++) {
//Don’t replace controle characters with pictures
if(!between(myFont.glyphSrt[i].charcode, 0, 31)){
var gfile = new File(myFont.glyphSrt[i].img);
if(!gfile.exists){
gfile = myFont.nullFile;
fffound = false;
}
var glyphPLace = tf.insertionPoints[0].place(gfile);
var glyphImg = glyphPLace[0];
var glyphFrame = glyphImg.parent;
glyphFrame.label = myFont.glyphSrt[i].charcode.toString();
if(preset.Hscale != 100 || preset.Vscale != 100){
glyphFrame.horizontalScale = preset.Hscale;
glyphFrame.verticalScale = preset.Vscale;
}
if(preset.multiply){
glyphFrame.transparencySettings.blendingSettings.blendMode = BlendMode.MULTIPLY;
}
if(glyphImg.actualPpi[0] < preset.minRes || glyphImg.actualPpi[1] < preset.minRes ){
lowres = true;
}
} else {
tf.insertionPoints[0].contents = String.fromCharCode(myFont.glyphSrt[i].charcode);
}
};
if(preset.warning && lowres){
alert("Some glyphs are low resolution\n< "+preset.minRes);
}
if(!fffound){
alert("Some glyphs could not be found.");
}
}
//EOF