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

+20 -8
View File
@@ -441,6 +441,7 @@ fn native_pdf_font_size(size: i64) -> i64 {
struct NativeTextRun {
font: crate::execution::FontId,
expansion: i32,
size: i64,
y: i64,
cursor: f64,
@@ -451,7 +452,11 @@ impl NativeTextRun {
fn flush(run: &mut Option<Self>, content: &mut String) {
if let Some(run) = run.take() {
content.push_str(&run.text);
content.push_str("] TJ ET\n");
if run.expansion == 0 {
content.push_str("] TJ ET\n");
} else {
content.push_str("] TJ 100 Tz ET\n");
}
}
}
fn push(&mut self, x: f64, slot: u8, face: &crate::native_font::NativeFont) {
@@ -682,7 +687,7 @@ fn page_content(
}
if let Some(run) = &native_text {
if run.point_size == 0.0
|| !matches!(item,DisplayItem::NativeGlyph{font,size,y,..}if font.id==run.font && *size==run.size && *y==run.y)
|| !matches!(item,DisplayItem::NativeGlyph{font,size,y,..}if font.id==run.font && font.expansion==run.expansion && *size==run.size && *y==run.y)
{
NativeTextRun::flush(&mut native_text, &mut content);
}
@@ -704,14 +709,22 @@ fn page_content(
let run = native_text.get_or_insert_with(|| {
let point_size =
native_font_units(font.size_sp(), 6_578_176, 1_000_000) as f64 / 10_000.0;
let spacing = if font.expansion == 0 {
String::new()
} else {
format!("{} Tz ", crate::pdf_device::print_decimal(
i64::from(1000 + font.expansion), 1,
))
};
NativeTextRun {
font: font.id,
expansion: font.expansion,
size: *size,
y: *y,
cursor: pt(*x).parse().unwrap(),
point_size,
point_size: point_size * f64::from(1000 + font.expansion) / 1000.0,
text: format!(
"BT /N{} {point_size:.4} Tf 1 0 0 1 {} {} Tm [",
"BT /N{} {point_size:.4} Tf {spacing}1 0 0 1 {} {} Tm [",
font.id.index(),
pt(*x),
pt(height - *y)
@@ -1218,7 +1231,7 @@ fn native_pdf(
return Err(format!("native font {} has no output encoding for slot {slot}",font.name));
}
if let Some((old_font,old_paint))=fonts.insert(font.id,(font,paint)) {
if old_font != font || old_paint != paint {
if !old_font.same_base(font) || old_paint != paint {
return Err("inconsistent native font snapshots or programs in PDF output".into());
}
}
@@ -1794,9 +1807,8 @@ fn write_pdf(
}
(font.clone(), paint.clone(), BTreeMap::new())
});
assert_eq!(
entry.0.as_ref(),
font.as_ref(),
assert!(
entry.0.same_base(font),
"native font ids from different jobs cannot share a PDF"
);
entry.2.insert(*slot, *character);