vmote 2003/07/04 20:23:24 Modified: src/java/org/apache/fop/datatypes ColorType.java src/java/org/apache/fop/rtf/renderer RTFHandler.java src/java/org/apache/fop/rtf/rtflib/rtfdoc RtfColorTable.java RtfParagraph.java Log: Changes to implement background color at the paragraph level in RTF output. Revision Changes Path 1.2 +24 -10 xml-fop/src/java/org/apache/fop/datatypes/ColorType.java Index: ColorType.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/datatypes/ColorType.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ColorType.java 11 Mar 2003 13:05:36 -0000 1.1 +++ ColorType.java 5 Jul 2003 03:23:23 -0000 1.2 @@ -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 * [EMAIL PROTECTED] - * + * * 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 <[EMAIL PROTECTED]>. For more information on the Apache * Software Foundation, please see <http://www.apache.org/>. - */ + */ package org.apache.fop.datatypes; import java.io.Serializable; @@ -230,6 +230,20 @@ */ public float alpha() { 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; } /** 1.9 +45 -3 xml-fop/src/java/org/apache/fop/rtf/renderer/RTFHandler.java Index: RTFHandler.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/rtf/renderer/RTFHandler.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- RTFHandler.java 30 Jun 2003 21:04:06 -0000 1.8 +++ RTFHandler.java 5 Jul 2003 03:23:23 -0000 1.9 @@ -56,6 +56,7 @@ 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.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; @@ -99,6 +101,12 @@ + "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 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 @@ } } - 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 @@ 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); } } 1.6 +4 -4 xml-fop/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfColorTable.java Index: RtfColorTable.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfColorTable.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- RtfColorTable.java 2 Jul 2003 16:13:29 -0000 1.5 +++ RtfColorTable.java 5 Jul 2003 03:23:23 -0000 1.6 @@ -168,9 +168,9 @@ * @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 1.8 +7 -3 xml-fop/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfParagraph.java Index: RtfParagraph.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/rtf/rtflib/rtfdoc/RtfParagraph.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- RtfParagraph.java 3 Jul 2003 18:31:11 -0000 1.7 +++ RtfParagraph.java 5 Jul 2003 03:23:23 -0000 1.8 @@ -149,8 +149,12 @@ 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
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]