外字をPDFに(1)

# 背景
お客様さんはPDF出力時、'おかしい'文字を出力したい
# 調査
外字ていうのは、自分定義したものです。unicodeのprivate use areaで定義される文字です。
        U+E000–U+F8FFまでです。
# 外字確認
外字の確認は、fontforageで確認できます。
.ttfファイルに転換して、コードの中で、Unicodeで判断して、外字かどうかを確認できます。
外字の場合は、定義された.ttfフォントを使って、PDFに正しくプリントできます。

## Java Code Sample(chatgpt ありがとう！)
```
    @Override
    public void run(ApplicationArguments args) throws Exception {
    try {
            // Read the file and save its content to the database
            String filePath = ("xxxx.外字ファイル");
            saveGaijiTextFromFile(filePath);

            // Fetch the Gaiji records from the database
            List<GaijiTest> gaijiList = gaijiTestService.getAllGaijiTestRecords();

            PDDocument document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage(page);

            // Load fonts
            File meiryoFontFile = new File("MEIRYO.ttf");
            PDFont meiryoFont = PDType0Font.load(document, meiryoFontFile);

            File gaijiFontFile = new File("外字.ttf");
            PDFont gaijiFont = PDType0Font.load(document, gaijiFontFile);

            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.newLineAtOffset(50, 700);

            // Iterate through the GaijiTest records and add them to the PDF
            for (GaijiTest gaijiTest : gaijiList) {

                // Write normal text using Meiryo font
                contentStream.setFont(meiryoFont, 12);
                contentStream.showText(String.valueOf(gaijiTest.getId()));
                contentStream.showText(": ");
                contentStream.showText(gaijiTest.getNormalText());

                // Write Gaiji text using Gaiji font, or handle with font switching logic
                if (gaijiTest.getId() != 15) {
                    contentStream.setFont(gaijiFont, 12);
                    contentStream.showText(gaijiTest.getGaijiText());
                } else {
                　　//外字と他の文字が混在する場合
                    String gaijiText = gaijiTest.getGaijiText();
                    contentStream.newLineAtOffset(0, -15);
                    // Check if the character is a Gaiji character 
                    for (char ch : gaijiText.toCharArray()) {
                        PDFont fontToUse = isGaiji(ch) ? gaijiFont : meiryoFont;
                        contentStream.setFont(fontToUse, 12);
                        contentStream.showText(String.valueOf(ch));
                    }
                }
            }
            contentStream.endText();
            contentStream.close();

            // Save the document
            document.save("output_with_gaiji.pdf");
            document.close();

            System.out.println("PDF creation complete: output_with_gaiji.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Check if the character is Gaiji (special character)
    private boolean isGaiji(char ch) {
        return (ch >= '\uE000' && ch <= '\uF8FF'); // Unicode range for Gaiji characters
    }

    // Method to read the file and save its content into the DB
    public String readFile(String filePath) {
        StringBuilder gaijiText = new StringBuilder();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                gaijiText.append(line).append("\n");  // Append each line with a newline character
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return gaijiText.toString();  // Return the full content of the file
    }

    // Method to read the file and save gaijiText to DB using GaijiTestService
    public void saveGaijiTextFromFile(String filePath) {
        // Read the file and get the content as a string
        String gaijiText = readFile(filePath);

        // Save the gaijiText into the database using the GaijiTestService
        GaijiTest savedGaijiTest = gaijiTestService.saveGaijiText(gaijiText);
        System.out.println("Saved GaijiTest with ID: " + savedGaijiTest.getId());
    }
}
```
なんかいろがついていない．．．