Skip to content

Commit 8751702

Browse files
authored
Merge pull request #69 from swhitty/remove-svgImage-objc
Remove @objc SVGImage
2 parents b1d4a2d + 79a5130 commit 8751702

File tree

5 files changed

+155
-88
lines changed

5 files changed

+155
-88
lines changed

SwiftDraw/DOM.SVG+Parse.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// DOM.SVG+Parse.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 23/2/25.
6+
// Copyright 2025 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import Foundation
33+
34+
extension DOM.SVG {
35+
36+
static func parse(fileURL url: URL, options: XMLParser.Options = .skipInvalidElements) throws -> DOM.SVG {
37+
let element = try XML.SAXParser.parse(contentsOf: url)
38+
let parser = XMLParser(options: options, filename: url.lastPathComponent)
39+
return try parser.parseSVG(element)
40+
}
41+
42+
static func parse(data: Data, options: XMLParser.Options = .skipInvalidElements) throws -> DOM.SVG {
43+
let element = try XML.SAXParser.parse(data: data)
44+
let parser = XMLParser(options: options)
45+
return try parser.parseSVG(element)
46+
}
47+
}

SwiftDraw/SVG+Insets.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// SVG+Insets.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 23/2/25.
6+
// Copyright 2025 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import Foundation
33+
34+
#if canImport(CoreGraphics)
35+
import CoreGraphics
36+
#endif
37+
38+
public extension SVG {
39+
struct Insets: Equatable {
40+
public var top: CGFloat
41+
public var left: CGFloat
42+
public var bottom: CGFloat
43+
public var right: CGFloat
44+
45+
public init(
46+
top: CGFloat = 0,
47+
left: CGFloat = 0,
48+
bottom: CGFloat = 0,
49+
right: CGFloat = 0
50+
) {
51+
self.top = top
52+
self.left = left
53+
self.bottom = bottom
54+
self.right = right
55+
}
56+
57+
public static let zero = Insets(top: 0, left: 0, bottom: 0, right: 0)
58+
}
59+
}

SwiftDraw/SVG.swift

Lines changed: 45 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,56 @@ import Foundation
3434
#if canImport(CoreGraphics)
3535
import CoreGraphics
3636

37-
@objc(SVGImage)
38-
public final class SVG: NSObject {
37+
public struct SVG {
3938
public let size: CGSize
4039

4140
//An Image is simply an array of CoreGraphics draw commands
4241
//see: Renderer.swift
4342
let commands: [RendererCommand<CGTypes>]
4443

44+
public init?(fileURL url: URL, options: SVG.Options = .default) {
45+
do {
46+
let svg = try DOM.SVG.parse(fileURL: url)
47+
self.init(dom: svg, options: options)
48+
} catch {
49+
XMLParser.logParsingError(for: error, filename: url.lastPathComponent, parsing: nil)
50+
return nil
51+
}
52+
}
53+
54+
public init?(named name: String, in bundle: Bundle = Bundle.main, options: SVG.Options = .default) {
55+
guard let url = bundle.url(forResource: name, withExtension: nil) else {
56+
return nil
57+
}
58+
59+
self.init(fileURL: url, options: options)
60+
}
61+
62+
public init?(data: Data, options: SVG.Options = .default) {
63+
guard let svg = try? DOM.SVG.parse(data: data) else {
64+
return nil
65+
}
66+
67+
self.init(dom: svg, options: options)
68+
}
69+
70+
public struct Options: OptionSet {
71+
public let rawValue: Int
72+
public init(rawValue: Int) {
73+
self.rawValue = rawValue
74+
}
75+
76+
public static let hideUnsupportedFilters = Options(rawValue: 1 << 0)
77+
78+
public static let `default`: Options = []
79+
}
80+
}
81+
82+
@available(*, unavailable, renamed: "SVG")
83+
public enum Image { }
84+
85+
extension SVG {
86+
4587
init(dom: DOM.SVG, options: Options) {
4688
self.size = CGSize(width: dom.width, height: dom.height)
4789

@@ -60,25 +102,11 @@ public final class SVG: NSObject {
60102
generator.renderCommands(for: layer)
61103
)
62104
}
63-
64-
public struct Options: OptionSet {
65-
public let rawValue: Int
66-
public init(rawValue: Int) {
67-
self.rawValue = rawValue
68-
}
69-
70-
public static let hideUnsupportedFilters = Options(rawValue: 1 << 0)
71-
72-
public static let `default`: Options = []
73-
}
74105
}
75106

76-
@available(*, unavailable, renamed: "SVG")
77-
public enum Image { }
78-
79107
#else
80108

81-
public final class SVG: NSObject {
109+
public struct SVG {
82110
public let size: CGSize
83111

84112
init(dom: DOM.SVG, options: Options) {
@@ -116,69 +144,3 @@ public extension SVG {
116144
}
117145
}
118146
#endif
119-
120-
extension DOM.SVG {
121-
122-
static func parse(fileURL url: URL, options: XMLParser.Options = .skipInvalidElements) throws -> DOM.SVG {
123-
let element = try XML.SAXParser.parse(contentsOf: url)
124-
let parser = XMLParser(options: options, filename: url.lastPathComponent)
125-
return try parser.parseSVG(element)
126-
}
127-
128-
static func parse(data: Data, options: XMLParser.Options = .skipInvalidElements) throws -> DOM.SVG {
129-
let element = try XML.SAXParser.parse(data: data)
130-
let parser = XMLParser(options: options)
131-
return try parser.parseSVG(element)
132-
}
133-
}
134-
135-
public extension SVG {
136-
137-
convenience init?(fileURL url: URL, options: SVG.Options = .default) {
138-
do {
139-
let svg = try DOM.SVG.parse(fileURL: url)
140-
self.init(dom: svg, options: options)
141-
} catch {
142-
XMLParser.logParsingError(for: error, filename: url.lastPathComponent, parsing: nil)
143-
return nil
144-
}
145-
}
146-
147-
convenience init?(named name: String, in bundle: Bundle = Bundle.main, options: SVG.Options = .default) {
148-
guard let url = bundle.url(forResource: name, withExtension: nil) else {
149-
return nil
150-
}
151-
152-
self.init(fileURL: url, options: options)
153-
}
154-
155-
convenience init?(data: Data, options: SVG.Options = .default) {
156-
guard let svg = try? DOM.SVG.parse(data: data) else {
157-
return nil
158-
}
159-
160-
self.init(dom: svg, options: options)
161-
}
162-
163-
164-
struct Insets: Equatable {
165-
public var top: CGFloat
166-
public var left: CGFloat
167-
public var bottom: CGFloat
168-
public var right: CGFloat
169-
170-
public init(
171-
top: CGFloat = 0,
172-
left: CGFloat = 0,
173-
bottom: CGFloat = 0,
174-
right: CGFloat = 0
175-
) {
176-
self.top = top
177-
self.left = left
178-
self.bottom = bottom
179-
self.right = right
180-
}
181-
182-
public static let zero = Insets(top: 0, left: 0, bottom: 0, right: 0)
183-
}
184-
}

SwiftDrawTests/SVGTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import XCTest
3333
@testable import SwiftDraw
3434

35+
#if canImport(CoreGraphics)
3536
final class SVGTests: XCTestCase {
3637

3738
func testValidSVGLoads() {
@@ -46,7 +47,6 @@ final class SVGTests: XCTestCase {
4647
XCTAssertNil(SVG(named: "missing.svg", in: .test))
4748
}
4849

49-
#if canImport(CoreGraphics)
5050
func testImageRasterizes() {
5151
let image = SVG.makeLines()
5252
let rendered = image.rasterize(scale: 1)
@@ -71,7 +71,6 @@ final class SVGTests: XCTestCase {
7171
XCTAssertNoThrow(try image.jpegData())
7272
XCTAssertNoThrow(try image.pdfData())
7373
}
74-
#endif
7574

7675
#if canImport(UIKit)
7776
func testRasterize() {
@@ -86,7 +85,6 @@ final class SVGTests: XCTestCase {
8685
XCTAssertEqual(reloaded.scale, 1)
8786
}
8887
#endif
89-
9088
}
9189

9290
private extension SVG {
@@ -98,3 +96,4 @@ private extension SVG {
9896
return SVG(dom: svg, options: .default)
9997
}
10098
}
99+
#endif

SwiftDrawTests/UIImage+ImageTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ final class UIImageTests: XCTestCase {
7474
}
7575
}
7676

77-
#endif
78-
7977
private extension SVG {
8078

8179
static func parse(_ code: String) throws -> SVG {
@@ -90,3 +88,5 @@ private extension SVG {
9088
var errorDescription: String? = "Invalid SVG"
9189
}
9290
}
91+
92+
#endif

0 commit comments

Comments
 (0)