use crate::char::Char;
use crate::error::{Error, Result};
use crate::geom::{Matrix, Point};
use crate::page::{Page, PageMetrics};
use compact_str::CompactString;
use lopdf::content::Content;
use lopdf::{Dictionary, Document as LDoc, Encoding, Object, ObjectId};
use std::collections::HashMap;
pub(crate) fn page_metrics(
doc: &LDoc,
page_id: ObjectId,
page_index: usize,
) -> Result<PageMetrics> {
let page = doc.get_dictionary(page_id)?;
let media_box =
resolve_inheritable(doc, page, b"MediaBox").ok_or_else(|| Error::ContentStream {
page: page_index,
reason: "missing /MediaBox".into(),
})?;
let media = media_box_dimensions(doc, page_index, &media_box)?;
let rotation = resolve_inheritable(doc, page, b"Rotate")
.and_then(|o| match o {
Object::Integer(i) => Some(i),
Object::Real(r) => Some(r as i64),
_ => None,
})
.map(normalize_rotation)
.unwrap_or(0);
Ok(PageMetrics {
media_width: media.width,
media_height: media.height,
origin_x: media.origin_x,
origin_y: media.origin_y,
rotation,
})
}
fn normalize_rotation(raw: i64) -> i16 {
let wrapped = raw.rem_euclid(360);
if wrapped % 90 != 0 {
log::warn!("/Rotate {raw} is not a multiple of 90 โ treating the page as unrotated");
return 0;
}
wrapped as i16
}
const MAX_PAGE_TREE_DEPTH: usize = 64;
fn resolve_inheritable(doc: &LDoc, page: &Dictionary, key: &[u8]) -> Option<Object> {
let mut cursor: &Dictionary = page;
for _ in 0..MAX_PAGE_TREE_DEPTH {
if let Ok(v) = cursor.get(key) {
return Some(deref(doc, v.clone()));
}
let Ok(Object::Reference(id)) = cursor.get(b"Parent") else {
return None;
};
let Ok(parent) = doc.get_dictionary(*id) else {
return None;
};
cursor = parent;
}
log::warn!("/Parent chain exceeded {MAX_PAGE_TREE_DEPTH} levels โ treating as cyclic");
None
}
fn deref(doc: &LDoc, obj: Object) -> Object {
match obj {
Object::Reference(id) => doc.get_object(id).cloned().unwrap_or(Object::Null),
other => other,
}
}
fn media_box_dimensions(doc: &LDoc, page_index: usize, o: &Object) -> Result<MediaBox> {
let arr = match o {
Object::Array(a) => a,
_ => {
return Err(Error::ContentStream {
page: page_index,
reason: "/MediaBox is not an array".into(),
});
}
};
if arr.len() != 4 {
return Err(Error::ContentStream {
page: page_index,
reason: format!("/MediaBox has {} elements, expected 4", arr.len()),
});
}
let n = |o: &Object| -> Result<f32> {
match deref(doc, o.clone()) {
Object::Integer(i) => Ok(i as f32),
Object::Real(r) => Ok(r),
_ => Err(Error::ContentStream {
page: page_index,
reason: "/MediaBox contains a non-numeric value".into(),
}),
}
};
let x0 = n(&arr[0])?;
let y0 = n(&arr[1])?;
let x1 = n(&arr[2])?;
let y1 = n(&arr[3])?;
let (width, height) = ((x1 - x0).abs(), (y1 - y0).abs());
if !width.is_finite() || !height.is_finite() || width <= 0.0 || height <= 0.0 {
return Err(Error::ContentStream {
page: page_index,
reason: format!("/MediaBox has no usable area ({width} x {height})"),
});
}
Ok(MediaBox {
origin_x: x0.min(x1),
origin_y: y0.min(y1),
width,
height,
})
}
struct MediaBox {
origin_x: f32,
origin_y: f32,
width: f32,
height: f32,
}
pub(crate) fn extract_chars(page: &Page<'_>) -> Result<Vec<Char>> {
let doc = &page.document().inner;
let page_id = page.page_id();
let metrics = page.metrics();
let geom = PageGeometry {
height: metrics.display_height(),
doctop_offset: page.document().doctop_offset(page.index()),
};
let page_rotation = metrics.page_transform();
let raw = doc.get_page_content(page_id);
let content = Content::decode(&raw).map_err(|e| Error::ContentStream {
page: page.index(),
reason: format!("decode content: {e}"),
})?;
let mut fonts = FontTable::default();
if let Ok(map) = doc.get_page_fonts(page_id) {
for (name, dict) in map {
fonts.insert(name, FontInfo::from_dict(doc, dict));
}
}
let mut tm = TextMatrices::default();
let mut gs_stack: Vec<GraphicsState> = vec![GraphicsState::default()];
let mut out = Vec::new();
macro_rules! gs {
() => {
gs_stack.last().expect("graphics stack is never empty")
};
}
macro_rules! gs_mut {
() => {
gs_stack.last_mut().expect("graphics stack is never empty")
};
}
for op in content.operations {
match op.operator.as_str() {
"q" => {
let top = gs!().clone();
gs_stack.push(top);
}
"Q" => {
gs_stack.pop();
if gs_stack.is_empty() {
gs_stack.push(GraphicsState::default());
}
}
"cm" => {
if let Some(m) = matrix_from_operands(&op.operands) {
let top = gs_mut!();
top.ctm = m.then(top.ctm);
}
}
"BT" => tm = TextMatrices::default(),
"ET" => {}
"Tf" => {
if let (Some(name), Some(size)) = (
op.operands.first().and_then(name_of),
op.operands.get(1).and_then(num_of),
) {
let t = &mut gs_mut!().text;
t.font_name = name;
t.font_size = size;
}
}
"Tc" => gs_mut!().text.char_space = first_num(&op.operands).unwrap_or(0.0),
"Tw" => gs_mut!().text.word_space = first_num(&op.operands).unwrap_or(0.0),
"Tz" => gs_mut!().text.h_scale = first_num(&op.operands).unwrap_or(100.0) / 100.0,
"TL" => gs_mut!().text.leading = first_num(&op.operands).unwrap_or(0.0),
"Ts" => gs_mut!().text.rise = first_num(&op.operands).unwrap_or(0.0),
"Tr" => { }
"Tm" => {
if let Some(m) = matrix_from_operands(&op.operands) {
tm.set_line(m);
}
}
"Td" => {
if let (Some(tx), Some(ty)) = (
op.operands.first().and_then(num_of),
op.operands.get(1).and_then(num_of),
) {
tm.next_line(tx, ty);
}
}
"TD" => {
if let (Some(tx), Some(ty)) = (
op.operands.first().and_then(num_of),
op.operands.get(1).and_then(num_of),
) {
gs_mut!().text.leading = -ty;
tm.next_line(tx, ty);
}
}
"T*" => {
let leading = gs!().text.leading;
tm.next_line(0.0, -leading);
}
"Tj" => {
if let Some(bytes) = first_string(&op.operands) {
emit_string(
&bytes,
&mut tm,
gs!(),
&fonts,
&geom,
page_rotation,
&mut out,
);
}
}
"'" => {
let leading = gs!().text.leading;
tm.next_line(0.0, -leading);
if let Some(bytes) = first_string(&op.operands) {
emit_string(
&bytes,
&mut tm,
gs!(),
&fonts,
&geom,
page_rotation,
&mut out,
);
}
}
"\"" => {
if op.operands.len() == 3 {
let t = &mut gs_mut!().text;
t.word_space = num_of(&op.operands[0]).unwrap_or(0.0);
t.char_space = num_of(&op.operands[1]).unwrap_or(0.0);
}
let leading = gs!().text.leading;
tm.next_line(0.0, -leading);
if let Some(bytes) = op.operands.last().and_then(string_bytes) {
emit_string(
&bytes,
&mut tm,
gs!(),
&fonts,
&geom,
page_rotation,
&mut out,
);
}
}
"TJ" => {
if let Some(Object::Array(arr)) = op.operands.first() {
for item in arr {
match item {
Object::String(bytes, _) => emit_string(
bytes,
&mut tm,
gs!(),
&fonts,
&geom,
page_rotation,
&mut out,
),
Object::Integer(_) | Object::Real(_) => {
let t = &gs!().text;
let adj = num_of(item).unwrap_or(0.0);
let tx = -(adj / 1000.0) * t.font_size * t.h_scale;
tm.advance(tx);
}
_ => {}
}
}
}
}
_ => { }
}
}
Ok(out)
}
fn matrix_from_operands(operands: &[Object]) -> Option<Matrix> {
if operands.len() < 6 {
return None;
}
let n = |i: usize| num_of(&operands[i]);
Some(Matrix::new(n(0)?, n(1)?, n(2)?, n(3)?, n(4)?, n(5)?))
}
fn num_of(obj: &Object) -> Option<f32> {
match obj {
Object::Integer(i) => Some(*i as f32),
Object::Real(r) => Some(*r),
_ => None,
}
}
fn first_num(operands: &[Object]) -> Option<f32> {
operands.first().and_then(num_of)
}
fn name_of(obj: &Object) -> Option<Vec<u8>> {
match obj {
Object::Name(n) => Some(n.clone()),
_ => None,
}
}
fn string_bytes(obj: &Object) -> Option<Vec<u8>> {
match obj {
Object::String(b, _) => Some(b.clone()),
_ => None,
}
}
fn first_string(operands: &[Object]) -> Option<Vec<u8>> {
operands.first().and_then(string_bytes)
}
struct PageGeometry {
height: f32,
doctop_offset: f32,
}
#[derive(Debug, Clone)]
struct TextParams {
font_name: Vec<u8>,
font_size: f32,
char_space: f32,
word_space: f32,
h_scale: f32,
leading: f32,
rise: f32,
}
impl Default for TextParams {
fn default() -> Self {
Self {
font_name: Vec::new(),
font_size: 1.0,
char_space: 0.0,
word_space: 0.0,
h_scale: 1.0,
leading: 0.0,
rise: 0.0,
}
}
}
#[derive(Debug, Clone, Default)]
struct GraphicsState {
ctm: Matrix,
text: TextParams,
}
#[derive(Debug, Clone, Copy)]
struct TextMatrices {
tm: Matrix,
tlm: Matrix,
}
impl Default for TextMatrices {
fn default() -> Self {
Self {
tm: Matrix::IDENTITY,
tlm: Matrix::IDENTITY,
}
}
}
impl TextMatrices {
fn set_line(&mut self, m: Matrix) {
self.tm = m;
self.tlm = m;
}
fn next_line(&mut self, tx: f32, ty: f32) {
let new = Matrix::translation(tx, ty).then(self.tlm);
self.set_line(new);
}
fn advance(&mut self, tx: f32) {
self.tm = Matrix::translation(tx, 0.0).then(self.tm);
}
}
#[derive(Default)]
struct FontTable<'doc> {
by_name: HashMap<Vec<u8>, FontInfo<'doc>>,
}
impl<'doc> FontTable<'doc> {
fn insert(&mut self, name: Vec<u8>, info: FontInfo<'doc>) {
self.by_name.insert(name, info);
}
fn get(&self, name: &[u8]) -> Option<&FontInfo<'doc>> {
self.by_name.get(name)
}
}
struct FontInfo<'doc> {
encoding: Option<Encoding<'doc>>,
widths: Widths,
is_composite: bool,
differences: Option<crate::parser::fonts::differences::Differences>,
}
struct DecodedCode {
code: u32,
text: String,
}
impl FontInfo<'_> {
fn decode_codes(&self, bytes: &[u8]) -> Vec<DecodedCode> {
let chunk = if self.is_composite { 2 } else { 1 };
bytes
.chunks(chunk)
.map(|c| {
let code = c.iter().fold(0u32, |acc, &b| (acc << 8) | b as u32);
DecodedCode {
code,
text: self.decode_one(c, code),
}
})
.collect()
}
fn decode_one(&self, bytes: &[u8], code: u32) -> String {
if !self.is_composite
&& let Some(diffs) = &self.differences
&& let Ok(byte) = u8::try_from(code)
&& let Some(c) = diffs.get(&byte)
{
return c.to_string();
}
match &self.encoding {
Some(enc) => enc.bytes_to_string(bytes).unwrap_or_else(|_| latin1(bytes)),
None => latin1(bytes),
}
}
}
fn latin1(bytes: &[u8]) -> String {
bytes.iter().map(|&b| b as char).collect()
}
struct Widths {
by_code: HashMap<u32, f32>,
default_width: f32,
}
impl Widths {
fn empty() -> Self {
Self {
by_code: HashMap::new(),
default_width: 0.5,
}
}
fn width_of(&self, code: u32) -> f32 {
self.by_code
.get(&code)
.copied()
.unwrap_or(self.default_width)
}
}
impl<'doc> FontInfo<'doc> {
fn from_dict(doc: &'doc LDoc, dict: &'doc Dictionary) -> Self {
let encoding = dict.get_font_encoding(doc).ok();
let is_composite = matches!(
dict.get(b"Subtype").and_then(Object::as_name).ok(),
Some(b"Type0")
);
let widths = if is_composite {
crate::parser::fonts::widths::extract_type0(doc, dict)
.map(|by_code| Widths {
by_code,
default_width: 0.5,
})
.unwrap_or_else(Widths::empty)
} else {
simple_widths(dict).unwrap_or_else(Widths::empty)
};
let differences = crate::parser::fonts::differences::extract(doc, dict);
Self {
encoding,
widths,
is_composite,
differences,
}
}
}
fn simple_widths(dict: &Dictionary) -> Option<Widths> {
let first_char = dict.get(b"FirstChar").ok().and_then(|o| match o {
Object::Integer(i) => u8::try_from(*i).ok().map(u32::from),
_ => None,
})?;
let widths_arr = match dict.get(b"Widths").ok()? {
Object::Array(a) => a,
_ => return None,
};
let mut by_code = HashMap::with_capacity(widths_arr.len());
for (i, w) in widths_arr.iter().enumerate() {
let Some(code) = u32::try_from(i)
.ok()
.and_then(|i| first_char.checked_add(i))
else {
break;
};
if let Some(v) = num_of(w) {
by_code.insert(code, v / 1000.0);
}
}
let default_width = dict
.get(b"MissingWidth")
.ok()
.and_then(num_of)
.map(|v| v / 1000.0)
.unwrap_or(0.5);
Some(Widths {
by_code,
default_width,
})
}
fn emit_string(
bytes: &[u8],
tm: &mut TextMatrices,
gs: &GraphicsState,
fonts: &FontTable,
geom: &PageGeometry,
page_rotation: Matrix,
out: &mut Vec<Char>,
) {
let ctm = gs.ctm.then(page_rotation);
let ts = &gs.text;
let font = fonts.get(&ts.font_name);
let fontname_str = String::from_utf8_lossy(&ts.font_name).into_owned();
let fontname = CompactString::from(&fontname_str);
let is_composite = font.is_some_and(|f| f.is_composite);
let decoded: Vec<DecodedCode> = match font {
Some(f) => f.decode_codes(bytes),
None => bytes
.iter()
.map(|&b| DecodedCode {
code: b as u32,
text: (b as char).to_string(),
})
.collect(),
};
for DecodedCode { code, text } in decoded {
let w = font.map(|f| f.widths.width_of(code)).unwrap_or(0.5);
let glyph_width = w * ts.font_size;
let trm = tm.tm.then(ctm);
let origin = trm.transform(Point::new(0.0, ts.rise));
let upper = trm.transform(Point::new(0.0, ts.rise + ts.font_size));
let x_scale = trm.x_scale();
let effective_size = ts.font_size * trm.y_scale().max(x_scale);
let top = geom.height - upper.y;
let bottom = geom.height - origin.y;
let glyph_render_width = glyph_width * ts.h_scale * x_scale;
let x0 = origin.x;
let x1 = x0 + glyph_render_width;
let upright = trm.is_upright();
let (x0, x1) = if x0 <= x1 { (x0, x1) } else { (x1, x0) };
let (top, bottom) = if top <= bottom {
(top, bottom)
} else {
(bottom, top)
};
let visible: String = text.chars().filter(|c| !c.is_control()).collect();
if !visible.is_empty() {
out.push(Char {
text: CompactString::from(visible.as_str()),
x0,
x1,
top,
bottom,
doctop: top + geom.doctop_offset,
size: effective_size,
fontname: fontname.clone(),
upright,
});
}
let word_space = word_space_for(code, is_composite, ts);
let adv_t = (w * ts.font_size + ts.char_space + word_space) * ts.h_scale;
tm.advance(adv_t);
}
}
fn word_space_for(code: u32, is_composite: bool, ts: &TextParams) -> f32 {
if is_composite {
return 0.0;
}
if code == 32 { ts.word_space } else { 0.0 }
}