-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerator.js
342 lines (281 loc) · 9.3 KB
/
generator.js
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
var newLine = "\n";
var suggestedColorsPlistTemplate = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
'<plist version="1.0">',
'<dict>',
' <key>colors</key>',
' <dict>',
'{template}',
'</dict>',
' <key>useMyColors</key>',
' <true/>',
'</dict>',
'</plist>'
].join('');
function generateFilesForPlatform(platform, prefix)
{
// lets generate some stuff
// 0 is iOS
// 1 is Android
// 2 is Windows
var fontFunction;
var colorFunction;
if (platform == 0)
{
fontFunction = generateFontiOS;
colorFunction = generateColoriOS;
}
else if (platform == 1)
{
fontFunction = generateFontAndroid;
colorFunction = generateColorAndroid;
}
else
{
fontFunction = generateFontWindows;
colorFunction = generateColorAndroid;
}
generateFontFilesForPlatform(platform, prefix, 'UIFont', fontFunction)
generateFontFilesForPlatform(platform, prefix, 'UIColor', colorFunction)
}
function generateLineForPlatform(methodName, codeSignature, platform, output, method, callback)
{
if (platform == 0)
{
output += methodName;
output += newLine;
output += "{";
output += newLine;
output += codeSignature;
output += newLine;
output += "}";
output += newLine;
output += newLine;
}
else if (platform == 1)
{
output += "<style name=\""+methodName+"\" parent=\"@android:style/TextAppearance\">";
}
return output;
}
function call_others(function_name, object)
{
eval(function_name + "(" + eval(object) +")");
}
var main = this;
function generateFontFilesForPlatform(platform, prefix, classType, callback)
{
var methodSignatures = [];
var codes = [];
var output = '';
//https://github.com/jwaitzel/SuggestedColors/
//This will generate a Plist file that can be used by the above plugin in XCode
var suggestedColors = '';
var parsingFont = classType == 'UIFont';
var swatchLayers = getSwatchLayer(parsingFont ? 'Font Swatch' : 'Color Swatch');
var memberClass = parsingFont ? [MSTextLayer class] : [MSShapeGroup class];
var isIOS = platform == 0;
var isAndroid = platform == 1;
var isWindows = !isIOS && !isAndroid;
//suggested color template
var keyTemplate = '<key>{key}</key>';
for (var i=0; i < [swatchLayers count]; i++)
{
var layer = [swatchLayers objectAtIndex:i];
if ([layer isMemberOfClass:memberClass])
{
var swatchName = layer.name();
if (swatchName != "please rename")
{
if (isIOS)
{
swatchName = makeNiceName(swatchName);
var methodName = generateMethodSignature(classType, swatchName, prefix);
methodSignatures.push(methodName);
//
var codeSignature = callback(layer)
output = generateLineForPlatform(methodName, codeSignature, platform, output, callback);
if(!parsingFont)
{
/*suggested color*/
var hexColorForLayer = generateSuggestedColorsHex(layer);
suggestedColors += keyTemplate.replace('{key}', swatchName);
suggestedColors += hexColorForLayer;
}
}
else if (isAndroid)
{
if (parsingFont)
{
output += "<style name=\""+swatchName+"\" parent=\"@android:style/TextAppearance\">";
output += newLine;
output += callback(layer);
output += "</style>";
output += newLine;
output += newLine;
}
else
{
output += callback(layer).replace("{template}", swatchName);
output += newLine;
}
}
else
{
if (parsingFont)
{
output += "<Style x:Key=\""+swatchName+"\" TargetType=\"TextBlock\">";
output += newLine;
output += callback(layer);
output += "</Style>";
output += newLine;
output += newLine;
}
else
{
var sanitiseString = callback(layer).replace("{template}", swatchName);
sanitiseString = sanitiseString.replace('color', 'Color');
sanitiseString = sanitiseString.replace('/color', '/Color');
sanitiseString = sanitiseString.replace('name=', 'x:Key=');
output += sanitiseString;
output += newLine;
}
}
}
}
}
// BIG IF STATEMENT HERE FOR PLATFORM
if (isIOS)
{
var mPrefix = "#import \""+classType+"+Sketch.h\"";
mPrefix += newLine;
mPrefix += newLine;
mPrefix += "@implementation "+classType+" (Sketch)";
mPrefix += newLine;
mPrefix += newLine;
mPrefix += output;
mPrefix += newLine;
mPrefix += "@end";
var hPrefix = "#import <UIKit/UIKit.h>";
hPrefix += newLine;
hPrefix += newLine;
hPrefix += "@interface "+classType+" (Sketch)";
hPrefix += newLine;
hPrefix += newLine;
hPrefix += methodSignatures.join(newLine);
hPrefix += newLine;
hPrefix += newLine;
hPrefix += "@end";
var mPath = getDocumentPath() + classType + "+Sketch.m";
var hPath = getDocumentPath() + classType + "+Sketch.h";
saveFile(mPath, mPrefix);
saveFile(hPath, hPrefix);
//save the file
if(!parsingFont)
{
suggestedColorsPlistTemplate = suggestedColorsPlistTemplate.replace('{template}', suggestedColors);
var path = getDocumentPath() + "SuggestedColors.plist";
saveFile(path, suggestedColorsPlistTemplate);
}
}
else if (isAndroid)
{
var header = '<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources xmlns:android=\"http://schemas.android.com/apk/res/android\">\n';
header += output;
header += "</resources>";
var filename = (parsingFont ? "Fonts.xml" : "Colors.xml");
var mPath = getDocumentPath() + filename;
saveFile(mPath, header);
}
else if (isWindows)
{
var header = '<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">\n';
header += output;
header += "</ResourceDictionary>";
var filename = (parsingFont ? "Fonts.xaml" : "Colors.xaml");
var mPath = getDocumentPath() + filename;
saveFile(mPath, header);
}
}
function getSwatchLayer(name)
{
var pages = [doc pages];
var swatchPage;
for (var i=0; i < [pages count]; i++)
{
if (pages[i].name() == name)
{
swatchPage = pages[i];
break;
}
}
return [swatchPage layers];
}
function generateMethodSignature(className, name, prefix)
{
return "+ ("+className+" *)" + prefix + name;
}
function generateFontiOS(layer)
{
return " return [UIFont fontWithName:@\"" + layer.fontPostscriptName() + "\" size:" + layer.fontSize() + ".f];";
}
function generateFontAndroid(layer)
{
var output = newLine;
output = " <item name=\"typeface\">"+layer.fontPostscriptName()+".ttf</item>";
output += newLine;
output += " <item name=\"android:textSize\">"+layer.fontSize()+"sp</item>";
output += newLine;
log(output);
return output;
}
function generateFontWindows(layer)
{
var output = newLine;
output = " <Setter Property=\"FontFamily\" Value=\""+layer.fontPostscriptName()+"\" />";
output += newLine;
output += " <Setter Property=\"FontSize\" Value=\""+layer.fontSize()+"\" />";
output += newLine;
return output;
}
function generateColoriOS(layer)
{
var fill = layer.style().fills().firstObject();
var red = fill.color().red().toFixed(3).toString();
var green = fill.color().green().toFixed(3).toString();
var blue = fill.color().blue().toFixed(3).toString();
var alpha = fill.color().alpha().toFixed(3).toString();
return " return [UIColor colorWithRed:" + red + " green:" + green + " blue:" + blue + " alpha:" + alpha + "];";
}
function generateColorAndroid(layer)
{
var fill = layer.style().fills().firstObject();
var red = parseInt((fill.color().red().toFixed(3) * 255).toFixed(0));
var green = parseInt((fill.color().green().toFixed(3) * 255).toFixed(0));
var blue = parseInt((fill.color().blue().toFixed(3) * 255).toFixed(0));
var alpha = parseInt((fill.color().alpha().toFixed(3) * 255).toFixed(0));
return "<color name=\"{template}\">#"+rgba2hex(red,green,blue,alpha)+"</color>";
}
function generateSuggestedColorsHex(layer)
{
var hexTemplate = '<string>{hex}</string>';
return hexTemplate.replace('{hex}',hexColorForLayer(layer));
}
function hexColorForLayer(layer)
{
var fill = layer.style().fills().firstObject();
var red = parseInt((fill.color().red().toFixed(3) * 255).toFixed(0));
var green = parseInt((fill.color().green().toFixed(3) * 255).toFixed(0));
var blue = parseInt((fill.color().blue().toFixed(3) * 255).toFixed(0));
var alpha = parseInt((fill.color().alpha().toFixed(3) * 255).toFixed(0));
var hexColor = rgba2hex(red,green,blue,alpha);
hexColor = hexColor.substring(0, hexColor.length - 2);
return hexColor;
}
// convert RGBA color data to hex
function rgba2hex(r, g, b, a) {
if (r > 255 || g > 255 || b > 255 || a > 255)
throw "Invalid color component";
return (256 + a).toString(16).substr(1) +((1 << 24) + (r << 16) | (g << 8) | b).toString(16).substr(1);
}