All Projects → dbrgn → svg2polylines

dbrgn / svg2polylines

Licence: other
Rust library to convert SVG data to a list of flattened polylines. Also includes FFI bindings.

Programming Languages

rust
11053 projects
c
50402 projects - #5 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to svg2polylines

romans
A Simple PHP Roman Numerals Library
Stars: ✭ 40 (+150%)
Mutual labels:  converter
Tracker
Even the best of apps have their issues
Stars: ✭ 113 (+606.25%)
Mutual labels:  converter
xml-to-json
Simple API that converts dynamic XML feeds to JSON through a URL or pasting the raw XML data. Made 100% in PHP.
Stars: ✭ 38 (+137.5%)
Mutual labels:  converter
gluon2pytorch
Gluon to PyTorch deep neural network model converter
Stars: ✭ 72 (+350%)
Mutual labels:  converter
cyan
Cyan Color Converter
Stars: ✭ 68 (+325%)
Mutual labels:  converter
video2gif
A batch script for convert video to GIF files by FFmpeg.exe on Windows
Stars: ✭ 48 (+200%)
Mutual labels:  converter
CTR-tools
Crash Team Racing (PS1) tools - a C# framework by DCxDemo and a set of tools to parse files found in the original kart racing game by Naughty Dog.
Stars: ✭ 93 (+481.25%)
Mutual labels:  converter
Text to MD
Convert your docs to markdown format.
Stars: ✭ 15 (-6.25%)
Mutual labels:  converter
mdtable2csv
convert tables in .md to .csv
Stars: ✭ 91 (+468.75%)
Mutual labels:  converter
form-data-json
A zero dependency, cross browser library to easily get or set/manipulate form input values as/from a json object.
Stars: ✭ 37 (+131.25%)
Mutual labels:  converter
medium-to-markdown
Converts Medium posts to markdown.
Stars: ✭ 68 (+325%)
Mutual labels:  converter
bot-whatsapp
Unmaintained - Multipurpose WhatsApp Bot 🤖 using open-wa/wa-automate-nodejs library! ✨
Stars: ✭ 78 (+387.5%)
Mutual labels:  converter
color-converter
Command line tool for converting colors from RGB to HEX and vice versa.
Stars: ✭ 17 (+6.25%)
Mutual labels:  converter
Rates
A currency rate converter App.
Stars: ✭ 13 (-18.75%)
Mutual labels:  converter
react-in-out-textarea
A simple React.js User Interface Component that is like Google Translate with full TypeScript Support
Stars: ✭ 28 (+75%)
Mutual labels:  converter
CodeProject
Common code for unity project develop.
Stars: ✭ 28 (+75%)
Mutual labels:  converter
ical2json
A simple node package to convert ical data to json
Stars: ✭ 46 (+187.5%)
Mutual labels:  converter
Video-to-audio-converter
A simple tool to convert video files into mp3 audio files
Stars: ✭ 40 (+150%)
Mutual labels:  converter
ubase
remove accents from utf8 strings
Stars: ✭ 14 (-12.5%)
Mutual labels:  converter
ipynb-py-convert
Convert .py files runnable in VSCode or Atom/Hydrogen to Jupyter .ipynb notebooks and vice versa
Stars: ✭ 38 (+137.5%)
Mutual labels:  converter

svg2polylines

CircleCI Crates.io

Convert SVG data to a list of polylines (aka polygonal chains or polygonal paths).

This can be used e.g. for simple drawing robot that just support drawing straight lines and liftoff / drop pen commands.

Flattening of Bézier curves is done using the Lyon library.

Note: Currently the path style is completely ignored. Only the path itself is returned.

This repository contains two creates:

  • svg2polylines contains all the functionality and can be used like a regular Rust library.
  • svg2polylines-ffi contains a C interface so that the library can be used from other programming languages like C or Python.

Preview

There is a small preview tool to view the generated polylines. It's simple and hacky, but helps to debug stuff.

cd svg2polylines
cargo run --release --example preview path/to/file.svg

The --release parameter is important, otherwise it's going to be very slow.

Use the mouse to drag the image and the Esc key to close the window.

Usage: Rust

Signature:

fn svg2polylines::parse(&str) -> Result<Vec<Polyline>, String>;

See svg2polylines/examples/basic.rs for a full usage example.

FFI

A shared library can be built in the svg2polylines-ffi directory with cargo build. You will then find a libsvg2polylines.so file in the target directory.

The C interface for svg2polylines looks like this

typedef struct CoordinatePair {
    double x;
    double y;
} CoordinatePair;

typedef struct Polyline {
    CoordinatePair* ptr;
    size_t len;
} Polyline;

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

You should call the svg_str_to_polylines function with the following arguments:

  • A pointer to the SVG contents (must be valid UTF8).
  • A pointer to a Polyline pointer. It can be initialized to NULL and will be updated by the Rust library code.
  • A pointer to a size_t variable. The variable will be updated by the Rust library code.

The return value indicates errors during processing. You must check it before accessing the polylines and polylines_len pointers. If it equals 0, then processing was successful.

Make sure to free the memory again with free_polylines once you're done.

Usage: C

A C usage example can be found at svg2polylines-ffi/example.c.

Compile it like this:

$ clang example.c -o example -L target/debug/ -lsvg2polylines -Wall -Wextra -g

Then run the resulting binary like this:

$ LD_LIBRARY_PATH=target/debug/ ./example

Example output:

Found 2 polylines!
Out vec address: 0x55c90045b180
Polyline 1:
  Address: 0x55c90045b180
  Length: 4
  Points to: 0x55c90045b010
  Data:
    (0.000000, 0.000000)
    (-40.443453, 44.601188)
    (65.767856, 4.913690)
    (70.303571, 34.306548)
Polyline 2:
  Address: 0x55c90045b190
  Length: 4
  Points to: 0x55c90045b0f0
  Data:
    (0.000000, 35.818452)
    (40.443450, 35.818452)
    (-39.687500, 49.514881)
    (40.065480, 49.514881)

Usage: Python

A Python usage example (with CFFI) can be found at svg2polylines-ffi/example.py.

Simply run the script:

$ python example.py

Example output:

Found 2 polylines!
Polyline 1:
  Length: 4
  Points to: <cdata 'CoordinatePair *' 0x2305350>
  Data:
    (0.000000, 34.306548)
    (-40.443453, 44.601188)
    (65.767856, 4.913690)
    (70.303571, 34.306548)
Polyline 2:
  Length: 4
  Points to: <cdata 'CoordinatePair *' 0x2263370>
  Data:
    (0.000000, 35.818452)
    (40.443450, 35.818452)
    (-39.687500, 49.514881)
    (40.065480, 49.514881)

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].