Hello!

As Torsten Straube pointed out that would be nice to have a possibility 
to set TXTRenderer output encoding. I like the idea and here is my 
proposed patch. I have added new TXTRenderer option "txt.encoding", 
which could be set either from command line:

fop.bat d:\table.fo -txt d:\table.txt -d -txt.encoding Windows-1251

or from java code using TXTRenderer's setOptions(Hashtable) method.
In the case of unsupported encoding TXTStream escapes back to UTF-8.

-- 
Oleg Tkachenko
Multiconn International, Israel
Index: fop-cvs/xml-fop/src/org/apache/fop/apps/CommandLineOptions.java
===================================================================
RCS file: /home/cvspublic/xml-fop/src/org/apache/fop/apps/CommandLineOptions.java,v
retrieving revision 1.14.2.4
diff -u -r1.14.2.4 CommandLineOptions.java
--- fop-cvs/xml-fop/src/org/apache/fop/apps/CommandLineOptions.java     6 Jul 2002 
16:43:45 -0000       1.14.2.4
+++ fop-cvs/xml-fop/src/org/apache/fop/apps/CommandLineOptions.java     13 Jul 2002 
+20:36:29 -0000
@@ -16,6 +16,7 @@
 import org.apache.fop.configuration.Configuration;
 import org.apache.fop.apps.FOPException;
 import org.apache.fop.messaging.MessageHandler;
+import org.apache.fop.render.txt.TXTRenderer;
 
 // Avalon
 import org.apache.avalon.framework.logger.ConsoleLogger;
@@ -252,6 +253,14 @@
                     outfile = new File(args[i + 1]);
                     i++;
                 }
+             } else if (args[i].equals("-" + TXTRenderer.encodingOptionName)) {
+                if ((i + 1 == args.length)
+                        || (args[i + 1].charAt(0) == '-')) {
+                    throw new FOPException("you must specify text renderer encoding");
+                } else {
+                    rendererOptions.put(TXTRenderer.encodingOptionName, args[i + 1]);
+                    i++;
+                }
             } else {
                 printUsage();
                 return false;
@@ -587,6 +596,8 @@
         case TXT_OUTPUT:
             log.debug("txt");
             log.debug("output file: " + outfile.toString());
+            if (rendererOptions.containsKey(TXTRenderer.encodingOptionName))
+                log.debug("output encoding: " + 
+rendererOptions.get(TXTRenderer.encodingOptionName));
             break;
         case SVG_OUTPUT:
             log.debug("svg");
Index: fop-cvs/xml-fop/src/org/apache/fop/render/txt/TXTRenderer.java
===================================================================
RCS file: /home/cvspublic/xml-fop/src/org/apache/fop/render/txt/TXTRenderer.java,v
retrieving revision 1.12.2.2
diff -u -r1.12.2.2 TXTRenderer.java
--- fop-cvs/xml-fop/src/org/apache/fop/render/txt/TXTRenderer.java      23 Apr 2002 
22:33:40 -0000      1.12.2.2
+++ fop-cvs/xml-fop/src/org/apache/fop/render/txt/TXTRenderer.java      13 Jul 2002 
+20:36:41 -0000
@@ -46,6 +46,7 @@
      * the current stream to add Text commands to
      */
     TXTStream currentStream;
+    public static final String encodingOptionName = "txt.encoding";
 
     private int pageHeight = 7920;
 
@@ -1605,8 +1606,6 @@
         if (debug)
             System.out.println("TXTRenderer.renderPage() page.getHeight() = "
                                + page.getHeight());
-        BodyAreaContainer body;
-        AreaContainer before, after, start, end;
 
         maxX = (int)(textCPI * page.getWidth() / 72000 + 1);
         maxY = (int)(textLPI * page.getHeight() / 72000 + 1);
@@ -1626,29 +1625,11 @@
                                + " yFactor=" + yFactor + " paperHeight="
                                + pageHeight);
 
-        body = page.getBody();
-        before = page.getBefore();
-        after = page.getAfter();
-        start = page.getStart();
-        end = page.getEnd();
-
         this.currentFontName = "";
         this.currentFontSize = 0;
 
         // currentStream.add("BT\n");
-        renderBodyAreaContainer(body);
-
-        if (before != null)
-            renderAreaContainer(before);
-
-        if (after != null)
-            renderAreaContainer(after);
-
-        if (start != null)
-            renderAreaContainer(start);
-
-        if (end != null)
-            renderAreaContainer(end);
+        renderRegions(page);
 
         // Write out the buffers.
         for (int row = 0; row <= maxY; row++) {
@@ -1719,6 +1700,7 @@
     throws IOException {
         log.info("rendering areas to TEXT");
         currentStream = new TXTStream(outputStream);
+        currentStream.setEncoding((String)options.get(encodingOptionName));
         firstPage=true;
     }
 
Index: fop-cvs/xml-fop/src/org/apache/fop/render/txt/TXTStream.java
===================================================================
RCS file: /home/cvspublic/xml-fop/src/org/apache/fop/render/txt/TXTStream.java,v
retrieving revision 1.1
diff -u -r1.1 TXTStream.java
--- fop-cvs/xml-fop/src/org/apache/fop/render/txt/TXTStream.java        31 Jan 2002 
18:14:42 -0000      1.1
+++ fop-cvs/xml-fop/src/org/apache/fop/render/txt/TXTStream.java        13 Jul 2002 
+20:36:42 -0000
@@ -11,6 +11,7 @@
 public class TXTStream {
     OutputStream out = null;
     boolean doOutput = true;
+    private String encoding;
 
     public TXTStream(OutputStream os) {
         out = os;
@@ -21,8 +22,12 @@
             return;
 
         try {
-               byte buff[] = str.getBytes("UTF-8");

-            out.write(buff);
+            try {
+                out.write(str.getBytes(encoding));
+            } catch (UnsupportedEncodingException uee) {
+                encoding = "UTF-8";
+                out.write(str.getBytes(encoding));
+            }
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -32,4 +37,10 @@
         doOutput = doout;
     }
 
+    public void setEncoding(String encoding) {
+        if (encoding != null)
+            this.encoding = encoding;
+        else
+            this.encoding = "UTF-8";
+    }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to