Skip to content

Commit

Permalink
Added CGContextWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Oct 5, 2016
1 parent f97932b commit 351fc9c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Sources/Silica/AffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct AffineTransform {

public protocol AffineTransformMath {

func applied(transform: AffineTransform) -> Self
func applying(_ transform: AffineTransform) -> Self
}

// Mutable versions
Expand All @@ -47,7 +47,7 @@ public extension AffineTransformMath {
@inline(__always)
mutating func apply(_ transform: AffineTransform) {

self = self.applied(transform: transform)
self = self.applying(transform)
}
}

Expand All @@ -56,7 +56,7 @@ public extension AffineTransformMath {
extension Point: AffineTransformMath {

@inline(__always)
public func applied(transform t: AffineTransform) -> Point {
public func applying(_ t: AffineTransform) -> Point {

return Point(x: t.a * x + t.c * y + t.t.x, y: t.b * x + t.d * y + t.t.y)
}
Expand All @@ -65,7 +65,7 @@ extension Point: AffineTransformMath {
extension Size: AffineTransformMath {

@inline(__always)
public func applied(transform: AffineTransform) -> Size {
public func applying( _ transform: AffineTransform) -> Size {

var newSize = Size(width: transform.a * width + transform.c * height, height: transform.b * width + transform.d * height)

Expand Down
48 changes: 48 additions & 0 deletions Sources/Silica/CGContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// CGContext.swift
// Silica
//
// Created by Alsey Coleman Miller on 10/5/16.
// Copyright © 2016 PureSwift. All rights reserved.
//

/// CoreGraphics compatible wrapper for `Silica.Context`.
public final class CGContextWrapper {

public let silicaContext: Silica.Context

public init(_ context: Silica.Context) {

self.silicaContext = context
}

@inline(__always)
public func beginTransparencyLayer(auxiliaryInfo: [String: Any]?) {

try! silicaContext.beginTransparencyLayer()
}

@inline(__always)
public func endTransparencyLayer() {

try! silicaContext.endTransparencyLayer()
}

@inline(__always)
public func saveGState() {

try! silicaContext.save()
}

@inline(__always)
public func restoreGState() {

try! silicaContext.restore()
}

@inline(__always)
public func clip(to rect: CGRect) {

silicaContext.clip(to: rect)
}
}
2 changes: 1 addition & 1 deletion Sources/Silica/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public final class Context {

cairoGlyph.index = UInt(element.glyph)

let userSpacePoint = element.position.applied(transform: textMatrix)
let userSpacePoint = element.position.applying(textMatrix)

cairoGlyph.x = userSpacePoint.x

Expand Down
2 changes: 1 addition & 1 deletion Sources/Silica/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 PureSwift. All rights reserved.
//

public typealias CGContext = Silica.Context
public typealias CGContext = Silica.CGContextWrapper
public typealias CGColor = Silica.Color
public typealias CGFloat = Double
public typealias CGLineCap = Silica.LineCap
Expand Down
4 changes: 2 additions & 2 deletions Sources/Silica/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public extension Font {
// calculate advances
let glyphSpaceToTextSpace = fontSize / Double(scaledFont.unitsPerEm)

return scaledFont.advances(for: glyphs).map { Size(width: (Double($0) * glyphSpaceToTextSpace) + characterSpacing, height: 0).applied(transform: textMatrix) }
return scaledFont.advances(for: glyphs).map { Size(width: (Double($0) * glyphSpaceToTextSpace) + characterSpacing, height: 0).applying(textMatrix) }
}

func positions(for advances: [Size], textMatrix: AffineTransform = AffineTransform.identity) -> [Point] {
Expand All @@ -103,7 +103,7 @@ public extension Font {
// first position is {0, 0}
for i in 1 ..< glyphPositions.count {

let textSpaceAdvance = advances[i-1].applied(transform: textMatrix)
let textSpaceAdvance = advances[i-1].applying(textMatrix)

glyphPositions[i] = Point(x: glyphPositions[i-1].x + textSpaceAdvance.width,
y: glyphPositions[i-1].y + textSpaceAdvance.height)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Silica/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public enum PathElement {

public extension Path {

mutating func add(_ rect: Rect) {
mutating func add(rect: Rect) {

let newElements: [Element] = [.moveToPoint(Point(x: rect.minX, y: rect.minY)),
.addLineToPoint(Point(x: rect.maxX, y: rect.minY)),
Expand All @@ -60,7 +60,7 @@ public extension Path {
elements.append(contentsOf: newElements)
}

mutating func add(ellipseInRect rect: Rect) {
mutating func add(ellipseIn rect: Rect) {

var p = Point()
var p1 = Point()
Expand Down
12 changes: 11 additions & 1 deletion Xcode/Silica/Silica.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
6E5041661CE8F13C0087E9A3 /* CGColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5041611CE8F13C0087E9A3 /* CGColor.swift */; };
6E5041691CE8F13C0087E9A3 /* CoreGraphics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5041641CE8F13C0087E9A3 /* CoreGraphics.swift */; };
6E572EBA1CDFE21E002E245A /* AffineTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E572EB91CDFE21E002E245A /* AffineTransform.swift */; };
6E7C02A11DA594D30089CF26 /* CGContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E7C02A01DA594D30089CF26 /* CGContext.swift */; };
6EEFFEAF1CE0797900515526 /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EEFFEAE1CE0797900515526 /* Color.swift */; };
6EEFFEB11CE07B6B00515526 /* Font.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EEFFEB01CE07B6B00515526 /* Font.swift */; };
6EEFFEB31CE07BDE00515526 /* DrawingMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EEFFEB21CE07BDE00515526 /* DrawingMode.swift */; };
Expand Down Expand Up @@ -78,6 +79,7 @@
6E5041631CE8F13C0087E9A3 /* CGPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPath.swift; sourceTree = "<group>"; };
6E5041641CE8F13C0087E9A3 /* CoreGraphics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreGraphics.swift; sourceTree = "<group>"; };
6E572EB91CDFE21E002E245A /* AffineTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AffineTransform.swift; sourceTree = "<group>"; };
6E7C02A01DA594D30089CF26 /* CGContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGContext.swift; sourceTree = "<group>"; };
6EC4D07E1D031435009E499F /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Package.swift; path = ../../Package.swift; sourceTree = "<group>"; };
6EEFFEAE1CE0797900515526 /* Color.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
6EEFFEB01CE07B6B00515526 /* Font.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Font.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -198,6 +200,7 @@
isa = PBXGroup;
children = (
6E5041641CE8F13C0087E9A3 /* CoreGraphics.swift */,
6E7C02A01DA594D30089CF26 /* CGContext.swift */,
6E5041601CE8F13C0087E9A3 /* CGAffineTransform.swift */,
6E5041611CE8F13C0087E9A3 /* CGColor.swift */,
6E5041631CE8F13C0087E9A3 /* CGPath.swift */,
Expand Down Expand Up @@ -263,7 +266,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = PureSwift;
TargetAttributes = {
6E169A4A1CE404A200C60B0B = {
Expand Down Expand Up @@ -330,6 +333,7 @@
6E339B881CDFC67A008E399E /* Path.swift in Sources */,
6EEFFEB91CE09C9300515526 /* CairoConvertible.swift in Sources */,
6E16EEAD1CE3429E00DF76A0 /* ImageSourcePNG.swift in Sources */,
6E7C02A11DA594D30089CF26 /* CGContext.swift in Sources */,
6EEFFEB71CE099D400515526 /* LineJoin.swift in Sources */,
6E16EEA81CE338B500DF76A0 /* BitmapInfo.swift in Sources */,
6E16EEA21CE2F0BF00DF76A0 /* Size.swift in Sources */,
Expand Down Expand Up @@ -399,8 +403,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -452,8 +458,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -488,6 +496,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
Expand All @@ -510,6 +519,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down

0 comments on commit 351fc9c

Please sign in to comment.