From 182812ae39ec54fb863a0109f1cb1d9b59841309 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Wed, 8 Dec 2004 18:52:32 +0000 Subject: [PATCH] Make RTF output human-friendly by inserting line feeds. Adding support for table cell padding. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@198194 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/render/rtf/FOPRtfAttributes.java | 11 +++++ .../render/rtf/TableAttributesConverter.java | 42 ++++++++++++++++++- .../rtf/rtflib/rtfdoc/RtfColorTable.java | 3 ++ .../render/rtf/rtflib/rtfdoc/RtfElement.java | 9 ++++ .../rtf/rtflib/rtfdoc/RtfFontManager.java | 3 ++ .../rtf/rtflib/rtfdoc/RtfGenerator.java | 13 +++--- .../render/rtf/rtflib/rtfdoc/RtfHeader.java | 9 ++-- .../rtf/rtflib/rtfdoc/RtfTableCell.java | 2 + .../render/rtf/rtflib/rtfdoc/RtfTableRow.java | 3 ++ .../render/rtf/rtflib/rtfdoc/RtfTextrun.java | 3 +- 10 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/java/org/apache/fop/render/rtf/FOPRtfAttributes.java b/src/java/org/apache/fop/render/rtf/FOPRtfAttributes.java index 5dea87cf5..1f287c83c 100755 --- a/src/java/org/apache/fop/render/rtf/FOPRtfAttributes.java +++ b/src/java/org/apache/fop/render/rtf/FOPRtfAttributes.java @@ -41,6 +41,17 @@ public class FOPRtfAttributes extends RtfAttributes { return this; } + /** + * Set an attribute using a value in millipoints (internal units in twips) + * @param name name of attribute + * @param value value of attribute (in millipoints) + * @return this (which now contains the new entry) + */ + public RtfAttributes setTwips(String name, int value) { + set(name, value / (1000 / 20)); //Convert millipoints to twips + return this; + } + /** * Set an attribute that has a Length value (internal units in half-points) * @param name name of attribute diff --git a/src/java/org/apache/fop/render/rtf/TableAttributesConverter.java b/src/java/org/apache/fop/render/rtf/TableAttributesConverter.java index 6f2000e02..d16f17119 100644 --- a/src/java/org/apache/fop/render/rtf/TableAttributesConverter.java +++ b/src/java/org/apache/fop/render/rtf/TableAttributesConverter.java @@ -116,9 +116,10 @@ public class TableAttributesConverter { FOPRtfAttributes attrib = new FOPRtfAttributes(); boolean isBorderPresent = false; + CommonBorderPaddingBackground border = fobj.getCommonBorderPaddingBackground(); // Cell background color - ColorType color = fobj.getCommonBorderPaddingBackground().backgroundColor; + ColorType color = border.backgroundColor; if ((color != null) && (color.getAlpha() != 0 || color.getRed() != 0 @@ -127,7 +128,6 @@ public class TableAttributesConverter { attrib.set(ITableAttributes.CELL_COLOR_BACKGROUND, color); } - CommonBorderPaddingBackground border = fobj.getCommonBorderPaddingBackground(); BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.BEFORE, attrib, ITableAttributes.CELL_BORDER_TOP); BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.AFTER, @@ -137,6 +137,44 @@ public class TableAttributesConverter { BorderAttributesConverter.makeBorder(border, CommonBorderPaddingBackground.END, attrib, ITableAttributes.CELL_BORDER_RIGHT); + int padding; + boolean reproduceMSWordBug = true; + //TODO Make this configurable + if (reproduceMSWordBug) { + //MS Word has a bug where padding left and top are exchanged + padding = border.getPaddingStart(false); + if (padding != 0) { + attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_TOP, padding); + attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_TOP, 3 /*=twips*/); + } + padding = border.getPaddingBefore(false); + if (padding != 0) { + attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_LEFT, padding); + attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_LEFT, 3 /*=twips*/); + } + } else { + padding = border.getPaddingStart(false); + if (padding != 0) { + attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_LEFT, padding); + attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_LEFT, 3 /*=twips*/); + } + padding = border.getPaddingBefore(false); + if (padding != 0) { + attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_TOP, padding); + attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_TOP, 3 /*=twips*/); + } + } + padding = border.getPaddingEnd(false); + if (padding != 0) { + attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_RIGHT, padding); + attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_RIGHT, 3 /*=twips*/); + } + padding = border.getPaddingAfter(false); + if (padding != 0) { + attrib.setTwips(ITableAttributes.ATTR_CELL_PADDING_BOTTOM, padding); + attrib.set(ITableAttributes.ATTR_CELL_U_PADDING_BOTTOM, 3 /*=twips*/); + } + int n = fobj.getNumberColumnsSpanned(); // Column spanning : if (n > 1) { diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfColorTable.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfColorTable.java index 4bbaefd2b..ab77a7dd9 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfColorTable.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfColorTable.java @@ -184,6 +184,7 @@ public class RtfColorTable { return; } + header.newLine(); header.writeGroupMark (true); //Don't use writeControlWord, because it appends a blank, //which may confuse Wordpad. @@ -196,11 +197,13 @@ public class RtfColorTable { for (int i = 0; i < len; i++) { int identifier = ((Integer) colorTable.get (i)).intValue (); + header.newLine(); header.write ("\\red" + determineColorLevel (identifier, RED)); header.write ("\\green" + determineColorLevel (identifier, GREEN)); header.write ("\\blue" + determineColorLevel (identifier, BLUE) + ";"); } + header.newLine(); header.writeGroupMark (false); } diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfElement.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfElement.java index 03d5697d3..9d66997ee 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfElement.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfElement.java @@ -89,6 +89,15 @@ public abstract class RtfElement { } } + /** + * Starts a new line in the RTF file being written. This is only to format + * the RTF file itself (for easier debugging), not its content. + * @throws IOException in case of an I/O problem + */ + protected void newLine() throws IOException { + writer.write("\n"); + } + /** * Write an RTF control word to our Writer * @param word RTF control word to write diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfFontManager.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfFontManager.java index e92154f65..bf839b489 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfFontManager.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfFontManager.java @@ -144,6 +144,7 @@ public class RtfFontManager { return; } + header.newLine(); header.writeGroupMark (true); header.writeControlWord ("fonttbl;"); @@ -151,11 +152,13 @@ public class RtfFontManager { for (int i = 0; i < len; i++) { header.writeGroupMark (true); + header.newLine(); header.write ("\\f" + i); header.write (" " + (String) fontTable.elementAt (i)); header.writeGroupMark (false); } + header.newLine(); header.writeGroupMark (false); } diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfGenerator.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfGenerator.java index 91a63f26e..e2ee8b618 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfGenerator.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfGenerator.java @@ -37,12 +37,13 @@ public class RtfGenerator extends RtfElement { * @see org.apache.fop.render.rtf.rtflib.rtfdoc.RtfElement#writeRtfContent() */ protected void writeRtfContent() throws IOException { - parent.writeGroupMark(true); - parent.writeStarControlWord("generator"); - parent.writer.write("Apache FOP "); - parent.writer.write(Fop.getVersion()); - parent.writer.write(";"); - parent.writeGroupMark(false); + newLine(); + writeGroupMark(true); + writeStarControlWord("generator"); + writer.write("Apache FOP "); + writer.write(Fop.getVersion()); + writer.write(";"); + writeGroupMark(false); } /** diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfHeader.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfHeader.java index c63b0a8e3..9a00f4bf1 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfHeader.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfHeader.java @@ -103,9 +103,10 @@ class RtfHeader extends RtfContainer { *write properties for footnote handling */ private void writeFootnoteProperties() throws IOException { - writeControlWord("fet0"); //footnotes, not endnotes - writeControlWord("ftnbj"); //place footnotes at the end of the - //page (should be the default, but - //Word 2000 thinks otherwise) + newLine(); + writeControlWord("fet0"); //footnotes, not endnotes + writeControlWord("ftnbj"); //place footnotes at the end of the + //page (should be the default, but + //Word 2000 thinks otherwise) } } diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableCell.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableCell.java index c1856d7d9..f7a1b93b3 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableCell.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableCell.java @@ -226,6 +226,7 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer, * @return widthOffset + width of this cell */ int writeCellDef(int widthOffset) throws IOException { + newLine(); this.widthOffset = widthOffset; // vertical cell merge codes @@ -268,6 +269,7 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer, writeControlWord("cellx" + xPos); + //TODO Why is this here, right after an alignment command is written (see above)? writeControlWord("ql"); return xPos; diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableRow.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableRow.java index 681b07540..a0777e620 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableRow.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableRow.java @@ -122,6 +122,7 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes { * @throws IOException for I/O problems */ protected void writeRtfPrefix() throws IOException { + newLine(); writeGroupMark(true); } @@ -276,6 +277,8 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes { index++; // Added by Boris POUDEROUS on 2002/07/02 } + newLine(); + // now children can write themselves, we have the correct RTF prefix code super.writeRtfContent(); } diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java index 3ef9dcc0f..ab6761dc4 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java @@ -235,7 +235,7 @@ public class RtfTextrun extends RtfContainer { if (rtfListItem != null) { rtfListItem.getRtfListStyle().writeParagraphPrefix(this); } - + //write all children boolean bPrevPar = false; boolean bFirst = true; @@ -260,6 +260,7 @@ public class RtfTextrun extends RtfContainer { && e == lastParagraphBreak)); if (!bHide) { + newLine(); e.writeRtf(); if (rtfListItem != null && e instanceof RtfParagraphBreak) { -- 2.39.5