xmlgraphics-fop-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas L Delmelle <a_l.delme...@pandora.be>
Subject Re: RTF - list-item-label encoding (was: RTF and table/column widths (moved from fop-users))
Date Fri, 03 Mar 2006 22:31:35 GMT

On Mar 1, 2006, at 19:21, Andreas L Delmelle wrote:

> Maybe we could make sure the Writer is wrapped into a  
> BufferedWriter when creating it in RTFHandler, just to streamline a  
> bit.
>
> I'd drop that static Map anyways. Makes sense if you need the same  
> constant in a hundred different places, but since all remains  
> private to that tiny class here, they could as well be hardcoded.  
> Work directly on the char primitive instead of the Character object  
> as much as possible. (?)

New tiny patch proposal for this below. Nothing broke, and strictly  
RTF-wise the output for list-item-label text now seems more correct  
than before. Unfortunately, I lack extensive tests for this sort of  
thing, so I wouldn't mind a second opinion --especially WRT the  
generated RTF code. I tried to make sure nothing was altered apart  
from the way the characters for those labels are written to the  
output stream, but you never know...

TIA,

Andreas


Index: src/java/org/apache/fop/render/rtf/RTFHandler.java
===================================================================
--- src/java/org/apache/fop/render/rtf/RTFHandler.java  (revision  
382930)
+++ src/java/org/apache/fop/render/rtf/RTFHandler.java  (working copy)
@@ -19,6 +19,7 @@
package org.apache.fop.render.rtf;
// Java
+import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -52,9 +53,9 @@
import org.apache.fop.fo.flow.ListItemLabel;
import org.apache.fop.fo.flow.PageNumber;
import org.apache.fop.fo.flow.Table;
-import org.apache.fop.fo.flow.TableColumn;
import org.apache.fop.fo.flow.TableBody;
import org.apache.fop.fo.flow.TableCell;
+import org.apache.fop.fo.flow.TableColumn;
import org.apache.fop.fo.flow.TableHeader;
import org.apache.fop.fo.flow.TableRow;
import org.apache.fop.fo.pagination.Flow;
@@ -147,7 +148,8 @@
      public void startDocument() throws SAXException {
          // TODO sections should be created
          try {
-            rtfFile = new RtfFile(new OutputStreamWriter(os));
+            rtfFile = new RtfFile(new BufferedWriter(
+                    new OutputStreamWriter(os, "US-ASCII")));
              docArea = rtfFile.startDocumentArea();
          } catch (IOException ioe) {
              // TODO could we throw Exception in all FOEventHandler  
events?
Index: src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/ 
RtfListStyleText.java
===================================================================
--- src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/ 
RtfListStyleText.java      (revision 382930)
+++ src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/ 
RtfListStyleText.java      (working copy)
@@ -63,7 +63,8 @@
          item.writeGroupMark(true);
          //item.writeControlWord("pndec");
          item.writeOneAttribute(RtfListTable.LIST_FONT_TYPE, "2");
-        item.writeControlWord("pntxtb " + text);
+        item.writeControlWord("pntxtb ");
+        RtfStringConverter.getInstance().writeRtfString(item.writer,  
text);
          item.writeGroupMark(false);
      }

@@ -102,8 +103,9 @@
                  sCount = "0" + sCount;
              }
          }
-        element.writeOneAttributeNS(
-                RtfListTable.LIST_TEXT_FORM, "\\'" + sCount + text);
+        element.writeControlWordNS(RtfListTable.LIST_TEXT_FORM + "\ 
\'" + sCount);
+        RtfStringConverter.getInstance().writeRtfString(
+                element.writer, text);
          element.writeGroupMark(false);

          element.writeGroupMark(true);
Index: src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/ 
RtfStringConverter.java
===================================================================
--- src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/ 
RtfStringConverter.java    (revision 382930)
+++ src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/ 
RtfStringConverter.java    (working copy)
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
@@ -26,8 +26,6 @@
package org.apache.fop.render.rtf.rtflib.rtfdoc;
-import java.util.Map;
-import java.util.HashMap;
import java.io.IOException;
import java.io.Writer;
@@ -38,23 +36,6 @@
public class RtfStringConverter {
      private static final RtfStringConverter INSTANCE = new  
RtfStringConverter();
-    private static final Map SPECIAL_CHARS;
-    private static final Character DBLQUOTE = new Character('\"');
-    private static final Character QUOTE = new Character('\'');
-    private static final Character SPACE = new Character(' ');
-
-    /** List of characters to escape with corresponding replacement  
strings */
-    static {
-        SPECIAL_CHARS = new HashMap();
-        SPECIAL_CHARS.put(new Character('\t'), "tab");
-        SPECIAL_CHARS.put(new Character('\n'), "line");
-        SPECIAL_CHARS.put(new Character('\''), "rquote");
-        SPECIAL_CHARS.put(new Character('\"'), "rdblquote");
-        SPECIAL_CHARS.put(new Character('\\'), "\\");
-        SPECIAL_CHARS.put(new Character('{'), "{");
-        SPECIAL_CHARS.put(new Character('}'), "}");
-    }
-
      /** singleton pattern */
      private RtfStringConverter() {
      }
@@ -79,43 +60,47 @@
              return;
          }
+        String replacement;
+        char c, d;
          // TODO: could be made more efficient (binary lookup, etc.)
-        for (int i = 0; i < str.length(); i++) {
-            final Character c = new Character(str.charAt(i));
-            Character d;
-            String replacement;
-            if (i != 0) {
-                d = new Character(str.charAt(i - 1));
-            } else {
-                d = new Character(str.charAt(i));
+        for (int i = -1; ++i < str.length();) {
+            c = str.charAt(i);
+            switch (c) {
+            case '\"':
+            case '\'':
+                d = str.charAt((i == 0) ? i : i - 1);
+                if (d == ' ') {
+                    replacement = (c == '\"') ? "ldblquote" : "lquote";
+                } else {
+                    replacement = (c == '\"') ? "rdblquote" : "rquote";
+                }
+                break;
+            case '\t':
+                replacement = "tab";
+                break;
+            case '\n':
+                replacement = "line";
+                break;
+            case '\\':
+            case '{':
+            case '}':
+                replacement = Character.toString(c);
+                break;
+            default:
+                replacement = null;
              }
-            //This section modified by Chris Scott
-            //add "smart" quote recognition
-            if (c.equals((Object)DBLQUOTE) && d.equals((Object) 
SPACE)) {
-                replacement = "ldblquote";
-            } else if (c.equals((Object)QUOTE) && d.equals((Object) 
SPACE)) {
-                replacement = "lquote";
-            } else {
-                replacement = (String)SPECIAL_CHARS.get(c);
-            }
-
              if (replacement != null) {
                  // RTF-escaped char
-                w.write('\\');
-                w.write(replacement);
-                w.write(' ');
-            } else if (c.charValue() > 127) {
+                w.write('\\' + replacement + ' ');
+            } else if (c > 127) {
                  // write unicode representation - contributed by  
Michel Jacobson
                  // <jacobson@idf.ext.jussieu.fr>
-                w.write("\\u");
-                w.write(Integer.toString((int)c.charValue()));
-                w.write("\\\'3f");
+                w.write("\\u" + (int) c + "\\\'3f");
              } else {
                  // plain char that is understood by RTF natively
-                w.write(c.charValue());
+                w.write(c);
              }
          }
      }
-
}


Mime
View raw message