Merge upstream/main into fix/symbolic-truetype-glyph-names-3962

Main's #6569 (hayro-interpret patch 101) reads a symbolic TrueType font that
names no glyphs and keeps the Macintosh order as Mac OS Roman; this branch's
patch (79) reads one that names its glyphs by those names. Both are kept: the
renderer struct holds both fields, and the editor's unicode_map applies the
glyph-name reading only to a font whose post table names glyphs, then the Mac
OS Roman one, so each font takes the reading the renderer gives it. The ledger
keeps main's rows with (79) added; the editor reuses descriptor_flags and
MAX_SFNT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
This commit is contained in:
mtlcl410andClaude Opus 5.5 committed 2026-11-09 11:15:40 +08:00
commit d130c4f4de
45 files changed
+4179 -281

No files matched your search

-4
View File
@@ -53,10 +53,6 @@ pub struct DocInfo {
pub keywords: Option<String>,
pub creator: Option<String>,
pub producer: Option<String>,
/// `/Info /CreationDate` and `/ModDate` for display ("2026-10-01 12:30", see [`pretty_date`]);
/// a value that isn't a PDF date is shown as written.
pub created: Option<String>,
pub modified: Option<String>,
/// Custom document properties: `/Info` entries with a key the spec does not name and a text
/// value, as (name, text), in the dictionary's order (at most 256).
pub custom: Vec<(String, String)>,
-8
View File
@@ -188,14 +188,6 @@ impl<'a> Inspector<'a> {
info.keywords = self.text(d, b"Keywords");
info.creator = self.text(d, b"Creator");
info.producer = self.text(d, b"Producer");
// Dates are text strings only (ISO 32000-2 §7.9.4), as the engine reads them after an
// edit (`pdfcraft_organize::info`): a name value is no date.
let date = |key: &[u8]| match self.resolve(d.get(key).ok()?) {
Object::String(..) => self.text(d, key).map(|s| pretty_date(&s)),
_ => None,
};
info.created = date(b"CreationDate");
info.modified = date(b"ModDate");
const STANDARD: [&[u8]; 9] =
[b"Title", b"Author", b"Subject", b"Keywords", b"Creator", b"Producer", b"CreationDate", b"ModDate", b"Trapped"];
// The same rule as the engine's list after an edit (`pdfcraft_organize::custom_info`,
-25
View File
@@ -729,31 +729,6 @@ const CUSTOM_INFO: &[u8] = b"%PDF-1.7
trailer << /Root 1 0 R /Info 4 0 R >>
%%EOF";
/// One page whose `/Info` is `info` (the dictionary's contents).
fn with_info(info: &str) -> Vec<u8> {
format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 200 200] >> endobj
3 0 obj << /Type /Page /Parent 2 0 R >> endobj
4 0 obj << {info} >> endobj
trailer << /Root 1 0 R /Info 4 0 R >>
%%EOF"
)
.into_bytes()
}
#[test]
fn creation_and_modification_dates_are_read_for_document_properties() {
let info = inspect(Arc::new(with_info("/CreationDate (D:20240105093000+01'00') /ModDate (D:20261031174500Z)")), None).expect("opens");
assert_eq!((info.created.as_deref(), info.modified.as_deref()), (Some("2024-01-05 09:30"), Some("2026-10-31 17:45")));
// Not a PDF date: shown as written. Empty, a number, a name and absent: no date.
let info = inspect(Arc::new(with_info("/CreationDate (last Tuesday) /ModDate ()")), None).expect("opens");
assert_eq!((info.created.as_deref(), info.modified.as_deref()), (Some("last Tuesday"), None));
let info = inspect(Arc::new(with_info("/CreationDate 20240105 /ModDate /D:20240105093000Z")), None).expect("opens");
assert_eq!((info.created, info.modified), (None, None));
}
#[test]
fn custom_document_properties_are_listed_apart_from_the_standard_ones() {
let info = inspect(Arc::new(CUSTOM_INFO.to_vec()), None).expect("opens");
+4
View File
@@ -270,6 +270,9 @@ pub enum RenderWarning {
/// A colour could not be computed (a colour space that cannot be read, or a Separation or
/// DeviceN tint transform that cannot be evaluated) and was painted in a fallback colour.
ColourFallback,
/// A Type 3 font without the `/Widths` it requires (ISO 32000-2 Table 110) was used; its
/// glyphs were spaced by the widths their glyph procedures give.
Type3WidthsMissing,
}
/// Counters for work performed by a renderer or render pool.
@@ -478,6 +481,7 @@ fn warning_settings(settings: &InterpreterSettings, warnings: Arc<Mutex<Vec<Rend
let warning = match warning {
InterpreterWarning::ContentTruncated => RenderWarning::ContentTruncated,
InterpreterWarning::ColorFallback => RenderWarning::ColourFallback,
InterpreterWarning::Type3WidthsMissing => RenderWarning::Type3WidthsMissing,
InterpreterWarning::UnsupportedFont | InterpreterWarning::ImageDecodeFailure => return,
};
let mut warnings = lock(&target);
+641 -3
View File
@@ -5689,6 +5689,22 @@ fn a_font_whose_post_version_has_swapped_halves_is_still_used() {
assert_eq!(alternate([0, 0, 0, 2]), alternate([0, 2, 0, 0]), "and also with swapped halves");
}
/// A font dictionary whose `/Subtype` is missing or the empty name is read as a Type 1 font,
/// so its `/BaseFont` picks the face (vendored hayro-interpret patch 94). `/Subtype` is required
/// (ISO 32000-2 Table 109), but such a `/Courier-Bold` was drawn in the Helvetica fallback,
/// shifting its columns (PDFBOX-1988).
#[test]
fn a_font_without_a_subtype_is_drawn_by_its_base_font() {
let render = |subtype: &str, base_font: &str| {
let resources = format!("<< /Font << /F1 << /Type /Font {subtype} /BaseFont /{base_font} >> >> >>");
render_first_page(image_cache_pdf("BT /F1 20 Tf 5 25 Td (iiiiii) Tj ET", &resources, &[])).rgba.to_vec()
};
let courier = render("/Subtype /Type1", "Courier-Bold");
assert_ne!(courier, render("/Subtype /Type1", "Helvetica"), "Courier Bold and Helvetica differ");
assert_eq!(render("/Subtype /", "Courier-Bold"), courier, "an empty /Subtype");
assert_eq!(render("", "Courier-Bold"), courier, "no /Subtype");
}
/// A page with no `/MediaBox`, its own or inherited, is US Letter (612 × 792 pt), as other
/// readers draw it and as the page model and inspector already sized it; it was A4 (vendored
/// hayro-syntax patch 44; ISO 32000-2 Table 31 makes the box required and gives no default;
@@ -6604,6 +6620,94 @@ trailer << /Root 1 0 R >>
assert!(p != [255, 255, 255, 255] && p[3] == 255, "the triangles are drawn: {p:?}");
}
/// A rainbow of tensor-product patches (two quarter rings per band) painted as a pattern
/// through a rectangle that is just the rainbow's box. Every patch is 722 triangles, and
/// across a narrow curved band they are thin and lie at every angle, so their boxes hold
/// many times their area: walking those boxes spent the visit budget (eight per pixel of the
/// rectangle, four per triangle) before the last patches, and the inner bands were left white
/// (through the page-sized rectangle, or with `sh`, the same mesh drew whole). The sampler
/// now walks only the columns of each row a triangle can reach. Vendored hayro-interpret
/// patch 99.
#[test]
fn thin_patches_of_a_mesh_in_a_tight_path_are_all_painted() {
const K: f64 = 0.5523; // a quarter circle's Bézier handles, as a fraction of the radius
const BANDS: [[u8; 3]; 6] = [[255, 0, 0], [255, 128, 0], [255, 255, 0], [0, 200, 0], [0, 0, 255], [128, 0, 255]];
// The stream order of a tensor patch's points (column i, row j), ISO 32000-2 8.7.4.5.8.
const ORDER: [(usize, usize); 16] =
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 3), (2, 3), (3, 3), (3, 2), (3, 1), (3, 0), (2, 0), (1, 0), (1, 1), (1, 2), (2, 2), (2, 1)];
// Bands 20/6 pt wide from radius 50 inwards, centred on (100, 100) by the pattern matrix;
// 16-bit coordinates decoded onto -64..64.
let width = 20.0 / BANDS.len() as f64;
let mut data = Vec::new();
for (band, colour) in BANDS.iter().enumerate() {
let (outer, inner) = (50.0 - band as f64 * width, 50.0 - (band + 1) as f64 * width);
for side in [1.0, -1.0] {
data.push(0u8);
for (i, j) in ORDER {
let rho = inner + (outer - inner) * j as f64 / 3.0;
let (x, y) = [(side * rho, 0.0), (side * rho, K * rho), (side * K * rho, rho), (0.0, rho)][i];
for v in [x, y] {
data.extend_from_slice(&(((v + 64.0) / 128.0 * 65535.0).round() as u16).to_be_bytes());
}
}
for _ in 0..4 {
data.extend_from_slice(colour);
}
}
}
let render = |rect: &str| {
let content = format!("/Pattern cs /P0 scn {rect} re f");
let mut pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R /Resources << /Pattern << /P0 6 0 R >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
6 0 obj << /Type /Pattern /PatternType 2 /Shading 5 0 R /Matrix [1 0 0 1 100 100] >> endobj
5 0 obj << /ShadingType 7 /ColorSpace /DeviceRGB /BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8 /Decode [-64 64 -64 64 0 1 0 1 0 1] /Length {} >> stream
",
content.len(),
data.len()
)
.into_bytes();
pdf.extend_from_slice(&data);
pdf.extend_from_slice(
b"
endstream endobj
trailer << /Root 1 0 R >>
%%EOF
",
);
let mut r = PageRenderer::new(Arc::new(pdf), RenderConfig::default());
let page = r.render(RenderRequest { page: 0, kind: RequestKind::Pixels, tile: None, scale: 1.5, tag: 0, ..Default::default() });
assert!(page.error.is_none(), "{:?}", page.error);
assert_eq!((page.width, page.height), (300, 300));
page
};
// At 45° on either side, the middle of the outer and of the inner band, in device pixels
// (scale 1.5, y down).
let at = |rho: f64, side: f64| {
let d = rho * std::f64::consts::FRAC_1_SQRT_2;
(((100.0 + side * d) * 1.5) as usize, ((100.0 - d) * 1.5) as usize)
};
for (what, rect) in [("the rainbow's box", "50 100 100 50"), ("the page", "0 0 200 200")] {
let page = render(rect);
for (band, rho) in [(0, 50.0 - width / 2.0), (BANDS.len() - 1, 30.0 + width / 2.0)] {
for side in [1.0, -1.0] {
let (x, y) = at(rho, side);
let got = rgb_at(&page.rgba, 300, x, y);
let want = BANDS[band];
assert!(
got.iter().zip(want).all(|(g, w)| g.abs_diff(w) <= 8),
"through {what}, band {band} at ({x}, {y}): got {got:?}, want {want:?}"
);
}
}
}
}
/// Two triangles of a mesh that share an edge cover it together. The sampler tested each
/// pixel's corner, and only strictly inside a triangle, so the diagonal two triangles share
/// was left white wherever it ran through the samples, and so was a pixel row or column
@@ -7963,6 +8067,329 @@ trailer << /Root 1 0 R >>
}
}
/// A page of `/ImageMask` stencils painted with a tiling pattern: the 4 × 4 stencil inks a ring
/// (its centre 2 × 2 samples are `1`, unpainted under the default `/Decode [0 1]`), filled with
/// a red coloured pattern.
fn patterned_stencil_page(media: (u32, u32), content: &str) -> RenderedPage {
let tile = "1 0 0 rg 0 0 10 10 re f";
let pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 {} {}] /Contents 4 0 R /Resources << /XObject << /Msk 5 0 R >> /Pattern << /P 6 0 R >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /XObject /Subtype /Image /Width 4 /Height 4 /ImageMask true /Filter /ASCIIHexDecode /Length 9 >> stream
00606000>
endstream endobj
6 0 obj << /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Resources << >> /Length {} >> stream
{tile}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
media.0,
media.1,
content.len(),
tile.len()
);
let page = PageRenderer::new(Arc::new(pdf.into_bytes()), RenderConfig::default()).render(RenderRequest {
page: 0,
kind: RequestKind::Pixels,
scale: 1.0,
..Default::default()
});
assert!(page.error.is_none(), "{:?}", page.error);
page
}
/// A stencil mask filled with a pattern shows the pattern where the stencil inks and nothing
/// elsewhere: not in its unpainted samples, and not around it, also when it is drawn smaller
/// than its samples and lands on whole device pixels.
#[test]
fn a_patterned_stencil_mask_paints_only_where_it_inks() {
// Pixel rows run top down: PDF (x, y) is pixel (x, 200 - y).
let px = |p: &RenderedPage, x: u32, y: u32| p.rgba[(((200 - y) * p.width + x) * 4) as usize..][..3].to_vec();
let page = patterned_stencil_page((200, 200), "/Pattern cs /P scn q 40 0 0 40 20 20 cm /Msk Do Q q 3 0 0 3 100.4 100.4 cm /Msk Do Q");
assert_eq!(px(&page, 25, 25), [255, 0, 0], "the ring is red");
assert_eq!(px(&page, 40, 40), [255, 255, 255], "the stencil's unpainted centre stays white");
for (x, y) in [(15, 40), (65, 40), (40, 15), (40, 65)] {
assert_eq!(px(&page, x, y), [255, 255, 255], "outside the stencil at ({x}, {y})");
}
assert!((100..104).any(|x| px(&page, x, 101) != [255, 255, 255]), "the shrunk stencil is drawn");
for edge in [97, 98, 106, 107] {
for along in 97..=107 {
assert_eq!(px(&page, edge, along), [255, 255, 255], "beside the shrunk stencil at ({edge}, {along})");
assert_eq!(px(&page, along, edge), [255, 255, 255], "above or below the shrunk stencil at ({along}, {edge})");
}
}
}
/// A patterned stencil mask under a soft mask is drawn at the soft mask's opacity once, as a
/// rectangle filled with the pattern beside it is: the stencil's coverage, which cuts the pattern
/// to the stencil's shape, is not masked a second time.
#[test]
fn a_patterned_stencil_mask_under_a_soft_mask_is_masked_once() {
let content = "/S gs /Pattern cs /P scn q 40 0 0 40 20 20 cm /Msk Do Q 120 20 40 40 re f";
let tile = "1 0 0 rg 0 0 10 10 re f";
let group = "0.5 g 0 0 200 200 re f";
let pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R /Resources << /XObject << /Msk 5 0 R >> /Pattern << /P 6 0 R >> /ExtGState << /S << /SMask << /S /Luminosity /G 7 0 R >> >> >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /XObject /Subtype /Image /Width 4 /Height 4 /ImageMask true /Filter /ASCIIHexDecode /Length 9 >> stream
00606000>
endstream endobj
6 0 obj << /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Resources << >> /Length {} >> stream
{tile}
endstream endobj
7 0 obj << /Type /XObject /Subtype /Form /BBox [0 0 200 200] /Group << /S /Transparency /CS /DeviceGray >> /Length {} >> stream
{group}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
content.len(),
tile.len(),
group.len()
);
let page = PageRenderer::new(Arc::new(pdf.into_bytes()), RenderConfig::default()).render(RenderRequest {
page: 0,
kind: RequestKind::Pixels,
scale: 1.0,
..Default::default()
});
assert!(page.error.is_none(), "{:?}", page.error);
// Pixel rows run top down: PDF (x, y) is pixel (x, 200 - y).
let px = |x: u32, y: u32| page.rgba[(((200 - y) * page.width + x) * 4) as usize..][..3].to_vec();
let (ring, rect) = (px(25, 25), px(140, 40));
assert!(rect != [255, 255, 255] && rect != [255, 0, 0], "the rectangle is half masked: {rect:?}");
assert!(ring.iter().zip(&rect).all(|(a, b)| a.abs_diff(*b) <= 2), "stencil {ring:?}, rectangle {rect:?}");
assert_eq!(px(40, 40), [255, 255, 255], "the stencil's unpainted centre stays white");
}
/// A patterned stencil mask in a Type 3 glyph is shrunk by area averaging, as it is outside
/// one (#4800): the glyph's own image rules are for its images, not for the coverage that cuts
/// the pattern. Here a one-column stroke of an 8-column stencil, drawn 4 pixels wide, is a
/// half-covered red; nearest-neighbour would read columns 1, 3, 5 and 7 and miss it.
#[test]
fn a_patterned_stencil_mask_in_a_type3_glyph_keeps_thin_strokes() {
let content = "BT /Pattern cs /P scn /T3 1 Tf 0 0 Td (a) Tj ET";
let glyph = "400 0 0 0 400 100 d1 q 400 0 0 100 0 0 cm /Im0 Do Q";
let tile = "1 0 0 rg 0 0 10 10 re f";
let pdf = format!(
"%PDF-1.4
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 4 1] /Contents 4 0 R /Resources << /Font << /T3 6 0 R >> /Pattern << /P 8 0 R >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /XObject /Subtype /Image /Width 8 /Height 1 /ImageMask true /Filter /ASCIIHexDecode /Length 3 >> stream
DF>
endstream endobj
6 0 obj << /Type /Font /Subtype /Type3 /FontBBox [0 0 400 100] /FontMatrix [0.01 0 0 0.01 0 0] /CharProcs << /a 7 0 R >> /Encoding << /Type /Encoding /Differences [97 /a] >> /FirstChar 97 /LastChar 97 /Widths [400] /Resources << /XObject << /Im0 5 0 R >> >> >> endobj
7 0 obj << /Length {} >> stream
{glyph}
endstream endobj
8 0 obj << /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Resources << >> /Length {} >> stream
{tile}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
content.len(),
glyph.len(),
tile.len()
);
let page = PageRenderer::new(Arc::new(pdf.into_bytes()), RenderConfig::default()).render(RenderRequest {
page: 0,
kind: RequestKind::Pixels,
scale: 1.0,
..Default::default()
});
assert!(page.error.is_none(), "{:?}", page.error);
assert_eq!((page.width, page.height), (4, 1));
let px = page.rgba.as_chunks::<4>().0.to_vec();
let [r, g, b, _] = px.get(1).copied().unwrap_or_default();
assert!(r >= 254 && g.abs_diff(128) <= 1 && b == g, "half-covered red over white, got {px:?}");
assert_eq!(px.get(3), Some(&[255, 255, 255, 255]), "{px:?}");
}
/// pdf.js issue 8078 paints 73,047 small stencil masks with one tiling pattern. Each was drawn
/// through a canvas-sized luminance mask (a full-page render and composite per stencil), which
/// took minutes for the page; a stencil now costs what its rectangle covers. Here 5,000
/// patterned stencils on a 1000 × 2000 page. What the page draws offscreen is counted (hayro
/// patch 30) rather than timed, so the check is the same on every machine: the one pattern cell,
/// and not a page-sized pixmap per stencil.
#[test]
fn many_patterned_stencil_masks_draw_no_page_sized_masks() {
let mut content = String::from("/Pattern cs /P scn ");
for i in 0..5000u32 {
content.push_str(&format!("q 16 0 0 16 {} {} cm /Msk Do Q ", (i % 50) * 20, (i / 50) * 20));
}
let before = hayro::offscreen_draws();
let page = patterned_stencil_page((1000, 2000), &content);
let after = hayro::offscreen_draws();
assert_eq!(after.tiling_cells - before.tiling_cells, 1, "one cell for the pattern's one setting");
let pixels = after.pixels - before.pixels;
assert!(pixels < 1000 * 2000, "5,000 patterned stencils drew {pixels} pixels offscreen");
// The last stencil sits at (980, 1980): its ring is red, its centre white.
let px = |x: u32, y: u32| page.rgba[(((2000 - y) * page.width + x) * 4) as usize..][..3].to_vec();
assert_eq!(px(981, 1981), [255, 0, 0]);
assert_eq!(px(988, 1988), [255, 255, 255]);
}
/// A coloured tiling pattern's cell is drawn once for all the fills of one setting (`scn`), as
/// counted by hayro patch 30: here 3,000 small squares filled with one pattern. Drawn again for
/// each fill, each cell was kept until the page was composited (pdf.js issue 8078 ran out of
/// memory that way, its 3.5 MB cell drawn for each of 73,047 stencils). The squares all show the
/// pattern in place. The same holds for a step so large that the fills show one cell, which is
/// drawn with a transparent border instead of repeated.
#[test]
fn fills_of_one_pattern_setting_share_its_cell() {
let mut content = String::from("/Pattern cs /P scn ");
for i in 0..3000u32 {
content.push_str(&format!("{} {} 4 4 re f ", (i % 60) * 5, (i / 60) * 5));
}
// The cell is blue with a red lower-left quarter; at `/Matrix` scale 10 a cell is 100 units,
// so with a step of 10 red shows where both coordinates, modulo 100, are below 50. With a
// step of 100000 the one cell shows and nothing past it.
let cell = "0 0 1 rg 0 0 10 10 re f 1 0 0 rg 0 0 5 5 re f";
for (step, past_the_first_cell) in [("10", [0, 0, 255]), ("100000", [255, 255, 255])] {
let pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Resources << /Pattern << /P 5 0 R >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep {step} /YStep {step} /Matrix [10 0 0 10 0 0] /Resources << >> /Length {} >> stream
{cell}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
content.len(),
cell.len()
);
let before = hayro::offscreen_draws();
let page = render_first_page(pdf.into_bytes());
let cells = hayro::offscreen_draws().tiling_cells - before.tiling_cells;
assert_eq!(cells, 1, "step {step}: cells drawn for 3,000 fills of one setting");
// Pixel rows run top down: PDF (x, y) is pixel (x, 300 - y).
let px = |x: u32, y: u32| page.rgba[(((300 - y) * page.width + x) * 4) as usize..][..3].to_vec();
// The first square, at (0, 0), and the last, at (295, 245).
assert_eq!(px(1, 1), [255, 0, 0], "step {step}");
assert_eq!(px(296, 246), past_the_first_cell, "step {step}");
}
}
/// Fills interleaving the settings of two coloured patterns with cells of one size, and of one
/// of them at two cell scales, each show their own pattern in place: a cell kept for one setting
/// is not taken for another (hayro patch 30). `/P` is blue with a red lower-left quarter and `/Q`
/// green with a yellow one, both at `/Matrix` scale 10, so a cell is 100 units and its quarter
/// shows where both coordinates, modulo 100, are below 50. The form `/X`, drawn at scale 2, sets
/// `/P` in its own space, where a cell is 200 units, red where both page coordinates, modulo
/// 200, are below 100. Each of the six settings draws its cell once, for all its fills.
#[test]
fn fills_of_interleaved_pattern_settings_each_show_their_own_cell() {
let content = "/Pattern cs /P scn 60 60 4 4 re f 10 10 4 4 re f q 2 0 0 2 0 0 cm /X Do Q \
/Q scn 110 10 4 4 re f 160 160 4 4 re f /P scn 210 210 4 4 re f q 2 0 0 2 0 0 cm /X Do Q \
/Q scn 260 260 4 4 re f 110 110 4 4 re f";
// In page space, squares at (60, 260) and (260, 60), where `/P` at scale 1 is blue.
let form = "/Pattern cs /P scn 30 130 2 2 re f 130 30 2 2 re f";
let p = "0 0 1 rg 0 0 10 10 re f 1 0 0 rg 0 0 5 5 re f";
let q = "0 1 0 rg 0 0 10 10 re f 1 1 0 rg 0 0 5 5 re f";
let pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Resources << /Pattern << /P 5 0 R /Q 6 0 R >> /XObject << /X 7 0 R >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Matrix [10 0 0 10 0 0] /Resources << >> /Length {} >> stream
{p}
endstream endobj
6 0 obj << /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Matrix [10 0 0 10 0 0] /Resources << >> /Length {} >> stream
{q}
endstream endobj
7 0 obj << /Type /XObject /Subtype /Form /BBox [0 0 150 150] /Resources << /Pattern << /P 5 0 R >> >> /Length {} >> stream
{form}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
content.len(),
p.len(),
q.len(),
form.len()
);
let before = hayro::offscreen_draws();
let page = render_first_page(pdf.into_bytes());
assert_eq!(hayro::offscreen_draws().tiling_cells - before.tiling_cells, 6, "one cell per setting");
// The middle of the 4 × 4 square at PDF (x, y); pixel rows run top down.
let px = |x: u32, y: u32| page.rgba[(((300 - 2 - y) * page.width + x + 2) * 4) as usize..][..3].to_vec();
let (red, blue, green, yellow) = ([255, 0, 0], [0, 0, 255], [0, 255, 0], [255, 255, 0]);
for (x, y, colour, what) in [
(60, 60, blue, "/P"),
(10, 10, red, "/P"),
(60, 260, red, "/P at scale 2"),
(260, 60, red, "/P at scale 2"),
(110, 10, yellow, "/Q"),
(160, 160, green, "/Q"),
(210, 210, red, "/P set again"),
(260, 260, green, "/Q set again"),
(110, 110, yellow, "/Q set again"),
] {
assert_eq!(px(x, y), colour, "{what} at ({x}, {y})");
}
}
/// An uncoloured tiling pattern (`/PaintType 2`) takes the colour of each setting, so fills that
/// alternate red and green settings of it show both, each in place. Its cell also takes the
/// opacity the setting captured, which a patterned stencil mask's copy of the setting leaves out
/// (its transparency group applies it, hayro-interpret patch 46), so the cell drawn for one fill
/// of a setting is not the other's (hayro patch 30 keeps only coloured cells): a square and a
/// stencil painted with one half-opaque blue setting show the same half blue.
#[test]
fn an_uncoloured_pattern_shows_each_settings_colour() {
let content = "/C cs 1 0 0 /U scn 10 10 20 20 re f 0 1 0 /U scn 40 10 20 20 re f \
1 0 0 /U scn 70 10 20 20 re f 0 1 0 /U scn 100 10 20 20 re f \
/H gs 0 0 1 /U scn 10 50 20 20 re f q 20 0 0 20 40 50 cm /Msk Do Q";
let tile = "0 0 10 10 re f";
let pdf = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 150 100] /Contents 4 0 R /Resources << /ColorSpace << /C [/Pattern /DeviceRGB] >> /Pattern << /U 6 0 R >> /XObject << /Msk 5 0 R >> /ExtGState << /H << /ca 0.5 >> >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /XObject /Subtype /Image /Width 4 /Height 4 /ImageMask true /Filter /ASCIIHexDecode /Length 9 >> stream
00000000>
endstream endobj
6 0 obj << /Type /Pattern /PatternType 1 /PaintType 2 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Resources << >> /Length {} >> stream
{tile}
endstream endobj
trailer << /Root 1 0 R >>
%%EOF",
content.len(),
tile.len()
);
let page = render_first_page(pdf.into_bytes());
// Pixel rows run top down: PDF (x, y) is pixel (x, 100 - y).
let px = |x: u32, y: u32| page.rgba[(((100 - y) * page.width + x) * 4) as usize..][..3].to_vec();
for (x, colour) in [(20, [255, 0, 0]), (50, [0, 255, 0]), (80, [255, 0, 0]), (110, [0, 255, 0])] {
assert_eq!(px(x, 20), colour, "the square at x {x}");
}
let (square, stencil) = (px(20, 60), px(50, 60));
assert!(square[2] == 255 && square[0].abs_diff(128) <= 1 && square[1] == square[0], "half blue: {square:?}");
assert!(square.iter().zip(&stencil).all(|(a, b)| a.abs_diff(*b) <= 2), "square {square:?}, stencil {stencil:?}");
}
/// #2971: in a knockout transparency group (`/K true`) each element composites with the
/// group's initial backdrop, not with the elements drawn before it in the group, over the
/// element's shape. A blue square at 50 % opacity over a red one at 50 % shows only the blue
@@ -8635,21 +9062,232 @@ fn decoded_images_serve_every_size_they_are_drawn_at() {
let mut device = Draws::default();
interpret_page(page, &mut ctx, &mut device);
assert_eq!(device.0, 5, "every draw of /{name} was handed its pixels");
cache.decoded_image_decodes()
// (image decodes, mask decodes)
let masks = cache.decoded_mask_decodes();
(cache.decoded_image_decodes() - masks, masks)
};
assert_eq!(decodes("A"), 1, "a Flate image decodes the same at every size");
assert_eq!(decodes("B"), 5, "a JPX soft mask decodes for the size it is drawn at");
assert_eq!(decodes("A"), (1, 0), "a Flate image decodes the same at every size");
// The image decodes once for each of the five sizes, its JPX soft mask within it: only a mask
// averaged down (hayro-interpret patch 82) has a cache entry of its own.
assert_eq!(decodes("B"), (5, 0), "an image with a JPX soft mask decodes for the size it is drawn at");
}
/// A soft mask far larger than where its image is drawn is averaged down to that size and cached
/// there (vendored hayro-interpret patch 82): a 2 × 2 image whose mask is 34,862 × 4,332, drawn 40
/// times on one slide (pdf.js `issue16263.pdf`), was decoded and drawn at full size each time and
/// the page took minutes. Drawn ten times at one size, the mask is now decoded once and handed
/// over at that size rounded up to a power of two; a mask no larger than twice its drawn size
/// keeps its own.
#[test]
fn a_soft_mask_far_larger_than_where_it_is_drawn_is_shrunk_once() {
use hayro::hayro_interpret::font::Glyph;
use hayro::hayro_interpret::hayro_syntax::Pdf;
use hayro::hayro_interpret::{
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, Paint, PathDrawMode, SoftMask, TransformExt, interpret_page,
};
use kurbo::{Affine, BezPath, Rect};
use std::io::Write;
/// Asks for every raster image at the size it is drawn at, as the renderer does, and keeps
/// the size of each alpha channel it was handed.
#[derive(Default)]
struct Draws(Vec<(u32, u32)>);
impl<'a> Device<'a> for Draws {
fn set_soft_mask(&mut self, _: Option<SoftMask<'a>>) {}
fn set_blend_mode(&mut self, _: BlendMode) {}
fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'a>, _: &PathDrawMode) {}
fn push_clip_path(&mut self, _: &ClipPath) {}
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'a>>, _: BlendMode) {}
fn draw_glyph(&mut self, _: &Glyph<'a>, _: Affine, _: Affine, _: &Paint<'a>, _: &GlyphDrawMode) {}
fn draw_image(&mut self, image: Image<'a, '_>, transform: Affine) {
let [a, b, c, d, ..] = transform.as_coeffs();
let target = ((a.hypot(b) * f64::from(image.width())).ceil() as u32, (c.hypot(d) * f64::from(image.height())).ceil() as u32);
if let Image::Raster(r) = image {
r.with_rgba(|_, alpha| self.0.push(alpha.map_or((0, 0), |a| (a.width, a.height))), Some(target));
}
}
fn pop_clip_path(&mut self) {}
fn pop_transparency_group(&mut self) {}
}
let flate = |data: &[u8]| {
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast());
z.write_all(data).unwrap();
z.finish().unwrap()
};
let rgb = flate(&[0u8; 2 * 2 * 3]);
let image = format!(
"<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode /SMask 6 0 R /Length {} >>",
rgb.len()
);
// `side` × `side` mask samples, drawn ten times at 20 × 20 points (device pixels at 72 dpi).
let draws = |side: u32| {
let alpha = flate(&(0..side * side).map(|i| (i % 256) as u8).collect::<Vec<_>>());
let mask = format!(
"<< /Type /XObject /Subtype /Image /Width {side} /Height {side} /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>",
alpha.len()
);
let content = (0..10).map(|i| format!("q 20 0 0 20 {} 0 cm /A Do Q", i * 22)).collect::<Vec<_>>().join(" ");
let bytes = image_cache_pdf(&content, "<< /XObject << /A 5 0 R >> >>", &[(&image, &rgb), (&mask, &alpha)]);
let parsed = Pdf::new(Arc::new(bytes)).expect("parses");
let page = &parsed.pages()[0];
let cache = InterpreterCache::new();
let initial = page.initial_transform(true).to_kurbo();
let mut ctx = Context::new(initial, Rect::new(0.0, 0.0, 230.0, 30.0), &cache, page.xref(), InterpreterSettings::default());
let mut device = Draws::default();
interpret_page(page, &mut ctx, &mut device);
(device.0, cache.decoded_image_decodes())
};
let (alphas, decodes) = draws(1000);
// Drawn 20 pixels square, averaged down to that rounded up to a power of two.
assert_eq!(alphas, vec![(32, 32); 10], "averaged down to the size it is drawn at");
// One decode for the image and one for its mask (here no larger than the intermediate the
// averaged-down sizes are made from, so that is the mask itself), plus the averaged-down mask,
// all shared by the ten draws.
assert_eq!(decodes, 3, "decoded once");
let (alphas, _) = draws(40);
assert_eq!(alphas, vec![(40, 40); 10], "no more than twice the drawn size: kept as it is");
}
/// The soft masks a small image is handed, drawn at each of `sizes` (width × height points at 72
/// dpi): its `mask` (`width`, `height`, 8-bit samples) is larger than the 2 × 1 image, so it may
/// be averaged down (vendored hayro-interpret patch 82). Each mask is given as its size and
/// samples.
fn soft_masks_handed(width: u32, height: u32, mask: &[u8], sizes: &[(u32, u32)]) -> (Vec<(u32, u32, Vec<u8>)>, usize) {
soft_masks_handed_keyed(width, height, mask, sizes, ("Width", "Height"))
}
/// `soft_masks_handed`, with the mask's dimensions under `keys` (`/W` and `/H`, say).
fn soft_masks_handed_keyed(width: u32, height: u32, mask: &[u8], sizes: &[(u32, u32)], keys: (&str, &str)) -> (Vec<(u32, u32, Vec<u8>)>, usize) {
use hayro::hayro_interpret::font::Glyph;
use hayro::hayro_interpret::hayro_syntax::Pdf;
use hayro::hayro_interpret::{
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, Paint, PathDrawMode, SoftMask, TransformExt, interpret_page,
};
use kurbo::{Affine, BezPath, Rect};
use std::io::Write;
#[derive(Default)]
struct Draws(Vec<(u32, u32, Vec<u8>)>);
impl<'a> Device<'a> for Draws {
fn set_soft_mask(&mut self, _: Option<SoftMask<'a>>) {}
fn set_blend_mode(&mut self, _: BlendMode) {}
fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'a>, _: &PathDrawMode) {}
fn push_clip_path(&mut self, _: &ClipPath) {}
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'a>>, _: BlendMode) {}
fn draw_glyph(&mut self, _: &Glyph<'a>, _: Affine, _: Affine, _: &Paint<'a>, _: &GlyphDrawMode) {}
fn draw_image(&mut self, image: Image<'a, '_>, transform: Affine) {
let [a, b, c, d, ..] = transform.as_coeffs();
let target = ((a.hypot(b) * f64::from(image.width())).ceil() as u32, (c.hypot(d) * f64::from(image.height())).ceil() as u32);
if let Image::Raster(r) = image {
r.with_rgba(|_, alpha| self.0.extend(alpha.map(|a| (a.width, a.height, a.data))), Some(target));
}
}
fn pop_clip_path(&mut self) {}
fn pop_transparency_group(&mut self) {}
}
let flate = |data: &[u8]| {
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast());
z.write_all(data).unwrap();
z.finish().unwrap()
};
let rgb = flate(&[0u8; 2 * 3]);
let image = format!(
"<< /Type /XObject /Subtype /Image /Width 2 /Height 1 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode /SMask 6 0 R /Length {} >>",
rgb.len()
);
let alpha = flate(mask);
let mask_dict = format!(
"<< /Type /XObject /Subtype /Image /{} {width} /{} {height} /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>",
keys.0,
keys.1,
alpha.len()
);
let content = sizes.iter().map(|(w, h)| format!("q {w} 0 0 {h} 0 0 cm /A Do Q")).collect::<Vec<_>>().join(" ");
let bytes = image_cache_pdf(&content, "<< /XObject << /A 5 0 R >> >>", &[(&image, &rgb), (&mask_dict, &alpha)]);
let parsed = Pdf::new(Arc::new(bytes)).expect("parses");
let page = &parsed.pages()[0];
let cache = InterpreterCache::new();
let initial = page.initial_transform(true).to_kurbo();
let mut ctx = Context::new(initial, Rect::new(0.0, 0.0, 400.0, 400.0), &cache, page.xref(), InterpreterSettings::default());
let mut device = Draws::default();
interpret_page(page, &mut ctx, &mut device);
(device.0, cache.decoded_image_decodes())
}
/// An averaged-down soft mask (vendored hayro-interpret patch 82):
/// - takes each source sample into the target sample its centre falls in: seven columns
/// `0 0 0 255 0 0 0`, drawn three wide and so averaged to four, give `0 0 127 0` (by left edges,
/// `0 127 0 0`);
/// - is never sized past the samples a truncated stream decodes: a mask that says it is 100 rows
/// but holds 10, drawn 20 high, is handed no rows that no sample covers;
/// - is cached per drawn size, rounded up to a power of two: the same image drawn 20 and then 200
/// points wide is handed its mask at 32 and then at its own 100, not the first draw's 32 again,
/// and drawn 20 and 21 wide it is handed one mask of 32 both times, whether the mask's
/// dimensions are under `/Width` and `/Height` or `/W` and `/H`;
/// - is made, at every size, from one intermediate mask at most 4096 on a side, so a mask wider
/// than that, drawn at four sizes, is decoded in full once and not four times.
#[test]
fn an_averaged_down_soft_mask_follows_its_samples_and_its_drawn_size() {
let (handed, _) = soft_masks_handed(7, 1, &[0, 0, 0, 255, 0, 0, 0], &[(3, 1)]);
assert_eq!(handed, vec![(4, 1, vec![0, 0, 127, 0])], "centres, not left edges");
let (handed, _) = soft_masks_handed(100, 100, &[255; 100 * 10], &[(20, 20)]);
// 32 wide (20 rounded up to a power of two), and 10 rows, the ones decoded, not 32: all opaque.
assert_eq!(handed, vec![(32, 10, vec![255; 32 * 10])], "no uncovered rows");
let sizes = |draws: &[(u32, u32)]| -> Vec<(u32, u32)> {
soft_masks_handed(100, 100, &[255; 100 * 100], draws).0.iter().map(|(w, h, _)| (*w, *h)).collect()
};
assert_eq!(sizes(&[(20, 20), (200, 200)]), vec![(32, 32), (100, 100)], "each draw at its own size");
assert_eq!(sizes(&[(20, 20), (21, 21)]), vec![(32, 32), (32, 32)], "draws a pixel apart share one");
// The same with the mask's dimensions under the abbreviated `/W` and `/H`, which an image is
// read with too.
let (handed, _) = soft_masks_handed_keyed(100, 100, &[255; 100 * 100], &[(20, 20), (200, 200)], ("W", "H"));
let abbreviated: Vec<(u32, u32)> = handed.iter().map(|(w, h, _)| (*w, *h)).collect();
assert_eq!(abbreviated, vec![(32, 32), (100, 100)], "each draw at its own size, under /W and /H");
// Columns alternating 0 and 255, so every averaged-down sample is about half: within 8, as a
// sample covering 19 or 20 columns, say, holds 9 or 10 opaque ones.
let wide: Vec<u8> = (0..5000 * 2).map(|i| if i % 2 == 0 { 0 } else { 255 }).collect();
let (handed, decodes) = soft_masks_handed(5000, 2, &wide, &[(20, 1), (40, 1), (80, 1), (160, 1)]);
let sizes: Vec<(u32, u32)> = handed.iter().map(|(w, h, _)| (*w, *h)).collect();
assert_eq!(sizes, vec![(32, 1), (64, 1), (128, 1), (256, 1)]);
assert!(handed.iter().all(|(_, _, data)| data.iter().all(|&a| a.abs_diff(127) <= 8)), "{handed:?}");
// The image at each of the four sizes, the 4096-wide intermediate once, and the mask at each
// of the four sizes made from it. Without the intermediate there are 8, each mask size a full
// decode.
assert_eq!(decodes, 9, "the full mask decoded once");
}
// ICC profiles whose stream dictionaries are the same (vendored hayro-interpret patch 48).
mod icc_profile_cache;
// A Type 3 glyph shown again decodes its procedure once (vendored hayro-interpret patch 87).
mod type3_proc_cache;
// A picture that doesn't decode is decoded once however often it is drawn (vendored
// hayro-interpret patch 91).
mod failed_image_decodes;
// A page past its rasterizer coverage leaves the rest out and says so (vendored vello_cpu patch 1).
mod coverage_budget;
// A symbolic TrueType font draws through its (3, 0) cmap subtable first (#4156).
mod symbolic_truetype_cmap;
// A TrueType font whose `head.indexToLocFormat` is out of range reads `loca` in the format its
// length gives (vendored hayro-interpret patch 83).
mod truetype_loca_format;
// An Arial (#5889) or Times New Roman (#6449) the PDF doesn't embed draws the glyphs its codes
// name.
mod non_embedded_arial;
// A Type 3 font without /Widths advances by its glyphs' d0/d1 widths (PDFBOX-2943).
mod type3_proc_widths;
// A symbolic TrueType font in the standard Macintosh glyph order reads its codes as Mac OS Roman
// (vendored hayro-interpret patch 101).
mod symbolic_truetype_mac_roman_text;
/// Render `pdf`'s first page at 72 dpi with no time limit beyond the test's own.
fn render_first_page(pdf: Vec<u8>) -> RenderedPage {
let mut r = PageRenderer::new(Arc::new(pdf), RenderConfig::default());
@@ -0,0 +1,239 @@
//! A picture that doesn't decode is decoded once however often a page draws it (vendored
//! hayro-interpret patch 91). The decoded-image cache kept only decodes that succeeded, so a
//! damaged picture, or a JPX or JBIG2 stream its decoder refuses, was decoded again at every
//! draw: a page drawing it a thousand times decoded it a thousand times. The failure is kept for
//! the same key now, and every draw still reports it as it did.
use super::image_cache_pdf;
use hayro::hayro_interpret::font::Glyph;
use hayro::hayro_interpret::hayro_syntax::Pdf;
use hayro::hayro_interpret::{
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, InterpreterWarning, Paint, PathDrawMode,
SoftMask, TransformExt, interpret_page,
};
use kurbo::{Affine, BezPath, Rect};
use std::io::Write;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
/// What a page handed its device: the raster images with their pixels, those of them with a
/// mask, and the stencil masks with theirs. Every image is asked for at the size it is drawn
/// at, as the renderer does.
#[derive(Default)]
struct Draws {
rasters: usize,
masked: usize,
stencils: usize,
}
impl<'a> Device<'a> for Draws {
fn set_soft_mask(&mut self, _: Option<SoftMask<'a>>) {}
fn set_blend_mode(&mut self, _: BlendMode) {}
fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'a>, _: &PathDrawMode) {}
fn push_clip_path(&mut self, _: &ClipPath) {}
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'a>>, _: BlendMode) {}
fn draw_glyph(&mut self, _: &Glyph<'a>, _: Affine, _: Affine, _: &Paint<'a>, _: &GlyphDrawMode) {}
fn draw_image(&mut self, image: Image<'a, '_>, transform: Affine) {
let [a, b, c, d, ..] = transform.as_coeffs();
let target = ((a.hypot(b) * f64::from(image.width())).ceil() as u32, (c.hypot(d) * f64::from(image.height())).ceil() as u32);
match image {
Image::Raster(r) => r.with_rgba(
|_, alpha| {
self.rasters += 1;
self.masked += usize::from(alpha.is_some());
},
Some(target),
),
Image::Stencil(s) => s.with_stencil(|_, _| self.stencils += 1, Some(target)),
}
}
fn pop_clip_path(&mut self) {}
fn pop_transparency_group(&mut self) {}
}
/// What drawing `/name` five times at one size, and then the picture that decodes (`/G`) once
/// on the right, did: the decodes the cache ran, the image decode failures reported, and what
/// the device was handed.
struct Outcome {
decodes: usize,
failures: usize,
draws: Draws,
}
fn draw_five_times(name: &str) -> Outcome {
draw_five_times_at(name, [20; 5])
}
/// [`draw_five_times`], drawing `/name` the five times at `sizes` (units square).
fn draw_five_times_at(name: &str, sizes: [u32; 5]) -> Outcome {
let flate = |data: &[u8]| {
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast());
z.write_all(data).unwrap();
z.finish().unwrap()
};
let pixels = flate(&(0..64 * 64 * 3).map(|i| (i % 251) as u8).collect::<Vec<_>>());
let image = |filter: &str, extra: &str, len: usize| {
format!(
"<< /Type /XObject /Subtype /Image /Width 64 /Height 64 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /{filter} {extra}/Length {len} >>"
)
};
let good = image("FlateDecode", "", pixels.len());
// A JPEG 2000 picture whose codestream is four zero bytes.
let bad_jpx = image("JPXDecode", "", 4);
// A picture whose soft mask is such a JPEG 2000 stream.
let bad_mask = image("FlateDecode", "/SMask 9 0 R ", pixels.len());
// A stencil mask whose JBIG2 data is four zero bytes.
let bad_stencil = "<< /Type /XObject /Subtype /Image /Width 64 /Height 64 /ImageMask true /Filter /JBIG2Decode /Length 4 >>";
let jpx_mask =
"<< /Type /XObject /Subtype /Image /Width 64 /Height 64 /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter /JPXDecode /Length 4 >>";
let content = [0, 24, 48, 72, 96].iter().zip(sizes).map(|(x, s)| format!("q {s} 0 0 {s} {x} 10 cm /{name} Do Q")).collect::<Vec<_>>().join(" ")
+ " q 10 0 0 10 118 10 cm /G Do Q";
let bytes = image_cache_pdf(
&content,
"<< /XObject << /G 5 0 R /J 6 0 R /S 7 0 R /M 8 0 R >> >>",
&[(&good, &pixels), (&bad_jpx, b"\0\0\0\0"), (&bad_mask, &pixels), (bad_stencil, b"\0\0\0\0"), (jpx_mask, b"\0\0\0\0")],
);
let parsed = Pdf::new(Arc::new(bytes)).expect("parses");
let page = &parsed.pages()[0];
let cache = InterpreterCache::new();
let failures = Arc::new(AtomicUsize::new(0));
let others = Arc::new(Mutex::new(Vec::new()));
let settings = InterpreterSettings {
warning_sink: {
let (failures, others) = (failures.clone(), others.clone());
Arc::new(move |warning| match warning {
InterpreterWarning::ImageDecodeFailure => {
failures.fetch_add(1, Ordering::Relaxed);
}
other => others.lock().unwrap().push(format!("{other:?}")),
})
},
..InterpreterSettings::default()
};
let initial = page.initial_transform(true).to_kurbo();
let mut ctx = Context::new(initial, Rect::new(0.0, 0.0, 130.0, 70.0), &cache, page.xref(), settings);
let mut draws = Draws::default();
interpret_page(page, &mut ctx, &mut draws);
assert!(others.lock().unwrap().is_empty(), "/{name} reported {:?}", others.lock().unwrap());
Outcome { decodes: cache.decoded_image_decodes(), failures: failures.load(Ordering::Relaxed), draws }
}
/// A JPEG 2000 picture that doesn't decode, drawn five times at one size, is decoded once, and
/// each of the five draws still reports the failure.
#[test]
fn a_picture_that_does_not_decode_is_decoded_once_however_often_it_is_drawn() {
let out = draw_five_times("J");
assert_eq!(out.draws.rasters, 1, "only the picture after it drew");
assert_eq!(out.decodes, 2, "decoded again at every draw");
assert_eq!(out.failures, 5, "every draw reports the failure, as before");
}
/// A JPEG 2000 picture decodes at a reduced resolution for the size it is drawn at, so its
/// failure is kept for that size: drawn at two sizes, it is decoded once at each, also when it
/// comes back to the first size after the second.
#[test]
fn a_picture_that_does_not_decode_is_decoded_once_at_each_size() {
let out = draw_five_times_at("J", [20, 20, 10, 10, 20]);
assert_eq!(out.decodes, 3, "once at each of its two sizes, and the picture after it");
assert_eq!(out.failures, 5, "every draw reports the failure");
}
/// A JBIG2 stencil mask that doesn't decode, drawn five times, is decoded once, and each draw
/// still reports the failure: it isn't the page's JBIG2 allowance that refused it, which a
/// later page would have afresh.
#[test]
fn a_stencil_mask_that_does_not_decode_is_decoded_once_however_often_it_is_drawn() {
let out = draw_five_times("M");
assert_eq!((out.draws.stencils, out.draws.rasters), (0, 1), "only the picture after it drew");
assert_eq!(out.decodes, 2, "decoded again at every draw");
assert_eq!(out.failures, 5, "every draw reports the failure, as before");
}
/// A picture whose soft mask doesn't decode draws without it at every draw, decoded once, and
/// the mask's failure is reported once, as it was (the picture decodes; only its mask fails).
#[test]
fn a_picture_whose_soft_mask_does_not_decode_draws_unmasked_decoded_once() {
let out = draw_five_times("S");
assert_eq!((out.draws.rasters, out.draws.masked), (6, 0), "drawn five times without its mask, and the picture after it");
assert_eq!(out.decodes, 2);
assert_eq!(out.failures, 1);
}
/// A picture that decodes is still decoded once and handed to every draw.
#[test]
fn a_picture_that_decodes_is_still_decoded_once_and_drawn_every_time() {
let out = draw_five_times("G");
assert_eq!(out.draws.rasters, 6);
assert_eq!(out.decodes, 1);
assert_eq!(out.failures, 0);
}
/// A JBIG2 picture the page's allowance left out isn't kept as a failure: the next page drawing
/// it starts with an allowance of its own and draws it, from the same cache.
#[test]
fn a_picture_left_out_for_the_pages_allowance_draws_on_the_next_page() {
use super::{RenderWarning, black_jbig2_page, jbig2_segment};
use crate::raster::{parse, render_page, take_warnings, warning_settings};
use crate::{RenderConfig, RenderRequest, RequestKind};
use hayro::RenderCache;
let image = |number: u8, id: u8| {
let mut stream = black_jbig2_page();
jbig2_segment(&mut stream, 1, 62, &[], &[0, 0, 0, 0, id]);
let mut object = format!(
"{number} 0 obj << /Type /XObject /Subtype /Image /Width 16 /Height 2 /BitsPerComponent 1 /ColorSpace /DeviceGray /Filter /JBIG2Decode /Length {} >> stream\n",
stream.len()
)
.into_bytes();
object.extend_from_slice(&stream);
object.extend_from_slice(b"\nendstream endobj\n");
object
};
let page = |number: u8, content: u8| {
format!("{number} 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 40] /Contents {content} 0 R /Resources << /XObject << /A 7 0 R /B 8 0 R >> >> >> endobj\n").into_bytes()
};
let content = |number: u8, text: &str| format!("{number} 0 obj << /Length {} >> stream\n{text}\nendstream endobj\n", text.len()).into_bytes();
let mut pdf =
b"%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >> endobj\n".to_vec();
pdf.extend(page(3, 5));
pdf.extend(page(4, 6));
// The first page spends its allowance on A and has nothing left for B; the second draws A,
// decoded already, and then B.
pdf.extend(content(5, "q 20 0 0 40 0 0 cm /A Do Q q 20 0 0 40 20 0 cm /B Do Q"));
pdf.extend(content(6, "q 20 0 0 40 0 0 cm /A Do Q q 20 0 0 40 20 0 cm /B Do Q"));
pdf.extend(image(7, 1));
pdf.extend(image(8, 2));
pdf.extend_from_slice(b"trailer << /Root 1 0 R >>\n%%EOF\n");
let pdf = parse(&Arc::new(pdf), None).expect("parses");
/// A page's pixels and the warnings it reported.
type Drawn = (Vec<u8>, Vec<RenderWarning>);
let black = |(rgba, _): &Drawn, x: usize| match &rgba[((20 * 40 + x) * 4)..][..4] {
[0, 0, 0, 255] => true,
[255, 255, 255, 255] => false,
other => panic!("neither an image nor the page: {other:?}"),
};
// Both pages, or the first alone, through one cache, as a render worker keeps it.
let pages = |work: u64, count: usize| {
let config = RenderConfig { jbig2_work: Some(work), ..RenderConfig::default() };
let cache = RenderCache::new();
(0..count)
.map(|page| {
let warnings = Arc::new(Mutex::new(Vec::new()));
let settings = warning_settings(&config.settings(), warnings.clone());
let req = RenderRequest { page, kind: RequestKind::Pixels, tile: None, scale: 1.0, tag: 0, ..Default::default() };
let (_, _, rgba, _) = render_page(&pdf, &cache, &settings, &config, req).expect("renders");
(rgba[..].to_vec(), take_warnings(&warnings))
})
.collect::<Vec<Drawn>>()
};
// The smallest allowance that draws A on the first page. A larger allowance draws at least as
// much, so a binary search over the first page alone finds it in about ten draws.
let draws_a = |work: u64| pages(work, 1).first().is_some_and(|first| black(first, 10));
let candidates: Vec<u64> = (1..1_000).collect();
let one = candidates.get(candidates.partition_point(|work| !draws_a(*work))).copied().expect("a small image is cheap");
let Ok([first, second]) = <[Drawn; 2]>::try_from(pages(one, 2)) else { panic!("two pages") };
assert_eq!((black(&first, 10), black(&first, 30)), (true, false), "the first page had enough for A alone");
assert!(first.1.contains(&RenderWarning::ContentTruncated), "{:?}", first.1);
assert!(black(&second, 10) && black(&second, 30), "A, and B left out of the first page, draw on the second");
assert!(second.1.is_empty(), "{:?}", second.1);
}
@@ -0,0 +1,150 @@
//! An Arial or a Times New Roman the PDF doesn't embed draws the glyphs its codes name (vendored
//! hayro-interpret patches 76 and 88, #5889, #6449). Word and PDFMaker write such a font as a
//! Type 0 `CIDFontType2` with `Identity-H`, often without `/ToUnicode`, so each code is a glyph
//! index in the author's font. Both number their first 258 glyphs in the standard Macintosh order
//! of the TrueType `post` table (`L` is 47, `o` 82, `w` 90). The standard substitute numbers its
//! glyphs another way: "Low Power" drew as "@ck 'Dck Yf", and "Salarié(e) du régime général" as
//! "Salari²*g+f w't²i lo g'ì²p²tcn".
//!
//! Each document is drawn twice, once as such a file has it and once with a `/ToUnicode` that
//! names each code's character, which draws through the substitute's glyph for that character.
//! The two must be the same pixels.
use super::render_first_page;
/// "Low É" as Arial's glyph indexes: L, o, w, space, Eacute.
const LOW_E_ACUTE: [(u16, char); 5] = [(47, 'L'), (82, 'o'), (90, 'w'), (3, ' '), (101, 'É')];
struct Font<'a> {
/// The `/BaseFont` of both the Type 0 font and its descendant.
base_font: &'a str,
/// The descendant's `/Subtype`.
subtype: &'a str,
/// `true`: the codes are 1, 2, … and a `/CIDToGIDMap` stream gives each its glyph in the author's font.
/// `false`: `/CIDToGIDMap /Identity`, and the codes are the glyph indexes themselves.
mapped: bool,
to_unicode: bool,
}
/// A page showing [`LOW_E_ACUTE`] at 40 pt in `font`, which has no font program.
fn pdf(font: &Font<'_>) -> Vec<u8> {
let codes: Vec<u16> = if font.mapped { (1..=LOW_E_ACUTE.len() as u16).collect() } else { LOW_E_ACUTE.iter().map(|(gid, _)| *gid).collect() };
let hex: String = codes.iter().map(|code| format!("{code:04X}")).collect();
let content = format!("BT /F1 40 Tf 5 15 Td <{hex}> Tj ET");
let bfchar: String = codes.iter().zip(LOW_E_ACUTE).map(|(code, (_, c))| format!("<{code:04X}> <{:04X}>\n", u32::from(c))).collect();
let cmap = format!(
"/CIDInit /ProcSet findresource begin 12 dict begin begincmap
/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
/CMapName /Adobe-Identity-UCS def /CMapType 2 def
1 begincodespacerange <0000> <FFFF> endcodespacerange
{} beginbfchar
{bfchar}endbfchar
endcmap CMapName currentdict /CMap defineresource pop end end",
codes.len()
);
// CID 0 is .notdef; CIDs 1… give the glyphs in order.
let cid_to_gid: Vec<u8> = std::iter::once(0).chain(LOW_E_ACUTE.iter().map(|(gid, _)| *gid)).flat_map(u16::to_be_bytes).collect();
let Font { base_font, subtype, .. } = font;
let to_unicode = if font.to_unicode { " /ToUnicode 8 0 R" } else { "" };
let map = if font.mapped { "9 0 R" } else { "/Identity" };
let mut out = format!(
"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 220 60] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >> endobj
4 0 obj << /Length {} >> stream
{content}
endstream endobj
5 0 obj << /Type /Font /Subtype /Type0 /BaseFont /{base_font} /Encoding /Identity-H /DescendantFonts [6 0 R]{to_unicode} >> endobj
6 0 obj << /Type /Font /Subtype /{subtype} /BaseFont /{base_font} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >>
/FontDescriptor 7 0 R /CIDToGIDMap {map} /DW 1000 >> endobj
7 0 obj << /Type /FontDescriptor /FontName /{base_font} /Flags 32 /FontBBox [-665 -325 2000 1040] /ItalicAngle 0 /Ascent 728 /Descent -210 /CapHeight 716 /StemV 80 >> endobj
8 0 obj << /Length {} >> stream
{cmap}
endstream endobj
9 0 obj << /Length {} >> stream
",
content.len(),
cmap.len(),
cid_to_gid.len()
)
.into_bytes();
out.extend_from_slice(&cid_to_gid);
out.extend_from_slice(b"\nendstream endobj\ntrailer << /Root 1 0 R >>\n%%EOF");
out
}
/// `font` drawn without and with a `/ToUnicode`; the second must draw ink.
fn without_and_with_unicode(font: Font<'_>) -> (Vec<u8>, Vec<u8>) {
let without = render_first_page(pdf(&Font { to_unicode: false, ..font }));
let with = render_first_page(pdf(&Font { to_unicode: true, ..font }));
assert!(with.rgba.as_chunks::<4>().0.iter().any(|pixel| pixel[0] < 128), "{}: the control must draw ink", font.base_font);
(without.rgba.to_vec(), with.rgba.to_vec())
}
/// Each Arial face, as Windows exporters name it and as Word names a face it doesn't embed, draws
/// "Low É" from its glyph indexes, with `/CIDToGIDMap /Identity` and with a map stream. The name
/// is read without regard to case, `MT` included.
#[test]
fn an_arial_the_pdf_does_not_embed_draws_the_glyphs_its_codes_name() {
for base_font in [
"ArialMT",
"Arial-BoldMT",
"Arial-ItalicMT",
"Arial-BoldItalicMT",
"Arial",
"Arial,Bold",
"Arial,BoldItalic",
"ABCDEF+Arial-BoldMT",
"arialmt",
"Arial-Boldmt",
] {
for mapped in [false, true] {
let (without, with) = without_and_with_unicode(Font { base_font, subtype: "CIDFontType2", mapped, to_unicode: false });
assert!(without == with, "{base_font} (CIDToGIDMap {}): the codes draw \"Low É\"", if mapped { "stream" } else { "/Identity" });
}
}
}
/// Each Times New Roman face, as Windows exporters name it and as Word names a face it doesn't
/// embed (`TimesNewRoman,Bold` in #6449), draws "Low É" from its glyph indexes.
#[test]
fn a_times_new_roman_the_pdf_does_not_embed_draws_the_glyphs_its_codes_name() {
for base_font in [
"TimesNewRomanPSMT",
"TimesNewRomanPS-BoldMT",
"TimesNewRomanPS-ItalicMT",
"TimesNewRomanPS-BoldItalicMT",
"TimesNewRoman",
"TimesNewRoman,Bold",
"TimesNewRoman,Italic",
"TimesNewRoman,BoldItalic",
"WFXLUF+TimesNewRoman,Bold",
] {
for mapped in [false, true] {
let (without, with) = without_and_with_unicode(Font { base_font, subtype: "CIDFontType2", mapped, to_unicode: false });
assert!(without == with, "{base_font} (CIDToGIDMap {}): the codes draw \"Low É\"", if mapped { "stream" } else { "/Identity" });
}
}
}
/// Only the faces of Arial and Times New Roman are read in that order: Arial Narrow, Arial Black,
/// Arial Unicode MS, Times and Times New Roman MT Std are other fonts, and a `CIDFontType0` font's
/// CIDs aren't TrueType glyph indexes.
#[test]
fn codes_of_other_fonts_are_not_read_in_the_macintosh_order() {
for (base_font, subtype) in [
("ArialNarrow", "CIDFontType2"),
("ArialNarrow-Bold", "CIDFontType2"),
("Arial-Black", "CIDFontType2"),
("ArialUnicodeMS", "CIDFontType2"),
("ArialMT", "CIDFontType0"),
("Times-Bold", "CIDFontType2"),
("TimesNewRomanMTStd", "CIDFontType2"),
("TimesNewRoman,Bold", "CIDFontType0"),
] {
let (without, with) = without_and_with_unicode(Font { base_font, subtype, mapped: false, to_unicode: false });
assert!(without != with, "{base_font} ({subtype}) is not read in the Macintosh order");
}
}
@@ -0,0 +1,281 @@
//! A symbolic TrueType font with no `/Encoding`, no `/ToUnicode` and no glyph names reads a code
//! as Mac OS Roman when its (1, 0) `cmap` subtable selects the glyph the code draws and that glyph
//! sits where the standard Macintosh glyph order puts the code's Mac OS Roman character (vendored
//! hayro-interpret patch 101; ISO 32000-2 §9.6.5.4: single bytes "look up the associated glyph
//! descriptions" in the (1, 0) subtable, and a (3, 0) subtable maps "the single-byte codes in the
//! (1, 0) subtable" prepended with 0xF0). pdf.js's issue2017r.pdf has such a font; its text read as
//! WinAnsi, `ˆ` for `à`.
//!
//! The fonts are built here, in memory: [`GLYPHS`] glyphs, those the tests show filled squares, no
//! `post` table, and the `cmap` subtables each test asks for. No font file is involved.
use super::*;
/// Glyphs in each font: past the standard Macintosh order's `quoteright` (183).
const GLYPHS: u16 = 200;
/// The standard Macintosh order's glyphs for `agrave`, `eacute`, `endash` and `quoteright`.
const AGRAVE: u8 = 106;
const EACUTE: u8 = 112;
const ENDASH: u8 = 178;
const QUOTERIGHT: u8 = 183;
fn be16(out: &mut Vec<u8>, n: u16) {
out.extend_from_slice(&n.to_be_bytes());
}
fn be32(out: &mut Vec<u8>, n: u32) {
out.extend_from_slice(&n.to_be_bytes());
}
/// A (1, 0) format 0 subtable mapping each code to its glyph.
fn mac_subtable(map: &[(u8, u8)]) -> Vec<u8> {
let mut out = Vec::new();
for n in [0u16, 262, 0] {
be16(&mut out, n); // format, length, language
}
let mut ids = [0u8; 256];
for &(code, glyph) in map {
ids[usize::from(code)] = glyph;
}
out.extend(ids);
out
}
/// A (3, 0) format 4 subtable mapping each code at 0xF000 to its glyph, one segment each.
fn windows_subtable(map: &[(u8, u8)]) -> Vec<u8> {
let segments: Vec<(u16, u16)> = map.iter().map(|&(code, glyph)| (0xF000 | u16::from(code), u16::from(glyph))).chain([(0xFFFF, 0)]).collect();
let count = segments.len() as u16;
let mut out = Vec::new();
for n in [4u16, 16 + 8 * count, 0, 2 * count, 2, 0, 0] {
be16(&mut out, n); // format, length, language, segCountX2, searchRange, entrySelector, rangeShift
}
for &(code, _) in &segments {
be16(&mut out, code); // endCode
}
be16(&mut out, 0); // reservedPad
for &(code, _) in &segments {
be16(&mut out, code); // startCode
}
for &(code, glyph) in &segments {
be16(&mut out, if code == 0xFFFF { 1 } else { glyph.wrapping_sub(code) }); // idDelta
}
for _ in &segments {
be16(&mut out, 0); // idRangeOffset
}
out
}
/// A TrueType font of [`GLYPHS`] glyphs, the `inked` ones squares, whose (3, 0) subtable maps
/// `windows` and whose (1, 0) subtable, if any, maps `mac`. A `named` font has a version 2.0 `post`
/// table naming its `inked` glyphs `bullet0`, `bullet1` and so on.
fn font(inked: &[u8], mac: Option<&[(u8, u8)]>, windows: &[(u8, u8)], named: bool) -> Vec<u8> {
// One contour, the square 0..1000 on both axes.
let mut square = Vec::new();
for n in [1u16, 0, 0, 1000, 1000] {
be16(&mut square, n);
}
be16(&mut square, 3); // the contour's last point
be16(&mut square, 0); // no instructions
square.extend([1u8; 4]); // four on-curve points, two-byte coordinates
for dx in [0i16, 1000, 0, -1000] {
square.extend_from_slice(&dx.to_be_bytes());
}
for dy in [0i16, 0, 1000, 0] {
square.extend_from_slice(&dy.to_be_bytes());
}
let mut glyf = Vec::new();
let mut loca = Vec::new();
for glyph in 0..GLYPHS {
be16(&mut loca, (glyf.len() / 2) as u16);
if inked.iter().any(|g| u16::from(*g) == glyph) {
glyf.extend_from_slice(&square);
}
}
be16(&mut loca, (glyf.len() / 2) as u16);
let mut head = vec![0; 54];
head[0..4].copy_from_slice(&[0, 1, 0, 0]);
head[12..16].copy_from_slice(&0x5f0f_3cf5u32.to_be_bytes());
head[18..20].copy_from_slice(&1000u16.to_be_bytes());
head[40..42].copy_from_slice(&1000i16.to_be_bytes());
head[42..44].copy_from_slice(&1000i16.to_be_bytes());
let mut hhea = vec![0; 36];
hhea[0..4].copy_from_slice(&[0, 1, 0, 0]);
hhea[4..6].copy_from_slice(&1000i16.to_be_bytes());
hhea[34..36].copy_from_slice(&GLYPHS.to_be_bytes());
let mut hmtx = Vec::new();
for _ in 0..GLYPHS {
be16(&mut hmtx, 1000);
be16(&mut hmtx, 0);
}
let mut maxp = vec![0, 1, 0, 0];
be16(&mut maxp, GLYPHS);
maxp.resize(32, 0);
let subtables: Vec<((u16, u16), Vec<u8>)> =
mac.map(|m| ((1, 0), mac_subtable(m))).into_iter().chain([((3, 0), windows_subtable(windows))]).collect();
let mut cmap = Vec::new();
be16(&mut cmap, 0);
be16(&mut cmap, subtables.len() as u16);
let mut offset = 4 + 8 * subtables.len() as u32;
for ((platform, encoding), bytes) in &subtables {
be16(&mut cmap, *platform);
be16(&mut cmap, *encoding);
be32(&mut cmap, offset);
offset += bytes.len() as u32;
}
for (_, bytes) in &subtables {
cmap.extend(bytes);
}
let mut tables =
vec![(*b"cmap", cmap), (*b"glyf", glyf), (*b"head", head), (*b"hhea", hhea), (*b"hmtx", hmtx), (*b"loca", loca), (*b"maxp", maxp)];
if named {
let mut post = vec![0, 2, 0, 0];
post.resize(32, 0);
be16(&mut post, GLYPHS);
for glyph in 0..GLYPHS {
// Index 258 and on are the table's own names, in order.
let own = inked.iter().position(|g| u16::from(*g) == glyph);
be16(&mut post, own.map_or(0, |i| 258 + i as u16));
}
for i in 0..inked.len() {
let name = format!("bullet{i}");
post.push(name.len() as u8);
post.extend(name.bytes());
}
tables.push((*b"post", post));
}
let mut out = vec![0, 1, 0, 0];
be16(&mut out, tables.len() as u16);
for n in [64u16, 2, 48] {
be16(&mut out, n);
}
let mut payload = Vec::new();
let mut offset = 12 + 16 * tables.len();
for (tag, bytes) in tables {
out.extend(tag);
let mut padded = bytes.clone();
padded.resize(bytes.len().div_ceil(4) * 4, 0);
let sum = padded.as_chunks::<4>().0.iter().fold(0u32, |s, b| s.wrapping_add(u32::from_be_bytes(*b)));
be32(&mut out, sum);
be32(&mut out, offset as u32);
be32(&mut out, bytes.len() as u32);
offset += padded.len();
payload.extend(padded);
}
out.extend(payload);
out
}
/// The text extracted from a page that shows `codes` in `font`, marked symbolic and with no
/// `/Encoding` and no `/ToUnicode`.
fn extracted(font: &[u8], codes: &[u8]) -> String {
extracted_with(font, codes, "AAAAAA+Sym", "")
}
/// [`extracted`] with the font dictionary's `/BaseFont` named `base_font` and the `extra` entries.
fn extracted_with(font: &[u8], codes: &[u8], base_font: &str, extra: &str) -> String {
let hex: String = codes.iter().map(|c| format!("{c:02X}")).collect();
let content = format!("BT /F1 30 Tf 5 5 Td <{hex}> Tj ET").into_bytes();
let widths = vec!["1000"; 256].join(" ");
let objects: Vec<Vec<u8>> = vec![
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 80 40] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>".to_vec(),
[format!("<< /Length {} >>\nstream\n", content.len()).into_bytes(), content.clone(), b"\nendstream".to_vec()].concat(),
format!("<< /Type /Font /Subtype /TrueType /BaseFont /{base_font} /FirstChar 0 /LastChar 255 /Widths [{widths}] /FontDescriptor 6 0 R {extra}>>").into_bytes(),
b"<< /Type /FontDescriptor /FontName /AAAAAA+Sym /Flags 4 /FontBBox [0 0 1000 1000] /ItalicAngle 0 /Ascent 1000 /Descent 0 /CapHeight 1000 /StemV 80 /FontFile2 7 0 R >>".to_vec(),
[format!("<< /Length {} >>\nstream\n", font.len()).into_bytes(), font.to_vec(), b"\nendstream".to_vec()].concat(),
];
let mut pdf = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, object) in objects.iter().enumerate() {
offsets.push(pdf.len());
pdf.extend(format!("{} 0 obj\n", i + 1).into_bytes());
pdf.extend(object);
pdf.extend(b"\nendobj\n");
}
let xref = pdf.len();
pdf.extend(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).into_bytes());
for offset in offsets {
pdf.extend(format!("{offset:010} 00000 n \n").into_bytes());
}
pdf.extend(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).into_bytes());
let mut r = PageRenderer::new(Arc::new(pdf), RenderConfig::default());
let out = r.render(RenderRequest { page: 0, kind: RequestKind::Text, tile: None, scale: 1.0, tag: 0, ..Default::default() });
assert!(out.error.is_none(), "{:?}", out.error);
out.text.map(|t| t.plain_text()).unwrap_or_default()
}
#[test]
fn a_symbolic_truetype_font_in_the_macintosh_order_reads_its_codes_as_mac_roman() {
// As issue2017r.pdf's font: code c and 0xF000 + c select the glyph the standard Macintosh
// order gives Mac OS Roman c.
let map = [(0x88, AGRAVE), (0x8E, EACUTE)];
let font = font(&[AGRAVE, EACUTE], Some(&map), &map, false);
assert_eq!(extracted(&font, &[0x88, 0x8E]), "àé", "Mac OS Roman 0x88 is à and 0x8E é");
}
#[test]
fn winansi_codes_of_a_font_in_the_macintosh_order_read_as_before() {
// A Windows subset in the same order: code 0x92 draws `quoteright`, WinAnsi 0x92, where Mac OS
// Roman 0x92 is `iacute`.
let map = [(0x92, QUOTERIGHT), (0x96, ENDASH)];
let font = font(&[ENDASH, QUOTERIGHT], Some(&map), &map, false);
assert_eq!(extracted(&font, &[0x92, 0x96]), "\u{2019}\u{2013}", "WinAnsi, not Mac OS Roman's íñ");
}
#[test]
fn a_symbolic_truetype_font_without_a_1_0_subtable_reads_as_before() {
let font = font(&[AGRAVE, EACUTE], None, &[(0x88, AGRAVE), (0x8E, EACUTE)], false);
assert_eq!(extracted(&font, &[0x88, 0x8E]), "ˆŽ", "with no (1, 0) subtable the codes read as WinAnsi");
}
#[test]
fn a_1_0_subtable_selecting_other_glyphs_than_the_drawn_ones_is_not_read() {
// The (3, 0) subtable is used first (#4156); a (1, 0) one that disagrees doesn't say what the
// drawn glyphs are.
let font = font(&[AGRAVE, EACUTE], Some(&[(0x88, EACUTE), (0x8E, AGRAVE)]), &[(0x88, AGRAVE), (0x8E, EACUTE)], false);
assert_eq!(extracted(&font, &[0x88, 0x8E]), "ˆŽ", "the codes read as WinAnsi, as before");
}
#[test]
fn a_font_that_names_its_glyphs_is_not_read_by_their_order() {
// A Wingdings subset draws code 0x83 with glyph 101, `Eacute` in the order, and names it
// `threesans`: a font with glyph names needn't keep the order.
let map = [(0x88, AGRAVE), (0x8E, EACUTE)];
let font = font(&[AGRAVE, EACUTE], Some(&map), &map, true);
assert_eq!(extracted(&font, &[0x88, 0x8E]), "ˆŽ", "the codes read as WinAnsi, as before");
}
#[test]
fn a_symbol_or_zapfdingbats_font_in_the_macintosh_order_gets_no_mac_roman_reading() {
// Symbol and ZapfDingbats have built-in encodings that aren't Latin (ISO 32000-2 §9.6.6.1), so their codes get
// no implicit Mac OS Roman reading, as the editor's `unicode_map` gives them none.
let map = [(0x88, AGRAVE), (0x8E, EACUTE)];
let font = font(&[AGRAVE, EACUTE], Some(&map), &map, false);
for name in ["ZapfDingbats", "AAAAAA+Symbol"] {
let text = extracted_with(&font, &[0x88, 0x8E], name, "");
assert_eq!(text, "ˆŽ", "{name}: the codes read as WinAnsi, as before");
}
}
#[test]
fn differences_naming_notdef_clear_the_mac_roman_reading_for_its_code() {
// `/Differences` without a base encoding: code 0x8E names `.notdef`, and code 0x88 keeps the Mac OS Roman reading.
let map = [(0x88, AGRAVE), (0x8E, EACUTE)];
let font = font(&[AGRAVE, EACUTE], Some(&map), &map, false);
let text = extracted_with(&font, &[0x88, 0x8E], "AAAAAA+Sym", "/Encoding << /Differences [142 /.notdef] >> ");
assert_eq!(text, "à", "0x8E reads as nothing");
}
#[test]
fn a_base_encoding_name_this_reader_does_not_know_keeps_the_mac_roman_reading_off() {
// An unknown `/Encoding` name is read as the built-in encoding, but it names a base encoding all the same, so the
// editor gives it no Mac OS Roman reading and neither does the renderer: the codes read as WinAnsi, as before.
let map = [(0x88, AGRAVE), (0x8E, EACUTE)];
let font = font(&[AGRAVE, EACUTE], Some(&map), &map, false);
assert_eq!(extracted_with(&font, &[0x88, 0x8E], "AAAAAA+Sym", "/Encoding /NoSuchEncoding "), "ˆŽ");
}
@@ -0,0 +1,147 @@
//! A TrueType font whose `head.indexToLocFormat` holds neither 0 nor 1 draws through the `loca`
//! format its table is in (vendored hayro-interpret patch 83). The OpenType `head` table allows
//! only 0 (short offsets) and 1 (long); the glyph reader took anything but 1 as short, so a font
//! whose field reads 256 (pdf.js `issue2537r.pdf`) had its long offsets read as short ones, and
//! "LINE UP" drew as boxes. ISO 32000-2 §9.9 has an embedded TrueType program drawn as the
//! font program it is.
//!
//! The font is built here, in memory: two glyphs (`.notdef`, empty, and a filled square) and a
//! (1, 0) `cmap` subtable mapping `A` to the square. No font file is involved.
use super::render_first_page;
fn be16(out: &mut Vec<u8>, n: u16) {
out.extend_from_slice(&n.to_be_bytes());
}
fn be32(out: &mut Vec<u8>, n: u32) {
out.extend_from_slice(&n.to_be_bytes());
}
/// The font, its `loca` offsets long when `long` and followed by `padding` zero bytes, its
/// `head.indexToLocFormat` set to `format`.
fn square_font(long: bool, padding: usize, format: u16) -> Vec<u8> {
// Glyph 1: one contour, the square 0..1000 on both axes (glyph 0 is empty).
let mut glyf = Vec::new();
for n in [1u16, 0, 0, 1000, 1000] {
be16(&mut glyf, n);
}
be16(&mut glyf, 3); // the contour's last point
be16(&mut glyf, 0); // no instructions
glyf.extend([1u8; 4]); // four on-curve points, two-byte coordinates
for dx in [0i16, 1000, 0, -1000] {
glyf.extend_from_slice(&dx.to_be_bytes());
}
for dy in [0i16, 0, 1000, 0] {
glyf.extend_from_slice(&dy.to_be_bytes());
}
let mut loca = Vec::new();
for offset in [0u32, 0, glyf.len() as u32] {
if long { be32(&mut loca, offset) } else { be16(&mut loca, (offset / 2) as u16) }
}
loca.resize(loca.len() + padding, 0);
let mut head = vec![0; 54];
head[0..4].copy_from_slice(&[0, 1, 0, 0]);
head[12..16].copy_from_slice(&0x5f0f_3cf5u32.to_be_bytes());
head[18..20].copy_from_slice(&1000u16.to_be_bytes());
head[40..42].copy_from_slice(&1000i16.to_be_bytes());
head[42..44].copy_from_slice(&1000i16.to_be_bytes());
head[50..52].copy_from_slice(&format.to_be_bytes());
let mut hhea = vec![0; 36];
hhea[0..4].copy_from_slice(&[0, 1, 0, 0]);
hhea[4..6].copy_from_slice(&1000i16.to_be_bytes());
hhea[34..36].copy_from_slice(&2u16.to_be_bytes());
let mut hmtx = Vec::new();
for _ in 0..2 {
be16(&mut hmtx, 1000);
be16(&mut hmtx, 0);
}
let mut maxp = vec![0, 1, 0, 0];
be16(&mut maxp, 2);
maxp.resize(32, 0);
// (1, 0), format 0: `A` → glyph 1 (the square).
let mut mac = Vec::new();
be16(&mut mac, 0);
be16(&mut mac, 262);
be16(&mut mac, 0);
let mut ids = [0u8; 256];
ids[usize::from(b'A')] = 1;
mac.extend(ids);
let mut cmap = Vec::new();
be16(&mut cmap, 0);
be16(&mut cmap, 1);
be16(&mut cmap, 1);
be16(&mut cmap, 0);
be32(&mut cmap, 12);
cmap.extend(mac);
let tables = [(*b"cmap", cmap), (*b"glyf", glyf), (*b"head", head), (*b"hhea", hhea), (*b"hmtx", hmtx), (*b"loca", loca), (*b"maxp", maxp)];
let mut out = vec![0, 1, 0, 0];
be16(&mut out, tables.len() as u16);
for n in [64u16, 2, 48] {
be16(&mut out, n);
}
let mut payload = Vec::new();
let mut offset = 12 + 16 * tables.len();
for (tag, bytes) in tables {
out.extend(tag);
let mut padded = bytes.clone();
padded.resize(bytes.len().div_ceil(4) * 4, 0);
let sum = padded.as_chunks::<4>().0.iter().fold(0u32, |s, b| s.wrapping_add(u32::from_be_bytes(*b)));
be32(&mut out, sum);
be32(&mut out, offset as u32);
be32(&mut out, bytes.len() as u32);
offset += padded.len();
payload.extend(padded);
}
out.extend(payload);
out
}
/// A 40 × 40 pt page that shows `A` at 30 pt from (5, 5) in the font.
fn page_with_font(font: &[u8]) -> Vec<u8> {
let content = b"BT /F1 30 Tf 5 5 Td (A) Tj ET".to_vec();
let objects: Vec<Vec<u8>> = vec![
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 40 40] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>".to_vec(),
[format!("<< /Length {} >>\nstream\n", content.len()).into_bytes(), content.clone(), b"\nendstream".to_vec()].concat(),
b"<< /Type /Font /Subtype /TrueType /BaseFont /AAAAAA+Sq /FirstChar 65 /LastChar 65 /Widths [1000] /FontDescriptor 6 0 R >>".to_vec(),
b"<< /Type /FontDescriptor /FontName /AAAAAA+Sq /Flags 4 /FontBBox [0 0 1000 1000] /ItalicAngle 0 /Ascent 1000 /Descent 0 /CapHeight 1000 /StemV 80 /FontFile2 7 0 R >>".to_vec(),
[format!("<< /Length {} >>\nstream\n", font.len()).into_bytes(), font.to_vec(), b"\nendstream".to_vec()].concat(),
];
let mut pdf = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, object) in objects.iter().enumerate() {
offsets.push(pdf.len());
pdf.extend(format!("{} 0 obj\n", i + 1).into_bytes());
pdf.extend(object);
pdf.extend(b"\nendobj\n");
}
let xref = pdf.len();
pdf.extend(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).into_bytes());
for offset in offsets {
pdf.extend(format!("{offset:010} 00000 n \n").into_bytes());
}
pdf.extend(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).into_bytes());
pdf
}
#[test]
fn a_truetype_font_with_an_out_of_range_loca_format_draws_its_glyphs() {
// The square covers (5, 5)–(35, 35) in page space: row 20 from the top is inside it.
let square_drawn = |long: bool, padding: usize, format: u16| {
let page = render_first_page(page_with_font(&square_font(long, padding, format)));
let at = |x: u32, y: u32| page.rgba.get(((y * page.width + x) * 4) as usize).copied().unwrap_or(255);
assert_eq!(at(2, 2), 255, "nothing is drawn outside the square");
at(20, 20) < 128
};
assert!(square_drawn(true, 0, 1), "long offsets, marked long");
assert!(square_drawn(false, 0, 0), "short offsets, marked short");
assert!(square_drawn(true, 0, 0x0100), "long offsets marked 256: the table's length says long");
assert!(square_drawn(false, 0, 0x0100), "short offsets marked 256: the table's length says short");
// Three short offsets are 6 bytes and three long ones 12: 8 bytes is short ones and padding.
assert!(square_drawn(false, 2, 0x0100), "short offsets and padding marked 256: still short");
}
@@ -0,0 +1,152 @@
//! A Type 3 glyph shown many times decodes its glyph procedure once (vendored hayro-interpret
//! patch 87). hayro inflated the procedure again for every copy of the glyph, so a font used as
//! a fill pattern, one glyph drawn hundreds of thousands of times, took seconds a page.
use super::image_cache_pdf;
use hayro::hayro_interpret::font::Glyph;
use hayro::hayro_interpret::hayro_syntax::Pdf;
use hayro::hayro_interpret::{
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, SoftMask, TransformExt,
interpret_page,
};
use kurbo::{Affine, BezPath, Rect, Shape};
use std::io::Write;
use std::sync::Arc;
/// Records the width in device pixels of each path drawn, and draws each Type 3 glyph through its
/// procedure, as the renderer does.
#[derive(Default)]
struct Paths(Vec<f64>);
impl<'a> Device<'a> for Paths {
fn set_soft_mask(&mut self, _: Option<SoftMask<'a>>) {}
fn set_blend_mode(&mut self, _: BlendMode) {}
fn draw_path(&mut self, path: &BezPath, transform: Affine, _: &Paint<'a>, _: &PathDrawMode) {
self.0.push((transform * path.clone()).bounding_box().width());
}
fn push_clip_path(&mut self, _: &ClipPath) {}
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'a>>, _: BlendMode) {}
fn draw_glyph(&mut self, glyph: &Glyph<'a>, transform: Affine, glyph_transform: Affine, paint: &Paint<'a>, _: &GlyphDrawMode) {
if let Glyph::Type3(glyph) = glyph {
glyph.interpret(self, transform, glyph_transform, paint);
}
}
fn draw_image(&mut self, _: Image<'a, '_>, _: Affine) {}
fn pop_clip_path(&mut self) {}
fn pop_transparency_group(&mut self) {}
}
fn flate(data: &[u8]) -> Vec<u8> {
let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast());
z.write_all(data).unwrap();
z.finish().unwrap()
}
/// A Type 3 font on an 8 × 8 grid whose glyphs, named `names`, take the codes from 33 on and
/// have their procedures in the objects from `first_object` on.
fn font(names: &[String], first_object: usize) -> String {
let procs: String = names.iter().enumerate().map(|(i, n)| format!("/{n} {} 0 R ", first_object + i)).collect();
let names_list: String = names.iter().map(|n| format!("/{n} ")).collect();
format!(
"<< /Type /Font /Subtype /Type3 /FontBBox [0 0 8 8] /FontMatrix [0.125 0 0 0.125 0 0] /CharProcs << {procs}>> \
/Encoding << /Type /Encoding /Differences [33 {names_list}] >> /FirstChar 33 /LastChar {} /Widths [{}] >>",
32 + names.len(),
"8 ".repeat(names.len())
)
}
/// Renders the page of `bytes` through a new cache; the widths of the paths drawn, and how many
/// glyph procedures were decoded.
fn render(bytes: Vec<u8>) -> (Vec<f64>, usize) {
let parsed = Pdf::new(Arc::new(bytes)).expect("parses");
let page = &parsed.pages()[0];
let initial = page.initial_transform(true).to_kurbo();
let cache = InterpreterCache::new();
let mut ctx = Context::new(initial, Rect::new(0.0, 0.0, 130.0, 70.0), &cache, page.xref(), InterpreterSettings::default());
let mut device = Paths::default();
interpret_page(page, &mut ctx, &mut device);
(device.0, cache.type3_proc_decodes())
}
/// A Type 3 font whose one glyph, `!`, is a Flate-compressed procedure filling its 8 × 8 box,
/// shown 200 times on one page and rendered twice through one cache: every copy draws, and the
/// procedure is decoded once, on the first copy.
#[test]
fn a_type3_glyph_shown_again_decodes_its_procedure_once() {
let proc_bytes = flate(b"8 0 0 0 8 8 d1 0 0 8 8 re f");
let proc_dict = format!("<< /Filter /FlateDecode /Length {} >>", proc_bytes.len());
let content = format!("BT /F1 0.5 Tf 1 0 0 1 2 30 Tm ({}) Tj ET", "!".repeat(200));
let resources = format!("<< /Font << /F1 {} >> >>", font(&["dot".into()], 5));
let parsed = Pdf::new(Arc::new(image_cache_pdf(&content, &resources, &[(&proc_dict, &proc_bytes)]))).expect("parses");
let page = &parsed.pages()[0];
let cache = InterpreterCache::new();
for render in ["first", "second"] {
let initial = page.initial_transform(true).to_kurbo();
let mut ctx = Context::new(initial, Rect::new(0.0, 0.0, 130.0, 70.0), &cache, page.xref(), InterpreterSettings::default());
let mut device = Paths::default();
interpret_page(page, &mut ctx, &mut device);
assert_eq!(device.0.len(), 200, "the {render} render draws every copy of the glyph");
assert_eq!(cache.type3_proc_decodes(), 1, "after the {render} render: the procedure is decoded once");
}
}
/// A procedure that decodes to more than 64 KiB isn't kept: shown three times, it is decoded three
/// times, and draws each time.
#[test]
fn a_procedure_over_64_kib_is_decoded_for_each_copy() {
let mut program = b"8 0 0 0 8 8 d1 0 0 8 8 re f".to_vec();
program.resize((64 << 10) + 1, b' ');
let proc_bytes = flate(&program);
let proc_dict = format!("<< /Filter /FlateDecode /Length {} >>", proc_bytes.len());
let resources = format!("<< /Font << /F1 {} >> >>", font(&["big".into()], 5));
let bytes = image_cache_pdf("BT /F1 0.5 Tf 1 0 0 1 2 30 Tm (!!!) Tj ET", &resources, &[(&proc_dict, &proc_bytes)]);
let (widths, decodes) = render(bytes);
assert_eq!(widths.len(), 3);
assert_eq!(decodes, 3, "a procedure over the size kept is decoded for each copy");
}
/// The fonts of one cache keep at most 32 MiB of procedures together: 600 glyphs of 60,000 bytes
/// each, over three fonts (36 MB), each shown twice, all draw; the procedures kept are decoded
/// once and the rest twice, so there are more decodes than glyphs and fewer than copies.
#[test]
fn the_procedures_one_cache_keeps_stop_at_32_mib() {
let mut program = b"8 0 0 0 8 8 d1 0 0 8 8 re f".to_vec();
program.resize(60_000, b' ');
let proc_bytes = flate(&program);
let proc_dict = format!("<< /Filter /FlateDecode /Length {} >>", proc_bytes.len());
let (fonts, per_font) = (3, 200);
let mut resources = String::from("<< /Font << ");
let mut content = String::new();
for f in 0..fonts {
let names: Vec<String> = (0..per_font).map(|g| format!("g{f}x{g}")).collect();
resources.push_str(&format!("/F{f} {} ", font(&names, 5 + f * per_font)));
let codes: String = (0..per_font).map(|g| format!("{:02x}", 33 + g)).collect();
content.push_str(&format!("BT /F{f} 0.05 Tf 1 0 0 1 2 {} Tm <{codes}{codes}> Tj ET ", 10 + 20 * f));
}
resources.push_str(">> >>");
let objects: Vec<(&str, &[u8])> = (0..fonts * per_font).map(|_| (proc_dict.as_str(), proc_bytes.as_slice())).collect();
let bytes = image_cache_pdf(&content, &resources, &objects);
let (widths, decodes) = render(bytes);
assert_eq!(widths.len(), 2 * fonts * per_font, "every copy draws");
assert!(decodes > fonts * per_font && decodes < 2 * fonts * per_font, "{decodes} decodes: some procedures kept, not all");
}
/// Two fonts each with a glyph named `dot`, one filling 8 × 8 and the other 4 × 4, draw their own
/// procedures: what one font keeps isn't looked up by another with the same glyph name.
#[test]
fn fonts_with_the_same_glyph_name_keep_their_own_procedures() {
let (big, small) = (b"8 0 0 0 8 8 d1 0 0 8 8 re f".to_vec(), b"8 0 0 0 8 8 d1 0 0 4 4 re f".to_vec());
let dict = |data: &[u8]| format!("<< /Length {} >>", data.len());
let (big_dict, small_dict) = (dict(&big), dict(&small));
let resources = format!("<< /Font << /A {} /B {} >> >>", font(&["dot".into()], 5), font(&["dot".into()], 6));
let bytes = image_cache_pdf(
"BT /A 8 Tf 1 0 0 1 2 30 Tm (!!) Tj /B 8 Tf (!!) Tj ET",
&resources,
&[(big_dict.as_str(), big.as_slice()), (small_dict.as_str(), small.as_slice())],
);
let (widths, _) = render(bytes);
assert_eq!(widths.len(), 4);
let (a, b) = (widths[0], widths[3]);
assert!((a - 2.0 * b).abs() < 0.01, "font A's glyph is twice as wide as font B's: {widths:?}");
assert_eq!((widths[1], widths[2]), (a, b), "each font draws its own glyph again: {widths:?}");
}
@@ -0,0 +1,130 @@
//! A Type 3 font without `/Widths` advances each glyph by the width its glyph procedure sets
//! with `d0` or `d1` (vendored hayro-interpret patch 90). ISO 32000-2 Table 110 requires
//! `/Widths` in a Type 3 font, and §9.6.4 has the procedure state the same width: "wx … shall
//! be consistent with the corresponding width in the font's Widths array". A font whose
//! `/Widths` key was misspelt (PDFBOX-2943) gave every glyph no advance, so its text was drawn
//! as one blot and read as characters stacked on one spot. PDFium takes the procedure's width
//! here. A font that has `/Widths` is unchanged: Table 110 gives a code outside `/FirstChar` to
//! `/LastChar` width 0, as Poppler and MuPDF draw it.
//!
//! The glyphs are squares 400 units wide (`a`, set by `d1`, advance 500) and 600 units wide
//! (`b`, set by `d0`, advance 700), shown at 100 pt from x = 20.
use super::render_first_page;
use crate::RenderWarning;
use crate::text::extract_page;
use hayro::hayro_interpret::InterpreterSettings;
use hayro::hayro_interpret::hayro_syntax::Pdf;
use std::sync::Arc;
/// A page showing `aabb` in a Type 3 font whose dictionary ends with `widths`.
fn type3_page(widths: &str) -> Vec<u8> {
type3_page_with(widths, "aabb", "700 0 d0 0 0 600 600 re f")
}
/// A page showing `text` in a Type 3 font whose dictionary ends with `widths` and whose `b`
/// glyph is `glyph_b`.
fn type3_page_with(widths: &str, text: &str, glyph_b: &str) -> Vec<u8> {
let content = format!("BT /F1 100 Tf 20 100 Td ({text}) Tj ET");
let glyph_a = "500 0 0 0 400 400 d1 0 0 400 400 re f";
let objects = [
"<< /Type /Catalog /Pages 2 0 R >>".to_string(),
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 200] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>".to_string(),
format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),
format!(
"<< /Type /Font /Subtype /Type3 /FontBBox [0 0 1000 1000] /FontMatrix [0.001 0 0 0.001 0 0] \
/CharProcs << /a 6 0 R /b 7 0 R >> /Encoding << /Type /Encoding /Differences [97 /a /b] >> {widths} >>"
),
format!("<< /Length {} >>\nstream\n{glyph_a}\nendstream", glyph_a.len()),
format!("<< /Length {} >>\nstream\n{glyph_b}\nendstream", glyph_b.len()),
];
let mut pdf = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, object) in objects.iter().enumerate() {
offsets.push(pdf.len());
pdf.extend(format!("{} 0 obj\n{object}\nendobj\n", i + 1).into_bytes());
}
let xref = pdf.len();
pdf.extend(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).into_bytes());
for offset in offsets {
pdf.extend(format!("{offset:010} 00000 n \n").into_bytes());
}
pdf.extend(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).into_bytes());
pdf
}
/// The columns (start, end) of the rendered page that hold ink, as runs.
fn ink_runs(pdf: Vec<u8>) -> Vec<(u32, u32)> {
let page = render_first_page(pdf);
let inked = |x: u32| (0..page.height).any(|y| page.rgba.get(((y * page.width + x) * 4) as usize).is_some_and(|v| *v < 128));
let mut runs = Vec::new();
let mut start = None;
for x in 0..=page.width {
match (x < page.width && inked(x), start) {
(true, None) => start = Some(x),
(false, Some(s)) => {
runs.push((s, x));
start = None;
}
_ => {}
}
}
runs
}
/// The left edge of each extracted glyph, rounded to a point.
fn glyph_lefts(pdf: Vec<u8>) -> Vec<i32> {
let pdf = Pdf::new(Arc::new(pdf)).expect("parses");
let text = extract_page(&pdf, 0, &InterpreterSettings::default()).expect("text");
text.glyphs.iter().map(|g| g.rect[0].round() as i32).collect()
}
#[test]
fn a_type3_font_without_widths_advances_by_its_glyph_procedures() {
// Each glyph in its own place: a at 20 and 70 (d1 width 500), b at 120 and 190 (d0 700).
for widths in ["", "/FirstChar 97 /LastChar 98 /WXdths [300 300]"] {
assert_eq!(ink_runs(type3_page(widths)), [(20, 60), (70, 110), (120, 180), (190, 250)], "{widths:?}");
assert_eq!(glyph_lefts(type3_page(widths)), [20, 70, 120, 190], "{widths:?}");
}
}
#[test]
fn a_code_outside_first_char_to_last_char_still_has_width_zero() {
// a takes /Widths (300); b, past /LastChar, has width 0 (Table 110), whatever d0 says.
let widths = "/FirstChar 97 /LastChar 97 /Widths [300]";
assert_eq!(ink_runs(type3_page(widths)), [(20, 140)]);
assert_eq!(glyph_lefts(type3_page(widths)), [20, 50, 80]);
}
#[test]
fn widths_the_font_gives_are_kept() {
// /Widths says 300 for both, whatever the procedures say: the glyphs overlap, as written.
let widths = "/FirstChar 97 /LastChar 98 /Widths [300 300]";
assert_eq!(ink_runs(type3_page(widths)), [(20, 170)]);
assert_eq!(glyph_lefts(type3_page(widths)), [20, 50, 80, 110]);
// An explicit 0 is an entry too: every glyph stays at x = 20 (text extraction keeps one
// of each glyph drawn again on the same spot).
let zero = "/FirstChar 97 /LastChar 98 /Widths [0 0]";
assert_eq!(glyph_lefts(type3_page(zero)), [20, 20]);
}
#[test]
fn a_code_without_a_usable_procedure_width_still_advances_nothing() {
// b's d0 has a name for its width and c has no procedure: neither advances, and neither
// panics.
assert_eq!(glyph_lefts(type3_page_with("", "bcb", "/x 0 d0 0 0 600 600 re f")), [20, 20]);
}
#[test]
fn a_type3_font_without_widths_is_reported() {
// The page draws, and says once that a font's /Widths was missing; a font with /Widths
// (even one that leaves `b` out) says nothing.
for widths in ["", "/FirstChar 97 /LastChar 98 /WXdths [300 300]"] {
let page = render_first_page(type3_page(widths));
assert_eq!(page.warnings, [RenderWarning::Type3WidthsMissing], "{widths:?}");
}
for widths in ["/FirstChar 97 /LastChar 98 /Widths [300 300]", "/FirstChar 97 /LastChar 97 /Widths [300]"] {
assert!(render_first_page(type3_page(widths)).warnings.is_empty(), "{widths:?}");
}
}
+11 -3
View File
@@ -458,9 +458,17 @@ impl<'a> Device<'a> for TextDevice {
eprintln!("{kind} {text:?} transform={:?} glyph_transform={:?}", transform.as_coeffs(), glyph_transform.as_coeffs());
}
let advance = match glyph {
Glyph::Outline(g) => {
g.advance_width().filter(|a| *a > 0.0).map(f64::from).unwrap_or_else(|| g.outline().bounding_box().width().max(500.0))
}
// A vertical font's advance runs down the column; across it, the glyph is as wide as
// its horizontal width w0, centred on the column by default (ISO 32000-2 §9.7.4.3). Taking
// the outline's width instead gave a column of Latin letters boxes of uneven widths,
// off its centre, and a narrow letter cut the line's gap allowance below the spacing
// of widely tracked text, so the column fell apart into lines.
Glyph::Outline(g) => g
.vertical_glyph_width()
.or_else(|| g.advance_width())
.filter(|a| a.is_finite() && *a > 0.0)
.map(f64::from)
.unwrap_or_else(|| g.outline().bounding_box().width().max(500.0)),
Glyph::Type3(g) => g.advance_width().filter(|a| a.is_finite() && *a > 0.0).map(f64::from).unwrap_or(600.0),
};
// The baseline direction in view space (y down).
+432 -6
View File
@@ -664,10 +664,12 @@ fn the_annex_d2_alias_codes_read_as_an_ordinary_hyphen_and_space() {
/// #3169, #3170: glyph names outside the Adobe Glyph List still read as text. Ligature
/// (`f_i`) and suffixed (`hyphen.case`) names go by the glyph list specification's full
/// mapping. A name that only numbers the glyph (`c251` small caps, `C39`, `C174`, `C39.sc`)
/// reads as nothing, not as a guess from the code (review on #3239: with `/C1` at 233,
/// StandardEncoding's 233 would read `Ø` for a glyph drawn as `é`), and so do a ZapfDingbats
/// name (`a12`) and a name with no meaning left (`foo`).
/// mapping. A name that only numbers the glyph reads as nothing, not as a guess from the code
/// (review on #3239: with `/C1` at 233, StandardEncoding's 233 would read `Ø` for a glyph drawn
/// as `é`). `C39` at 39 and `C174` at 174 name their own codes, but this font's other numbered
/// names (`c251` at 97, `C39.sc` at 61, `C1` at 233, `a12` at 88) don't, so they read as
/// nothing too (see `numbered_glyph_names_read_as_their_own_code`). A name with no meaning
/// left (`foo`) reads as nothing.
#[test]
fn glyph_names_outside_the_glyph_list_still_read_as_text() {
use lopdf::{Object, dictionary};
@@ -696,11 +698,145 @@ fn glyph_names_outside_the_glyph_list_still_read_as_text() {
assert_eq!(lines, ["fi", "fl", "-", "fi"], "{text:?}");
}
/// A glyph name that only numbers its glyph, with no `/ToUnicode` (ISO 32000-2 §9.10.2 has no
/// route for it), reads as its code's Latin-1 character when the number is that very code, in
/// decimal (`C70` at 70, `C0071` at 71, `MT72` at 72, `C39` at 39, `C174` at 174) or
/// hexadecimal (`G49` at 0x49, `GE9` at 0xE9), as pdftotext, PDFium and MuPDF read
/// it: Windows printer drivers and typesetters name subset glyphs so. Only in a font where every
/// numbered name names its own code: one more name numbering another code (`/C1` at 233, review
/// on #3239) turns the whole font back to nothing, and so does a font whose names number
/// something else, where a name can match its code by coincidence (`/MT40` at 0x40). A subset
/// that counts its glyphs in order of first use from code 1 (`/Differences [1 /C1 /C2 … /C80]`)
/// names its own codes too, but `/C65` there is the 65th glyph used, not `A`: a numbered name at
/// a control code (`C1` at 1, `C9` at 9) gives the counter away and the font reads nothing.
/// Code 32 is the first code a numbered name may name: `C32` at 32 reads as a space.
#[test]
fn numbered_glyph_names_read_as_their_own_code() {
use lopdf::{Object, dictionary};
let name = |n: &str| Object::Name(n.as_bytes().to_vec());
let encoding = |differences: Vec<Object>| Object::Dictionary(dictionary! { "Type" => "Encoding", "Differences" => differences });
// One glyph per line, as in `glyph_names_outside_the_glyph_list_still_read_as_text`.
let one_per_line = |codes: &[u8]| {
let mut content = b"BT /F1 20 Tf 20 290 Td".to_vec();
for code in codes {
content.extend_from_slice(format!(" <{code:02x}> Tj 0 -25 Td").as_bytes());
}
content.extend_from_slice(b" ET");
content
};
let own = encoding(vec![
Object::Integer(70),
name("C70"),
name("C0071"),
name("MT72"),
name("G49"),
Object::Integer(233),
name("GE9"),
Object::Integer(39),
name("C39"),
Object::Integer(174),
name("C174"),
]);
let one_other = encoding(vec![Object::Integer(70), name("C70"), Object::Integer(233), name("C1")]);
let numbered_otherwise = encoding(vec![Object::Integer(1), name("C70"), name("C105"), Object::Integer(64), name("MT40")]);
let counter = encoding(std::iter::once(Object::Integer(1)).chain((1..=80).map(|n| name(&format!("C{n}")))).collect());
let own_and_a_control = encoding(vec![Object::Integer(9), name("C9"), Object::Integer(70), name("C70"), name("C0071")]);
// Code 32 is the first code a numbered name may name: `/C32` at 32 is a space, not a counter.
let own_from_32 = encoding(vec![Object::Integer(32), name("C32"), Object::Integer(70), name("C70"), name("C0071")]);
let pages = [
one_per_line(b"FGHI\xe9'\xae"),
one_per_line(b"F\xe9"),
one_per_line(b"\x01\x02@"),
one_per_line(b"\x01\x20\x41\x42\x50"),
one_per_line(b"\x09FG"),
b"BT /F1 20 Tf 20 290 Td <462047> Tj ET".to_vec(),
];
let pdf = encoded_pages(&[
(own, &pages[0][..]),
(one_other, &pages[1][..]),
(numbered_otherwise, &pages[2][..]),
(counter, &pages[3][..]),
(own_and_a_control, &pages[4][..]),
(own_from_32, &pages[5][..]),
]);
let lines = |page| {
let text = extract_page(&pdf, page, &InterpreterSettings::default()).unwrap().plain_text();
text.lines().map(str::trim).filter(|l| !l.is_empty()).map(String::from).collect::<Vec<_>>()
};
assert_eq!(lines(0), ["F", "G", "H", "I", "\u{e9}", "'", "\u{ae}"]);
assert!(lines(1).is_empty(), "/C1 at 233 beside /C70 at 70: {:?}", lines(1));
assert!(lines(2).is_empty(), "/MT40 at 0x40 among names numbering other codes: {:?}", lines(2));
assert!(lines(3).is_empty(), "a first-use counter /C1 … /C80 from code 1: {:?}", lines(3));
assert!(lines(4).is_empty(), "/C9 at 9 beside /C70 at 70: {:?}", lines(4));
let page5 = extract_page(&pdf, 5, &InterpreterSettings::default()).unwrap().plain_text();
assert_eq!(page5.trim_matches('\n'), "F G", "/C32 at 32 beside /C70 at 70 reads as a space");
}
/// `numbered_glyph_names_read_as_their_own_code` in a Type 3 font, which already reads codes
/// 32-126 by the code: above 126, `C174` at 174, `C176` at 176 and `GE9` at 0xE9 read as the
/// code's Latin-1 character, unless one numbered name in the font numbers another code
/// (`/C1` at 233) or sits at a control code, as a first-use counter's `/C1` at 1 does.
#[test]
fn numbered_type3_glyph_names_read_as_their_own_code() {
use lopdf::{Document, Object, Stream, dictionary};
let name = |n: &str| Object::Name(n.as_bytes().to_vec());
// Each font's /Differences as (code, glyph name) pairs, and the codes its page shows.
type Names<'a> = &'a [(i64, &'a str)];
let fonts: [(Names, &[u8]); 3] = [
(&[(70, "C70"), (174, "C174"), (176, "C176"), (233, "GE9")], b"F\xae\xb0\xe9"),
(&[(174, "C174"), (176, "C176"), (233, "C1")], b"\xae\xb0\xe9"),
(&[(1, "C1"), (2, "C2"), (174, "C174"), (176, "C176")], b"\xae\xb0"),
];
let mut doc = Document::with_version("1.7");
let pages_id = doc.new_object_id();
let mut kids = Vec::new();
for (names, codes) in fonts {
let glyph = doc.add_object(Stream::new(dictionary! {}, b"600 0 0 0 500 700 d1 0 0 500 700 re f".to_vec()));
let mut differences = Vec::new();
let mut procs = lopdf::Dictionary::new();
for &(code, glyph_name) in names {
differences.extend([Object::Integer(code), name(glyph_name)]);
procs.set(glyph_name, glyph);
}
let font = doc.add_object(dictionary! {
"Type" => "Font", "Subtype" => "Type3",
"FontBBox" => vec![0.into(), 0.into(), 1000.into(), 1000.into()],
"FontMatrix" => vec![0.001.into(), 0.into(), 0.into(), 0.001.into(), 0.into(), 0.into()],
"CharProcs" => procs,
"Encoding" => dictionary! { "Type" => "Encoding", "Differences" => differences },
"FirstChar" => 0, "LastChar" => 255, "Widths" => vec![Object::Integer(600); 256],
});
// One glyph per line, as in `glyph_names_outside_the_glyph_list_still_read_as_text`.
let mut content = b"BT /T3 20 Tf 20 290 Td".to_vec();
for code in codes {
content.extend_from_slice(format!(" <{code:02x}> Tj 0 -25 Td").as_bytes());
}
content.extend_from_slice(b" ET");
let content = doc.add_object(Stream::new(dictionary! {}, content));
let page = doc.add_object(dictionary! { "Type" => "Page", "Parent" => pages_id, "MediaBox" => vec![0.into(), 0.into(), 300.into(), 300.into()], "Resources" => dictionary! {"Font" => dictionary! {"T3" => font}}, "Contents" => content });
kids.push(Object::Reference(page));
}
let count = kids.len() as i64;
doc.objects.insert(pages_id, dictionary! {"Type" => "Pages", "Kids" => kids, "Count" => count}.into());
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages_id});
doc.trailer.set("Root", catalog);
let mut bytes = Vec::new();
doc.save_to(&mut bytes).unwrap();
let pdf = Pdf::new(std::sync::Arc::new(bytes)).unwrap();
let lines = |page| {
let text = extract_page(&pdf, page, &InterpreterSettings::default()).unwrap().plain_text();
text.lines().map(str::trim).filter(|l| !l.is_empty()).map(String::from).collect::<Vec<_>>()
};
assert_eq!(lines(0), ["F", "\u{ae}", "\u{b0}", "\u{e9}"]);
assert!(lines(1).is_empty(), "/C1 at 233 beside /C174 at 174: {:?}", lines(1));
assert!(lines(2).is_empty(), "a counter's /C1 at 1 beside /C174 at 174: {:?}", lines(2));
}
/// Review on #3239: the glyph list specification's `uni` and `u` forms take uppercase hex
/// digits only, and map a name with lowercase ones to nothing ("uni20ac" in its examples).
/// Names upstream's own lookup can't read (several groups, a suffix) show it: lowercase reads
/// as nothing, uppercase as its characters. A bare `uni20ac` or `u1040c` is still read by
/// upstream's lenient `unicode_from_name`, outside this mapping.
/// as nothing, uppercase as its characters. A bare `uni20ac` or `u1040c` reads as nothing too
/// (`agl_unicode_from_name_rule_reads_text`).
#[test]
fn glyph_names_with_lowercase_hex_read_as_nothing() {
use lopdf::{Object, dictionary};
@@ -720,6 +856,34 @@ fn glyph_names_with_lowercase_hex_read_as_nothing() {
assert_eq!(lines, ["\u{20ac}\u{308}", "\u{1040c}", "f", "f\u{20ac}"], "{text:?}");
}
/// The glyph list specification's `uni` form is `uni` and groups of exactly four uppercase hex
/// digits, its `u` form `u` and four to six, outside the surrogate range: `uni65`, `u65`,
/// `uni20ac`, `u1f600`, `uni+065` and `uniD800` name no character, and their glyphs read as
/// nothing rather than as `e`, `€` or an emoji. The well-formed names read as before.
#[test]
fn agl_unicode_from_name_rule_reads_text() {
use lopdf::{Object, dictionary};
let name = |n: &str| Object::Name(n.as_bytes().to_vec());
let names = ["uni65", "u65", "uni20ac", "u1f600", "uni+065", "uniD800", "u10FFFF0", "uni0065", "u0066", "u1F600", "uni20AC"];
let differences = Object::Dictionary(dictionary! {
"Type" => "Encoding",
"Differences" => std::iter::once(Object::Integer(65)).chain(names.iter().map(|n| name(n))).collect::<Vec<_>>(),
});
// One glyph per line, so each name's text stays its own (and a name read as nothing
// leaves no line).
let mut content = b"BT /F1 20 Tf 20 290 Td".to_vec();
for code in (b'A'..).take(names.len()) {
content.extend_from_slice(b" (");
content.push(code);
content.extend_from_slice(b") Tj 0 -25 Td");
}
content.extend_from_slice(b" ET");
let pdf = encoded_pages(&[(differences, &content[..])]);
let text = extract_page(&pdf, 0, &InterpreterSettings::default()).unwrap().plain_text();
let lines: Vec<&str> = text.lines().map(str::trim).filter(|l| !l.is_empty()).collect();
assert_eq!(lines, ["e", "f", "\u{1f600}", "\u{20ac}"], "{text:?}");
}
#[test]
fn narrow_ligatures_keep_every_letter() {
// Split evenly, this "ffi" put its two "f"s so close that the second was dropped as a
@@ -771,6 +935,113 @@ fn vertical_ligatures_stay_in_their_column() {
assert_eq!(text.line_rects(0..3).len(), 1, "one column");
}
/// A column of Latin capitals in a vertical font, tracked out with negative character spacing
/// (pdf.js issue 20930: `-40 Tc` puts 40 pt between 24 pt letters). Across the column each glyph
/// is as wide as its `/W` width w0 and centred on the column (ISO 32000-2 §9.7.4.3), so the
/// letters stay one line and Find matches the word. Boxes as wide as each outline made a narrow
/// letter's line allowance smaller than the tracking, and the column broke into lines.
#[test]
fn tracked_vertical_latin_stays_one_line() {
tracked_vertical_latin(-40.0, 100.0);
}
#[test]
fn horizontally_scaled_vertical_latin_box_is_scaled_across_the_column() {
// Horizontal scaling Th = Tz / 100 scales the glyph's width w0 across the column (ISO 32000-2
// §9.4.4, the text rendering matrix), and not the advance down it. The tracking is 20 pt here:
// the line's gap allowance is three times the narrower box across it, 3 × 8 pt at 50 Tz.
tracked_vertical_latin(-20.0, 50.0);
}
/// `ABCDE` down one Identity-V column at 24 pt with `tc Tc` and `tz Tz`.
fn tracked_vertical_latin(tc: f32, tz: f32) {
let text = vertical_latin(
&[(36, 667), (37, 667), (38, 722), (39, 722), (40, 667)],
None,
&format!("BT /F1 24 Tf {tc} Tc {tz} Tz 1 0 0 1 100 340 Tm <00240025002600270028> Tj ET"),
);
assert_eq!(text.plain_text(), "ABCDE");
assert_eq!(text.find("ABCDE"), vec![0..5]);
// Each box spans w0 (here 24 pt × 0.667 or 0.722, times Th) centred on the column at x = 100.
assert_eq!(text.glyphs.len(), 5);
for (g, w0) in text.glyphs.iter().zip([667.0, 667.0, 722.0, 722.0, 667.0]) {
let [x0, _, x1, _] = g.rect;
assert!((x1 - x0 - 24.0 * w0 / 1000.0 * tz / 100.0).abs() < 0.01, "{:?} is w0 wide: {:?}", g.text, g.rect);
assert!(((x0 + x1) / 2.0 - 100.0).abs() < 0.01, "{:?} is centred on the column: {:?}", g.text, g.rect);
}
}
/// The text of a page showing `content` in a vertical CIDFontType2 `/F1` with `/DW 1000`, the
/// `widths` (CID, w0) in `/W`, and CIDs 36 to 41 reading `A` to `F`. `cmap` is the body of an
/// embedded vertical CMap mapping codes to CIDs (`begincidchar` … `endcidchar`); `None` uses
/// Identity-V.
fn vertical_latin(widths: &[(i64, i64)], cmap: Option<&str>, content: &str) -> crate::text::PageText {
use lopdf::{Document, Object, Stream, dictionary};
let mut doc = Document::with_version("1.7");
let to_unicode = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def /CMapName /TestUnicode def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange 6 beginbfchar <0024> <0041> <0025> <0042> <0026> <0043> <0027> <0044> <0028> <0045> <0029> <0046> endbfchar endcmap CMapName currentdict /CMap defineresource pop end end";
let unicode = doc.add_object(Stream::new(dictionary! {}, to_unicode.to_vec()));
let widths: Vec<Object> = widths.iter().flat_map(|&(cid, w)| [Object::Integer(cid), Object::Array(vec![Object::Integer(w)])]).collect();
let cid = doc.add_object(dictionary! {
"Type" => "Font", "Subtype" => "CIDFontType2", "BaseFont" => "TestVerticalLatin",
"CIDSystemInfo" => dictionary! { "Registry" => Object::string_literal("Adobe"), "Ordering" => Object::string_literal("Identity"), "Supplement" => 0 },
"DW" => 1000, "W" => widths, "DW2" => vec![Object::Integer(880), Object::Integer(-1000)], "CIDToGIDMap" => "Identity"
});
let encoding = match cmap {
Some(body) => {
let stream = format!(
"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> def /CMapName /TestCodes-V def /CMapType 1 def /WMode 1 def 1 begincodespacerange <0000> <FFFF> endcodespacerange {body} endcmap CMapName currentdict /CMap defineresource pop end end"
);
Object::Reference(doc.add_object(Stream::new(
dictionary! { "Type" => "CMap", "CMapName" => "TestCodes-V", "WMode" => 1, "CIDSystemInfo" => dictionary! { "Registry" => Object::string_literal("Adobe"), "Ordering" => Object::string_literal("Identity"), "Supplement" => 0 } },
stream.into_bytes(),
)))
}
None => Object::Name(b"Identity-V".to_vec()),
};
let font = doc.add_object(dictionary! { "Type" => "Font", "Subtype" => "Type0", "BaseFont" => "TestVerticalLatin", "Encoding" => encoding, "DescendantFonts" => vec![Object::Reference(cid)], "ToUnicode" => unicode });
let content = doc.add_object(Stream::new(dictionary! {}, content.as_bytes().to_vec()));
let pages = doc.new_object_id();
let page = doc.add_object(dictionary! { "Type" => "Page", "Parent" => pages, "MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(400), Object::Integer(500)], "Resources" => dictionary! {"Font" => dictionary! {"F1" => font}}, "Contents" => content });
doc.objects.insert(pages, dictionary! {"Type" => "Pages", "Kids" => vec![Object::Reference(page)], "Count" => 1}.into());
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages});
doc.trailer.set("Root", catalog);
let mut bytes = Vec::new();
doc.save_to(&mut bytes).unwrap();
let pdf = Pdf::new(std::sync::Arc::new(bytes)).unwrap();
extract_page(&pdf, 0, &InterpreterSettings::default()).unwrap()
}
/// A CID that `/W` doesn't list is `/DW` wide across the column (ISO 32000-2 §9.7.4.3), and the
/// widths are looked up by CID, not by character code: an embedded vertical CMap maps the codes
/// `<0024>`… (which `/W` lists 500 wide as CIDs) to CIDs 50…. The tracked column stays one line
/// that Find matches.
#[test]
fn vertical_latin_widths_come_from_dw_and_are_looked_up_by_cid() {
let cmap = "5 begincidchar <0024> 50 <0025> 51 <0026> 52 <0027> 53 <0028> 54 endcidchar";
// CID 52 (`C`) has no /W entry: it is /DW = 1000 wide.
let widths = [(36, 500), (37, 500), (38, 500), (39, 500), (40, 500), (50, 667), (51, 667), (53, 722), (54, 667)];
let text = vertical_latin(&widths, Some(cmap), "BT /F1 24 Tf -40 Tc 1 0 0 1 100 340 Tm <00240025002600270028> Tj ET");
assert_eq!(text.plain_text(), "ABCDE");
assert_eq!(text.find("ABCDE"), vec![0..5]);
let got: Vec<f32> = text.glyphs.iter().map(|g| g.rect[2] - g.rect[0]).collect();
assert_eq!(got.len(), 5);
for (w, w0) in got.iter().zip([667.0, 667.0, 1000.0, 722.0, 667.0]) {
assert!((w - 24.0 * w0 / 1000.0).abs() < 0.01, "{got:?}");
}
}
/// Two vertical columns of `/DW`-wide letters set 13.5 pt apart at 20 pt overlap across the
/// column (each box is 20 pt wide), and still read as two lines, in order, with Find matching
/// each column and not a word spanning both.
#[test]
fn close_vertical_columns_of_dw_wide_letters_stay_apart() {
let text = vertical_latin(&[], None, "BT /F1 20 Tf 1 0 0 1 113.5 340 Tm <002400250026> Tj 1 0 0 1 100 340 Tm <002700280029> Tj ET");
assert_eq!(text.plain_text().lines().map(str::trim).collect::<Vec<_>>(), ["ABC", "DEF"]);
assert_eq!(text.find("ABC").len(), 1);
assert_eq!(text.find("DEF").len(), 1);
assert!(text.find("CD").is_empty() && text.find("ABCDEF").is_empty(), "{:?}", text.plain_text());
}
#[test]
fn upright_mapping_round_trips() {
let r = [10.0, 20.0, 30.0, 25.0];
@@ -2176,6 +2447,161 @@ fn text_outside_the_crop_box_is_clipped() {
}
}
/// A one-page document showing `codes` in a Type 0 font with `encoding` over an embedded
/// `descendant` (`CIDFontType0` takes the bundled CFF program, `CIDFontType2` a TrueType one)
/// of the Adobe-Japan1 collection, with `to_unicode` as its `/ToUnicode` CMap when given.
fn japan1_page(encoding: &str, descendant: &str, to_unicode: Option<&[u8]>, codes: &str) -> String {
collection_page(("Adobe", "Japan1"), encoding, descendant, to_unicode, codes)
}
/// [`japan1_page`] for the character collection `(registry, ordering)`.
fn collection_page(collection: (&str, &str), encoding: &str, descendant: &str, to_unicode: Option<&[u8]>, codes: &str) -> String {
use lopdf::{Document, Object, Stream, dictionary};
let (registry, ordering) = collection;
let mut doc = Document::with_version("1.7");
let (key, program): (&str, &[u8]) = if descendant == "CIDFontType0" {
("FontFile3", include_bytes!("../../../../vendor/hayro-interpret/assets/FoxitFixed.pfb"))
} else {
("FontFile2", include_bytes!("../../../../assets/fonts/Inter-Regular.ttf"))
};
let mut file = dictionary! {};
if descendant == "CIDFontType0" {
file.set("Subtype", "CIDFontType0C");
}
let program = doc.add_object(Stream::new(file, program.to_vec()));
let descriptor = doc.add_object(dictionary! { "Type" => "FontDescriptor", "FontName" => "TestJapan1", "Flags" => 4, key => program });
let cid = doc.add_object(dictionary! {
"Type" => "Font", "Subtype" => descendant, "BaseFont" => "TestJapan1",
"CIDSystemInfo" => dictionary! { "Registry" => Object::string_literal(registry), "Ordering" => Object::string_literal(ordering), "Supplement" => 6 },
"FontDescriptor" => descriptor, "DW" => 1000, "CIDToGIDMap" => "Identity"
});
let mut font = dictionary! { "Type" => "Font", "Subtype" => "Type0", "BaseFont" => "TestJapan1", "Encoding" => encoding, "DescendantFonts" => vec![Object::Reference(cid)] };
if let Some(cmap) = to_unicode {
font.set("ToUnicode", doc.add_object(Stream::new(dictionary! {}, cmap.to_vec())));
}
let font = doc.add_object(font);
let content = doc.add_object(Stream::new(dictionary! {}, format!("BT /F1 20 Tf 1 0 0 1 20 250 Tm <{codes}> Tj ET").into_bytes()));
let pages = doc.new_object_id();
let page = doc.add_object(dictionary! { "Type" => "Page", "Parent" => pages, "MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(300), Object::Integer(300)], "Resources" => dictionary! {"Font" => dictionary! {"F1" => font}}, "Contents" => content });
doc.objects.insert(pages, dictionary! {"Type" => "Pages", "Kids" => vec![Object::Reference(page)], "Count" => 1}.into());
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages});
doc.trailer.set("Root", catalog);
let mut bytes = Vec::new();
doc.save_to(&mut bytes).unwrap();
let pdf = Pdf::new(std::sync::Arc::new(bytes)).unwrap();
extract_page(&pdf, 0, &InterpreterSettings::default()).unwrap().plain_text()
}
/// An embedded font of an Adobe character collection reads as text through the collection's
/// UCS2 CMap, code to CID to Unicode (ISO 32000-2 §9.10.2), where its `/ToUnicode` is missing or
/// has nothing for a code: with Identity-H or a predefined CMap such as `/H`, CFF or TrueType.
/// Only fonts that are not embedded took that route, so Japanese and Chinese pages whose fonts
/// are embedded without a `/ToUnicode` had no text to select, find or copy. `/ToUnicode` still
/// comes first where it maps a code.
#[test]
fn an_embedded_adobe_collection_font_reads_through_its_ucs2_cmap() {
// Adobe-Japan1 CIDs 34, 1125 and 843: A, 亜 and あ.
assert_eq!(japan1_page("Identity-H", "CIDFontType0", None, "00220465034B"), "A亜あ", "Identity-H, CFF, no /ToUnicode");
assert_eq!(japan1_page("Identity-H", "CIDFontType2", None, "00220465034B"), "A亜あ", "Identity-H, TrueType, no /ToUnicode");
// `/H` maps JIS X 0208 codes to CIDs: 3021 亜, 2422 あ, 2341 A.
assert_eq!(japan1_page("H", "CIDFontType2", None, "302124222341"), "亜あA", "predefined /H");
let empty = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /Empty def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange endcmap CMapName currentdict /CMap defineresource pop end end";
assert_eq!(japan1_page("Identity-H", "CIDFontType2", Some(empty), "00220465034B"), "A亜あ", "a /ToUnicode with no mappings");
let one = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /One def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange 1 beginbfchar <0022> <005A> endbfchar endcmap CMapName currentdict /CMap defineresource pop end end";
assert_eq!(japan1_page("Identity-H", "CIDFontType0", Some(one), "00220465034B"), "Z亜あ", "/ToUnicode first, the UCS2 CMap for the rest");
}
/// The same route for the other Adobe collections, each through its own UCS2 CMap
/// (Adobe-GB1-UCS2, Adobe-CNS1-UCS2, Adobe-Korea1-UCS2), and for vertical writing (Identity-V)
/// and a predefined CMap over a CFF program. The pairs are read from the bundled CMaps.
#[test]
fn every_adobe_collection_reads_through_its_own_ucs2_cmap() {
for descendant in ["CIDFontType0", "CIDFontType2"] {
// Adobe-GB1 CID 4000 is 姓, Adobe-CNS1 CID 4000 is 團, Adobe-Korea1 CID 2000 is 밧; CID 34
// is A in each.
for (ordering, codes, text) in [("GB1", "00220FA0", "A姓"), ("CNS1", "00220FA0", "A團"), ("Korea1", "002207D0", "A밧")] {
assert_eq!(collection_page(("Adobe", ordering), "Identity-H", descendant, None, codes), text, "Adobe-{ordering}, {descendant}");
}
assert_eq!(japan1_page("Identity-V", descendant, None, "00220465034B"), "A亜あ", "Identity-V, {descendant}");
}
assert_eq!(japan1_page("H", "CIDFontType0", None, "302124222341"), "亜あA", "predefined /H, CFF");
}
/// A Type 0 font outside the Adobe collections has no UCS2 CMap: without a `/ToUnicode` its codes
/// give no text through this route (the CIDs mean nothing outside their collection).
#[test]
fn a_font_outside_the_adobe_collections_gets_no_text_from_a_ucs2_cmap() {
for descendant in ["CIDFontType0", "CIDFontType2"] {
assert_eq!(collection_page(("Example", "Custom"), "Identity-H", descendant, None, "00220465034B"), "", "{descendant}");
}
}
/// A Type 0 font whose `/ToUnicode` is written as a CID CMap (`begincidchar`, destinations as
/// numbers, as Word writes it) reads each number as the code's Unicode value (vendored
/// hayro-interpret patch 85), as glyph lookup already did and as pdftotext and PDFium extract it.
/// Read as a bfchar map only, such text extracted nothing (pdf.js `issue9367`).
#[test]
fn a_tounicode_written_as_cid_ranges_gives_text() {
let two = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /Words def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange 3 begincidchar <0001> 84 <0002> 101 <0004> 116 endcidchar 1 begincidrange <0003> <0003> 120 endcidrange endcmap CMapName currentdict /CMap defineresource pop end end";
assert_eq!(cid_range_to_unicode_text(two), "Text", "two-byte entries");
// Entries written with four-byte codes are found too (review on #6359).
let four = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /Words def /CMapType 2 def 1 begincodespacerange <00000000> <FFFFFFFF> endcodespacerange 2 begincidchar <00000001> 84 <00000002> 101 endcidchar 1 begincidrange <00000003> <00000004> 120 endcidrange endcmap CMapName currentdict /CMap defineresource pop end end";
assert_eq!(cid_range_to_unicode_text(four), "Texy", "four-byte entries");
}
/// When a `/ToUnicode` gives one code both a bf entry and a CID entry, the bf entry is the
/// code's text (ISO 32000-2 §9.10.3 defines `/ToUnicode` by `beginbfchar` / `beginbfrange`); the
/// CID number is read only for codes no bf entry covers (vendored hayro-interpret patch 85).
#[test]
fn a_tounicode_bf_entry_wins_over_a_cid_entry_for_the_same_code() {
// Codes 1 and 3 have both kinds of entry: bf says `T` / `x`, CID says `X` / `Y`. Codes 2 and 4
// have CID entries only.
let two = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /Words def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange 3 begincidchar <0001> 88 <0002> 101 <0004> 116 endcidchar 1 begincidrange <0003> <0003> 89 endcidrange 1 beginbfchar <0001> <0054> endbfchar 1 beginbfrange <0003> <0003> <0078> endbfrange endcmap CMapName currentdict /CMap defineresource pop end end";
assert_eq!(cid_range_to_unicode_text(two), "Text", "two-byte entries");
// The same with one-byte entries. For two-byte entries the CID lookup, tried at one byte
// first, already falls back to the bf entries at that length; one-byte entries are where
// reading CID entries before bf entries would show, as `XeYt`.
let one = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CMapName /Words def /CMapType 2 def 1 begincodespacerange <00> <FF> endcodespacerange 3 begincidchar <01> 88 <02> 101 <04> 116 endcidchar 1 begincidrange <03> <03> 89 endcidrange 1 beginbfchar <01> <0054> endbfchar 1 beginbfrange <03> <03> <0078> endbfrange endcmap CMapName currentdict /CMap defineresource pop end end";
assert_eq!(cid_range_to_unicode_text(one), "Text", "one-byte entries");
}
/// The page [`a_tounicode_written_as_cid_ranges_gives_text`] and
/// [`a_tounicode_bf_entry_wins_over_a_cid_entry_for_the_same_code`] read: four codes in a Type 0 font
/// (Inter, Identity-H) whose `/ToUnicode` is `cmap`.
fn cid_range_to_unicode_text(cmap: &[u8]) -> String {
use lopdf::{Document, Object, Stream, dictionary};
let mut doc = Document::with_version("1.7");
let program = doc.add_object(Stream::new(dictionary! {}, include_bytes!("../../../../assets/fonts/Inter-Regular.ttf").to_vec()));
let descriptor = doc.add_object(dictionary! { "Type" => "FontDescriptor", "FontName" => "Inter-Regular", "Flags" => 4, "FontFile2" => program });
let cid_font = doc.add_object(dictionary! {
"Type" => "Font",
"Subtype" => "CIDFontType2",
"BaseFont" => "Inter-Regular",
"CIDSystemInfo" => dictionary! { "Registry" => Object::string_literal("Adobe"), "Ordering" => Object::string_literal("Identity"), "Supplement" => 0 },
"FontDescriptor" => descriptor,
"CIDToGIDMap" => "Identity",
});
let to_unicode = doc.add_object(Stream::new(dictionary! {}, cmap.to_vec()));
let font = doc.add_object(dictionary! {
"Type" => "Font",
"Subtype" => "Type0",
"BaseFont" => "Inter-Regular",
"Encoding" => "Identity-H",
"DescendantFonts" => vec![Object::Reference(cid_font)],
"ToUnicode" => to_unicode,
});
let content = doc.add_object(Stream::new(dictionary! {}, b"BT /F1 20 Tf 20 200 Td <0001000200030004> Tj ET".to_vec()));
let pages = doc.new_object_id();
let page = doc.add_object(dictionary! { "Type" => "Page", "Parent" => pages, "MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(400), Object::Integer(400)], "Resources" => dictionary! {"Font" => dictionary! {"F1" => font}}, "Contents" => content });
doc.objects.insert(pages, dictionary! {"Type" => "Pages", "Kids" => vec![Object::Reference(page)], "Count" => 1}.into());
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages});
doc.trailer.set("Root", catalog);
let mut bytes = Vec::new();
doc.save_to(&mut bytes).unwrap();
let pdf = Pdf::new(std::sync::Arc::new(bytes)).unwrap();
extract_page(&pdf, 0, &InterpreterSettings::default()).unwrap().plain_text().trim().to_string()
}
/// A page box that encloses no area is read as a missing one (vendored hayro-syntax patch 38,
/// ISO 32000-2 §14.11.2): a `/MediaBox [0 0 0 0]` as the default page, a `/CropBox [0 0 0 0]`,
/// or one with nothing in common with the media box, as the media box. The page's visible region