Skip to content

Commit 38b6f8d

Browse files
committed
remove @objc SVGImage
1 parent b1d4a2d commit 38b6f8d

File tree

4 files changed

+115
-84
lines changed

4 files changed

+115
-84
lines changed

SwiftDraw/CommandLine+Process.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public extension CommandLine {
111111
#endif
112112
}
113113

114-
static func makeImageInsets(for insets: CommandLine.Insets) throws -> SVG.Insets {
114+
static func makeImageInsets
115+
(for insets: CommandLine.Insets) throws -> SVG.Insets {
115116
guard !insets.isEmpty else { return .zero }
116117

117118
guard insets.top != nil,

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+CoreGraphics.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ public extension CGContext {
6161

6262
public extension SVG {
6363

64+
struct Insets: Equatable {
65+
public var top: CGFloat
66+
public var left: CGFloat
67+
public var bottom: CGFloat
68+
public var right: CGFloat
69+
70+
public init(
71+
top: CGFloat = 0,
72+
left: CGFloat = 0,
73+
bottom: CGFloat = 0,
74+
right: CGFloat = 0
75+
) {
76+
self.top = top
77+
self.left = left
78+
self.bottom = bottom
79+
self.right = right
80+
}
81+
82+
public static let zero = Insets(top: 0, left: 0, bottom: 0, right: 0)
83+
}
84+
6485
func pdfData(size: CGSize? = nil, insets: Insets = .zero) throws -> Data {
6586
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: 1, insets: insets)
6687
var mediaBox = CGRect(x: 0.0, y: 0.0, width: CGFloat(pixelsWide), height: CGFloat(pixelsHigh))

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-
}

0 commit comments

Comments
 (0)