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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Pikchr image creation binding
//!
//! This crate provides a binding for the
//! [`pikchr`](https://pikchr.org/home/doc/trunk/homepage.md) diagramming
//! language.  Using this crate you can convert PIC-like markup
//! into SVG diagrams trivially.  If you are embedding into HTML then
//! you can have any errors generated as HTML, otherwise errors are
//! generated as plain text.
//!
//! The main interface is the [`Pikchr`] struct, specifically its
//! [`Pikchr::render`] function.
//!
//! ```
//! use pikchr::{Pikchr, PikchrFlags};
//!
//! let INPUT = r#"
//! arrow right 200% "Markdown" "Source"
//! box rad 10px "Markdown" "Formatter" "(docs.rs/markdown)" fit
//! arrow right 200% "HTML+SVG" "Output"
//! arrow <-> down 70% from last box.s
//! box same "Pikchr" "Formatter" "(docs.rs/pikchr)" fit
//! "#;
//!
//! let pic = Pikchr::render(INPUT, None, PikchrFlags::default()).unwrap();
//!
//! println!("{}", pic);
//! ```
//! <svg xmlns='http://www.w3.org/2000/svg' viewBox="0 0 475.315 195.84"><polygon points="146,37 134,41 134,33" style="fill:rgb(0,0,0)"/><path d="M2,37L140,37"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /><text x="74" y="25" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Markdown</text><text x="74" y="49" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Source</text><path d="M161,72L309,72A15 15 0 0 0 324 57L324,17A15 15 0 0 0 309 2L161,2A15 15 0 0 0 146 17L146,57A15 15 0 0 0 161 72Z"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /><text x="235" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Markdown</text><text x="235" y="37" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Formatter</text><text x="235" y="57" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">(docs.rs/markdown)</text><polygon points="468,37 457,41 457,33" style="fill:rgb(0,0,0)"/><path d="M324,37L463,37"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /><text x="396" y="25" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">HTML+SVG</text><text x="396" y="49" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Output</text><polygon points="235,72 239,84 231,84" style="fill:rgb(0,0,0)"/><polygon points="235,123 231,111 239,111" style="fill:rgb(0,0,0)"/><path d="M235,78L235,117"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /><path d="M178,193L292,193A15 15 0 0 0 307 178L307,138A15 15 0 0 0 292 123L178,123A15 15 0 0 0 163 138L163,178A15 15 0 0 0 178 193Z"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /><text x="235" y="138" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Pikchr</text><text x="235" y="158" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Formatter</text><text x="235" y="178" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">(docs.rs/pikchr)</text></svg>

use libc::{c_char, c_int, c_uint, c_void, free};
use std::ffi::{CStr, CString};
use std::fmt;
use std::ops::Deref;

pub mod raw {
    use libc::{c_char, c_int, c_uint};

    extern "C" {
        /// The main interface.  Invoke this routine to translate PIKCHR source
        /// text into SVG. The SVG is returned in a buffer obtained from malloc().
        /// The caller is responsible for freeing the buffer.
        ///
        /// If an error occurs, *pnWidth is filled with a negative number and
        /// the return buffer contains error message text instead of SVG.  By
        /// default, the error message is HTML encoded.  However, error messages
        /// come out as plaintext if the PIKCHR_PLAINTEXT_ERRORS flag is included
        /// as one of the bits in the mFlags parameter.
        ///
        /// - `zText`: Input PIKCHR source text.  zero-terminated
        /// - `zClass`: Add class="%s" to <svg> markup
        /// - `mFlags`: Flags used to influence rendering behavior
        /// - `pnWidth`: OUT: Write width of <svg> here, if not NULL
        /// - `pnHeight`: OUT: Write height here, if not NULL
        #[allow(non_snake_case)]
        pub fn pikchr(
            zText: *const c_char,
            zClass: *const c_char,
            mFlags: c_uint,
            pnWidth: *mut c_int,
            pnHeight: *mut c_int,
        ) -> *mut c_char;
    }

    /// Include PIKCHR_PLAINTEXT_ERRORS among the bits of mFlags on the 3rd
    /// argument to pikchr() in order to cause error message text to come out
    /// as text/plain instead of as text/html
    pub const PIKCHR_PLAINTEXT_ERRORS: c_uint = 0x0001;
}

/// Flags for converting pikchr source
///
/// You can construct a default set of flags using the [`std::default::Default`] trait
///
/// The default flags will generate plain text errors
#[derive(Copy, Clone)]
pub struct PikchrFlags {
    plain_errors: bool,
}

impl PikchrFlags {
    /// Return whether or not plain text errors will be generated
    ///
    /// ```
    /// # use pikchr::PikchrFlags;
    /// let flags = PikchrFlags::default();
    /// assert!(flags.plain_errors())
    /// ```
    pub fn plain_errors(&self) -> bool {
        self.plain_errors
    }

    /// Request plain text errors be generated
    ///
    /// ```
    /// # use pikchr::PikchrFlags;
    /// let mut flags = PikchrFlags::default();
    /// flags.generate_plain_errors();
    /// assert!(flags.plain_errors());
    /// ```
    pub fn generate_plain_errors(&mut self) -> &mut PikchrFlags {
        self.plain_errors = true;
        self
    }

    /// Request help encoded errors be generated
    ///
    /// ```
    /// # use pikchr::PikchrFlags;
    /// let mut flags = PikchrFlags::default();
    /// flags.generate_html_errors();
    /// assert!(!flags.plain_errors());
    /// ```
    pub fn generate_html_errors(&mut self) -> &mut PikchrFlags {
        self.plain_errors = false;
        self
    }
}

impl Into<c_uint> for PikchrFlags {
    fn into(self) -> c_uint {
        if self.plain_errors {
            raw::PIKCHR_PLAINTEXT_ERRORS
        } else {
            0
        }
    }
}

impl std::default::Default for PikchrFlags {
    fn default() -> Self {
        Self { plain_errors: true }
    }
}

/// A rendered pikchr diagram
///
/// Pikchr renders diagrams as SVG.  This SVG is a given width
/// and height.  The Pikchr derefs to the SVG string, or you
/// can access it explicitly.  The width and height are accessible
/// as plain numbers.
pub struct Pikchr {
    rendered: *const c_char,
    width: c_int,
    height: c_int,
}

impl Drop for Pikchr {
    fn drop(&mut self) {
        if self.rendered.is_null() {
            unsafe {
                free(self.rendered as *mut c_void);
            }
            self.rendered = std::ptr::null();
        }
    }
}

impl Deref for Pikchr {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        // We're assuming a Pikchr instance can only
        // be constructed from valid utf8 and thus can
        // only contain valid utf8
        unsafe {
            let cstr = CStr::from_ptr(self.rendered);
            std::str::from_utf8_unchecked(cstr.to_bytes())
        }
    }
}

impl fmt::Display for Pikchr {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.write_str(self)
    }
}

impl Pikchr {
    /// Render some input pikchr source as an SVG
    ///
    /// You can convert arbitrary pikchr source into an SVG using this function.
    /// The class name is optional, and the flags field controls the generation
    /// of errors.  Since pikchr does not have a structured error format, the
    /// returned error is simply a string.
    ///
    /// ```
    /// # use pikchr::{Pikchr, PikchrFlags};
    /// let image = Pikchr::render(r#"
    /// arrow right 200% "Markdown" "Source"
    /// box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
    /// arrow right 200% "HTML+SVG" "Output"
    /// arrow <-> down 70% from last box.s
    /// box same "Pikchr" "Formatter" "(pikchr.c)" fit"#,
    ///      None, PikchrFlags::default())
    ///     .unwrap();
    /// assert!(image.contains("<svg"))
    /// ```
    pub fn render(source: &str, class: Option<&str>, flags: PikchrFlags) -> Result<Pikchr, String> {
        let mut width: c_int = 0;
        let mut height: c_int = 0;
        let source = CString::new(source).map_err(|e| format!("{:?}", e))?;
        let res: *mut c_char = unsafe {
            raw::pikchr(
                source.as_ptr() as *const c_char,
                class
                    .map(|s| s.as_ptr() as *const c_char)
                    .unwrap_or(std::ptr::null()),
                flags.into(),
                &mut width as *mut c_int,
                &mut height as *mut c_int,
            )
        };
        if width < 0 {
            let err = unsafe { CStr::from_ptr(res) };
            let err = err.to_bytes();
            let err = String::from_utf8_lossy(err).into_owned();
            unsafe {
                free(res as *mut c_void);
            }
            Err(err)
        } else {
            Ok(Pikchr {
                rendered: res,
                width,
                height,
            })
        }
    }

    /// Retrieve the width of this Pikchr
    ///
    /// ```
    /// # use pikchr::{Pikchr, PikchrFlags};
    /// # let pic = Pikchr::render(r#"arrow right 200% "Markdown" "Source""#,
    /// #     None, PikchrFlags::default()).unwrap();
    /// println!("Picture is {} pixels wide", pic.width());
    /// ```
    pub fn width(&self) -> isize {
        self.width as isize
    }

    /// Retrieve the height of this Pikchr
    ///
    /// ```
    /// # use pikchr::{Pikchr, PikchrFlags};
    /// # let pic = Pikchr::render(r#"arrow right 200% "Markdown" "Source""#,
    /// #     None, PikchrFlags::default()).unwrap();
    /// println!("Picture is {} pixels tall", pic.height());
    /// ```
    pub fn height(&self) -> isize {
        self.height as isize
    }

    /// Retrieve the rendered pikchr (same as dereferencing)
    ///
    /// ```
    /// # use pikchr::{Pikchr, PikchrFlags};
    /// # let pic = Pikchr::render(r#"arrow right 200% "Makdown" "Source""#,
    /// #     None, PikchrFlags::default()).unwrap();
    /// println!("Picture content:\n{}", pic.rendered());
    /// ```
    pub fn rendered(&self) -> &str {
        &*self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn validate_diagram() {
        const SOURCE: &str = r#"arrow right 200% "Markdown" "Source""#;
        const OUTPUT: &str = r#"<svg xmlns='http://www.w3.org/2000/svg' viewBox="0 0 152.64 47.88">
<polygon points="146,23 134,28 134,19" style="fill:rgb(0,0,0)"/>
<path d="M2,23L140,23"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" />
<text x="74" y="12" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Markdown</text>
<text x="74" y="35" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">Source</text>
</svg>
"#;
        let flags = PikchrFlags::default();
        let p = Pikchr::render(SOURCE, None, flags).unwrap();
        assert_eq!(OUTPUT, p.rendered());
    }
}