KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > LineArea


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: LineArea.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.area;
21
22 import org.apache.fop.area.inline.InlineArea;
23 import org.apache.fop.fo.Constants;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * The line area.
31  * This is a line area that contains inline areas.
32  */

33 public class LineArea extends Area {
34
35     /**
36      * this class stores information about line width and potential adjustments
37      * that can be used in order to re-compute adjustement and / or indents when a
38      * page-number or a page-number-citation is resolved
39      */

40     private class LineAdjustingInfo implements Serializable JavaDoc {
41         private int lineAlignment;
42         private int difference;
43         private int availableStretch;
44         private int availableShrink;
45         private double variationFactor;
46         private boolean bAddedToAreaTree;
47         
48         private LineAdjustingInfo(int alignment, int diff,
49                                   int stretch, int shrink) {
50             lineAlignment = alignment;
51             difference = diff;
52             availableStretch = stretch;
53             availableShrink = shrink;
54             variationFactor = 1.0;
55             bAddedToAreaTree = false;
56         }
57     }
58     
59     private LineAdjustingInfo adjustingInfo = null;
60
61     // this class can contain the dominant char styling info
62
// this means that many renderers can optimise a bit
63

64     private List JavaDoc inlineAreas = new ArrayList JavaDoc();
65
66     /**
67      * default constructor:
68      * nothing to do
69      */

70     public LineArea() {
71     }
72
73     /**
74      * constructor with extra parameters:
75      * a new LineAdjustingInfo object is created
76      * @param alignment alignment of this line
77      * @param diff difference between content width and line width
78      * @param stretch the available stretch for any adjustments
79      * @param shrink the available shrink for any adjustments
80      */

81     public LineArea(int alignment, int diff,
82                     int stretch, int shrink) {
83         adjustingInfo = new LineAdjustingInfo(alignment, diff, stretch, shrink);
84     }
85
86     /**
87      * Add a child area to this line area.
88      *
89      * @param childArea the inline child area to add
90      */

91     public void addChildArea(Area childArea) {
92         if (childArea instanceof InlineArea) {
93             addInlineArea((InlineArea)childArea);
94             // set the parent area for the child area
95
((InlineArea) childArea).setParentArea(this);
96         }
97     }
98
99     /**
100      * Add an inline child area to this line area.
101      *
102      * @param area the inline child area to add
103      */

104     public void addInlineArea(InlineArea area) {
105         inlineAreas.add(area);
106     }
107
108     /**
109      * Get the inline child areas of this line area.
110      *
111      * @return the list of inline areas
112      */

113     public List JavaDoc getInlineAreas() {
114         return inlineAreas;
115     }
116
117     /**
118      * Get the start indent of this line area.
119      * The start indent is used for offsetting the start of
120      * the inline areas for alignment or other indents.
121      *
122      * @return the start indent value
123      */

124     public int getStartIndent() {
125         if (hasTrait(Trait.START_INDENT)) {
126             return getTraitAsInteger(Trait.START_INDENT);
127         } else {
128             return 0;
129         }
130     }
131
132     /**
133      * Updates the extents of the line area from its children.
134      */

135     public void updateExtentsFromChildren() {
136         int ipd = 0;
137         int bpd = 0;
138         for (int i = 0, len = inlineAreas.size(); i < len; i++) {
139             ipd = Math.max(ipd, ((InlineArea)inlineAreas.get(i)).getAllocIPD());
140             bpd += ((InlineArea)inlineAreas.get(i)).getAllocBPD();
141         }
142         setIPD(ipd);
143         setBPD(bpd);
144     }
145     
146     /**
147      * receive notification about the ipd variation of a descendant area
148      * and perform the needed adjustment, according to the alignment;
149      * in particular:
150      * <ul>
151      * <li>left-aligned text needs no adjustement;</li>
152      * <li>right-aligned text and centered text are handled locally,
153      * adjusting the indent of this LineArea;</li>
154      * <li>justified text requires a more complex adjustment, as the
155      * variation factor computed on the basis of the total
156      * stretch and shrink of the line must be applied in every
157      * descendant leaf areas (text areas and leader areas).</li>
158      * </ul>
159      * @param ipdVariation the difference between old and new ipd
160      */

161     public void handleIPDVariation(int ipdVariation) {
162         switch (adjustingInfo.lineAlignment) {
163             case Constants.EN_START:
164                 // nothing to do in this case
165
break;
166             case Constants.EN_CENTER:
167                 // re-compute indent
168
addTrait(Trait.START_INDENT, new Integer JavaDoc(getStartIndent() - ipdVariation / 2));
169                 break;
170             case Constants.EN_END:
171                 // re-compute indent
172
addTrait(Trait.START_INDENT, new Integer JavaDoc(getStartIndent() - ipdVariation));
173                 break;
174             case Constants.EN_JUSTIFY:
175                 // compute variation factor
176
adjustingInfo.variationFactor *= (float) (adjustingInfo.difference - ipdVariation)
177                         / adjustingInfo.difference;
178                 adjustingInfo.difference -= ipdVariation;
179                 // if the LineArea has already been added to the area tree,
180
// call finalize(); otherwise, wait for the LineLM to call it
181
if (adjustingInfo.bAddedToAreaTree) {
182                     finalise();
183                 }
184                 break;
185             default:
186                 throw new RuntimeException JavaDoc();
187         }
188     }
189     
190     /**
191      * apply the variation factor to all descendant areas
192      * and destroy the AdjustingInfo object if there are
193      * no UnresolvedAreas left
194      */

195     public void finalise() {
196         if (adjustingInfo.lineAlignment == Constants.EN_JUSTIFY) {
197             // justified line: apply the variation factor
198
boolean bUnresolvedAreasPresent = false;
199             // recursively apply variation factor to descendant areas
200
for (int i = 0, len = inlineAreas.size(); i < len; i++) {
201                 bUnresolvedAreasPresent |= ((InlineArea) inlineAreas.get(i))
202                         .applyVariationFactor(adjustingInfo.variationFactor,
203                                 adjustingInfo.availableStretch,
204                                 adjustingInfo.availableShrink);
205             }
206             if (!bUnresolvedAreasPresent) {
207                 // there are no more UnresolvedAreas:
208
// destroy the AdjustingInfo instance
209
adjustingInfo = null;
210             } else {
211                 // this method will be called again later:
212
// the first time, it is called by the LineLM,
213
// afterwards it must be called by the LineArea itself
214
if (!adjustingInfo.bAddedToAreaTree) {
215                     adjustingInfo.bAddedToAreaTree = true;
216                 }
217                 // reset the variation factor
218
adjustingInfo.variationFactor = 1.0;
219             }
220         } else {
221             // the line is not justified: the ipd variation has already
222
// been handled, modifying the line indent
223
}
224     }
225 }
226
227
Popular Tags