Files
texrra-engine/crates/texrra-tex-core/src/pdf_text.rs
T

147 lines
4.3 KiB
Rust

//! Native pdfTeX text cursor, in original TeX sp, before the page matrix.
use crate::{
execution::FontId,
native_font::NativeFont,
pdf_device::{divide_scaled, print_decimal, print_sp},
};
pub(super) struct NativeText {
font: Option<FontId>,
effective_size: i64,
v: i64,
start_h: i64,
delta_h: i64,
opened: bool,
}
impl NativeText {
pub(super) fn glyph(
text: &mut Option<Self>,
out: &mut String,
x: i64,
y: i64,
font: &NativeFont,
paint: &crate::native_font::NativeFontPaint,
slot: u8,
height: i64,
digits: u8,
) {
let first = text.is_none();
let state = text.get_or_insert_with(|| {
out.push_str("BT\n");
Self {
font: None,
effective_size: 0,
v: height,
start_h: 0,
delta_h: 0,
opened: false,
}
});
if state.font != Some(font.id) {
state.close_string(out);
let (number, size) = divide_scaled(font.size_sp(), 6_578_176, 6);
state.effective_size = size;
state.font = Some(font.id);
out.push_str(&format!(
"/N{} {} Tf\n",
font.id.index(),
print_decimal(number, 4)
));
}
let (mut shift, shift_out) =
divide_scaled(x - state.start_h - state.delta_h, state.effective_size, 3);
let minimum = divide_scaled(6_578_176, 10i64.pow(u32::from(digits) + 2), 0).0;
let (v, v_out) = if (state.v - y).abs() >= minimum {
divide_scaled(state.v - y, 6_578_176, digits + 2)
} else {
(0, 0)
};
if first || v != 0 || shift.abs() >= 32768 {
state.close_string(out);
let (h, h_out) = divide_scaled(x - state.start_h, 6_578_176, digits + 2);
out.push_str(&format!(
"{} {} Td\n",
print_decimal(h, digits),
print_decimal(v, digits)
));
state.start_h += h_out;
state.v -= v_out;
state.delta_h = 0;
shift = 0;
}
if !state.opened {
out.push('[');
state.opened = true;
}
if shift != 0 {
out.push_str(&format!(" {} ", -shift));
state.delta_h += shift_out;
}
out.push_str(&format!("<{slot:02X}>"));
state.delta_h += match &paint.program {
crate::native_font::NativeFontProgram::Pk(pk) => pk.advances[&slot],
crate::native_font::NativeFontProgram::Type1(_) => {
divide_scaled(
font.metrics().width(char::from(slot)),
state.effective_size,
4,
)
.1
}
};
}
fn close_string(&mut self, out: &mut String) {
if self.opened {
out.push_str("] TJ\n");
self.opened = false;
}
}
pub(super) fn finish(text: &mut Option<Self>, out: &mut String) {
if let Some(mut text) = text.take() {
text.close_string(out);
out.push_str("ET\n");
}
}
}
pub(super) fn rule(
out: &mut String,
x: i64,
top: i64,
w: i64,
h: i64,
page_height: i64,
digits: u8,
) {
if w <= 0 || h <= 0 {
return;
}
let bottom = page_height - top - h;
out.push_str("q\n");
if h <= 65_782 {
out.push_str(&format!(
"1 0 0 1 {} {} cm []0 d 0 J {} w 0 0 m {} 0 l S\n",
print_sp(x, digits),
print_sp(bottom + (h + 1) / 2, digits),
print_sp(h, digits),
print_sp(w, digits)
));
} else if w <= 65_782 {
out.push_str(&format!(
"1 0 0 1 {} {} cm []0 d 0 J {} w 0 0 m 0 {} l S\n",
print_sp(x + (w + 1) / 2, digits),
print_sp(bottom, digits),
print_sp(w, digits),
print_sp(h, digits)
));
} else {
out.push_str(&format!(
"1 0 0 1 {} {} cm 0 0 {} {} re f\n",
print_sp(x, digits),
print_sp(bottom, digits),
print_sp(w, digits),
print_sp(h, digits)
));
}
out.push_str("Q\n");
}