KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > inline > BasicScaledBaselineTable


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: BasicScaledBaselineTable.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.fop.datatypes.Length;
25 import org.apache.fop.datatypes.LengthBase;
26 import org.apache.fop.datatypes.SimplePercentBaseContext;
27 import org.apache.fop.fo.Constants;
28
29
30 /**
31  * An implementation of the ScaledBaselineTable interface which calculates
32  * all baselines given the height above and below the dominant baseline.
33  */

34 public class BasicScaledBaselineTable implements ScaledBaselineTable, Constants {
35
36     /** A logger for this class */
37     protected Log log = LogFactory.getLog(BasicScaledBaselineTable.class);
38
39     private int altitude;
40     private int depth;
41     private int xHeight;
42     private int dominantBaselineIdentifier;
43     private int writingMode;
44     private int dominantBaselineOffset;
45     private int beforeEdgeOffset;
46     private int afterEdgeOffset;
47     
48     private static final float HANGING_BASELINE_FACTOR = 0.8f;
49     private static final float MATHEMATICAL_BASELINE_FACTOR = 0.5f;
50
51     /**
52      *
53      * Creates a new instance of BasicScaledBaselineTable for the given
54      * altitude, depth, xHeight, baseline and writingmode.
55      * @param altitude the height of the box or the font ascender
56      * @param depth the font descender or 0
57      * @param xHeight the font xHeight
58      * @param dominantBaselineIdentifier the dominant baseline given as an integer constant
59      * @param writingMode the writing mode given as an integer constant
60      */

61     public BasicScaledBaselineTable(int altitude
62                                     , int depth
63                                     , int xHeight
64                                     , int dominantBaselineIdentifier
65                                     , int writingMode) {
66         this.altitude = altitude;
67         this.depth = depth;
68         this.xHeight = xHeight;
69         this.dominantBaselineIdentifier = dominantBaselineIdentifier;
70         this.writingMode = writingMode;
71         this.dominantBaselineOffset = getBaselineDefaultOffset(this.dominantBaselineIdentifier);
72         this.beforeEdgeOffset = altitude - dominantBaselineOffset;
73         this.afterEdgeOffset = depth - dominantBaselineOffset;
74     }
75     
76     /**
77      * Return the dominant baseline for this baseline table.
78      * @return the dominant baseline
79      */

80     public int getDominantBaselineIdentifier() {
81         return this.dominantBaselineIdentifier;
82     }
83     
84     /**
85      * Return the writing mode for this baseline table.
86      * @return the writing mode
87      */

88     public int getWritingMode() {
89         return this.writingMode;
90     }
91
92     /**
93      * Return the baseline offset measured from the dominant
94      * baseline for the given baseline.
95      * @param baselineIdentifier the baseline identifier
96      * @return the baseline offset
97      */

98     public int getBaseline(int baselineIdentifier) {
99         int offset = 0;
100         if (!isHorizontalWritingMode()) {
101             switch (baselineIdentifier) {
102                 case EN_TOP:
103                 case EN_TEXT_TOP:
104                 case EN_TEXT_BOTTOM:
105                 case EN_BOTTOM:
106                     log.warn("The given baseline is only supported for horizontal"
107                         + " writing modes");
108                     return 0;
109             }
110         }
111         switch (baselineIdentifier) {
112             case EN_TOP: // fall through
113
case EN_BEFORE_EDGE:
114                 offset = beforeEdgeOffset;
115                 break;
116             case EN_TEXT_TOP:
117             case EN_TEXT_BEFORE_EDGE:
118             case EN_HANGING:
119             case EN_CENTRAL:
120             case EN_MIDDLE:
121             case EN_MATHEMATICAL:
122             case EN_ALPHABETIC:
123             case EN_IDEOGRAPHIC:
124             case EN_TEXT_BOTTOM:
125             case EN_TEXT_AFTER_EDGE:
126                 offset = getBaselineDefaultOffset(baselineIdentifier) - dominantBaselineOffset;
127                 break;
128             case EN_BOTTOM: // fall through
129
case EN_AFTER_EDGE:
130                 offset = afterEdgeOffset;
131                 break;
132         }
133         return offset;
134     }
135     
136     private boolean isHorizontalWritingMode() {
137         return writingMode == EN_LR_TB || writingMode == EN_RL_TB;
138     }
139     
140     /**
141      * Return the baseline offset measured from the font's default
142      * baseline for the given baseline.
143      * @param baselineIdentifier the baseline identifier
144      * @return the baseline offset
145      */

146     private int getBaselineDefaultOffset(int baselineIdentifier) {
147         int offset = 0;
148         switch (baselineIdentifier) {
149             case EN_TEXT_BEFORE_EDGE:
150                 offset = altitude;
151                 break;
152             case EN_HANGING:
153                 offset = (int)Math.round(altitude * HANGING_BASELINE_FACTOR);
154                 break;
155             case EN_CENTRAL:
156                 offset = (altitude - depth) / 2 + depth;
157                 break;
158             case EN_MIDDLE:
159                 offset = xHeight / 2;
160                 break;
161             case EN_MATHEMATICAL:
162                 offset = (int)Math.round(altitude * MATHEMATICAL_BASELINE_FACTOR);
163                 break;
164             case EN_ALPHABETIC:
165                 offset = 0;
166                 break;
167             case EN_IDEOGRAPHIC: // Fall through
168
case EN_TEXT_AFTER_EDGE:
169                 offset = depth;
170                 break;
171         }
172         return offset;
173     }
174     
175     /**
176      * @see ScaledBaselineTable#setBeforeAndAfterBaselines(int, int)
177      */

178     public void setBeforeAndAfterBaselines(int beforeBaseline, int afterBaseline) {
179         beforeEdgeOffset = beforeBaseline;
180         afterEdgeOffset = afterBaseline;
181     }
182
183     /**
184      * @see ScaledBaselineTable#deriveScaledBaselineTable(int)
185      */

186     public ScaledBaselineTable deriveScaledBaselineTable(int baselineIdentifier) {
187         BasicScaledBaselineTable bac
188             = new BasicScaledBaselineTable(altitude, depth, xHeight
189                                             , baselineIdentifier, this.writingMode);
190         return bac;
191     }
192
193 }
194
Popular Tags