1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-08-15 16:10:39 +03:00

#548 remove imageContextStack, add scale property to CGContext

This commit is contained in:
Daniil Manin 2019-05-16 18:09:50 +07:00
parent 34d2382d93
commit 231d1ba982
2 changed files with 25 additions and 21 deletions

View File

@ -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

View File

@ -11,8 +11,6 @@ import Foundation
#if os(OSX)
import AppKit
private var imageContextStack: [CGFloat] = []
func MGraphicsGetCurrentContext() -> CGContext? {
return NSGraphicsContext.current?.cgContext
}
@ -54,8 +52,6 @@ func MGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ sca
let height = Int(size.height * scale)
if width > 0 && height > 0 {
imageContextStack.append(scale)
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 {
@ -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.scaleBy(x: scale, y: scale)
ctx.scale = scale
MGraphicsPushContext(ctx)
}
}
func MGraphicsGetImageFromCurrentImageContext() -> MImage? {
if !imageContextStack.isEmpty {
guard let ctx = MGraphicsGetCurrentContext() else {
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
}
guard let ctx = MGraphicsGetCurrentContext(),
let theCGImage = ctx.makeImage() else {
return nil
}
return nil
let scale = ctx.scale
let size = CGSize(width: CGFloat(ctx.width) / scale, height: CGFloat(ctx.height) / scale)
let image = NSImage(cgImage: theCGImage, size: size)
return image
}
func MGraphicsEndImageContext() {
if imageContextStack.last != nil {
imageContextStack.removeLast()
MGraphicsPopContext()
}
MGraphicsPopContext()
}
func MNoIntrinsicMetric() -> CGFloat {