Skip to content

Commit

Permalink
add example code and test case for gh-591 (#592)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuki YOSHIDA <yoshida@sports-it.jp>
  • Loading branch information
sis-yoshiday and Yuki YOSHIDA authored Nov 13, 2023
1 parent cb2ed37 commit cd7c591
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public void testInMemoryFonts() throws Exception{
assertArrayEquals(expectedOutput,str.toCharArray());
}

@Test
public void testSurrogatePair() throws Exception{

BaseFont baseFont = BaseFont.createFont("fonts/jp/GenShinGothic-Normal.ttf", BaseFont.IDENTITY_H, false);

char[] expectedOutput = {17369};
// http://en.glyphwiki.org/wiki/u20bb7
String text = "\uD842\uDFB7";
byte[] processedContent = FopGlyphProcessor.convertToBytesWithGlyphs(
baseFont, text, "fonts/jp/GenShinGothic-Normal.ttf", new HashMap<>(), "dflt");
String str = new String(processedContent, "UnicodeBigUnmarked");
char[] actual = str.toCharArray();
assertArrayEquals(expectedOutput, actual);
}

private byte[] getFontByte(String fileName) throws IOException {
InputStream stream = BaseFont.getResourceStream(fileName, null);
return IOUtils.toByteArray(stream);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.librepdf.openpdf.examples.fonts.languages;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Japanese {

public static void main(String[] args) throws IOException {
// step 0: prepare font with chinese symbols
BaseFont baseFont = BaseFont.createFont(
Japanese.class.getClassLoader().getResource("fonts/GenShinGothic-Normal.ttf").getFile(),
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(baseFont, 12, Font.NORMAL);

// step 1: Prepare document for japanese text
Document document = new Document();
ByteArrayOutputStream pdfOutput = new ByteArrayOutputStream();
PdfWriter.getInstance(document, pdfOutput);
document.open();

// step 2: we add content to the document
// http://en.glyphwiki.org/wiki/u20bb7
document.add(new Chunk("\uD842\uDFB7", font));

// step 3: we close the document
document.close();

Files.write(Paths.get(Japanese.class.getSimpleName() + ".pdf"), pdfOutput.toByteArray());
}
}
Binary file not shown.

0 comments on commit cd7c591

Please sign in to comment.