Skip to content
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

Merged
merged 24 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2362b5f
Migrate Swift code from thorvg-swift
andyf-canva Jun 19, 2024
bc83e74
Update submodule reference to andyf-canva fork
andyf-canva Jun 19, 2024
dfc52f5
Update gitmodules to include specific branch
andyf-canva Aug 13, 2024
5440e25
Add savers/gif to the exclusion list
andyf-canva Aug 13, 2024
7de9978
Support the last frame
andyf-canva Aug 13, 2024
0965d37
Rename error enum
andyf-canva Aug 13, 2024
5cf39e8
Remove unsued mime types
andyf-canva Aug 13, 2024
bd4ec6c
Remove force unwrapping inside snapshot tests
andyf-canva Aug 21, 2024
5432b3f
Use Float instead of Int for frame numbers
andyf-canva Aug 21, 2024
81d91e9
Rename default to main
andyf-canva Aug 21, 2024
7116c40
Remove force unwrapping inside snapshot tests - number 2
andyf-canva Aug 21, 2024
e285f0e
Update Package to include pthread details
andyf-canva Aug 21, 2024
5816d2f
Make Engine initialiser public
andyf-canva Aug 21, 2024
0539567
Add jerryscript headers
andyf-canva Aug 21, 2024
2144bbe
Point path to a directory higher
andyf-canva Aug 21, 2024
9871ef7
Restructure package to include scripts and config folders
andyf-canva Aug 25, 2024
e17efd7
Bump up submodule reference to v0.14.7
andyf-canva Aug 25, 2024
ceca80c
Remove remote flag in git submodule update
andyf-canva Aug 25, 2024
6efc77b
Regenerate snapshots
andyf-canva Aug 25, 2024
3150a0d
Simplify Package structure
andyf-canva Aug 25, 2024
1779463
Exclude tools directory
andyf-canva Aug 25, 2024
034db8e
Update README
andyf-canva Aug 25, 2024
55a2969
Update README and config.h
andyf-canva Sep 12, 2024
0f7f0a0
Clean up
andyf-canva Sep 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
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/
4 changes: 4 additions & 0 deletions .gitmodules
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
Copy link
Member

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

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

branch = release-v0.13.3
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 v0.14.7 and all the tests are passing and everything is working as expected.

23 changes: 23 additions & 0 deletions Package.resolved
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
}
96 changes: 96 additions & 0 deletions Package.swift
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
)
75 changes: 75 additions & 0 deletions swift-tests/LottieRendererTests.swift
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)
}
}
54 changes: 54 additions & 0 deletions swift-tests/LottieTests.swift
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)
}
}
1 change: 1 addition & 0 deletions swift-tests/Resources/test.json

Large diffs are not rendered by default.

Loading