You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by jg...@apache.org on 2026/02/20 22:28:49 UTC

(xmlgraphics-fop) branch main updated: FOP-3292 Remove character max index restriction

This is an automated email from the ASF dual-hosted git repository.

jgoncalves pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/xmlgraphics-fop.git


The following commit(s) were added to refs/heads/main by this push:
     new 4eb63043a FOP-3292 Remove character max index restriction
4eb63043a is described below

commit 4eb63043a076867a7878b08fac1c7d3ce70c8da0
Author: Joao Goncalves <an...@gmail.com>
AuthorDate: Fri Jan 30 15:28:22 2026 -0600

    FOP-3292 Remove character max index restriction
---
 .../src/main/java/org/apache/fop/pdf/PDFName.java  | 26 +++++++++++-----------
 .../java/org/apache/fop/pdf/PDFNameTestCase.java   | 18 +++++++++++----
 2 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFName.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFName.java
index 444a8ce3e..f069f54a4 100644
--- a/fop-core/src/main/java/org/apache/fop/pdf/PDFName.java
+++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFName.java
@@ -22,6 +22,7 @@ package org.apache.fop.pdf;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.commons.io.output.CountingOutputStream;
 
@@ -56,14 +57,16 @@ public class PDFName extends PDFObject implements Serializable {
         if (name.startsWith("/")) {
             skipFirst = true;
         }
-        for (int i = (skipFirst ? 1 : 0), c = name.length(); i < c; i++) {
-            char ch = name.charAt(i);
 
-            if (ch < 33 || ch > 126 || ESCAPED_NAME_CHARS.indexOf(ch) >= 0) {
-                sb.append('#');
-                toHex(ch, sb);
+        name = name.substring(skipFirst ? 1 : 0);
+
+        byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
+        for (byte nameByte : nameBytes) {
+            int currentChar = nameByte & 255;
+            if (currentChar < 33 || currentChar > 126 || ESCAPED_NAME_CHARS.indexOf(currentChar) >= 0) {
+                toHex(currentChar, sb);
             } else {
-                sb.append(ch);
+                sb.append((char)nameByte);
             }
         }
         return sb.toString();
@@ -73,13 +76,10 @@ public class PDFName extends PDFObject implements Serializable {
         = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
-    private static void toHex(char ch, StringBuilder sb) {
-        if (ch >= 256) {
-            throw new IllegalArgumentException(
-                    "Only 8-bit characters allowed by this implementation");
-        }
-        sb.append(DIGITS[ch >>> 4 & 0x0F]);
-        sb.append(DIGITS[ch & 0x0F]);
+    private static void toHex(int currentChar, StringBuilder sb) {
+        sb.append('#');
+        sb.append(DIGITS[currentChar >>> 4 & 0x0F]);
+        sb.append(DIGITS[currentChar & 0x0F]);
     }
 
     /** {@inheritDoc} */
diff --git a/fop-core/src/test/java/org/apache/fop/pdf/PDFNameTestCase.java b/fop-core/src/test/java/org/apache/fop/pdf/PDFNameTestCase.java
index af934b804..a8bdb78ec 100644
--- a/fop-core/src/test/java/org/apache/fop/pdf/PDFNameTestCase.java
+++ b/fop-core/src/test/java/org/apache/fop/pdf/PDFNameTestCase.java
@@ -21,6 +21,7 @@ package org.apache.fop.pdf;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -65,6 +66,9 @@ public class PDFNameTestCase extends PDFObjectTestCase {
         assertEquals("/Test", PDFName.escapeName("/Test"));
         // Test with a space in the middle
         assertEquals("/Test#20test", PDFName.escapeName("Test test"));
+        // Test with chars whose index are over 256
+        assertEquals("/BCDEEE+#EF#BC#AD#EF#BC#B3#20#E3#82#B4#E3#82#B7#E3#83#83#E3#82#AF",
+                PDFName.escapeName("BCDEEE+MS ゴシック"));
         // Test that all chars apart from ASCII '!' --> '~' are escaped
         nonEscapedCharactersTests();
         escapedCharactersTests();
@@ -76,11 +80,17 @@ public class PDFNameTestCase extends PDFObjectTestCase {
             str += Integer.toHexString(i & 0x0f).toUpperCase();
             assertEquals("/#" + str, PDFName.escapeName(String.valueOf(i)));
         }
-        for (char i = '~' + 1; i < 256; i++) {
-            String str = Integer.toHexString(i >>> 4 & 0x0f).toUpperCase();
-            str += Integer.toHexString(i & 0x0f).toUpperCase();
-            assertEquals("/#" + str, PDFName.escapeName(String.valueOf(i)));
+        for (char i = 128; i < 256; i++) {
+            byte[] bytes = String.valueOf(i).getBytes(StandardCharsets.UTF_8);
+            StringBuilder str = new StringBuilder("/");
+            for (byte b : bytes) {
+                str.append("#");
+                str.append(Integer.toHexString(b >>> 4 & 0x0f).toUpperCase());
+                str.append(Integer.toHexString(b & 0x0f).toUpperCase());
+            }
+            assertEquals(str.toString(), PDFName.escapeName(String.valueOf(i)));
         }
+
         checkCharacterIsEscaped('#');
         checkCharacterIsEscaped('%');
         checkCharacterIsEscaped('(');


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org