forked from graysitory/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XHeight_ParagraphStyle.jsx
executable file
·128 lines (107 loc) · 4.11 KB
/
XHeight_ParagraphStyle.jsx
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
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// XHeight_ParagraphStyle.jsx
// A InDesign javascript to get and set the xHeight from seleced paragraph style
//
// Bruno Herfst 2015
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#target InDesign;
//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;
if (app.selection.length == 1){
switch (app.selection[0].constructor.name){
//add case for insertion point
case "Text":
case "Paragraph":
case "Character":
case "Word":
showXheightPallette(app.selection[0].appliedParagraphStyle);
break;
default:
alert("This is " + app.selection[0].constructor.name + "\n Please select some text.");
break;
}
} else {
alert("Select some text and try again.");
}
} else {
alert("No documents are open. Please open a document, select some text, and try again.");
}
function getXHeight(ParagraphStyle, xPercent){
if (typeof xPercent === 'undefined') {
var xPercent = getXPercent(ParagraphStyle);
}
var fontSize = ParagraphStyle.pointSize;
var xHeight = fontSize/xPercent;
return(xHeight);
}
function getFontSize(ParagraphStyle, xHeight, xPercent){
if (typeof xPercent === 'undefined') {
var xPercent = getXPercent(ParagraphStyle);
}
var fontSize = xHeight*xPercent;
return fontSize;
}
function getXPercent(ParagraphStyle){
var baseSize = 100;
tempDoc = app.documents.add(false);
//Set up the new cover.
with(tempDoc.documentPreferences){
horizontalMeasurementUnits = MeasurementUnits.POINTS;
verticalMeasurementUnits = MeasurementUnits.POINTS;
//Set page size
pageHeight = "2000pt";
pageWidth = "2000pt";
pagesPerDocument = 1;
}
with(tempDoc.viewPreferences){
horizontalMeasurementUnits = MeasurementUnits.POINTS;
verticalMeasurementUnits = MeasurementUnits.POINTS;
rulerOrigin = RulerOrigin.pageOrigin;
}
var PAGE = tempDoc.pages[0];
var myTextFrame = PAGE.textFrames.add();
myTextFrame.label = "0987654334567890";
myTextFrame.geometricBounds = PAGE.bounds;
myTextFrame.contents = "x";
myTextFrame.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
myTextFrame.textFramePreferences.insetSpacing = 0;
myTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.X_HEIGHT;
var myStory = myTextFrame.parentStory;
myStory.appliedFont = ParagraphStyle.appliedFont;
myStory.pointSize = baseSize;
myTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
var tfBounds = myTextFrame.geometricBounds; // [y1, x1, y2, x2]
var baseXHeight = tfBounds[2] - tfBounds[0];
if(tempDoc.isValid){
tempDoc.close( SaveOptions.NO );
}
var xPercent = baseSize/baseXHeight;
return(xPercent);
}
function showXheightPallette(ParagraphStyle){
var xPercent = getXPercent(ParagraphStyle);
var currXheight = getXHeight(ParagraphStyle, xPercent);
var myDialog = app.dialogs.add({name:"Set X Height", canCancel:true});
with(myDialog){
with(dialogColumns.add()){
with(dialogRows.add()){
var xHeightField = measurementComboboxes.add({editUnits:MeasurementUnits.POINTS, editContents:String(currXheight),smallNudge:0.01,largeNudge:1,minWidth:100});
}
}
}
if(myDialog.show() == true){
var xHeight=parseFloat(xHeightField.editValue);
if(xHeight != currXheight){
var newFontSize = getFontSize(ParagraphStyle, xHeight, xPercent);
ParagraphStyle.pointSize = newFontSize;
}
} else {
myDialog.destroy();
exit();
}
}