]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Improved handling of AFP double-byte character sets
authorMehdi Houshmand <mehdi@apache.org>
Tue, 15 May 2012 09:07:02 +0000 (09:07 +0000)
committerMehdi Houshmand <mehdi@apache.org>
Tue, 15 May 2012 09:07:02 +0000 (09:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1338605 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/afp/fonts/CharacterSet.java
src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java
src/java/org/apache/fop/afp/fonts/CharactersetEncoder.java
src/java/org/apache/fop/afp/fonts/FopCharacterSet.java
src/java/org/apache/fop/afp/ptoca/PtocaProducer.java
src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java
src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java
test/java/org/apache/fop/afp/fonts/CharactersetEncoderTestCase.java

index 341abde0b0dc42abf868f8ccfd92a2c0ccda93ba..fad5e95e65c569ede6b4e11e6e14b594af4d7652 100644 (file)
@@ -94,12 +94,12 @@ public class CharacterSet {
      *
      * @param codePage the code page identifier
      * @param encoding the encoding of the font
-     * @param isEBDCS if this is an EBCDIC double byte character set.
+     * @param charsetType the type of the characterset
      * @param name the character set name
      * @param accessor the resource accessor to load resource with
      * @param eventProducer for handling AFP related events
      */
-    CharacterSet(String codePage, String encoding, boolean isEBDCS, String name,
+    CharacterSet(String codePage, String encoding, CharacterSetType charsetType, String name,
             ResourceAccessor accessor, AFPEventProducer eventProducer) {
         if (name.length() > MAX_NAME_LEN) {
             String msg = "Character set name '" + name + "' must be a maximum of "
@@ -115,7 +115,7 @@ public class CharacterSet {
         }
         this.codePage = codePage;
         this.encoding = encoding;
-        this.encoder = CharactersetEncoder.newInstance(encoding, isEBDCS);
+        this.encoder = CharactersetEncoder.newInstance(encoding, charsetType);
         this.accessor = accessor;
 
         this.characterSetOrientations = new HashMap<String, CharacterSetOrientation>(4);
@@ -306,7 +306,7 @@ public class CharacterSet {
      */
     private CharacterSetOrientation getCharacterSetOrientation() {
         CharacterSetOrientation c
-            = (CharacterSetOrientation) characterSetOrientations.get(currentOrientation);
+            = characterSetOrientations.get(currentOrientation);
         return c;
     }
 
index 4988bb949400ffb25658fa88df7260cd11eb1f1a..7da2d71caeb4678d157832df0a57838444a5dc72 100644 (file)
@@ -190,10 +190,10 @@ public abstract class CharacterSetBuilder {
      * @return CharacterSet object
      * @throws IOException if an I/O error occurs
      */
-    public CharacterSet build(String characterSetName, String codePageName, String encoding,
+    public CharacterSet buildSBCS(String characterSetName, String codePageName, String encoding,
             ResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException {
-        return processFont(characterSetName, codePageName, encoding, false, accessor,
-                eventProducer);
+        return processFont(characterSetName, codePageName, encoding, CharacterSetType.SINGLE_BYTE,
+                accessor, eventProducer);
     }
 
     /**
@@ -204,16 +204,16 @@ public abstract class CharacterSetBuilder {
      * @param characterSetName name of the characterset
      * @param codePageName name of the code page file
      * @param encoding encoding name
-     * @param isEDBCS if this is an EBCDIC double byte character set (DBCS)
+     * @param charsetType the characterset type
      * @param accessor used to load codepage and characterset
      * @param eventProducer for handling AFP related events
      * @return CharacterSet object
      * @throws IOException if an I/O error occurs
      */
     public CharacterSet buildDBCS(String characterSetName, String codePageName, String encoding,
-            boolean isEDBCS, ResourceAccessor accessor, AFPEventProducer eventProducer)
+            CharacterSetType charsetType, ResourceAccessor accessor, AFPEventProducer eventProducer)
             throws IOException {
-        return processFont(characterSetName, codePageName, encoding, isEDBCS, accessor,
+        return processFont(characterSetName, codePageName, encoding, charsetType, accessor,
                 eventProducer);
     }
 
@@ -236,7 +236,7 @@ public abstract class CharacterSetBuilder {
     }
 
     private CharacterSet processFont(String characterSetName, String codePageName, String encoding,
-            boolean isEDBCS, ResourceAccessor accessor, AFPEventProducer eventProducer)
+            CharacterSetType charsetType, ResourceAccessor accessor, AFPEventProducer eventProducer)
             throws IOException {
         // check for cached version of the characterset
         String descriptor = characterSetName + "_" + encoding + "_" + codePageName;
@@ -247,7 +247,7 @@ public abstract class CharacterSetBuilder {
         }
 
         // characterset not in the cache, so recreating
-        characterSet = new CharacterSet(codePageName, encoding, isEDBCS, characterSetName,
+        characterSet = new CharacterSet(codePageName, encoding, charsetType, characterSetName,
                 accessor, eventProducer);
 
         InputStream inputStream = null;
@@ -465,8 +465,7 @@ public abstract class CharacterSetBuilder {
             }
         }
 
-        return (CharacterSetOrientation[]) orientations
-            .toArray(EMPTY_CSO_ARRAY);
+        return orientations.toArray(EMPTY_CSO_ARRAY);
     }
 
     /**
@@ -570,7 +569,7 @@ public abstract class CharacterSetBuilder {
 
                 String gcgiString = new String(gcgid, AFPConstants.EBCIDIC_ENCODING);
 
-                String idx = (String) codepage.get(gcgiString);
+                String idx = codepage.get(gcgiString);
 
                 if (idx != null) {
 
index 6d85c0f5225e8d7f042b14296d73e539c6ce7639..f101bdab47858b87b7152bfaa8201a435a59a8a9 100644 (file)
@@ -82,13 +82,12 @@ public abstract class CharactersetEncoder {
      *
      * @param chars the character sequence
      * @param encoding the encoding type
-     * @param isEDBCS if this encoding represents a double-byte character set
      * @return encoded data
      * @throws CharacterCodingException if encoding fails
      */
-    public static EncodedChars encodeSBCS(CharSequence chars, String encoding, boolean isEDBCS)
+    public static EncodedChars encodeSBCS(CharSequence chars, String encoding)
             throws CharacterCodingException {
-        CharactersetEncoder encoder = newInstance(encoding, isEDBCS);
+        CharactersetEncoder encoder = newInstance(encoding, CharacterSetType.SINGLE_BYTE);
         return encoder.encode(chars);
     }
 
@@ -98,8 +97,8 @@ public abstract class CharactersetEncoder {
      * sequence it will return its EBCDIC code-point, however, the "Shift In - Shift Out" operators
      * are removed from the sequence of bytes. These are only used in Line Data.
      */
-    private static final class EbcdicDoubleByteEncoder extends CharactersetEncoder {
-        private EbcdicDoubleByteEncoder(String encoding) {
+    private static final class EbcdicDoubleByteLineDataEncoder extends CharactersetEncoder {
+        private EbcdicDoubleByteLineDataEncoder(String encoding) {
             super(encoding);
         }
         @Override
@@ -117,13 +116,16 @@ public abstract class CharactersetEncoder {
      * byte character sets (DBCS).
      */
     private static final class DefaultEncoder extends CharactersetEncoder {
-        private DefaultEncoder(String encoding) {
+        private final boolean isDBCS;
+
+        private DefaultEncoder(String encoding, boolean isDBCS) {
             super(encoding);
+            this.isDBCS = isDBCS;
         }
 
         @Override
         EncodedChars getEncodedChars(byte[] byteArray, int length) {
-            return new EncodedChars(byteArray, false);
+            return new EncodedChars(byteArray, isDBCS);
         }
     }
 
@@ -134,17 +136,21 @@ public abstract class CharactersetEncoder {
      * @param isEbcdicDBCS whether or not this wraps a double-byte EBCDIC code page.
      * @return the CharactersetEncoder
      */
-    static CharactersetEncoder newInstance(String encoding, boolean isEbcdicDBCS) {
-        if (isEbcdicDBCS) {
-            return new EbcdicDoubleByteEncoder(encoding);
-        } else {
-            return new DefaultEncoder(encoding);
+    static CharactersetEncoder newInstance(String encoding, CharacterSetType charsetType) {
+        switch (charsetType) {
+        case DOUBLE_BYTE_LINE_DATA:
+            return new EbcdicDoubleByteLineDataEncoder(encoding);
+        case DOUBLE_BYTE:
+            return new DefaultEncoder(encoding, true);
+        default:
+            return new DefaultEncoder(encoding, false);
         }
     }
 
     /**
      * A container for encoded character bytes
      */
+    // CSOFF: FinalClass - disabling "final" modifier so that this class can be mocked
     public static class EncodedChars {
 
         private final byte[] bytes;
index f949976ba2c26561f85254683a3aa1605a08233e..f83c38621c205bc5fce93c382033e6a3b372b7cf 100644 (file)
@@ -42,7 +42,8 @@ public class FopCharacterSet extends CharacterSet {
      */
     public FopCharacterSet(String codePage, String encoding, String name, Typeface charSet,
             AFPEventProducer eventProducer) {
-        super(codePage, encoding, false, name, (ResourceAccessor) null, eventProducer);
+        super(codePage, encoding, CharacterSetType.SINGLE_BYTE, name, (ResourceAccessor) null,
+                eventProducer);
         this.charSet = charSet;
     }
 
@@ -132,5 +133,4 @@ public class FopCharacterSet extends CharacterSet {
     public char mapChar(char c) {
         return charSet.mapChar(c);
     }
-
 }
index 9b6d97deca54f2c125ebd6d002e3aaa8e15eff73..5f29bef9637f8746656b37169d784ad289379c24 100644 (file)
@@ -22,8 +22,8 @@ package org.apache.fop.afp.ptoca;
 import java.io.IOException;
 
 /**
- * Producer interface that is passed to a {@link PresentationTextObject} to produce PTOCA control
- * sequences using a {@link PtocaBuilder}.
+ * Producer interface that is passed to a {@link org.apache.fop.afp.modca.PresentationTextObject}
+ * to produce PTOCA control sequences using a {@link PtocaBuilder}.
  */
 public interface PtocaProducer {
 
index f7ed5a85c76ddd6ec51a8cbdeb3cdbb553aa647a..4af21b12b993df169d48b6fce7ed58eba0c82703 100644 (file)
@@ -56,7 +56,7 @@ public class TextDataInfoProducer implements PtocaProducer, PtocaConstants {
         // Add transparent data
         String textString = textDataInfo.getString();
         String encoding = textDataInfo.getEncoding();
-        builder.addTransparentData(CharactersetEncoder.encodeSBCS(textString, encoding, false));
+        builder.addTransparentData(CharactersetEncoder.encodeSBCS(textString, encoding));
     }
 
 }
index bf7fbde4a4533957f147d7a07fd8f95ca758bc3e..36cdbd077f824d1143265191270b08efb03aa4aa 100644 (file)
@@ -37,6 +37,7 @@ import org.apache.fop.afp.fonts.AFPFontCollection;
 import org.apache.fop.afp.fonts.AFPFontInfo;
 import org.apache.fop.afp.fonts.CharacterSet;
 import org.apache.fop.afp.fonts.CharacterSetBuilder;
+import org.apache.fop.afp.fonts.CharacterSetType;
 import org.apache.fop.afp.fonts.DoubleByteFont;
 import org.apache.fop.afp.fonts.OutlineFont;
 import org.apache.fop.afp.fonts.RasterFont;
@@ -220,7 +221,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                     }
                 } else {
                     font.addCharacterSet(sizeMpt, CharacterSetBuilder.getSingleByteInstance()
-                                .build(characterset, codepage, encoding, accessor, eventProducer));
+                                .buildSBCS(characterset, codepage, encoding, accessor, eventProducer));
                 }
             }
             return font;
@@ -254,7 +255,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                     log.error(msg);
                 }
             } else {
-                characterSet = CharacterSetBuilder.getSingleByteInstance().build(
+                characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS(
                         characterset, codepage, encoding, accessor, eventProducer);
             }
             // Return new font object
@@ -269,10 +270,10 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
             }
             String name = afpFontCfg.getAttribute("name", characterset);
             CharacterSet characterSet = null;
-            boolean ebcdicDBCS = afpFontCfg.getAttributeAsBoolean("ebcdic-dbcs", false);
-
+            CharacterSetType charsetType = afpFontCfg.getAttributeAsBoolean("ebcdic-dbcs", false)
+                    ? CharacterSetType.DOUBLE_BYTE_LINE_DATA : CharacterSetType.DOUBLE_BYTE;
             characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS(characterset,
-                    codepage, encoding, ebcdicDBCS, accessor, eventProducer);
+                    codepage, encoding, charsetType, accessor, eventProducer);
 
             // Create a new font object
             DoubleByteFont font = new DoubleByteFont(name, characterSet);
@@ -322,7 +323,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                 }
                 List<FontTriplet> fontTriplets = afi.getFontTriplets();
                 for (int j = 0; j < fontTriplets.size(); ++j) {
-                    FontTriplet triplet = (FontTriplet) fontTriplets.get(j);
+                    FontTriplet triplet = fontTriplets.get(j);
                     if (log.isDebugEnabled()) {
                         log.debug("  Font triplet "
                                 + triplet.getName() + ", "
index dd776e41c2999c239cf70ebe600a8933b7e76702..6d0daa2bea51f6683dcbe8e6fd6dc3481a935271 100644 (file)
 
 package org.apache.fop.afp.fonts;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.nio.charset.CharacterCodingException;
@@ -30,6 +26,10 @@ import java.nio.charset.CharacterCodingException;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 /**
  * Test {@link CharactersetEncoder}
  */
@@ -39,8 +39,9 @@ public class CharactersetEncoderTestCase {
 
     @Before
     public void setUp() {
-        singlebyteEncoder = CharactersetEncoder.newInstance("cp500", false);
-        doublebyteEncoder = CharactersetEncoder.newInstance("cp937", true);
+        singlebyteEncoder = CharactersetEncoder.newInstance("cp500", CharacterSetType.SINGLE_BYTE);
+        doublebyteEncoder = CharactersetEncoder.newInstance("cp937",
+                CharacterSetType.DOUBLE_BYTE_LINE_DATA);
     }
 
     // This is just an arbitrary CJK string
@@ -95,7 +96,7 @@ public class CharactersetEncoderTestCase {
 
     @Test
     public void testEncode() throws CharacterCodingException, IOException {
-        CharactersetEncoder.EncodedChars encChars;// = doublebyteEncoder.encode(testCJKText);
+        CharactersetEncoder.EncodedChars encChars; // = doublebyteEncoder.encode(testCJKText);
         ByteArrayOutputStream bOut = new ByteArrayOutputStream();
         // JAVA 1.5 has a bug in the JVM in which these err for some reason... JAVA 1.6 no issues
         /*encChars.writeTo(bOut, 0, encChars.getLength());