Skip to content

Commit 32fd787

Browse files
committed
adding NSImage / UIImage extensions
1 parent e20ffe4 commit 32fd787

File tree

7 files changed

+148
-106
lines changed

7 files changed

+148
-106
lines changed

SwiftVG.xcodeproj/project.pbxproj

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
D9778E651E6F6DFA00FF6EBB /* Parser.PathTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9778E631E6F6DFA00FF6EBB /* Parser.PathTests.swift */; };
100100
D9778E711E7204EF00FF6EBB /* CGPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9778E701E7204EF00FF6EBB /* CGPath.swift */; };
101101
D9778E721E7204EF00FF6EBB /* CGPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9778E701E7204EF00FF6EBB /* CGPath.swift */; };
102+
D97CBC421ED503D7006B4A69 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D97CBC411ED503D7006B4A69 /* Image.swift */; };
103+
D97CBC431ED50465006B4A69 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D97CBC411ED503D7006B4A69 /* Image.swift */; };
104+
D97CBC451ED506D1006B4A69 /* UIImage+Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D97CBC441ED506D1006B4A69 /* UIImage+Image.swift */; };
105+
D97CBC481ED508F2006B4A69 /* NSImage+Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D97CBC461ED506DB006B4A69 /* NSImage+Image.swift */; };
102106
D98971FA1E88F9B7008DC621 /* CoreGraphicsRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D98971F91E88F9B7008DC621 /* CoreGraphicsRenderer.swift */; };
103107
D98971FB1E88F9B7008DC621 /* CoreGraphicsRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D98971F91E88F9B7008DC621 /* CoreGraphicsRenderer.swift */; };
104108
D98972041E892C29008DC621 /* Builder.Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = D98972001E892C29008DC621 /* Builder.Command.swift */; };
@@ -125,10 +129,6 @@
125129
D9A843941E85F1C1002A804B /* quad.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9A843921E85F1C1002A804B /* quad.svg */; };
126130
D9A843951E8611D6002A804B /* curves.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9A843901E855672002A804B /* curves.svg */; };
127131
D9A843961E8611D7002A804B /* curves.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9A843901E855672002A804B /* curves.svg */; };
128-
D9A843991E8612BD002A804B /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9A843981E8612BD002A804B /* UIImage.swift */; };
129-
D9A8439C1E861346002A804B /* NSImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9A8439A1E861340002A804B /* NSImage.swift */; };
130-
D9A8439E1E861473002A804B /* ImageLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9A8439D1E861473002A804B /* ImageLoader.swift */; };
131-
D9A8439F1E861473002A804B /* ImageLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9A8439D1E861473002A804B /* ImageLoader.swift */; };
132132
D9B118DC1E865164006AFDBD /* Renderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9B118DB1E865164006AFDBD /* Renderer.swift */; };
133133
D9B118DD1E865164006AFDBD /* Renderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9B118DB1E865164006AFDBD /* Renderer.swift */; };
134134
D9C1EEFA1E63B1B900C0F213 /* Parser.GraphicAttributeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9C1EEF91E63B1B900C0F213 /* Parser.GraphicAttributeTests.swift */; };
@@ -200,6 +200,9 @@
200200
D9778E601E6F6C0300FF6EBB /* Parser.XML.Path.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Parser.XML.Path.swift; sourceTree = "<group>"; };
201201
D9778E631E6F6DFA00FF6EBB /* Parser.PathTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Parser.PathTests.swift; sourceTree = "<group>"; };
202202
D9778E701E7204EF00FF6EBB /* CGPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPath.swift; sourceTree = "<group>"; };
203+
D97CBC411ED503D7006B4A69 /* Image.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
204+
D97CBC441ED506D1006B4A69 /* UIImage+Image.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Image.swift"; sourceTree = "<group>"; };
205+
D97CBC461ED506DB006B4A69 /* NSImage+Image.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSImage+Image.swift"; sourceTree = "<group>"; };
203206
D98971F91E88F9B7008DC621 /* CoreGraphicsRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreGraphicsRenderer.swift; sourceTree = "<group>"; };
204207
D98972001E892C29008DC621 /* Builder.Command.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Builder.Command.swift; sourceTree = "<group>"; };
205208
D98972011E892C29008DC621 /* Builder.Path.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Builder.Path.swift; sourceTree = "<group>"; };
@@ -219,9 +222,6 @@
219222
D99CC99D1E60366300E7886A /* DOM.ElementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DOM.ElementTests.swift; sourceTree = "<group>"; };
220223
D9A843901E855672002A804B /* curves.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = curves.svg; sourceTree = "<group>"; };
221224
D9A843921E85F1C1002A804B /* quad.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = quad.svg; sourceTree = "<group>"; };
222-
D9A843981E8612BD002A804B /* UIImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = "<group>"; };
223-
D9A8439A1E861340002A804B /* NSImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSImage.swift; sourceTree = "<group>"; };
224-
D9A8439D1E861473002A804B /* ImageLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageLoader.swift; sourceTree = "<group>"; };
225225
D9B118DB1E865164006AFDBD /* Renderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Renderer.swift; sourceTree = "<group>"; };
226226
D9C1EEF91E63B1B900C0F213 /* Parser.GraphicAttributeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Parser.GraphicAttributeTests.swift; sourceTree = "<group>"; };
227227
D9C1EEFB1E63C94600C0F213 /* DOM.Use.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DOM.Use.swift; sourceTree = "<group>"; };
@@ -343,6 +343,16 @@
343343
path = Renderer;
344344
sourceTree = "<group>";
345345
};
346+
D97CBC401ED503CE006B4A69 /* Image */ = {
347+
isa = PBXGroup;
348+
children = (
349+
D97CBC411ED503D7006B4A69 /* Image.swift */,
350+
D97CBC441ED506D1006B4A69 /* UIImage+Image.swift */,
351+
D97CBC461ED506DB006B4A69 /* NSImage+Image.swift */,
352+
);
353+
path = Image;
354+
sourceTree = "<group>";
355+
};
346356
D98971FF1E892C29008DC621 /* Builder */ = {
347357
isa = PBXGroup;
348358
children = (
@@ -382,16 +392,6 @@
382392
path = DOM;
383393
sourceTree = "<group>";
384394
};
385-
D9A843971E8612AB002A804B /* Image */ = {
386-
isa = PBXGroup;
387-
children = (
388-
D9A843981E8612BD002A804B /* UIImage.swift */,
389-
D9A8439A1E861340002A804B /* NSImage.swift */,
390-
D9A8439D1E861473002A804B /* ImageLoader.swift */,
391-
);
392-
path = Image;
393-
sourceTree = "<group>";
394-
};
395395
D9F3EBA71DFF5BA3006691A1 = {
396396
isa = PBXGroup;
397397
children = (
@@ -416,6 +416,7 @@
416416
D9F3EBB31DFF5BA3006691A1 /* SwiftVG */ = {
417417
isa = PBXGroup;
418418
children = (
419+
D97CBC401ED503CE006B4A69 /* Image */,
419420
D92CE6041E64E95400E0BE1B /* Extensions */,
420421
D9F3EBCD1DFF5BE1006691A1 /* SVG */,
421422
D9F3EBB41DFF5BA3006691A1 /* SwiftVG.h */,
@@ -441,7 +442,6 @@
441442
D9F3EBCD1DFF5BE1006691A1 /* SVG */ = {
442443
isa = PBXGroup;
443444
children = (
444-
D9A843971E8612AB002A804B /* Image */,
445445
D99CC9871E5F8EF400E7886A /* Scanner */,
446446
D9002B141E08B9710090BCC8 /* XML */,
447447
D958D9F31E02013B004D811F /* Formatter */,
@@ -711,8 +711,8 @@
711711
D962D2D81E6ED01700B825C6 /* Parser.XML.Use.swift in Sources */,
712712
D98971FB1E88F9B7008DC621 /* CoreGraphicsRenderer.swift in Sources */,
713713
D962D2DD1E6ED02000B825C6 /* XML.swift in Sources */,
714+
D97CBC431ED50465006B4A69 /* Image.swift in Sources */,
714715
D962D2C41E6ECFCA00B825C6 /* DOM.Element.swift in Sources */,
715-
D9A8439C1E861346002A804B /* NSImage.swift in Sources */,
716716
D963D2AB1E95BA06008DD03D /* CodeRenderer.Path.swift in Sources */,
717717
D962D2D71E6ED01700B825C6 /* Parser.XML.Text.swift in Sources */,
718718
D9B118DD1E865164006AFDBD /* Renderer.swift in Sources */,
@@ -730,14 +730,14 @@
730730
D962D2D91E6ED01700B825C6 /* Parser.XML.Image.swift in Sources */,
731731
D98972071E892C29008DC621 /* Builder.Path.swift in Sources */,
732732
D98972051E892C29008DC621 /* Builder.Command.swift in Sources */,
733+
D97CBC481ED508F2006B4A69 /* NSImage+Image.swift in Sources */,
733734
D963D2AC1E95BA0A008DD03D /* CodeRenderer.swift in Sources */,
734735
D962D2CC1E6ECFCA00B825C6 /* DOM.Switch.swift in Sources */,
735736
D962D2C71E6ECFCA00B825C6 /* DOM.Color.swift in Sources */,
736737
D989720B1E892C29008DC621 /* Builder.swift in Sources */,
737738
D9778E621E6F6C0300FF6EBB /* Parser.XML.Path.swift in Sources */,
738739
D962D2C31E6ECFCA00B825C6 /* DOM.swift in Sources */,
739740
D962D2E01E6ED02500B825C6 /* Scanner.swift in Sources */,
740-
D9A8439F1E861473002A804B /* ImageLoader.swift in Sources */,
741741
D962D2DF1E6ED02500B825C6 /* CharacterSet.swift in Sources */,
742742
);
743743
runOnlyForDeploymentPostprocessing = 0;
@@ -778,15 +778,16 @@
778778
D99CC9911E5FDCFB00E7886A /* Parser.XML.Gradient.swift in Sources */,
779779
D933D0A61E5CFF4D00937A59 /* Parser.XML.Color.swift in Sources */,
780780
D91B1E201E6E4D310028EF61 /* DOM.Image.swift in Sources */,
781+
D97CBC451ED506D1006B4A69 /* UIImage+Image.swift in Sources */,
781782
D933D0A81E5D1F7F00937A59 /* DOM.Element.swift in Sources */,
782783
D98972081E892C29008DC621 /* Builder.State.swift in Sources */,
783784
D91B1E221E6E4D650028EF61 /* Parser.XML.Image.swift in Sources */,
784785
D9D0CFCE1E027B4000B7947B /* DOM.swift in Sources */,
785786
D99CC9951E5FEA1200E7886A /* DOM.Text.swift in Sources */,
786787
D965B5841E3C4A34003E859E /* XML.Parser.swift in Sources */,
787-
D9A843991E8612BD002A804B /* UIImage.swift in Sources */,
788788
D98971FA1E88F9B7008DC621 /* CoreGraphicsRenderer.swift in Sources */,
789789
D99CC9971E5FECEB00E7886A /* Parser.XML.Text.swift in Sources */,
790+
D97CBC421ED503D7006B4A69 /* Image.swift in Sources */,
790791
D99CC9891E5F8F0000E7886A /* CharacterSet.swift in Sources */,
791792
D963D2A81E95B761008DD03D /* CodeRenderer.swift in Sources */,
792793
D933D0A11E5CEE1700937A59 /* DOM.Color.swift in Sources */,
@@ -813,7 +814,6 @@
813814
D965B58B1E4E1273003E859E /* Parser.XML.Svg.swift in Sources */,
814815
D9778E611E6F6C0300FF6EBB /* Parser.XML.Path.swift in Sources */,
815816
D958D9F51E020158004D811F /* Formatter.swift in Sources */,
816-
D9A8439E1E861473002A804B /* ImageLoader.swift in Sources */,
817817
D9C1EEFE1E63C9D300C0F213 /* Parser.XML.Use.swift in Sources */,
818818
);
819819
runOnlyForDeploymentPostprocessing = 0;

SwiftVG/Image/Image.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Image.swift
3+
// SwiftVG
4+
//
5+
// Created by Simon Whitty on 24/5/17.
6+
// Copyright © 2017 WhileLoop Pty Ltd. All rights reserved.
7+
//
8+
9+
import CoreGraphics
10+
import Foundation
11+
12+
13+
@objc(SVGImage)
14+
public final class Image: NSObject {
15+
public let size: CGSize
16+
let commands: [RendererCommand<CoreGraphicsProvider>]
17+
18+
public init?(fileURL url: URL) {
19+
guard let element = try? XML.SAXParser.parse(contentsOf: url),
20+
let svg = try? XMLParser().parseSvg(element) else {
21+
return nil
22+
}
23+
24+
size = CGSize(width: svg.width, height: svg.height)
25+
commands = Builder().createCommands(for: svg,
26+
with: CoreGraphicsProvider(),
27+
isFlipped: Image.isFlipped)
28+
}
29+
30+
public func render(in context: CGContext) {
31+
let renderer = CoreGraphicsRenderer(context: context)
32+
renderer.perform(commands)
33+
}
34+
35+
//TODO: flip the context in the render method
36+
static var isFlipped: Bool {
37+
#if os(macOS)
38+
return false
39+
#else
40+
return true
41+
#endif
42+
}
43+
}

SwiftVG/Image/NSImage+Image.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Image+NSImage.swift
3+
// SwiftVG
4+
//
5+
// Created by Simon Whitty on 24/5/17.
6+
// Copyright © 2017 WhileLoop Pty Ltd. All rights reserved.
7+
//
8+
9+
import AppKit
10+
11+
extension NSImage {
12+
public class func svgNamed(_ name: String,
13+
in bundle: Bundle = Bundle.main) -> NSImage? {
14+
15+
guard let url = bundle.url(forResource: name, withExtension: nil),
16+
let image = Image(fileURL: url) else {
17+
return nil
18+
}
19+
20+
return image.rasterize()
21+
}
22+
}
23+
24+
public extension Image {
25+
func rasterize() -> NSImage {
26+
return rasterize(with: size)
27+
}
28+
29+
func rasterize(with size: CGSize) -> NSImage {
30+
let image = NSImage(size: size)
31+
image.lockFocus()
32+
33+
if let ctx = NSGraphicsContext.current()?.cgContext {
34+
let renderer = CoreGraphicsRenderer(context: ctx)
35+
renderer.perform(commands)
36+
}
37+
38+
image.unlockFocus()
39+
return image
40+
}
41+
}

SwiftVG/Image/UIImage+Image.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Image+UIImage.swift
3+
// SwiftVG
4+
//
5+
// Created by Simon Whitty on 24/5/17.
6+
// Copyright © 2017 WhileLoop Pty Ltd. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIImage {
12+
public class func svgNamed(_ name: String,
13+
in bundle: Bundle = Bundle.main) -> UIImage? {
14+
15+
guard let url = bundle.url(forResource: name, withExtension: nil),
16+
let image = Image(fileURL: url) else {
17+
return nil
18+
}
19+
20+
return image.rasterize()
21+
}
22+
}
23+
24+
public extension Image {
25+
func rasterize() -> UIImage {
26+
return rasterize(with: size)
27+
}
28+
29+
func rasterize(with size: CGSize) -> UIImage {
30+
let f = UIGraphicsImageRendererFormat.default()
31+
f.opaque = false
32+
f.prefersExtendedRange = false
33+
let r = UIGraphicsImageRenderer(size: size, format: f)
34+
35+
let commands = self.commands
36+
37+
return r.image{
38+
let renderer = CoreGraphicsRenderer(context: $0.cgContext)
39+
renderer.perform(commands)
40+
}
41+
}
42+
}

SwiftVG/SVG/Image/ImageLoader.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.

SwiftVG/SVG/Image/NSImage.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.

SwiftVG/SVG/Image/UIImage.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)