forked from kyubuns/oden
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoden.js
More file actions
211 lines (190 loc) · 6.58 KB
/
oden.js
File metadata and controls
211 lines (190 loc) · 6.58 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
// Generated by CoffeeScript 1.9.1
(function() {
var base, base1, clipping_image, createLayerParts, generate_slice, hiddenLayer, hiddenSystemLayer, main, originalFileName, originalFilePath, restoreLayer, restoreSystemLayer, run, save_image, save_text, setup;
if ((base = String.prototype).startsWith == null) {
base.startsWith = function(s) {
return this.slice(0, s.length) === s;
};
}
if ((base1 = String.prototype).endsWith == null) {
base1.endsWith = function(s) {
return s === '' || this.slice(-s.length) === s;
};
}
if (app.documents.length === 0) {
alert('対象のpsdを開いてから実行してください。');
return;
}
originalFilePath = '';
originalFileName = '';
setup = function() {
originalFilePath = app.activeDocument.path;
originalFileName = app.activeDocument.name;
return preferences.rulerUnits = Units.PIXELS;
};
main = function() {
var copiedImage, i, layerSet, len, original, ref;
copiedImage = app.activeDocument.duplicate(activeDocument.name.slice(0, -4) + '.oden.pdf');
original = hiddenLayer(copiedImage);
ref = copiedImage.layerSets;
for (i = 0, len = ref.length; i < len; i++) {
layerSet = ref[i];
if (!layerSet.name.startsWith('#')) {
continue;
}
layerSet.visible = true;
run(layerSet);
layerSet.visible = false;
}
restoreLayer(copiedImage, original);
return copiedImage.close(SaveOptions.DONOTSAVECHANGES);
};
run = function(layerSet) {
var fileName, i, layer, len, maxLayer, minLayer, original_active, ref;
original_active = hiddenSystemLayer(layerSet);
fileName = layerSet.name.slice(1);
maxLayer = layerSet;
minLayer = null;
ref = layerSet.artLayers;
for (i = 0, len = ref.length; i < len; i++) {
layer = ref[i];
if (layer.name === "#Min") {
minLayer = layer;
}
}
if (minLayer === null) {
alert(layerSet.name + '内に#Minレイヤーがありません。');
return;
}
clipping_image(fileName, minLayer.bounds, maxLayer.bounds);
return restoreSystemLayer(layerSet, original_active);
};
clipping_image = function(fileName, minLayerBounds, maxLayerBounds) {
var filePath, h, t1_h, t1_w, t2_h, t2_w, w, x1, x2, y1, y2;
x1 = maxLayerBounds[0];
y1 = maxLayerBounds[1];
x2 = maxLayerBounds[2];
y2 = maxLayerBounds[3];
w = x2 - x1;
h = y2 - y1;
t1_w = activeDocument.width;
t1_h = activeDocument.height;
activeDocument.resizeCanvas(x2, y2, AnchorPosition.TOPLEFT);
t2_w = activeDocument.width;
t2_h = activeDocument.height;
activeDocument.resizeCanvas(w, h, AnchorPosition.BOTTOMRIGHT);
filePath = save_image(fileName, app.activeDocument);
generate_slice(filePath, fileName, minLayerBounds, maxLayerBounds);
activeDocument.resizeCanvas(t2_w, t2_h, AnchorPosition.BOTTOMRIGHT);
return activeDocument.resizeCanvas(t1_w, t1_h, AnchorPosition.TOPLEFT);
};
generate_slice = function(filePath, fileName, min, max) {
var a, center_px, dx, dy, h, originalLayer, pngOptions, w, x1, x2, y1, y2;
a = open(File(filePath));
x1 = min[0] - max[0];
y1 = min[1] - max[1];
x2 = min[2] - max[0];
y2 = min[3] - max[1];
w = max[2] - max[0];
h = max[3] - max[1];
center_px = 1;
dx = x1 - x2 + (center_px * 2);
dy = y1 - y2 + (center_px * 2);
originalLayer = a.activeLayer;
createLayerParts(a, originalLayer, 0, 0, x1 + center_px, y1 + center_px, 0, 0);
createLayerParts(a, originalLayer, 0, y2 - center_px, x1 + center_px, h, 0, dy);
createLayerParts(a, originalLayer, x2 - center_px, y2 - center_px, w, h, dx, dy);
createLayerParts(a, originalLayer, x2 - center_px, 0, w, y1 + center_px, dx, 0);
originalLayer.clear();
a.resizeCanvas(w + dx, h + dy, AnchorPosition.TOPLEFT);
pngOptions = new PNGSaveOptions();
a.saveAs(File(filePath), pngOptions, true);
a.close(SaveOptions.DONOTSAVECHANGES);
return save_text(fileName, x1 + ',' + (w - x2) + ',' + y1 + ',' + (h - y2));
};
createLayerParts = function(doc, originalLayer, x1, y1, x2, y2, dx, dy) {
var newLayer, sel;
newLayer = originalLayer.duplicate();
doc.activeLayer = newLayer;
sel = Array(Array(x1, y1), Array(x2, y1), Array(x2, y2), Array(x1, y2), Array(x1, y1));
doc.selection.select(sel);
doc.selection.invert();
doc.selection.clear();
doc.selection.deselect();
return newLayer.translate(dx, dy);
};
save_text = function(fileName, text) {
var filepath, outputFolderPath, txtFile;
outputFolderPath = originalFilePath + "/" + originalFileName.slice(0, -4);
filepath = outputFolderPath + "/" + fileName + ".oden";
txtFile = new File(filepath);
txtFile.open("w", "TEXT");
txtFile.write(text);
txtFile.close();
return filepath;
};
save_image = function(fileName, document) {
var filepath, outputFolder, outputFolderPath, pngOptions;
outputFolderPath = originalFilePath + "/" + originalFileName.slice(0, -4);
filepath = outputFolderPath + "/" + fileName + ".png";
outputFolder = new Folder(outputFolderPath);
if (outputFolder.exists) {
outputFolder.remove();
}
outputFolder.create();
pngOptions = new PNGSaveOptions();
document.saveAs(File(filepath), pngOptions, true);
return filepath;
};
hiddenLayer = function(root) {
var i, layer, len, original, ref;
original = {};
ref = root.layers;
for (i = 0, len = ref.length; i < len; i++) {
layer = ref[i];
original[layer.name] = layer.visible;
layer.visible = false;
}
return original;
};
hiddenSystemLayer = function(root) {
var i, layer, len, original, ref;
original = {};
ref = root.layers;
for (i = 0, len = ref.length; i < len; i++) {
layer = ref[i];
if (layer.name[0] !== '#') {
continue;
}
original[layer.name] = layer.visible;
layer.visible = false;
}
return original;
};
restoreLayer = function(root, original) {
var i, layer, len, ref, results;
ref = root.layers;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
layer = ref[i];
results.push(layer.visible = original[layer.name]);
}
return results;
};
restoreSystemLayer = function(root, original) {
var i, layer, len, ref, results;
ref = root.layers;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
layer = ref[i];
if (layer.name[0] !== '#') {
continue;
}
results.push(layer.visible = original[layer.name]);
}
return results;
};
setup();
main();
alert('complete!');
}).call(this);