| 1 |
use crate::{PathBuilder, OutputPath, OutputVertex, FillMode, rasterize_to_tri_list}; |
| 2 |
use crate::types::{BYTE, POINT}; |
| 3 |
|
| 4 |
#[no_mangle] |
| 5 |
pub extern "C" fn wgr_new_builder() -> *mut PathBuilder { |
| 6 |
let pb = PathBuilder::new(); |
| 7 |
Box::into_raw(Box::new(pb)) |
| 8 |
} |
| 9 |
|
| 10 |
#[no_mangle] |
| 11 |
pub extern "C" fn wgr_builder_reset(pb: &mut PathBuilder) { |
| 12 |
pb.reset(); |
| 13 |
} |
| 14 |
|
| 15 |
#[no_mangle] |
| 16 |
pub extern "C" fn wgr_builder_move_to(pb: &mut PathBuilder, x: f32, y: f32) { |
| 17 |
pb.move_to(x, y); |
| 18 |
} |
| 19 |
|
| 20 |
#[no_mangle] |
| 21 |
pub extern "C" fn wgr_builder_line_to(pb: &mut PathBuilder, x: f32, y: f32) { |
| 22 |
pb.line_to(x, y); |
| 23 |
} |
| 24 |
|
| 25 |
#[no_mangle] |
| 26 |
pub extern "C" fn wgr_builder_curve_to(pb: &mut PathBuilder, c1x: f32, c1y: f32, c2x: f32, c2y: f32, x: f32, y: f32) { |
| 27 |
pb.curve_to(c1x, c1y, c2x, c2y, x, y); |
| 28 |
} |
| 29 |
|
| 30 |
#[no_mangle] |
| 31 |
pub extern "C" fn wgr_builder_quad_to(pb: &mut PathBuilder, cx: f32, cy: f32, x: f32, y: f32) { |
| 32 |
pb.quad_to(cx, cy, x, y); |
| 33 |
} |
| 34 |
|
| 35 |
#[no_mangle] |
| 36 |
pub extern "C" fn wgr_builder_close(pb: &mut PathBuilder) { |
| 37 |
pb.close(); |
| 38 |
} |
| 39 |
|
| 40 |
#[no_mangle] |
| 41 |
pub extern "C" fn wgr_builder_set_fill_mode(pb: &mut PathBuilder, fill_mode: FillMode) { |
| 42 |
pb.set_fill_mode(fill_mode) |
| 43 |
} |
| 44 |
|
| 45 |
#[repr(C)] |
| 46 |
pub struct Path { |
| 47 |
fill_mode: FillMode, |
| 48 |
points: *const POINT, |
| 49 |
num_points: usize, |
| 50 |
types: *const BYTE, |
| 51 |
num_types: usize, |
| 52 |
} |
| 53 |
|
| 54 |
impl From<OutputPath> for Path { |
| 55 |
fn from(output_path: OutputPath) -> Self { |
| 56 |
let path = Self { |
| 57 |
fill_mode: output_path.fill_mode, |
| 58 |
points: output_path.points.as_ptr(), |
| 59 |
num_points: output_path.points.len(), |
| 60 |
types: output_path.types.as_ptr(), |
| 61 |
num_types: output_path.types.len(), |
| 62 |
}; |
| 63 |
std::mem::forget(output_path); |
| 64 |
path |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl Into<OutputPath> for Path { |
| 69 |
fn into(self) -> OutputPath { |
| 70 |
OutputPath { |
| 71 |
fill_mode: self.fill_mode, |
| 72 |
points: unsafe { |
| 73 |
if self.points == std::ptr::null() { |
| 74 |
Default::default() |
| 75 |
} else { |
| 76 |
Box::from_raw(std::slice::from_raw_parts_mut(self.points as *mut POINT, self.num_points)) |
| 77 |
} |
| 78 |
}, |
| 79 |
types: unsafe { |
| 80 |
if self.types == std::ptr::null() { |
| 81 |
Default::default() |
| 82 |
} else { |
| 83 |
Box::from_raw(std::slice::from_raw_parts_mut(self.types as *mut BYTE, self.num_types)) |
| 84 |
} |
| 85 |
}, |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[no_mangle] |
| 91 |
pub extern "C" fn wgr_builder_get_path(pb: &mut PathBuilder) -> Path { |
| 92 |
Path::from(pb.get_path().unwrap_or_default()) |
| 93 |
} |
| 94 |
|
| 95 |
#[repr(C)] |
| 96 |
pub struct VertexBuffer { |
| 97 |
data: *const OutputVertex, |
| 98 |
len: usize |
| 99 |
} |
| 100 |
|
| 101 |
#[no_mangle] |
| 102 |
pub extern "C" fn wgr_path_rasterize_to_tri_list( |
| 103 |
path: &Path, |
| 104 |
clip_x: i32, |
| 105 |
clip_y: i32, |
| 106 |
clip_width: i32, |
| 107 |
clip_height: i32, |
| 108 |
need_inside: bool, |
| 109 |
need_outside: bool, |
| 110 |
rasterization_truncates: bool, |
| 111 |
output_ptr: *mut OutputVertex, |
| 112 |
output_capacity: usize, |
| 113 |
) -> VertexBuffer { |
| 114 |
let output_buffer = if output_ptr != std::ptr::null_mut() { |
| 115 |
unsafe { Some(std::slice::from_raw_parts_mut(output_ptr, output_capacity)) } |
| 116 |
} else { |
| 117 |
None |
| 118 |
}; |
| 119 |
let mut result = rasterize_to_tri_list( |
| 120 |
path.fill_mode, |
| 121 |
unsafe { std::slice::from_raw_parts(path.types, path.num_types) }, |
| 122 |
unsafe { std::slice::from_raw_parts(path.points, path.num_points) }, |
| 123 |
clip_x, clip_y, clip_width, clip_height, |
| 124 |
need_inside, need_outside, |
| 125 |
rasterization_truncates, |
| 126 |
output_buffer |
| 127 |
); |
| 128 |
if let Some(output_buffer_size) = result.get_output_buffer_size() { |
| 129 |
VertexBuffer { |
| 130 |
data: std::ptr::null(), |
| 131 |
len: output_buffer_size, |
| 132 |
} |
| 133 |
} else { |
| 134 |
let slice = result.flush_output(); |
| 135 |
let vb = VertexBuffer { |
| 136 |
data: slice.as_ptr(), |
| 137 |
len: slice.len(), |
| 138 |
}; |
| 139 |
std::mem::forget(slice); |
| 140 |
vb |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[no_mangle] |
| 145 |
pub extern "C" fn wgr_path_release(path: Path) { |
| 146 |
let output_path: OutputPath = path.into(); |
| 147 |
drop(output_path); |
| 148 |
} |
| 149 |
|
| 150 |
#[no_mangle] |
| 151 |
pub extern "C" fn wgr_vertex_buffer_release(vb: VertexBuffer) |
| 152 |
{ |
| 153 |
if vb.data != std::ptr::null() { |
| 154 |
unsafe { |
| 155 |
drop(Box::from_raw(std::slice::from_raw_parts_mut(vb.data as *mut OutputVertex, vb.len))); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[no_mangle] |
| 161 |
pub unsafe extern "C" fn wgr_builder_release(pb: *mut PathBuilder) { |
| 162 |
drop(Box::from_raw(pb)); |
| 163 |
} |