From 29eaa6ad51183fc950523c14021cc73bae121bfe Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Sat, 14 Dec 2024 01:34:07 -0500 Subject: [PATCH] Remove `OpaquePointerProvider` --- Sources/Cairo/Context.swift | 10 ++++--- Sources/Cairo/Font.swift | 20 +++++++++++-- Sources/Cairo/OpaquePointerProvider.swift | 36 ----------------------- Sources/Cairo/Pattern.swift | 8 +++-- Sources/Cairo/Surface.swift | 6 +++- 5 files changed, 34 insertions(+), 46 deletions(-) delete mode 100644 Sources/Cairo/OpaquePointerProvider.swift diff --git a/Sources/Cairo/Context.swift b/Sources/Cairo/Context.swift index 3f20d94..abe0b53 100755 --- a/Sources/Cairo/Context.swift +++ b/Sources/Cairo/Context.swift @@ -9,14 +9,12 @@ import CCairo /// Cairo Context -public final class Context: OpaquePointerOwner { +public final class Context { // MARK: - Properties public let surface: Surface - - // MARK: - Internal Properties - + internal let internalPointer: OpaquePointer // MARK: - Initialization @@ -41,6 +39,10 @@ public final class Context: OpaquePointerOwner { // MARK: - Methods + public func withUnsafePointer(_ body: (OpaquePointer) throws(E) -> R) throws(E) -> R where E: Error { + try body(internalPointer) + } + /// Makes a copy of the current state of the context and saves it on an internal stack of saved states. /// When `restore()` is called, the context will be restored to the saved state. /// Multiple calls to `save()` and `restore()` can be nested; diff --git a/Sources/Cairo/Font.swift b/Sources/Cairo/Font.swift index a6a03df..2317c84 100755 --- a/Sources/Cairo/Font.swift +++ b/Sources/Cairo/Font.swift @@ -10,7 +10,7 @@ import CCairo import CFontConfig import CFreeType -public final class ScaledFont: OpaquePointerOwner { +public final class ScaledFont { // MARK: - Properties @@ -206,6 +206,10 @@ public final class ScaledFont: OpaquePointerOwner { // MARK: - Methods + public func withUnsafePointer(_ body: (OpaquePointer) throws(E) -> R) throws(E) -> R where E: Error { + try body(internalPointer) + } + public func advances(for glyphs: [FontIndex]) -> [Int] { return self.lockFontFace { (fontFace) in @@ -253,7 +257,7 @@ public typealias FontIndex = UInt16 /// The font's face. /// /// - Note: Only compatible with FreeType and FontConfig. -public final class FontFace: OpaquePointerOwner { +public final class FontFace { // MARK: - Properties @@ -282,9 +286,15 @@ public final class FontFace: OpaquePointerOwner { public var type: cairo_font_type_t { cairo_font_face_get_type(internalPointer) // Never changes } + + // MARK: - Methods + + public func withUnsafePointer(_ body: (OpaquePointer) throws(E) -> R) throws(E) -> R where E: Error { + try body(internalPointer) + } } -public final class FontOptions: OpaquePointerOwner { +public final class FontOptions { // MARK: - Properties @@ -310,6 +320,10 @@ public final class FontOptions: OpaquePointerOwner { // MARK: - Methods + public func withUnsafePointer(_ body: (OpaquePointer) throws(E) -> R) throws(E) -> R where E: Error { + try body(internalPointer) + } + public func merge(_ other: FontOptions) { cairo_font_options_merge(internalPointer, other.internalPointer) diff --git a/Sources/Cairo/OpaquePointerProvider.swift b/Sources/Cairo/OpaquePointerProvider.swift deleted file mode 100644 index fd1311a..0000000 --- a/Sources/Cairo/OpaquePointerProvider.swift +++ /dev/null @@ -1,36 +0,0 @@ -// -// OpaquePointerProvider.swift -// Cairo -// -// Created by Rayman Rosevear on 2023/05/16. -// Copyright © 2023 PureSwift. All rights reserved. -// - -/// This file adds methods providing some extensibility to the library, giving access to the `OpaquePointer`s to the -/// underlying Cairo opaque struct pointers. - -public protocol OpaquePointerProvider { - - /// Calls the given closure with the underlying `OpaquePointer`. - /// - /// The pointer passed as an argument to `body` is valid only during the execution of `withUnsafeOpaquePointer(_:)`. - /// Do not store or return the pointer for later use. - /// - /// - Parameters: - /// - body: A closure with an opaque pointer parameter. If `body` has a return value, that value is also used as - /// the return value for the `withUnsafeOpaquePointer(_:)` method. The pointer argument is valid only for the - /// duration of the method's execution. - /// - Returns: The return value, if any, of the `body` closure parameter. - func withUnsafeOpaquePointer(_ body: (OpaquePointer) throws -> Result) rethrows -> Result -} - -/// Internally conform to this protocol to automatically become an `OpaquePointerProvider`. -internal protocol OpaquePointerOwner: OpaquePointerProvider { - var internalPointer: OpaquePointer { get } -} - -extension OpaquePointerOwner { - public func withUnsafeOpaquePointer(_ body: (OpaquePointer) throws -> Result) rethrows -> Result { - try body(self.internalPointer) - } -} diff --git a/Sources/Cairo/Pattern.swift b/Sources/Cairo/Pattern.swift index 62a2af6..91f9dd7 100755 --- a/Sources/Cairo/Pattern.swift +++ b/Sources/Cairo/Pattern.swift @@ -11,9 +11,9 @@ import CCairo /// Represents a source when drawing onto a surface. /// /// There are different subtypes of patterns, for different types of sources. -public final class Pattern: OpaquePointerOwner { +public final class Pattern { - // MARK: - Internal Properties + // MARK: - Properties internal var internalPointer: OpaquePointer @@ -105,6 +105,10 @@ public final class Pattern: OpaquePointerOwner { // MARK: - Methods + public func withUnsafePointer(_ body: (OpaquePointer) throws(E) -> R) throws(E) -> R where E: Error { + try body(internalPointer) + } + /// Adds an opaque color stop to a gradient pattern. public func addColorStop(_ offset: Double, red: Double, green: Double, blue: Double) { diff --git a/Sources/Cairo/Surface.swift b/Sources/Cairo/Surface.swift index 78d030e..93ef49d 100755 --- a/Sources/Cairo/Surface.swift +++ b/Sources/Cairo/Surface.swift @@ -8,7 +8,7 @@ import CCairo -public class Surface: OpaquePointerOwner { +public class Surface { // MARK: - Internal Properties @@ -39,6 +39,10 @@ public class Surface: OpaquePointerOwner { // MARK: - Methods + public func withUnsafePointer(_ body: (OpaquePointer) throws(E) -> R) throws(E) -> R where E: Error { + try body(internalPointer) + } + /// Do any pending drawing for the surface and also restore any temporary /// modifications cairo has made to the surface's state. /// This function must be called before switching from drawing on the surface