Skip to content

Commit

Permalink
PDFBOX-5812: use codepoints instead of chars, as suggested by Toshiak…
Browse files Browse the repository at this point in the history
…i Ito

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1917506 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed May 5, 2024
1 parent a45351a commit 17d5f61
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1636,18 +1636,18 @@ private byte[] encodeForGsub(GsubWorker gsubWorker,

private List<Integer> applyGSUBRules(GsubWorker gsubWorker, ByteArrayOutputStream out, PDType0Font font, String word) throws IOException
{
char[] charArray = word.toCharArray();
List<Integer> originalGlyphIds = new ArrayList<>(charArray.length);
int[] codePointArray = word.codePoints().toArray();
List<Integer> originalGlyphIds = new ArrayList<>(word.codePointCount(0, word.length()));
CmapLookup cmapLookup = font.getCmapLookup();

// convert characters into glyphIds
for (char unicodeChar : charArray)
for (int codePoint : codePointArray)
{
int glyphId = cmapLookup.getGlyphId(unicodeChar);
int glyphId = cmapLookup.getGlyphId(codePoint);
if (glyphId <= 0)
{
throw new IllegalStateException(
"could not find the glyphId for the character: " + unicodeChar);
"could not find the glyphId for the character: " + codePoint);
}
originalGlyphIds.add(glyphId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,49 @@ void testIsEmbeddingPermittedMultipleVersions() throws IOException

// no test for 1111
}

/**
* PDFBOX-5812: Atka Mackerel in Japanese kanji. (surrogate pair)
*/
@Test
void testSurrogatePairCharacter() throws IOException
{
final String message = "𩸽\uD867\uDE3D";
File pdf = new File(OUT_DIR, "PDFBOX-5812.pdf");
File IN_DIR = new File("src/test/resources/org/apache/pdfbox/ttf");

ByteArrayOutputStream baos;
try (PDDocument doc = new PDDocument())
{
PDPage page = new PDPage();
doc.addPage(page);
File ipafont = new File("target/fonts/ipag00303", "ipag.ttf");
PDFont font = PDType0Font.load(doc, ipafont);
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
{
contents.beginText();
contents.setFont(font, 64);
contents.newLineAtOffset(100, 700);
contents.showText(message);
contents.endText();
}

baos = new ByteArrayOutputStream();
doc.save(baos);
doc.save(pdf);
}
try (PDDocument doc = Loader.loadPDF(baos.toByteArray()))
{
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(doc).trim();
assertEquals(message, text);
}

// compare rendering
if (!TestPDFToImage.doTestFile(pdf, IN_DIR.getAbsolutePath(), OUT_DIR.getAbsolutePath()))
{
// don't fail, rendering is different on different systems, result must be viewed manually
System.err.println("Rendering of " + pdf + " failed or is not identical to expected rendering in " + IN_DIR + " directory");
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 17d5f61

Please sign in to comment.