diff --git a/src/parse/pdf_resources/page_font.h b/src/parse/pdf_resources/page_font.h index 20163197..5bda15ee 100644 --- a/src/parse/pdf_resources/page_font.h +++ b/src/parse/pdf_resources/page_font.h @@ -1,4 +1,5 @@ //-*-C++-*- +// SPDX-License-Identifier: MIT #ifndef PDF_PAGE_FONT_RESOURCE_H #define PDF_PAGE_FONT_RESOURCE_H @@ -750,9 +751,11 @@ namespace pdflib return glyph_name == "space" or bbox[3] > bbox[1]; } - // Builtin subset codes do not identify standard-font characters: - // code 32, for example, may draw "m" rather than a space. - return not uses_builtin_encoding() and bfont.has_char_bbox(c); + // Composite-font codes address CIDs, not AFM character slots. A + // subset CID 32 may draw a letter rather than the standard space. + // The same applies to simple fonts using their builtin encoding. + return subtype != TYPE_0 and not uses_builtin_encoding() and + bfont.has_char_bbox(c); } return false; diff --git a/tests/test_regression_composite_font_rendering.py b/tests/test_regression_composite_font_rendering.py new file mode 100644 index 00000000..e91bc448 --- /dev/null +++ b/tests/test_regression_composite_font_rendering.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: MIT +"""Keep a real composite-font glyph from being treated as an AFM space.""" + +from pathlib import Path + +import pytest + +from tests.pdf_builder import render_page +from tests.rendering_regression import ( + ImageTolerance, + flatten_on_white, + image_comparison_failed, + measure_image_pair, + render_pypdfium_page, +) + +REGRESSION_DIR = Path(__file__).parent / "data" / "regression" + + +def test_composite_cid_32_keeps_formula_equals() -> None: + pytest.importorskip("pypdfium2") + document = "10749817875312063576_009.pdf" + pdf_path = REGRESSION_DIR / document + page_no, scale = 5, 3.0 + + # "Nonideal Flow Models in Homogeneous Chemical Reactors", equation (9). + # The embedded SymbolMT subset maps CID 32 to '='. Applying the standard + # font's numeric AFM slot 32 gives it a zero-height space bbox (#466). + # Focus on the first '=', which otherwise contributes too few pixels to + # fail a whole-page image comparison. Coordinates are top-left PDF points. + left, top, right, bottom = 100, 124, 114, 139 + crop = ( + round(left * scale), + round(top * scale), + round(right * scale), + round(bottom * scale), + ) + result = render_page(pdf_path.read_bytes(), page_no=page_no, scale=scale) + actual = flatten_on_white(result.get_image()).crop(crop) + reference = flatten_on_white( + render_pypdfium_page(pdf_path, page_no, scale=scale) + ).crop(crop) + assert sum(reference.convert("L").histogram()[:128]) > 0, ( + "the reference '=' must have visible ink" + ) + tolerance = ImageTolerance( + pixel_threshold=12, mean_abs_error=3.0, changed_pixels_ratio=0.08 + ) + comparison = measure_image_pair( + document, page_no, actual, reference, tolerance=tolerance + ) + + assert not image_comparison_failed(comparison, tolerance=tolerance), ( + f"the formula's '=' does not match PDFium: " + f"mean_abs_error={comparison.mean_abs_error:.4f} " + f"(limit {tolerance.mean_abs_error}), " + f"changed_pixels_ratio={comparison.changed_pixels_ratio:.4f} " + f"(limit {tolerance.changed_pixels_ratio})" + ) + + +@pytest.mark.parametrize( + ("document", "page_no", "tolerance"), + [ + pytest.param( + "15643741724817381873-1.pdf", + 1, + ImageTolerance( + pixel_threshold=12, mean_abs_error=5.0, changed_pixels_ratio=0.15 + ), + id="embedded-times", + ), + pytest.param( + "926659986622553168-1.pdf", + 2, + ImageTolerance( + pixel_threshold=12, mean_abs_error=2.5, changed_pixels_ratio=0.07 + ), + id="embedded-arial", + ), + pytest.param( + "indexed_iccbased.pdf", + 1, + ImageTolerance( + pixel_threshold=12, mean_abs_error=4.0, changed_pixels_ratio=0.14 + ), + id="embedded-cff", + ), + ], +) +def test_composite_font_rendering_control_pages( + document: str, page_no: int, tolerance: ImageTolerance +) -> None: + """Keep the small edge-pixel differences in #466 from hiding larger regressions. + + These controls pass on main too. All their text fonts are embedded, so the + comparison does not depend on the host's substitute fonts. The observed + MAEs at scale 2 were 4.42, 2.11, and 3.62; allow rasterization differences + without using the much looser corpus-wide reporting tolerance. + """ + pytest.importorskip("pypdfium2") + pdf_path = REGRESSION_DIR / document + result = render_page(pdf_path.read_bytes(), page_no=page_no, scale=2.0) + comparison = measure_image_pair( + document, + page_no, + flatten_on_white(result.get_image()), + flatten_on_white(render_pypdfium_page(pdf_path, page_no, scale=2.0)), + tolerance=tolerance, + ) + + assert not image_comparison_failed(comparison, tolerance=tolerance), ( + f"{document}@{page_no}: mean_abs_error={comparison.mean_abs_error:.4f} " + f"(limit {tolerance.mean_abs_error}), " + f"changed_pixels_ratio={comparison.changed_pixels_ratio:.4f} " + f"(limit {tolerance.changed_pixels_ratio})" + ) diff --git a/tests/test_unit_cid_to_gid_map.py b/tests/test_unit_cid_to_gid_map.py index 6ba0111a..37aa2238 100644 --- a/tests/test_unit_cid_to_gid_map.py +++ b/tests/test_unit_cid_to_gid_map.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# SPDX-License-Identifier: MIT """A CIDFontType2 draws the glyph its /CIDToGIDMap stream names (9.7.4.2). Subset TrueType programs embedded under a /Type0 font often carry no `cmap` @@ -23,6 +24,7 @@ from io import BytesIO import pytest +from PIL import ImageChops from tests.pdf_builder import render_page, simple_page_pdf, stream_object from tests.rendering_regression import flatten_on_white @@ -128,6 +130,7 @@ def _page( code: int = 1, cid_to_gid: bytes = bytes([0, 0, 0, 2, 0, 1]), # CID 1 -> GID 2, CID 2 -> GID 1 encoding_cmap: bytes | None = None, + base_font: str = "AAAAAA+DoclingCidMapped", ) -> bytes: if program is None: program = _cmapless_font() @@ -147,14 +150,14 @@ def _page( objects: list[str | bytes] = [ # 5: the Type0 font - "<< /Type /Font /Subtype /Type0 /BaseFont /AAAAAA+DoclingCidMapped " + f"<< /Type /Font /Subtype /Type0 /BaseFont /{base_font} " f"/Encoding {encoding} /DescendantFonts [6 0 R] /ToUnicode 10 0 R >>", # 6: its CIDFontType2 descendant - "<< /Type /Font /Subtype /CIDFontType2 /BaseFont /AAAAAA+DoclingCidMapped " + f"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{base_font} " "/CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> " "/FontDescriptor 7 0 R /DW 1000 /CIDToGIDMap 9 0 R >>", # 7 - "<< /Type /FontDescriptor /FontName /AAAAAA+DoclingCidMapped /Flags 4 " + f"<< /Type /FontDescriptor /FontName /{base_font} /Flags 4 " "/FontBBox [0 -200 1000 800] /ItalicAngle 0 /Ascent 800 /Descent -200 " "/CapHeight 700 /StemV 80 /FontFile2 8 0 R >>", # 8 @@ -201,6 +204,33 @@ def test_cid_to_gid_map_stream_selects_the_glyph(): _assert_draws(_page(), BIG) +@pytest.mark.parametrize("base_font", ["Helvetica", "Times-Roman", "Courier", "Symbol"]) +@pytest.mark.parametrize("complete", [False, True], ids=["freetype", "blend2d"]) +def test_composite_code_32_keeps_its_visible_glyph( + base_font: str, complete: bool +) -> None: + """A subset code 32 draws its mapped glyph, not the AFM space (issue #466).""" + pdfium = pytest.importorskip("pypdfium2") + cid_to_gid = bytes(2 * 32) + bytes([0, 1]) + pdf = _page( + _complete_font() if complete else _cmapless_font(), + code=32, + cid_to_gid=cid_to_gid, + base_font=base_font, + ) + actual = flatten_on_white(render_page(pdf, scale=MEASURE_SCALE).get_image()) + with pdfium.PdfDocument(pdf) as document: + expected = flatten_on_white(document[0].render(scale=MEASURE_SCALE).to_pil()) + + assert actual.size == expected.size + actual_mask = actual.convert("L").point(lambda value: 255 if value < 128 else 0) + expected_mask = expected.convert("L").point(lambda value: 255 if value < 128 else 0) + intersection = ImageChops.darker(actual_mask, expected_mask).histogram()[255] + union = ImageChops.lighter(actual_mask, expected_mask).histogram()[255] + assert union > 0, "the reference glyph must have ink" + assert intersection / union > 0.95, "the composite glyph does not match PDFium" + + # CID 34 -> GID 1 (small), and CID 65 -> GID 2 (big), where code 0x41 read # as a CID would land. _UNIJIS_MAP = bytearray(2 * 66)