KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > properties > IndentPropertyMaker


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: IndentPropertyMaker.java 427939 2006-08-02 09:28:51Z jeremias $ */
19
20 package org.apache.fop.fo.properties;
21
22 import org.apache.fop.datatypes.Numeric;
23 import org.apache.fop.fo.FONode;
24 import org.apache.fop.fo.FObj;
25 import org.apache.fop.fo.PropertyList;
26 import org.apache.fop.fo.expr.NumericOp;
27 import org.apache.fop.fo.expr.PropertyException;
28
29 /**
30  * This property maker handles the calculations described in 5.3.2 which
31  * involves the sizes of the corresponding margin-* properties and the
32  * padding-* and border-*-width properties.
33  */

34 public class IndentPropertyMaker extends CorrespondingPropertyMaker {
35     /**
36      * The corresponding padding-* propIds
37      */

38     private int[] paddingCorresponding = null;
39
40     /**
41      * The corresponding border-*-width propIds
42      */

43     private int[] borderWidthCorresponding = null;
44     
45     /**
46      * Create a start-indent or end-indent property maker.
47      * @param baseMaker the property maker to use
48      */

49     public IndentPropertyMaker(PropertyMaker baseMaker) {
50         super(baseMaker);
51     }
52
53     /**
54      * Set the corresponding values for the padding-* properties.
55      * @param paddingCorresponding the corresping propids.
56      */

57     public void setPaddingCorresponding(int[] paddingCorresponding) {
58         this.paddingCorresponding = paddingCorresponding;
59     }
60     
61     /**
62      * Set the corresponding values for the border-*-width properties.
63      * @param borderWidthCorresponding the corresping propids.
64      */

65     public void setBorderWidthCorresponding(int[] borderWidthCorresponding) {
66         this.borderWidthCorresponding = borderWidthCorresponding;
67     }
68     
69     /**
70      * Calculate the corresponding value for start-indent and end-indent.
71      * @see CorrespondingPropertyMaker#compute(PropertyList)
72      */

73     public Property compute(PropertyList propertyList) throws PropertyException {
74         if (propertyList.getFObj().getUserAgent()
75                     .isBreakIndentInheritanceOnReferenceAreaBoundary()) {
76             return computeAlternativeRuleset(propertyList);
77         } else {
78             return computeConforming(propertyList);
79         }
80     }
81     
82     /**
83      * Calculate the corresponding value for start-indent and end-indent.
84      * @see CorrespondingPropertyMaker#compute(PropertyList)
85      */

86     public Property computeConforming(PropertyList propertyList) throws PropertyException {
87         PropertyList pList = getWMPropertyList(propertyList);
88         if (pList == null) {
89             return null;
90         }
91         // Calculate the values as described in 5.3.2.
92

93         Numeric padding = getCorresponding(paddingCorresponding, propertyList).getNumeric();
94         Numeric border = getCorresponding(borderWidthCorresponding, propertyList).getNumeric();
95         
96         int marginProp = pList.getWritingMode(lr_tb, rl_tb, tb_rl);
97         // Calculate the absolute margin.
98
if (propertyList.getExplicitOrShorthand(marginProp) == null) {
99             Property indent = propertyList.getExplicit(baseMaker.propId);
100             if (indent == null) {
101                 //Neither indent nor margin is specified, use inherited
102
return null;
103             } else {
104                 //Use explicit indent directly
105
return indent;
106             }
107         } else {
108             //Margin is used
109
Numeric margin = propertyList.get(marginProp).getNumeric();
110             
111             Numeric v = new FixedLength(0);
112             if (!propertyList.getFObj().generatesReferenceAreas()) {
113                 // The inherited_value_of([start|end]-indent)
114
v = NumericOp.addition(v, propertyList.getInherited(baseMaker.propId).getNumeric());
115             }
116             // The corresponding absolute margin-[right|left}.
117
v = NumericOp.addition(v, margin);
118             v = NumericOp.addition(v, padding);
119             v = NumericOp.addition(v, border);
120             return (Property) v;
121         }
122         
123     }
124     
125     private boolean isInherited(PropertyList pList) {
126         if (pList.getFObj().getUserAgent().isBreakIndentInheritanceOnReferenceAreaBoundary()) {
127             FONode nd = pList.getFObj().getParent();
128             return !((nd instanceof FObj) && ((FObj)nd).generatesReferenceAreas());
129         } else {
130             return true;
131         }
132     }
133     
134     /**
135      * Calculate the corresponding value for start-indent and end-indent.
136      * This method calculates indent following an alternative rule set that
137      * tries to mimic many commercial solutions that chose to violate the
138      * XSL specification.
139      * @see CorrespondingPropertyMaker#compute(PropertyList)
140      */

141     public Property computeAlternativeRuleset(PropertyList propertyList) throws PropertyException {
142         PropertyList pList = getWMPropertyList(propertyList);
143         if (pList == null) {
144             return null;
145         }
146
147         // Calculate the values as described in 5.3.2.
148

149         Numeric padding = getCorresponding(paddingCorresponding, propertyList).getNumeric();
150         Numeric border = getCorresponding(borderWidthCorresponding, propertyList).getNumeric();
151         
152         int marginProp = pList.getWritingMode(lr_tb, rl_tb, tb_rl);
153
154         //Determine whether the nearest anscestor indent was specified through
155
//start-indent|end-indent or through a margin property.
156
boolean marginNearest = false;
157         PropertyList pl = propertyList.getParentPropertyList();
158         while (pl != null) {
159             if (pl.getExplicit(baseMaker.propId) != null) {
160                 break;
161             } else if (pl.getExplicitOrShorthand(marginProp) != null) {
162                 marginNearest = true;
163                 break;
164             }
165             pl = pl.getParentPropertyList();
166         }
167         
168         // Calculate the absolute margin.
169
if (propertyList.getExplicitOrShorthand(marginProp) == null) {
170             Property indent = propertyList.getExplicit(baseMaker.propId);
171             if (indent == null) {
172                 //Neither start-indent nor margin is specified, use inherited
173
if (isInherited(propertyList) || !marginNearest) {
174                     return null;
175                 } else {
176                     return new FixedLength(0);
177                 }
178             } else {
179                 return indent;
180             }
181         } else {
182             //Margin is used
183
Numeric margin = propertyList.get(marginProp).getNumeric();
184             
185             Numeric v = new FixedLength(0);
186             if (isInherited(propertyList)) {
187                 // The inherited_value_of([start|end]-indent)
188
v = NumericOp.addition(v, propertyList.getInherited(baseMaker.propId).getNumeric());
189             }
190             // The corresponding absolute margin-[right|left}.
191
v = NumericOp.addition(v, margin);
192             v = NumericOp.addition(v, padding);
193             v = NumericOp.addition(v, border);
194             return (Property) v;
195         }
196     }
197     
198     private Property getCorresponding(int[] corresponding, PropertyList propertyList)
199                 throws PropertyException {
200         PropertyList pList = getWMPropertyList(propertyList);
201         if (pList != null) {
202             int wmcorr = pList.getWritingMode(corresponding[0], corresponding[1], corresponding[2]);
203             return propertyList.get(wmcorr);
204         } else {
205             return null;
206         }
207     }
208 }
209
Popular Tags