From 231d1ba9828c87eef92012764d260eb7fe921aa4 Mon Sep 17 00:00:00 2001 From: Daniil Manin Date: Thu, 16 May 2019 18:09:50 +0700 Subject: [PATCH] #548 remove imageContextStack, add scale property to CGContext --- Source/platform/macOS/Common_macOS.swift | 16 ++++++++++++ Source/platform/macOS/Graphics_macOS.swift | 30 +++++++--------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Source/platform/macOS/Common_macOS.swift b/Source/platform/macOS/Common_macOS.swift index c4739d2c..87a8751a 100644 --- a/Source/platform/macOS/Common_macOS.swift +++ b/Source/platform/macOS/Common_macOS.swift @@ -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 diff --git a/Source/platform/macOS/Graphics_macOS.swift b/Source/platform/macOS/Graphics_macOS.swift index 5f93e801..57fa0fe0 100644 --- a/Source/platform/macOS/Graphics_macOS.swift +++ b/Source/platform/macOS/Graphics_macOS.swift @@ -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 {