-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSVGRepair.jsx
More file actions
256 lines (232 loc) · 7.97 KB
/
SVGRepair.jsx
File metadata and controls
256 lines (232 loc) · 7.97 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
// (c) 2026 MapCreator BV, the Netherlands
var defaultColorMappings=
[
//list of colors to replace.
// format of rgbin is "<R> <G> <B>" with R,G,B as whole numbers from 0..255
// format of cmykin is "<C> <M> <Y> <K>" with C,M,Y,K as fractional numbers rounded to 2 decimal places, incl trailing zeros from 0..100
// format of cmykout is "<C> <M> <Y> <K>" with C,M,Y,K as fractional numbers from 0..1
//samples:
//{rgbin:"214 250 217", cmykout:"1 0 0 0"},
//{rgbin:"255 255 255", cmykout:"1 0 0 0"},
//{cmykin:"14.12 0.00 12.94 1.96", cmykout:"0 0 1 0"}
]
var defaultFontMappings=
[
//list of fonts to replace.
//sample:
//{fontIn:{family:'Helvetica Neue LT Std',style:'55 Roman'},fontOut:{family:'Symbol',style:'Regular'}}
]
// Configuration:
// set convertColors to true for enabling colorconversion
// set fixTextOnPath to true for enabling textOnPath fixups
// add fonts to defaultFontMappings to enable fontreplacement
var convertColors = false;
var fixTextOnPath = false;
{
if (convertColors) {
//Convert colors to cmyk
var items = app.activeDocument.pageItems;
for (var i = 0; i < items.length; i++) {
AddNote(items[i]);
}
app.executeMenuCommand('doc-color-cmyk');
var items = app.activeDocument.pageItems;
for (var i = 0; i < items.length; i++) {
ParseNote(items[i],defaultColorMappings);
}
}
if (fixTextOnPath) {
//Fix text on path
var items = app.activeDocument.groupItems;
for (var i = items.length - 1; i >= 0; i--) {
if (items[i].name.indexOf('fixtextpath') == 0)
FixPath(items[i]);
}
}
if (defaultFontMappings.length > 0 ) {
//Replace fonts
var items = app.activeDocument.textFrames;
for (var i = items.length - 1; i >= 0; i--) {
ReplaceFont(items[i]);
}
}
alert('Ready');
}
function AddNote(item) {
try {
item.note=onecol(item.textRange.characterAttributes.fillColor)+'|'+onecol(item.textRange.characterAttributes.strokeColor);
}catch(e){};
try {
var f=onecol(item.fillColor);
if (!item.filled)
f="none";
var s=onecol(item.strokeColor);
if (!item.stroked)
s="none";
item.note=f+'|'+s;
}catch(e){};
}
function ParseNote(item,defaultColorMappings) {
if (item.note!="")
{
var colors=ParseOne(item.note,defaultColorMappings);
if (item.textRange!=null)
{
try {
item.textRange.characterAttributes.fillColor=colors.fill;
item.textRange.characterAttributes.strokeColor=colors.stroke;
item.note="";
}catch(e){};
}
else
{
try {
if (item.filled)
item.fillColor=colors.fill;
if (item.stroked)
item.strokeColor=colors.stroke;
item.note="";
}catch(e){}
}
}
}
function onecol(col) {
if (col.toString()=="[NoColor]")
return "none";
else
if (col.toString()=="[GrayColor]")
return ''+col.gray*2.55+' '+col.gray*2.55+' '+col.gray*2.55;
else
return ''+col.red+' '+col.green+' '+col.blue;
}
function ParseOne(note,defaultColorMappings) {
var parts=note.split("|");
return { fill:parse(parts[0],defaultColorMappings), stroke:parse(parts[1],defaultColorMappings) };
}
function parse(col,defaultColorMappings) {
if (col=="none")
return new NoColor();
for (var i = 0; i < defaultColorMappings.length; i++) {
if (defaultColorMappings[i].rgbin==col)
return colFromString(defaultColorMappings[i].cmykout);
}
var rgb=col.split(" ");
var c=(255-Number(rgb[0]))/255;
var m=(255-Number(rgb[1]))/255;
var y=(255-Number(rgb[2]))/255;
var k=Math.min(c,Math.min(m,y));
var res= new CMYKColor();
res.black = k*100;
res.cyan = (c-k)*100;
res.magenta = (m-k)*100;
res.yellow = (y-k)*100;
var searchColor = ''+res.cyan.toFixed(2)+' '+res.magenta.toFixed(2)+' '+res.yellow.toFixed(2)+' '+res.black.toFixed(2);
for (var i = 0; i < defaultColorMappings.length; i++) {
if (defaultColorMappings[i].cmykin==searchColor)
return colFromString(defaultColorMappings[i].cmykout);
}
return res;
}
function colFromString(s) {
var res= new CMYKColor();
var cmyk=s.split(" ");
res.cyan = Number(cmyk[0])*100;
res.magenta = Number(cmyk[1])*100;
res.yellow = Number(cmyk[2])*100;
res.black = Number(cmyk[3])*100;
return res;
}
function getFont(family,style) {
var fonts = app.textFonts;
for (var i=0; i<fonts.length; i++) {
if (fonts[i].family === family && fonts[i].style === style) {
return fonts[i];
}
}
throw new Error('Font '+family+' '+style+' not found');
}
function ReplaceFont(item) {
for (var i = 0; i < item.textRanges.length; i++) {
var tr = item.textRanges[i];
var tf = tr.characterAttributes.textFont;
for (var j = 0; j < defaultFontMappings.length; j++) {
if (defaultFontMappings[j].fontIn.family === tf.family && defaultFontMappings[j].fontIn.style === tf.style) {
if (defaultFontMappings[j].font === undefined) {
defaultFontMappings[j].font = getFont(defaultFontMappings[j].fontOut.family, defaultFontMappings[j].fontOut.style);
}
tr.characterAttributes.textFont = defaultFontMappings[j].font;
}
}
}
}
function add(a, b) {
return [a[0] + b[0], a[1] + b[1]];
}
function sub(a, b) {
return [a[0] - b[0], a[1] - b[1]];
}
function mult(a, k) {
return [a[0] * k, a[1] * k];
}
function unit(p) {
var mag = Math.sqrt(p[0] * p[0] + p[1] * p[1]);
return [p[0] / mag, p[1] / mag];
}
function sup(a, b) {
var r = unit(sub(a, b));
return [-r[1], r[0]];
}
function offsetLine(line, offset) {
var zero = [0, 0];
var newLine = [];
for (var i = 0; i < line.length; i++) {
var b = line[i].anchor;
var aToB = i === 0 ? zero : sup(b, line[i - 1].anchor);
var bToC = i === line.length - 1 ? zero : sup(line[i + 1].anchor, b);
var extrude = add(aToB, bToC);
extrude = unit(extrude);
var cosHalfAngle = extrude[0] * bToC[0] + extrude[1] * bToC[1];
if (cosHalfAngle !== 0) {
extrude = mult(extrude, 1 / cosHalfAngle);
}
var newP = add(mult(extrude, offset), b);
newLine.push(newP);
}
return newLine;
}
function FixPath(item) {
var pathItem = null;
var totals = [];
var textFrameContents = '';
var templateFrames = [];
for (var i = 0; i < item.pageItems.length; i++) {
var subItem = item.pageItems[i];
if (subItem.typename == 'TextFrame') {
textFrameContents = subItem.contents;
} else {
if (subItem.typename == 'GroupItem') {
var total = '';
for (var j = 0; j < subItem.textFrames.length; j++) {
total = subItem.textFrames[j].contents + total;
}
if (subItem.textFrames.length > 0) {
subItem.textFrames[0].contents = total + textFrameContents;
var p=subItem.textFrames[0].paragraphs[0].paragraphAttributes;
p.justification = Justification.CENTER;
templateFrames.push(subItem.textFrames[0]);
}
if (subItem.pathItems.length > 0)
pathItem = subItem.pathItems[0];
}
}
}
if (pathItem != null && item.parent.typename == 'GroupItem' && templateFrames.length > 0) {
var newGeo = offsetLine(pathItem.pathPoints, -templateFrames[0].textRange.characterAttributes.size * 0.4 / 2);
pathItem.setEntirePath(newGeo);
for (var i = templateFrames.length-1; i>=0; i--) {
var nw = item.parent.textFrames.pathText(pathItem.duplicate(), 0, 0, TextOrientation.HORIZONTAL, templateFrames[i]);
nw.opacity = item.opacity;
}
item.remove();
}
}