-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate Swift code from thorvg-swift #2
Changes from 15 commits
2362b5f
bc83e74
dfc52f5
5440e25
7de9978
0965d37
5cf39e8
bd4ec6c
5432b3f
81d91e9
7116c40
e285f0e
5816d2f
0539567
2144bbe
9871ef7
e17efd7
ceca80c
6efc77b
3150a0d
1779463
034db8e
55a2969
0f7f0a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## User settings | ||
xcuserdata/ | ||
|
||
# Swift Package Manager | ||
# | ||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
Packages/ | ||
# Package.pins | ||
# Package.resolved | ||
# *.xcodeproj | ||
# | ||
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata | ||
# hence it is not needed unless you have added a package configuration file to your project | ||
.swiftpm | ||
|
||
.build/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[submodule "thorvg"] | ||
path = thorvg | ||
url = [email protected]:andyf-canva/thorvg-swift.git | ||
branch = release-v0.13.3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to ask you to update ThorVG version to latest(v0.14.7), and check whether it's nicely working. Ultimately, we'll need to align with the latest version whenever it's ready for merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea. As per my comment above (and below), I've moved the repo onto |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-snapshot-testing", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/pointfreeco/swift-snapshot-testing", | ||
"state" : { | ||
"revision" : "8e68404f641300bfd0e37d478683bb275926760c", | ||
"version" : "1.15.2" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-syntax", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-syntax.git", | ||
"state" : { | ||
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d", | ||
"version" : "509.1.1" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// swift-tools-version: 5.9 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "ThorVGSwift", | ||
platforms: [ | ||
.iOS(.v13) | ||
], | ||
products: [ | ||
.library( | ||
name: "ThorVGSwift", | ||
targets: ["ThorVGSwift"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", .upToNextMinor(from: "1.15.0")), | ||
], | ||
targets: [ | ||
.target( | ||
name: "ThorVGSwift", | ||
dependencies: ["thorvg"], | ||
path: "swift" | ||
), | ||
.target( | ||
name: "thorvg", | ||
path: "thorvg", | ||
exclude: [ | ||
"cross", | ||
"docs", | ||
"examples", | ||
"pc", | ||
"res", | ||
"src/bindings/wasm", | ||
"src/loaders/external_jpg", | ||
"src/loaders/external_png", | ||
"src/loaders/external_webp", | ||
"src/renderer/gl_engine", | ||
"src/renderer/wg_engine", | ||
"src/savers/gif", | ||
"src/tools", | ||
"test", | ||
"web" | ||
], | ||
publicHeadersPath: "src/bindings/capi", | ||
cxxSettings: [ | ||
.headerSearchPath("inc"), | ||
.headerSearchPath("src/common"), | ||
.headerSearchPath("src/bindings"), | ||
.headerSearchPath("src/loaders/jpg"), | ||
.headerSearchPath("src/loaders/lottie"), | ||
.headerSearchPath("src/loaders/lottie"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript"), | ||
.headerSearchPath("src/loaders/lottie/rapidjson"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/jcontext"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/lit"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/include"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/parser"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/jrt"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/vm"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/ecma"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/jmem"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/api"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/parser/regexp"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/parser/js"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/ecma/builtin-objects"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/ecma/operations"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/ecma/base"), | ||
.headerSearchPath("src/loaders/lottie/jerryscript/jerry-core/ecma/builtin-objects/typedarray"), | ||
.headerSearchPath("src/loaders/lottie/rapidjson/internal"), | ||
.headerSearchPath("src/loaders/lottie/rapidjson/error"), | ||
.headerSearchPath("src/loaders/png"), | ||
.headerSearchPath("src/loaders/raw"), | ||
.headerSearchPath("src/loaders/svg"), | ||
.headerSearchPath("src/loaders/ttf"), | ||
.headerSearchPath("src/loaders/tvg"), | ||
.headerSearchPath("src/renderer"), | ||
.headerSearchPath("src/renderer/sw_engine"), | ||
], | ||
linkerSettings: [ | ||
.linkedLibrary("pthread") | ||
] | ||
), | ||
.testTarget( | ||
name: "ThorVGSwift-tests", | ||
dependencies: [ | ||
"ThorVGSwift", | ||
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"), | ||
], | ||
path: "swift-tests", | ||
exclude: ["SnapshotTests/__Snapshots__"], | ||
resources: [.process("Resources")] | ||
), | ||
], | ||
cxxLanguageStandard: .cxx14 | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import CoreMedia | ||
import XCTest | ||
|
||
@testable import ThorVGSwift | ||
|
||
final class LottieRendererTests: XCTestCase { | ||
|
||
let size = CGSize(width: 1024, height: 1024) | ||
let contentRect = CGRect(x: 0, y: 0, width: 1024, height: 1024) | ||
let pixelFormat = PixelFormat.argb | ||
|
||
var lottie: Lottie { | ||
get throws { | ||
guard let url = Bundle.module.url(forResource: "test", withExtension: "json") else { | ||
preconditionFailure("Required resource for testing not found.") | ||
} | ||
return try Lottie(path: url.path) | ||
} | ||
} | ||
|
||
func testRender_WithValidFrameIndex_BufferPopulatedWithContent() throws { | ||
var buffer = [UInt32](repeating: 0, count: Int(size.width * size.height)) | ||
let renderer = LottieRenderer(try lottie, size: size, buffer: &buffer, stride: Int(size.width), pixelFormat: pixelFormat) | ||
|
||
try renderer.render(frameIndex: 0, contentRect: contentRect) | ||
|
||
let bufferHasContent = buffer.contains { $0 != 0 } | ||
XCTAssertTrue(bufferHasContent, "Buffer should have non-zero values after rendering.") | ||
} | ||
|
||
func testRender_WithAllFrames_Succeeds() throws { | ||
var buffer = [UInt32](repeating: 0, count: Int(size.width * size.height)) | ||
let renderer = LottieRenderer(try lottie, size: size, buffer: &buffer, stride: Int(size.width), pixelFormat: pixelFormat) | ||
|
||
do { | ||
for index in stride(from: 0, through: try lottie.numberOfFrames, by: 1.0) { | ||
try renderer.render(frameIndex: index, contentRect: contentRect) | ||
} | ||
} catch { | ||
XCTFail("Expected to render all lottie frames successfully, but \(error) error was thrown") | ||
} | ||
} | ||
|
||
func testRenderFrame_WithFrameIndexBelowBounds_ThrowsError() throws { | ||
var buffer = [UInt32](repeating: 0, count: Int(size.width * size.height)) | ||
let renderer = LottieRenderer(try lottie, size: size, buffer: &buffer, stride: Int(size.width), pixelFormat: pixelFormat) | ||
|
||
do { | ||
try renderer.render(frameIndex: -1, contentRect: contentRect) | ||
|
||
XCTFail("Expected frameIndexOutOfRange error to be thrown, but no error was thrown.") | ||
} catch { | ||
XCTAssertEqual(error as? LottieRenderingError, .frameIndexOutOfRange) | ||
} | ||
} | ||
|
||
func testRenderFrame_WithFrameIndexAboveBounds_ThrowsError() throws { | ||
var buffer = [UInt32](repeating: 0, count: Int(size.width * size.height)) | ||
let renderer = LottieRenderer(try lottie, size: size, buffer: &buffer, stride: Int(size.width), pixelFormat: pixelFormat) | ||
|
||
do { | ||
try renderer.render(frameIndex: 181, contentRect: contentRect) | ||
|
||
XCTFail("Expected frameIndexOutOfRange error to be thrown, but no error was thrown.") | ||
} catch { | ||
XCTAssertEqual(error as? LottieRenderingError, .frameIndexOutOfRange) | ||
} | ||
} | ||
} | ||
|
||
private extension CMTime { | ||
init(seconds: TimeInterval) { | ||
self.init(seconds: seconds, preferredTimescale: 600) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import CoreMedia | ||
import XCTest | ||
|
||
@testable import ThorVGSwift | ||
|
||
final class LottieTests: XCTestCase { | ||
|
||
let testLottieUrl = Bundle.module.url(forResource: "test", withExtension: "json")! | ||
|
||
func testInit_WithValidPath_ReturnsCorrectNumberOfFrames() throws { | ||
let lottie = try Lottie(path: testLottieUrl.path) | ||
|
||
XCTAssertEqual(lottie.numberOfFrames, 180) | ||
} | ||
|
||
func testInit_WithValidPath_ReturnsCorrectDuration() throws { | ||
let lottie = try Lottie(path: testLottieUrl.path) | ||
|
||
XCTAssertEqual(lottie.duration, CMTime(seconds: 3)) | ||
} | ||
|
||
func testInit_WithInvalidPath_ThrowsError() { | ||
do { | ||
_ = try Lottie(path: "") | ||
|
||
XCTFail("Expected failedToLoadFromPath error to be thrown, but no error was thrown.") | ||
} catch { | ||
XCTAssertEqual(error as? LottieRenderingError, .failedToLoadFromPath) | ||
} | ||
} | ||
|
||
func testInit_WithValidString_Succeeds() throws { | ||
let animationJson = try NSMutableString(contentsOf: testLottieUrl, encoding: String.Encoding.utf8.rawValue) as String | ||
let lottie = try Lottie(string: animationJson) | ||
|
||
XCTAssertEqual(lottie.numberOfFrames, 180) | ||
} | ||
|
||
func testInit_WithInvalidString_ThrowsError() throws { | ||
do { | ||
_ = try Lottie(string: "") | ||
|
||
XCTFail("Expected failedToLoadFromString error to be thrown, but no error was thrown.") | ||
} catch { | ||
XCTAssertEqual(error as? LottieRenderingError, .failedToLoadFromDataString) | ||
} | ||
} | ||
} | ||
|
||
private extension CMTime { | ||
init(seconds: TimeInterval) { | ||
self.init(seconds: seconds, preferredTimescale: 600) | ||
} | ||
} |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we now switch repo to official one?
I understand additional code from thorvg inside is not necessary.
History: thorvg/thorvg#2420
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do. 😄 And as per your comment, I'll also try to move us onto the latest release version of ThorVG as well 👍
Once I've got that going, I'll report back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tinyjin - see my comment below, I've now moved this onto the latest ThorVG version (v0.14.7), yet had to exclude the
tools
directory due to some duplicate symbols errors.