Skip to content

Commit

Permalink
Fix test's method - to native image, add throw CommonError.invalidIma…
Browse files Browse the repository at this point in the history
…geContext
  • Loading branch information
mnndnl committed Oct 5, 2021
1 parent 2b406e5 commit d5ae291
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Macaw.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@
A7B47E44230EA402009DD7E5 /* Graphics_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = A718CD451F45C28700966E06 /* Graphics_iOS.swift */; };
A7B47E45230EA404009DD7E5 /* Common_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = A718CD431F45C28200966E06 /* Common_iOS.swift */; };
A7E675561EC4213500BD9ECB /* NodeBoundsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7E675551EC4213500BD9ECB /* NodeBoundsTests.swift */; };
A7F46DEA270B2219008DA1DF /* CommonError.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7F46DE9270B2219008DA1DF /* CommonError.swift */; };
A7F46DEB270B2219008DA1DF /* CommonError.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7F46DE9270B2219008DA1DF /* CommonError.swift */; };
C410148E1F834D290022EE44 /* style.svg in Resources */ = {isa = PBXBuildFile; fileRef = C410148D1F834D280022EE44 /* style.svg */; };
C4153A8F1F8793DE001BA5EE /* small-logo.png in Resources */ = {isa = PBXBuildFile; fileRef = C4153A8E1F8793DD001BA5EE /* small-logo.png */; };
C46E83551F94B20E00208037 /* transform.svg in Resources */ = {isa = PBXBuildFile; fileRef = C46E83541F94B20E00208037 /* transform.svg */; };
Expand Down Expand Up @@ -1260,6 +1262,7 @@
A74C832B229FB7690085A832 /* color-prop-04-t-manual-osx.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "color-prop-04-t-manual-osx.svg"; sourceTree = "<group>"; };
A74C832D229FBA4C0085A832 /* color-prop-04-t-manual-osx.reference */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "color-prop-04-t-manual-osx.reference"; sourceTree = "<group>"; };
A7E675551EC4213500BD9ECB /* NodeBoundsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NodeBoundsTests.swift; path = Bounds/NodeBoundsTests.swift; sourceTree = "<group>"; };
A7F46DE9270B2219008DA1DF /* CommonError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommonError.swift; sourceTree = "<group>"; };
C410148D1F834D280022EE44 /* style.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = style.svg; sourceTree = "<group>"; };
C4153A8E1F8793DD001BA5EE /* small-logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "small-logo.png"; sourceTree = "<group>"; };
C43B064C1F9738EF00787A35 /* clip.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = clip.svg; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1469,6 +1472,7 @@
57900FF81EA0DEBF00809FFB /* UIImage2Image.swift */,
3081E77C20DB58B100640F96 /* DescriptionExtensions.swift */,
3002331B20CC0BDD00EFD92D /* BoundsUtils.swift */,
A7F46DE9270B2219008DA1DF /* CommonError.swift */,
);
path = utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -2703,6 +2707,7 @@
57614B0A1F83D15600875933 /* DoubleInterpolation.swift in Sources */,
57614B0B1F83D15600875933 /* PathSegmentType.swift in Sources */,
57614B0C1F83D15600875933 /* AnimatableVariable.swift in Sources */,
A7F46DEB270B2219008DA1DF /* CommonError.swift in Sources */,
5B6E193C20AC58F900454E7E /* Effect.swift in Sources */,
57614B0E1F83D15600875933 /* AnimationCache.swift in Sources */,
57614B0F1F83D15600875933 /* Transform.swift in Sources */,
Expand Down Expand Up @@ -2849,6 +2854,7 @@
57E5E15B1E3B393900D1CB28 /* DoubleInterpolation.swift in Sources */,
57E5E1961E3B393900D1CB28 /* PathSegmentType.swift in Sources */,
57E5E1531E3B393900D1CB28 /* AnimatableVariable.swift in Sources */,
A7F46DEA270B2219008DA1DF /* CommonError.swift in Sources */,
5B6E193B20AC58F900454E7E /* Effect.swift in Sources */,
A7B47E3A230EA398009DD7E5 /* MCAShapeLayerLineCap_macOS.swift in Sources */,
57E5E1631E3B393900D1CB28 /* AnimationCache.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion MacawTests/MacawSVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ class MacawSVGTests: XCTestCase {
frame = Group(contents: group.contents).bounds
}

let image = node.toNativeImage(size: frame?.size() ?? Size.init(w: 100, h: 100))
let image = try node.toNativeImage(size: frame?.size() ?? Size.init(w: 100, h: 100))
return image
} catch {
XCTFail(error.localizedDescription)
Expand Down
14 changes: 10 additions & 4 deletions Source/utils/CGMappings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public extension CGAffineTransform {

public extension Node {

func toNativeImage(size: Size, layout: ContentLayout = .of()) -> MImage {
func toNativeImage(size: Size, layout: ContentLayout = .of()) throws -> MImage {
let renderer = RenderUtils.createNodeRenderer(self, view: nil)
let rect = size.rect()

Expand All @@ -146,10 +146,16 @@ public extension Node {
let transform = LayoutHelper.calcTransform(self, layout, size)
ctx.concatenate(transform.toCG())
renderer.render(in: ctx, force: false, opacity: self.opacity)

defer {
MGraphicsEndImageContext()
}

guard let img = MGraphicsGetImageFromCurrentImageContext() else {
throw CommonError.invalidImageContext
}

let img = MGraphicsGetImageFromCurrentImageContext()
MGraphicsEndImageContext()
return img!
return img
}

}
Expand Down
11 changes: 11 additions & 0 deletions Source/utils/CommonError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// CommonError.swift
// Macaw
//
// Created by Daniil Manin on 04.10.2021.
// Copyright © 2021 Exyte. All rights reserved.
//

enum CommonError: Error, Equatable {
case invalidImageContext
}

0 comments on commit d5ae291

Please sign in to comment.