Hi,

First up thanks for all your hard work on PDFBox, it’s a great tool. I’ve
just started moving to it and found it’s doing a great job.

We encountered a minor regression in 3.0.8 related to Type 3 glyphs with
non-UTF-8 names.

The report below was generated by Claude and is thorough so I’ve included
it verbatim.

I'm happy to open a JIRA issue if you approve my account with the PDF
attached if that helps.

Cheers,

Ben

--

Since 3.0.8, a Type 3 glyph whose name is not valid UTF-8 is no longer
found: PDType3Font.getCharProc(code) returns null, and the glyph is not
rendered. 3.0.7 found and drew the same glyph. It looks like a side
effect of PDFBOX-6178, after which COSName compares names as bytes.

Cause: getCharProc turns the glyph name into a String and back:

    String name = getEncoding().getName(code);
    COSStream stream =
        getCharProcs().getCOSStream(COSName.getPDFName(name));


For the name /g#E9 (bytes 67 E9), COSName.getName() falls back to
ISO-8859-1 and gives "g\u00E9". COSName.getPDFName() encodes that as
UTF-8, bytes 67 C3 A9, which is a different name now that names compare
as bytes, so the /CharProcs key /g#E9 is never matched. A name that is
valid UTF-8 survives the round trip.

Such a name is valid PDF: ISO 32000 7.3.5 allows any bytes except null
in a name, and recommends UTF-8 only where a name is treated as text.

The same two lines are in PDType3Font.getCharProc on the 3.0 branch,
trunk and the 2.0 branch today, and hasGlyph(String) looks names up
through COSName.getPDFName(String) the same way. The 2.0 branch also
stores names as bytes, so 2.0.37 is probably affected too; I have not
tested it.

To reproduce: the program below writes a one-page test PDF by hand and
checks it with PDFBox. The page draws code 65 from two Type 3 fonts,
each glyph a filled 80 x 80 pt square. F1 names the glyph /g#C3#A9
(UTF-8, the control) and F2 names it /g#E9 (not UTF-8); the fonts are
otherwise identical. I ran it with Java 25 on macOS.

---- Type3NonUtf8GlyphName.java ----
import java.awt.image.BufferedImage;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.util.Version;

/*
 * Writes a one-page test PDF, then shows a Type 3 glyph whose name is
 * not UTF-8 going missing on PDFBox 3.0.8. The page draws code 65 twice,
 * a filled 80 x 80 pt square each time:
 *   F1 (left half):  glyph /g#C3#A9, bytes 67 C3 A9 (UTF-8), the control
 *   F2 (right half): glyph /g#E9, bytes 67 E9 (not UTF-8)
 * The PDF is written by hand, not by PDFBox, and kept on disk.
 *
 * java -Djava.awt.headless=true \
 *   -cp pdfbox.jar:pdfbox-io.jar:fontbox.jar:commons-logging.jar \
 *   Type3NonUtf8GlyphName.java
 */
public class Type3NonUtf8GlyphName {
  public static void main(String[] args) throws Exception {
    Path pdf = Path.of(args.length > 0 ? args[0]
        : "type3-non-utf8-glyph-name.pdf");
    Files.write(pdf, testPdf());
    try (PDDocument doc = Loader.loadPDF(pdf.toFile())) {
      System.out.println("PDFBox " + Version.getVersion());
      PDPage page = doc.getPage(0);
      for (String f : new String[] {"F1", "F2"}) {
        PDType3Font font = (PDType3Font)
            page.getResources().getFont(COSName.getPDFName(f));
        String name = font.getEncoding().getName(65);
        boolean key = font.getCharProcs()
            .containsKey(COSName.getPDFName(name));
        String proc = font.getCharProc(65) != null ? "found" : "null";
        System.out.printf("%s: encoding names code 65 \"%s\";"
            + " CharProcs has COSName.getPDFName(that): %s;"
            + " getCharProc(65): %s%n", f, escaped(name), key, proc);
      }
      // 72 dpi: 200 x 100 px
      BufferedImage image = new PDFRenderer(doc).renderImage(0);
      System.out.printf("rendered dark pixels: left (F1) %d,"
          + " right (F2) %d (each square is 6400)%n",
          dark(image, 0, 100), dark(image, 100, 200));
    }
  }

  // All ASCII except the binary comment on line 2, which ISO-8859-1
  // writes one byte per char, so string offsets are byte offsets.
  static byte[] testPdf() {
    String glyph = "1000 0 0 0 1000 1000 d1\n0 0 1000 1000 re f\n";
    String content = "BT /F1 80 Tf 10 10 Td (A) Tj ET\n"
        + "BT /F2 80 Tf 110 10 Td (A) Tj ET\n";
    String[] objects = {
      "<< /Type /Catalog /Pages 2 0 R >>",
      "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
      "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100]\n"
          + "   /Resources << /Font << /F1 4 0 R /F2 6 0 R >> >>"
          + " /Contents 8 0 R >>",
      type3Font("/g#C3#A9", "5 0 R"),
      stream(glyph),
      type3Font("/g#E9", "7 0 R"),
      stream(glyph),
      stream(content)};
    StringBuilder pdf =
        new StringBuilder("%PDF-1.7\n%\u00E2\u00E3\u00CF\u00D3\n");
    List<Integer> offsets = new ArrayList<>();
    for (int i = 0; i < objects.length; i++) {
      offsets.add(pdf.length());
      pdf.append(i + 1).append(" 0 obj\n").append(objects[i])
          .append("\nendobj\n");
    }
    int xref = pdf.length();
    pdf.append("xref\n0 ").append(objects.length + 1)
        .append("\n0000000000 65535 f \n");
    for (int offset : offsets) {
      pdf.append(String.format("%010d 00000 n \n", offset));
    }
    pdf.append("trailer\n<< /Size ").append(objects.length + 1)
        .append(" /Root 1 0 R >>\nstartxref\n").append(xref)
        .append("\n%%EOF\n");
    return pdf.toString().getBytes(StandardCharsets.ISO_8859_1);
  }

  static String type3Font(String glyph, String charProc) {
    return "<< /Type /Font /Subtype /Type3 /FontBBox [0 0 1000 1000]"
        + " /FontMatrix [0.001 0 0 0.001 0 0]\n"
        + "   /CharProcs << " + glyph + " " + charProc + " >>\n"
        + "   /Encoding << /Type /Encoding /Differences [65 " + glyph
        + "] >>\n"
        + "   /FirstChar 65 /LastChar 65 /Widths [1000]"
        + " /Resources << >> >>";
  }

  static String stream(String data) {
    return "<< /Length " + data.length() + " >>\nstream\n" + data
        + "endstream";
  }

  static int dark(BufferedImage image, int fromX, int toX) {
    int count = 0;
    for (int y = 0; y < image.getHeight(); y++) {
      for (int x = fromX; x < toX; x++) {
        int rgb = image.getRGB(x, y);
        int sum = (rgb >> 16 & 0xFF) + (rgb >> 8 & 0xFF) + (rgb & 0xFF);
        if (sum / 3 < 128) {
          count++;
        }
      }
    }
    return count;
  }

  static String escaped(String s) {
    StringBuilder b = new StringBuilder();
    s.chars().forEach(c -> b.append(c < 0x80 ? String.valueOf((char) c)
        : String.format("\\u%04X", c)));
    return b.toString();
  }
}
---- end ----


On 3.0.7:

    PDFBox 3.0.7
    F1: encoding names code 65 "g\u00E9"; CharProcs has
COSName.getPDFName(that): true; getCharProc(65): found
    F2: encoding names code 65 "g\u00E9"; CharProcs has
COSName.getPDFName(that): true; getCharProc(65): found
    rendered dark pixels: left (F1) 6400, right (F2) 6400 (each square is
6400)

On 3.0.8:

    PDFBox 3.0.8
    F1: encoding names code 65 "g\u00E9"; CharProcs has
COSName.getPDFName(that): true; getCharProc(65): found
    F2: encoding names code 65 "g\u00E9"; CharProcs has
COSName.getPDFName(that): false; getCharProc(65): null
    rendered dark pixels: left (F1) 6400, right (F2) 0 (each square is 6400)

Expected: both squares drawn, as on 3.0.7.

A possible fix: look the glyph up by the name object in the font's
/Encoding /Differences array rather than by its String form, for
example by letting DictionaryEncoding keep the COSName for each code so
getCharProc can pass it straight to getCharProcs().getCOSStream(). For
now we work around it outside PDFBox by reading /Differences ourselves.

Reply via email to