Skip to content

Commit

Permalink
Add tol parameter to remove flattening tolerance constant (#16)
Browse files Browse the repository at this point in the history
Remove `LATTENING_TOLERANCE` const and add `tol` parameter down from `parse()`.

- Update the ffi wrapper
- Tests are using the old const value

Closes #3.
  • Loading branch information
naufraghi committed Jul 20, 2021
1 parent e022171 commit 1bfbee3
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 68 deletions.
4 changes: 2 additions & 2 deletions svg2polylines-ffi/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct Polyline {
size_t len;
} Polyline;

uint8_t svg_str_to_polylines(char* svg, Polyline** polylines, size_t* polylines_len);
uint8_t svg_str_to_polylines(char* svg, double tol, Polyline** polylines, size_t* polylines_len);
void free_polylines(Polyline* polylines, size_t polylines_len);


Expand Down Expand Up @@ -53,7 +53,7 @@ int main() {
size_t polylines_len = 0;

// Process data
uint8_t err = svg_str_to_polylines(input, &polylines, &polylines_len);
uint8_t err = svg_str_to_polylines(input, 0.15, &polylines, &polylines_len);

// Error handling
if (err > 0) {
Expand Down
4 changes: 2 additions & 2 deletions svg2polylines-ffi/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
size_t len;
} Polyline;
uint8_t svg_str_to_polylines(char* svg, Polyline** polylines, size_t* polylines_len);
uint8_t svg_str_to_polylines(char* svg, double tol, Polyline** polylines, size_t* polylines_len);
void free_polylines(Polyline* polylines, size_t polylines_len);
''')

Expand Down Expand Up @@ -47,7 +47,7 @@ def print_polyline(p):

polylines = ffi.new('Polyline**')
polylines_len = ffi.new('size_t*')
lib.svg_str_to_polylines(svg_input, polylines, polylines_len)
lib.svg_str_to_polylines(svg_input, 0.15, polylines, polylines_len)


print('Found %d polylines!' % polylines_len[0])
Expand Down
5 changes: 3 additions & 2 deletions svg2polylines-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::ffi::CStr;
use std::mem;

use libc::{c_char, size_t};
use libc::{c_char, c_double, size_t};
use svg2polylines::{parse, CoordinatePair};

/// Structure that contains a pointer to the coordinate pairs as well as the
Expand Down Expand Up @@ -37,6 +37,7 @@ pub struct Polyline {
#[no_mangle]
pub unsafe extern "C" fn svg_str_to_polylines(
svg: *const c_char,
tol: c_double,
polylines: *mut *mut Polyline,
polylines_len: *mut size_t,
) -> u8 {
Expand All @@ -48,7 +49,7 @@ pub unsafe extern "C" fn svg_str_to_polylines(
let r_str = c_str.to_str().unwrap();

// Process
match parse(r_str) {
match parse(r_str, tol) {
Ok(vec) => {
// Convert `Vec<Vec<CoordinatePair>>` to `Vec<Polyline>`
let mut tmp_vec: Vec<Polyline> = vec
Expand Down
2 changes: 1 addition & 1 deletion svg2polylines/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {
file.read_to_string(&mut s).unwrap();

// Parse data
let polylines: Vec<Polyline> = svg2polylines::parse(&s).unwrap_or_else(|e| {
let polylines: Vec<Polyline> = svg2polylines::parse(&s, 0.15).unwrap_or_else(|e| {
println!("Error: {}", e);
exit(2);
});
Expand Down
2 changes: 1 addition & 1 deletion svg2polylines/examples/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
file.read_to_string(&mut s).unwrap();

// Parse data
let polylines: Vec<Polyline> = svg2polylines::parse(&s).unwrap_or_else(|e| {
let polylines: Vec<Polyline> = svg2polylines::parse(&s, 0.15).unwrap_or_else(|e| {
println!("Error: {}", e);
exit(2);
});
Expand Down
Loading

0 comments on commit 1bfbee3

Please sign in to comment.