Hi everyone,

we are using Apache PDFBox 3.0.2 in our software and have the following issue.
We want to write a String using the font FreeSansBold.
The font is loaded via PDType0Font#load from a TTF-file.

If we load the font with embedSubset=true than the following exception occurs:

    java.lang.ArrayIndexOutOfBoundsException: Index 2941 out of bounds for 
length 2912
    at 
org.apache.fontbox.ttf.TTFSubsetter.addCompoundReferences(TTFSubsetter.java:500)
    at org.apache.fontbox.ttf.TTFSubsetter.getGIDMap(TTFSubsetter.java:147)
    at 
org.apache.pdfbox.pdmodel.font.TrueTypeEmbedder.subset(TrueTypeEmbedder.java:336)
    at org.apache.pdfbox.pdmodel.font.PDType0Font.subset(PDType0Font.java:304)
    at 
org.apache.pdfbox.pdmodel.PDDocument.subsetDesignatedFonts(PDDocument.java:1046)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1034)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:988)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:944)

The reason is the question-mark-character (?). The character ! also causes an 
exception.
Letters like a-zA-Z dont.
This character is first correctly identified as Glyph-ID 34 but then in 
PDAbstractContentStream#encodeForGsub converted to 2914 by 
GsubWorkerForDevanagari.
This glyph does not exist for this font.
This causes an exception when subsetting the font.
The exception does not occur when writing the text in the PDPageContentStream.

If we load the font with embetSubst=false then no exception occurs but the 
character is not visible/skipped in the pdf.
I have only found old and fixed issues with ArrayIndexOutOfBoundsExceptions 
(https://issues.apache.org/jira/browse/PDFBOX-4946).

Code to redproduce (you need the font FreeSansBold):

    PDDocument document = new PDDocument();
    File boldF = new File("src/test/resources/fonts", "FreeSansBold.ttf");
    TrueTypeFont boldT = new TTFParser().parse(new 
RandomAccessReadBufferedFile(boldF));
    PDFont bold = PDType0Font.load(document, boldT, true);
    PDPage page = new PDPage(PDRectangle.A4);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, 
PDPageContentStream.AppendMode.APPEND, true, true);
    contentStream.setFont(bold, 11);
    contentStream.beginText();
    contentStream.newLineAtOffset(50, 50);
    contentStream.showText("?");
    contentStream.endText();
    contentStream.close();
    document.addPage(page);
    document.save(new File("Test.pdf"));

Can you help us?
Is this a bug in our code / the font oder Apache PDFBox?

Thanks.

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to