Skip to content

Commit a9cdadf

Browse files
authored
Merge pull request #3 from rustq/image
Image and Filter
2 parents 224761d + 4ccb408 commit a9cdadf

File tree

15 files changed

+132
-15
lines changed

15 files changed

+132
-15
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import SwiftUI
2+
3+
public struct Image<Content: View>: View {
4+
var image: String
5+
var x: Int32
6+
var y: Int32
7+
var width: UInt32
8+
var height: UInt32
9+
var blur: Float32?
10+
var grayscale: Bool?
11+
var brighten: Int32?
12+
var invert: Bool?
13+
let content: Content
14+
@State var b64: String = "";
15+
@EnvironmentObject var ffi: FFI;
16+
17+
public init(image: String, x: Int32, y: Int32, width: UInt32, height: UInt32, blur: Float32? = nil, grayscale: Bool? = nil, brighten: Int32? = nil, invert: Bool? = nil, @ViewBuilder builder: () -> Content) {
18+
self.image = image
19+
self.x = x
20+
self.y = y
21+
self.width = width
22+
self.height = height
23+
self.blur = blur
24+
self.grayscale = grayscale
25+
self.brighten = brighten
26+
self.invert = invert
27+
self.content = builder()
28+
}
29+
30+
public var body: some View {
31+
ZStack {
32+
content
33+
}.onAppear{
34+
let id = AutoIncrementID.id();
35+
self.ffi.soft.create(UInt(id))
36+
37+
if (self.image.starts(with: "data:image/png;base64")) {
38+
self.b64 = self.image.replacingOccurrences(of: "data:image/png;base64,", with: "")
39+
print(self.b64)
40+
} else {
41+
Task {
42+
do {
43+
let request = URLRequest(url: URL(string: self.image)!)
44+
let (data, _) = try await URLSession.shared.data(for: request)
45+
b64 = data.base64EncodedString()
46+
self.ffi.soft.set_image_attr(UInt(id), self.b64, self.x, self.y, self.width, self.height, self.blur, self.grayscale, self.brighten, self.invert)
47+
self.ffi.willChange();
48+
} catch {
49+
print("Error: \(error.localizedDescription)")
50+
}
51+
}
52+
}
53+
54+
}
55+
}
56+
}

SwiftUISkia/Sources/SwiftUISkia/Text.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public struct Text<Content: View>: View {
66
var y: Int32
77
var fontSize: Float32
88
var color: String
9-
var maxWidth: Optional<Float32>
9+
var maxWidth: Float32?
1010
let content: Content
1111
@EnvironmentObject var ffi: FFI;
1212

13-
public init(text: String, x: Int32, y: Int32, fontSize: Float32, color: String, maxWidth: Optional<Float32>, @ViewBuilder builder: () -> Content) {
13+
public init(text: String, x: Int32, y: Int32, fontSize: Float32, color: String, maxWidth: Float32? = nil, @ViewBuilder builder: () -> Content) {
1414
self.text = text
1515
self.x = x
1616
self.y = y
@@ -26,7 +26,7 @@ public struct Text<Content: View>: View {
2626
}.onAppear{
2727
let id = AutoIncrementID.id();
2828
self.ffi.soft.create(UInt(id))
29-
self.ffi.soft.set_text_attr(UInt(id), self.text, self.x, self.y, self.fontSize, self.color, self.maxWidth)
29+
self.ffi.soft.set_text_attr(UInt(id), self.text, self.x, self.y, self.fontSize, self.color, (self.maxWidth != nil) ? self.maxWidth! : nil)
3030
self.ffi.willChange();
3131
}
3232
}

SwiftUISkiaBridge/RustXcframework.xcframework/Info.plist

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,32 @@
2929
<key>HeadersPath</key>
3030
<string>Headers</string>
3131
<key>LibraryIdentifier</key>
32-
<string>ios-arm64</string>
32+
<string>macos-arm64_x86_64</string>
3333
<key>LibraryPath</key>
3434
<string>libswiftui_skia.a</string>
3535
<key>SupportedArchitectures</key>
3636
<array>
3737
<string>arm64</string>
38+
<string>x86_64</string>
3839
</array>
3940
<key>SupportedPlatform</key>
40-
<string>ios</string>
41+
<string>macos</string>
4142
</dict>
4243
<dict>
4344
<key>BinaryPath</key>
4445
<string>libswiftui_skia.a</string>
4546
<key>HeadersPath</key>
4647
<string>Headers</string>
4748
<key>LibraryIdentifier</key>
48-
<string>macos-arm64_x86_64</string>
49+
<string>ios-arm64</string>
4950
<key>LibraryPath</key>
5051
<string>libswiftui_skia.a</string>
5152
<key>SupportedArchitectures</key>
5253
<array>
5354
<string>arm64</string>
54-
<string>x86_64</string>
5555
</array>
5656
<key>SupportedPlatform</key>
57-
<string>macos</string>
57+
<string>ios</string>
5858
</dict>
5959
</array>
6060
<key>CFBundlePackageType</key>

SwiftUISkiaBridge/RustXcframework.xcframework/ios-arm64/Headers/swiftui-skia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
2121
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2222
void __swift_bridge__$SoftSkia$set_text_attr(void* self, uintptr_t id, void* text, int32_t x, int32_t y, float font_size, void* color, struct __private__OptionF32 max_width);
23+
void __swift_bridge__$SoftSkia$set_image_attr(void* self, uintptr_t id, void* image, int32_t x, int32_t y, uint32_t width, uint32_t height, struct __private__OptionF32 blur, struct __private__OptionBool grayscale, struct __private__OptionI32 brighten, struct __private__OptionBool invert);
2324
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2425

2526

SwiftUISkiaBridge/RustXcframework.xcframework/ios-arm64_x86_64-simulator/Headers/swiftui-skia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
2121
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2222
void __swift_bridge__$SoftSkia$set_text_attr(void* self, uintptr_t id, void* text, int32_t x, int32_t y, float font_size, void* color, struct __private__OptionF32 max_width);
23+
void __swift_bridge__$SoftSkia$set_image_attr(void* self, uintptr_t id, void* image, int32_t x, int32_t y, uint32_t width, uint32_t height, struct __private__OptionF32 blur, struct __private__OptionBool grayscale, struct __private__OptionI32 brighten, struct __private__OptionBool invert);
2324
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2425

2526

SwiftUISkiaBridge/RustXcframework.xcframework/macos-arm64_x86_64/Headers/swiftui-skia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
2121
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2222
void __swift_bridge__$SoftSkia$set_text_attr(void* self, uintptr_t id, void* text, int32_t x, int32_t y, float font_size, void* color, struct __private__OptionF32 max_width);
23+
void __swift_bridge__$SoftSkia$set_image_attr(void* self, uintptr_t id, void* image, int32_t x, int32_t y, uint32_t width, uint32_t height, struct __private__OptionF32 blur, struct __private__OptionBool grayscale, struct __private__OptionI32 brighten, struct __private__OptionBool invert);
2324
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2425

2526

SwiftUISkiaBridge/Sources/SwiftUISkiaBridge/swiftui-skia.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ extension SoftSkiaRefMut {
5252
__swift_bridge__$SoftSkia$set_text_attr(ptr, id, { let rustString = text.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), x, y, font_size, { let rustString = color.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), max_width.intoFfiRepr())
5353
}
5454

55+
public func set_image_attr<GenericIntoRustString: IntoRustString>(_ id: UInt, _ image: GenericIntoRustString, _ x: Int32, _ y: Int32, _ width: UInt32, _ height: UInt32, _ blur: Optional<Float>, _ grayscale: Optional<Bool>, _ brighten: Optional<Int32>, _ invert: Optional<Bool>) {
56+
__swift_bridge__$SoftSkia$set_image_attr(ptr, id, { let rustString = image.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), x, y, width, height, blur.intoFfiRepr(), grayscale.intoFfiRepr(), brighten.intoFfiRepr(), invert.intoFfiRepr())
57+
}
58+
5559
public func to_base64() -> RustString {
5660
RustString(ptr: __swift_bridge__$SoftSkia$to_base64(ptr))
5761
}

examples/first-example/first-example.xcodeproj/project.pbxproj

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
8FD136FB2C1D3B6800A4C1B7 /* SwiftUISkia in Frameworks */ = {isa = PBXBuildFile; productRef = 8FD136FA2C1D3B6800A4C1B7 /* SwiftUISkia */; };
1110
8FD136FE2C1D3B7C00A4C1B7 /* SwiftUISkiaBridge in Frameworks */ = {isa = PBXBuildFile; productRef = 8FD136FD2C1D3B7C00A4C1B7 /* SwiftUISkiaBridge */; };
11+
8FE777162C4986FF0029467A /* SwiftUISkia in Frameworks */ = {isa = PBXBuildFile; productRef = 8FE777152C4986FF0029467A /* SwiftUISkia */; };
1212
8FF8783B2C15A8CC007A9F3A /* first_exampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FF8783A2C15A8CC007A9F3A /* first_exampleApp.swift */; };
1313
8FF8783D2C15A8CC007A9F3A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FF8783C2C15A8CC007A9F3A /* ContentView.swift */; };
1414
8FF8783F2C15A8CE007A9F3A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8FF8783E2C15A8CE007A9F3A /* Assets.xcassets */; };
@@ -29,19 +29,27 @@
2929
isa = PBXFrameworksBuildPhase;
3030
buildActionMask = 2147483647;
3131
files = (
32-
8FD136FB2C1D3B6800A4C1B7 /* SwiftUISkia in Frameworks */,
32+
8FE777162C4986FF0029467A /* SwiftUISkia in Frameworks */,
3333
8FD136FE2C1D3B7C00A4C1B7 /* SwiftUISkiaBridge in Frameworks */,
3434
);
3535
runOnlyForDeploymentPostprocessing = 0;
3636
};
3737
/* End PBXFrameworksBuildPhase section */
3838

3939
/* Begin PBXGroup section */
40+
8FE777102C48D1B40029467A /* Frameworks */ = {
41+
isa = PBXGroup;
42+
children = (
43+
);
44+
name = Frameworks;
45+
sourceTree = "<group>";
46+
};
4047
8FF8782E2C15A8CC007A9F3A = {
4148
isa = PBXGroup;
4249
children = (
4350
8FF878392C15A8CC007A9F3A /* first-example */,
4451
8FF878382C15A8CC007A9F3A /* Products */,
52+
8FE777102C48D1B40029467A /* Frameworks */,
4553
);
4654
sourceTree = "<group>";
4755
};
@@ -90,8 +98,8 @@
9098
);
9199
name = "first-example";
92100
packageProductDependencies = (
93-
8FD136FA2C1D3B6800A4C1B7 /* SwiftUISkia */,
94101
8FD136FD2C1D3B7C00A4C1B7 /* SwiftUISkiaBridge */,
102+
8FE777152C4986FF0029467A /* SwiftUISkia */,
95103
);
96104
productName = "first-example";
97105
productReference = 8FF878372C15A8CC007A9F3A /* first-example.app */;
@@ -380,14 +388,14 @@
380388
/* End XCLocalSwiftPackageReference section */
381389

382390
/* Begin XCSwiftPackageProductDependency section */
383-
8FD136FA2C1D3B6800A4C1B7 /* SwiftUISkia */ = {
384-
isa = XCSwiftPackageProductDependency;
385-
productName = SwiftUISkia;
386-
};
387391
8FD136FD2C1D3B7C00A4C1B7 /* SwiftUISkiaBridge */ = {
388392
isa = XCSwiftPackageProductDependency;
389393
productName = SwiftUISkiaBridge;
390394
};
395+
8FE777152C4986FF0029467A /* SwiftUISkia */ = {
396+
isa = XCSwiftPackageProductDependency;
397+
productName = SwiftUISkia;
398+
};
391399
/* End XCSwiftPackageProductDependency section */
392400
};
393401
rootObject = 8FF8782F2C15A8CC007A9F3A /* Project object */;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "swiftui-96x96_2x.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}

examples/first-example/first-example/ContentView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ struct ContentView: View {
1212
var body: some View {
1313
SwiftUISkia.Surface(width: 360, height: 360) {
1414
SwiftUISkia.Rect(x: 10, y: 220, width: 30, height: 30, style: "fill", color: "cyan") {}
15+
SwiftUISkia.Image(image: "https://developer.apple.com/assets/elements/icons/swiftui/swiftui-96x96_2x.png", x: 70, y: 70, width: 48, height: 48, blur: 5, brighten: 80) {}
16+
SwiftUISkia.Image(image: "https://developer.apple.com/assets/elements/icons/swiftui/swiftui-96x96_2x.png", x: 0, y: 0, width: 48, height: 48) {}
1517
SwiftUISkia.Line(p1: [100, 260], p2: [50, 285], strokeWidth: 8, color: "black") {}
1618
SwiftUISkia.RoundRect(x: 220, y: 50, r: 10, width: 80, height: 80, style: "stroke", color: "fuchsia") {}
1719
SwiftUISkia.Points(points: [
@@ -30,6 +32,7 @@ struct ContentView: View {
3032
SwiftUISkia.Circle(cx: 200, cy: 220, r: 70, style: "stroke", color: "violet") {}
3133
SwiftUISkia.Circle(cx: 200, cy: 220, r: 50, style: "fill", color: "violet") {}
3234
SwiftUISkia.Text(text: "Hello SwiftUI Skia!",x: 80, y: 0, fontSize: 16, color: "black", maxWidth: 60) {}
35+
SwiftUISkia.Text(text: "Hello SwiftUI Skia!",x: 80, y: 60, fontSize: 16, color: "black") {}
3336
}
3437
}
3538
}

generated/swiftui-skia/swiftui-skia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
2121
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2222
void __swift_bridge__$SoftSkia$set_text_attr(void* self, uintptr_t id, void* text, int32_t x, int32_t y, float font_size, void* color, struct __private__OptionF32 max_width);
23+
void __swift_bridge__$SoftSkia$set_image_attr(void* self, uintptr_t id, void* image, int32_t x, int32_t y, uint32_t width, uint32_t height, struct __private__OptionF32 blur, struct __private__OptionBool grayscale, struct __private__OptionI32 brighten, struct __private__OptionBool invert);
2324
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2425

2526

generated/swiftui-skia/swiftui-skia.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ extension SoftSkiaRefMut {
5151
__swift_bridge__$SoftSkia$set_text_attr(ptr, id, { let rustString = text.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), x, y, font_size, { let rustString = color.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), max_width.intoFfiRepr())
5252
}
5353

54+
public func set_image_attr<GenericIntoRustString: IntoRustString>(_ id: UInt, _ image: GenericIntoRustString, _ x: Int32, _ y: Int32, _ width: UInt32, _ height: UInt32, _ blur: Optional<Float>, _ grayscale: Optional<Bool>, _ brighten: Optional<Int32>, _ invert: Optional<Bool>) {
55+
__swift_bridge__$SoftSkia$set_image_attr(ptr, id, { let rustString = image.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), x, y, width, height, blur.intoFfiRepr(), grayscale.intoFfiRepr(), brighten.intoFfiRepr(), invert.intoFfiRepr())
56+
}
57+
5458
public func to_base64() -> RustString {
5559
RustString(ptr: __swift_bridge__$SoftSkia$to_base64(ptr))
5660
}

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate soft_skia;
22
extern crate base64;
33
extern crate cssparser;
44
use soft_skia::instance::Instance;
5+
use soft_skia::shape::Image;
56
use soft_skia::shape::PaintStyle;
67
use soft_skia::shape::Rect;
78
use soft_skia::shape::Circle;
@@ -31,6 +32,7 @@ mod ffi {
3132
fn set_points_attr(&mut self, id: usize, points: &[u32], stroke_width: Option<u32>, style: String, color: String);
3233
fn set_round_rect_attr(&mut self, id: usize, x: u32, y: u32, r: u32, width: u32, height: u32, style: String, color: String);
3334
fn set_text_attr(&mut self, id: usize, text: String, x: i32, y: i32, font_size: f32, color: String, max_width: Option<f32>);
35+
fn set_image_attr(&mut self, id: usize, image: String, x: i32, y: i32, width: u32, height: u32, blur: Option<f32>, grayscale: Option<bool>, brighten: Option<i32>, invert: Option<bool>);
3436

3537
fn to_base64(&mut self) -> String;
3638
}
@@ -139,6 +141,21 @@ impl SoftSkia {
139141
}))
140142
}
141143

144+
pub fn set_image_attr(&mut self, id: usize, image: String, x: i32, y: i32, width: u32, height: u32, blur: Option<f32>, grayscale: Option<bool>, brighten: Option<i32>, invert: Option<bool>) {
145+
self.instance.set_shape_to_child(id, Shapes::I(Image {
146+
image,
147+
x,
148+
y,
149+
width,
150+
height,
151+
blur,
152+
grayscale,
153+
brighten,
154+
invert,
155+
}))
156+
}
157+
158+
142159
pub fn to_base64(&mut self) -> String {
143160
to_base64(&mut self.instance.tree)
144161
}

0 commit comments

Comments
 (0)