* Text encoding is now handled by PresentationTextData and character set encoding errors are now handled by AFPEventProducer.
* Moved AFPEventProducer from org.apache.fop.render.afp package to org.apache.fop.afp package.


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@721151 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/fop/render/afp/AFPEventProducer.java b/src/java/org/apache/fop/afp/AFPEventProducer.java
similarity index 86%
rename from src/java/org/apache/fop/render/afp/AFPEventProducer.java
rename to src/java/org/apache/fop/afp/AFPEventProducer.java
index 28d93cf..4979218 100644
--- a/src/java/org/apache/fop/render/afp/AFPEventProducer.java
+++ b/src/java/org/apache/fop/afp/AFPEventProducer.java
@@ -17,7 +17,7 @@
 
 /* $Id$ */
 
-package org.apache.fop.render.afp;
+package org.apache.fop.afp;
 
 import org.apache.fop.events.EventBroadcaster;
 import org.apache.fop.events.EventProducer;
@@ -55,6 +55,7 @@
 
     /**
      * Warn about using default font setup.
+     *
      * @param source the event source
      * @event.severity WARN
      */
@@ -62,10 +63,21 @@
 
     /**
      * Warn about a missing default "any" font configuration.
+     *
      * @param source the event source
      * @param style the font style
      * @param weight the font weight
      * @event.severity WARN
      */
     void warnMissingDefaultFont(Object source, String style, int weight);
+
+    /**
+     * A character set encoding error occurred.
+     *
+     * @param source the event source
+     * @param charSetName the character set name
+     * @param encoding the encoding
+     * @event.severity ERROR
+     */
+    void characterSetEncodingError(Object source, String charSetName, String encoding);
 }
diff --git a/src/java/org/apache/fop/afp/AFPEventProducer.xml b/src/java/org/apache/fop/afp/AFPEventProducer.xml
new file mode 100644
index 0000000..8e6bb54
--- /dev/null
+++ b/src/java/org/apache/fop/afp/AFPEventProducer.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<catalogue xml:lang="en">
+  <message key="org.apache.fop.afp.AFPEventProducer.warnDefaultFontSetup">No AFP fonts configured. Using default setup.</message>
+  <message key="org.apache.fop.afp.AFPEventProducer.warnMissingDefaultFont">No AFP default "any", {style}, {weight} font configured.</message>
+  <message key="org.apache.fop.afp.AFPEventProducer.characterSetEncodingError">An error occurred when attempting to encode character set {charSetName} with encoding scheme {encoding}.</message>
+</catalogue>
diff --git a/src/java/org/apache/fop/afp/AFPTextDataInfo.java b/src/java/org/apache/fop/afp/AFPTextDataInfo.java
index 3e87fc4..8047c92 100644
--- a/src/java/org/apache/fop/afp/AFPTextDataInfo.java
+++ b/src/java/org/apache/fop/afp/AFPTextDataInfo.java
@@ -26,22 +26,33 @@
  */
 public class AFPTextDataInfo {
 
+    /** the text font reference */
     private int fontReference;
 
+    /** the text x coordinate position */
     private int x;
 
+    /** the text y coordinate position */
     private int y;
 
+    /** the text color */
     private Color color;
 
+    /** the text variable space adjustment */
     private int variableSpaceCharacterIncrement;
 
+    /** the text inter character adjustment */
     private int interCharacterAdjustment;
 
-    private byte[] data;
-
+    /** the text orientation */
     private int rotation;
 
+    /** the text encoding */
+    private String textEncoding;
+
+    /** the text string */
+    private String textString;
+
     /**
      * Returns the font reference
      *
@@ -152,24 +163,6 @@
     }
 
     /**
-     * Return the text data
-     *
-     * @return the text data
-     */
-    public byte[] getData() {
-        return data;
-    }
-
-    /**
-     * Sets the text data
-     *
-     * @param data the text orientation
-     */
-    public void setData(byte[] data) {
-        this.data = data;
-    }
-
-    /**
      * Sets the text orientation
      *
      * @param rotation the text rotation
@@ -187,6 +180,42 @@
         return this.rotation;
     }
 
+    /**
+     * Sets the text encoding
+     *
+     * @param textEncoding the text encoding
+     */
+    public void setEncoding(String textEncoding) {
+        this.textEncoding = textEncoding;
+    }
+
+    /**
+     * Returns the text encoding
+     *
+     * @return the text encoding
+     */
+    public String getEncoding() {
+        return this.textEncoding;
+    }
+
+    /**
+     * Sets the text string
+     *
+     * @param textString the text string
+     */
+    public void setString(String textString) {
+        this.textString = textString;
+    }
+
+    /**
+     * Returns the text string
+     *
+     * @return the text string
+     */
+    public String getString() {
+        return this.textString;
+    }
+
     /** {@inheritDoc} */
     public String toString() {
         return "TextDataInfo{fontReference=" + fontReference
@@ -196,7 +225,8 @@
         + ", vsci=" + variableSpaceCharacterIncrement
         + ", ica=" + interCharacterAdjustment
         + ", orientation=" + rotation
-        + ", data=" + data
+        + ", textString=" + textString
+        + ", textEncoding=" + textEncoding
         + "}";
     }
 }
\ No newline at end of file
diff --git a/src/java/org/apache/fop/afp/DataStream.java b/src/java/org/apache/fop/afp/DataStream.java
index 34a7f0f..783c698 100644
--- a/src/java/org/apache/fop/afp/DataStream.java
+++ b/src/java/org/apache/fop/afp/DataStream.java
@@ -23,6 +23,7 @@
 import java.awt.Point;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -364,8 +365,9 @@
      *
      * @param textDataInfo
      *            the afp text data
+     * @throws UnsupportedEncodingException thrown if character encoding is not supported
      */
-    public void createText(AFPTextDataInfo textDataInfo) {
+    public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException {
         int rotation = paintingState.getRotation();
         if (rotation != 0) {
             textDataInfo.setRotation(rotation);
diff --git a/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java b/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java
index f7216eb..bf13622 100644
--- a/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java
+++ b/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java
@@ -22,12 +22,12 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.fop.afp.AFPEventProducer;
 import org.apache.fop.events.EventBroadcaster;
 import org.apache.fop.fonts.Font;
 import org.apache.fop.fonts.FontCollection;
 import org.apache.fop.fonts.FontInfo;
 import org.apache.fop.fonts.FontTriplet;
-import org.apache.fop.render.afp.AFPEventProducer;
 
 /**
  * A base collection of AFP fonts
diff --git a/src/java/org/apache/fop/afp/modca/AbstractPageObject.java b/src/java/org/apache/fop/afp/modca/AbstractPageObject.java
index c7559a8..249de13 100644
--- a/src/java/org/apache/fop/afp/modca/AbstractPageObject.java
+++ b/src/java/org/apache/fop/afp/modca/AbstractPageObject.java
@@ -21,6 +21,7 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.List;
 
 import org.apache.fop.afp.AFPLineDataInfo;
@@ -171,8 +172,9 @@
      *
      * @param textDataInfo
      *            the afp text data
+     * @throws UnsupportedEncodingException thrown if character encoding is not supported
      */
-    public void createText(AFPTextDataInfo textDataInfo) {
+    public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException {
         getPresentationTextObject().createTextData(textDataInfo);
     }
 
diff --git a/src/java/org/apache/fop/afp/modca/PresentationTextData.java b/src/java/org/apache/fop/afp/modca/PresentationTextData.java
index 4bc2b91..645a461 100644
--- a/src/java/org/apache/fop/afp/modca/PresentationTextData.java
+++ b/src/java/org/apache/fop/afp/modca/PresentationTextData.java
@@ -22,6 +22,7 @@
 import java.awt.Color;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.fop.afp.AFPLineDataInfo;
@@ -50,49 +51,31 @@
  */
 public class PresentationTextData extends AbstractAFPObject {
 
-    /**
-     * The maximum size of the presentation text data.
-     */
+    /** the maximum size of the presentation text data.*/
     private static final int MAX_SIZE = 8192;
 
-    /**
-     * The afp data relating to this presentation text data.
-     */
+    /** the AFP data relating to this presentation text data. */
     private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
-    /**
-     * The current x coordinate.
-     */
+    /** the current x coordinate. */
     private int currentX = -1;
 
-    /**
-     * The current y cooridnate
-     */
+    /** the current y cooridnate */
     private int currentY = -1;
 
-    /**
-     * The current font
-     */
+    /** the current font */
     private String currentFont = "";
 
-    /**
-     * The current orientation
-     */
+    /** the current orientation */
     private int currentOrientation = 0;
 
-    /**
-     * The current color
-     */
+    /** the current color */
     private Color currentColor = new Color(0, 0, 0);
 
-    /**
-     * The current variable space increment
-     */
+    /** the current variable space increment */
     private int currentVariableSpaceCharacterIncrement = 0;
 
-    /**
-     * The current inter character adjustment
-     */
+    /** the current inter character adjustment */
     private int currentInterCharacterAdjustment = 0;
 
     /**
@@ -115,9 +98,9 @@
                 0x5A, // Structured field identifier
                 0x00, // Record length byte 1
                 0x00, // Record length byte 2
-                (byte) 0xD3, // PresentationTextData identifier byte 1
-                (byte) 0xEE, // PresentationTextData identifier byte 2
-                (byte) 0x9B, // PresentationTextData identifier byte 3
+                SF_CLASS, // PresentationTextData identifier byte 1
+                Type.DATA, // PresentationTextData identifier byte 2
+                Category.PRESENTATION_TEXT, // PresentationTextData identifier byte 3
                 0x00, // Flag
                 0x00, // Reserved
                 0x00, // Reserved
@@ -270,10 +253,12 @@
      * @param textDataInfo
      *            the afp text data
      * @throws MaximumSizeExceededException
-     *             thrown if the maximum number of text data is exceeded
+     *            thrown if the maximum number of text data is exceeded
+     * @throws UnsupportedEncodingException
+     *            thrown if character encoding is not supported
      */
     public void createTextData(AFPTextDataInfo textDataInfo)
-            throws MaximumSizeExceededException {
+            throws MaximumSizeExceededException, UnsupportedEncodingException {
 
         ByteArrayOutputStream afpdata = new ByteArrayOutputStream();
 
@@ -325,7 +310,9 @@
                 afpdata);
 
         // Add transparent data
-        byte[] data = textDataInfo.getData();
+        String textString = textDataInfo.getString();
+        String encoding = textDataInfo.getEncoding();
+        byte[] data = textString.getBytes(encoding);
         if (data.length <= TRANSPARENT_MAX_SIZE) {
             addTransparentData(data, afpdata);
         } else {
diff --git a/src/java/org/apache/fop/afp/modca/PresentationTextObject.java b/src/java/org/apache/fop/afp/modca/PresentationTextObject.java
index 070a33a..4a8bbbb 100644
--- a/src/java/org/apache/fop/afp/modca/PresentationTextObject.java
+++ b/src/java/org/apache/fop/afp/modca/PresentationTextObject.java
@@ -21,6 +21,7 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.List;
 
 import org.apache.fop.afp.AFPLineDataInfo;
@@ -68,8 +69,9 @@
      *
      * @param textDataInfo
      *            The afp text data
+     * @throws UnsupportedEncodingException thrown if character encoding is not supported
      */
-    public void createTextData(AFPTextDataInfo textDataInfo) {
+    public void createTextData(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException {
         if (currentPresentationTextData == null) {
             startPresentationTextData();
         }
@@ -78,6 +80,9 @@
         } catch (MaximumSizeExceededException msee) {
             endPresentationTextData();
             createTextData(textDataInfo);
+        } catch (UnsupportedEncodingException e) {
+            endPresentationTextData();
+            throw e;
         }
     }
 
diff --git a/src/java/org/apache/fop/render/afp/AFPEventProducer.xml b/src/java/org/apache/fop/render/afp/AFPEventProducer.xml
deleted file mode 100644
index 31ce19a..0000000
--- a/src/java/org/apache/fop/render/afp/AFPEventProducer.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<catalogue xml:lang="en">
-  <message key="org.apache.fop.render.afp.AFPEventProducer.warnDefaultFontSetup">No AFP fonts configured. Using default setup.</message>
-  <message key="org.apache.fop.render.afp.AFPEventProducer.warnMissingDefaultFont">No AFP default "any", {style}, {weight} font configured.</message>
-</catalogue>
diff --git a/src/java/org/apache/fop/render/afp/AFPRenderer.java b/src/java/org/apache/fop/render/afp/AFPRenderer.java
index b73b036..8035a94 100644
--- a/src/java/org/apache/fop/render/afp/AFPRenderer.java
+++ b/src/java/org/apache/fop/render/afp/AFPRenderer.java
@@ -34,8 +34,8 @@
 import java.util.Map;
 
 import org.apache.fop.afp.AFPBorderPainter;
-import org.apache.fop.afp.AFPConstants;
 import org.apache.fop.afp.AFPDataObjectInfo;
+import org.apache.fop.afp.AFPEventProducer;
 import org.apache.fop.afp.AFPPaintingState;
 import org.apache.fop.afp.AFPRectanglePainter;
 import org.apache.fop.afp.AFPResourceManager;
@@ -48,6 +48,7 @@
 import org.apache.fop.afp.fonts.AFPFontAttributes;
 import org.apache.fop.afp.fonts.AFPFontCollection;
 import org.apache.fop.afp.fonts.AFPPageFonts;
+import org.apache.fop.afp.fonts.CharacterSet;
 import org.apache.fop.afp.modca.PageObject;
 import org.apache.fop.apps.FOPException;
 import org.apache.fop.apps.FOUserAgent;
@@ -537,40 +538,34 @@
         Color color = (Color) text.getTrait(Trait.COLOR);
         textDataInfo.setColor(color);
 
-        int variableSpaceCharacterIncrement = font.getWidth(' ', fontSize) / 1000
-            + text.getTextWordSpaceAdjust()
-            + text.getTextLetterSpaceAdjust();
+        int textWordSpaceAdjust = text.getTextWordSpaceAdjust();
+        int textLetterSpaceAdjust = text.getTextLetterSpaceAdjust();
+        int textWidth = font.getWidth(' ', fontSize) / 1000;
+        int variableSpaceCharacterIncrement
+            = textWidth + textWordSpaceAdjust + textLetterSpaceAdjust;
+
         variableSpaceCharacterIncrement
             = Math.round(unitConv.mpt2units(variableSpaceCharacterIncrement));
         textDataInfo.setVariableSpaceCharacterIncrement(variableSpaceCharacterIncrement);
 
         int interCharacterAdjustment
-            = Math.round(unitConv.mpt2units(text.getTextLetterSpaceAdjust()));
+            = Math.round(unitConv.mpt2units(textLetterSpaceAdjust));
         textDataInfo.setInterCharacterAdjustment(interCharacterAdjustment);
 
-        // Try and get the encoding to use for the font
-        String encoding = null;
-        try {
-            encoding = font.getCharacterSet(fontSize).getEncoding();
-        } catch (Throwable ex) {
-            encoding = AFPConstants.EBCIDIC_ENCODING;
-            log.warn("renderText():: Error getting encoding for font '"
-                    + font.getFullName() + "' - using default encoding "
-                    + encoding);
-        }
+        CharacterSet charSet = font.getCharacterSet(fontSize);
+        String encoding = charSet.getEncoding();
+        textDataInfo.setEncoding(encoding);
 
         String textString = text.getText();
-        byte[] data = null;
-        try {
-            data = textString.getBytes(encoding);
-            textDataInfo.setData(data);
-        } catch (UnsupportedEncodingException usee) {
-            log.error("renderText:: Font " + fontAttributes.getFontKey()
-                    + " caused UnsupportedEncodingException");
-            return;
-        }
+        textDataInfo.setString(textString);
 
-        dataStream.createText(textDataInfo);
+        try {
+            dataStream.createText(textDataInfo);
+        } catch (UnsupportedEncodingException e) {
+            AFPEventProducer eventProducer
+                = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster());
+            eventProducer.characterSetEncodingError(this, charSet.getName(), encoding);
+        }
         // word.getOffset() = only height of text itself
         // currentBlockIPPosition: 0 for beginning of line; nonzero
         // where previous line area failed to take up entire allocated space