1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! C-compatible foreign function interface for guillotiere, that can be easily fed to cbindgen.

use guillotiere::*;
use std::mem::transmute;

use guillotiere::AtlasAllocator as guillotiere_atlas_allocator_t;
use guillotiere::SimpleAtlasAllocator as guillotiere_simple_atlas_allocator_t;
use guillotiere::ChangeList as guillotiere_change_list_t;

#[repr(C)]
#[no_mangle]
pub struct guillotiere_size_t {
    pub width: i32,
    pub height: i32,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_rectangle_t {
    pub min_x: i32,
    pub min_y: i32,
    pub max_x: i32,
    pub max_y: i32,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_change_t {
    pub old_alloc: guillotiere_allocation_t,
    pub new_alloc: guillotiere_allocation_t,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_changes_t {
    pub changes: *const guillotiere_change_t,
    pub count: usize,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_failures_t {
    pub failures: *const guillotiere_allocation_t,
    pub count: usize,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_alloc_id_t {
    id: u32,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_allocation_t {
    pub id: guillotiere_alloc_id_t,
    pub rectangle: guillotiere_rectangle_t,
}

#[repr(C)]
#[no_mangle]
pub struct guillotiere_allocator_options_t {
    pub snap_size: i32,
    pub small_size_threshold: i32,
    pub large_size_threshold: i32,
}

fn from_ffi_options(options: &guillotiere_allocator_options_t) -> AllocatorOptions {
    AllocatorOptions {
        snap_size: options.snap_size,
        small_size_threshold: options.small_size_threshold,
        large_size_threshold: options.large_size_threshold,
    }
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_new(size: guillotiere_size_t) -> *mut guillotiere_atlas_allocator_t {
    Box::into_raw(Box::new(AtlasAllocator::new(transmute(size))))
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_with_options(size: guillotiere_size_t, options: &guillotiere_allocator_options_t) -> *mut guillotiere_atlas_allocator_t {
    let options = from_ffi_options(options);
    Box::into_raw(Box::new(AtlasAllocator::with_options(transmute(size), &options)))
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_delete(atlas: *mut guillotiere_atlas_allocator_t) {
    Box::from_raw(atlas);
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_size(atlas: &guillotiere_atlas_allocator_t) -> guillotiere_size_t {
    transmute(atlas.size())
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_allocate(atlas: &mut guillotiere_atlas_allocator_t, size: guillotiere_size_t, result: &mut guillotiere_allocation_t) -> bool {
    if let Some(alloc) = atlas.allocate(transmute(size)) {
        *result = transmute(alloc);
        return true;
    }

    false
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_deallocate(atlas: &mut guillotiere_atlas_allocator_t, id: guillotiere_alloc_id_t) {
    atlas.deallocate(transmute(id));
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_grow(atlas: &mut guillotiere_atlas_allocator_t, new_size: guillotiere_size_t) {
    atlas.grow(transmute(new_size));
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_rearrange(atlas: &mut guillotiere_atlas_allocator_t, change_list: &mut guillotiere_change_list_t) {
    std::mem::swap(change_list, &mut atlas.rearrange());
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_atlas_allocator_resize_and_rearrange(atlas: &mut guillotiere_atlas_allocator_t, new_size: guillotiere_size_t, change_list: &mut guillotiere_change_list_t) {
    std::mem::swap(change_list, &mut atlas.resize_and_rearrange(transmute(new_size)));
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_change_list_new() -> *mut guillotiere_change_list_t {
    Box::into_raw(Box::new(ChangeList { changes: Vec::new(), failures: Vec::new() }))    
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_change_list_delete(change_list: *mut guillotiere_change_list_t) {
    Box::from_raw(change_list);
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_change_list_changes(change_list: &guillotiere_change_list_t) -> guillotiere_changes_t {
    guillotiere_changes_t {
        changes: transmute(change_list.changes.as_ptr()),
        count: change_list.changes.len(),
    }
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_change_list_failures(change_list: &guillotiere_change_list_t) -> guillotiere_failures_t {
    guillotiere_failures_t {
        failures: transmute(change_list.failures.as_ptr()),
        count: change_list.failures.len(),
    }
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_new(size: guillotiere_size_t) -> *mut guillotiere_simple_atlas_allocator_t {
    Box::into_raw(Box::new(SimpleAtlasAllocator::new(transmute(size))))
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_with_options(size: guillotiere_size_t, options: &guillotiere_allocator_options_t) -> *mut guillotiere_simple_atlas_allocator_t {
    let options = from_ffi_options(options);
    Box::into_raw(Box::new(SimpleAtlasAllocator::with_options(transmute(size), &options)))
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_delete(atlas: *mut guillotiere_simple_atlas_allocator_t) {
    Box::from_raw(atlas);
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_size(atlas: &guillotiere_simple_atlas_allocator_t) -> guillotiere_size_t {
    transmute(atlas.size())
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_allocate(atlas: &mut guillotiere_simple_atlas_allocator_t, size: guillotiere_size_t, result: &mut guillotiere_rectangle_t) -> bool {
    if let Some(alloc) = atlas.allocate(transmute(size)) {
        *result = transmute(alloc);
        return true;
    }

    false
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_grow(atlas: &mut guillotiere_simple_atlas_allocator_t, new_size: guillotiere_size_t) {
    atlas.grow(transmute(new_size));
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_simple_atlas_allocator_init_from_allocator(atlas: &mut guillotiere_simple_atlas_allocator_t, src: &guillotiere_atlas_allocator_t) {
    atlas.init_from_allocator(src);
}

#[no_mangle]
pub unsafe extern "C"
fn guillotiere_allocator_options_default(options: &mut guillotiere_allocator_options_t) {
    *options = guillotiere_allocator_options_t {
        snap_size: DEFAULT_OPTIONS.snap_size,
        small_size_threshold: DEFAULT_OPTIONS.small_size_threshold,
        large_size_threshold: DEFAULT_OPTIONS.large_size_threshold,
    };
}

// TODO:
// for_each_free_rectangle
// for_each_allocated_rectangle
// svg dump