KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > inline > AbstractTextArea


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: AbstractTextArea.java 462812 2006-10-11 14:19:28Z spepping $ */
19
20 package org.apache.fop.area.inline;
21
22 /**
23  * Abstract base class for both TextArea and Character.
24  */

25 public abstract class AbstractTextArea extends InlineParent {
26
27     /**
28      * this class stores information about spaces and potential adjustments
29      * that can be used in order to re-compute adjustments when a
30      * page-number or a page-number-citation is resolved
31      */

32     protected class TextAdjustingInfo extends InlineAdjustingInfo {
33
34         /** difference between the optimal width of a space
35          * and the default width of a space according to the font
36          * (this is equivalent to the property word-spacing.optimum)
37          */

38         protected int spaceDifference = 0;
39         
40         /**
41          * Constructor
42          *
43          * @param stretch the available space for stretching
44          * @param shrink the available space for shrinking
45          * @param adj space adjustment type
46          */

47         protected TextAdjustingInfo(int stretch, int shrink, int adj) {
48             super(stretch, shrink, adj);
49         }
50     }
51
52     private int textWordSpaceAdjust = 0;
53     private int textLetterSpaceAdjust = 0;
54     private TextAdjustingInfo textAdjustingInfo = null;
55     private int baselineOffset = 0;
56
57     /**
58      * Default constructor
59      */

60     public AbstractTextArea() {
61     }
62
63     /**
64      * Constructor with extra parameters:
65      * create a TextAdjustingInfo object
66      * @param stretch the available stretch of the text
67      * @param shrink the available shrink of the text
68      * @param adj the current adjustment of the area
69      */

70     public AbstractTextArea(int stretch, int shrink, int adj) {
71         textAdjustingInfo = new TextAdjustingInfo(stretch, shrink, adj);
72     }
73     
74     /**
75      * Get text word space adjust.
76      *
77      * @return the text word space adjustment
78      */

79     public int getTextWordSpaceAdjust() {
80         return textWordSpaceAdjust;
81     }
82
83     /**
84      * Set text word space adjust.
85      *
86      * @param textWordSpaceAdjust the text word space adjustment
87      */

88     public void setTextWordSpaceAdjust(int textWordSpaceAdjust) {
89         this.textWordSpaceAdjust = textWordSpaceAdjust;
90     }
91
92     /**
93      * Get text letter space adjust.
94      *
95      * @return the text letter space adjustment
96      */

97     public int getTextLetterSpaceAdjust() {
98         return textLetterSpaceAdjust;
99     }
100
101     /**
102      * Set text letter space adjust.
103      *
104      * @param textLetterSpaceAdjust the text letter space adjustment
105      */

106     public void setTextLetterSpaceAdjust(int textLetterSpaceAdjust) {
107         this.textLetterSpaceAdjust = textLetterSpaceAdjust;
108     }
109
110     /**
111      * Set the difference between optimal width of a space and
112      * default width of a space according to the font; this part
113      * of the space adjustment is fixed and must not be
114      * multiplied by the variation factor.
115      * @param spaceDiff the space difference
116      */

117     public void setSpaceDifference(int spaceDiff) {
118         textAdjustingInfo.spaceDifference = spaceDiff;
119     }
120
121     /**
122      * recursively apply the variation factor to all descendant areas
123      * @param variationFactor the variation factor that must be applied to adjustments
124      * @param lineStretch the total stretch of the line
125      * @param lineShrink the total shrink of the line
126      * @return true if there is an UnresolvedArea descendant
127      */

128     public boolean applyVariationFactor(double variationFactor,
129                                         int lineStretch, int lineShrink) {
130         if (textAdjustingInfo != null) {
131             // compute the new adjustments:
132
// if the variation factor is negative, it means that before
133
// the ipd variation the line had to stretch and now it has
134
// to shrink (or vice versa);
135
// in this case, if the stretch and shrink are not equally
136
// divided among the inline areas, we must compute a
137
// balancing factor
138
double balancingFactor = 1.0;
139             if (variationFactor < 0) {
140                 if (textWordSpaceAdjust < 0) {
141                     // from a negative adjustment to a positive one
142
balancingFactor
143                         = ((double) textAdjustingInfo.availableStretch / textAdjustingInfo.availableShrink)
144                             * ((double) lineShrink / lineStretch);
145                 } else {
146                     // from a positive adjustment to a negative one
147
balancingFactor
148                         = ((double) textAdjustingInfo.availableShrink / textAdjustingInfo.availableStretch)
149                             * ((double) lineStretch / lineShrink);
150                 }
151             }
152             textWordSpaceAdjust = (int) ((textWordSpaceAdjust - textAdjustingInfo.spaceDifference)
153                     * variationFactor * balancingFactor)
154                     + textAdjustingInfo.spaceDifference;
155             textLetterSpaceAdjust *= variationFactor;
156             // update the ipd of the area
157
int oldAdjustment = textAdjustingInfo.adjustment;
158             textAdjustingInfo.adjustment *= balancingFactor * variationFactor;
159             ipd += textAdjustingInfo.adjustment - oldAdjustment;
160         }
161         return false;
162     }
163
164     /**
165      * Get baseline offset, i.e. the distance from the before edge
166      * of this area to the nominal baseline.
167      *
168      * @return the baseline offset
169      */

170     public int getBaselineOffset() {
171         return baselineOffset;
172     }
173
174     /**
175      * Set the baseline offset.
176      *
177      * @param baselineOffset the baseline offset
178      */

179     public void setBaselineOffset(int baselineOffset) {
180         this.baselineOffset = baselineOffset;
181     }
182 }
183
Popular Tags