mod diagram;
mod formula;
mod glyphs;
use std::panic::{AssertUnwindSafe, catch_unwind};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Setting {
Block,
Line,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Palette {
pub ink: u32,
pub page: u32,
}
impl Default for Palette {
fn default() -> Self {
Self {
ink: 0x001b_1b1b,
page: 0x00ff_ffff,
}
}
}
impl Palette {
fn mix(self, part: f32) -> u32 {
let channel = |shift: u32| {
let page = f32::from(u8::try_from((self.page >> shift) & 0xff).unwrap_or(0));
let ink = f32::from(u8::try_from((self.ink >> shift) & 0xff).unwrap_or(0));
let mixed = (page + (ink - page) * part).round().clamp(0.0, 255.0);
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the value was clamped to the range of a byte"
)]
let byte = mixed as u32;
byte << shift
};
channel(16) | channel(8) | channel(0)
}
fn is_dark(self) -> bool {
let light = |colour: u32| (colour >> 16 & 0xff) + (colour >> 8 & 0xff) + (colour & 0xff);
light(self.page) < light(self.ink)
}
}
fn hex(colour: u32) -> String {
format!("#{:06x}", colour & 0x00ff_ffff)
}
#[derive(Clone, Debug, PartialEq)]
pub struct Figure {
svg: String,
width: f32,
height: f32,
baseline: f32,
em: f32,
}
impl Figure {
#[must_use]
pub fn svg(&self) -> &str {
&self.svg
}
#[must_use]
pub fn into_svg(self) -> String {
self.svg
}
#[must_use]
pub fn width(&self) -> f32 {
self.width
}
#[must_use]
pub fn height(&self) -> f32 {
self.height
}
#[must_use]
pub fn baseline(&self) -> f32 {
self.baseline
}
#[must_use]
pub fn em(&self) -> f32 {
self.em
}
}
#[cfg(test)]
impl Figure {
pub(crate) fn with_svg(mut self, svg: &str) -> Self {
svg.clone_into(&mut self.svg);
self
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum FigureError {
#[error("there is nothing to draw")]
Empty,
#[error("the diagram cannot be drawn: {0}")]
Diagram(String),
#[error("the formula cannot be set: {0}")]
Formula(String),
#[error("the renderer stopped unexpectedly")]
Renderer,
}
pub fn diagram(source: &str, palette: Palette) -> Result<Figure, FigureError> {
guarded(source, |source| diagram::draw(source, palette))
}
pub fn formula(source: &str, setting: Setting, palette: Palette) -> Result<Figure, FigureError> {
guarded(source, |source| formula::draw(source, setting, palette))
}
fn guarded(
source: &str,
draw: impl FnOnce(&str) -> Result<Figure, FigureError>,
) -> Result<Figure, FigureError> {
let source = source.trim();
if source.is_empty() {
return Err(FigureError::Empty);
}
catch_unwind(AssertUnwindSafe(|| draw(source))).unwrap_or(Err(FigureError::Renderer))
}
#[cfg(test)]
mod tests {
use super::*;
const NIGHT: Palette = Palette {
ink: 0x00b8_b8b8,
page: 0,
};
#[test]
fn a_colour_between_page_and_ink_is_mixed_by_channel() {
let palette = Palette {
ink: 0x0000_0000,
page: 0x00ff_8040,
};
assert_eq!(palette.mix(0.0), 0x00ff_8040);
assert_eq!(palette.mix(1.0), 0);
assert_eq!(palette.mix(0.5), 0x0080_4020);
}
#[test]
fn a_page_darker_than_its_text_is_dark() {
assert!(!Palette::default().is_dark());
assert!(NIGHT.is_dark());
}
#[test]
fn a_colour_is_written_as_six_hex_digits() {
assert_eq!(hex(0x0000_0a0b), "#000a0b");
}
#[test]
fn an_empty_source_draws_nothing() {
assert_eq!(diagram(" \n", Palette::default()), Err(FigureError::Empty));
assert_eq!(
formula("\t", Setting::Block, Palette::default()),
Err(FigureError::Empty)
);
}
#[test]
fn white_space_around_a_source_is_not_a_part_of_it() {
let palette = Palette::default();
assert_eq!(
formula(" a+b \n", Setting::Line, palette),
formula("a+b", Setting::Line, palette)
);
}
#[test]
fn a_panicking_renderer_is_reported_as_an_error() {
let drawn = guarded("x", |_| -> Result<Figure, FigureError> {
std::panic::panic_any("the renderer broke")
});
assert_eq!(drawn, Err(FigureError::Renderer));
}
#[test]
fn a_figure_gives_its_document_and_measures() {
let figure = Figure {
svg: "<svg/>".to_owned(),
width: 3.0,
height: 2.0,
baseline: 1.5,
em: 20.0,
};
assert_eq!(
(
figure.width(),
figure.height(),
figure.baseline(),
figure.em()
),
(3.0, 2.0, 1.5, 20.0)
);
assert_eq!(figure.svg(), "<svg/>");
assert_eq!(figure.into_svg(), "<svg/>");
}
#[test]
fn a_failure_says_what_the_renderer_said() {
assert_eq!(
FigureError::Diagram("no diagram type".into()).to_string(),
"the diagram cannot be drawn: no diagram type"
);
assert_eq!(
FigureError::Formula("missing brace".into()).to_string(),
"the formula cannot be set: missing brace"
);
assert_eq!(FigureError::Empty.to_string(), "there is nothing to draw");
assert_eq!(
FigureError::Renderer.to_string(),
"the renderer stopped unexpectedly"
);
}
}