* ============================================================================
* 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
* (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 <jtauber@jtauber.com>. For more information on the Apache
* Software Foundation, please see <http://www.apache.org/>.
- */
+ */
package org.apache.fop.datatypes;
import java.io.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()
*/
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;
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;
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
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?
}
}
- 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) {
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);
}
}
* @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
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