Skip to content

Commit eadb3b8

Browse files
committed
Deprecate SwiftDraw.Image
1 parent c3d9321 commit eadb3b8

14 files changed

+176
-169
lines changed

Examples/Sources/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ class ViewController: UIViewController {
6565

6666
override func loadView() {
6767
let imageView = UIImageView(frame: UIScreen.main.bounds)
68-
imageView.image = Image(named: "stylesheet.svg", in: .samples)?.rasterize()
68+
imageView.image = SVG(named: "stylesheet.svg", in: .samples)?.rasterize()
6969
imageView.contentMode = .scaleAspectFit
7070
imageView.backgroundColor = .white
7171
self.view = imageView
7272
}
7373
}
7474

75-
private extension Image {
75+
private extension SVG {
7676

7777
// UIImage backed with PDF preserves vector data.
7878

SwiftDraw/CGTextRenderer+Code.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public extension CGTextRenderer {
3838
static func render(named name: String,
3939
in bundle: Bundle = Bundle.main,
4040
size: Size? = nil,
41-
options: Image.Options,
41+
options: SVG.Options,
4242
api: CGTextRenderer.API,
4343
precision: Int) throws -> String {
4444
guard let url = bundle.url(forResource: name, withExtension: nil) else {
@@ -47,7 +47,7 @@ public extension CGTextRenderer {
4747
return try render(fileURL: url, size: size, options: options, api: api, precision: precision)
4848
}
4949

50-
static func render(fileURL: URL, size: Size? = nil, options: Image.Options, api: CGTextRenderer.API, precision: Int) throws -> String {
50+
static func render(fileURL: URL, size: Size? = nil, options: SVG.Options, api: CGTextRenderer.API, precision: Int) throws -> String {
5151
let svg = try DOM.SVG.parse(fileURL: fileURL)
5252
let size = makeSize(svg: svg, size: size)
5353
let identifier = fileURL.lastPathComponent
@@ -65,7 +65,7 @@ public extension CGTextRenderer {
6565
precision: precision)
6666
}
6767

68-
static func render(data: Data, options: Image.Options, api: CGTextRenderer.API, precision: Int) throws -> String {
68+
static func render(data: Data, options: SVG.Options, api: CGTextRenderer.API, precision: Int) throws -> String {
6969
let svg = try DOM.SVG.parse(data: data)
7070
let size = makeSize(svg: svg, size: nil)
7171
return cgCodeText(api: api,
@@ -94,7 +94,7 @@ public extension CGTextRenderer {
9494
name: String,
9595
svg: DOM.SVG,
9696
size: LayerTree.Size,
97-
options: Image.Options,
97+
options: SVG.Options,
9898
precision: Int) -> String {
9999
let layer = LayerTree.Builder(svg: svg).makeLayer()
100100
let commandSize = LayerTree.Size(svg.width, svg.height)

SwiftDraw/CommandLine+Process.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public extension CommandLine {
6262
return svg.data(using: .utf8)!
6363
case .jpeg, .pdf, .png:
6464
#if canImport(CoreGraphics)
65-
let options = makeImageOptions(for: config)
66-
guard let image = SwiftDraw.Image(fileURL: config.input, options: options) else {
65+
let options = makeSVGOptions(for: config)
66+
guard let image = SVG(fileURL: config.input, options: options) else {
6767
throw Error.invalid
6868
}
6969
return try processImage(image, with: config)
@@ -74,7 +74,7 @@ public extension CommandLine {
7474
}
7575
}
7676

77-
static func makeImageOptions(for config: Configuration) -> Image.Options {
77+
static func makeSVGOptions(for config: Configuration) -> SVG.Options {
7878
var options = config.options
7979
options.insert(.commandLine)
8080
if config.format == .pdf {
@@ -91,7 +91,7 @@ public extension CommandLine {
9191
}
9292
}
9393

94-
static func processImage(_ image: SwiftDraw.Image, with config: Configuration) throws -> Data {
94+
static func processImage(_ image: SVG, with config: Configuration) throws -> Data {
9595
#if canImport(CoreGraphics)
9696
switch config.format {
9797
case .jpeg:
@@ -111,7 +111,7 @@ public extension CommandLine {
111111
#endif
112112
}
113113

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

117117
guard insets.top != nil,
@@ -120,7 +120,7 @@ public extension CommandLine {
120120
insets.bottom != nil else {
121121
throw Error.unsupported
122122
}
123-
return Image.Insets(
123+
return SVG.Insets(
124124
top: insets.top!,
125125
left: insets.left!,
126126
bottom: insets.bottom!,

SwiftDraw/CommandLine.Configuration.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension CommandLine {
4545
public var insetsUltralight: Insets?
4646
public var insetsBlack: Insets?
4747
public var scale: Scale
48-
public var options: Image.Options
48+
public var options: SVG.Options
4949
public var precision: Int?
5050
}
5151

@@ -226,8 +226,8 @@ extension CommandLine {
226226
)
227227
}
228228

229-
static func parseOptions(from modifiers: [CommandLine.Modifier: String?]) throws -> Image.Options {
230-
var options: Image.Options = .default
229+
static func parseOptions(from modifiers: [CommandLine.Modifier: String?]) throws -> SVG.Options {
230+
var options: SVG.Options = .default
231231

232232
if modifiers.keys.contains(.hideUnsupportedFilters) {
233233
options.insert(.hideUnsupportedFilters)
@@ -247,7 +247,7 @@ private extension XMLParser.Scanner {
247247
}
248248
}
249249

250-
extension Image.Options {
250+
extension SVG.Options {
251251
static let disableTransparencyLayers = Self(rawValue: 1 << 8)
252252
static let commandLine = Self(rawValue: 1 << 9)
253253
}

SwiftDraw/Image+CoreGraphics.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Foundation
3535

3636
public extension CGContext {
3737

38-
func draw(_ image: Image, in rect: CGRect? = nil) {
38+
func draw(_ image: SVG, in rect: CGRect? = nil) {
3939
let defaultRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
4040
let renderer = CGRenderer(context: self)
4141

@@ -59,7 +59,7 @@ public extension CGContext {
5959
}
6060
}
6161

62-
public extension Image {
62+
public extension SVG {
6363

6464
func pdfData(size: CGSize? = nil, insets: Insets = .zero) throws -> Data {
6565
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: 1, insets: insets)
@@ -90,7 +90,7 @@ public extension Image {
9090
}
9191
}
9292

93-
extension Image {
93+
extension SVG {
9494

9595
static func makeBounds(size: CGSize?,
9696
defaultSize: CGSize,
@@ -121,7 +121,7 @@ extension Image {
121121
}
122122
}
123123

124-
private extension Image.Insets {
124+
private extension SVG.Insets {
125125
func applying(sx: CGFloat, sy: CGFloat) -> Self {
126126
Self(
127127
top: top * sy,

SwiftDraw/Image.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,24 @@ import Foundation
3535
import CoreGraphics
3636

3737
@objc(SVGImage)
38-
public final class Image: NSObject {
38+
public final class SVG: NSObject {
3939
public let size: CGSize
4040

4141
//An Image is simply an array of CoreGraphics draw commands
4242
//see: Renderer.swift
4343
let commands: [RendererCommand<CGTypes>]
4444

45-
init(svg: DOM.SVG, options: Options) {
46-
size = CGSize(width: svg.width, height: svg.height)
45+
init(dom: DOM.SVG, options: Options) {
46+
self.size = CGSize(width: dom.width, height: dom.height)
4747

4848
//To create the draw commands;
4949
// - XML is parsed into DOM.SVG
5050
// - DOM.SVG is converted into a LayerTree
5151
// - LayerTree is converted into RenderCommands
5252
// - RenderCommands are performed by Renderer (drawn to CGContext)
53-
let layer = LayerTree.Builder(svg: svg).makeLayer()
53+
let layer = LayerTree.Builder(svg: dom).makeLayer()
5454
let generator = LayerTree.CommandGenerator(provider: CGProvider(),
55-
size: LayerTree.Size(svg.width, svg.height),
55+
size: LayerTree.Size(dom.width, dom.height),
5656
options: options)
5757

5858
let optimizer = LayerTree.CommandOptimizer<CGTypes>()
@@ -72,13 +72,17 @@ public final class Image: NSObject {
7272
public static let `default`: Options = []
7373
}
7474
}
75+
76+
@available(*, deprecated, renamed: "SVG")
77+
public typealias Image = SVG
78+
7579
#else
7680

77-
public final class Image: NSObject {
81+
public final class SVG: NSObject {
7882
public let size: CGSize
7983

80-
init(svg: DOM.SVG, options: Options) {
81-
size = CGSize(width: svg.width, height: svg.height)
84+
init(dom: DOM.SVG, options: Options) {
85+
size = CGSize(width: dom.width, height: dom.height)
8286
}
8387

8488
public struct Options: OptionSet {
@@ -93,7 +97,7 @@ public final class Image: NSObject {
9397
}
9498
}
9599

96-
public extension Image {
100+
public extension SVG {
97101

98102
func pngData(size: CGSize? = nil, scale: CGFloat = 1) -> Data? {
99103
return nil
@@ -128,32 +132,32 @@ extension DOM.SVG {
128132
}
129133
}
130134

131-
public extension Image {
135+
public extension SVG {
132136

133-
convenience init?(fileURL url: URL, options: Image.Options = .default) {
137+
convenience init?(fileURL url: URL, options: SVG.Options = .default) {
134138
do {
135139
let svg = try DOM.SVG.parse(fileURL: url)
136-
self.init(svg: svg, options: options)
140+
self.init(dom: svg, options: options)
137141
} catch {
138142
XMLParser.logParsingError(for: error, filename: url.lastPathComponent, parsing: nil)
139143
return nil
140144
}
141145
}
142146

143-
convenience init?(named name: String, in bundle: Bundle = Bundle.main, options: Image.Options = .default) {
147+
convenience init?(named name: String, in bundle: Bundle = Bundle.main, options: SVG.Options = .default) {
144148
guard let url = bundle.url(forResource: name, withExtension: nil) else {
145149
return nil
146150
}
147151

148152
self.init(fileURL: url, options: options)
149153
}
150154

151-
convenience init?(data: Data, options: Image.Options = .default) {
155+
convenience init?(data: Data, options: SVG.Options = .default) {
152156
guard let svg = try? DOM.SVG.parse(data: data) else {
153157
return nil
154158
}
155159

156-
self.init(svg: svg, options: options)
160+
self.init(dom: svg, options: options)
157161
}
158162

159163

SwiftDraw/LayerTree.CommandGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ extension LayerTree {
3939
let provider: P
4040
let size: LayerTree.Size
4141
let scale: LayerTree.Float
42-
let options: SwiftDraw.Image.Options
42+
let options: SVG.Options
4343

4444
private var hasLoggedFilterWarning = false
4545
private var hasLoggedGradientWarning = false
4646
private var hasLoggedMaskWarning = false
4747

48-
init(provider: P, size: LayerTree.Size, scale: LayerTree.Float = 3.0, options: SwiftDraw.Image.Options) {
48+
init(provider: P, size: LayerTree.Size, scale: LayerTree.Float = 3.0, options: SVG.Options) {
4949
self.provider = provider
5050
self.size = size
5151
self.scale = scale

SwiftDraw/NSImage+Image.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ import CoreGraphics
3535

3636
public extension NSImage {
3737

38-
convenience init?(svgNamed name: String, in bundle: Bundle = .main, options: Image.Options = .default) {
39-
guard let image = Image(named: name, in: bundle, options: options) else { return nil }
38+
convenience init?(svgNamed name: String, in bundle: Bundle = .main, options: SVG.Options = .default) {
39+
guard let image = SVG(named: name, in: bundle, options: options) else { return nil }
4040
self.init(image)
4141
}
4242

4343
@objc(initWithSVGData:)
4444
convenience init?(_ data: Data) {
45-
guard let image = Image(data: data) else { return nil }
45+
guard let image = SVG(data: data) else { return nil }
4646
self.init(image)
4747
}
4848

4949
@objc(initWithContentsOfSVGFile:)
5050
convenience init?(contentsOfSVGFile path: String) {
51-
guard let image = Image(fileURL: URL(fileURLWithPath: path)) else { return nil }
51+
guard let image = SVG(fileURL: URL(fileURLWithPath: path)) else { return nil }
5252
self.init(image)
5353
}
5454

@@ -62,7 +62,7 @@ public extension NSImage {
6262
NSImage(svgNamed: name, in: bundle)
6363
}
6464

65-
convenience init(_ image: Image) {
65+
convenience init(_ image: SVG) {
6666
self.init(size: image.size, flipped: true) { rect in
6767
guard let ctx = NSGraphicsContext.current?.cgContext else { return false }
6868
ctx.draw(image, in: CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height))
@@ -71,20 +71,23 @@ public extension NSImage {
7171
}
7272
}
7373

74-
public extension Image {
74+
public extension SVG {
7575
func rasterize() -> NSImage {
7676
return rasterize(with: size)
7777
}
7878

79-
func rasterize(with size: CGSize) -> NSImage {
80-
let imageSize = NSSize(width: size.width, height: size.height)
79+
func rasterize(with size: CGSize? = nil, scale: CGFloat = 0) -> NSImage {
80+
let scale = scale == 0 ? (NSScreen.main?.backingScaleFactor ?? 1.0) : scale
81+
let size = size ?? self.size
82+
let imageSize = NSSize(width: size.width * scale, height: size.height * scale)
8183

8284
let image = NSImage(size: imageSize, flipped: true) { rect in
8385
guard let ctx = NSGraphicsContext.current?.cgContext else { return false }
8486
ctx.draw(self, in: CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height))
8587
return true
8688
}
8789

90+
8891
return image
8992
}
9093

@@ -133,7 +136,7 @@ public extension Image {
133136
}
134137
}
135138

136-
extension Image {
139+
extension SVG {
137140

138141
func makeBounds(size: CGSize?, scale: CGFloat, insets: Insets) -> (bounds: CGRect, pixelsWide: Int, pixelsHigh: Int) {
139142
let scale = scale == 0 ? (NSScreen.main?.backingScaleFactor ?? 1.0) : scale

SwiftDraw/Renderer.SFSymbol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ import Foundation
3333

3434
public struct SFSymbolRenderer {
3535

36-
private let options: Image.Options
36+
private let options: SVG.Options
3737
private let insets: CommandLine.Insets
3838
private let insetsUltralight: CommandLine.Insets
3939
private let insetsBlack: CommandLine.Insets
4040
private let formatter: CoordinateFormatter
4141

42-
public init(options: Image.Options,
42+
public init(options: SVG.Options,
4343
insets: CommandLine.Insets,
4444
insetsUltralight: CommandLine.Insets,
4545
insetsBlack: CommandLine.Insets,

0 commit comments

Comments
 (0)