const DVI_BUF_SIZE: usize = 16384;
const HALF_BUF: usize = DVI_BUF_SIZE / 2;
const SET1: u8 = 128;
const SET_RULE: u8 = 132;
const PUT_RULE: u8 = 137;
const BOP: u8 = 139;
const EOP: u8 = 140;
const PUSH: u8 = 141;
const POP: u8 = 142;
const RIGHT1: u8 = 143;
#[cfg(test)]
const W0: u8 = 147;
const DOWN1: u8 = 157;
const Y0: u8 = 161;
const Y1: u8 = 162;
const Z0: u8 = 166;
const Z1: u8 = 167;
const FNT_NUM_0: u8 = 171;
const FNT1: u8 = 235;
const XXX1: u8 = 239;
const XXX4: u8 = 242;
const FNT_DEF1: u8 = 243;
const PRE: u8 = 247;
const POST: u8 = 248;
const POST_POST: u8 = 249;
const ID_BYTE: u8 = 2;
const Y_HERE: u8 = 1;
const Z_HERE: u8 = 2;
const YZ_OK: u8 = 3;
const Y_OK: u8 = 4;
const Z_OK: u8 = 5;
const D_FIXED: u8 = 6;
const NONE_SEEN: u8 = 0;
const Y_SEEN: u8 = 6;
const Z_SEEN: u8 = 12;
#[derive(Clone, Copy, Debug)]
struct Movement {
width: i32,
location: usize,
info: u8,
}
#[derive(Clone, Debug)]
pub struct FontDef {
pub checksum: u32,
pub size: i32,
pub design_size: i32,
pub area: String,
pub name: String,
}
pub struct Out {
bytes: Vec<u8>,
dvi_ptr: usize,
dvi_limit: usize,
dvi_offset: usize,
dvi_gone: usize,
down: Vec<Movement>,
right: Vec<Movement>,
fonts: Vec<Option<FontDef>>,
font_used: Vec<bool>,
last_bop: i32,
pub total_pages: i32,
pub max_push: i32,
pub max_v: i32,
pub max_h: i32,
mag: i32,
}
impl Out {
pub fn new(comment: &str, mag: i32, fonts: Vec<Option<FontDef>>) -> Out {
let used = vec![false; fonts.len()];
let mut o = Out {
bytes: Vec::new(),
dvi_ptr: 0,
dvi_limit: DVI_BUF_SIZE,
dvi_offset: 0,
dvi_gone: 0,
down: Vec::new(),
right: Vec::new(),
fonts,
font_used: used,
last_bop: -1,
total_pages: 0,
max_push: 0,
max_v: 0,
max_h: 0,
mag,
};
o.out(PRE);
o.out(ID_BYTE);
o.four(25_400_000);
o.four(473_628_672);
o.four(mag);
let c: Vec<u8> = comment.bytes().take(255).collect();
o.out(c.len() as u8);
for b in c {
o.out(b);
}
o
}
fn out(&mut self, b: u8) {
self.bytes.push(b);
self.dvi_ptr += 1;
if self.dvi_ptr == self.dvi_limit {
if self.dvi_limit == DVI_BUF_SIZE {
self.dvi_limit = HALF_BUF;
self.dvi_offset += DVI_BUF_SIZE;
self.dvi_ptr = 0;
} else {
self.dvi_limit = DVI_BUF_SIZE;
}
self.dvi_gone += HALF_BUF;
}
}
pub fn here(&self) -> usize {
self.bytes.len()
}
fn four(&mut self, x: i32) {
for b in x.to_be_bytes() {
self.out(b);
}
}
fn font_def(&mut self, f: usize) {
let Some(Some(def)) = self.fonts.get(f).cloned() else {
return;
};
self.out(FNT_DEF1);
self.out((f - 1) as u8);
for b in def.checksum.to_be_bytes() {
self.out(b);
}
self.four(def.size);
self.four(def.design_size);
self.out(def.area.len() as u8);
self.out(def.name.len() as u8);
for b in def.area.bytes().chain(def.name.bytes()) {
self.out(b);
}
}
pub fn font(&mut self, f: usize) {
if f >= self.font_used.len() {
self.font_used.resize(f + 1, false);
}
if !self.font_used[f] {
self.font_def(f);
self.font_used[f] = true;
}
if f <= 64 {
self.out((f - 1) as u8 + FNT_NUM_0);
} else {
self.out(FNT1);
self.out((f - 1) as u8);
}
}
pub fn set_char(&mut self, c: u32) {
if c >= 128 {
self.out(SET1);
}
self.out(c as u8);
}
pub fn rule(&mut self, height: i32, width: i32, set: bool) {
self.out(if set { SET_RULE } else { PUT_RULE });
self.four(height);
self.four(width);
}
pub fn special(&mut self, text: &[u8]) {
if text.len() < 256 {
self.out(XXX1);
self.out(text.len() as u8);
} else {
self.out(XXX4);
self.four(text.len() as i32);
}
for &b in text {
self.out(b);
}
}
pub fn push(&mut self) -> usize {
self.out(PUSH);
self.here()
}
pub fn pop(&mut self, save_loc: usize) {
if save_loc == self.here() && self.dvi_ptr > 0 {
self.bytes.pop();
self.dvi_ptr -= 1;
} else {
self.out(POP);
}
}
pub fn prune(&mut self, l: usize) {
while self.down.last().is_some_and(|m| m.location >= l) {
self.down.pop();
}
while self.right.last().is_some_and(|m| m.location >= l) {
self.right.pop();
}
}
pub fn right(&mut self, w: i32) {
self.movement(w, RIGHT1);
}
pub fn down(&mut self, w: i32) {
self.movement(w, DOWN1);
}
fn movement(&mut self, w: i32, o: u8) {
let location = self.here();
let stack = match o {
DOWN1 => &mut self.down,
_ => &mut self.right,
};
stack.push(Movement {
width: w,
location,
info: YZ_OK,
});
let q = stack.len() - 1;
let mut mstate = NONE_SEEN;
let mut found: Option<usize> = None;
let mut i = q;
'look: while i > 0 {
i -= 1;
let p = stack[i];
if p.width == w {
match mstate + p.info {
s if s == NONE_SEEN + YZ_OK
|| s == NONE_SEEN + Y_OK
|| s == Z_SEEN + YZ_OK
|| s == Z_SEEN + Y_OK =>
{
if p.location < self.dvi_gone {
break 'look;
}
self.bytes[p.location] += Y1 - DOWN1;
stack[i].info = Y_HERE;
found = Some(i);
break 'look;
}
s if s == NONE_SEEN + Z_OK || s == Y_SEEN + YZ_OK || s == Y_SEEN + Z_OK => {
if p.location < self.dvi_gone {
break 'look;
}
self.bytes[p.location] += Z1 - DOWN1;
stack[i].info = Z_HERE;
found = Some(i);
break 'look;
}
s if s == NONE_SEEN + Y_HERE
|| s == NONE_SEEN + Z_HERE
|| s == Y_SEEN + Z_HERE
|| s == Z_SEEN + Y_HERE =>
{
found = Some(i);
break 'look;
}
_ => {}
}
} else {
match mstate + p.info {
s if s == NONE_SEEN + Y_HERE => mstate = Y_SEEN,
s if s == NONE_SEEN + Z_HERE => mstate = Z_SEEN,
s if s == Y_SEEN + Z_HERE || s == Z_SEEN + Y_HERE => break 'look,
_ => {}
}
}
}
if let Some(p) = found {
let info = stack[p].info;
stack[q].info = info;
if info == Y_HERE {
for e in stack[p + 1..q].iter_mut() {
e.info = match e.info {
YZ_OK => Z_OK,
Y_OK => D_FIXED,
other => other,
};
}
self.out(o + (Y0 - DOWN1));
} else {
for e in stack[p + 1..q].iter_mut() {
e.info = match e.info {
YZ_OK => Y_OK,
Z_OK => D_FIXED,
other => other,
};
}
self.out(o + (Z0 - DOWN1));
}
return;
}
let a = w.unsigned_abs();
if a >= 0o40000000 {
self.out(o + 3);
self.four(w);
return;
}
let mut w = i64::from(w);
if a >= 0o100000 {
self.out(o + 2);
if w < 0 {
w += 0o100000000;
}
self.out((w / 0o200000) as u8);
w %= 0o200000;
self.out((w / 0o400) as u8);
self.out((w % 0o400) as u8);
return;
}
if a >= 0o200 {
self.out(o + 1);
if w < 0 {
w += 0o200000;
}
self.out((w / 0o400) as u8);
self.out((w % 0o400) as u8);
return;
}
self.out(o);
if w < 0 {
w += 0o400;
}
self.out((w % 0o400) as u8);
}
pub fn begin_page(&mut self, counts: [i32; 10]) {
let page_loc = self.here() as i32;
self.out(BOP);
for c in counts {
self.four(c);
}
let last = self.last_bop;
self.four(last);
self.last_bop = page_loc;
}
pub fn end_page(&mut self) {
self.out(EOP);
self.total_pages += 1;
}
pub fn finish(mut self) -> Vec<u8> {
self.out(POST);
let last = self.last_bop;
self.four(last);
self.last_bop = self.here() as i32 - 5;
self.four(25_400_000);
self.four(473_628_672);
let mag = self.mag;
self.four(mag);
let (v, h) = (self.max_v, self.max_h);
self.four(v);
self.four(h);
self.out((self.max_push / 256) as u8);
self.out((self.max_push % 256) as u8);
self.out(((self.total_pages / 256) % 256) as u8);
self.out((self.total_pages % 256) as u8);
for f in (1..self.font_used.len()).rev() {
if self.font_used[f] {
self.font_def(f);
}
}
self.out(POST_POST);
let last = self.last_bop;
self.four(last);
self.out(ID_BYTE);
let k = 4 + ((DVI_BUF_SIZE - self.dvi_ptr % DVI_BUF_SIZE) % 4);
for _ in 0..k {
self.out(223);
}
self.bytes
}
}
impl crate::shipout::Sink for Out {
fn here(&self) -> usize {
Out::here(self)
}
fn push(&mut self) -> usize {
Out::push(self)
}
fn pop(&mut self, save_loc: usize) {
Out::pop(self, save_loc)
}
fn prune(&mut self, save_loc: usize) {
Out::prune(self, save_loc)
}
fn right(&mut self, w: i32) {
Out::right(self, w)
}
fn down(&mut self, w: i32) {
Out::down(self, w)
}
fn font(&mut self, f: usize) {
Out::font(self, f)
}
fn set_char(&mut self, c: u32, _width: i32) {
Out::set_char(self, c)
}
fn rule(&mut self, height: i32, width: i32, set: bool) {
Out::rule(self, height, width, set)
}
fn special(&mut self, text: &str) {
Out::special(self, text.as_bytes())
}
fn begin_page(&mut self, counts: [i32; 10]) {
Out::begin_page(self, counts)
}
fn end_page(&mut self) {
Out::end_page(self)
}
fn note_push(&mut self, depth: i32) {
self.max_push = self.max_push.max(depth);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn out() -> Out {
Out::new("", 1000, vec![None])
}
#[test]
fn a_movement_takes_the_fewest_bytes_it_fits() {
let mut o = out();
let start = o.here();
o.right(100);
o.right(-200);
assert_eq!(&o.bytes[start..], &[RIGHT1, 100, RIGHT1 + 1, 0xff, 0x38]);
}
#[test]
fn a_repeated_movement_is_w0_after_w1() {
let mut o = out();
let start = o.here();
o.right(5);
o.right(5);
assert_eq!(&o.bytes[start..], &[RIGHT1 + (Y1 - DOWN1), 5, W0]);
let mut o = out();
let start = o.here();
o.down(7);
o.down(9);
o.down(7);
assert_eq!(&o.bytes[start..], &[Y1, 7, DOWN1, 9, Y0]);
}
#[test]
fn an_empty_push_is_taken_back() {
let mut o = out();
let start = o.here();
let loc = o.push();
o.pop(loc);
assert_eq!(o.here(), start);
let loc = o.push();
o.right(3);
o.pop(loc);
assert_eq!(&o.bytes[start..], &[PUSH, RIGHT1, 3, POP]);
}
}