KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > traits > SpaceVal


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: SpaceVal.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.traits;
21
22 import org.apache.fop.datatypes.PercentBaseContext;
23 import org.apache.fop.fo.Constants;
24 import org.apache.fop.fo.properties.Property;
25 import org.apache.fop.fo.properties.SpaceProperty;
26 import org.apache.fop.fonts.Font;
27
28 /**
29  * Store a single Space property value in simplified form, with all
30  * Length values resolved. See section 4.3 in the specs.
31  */

32 public class SpaceVal {
33     
34     private final MinOptMax space;
35     private final boolean bConditional;
36     private final boolean bForcing;
37     private final int iPrecedence; // Numeric only, if forcing, set to 0
38

39     /**
40      * Constructor for SpaceVal objects based on Space objects.
41      * @param spaceprop Space object to use
42      * @param context Percentage evaluation context
43      */

44     public SpaceVal(SpaceProperty spaceprop, PercentBaseContext context) {
45         space = new MinOptMax(spaceprop.getMinimum(context).getLength().getValue(context),
46                               spaceprop.getOptimum(context).getLength().getValue(context),
47                               spaceprop.getMaximum(context).getLength().getValue(context));
48         bConditional =
49                 (spaceprop.getConditionality().getEnum() == Constants.EN_DISCARD);
50         Property precProp = spaceprop.getPrecedence();
51         if (precProp.getNumber() != null) {
52             iPrecedence = precProp.getNumber().intValue();
53             bForcing = false;
54         } else {
55             bForcing = (precProp.getEnum() == Constants.EN_FORCE);
56             iPrecedence = 0;
57         }
58     }
59
60     /**
61      * Constructor for SpaceVal objects based on the full set of properties.
62      * @param space space to use
63      * @param bConditional Conditionality value
64      * @param bForcing Forcing value
65      * @param iPrecedence Precedence value
66      */

67     public SpaceVal(MinOptMax space, boolean bConditional,
68                     boolean bForcing, int iPrecedence) {
69         this.space = space;
70         this.bConditional = bConditional;
71         this.bForcing = bForcing;
72         this.iPrecedence = iPrecedence;
73     }
74
75     static public SpaceVal makeWordSpacing(Property wordSpacing,
76                                            SpaceVal letterSpacing,
77                                            Font fs) {
78         if (wordSpacing.getEnum() == Constants.EN_NORMAL) {
79             // give word spaces the possibility to shrink by a third,
80
// and stretch by a half;
81
int spaceCharIPD = fs.getCharWidth(' ');
82             MinOptMax space = new MinOptMax(-spaceCharIPD / 3, 0, spaceCharIPD / 2);
83             //TODO Adding 2 letter spaces here is not 100% correct. Spaces don't have letter spacing
84
return new SpaceVal(
85                     MinOptMax.add
86                      (space, MinOptMax.multiply(letterSpacing.getSpace(), 2)),
87                      true, true, 0);
88         } else {
89             return new SpaceVal(wordSpacing.getSpace(), null);
90         }
91     }
92
93     static public SpaceVal makeLetterSpacing(Property letterSpacing) {
94         if (letterSpacing.getEnum() == Constants.EN_NORMAL) {
95             // letter spaces are set to zero (or use different values?)
96
return new SpaceVal(new MinOptMax(0), true, true, 0);
97         } else {
98             return new SpaceVal(letterSpacing.getSpace(), null);
99         }
100     }
101
102     /**
103      * Returns the Conditionality value.
104      * @return the Conditionality value
105      */

106     public boolean isConditional() {
107         return bConditional;
108     }
109
110     /**
111      * Returns the Forcing value.
112      * @return the Forcing value
113      */

114     public boolean isForcing() {
115         return bForcing;
116     }
117
118     /**
119      * Returns the Precedence value.
120      * @return the Precedence value
121      */

122     public int getPrecedence() {
123         return iPrecedence;
124     }
125
126     /**
127      * Returns the Space value.
128      * @return the Space value
129      */

130     public MinOptMax getSpace() {
131         return space;
132     }
133
134     public String JavaDoc toString() {
135         return "SpaceVal: " + getSpace().toString();
136     }
137 }
138
139
Popular Tags