From: William Victor Mote Date: Sat, 5 Jul 2003 03:23:23 +0000 (+0000) Subject: Changes to implement background color at the paragraph level in RTF output. X-Git-Tag: Root_Temp_KnuthStylePageBreaking~1333 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=44d9dd78cc350892ed1f380a36d5ef1a9a4f1441;p=xmlgraphics-fop.git Changes to implement background color at the paragraph level in RTF output. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196608 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/datatypes/ColorType.java b/src/java/org/apache/fop/datatypes/ColorType.java index 7b214be20..b0d04945b 100644 --- a/src/java/org/apache/fop/datatypes/ColorType.java +++ b/src/java/org/apache/fop/datatypes/ColorType.java @@ -3,34 +3,34 @@ * ============================================================================ * The Apache Software License, Version 1.1 * ============================================================================ - * + * * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without modifica- * tion, are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: "This product includes software * developed by the Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. - * + * * 4. The names "FOP" and "Apache Software Foundation" must not be used to * endorse or promote products derived from this software without prior * written permission. For written permission, please contact * apache@apache.org. - * + * * 5. Products derived from this software may not be called "Apache", nor may * "Apache" appear in their name, without prior written permission of the * Apache Software Foundation. - * + * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE @@ -42,12 +42,12 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================ - * + * * This software consists of voluntary contributions made by many individuals * on behalf of the Apache Software Foundation and was originally created by * James Tauber . For more information on the Apache * Software Foundation, please see . - */ + */ package org.apache.fop.datatypes; import java.io.Serializable; @@ -232,6 +232,20 @@ public class ColorType implements Serializable { return this.alpha; } + /** + * @param floatValue value (between 0.0 and 1.0) of color channel + * @return integer equivalent (between 0 and 255) + */ + public static int convertChannelToInteger (float floatValue) { + if (floatValue > 1.0) { + floatValue = 1.0f; + } + if (floatValue < 0) { + floatValue = 0; + } + return (int) floatValue * 255; + } + /** * @see java.lang.Object#toString() */ diff --git a/src/java/org/apache/fop/rtf/renderer/RTFHandler.java b/src/java/org/apache/fop/rtf/renderer/RTFHandler.java index a59e93059..a06521ecd 100644 --- a/src/java/org/apache/fop/rtf/renderer/RTFHandler.java +++ b/src/java/org/apache/fop/rtf/renderer/RTFHandler.java @@ -56,6 +56,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import org.apache.fop.apps.FOPException; +import org.apache.fop.datatypes.ColorType; import org.apache.fop.fo.StructureHandler; import org.apache.fop.fo.flow.Block; import org.apache.fop.fo.flow.ExternalGraphic; @@ -72,6 +73,7 @@ import org.apache.fop.fo.pagination.PageSequence; import org.apache.fop.fo.properties.Constants; import org.apache.fop.layout.FontInfo; import org.apache.fop.rtf.rtflib.rtfdoc.RtfAttributes; +import org.apache.fop.rtf.rtflib.rtfdoc.RtfColorTable; import org.apache.fop.rtf.rtflib.rtfdoc.RtfDocumentArea; import org.apache.fop.rtf.rtflib.rtfdoc.RtfFile; import org.apache.fop.rtf.rtflib.rtfdoc.RtfParagraph; @@ -98,6 +100,12 @@ public class RTFHandler extends StructureHandler { private static final String ALPHA_WARNING = "WARNING: RTF renderer is " + "veryveryalpha at this time, see class org.apache.fop.rtf.renderer.RTFHandler"; + /** + * Tracks current background color. BG color is not reset automatically + * anywhere, so we need a persistent way to see whether it has changed. + */ + private int currentRTFBackgroundColor = -1; + /** * Creates a new RTF structure handler. * @param os OutputStream to write to @@ -182,7 +190,8 @@ public class RTFHandler extends StructureHandler { public void startBlock(Block bl) { try { RtfAttributes rtfAttr = new RtfAttributes(); - rtfAttr.set(mapBlockTextAlign(bl)); + attrBlockTextAlign(bl, rtfAttr); + attrBlockBackgroundColor(bl, rtfAttr); para = sect.newParagraph(rtfAttr); } catch (IOException ioe) { // FIXME could we throw Exception in all StructureHandler events? @@ -397,7 +406,7 @@ public class RTFHandler extends StructureHandler { } } - private static String mapBlockTextAlign(Block bl) { + private void attrBlockTextAlign(Block bl, RtfAttributes rtfAttr) { int fopValue = bl.properties.get("text-align").getEnum(); String rtfValue = null; switch (fopValue) { @@ -418,7 +427,40 @@ public class RTFHandler extends StructureHandler { break; } } - return rtfValue; + rtfAttr.set(rtfValue); + } + + private void attrBlockBackgroundColor(Block bl, RtfAttributes rtfAttr) { + ColorType fopValue = bl.properties.get("background-color").getColorType(); + int rtfColor = 0; + /* FOP uses a default background color of "transparent", which is + actually a transparent black, which is generally not suitable as a + default here. Changing FOP's default to "white" causes problems in + PDF output, so we will look for the default here & change it to + white. */ + if ((fopValue.getRed() == 0) && (fopValue.getGreen() == 0) + && (fopValue.getBlue() == 0) && (fopValue.alpha() == 1)) { + rtfColor = RtfColorTable.getInstance().getColorNumber("white"); + } else { + rtfColor = convertFOPColorToRTF(fopValue); + } + if (rtfColor != currentRTFBackgroundColor) { + rtfAttr.set(RtfText.ATTR_BACKGROUND_COLOR, rtfColor); + currentRTFBackgroundColor = rtfColor; + } + } + + /** + * Converts a FOP ColorType to the integer pointing into the RTF color table + * @param fopColor the ColorType object to be converted + * @return integer pointing into the RTF color table + */ + public static int convertFOPColorToRTF(ColorType fopColor) { + int redComponent = ColorType.convertChannelToInteger (fopColor.getRed()); + int greenComponent = ColorType.convertChannelToInteger (fopColor.getGreen()); + int blueComponent = ColorType.convertChannelToInteger (fopColor.getBlue()); + return RtfColorTable.getInstance().getColorNumber(redComponent, + greenComponent, blueComponent); } } diff --git a/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfColorTable.java b/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfColorTable.java index 97843a592..ed5656a87 100755 --- a/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfColorTable.java +++ b/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfColorTable.java @@ -168,9 +168,9 @@ public class RtfColorTable { * @param name a named color * @return the RTF number of a named color, or null if name not found */ - public Integer getColorNumber (String name) { - return (Integer)namedColors.get(name.toLowerCase()); - } + public int getColorNumber (String name) { + return ((Integer)namedColors.get(name.toLowerCase())).intValue(); + } /** * Gets the number of color in the color table diff --git a/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfParagraph.java b/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfParagraph.java index 664f49c27..9149a27ee 100755 --- a/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfParagraph.java +++ b/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfParagraph.java @@ -149,8 +149,12 @@ implements IRtfTextContainer, IRtfPageBreakContainer, IRtfHyperLinkContainer, writeControlWord("pard"); } - // do not write text attributes here, they are handled - // by RtfText + /* + * Original comment said "do not write text attributes here, they are + * handled by RtfText." However, the text attributes appear to be + * relevant to paragraphs as well. + */ + writeAttributes(attrib, RtfText.ATTR_NAMES); writeAttributes(attrib, PARA_ATTRIBUTES); // Added by Normand Masse // Write alignment attributes after \intbl for cells