Report matching PDF widths for accented glyphs
This commit is contained in:
1 parent
3d94e28dff
commit
c6a58dd646
3 files changed
+68
-21
No files matched your search
@@ -176,28 +176,19 @@ fn tounicode_cmap(face: &'static PdfFace, characters: &BTreeSet<char>) -> Vec<u8
|
|||||||
map.into_bytes()
|
map.into_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cid_widths(face: &'static PdfFace) -> String {
|
fn cid_widths(face: &'static PdfFace, used: &std::collections::BTreeSet<char>) -> String {
|
||||||
let font = face_font(face);
|
let font = face_font(face);
|
||||||
let units = font.units_per_em() as f32;
|
|
||||||
let scale = 1000.0 / units;
|
|
||||||
let metrics = Cmr10Metrics::new();
|
let metrics = Cmr10Metrics::new();
|
||||||
let mut entries = Vec::new();
|
let mut entries = Vec::new();
|
||||||
for character in (0x20u8..=0x7e).map(char::from) {
|
let mut characters = used.clone();
|
||||||
|
characters.extend((0x20u8..=0x7e).map(char::from));
|
||||||
|
for character in characters {
|
||||||
|
if character.is_control() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let glyph = font.glyph(character);
|
let glyph = font.glyph(character);
|
||||||
let id = glyph.id().0;
|
let id = glyph.id().0;
|
||||||
// The layout positions every glyph with CMR10 advances, independent of
|
let width = metrics.width(character) * 1000 / (10 * 65_536);
|
||||||
// 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.push((id, width));
|
||||||
}
|
}
|
||||||
entries.sort_unstable();
|
entries.sort_unstable();
|
||||||
@@ -373,8 +364,8 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
|||||||
);
|
);
|
||||||
objects.push(
|
objects.push(
|
||||||
format!(
|
format!(
|
||||||
"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{base} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor {descriptor_id} 0 R /DW 1000 /W {} >>",
|
"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{base} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor {descriptor_id} 0 R /DW 500 /W {} >>",
|
||||||
cid_widths(face)
|
cid_widths(face, &used_chars[slot])
|
||||||
)
|
)
|
||||||
.into_bytes(),
|
.into_bytes(),
|
||||||
);
|
);
|
||||||
@@ -557,7 +548,19 @@ pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::escape;
|
use super::{FULL_FACES, cid_widths, escape, face_font};
|
||||||
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn widths_cover_used_latin1_characters() {
|
||||||
|
let face = &FULL_FACES[0];
|
||||||
|
let accent = char::from_u32(0xe9).unwrap();
|
||||||
|
let id = face_font(face).glyph(accent).id().0;
|
||||||
|
let mut used = BTreeSet::new();
|
||||||
|
used.insert(accent);
|
||||||
|
let widths = cid_widths(face, &used);
|
||||||
|
assert!(widths.contains(&format!("{id} [500]")), "{widths}");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn latin1_text_is_octal_escaped_in_the_content_stream() {
|
fn latin1_text_is_octal_escaped_in_the_content_stream() {
|
||||||
|
|||||||
@@ -366,3 +366,47 @@ Second page text\par
|
|||||||
}
|
}
|
||||||
let _ = std::fs::remove_dir_all(&root);
|
let _ = std::fs::remove_dir_all(&root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn accented_text_round_trips_through_pdf_extraction() {
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
if Command::new("pdftotext")
|
||||||
|
.arg("-v")
|
||||||
|
.stdout(std::process::Stdio::null())
|
||||||
|
.stderr(std::process::Stdio::null())
|
||||||
|
.status()
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let document = Document::new(
|
||||||
|
60,
|
||||||
|
vec![Unit::paragraph_with_style(
|
||||||
|
1,
|
||||||
|
"caf\u{e9} na\u{ef}ve r\u{e9}sum\u{e9}",
|
||||||
|
ParagraphStyle::default(),
|
||||||
|
)],
|
||||||
|
);
|
||||||
|
let engine = Engine::layout(&document);
|
||||||
|
let lists: Vec<_> = engine
|
||||||
|
.pages()
|
||||||
|
.iter()
|
||||||
|
.map(|page| page.display_list())
|
||||||
|
.collect();
|
||||||
|
let pdf = display_lists_to_pdf(&lists, 792);
|
||||||
|
let path = std::env::temp_dir().join(format!("texrra-accents-{}.pdf", std::process::id()));
|
||||||
|
std::fs::write(&path, &pdf).expect("write PDF");
|
||||||
|
let output = Command::new("pdftotext")
|
||||||
|
.arg(&path)
|
||||||
|
.arg("-")
|
||||||
|
.output()
|
||||||
|
.expect("pdftotext");
|
||||||
|
let _ = std::fs::remove_file(&path);
|
||||||
|
let text = String::from_utf8_lossy(&output.stdout);
|
||||||
|
assert!(
|
||||||
|
text.contains("caf\u{e9} na\u{ef}ve r\u{e9}sum\u{e9}"),
|
||||||
|
"got {text:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
+1
-1
@@ -270,7 +270,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
|
|||||||
|
|
||||||
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
|
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
|
||||||
|
|
||||||
The crate currently has 164 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 264 tests (164 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path.
|
The crate currently has 165 unit tests plus four route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 261 tests (165 route-D unit, 4 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path.
|
||||||
|
|
||||||
### M0 incremental demo
|
### M0 incremental demo
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user