Hi Devs,

I attempted to render emojis using the emoji-supported font Noto Color 
Emoji<https://fonts.google.com/noto/specimen/Noto+Color+Emoji> with the PDFBox 
3.0.3 Java library. However, the font.hasGlyph(codePoint) method consistently 
returns false for all codepoint availability checks. Despite this, the emojis 
in question are clearly supported by the specified Google font. I have included 
the code below. Could someone kindly guide me in identifying the cause of this 
issue?


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.File;
import java.io.IOException;

public class EmojiPdfExample {
    public static void main(String[] args) {
        try (PDDocument doc = new PDDocument()) {

            File fontFile = new File("NotoColorEmoji-Regular.ttf");
            var font = PDType0Font.load(doc, fontFile);

            // Add a page
            PDPage page = new PDPage();
            doc.addPage(page);

            try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
                cs.beginText();
                cs.setFont(font, 20);
                cs.newLineAtOffset(50, 700);

                // Emoji string
                String text = "\uD83D\uDE00 \uD83D\uDE09";

                for (int i = 0; i < text.length(); ) {
                    int codePoint = Character.codePointAt(text, i);
                    i += Character.charCount(codePoint);

                    String glyph;
                    try {
                        glyph = new String(Character.toChars(codePoint));
                    } catch (IllegalArgumentException e) {
                        System.err.println("Invalid code point: U+" + 
Integer.toHexString(codePoint));
                        continue; // Skip invalid code points
                    }

                    try {
                        if (font.hasGlyph(codePoint)) {
                            cs.showText(glyph);
                        } else {
                            // Handle unsupported glyphs
                            System.out.println("Unsupported glyph," + glyph);
                        }
                    } catch (IOException e) {
                        System.err.println("IOException while showing glyph: 
U+" + Integer.toHexString(codePoint));
                    }
                }
                cs.endText();
            }

            doc.save("emojis.pdf");
            System.out.println("PDF created: emojis.pdf");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Thanks,
Sudeepa.

Reply via email to