Skip to content

Commit

Permalink
PDFBOX-5812: improve exception message + test, as suggested by Toshia…
Browse files Browse the repository at this point in the history
…ki Ito

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1917556 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed May 7, 2024
1 parent 83d6b88 commit edc2873
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,20 @@ private List<Integer> applyGSUBRules(GsubWorker gsubWorker, ByteArrayOutputStrea
int glyphId = cmapLookup.getGlyphId(codePoint);
if (glyphId <= 0)
{
throw new IllegalStateException(
"could not find the glyphId for the character: " + codePoint);
String source;
if (Character.isBmpCodePoint(codePoint))
{
source = String.valueOf((char) codePoint);
}
else if (Character.isValidCodePoint(codePoint))
{
source = new String(new int[] {codePoint},0,1);
}
else
{
source = "?";
}
throw new IllegalStateException("could not find the glyphId for the character: " + source);
}
originalGlyphIds.add(glyphId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
Expand Down Expand Up @@ -632,4 +633,69 @@ void testSurrogatePairCharacter() throws IOException
System.err.println("Rendering of " + pdf + " failed or is not identical to expected rendering in " + IN_DIR + " directory");
}
}

@Test
void testSurrogatePairCharacterExceptionIsBmpCodePoint() throws IOException
{
final String message = "あ";

try (PDDocument doc = new PDDocument())
{
PDPage page = new PDPage();
doc.addPage(page);
PDFont font = PDType0Font.load(doc,
this.getClass().getResourceAsStream("/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));

try (PDPageContentStream contents = new PDPageContentStream(doc, page))
{
contents.beginText();
contents.setFont(font, 64);
contents.newLineAtOffset(100, 700);
contents.showText(message);
contents.endText();
}

fail();
}
catch (IllegalStateException e)
{
assertEquals("could not find the glyphId for the character: あ", e.getMessage());
}
catch (Exception e)
{
fail();
}
}

@Test
void testSurrogatePairCharacterExceptionIsValidCodePoint() throws IOException
{
final String message = "𩸽";
try (PDDocument doc = new PDDocument())
{
PDPage page = new PDPage();
doc.addPage(page);
PDFont font = PDType0Font.load(doc,
this.getClass().getResourceAsStream("/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));

try (PDPageContentStream contents = new PDPageContentStream(doc, page))
{
contents.beginText();
contents.setFont(font, 64);
contents.newLineAtOffset(100, 700);
contents.showText(message);
contents.endText();
}

fail();
}
catch (IllegalStateException e)
{
assertEquals("could not find the glyphId for the character: 𩸽" ,e.getMessage());
}
catch (Exception e)
{
fail();
}
}
}

0 comments on commit edc2873

Please sign in to comment.