KickJava   Java API By Example, From Geeks To Geeks.

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


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: PageAttributesConverter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.impl.SimpleLog;
24 import org.apache.fop.datatypes.Length;
25 import org.apache.fop.fo.Constants;
26 import org.apache.fop.fo.expr.NumericOp;
27 import org.apache.fop.fo.pagination.RegionBA;
28 import org.apache.fop.fo.pagination.RegionBody;
29 import org.apache.fop.fo.pagination.SimplePageMaster;
30 import org.apache.fop.fo.properties.CommonMarginBlock;
31 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfAttributes;
32 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfPage;
33
34
35 /** Converts simple-page-master attributes into strings as defined in RtfPage.
36  * @author Christopher Scott, scottc@westinghouse.com
37  * @author Peter Herweg, pherweg@web.de
38  */

39
40 final class PageAttributesConverter {
41
42     private static Log log = new SimpleLog("FOP/RTF");
43
44     /**
45      * Constructor is private, because it's just a utility class.
46      */

47     private PageAttributesConverter() {
48     }
49     
50     /** convert xsl:fo attributes to RTF text attributes */
51     static RtfAttributes convertPageAttributes(SimplePageMaster pagemaster) {
52         FOPRtfAttributes attrib = new FOPRtfAttributes();
53         
54         try {
55             RegionBA before = (RegionBA) pagemaster.getRegion(Constants.FO_REGION_BEFORE);
56             RegionBody body = (RegionBody) pagemaster.getRegion(Constants.FO_REGION_BODY);
57             RegionBA after = (RegionBA) pagemaster.getRegion(Constants.FO_REGION_AFTER);
58             
59             attrib.setTwips(RtfPage.PAGE_WIDTH, pagemaster.getPageWidth());
60             attrib.setTwips(RtfPage.PAGE_HEIGHT, pagemaster.getPageHeight());
61             
62             Object JavaDoc widthRaw = attrib.getValue(RtfPage.PAGE_WIDTH);
63             Object JavaDoc heightRaw = attrib.getValue(RtfPage.PAGE_HEIGHT);
64             if ((widthRaw instanceof Integer JavaDoc) && (heightRaw instanceof Integer JavaDoc)
65                     && ((Integer JavaDoc) widthRaw).intValue() > ((Integer JavaDoc) heightRaw).intValue()) {
66                 attrib.set(RtfPage.LANDSCAPE);
67             }
68
69             Length pageTop = pagemaster.getCommonMarginBlock().marginTop;
70             Length pageBottom = pagemaster.getCommonMarginBlock().marginBottom;
71             Length pageLeft = pagemaster.getCommonMarginBlock().marginLeft;
72             Length pageRight = pagemaster.getCommonMarginBlock().marginRight;
73
74             Length bodyTop = pageTop;
75             Length bodyBottom = pageBottom;
76             Length bodyLeft = pageLeft;
77             Length bodyRight = pageRight;
78
79             if (body != null) {
80                 // Should perhaps be replaced by full reference-area handling.
81
CommonMarginBlock bodyMargin = body.getCommonMarginBlock();
82                 bodyTop = (Length) NumericOp.addition(pageTop, bodyMargin.marginTop);
83                 bodyBottom = (Length) NumericOp.addition(pageBottom, bodyMargin.marginBottom);
84                 bodyLeft = (Length) NumericOp.addition(pageLeft, bodyMargin.marginLeft);
85                 bodyRight = (Length) NumericOp.addition(pageRight, bodyMargin.marginRight);
86             }
87             
88             attrib.setTwips(RtfPage.MARGIN_TOP, bodyTop);
89             attrib.setTwips(RtfPage.MARGIN_BOTTOM, bodyBottom);
90             attrib.setTwips(RtfPage.MARGIN_LEFT, bodyLeft);
91             attrib.setTwips(RtfPage.MARGIN_RIGHT, bodyRight);
92
93             //region-before attributes
94
Length beforeTop = pageTop;
95             if (before != null) {
96                 beforeTop = (Length) NumericOp.addition(pageTop, before.getExtent());
97             }
98             attrib.setTwips(RtfPage.HEADERY, beforeTop);
99
100             //region-after attributes
101
Length afterBottom = pageBottom;
102             if (after != null) {
103                 afterBottom = (Length) NumericOp.addition(pageBottom, after.getExtent());
104             }
105             attrib.setTwips(RtfPage.FOOTERY, beforeTop);
106         } catch (Exception JavaDoc e) {
107             log.error("Exception in convertPageAttributes: "
108                 + e.getMessage() + "- page attributes ignored");
109             attrib = new FOPRtfAttributes();
110         }
111
112         return attrib;
113     }
114 }
115
Popular Tags