pikt 0.1.0

Pikchr high-level API
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use pikchr_sys::{pikchr, PIKCHR_DARK_MODE, PIKCHR_PLAINTEXT_ERRORS};
use std::ffi::{CStr, CString, NulError};
use std::str::FromStr;
use thiserror::Error;

bitflags::bitflags! {
    /// Flags to configure the render behaviour.
    ///
    /// Note that `PIKCHR_PLAINTEXT_ERRORS` can't be switched off because errors
    /// are handled by pikt.
    #[derive(Default)]
    pub struct Flags: u32 {
        // const PLAINTEXT_ERRORS = PIKCHR_PLAINTEXT_ERRORS;
        const DARK_MODE = PIKCHR_DARK_MODE;
    }
}

/// Represents the set of options the renderer can take.
///
/// Use the [`OptionsBuilder`] to construct it.
#[derive(Debug, Clone, PartialEq)]
pub struct Options {
    flags: Flags,
    width: u32,
    height: u32,
    class: String,
}

impl Options {
    pub fn flags(&self) -> Flags {
        self.flags
    }

    pub fn width(&self) -> u32 {
        self.width
    }

    pub fn height(&self) -> u32 {
        self.height
    }

    pub fn class(&self) -> &str {
        &self.class
    }
}

#[derive(Debug, Clone)]
pub struct OptionsBuilder {
    flags: Flags,
    width: u32,
    height: u32,
    class: String,
}

impl Default for OptionsBuilder {
    fn default() -> Self {
        Self {
            flags: Flags::empty(),
            width: 0,
            height: 0,
            class: "pikchr".to_string(),
        }
    }
}

impl OptionsBuilder {
    pub fn flags(&mut self, flags: Flags) {
        self.flags = flags;
    }

    pub fn width(&mut self, width: u32) {
        self.width = width;
    }

    pub fn height(&mut self, height: u32) {
        self.height = height;
    }

    /// Replaces the entire value for `class`. See [`OptionsBuilder.classes`] to append a list of
    /// values.
    ///
    /// By default it already has the value `pikchr`.
    pub fn class(&mut self, class: &str) {
        self.class = class.to_string();
    }

    pub fn classes(&mut self, values: &[&str]) {
        let s = values.join(" ");
        self.class.push(' ');
        self.class.push_str(&s);
    }

    /// Builds the set of options.
    ///
    /// ## Example
    ///
    /// ```
    /// use pikt::OptionsBuilder;
    ///
    /// let mut builder = OptionsBuilder::default();
    /// builder.width(300);
    /// builder.height(150);
    /// builder.classes(&vec!["foo", "bar"]);
    /// let options = builder.build();
    ///
    /// assert_eq!(options.width(), 300);
    /// assert_eq!(options.class(), "pikchr foo bar");
    /// ```
    pub fn build(self) -> Options {
        Options {
            flags: self.flags,
            width: self.width,
            height: self.height,
            class: self.class,
        }
    }
}

/// Renders the given pikchr markup as SVG.
///
/// Use [`render_with`] if you want to change the default options.
///
/// ## Example
///
/// ```
/// use pikt::render;
///
/// let markup = r#"
/// circle "1"
/// move
/// circle "2"
/// arrow from first circle.end to last circle.start
/// "#;
/// let svg = render(markup);
///
/// assert!(svg.is_ok());
/// ```
pub fn render(input: &str) -> Result<String, PiktError> {
    let options = OptionsBuilder::default().build();
    render_with(input, options)
}

/// Renders the given pikchr markup as SVG with the given configuration.
///
/// ```
/// use pikt::{render_with, OptionsBuilder, Flags};
///
/// let markup = r#"
/// circle "1"
/// move
/// circle "2"
/// arrow from first circle.end to last circle.start
/// "#;
/// let mut opt_builder = OptionsBuilder::default();
/// opt_builder.flags(Flags::DARK_MODE);
/// opt_builder.classes(&["foo", "bar"]);
/// let options = opt_builder.build();
/// let svg = render_with(markup, options);
///
/// assert!(svg.is_ok());
/// ```
///
/// ## Errors
///
/// It can fail either because the given input has an unexpected NUL terminator or for any of the
/// errors the native pikchr library handles. See [`PiktError`].
pub fn render_with(input: &str, options: Options) -> Result<String, PiktError> {
    use libc::free;
    use std::os::raw::*;

    let mut width: c_int = options.width() as i32;
    let mut height: c_int = options.height() as i32;
    let class = CString::new(options.class())?;
    let input = CString::new(input)?;

    let res: *mut c_char = unsafe {
        pikchr(
            input.as_ptr() as *const c_char,
            class.as_ptr() as *const c_char,
            options.flags().bits() | 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) };

    if width < 0 {
        let err = PiktError::from_str(&output).unwrap();
        return Err(err);
    }

    Ok(output)
}

#[derive(Error, Debug, PartialEq)]
#[error("line {line}, column {column}: {reason}")]
pub struct PiktError {
    line: usize,
    column: usize,
    reason: PiktErrorReason,
}

#[derive(Error, Debug, PartialEq)]
pub enum PiktErrorReason {
    /// Raised when the given input has a nul byte.
    #[error("incompatible input. Nul bytes are not allowed.")]
    IncompatibleInput(NulError),
    #[error("parser stack overflow")]
    ParserStackOverflow,
    #[error("out of memory")]
    OutOfMemory,
    #[error("division by zero")]
    DivisionByZero,
    #[error("syntax error")]
    SyntaxError,
    #[error("arc geometry error")]
    ArcGeometryError,
    #[error("unknown object")]
    UnknownObject,
    #[error("unknown object type")]
    UnknownObjectType,
    #[error("value already set")]
    ValueAlreadySet,
    #[error("value already fixed by prior constraints")]
    ValueAlreadyFixed,
    #[error("use with line-oriented objects only")]
    OnlyWithLineOrientedObject,
    #[error("no prior path points")]
    NoPriorPathPoints,
    #[error("headings should be between 0 and 360")]
    HeadingOutOfBounds,
    #[error("use `at` to position this object")]
    MissingAt,
    #[error("use `from` and `to` to position this object")]
    MissingFromTo,
    #[error("polygon is closed")]
    ClosedPolygon,
    #[error("line start position already fixed")]
    StartLineAlreadyFixed,
    #[error("need at least 3 vertexes in order to close the polygon")]
    TooFewVertexes,
    #[error("location fixed by prior `at`")]
    PositionAlreadyFixedByAt,
    #[error("too many text terms")]
    AttributeTooManyTerms,
    #[error("no text to fit to")]
    AttributeMissingText,
    #[error("unknown color name")]
    UnknownColorName,
    #[error("unknown variable")]
    UnknownVariable,
    #[error("the maximum ordinal is `1000th`")]
    OrdinalOutOfBounds,
    #[error("no prior objects of the same type")]
    MissingPriorObjectType,
    #[error("object is not a line")]
    NotALine,
    #[error("unknown vertex")]
    VertexUnknown,
    #[error("negative sqrt")]
    NegativeSqrt,
    #[error("too many macro arguments - max 9")]
    MacroTooManyArguments,
    #[error("unterminated macro argument list")]
    MacroUnterminatedArgumentList,
    #[error("token is too long - max length 50000 bytes")]
    TokenTooLong,
    #[error("unknown token")]
    TokenUnknown,
    #[error("macros nested too deep")]
    MacroTooDeep,
    #[error("recursive macro definition")]
    MacroRecursive,

    /// Raised when the given pikchr input cannot be parsed by Pikchr for an unknown reason.
    #[error("other")]
    Other(String),
}

impl FromStr for PiktError {
    type Err = PiktError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use PiktErrorReason::*;

        if s.contains("parser stack overflow") {
            return Ok(PiktError {
                line: 0,
                column: 0,
                reason: ParserStackOverflow,
            });
        }
        if s.contains("Out of memory") {
            return Ok(PiktError {
                line: 0,
                column: 0,
                reason: OutOfMemory,
            });
        }

        let line_padding = 12;
        let lines = s.lines();
        let mut message = "unknown error";
        let mut err = PiktError {
            line: 0,
            column: 0,
            reason: Other(message.to_string()),
        };

        for line in lines {
            // markup lines are formatted like:
            //
            // /*    1 */  circle "1"
            if line.starts_with("/*") {
                err.line += 1;
            }

            // caret lines always end with a caret. multiple carets are ignored.
            if line.ends_with('^') {
                err.column = line.len() + 1 - line_padding;
            }

            // the last line always follow a pattern like:
            //
            // ERROR: <error message>
            if line.starts_with("ERROR:") {
                if let Some((_, msg)) = line.split_once(' ') {
                    message = msg
                };
            }
        }

        err.reason = match message {
            "division by zero" => DivisionByZero,
            "syntax error" => SyntaxError,
            "arc geometry error" => ArcGeometryError,
            "unknown object type" => UnknownObjectType,
            "no such object" => UnknownObject,
            "value is already set" => ValueAlreadySet,
            "value already fixed by prior constraints" => ValueAlreadyFixed,
            "use with line-oriented objects only" => OnlyWithLineOrientedObject,
            "no prior path points" => NoPriorPathPoints,
            "too many path elements" => NoPriorPathPoints,
            "headings should be between 0 and 360" => HeadingOutOfBounds,
            "use \"at\" to position this object" => MissingAt,
            "use \"from\" and \"to\" to position this object" => MissingFromTo,
            "polygon is closed" => ClosedPolygon,
            "need at least 3 vertexes in order to close the polygon" => TooFewVertexes,
            "line start location already fixed" => StartLineAlreadyFixed,
            "location fixed by prior \"at\"" => PositionAlreadyFixedByAt,
            "too many text terms" => AttributeTooManyTerms,
            "no text to fit to" => AttributeMissingText,
            "not a known color name" => UnknownColorName,
            "no such variable" => UnknownVariable,
            "value too big - max '1000th'" => OrdinalOutOfBounds,
            "no prior objects of the same type" => MissingPriorObjectType,
            "object is not a line" => NotALine,
            "no such vertex" => VertexUnknown,
            "sqrt of negative value" => NegativeSqrt,
            "too many macro arguments - max 9" => MacroTooManyArguments,
            "unterminated macro argument list" => MacroUnterminatedArgumentList,
            "token is too long - max length 50000 bytes" => TokenTooLong,
            "unrecognized token" => TokenUnknown,
            "macros nested too deep" => MacroTooDeep,
            "recursive macro definition" => MacroRecursive,
            msg => Other(msg.to_string()),
        };

        Ok(err)
    }
}

impl From<NulError> for PiktError {
    fn from(err: NulError) -> Self {
        Self {
            line: 0,
            column: 0,
            reason: PiktErrorReason::IncompatibleInput(err),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn simple_box() -> Result<(), PiktError> {
        let source = "box \"pikchr\"";
        let expected = "<svg xmlns='http://www.w3.org/2000/svg' class=\"pikchr\" viewBox=\"0 0 112.32 76.32\">\n<path d=\"M2,74L110,74L110,2L2,2Z\"  style=\"fill:none;stroke-width:2.16;stroke:rgb(0,0,0);\" />\n<text x=\"56\" y=\"38\" text-anchor=\"middle\" fill=\"rgb(0,0,0)\" dominant-baseline=\"central\">pikchr</text>\n</svg>\n";

        let actual = render(source)?;

        assert_eq!(&actual, expected);

        Ok(())
    }

    #[test]
    fn input_with_nul() {
        let source = "box \"pikchr\"\0";

        let actual = render(source);

        assert!(actual.is_err(), "expected a nul pointer error");
    }

    #[test]
    fn malformed_input() {
        let source = "box 'pikchr'";

        let actual = render(source);

        assert_eq!(
            actual.expect_err("expected unknown token"),
            PiktError {
                line: 1,
                column: 5,
                reason: PiktErrorReason::TokenUnknown,
            }
        );
    }

    #[test]
    fn division_by_zero() {
        let source = r#"box "pikchr"
        arrow from first box to (0/0, 0)
        "#;

        let actual = render(source);

        assert_eq!(
            actual.expect_err("expected div by zero err"),
            PiktError {
                line: 2,
                column: 36,
                reason: PiktErrorReason::DivisionByZero,
            }
        );
    }

    #[test]
    fn syntax_error() {
        let source = r#"circ "1""#;

        let actual = render(source);

        assert_eq!(
            actual.expect_err("expected syntax error"),
            PiktError {
                line: 1,
                column: 8,
                reason: PiktErrorReason::SyntaxError,
            }
        );
    }

    #[test]
    fn unknown_object() {
        let source = r#"arrow from A to B"#;

        let actual = render(source);

        assert_eq!(
            actual.expect_err("expected unknown object"),
            PiktError {
                line: 1,
                column: 12,
                reason: PiktErrorReason::UnknownObject,
            }
        );
    }

    #[test]
    fn box_dark_mode() -> Result<(), PiktError> {
        let source = "box \"pikchr\"";
        let expected = "<svg xmlns='http://www.w3.org/2000/svg' class=\"pikchr\" viewBox=\"0 0 112.32 76.32\">\n<path d=\"M2,74L110,74L110,2L2,2Z\"  style=\"fill:none;stroke-width:2.16;stroke:rgb(255,255,255);\" />\n<text x=\"56\" y=\"38\" text-anchor=\"middle\" fill=\"rgb(255,255,255)\" dominant-baseline=\"central\">pikchr</text>\n</svg>\n";
        let mut flags = Flags::default();
        flags.insert(Flags::DARK_MODE);

        let mut builder = OptionsBuilder::default();
        builder.flags(flags);
        let options = builder.build();

        let actual = render_with(source, options)?;

        assert_eq!(&actual, expected);

        Ok(())
    }
}