[ 
https://issues.apache.org/jira/browse/PDFBOX-6172?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18062202#comment-18062202
 ] 

Tilman Hausherr edited comment on PDFBOX-6172 at 3/2/26 3:59 PM:
-----------------------------------------------------------------

I have written a detection for this problem (to be added to 
{{PDCIDFontType2Embedder.createCIDFont()}}) with some help by ChatGPT. You 
still won't be able to use that font but now you'll get an exception instead of 
no exception and a broken output.
{code:java}
    private void checkCidGidIdentity() throws IOException
    {
        // PDFBOX-6172: if somebody is using a not subsetted otf font, check 
whether cid == gid
        // (subsetted will fail anyway)
        OpenTypeFont otf = (OpenTypeFont) ttf;
        CFFTable cffTable = otf.getCFF();
        if (cffTable == null)
        {
            return;
        }
        CFFCIDFont cff = (CFFCIDFont) cffTable.getFont();
        if (cff == null)
        {
            return;
        }
        CFFCharset charset = cff.getCharset();
        if (charset == null)
        {
            return;
        }
        int glyphCount = otf.getNumberOfGlyphs();
        for (int gid = 0; gid < glyphCount; gid++)
        {
            int cid = charset.getCIDForGID(gid);
            if (gid != cid)
            {
                throw new IllegalStateException("CID and GID not identical: CID 
" + cid + " != GID " + gid+ ", use a ttf font instead");
            }
        }
    }
{code}



was (Author: tilman):
I'd written a detection for this problem (to be added to 
{{PDCIDFontType2Embedder.createCIDFont()}}) with some help by ChatGPT. You 
still won't be able to use that font but now you'll get an exception instead of 
no exception and a broken output.
{code:java}
    private void checkCidGidIdentity() throws IOException
    {
        // PDFBOX-6172: if somebody is using a not subsetted otf font, check 
whether cid == gid
        // (subsetted will fail anyway)
        OpenTypeFont otf = (OpenTypeFont) ttf;
        CFFTable cffTable = otf.getCFF();
        if (cffTable == null)
        {
            return;
        }
        CFFCIDFont cff = (CFFCIDFont) cffTable.getFont();
        if (cff == null)
        {
            return;
        }
        CFFCharset charset = cff.getCharset();
        if (charset == null)
        {
            return;
        }
        int glyphCount = otf.getNumberOfGlyphs();
        for (int gid = 0; gid < glyphCount; gid++)
        {
            int cid = charset.getCIDForGID(gid);
            if (gid != cid)
            {
                throw new IllegalStateException("CID and GID not identical: CID 
" + cid + " != GID " + gid+ ", use a ttf font instead");
            }
        }
    }
{code}


> PDF displays garbled characters in Adobe Reader but renders correctly in web 
> browsers
> -------------------------------------------------------------------------------------
>
>                 Key: PDFBOX-6172
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-6172
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.35
>            Reporter: bai yuan
>            Priority: Major
>         Attachments: NotoSansSC-Regular.otf, 
> image-2026-03-02-15-59-36-196.png, image-2026-03-02-16-00-28-488.png, 
> output.pdf, screenshot-1.png
>
>
> When exporting a PDF document, some characters appear as garbled text or 
> square boxes when the file is opened in Adobe Acrobat Reader. However, the 
> same PDF renders correctly when opened in web browsers such as Chrome or Edge.
> {code:java}
> try (PDDocument document = new PDDocument()) {
>     PDPage page = new PDPage();
>     document.addPage(page);
>     OpenTypeFont otf = new OTFParser().parse("NotoSansSC-Regular.otf");
>     PDFont font = PDType0Font.load(document, otf, false);
>     PDPageContentStream contentStream = new PDPageContentStream(document, 
> page);
>     contentStream.beginText();
>     contentStream.setFont(font, 12);
>     contentStream.newLineAtOffset(100, 700);
>     contentStream.showText("手机号码");
>     contentStream.endText();
>     contentStream.close();
>     document.save("output.pdf");
> } catch (IOException e) {
>     e.printStackTrace();
> } {code}
> !image-2026-03-02-16-00-28-488.png!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to