forked from Charimon/iosViews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_as_cgContext.sketchplugin
executable file
·254 lines (202 loc) · 9.1 KB
/
export_as_cgContext.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
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
// (ctrl alt cmd p)
//doc, selection, scriptPath
main()
function main() {
var loop = selection.objectEnumerator()
while(item = loop.nextObject()) {
parseSelection(item)
}
}
function parseSelection(selection){
var str = drawLayerGroup(selection, selection.frame(), 0, 0)
var pasteBoard = NSPasteboard.generalPasteboard();
pasteBoard.declareTypes_owner( [ NSPasteboardTypeString ], null );
pasteBoard.setString_forType_(str, NSStringPboardType)
}
function drawLayerGroup(layer, boundingFrame, offsetX, offsetY) {
if(!layer.isVisible()) return ""
var str = "func " + safeName(layer) + "(rect:CGRect){\n"
str += "\tlet ctx = UIGraphicsGetCurrentContext()\n"
str += "\tlet width = rect.width\n"
str += "\tlet height = rect.height\n"
str += "\tvar pstyleRightAlign = NSMutableParagraphStyle()\n"
str += "\tpstyleRightAlign.alignment = NSTextAlignment.Right\n"
str += "\tvar pstyleLeftAlign = NSMutableParagraphStyle()\n"
str += "\tpstyleLeftAlign.alignment = NSTextAlignment.Left\n"
str += "\tvar pstyleCenterAlign = NSMutableParagraphStyle()\n"
str += "\tpstyleCenterAlign.alignment = NSTextAlignment.Center\n"
var count = layer.layers().count()
var groupFunctions = ""
for(var i=0; i<count; i++) {
var item = layer.layers().objectAtIndex(i)
var oX = offsetX + item.frame().x()
var oY = offsetY + item.frame().y()
if(!item.isVisible()) continue
if(item.class() == MSLayerGroup) {
groupFunctions += drawLayerGroup(item, boundingFrame, oX, oY)
str += "\n " + safeName(item) + "(rect)\n"
}
else if(item.class() == MSShapeGroup) {
str += "\n//" + safeName(item) + "\n"
str += drawShapeGroup(item, boundingFrame, offsetX, offsetY)
}
else if(item.class() == MSTextLayer) {
str += drawTextLayer(item, boundingFrame, offsetX, offsetY)
}
}
str += "}\n"
str += "\n" + groupFunctions
return str
}
function drawShapeGroup(shapeGroup, boundingFrame, offsetX, offsetY) {
if(shapeGroup.layers().count() > 1) {
var alert = NSAlert.alloc().init()
alert.setMessageText("warning: union, subtract, etc not supported")
alert.addButtonWithTitle("OK")
alert.runModal()
} else if(shapeGroup.layers().count() == 1){
var shape = shapeGroup.layers().objectAtIndex(0)
var str = ""
str += drawCGShapePath(shapeGroup, boundingFrame.width(), boundingFrame.height(), offsetX, offsetY)
return str
}
return ""
}
/*
generates swift code, using CGContext magic
assumes we will have defined previously something like:
let ctx = UIGraphicsGetCurrentContext()
let width = ...
let height = ...
*/
function drawCGShapePath(shapeGroup, boundingWidth, boundingHeight, offsetX, offsetY) {
var str = ""
var points = shapeGroup.layers().objectAtIndex(0).path().points()
var count = points.count()
var shapeFrame = shapeGroup.frame()
var widthRatio = "*(width/" + boundingWidth + ")"
var heightRatio = "*(height/" + boundingHeight + ")"
for(var i = 0; i<count; i++) {
var point = points.objectAtIndex(i)
var p = point.point()
var px = round(p.x*shapeFrame.width()) + round(shapeFrame.x()) + offsetX
var py = p.y*shapeFrame.height() + shapeFrame.y() + offsetY
var cp2 = point.curveTo()
if(!point.hasCurveTo()) { cp2 = p }
var cp2x = round(cp2.x*shapeFrame.width()) + round(shapeFrame.x()) + offsetX
var cp2y = round(cp2.y*shapeFrame.height()) + round(shapeFrame.y()) + offsetY
if(i == 0) {
str += "\tCGContextMoveToPoint(ctx, " + px + widthRatio + ", " + py + heightRatio + ")\n"
} else {
var cp1 = points.objectAtIndex(i - 1).curveFrom()
if(!points.objectAtIndex(i - 1).hasCurveFrom()) {
cp1 = points.objectAtIndex(i - 1).point()
}
var cp1x = round(cp1.x*shapeFrame.width()) + round(shapeFrame.x()) + offsetX
var cp1y = round(cp1.y*shapeFrame.height()) + round(shapeFrame.y()) + offsetY
str += "\tCGContextAddCurveToPoint(ctx, " + cp1x + widthRatio + ", " + cp1y + heightRatio + ", " + cp2x + widthRatio + ", " + cp2y + heightRatio + ", " + px + widthRatio + ", " + py + heightRatio + ")\n"
}
}
var p0 = points.objectAtIndex(0).point()
var p0x = round(p0.x*shapeFrame.width()) + round(shapeFrame.x()) + offsetX
var p0y = round(p0.y*shapeFrame.height()) + round(shapeFrame.y()) + offsetY
var cp2 = points.objectAtIndex(0).curveTo()
if(!points.objectAtIndex(0).hasCurveTo()) {
cp2 = points.objectAtIndex(0).point()
}
var cp2x = round(cp2.x*shapeFrame.width()) + round(shapeFrame.x()) + offsetX
var cp2y = round(cp2.y*shapeFrame.height()) + round(shapeFrame.y()) + offsetY
var cp1 = points.objectAtIndex(points.count() - 1).curveFrom()
if(!points.objectAtIndex(points.count() - 1).hasCurveFrom()) {
cp1 = points.objectAtIndex(points.count() - 1).point()
}
var cp1x = round(cp1.x*shapeFrame.width()) + round(shapeFrame.x()) + offsetX
var cp1y = round(cp1.y*shapeFrame.height()) + round(shapeFrame.y()) + offsetY
var p = points.objectAtIndex(points.count() - 1)
var pp = p.point()
log("(" + cp1x + ", " + cp1y + ")")
log("(" + cp2x + ", " + cp2y + ")")
log("(" + p0x + ", " + p0y + ")")
//draw path back to first point
str += "\tCGContextAddCurveToPoint(ctx, " + cp1x + widthRatio + ", " + cp1y + heightRatio + ", " + cp2x + widthRatio + ", " + cp2y + heightRatio + ", " + p0x + widthRatio + ", " + p0y + heightRatio + ")\n"
var stroke = extractStroke(shapeGroup, boundingWidth, boundingHeight)
var fill = extractFill(shapeGroup)
str += stroke
str += fill
var mode = null
if(stroke && fill) mode = "kCGPathFillStroke"
else if(stroke) mode = "kCGPathStroke"
else if(fill) mode = "kCGPathFill"
if(mode) {
str += "\tCGContextDrawPath(ctx, " + mode + ")\n"
}
return "\tCGContextSaveGState(ctx)\n" + str + "\tCGContextRestoreGState(ctx)\n"
}
function drawTextLayer(textLayer, boundingFrame, offsetX, offsetY) {
var str = ""
var boundingWidth = boundingFrame.width()
var boundingHeight = boundingFrame.height()
var widthRatio = "*(width/" + boundingWidth + ")"
var heightRatio = "*(height/" + boundingHeight + ")"
var frame = textLayer.frame()
var x = offsetX + frame.x()
var y = offsetY + frame.y()
var width = frame.width()
var height = frame.height()
var yHeightOffset = " + " + height + "*(1 - (height / " + boundingHeight + " ))/2"
var name = textLayer.stringValue().stringByReplacingOccurrencesOfString_withString("\n", "\\n")
str += "\"" + name + "\".drawInRect(CGRect(x:" + x + widthRatio + ", y:" + y + heightRatio + yHeightOffset + ", width:" + width + widthRatio + ", height:" + height + heightRatio + "), withAttributes: " + drawAttributes(textLayer, boundingFrame) + ")\n"
return "\tCGContextSaveGState(ctx)\n" + str + "\tCGContextRestoreGState(ctx)\n"
}
function drawAttributes(textLayer, boundingFrame) {
var boundingWidth = boundingFrame.width()
var widthRatio = "*(width/" + boundingWidth + ")"
var str = ""
var color = textLayer.textColor()
str += "NSFontAttributeName: UIFont(name: \"" + textLayer.fontPostscriptName() + "\", size: " + textLayer.fontSize() + widthRatio + ")!"
str +=", NSForegroundColorAttributeName: UIColor(hue: " + color.hue() + ", saturation: " + color.saturation() + ", brightness: " + color.brightness() + ", alpha: " + color.alpha() + ")"
if(textLayer.textAlignment() == 0) str +=", NSParagraphStyleAttributeName: pstyleLeftAlign"
else if(textLayer.textAlignment() == 1) str +=", NSParagraphStyleAttributeName: pstyleRightAlign"
else if(textLayer.textAlignment() == 2) str +=", NSParagraphStyleAttributeName: pstyleCenterAlign"
return "[" + str + "]"
}
/*
will grab the first available fill (has to be a solid color and enabled)
*/
function extractStroke(shapeGroup, boundingWidth, boundingHeight) {
var stroke = ""
var strokes = shapeGroup.style().borders()
for(var i=0; i<strokes.count(); i++) {
var item = strokes.objectAtIndex(i)
if(item.isEnabled() && item.fillType() == 0 && item.position() == 0) {
var thicknessMult = "*min(width/" + boundingWidth + ", height/" + boundingHeight + ")"
var color = item.color()
stroke = "\tCGContextSetStrokeColorWithColor(ctx, UIColor(hue: " + color.hue() + ", saturation: " + color.saturation() + ", brightness: " + color.brightness() + ", alpha: " + color.alpha() + ").CGColor)\n"
stroke += "\tCGContextSetLineWidth(ctx, " + item.thickness() + thicknessMult + ")\n"
break
}
}
return stroke
}
function extractFill(shapeGroup) {
var fill = ""
var fills = shapeGroup.style().fills()
for(var i=0; i<fills.count(); i++) {
var item = fills.objectAtIndex(i)
if(item.isEnabled() && item.fillType() == 0) {
var color = item.color()
fill = "\tCGContextSetFillColorWithColor(ctx, UIColor(hue: " + color.hue() + ", saturation: " + color.saturation() + ", brightness: " + color.brightness() + ", alpha: " + color.alpha() + ").CGColor)\n"
break
}
}
return fill
}
function safeName(layer) {
var name = layer.name()
return name.stringByReplacingOccurrencesOfString_withString_(" ", "_")
}
function round(value, decimals) {
if(decimals == null) decimals = 2
var power = Math.pow(10, decimals)
return Math.round(value * power)/power
}