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()
|
||||
}
|
||||
|
||||
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 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 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 id = glyph.id().0;
|
||||
// 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
|
||||
};
|
||||
let width = metrics.width(character) * 1000 / (10 * 65_536);
|
||||
entries.push((id, width));
|
||||
}
|
||||
entries.sort_unstable();
|
||||
@@ -373,8 +364,8 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
||||
);
|
||||
objects.push(
|
||||
format!(
|
||||
"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{base} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor {descriptor_id} 0 R /DW 1000 /W {} >>",
|
||||
cid_widths(face)
|
||||
"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{base} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor {descriptor_id} 0 R /DW 500 /W {} >>",
|
||||
cid_widths(face, &used_chars[slot])
|
||||
)
|
||||
.into_bytes(),
|
||||
);
|
||||
@@ -557,7 +548,19 @@ pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec<u8> {
|
||||
|
||||
#[cfg(test)]
|
||||
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]
|
||||
fn latin1_text_is_octal_escaped_in_the_content_stream() {
|
||||
|
||||
Reference in new issue
Block a user