Author: tilman
Date: Tue May  7 18:02:14 2024
New Revision: 1917555

URL: http://svn.apache.org/viewvc?rev=1917555&view=rev
Log:
PDFBOX-5812: improve exception message + test, as suggested by Toshiaki Ito

Modified:
    
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
    
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestFontEmbedding.java

Modified: 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java?rev=1917555&r1=1917554&r2=1917555&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
 (original)
+++ 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDAbstractContentStream.java
 Tue May  7 18:02:14 2024
@@ -1648,8 +1648,20 @@ abstract class PDAbstractContentStream i
             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);
         }

Modified: 
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestFontEmbedding.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestFontEmbedding.java?rev=1917555&r1=1917554&r2=1917555&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestFontEmbedding.java
 (original)
+++ 
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestFontEmbedding.java
 Tue May  7 18:02:14 2024
@@ -45,6 +45,7 @@ import static org.junit.jupiter.api.Asse
 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;
@@ -631,4 +632,69 @@ class TestFontEmbedding
             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();
+        }
+    }
 }


Reply via email to