wavedrom 0.1.0

A Pure Rust Digital Timing Diagram Generator based on WaveDrom-JS
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
use std::borrow::Cow;
use std::io;

use crate::path::{PathAssembleOptions, PathCommand, PathSegmentBackground};
use crate::{ClockEdge, Color};

use self::edges::{write_edge_text, write_line_edge, write_line_edge_markers};
use self::options::SignalOptions;

use super::path::AssembledSignalPath;
use super::AssembledFigure;

mod dimensions;
mod edges;
mod font;
pub mod options;

use dimensions::SvgDimensions;
pub use font::Font;
use options::RenderOptions;

fn escape_str(s: &str) -> Cow<str> {
    if !s.contains(['<', '>', '"', '&']) {
        return Cow::Borrowed(s);
    }

    let mut output = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            '"' => output.push_str("&quot;"),
            '&' => output.push_str("&amp;"),
            _ => output.push(c),
        }
    }

    Cow::Owned(output)
}

fn gap(
    writer: &mut impl io::Write,
    wave_height: u16,
    color: Color,
    background: Color,
) -> io::Result<()> {
    let wave_height = f32::from(wave_height);

    let a: f32 = 8.0;
    let b = wave_height / 2.0 + 6.0;

    const DISTANCE: f32 = 4.0;

    let start = (-a, b);
    let end = (a, -b);

    let control_1 = (-a / 2.0, b);

    let rad = (-2.0 * b / a).atan();
    let control_2 = (rad.cos() * a / -2.0, rad.sin() * a / -2.0);

    let control_3 = (a / 2.0, -b);

    write!(
        writer,
        r##"<path d="M{lp1x},{lp1y}C{lp2x},{lp2y} {lp3x},{lp3y} {lp4x},{lp4y}S{lp5x},{lp5y} {lp6x},{lp6y}H{rp1x}C{rp2x},{rp2y} {rp3x},{rp3y} {rp4x},{rp4y}S{rp5x},{rp5y} {rp6x},{rp6y}H{lp1x}z" fill="{background}" stroke="none"/><path d="M{lp1x},{lp1y}C{lp2x},{lp2y} {lp3x},{lp3y} {lp4x},{lp4y}S{lp5x},{lp5y} {lp6x},{lp6y}" fill="none" stroke="{color}" stroke-width="1"/><path d="M{rp1x},{rp1y}C{rp2x},{rp2y} {rp3x},{rp3y} {rp4x},{rp4y}S{rp5x},{rp5y} {rp6x},{rp6y}" fill="none" stroke="{color}" stroke-width="1"/>"##,
        lp1x = start.0 - DISTANCE / 2.0,
        lp1y = start.1,
        lp2x = control_1.0 - DISTANCE / 2.0,
        lp2y = control_1.1,
        lp3x = control_2.0 - DISTANCE / 2.0,
        lp3y = control_2.1,
        lp4x = 0.0 - DISTANCE / 2.0,
        lp4y = 0,
        lp5x = control_3.0 - DISTANCE / 2.0,
        lp5y = control_3.1,
        lp6x = end.0 - DISTANCE / 2.0,
        lp6y = end.1,
        rp1x = end.0 + DISTANCE / 2.0,
        rp1y = end.1,
        rp2x = control_3.0 + DISTANCE / 2.0,
        rp2y = control_3.1,
        rp3x = -control_2.0 + DISTANCE / 2.0,
        rp3y = -control_2.1,
        rp4x = 0.0 + DISTANCE / 2.0,
        rp4y = 0,
        rp5x = control_1.0 + DISTANCE / 2.0,
        rp5y = control_1.1,
        rp6x = start.0 + DISTANCE / 2.0,
        rp6y = start.1,
    )
}

fn posedge_arrow(writer: &mut impl io::Write, wave_height: u32, color: Color) -> io::Result<()> {
    let scale = i64::from(wave_height / 6);

    write!(
        writer,
        r##"<path d="M{x1},{y1}L{x2},{y2}L{x3},{y3}H{hback}z" fill="{color}" stroke="none"/>"##,
        x1 = -scale,
        y1 = scale,
        x2 = 0,
        y2 = -scale,
        x3 = scale,
        y3 = scale,
        hback = -scale * 2,
    )
}

fn negedge_arrow(writer: &mut impl io::Write, wave_height: u32, color: Color) -> io::Result<()> {
    let scale = i64::from(wave_height / 6);

    write!(
        writer,
        r##"<path d="M{x1},{y1}L{x2},{y2}L{x3},{y3}H{hback}z" fill="{color}" stroke="none"/>"##,
        x1 = -scale,
        y1 = -scale,
        x2 = 0,
        y2 = scale,
        x3 = scale,
        y3 = -scale,
        hback = -scale * 2,
    )
}

impl<'a> AssembledFigure<'a> {
    /// Render a [`AssembledFigure`] into a `writer`.
    #[inline]
    pub fn write_svg(&self, writer: &mut impl io::Write) -> io::Result<()> {
        self.write_svg_with_options(writer, &RenderOptions::default())
    }

    /// Render a [`AssembledFigure`] into a `writer` with a set of options.
    pub fn write_svg_with_options(
        &self,
        writer: &mut impl io::Write,
        options: &RenderOptions,
    ) -> io::Result<()> {
        let RenderOptions {
            background,
            padding,
            spacing,
            signal,
            group_indicator,
            header,
            footer,
            edge,
        } = options;

        let PathAssembleOptions {
            signal_height,
            cycle_width,
            transition_offset: _,
        } = self.path_assemble_options;

        let signal_height = u32::from(signal_height);
        let cycle_width = u32::from(cycle_width);

        let font = Font::default();
        let font_family = font
            .get_font_family_name()
            .unwrap_or_else(|| "helvetica".to_string());

        let dims = SvgDimensions::new(self, font, options, self.path_assemble_options);

        write!(
            writer,
            r#"<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewport="0 0 {figure_width} {figure_height}" overflow="hidden" width="{figure_width}" height="{figure_height}">"#,
            figure_width = dims.figure_width(),
            figure_height = dims.figure_height(),
        )?;

        // Definitions
        write!(writer, "<defs>")?;
        if self.definitions.has_undefined {
            write!(
                writer,
                r##"<pattern id="x-bg" patternUnits="userSpaceOnUse" width="4" height="10" patternTransform="rotate(45)">"##,
            )?;

            if let Some(background) = signal.undefined_background {
                write!(
                    writer,
                    r##"<rect x="0" y="0" width="4" height="10" fill="{background}"/>"##
                )?;
            }

            write!(
                writer,
                r##"<line x1="0" y="0" x2="0" y2="10" stroke="{color}" stroke-width="1"/></pattern>"##,
                color = signal.undefined_color
            )?;
        }

        if self.definitions.has_posedge_marker {
            write!(writer, r##"<g id="pei">"##)?;
            posedge_arrow(writer, signal_height, signal.path_color)?;
            write!(writer, r##"</g>"##)?;
        }

        if self.definitions.has_negedge_marker {
            write!(writer, r##"<g id="nei">"##)?;
            negedge_arrow(writer, signal_height, signal.path_color)?;
            write!(writer, r##"</g>"##)?;
        }

        if self.definitions.has_gaps {
            write!(writer, r##"<g id="gap">"##)?;
            gap(
                writer,
                self.path_assemble_options.signal_height,
                signal.gap_color,
                signal.gap_background_color,
            )?;
            write!(writer, r##"</g>"##)?;
        }

        write!(
            writer,
            r##"<g id="cl"><path fill="none" d="M0,0v{schema_height}" stroke-width="1" stroke-dasharray="2" stroke="{color}"/></g>"##,
            color = signal.hint_line_color,
            schema_height = dims.schema_height(),
        )?;
        write!(writer, "</defs>")?;

        // Background
        if let Some(background) = background {
            write!(
                writer,
                r##"<rect width="100%" height="100%" fill="{background}"/>"##
            )?;
        }

        // Header Text
        if let Some(title) = self.header_text {
            let title_font_size = header.font_size;
            let title_color = header.color;

            write!(
                writer,
                r##"<text x="{x}" y="{y}" text-anchor="middle" dominant-baseline="middle" font-family="{font_family}" font-size="{title_font_size}" fill="{title_color}" letter-spacing="0"><tspan>{text}</tspan></text>"##,
                x = dims.header_x() + dims.header_width() / 2,
                y = dims.header_y() + dims.header_height() / 2,
                text = escape_str(title),
            )?;
        }

        // Top Cycle Enumeration Markers
        if let Some(cycle_marker) = self.top_cycle_marker {
            let start = cycle_marker.start();
            let every = cycle_marker.every();

            let marker_font_size = header.cycle_marker_fontsize;
            let marker_color = header.cycle_marker_color;
            let end = start + self.num_cycles;

            if every != 0 {
                write!(writer, "<g>")?;
                for offset in (start..end).step_by(every as usize) {
                    write!(
                        writer,
                        r##"<text x="{x}" y="{y}" text-anchor="middle" dominant-baseline="middle" font-family="{font_family}" font-size="{marker_font_size}" fill="{marker_color}" letter-spacing="0"><tspan>{offset}</tspan></text>"##,
                        x = dims.schema_x()
                            + dims.cycle_width() * (offset - start)
                            + dims.cycle_width() / 2,
                        y = dims.header_y() + dims.header_height(),
                    )?;
                }
                write!(writer, "</g>")?;
            }
        }

        // Cycle Hint Lines
        write!(writer, "<g>")?;
        for i in 0..=self.num_cycles {
            write!(
                writer,
                r##"<use transform="translate({x},{y})" xlink:href="#cl"/>"##,
                x = dims.schema_x() + i * dims.cycle_width(),
                y = dims.schema_y(),
            )?;
        }
        write!(writer, "</g>")?;

        // Group Indicators
        if !self.group_markers.is_empty() {
            let label_font_size = group_indicator.label_fontsize;
            let label_color = group_indicator.label_color;

            write!(writer, "<g>")?;
            for group in self.group_markers.iter() {
                if group.is_empty() {
                    continue;
                }

                let depth = group.depth();
                let num_labels_below = self.amount_labels_below(depth);

                let height = group.len() * signal_height + (group.len() - 1) * spacing.line_to_line;
                let x = dims.grouping_x()
                    + if num_labels_below == 0 {
                        0
                    } else {
                        num_labels_below * group_indicator.label_height()
                            - group_indicator.label_spacing
                    }
                    + if depth == 0 {
                        0
                    } else {
                        depth * group_indicator.width + (depth - 1) * group_indicator.spacing
                    };
                let y = dims.schema_y()
                    + padding.schema_top
                    + if group.start() == 0 {
                        0
                    } else {
                        group.start() * signal_height + group.start() * spacing.line_to_line
                    };

                if let Some(label) = group.label() {
                    let x = x - group_indicator.label_fontsize / 2;

                    write!(
                        writer,
                        r##"<g transform="translate({x},{y})"><text text-anchor="middle" dominant-baseline="middle" font-family="{font_family}" font-size="{label_font_size}" fill="{label_color}" letter-spacing="0" transform="rotate(270)"><tspan>{text}</tspan></text></g>"##,
                        y = y + height / 2,
                        text = escape_str(label),
                    )?;
                }

                write!(
                    writer,
                    r##"<path fill="none" d="M{x},{y}m{w},0c-3,0 -{w},1 -{w},{w}v{h}c0,3 1,{w} {w},{w}" stroke="{color}"/>"##,
                    color = group_indicator.color,
                    h = height - group_indicator.width * 2,
                    w = group_indicator.width,
                )?;
            }
            write!(writer, "</g>")?;
        }

        // Signal Lines
        write!(writer, "<g>")?;
        for (i, line) in self.lines.iter().enumerate() {
            if line.is_empty() {}

            let Ok(i) = u32::try_from(i) else {
                break;
            };

            let x = if dims.has_textbox() {
                dims.textbox_x()
            } else {
                dims.schema_x()
            };
            let y = dims.signal_top(i);

            write!(writer, r##"<g transform="translate({x},{y})">"##)?;

            if !line.text.is_empty() {
                let name_font_size = signal.name_font_size;
                let name_color = signal.name_color;

                write!(
                    writer,
                    r##"<g transform="translate(0,{y})"><text dominant-baseline="middle" font-family="{font_family}" font-size="{name_font_size}" fill="{name_color}" letter-spacing="0"><tspan>{text}</tspan></text></g>"##,
                    y = signal_height / 2,
                    text = escape_str(line.text),
                )?;
            }

            if dims.has_textbox() {
                write!(
                    writer,
                    r##"<g transform="translate({schema_x})">"##,
                    schema_x = dims.schema_x() - dims.textbox_x()
                )?;
                write_signal(&line.path, writer, signal, self.hscale)?;
                write!(writer, r##"</g>"##)?;
            } else {
                write_signal(&line.path, writer, signal, self.hscale)?;
            }

            write!(writer, r##"</g>"##)?;
        }
        write!(writer, "</g>")?;

        // Footer Text
        if let Some(footer_text) = self.footer_text {
            let footer_font_size = footer.font_size;
            let footer_color = footer.color;

            write!(
                writer,
                r##"<text x="{x}" y="{y}" text-anchor="middle" dominant-baseline="middle" font-family="{font_family}" font-size="{footer_font_size}" fill="{footer_color}" letter-spacing="0"><tspan>{text}</tspan></text>"##,
                x = dims.footer_width() / 2,
                y = dims.footer_y() + dims.footer_height() / 2,
                text = escape_str(footer_text),
            )?;
        }

        // Bottom Cycle Enumeration Markers
        if let Some(cycle_marker) = self.bottom_cycle_marker {
            let start = cycle_marker.start();
            let every = cycle_marker.every();

            let marker_font_size = footer.cycle_marker_fontsize;
            let marker_color = footer.cycle_marker_color;

            let end = start + self.num_cycles;

            if every != 0 {
                write!(writer, "<g>")?;
                for offset in (start..end).step_by(every as usize) {
                    write!(
                        writer,
                        r##"<text x="{x}" y="{y}" text-anchor="middle" dominant-baseline="middle" font-family="{font_family}" font-size="{marker_font_size}" fill="{marker_color}" letter-spacing="0"><tspan>{offset}</tspan></text>"##,
                        x = dims.schema_x()
                            + dims.cycle_width() * (offset - start)
                            + dims.cycle_width() / 2,
                        y = dims.footer_y()
                    )?;
                }
                write!(writer, "</g>")?;
            }
        }

        // Edge markers
        if !self.line_edge_markers.lines().is_empty() {
            let mut middles = Vec::with_capacity(self.line_edge_markers.lines().len());

            write!(writer, "<g>")?;
            for line_edge in self.line_edge_markers.lines() {
                middles.push(write_line_edge(
                    writer,
                    line_edge.clone(),
                    &dims,
                    self.path_assemble_options,
                    options,
                    &font,
                )?);
            }

            for (line_edge, middle) in self
                .line_edge_markers
                .lines()
                .iter()
                .zip(middles.into_iter())
            {
                write_line_edge_markers(
                    writer,
                    line_edge.clone(),
                    middle,
                    &dims,
                    self.path_assemble_options,
                    options,
                    &font,
                )?;
            }
            write!(writer, "</g>")?;
        }

        // Edge separate text markers
        if !self.line_edge_markers.text_nodes().is_empty() {
            write!(writer, "<g>")?;
            for text_node in self.line_edge_markers.text_nodes() {
                let text = text_node.text().to_string();
                let x = dims.schema_x() + text_node.at().x().width_offset(cycle_width);
                let y = dims.signal_top(text_node.at().y()) + signal_height / 2;

                write_edge_text(
                    writer,
                    (x.into(), y.into()),
                    &text,
                    edge.node_font_size,
                    edge.node_text_color,
                    edge.node_background_color,
                    &font,
                )?;
            }
            write!(writer, "</g>")?;
        }

        write!(writer, "</svg>")?;

        Ok(())
    }
}

fn draw_dashed_horizontal_line(writer: &mut impl io::Write, dx: i32) -> io::Result<()> {
    let mut cx = 0i32;

    loop {
        if cx.abs() == dx.abs() {
            break;
        }

        write!(writer, "h{signed_len}", signed_len = dx.signum() * 4)?;
        cx = i32::min(dx.abs(), cx + 4);

        if cx.abs() >= dx.abs() {
            break;
        }

        write!(writer, "m{signed_len},0", signed_len = dx.signum() * 4)?;
        cx = i32::min(dx.abs(), cx + 4);
    }

    Ok(())
}

fn write_signal(
    wave_path: &AssembledSignalPath,
    writer: &mut impl io::Write,
    options: &SignalOptions,
    hscale: u16,
) -> io::Result<()> {
    let PathAssembleOptions {
        signal_height,
        cycle_width,
        transition_offset: _,
    } = wave_path.options();

    let signal_height = u32::from(*signal_height);
    let cycle_width = u32::from(*cycle_width);

    for segment in wave_path.segments() {
        let x = segment.x();
        let y = segment.y();

        write!(writer, r##"<path fill=""##)?;
        match segment.background() {
            Some(PathSegmentBackground::B2) => write!(writer, "{}", options.backgrounds[0])?,
            Some(PathSegmentBackground::B3) => write!(writer, "{}", options.backgrounds[1])?,
            Some(PathSegmentBackground::B4) => write!(writer, "{}", options.backgrounds[2])?,
            Some(PathSegmentBackground::B5) => write!(writer, "{}", options.backgrounds[3])?,
            Some(PathSegmentBackground::B6) => write!(writer, "{}", options.backgrounds[4])?,
            Some(PathSegmentBackground::B7) => write!(writer, "{}", options.backgrounds[5])?,
            Some(PathSegmentBackground::B8) => write!(writer, "{}", options.backgrounds[6])?,
            Some(PathSegmentBackground::B9) => write!(writer, "{}", options.backgrounds[7])?,
            Some(PathSegmentBackground::Undefined) => write!(writer, "url(#x-bg)")?,
            None => write!(writer, "none")?,
        }
        write!(writer, r#"" d=""#)?;
        write!(writer, "M{x},{y}")?;
        for action in segment.actions() {
            match action {
                PathCommand::LineVerticalNoStroke(dy) => write!(writer, "v{dy}"),
                PathCommand::LineVertical(dy) => write!(writer, "v{dy}"),
                PathCommand::LineHorizontal(dx) => write!(writer, "h{dx}"),
                PathCommand::DashedLineHorizontal(dx) => draw_dashed_horizontal_line(writer, *dx),
                PathCommand::Line(dx, dy) => write!(writer, "l{dx},{dy}"),
                PathCommand::Curve(cdx1, cdy1, cdx2, cdy2, dx, dy) => {
                    write!(writer, "c{cdx1},{cdy1} {cdx2},{cdy2} {dx},{dy}")
                }
            }?
        }

        if segment.background().is_some() {
            write!(writer, r##"z"##)?;
        }

        // If there is a `no_stroke` element, we need to divide up the filling and the
        // stroking.
        if !segment.is_fully_stroked() {
            write!(writer, r##"" stroke="none"/>"##)?;

            write!(writer, r##"<path fill="none" d=""##)?;
            write!(writer, "M{x},{y}")?;
            for action in segment.actions() {
                match action {
                    PathCommand::LineVerticalNoStroke(dy) => write!(writer, "m0,{dy}"),
                    PathCommand::LineVertical(dy) => write!(writer, "v{dy}"),
                    PathCommand::LineHorizontal(dx) => write!(writer, "h{dx}"),
                    PathCommand::DashedLineHorizontal(dx) => {
                        draw_dashed_horizontal_line(writer, *dx)
                    }
                    PathCommand::Line(dx, dy) => write!(writer, "l{dx},{dy}"),
                    PathCommand::Curve(cdx1, cdy1, cdx2, cdy2, dx, dy) => {
                        write!(writer, "c{cdx1},{cdy1} {cdx2},{cdy2} {dx},{dy}")
                    }
                }?
            }
        }
        write!(
            writer,
            r##"" stroke-width="1" stroke="{path_color}"/>"##,
            path_color = options.path_color
        )?;

        if let Some(marker_text) = segment.marker_text() {
            write!(
                writer,
                r##"<g transform="translate({x},{y})"><text text-anchor="middle" dominant-baseline="middle" font-family="{font_family}" font-size="{font_size}" fill="{color}" letter-spacing="0"><tspan>{text}</tspan></text></g>"##,
                font_family = Font::default()
                    .get_font_family_name()
                    .as_ref()
                    .map(|s| &s[..])
                    .unwrap_or("Helvetica"),
                font_size = options.marker_font_size,
                text = marker_text,
                color = options.marker_color,
                x = segment.x() + segment.width() / 2,
                y = signal_height / 2,
            )?;
        }

        for clock_edge_marker in segment.clock_edge_markers() {
            let x = clock_edge_marker
                .at()
                .width_offset(cycle_width * u32::from(hscale));
            let y = signal_height / 2;

            match clock_edge_marker.edge() {
                ClockEdge::Positive => {
                    write!(
                        writer,
                        r##"<use transform="translate({x},{y})" xlink:href="#pei"/>"##,
                    )?;
                }
                ClockEdge::Negative => {
                    write!(
                        writer,
                        r##"<use transform="translate({x},{y})" xlink:href="#nei"/>"##,
                    )?;
                }
            };
        }

        for gap in segment.gaps() {
            let x = gap.width_offset(cycle_width * u32::from(hscale));
            let y = signal_height / 2;

            write!(
                writer,
                r##"<use transform="translate({x},{y})" xlink:href="#gap"/>"##,
            )?;
        }
    }

    Ok(())
}