From b504b0368de794dde11c6e9da71fbd4ae4845c99 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Wed, 22 Nov 2017 04:05:08 -0500 Subject: [PATCH] Fixed Unit Tests and Swift 3 compatibility --- Package.pins | 16 +- Package.resolved | 80 +++--- Package.swift | 27 +- Sources/Silica/CGAffineTransform.swift | 7 +- Sources/Silica/CGContext.swift | 5 +- Sources/Silica/CGFont.swift | 5 +- Sources/Silica/CGPoint.swift | 19 ++ Sources/Silica/CGRect.swift | 238 ++++++++++++++++++ Sources/Silica/CGSize.swift | 19 ++ Tests/SilicaTests/FontTests.swift | 11 +- Tests/SilicaTests/StyleKitTests.swift | 16 +- .../Utilities/Cacao/UIBezierPath.swift | 159 +++++++++--- .../SilicaTests/Utilities/Cacao/UIColor.swift | 47 ++-- .../Utilities/Cacao/UIGraphics.swift | 4 +- .../SilicaTests/Utilities/Cacao/UIImage.swift | 7 +- .../Utilities/Cacao/UIRectCorner.swift | 22 ++ Tests/SilicaTests/Utilities/TestAssets.swift | 2 +- 17 files changed, 523 insertions(+), 161 deletions(-) create mode 100755 Sources/Silica/CGPoint.swift create mode 100755 Sources/Silica/CGRect.swift create mode 100755 Sources/Silica/CGSize.swift create mode 100644 Tests/SilicaTests/Utilities/Cacao/UIRectCorner.swift diff --git a/Package.pins b/Package.pins index 782233f..d2dba36 100644 --- a/Package.pins +++ b/Package.pins @@ -1,6 +1,12 @@ { "autoPin": true, "pins": [ + { + "package": "Cairo", + "reason": null, + "repositoryURL": "https://github.com/PureSwift/Cairo.git", + "version": "1.2.3" + }, { "package": "CCairo", "reason": null, @@ -17,7 +23,7 @@ "package": "CFreeType", "reason": null, "repositoryURL": "https://github.com/PureSwift/CFreeType.git", - "version": "1.0.3" + "version": "1.0.4" }, { "package": "CLCMS", @@ -25,17 +31,11 @@ "repositoryURL": "https://github.com/PureSwift/CLCMS.git", "version": "1.0.0" }, - { - "package": "Cairo", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/Cairo.git", - "version": "1.2.2" - }, { "package": "LittleCMS", "reason": null, "repositoryURL": "https://github.com/PureSwift/LittleCMS.git", - "version": "1.0.1" + "version": "1.0.2" } ], "version": 1 diff --git a/Package.resolved b/Package.resolved index d83b3b7..bd55779 100644 --- a/Package.resolved +++ b/Package.resolved @@ -2,78 +2,60 @@ "object": { "pins": [ { - "reference": { - "identity": "cairo", - "isLocal": false, - "path": "https://github.com/PureSwift/Cairo.git" - }, + "package": "CCairo", + "repositoryURL": "https://github.com/PureSwift/CCairo.git", "state": { - "branch": "master", - "revision": "9fa92e223d8f596e3c545f320f1235178dec79d2", - "version": null + "branch": null, + "revision": "1bd46ac81056766182be22fccc2cdf0e457f042f", + "version": "1.1.1" } }, { - "reference": { - "identity": "ccairo", - "isLocal": false, - "path": "https://github.com/PureSwift/CCairo.git" - }, + "package": "CFontConfig", + "repositoryURL": "https://github.com/PureSwift/CFontConfig.git", "state": { - "branch": "master", - "revision": "9828dafe5d7d4df38cc81614416c52df1c707d7d", - "version": null + "branch": null, + "revision": "e5bae817dbd1eb402f395d960485c52350b2bd88", + "version": "1.0.1" } }, { - "reference": { - "identity": "cfontconfig", - "isLocal": false, - "path": "https://github.com/PureSwift/CFontConfig.git" - }, + "package": "CFreeType", + "repositoryURL": "https://github.com/PureSwift/CFreeType.git", "state": { - "branch": "master", - "revision": "149b01e9a29e265f785dc6ab5003b4a6f1074a5f", - "version": null + "branch": null, + "revision": "079909dc68c13c700bb6bab3059ddf51f642b43a", + "version": "1.0.4" } }, { - "reference": { - "identity": "cfreetype", - "isLocal": false, - "path": "https://github.com/PureSwift/CFreeType.git" - }, + "package": "CLCMS", + "repositoryURL": "https://github.com/PureSwift/CLCMS.git", "state": { - "branch": "master", - "revision": "5d65c8825addca5f30ff2c16e8b7e6f61562f618", - "version": null + "branch": null, + "revision": "20d18d62ebc642e52758512cab59af06f2d3655e", + "version": "1.0.0" } }, { - "reference": { - "identity": "clcms", - "isLocal": false, - "path": "https://github.com/PureSwift/CLCMS.git" - }, + "package": "Cairo", + "repositoryURL": "https://github.com/PureSwift/Cairo.git", "state": { - "branch": "master", - "revision": "4808ad3fa6d244eb5a328a1b3fe0439f5954a831", - "version": null + "branch": null, + "revision": "bc207ab2e367fbafef51a47b85a7e1eb2821ce57", + "version": "1.2.3" } }, { - "reference": { - "identity": "littlecms", - "isLocal": false, - "path": "https://github.com/PureSwift/LittleCMS.git" - }, + "package": "LittleCMS", + "repositoryURL": "https://github.com/PureSwift/LittleCMS.git", "state": { - "branch": "master", - "revision": "5bfebf596b080f8c7dc5f403f2f0784cef130906", - "version": null + "branch": null, + "revision": "4143dfd3916be5b1ccd3cc46c27d939e9b63c0dd", + "version": "1.0.2" } } ] }, - "version": 2 + "version": 1 } diff --git a/Package.swift b/Package.swift index 970c1cf..f9b413f 100644 --- a/Package.swift +++ b/Package.swift @@ -1,28 +1,15 @@ -// swift-tools-version:4.0 +// swift-tools-version:3.0.2 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "Silica", - products: [ - // Products define the executables and libraries produced by a package, and make them visible to other packages. - .library( - name: "Silica", - targets: ["Silica"]), - ], - dependencies: [ - .package(url: "https://github.com/PureSwift/Cairo.git", .branch("master")), - .package(url: "https://github.com/PureSwift/LittleCMS.git", .branch("master")) - ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages which this package depends on. - .target( - name: "Silica", - dependencies: ["Cairo", "LittleCMS"]), - .testTarget( - name: "SilicaTests", - dependencies: ["Silica"]), - ] + Target(name: "Silica") + ], + dependencies: [ + .Package(url: "https://github.com/PureSwift/Cairo.git", majorVersion: 1), + .Package(url: "https://github.com/PureSwift/LittleCMS.git", majorVersion: 1) + ] ) diff --git a/Sources/Silica/CGAffineTransform.swift b/Sources/Silica/CGAffineTransform.swift index 097fde8..f5984d2 100644 --- a/Sources/Silica/CGAffineTransform.swift +++ b/Sources/Silica/CGAffineTransform.swift @@ -10,7 +10,12 @@ import Cairo import CCairo import Foundation -#if os(Linux) +#if os(macOS) + +import struct CoreGraphics.CGAffineTransform +public typealias CGAffineTransform = CoreGraphics.CGAffineTransform + +#else /// Affine Transform public struct CGAffineTransform { diff --git a/Sources/Silica/CGContext.swift b/Sources/Silica/CGContext.swift index 629b34c..e27a2a6 100644 --- a/Sources/Silica/CGContext.swift +++ b/Sources/Silica/CGContext.swift @@ -14,10 +14,7 @@ import Cairo import CCairo -import struct Foundation.CGFloat -import struct Foundation.CGPoint -import struct Foundation.CGSize -import struct Foundation.CGRect +import Foundation public final class CGContext { diff --git a/Sources/Silica/CGFont.swift b/Sources/Silica/CGFont.swift index 7d3e963..15d7b03 100644 --- a/Sources/Silica/CGFont.swift +++ b/Sources/Silica/CGFont.swift @@ -232,7 +232,10 @@ internal extension String { func substring(range: Range) -> String? { let indexRange = utf8.index(utf8.startIndex, offsetBy: range.lowerBound) ..< utf8.index(utf8.startIndex, offsetBy: range.upperBound) - return String(utf8[indexRange]) + + let substring = String(utf8[indexRange]) + + return substring } } diff --git a/Sources/Silica/CGPoint.swift b/Sources/Silica/CGPoint.swift new file mode 100755 index 0000000..83cb2b5 --- /dev/null +++ b/Sources/Silica/CGPoint.swift @@ -0,0 +1,19 @@ +// +// CGPoint.swift +// Silica +// +// Created by Alsey Coleman Miller on 6/16/17. +// + +import struct Foundation.CGFloat +import struct Foundation.CGPoint + +#if os(Linux) +public extension CGPoint { + + #if swift(>=4) + #elseif swift(>=3.0.2) + public static var zero: CGPoint { return CGPoint() } + #endif +} +#endif diff --git a/Sources/Silica/CGRect.swift b/Sources/Silica/CGRect.swift new file mode 100755 index 0000000..d8ff7d6 --- /dev/null +++ b/Sources/Silica/CGRect.swift @@ -0,0 +1,238 @@ +// +// Rect.swift +// Silica +// +// Created by Alsey Coleman Miller on 5/10/16. +// Copyright © 2016 PureSwift. All rights reserved. +// + +#if os(macOS) + import Darwin.C.math +#elseif os(Linux) + import Glibc +#endif + +import Foundation + +#if os(Linux) + +public extension CGRect { + + // MARK: - Accessors + + public var x: CGFloat { + + get { return origin.x } + + set { origin.x = newValue } + } + + public var y: CGFloat { + + get { return origin.y } + + set { origin.y = newValue } + } + + public var width: CGFloat { + + get { return size.width } + + set { size.width = newValue } + } + + public var height: CGFloat { + + get { return size.height } + + set { size.height = newValue } + } + + public var minX: CGFloat { + + return (size.width < 0) ? origin.x + size.width : origin.x + } + + public var midX: CGFloat { + + return origin.x + (size.width / 2.0) + } + + public var maxX: CGFloat { + + return (size.width < 0) ? origin.x : origin.x + size.width + } + + public var minY: CGFloat { + + return (size.height < 0) ? origin.y + size.height : origin.y + } + + public var midY: CGFloat { + + return origin.y + (size.height / 2.0) + } + + public var maxY: CGFloat { + + return (size.height < 0) ? origin.y : origin.y + size.height + } + + /// Returns a rectangle with a positive width and height. + public var standardized: CGRect { + + var rect = self + + if (rect.size.width < 0) { + rect.origin.x += rect.size.width + rect.size.width = -rect.size.width + } + + if (rect.size.height < 0) { + rect.origin.y += rect.size.height + rect.size.height = -rect.size.height + } + + return rect; + } + + /// Returns the smallest rectangle that results from converting the source rectangle values to integers. + public var integral: CGRect { + + var rect = self.standardized + + rect.size.width = ceil(rect.origin.x + rect.size.width) + rect.size.height = ceil(rect.origin.y + rect.size.height) + rect.origin.x = floor(rect.origin.x) + rect.origin.y = floor(rect.origin.y) + rect.size.width -= rect.origin.x + rect.size.height -= rect.origin.y + + return rect; + } + + /// Returns whether a rectangle has zero width or height, or is a null rectangle. + public var isEmpty: Bool { + + return size.width == 0 + || size.height == 0 + || isNull + } + + /// Returns whether the rectangle is equal to the null rectangle. + public var isNull: Bool { + + return origin.x.isNaN + || origin.y.isNaN + || size.width.isNaN + || size.height.isNaN + } + + // MARK: - Methods + + public func contains(_ point: CGPoint) -> Bool { + + return (point.x >= minX && point.x <= maxX) + && (point.y >= minY && point.y <= maxY) + } + + public func contains(_ rect: CGRect) -> Bool { + + return self == union(rect) + } + + public func union(_ rect: CGRect) -> CGRect { + + var r1 = self + var r2 = rect + + var union = CGRect() + + if r1.isEmpty { + return r2 + } + else if r2.isEmpty { + return r1 + } + + r1 = r1.standardized + r2 = r2.standardized + union.origin.x = min(r1.origin.x, r2.origin.x) + union.origin.y = min(r1.origin.y, r2.origin.y) + + var farthestPoint = CGPoint() + farthestPoint.x = max(r1.origin.x + r1.size.width, r2.origin.x + r2.size.width) + farthestPoint.y = max(r1.origin.y + r1.size.height, r2.origin.y + r2.size.height) + union.size.width = farthestPoint.x - union.origin.x + union.size.height = farthestPoint.y - union.origin.y + + return union + } + + public func intersects(_ r2: CGRect) -> Bool { + return self.intersection(r2).isNull == false + } + + /// Returns the intersection of two rectangles. + public func intersection(_ other: CGRect) -> CGRect { + + var r1 = self + var r2 = other + + var rect = CGRect() + + guard r1.isEmpty == false else { return r2 } + guard r2.isEmpty == false else { return r1 } + + r1 = r1.standardized + r2 = r2.standardized + + guard (r1.origin.x + r1.size.width <= r2.origin.x || + r2.origin.x + r2.size.width <= r1.origin.x || + r1.origin.y + r1.size.height <= r2.origin.y || + r2.origin.y + r2.size.height <= r1.origin.y) == false + else { return .null } + + rect.origin.x = (r1.origin.x > r2.origin.x ? r1.origin.x : r2.origin.x) + rect.origin.y = (r1.origin.y > r2.origin.y ? r1.origin.y : r2.origin.y) + + if (r1.origin.x + r1.size.width < r2.origin.x + r2.size.width) { + rect.size.width = r1.origin.x + r1.size.width - rect.origin.x + } else { + rect.size.width = r2.origin.x + r2.size.width - rect.origin.x + } + + + if (r1.origin.y + r1.size.height < r2.origin.y + r2.size.height) { + rect.size.height = r1.origin.y + r1.size.height - rect.origin.y + } else { + rect.size.height = r2.origin.y + r2.size.height - rect.origin.y + } + + return rect; + } + +} + +// MARK: - Swift 3 compatibility + +#if swift(>=4) + +#elseif swift(>=3.0.2) + +public extension CGRect { + + public init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) { + + self.origin = CGPoint(x: x, y: y) + self.size = CGSize(width: width, height: height) + } + + public static var zero: CGRect { return CGRect() } + + public static var null: CGRect { return CGRect(x: CGFloat.nan, y: CGFloat.nan, width: CGFloat.nan, height: CGFloat.nan) } +} + +#endif + +#endif diff --git a/Sources/Silica/CGSize.swift b/Sources/Silica/CGSize.swift new file mode 100755 index 0000000..c89b4b8 --- /dev/null +++ b/Sources/Silica/CGSize.swift @@ -0,0 +1,19 @@ +// +// CGSize.swift +// Silica +// +// Created by Alsey Coleman Miller on 6/16/17. +// + +import struct Foundation.CGFloat +import struct Foundation.CGSize + +#if os(Linux) + public extension CGSize { + + #if swift(>=4) + #elseif swift(>=3.0.2) + public static var zero: CGSize { return CGSize() } + #endif + } +#endif diff --git a/Tests/SilicaTests/FontTests.swift b/Tests/SilicaTests/FontTests.swift index 0906b6f..ea88049 100644 --- a/Tests/SilicaTests/FontTests.swift +++ b/Tests/SilicaTests/FontTests.swift @@ -15,16 +15,19 @@ final class FontTests: XCTestCase { func testCreateFont() { - let fontNames = [ - ("MicrosoftSansSerif", "Microsoft Sans Serif"), - ("MicrosoftSansSerif-Bold", "Microsoft Sans Serif"), + var fontNames = [ ("TimesNewRoman", "Times New Roman"), ("TimesNewRoman-Bold", "Times New Roman") ] + #if os(macOS) + fontNames += [("MicrosoftSansSerif", "Microsoft Sans Serif"), + ("MicrosoftSansSerif-Bold", "Microsoft Sans Serif")] + #endif + for (fontName, expectedFullName) in fontNames { - guard let font = Silica.Font(name: fontName) + guard let font = Silica.CGFont(name: fontName) else { XCTFail("Could not create font"); return } XCTAssert(font.name == font.name) diff --git a/Tests/SilicaTests/StyleKitTests.swift b/Tests/SilicaTests/StyleKitTests.swift index d193fa9..2174389 100644 --- a/Tests/SilicaTests/StyleKitTests.swift +++ b/Tests/SilicaTests/StyleKitTests.swift @@ -17,17 +17,17 @@ final class StyleKitTests: XCTestCase { ("testAdvancedShapes", testAdvancedShapes), ("testImagePNG", testImagePNG)] - private func draw(_ drawingMethod: () -> (), _ name: String, _ size: Size) { + private func draw(_ drawingMethod: () -> (), _ name: String, _ size: CGSize) { let filename = TestPath.testData + name + ".pdf" - let frame = Rect(size: size) + let frame = CGRect(origin: .zero, size: size) - let surface = try! Surface.PDF(filename: filename, width: frame.width, height: frame.height) + let surface = try! Surface.PDF(filename: filename, width: Double(frame.width), height: Double(frame.height)) - let context = try! Silica.Context(surface: surface, size: frame.size) + let context = try! Silica.CGContext(surface: surface, size: frame.size) - UIGraphicsPushContext(CGContext(context)) + UIGraphicsPushContext(context) drawingMethod() @@ -38,12 +38,12 @@ final class StyleKitTests: XCTestCase { func testSimpleShapes() { - draw(TestStyleKit.drawSimpleShapes, "simpleShapes", Size(width: 240, height: 120)) + draw(TestStyleKit.drawSimpleShapes, "simpleShapes", CGSize(width: 240, height: 120)) } func testAdvancedShapes() { - draw(TestStyleKit.drawAdvancedShapes, "advancedShapes", Size(width: 240, height: 120)) + draw(TestStyleKit.drawAdvancedShapes, "advancedShapes", CGSize(width: 240, height: 120)) } func testImagePNG() { @@ -52,7 +52,7 @@ final class StyleKitTests: XCTestCase { catch { XCTFail("Could not get test assets (\(error))"); return } - draw(TestStyleKit.drawImagePNG, "imagePNG", Size(width: 240, height: 180)) + draw(TestStyleKit.drawImagePNG, "imagePNG", CGSize(width: 240, height: 180)) } } diff --git a/Tests/SilicaTests/Utilities/Cacao/UIBezierPath.swift b/Tests/SilicaTests/Utilities/Cacao/UIBezierPath.swift index f112006..fb5ebe6 100644 --- a/Tests/SilicaTests/Utilities/Cacao/UIBezierPath.swift +++ b/Tests/SilicaTests/Utilities/Cacao/UIBezierPath.swift @@ -1,12 +1,19 @@ // // UIBezierPath.swift -// Silica +// Cacao // // Created by Alsey Coleman Miller on 5/12/16. // Copyright © 2016 PureSwift. All rights reserved. // -@testable import Silica +#if os(macOS) + import Darwin.C.math +#elseif os(Linux) + import Glibc +#endif + +import Foundation +import Silica /// The `UIBezierPath` class lets you define a path consisting of straight and curved line segments /// and render that path in your custom views. You use this class initially to specify just the geometry for your path. @@ -25,60 +32,122 @@ public final class UIBezierPath { // MARK: - Properties - public var CGPath: Path + public var cgPath: Silica.CGPath - public var lineWidth: Double = 1.0 + public var lineWidth: CGFloat = 1.0 - public var lineCapStyle: LineCap = .butt + public var lineCapStyle: Silica.CGLineCap = .butt - public var lineJoinStyle: LineJoin = .miter + public var lineJoinStyle: Silica.CGLineJoin = .miter - public var miterLimit: Double = 10 + public var miterLimit: CGFloat = 10 - public var flatness: Double = 0.6 + public var flatness: CGFloat = 0.6 public var usesEvenOddFillRule: Bool = false - public var lineDash: (phase: Double, lengths: [Double]) = (0.0, []) + public var lineDash: (phase: CGFloat, lengths: [CGFloat]) = (0.0, []) // MARK: - Initialization - public init(CGPath path: Path = Path()) { + public init(cgPath path: Silica.CGPath = CGPath()) { - self.CGPath = path + self.cgPath = path } - public init(rect: Rect) { + public init(rect: CGRect) { - var path = Path() + var path = CGPath() path.addRect(rect) - self.CGPath = path + self.cgPath = path } - public init(ovalIn rect: Rect) { + public init(ovalIn rect: CGRect) { - var path = Path() + var path = CGPath() path.addEllipse(in: rect) - self.CGPath = path + self.cgPath = path } + public convenience init(roundedRect rect: CGRect, cornerRadius: CGFloat) { + + self.init(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)) + } + + public init(roundedRect rect: CGRect, byRoundingCorners corners: UIRectCorner, cornerRadii: CGSize) { + + var path = CGPath() + + func addCurve(_ control1: CGPoint, _ control2: CGPoint, _ end: CGPoint) { + + path.addCurve(to: end, control1: control1, control2: control2) + } + + let topLeft = rect.origin + let topRight = CGPoint(x: rect.maxX, y: rect.minY) + let bottomRight = CGPoint(x: rect.maxX, y: rect.maxY) + let bottomLeft = CGPoint(x: rect.minX, y: rect.maxY) + + if corners.contains(.topLeft) { + path.move(to: CGPoint(x: topLeft.x+cornerRadii.width, y:topLeft.y)) + } else { + path.move(to: CGPoint(x: topLeft.x, y:topLeft.y)) + } + if corners.contains(.topRight) { + path.addLine(to: CGPoint(x: topRight.x-cornerRadii.width, y: topRight.y)) + addCurve(CGPoint(x: topRight.x, y: topRight.y), + CGPoint(x: topRight.x, y: topRight.y+cornerRadii.height), + CGPoint(x: topRight.x, y: topRight.y+cornerRadii.height)) + } else { + path.addLine(to: CGPoint(x: topRight.x, y: topRight.y)) + } + if corners.contains(.bottomRight) { + path.addLine(to: CGPoint(x: bottomRight.x, y: bottomRight.y-cornerRadii.height)) + addCurve(CGPoint(x: bottomRight.x, y: bottomRight.y), + CGPoint(x: bottomRight.x-cornerRadii.width, y: bottomRight.y), + CGPoint(x: bottomRight.x-cornerRadii.width, y: bottomRight.y)) + } else { + path.addLine(to: CGPoint(x: bottomRight.x, y: bottomRight.y)) + } + if corners.contains(.bottomLeft) { + path.addLine(to: CGPoint(x: bottomLeft.x+cornerRadii.width, y: bottomLeft.y)) + addCurve(CGPoint(x: bottomLeft.x, y: bottomLeft.y), + CGPoint(x: bottomLeft.x, y: bottomLeft.y-cornerRadii.height), + CGPoint(x:bottomLeft.x, y: bottomLeft.y-cornerRadii.height)) + } else { + path.addLine(to: CGPoint(x: bottomLeft.x, y: bottomLeft.y)) + } + if corners.contains(.topLeft) { + path.addLine(to: CGPoint(x: topLeft.x, y: topLeft.y+cornerRadii.height)) + addCurve(CGPoint(x: topLeft.x, y: topLeft.y), + CGPoint(x: topLeft.x+cornerRadii.width, y: topLeft.y), + CGPoint(x: topLeft.x+cornerRadii.width, y: topLeft.y)) + } else { + path.addLine(to: CGPoint(x: topLeft.x, y: topLeft.y)) + } + + path.closeSubpath() + + self.cgPath = path + } + // MARK: - Accessors - public var currentPoint: Point { + public var currentPoint: CGPoint { fatalError("Not implemented") } public var isEmpty: Bool { - return CGPath.elements.isEmpty + return cgPath.elements.isEmpty } - public var bounds: Rect { + public var bounds: CGRect { fatalError("Not implemented") } @@ -89,23 +158,34 @@ public final class UIBezierPath { public func fill() { - guard let context = UIGraphicsGetCurrentContext()?.silicaContext + guard let context = UIGraphicsGetCurrentContext() else { return } - try! context.save() - + context.saveGState() setContextPath() + let fillRule: Silica.CGPathFillRule = usesEvenOddFillRule ? .evenOdd : .winding + context.fillPath(using: fillRule) + context.beginPath() + context.restoreGState() + } + + public func stroke() { + + guard let context = UIGraphicsGetCurrentContext() + else { return } - try! context.fill(evenOdd: usesEvenOddFillRule) + context.saveGState() + setContextPath() + context.strokePath() context.beginPath() - try! context.restore() + context.restoreGState() } // MARK: Clipping Paths public func addClip() { - guard let context = UIGraphicsGetCurrentContext()?.silicaContext + guard let context = UIGraphicsGetCurrentContext() else { return } setContextPath() @@ -115,40 +195,45 @@ public final class UIBezierPath { // MARK: - Constructing a Path - public func move(to point: Point) { + public func move(to point: CGPoint) { - CGPath.elements.append(.moveToPoint(point)) + cgPath.elements.append(.moveToPoint(point)) } - public func addLine(to point: Point) { + public func addLine(to point: CGPoint) { - CGPath.elements.append(.addLineToPoint(point)) + cgPath.elements.append(.addLineToPoint(point)) } - public func addCurve(to endPoint: Point, controlPoint1: Point, controlPoint2: Point) { + public func addCurve(to endPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) { - CGPath.elements.append(.addCurveToPoint(controlPoint1, controlPoint2, endPoint)) + cgPath.elements.append(.addCurveToPoint(controlPoint1, controlPoint2, endPoint)) } - public func addQuadCurve(to endPoint: Point, controlPoint: Point) { + public func addQuadCurve(to endPoint: CGPoint, controlPoint: CGPoint) { - CGPath.elements.append(.addQuadCurveToPoint(controlPoint, endPoint)) + cgPath.elements.append(.addQuadCurveToPoint(controlPoint, endPoint)) } public func close() { - CGPath.elements.append(.closeSubpath) + cgPath.elements.append(.closeSubpath) + } + + public func addArc(with center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) { + + fatalError("Not implemented") } // MARK: - Private Methods private func setContextPath() { - guard let context = UIGraphicsGetCurrentContext()?.silicaContext + guard let context = UIGraphicsGetCurrentContext() else { return } context.beginPath() - context.add(path: CGPath) + context.addPath(cgPath) context.lineWidth = lineWidth context.lineCap = lineCapStyle context.lineJoin = lineJoinStyle diff --git a/Tests/SilicaTests/Utilities/Cacao/UIColor.swift b/Tests/SilicaTests/Utilities/Cacao/UIColor.swift index ffcb161..ace0f52 100644 --- a/Tests/SilicaTests/Utilities/Cacao/UIColor.swift +++ b/Tests/SilicaTests/Utilities/Cacao/UIColor.swift @@ -6,43 +6,44 @@ // Copyright © 2016 PureSwift. All rights reserved. // +import Foundation @testable import Silica public final class UIColor { // MARK: - Properties - public let CGColor: Color + public let cgColor: Silica.CGColor // MARK: - Initialization - public init(cgColor color: Color) { + public init(cgColor color: Silica.CGColor) { - self.CGColor = color + self.cgColor = color } /// An initialized color object. The color information represented by this object is in the device RGB colorspace. - public init(red: Double, - green: Double, - blue: Double, - alpha: Double = 1.0) { + public init(red: CGFloat, + green: CGFloat, + blue: CGFloat, + alpha: CGFloat = 1.0) { - self.CGColor = Color(red: red, green: green, blue: blue, alpha: alpha) + self.cgColor = Silica.CGColor(red: red, green: green, blue: blue, alpha: alpha) } // MARK: - Methods // MARK: Retrieving Color Information - public func getRed(_ red: inout Double, - green: inout Double, - blue: inout Double, - alpha: inout Double) -> Bool { + public func getRed(_ red: inout CGFloat, + green: inout CGFloat, + blue: inout CGFloat, + alpha: inout CGFloat) -> Bool { - red = CGColor.red - green = CGColor.green - blue = CGColor.blue - alpha = CGColor.alpha + red = cgColor.red + green = cgColor.green + blue = cgColor.blue + alpha = cgColor.alpha return true } @@ -59,24 +60,24 @@ public final class UIColor { /// Sets the color of subsequent fill operations to the color that the receiver represents. public func setFill() { - UIGraphicsGetCurrentContext()?.silicaContext.fillColor = CGColor + UIGraphicsGetCurrentContext()?.fillColor = self.cgColor } /// Sets the color of subsequent stroke operations to the color that the receiver represents. public func setStroke() { - UIGraphicsGetCurrentContext()?.silicaContext.strokeColor = CGColor + UIGraphicsGetCurrentContext()?.strokeColor = self.cgColor } // MARK: - Singletons - public static let red = UIColor(cgColor: Color.red) + public static let red = UIColor(cgColor: .red) - public static let green = UIColor(cgColor: Color.green) + public static let green = UIColor(cgColor: .green) - public static let blue = UIColor(cgColor: Color.blue) + public static let blue = UIColor(cgColor: .blue) - public static let white = UIColor(cgColor: Color.white) + public static let white = UIColor(cgColor: .white) - public static let black = UIColor(cgColor: Color.black) + public static let black = UIColor(cgColor: .black) } diff --git a/Tests/SilicaTests/Utilities/Cacao/UIGraphics.swift b/Tests/SilicaTests/Utilities/Cacao/UIGraphics.swift index 72ad020..4e86901 100644 --- a/Tests/SilicaTests/Utilities/Cacao/UIGraphics.swift +++ b/Tests/SilicaTests/Utilities/Cacao/UIGraphics.swift @@ -1,12 +1,12 @@ // // UIGraphics.swift -// Silica +// Cacao // // Created by Alsey Coleman Miller on 5/12/16. // Copyright © 2016 PureSwift. All rights reserved. // -@testable import Silica +import Silica /// Returns the current graphics context. /// diff --git a/Tests/SilicaTests/Utilities/Cacao/UIImage.swift b/Tests/SilicaTests/Utilities/Cacao/UIImage.swift index a50fd6e..0228764 100644 --- a/Tests/SilicaTests/Utilities/Cacao/UIImage.swift +++ b/Tests/SilicaTests/Utilities/Cacao/UIImage.swift @@ -6,19 +6,20 @@ // // +import Foundation import Silica public final class UIImage { - public let cgImage: CGImage + public let cgImage: Silica.CGImage - public init(cgImage: CGImage) { + public init(cgImage: Silica.CGImage) { self.cgImage = cgImage } public var size: CGSize { - return Size(width: CGFloat(cgImage.width), height: CGFloat(cgImage.height)) + return CGSize(width: CGFloat(cgImage.width), height: CGFloat(cgImage.height)) } } diff --git a/Tests/SilicaTests/Utilities/Cacao/UIRectCorner.swift b/Tests/SilicaTests/Utilities/Cacao/UIRectCorner.swift new file mode 100644 index 0000000..97da097 --- /dev/null +++ b/Tests/SilicaTests/Utilities/Cacao/UIRectCorner.swift @@ -0,0 +1,22 @@ +// +// UIRectCorner.swift +// Cacao +// +// Created by Alsey Coleman Miller on 6/15/17. +// + +/// +public struct UIRectCorner: OptionSet { + + public let rawValue: Int + + public init(rawValue: Int) { + self.rawValue = rawValue + } + + public static let topLeft = UIRectCorner(rawValue: 1 << 0) + public static let topRight = UIRectCorner(rawValue: 1 << 1) + public static let bottomLeft = UIRectCorner(rawValue: 1 << 2) + public static let bottomRight = UIRectCorner(rawValue: 1 << 3) + public static let allCorners = UIRectCorner(rawValue: ~0) +} diff --git a/Tests/SilicaTests/Utilities/TestAssets.swift b/Tests/SilicaTests/Utilities/TestAssets.swift index 05a2233..eed8958 100644 --- a/Tests/SilicaTests/Utilities/TestAssets.swift +++ b/Tests/SilicaTests/Utilities/TestAssets.swift @@ -114,7 +114,7 @@ extension UIImage { let fileURL = TestAssetManager.shared.cacheURL(for: name) guard let data = try? Data(contentsOf: fileURL), - let imageSource = ImageSourcePNG(data: data) + let imageSource = CGImageSourcePNG(data: data) else { return nil } let image = imageSource[0]