Use CMR10 advances for route-D paragraphs and PDF text

This commit is contained in:
gloamxun committed 2026-10-20 22:55:38 +08:00
1 parent 8251e35de2
commit 67915402a6
7 files changed
+45 -19

No files matched your search

+22 -5
View File
@@ -461,7 +461,8 @@ impl Unit {
style: ParagraphStyle,
) -> Self {
let source = source.into();
let paragraph = layout_paragraph(&source, &style);
let paragraph =
layout_paragraph_with_font(&source, &style, &crate::cmr10::Cmr10Metrics::new());
Self {
id,
source,
@@ -1353,15 +1354,16 @@ mod tests {
let mut document = Document::new(
100,
vec![
Unit::paragraph(1, "a ".repeat(200)),
Unit::paragraph(2, "b ".repeat(200)),
Unit::paragraph(1, "a ".repeat(20)),
Unit::vertical(2, vec![VerticalItem::Penalty(-10_000)]),
Unit::paragraph(3, "b ".repeat(200)),
],
);
let mut engine = Engine::layout(&document);
let before = engine.pages()[0].hash;
engine.edit(&document, 2, "c ".repeat(200));
engine.edit(&document, 3, "c ".repeat(200));
assert_eq!(engine.pages()[0].hash, before);
document.units[1].source = "c ".repeat(200);
document.units[2].source = "c ".repeat(200);
}
#[test]
@@ -1598,6 +1600,21 @@ mod tests {
assert_eq!(before, after);
}
#[test]
fn paragraph_widths_come_from_cmr10_not_fixed_cells() {
let width = |source: &str| {
let unit = Unit::paragraph(1, source);
match &unit.items[0] {
VerticalItem::Paragraph(paragraph) => paragraph.width,
other => panic!("expected paragraph, got {other:?}"),
}
};
assert!(
width("mmm") > width("iii"),
"Computer Modern m is wider than i"
);
}
#[test]
fn tall_insertions_split_across_pages() {
let document = Document::new(
+15 -6
View File
@@ -8,6 +8,7 @@ use std::sync::OnceLock;
use rusttype::Font;
use crate::cmr10::Cmr10Metrics;
use crate::render::{DisplayItem, DisplayList, FontFace};
struct PdfFace {
@@ -160,16 +161,24 @@ fn cid_widths(face: &'static PdfFace) -> String {
let font = face_font(face);
let units = font.units_per_em() as f32;
let scale = 1000.0 / units;
let metrics = Cmr10Metrics::new();
let mut entries = Vec::new();
for character in (0x20u8..=0x7e).map(char::from) {
let glyph = font.glyph(character);
let id = glyph.id().0;
let width = (glyph
.scaled(rusttype::Scale::uniform(units))
.h_metrics()
.advance_width
* scale)
.round() as i64;
// The layout positions every glyph with CMR10 advances, independent of
// the face used for painting. Reporting the CMR10 advance in `/W` keeps
// PDF text extraction from inserting spaces between letters.
let width = if character.is_ascii() {
metrics.width(character) * 1000 / (10 * 65_536)
} else {
(glyph
.scaled(rusttype::Scale::uniform(units))
.h_metrics()
.advance_width
* scale)
.round() as i64
};
entries.push((id, width));
}
entries.sort_unstable();
+2 -2
View File
@@ -280,7 +280,7 @@ impl Halign {
font: FontFace::Roman,
x,
y: baseline,
size: self.line_height * 5 / 6,
size: 10 * 65_536,
text: cell.text.clone(),
});
}
@@ -375,7 +375,7 @@ impl Page {
};
let lines = paragraph.lines.len().max(1) as i64;
let line_height = (paragraph.height / lines).max(1);
let size = ((line_height as f64) * 0.7) as i64;
let size = 10 * 65_536;
for line in &paragraph.lines {
let baseline = y + ((line_height as f64) * 0.8) as i64;
let natural: i64 = line