77
88# SwiftDraw
99
10- A Swift library for parsing and drawing SVG images to CoreGraphics contexts. SwiftDraw can also convert an SVG into Swift source code.
10+ A Swift library for parsing and drawing SVG images. SwiftDraw can also convert SVGs into SFSymbol, PNG, PDF and Swift source code.
1111
1212## Usage
1313
14+ Vector images can be easily loaded and rasterized to ` UIImage ` or ` NSImage ` :
15+
16+ ``` swift
17+ let svg = SVG (named : " sample.svg" , in : .main )!
18+ imageView.image = svg.rasterize ()
19+ ```
20+
21+ Rasterize to any size:
22+
23+ ``` swift
24+ let svg = SVG (named : " sample.svg" , in : .main )!
25+ imageView.image = svg.rasterize (with : CGSize (width : 640 , height : 480 ))
26+ ```
27+
28+ Or crop the image using insets
29+
30+ ``` swift
31+ let svg = SVG (named : " sample.svg" , in : .main )!
32+ imageView.image = svg.rasterize (insets : .init (top : 10 , left : 0 , bottom : 10 , bottom : 0 ))
33+ ```
34+
1435## iOS
1536
37+ Create a ` UIImage ` directly from an SVG within a bundle, ` Data ` or file ` URL ` .
38+
1639``` swift
1740import SwiftDraw
1841let image = UIImage (svgNamed : " sample.svg" )
1942```
2043
2144## macOS
2245
46+ Create an ` NSImage ` directly from an SVG within a bundle, ` Data ` or file ` URL ` .
47+
2348``` swift
2449import SwiftDraw
2550let image = NSImage (svgNamed : " sample.svg" )
2651```
2752
28- ### Command line tool
53+ ## Command line tool
54+
55+ The command line tool converts SVGs to other formats: PNG, JPEG, SFSymbol and Swift source code.
56+
57+ ```
58+ copyright (c) 2022 Simon Whitty
59+
60+ usage: swiftdraw <file.svg> [--format png | pdf | jpeg | swift | sfsymbol] [--size wxh] [--scale 1x | 2x | 3x]
61+
62+ <file> svg file to be processed
63+
64+ Options:
65+ --format format to output image: png | pdf | jpeg | swift | sfsymbol
66+ --size size of output image: 100x200
67+ --scale scale of output image: 1x | 2x | 3x
68+ --insets crop inset of output image: top,left,bottom,right
69+ --precision maximum number of decimal places
70+
71+ --hideUnsupportedFilters hide elements with unsupported filters.
72+
73+ Available keys for --format swift:
74+ --api api of generated code: appkit | uikit
75+
76+ Available keys for --format sfymbol:
77+ --insets alignment of regular variant: top,left,bottom,right | auto
78+ --ultralight svg file of ultralight variant
79+ --ultralightInsets alignment of ultralight variant: top,left,bottom,right | auto
80+ --black svg file of black variant
81+ --blackInsets alignment of black variant: top,left,bottom,right | auto
82+ ```
83+
84+ ``` bash
85+ $ swiftdraw simple.svg --format png --scale 3x
86+ ```
87+
88+ ``` bash
89+ $ swiftdraw simple.svg --format pdf
90+ ```
91+
92+ ** Installation:**
93+
94+ You can install the ` swiftdraw ` command-line tool on macOS using [ Homebrew] ( http://brew.sh/ ) . Assuming you already have Homebrew installed, just type:
95+
96+ ``` bash
97+ $ brew install swiftdraw
98+ ```
99+
100+ To update to the latest version once installed:
101+
102+ ``` bash
103+ $ brew upgrade swiftdraw
104+ ```
105+
106+ Alternatively download the latest command line tool [ here] ( https://github.com/swhitty/SwiftDraw/releases/latest/download/SwiftDraw.dmg ) .
29107
30- Download the latest command line tool [ here ] ( https://github.com/swhitty/SwiftDraw/releases/latest/download/SwiftDraw.dmg ) .
108+ ### SF Symbol
31109
32- ` $ swiftdraw sample.svg --format pdf --size 48x48 `
110+ Custom SF Symbols can be generated from a single SVG:
33111
112+ ``` bash
113+ $ swiftdraw key.svg --format sfsymbol
114+ ```
115+
116+ Optional variants ` --ultralight ` and ` --black ` can also be provided:
117+
118+ ``` bash
119+ $ swiftdraw key.svg --format sfsymbol --ultralight key-ultralight.svg --black key-black.svg
120+ ```
121+
122+ #### Alignment
123+
124+ By default, SwiftDraw automatically sizes and aligns the content to the template guides. SwiftDraw will output the alignment insets used:
34125
35- #### Source code generation
126+ ``` bash
127+ $ swiftdraw simple.svg --format sfsymbol --insets auto
128+ Alignment: --insets 30,30,30,30
129+ ```
130+
131+ Values can be provided in the form ` --insets top,left,bottom,right ` specifying an ` Double ` or ` auto ` for each inset.
132+
133+ ``` bash
134+ $ swiftdraw simple.svg --format sfsymbol --insets 40,auto,40,auto
135+ Alignment: --insets 40,30,40,30
136+ ```
137+
138+ ### Source code generation
36139
37140Source code can be generated using [ www.whileloop.com/swiftdraw ] ( https://www.whileloop.com/swiftdraw ) .
38141
@@ -46,7 +149,9 @@ The command line tool can also convert an SVG image into Swift source code:
46149</svg >
47150```
48151
49- ` $ swiftdraw simple.svg --format swift `
152+ ``` bash
153+ $ swiftdraw simple.svg --format swift
154+ ```
50155
51156``` swift
52157extension UIImage {
@@ -62,31 +167,31 @@ extension UIImage {
62167 private static func drawSimple (in ctx : CGContext, scale : CGSize) {
63168 ctx.scaleBy (x : scale.width , y : scale.height )
64169 let rgb = CGColorSpaceCreateDeviceRGB ()
65- let color1 = CGColor (colorSpace : rgb, components : [1.0 , 0.98039216 , 0.98039216 , 1.0 ])!
170+ let color1 = CGColor (colorSpace : rgb, components : [1 , 0.98 , 0.98 , 1 ])!
66171 ctx.setFillColor (color1)
67- ctx.fill (CGRect (x : 0.0 , y : 0.0 , width : 160.0 , height : 160.0 ))
68- let color2 = CGColor (colorSpace : rgb, components : [1.0 , 0.7529412 , 0.79607844 , 1.0 ])!
172+ ctx.fill (CGRect (x : 0 , y : 0 , width : 160 , height : 160 ))
173+ let color2 = CGColor (colorSpace : rgb, components : [1 , 0.753 , 0.796 , 1 ])!
69174 ctx.setFillColor (color2)
70175 let path = CGMutablePath ()
71- path.move (to : CGPoint (x : 80.0 , y : 20.0 ))
72- path.addCurve (to : CGPoint (x : 30.0 , y : 69.99999 ),
73- control1 : CGPoint (x : 52.38576 , y : 20.0 ),
74- control2 : CGPoint (x : 30.000004 , y : 42.385757 ))
75- path.addCurve (to : CGPoint (x : 79.99998 , y : 120.0 ),
76- control1 : CGPoint (x : 29.999992 , y : 97.61423 ),
77- control2 : CGPoint (x : 52.385742 , y : 119.999985 ))
78- path.addCurve (to : CGPoint (x : 130.0 , y : 70.00004 ),
79- control1 : CGPoint (x : 107.61421 , y : 120.000015 ),
80- control2 : CGPoint (x : 129.99998 , y : 97.61427 ))
81- path.addLine (to : CGPoint (x : 80.0 , y : 70.00004 ))
176+ path.move (to : CGPoint (x : 80 , y : 30 ))
177+ path.addCurve (to : CGPoint (x : 30 , y : 80 ),
178+ control1 : CGPoint (x : 52.39 , y : 30 ),
179+ control2 : CGPoint (x : 30 , y : 52.39 ))
180+ path.addCurve (to : CGPoint (x : 80 , y : 130 ),
181+ control1 : CGPoint (x : 30 , y : 107.61 ),
182+ control2 : CGPoint (x : 52.39 , y : 130 ))
183+ path.addCurve (to : CGPoint (x : 130 , y : 80 ),
184+ control1 : CGPoint (x : 107.61 , y : 130 ),
185+ control2 : CGPoint (x : 130 , y : 107.61 ))
186+ path.addLine (to : CGPoint (x : 80 , y : 80 ))
82187 path.closeSubpath ()
83188 ctx.addPath (path)
84189 ctx.fillPath (using : .evenOdd )
85190 ctx.setLineCap (.butt )
86191 ctx.setLineJoin (.miter )
87- ctx.setLineWidth (2.0 )
88- ctx.setMiterLimit (4.0 )
89- let color3 = CGColor (colorSpace : rgb, components : [0.0 , 0.0 , 0.0 , 1.0 ])!
192+ ctx.setLineWidth (2 )
193+ ctx.setMiterLimit (4 )
194+ let color3 = CGColor (colorSpace : rgb, components : [0 , 0 , 0 , 1 ])!
90195 ctx.setStrokeColor (color3)
91196 ctx.addPath (path)
92197 ctx.strokePath ()
0 commit comments