KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > TextAttributesConverter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: TextAttributesConverter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf;
21
22 import java.awt.Color JavaDoc;
23
24 //FOP
25
import org.apache.fop.apps.FOPException;
26 import org.apache.fop.datatypes.Length;
27 import org.apache.fop.fo.Constants;
28 import org.apache.fop.fo.FONode;
29 import org.apache.fop.fo.FOText;
30 import org.apache.fop.fo.flow.Block;
31 import org.apache.fop.fo.flow.BlockContainer;
32 import org.apache.fop.fo.flow.Inline;
33 import org.apache.fop.fo.flow.PageNumber;
34 import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
35 import org.apache.fop.fo.properties.CommonFont;
36 import org.apache.fop.fo.properties.CommonMarginBlock;
37 import org.apache.fop.fo.properties.CommonTextDecoration;
38 import org.apache.fop.render.rtf.BorderAttributesConverter;
39 import org.apache.fop.render.rtf.rtflib.rtfdoc.IBorderAttributes;
40 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
41 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfColorTable;
42 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfFontManager;
43 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfText;
44
45 /** Converts FO properties to RtfAttributes
46  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
47  * @author Andreas Putz a.putz@skynamics.com
48  * @author Boris Poudérous boris.pouderous@eads-telecom.com
49  * @author Peter Herweg, pherweg@web.de
50  * @author Normand Massé
51  * @author Chris Scott
52  * @author rmarra
53  */

54 final class TextAttributesConverter {
55     
56     /**
57      * Constructor is private, because it's just a utility class.
58      */

59     private TextAttributesConverter() {
60     }
61     
62     /**
63      * Converts all known text FO properties to RtfAttributes
64      * @param props list of FO properites, which are to be converted
65      */

66     public static RtfAttributes convertAttributes(Block fobj)
67                 throws FOPException {
68         FOPRtfAttributes attrib = new FOPRtfAttributes();
69         attrFont(fobj.getCommonFont(), attrib);
70         attrFontColor(fobj.getColor(), attrib);
71         //attrTextDecoration(fobj.getTextDecoration(), attrib);
72
attrBlockBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib);
73         attrBlockMargin(fobj.getCommonMarginBlock(), attrib);
74         attrBlockTextAlign(fobj.getTextAlign(), attrib);
75         attrBorder(fobj.getCommonBorderPaddingBackground(), attrib, fobj);
76
77         return attrib;
78     }
79
80     /**
81      * Converts all known text FO properties to RtfAttributes
82      * @param props list of FO properites, which are to be converted
83      */

84     public static RtfAttributes convertBlockContainerAttributes(BlockContainer fobj)
85                 throws FOPException {
86         FOPRtfAttributes attrib = new FOPRtfAttributes();
87         attrBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib);
88         attrBlockMargin(fobj.getCommonMarginBlock(), attrib);
89         //attrBlockDimension(fobj, attrib);
90
attrBorder(fobj.getCommonBorderPaddingBackground(), attrib, fobj);
91
92         return attrib;
93     }
94
95     /**
96      * Converts all character related FO properties to RtfAttributes.
97      * @param fobj FObj whose properties are to be converted
98      */

99     public static RtfAttributes convertCharacterAttributes(
100             FOText fobj) throws FOPException {
101
102         FOPRtfAttributes attrib = new FOPRtfAttributes();
103         attrFont(fobj.getCommonFont(), attrib);
104         attrFontColor(fobj.getColor(), attrib);
105         attrTextDecoration(fobj.getTextDecoration(), attrib);
106         attrBaseLineShift(fobj.getBaseLineShift(), attrib);
107         return attrib;
108     }
109     
110     /**
111      * Converts all character related FO properties to RtfAttributes.
112      * @param fobj FObj whose properties are to be converted
113      */

114     public static RtfAttributes convertCharacterAttributes(
115             PageNumber fobj) throws FOPException {
116
117         FOPRtfAttributes attrib = new FOPRtfAttributes();
118         attrFont(fobj.getCommonFont(), attrib);
119         attrTextDecoration(fobj.getTextDecoration(), attrib);
120         attrBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib);
121         return attrib;
122     }
123
124     /**
125      * Converts all character related FO properties to RtfAttributes.
126      * @param fobj FObj whose properties are to be converted
127      */

128     public static RtfAttributes convertCharacterAttributes(
129             Inline fobj) throws FOPException {
130
131         FOPRtfAttributes attrib = new FOPRtfAttributes();
132         attrFont(fobj.getCommonFont(), attrib);
133         attrFontColor(fobj.getColor(), attrib);
134
135         attrBackgroundColor(fobj.getCommonBorderPaddingBackground(), attrib);
136         attrInlineBorder(fobj.getCommonBorderPaddingBackground(), attrib);
137         return attrib;
138     }
139
140     private static void attrFont(CommonFont font, FOPRtfAttributes rtfAttr) {
141         rtfAttr.set(RtfText.ATTR_FONT_FAMILY,
142                 RtfFontManager.getInstance().getFontNumber(font.getFirstFontFamily()));
143         rtfAttr.setHalfPoints(RtfText.ATTR_FONT_SIZE, font.fontSize);
144
145         if (font.fontWeight == Constants.EN_700
146                 || font.fontWeight == Constants.EN_800
147                 || font.fontWeight == Constants.EN_900) {
148             //Everything from 700 and above is declared as bold
149
rtfAttr.set("b", 1);
150         } else {
151             rtfAttr.set("b", 0);
152         }
153         
154         if (font.fontStyle == Constants.EN_ITALIC) {
155             rtfAttr.set(RtfText.ATTR_ITALIC, 1);
156         } else {
157             rtfAttr.set(RtfText.ATTR_ITALIC, 0);
158         }
159
160
161     }
162
163
164     private static void attrFontColor(Color JavaDoc colorType, RtfAttributes rtfAttr) {
165         // Cell background color
166
if (colorType != null) {
167            if (colorType.getAlpha() != 0
168                     || colorType.getRed() != 0
169                     || colorType.getGreen() != 0
170                     || colorType.getBlue() != 0) {
171                 rtfAttr.set(RtfText.ATTR_FONT_COLOR,
172                         convertFOPColorToRTF(colorType));
173             }
174         }
175     }
176
177
178
179     private static void attrTextDecoration(CommonTextDecoration textDecoration,
180                 RtfAttributes rtfAttr) {
181         if (textDecoration == null) {
182             rtfAttr.set(RtfText.ATTR_UNDERLINE, 0);
183             rtfAttr.set(RtfText.ATTR_STRIKETHROUGH, 0);
184             return;
185         }
186                 
187         if (textDecoration.hasUnderline()) {
188             rtfAttr.set(RtfText.ATTR_UNDERLINE, 1);
189         } else {
190             rtfAttr.set(RtfText.ATTR_UNDERLINE, 0);
191         }
192         
193         if (textDecoration.hasLineThrough()) {
194             rtfAttr.set(RtfText.ATTR_STRIKETHROUGH, 1);
195         } else {
196             rtfAttr.set(RtfText.ATTR_STRIKETHROUGH, 0);
197         }
198     }
199
200     private static void attrBlockMargin(CommonMarginBlock cmb, FOPRtfAttributes rtfAttr) {
201         rtfAttr.setTwips(RtfText.SPACE_BEFORE,
202                 cmb.spaceBefore.getOptimum(null).getLength());
203         rtfAttr.setTwips(RtfText.SPACE_AFTER,
204                 cmb.spaceAfter.getOptimum(null).getLength());
205         rtfAttr.setTwips(RtfText.LEFT_INDENT_BODY, cmb.startIndent);
206         rtfAttr.setTwips(RtfText.RIGHT_INDENT_BODY, cmb.endIndent);
207     }
208
209
210     /*
211     private static void attrBlockDimension(FObj fobj, FOPRtfAttributes rtfAttr) {
212         Length ipd = fobj.getProperty(Constants.PR_INLINE_PROGRESSION_DIMENSION)
213                 .getLengthRange().getOptimum().getLength();
214         if (ipd.getEnum() != Constants.EN_AUTO) {
215             rtfAttr.set(RtfText.FRAME_WIDTH, ipd);
216         }
217         Length bpd = fobj.getProperty(Constants.PR_BLOCK_PROGRESSION_DIMENSION)
218                 .getLengthRange().getOptimum().getLength();
219         if (bpd.getEnum() != Constants.EN_AUTO) {
220             rtfAttr.set(RtfText.FRAME_HEIGHT, bpd);
221         }
222     }
223     */

224
225     private static void attrBlockTextAlign(int alignment, RtfAttributes rtfAttr) {
226         String JavaDoc rtfValue = null;
227         switch (alignment) {
228             case Constants.EN_CENTER:
229                 rtfValue = RtfText.ALIGN_CENTER;
230                 break;
231             case Constants.EN_END:
232                 rtfValue = RtfText.ALIGN_RIGHT;
233                 break;
234             case Constants.EN_JUSTIFY:
235                 rtfValue = RtfText.ALIGN_JUSTIFIED;
236                 break;
237             default:
238                 rtfValue = RtfText.ALIGN_LEFT;
239                 break;
240         }
241
242         rtfAttr.set(rtfValue);
243     }
244
245     /**
246      * Reads background-color for block from <code>bpb</code> and writes it to
247      * <code>rtfAttr</code>.
248      */

249     private static void attrBlockBackgroundColor(
250                 CommonBorderPaddingBackground bpb, RtfAttributes rtfAttr) {
251         if (bpb.hasBackground()) {
252             rtfAttr.set(RtfText.SHADING, RtfText.FULL_SHADING);
253             rtfAttr.set(RtfText.SHADING_FRONT_COLOR,
254                     convertFOPColorToRTF(bpb.backgroundColor));
255         }
256     }
257
258     /** Adds border information from <code>bpb</code> to <code>rtrAttr</code>. */
259     private static void attrBorder(CommonBorderPaddingBackground bpb,
260            RtfAttributes rtfAttr, FONode fobj) {
261        if (hasBorder(fobj.getParent())) {
262            attrInlineBorder(bpb, rtfAttr);
263            return;
264        }
265
266        BorderAttributesConverter.makeBorder(bpb,
267                CommonBorderPaddingBackground.BEFORE, rtfAttr,
268                IBorderAttributes.BORDER_TOP);
269        BorderAttributesConverter.makeBorder(bpb,
270                CommonBorderPaddingBackground.AFTER, rtfAttr,
271                IBorderAttributes.BORDER_BOTTOM);
272        BorderAttributesConverter.makeBorder(bpb,
273                CommonBorderPaddingBackground.START, rtfAttr,
274                IBorderAttributes.BORDER_LEFT);
275        BorderAttributesConverter.makeBorder(bpb,
276                CommonBorderPaddingBackground.END, rtfAttr,
277                IBorderAttributes.BORDER_RIGHT);
278     }
279
280     /** @return true, if element <code>node</code> has border. */
281     private static boolean hasBorder(FONode node) {
282         while (node != null) {
283             CommonBorderPaddingBackground commonBorderPaddingBackground = null;
284             if (node instanceof Block) {
285                 Block block = (Block) node;
286                 commonBorderPaddingBackground = block.getCommonBorderPaddingBackground();
287             } else if (node instanceof BlockContainer) {
288                 BlockContainer container = (BlockContainer) node;
289                 commonBorderPaddingBackground = container.getCommonBorderPaddingBackground();
290             }
291
292             if (commonBorderPaddingBackground != null
293                     && commonBorderPaddingBackground.hasBorder()) {
294                 return true;
295             }
296
297             node = node.getParent();
298         }
299         return false;
300     }
301
302     /** Adds inline border information from <code>bpb</code> to <code>rtrAttr</code>. */
303     private static void attrInlineBorder(CommonBorderPaddingBackground bpb,
304             RtfAttributes rtfAttr) {
305         BorderAttributesConverter.makeBorder(bpb,
306                 CommonBorderPaddingBackground.BEFORE, rtfAttr,
307                 IBorderAttributes.BORDER_CHARACTER);
308     }
309
310     /**
311      * Reads background-color from bl and writes it to rtfAttr.
312      *
313      * @param bl the Block object the properties are read from
314      * @param rtfAttr the RtfAttributes object the attributes are written to
315      */

316     private static void attrBackgroundColor(CommonBorderPaddingBackground bpb,
317                 RtfAttributes rtfAttr) {
318         Color JavaDoc fopValue = bpb.backgroundColor;
319         int rtfColor = 0;
320         /* FOP uses a default background color of "transparent", which is
321            actually a transparent black, which is generally not suitable as a
322            default here. Changing FOP's default to "white" causes problems in
323            PDF output, so we will look for the default here & change it to
324            "auto". */

325         if ((fopValue == null)
326                 || ((fopValue.getRed() == 0)
327                 && (fopValue.getGreen() == 0)
328                 && (fopValue.getBlue() == 0)
329                 && (fopValue.getAlpha() == 0))) {
330             return;
331         } else {
332             rtfColor = convertFOPColorToRTF(fopValue);
333         }
334
335         rtfAttr.set(RtfText.ATTR_BACKGROUND_COLOR, rtfColor);
336    }
337     
338    private static void attrBaseLineShift(Length baselineShift, RtfAttributes rtfAttr) {
339        
340        int s = baselineShift.getEnum();
341        
342        if (s == Constants.EN_SUPER) {
343            rtfAttr.set(RtfText.ATTR_SUPERSCRIPT);
344        } else if (s == Constants.EN_SUB) {
345            rtfAttr.set(RtfText.ATTR_SUBSCRIPT);
346        }
347    }
348
349    /**
350     * Converts a FOP ColorType to the integer pointing into the RTF color table
351     * @param fopColor the ColorType object to be converted
352     * @return integer pointing into the RTF color table
353     */

354    public static int convertFOPColorToRTF(Color JavaDoc fopColor) {
355        // TODO: This code is duplicated in FOPRtfAttributesConverter
356
int redComponent = fopColor.getRed();
357        int greenComponent = fopColor.getGreen();
358        int blueComponent = fopColor.getBlue();
359        return RtfColorTable.getInstance().getColorNumber(redComponent,
360                greenComponent, blueComponent).intValue();
361    }
362 }
363
Popular Tags