Implement native automatic font expansion for paragraphs

This commit is contained in:
gloamxun committed 2026-11-20 23:29:37 +08:00
1 parent 1dd745afcb
commit ebbc9e2c17
24 files changed
+632 -34

No files matched your search

+25 -2
View File
@@ -7,6 +7,7 @@ use crate::{
pub(super) struct NativeText {
font: Option<FontId>,
effective_size: i64,
expansion: i32,
v: i64,
start_h: i64,
delta_h: i64,
@@ -30,6 +31,7 @@ impl NativeText {
Self {
font: None,
effective_size: 0,
expansion: 0,
v: height,
start_h: 0,
delta_h: 0,
@@ -47,8 +49,21 @@ impl NativeText {
print_decimal(number, 4)
));
}
if state.expansion != font.expansion {
state.close_string(out);
state.expansion = font.expansion;
out.push_str(&format!(
"{} Tz\n",
print_decimal(i64::from(1000 + font.expansion), 1)
));
}
let horizontal_size = if font.expansion == 0 {
state.effective_size
} else {
crate::pdf_device::magnify(state.effective_size, 1000 + font.expansion)
};
let (mut shift, shift_out) =
divide_scaled(x - state.start_h - state.delta_h, state.effective_size, 3);
divide_scaled(x - state.start_h - state.delta_h, horizontal_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)
@@ -77,7 +92,7 @@ impl NativeText {
state.delta_h += shift_out;
}
out.push_str(&format!("<{slot:02X}>"));
state.delta_h += match &paint.program {
let advance = match &paint.program {
crate::native_font::NativeFontProgram::Pk(pk) => pk.advances[&slot],
crate::native_font::NativeFontProgram::Type1(_) => {
divide_scaled(
@@ -88,6 +103,11 @@ impl NativeText {
.1
}
};
state.delta_h += if font.expansion == 0 {
advance
} else {
crate::pdf_device::magnify(advance, 1000 + font.expansion)
};
}
fn close_string(&mut self, out: &mut String) {
if self.opened {
@@ -98,6 +118,9 @@ impl NativeText {
pub(super) fn finish(text: &mut Option<Self>, out: &mut String) {
if let Some(mut text) = text.take() {
text.close_string(out);
if text.expansion != 0 {
out.push_str("100 Tz\n");
}
out.push_str("ET\n");
}
}