Debian Codegraph @

third_party/rust/etagere/src/ffi.rs

raw
1 use crate::{AtlasAllocator, size2};
2 use std::os::raw::c_char;
3
4 /// 1 means OK, 0 means error.
5 pub type EtagereStatus = u32;
6 pub type EtagereAllocationId = u32;
7
8
9 #[repr(C)]
10 pub struct EtagereAllocation {
11 pub rectangle: EtagereRectangle,
12 pub id: EtagereAllocationId,
13 }
14
15 #[repr(C)]
16 pub struct EtagereRectangle {
17 pub min_x: i32,
18 pub min_y: i32,
19 pub max_x: i32,
20 pub max_y: i32,
21 }
22
23
24 #[repr(C)]
25 pub struct EtagereAllocatorOptions {
26 pub width_alignment: i32,
27 pub height_alignment: i32,
28 pub num_columns: i32,
29 pub flags: u32,
30 }
31
32 pub const ETAGERE_FLAGS_VERTICAL_SHELVES: u32 = 1;
33
34 #[no_mangle]
35 pub unsafe extern "C" fn etagere_atlas_allocator_new(width: i32, height: i32) -> *mut AtlasAllocator {
36 Box::into_raw(Box::new(AtlasAllocator::new(size2(width, height))))
37 }
38
39 #[no_mangle]
40 pub unsafe extern "C" fn etagere_atlas_allocator_with_options(
41 width: i32,
42 height: i32,
43 options: &EtagereAllocatorOptions
44 ) -> *mut AtlasAllocator {
45 let options = crate::AllocatorOptions {
46 alignment: size2(options.width_alignment, options.height_alignment),
47 num_columns: options.num_columns,
48 vertical_shelves: (options.flags & ETAGERE_FLAGS_VERTICAL_SHELVES) != 0,
49 };
50 Box::into_raw(Box::new(AtlasAllocator::with_options(size2(width, height), &options)))
51 }
52
53 #[no_mangle]
54 pub unsafe extern "C" fn etagere_atlas_allocator_delete(allocator: *mut AtlasAllocator) {
55 let _ = Box::from_raw(allocator);
56 }
57
58 #[no_mangle]
59 pub unsafe extern "C" fn etagere_atlas_allocator_allocate(allocator: &mut AtlasAllocator, width: i32, height: i32, allocation: &mut EtagereAllocation) -> EtagereStatus {
60 match allocator.allocate(size2(width, height)) {
61 Some(alloc) => {
62 allocation.id = alloc.id.serialize();
63 allocation.rectangle.min_x = alloc.rectangle.min.x;
64 allocation.rectangle.min_y = alloc.rectangle.min.y;
65 allocation.rectangle.max_x = alloc.rectangle.max.x;
66 allocation.rectangle.max_y = alloc.rectangle.max.y;
67 return 1;
68 }
69 None => {
70 allocation.id = std::u32::MAX;
71 allocation.rectangle.min_x = 0;
72 allocation.rectangle.min_y = 0;
73 allocation.rectangle.max_x = 0;
74 allocation.rectangle.max_y = 0;
75 return 0;
76 }
77 }
78 }
79
80 #[no_mangle]
81 pub unsafe extern "C" fn etagere_atlas_allocator_deallocate(allocator: &mut AtlasAllocator, id: EtagereAllocationId) {
82 allocator.deallocate(crate::AllocId::deserialize(id));
83 }
84
85 #[no_mangle]
86 pub unsafe extern "C" fn etagere_atlas_allocator_clear(allocator: &mut AtlasAllocator) {
87 allocator.clear();
88 }
89
90 #[no_mangle]
91 pub unsafe extern "C" fn etagere_atlas_allocator_allocated_space(allocator: &AtlasAllocator) -> i32 {
92 allocator.allocated_space()
93 }
94
95 #[no_mangle]
96 pub unsafe extern "C" fn etagere_atlas_allocator_free_space(allocator: &AtlasAllocator) -> i32 {
97 allocator.free_space()
98 }
99
100 #[no_mangle]
101 pub unsafe extern "C" fn etagere_atlas_allocator_get(allocator: &AtlasAllocator, id: EtagereAllocationId) -> EtagereRectangle {
102 let rect = allocator.get(crate::AllocId::deserialize(id));
103 return EtagereRectangle {
104 min_x: rect.min.x,
105 min_y: rect.min.y,
106 max_x: rect.max.x,
107 max_y: rect.max.y,
108 };
109 }
110
111 #[no_mangle]
112 pub unsafe extern "C" fn etagere_atlas_allocator_dump_svg(allocator: &AtlasAllocator, file_name: *const c_char) -> EtagereStatus {
113 let cstr = std::ffi::CStr::from_ptr(file_name);
114 let rstr = String::from_utf8_lossy(cstr.to_bytes());
115 let mut file = match std::fs::File::create(rstr.as_ref()) {
116 Ok(file) => file,
117 Err(..) => {
118 return 0;
119 }
120 };
121
122 match allocator.dump_svg(&mut file) {
123 Ok(..) => 1,
124 Err(..) => 0,
125 }
126 }