Skip to content

Conversation

@khoi
Copy link
Collaborator

@khoi khoi commented May 15, 2025

This PR makes the library XP compliant while keeping all the existing features for platforms that support them

  • Wrap UI stuff in proper #if compiler directives
  • Implement some nonexisting CoreGraphics types for XP
    • CG* classes
    • CGPath extensions
    • MBezierPath (a NSBezierPath knocks off)

https://github.com/GoodNotes/GoodNotes-5/pull/37926 is pointing to this branch, so if you want to try compile it, use that.

khoi added 2 commits May 15, 2025 15:06
- Add conditional import statements across all SVG-related files
- Use Foundation for WebAssembly System Interface (WASI) platform
- Keep SwiftUI/Combine imports for other platforms
- Enable cross-platform compatibility for WebAssembly environments
- Remove @published property wrappers when targeting WASI platform
- Maintain identical public API surface with conditional compilation
- Continue implementation of WASI platform support from previous commit
@khoi khoi changed the title Add WASI platform support with conditional imports Making SVGView XP compliant May 15, 2025
@khoi khoi mentioned this pull request May 15, 2025
khoi added 15 commits May 15, 2025 15:45
- Change approach from conditionally compiling model properties to conditionally compiling UI code
- Wrap SwiftUI view implementations in #if !os(WASI) blocks instead of duplicating property declarations
- Simplify code structure while maintaining full WASI platform compatibility
- Improve maintainability by reducing conditional compilation complexity
- Upgrade Swift tools version from 5.3 to 5.9
- Increase minimum macOS version from 11 to 14
- Maintain existing requirements for iOS (14) and watchOS (7)
- Change `#if os(WASI)` to `#if os(WASI) || os(Linux)` throughout codebase
- Replace `#if !os(WASI)` with `#if canImport(SwiftUI)` for better platform detection
- Add conditional compilation for published properties on Linux platform
- Ensure proper handling of SwiftUI dependencies across all platforms
- Implement CGPath, CGAffineTransform and related types for non-Apple platforms
- Create MBezierPath implementation compatible with Linux/WASI
- Add mathematical function wrappers needed for graphics operations
- Use conditional compilation to maintain native CoreGraphics on Apple platforms
- Improves code organization by separating core SVG functionality from SwiftUI integrations
- Reduces platform-specific dependencies in base class definitions
- Makes the codebase more maintainable by isolating UI framework bindings
- Consistent with modern Swift architectural practices of protocol conformance via extensions
- Wrap SwiftUI-dependent methods in SVGNode, SVGColor, SVGFont and SVGStroke with #if canImport(SwiftUI)
- Move gesture handling code inside conditional compilation blocks
- Ensure SVG components work on platforms where SwiftUI is unavailable
- Continues the cross-platform compatibility work for Linux and WASI platforms
- Organize imports into clearer platform-specific sections
- Add explicit UIKit import for iOS/tvOS/watchOS platforms
- Remove unnecessary SwiftUI import
- Move Foundation import to Linux/WASI specific section
- Replace SwiftUI's HorizontalAlignment with custom SVGText.Anchor enum
- Add conversion between Anchor and HorizontalAlignment for SwiftUI compatibility
- Update serialization to work with the new enum
- Continue ongoing efforts to reduce platform-specific dependencies
@khoi khoi force-pushed the khoi/xp-compliant-try-2 branch from 09038a2 to e24b4ed Compare May 22, 2025 02:45
- Add conditional compilation directives to make bezierPath variables mutable on Linux/WASI
- Follows pattern of other platform-specific fixes for cross-platform compatibility
@khoi khoi changed the title Making SVGView XP compliant Make SVGView XP compliant May 22, 2025

public func concatenating(_ t: CGAffineTransform) -> CGAffineTransform {
return CGAffineTransform(
a: a * t.a + c * t.b,
Copy link
Collaborator Author

@khoi khoi May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the meat of the PR is in this file, take a look

name: "SVGView",
platforms: [
.macOS(.v11),
.macOS(.v14),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bumping to match our packages, also to get rid of some of the unnecessary methods like cgPath on NSBezierPath

Copy link
Member

@pedrovgs pedrovgs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done @khoi ! Please add automated tests to the polyfill. If one of the operations is not computed properly we all the SVG parsing will break later when we position elements on the screen based on the parsed data.

Thank you so much for the PR.

public typealias CGPathFillRule = CoreGraphics.CGPathFillRule
public typealias CGPath = CoreGraphics.CGPath
public typealias CGAffineTransform = CoreGraphics.CGAffineTransform
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nice contriution. Thank you so much @khoi. Did you add some tests to guarantee the implementation is correct?

Copy link
Collaborator Author

@khoi khoi May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private let KAPPA: CGFloat = 0.5522847498 // 4 *(sqrt(2) -1)/3

public struct CGAffineTransform: Equatable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you consider if we can use Foundation.AffineTransform?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not directly translatable as the props are stored differently in AffineTransform under

 public var m11: CGFloat

    public var m12: CGFloat

    public var m21: CGFloat

    public var m22: CGFloat

Not going to change for now

khoi added 4 commits May 22, 2025 16:01
- Remove unnecessary CGFloat, CGSize, and CGPoint typealiases
- Add missing Foundation imports to SVG parser files
- Continue improving cross-platform compatibility
These math function declarations (sqrt, copysign, acos, cos, sin) are no longer needed
in the Linux/WASI environment as they're likely available from standard libraries or
other imports.
- Change implementation from struct to class to better match CoreGraphics behavior
- Remove mutating keywords from methods as they're no longer needed
- Update parameter types in addArcTo method to match class-based approach
- Tests CGAffineTransform operations (identity, translation, scaling, rotation)
- Verifies CGPath functionality (basic operations, bounding box calculation)
- Covers MBezierPath operations (initialization, path building, transformations)
- Includes tests for PathElement enum and extensions
- Handles edge cases and platform-specific behavior
khoi added 4 commits May 23, 2025 09:55
- Set up GitHub Actions workflow to run tests on Linux
- Add CoreGraphicsPolyfillTests test target to Package.swift
- Remove unnecessary exclude entry for Info.plist
- Test on macOS-latest to validate against native CoreGraphics implementation
- Mirror existing Linux test job structure with caching and test discovery
@khoi khoi merged commit 7509456 into main May 23, 2025
2 checks passed
@khoi khoi deleted the khoi/xp-compliant-try-2 branch May 23, 2025 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants