use std::collections::BTreeMap;
use std::ops::RangeInclusive;
use std::sync::Arc;
use pltrs_core::{Affine2D, Path, PathCode, Point};
use pltrs_font::{GlyphId, PsNumber, PsValue, Type1Encoding, Type1Font};
use pltrs_text::tex::{DviBox, DviFont, DviPage, PsFont, TexError, TexFile, TexFiles, TexText};
use super::PdfRenderer;
use super::fonts::subset_prefix;
use super::object::Obj;
use super::pages::Link;
use super::writer::PdfWriter;
use crate::gc::GraphicsContext;
use crate::renderer::text_placement;
use crate::vector::{POINTS_DPI, VectorError};
#[derive(Debug)]
struct UsedFont {
font: Arc<DviFont>,
glyphs: BTreeMap<i64, GlyphId>,
}
#[derive(Debug, Default)]
pub(super) struct DviFonts {
fonts: BTreeMap<String, UsedFont>,
files: Option<&'static TexFiles>,
}
#[derive(Clone, Debug, PartialEq)]
enum Item {
Font(String, f64),
Text {
x: f64,
y: f64,
shown: Vec<Shown>,
end: f64,
},
}
#[derive(Clone, Debug, PartialEq)]
enum Shown {
Codes(Vec<u8>),
Kern(f64),
}
#[expect(
clippy::float_cmp,
reason = "matplotlib joins strings on exactly one baseline"
)]
fn join_strings(mut seq: Vec<Item>) -> Vec<Item> {
let mut i = 0;
let mut fontsize = 0.0;
while i + 1 < seq.len() {
match (&seq[i], &seq[i + 1]) {
(Item::Font(_, size), _) => fontsize = *size,
(
Item::Text { y, end, .. },
Item::Text {
x: x2,
y: y2,
shown: next,
end: end2,
},
) if y == y2 => {
let offset = end - x2;
let (next, x2, end2) = (next.clone(), *x2, *end2);
if let Item::Text { shown, end, .. } = &mut seq[i] {
if offset.abs() < 0.1 {
if let (Some(Shown::Codes(last)), Some(Shown::Codes(first))) =
(shown.last_mut(), next.first())
{
last.extend_from_slice(first);
}
*end += end2 - x2;
} else {
shown.push(Shown::Kern(offset * 1000.0 / fontsize));
shown.extend(next.into_iter().take(1));
*end = end2;
}
}
seq.remove(i + 1);
continue;
}
_ => {}
}
i += 1;
}
seq
}
fn type1_error(err: impl std::fmt::Display) -> TexError {
TexError::malformed(TexFile::Type1, err.to_string())
}
fn number(value: PsNumber) -> Obj {
match value {
PsNumber::Int(int) => Obj::Int(int),
PsNumber::Real(real) => Obj::Real(real),
}
}
impl PdfRenderer {
pub(super) fn pdf_tex(
&mut self,
gc: &GraphicsContext,
tex: &TexText,
origin: Point,
angle: f64,
) -> Result<(), TexError> {
let page = tex.page(tex.size(), POINTS_DPI)?;
if let Some(url) = &gc.url {
self.pages.add_link(Link::over_block(
url,
origin,
page.width,
page.height,
angle,
));
}
let files = tex.manager.files();
self.dvi_fonts.files.get_or_insert(files);
let seq = self.track_text(&page, files)?;
let placement = text_placement(origin, angle);
self.check_gc(gc, Some(gc.stroke_color()));
self.show_items(join_strings(seq), &placement, angle);
self.fill_rules(gc, &page.boxes, &placement);
Ok(())
}
fn track_text(&mut self, page: &DviPage, files: &TexFiles) -> Result<Vec<Item>, TexError> {
let mut seq = Vec::new();
for run in page.text.chunk_by(|a, b| a.font == b.font) {
let font = &run[0].font;
let name = format!("F-{}", font.name());
seq.push(Item::Font(name.clone(), font.size()));
let used = self
.dvi_fonts
.fonts
.entry(name)
.or_insert_with(|| UsedFont {
font: Arc::clone(font),
glyphs: BTreeMap::new(),
});
for text in run {
let code = u8::try_from(text.glyph).map_err(|_| {
TexError::malformed(
TexFile::Dvi,
format!("character {} is not a byte", text.glyph),
)
})?;
seq.push(Item::Text {
x: text.x,
y: text.y,
shown: vec![Shown::Codes(vec![code])],
end: text.x + text.width,
});
let index = files.glyph_index(&text.font, text.glyph)?;
used.font = Arc::clone(&text.font);
used.glyphs.entry(text.glyph).or_insert(index);
}
}
Ok(seq)
}
fn show_items(&mut self, items: Vec<Item>, placement: &Affine2D, angle: f64) {
self.output(&[Obj::op("BT")]);
let mut old = Point::ORIGIN;
for item in items {
match item {
Item::Font(name, size) => {
self.output(&[Obj::name(name), Obj::Real(size), Obj::op("Tf")]);
}
Item::Text { x, y, shown, .. } => {
let at = placement.transform_point(Point::new(x, y));
self.setup_textpos(at, angle, old);
old = at;
if let [Shown::Codes(codes)] = shown.as_slice() {
self.output(&[Obj::Str(codes.clone()), Obj::op("Tj")]);
} else {
let array = shown
.into_iter()
.map(|item| match item {
Shown::Codes(codes) => Obj::Str(codes),
Shown::Kern(kern) => Obj::Real(kern),
})
.collect();
self.output(&[Obj::Array(array), Obj::op("TJ")]);
}
}
}
}
self.output(&[Obj::op("ET")]);
}
fn fill_rules(&mut self, gc: &GraphicsContext, rules: &[DviBox], placement: &Affine2D) {
let mut box_gc = gc.clone();
box_gc.linewidth = 0.0;
let codes = vec![
PathCode::MoveTo,
PathCode::LineTo,
PathCode::LineTo,
PathCode::LineTo,
PathCode::ClosePoly,
];
for b in rules {
let (x, y, w, h) = (b.x, b.y, b.width, b.height);
let vertices = vec![
Point::new(x, y),
Point::new(x + w, y),
Point::new(x + w, y + h),
Point::new(x, y + h),
Point::ORIGIN,
];
if let Ok(path) = Path::new(vertices, codes.clone()) {
crate::renderer::Renderer::draw_path(
self,
&box_gc,
&path,
placement,
Some(gc.stroke_color()),
);
}
}
}
pub(super) fn setup_textpos(&mut self, at: Point, angle: f64, old: Point) {
if angle == 0.0 {
self.output(&[
Obj::Real(at.x - old.x),
Obj::Real(at.y - old.y),
Obj::op("Td"),
]);
} else {
let (sin, cos) = angle.to_radians().sin_cos();
self.output(&[
Obj::Real(cos),
Obj::Real(sin),
Obj::Real(-sin),
Obj::Real(cos),
Obj::Real(at.x),
Obj::Real(at.y),
Obj::op("Tm"),
]);
self.output(&[Obj::Int(0), Obj::Int(0), Obj::op("Td")]);
}
}
}
impl DviFonts {
pub(super) fn register(&mut self, font: Arc<DviFont>, files: &'static TexFiles) -> String {
self.files.get_or_insert(files);
let name = format!("F-{}", font.name());
if let Some(used) = self.fonts.get_mut(&name) {
used.font = font;
} else {
let glyphs = BTreeMap::new();
self.fonts.insert(name.clone(), UsedFont { font, glyphs });
}
name
}
pub(super) fn is_used(&self, name: &str) -> bool {
self.fonts
.get(name)
.is_some_and(|used| !used.glyphs.is_empty())
}
pub(super) fn embed(&self, writer: &mut PdfWriter) -> Result<Vec<(String, Obj)>, VectorError> {
let Some(files) = self.files else {
return Ok(Vec::new());
};
self.fonts
.iter()
.map(|(name, used)| Ok((name.clone(), Obj::Ref(embed_font(writer, files, used)?))))
.collect()
}
}
fn embed_font(
writer: &mut PdfWriter,
files: &TexFiles,
used: &UsedFont,
) -> Result<u32, VectorError> {
let psfont = files.psfont(used.font.texname())?;
let filename = psfont.type1_file()?;
let dict_id = writer.reserve();
let font = embedded_program(files, &psfont, filename, used)?;
let prop = font.prop();
let font_name = prop.font_name().as_text().unwrap_or_default().to_owned();
let descriptor_id = type1_descriptor(writer, &font, &font_name)?;
let encoding: Vec<(i64, String)> = prop
.encoding()
.map(|e| {
e.iter()
.map(|(code, name)| (code, name.to_owned()))
.collect()
})
.unwrap_or_default();
let first = encoding.first().map_or(0, |(code, _)| *code);
let last = encoding.last().map_or(255, |(code, _)| *code);
let widths_id = writer.reserve();
writer.write_object(
widths_id,
&Obj::Array(tfm_widths(&used.font, first..=last)?),
)?;
writer.write_object(
dict_id,
&Obj::dict([
("Type", Obj::name("Font")),
("Subtype", Obj::name("Type1")),
("BaseFont", Obj::name(font_name)),
("FontDescriptor", Obj::Ref(descriptor_id)),
(
"Encoding",
Obj::dict([
("Type", Obj::name("Encoding")),
("Differences", Obj::Array(differences(&encoding))),
]),
),
("FirstChar", Obj::Int(first)),
("LastChar", Obj::Int(last)),
("Widths", Obj::Ref(widths_id)),
]),
)?;
Ok(dict_id)
}
fn embedded_program(
files: &TexFiles,
psfont: &PsFont,
filename: &std::path::Path,
used: &UsedFont,
) -> Result<Type1Font, VectorError> {
let mut font = Type1Font::open(filename).map_err(type1_error)?;
if let Some(encoding) = &psfont.encoding {
let names = files.encoding(encoding)?;
let codes = names
.iter()
.enumerate()
.map(|(i, name)| (i as i64, name.clone()));
font = font
.with_encoding(&Type1Encoding::from_codes(codes))
.map_err(type1_error)?;
}
if psfont.effects.slant.is_some() || psfont.effects.extend.is_some() {
font = font.transform(psfont.effects).map_err(type1_error)?;
}
let glyphs: Vec<u16> = used.glyphs.values().map(|glyph| glyph.0).collect();
let codes = used
.glyphs
.keys()
.filter_map(|&code| u32::try_from(code).ok());
Ok(font
.subset(codes, &subset_prefix(&glyphs))
.map_err(type1_error)?)
}
fn differences(encoding: &[(i64, String)]) -> Vec<Obj> {
let mut differences = Vec::new();
let mut previous = None;
for (code, name) in encoding {
if previous.is_none_or(|p: i64| p + 1 != *code) {
differences.push(Obj::Int(*code));
}
differences.push(Obj::name(name.clone()));
previous = Some(*code);
}
differences
}
fn tfm_widths(font: &DviFont, codes: RangeInclusive<i64>) -> Result<Vec<Obj>, TexError> {
codes
.map(|code| {
let metrics = font.char_metrics(i128::from(code))?;
Ok(Obj::Int(
metrics.map_or(0, |m| (1000 * i64::from(m.width)) >> 20),
))
})
.collect()
}
pub(super) fn type1_descriptor(
writer: &mut PdfWriter,
font: &Type1Font,
font_name: &str,
) -> Result<u32, VectorError> {
let descriptor_id = writer.reserve();
let file_id = writer.reserve();
let prop = font.prop();
let italic_angle = prop.italic_angle();
let flags = i64::from(prop.is_fixed_pitch().is_truthy())
| 1 << 2
| i64::from(italic_angle.is_truthy()) << 6;
let mut charset: Vec<String> = prop
.encoding()
.map(|e| {
e.iter()
.filter(|(_, name)| *name != ".notdef")
.map(|(_, name)| format!("/{name}"))
.collect()
})
.unwrap_or_default();
charset.sort();
let bbox = prop.font_bbox();
let italic = match italic_angle {
PsValue::Number(value) => number(*value),
other => Obj::Real(other.to_f64().unwrap_or(0.0)),
};
let family = prop
.family_name()
.as_text()
.unwrap_or_default()
.as_bytes()
.to_vec();
writer.write_object(
descriptor_id,
&Obj::dict([
("Type", Obj::name("FontDescriptor")),
("FontName", Obj::name(font_name)),
("Flags", Obj::Int(flags)),
(
"FontBBox",
Obj::Array(bbox.iter().copied().map(number).collect()),
),
("ItalicAngle", italic),
("Ascent", number(bbox[3])),
("Descent", number(bbox[1])),
("CapHeight", Obj::Int(1000)),
("XHeight", Obj::Int(500)),
("FontFile", Obj::Ref(file_id)),
("FontFamily", Obj::Str(family)),
("StemV", Obj::Int(50)),
("CharSet", Obj::Str(charset.concat().into_bytes())),
]),
)?;
let parts = font.parts();
let program = [parts.cleartext.as_slice(), parts.encrypted.as_slice()].concat();
writer.output_stream(
file_id,
&program,
vec![
("Length1".to_owned(), Obj::Int(parts.cleartext.len() as i64)),
("Length2".to_owned(), Obj::Int(parts.encrypted.len() as i64)),
("Length3".to_owned(), Obj::Int(0)),
],
)?;
Ok(descriptor_id)
}
#[cfg(test)]
mod tests {
use super::*;
fn text(x: f64, y: f64, code: u8, width: f64) -> Item {
Item::Text {
x,
y,
shown: vec![Shown::Codes(vec![code])],
end: x + width,
}
}
#[test]
fn fonts_never_registered_are_not_used() {
assert!(!DviFonts::default().is_used("F-cmr10"));
}
#[test]
fn strings_join_with_kerns_as_matplotlib_joins_them() {
let joined = join_strings(vec![
Item::Font("F-cmr10".into(), 10.0),
text(0.0, 0.0, b'a', 5.0),
text(5.05, 0.0, b'b', 5.0),
text(11.0, 0.0, b'c', 5.0),
text(16.0, 2.0, b'd', 5.0),
]);
assert_eq!(
joined,
[
Item::Font("F-cmr10".into(), 10.0),
Item::Text {
x: 0.0,
y: 0.0,
shown: vec![
Shown::Codes(b"ab".to_vec()),
Shown::Kern((10.0 - 11.0) * 1000.0 / 10.0),
Shown::Codes(b"c".to_vec()),
],
end: 16.0,
},
text(16.0, 2.0, b'd', 5.0),
]
);
}
}