Giter Club home page Giter Club logo

svg2polylines's Introduction

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.

svg2polylines's People

Contributors

dbrgn avatar enjmiah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.