Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 42 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@

import PackageDescription

var dependencies: [Package.Dependency] = [
.package(
url: "https://github.com/apple/swift-argument-parser.git",
from: "1.5.0"
),
]
var svgViewDependencies: [Target.Dependency] = []
var svgViewSwiftSettings: [SwiftSetting] = []

#if compiler(>=6.2)
dependencies.append(
.package(
url: "https://github.com/compnerd/xylem.git",
revision: "9881c95ce3a139f4ccfa584676201516c2a5751d"
)
)
svgViewDependencies.append(contentsOf: [
.product(
name: "SAXParser",
package: "xylem",
condition: .when(platforms: [.wasi])
),
.product(
name: "XMLCore",
package: "xylem",
condition: .when(platforms: [.wasi])
),
])
svgViewSwiftSettings.append(
.define(
"FOUNDATION_ESSENTIALS_BUILD",
.when(platforms: [.wasi])
)
)
#endif

let package = Package(
name: "SVGView",
platforms: [
Expand All @@ -19,12 +55,7 @@ let package = Package(
targets: ["GenerateReferencesCLI"]
)
],
dependencies: [
.package(
url: "https://github.com/apple/swift-argument-parser.git",
from: "1.5.0"
),
],
dependencies: dependencies,
targets: [
.executableTarget(
name: "GenerateReferencesCLI",
Expand All @@ -34,9 +65,11 @@ let package = Package(
],
path: "GenerateReferencesCLI"
),
.target(
name: "SVGView",
path: "Source"
.target(
name: "SVGView",
dependencies: svgViewDependencies,
path: "Source",
swiftSettings: svgViewSwiftSettings
),
.testTarget(
name: "CoreGraphicsPolyfillTests",
Expand Down
67 changes: 67 additions & 0 deletions Source/CoreGraphicsPolyfill.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,74 @@
// Created by khoi on 10/5/25.
//

#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
import WASILibc
#else
import Foundation
#endif

#if FOUNDATION_ESSENTIALS_BUILD
public typealias CGFloat = Double

public struct CGPoint: Equatable {
public static let zero = CGPoint()

public var x: CGFloat
public var y: CGFloat

public init(x: CGFloat = 0, y: CGFloat = 0) {
self.x = x
self.y = y
}
}

public struct CGSize {
public static let zero = CGSize()

public var width: CGFloat
public var height: CGFloat

public init(width: CGFloat = 0, height: CGFloat = 0) {
self.width = width
self.height = height
}
}

public struct CGRect {
public static let zero = CGRect()

public var origin: CGPoint
public var size: CGSize

public init(origin: CGPoint = .zero, size: CGSize = CGSize()) {
self.origin = origin
self.size = size
}

public init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
self.init(
origin: CGPoint(x: x, y: y),
size: CGSize(width: width, height: height)
)
}

public var width: CGFloat { maxX - minX }
public var height: CGFloat { maxY - minY }
public var minX: CGFloat { Swift.min(origin.x, origin.x + size.width) }
public var minY: CGFloat { Swift.min(origin.y, origin.y + size.height) }
public var maxX: CGFloat { Swift.max(origin.x, origin.x + size.width) }
public var maxY: CGFloat { Swift.max(origin.y, origin.y + size.height) }

public func union(_ other: CGRect) -> CGRect {
let minX = Swift.min(self.minX, other.minX)
let minY = Swift.min(self.minY, other.minY)
let maxX = Swift.max(self.maxX, other.maxX)
let maxY = Swift.max(self.maxY, other.maxY)
return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)
}
}
#endif

#if os(WASI) || os(Linux) || os(Android)
private let KAPPA: CGFloat = 0.5522847498 // 4 *(sqrt(2) -1)/3
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Images/SVGDataImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Alisa Mylnikova on 10/06/2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Images/SVGURLImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Alisa Mylnikova on 22/09/2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGDefs.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGGroup.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Alisa Mylnikova on 03/06/2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGMarker.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGNode.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGShape.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGText.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGUserSpaceNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Alisa Mylnikova on 14/10/2020.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Nodes/SVGViewport.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
23 changes: 11 additions & 12 deletions Source/Model/Primitives/SVGColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Yuriy Strot on 19.01.2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down Expand Up @@ -42,24 +44,24 @@ public class SVGColor: SVGPaint {
}

public convenience init(hex: String) {
let scanner = Scanner(string: hex)
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
self.init(Int(rgbValue))
self.init(Int(SVGStringUtilities.parseHex(hex) ?? 0))
}

override func serialize(key: String, serializer: Serializer) {
var prefix = ""
let transparency = t
if transparency != 0 {
prefix = "\(Int(round(Double(255 - transparency) * 100 / 255)))% "
prefix = "\(Int((Double(255 - transparency) * 100 / 255).rounded()))% "
}

let hex = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)
if let text = SVGColors.text(of: hex) {
serializer.add(key, "\(prefix)\(text)")
} else {
serializer.add(key, "\(prefix)#\(String(format: "%02X%02X%02X", r, g, b))")
serializer.add(
key,
"\(prefix)#\(SVGStringUtilities.hexByteString(r))\(SVGStringUtilities.hexByteString(g))\(SVGStringUtilities.hexByteString(b))"
)
}
}

Expand Down Expand Up @@ -137,14 +139,11 @@ extension Color: SerializableAtom {
return "\"\(prefix)\(text)\""
}

return "\"\(prefix)#\(String(format: "%02X%02X%02X", r, g, b))\""
return "\"\(prefix)#\(SVGStringUtilities.hexByteString(r))\(SVGStringUtilities.hexByteString(g))\(SVGStringUtilities.hexByteString(b))\""
}

init(hex: String) {
let scanner = Scanner(string: hex)
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
self.init(rgbValue: Int(rgbValue))
self.init(rgbValue: Int(SVGStringUtilities.parseHex(hex) ?? 0))
}

init(rgbValue: Int) {
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Primitives/SVGFont.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
5 changes: 4 additions & 1 deletion Source/Model/Primitives/SVGGradient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
// Created by Yuriy Strot on 22.02.2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
import WASILibc
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Primitives/SVGLength.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Alisa Mylnikova on 13/10/2020.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Primitives/SVGPaint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Yuriy Strot on 19.01.2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Primitives/SVGPreserveAspectRatio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Yuriy Strot on 20.01.2021.
//

#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Primitives/SVGStroke.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Shapes/SVGCircle.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Shapes/SVGEllipse.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if os(WASI) || os(Linux) || os(Android)
#if FOUNDATION_ESSENTIALS_BUILD
import FoundationEssentials
#elseif os(WASI) || os(Linux) || os(Android)
import Foundation
#else
import SwiftUI
Expand Down
Loading
Loading