mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-11 05:05:23 +03:00
Merge pull request #574 from exyte/bug/548-ImageContextStack-is-not-safe
Remove imageContextStack, add scale property to CGContext
This commit is contained in:
commit
c8c9505b83
@ -160,4 +160,20 @@ extension MBezierPath {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension CGContext {
|
||||||
|
private struct CGContextScale {
|
||||||
|
static var _scale: CGFloat = 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
var scale: CGFloat {
|
||||||
|
get {
|
||||||
|
return CGContextScale._scale
|
||||||
|
}
|
||||||
|
|
||||||
|
set(newValue) {
|
||||||
|
CGContextScale._scale = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -11,8 +11,6 @@ import Foundation
|
|||||||
#if os(OSX)
|
#if os(OSX)
|
||||||
import AppKit
|
import AppKit
|
||||||
|
|
||||||
private var imageContextStack: [CGFloat] = []
|
|
||||||
|
|
||||||
func MGraphicsGetCurrentContext() -> CGContext? {
|
func MGraphicsGetCurrentContext() -> CGContext? {
|
||||||
return NSGraphicsContext.current?.cgContext
|
return NSGraphicsContext.current?.cgContext
|
||||||
}
|
}
|
||||||
@ -54,8 +52,6 @@ func MGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ sca
|
|||||||
let height = Int(size.height * scale)
|
let height = Int(size.height * scale)
|
||||||
|
|
||||||
if width > 0 && height > 0 {
|
if width > 0 && height > 0 {
|
||||||
imageContextStack.append(scale)
|
|
||||||
|
|
||||||
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
||||||
|
|
||||||
guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4 * width, space: colorSpace, bitmapInfo: (opaque ? CGImageAlphaInfo.noneSkipFirst.rawValue : CGImageAlphaInfo.premultipliedFirst.rawValue)) else {
|
guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4 * width, space: colorSpace, bitmapInfo: (opaque ? CGImageAlphaInfo.noneSkipFirst.rawValue : CGImageAlphaInfo.premultipliedFirst.rawValue)) else {
|
||||||
@ -64,32 +60,24 @@ func MGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ sca
|
|||||||
|
|
||||||
ctx.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(height)))
|
ctx.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(height)))
|
||||||
ctx.scaleBy(x: scale, y: scale)
|
ctx.scaleBy(x: scale, y: scale)
|
||||||
|
ctx.scale = scale
|
||||||
MGraphicsPushContext(ctx)
|
MGraphicsPushContext(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MGraphicsGetImageFromCurrentImageContext() -> MImage? {
|
func MGraphicsGetImageFromCurrentImageContext() -> MImage? {
|
||||||
if !imageContextStack.isEmpty {
|
guard let ctx = MGraphicsGetCurrentContext(),
|
||||||
guard let ctx = MGraphicsGetCurrentContext() else {
|
let theCGImage = ctx.makeImage() else {
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
let scale = imageContextStack.last!
|
|
||||||
if let theCGImage = ctx.makeImage() {
|
|
||||||
let size = CGSize(width: CGFloat(ctx.width) / scale, height: CGFloat(ctx.height) / scale)
|
|
||||||
let image = NSImage(cgImage: theCGImage, size: size)
|
|
||||||
return image
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
let scale = ctx.scale
|
||||||
return nil
|
let size = CGSize(width: CGFloat(ctx.width) / scale, height: CGFloat(ctx.height) / scale)
|
||||||
|
let image = NSImage(cgImage: theCGImage, size: size)
|
||||||
|
return image
|
||||||
}
|
}
|
||||||
|
|
||||||
func MGraphicsEndImageContext() {
|
func MGraphicsEndImageContext() {
|
||||||
if imageContextStack.last != nil {
|
MGraphicsPopContext()
|
||||||
imageContextStack.removeLast()
|
|
||||||
MGraphicsPopContext()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MNoIntrinsicMetric() -> CGFloat {
|
func MNoIntrinsicMetric() -> CGFloat {
|
||||||
|
Loading…
Reference in New Issue
Block a user