-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.jsx
94 lines (92 loc) · 2.78 KB
/
mirror.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
#include 'lib.jsx'
var doc = app.activeDocument;
doc.suspendHistory('Mirror linking', 'exec()');
function deactivateMasks(l) {
var lmd = l.layerMaskDensity;
var vmd = l.vectorMaskDensity;
try { l.layerMaskDensity = 0; } catch (e) { }
try { l.vectorMaskDensity = 0; } catch (e) { }
return function() {
try { l.layerMaskDensity = lmd; } catch (e) { }
try { l.vectorMaskDensity = vmd; } catch (e) { }
}
}
function flipLayer(l){
var w = unitToNr(doc.width);
var h = unitToNr(doc.height);
var flipper = l.parent.layerSets.add();
flipper.move(l, ElementPlacement.PLACEBEFORE);
var dummy = flipper.artLayers.add();
l.move(dummy, ElementPlacement.PLACEBEFORE);
doc.selection.deselect();
doc.selection.select([ [0, 0], [w, 0], [w, h], [0, h], [0, 0] ]);
doc.selection.fill(app.backgroundColor);
doc.selection.deselect();
var bounds = boundsToRect(flipper.bounds);
if (bounds.x != 0 || bounds.y != 0 || bounds.w != w || bounds.h != h) {
var msg = 'Current flipper does not support its content is sticked out its canvas';
alert(msg);
throw msg;
}
flipper.resize(-100, 100, AnchorPosition.MIDDLECENTER);
l.move(flipper, ElementPlacement.PLACEBEFORE);
flipper.remove();
}
function mirrorLayer(from, to, placement) {
var l = from.duplicate(to, placement);
l.name = to.name;
flipLayer(l);
var fixname = function(layers, nameref) {
map(union(layers, nameref), function(union) {
union[0].name = union[1].name;
if (union[0].typename == 'LayerSet')
fixname(union[0].layers, union[1].layers);
});
};
if (l.typename == 'LayerSet')
fixname(l.layers, from.layers);
}
function queryLinkID(l){
var m = /#(.+)$/.exec(l.name);
if (!m) return;
m = /mirror\(([^)]+)\)/.exec(m[1]);
if (!m) return;
return m[1];
}
function getGroups(l) {
if (l.grouped) return [];
var groups = [];
map(l.parent.layers, function(sibling) {
if (sibling === l)
return groups;
if (sibling.grouped)
groups.splice(0, 0, sibling);
else
groups = [];
});
return groups;
}
function handleLayers(layers, active, id) {
map(layers, function(l) {
if (l === active) return;
var i = queryLinkID(l);
if (i != id || active.typename != l.typename) {
if (l.typename == 'LayerSet')
handleLayers(l.layers, active, id);
return;
}
var groups = getGroups(l);
mirrorLayer(active, l, ElementPlacement.PLACEBEFORE);
l.remove();
map(groups, function(g) { if (!g.grouped) g.grouped = true; });
});
}
function exec() {
var active = doc.activeLayer;
var id = queryLinkID(active);
if (!id) {
alert('Link ID does not found: mirror(ID)');
return;
}
handleLayers(doc.layers, active, id);
}