summaryrefslogtreecommitdiffstats
path: root/third_party/rust/aa-stroke/src/c_bindings.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/aa-stroke/src/c_bindings.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/aa-stroke/src/c_bindings.rs')
-rw-r--r--third_party/rust/aa-stroke/src/c_bindings.rs101
1 files changed, 101 insertions, 0 deletions
diff --git a/third_party/rust/aa-stroke/src/c_bindings.rs b/third_party/rust/aa-stroke/src/c_bindings.rs
new file mode 100644
index 0000000000..8880e49293
--- /dev/null
+++ b/third_party/rust/aa-stroke/src/c_bindings.rs
@@ -0,0 +1,101 @@
+use crate::{Stroker, StrokeStyle, Point};
+
+type OutputVertex = crate::Vertex;
+
+#[repr(C)]
+pub struct VertexBuffer {
+ data: *const OutputVertex,
+ len: usize
+}
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_new(
+ style: &StrokeStyle,
+ output_ptr: *mut OutputVertex,
+ output_capacity: usize,
+) -> *mut Stroker {
+ let mut s = Stroker::new(style);
+ if output_ptr != std::ptr::null_mut() {
+ let slice = unsafe { std::slice::from_raw_parts_mut(output_ptr, output_capacity) };
+ s.set_output_buffer(slice);
+ }
+ Box::into_raw(Box::new(s))
+}
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_move_to(s: &mut Stroker, x: f32, y: f32, closed: bool) {
+ s.move_to(Point::new(x, y), closed);
+}
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_line_to(s: &mut Stroker, x: f32, y: f32, end: bool) {
+ if end {
+ s.line_to_capped(Point::new(x, y))
+ } else {
+ s.line_to(Point::new(x, y));
+ }
+}
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_curve_to(s: &mut Stroker, c1x: f32, c1y: f32, c2x: f32, c2y: f32, x: f32, y: f32, end: bool) {
+ if end {
+ s.curve_to_capped(Point::new(c1x, c1y), Point::new(c2x, c2y), Point::new(x, y));
+ } else {
+ s.curve_to(Point::new(c1x, c1y), Point::new(c2x, c2y), Point::new(x, y));
+ }
+}
+
+/*
+#[no_mangle]
+pub extern "C" fn aa_stroke_quad_to(s: &mut Stroker, cx: f32, cy: f32, x: f32, y: f32) {
+ s.quad_to(cx, cy, x, y);
+}*/
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_close(s: &mut Stroker) {
+ s.close();
+}
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_finish(s: &mut Stroker) -> VertexBuffer {
+ let stroked_path = s.get_stroked_path();
+ if let Some(output_buffer_size) = stroked_path.get_output_buffer_size() {
+ VertexBuffer {
+ data: std::ptr::null(),
+ len: output_buffer_size,
+ }
+ } else {
+ let result = stroked_path.finish();
+ let vb = VertexBuffer { data: result.as_ptr(), len: result.len() };
+ std::mem::forget(result);
+ vb
+ }
+}
+
+#[no_mangle]
+pub extern "C" fn aa_stroke_vertex_buffer_release(vb: VertexBuffer)
+{
+ if vb.data != std::ptr::null() {
+ unsafe {
+ drop(Box::from_raw(std::slice::from_raw_parts_mut(vb.data as *mut OutputVertex, vb.len)));
+ }
+ }
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn aa_stroke_release(s: *mut Stroker) {
+ drop(Box::from_raw(s));
+}
+
+
+#[test]
+fn simple() {
+ let style = StrokeStyle::default();
+ let s = unsafe { &mut *aa_stroke_new(&style, std::ptr::null_mut(), 0) } ;
+ aa_stroke_move_to(s, 10., 10., false);
+ aa_stroke_line_to(s, 100., 100., false);
+ aa_stroke_line_to(s, 100., 10., true);
+ let vb = aa_stroke_finish(s);
+ aa_stroke_vertex_buffer_release(vb);
+ unsafe { aa_stroke_release(s) } ;
+}