From d64831107b61a5cb223cdb18e7a5751cf86cdd9e Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Mon, 10 Jul 2006 14:12:20 +0000 Subject: [PATCH] Escape Unicode characters properly for list-item labels. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@420533 13f79535-47bb-0310-9956-ffa450edef68 --- .../rtf/rtflib/rtfdoc/RtfListStyleText.java | 6 ++-- .../rtf/rtflib/rtfdoc/RtfStringConverter.java | 30 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfListStyleText.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfListStyleText.java index 230f2e7e6..6091d58f8 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfListStyleText.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfListStyleText.java @@ -63,7 +63,8 @@ public class RtfListStyleText extends RtfListStyle { 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); } @@ -103,7 +104,8 @@ public class RtfListStyleText extends RtfListStyle { } } element.writeOneAttributeNS( - RtfListTable.LIST_TEXT_FORM, "\\'" + sCount + text); + RtfListTable.LIST_TEXT_FORM, "\\'" + sCount + + RtfStringConverter.getInstance().escape(text)); element.writeGroupMark(false); element.writeGroupMark(true); diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfStringConverter.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfStringConverter.java index e3861ad5b..abbd8b485 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfStringConverter.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfStringConverter.java @@ -78,7 +78,20 @@ public class RtfStringConverter { if (str == null) { return; } + w.write(escape(str)); + } + /** + * Escapes a String as required by the RTF spec. + * @param str String to be escaped + * @return the escaped string + */ + public String escape(String str) { + if (str == null) { + return null; + } + + StringBuffer sb = new StringBuffer(Math.max(16, str.length())); // 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)); @@ -102,20 +115,21 @@ public class RtfStringConverter { if (replacement != null) { // RTF-escaped char - w.write('\\'); - w.write(replacement); - w.write(' '); + sb.append('\\'); + sb.append(replacement); + sb.append(' '); } else if (c.charValue() > 127) { // write unicode representation - contributed by Michel Jacobson // - w.write("\\u"); - w.write(Integer.toString((int)c.charValue())); - w.write("\\\'3f"); + sb.append("\\u"); + sb.append(Integer.toString((int)c.charValue())); + sb.append("\\\'3f"); } else { // plain char that is understood by RTF natively - w.write(c.charValue()); + sb.append(c.charValue()); } } + return sb.toString(); } - + } -- 2.39.5