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
//! WaveDrom allows for the programmatic creation of beautiful [Diagram Timing Diagrams][dtd] in
//! Rust. This is the crate that powers all the wavedrom tools including the [editor], the
//! [command-line interface][cli], and the [mdbook preprocessor][mdbook-wavedrom].
//!
//! This crate is be used in two ways. It can be given [WaveJson][wavejson] which is a JSON format
//! to describe [Diagram Timing Diagrams][dtd]. Alternatively, you can programmatically define a
//! figure by building it using the [`Figure`] struct.
//!
//! # Getting Started
//!
//! Getting started with this crate is quite easy. Here, we have two examples. First, how to use
//! [WaveJson][wavejson] as an input to your figures and second how to programmically define
//! figures.
//!
//! ## WaveJson
//!
#![cfg_attr(feature = "json5", doc = r####"
```
use std::fs::File;
let path = "path/to/file.svg";
# let path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/doc-root-wavejson.svg");
let mut file = File::create(path)?;
wavedrom::render_json5(r##"
{ signal: [
{ name: "clk", wave: "P......" },
{ name: "bus", wave: "x.==.=x", data: ["head", "body", "tail", "data"] },
{ name: "wire", wave: "0.1..0." }
]}
"##, &mut file)?;
# <Result<(), wavedrom::RenderJson5Error>>::Ok(())
```"####)]
//!
//! **Result:**
//!
#![doc=include_str!("../assets/doc-root-wavejson.svg")]
//!
//! ## Programmically defining a Figure
//!
//! ```
//! use std::fs::File;
//! use wavedrom::{Figure, Signal};
//!
//! let figure = Figure::new()
//! .header_text("Timing Schema")
//! .add_signals([
//! Signal::with_cycle_str("p........").name("clk"),
//! Signal::with_cycle_str("010......").name("req"),
//! Signal::with_cycle_str("0......10").name("done"),
//! Signal::with_cycle_str("0......10").name("done"),
//! Signal::with_cycle_str("==.=.=.=.").name("state")
//! .add_data_fields([
//! "Idle", "Fetch", "Calculate", "Return", "Idle",
//! ]),
//! ]);
//! let assembled_figure = figure.assemble();
//!
//! let path = "path/to/file.svg";
//! # let path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/doc-root-programatically.svg");
//! let mut file = File::create(path)?;
//!
//! assembled_figure.write_svg(&mut file)?;
//! # <Result<(), std::io::Error>>::Ok(())
//! ```
//!
//! **Result:**
//!
#![doc=include_str!("../assets/doc-root-programatically.svg")]
//!
//! # Cargo Features
//!
//! There are a set of cargo features, most of which are enabled by default.
//!
//! * `serde`. Enabled by default. Adds the [`wavejson`] module, which defines the serialize and
//! deserialize formats for a wave format for a wave.
//! * `embed_font`. Enabled by default. Adds an embedded [Helvetica][helvetica] into the library
//! which is used to find the dimensions of certain texts. When this is disabled, it is replaced by
//! a width look-up table that is only accurate for ASCII and over-estimates the width for other
//! UTF-8 characters.
//! * `json5`. Enabled by default. The human friendly variant of JSON that can be used with the
//! `serde` feature to deserialize a WaveJson file.
//! * `serde_json`. Disabled by default. Formal version of JSON that can be used with the `serde`
//! feature to deserialize a WaveJson file.
//! * `skins`. Enabled by default. Adds the [`skin`] module, which defines the serialize and
//! deserialize formats for WaveDrom skins. Also adds logic to merge a skin into an existing set of
//! options.
//!
//! # Rendering Process
//!
//! The rendering process of this crate is done in 3 steps.
//!
//! **1. Create [`Figure`]**
//!
//! A [`Figure`] can be created in two ways. First, a [`Figure`] can be built programmatically with
//! the [`Figure::new`] method and the builder pattern methods. Second, a [`Figure`] can be built
//! by loading a [WaveJson][wavejson] file. This can be done with the [`Figure::from_json5`] or
//! [`Figure::from_json`] methods.
//!
//! **2. Assemble [`Figure`] to [`AssembledFigure`]**
//!
//! A [`Figure`] needs to be assembled. This shapes the signal waves removes any invalid groups and
//! edges. Assembling is done with the [`Figure::assemble`] and [`Figure::assemble_with_options`]
//! methods.
//!
//! **3. Render [`AssembledFigure`] to SVG**
//!
//! An [`AssembledFigure`] can be rendered by calling the [`AssembledFigure::write_svg`] or
//! [`AssembledFigure::write_svg_with_options`] methods. This will write an SVG into an
//! [`io::Write`][std::io::Write] buffer. If a write to the [`io::Write`][std::io::Write] is
//! expensive, it is recommended to wrap the [`io::Write`][std::io::Write] in a
//! [`std::io::BufWriter`].
//!
//! [helvetica]: https://en.wikipedia.org/wiki/Helvetica
//! [dtd]: https://en.wikipedia.org/wiki/Digital_timing_diagram
//! [editor]: https://gburghoorn.com/wavedrom
//! [cli]: https://github.com/coastalwhite/wavedrom-rs/tree/main/wavedrom-cli
//! [mdbook-wavedrom]: https://github.com/coastalwhite/wavedrom-rs/tree/main/mdbook-wavedrom
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(all(feature = "serde_json", feature = "json5", feature = "serde", feature = "skins"), deny(rustdoc::broken_intra_doc_links))]
#![deny(missing_docs)]
#[cfg(feature = "json5")]
pub use json5;
#[cfg(feature = "serde_json")]
pub use serde_json;
#[cfg(feature = "skins")]
pub mod skin;
mod color;
mod cycle_offset;
pub mod edges;
mod figure;
mod path;
mod shortcuts;
mod signal;
mod svg;
pub use figure::Figure;
pub use color::Color;
pub use cycle_offset::{CycleOffset, InCycleOffset};
pub use shortcuts::*;
pub use signal::Signal;
pub use svg::*;
pub use path::*;
pub mod markers;
#[cfg(feature = "serde")]
pub mod wavejson;
use edges::{
EdgeArrowType, EdgeVariant, LineEdgeMarkers, SharpEdgeVariant,
SplineEdgeVariant,
};
use markers::{ClockEdge, CycleEnumerationMarker, GroupMarker};
/// A section of the figure's signals
#[derive(Debug, Clone)]
pub enum FigureSection {
/// A [`Signal`]
Signal(Signal),
/// A group of [`Signal`]s
Group(FigureSectionGroup),
}
/// A section of the figure's group
#[derive(Debug, Clone)]
pub struct FigureSectionGroup(Option<String>, Vec<FigureSection>);
/// A line of the [`AssembledFigure`].
///
/// This contains the shaped signal path, the group nesting depth and the name of the signal line.
#[derive(Debug, Clone)]
pub struct AssembledLine<'a> {
text: &'a str,
path: AssembledSignalPath,
}
impl From<Signal> for FigureSection {
fn from(wave: Signal) -> Self {
Self::Signal(wave)
}
}
#[derive(Default, Debug)]
struct DefinitionTracker {
has_undefined: bool,
has_gaps: bool,
has_posedge_marker: bool,
has_negedge_marker: bool,
}
/// A [`Figure`] that has been assembled with the [`Figure::assemble`] or
/// [`Figure::assemble_with_options`] methods.
///
/// An assembled figure contains all the information necessary to perform rendering.
#[derive(Debug)]
pub struct AssembledFigure<'a> {
num_cycles: u32,
hscale: u16,
definitions: DefinitionTracker,
group_label_at_depth: Vec<bool>,
max_group_depth: u32,
header_text: Option<&'a str>,
footer_text: Option<&'a str>,
top_cycle_marker: Option<CycleEnumerationMarker>,
bottom_cycle_marker: Option<CycleEnumerationMarker>,
path_assemble_options: PathAssembleOptions,
lines: Vec<AssembledLine<'a>>,
group_markers: Vec<GroupMarker<'a>>,
line_edge_markers: LineEdgeMarkers<'a>,
}
impl<'a> AssembledFigure<'a> {
#[inline]
fn amount_labels_below(&self, depth: u32) -> u32 {
self.group_label_at_depth
.iter()
.take(depth as usize)
.filter(|x| **x)
.count() as u32
}
/// Returns the maximum cycle width over all lines.
#[inline]
pub fn num_cycles(&self) -> u32 {
self.num_cycles
}
/// Returns the scaling factor for the horizontal axis.
#[inline]
pub fn horizontal_scale(&self) -> u16 {
self.hscale
}
/// Returns whether the [`AssembledFigure`] contains any [`CycleState::X`]
#[inline]
pub fn has_undefined(&self) -> bool {
self.definitions.has_undefined
}
/// Returns whether the [`AssembledFigure`] contains any [`CycleState::Gap`]
#[inline]
pub fn has_gaps(&self) -> bool {
self.definitions.has_gaps
}
/// Returns whether the [`AssembledFigure`] contains any [`CycleState::PosedgeClockMarked`]
#[inline]
pub fn has_posedge_marker(&self) -> bool {
self.definitions.has_posedge_marker
}
/// Returns whether the [`AssembledFigure`] contains any [`CycleState::NegedgeClockMarked`]
#[inline]
pub fn has_negedge_marker(&self) -> bool {
self.definitions.has_negedge_marker
}
/// Returns the whether there is a label at group nesting level `depth`.
#[inline]
pub fn has_group_label_at_depth(&self, depth: u32) -> bool {
let Ok(depth) = usize::try_from(depth) else {
return false;
};
self.group_label_at_depth
.get(depth)
.cloned()
.unwrap_or(false)
}
/// Returns the maximum depth of the group nesting.
#[inline]
pub fn group_nesting(&self) -> u32 {
self.max_group_depth
}
/// Returns the lines that the [`AssembledFigure`] contains
#[inline]
pub fn lines(&self) -> &[AssembledLine<'a>] {
&self.lines
}
/// Returns the markers for the group nestings
#[inline]
pub fn group_markers(&self) -> &[GroupMarker<'a>] {
&self.group_markers
}
/// Returns a potential header text of the [`AssembledFigure`]
#[inline]
pub fn header_text(&self) -> Option<&'a str> {
self.header_text
}
/// Returns a potential footer text of the [`AssembledFigure`]
#[inline]
pub fn footer_text(&self) -> Option<&'a str> {
self.footer_text
}
/// Returns a [`CycleEnumerationMarker`] above the signals of the [`AssembledFigure`]
#[inline]
pub fn top_cycle_marker(&self) -> Option<CycleEnumerationMarker> {
self.top_cycle_marker
}
/// Returns a [`CycleEnumerationMarker`] below the signals of the [`AssembledFigure`]
#[inline]
pub fn bottom_cycle_marker(&self) -> Option<CycleEnumerationMarker> {
self.bottom_cycle_marker
}
}
impl AssembledLine<'_> {
fn is_empty(&self) -> bool {
self.path.is_empty() && self.text.is_empty()
}
}