Skip to content

Commit

Permalink
[graphics] add CGContext extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
honghaoz committed Oct 6, 2024
1 parent 658f34b commit a865b77
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/honghaoz/ChouTi",
"state" : {
"branch" : "develop",
"revision" : "db4e47dbe69db57806049b7a99284809a03b573a"
"revision" : "43251518cd40350ffa0ac60c3a14c1e5cf0b9858"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/honghaoz/ChouTi",
"state" : {
"branch" : "develop",
"revision" : "db4e47dbe69db57806049b7a99284809a03b573a"
"revision" : "43251518cd40350ffa0ac60c3a14c1e5cf0b9858"
}
}
],
Expand Down
113 changes: 113 additions & 0 deletions Sources/ChouTiUI/Universal/CoreGraphics/CGContext+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// CGContext+Extensions.swift
// ChouTiUI
//
// Created by Honghao Zhang on 1/14/22.
// Copyright © 2020 Honghao Zhang.
//
// MIT License
//
// Copyright (c) 2020 Honghao Zhang (github.com/honghaoz)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//

#if canImport(AppKit)
import AppKit
#endif

#if canImport(UIKit)
import UIKit
#endif

import CoreGraphics

public extension CGContext {

/// Returns the current drawing context.
@inlinable
@inline(__always)
static var current: CGContext? {
#if os(macOS)
return NSGraphicsContext.current?.cgContext
#else
return UIGraphicsGetCurrentContext()
#endif

/// https://github.com/shaps80/GraphicsRenderer/blob/master/GraphicsRenderer/Classes/Platforms.swift
}

/// Flips the context's coordinate system vertically.
///
/// - Parameter height: The height of the context to flip.
func flipCoordinatesVertically(height: CGFloat) {
translateBy(x: 0.0, y: height)
scaleBy(x: 1.0, y: -1.0)

/// https://stackoverflow.com/questions/506622/cgcontextdrawimage-draws-image-upside-down-when-passed-uiimage-cgimage
///
/// Useful when drawing CGImage on context.
}

// Deprecated: The below implementation is not reliable.
//
/// Get the height of the context.
// private func getContextHeight() -> CGFloat {
// let contextHeight = self.height
// if contextHeight > 0 {
// return CGFloat(contextHeight)
// } else {
// // got zero height, the context is not a bitmap context
// // fallback to use the clip path to get the height
// saveGState()
//
// resetClip()
// let height = boundingBoxOfClipPath.size.height
//
// restoreGState()
//
// return height
// }
// }

/// Executes drawing operations within a saved graphics state.
///
/// Example:
/// ```swift
/// context.onPushedGraphicsState { context in
/// context.setFillColor(UIColor.red.cgColor)
/// context.fill(CGRect(x: 0, y: 0, width: 100, height: 100))
/// }
/// ```
///
/// - Parameter draw: A closure that performs drawing operations on the context.
@inlinable
@inline(__always)
func onPushedGraphicsState(_ draw: (_ context: CGContext) throws -> Void) rethrows {
saveGState()
try draw(self)
restoreGState()
}
}

/// NSView Drawing Issue on macOS Big Sur.md
/// https://gist.github.com/lukaskubanek/9a61ac71dc0db8bb04db2028f2635779

/// Cocoa Drawing Guide
/// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40003290-CH201-SW1
Loading

0 comments on commit a865b77

Please sign in to comment.