use std::fmt::Write as _;
use std::sync::Arc;
use egui::{Color32, Pos2};
use egui::{Context, Rect, Vec2};
use super::axis::Transform;
use super::scene::{self, Dash, Measure, Prim, Scene};
use super::{Plot, PlotState, overlay};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExportPreset {
#[default]
Screen,
HalfPage,
Letter,
Journal,
}
impl ExportPreset {
pub const fn size(self) -> [f32; 2] {
match self {
Self::Screen => [960.0, 540.0],
Self::HalfPage => [468.0, 360.0],
Self::Letter => [612.0, 792.0],
Self::Journal => [504.0, 360.0],
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ExportOptions {
pub width: f32,
pub height: f32,
pub scale: f32,
pub transparent: bool,
}
impl ExportOptions {
pub fn for_preset(preset: ExportPreset) -> Self {
let [width, height] = preset.size();
Self {
width,
height,
..Self::default()
}
}
}
impl Default for ExportOptions {
fn default() -> Self {
Self {
width: 960.0,
height: 540.0,
scale: 2.0,
transparent: false,
}
}
}
const FONT_FAMILY: &str = "Ubuntu, 'Ubuntu Light', 'Segoe UI', 'Helvetica Neue', Arial, sans-serif";
fn color(color: Color32) -> (String, f32) {
let [r, g, b, a] = color.to_srgba_unmultiplied();
(format!("#{r:02x}{g:02x}{b:02x}"), f32::from(a) / 255.0)
}
fn paint_attr(name: &str, value: Color32) -> String {
let (hex, alpha) = color(value);
if alpha <= 0.0 {
return format!(" {name}=\"none\"");
}
if alpha >= 0.999 {
format!(" {name}=\"{hex}\"")
} else {
format!(" {name}=\"{hex}\" {name}-opacity=\"{alpha:.3}\"")
}
}
fn escape(text: &str) -> String {
text.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}
fn points(points: &[Pos2]) -> String {
let mut out = String::with_capacity(points.len() * 14);
for (index, point) in points.iter().enumerate() {
if index > 0 {
out.push(' ');
}
let _ = write!(out, "{:.2},{:.2}", point.x, point.y);
}
out
}
fn prim(out: &mut String, prim: &Prim) {
match prim {
Prim::Line {
points: line,
width,
color,
dash,
} => {
let dash = match dash {
Dash::Solid => String::new(),
Dash::Dashed(dash, gap) => format!(" stroke-dasharray=\"{dash:.2} {gap:.2}\""),
Dash::Dotted(spacing) => {
format!(" stroke-dasharray=\"0 {spacing:.2}\" stroke-linecap=\"round\"")
}
Dash::Marching { dash, gap, offset } => {
format!(" stroke-dasharray=\"{dash:.2} {gap:.2}\" stroke-dashoffset=\"{offset:.2}\"")
}
};
let _ = writeln!(
out,
"<polyline points=\"{}\" fill=\"none\"{} stroke-width=\"{width:.2}\" stroke-linejoin=\"round\"{dash}/>",
points(line),
paint_attr("stroke", *color),
);
}
Prim::Area {
points: line,
base_y,
color,
} => {
if let (Some(first), Some(last)) = (line.first(), line.last()) {
let mut polygon = line.clone();
polygon.push(Pos2::new(last.x, *base_y));
polygon.push(Pos2::new(first.x, *base_y));
let _ = writeln!(
out,
"<polygon points=\"{}\"{}/>",
points(&polygon),
paint_attr("fill", *color)
);
}
}
Prim::Polygon { points: shape, color } => {
let _ = writeln!(
out,
"<polygon points=\"{}\"{}/>",
points(shape),
paint_attr("fill", *color)
);
}
Prim::Quad { points: shape, color } => {
let _ = writeln!(
out,
"<polygon points=\"{}\"{}/>",
points(shape),
paint_attr("fill", *color)
);
}
Prim::Fan {
center,
points: shape,
color,
} => {
let mut polygon = Vec::with_capacity(shape.len() + 1);
polygon.push(*center);
polygon.extend_from_slice(shape);
let _ = writeln!(
out,
"<polygon points=\"{}\"{}/>",
points(&polygon),
paint_attr("fill", *color)
);
}
Prim::Rect {
rect,
radius,
fill,
stroke,
} => {
let inset = stroke.map_or(0.0, |(width, _)| width * 0.5);
let rect = rect.shrink(inset);
let stroke = stroke.map_or_else(String::new, |(width, color)| {
format!("{} stroke-width=\"{width:.2}\"", paint_attr("stroke", color))
});
let _ = writeln!(
out,
"<rect x=\"{:.2}\" y=\"{:.2}\" width=\"{:.2}\" height=\"{:.2}\" rx=\"{radius:.2}\"{}{stroke}/>",
rect.left(),
rect.top(),
rect.width().max(0.0),
rect.height().max(0.0),
paint_attr("fill", *fill),
);
}
Prim::Circle {
center,
radius,
fill,
stroke,
} => {
let stroke = stroke.map_or_else(String::new, |(width, color)| {
format!("{} stroke-width=\"{width:.2}\"", paint_attr("stroke", color))
});
let _ = writeln!(
out,
"<circle cx=\"{:.2}\" cy=\"{:.2}\" r=\"{radius:.2}\"{}{stroke}/>",
center.x,
center.y,
paint_attr("fill", *fill),
);
}
Prim::Text {
pos,
text,
size,
color,
baseline,
angle,
} => {
let transform = if *angle == 0.0 {
String::new()
} else {
format!(
" transform=\"rotate({:.3} {:.2} {:.2})\"",
angle.to_degrees(),
pos.x,
pos.y
)
};
let _ = writeln!(
out,
"<text xml:space=\"preserve\" x=\"{:.2}\" y=\"{:.2}\" font-size=\"{size:.2}\"{}{transform}>{}</text>",
pos.x,
pos.y + baseline,
paint_attr("fill", *color),
escape(text),
);
}
}
}
pub(super) fn svg(scene: &Scene, canvas: Option<Color32>) -> String {
let (width, height) = (scene.size.x, scene.size.y);
let mut out = String::with_capacity(64 * 1024);
let _ = writeln!(
out,
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width:.0}\" height=\"{height:.0}\" \
viewBox=\"0 0 {width:.2} {height:.2}\" font-family=\"{FONT_FAMILY}\" font-weight=\"300\">"
);
let frame = scene.frame;
let _ = writeln!(
out,
"<defs><clipPath id=\"plot-frame\"><rect x=\"{:.2}\" y=\"{:.2}\" width=\"{:.2}\" height=\"{:.2}\"/></clipPath></defs>",
frame.left(),
frame.top(),
frame.width(),
frame.height()
);
if let Some(canvas) = canvas {
let _ = writeln!(
out,
"<rect width=\"100%\" height=\"100%\"{}/>",
paint_attr("fill", canvas)
);
}
for primitive in &scene.under {
prim(&mut out, primitive);
}
out.push_str("<g clip-path=\"url(#plot-frame)\">\n");
for primitive in &scene.data {
prim(&mut out, primitive);
}
out.push_str("</g>\n");
for primitive in &scene.over {
prim(&mut out, primitive);
}
out.push_str("</svg>\n");
out
}
const MAX_PIXELS: u64 = 100_000_000;
pub(super) fn png(svg: &str, scale: f32) -> Result<Vec<u8>, String> {
let mut options = usvg::Options::default();
{
let database = Arc::make_mut(&mut options.fontdb);
for data in egui::FontDefinitions::default().font_data.values() {
database.load_font_data((*data.font).as_ref().to_vec());
}
}
options.font_family = "Ubuntu".to_owned();
let tree = usvg::Tree::from_str(svg, &options).map_err(|error| format!("invalid SVG: {error}"))?;
let size = tree.size();
let scale = scale.clamp(0.25, 8.0);
let width = (size.width() * scale).round().max(1.0) as u32;
let height = (size.height() * scale).round().max(1.0) as u32;
if u64::from(width) * u64::from(height) > MAX_PIXELS {
return Err("the image is too large".to_owned());
}
let mut pixmap = resvg::tiny_skia::Pixmap::new(width, height).ok_or("the image is too large")?;
resvg::render(
&tree,
resvg::tiny_skia::Transform::from_scale(scale, scale),
&mut pixmap.as_mut(),
);
pixmap
.encode_png()
.map_err(|error| format!("PNG encoding failed: {error}"))
}
impl Plot<'_> {
fn export_scene(
&self,
ctx: &Context,
state: &PlotState,
options: &ExportOptions,
) -> (scene::Scene, Option<Color32>) {
let measure = Measure {
ctx: ctx.clone(),
pixels_per_point: 1.0,
};
let rect = Rect::from_min_size(
Pos2::ZERO,
Vec2::new(options.width.max(120.0), options.height.max(120.0)),
);
let view = state.view.unwrap_or_else(|| self.auto_bounds(state));
let y2 = self.second_axis(state);
let mode = self.chart_mode();
let layout = overlay::layout(&measure, &self.config, rect, view, y2, mode);
let transform = Transform {
frame: layout.frame,
view,
x_scale: self.config.x.scale,
y_scale: self.config.y.scale,
y2,
};
let visuals = ctx.style_of(ctx.theme()).visuals.clone();
let mut colors = overlay::Colors::resolve(&self.config, &visuals);
let canvas = if options.transparent {
colors.canvas.take()
} else {
Some(colors.canvas.take().unwrap_or(visuals.panel_fill))
};
let theme = crisol_ui_kit::theme::current(ctx);
let mut scene = overlay::build_scene(
&measure, self, &layout, &transform, state, &colors, &theme, true, mode, 0.0, false, false,
);
super::annotations::paint(&mut scene.data, &transform, &state.annotations, &theme);
(scene, canvas)
}
pub fn to_svg(&self, ctx: &Context, state: &PlotState, options: &ExportOptions) -> String {
let (scene, canvas) = self.export_scene(ctx, state, options);
svg(&scene, canvas)
}
pub fn to_pdf(&self, ctx: &Context, state: &PlotState, options: &ExportOptions) -> Vec<u8> {
let (scene, _) = self.export_scene(ctx, state, options);
super::pdf::write(&scene, options)
}
pub fn to_png(&self, ctx: &Context, state: &PlotState, options: &ExportOptions) -> Result<Vec<u8>, String> {
png(&self.to_svg(ctx, state, options), options.scale)
}
}