Skip to content

Commit 36b3266

Browse files
committed
xml helper
1 parent 62be73d commit 36b3266

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

Examples/Sources/GalleryView.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,33 @@ import SwiftUI
3434

3535
struct GalleryView: View {
3636

37-
var imageNames: [String] = [
38-
"avocado.svg",
39-
"angry.svg",
40-
"dish.svg",
41-
"mouth-open.svg",
42-
"sleepy.svg",
43-
"smile.svg",
44-
"snake.svg",
45-
"spider.svg",
46-
"star-struck.svg",
47-
"worried.svg",
48-
"yawning.svg",
49-
"thats-no-moon.svg",
50-
"alert.svg"
51-
]
37+
var images: [SVG] = {
38+
[
39+
"avocado.svg",
40+
"angry.svg",
41+
"dish.svg",
42+
"mouth-open.svg",
43+
"sleepy.svg",
44+
"smile.svg",
45+
"snake.svg",
46+
"spider.svg",
47+
"star-struck.svg",
48+
"worried.svg",
49+
"yawning.svg",
50+
"thats-no-moon.svg",
51+
"alert.svg"
52+
].compactMap {
53+
SVG(named: $0, in: .samples)
54+
}
55+
}()
5256

5357
var body: some View {
5458
ScrollView {
5559
LazyVStack(spacing: 20) {
56-
ForEach(imageNames, id: \.self) { name in
57-
SVGView(name, bundle: .samples)
60+
ForEach(images, id: \.self) { image in
61+
SVGView(svg: image)
5862
.aspectRatio(contentMode: .fit)
5963
.padding([.leading, .trailing], 10)
60-
// .frame(maxWidth: 320)
6164
}
6265
}
6366
.background(Color.white)

SwiftDraw/SVG.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import CoreGraphics
3737
public struct SVG: Hashable {
3838
public let size: CGSize
3939

40-
//An Image is simply an array of CoreGraphics draw commands
41-
//see: Renderer.swift
40+
// Array of commands that render the image
41+
// see: Renderer.swift
4242
let commands: [RendererCommand<CGTypes>]
4343

4444
public init?(fileURL url: URL, options: SVG.Options = .default) {
@@ -52,18 +52,17 @@ public struct SVG: Hashable {
5252
}
5353

5454
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-
55+
guard let url = bundle.url(forResource: name, withExtension: nil) else { return nil }
5956
self.init(fileURL: url, options: options)
6057
}
6158

62-
public init?(data: Data, options: SVG.Options = .default) {
63-
guard let svg = try? DOM.SVG.parse(data: data) else {
64-
return nil
65-
}
59+
public init?(xml: String, options: SVG.Options = .default) {
60+
guard let data = xml.data(using: .utf8) else { return nil }
61+
self.init(data: data)
62+
}
6663

64+
public init?(data: Data, options: SVG.Options = .default) {
65+
guard let svg = try? DOM.SVG.parse(data: data) else { return nil }
6766
self.init(dom: svg, options: options)
6867
}
6968

SwiftDrawTests/UIImage+ImageTests.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class UIImageTests: XCTestCase {
4848
}
4949

5050
func testImageSize() throws {
51-
let image = try SVG.parse(#"""
51+
let image = try SVG.parseXML(#"""
5252
<?xml version="1.0" encoding="UTF-8"?>
5353
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
5454
</svg>
@@ -76,9 +76,8 @@ final class UIImageTests: XCTestCase {
7676

7777
private extension SVG {
7878

79-
static func parse(_ code: String) throws -> SVG {
80-
guard let data = code.data(using: .utf8),
81-
let svg = SVG(data: data) else {
79+
static func parseXML(_ xml: String) throws -> SVG {
80+
guard let svg = SVG(xml: xml) else {
8281
throw InvalidSVG()
8382
}
8483
return svg

0 commit comments

Comments
 (0)