2 releases

0.1.1 Sep 13, 2021
0.1.0 Sep 13, 2021

#5 in #pic


Used in pikt

0BSD license

285KB
7K SLoC

C 7K SLoC // 0.1% comments Rust 51 SLoC

Pikchr-sys

Pikchr bindings for the Rust programming language.

High-level API

This crate provides bindings to the raw low-level C API. For a higher-level safe API to work with Pikchr see pikt.

Release support

License

pikchr-sys is licensed under the BSD Zero Clause License.

It bundles the compiled C89 source code (pikchr.c) and headers (pikchr.h) from Pikchr which is also licensed under the BSD Zero Clause license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in pikchr-sys by you shall be licensed as above without any additional terms or conditions.


lib.rs:

The raw bindings for pikchr.c.

Using pikchr will require manually freeing the buffer. Quoting the C source code:

This file implements a C-language subroutine that accepts a string of PIKCHR language text and generates a second string of SVG output that renders the drawing defined by the input. Space to hold the returned string is obtained from malloc() and should be freed by the caller. NULL might be returned if there is a memory allocation error.

The pikchr arguments are defined as follows:

  • zText: Input PIKCHR markup. Zero terminated.
  • zClass: The value to add to the SVG class attribute. It can be NULL.
  • mFlags: Flags to control the rendering behaviour.
  • pnWidth: The value for the SVG width attribute. It can be NULL.
  • pnHeight: The value for the SVG height attribute. It can be NULL.

Example

let input = "box \"example\"";
let mut width: c_int = 0;
let mut height: c_int = 0;
let input = CString::new(input)?;

let res: *mut c_char = unsafe {
    pikchr(
        input.as_ptr() as *const c_char,
        std::ptr::null(),
        PIKCHR_PLAINTEXT_ERRORS,
        &mut width as *mut c_int,
        &mut height as *mut c_int,
    )
};

let cstr = unsafe { CStr::from_ptr(res) };
let output = String::from_utf8_lossy(cstr.to_bytes()).into_owned();

unsafe { free(res as *mut c_void) };

Errors

If an error occurs, the width will be -1 and the buffer will contain the error message.

No runtime deps

~0–2MB
~37K SLoC