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
use std::{os::raw::c_char, ptr};
use detexify::{iter_symbols, Classifier, Point, Score, Stroke, StrokeSample, Symbol};
pub struct StrokeBuilder {
points: Vec<Point>,
}
#[no_mangle]
pub unsafe extern "C" fn stroke_builder_new(capacity: usize) -> *mut StrokeBuilder {
Box::into_raw(Box::new(StrokeBuilder {
points: Vec::with_capacity(capacity),
}))
}
#[no_mangle]
pub unsafe extern "C" fn stroke_builder_add_point(builder: *mut StrokeBuilder, x: f64, y: f64) {
(*builder).points.push(Point { x, y })
}
#[no_mangle]
pub unsafe extern "C" fn stroke_builder_build(builder: *mut StrokeBuilder) -> *mut Stroke {
let points = Box::from_raw(builder).points;
Box::into_raw(Box::new(Stroke::new(points)))
}
pub struct StrokeSampleBuilder {
strokes: Vec<Stroke>,
}
#[no_mangle]
pub unsafe extern "C" fn stroke_sample_builder_new(capacity: usize) -> *mut StrokeSampleBuilder {
Box::into_raw(Box::new(StrokeSampleBuilder {
strokes: Vec::with_capacity(capacity),
}))
}
#[no_mangle]
pub unsafe extern "C" fn stroke_sample_builder_add_stroke(
builder: *mut StrokeSampleBuilder,
stroke: *mut Stroke,
) {
(*builder).strokes.push(*Box::from_raw(stroke));
}
#[no_mangle]
pub unsafe extern "C" fn stroke_sample_builder_build(
builder: *mut StrokeSampleBuilder,
) -> *mut StrokeSample {
let stroke_sample_builder = Box::from_raw(builder);
match StrokeSample::new(stroke_sample_builder.strokes) {
Some(sample) => Box::into_raw(Box::new(sample)),
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn classifier_new_default() -> *mut Classifier {
Box::into_raw(Box::new(Classifier::default()))
}
pub unsafe extern "C" fn classifier_free(classifier: *mut Classifier) {
Box::from_raw(classifier);
}
pub struct Scores {
scores: Vec<Score>,
}
#[no_mangle]
pub unsafe extern "C" fn classify(
classifier: *mut Classifier,
sample: *mut StrokeSample,
) -> *mut Scores {
match (*classifier).classify(*Box::from_raw(sample)) {
Some(scores) => Box::into_raw(Box::new(Scores { scores })),
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn scores_length(scores: *mut Scores) -> usize {
(*scores).scores.len()
}
#[no_mangle]
pub unsafe extern "C" fn scores_get_score(scores: *mut Scores, i: usize) -> f64 {
(*scores).scores.get_unchecked(i).score
}
#[no_mangle]
pub unsafe extern "C" fn scores_get_symbol(scores: *mut Scores, i: usize) -> *const Symbol {
match Symbol::from_id(&(*scores).scores[i].id) {
Some(symbol) => Box::into_raw(Box::new(symbol)),
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn scores_free(scores: *mut Scores) {
Box::from_raw(scores);
}
#[no_mangle]
pub unsafe extern "C" fn symbol_get_command(
symbol: *const Symbol,
buffer: *mut c_char,
len: usize,
) {
let command = (*symbol).command.as_bytes();
ptr::copy(
command.as_ptr(),
buffer as *mut u8,
usize::min(len, command.len()),
);
ptr::write(buffer.offset(usize::min(len - 1, command.len()) as isize), 0)
}
#[no_mangle]
pub unsafe extern "C" fn symbol_get_package(
symbol: *const Symbol,
buffer: *mut c_char,
len: usize,
) {
let package = (*symbol).package.as_bytes();
ptr::copy(
package.as_ptr(),
buffer as *mut u8,
usize::min(len, package.len()),
);
ptr::write(buffer.offset(usize::min(len - 1, package.len()) as isize), 0)
}
#[no_mangle]
pub unsafe extern "C" fn symbol_get_font_encoding(
symbol: *const Symbol,
buffer: *mut c_char,
len: usize,
) {
let font_encoding = (*symbol).font_encoding.as_bytes();
ptr::copy(
font_encoding.as_ptr(),
buffer as *mut u8,
usize::min(len, font_encoding.len()),
);
ptr::write(buffer.offset(usize::min(len - 1, font_encoding.len()) as isize), 0)
}
#[no_mangle]
pub unsafe extern "C" fn symbol_get_text_mode(symbol: *const Symbol) -> bool {
(*symbol).text_mode
}
#[no_mangle]
pub unsafe extern "C" fn symbol_get_math_mode(symbol: *const Symbol) -> bool {
(*symbol).math_mode
}
#[no_mangle]
pub unsafe extern "C" fn symbol_free(symbol: *mut Symbol) {
Box::from_raw(symbol);
}
#[no_mangle]
pub unsafe extern "C" fn symbols_count() -> usize {
iter_symbols().count()
}
#[no_mangle]
pub unsafe extern "C" fn symbols_get(i: usize) -> *mut Symbol {
match iter_symbols().nth(i) {
Some(symbol) => Box::into_raw(Box::new(symbol)),
None => ptr::null_mut(),
}
}