255 lines
8.0 KiB
Rust
255 lines
8.0 KiB
Rust
//! VF file structure and the local DVI programs of individual characters.
|
|
use std::collections::BTreeMap;
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub(super) struct VirtualFont {
|
|
pub checksum: u32,
|
|
pub design_size: i64,
|
|
pub default_font: Option<u32>,
|
|
pub local_fonts: BTreeMap<u32, LocalFont>,
|
|
pub local_order: Vec<u32>,
|
|
pub packets: BTreeMap<u8, Packet>,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub(super) struct LocalFont {
|
|
pub checksum: u32,
|
|
pub name: String,
|
|
pub scale: i32,
|
|
pub design_size: i64,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub(super) struct Packet {
|
|
pub width: i32,
|
|
pub commands: Vec<Command>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(super) enum Axis {
|
|
Horizontal,
|
|
Vertical,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub(super) enum Command {
|
|
Character {
|
|
slot: u8,
|
|
advance: bool,
|
|
},
|
|
Rule {
|
|
height: i32,
|
|
width: i32,
|
|
advance: bool,
|
|
},
|
|
Right(i32),
|
|
Down(i32),
|
|
Repeat {
|
|
register: usize,
|
|
new: Option<i32>,
|
|
axis: Axis,
|
|
},
|
|
Font(u32),
|
|
Push,
|
|
Pop,
|
|
Special,
|
|
}
|
|
|
|
struct Reader<'a>(&'a [u8]);
|
|
|
|
impl<'a> Reader<'a> {
|
|
fn take(&mut self, count: usize) -> Result<&'a [u8], String> {
|
|
let (head, tail) = self.0.split_at_checked(count).ok_or("truncated VF data")?;
|
|
self.0 = tail;
|
|
Ok(head)
|
|
}
|
|
|
|
fn byte(&mut self) -> Result<u8, String> {
|
|
Ok(self.take(1)?[0])
|
|
}
|
|
|
|
fn unsigned(&mut self, count: usize) -> Result<u32, String> {
|
|
let mut word = [0; 4];
|
|
word[4 - count..].copy_from_slice(self.take(count)?);
|
|
Ok(u32::from_be_bytes(word))
|
|
}
|
|
|
|
fn signed(&mut self, count: usize) -> Result<i32, String> {
|
|
let bytes = self.take(count)?;
|
|
let mut word = [if bytes[0] & 128 != 0 { 255 } else { 0 }; 4];
|
|
word[4 - count..].copy_from_slice(bytes);
|
|
Ok(i32::from_be_bytes(word))
|
|
}
|
|
|
|
fn local_font(&mut self) -> Result<LocalFont, String> {
|
|
let checksum = self.unsigned(4)?;
|
|
let scale = self.signed(4)?;
|
|
let design_size = i64::from(self.signed(4)? / 16);
|
|
let area_len = usize::from(self.byte()?);
|
|
let name_len = usize::from(self.byte()?);
|
|
// pdfTeX reads the area but resolves a local font by its name.
|
|
self.take(area_len)?;
|
|
let name = std::str::from_utf8(self.take(name_len)?)
|
|
.map_err(|_| "invalid VF local font name")?
|
|
.to_owned();
|
|
if name.is_empty() || scale <= 0 || design_size <= 0 {
|
|
return Err("invalid VF local font definition".into());
|
|
}
|
|
Ok(LocalFont {
|
|
checksum,
|
|
name,
|
|
scale,
|
|
design_size,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl VirtualFont {
|
|
pub fn parse(bytes: &[u8]) -> Result<Self, String> {
|
|
let mut input = Reader(bytes);
|
|
if input.take(2)? != [247, 202] {
|
|
return Err("invalid VF preamble".into());
|
|
}
|
|
let comment_len = usize::from(input.byte()?);
|
|
input.take(comment_len)?;
|
|
let checksum = input.unsigned(4)?;
|
|
let design_size = i64::from(input.signed(4)? / 16);
|
|
if design_size <= 0 {
|
|
return Err("invalid VF design size".into());
|
|
}
|
|
let mut vf = Self {
|
|
checksum,
|
|
design_size,
|
|
default_font: None,
|
|
local_fonts: BTreeMap::new(),
|
|
local_order: Vec::new(),
|
|
packets: BTreeMap::new(),
|
|
};
|
|
let mut opcode = input.byte()?;
|
|
while (243..=246).contains(&opcode) {
|
|
let id = input.unsigned(usize::from(opcode - 242))?;
|
|
let font = input.local_font()?;
|
|
if vf.local_fonts.insert(id, font).is_some() {
|
|
return Err("duplicate local font in VF".into());
|
|
}
|
|
vf.default_font.get_or_insert(id);
|
|
vf.local_order.push(id);
|
|
opcode = input.byte()?;
|
|
}
|
|
while opcode <= 242 {
|
|
let (length, slot, width) = if opcode == 242 {
|
|
(
|
|
usize::try_from(input.unsigned(4)?).map_err(|_| "VF packet length overflow")?,
|
|
u8::try_from(input.unsigned(4)?).map_err(|_| "VF slot outside TFM range")?,
|
|
input.signed(4)?,
|
|
)
|
|
} else {
|
|
(
|
|
usize::from(opcode),
|
|
input.byte()?,
|
|
input.unsigned(3)? as i32,
|
|
)
|
|
};
|
|
let commands = parse_packet(input.take(length)?, &vf.local_fonts)?;
|
|
if vf
|
|
.packets
|
|
.insert(slot, Packet { width, commands })
|
|
.is_some()
|
|
{
|
|
return Err("duplicate character packet in VF".into());
|
|
}
|
|
opcode = input.byte()?;
|
|
}
|
|
if opcode != 248 || input.0.iter().any(|&byte| byte != 248) {
|
|
return Err("invalid VF postamble".into());
|
|
}
|
|
Ok(vf)
|
|
}
|
|
}
|
|
|
|
fn parse_packet(bytes: &[u8], fonts: &BTreeMap<u32, LocalFont>) -> Result<Vec<Command>, String> {
|
|
let mut input = Reader(bytes);
|
|
let mut commands = Vec::new();
|
|
let mut depth: usize = 0;
|
|
while !input.0.is_empty() {
|
|
let opcode = input.byte()?;
|
|
let command = match opcode {
|
|
0..=127 => Command::Character {
|
|
slot: opcode,
|
|
advance: true,
|
|
},
|
|
128..=131 | 133..=136 => {
|
|
let base = if opcode < 132 { 127 } else { 132 };
|
|
let slot = u8::try_from(input.unsigned(usize::from(opcode - base))?)
|
|
.map_err(|_| "VF packet character outside TFM range")?;
|
|
Command::Character {
|
|
slot,
|
|
advance: opcode < 132,
|
|
}
|
|
}
|
|
132 | 137 => Command::Rule {
|
|
height: input.signed(4)?,
|
|
width: input.signed(4)?,
|
|
advance: opcode == 132,
|
|
},
|
|
138 => continue,
|
|
141 => {
|
|
depth += 1;
|
|
if depth > 100 {
|
|
return Err("VF packet stack limit exceeded".into());
|
|
}
|
|
Command::Push
|
|
}
|
|
142 => {
|
|
depth = depth.checked_sub(1).ok_or("VF packet stack underflow")?;
|
|
Command::Pop
|
|
}
|
|
143..=146 => Command::Right(input.signed(usize::from(opcode - 142))?),
|
|
157..=160 => Command::Down(input.signed(usize::from(opcode - 156))?),
|
|
147..=156 | 161..=170 => {
|
|
let (axis, start, offset) = if opcode < 157 {
|
|
(Axis::Horizontal, 147, 0)
|
|
} else {
|
|
(Axis::Vertical, 161, 2)
|
|
};
|
|
let relative = opcode - start;
|
|
let register = offset + usize::from(relative / 5);
|
|
let size = usize::from(relative % 5);
|
|
let new = if size == 0 {
|
|
None
|
|
} else {
|
|
Some(input.signed(size)?)
|
|
};
|
|
Command::Repeat {
|
|
register,
|
|
new,
|
|
axis,
|
|
}
|
|
}
|
|
171..=238 => {
|
|
let id = if opcode <= 234 {
|
|
u32::from(opcode - 171)
|
|
} else {
|
|
input.unsigned(usize::from(opcode - 234))?
|
|
};
|
|
if !fonts.contains_key(&id) {
|
|
return Err("VF packet selects an undefined local font".into());
|
|
}
|
|
Command::Font(id)
|
|
}
|
|
239..=242 => {
|
|
let len = usize::try_from(input.unsigned(usize::from(opcode - 238))?)
|
|
.map_err(|_| "VF special length overflow")?;
|
|
input.take(len)?;
|
|
Command::Special
|
|
}
|
|
_ => return Err(format!("invalid DVI opcode {opcode} in VF packet")),
|
|
};
|
|
commands.push(command);
|
|
}
|
|
if depth != 0 {
|
|
return Err("unbalanced VF packet stack".into());
|
|
}
|
|
Ok(commands)
|
|
}
|