use std::fmt::Write as _;
use pltrs_core::{Bbox, Point};
use pltrs_text::tex::{TexError, TexText};
use super::dsc::bbox_header;
use super::{PS_DEFS, PageOrientation, PsRenderer, draw};
use crate::gc::GraphicsContext;
use crate::vector::{VectorError, fixed, trimmed};
#[derive(Clone, Debug, PartialEq)]
pub struct PsfragFigure {
pub figure: String,
pub psfrags: Vec<String>,
pub width: f64,
pub height: f64,
pub bbox: Bbox,
}
fn family_command(family: Option<&str>) -> &'static str {
match family {
Some("sans-serif") => r"\sffamily",
Some("monospace") => r"\ttfamily",
_ => r"\rmfamily",
}
}
impl PsRenderer {
pub(super) fn psfrag_mark(
&mut self,
gc: &GraphicsContext,
tex: &TexText,
origin: Point,
angle: f64,
) -> Result<(), TexError> {
let (_, _, descent) = tex
.manager
.text_width_height_descent(&tex.tex, tex.size(), 1.0)?;
let tag = format!("psmarker{}", self.psfrags.len());
let rgb = gc.stroke_color();
let color = [rgb.r, rgb.g, rgb.b].map(|v| trimmed(v, 3)).join(",");
let family = family_command(tex.manager.fonts().family.first().map(String::as_str));
let size = tex.size();
self.psfrags.push(format!(
r"\psfrag{{{tag}}}[bl][bl][1][{}]{{\fontsize{{{}}}{{{}}}\color[rgb]{{{color}}} {{{family} {}}}}}",
fixed(angle, 6),
fixed(size, 6),
fixed(size * 1.25, 6),
tex.tex
));
let up = (angle + 90.0).to_radians();
let position = draw::nums(&[origin.x - descent * up.cos(), origin.y - descent * up.sin()]);
let _ = write!(
self.body,
"gsave\n{position} moveto\n({tag})\nshow\ngrestore\n"
);
Ok(())
}
pub(super) fn psfrag_figure(self) -> Result<PsfragFigure, VectorError> {
let (width, height) = (self.width, self.height);
let page = Bbox::from_extents(0.0, 0.0, width * 72.0, height * 72.0);
let mut figure = format!(
"%!PS-Adobe-3.0 EPSF-3.0\n%%LanguageLevel: 3\n{}",
self.dsc_comments()
);
let _ = write!(
figure,
"{}\n%%EndComments\n%%BeginProlog\n/mpldict {} dict def\nmpldict begin\n{}\nend\n\
%%EndProlog\nmpldict begin\n0 0 translate\n0 0 {} rectclip\n{}\nend\nshowpage\n",
bbox_header(&page),
PS_DEFS.len(),
PS_DEFS.concat(),
draw::nums(&[width * 72.0, height * 72.0]),
self.body
);
let bbox = match self.options.orientation {
PageOrientation::Portrait => page,
PageOrientation::Landscape => Bbox::from_extents(0.0, 0.0, page.y1(), page.x1()),
};
self.problems.into_result(PsfragFigure {
figure,
psfrags: self.psfrags,
width,
height,
bbox,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_first_family_picks_the_command() {
assert_eq!(family_command(Some("sans-serif")), r"\sffamily");
assert_eq!(family_command(Some("monospace")), r"\ttfamily");
assert_eq!(family_command(Some("Sans-Serif")), r"\rmfamily");
assert_eq!(family_command(None), r"\rmfamily");
}
}