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>,
}

/// Creates a new stroke builder
#[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),
    }))
}

/// Adds a point to the stroke
#[no_mangle]
pub unsafe extern "C" fn stroke_builder_add_point(builder: *mut StrokeBuilder, x: f64, y: f64) {
    (*builder).points.push(Point { x, y })
}

/// Returns the stroke and frees `builder`
#[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>,
}

/// Creates a new stroke sample builder
#[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),
    }))
}

/// Adds a stroke to the stroke sample and frees the stroke
#[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));
}

/// Returns a stroke sample and free's `builder`
#[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(),
    }
}

/// Returns the default classifier
#[no_mangle]
pub unsafe extern "C" fn classifier_new_default() -> *mut Classifier {
    Box::into_raw(Box::new(Classifier::default()))
}

/// Free's a classifier
pub unsafe extern "C" fn classifier_free(classifier: *mut Classifier) {
    Box::from_raw(classifier);
}

pub struct Scores {
    scores: Vec<Score>,
}

/// Classifiy the sample returning scores and free's `sample`
#[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(),
    }
}

/// Returns the length of the list of symbols
#[no_mangle]
pub unsafe extern "C" fn scores_length(scores: *mut Scores) -> usize {
    (*scores).scores.len()
}

/// Returns the `i`-th score of `scores`
#[no_mangle]
pub unsafe extern "C" fn scores_get_score(scores: *mut Scores, i: usize) -> f64 {
    (*scores).scores.get_unchecked(i).score
}

/// Returns the `i`-th symbol of `scores`, callers responsible for calling `symbol_free` once finished
#[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(),
    }
}

/// Free's scores
#[no_mangle]
pub unsafe extern "C" fn scores_free(scores: *mut Scores) {
    Box::from_raw(scores);
}

/// Gets the command of the `i`-th score
#[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)
}

/// Gets the package of the `i`-th score
#[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)
}

/// Gets the font encoding of the `i`-th score
#[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)
}

/// Gets the text mode of the `i`-th score
#[no_mangle]
pub unsafe extern "C" fn symbol_get_text_mode(symbol: *const Symbol) -> bool {
    (*symbol).text_mode
}

/// Gets the math mode of the `i`-th score
#[no_mangle]
pub unsafe extern "C" fn symbol_get_math_mode(symbol: *const Symbol) -> bool {
    (*symbol).math_mode
}

/// Frees `symbol`
#[no_mangle]
pub unsafe extern "C" fn symbol_free(symbol: *mut Symbol) {
    Box::from_raw(symbol);
}

/// Returns the total number of symbols
#[no_mangle]
pub unsafe extern "C" fn symbols_count() -> usize {
    iter_symbols().count()
}

/// Returns the `i`-th symbol
#[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(),
    }
}