66
77# SwiftDraw
88
9- A Swift library for parsing and drawing SVG images to CoreGraphics contexts.
9+ A Swift library for parsing and drawing SVG images to CoreGraphics contexts. SwiftDraw can also convert an SVG into Swift source code.
1010
1111## Usage
1212
@@ -29,3 +29,71 @@ let image = NSImage(svgNamed: "sample.svg")
2929Download the latest command line tool [ here] ( https://github.com/swhitty/SwiftDraw/releases/latest/download/SwiftDraw.dmg ) .
3030
3131` $ swiftdraw sample.svg --format pdf --size 48x48 `
32+
33+
34+ #### Source code generation
35+
36+ The command line tool can also covert an SVG image into swift source code:
37+
38+ ``` xml
39+ <?xml version =" 1.0" encoding =" utf-8" ?>
40+ <svg version =" 1.1" xmlns =" http://www.w3.org/2000/svg" width =" 160" height =" 160" >
41+ <rect width =" 160" height =" 160" fill =" snow" />
42+ <path d =" m 80 20 a 50 50 0 1 0 50 50 h -50 z" fill =" pink" stroke =" black" stroke-width =" 2" />
43+ </svg >
44+ ```
45+
46+ ` $ swiftdraw simple.svg --format swift `
47+
48+ ``` swift
49+ extension UIImage {
50+ static func svgSimple () -> UIImage {
51+ let f = UIGraphicsImageRendererFormat.default ()
52+ f.opaque = false
53+ f.preferredRange = .standard
54+ return UIGraphicsImageRenderer (size : CGSize (width : 160.0 , height : 160.0 ), format : f).image {
55+ drawSVG (in : $0 .cgContext )
56+ }
57+ }
58+
59+ private static func drawSVG (in ctx : CGContext) {
60+ let rgb = CGColorSpaceCreateDeviceRGB ()
61+ let color1 = CGColor (colorSpace : rgb, components : [1.0 , 0.98039216 , 0.98039216 , 1.0 ])!
62+ ctx.setFillColor (color1)
63+ let path = CGPath (
64+ roundedRect : CGRect (x : 0.0 , y : 0.0 , width : 160.0 , height : 160.0 ),
65+ cornerWidth : 0.0 ,
66+ cornerHeight : 0.0 ,
67+ transform : nil
68+ )
69+ ctx.addPath (path)
70+ ctx.fillPath (using : .evenOdd )
71+ let color2 = CGColor (colorSpace : rgb, components : [1.0 , 0.7529412 , 0.79607844 , 1.0 ])!
72+ ctx.setFillColor (color2)
73+ let path1 = CGMutablePath ()
74+ path1.move (to : CGPoint (x : 80.0 , y : 20.0 ))
75+ path1.addCurve (to : CGPoint (x : 30.0 , y : 69.99999 ),
76+ control1 : CGPoint (x : 52.38576 , y : 20.0 ),
77+ control2 : CGPoint (x : 30.000004 , y : 42.385757 ))
78+ path1.addCurve (to : CGPoint (x : 79.99998 , y : 120.0 ),
79+ control1 : CGPoint (x : 29.999992 , y : 97.61423 ),
80+ control2 : CGPoint (x : 52.385742 , y : 119.999985 ))
81+ path1.addCurve (to : CGPoint (x : 130.0 , y : 70.00004 ),
82+ control1 : CGPoint (x : 107.61421 , y : 120.000015 ),
83+ control2 : CGPoint (x : 129.99998 , y : 97.61427 ))
84+ path1.addLine (to : CGPoint (x : 80.0 , y : 70.00004 ))
85+ path1.closeSubpath ()
86+ ctx.addPath (path1)
87+ ctx.fillPath (using : .evenOdd )
88+ ctx.setLineCap (.butt )
89+ ctx.setLineJoin (.miter )
90+ ctx.setLineWidth (2.0 )
91+ ctx.setMiterLimit (4.0 )
92+ let color3 = CGColor (colorSpace : rgb, components : [0.0 , 0.0 , 0.0 , 1.0 ])!
93+ ctx.setStrokeColor (color3)
94+ ctx.addPath (path1)
95+ ctx.strokePath ()
96+ }
97+ }
98+ ```
99+
0 commit comments