forked from Charimon/iosViews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_as_cgContext2.sketchplugin
196 lines (151 loc) · 7.84 KB
/
export_as_cgContext2.sketchplugin
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
// (ctrl alt cmd [)
//doc, selection, scriptPath
#import 'lib/common.js'
main()
function main() {
var loop = selection.objectEnumerator();
var lines = [];
while(item = loop.nextObject()) {
lines.push(processGroup(item, item, 0, 0));
}
paste( arrayToString(lines) );
}
function processShape(layer, boundingLayer, offsetX, offsetY) {
if(hasSubShapes(layer)) { alert("warning: union, subtract, etc not supported"); }
else {
var points = layer.firstLayer().path().points();
var lines = [];
for(var i=0; i<points.count(); i++) {
var item = points.objectAtIndex(i);
var p = item.point();
var px = p.x * layer.frame().width() + layer.frame().x() + offsetX;
var py = p.y * layer.frame().height() + layer.frame().y() + offsetY;
var pT = item.curveTo();
if(!item.hasCurveTo()){
pT = p;
}
var pTx = pT.x * layer.frame().width() + layer.frame().x() + offsetX;
var pTy = pT.y * layer.frame().height() + layer.frame().y() + offsetY;
if(i == 0){
lines.push("CGContextMoveToPoint(ctx, {{0}}*(width/{{2}}), {{1}}*(height/{{3}}))".format(round(px), round(py), round(boundingLayer.frame().width()), round(boundingLayer.frame().height()) ))
} else {
var pF = points.objectAtIndex(i - 1).curveFrom()
if(!points.objectAtIndex(i - 1).hasCurveFrom()){
pF = points.objectAtIndex(i - 1).point()
}
var pFx = pF.x * layer.frame().width() + layer.frame().x() + offsetX;
var pFy = pF.y * layer.frame().height() + layer.frame().y() + offsetY;
lines.push("CGContextAddCurveToPoint(ctx, {{0}}*(width/{{6}}), {{1}}*(height/{{7}}), {{2}}*(width/{{6}}), {{3}}*(height/{{7}}), {{4}}*(width/{{6}}), {{5}}*(height/{{7}}))".format(round(pFx), round(pFy), round(pTx), round(pTy), round(px), round(py), round(boundingLayer.frame().width()), round(boundingLayer.frame().height())))
}
}
var p0 = points.objectAtIndex(0).point();
var px = p0.x * layer.frame().width() + layer.frame().x() + offsetX;
var py = p0.y * layer.frame().height() + layer.frame().y() + offsetY;
var pT = points.objectAtIndex(0).curveTo();
if(!points.objectAtIndex(0).hasCurveTo()){
pT = points.objectAtIndex(0).point();
}
var pTx = pT.x * layer.frame().width() + layer.frame().x() + offsetX;
var pTy = pT.y * layer.frame().height() + layer.frame().y() + offsetY;
var pF = points.objectAtIndex(points.count() - 1).curveFrom();
if(!points.objectAtIndex(points.count() - 1).hasCurveFrom()){
pF = points.objectAtIndex(points.count() - 1).point();
}
var pFx = pF.x * layer.frame().width() + layer.frame().x() + offsetX;
var pFy = pF.y * layer.frame().height() + layer.frame().y() + offsetY;
lines.push("CGContextAddCurveToPoint(ctx, {{0}}*(width/{{6}}), {{1}}*(height/{{7}}), {{2}}*(width/{{6}}), {{3}}*(height/{{7}}), {{4}}*(width/{{6}}), {{5}}*(height/{{7}}))".format(round(pFx), round(pFy), round(pTx), round(pTy), round(px), round(py), round(boundingLayer.frame().width()), round(boundingLayer.frame().height())));
var strokeLines = processStroke(layer, boundingLayer);
var fillLines = processFill(layer);
lines = lines.concat(strokeLines);
lines = lines.concat(fillLines);
if(strokeLines.length > 0 && fillLines.length > 0) mode = "kCGPathFillStroke";
else if(strokeLines.length > 0) mode = "kCGPathStroke";
else if(fillLines.length > 0) mode = "kCGPathFill";
if(mode) lines.push("CGContextDrawPath(ctx, {{0}})".format(mode));
if(lines.length > 0) {
lines.unshift("CGContextSaveGState(ctx)");
lines.push("CGContextRestoreGState(ctx)");
}
return lines;
}
}
function processGroup(layer, boundingLayer, offsetX, offsetY) {
if(!layer.isVisible()) return [];
var lines = [];
lines.push("func {{0}}(rect: CGRect){".format(safeName(layer)));
lines.push("\tlet ctx = UIGraphicsGetCurrentContext()");
lines.push("\tlet width = rect.width");
lines.push("\tlet height = rect.height");
lines.push("");
lines.push("\tvar pstyleRightAlign = NSMutableParagraphStyle()");
lines.push("\tpstyleRightAlign.alignment = NSTextAlignment.Right");
lines.push("");
lines.push("\tvar pstyleLeftAlign = NSMutableParagraphStyle()");
lines.push("\tpstyleLeftAlign.alignment = NSTextAlignment.Left");
lines.push("");
lines.push("\tvar pstyleCenterAlign = NSMutableParagraphStyle()");
lines.push("\tpstyleCenterAlign.alignment = NSTextAlignment.Center");
lines.push("");
var groupFunctions = [];
forEach(layer.layers(), function(item){if(item.isVisible()) {
if(!item.isVisible()) return;
if(isShape(item)) {
lines.push(processShape(item, boundingLayer, offsetX, offsetY));
} else if(isGroup(item)) {
lines.push("\t{{0}}(rect)".format(safeName(item)));
groupFunctions.push("");
groupFunctions = groupFunctions.concat(processGroup(item, layer, offsetX + item.frame().x(), offsetY + item.frame().y()));
} else if(isText(item)) {
lines.push(processText(item, boundingLayer, offsetX, offsetY));
}
}})
lines.push("}");
lines = lines.concat(groupFunctions);
return lines;
}
function processText(layer, boundingLayer, offsetX, offsetY) {
var lines = [];
var x = offsetX + layer.frame().x();
var y = offsetY + layer.frame().y();
var layerWidth = layer.frame().width();
var layerHeight = layer.frame().height();
var width = boundingLayer.frame().width();
var height = boundingLayer.frame().height();
var cgRect = "CGRect(x:{{0}}*(width/{{4}}), y:{{1}}*(height/{{5}}) + {{3}}*(1-(height/{{5}}))/2, width:{{2}}*(width/{{4}}), height:{{3}}*(height/{{5}}))".format(round(x),round(y),round(layerWidth),round(layerHeight),round(width),round(height));
lines.push("CGContextSaveGState(ctx)");
lines.push("\"{{0}}\".drawInRect({{1}}, withAttributes:{{2}})".format(layer.name() ,cgRect, processAttributes(layer,boundingLayer)));
lines.push("CGContextRestoreGState(ctx)");
return lines;
}
function processStroke(layer, boundingLayer) {
var lines = [];
forEach(layer.style().borders(), function(item){
if(item.isEnabled() && item.fillType() == 0 && item.position() == 0){
var c = item.color();
lines.push("CGContextSetStrokeColorWithColor(ctx, UIColor(hue:{{0}}, saturation:{{1}} , brightness:{{2}} , alpha:{{3}}).CGColor)".format(round(c.hue()), round(c.saturation()), round(c.brightness()), round(c.alpha())));
lines.push("CGContextSetLineWidth(ctx, {{0}}*min(width/{{1}}, height/{{2}}))".format(round(item.thickness()), round(boundingLayer.frame().width()), round(boundingLayer.frame().height())));
}
})
return lines;
}
function processFill(layer, boundingLayer) {
var lines = [];
forEach(layer.style().fills(), function(item){
if(item.isEnabled() && item.fillType() == 0){
var c = item.color();
lines.push("CGContextSetFillColorWithColor(ctx, UIColor(hue:{{0}}, saturation:{{1}} , brightness:{{2}} , alpha:{{3}}).CGColor)".format(round(c.hue()), round(c.saturation()), round(c.brightness()), round(c.alpha())));
return false;
}
});
return lines;
}
function processAttributes(layer, boundingLayer) {
var color = layer.textColor();
var lines = [];
lines.push("NSFontAttributeName: UIFont(name: \"{{0}}\", size: {{1}}*(width/{{2}}))!".format(layer.fontPostscriptName(), round(layer.fontSize()), round(boundingLayer.frame().width())));
lines.push("NSForegroundColorAttributeName: UIColor(hue: {{0}}, saturation: {{1}}, brightness: {{2}}, alpha: {{3}})".format(round(color.hue()), round(color.saturation()), round(color.brightness()), round(color.alpha())));
if(layer.textAlignment() == 0) lines.push("NSParagraphStyleAttributeName: pstyleLeftAlign");
else if(layer.textAlignment() == 1) lines.push("NSParagraphStyleAttributeName: pstyleRightAlign");
else if(layer.textAlignment() == 2) lines.push("NSParagraphStyleAttributeName: pstyleCenterAlign");
return "[{{0}}]".format(lines.join(","));
}